-
Notifications
You must be signed in to change notification settings - Fork 37
/
mongoWire.pas
675 lines (601 loc) · 17.7 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
{
TMongoWire: mongoWire.pas
Copyright 2010-2022 Stijn Sanders
Made available under terms described in file "LICENSE"
https://github.com/stijnsanders/TMongoWire
v2.0.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;
public
constructor Create(const NameSpace: Widestring);
destructor Destroy; override;
procedure Open(const ServerName: AnsiString = 'localhost';
Port: integer = 27017);
procedure OpenSecure(const ServerName: AnsiString = 'localhost';
Port: integer = 27017);
procedure Close;
function Msg(const MsgObj: IJSONDocument; Data: TMemoryStream;
const LoadInto: IJSONDocument = nil): IJSONDocument;
function Get(
const Collection: WideString;
const QryObj: IJSONDocument;
const Projection: IJSONDocument = nil
): IJSONDocument;
//Query: see TMongoWireQuery.Create
function Update(
const Collection: WideString;
const Selector, Doc: IJSONDocument;
Upsert: boolean = false;
MultiUpdate: boolean = false
): IJSONDocument;
function Insert(
const Collection: WideString;
const Doc: IJSONDocument
): IJSONDocument; overload;
function Insert(
const Collection: WideString;
const Docs: array of IJSONDocument
): IJSONDocument; overload;
procedure Insert(
const Collection: WideString;
const Docs: IJSONDocArray
); overload;
function Delete(
const Collection: WideString;
const Selector: IJSONDocument;
SingleRemove: boolean = false
): IJSONDocument;
function Ping: Boolean;
function EnsureIndex(
const Collection: WideString;
const Index: IJSONDocument;
const Options: IJSONDocument = nil
): IJSONDocument;
function Count(const Collection: WideString): integer;
function Distinct(const Collection, Key: WideString;
const Query: IJSONDocument=nil): 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:integer;
FCollection:WideString;
FCursorID:int64;
FCursorData:IJSONDocument;
FBatch:IJSONDocArray;
FBatchIndex:integer;
procedure KillCursor;
public
constructor Create(MongoWire:TMongoWire);
destructor Destroy; override;
procedure Query(
const Collection:WideString;
const QryObj:IJSONDocument;
const Projection:IJSONDocument=nil;
const Sort: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_MsgFlag_ChecksumPresent = $00000001;
mongoWire_MsgFlag_MoreToCome = $00000002;
mongoWire_MsgFlag_ExhaustAllowed = $00010000;
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_COMPRESSED = 2012;
OP_MSG = 2013;
MongoWireStartDataSize=$10000;
type
TMongoWireMsgHeader=packed record
MsgLength:integer;
RequestID:integer;
ResponseTo:integer;
OpCode:integer;
Flags:integer;
end;
PMongoWireMsgHeader=^TMongoWireMsgHeader;
{ TMongoWire }
constructor TMongoWire.Create(const NameSpace: WideString);
begin
inherited Create;
FSocket:=nil;
FData:=TMemoryStream.Create;
FData.Size:=MongoWireStartDataSize;//start keeping some data
FNameSpace:=NameSpace;
FWriteLock:=TCriticalSection.Create;
FReadLock:=TCriticalSection.Create;
FRequestIndex:=1;
FQueueIndex:=0;
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: AnsiString; 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 "'+string(ServerName)+':'+IntToStr(Port)+'"');
end;
procedure TMongoWire.OpenSecure(const ServerName: AnsiString; 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 "'+string(ServerName)+':'+IntToStr(Port)+'"');
end;
procedure TMongoWire.Close;
begin
if FSocket<>nil then
FSocket.Disconnect;
end;
function TMongoWire.Msg(const MsgObj: IJSONDocument; Data: TMemoryStream;
const LoadInto: IJSONDocument = nil): IJSONDocument;
const
dSize=$10000;
var
p:PMongoWireMsgHeader;
r,i,l:integer;
h:TMongoWireMsgHeader;
d:array[0..dSize-1] of byte;
dx:TMemoryStream;
begin
if (FSocket=nil) or not(FSocket.Connected) then
raise EMongoNotConnected.Create('MongoWire: not connected');
//assert Data<>nil
FWriteLock.Enter;
try
//message header
p:=FData.Memory;
p.ResponseTo:=0;
p.OpCode:=OP_MSG;
p.Flags:=0;
FData.Position:=20;//SizeOf first part of TMongoWireMsgHeader
//kind:body
i:=0;
FData.Write(i,1);
SaveBSON(MsgObj,FData);
//update header
r:=FRequestIndex;
p.RequestID:=r;
p.MsgLength:=FData.Position;
inc(FRequestIndex);
if FRequestIndex<0 then FRequestIndex:=1;
//send request
FSocket.SendBuf(FData.Memory^,FData.Position);
//add to queue
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;
if Data=nil then Data:=FData;//default to connection data cache
FQueue[i].Data:=Data;
finally
FWriteLock.Leave;
end;
FReadLock.Enter;
try
//is the request already in?
i:=0;
while (i<FQueueIndex) and (FQueue[i].RequestID<>r) 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
//read header
i:=FSocket.ReceiveBuf(h,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.ResponseTo) do inc(i);
if i=FQueueIndex then
begin
//odd, not on queue, read to flush,
l:=h.MsgLength-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.ResponseTo));
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,12);
//read data
//dx.Position:=12;
l:=h.MsgLength-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;
//TODO:
if (h.Flags and mongoWire_MsgFlag_MoreToCome)<>0 then
raise Exception.Create('//TODO: support mongoWire_MsgFlag_MoreToCome');
//if (h.Flags and mongoWire_MsgFlag_ChecksumPresent)<>0 then
// TODO: check checksum
//set position after message header
//case h.OpCode of OP_MSG:? //TODO
dx.Position:=20;
i:=0;
dx.Read(i,1);
//case i? //TODO
if i<>0 then
raise EMongoException.Create('//TODO: support msg kind '+Format('%.2x',[i]));
end;
until h.ResponseTo=r;
//request is in!
end;
finally
FReadLock.Leave;
end;
if LoadInto=nil then Result:=JSON else Result:=LoadInto;
LoadBSON(Data,Result);
if Result['ok']<>1 then
try
if VarIsNull(Result['errmsg']) then
raise EMongoCommandError.Create('Unspecified error with "'+
VarToStr(MsgObj.ToVarArray[0,0])+'"')
else
raise EMongoCommandError.Create('Command "'+
VarToStr(MsgObj.ToVarArray[0,0])+'" failed: '+
VarToStr(Result['errmsg']));
except
on EMongoCommandError do
raise;
on Exception do
raise EMongoCommandError.Create('Command failed');
end;
end;
function TMongoWire.Get(const Collection: WideString; const QryObj,
Projection: IJSONDocument): IJSONDocument;
begin
Result:=JSON(JSON(Msg(JSON(
['find',Collection
,'$db',FNameSpace
,'filter',QryObj
,'projection',Projection
,'limit',1
]),FData)['cursor'])['firstBatch'][0]);
end;
function TMongoWire.Update(const Collection: WideString; const Selector,
Doc: IJSONDocument; Upsert, MultiUpdate: boolean): IJSONDocument;
begin
if Doc=nil then
raise EMongoException.Create('MongoWire.Update: Doc required');
Result:=Msg(JSON(
['update',Collection
,'$db',FNameSpace
,'updates',VarArrayOf([JSON(
['q',Selector
,'u',Doc
//,'c',
,'upsert',Upsert
,'multi',MultiUpdate
//,'collation',
//,'arrayFilters',
//,'hint',
])])
//,'ordered',
,'writeConcern',FWriteConcern
//,'bypassDocumentValidation',
//,'comment',
//,'let ',
]),FData);
end;
function TMongoWire.Insert(const Collection: WideString;
const Doc: IJSONDocument): IJSONDocument;
begin
if Doc=nil then
raise EMongoException.Create('MongoWire.Insert: Doc required');
Result:=Msg(JSON(
['insert',Collection
,'$db',FNameSpace
,'documents',ja([Doc])
,'writeConcern',FWriteConcern
//,'bypassDocumentValidation',
//,'comment',
]),FData);
end;
function TMongoWire.Insert(const Collection: WideString;
const Docs: array of IJSONDocument): 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];
Result:=Msg(JSON(
['insert',Collection
,'$db',FNameSpace
,'documents',v
,'writeConcern',FWriteConcern
//,'bypassDocumentValidation',
//,'comment',
]),FData);
end;
procedure TMongoWire.Insert(const Collection: WideString;
const Docs: IJSONDocArray);
begin
Msg(JSON(
['insert',Collection
,'$db',FNameSpace
,'documents',Docs
,'writeConcern',FWriteConcern
//,'bypassDocumentValidation',
//,'comment',
]),FData);
end;
function TMongoWire.Delete(const Collection: WideString;
const Selector: IJSONDocument; SingleRemove: boolean): IJSONDocument;
var
l:integer;
begin
if SingleRemove then l:=1 else l:=0;
Result:=Msg(JSON(
['delete',Collection
,'$db',FNameSpace
,'deletes',ja([JSON(
['q',Selector
,'limit',l
//,'collation',
//,'hint',
])])
//,'comment',
//,'let',
//,'ordered',
,'writeConcern',FWriteConcern
]),FData);
end;
function TMongoWire.Ping: Boolean;
begin
Result:=Msg(JSON(['ping',1,'$db','admin']),FData)['ok']=1;
end;
function TMongoWire.EnsureIndex(const Collection: WideString;
const Index, Options: IJSONDocument): 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;
Result:=Insert(mongoWire_Db_SystemIndexCollection, Document);
end;
function TMongoWire.Count(const Collection: WideString): integer;
begin
Result:=Msg(JSON(['count',Collection]),FData)['n'];
end;
function TMongoWire.Distinct(const Collection, Key: WideString;
const Query: IJSONDocument): Variant;
begin
Result:=Msg(JSON(
['distinct',Collection
,'$db',FNameSpace
,'key',Key
,'query',Query
]),FData)['values'];
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;
FCursorID:=0;
FBatchIndex:=0;
end;
destructor TMongoWireQuery.Destroy;
begin
KillCursor;
FOwner:=nil;//TODO: unregister
FBatch:=nil;
FCursorData:=nil;
FData.Free;
inherited;
end;
procedure TMongoWireQuery.KillCursor;
begin
if FCursorData<>nil then
begin
FOwner.Msg(JSON(
['killCursors',FCollection
,'$db',FOwner.NameSpace
,'cursors',VarArrayOf([FCursorID])
]),FData);
FCursorData:=nil;
end;
FCursorID:=0;
end;
procedure TMongoWireQuery.Query(const Collection: WideString; const QryObj,
Projection, Sort: IJSONDocument; Flags: integer);
var
d,p:IJSONDocument;
begin
KillCursor;
FCollection:=Collection;
d:=JSON(
['find',FCollection
,'$db',FOwner.NameSpace
,'filter',QryObj
,'sort',Sort
,'projection',Projection
//,'hint'
]);
if FNumberToSkip<>0 then d['skip']:=FNumberToSkip;
if FNumberToReturn<>0 then d['limit']:=FNumberToReturn;
//batchSize
//singleBatch
//comment
//maxTimeMS
//readConcern
//max
//returnKey
//showRecordId
//tailable
//oplogReplay
//noCursorTimeout
//awaitData
//allowPartialResults
//allowDiskUse
//left
FBatch:=JSONDocArray;
FBatchIndex:=0;
FCursorData:=JSON(['firstBatch',FBatch]);
p:=JSON(['cursor',FCursorData]);
FOwner.Msg(d,FData,p);
FCursorID:=FCursorData['id'];
end;
function TMongoWireQuery.Next(const Doc: IJSONDocument): boolean;
var
p:IJSONDocument;
begin
if Doc=nil then
raise EMongoException.Create('MongoWireQuery.Next: Doc required');
if FBatch=nil then
Result:=false
else
if FBatchIndex>=FBatch.Count then
begin
if (FCursorID<>0) then //and (FCursorData['partialResultsReturned']=true) then
begin
FCursorData.Delete('firstBatch');
FCursorData['nextBatch']:=FBatch;
FBatchIndex:=0;
p:=JSON(['cursor',FCursorData]);
FOwner.Msg(JSON(
['getMore',FCursorID
,'collection',FCollection
,'$db',FOwner.NameSpace
//,'batchSize',
//,'maxTimeMS',
//,'comment',
]),FData,p);
FCursorID:=FCursorData['id'];
Result:=FBatch.Count<>0;
end
else
Result:=false;
end
else
Result:=true;
if Result then
begin
FBatch.LoadItem(FBatchIndex,Doc);
inc(FBatchIndex);
end;
end;
end.