-
Notifications
You must be signed in to change notification settings - Fork 37
/
mongoID.pas
104 lines (88 loc) · 2.67 KB
/
mongoID.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{
TMongoWire: mongoID.pas
Copyright 2010-2014 Stijn Sanders
Made available under terms described in file "LICENSE"
https://github.com/stijnsanders/TMongoWire
}
unit mongoID;
{$D-}
{$L-}
interface
function mongoObjectId:string;
implementation
uses Windows, SysUtils, bsonTools, Registry;
var
mongoObjectID_MachineID,mongoObjectID_Counter:integer;
function mongoObjectId:string;
var
st:TSystemTime;
a,b,c,d:integer;
const
hex:array[0..15] of char='0123456789abcdef';
begin
//juse one way of generating mongoDB objectID's
GetSystemTime(st);
a:=(((Round(EncodeDate(st.wYear,st.wMonth,st.wDay))-UnixDateDelta)*24+st.wHour)*60+st.wMinute)*60+st.wSecond;
b:=mongoObjectID_MachineID; //see initialization
c:=GetCurrentThreadId;//GetCurrentProcessId;
d:=InterlockedIncrement(mongoObjectID_Counter);
Result:=
bsonObjectIDPrefix+
hex[(a shr 28) and $F]+hex[(a shr 24) and $F]+
hex[(a shr 20) and $F]+hex[(a shr 16) and $F]+
hex[(a shr 12) and $F]+hex[(a shr 8) and $F]+
hex[(a shr 4) and $F]+hex[(a ) and $F]+
hex[(b shr 20) and $F]+hex[(b shr 16) and $F]+
hex[(b shr 12) and $F]+hex[(b shr 8) and $F]+
hex[(b shr 4) and $F]+hex[(b ) and $F]+
hex[(c shr 12) and $F]+hex[(c shr 8) and $F]+
hex[(c shr 4) and $F]+hex[(c ) and $F]+
hex[(d shr 20) and $F]+hex[(d shr 16) and $F]+
hex[(d shr 12) and $F]+hex[(d shr 8) and $F]+
hex[(d shr 4) and $F]+hex[(d ) and $F]+
bsonObjectIDSuffix;
end;
procedure InitMongoObjectID;
const
KEY_WOW64_64KEY = $0100;
var
r:TRegistry;
s:string;
i,l:integer;
begin
//render a number out of the host name
r:=TRegistry.Create(KEY_READ or KEY_WOW64_64KEY);
try
r.RootKey:=HKEY_LOCAL_MACHINE;
if r.OpenKey('\Software\Microsoft\Cryptography',false) then
s:=r.ReadString('MachineGuid')
else
s:='';
finally
r.Free;
end;
if s='' then
begin
l:=MAX_PATH;
SetLength(s,l);
if GetComputerName(PChar(s),cardinal(l)) then SetLength(s,l) else
s:=GetEnvironmentVariable('COMPUTERNAME');
mongoObjectID_MachineID:=$10101;
for i:=1 to Length(s) do
case s[i] of
'0'..'9':
mongoObjectID_MachineID:=(mongoObjectID_MachineID*36+
(byte(s[i]) and $0F)) and $FFFFFF;
'A'..'Z','a'..'z':
mongoObjectID_MachineID:=(mongoObjectID_MachineID*36+
(byte(s[i]) and $1F)+9) and $FFFFFF;
//else ignore
end;
end
else
mongoObjectID_MachineID:=StrToInt('$'+Copy(s,1,6));
mongoObjectID_Counter:=GetTickCount;//0?
end;
initialization
InitMongoObjectID;
end.