-
Notifications
You must be signed in to change notification settings - Fork 5
/
Couchbase.pas
1117 lines (975 loc) · 35.9 KB
/
Couchbase.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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit Couchbase;
{ Delphi classes for Couchbase }
interface
uses
SysUtils,
System.Generics.Collections,
Couchbase.API;
type
TCouchbaseFormat = (
Unknown = 0,
Foreign = 1,
JSON = 2,
RAW = 3,
UTF8 = 4);
TCouchbaseOptions = record
Format: TCouchbaseFormat;
ExpireTime: UInt32;
CAS: UInt64;
public
procedure Initialize;
end;
TCouchbaseSubDocResponse = record
Value: Utf8String;
Status: Integer;
end;
TCouchbaseSubDocResponses = TArray<TCouchbaseSubDocResponse>;
type
TCouchbaseResult = record
Success: Boolean;
Error: Integer;
Key: Utf8String;
Value: TBytes;
Format: TCouchbaseFormat;
Flags: Integer;
CAS: Integer;
Operation: Integer;
Counter: Integer; { incr/decr ops }
public
procedure Initialize;
end;
PCouchbaseResult = ^TCouchbaseResult;
TCouchbaseFlushResult = record
Success: Boolean;
Error: Integer;
Flags: Integer;
CAS: Integer;
Node: Utf8String; { flush/stat ops }
public
procedure Initialize;
end;
PCouchbaseFlushResult = ^TCouchbaseFlushResult;
TCouchbaseStatsResult = record
Success: Boolean;
Error: Integer;
Stats: TDictionary<Utf8String, TBytes>;
Flags: Integer;
CAS: Integer;
Node: Utf8String; { flush/stat ops }
public
procedure Initialize;
procedure Finalize;
end;
PCouchbaseStatsResult = ^TCouchbaseStatsResult;
TCouchbaseSubDocResult = record
Success: Boolean;
Error: Integer;
ErrorIndex: Integer;
Key: Utf8String;
Flags: Integer;
CAS: Integer;
Responses: TCouchbaseSubDocResponses;
public
procedure Initialize;
end;
PCouchbaseSubDocResult = ^TCouchbaseSubDocResult;
TCouchbaseQueryError = record
Code: Integer;
Msg: Utf8String;
end;
TCouchbaseQueryMetrics = record
ElapsedTime: Utf8String;
ExecutionTime: Utf8String;
ResultCount: Integer;
ResultSize: Integer;
ErrorCount: Integer;
end;
TCouchbaseQueryResult = record
Success: Boolean;
RequestId: Utf8String;
Errors: TArray<TCouchbaseQueryError>;
Status: Utf8String;
Metrics: TCouchbaseQueryMetrics;
Rows: TList<Utf8String>;
MetaData: Utf8String;
public
procedure Initialize;
procedure Finalize;
end;
PCouchbaseQueryResult = ^TCouchbaseQueryResult;
type
TgoCouchbase = class;
ICouchbaseSubDoc = interface
function Get(const APath: Utf8String): ICouchbaseSubDoc;
function Exists(const APath: Utf8String): ICouchbaseSubDoc;
function GetCount(const APath: Utf8String): ICouchbaseSubDoc;
function Upsert(const APath: Utf8String; const AValue: TBytes; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function Upsert(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function Insert(const APath: Utf8String; const AValue: TBytes; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function Insert(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function Replace(const APath: Utf8String; const AValue: TBytes): ICouchbaseSubDoc; overload;
function Replace(const APath: Utf8String; const AValue: Utf8String): ICouchbaseSubDoc; overload;
function Remove(const APath: Utf8String): ICouchbaseSubDoc;
function ArrayAppend(const APath: Utf8String; const AValue: TBytes; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayAppend(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayPrepend(const APath: Utf8String; const AValue: TBytes; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayPrepend(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayAddUnique(const APath: Utf8String; const AValue: TBytes; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayAddUnique(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayInsert(const APath: Utf8String; const AValue: TBytes): ICouchbaseSubDoc; overload;
function ArrayInsert(const APath: Utf8String; const AValue: Utf8String): ICouchbaseSubDoc; overload;
function Counter(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc;
{ Executes the subdocument query }
function Execute: TCouchbaseSubDocResult;
end;
TgoCouchbaseSubDoc = class(TInterfacedObject, ICouchbaseSubDoc)
protected
FLastErrorCode: Integer;
FLastErrorDesc: String;
function Success(const AResult: Lcb_error_t): Boolean;
private
FCouchbase: TgoCouchbase;
FKey: Utf8String;
FMultiMode: Integer;
private
procedure Append(const ACommand: Integer; const APath: Utf8String); overload;
procedure Append(const ACommand: Integer; const APath: Utf8String;
const AValue: TBytes); overload;
procedure Append(const ACommand: Integer; const APath: Utf8String;
const AValue: TBytes; const ACreateParent: Boolean); overload;
private
FSpecs: TArray<lcb_SDSPEC>;
public
constructor Create(const ACouchbase: TgoCouchbase; const AKey: Utf8String; const AMultiMode: Integer);
destructor Destroy; override;
public
function Get(const APath: Utf8String): ICouchbaseSubDoc;
function Exists(const APath: Utf8String): ICouchbaseSubDoc;
function GetCount(const APath: Utf8String): ICouchbaseSubDoc;
function Upsert(const APath: Utf8String; const AValue: TBytes; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function Upsert(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function Insert(const APath: Utf8String; const AValue: TBytes; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function Insert(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function Replace(const APath: Utf8String; const AValue: TBytes): ICouchbaseSubDoc; overload;
function Replace(const APath: Utf8String; const AValue: Utf8String): ICouchbaseSubDoc; overload;
function Remove(const APath: Utf8String): ICouchbaseSubDoc;
function ArrayAppend(const APath: Utf8String; const AValue: TBytes; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayAppend(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayPrepend(const APath: Utf8String; const AValue: TBytes; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayPrepend(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayAddUnique(const APath: Utf8String; const AValue: TBytes; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayAddUnique(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc; overload;
function ArrayInsert(const APath: Utf8String; const AValue: TBytes): ICouchbaseSubDoc; overload;
function ArrayInsert(const APath: Utf8String; const AValue: Utf8String): ICouchbaseSubDoc; overload;
function Counter(const APath: Utf8String; const AValue: Utf8String; const ACreateParent: Boolean = True): ICouchbaseSubDoc;
{ Executes the subdocument query }
function Execute: TCouchbaseSubDocResult;
public
property LastErrorCode: Integer read FLastErrorCode;
property LastErrorDesc: String read FLastErrorDesc;
end;
TgoCouchbaseN1QL = class(TObject)
private
FParams: lcb_N1QLPARAMS;
public
constructor Create;
destructor Destroy; override;
public
function SetStatement(const AQuery: Utf8String): Lcb_error_t;
public
property Params: lcb_N1QLPARAMS read FParams;
end;
TgoCouchbase = class(TObject)
protected
FLastErrorCode: Integer;
FLastErrorDesc: String;
function Success(const AResult: Lcb_error_t): Boolean;
function Store(const AOperation: Integer; const AKey: Utf8String; const AValue: TBytes;
const AOptions: TCouchbaseOptions): TCouchbaseResult;
function StoreRaw(const AOperation: Integer; const AKey: Utf8String; const AValue: TBytes): TCouchbaseResult;
private
FInstance: lcb_t;
FOptions: lcb_create_st;
public
constructor Create;
destructor Destroy; override;
public
function Connect(const AConnection: String; const AUsername: String = ''; const APassword: String = ''): Boolean;
{ Get }
function Get(const AKey: Utf8String; out AValue: TBytes): TCouchbaseResult; overload;
function Get(const AKey: Utf8String; out AValue: String): TCouchbaseResult; overload;
{ Set/Upsert }
function Upsert(const AKey: Utf8String; const AValue: TBytes; const AOptions: TCouchbaseOptions): TCouchbaseResult; overload;
function Upsert(const AKey: Utf8String; const AValue: String; const AOptions: TCouchbaseOptions): TCouchbaseResult; overload;
function Upsert(const AKey: Utf8String; const AValue: String): TCouchbaseResult; overload;
{ Add }
function Add(const AKey: Utf8String; const AValue: TBytes; const AOptions: TCouchbaseOptions): TCouchbaseResult; overload;
function Add(const AKey: Utf8String; const AValue: String; const AOptions: TCouchbaseOptions): TCouchbaseResult; overload;
function Add(const AKey: Utf8String; const AValue: String): TCouchbaseResult; overload;
{ Replace }
function Replace(const AKey: Utf8String; const AValue: TBytes; const AOptions: TCouchbaseOptions): TCouchbaseResult; overload;
function Replace(const AKey: Utf8String; const AValue: String; const AOptions: TCouchbaseOptions): TCouchbaseResult; overload;
function Replace(const AKey: Utf8String; const AValue: String): TCouchbaseResult; overload;
{ Append/Prepend }
function Append(const AKey: Utf8String; const AValue: TBytes): TCouchbaseResult; overload;
function Append(const AKey: Utf8String; const AValue: String): TCouchbaseResult; overload;
function Prepend(const AKey: Utf8String; const AValue: TBytes): TCouchbaseResult; overload;
function Prepend(const AKey: Utf8String; const AValue: String): TCouchbaseResult; overload;
{ Touch }
function Touch(const AKey: Utf8String; const AExpireTime: UInt32 = 0): TCouchbaseResult;
{ Increment/Decrement }
function Incr(const AKey: Utf8String; const ADelta: Integer = 1; const AInitial: Integer = 0;
const ACreate: Boolean = True): TCouchbaseResult;
function Decr(const AKey: Utf8String; const ADelta: Integer = -1; const AInitial: Integer = 0;
const ACreate: Boolean = True): TCouchbaseResult;
{ Delete }
function Delete(const AKey: Utf8String): TCouchbaseResult;
{ Flush }
function Flush: TCouchbaseFlushResult;
{ Stats }
function Stats: TCouchbaseStatsResult;
public
{ Subdocuments }
{ Lookup subdoc operations }
function LookupIn(const AKey: Utf8String): ICouchbaseSubDoc;
{ Mutate subdoc operations }
function MutateIn(const AKey: Utf8String): ICouchbaseSubDoc;
public
{ N1QL }
function Query(const AParams: TgoCouchbaseN1QL): TCouchbaseQueryResult;
public
property LastErrorCode: Integer read FLastErrorCode;
property LastErrorDesc: String read FLastErrorDesc;
{ Bucket instance }
property Instance: lcb_t read FInstance;
end;
implementation
uses
Grijjy.Bson;
var
DEFAULT_OPTIONS: TCouchbaseOptions;
{ TCouchbase }
constructor TgoCouchbase.Create;
begin
FInstance := nil;
end;
destructor TgoCouchbase.Destroy;
begin
if FInstance <> nil then
begin
lcb_destroy(FInstance);
FInstance := nil;
end;
inherited;
end;
function TgoCouchbase.Success(const AResult: Lcb_error_t): Boolean;
begin
FLastErrorCode := AResult;
if FLastErrorCode = LCB_SUCCESS then
begin
FLastErrorDesc := 'Success';
Result := True;
end
else
begin
FLastErrorDesc := String(lcb_strerror(nil, FLastErrorCode));
Result := False;
end;
end;
function TgoCouchbase.Store(const AOperation: Integer; const AKey: Utf8String; const AValue: TBytes;
const AOptions: TCouchbaseOptions): TCouchbaseResult;
var
Command: lcb_CMDSTORE;
begin
FillChar(Command, SizeOf(Command), 0);
Result.Initialize;
Command.flags := Integer(AOptions.Format) shl 24;
Command.cmdbase.exptime := AOptions.ExpireTime;
Command.cmdbase.cas := AOptions.CAS;
LCB_CMD_SET_KEY(Command.cmdbase, MarshaledAString(AKey), Length(AKey));
LCB_CMD_SET_VALUE(Command, AValue, Length(AValue));
Command.operation := AOperation;
if Success(lcb_store3(FInstance, @Result, @Command)) then
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
end;
function TgoCouchbase.StoreRaw(const AOperation: Integer; const AKey: Utf8String; const AValue: TBytes): TCouchbaseResult;
var
Command: lcb_CMDSTORE;
begin
FillChar(Command, SizeOf(Command), 0);
Result.Initialize;
LCB_CMD_SET_KEY(Command.cmdbase, MarshaledAString(AKey), Length(AKey));
LCB_CMD_SET_VALUE(Command, AValue, Length(AValue));
Command.operation := AOperation;
if Success(lcb_store3(FInstance, @Result, @Command)) then
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
end;
procedure ResponseCallback(AInstance: lcb_t; ACBType: Integer; const AResponseBase: plcb_RESPBASE); cdecl;
var
CBResult: PCouchbaseResult;
ResponseGet: plcb_RESPGET;
ResponseStore: plcb_RESPSTORE;
ResponseCounter: plcb_RESPCOUNTER;
begin
if AResponseBase.cookie <> nil then
begin
CBResult := AResponseBase.cookie;
CBResult.Success := AResponseBase.rc = LCB_SUCCESS;
SetLength(CBResult.Key, AResponseBase.nkey);
Move(AResponseBase.key^, CBResult.Key[1], AResponseBase.nkey);
CBResult.Flags := AResponseBase.rflags;
CBResult.CAS := AResponseBase.cas;
if CBResult.Success then
begin
case ACBType of
LCB_CALLBACK_GET:
begin
ResponseGet := plcb_RESPGET(AResponseBase);
CBResult.Format := TCouchbaseFormat(ResponseGet.itmflags shr 24);
SetLength(CBResult.Value, ResponseGet.nvalue);
Move(ResponseGet.value^, CBResult.Value[0], ResponseGet.nvalue);
end;
LCB_CALLBACK_STORE:
begin
ResponseStore := plcb_RESPSTORE(AResponseBase);
CBResult.Operation := ResponseStore.op;
end;
LCB_CALLBACK_COUNTER:
begin
ResponseCounter := plcb_RESPCOUNTER(AResponseBase);
CBResult.Counter := ResponseCounter.value;
end;
end;
end
else
CBResult.Error := AResponseBase.rc;
end;
end;
procedure ResponseFlushCallback(AInstance: lcb_t; ACBType: Integer; const AResponseBase: plcb_RESPBASE); cdecl;
var
CBFlushResult: PCouchbaseFlushResult;
ResponseFlush: plcb_RESPFLUSH;
begin
if AResponseBase.cookie <> nil then
begin
CBFlushResult := AResponseBase.cookie;
CBFlushResult.Success := AResponseBase.rc = LCB_SUCCESS;
CBFlushResult.Flags := AResponseBase.rflags;
CBFlushResult.CAS := AResponseBase.cas;
if CBFlushResult.Success then
begin
case ACBType of
LCB_CALLBACK_FLUSH:
begin
ResponseFlush := plcb_RESPFLUSH(AResponseBase);
SetLength(CBFlushResult.Node, Length(ResponseFlush.respserverbase.respserverfields.server));
Move(ResponseFlush.respserverbase.respserverfields.server^, CBFlushResult.Node[1], Length(ResponseFlush.respserverbase.respserverfields.server));
end;
end;
end
else
CBFlushResult.Error := AResponseBase.rc;
end;
end;
procedure ResponseStatsCallback(AInstance: lcb_t; ACBType: Integer; const AResponseBase: plcb_RESPBASE); cdecl;
var
CBStatsResult: PCouchbaseStatsResult;
ResponseStats: plcb_RESPSTATS;
Key: Utf8String;
Value: TBytes;
begin
if AResponseBase.cookie <> nil then
begin
CBStatsResult := AResponseBase.cookie;
CBStatsResult.Success := AResponseBase.rc = LCB_SUCCESS;
CBStatsResult.Flags := AResponseBase.rflags;
CBStatsResult.CAS := AResponseBase.cas;
if CBStatsResult.Success then
begin
case ACBType of
LCB_CALLBACK_STATS:
begin
{ the callback for this command is invoked an indeterminate amount number of times and
is finished when resp->rflags & LCB_RESP_F_FINAL }
ResponseStats := plcb_RESPSTATS(AResponseBase);
if (ResponseStats.respserverbase.respbase.rflags AND LCB_RESP_F_FINAL) = 0 then
begin
SetLength(CBStatsResult.Node, Length(ResponseStats.respserverbase.respserverfields.server));
Move(ResponseStats.respserverbase.respserverfields.server^, CBStatsResult.Node[1], Length(ResponseStats.respserverbase.respserverfields.server));
SetLength(Key, ResponseStats.respserverbase.respbase.nkey);
Move(ResponseStats.respserverbase.respbase.key^, Key[1], ResponseStats.respserverbase.respbase.nkey);
SetLength(Value, ResponseStats.nvalue);
Move(ResponseStats.value^, Value[0], ResponseStats.nValue);
CBStatsResult.Stats.Add(Key, Value);
end;
end;
end;
end
else
CBStatsResult.Error := AResponseBase.rc;
end;
end;
procedure ResponseSubDocCallback(AInstance: lcb_t; ACBType: Integer; const AResponseBase: plcb_RESPBASE); cdecl;
var
CBSubDocResult: PCouchbaseSubDocResult;
CBSubDocResponse: TCouchbaseSubDocResponse;
ResponseSubDoc: plcb_RESPSUBDOC;
SDEntry: lcb_SDENTRY;
Iterator: size_t;
Index: Integer;
begin
if AResponseBase.cookie <> nil then
begin
CBSubDocResult := AResponseBase.cookie;
CBSubDocResult.Success := AResponseBase.rc = LCB_SUCCESS;
CBSubDocResult.Error := AResponseBase.rc;
SetLength(CBSubDocResult.Key, AResponseBase.nkey);
Move(AResponseBase.key^, CBSubDocResult.Key[1], AResponseBase.nkey);
CBSubDocResult.Flags := AResponseBase.rflags;
CBSubDocResult.CAS := AResponseBase.cas;
case ACBType of
LCB_CALLBACK_SDLOOKUP,
LCB_CALLBACK_SDMUTATE:
begin
ResponseSubDoc := plcb_RESPSUBDOC(AResponseBase);
Iterator := 0;
Index := 0;
while lcb_sdresult_next(ResponseSubDoc, SDEntry, Iterator) <> 0 do
begin
if ACBType = LCB_CALLBACK_SDMUTATE then
Index := SDEntry.index; { mutate only }
if Length(CBSubDocResult.Responses) < (Index + 1) then
SetLength(CBSubDocResult.Responses, (Index + 1));
SetLength(CBSubDocResponse.Value, SDEntry.nvalue);
Move(SDEntry.value^, CBSubDocResponse.Value[1], SDEntry.nvalue); { always json }
CBSubDocResponse.Status := SDEntry.status;
CBSubDocResult.Responses[Index] := CBSubDocResponse;
if ACBType = LCB_CALLBACK_SDLOOKUP then
Inc(Index);
end;
end;
end;
end;
end;
procedure ResponseQueryCallback(AInstance: lcb_t; ACBType: Integer; const AResponse: lcb_RESPN1QL); cdecl;
var
CBQueryResult: PCouchbaseQueryResult;
Value: Utf8String;
begin
if AResponse.respbase.cookie <> nil then
begin
CBQueryResult := AResponse.respbase.cookie;
CBQueryResult.Success := AResponse.respbase.rc = LCB_SUCCESS;
if (AResponse.respbase.rflags AND LCB_RESP_F_FINAL) = 0 then
begin
SetLength(Value, AResponse.nRow);
Move(AResponse.row^, Value[1], AResponse.nRow);
CBQueryResult.Rows.Add(Value);
end
else
begin
// metadata
SetLength(CBQueryResult.MetaData, AResponse.nRow);
Move(AResponse.row^, CBQueryResult.MetaData[1], AResponse.nRow);
end;
end;
end;
function TgoCouchbase.Connect(const AConnection: String; const AUsername, APassword: String): Boolean;
begin
Result := False;
FillChar(FOptions, SizeOf(FOptions), 0);
FOptions.version := 3;
FOptions.v3.connstr := MarshaledAString(TMarshal.AsAnsi(AConnection));
FOptions.v3.username := MarshaledAString(TMarshal.AsAnsi(AUsername));
FOptions.v3.passwd := MarshaledAString(TMarshal.AsAnsi(APassword));
if Success(lcb_create(@FInstance, @FOptions)) then
if Success(lcb_connect(FInstance)) then
begin
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
Result := Success(lcb_get_bootstrap_status(FInstance));
if Result then
begin
{ crud callbacks }
lcb_install_callback3(FInstance, LCB_CALLBACK_GET, ResponseCallback);
lcb_install_callback3(FInstance, LCB_CALLBACK_STORE, ResponseCallback);
lcb_install_callback3(FInstance, LCB_CALLBACK_COUNTER, ResponseCallback);
lcb_install_callback3(FInstance, LCB_CALLBACK_TOUCH, ResponseCallback);
lcb_install_callback3(FInstance, LCB_CALLBACK_REMOVE, ResponseCallback);
{ flush/stats callbacks }
lcb_install_callback3(FInstance, LCB_CALLBACK_FLUSH, ResponseFlushCallback);
lcb_install_callback3(FInstance, LCB_CALLBACK_STATS, ResponseStatsCallback);
{ json subdoc callbacks }
lcb_install_callback3(FInstance, LCB_CALLBACK_SDLOOKUP, ResponseSubDocCallback);
lcb_install_callback3(FInstance, LCB_CALLBACK_SDMUTATE, ResponseSubDocCallback);
end;
end;
end;
function TgoCouchbase.Get(const AKey: Utf8String; out AValue: TBytes): TCouchbaseResult;
var
Command: lcb_CMDGET;
begin
FillChar(Command, SizeOf(Command), 0);
Result.Initialize;
LCB_CMD_SET_KEY(Command.cmdbase, MarshaledAString(AKey), Length(AKey));
if Success(lcb_get3(FInstance, @Result, @Command)) then
begin
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
if Result.Success then
AValue := Result.Value;
end;
end;
function TgoCouchbase.Get(const AKey: Utf8String; out AValue: String): TCouchbaseResult;
var
Value: TBytes;
begin
Result := Get(AKey, Value);
if Result.Success then
AValue := TEncoding.Utf8.GetString(Value);
end;
function TgoCouchbase.Upsert(const AKey: Utf8String; const AValue: TBytes;
const AOptions: TCouchbaseOptions): TCouchbaseResult;
begin
Result := Store(LCB_SET, AKey, AValue, AOptions);
end;
function TgoCouchbase.Upsert(const AKey: Utf8String; const AValue: String;
const AOptions: TCouchbaseOptions): TCouchbaseResult;
begin
Result := Store(LCB_SET, AKey, TEncoding.UTF8.GetBytes(AValue), AOptions);
end;
function TgoCouchbase.Upsert(const AKey: Utf8String; const AValue: String): TCouchbaseResult;
begin
Result := Store(LCB_SET, AKey, TEncoding.UTF8.GetBytes(AValue), DEFAULT_OPTIONS);
end;
function TgoCouchbase.Add(const AKey: Utf8String; const AValue: TBytes;
const AOptions: TCouchbaseOptions): TCouchbaseResult;
begin
Result := Store(LCB_ADD, AKey, AValue, AOptions);
end;
function TgoCouchbase.Add(const AKey: Utf8String; const AValue: String;
const AOptions: TCouchbaseOptions): TCouchbaseResult;
begin
Result := Store(LCB_ADD, AKey, TEncoding.UTF8.GetBytes(AValue), AOptions);
end;
function TgoCouchbase.Add(const AKey: Utf8String; const AValue: String): TCouchbaseResult;
begin
Result := Store(LCB_ADD, AKey, TEncoding.UTF8.GetBytes(AValue), DEFAULT_OPTIONS);
end;
function TgoCouchbase.Replace(const AKey: Utf8String; const AValue: TBytes;
const AOptions: TCouchbaseOptions): TCouchbaseResult;
begin
Result := Store(LCB_REPLACE, AKey, AValue, AOptions);
end;
function TgoCouchbase.Replace(const AKey: Utf8String; const AValue: String;
const AOptions: TCouchbaseOptions): TCouchbaseResult;
begin
Result := Store(LCB_REPLACE, AKey, TEncoding.UTF8.GetBytes(AValue), AOptions);
end;
function TgoCouchbase.Replace(const AKey: Utf8String; const AValue: String): TCouchbaseResult;
begin
Result := Store(LCB_REPLACE, AKey, TEncoding.UTF8.GetBytes(AValue), DEFAULT_OPTIONS);
end;
function TgoCouchbase.Append(const AKey: Utf8String; const AValue: TBytes): TCouchbaseResult;
begin
Result := StoreRaw(LCB_APPEND, AKey, AValue);
end;
function TgoCouchbase.Append(const AKey: Utf8String; const AValue: String): TCouchbaseResult;
begin
Result := StoreRaw(LCB_APPEND, AKey, TEncoding.UTF8.GetBytes(AValue));
end;
function TgoCouchbase.Prepend(const AKey: Utf8String; const AValue: TBytes): TCouchbaseResult;
begin
Result := StoreRaw(LCB_PREPEND, AKey, AValue);
end;
function TgoCouchbase.Prepend(const AKey: Utf8String; const AValue: String): TCouchbaseResult;
begin
Result := StoreRaw(LCB_PREPEND, AKey, TEncoding.UTF8.GetBytes(AValue));
end;
function TgoCouchbase.Query(const AParams: TgoCouchbaseN1QL): TCouchbaseQueryResult;
var
Command: lcb_CMDN1QL;
Doc, Error, Metrics: TgoBsonDocument;
Errors: TgoBsonArray;
Value: TgoBsonValue;
I: Integer;
begin
FillChar(Command, SizeOf(Command), 0);
Result.Initialize;
Command.callback := ResponseQueryCallback;
if Success(lcb_n1p_mkcmd(AParams.Params, @Command)) then
if Success(lcb_n1ql_query(FInstance, @Result, @Command)) then
begin
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
if TgoBsonDocument.TryParse(Result.MetaData, Doc) then
begin
Result.Status := Doc['status'];
Result.Success := Result.Status = 'success';
Result.RequestId := Doc['requestID'];
if (Doc.TryGetValue('errors', Value)) then
begin
Errors := Value.AsBsonArray;
SetLength(Result.Errors, Errors.Count);
for I := 0 to Errors.Count - 1 do
begin
Error := Errors[I].AsBsonDocument;
Result.Errors[I].Code := Error['code'];
Result.Errors[I].Msg := Error['msg'];
end;
end;
if (Doc.TryGetValue('metrics', Value)) then
begin
Metrics := Value.AsBsonDocument;
Result.Metrics.ElapsedTime := Metrics['elapsedTime'];
Result.Metrics.ExecutionTime:= Metrics['executionTime'];
Result.Metrics.ResultCount := Metrics['resultCount'];
Result.Metrics.ResultSize := Metrics['resultSize'];
Result.Metrics.ErrorCount := Metrics['errorCount'];
end;
end;
end;
end;
function TgoCouchbase.Touch(const AKey: Utf8String; const AExpireTime: UInt32): TCouchbaseResult;
var
Command: lcb_CMDTOUCH;
begin
FillChar(Command, SizeOf(Command), 0);
Command.cmdbase.exptime := AExpireTime;
Result.Initialize;
LCB_CMD_SET_KEY(Command.cmdbase, MarshaledAString(AKey), Length(AKey));
if Success(lcb_touch3(FInstance, @Result, @Command)) then
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
end;
function TgoCouchbase.Incr(const AKey: Utf8String; const ADelta: Integer; const AInitial: Integer;
const ACreate: Boolean): TCouchbaseResult;
var
Command: lcb_CMDCOUNTER;
begin
FillChar(Command, SizeOf(Command), 0);
Command.delta := ADelta;
Command.initial := AInitial;
Command.create := Integer(ACreate);
Result.Initialize;
LCB_CMD_SET_KEY(Command.cmdbase, MarshaledAString(AKey), Length(AKey));
if Success(lcb_counter3(FInstance, @Result, @Command)) then
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
end;
function TgoCouchbase.Decr(const AKey: Utf8String; const ADelta: Integer; const AInitial: Integer;
const ACreate: Boolean): TCouchbaseResult;
var
Command: lcb_CMDCOUNTER;
begin
FillChar(Command, SizeOf(Command), 0);
Command.delta := ADelta;
Command.initial := AInitial;
Command.create := Integer(ACreate);
Result.Initialize;
LCB_CMD_SET_KEY(Command.cmdbase, MarshaledAString(AKey), Length(AKey));
if Success(lcb_counter3(FInstance, @Result, @Command)) then
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
end;
function TgoCouchbase.Delete(const AKey: Utf8String): TCouchbaseResult;
var
Command: lcb_CMDREMOVE;
begin
FillChar(Command, SizeOf(Command), 0);
Result.Initialize;
LCB_CMD_SET_KEY(Command.cmdbase, MarshaledAString(AKey), Length(AKey));
if Success(lcb_remove3(FInstance, @Result, @Command)) then
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
end;
function TgoCouchbase.Flush: TCouchbaseFlushResult;
var
Command: lcb_CMDFLUSH;
begin
FillChar(Command, SizeOf(Command), 0);
Result.Initialize;
if Success(lcb_flush3(FInstance, @Result, @Command)) then
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
end;
function TgoCouchbase.Stats: TCouchbaseStatsResult;
var
Command: lcb_CMDSTATS;
begin
FillChar(Command, SizeOf(Command), 0);
Result.Initialize;
if Success(lcb_stats3(FInstance, @Result, @Command)) then
lcb_wait3(FInstance, LCB_WAIT_NOCHECK);
end;
function TgoCouchbase.LookupIn(const AKey: Utf8String): ICouchbaseSubDoc;
begin
Result := TgoCouchbaseSubDoc.Create(Self, AKey, LCB_SDMULTI_MODE_LOOKUP);
end;
function TgoCouchbase.MutateIn(const AKey: Utf8String): ICouchbaseSubDoc;
begin
Result := TgoCouchbaseSubDoc.Create(Self, AKey, LCB_SDMULTI_MODE_MUTATE);
end;
{ TCouchbaseResult }
procedure TCouchbaseResult.Initialize;
begin
Success := False;
Error := 0;
Key := '';
Value := nil;
Format := TCouchbaseFormat.JSON;
Flags := 0;
CAS := 0;
Operation := 0;
Counter := 0;
end;
{ TCouchbaseSubDoc }
constructor TgoCouchbaseSubDoc.Create(const ACouchbase: TgoCouchbase;
const AKey: Utf8String; const AMultiMode: Integer);
begin
FCouchbase := ACouchbase;
FKey := AKey;
FMultiMode := AMultiMode;
end;
destructor TgoCouchbaseSubDoc.Destroy;
begin
inherited;
end;
function TgoCouchbaseSubDoc.Success(const AResult: Lcb_error_t): Boolean;
begin
FLastErrorCode := AResult;
if FLastErrorCode = LCB_SUCCESS then
begin
FLastErrorDesc := 'Success';
Result := True;
end
else
begin
FLastErrorDesc := String(lcb_strerror(nil, FLastErrorCode));
Result := False;
end;
end;
procedure TgoCouchbaseSubDoc.Append(const ACommand: Integer; const APath: Utf8String);
var
Spec: lcb_SDSPEC;
begin
FillChar(Spec, SizeOf(Spec), 0);
Spec.sdcmd := ACommand;
LCB_SDSPEC_SET_PATH(Spec, MarshaledAString(APath), Length(APath));
FSpecs := FSpecs + [Spec];
end;
procedure TgoCouchbaseSubDoc.Append(const ACommand: Integer; const APath: Utf8String;
const AValue: TBytes);
var
Spec: lcb_SDSPEC;
begin
FillChar(Spec, SizeOf(Spec), 0);
Spec.sdcmd := ACommand;
LCB_SDSPEC_SET_PATH(Spec, MarshaledAString(APath), Length(APath));
LCB_SDSPEC_SET_VALUE(Spec, AValue, Length(AValue));
FSpecs := FSpecs + [Spec];
end;
procedure TgoCouchbaseSubDoc.Append(const ACommand: Integer; const APath: Utf8String;
const AValue: TBytes; const ACreateParent: Boolean);
var
Spec: lcb_SDSPEC;
begin
FillChar(Spec, SizeOf(Spec), 0);
Spec.sdcmd := ACommand;
if ACreateParent then
Spec.options := LCB_SDSPEC_F_MKINTERMEDIATES;
LCB_SDSPEC_SET_PATH(Spec, MarshaledAString(APath), Length(APath));
LCB_SDSPEC_SET_VALUE(Spec, AValue, Length(AValue));
FSpecs := FSpecs + [Spec];
end;
function TgoCouchbaseSubDoc.Get(const APath: Utf8String): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_GET, APath);
Result := Self;
end;
function TgoCouchbaseSubDoc.Exists(const APath: Utf8String): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_EXISTS, APath);
Result := Self;
end;
function TgoCouchbaseSubDoc.GetCount(const APath: Utf8String): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_GET_COUNT, APath);
Result := Self;
end;
function TgoCouchbaseSubDoc.Upsert(const APath: Utf8String;
const AValue: TBytes; const ACreateParent: Boolean): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_DICT_UPSERT, APath, AValue, ACreateParent);
Result := Self;
end;
function TgoCouchbaseSubDoc.Upsert(const APath: Utf8String;
const AValue: Utf8String; const ACreateParent: Boolean): ICouchbaseSubDoc;
begin
Upsert(APath, TEncoding.UTF8.GetBytes(AValue), ACreateParent);
Result := Self;
end;
function TgoCouchbaseSubDoc.Insert(const APath: Utf8String;
const AValue: TBytes; const ACreateParent: Boolean): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_DICT_ADD, APath, AValue, ACreateParent);
Result := Self;
end;
function TgoCouchbaseSubDoc.Insert(const APath: Utf8String;
const AValue: Utf8String; const ACreateParent: Boolean): ICouchbaseSubDoc;
begin
Insert(APath, TEncoding.UTF8.GetBytes(AValue), ACreateParent);
Result := Self;
end;
function TgoCouchbaseSubDoc.Replace(const APath: Utf8String;
const AValue: TBytes): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_REPLACE, APath, AValue);
Result := Self;
end;
function TgoCouchbaseSubDoc.Replace(const APath: Utf8String;
const AValue: Utf8String): ICouchbaseSubDoc;
begin
Replace(APath, TEncoding.UTF8.GetBytes(AValue));
Result := Self;
end;
function TgoCouchbaseSubDoc.Remove(const APath: Utf8String): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_REMOVE, APath);
Result := Self;
end;
function TgoCouchbaseSubDoc.ArrayAppend(const APath: Utf8String;
const AValue: TBytes; const ACreateParent: Boolean): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_ARRAY_ADD_LAST, APath, AValue, ACreateParent);
Result := Self;
end;
function TgoCouchbaseSubDoc.ArrayAppend(const APath: Utf8String;
const AValue: Utf8String; const ACreateParent: Boolean): ICouchbaseSubDoc;
begin
ArrayAppend(APath, TEncoding.UTF8.GetBytes(AValue), ACreateParent);
Result := Self;
end;
function TgoCouchbaseSubDoc.ArrayPrepend(const APath: Utf8String;
const AValue: TBytes; const ACreateParent: Boolean): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_ARRAY_ADD_FIRST, APath, AValue, ACreateParent);
Result := Self;
end;
function TgoCouchbaseSubDoc.ArrayPrepend(const APath: Utf8String;
const AValue: Utf8String; const ACreateParent: Boolean): ICouchbaseSubDoc;
begin
ArrayPrepend(APath, TEncoding.UTF8.GetBytes(AValue), ACreateParent);
Result := Self;
end;
function TgoCouchbaseSubDoc.ArrayInsert(const APath: Utf8String;
const AValue: TBytes): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_ARRAY_INSERT, APath, AValue, False);
Result := Self;
end;
function TgoCouchbaseSubDoc.ArrayInsert(const APath: Utf8String;
const AValue: Utf8String): ICouchbaseSubDoc;
begin
ArrayInsert(APath, TEncoding.UTF8.GetBytes(AValue));
Result := Self;
end;
function TgoCouchbaseSubDoc.ArrayAddUnique(const APath: Utf8String;
const AValue: TBytes; const ACreateParent: Boolean): ICouchbaseSubDoc;
begin
Append(LCB_SDCMD_ARRAY_ADD_UNIQUE, APath, AValue, ACreateParent);
Result := Self;
end;
function TgoCouchbaseSubDoc.ArrayAddUnique(const APath: Utf8String;
const AValue: Utf8String; const ACreateParent: Boolean): ICouchbaseSubDoc;
begin
ArrayAddUnique(APath, TEncoding.UTF8.GetBytes(AValue), ACreateParent);