/
mongoWire.pas
748 lines (677 loc) · 19.5 KB
/
mongoWire.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
{
TMongoWire: mongoWire.pas
Copyright 2010-2017 Stijn Sanders
Made available under terms described in file "LICENSE"
https://github.com/stijnsanders/TMongoWire
v1.2.0
}
unit mongoWire;
{$D-}
{$L-}
interface
uses SysUtils, SyncObjs, Classes, simpleSock, jsonDoc, bsonTools;
type
TMongoWire=class(TObject)
private
FSocket: TTcpSocket;
FData: TMemoryStream;
FNameSpace: WideString;
FWriteLock, FReadLock: TCriticalSection;
FQueue: array of record
RequestID: integer;
Data: TMemoryStream;
end;
FQueueIndex, FRequestIndex: integer;
FWriteConcern: IJSONDocument;
procedure DataCString(const x: WideString);
procedure OpenMsg(OpCode, Flags: integer; const Collection: WideString);
function CloseMsg(Data: TMemoryStream): integer;//RequestID
procedure ReadMsg(RequestID: integer);
public
constructor Create(const NameSpace: Widestring);
destructor Destroy; override;
procedure Open(const ServerName: string = 'localhost';
Port: integer = 27017);
procedure OpenSecure(const ServerName: string = 'localhost';
Port: integer = 27017);
procedure Close;
function Get(
const Collection: WideString;
const QryObj: IJSONDocument;
const ReturnFieldSelector: IJSONDocument = nil
): IJSONDocument;
//Query: see TMongoWireQuery.Create
procedure Update(
const Collection: WideString;
const Selector, Doc: IJSONDocument;
Upsert: boolean = false;
MultiUpdate: boolean = false
);
procedure Insert(
const Collection: WideString;
const Doc: IJSONDocument
); overload;
procedure Insert(
const Collection: WideString;
const Docs: array of IJSONDocument
); overload;
procedure Insert(
const Collection: WideString;
const Docs: IJSONDocArray
); overload;
procedure Delete(
const Collection: WideString;
const Selector: IJSONDocument;
SingleRemove: boolean = false
);
function Ping: Boolean;
procedure EnsureIndex(
const Collection: WideString;
const Index: IJSONDocument;
const Options: IJSONDocument = nil
);
function RunCommand(
const CmdObj: IJSONDocument
): IJSONDocument;
function Count(const Collection: WideString): integer;
function Distinct(const Collection, Key: WideString;
const Query: IJSONDocument=nil): Variant;
function Eval(const Collection, JSFn: WideString;
const Args: array of Variant; NoLock: boolean=false):Variant;
property NameSpace: WideString read FNameSpace write FNameSpace;
property WriteConcern: IJSONDocument read FWriteConcern write FWriteConcern;
end;
TMongoWireQuery=class(TObject)
private
FOwner:TMongoWire;
FData:TMemoryStream;
FNumberToReturn,FNumberToSkip,FPageIndex,FNumberReturned:integer;
FCollection:WideString;
FCursorID:int64;
procedure KillCursor;
procedure ReadResponse(RequestID: integer);
public
constructor Create(MongoWire:TMongoWire);
destructor Destroy; override;
procedure Query(
const Collection:WideString;
const QryObj:IJSONDocument;
const ReturnFieldSelector:IJSONDocument=nil;
Flags:integer=0);
function Next(const Doc:IJSONDocument):boolean;
property NumberToReturn:integer read FNumberToReturn write FNumberToReturn;
property NumberToSkip:integer read FNumberToSkip write FNumberToSkip;//TODO: set?
end;
//TODO: TBSONDocumentsFromVariantArray=class(TBSONDocumentsEnumerator)
EMongoException=class(Exception);
EMongoConnectFailed=class(EMongoException);
EMongoNotConnected=class(EMongoException);
EMongoTransferError=class(EMongoException);
EMongoQueryError=class(EMongoException);
EMongoCommandError=class(EMongoException);
const
mongoWire_QueryFlag_TailableCursor = $0002;
mongoWire_QueryFlag_SlaveOk = $0004;
mongoWire_QueryFlag_OplogReplay = $0008;
mongoWire_QueryFlag_NoCursorTimeout = $0010;
mongoWire_QueryFlag_AwaitData = $0020;
mongoWire_QueryFlag_Exhaust = $0040;
mongoWire_Db_SystemIndexCollection = 'system.indexes';
implementation
uses ActiveX, Variants, WinSock;
const
OP_REPLY = 1;
OP_MSG = 1000;
//OP_UPDATE = 2001;
//OP_INSERT = 2002;
OP_QUERY = 2004;
OP_GET_MORE = 2005;
//OP_DELETE = 2006;
OP_KILL_CURSORS = 2007;
MongoWireStartDataSize=$10000;
type
TMongoWireMsgHeader=packed record
MsgLength:integer;
RequestID:integer;
ResponseTo:integer;
OpCode:integer;
Flags:integer;
//for server response only:
CursorID:int64;
StartingFrom:integer;
NumberReturned:integer;
end;
PMongoWireMsgHeader=^TMongoWireMsgHeader;
{ TMongoWire }
constructor TMongoWire.Create(const NameSpace: WideString);
begin
inherited Create;
FNameSpace:=NameSpace;
FSocket:=nil;
FData:=TMemoryStream.Create;
FData.Size:=MongoWireStartDataSize;//start keeping some data
FWriteLock:=TCriticalSection.Create;
FReadLock:=TCriticalSection.Create;
FRequestIndex:=1;
FWriteConcern:=nil;//:=BSON(['w',1]);
end;
destructor TMongoWire.Destroy;
begin
if FSocket<>nil then FSocket.Free;
FData.Free;
FWriteLock.Free;
FReadLock.Free;
FWriteConcern:=nil;
inherited;
end;
procedure TMongoWire.Open(const ServerName: string; Port: integer);
begin
if FSocket<>nil then
begin
FSocket.Disconnect;
FSocket.Free;
end;
FSocket:=TTcpSocket.Create;
FSocket.Connect(ServerName,Port);
if not FSocket.Connected then
raise EMongoConnectFailed.Create(
'MongoWire: failed to connect to "'+ServerName+':'+IntToStr(Port)+'"');
end;
procedure TMongoWire.OpenSecure(const ServerName: string; Port: integer);
begin
if FSocket<>nil then
begin
FSocket.Disconnect;
FSocket.Free;
end;
FSocket:=TTcpSecureSocket.Create;
FSocket.Connect(ServerName,Port);
if not FSocket.Connected then
raise EMongoConnectFailed.Create(
'MongoWire: failed to connect to "'+ServerName+':'+IntToStr(Port)+'"');
end;
procedure TMongoWire.Close;
begin
if FSocket<>nil then
FSocket.Disconnect;
end;
procedure TMongoWire.DataCString(const x:WideString);
var
s:UTF8String;
l:integer;
begin
s:=UTF8Encode(x);
l:=Length(s);
if l=0 then
FData.Write(l,1)
else
FData.Write(s[1],l+1);
end;
procedure TMongoWire.OpenMsg(OpCode,Flags:integer;const Collection:WideString);
var
p:PMongoWireMsgHeader;
begin
//assert caller did FQueueLock !!!
if (FSocket=nil) or not(FSocket.Connected) then
raise EMongoNotConnected.Create('MongoWire: not connected');
//message header
p:=FData.Memory;
//p.RequestID:=//see CloseMsg (within lock)
p.ResponseTo:=0;
p.OpCode:=OpCode;
p.Flags:=Flags;
FData.Position:=20;//SizeOf first part of TMongoWireMsgHeader
if OpCode<>OP_KILL_CURSORS then
DataCString(FNameSpace+'.'+Collection);
end;
function TMongoWire.CloseMsg(Data: TMemoryStream): integer;
var
i,r,l:integer;
p:PMongoWireMsgHeader;
begin
///ATTENTION: please resist the temptation to put CloseMsg in a try/finally:
/// this would cause incomplete requests to be sent to the server!
//assert caller did FQueueLock !!!
//get next RequestID
r:=FRequestIndex;
inc(FRequestIndex);
if FRequestIndex<0 then FRequestIndex:=1;
//add to queue
if Data<>nil then
begin
i:=0;
l:=Length(FQueue);
while (i<FQueueIndex) and (FQueue[i].RequestID<>0) do inc(i);
if i=FQueueIndex then
begin
if l=FQueueIndex then SetLength(FQueue,l+4);//grow in steps
inc(FQueueIndex);
end;
FQueue[i].RequestID:=r;
FQueue[i].Data:=Data;
end;
//fill in MsgLength, RequestID
i:=FData.Position;
p:=FData.Memory;
p.RequestID:=r;
p.MsgLength:=i;
//send data
FSocket.SendBuf(FData.Memory^,i);
Result:=r;
end;
procedure TMongoWire.ReadMsg(RequestID:integer);
const
dSize=$10000;
var
i,l:integer;
h:array[0..2] of integer;
d:array[0..dSize-1] of byte;
dx:TMemoryStream;
begin
//TODO: timeout?
//TODO: retries?
FReadLock.Enter;
try
//is the request already in?
i:=0;
while (i<FQueueIndex) and (FQueue[i].RequestID<>RequestID) do inc(i);
if i=FQueueIndex then
raise EMongoException.Create('MongoWire: ReadMsg called on unknown requestID');
if FQueue[i].Data=nil then
begin
//yes, it's in, release this spot on the queue
//FQueueLock? assert requestID is unique!
FQueue[i].RequestID:=0;
end
else
begin
//nope, read response(s)
repeat
//MsgLength,RequestID,ResponseTo
i:=FSocket.ReceiveBuf(h[0],12);
if i<>12 then raise EMongoException.Create('MongoWire: invalid response');
//find request on queue
i:=0;
while (i<FQueueIndex) and (FQueue[i].RequestID<>h[2]) do inc(i);
if i=FQueueIndex then
begin
//odd, not on queue, read to flush,
l:=h[0]-12;
while (l>0) and (i<>0) do
begin
if l<dSize then i:=l else i:=dSize;
i:=FSocket.ReceiveBuf(d[0],i);
dec(l,i);
end;
//then raise
raise EMongoTransferError.Create('MongoWire: unexpected response, requestID:'+IntToStr(h[2]));
end
else
begin
//queue found, flag done
//FQueueLock? assert requestID is unique!
dx:=FQueue[i].Data;
//release spot on queue
FQueue[i].Data:=nil;
FQueue[i].RequestID:=0;
//forward start of header
dx.Position:=0;
dx.Write(h[0],12);
//TODO: check p.OpCode=OP_REPLY!!!
//read data
dx.Position:=12;
l:=h[0]-12;
while l>0 do
begin
if l<dSize then i:=l else i:=dSize;
i:=FSocket.ReceiveBuf(d[0],i);
if i=0 then raise EMongoException.Create('MongoWire: response aborted');
dx.Write(d[0],i);
dec(l,i);
end;
//set position after message header
if h[0]<36 then dx.Position:=h[0] else dx.Position:=36;
end;
until h[2]=RequestID;
//request is in!
end;
finally
FReadLock.Leave;
end;
end;
function TMongoWire.Get(const Collection: WideString; const QryObj,
ReturnFieldSelector: IJSONDocument): IJSONDocument;
var
i:integer;
p:PMongoWireMsgHeader;
begin
FWriteLock.Enter;
try
OpenMsg(OP_QUERY,0,Collection);
i:=0;
FData.Write(i,4);//NumberToSkip
i:=1;//-1;
FData.Write(i,4);//NumberToReturn
if QryObj=nil then
begin
i:=5;//empty document
FData.Write(i,4);
i:=0;//terminator
FData.Write(i,1);
end
else
SaveBSON(QryObj,FData);
if ReturnFieldSelector<>nil then
SaveBSON(ReturnFieldSelector,FData);
ReadMsg(CloseMsg(FData));
p:=FData.Memory;
if (p.Flags and $0001)<>0 then
raise EMongoQueryError.Create('MongoWire.Get: cursor not found');
//CursorID//assert 0
//StartingFrom//assert 0
if p.NumberReturned=0 then
raise EMongoQueryError.Create('MongoWire.Get: no documents returned');
//Result:=nil?
Result:=JSON;
LoadBSON(FData,Result);
if (p.Flags and $0002)<>0 then
raise EMongoQueryError.Create('MongoWire.Get: '+VarToStr(Result.Item['$err']));
finally
FWriteLock.Leave;
end;
end;
procedure TMongoWire.Update(const Collection: WideString; const Selector,
Doc: IJSONDocument; Upsert, MultiUpdate: boolean);
var
d:IJSONDocument;
begin
if Doc=nil then
raise EMongoException.Create('MongoWire.Update: Doc required');
d:=JSON(
['q',Selector
,'u',Doc
]);
if Upsert then d['upsert']:=true;
if MultiUpdate then d['multi']:=true;
RunCommand(JSON(
['update',Collection
,'updates',ja([d])
,'writeConcern',FWriteConcern
]));
end;
procedure TMongoWire.Insert(const Collection: WideString;
const Doc: IJSONDocument);
begin
if Doc=nil then
raise EMongoException.Create('MongoWire.Insert: Doc required');
RunCommand(JSON(
['insert',Collection
,'documents',ja([Doc])
,'writeConcern',FWriteConcern
]));
end;
procedure TMongoWire.Insert(const Collection: WideString;
const Docs: array of IJSONDocument);
var
i,l:integer;
v:Variant;
begin
l:=Length(Docs);
v:=VarArrayCreate([0,l-1],varUnknown);
for i:=0 to l-1 do v[i]:=Docs[i];
RunCommand(JSON(
['insert',Collection
,'documents',v
,'writeConcern',FWriteConcern
]));
end;
procedure TMongoWire.Insert(const Collection: WideString;
const Docs: IJSONDocArray);
var
d,dx:IJSONDocument;
i:integer;
begin
d:=JSON;
dx:=JSON(
['insert',Collection
,'documents',d
,'writeConcern',FWriteConcern
]);
for i:=0 to Docs.Count-1 do
begin
Docs.LoadItem(i,d);
RunCommand(dx);
end;
end;
procedure TMongoWire.Delete(const Collection: WideString;
const Selector: IJSONDocument; SingleRemove: boolean);
var
l:integer;
begin
if SingleRemove then l:=1 else l:=0;
RunCommand(JSON(
['delete',Collection
,'deletes',ja([JSON(
['q',Selector
,'limit',l
])])
,'writeConcern',FWriteConcern
]));
end;
function TMongoWire.Ping: Boolean;
var
ns:WideString;
begin
ns:=FNameSpace;
try
FNameSpace:='admin';
Result := Get('$cmd', JSON(['ping', 1]))['ok'] = 1;
except
Result := False;
end;
FNameSpace:=ns;
end;
procedure TMongoWire.EnsureIndex(const Collection: WideString;
const Index, Options: IJSONDocument);
var
Document: IJSONDocument;
Name: String;
I: Integer;
IndexArray: Variant;
begin
Document := JSON([
'ns', FNameSpace + '.' + Collection,
'key', Index
]);
if (Options = nil) or (Options['name'] = Null) then begin
Name := '';
IndexArray := Index.ToVarArray;
for I := VarArrayLowBound(IndexArray, 1) to VarArrayHighBound(IndexArray, 1) do
Name := Name + VarToStr(VarArrayGet(IndexArray, [I, 0])) + '_' + VarToStr(VarArrayGet(IndexArray, [I, 1]));
if Length(FNameSpace + '.' + Collection + '.' + Name) > 128 then
raise Exception.Create('Index name too long, please specify a custom name using the name option.');
Document['name'] := Name;
end else
Document['name'] := Options['name'];
if Options <> nil then begin
if Options['background'] <> Null then
Document['background'] := Options['background'];
if Options['dropDups'] <> Null then
Document['dropDups'] := Options['dropDups'];
if Options['sparse'] <> Null then
Document['sparse'] := Options['sparse'];
if Options['unique'] <> Null then
Document['unique'] := Options['unique'];
if Options['v'] <> Null then
Document['v'] := Options['v'];
end;
Insert(mongoWire_Db_SystemIndexCollection, Document);
end;
function TMongoWire.RunCommand(const CmdObj: IJSONDocument): IJSONDocument;
begin
Result:=Get('$cmd',CmdObj);
if Result['ok']<>1 then
try
if VarIsNull(Result['errmsg']) then
raise EMongoCommandError.Create('Unspecified error with "'+
VarToStr(CmdObj.ToVarArray[0,0])+'"')
else
raise EMongoCommandError.Create('Command "'+
VarToStr(CmdObj.ToVarArray[0,0])+'" failed: '+
VarToStr(Result['errmsg']));
except
on EMongoCommandError do
raise;
on Exception do
raise EMongoCommandError.Create('Command failed');
end;
end;
function TMongoWire.Count(const Collection: WideString): integer;
begin
Result:=RunCommand(JSON(['count',Collection]))['n'];
end;
function TMongoWire.Distinct(const Collection, Key: WideString;
const Query: IJSONDocument): Variant;
var
d:IJSONDocument;
begin
d:=JSON(['distinct',Collection,'key',Key]);
if Query<>nil then d['query']:=Query;
Result:=RunCommand(d)['values'];
end;
function TMongoWire.Eval(const Collection, JSFn: WideString;
const Args: array of Variant; NoLock: boolean): Variant;
begin
Result:=RunCommand(JSON(['eval',bsonJavaScriptCodePrefix+
JSFn,'args',ja(Args)]))['retval'];
end;
{ TMongoWireQuery }
constructor TMongoWireQuery.Create(MongoWire: TMongoWire);
begin
inherited Create;
FOwner:=MongoWire;
//TODO: register for invalidation on owner's TMongoWire.Destroy
FData:=TMemoryStream.Create;
FData.Size:=MongoWireStartDataSize;//start keeping some data
FNumberToReturn:=0;//use db default
FNumberToSkip:=0;
FPageIndex:=0;
FNumberReturned:=0;
FCursorID:=0;
end;
destructor TMongoWireQuery.Destroy;
begin
KillCursor;
FOwner:=nil;//TODO: unregister
FData.Free;
inherited;
end;
procedure TMongoWireQuery.KillCursor;
var
i:integer;
begin
if FCursorID<>0 then
begin
FOwner.FReadLock.Enter;
try
FOwner.OpenMsg(OP_KILL_CURSORS,0,'');
i:=1;
FOwner.FData.Write(i,4);//just the one
FOwner.FData.Write(FCursorID,8);
FOwner.CloseMsg(nil);
finally
FOwner.FReadLock.Leave;
end;
FCursorID:=0;
end;
end;
procedure TMongoWireQuery.Query(const Collection: WideString; const QryObj,
ReturnFieldSelector: IJSONDocument; Flags: integer);
var
i:integer;
begin
KillCursor;
FOwner.FReadLock.Enter;
try
FOwner.OpenMsg(OP_QUERY,Flags,Collection);
FOwner.FData.Write(FNumberToSkip,4);
FOwner.FData.Write(FNumberToReturn,4);
if QryObj=nil then
begin
i:=5;//empty document
FOwner.FData.Write(i,4);
i:=0;//terminator
FOwner.FData.Write(i,1);
end
else
SaveBSON(QryObj,FOwner.FData);
if ReturnFieldSelector<>nil then
SaveBSON(ReturnFieldSelector,FOwner.FData);
i:=FOwner.CloseMsg(FData);//queue self's data
finally
FOwner.FReadLock.Leave;
end;
ReadResponse(i);
FCollection:=Collection;
end;
function TMongoWireQuery.Next(const Doc: IJSONDocument): boolean;
var
i:integer;
begin
if Doc=nil then
raise EMongoException.Create('MongoWireQuery.Next: Doc required');
if FPageIndex=FNumberReturned then
begin
if FCursorID=0 then
Result:=false
else
begin
//get more
FOwner.FReadLock.Enter;
try
FOwner.OpenMsg(OP_GET_MORE,0,FCollection);
FOwner.FData.Write(FNumberToReturn,4);
FOwner.FData.Write(FCursorID,8);
i:=FOwner.CloseMsg(FData);//queue self's data
finally
FOwner.FReadLock.Leave;
end;
ReadResponse(i);
Result:=FNumberReturned<>0;
end;
end
else
Result:=true;
if Result then
begin
//assert(FData.Position<PMongoWireMsgHeader((FData.Stream as TMemoryStream).Memory)^.MsgLength);
inc(FNumberToSkip);
inc(FPageIndex);
Doc.Clear;//?
LoadBSON(FData,Doc);
end;
end;
procedure TMongoWireQuery.ReadResponse(RequestID:integer);
var
i:integer;
d:IJSONDocument;
p:PMongoWireMsgHeader;
begin
FOwner.ReadMsg(RequestID);
p:=FData.Memory;
i:=p.Flags;
//assert StartingFrom=0
FCursorID:=p.CursorID;
FPageIndex:=0;
FNumberReturned:=p.NumberReturned;
if (i and $0001)<>0 then raise EMongoQueryError.Create('MongoWire.Query: cursor not found');
if (i and $0002)<>0 then //query failure
begin
//FCursorID:=0;//?
FNumberReturned:=0;
d:=JSON;
LoadBSON(FData,d);
raise EMongoQueryError.Create('MongoWire.Query: '+VarToStr(d['$err']));
end;
end;
end.