-
Notifications
You must be signed in to change notification settings - Fork 199
/
command.go
2907 lines (2424 loc) · 76.4 KB
/
command.go
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
// Copyright 2014-2022 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aerospike
import (
"bytes"
"compress/zlib"
"encoding/binary"
"errors"
"fmt"
"time"
"github.com/aerospike/aerospike-client-go/v7/logger"
"github.com/aerospike/aerospike-client-go/v7/types"
"github.com/aerospike/aerospike-client-go/v7/types/pool"
ParticleType "github.com/aerospike/aerospike-client-go/v7/types/particle_type"
Buffer "github.com/aerospike/aerospike-client-go/v7/utils/buffer"
)
const (
// Flags commented out are not supported by cmd client.
// Contains a read operation.
_INFO1_READ int = (1 << 0)
// Get all bins.
_INFO1_GET_ALL int = (1 << 1)
// Short query
_INFO1_SHORT_QUERY int = (1 << 2)
// Batch read or exists.
_INFO1_BATCH int = (1 << 3)
// Do not read the bins
_INFO1_NOBINDATA int = (1 << 5)
// Involve all replicas in read operation.
_INFO1_READ_MODE_AP_ALL = (1 << 6)
// Tell server to compress its response.
_INFO1_COMPRESS_RESPONSE = (1 << 7)
// Create or update record
_INFO2_WRITE int = (1 << 0)
// Fling a record into the belly of Moloch.
_INFO2_DELETE int = (1 << 1)
// Update if expected generation == old.
_INFO2_GENERATION int = (1 << 2)
// Update if new generation >= old, good for restore.
_INFO2_GENERATION_GT int = (1 << 3)
// Transaction resulting in record deletion leaves tombstone (Enterprise only).
_INFO2_DURABLE_DELETE int = (1 << 4)
// Create only. Fail if record already exists.
_INFO2_CREATE_ONLY int = (1 << 5)
// Treat as long query, but relax read consistency.
_INFO2_RELAX_AP_LONG_QUERY = (1 << 6)
// Return a result for every operation.
_INFO2_RESPOND_ALL_OPS int = (1 << 7)
// This is the last of a multi-part message.
_INFO3_LAST int = (1 << 0)
// Commit to master only before declaring success.
_INFO3_COMMIT_MASTER int = (1 << 1)
// On send: Do not return partition done in scan/query.
// On receive: Specified partition is done in scan/query.
_INFO3_PARTITION_DONE int = (1 << 2)
// Update only. Merge bins.
_INFO3_UPDATE_ONLY int = (1 << 3)
// Create or completely replace record.
_INFO3_CREATE_OR_REPLACE int = (1 << 4)
// Completely replace existing record only.
_INFO3_REPLACE_ONLY int = (1 << 5)
// See Below
_INFO3_SC_READ_TYPE int = (1 << 6)
// See Below
_INFO3_SC_READ_RELAX int = (1 << 7)
// Interpret SC_READ bits in info3.
//
// RELAX TYPE
// strict
// ------
// 0 0 sequential (default)
// 0 1 linearize
//
// relaxed
// -------
// 1 0 allow replica
// 1 1 allow unavailable
_STATE_READ_AUTH_HEADER uint8 = 1
_STATE_READ_HEADER uint8 = 2
_STATE_READ_DETAIL uint8 = 3
_STATE_COMPLETE uint8 = 4
_BATCH_MSG_READ uint8 = 0x0
_BATCH_MSG_REPEAT uint8 = 0x1
_BATCH_MSG_INFO uint8 = 0x2
_BATCH_MSG_GEN uint8 = 0x4
_BATCH_MSG_TTL uint8 = 0x8
_MSG_TOTAL_HEADER_SIZE uint8 = 30
_FIELD_HEADER_SIZE uint8 = 5
_OPERATION_HEADER_SIZE uint8 = 8
_MSG_REMAINING_HEADER_SIZE uint8 = 22
_DIGEST_SIZE uint8 = 20
_COMPRESS_THRESHOLD int = 128
_CL_MSG_VERSION int64 = 2
_AS_MSG_TYPE int64 = 3
_AS_MSG_TYPE_COMPRESSED int64 = 4
)
type transactionType int
const (
ttNone transactionType = iota
ttGet
ttGetHeader
ttExists
ttPut
ttDelete
ttOperate
ttQuery
ttScan
ttUDF
ttBatchRead
ttBatchWrite
)
var (
buffPool = pool.NewTieredBufferPool(MinBufferSize, PoolCutOffBufferSize)
)
// command interface describes all commands available
type command interface {
getPolicy(ifc command) Policy
writeBuffer(ifc command) Error
getNode(ifc command) (*Node, Error)
getConnection(policy Policy) (*Connection, Error)
putConnection(conn *Connection)
parseResult(ifc command, conn *Connection) Error
parseRecordResults(ifc command, receiveSize int) (bool, Error)
prepareRetry(ifc command, isTimeout bool) bool
transactionType() transactionType
isRead() bool
execute(ifc command) Error
executeAt(ifc command, policy *BasePolicy, deadline time.Time, iterations int) Error
canPutConnBack() bool
// Executes the command
Execute() Error
}
// Holds data buffer for the command
type baseCommand struct {
bufferEx
node *Node
conn *Connection
// dataBufferCompress is not a second buffer; it is just a pointer to
// the beginning of the dataBuffer.
// To avoid allocating multiple buffers before compression, the dataBuffer
// will be referencing to a padded buffer. After the command is written to
// the buffer, this padding will be used to compress the command in-place,
// and then the compressed proto header will be written.
dataBufferCompress []byte
// oneShot determines if streaming commands like query, scan or queryAggregate
// are not retried if they error out mid-parsing
oneShot bool
// will determine if the buffer will be compressed
// before being sent to the server
compressed bool
commandSentCounter int
commandWasSent bool
}
// Writes the command for write operations
func (cmd *baseCommand) setWrite(policy *WritePolicy, operation OperationType, key *Key, bins []*Bin, binMap BinMap) Error {
cmd.begin()
fieldCount, err := cmd.estimateKeySize(key, policy.SendKey)
if err != nil {
return err
}
predSize := 0
if policy.FilterExpression != nil {
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
if binMap == nil {
for i := range bins {
if err := cmd.estimateOperationSizeForBin(bins[i]); err != nil {
return err
}
}
} else {
for name, value := range binMap {
if err := cmd.estimateOperationSizeForBinNameAndValue(name, value); err != nil {
return err
}
}
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
if binMap == nil {
cmd.writeHeaderWrite(policy, _INFO2_WRITE, fieldCount, len(bins))
} else {
cmd.writeHeaderWrite(policy, _INFO2_WRITE, fieldCount, len(binMap))
}
if err := cmd.writeKey(key, policy.SendKey); err != nil {
return err
}
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return err
}
}
if binMap == nil {
for i := range bins {
if err := cmd.writeOperationForBin(bins[i], operation); err != nil {
return err
}
}
} else {
for name, value := range binMap {
if err := cmd.writeOperationForBinNameAndValue(name, value, operation); err != nil {
return err
}
}
}
cmd.end()
cmd.markCompressed(policy)
return nil
}
// Writes the command for delete operations
func (cmd *baseCommand) setDelete(policy *WritePolicy, key *Key) Error {
cmd.begin()
fieldCount, err := cmd.estimateKeySize(key, false)
if err != nil {
return err
}
predSize := 0
if policy.FilterExpression != nil {
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
cmd.writeHeaderWrite(policy, _INFO2_WRITE|_INFO2_DELETE, fieldCount, 0)
if err := cmd.writeKey(key, false); err != nil {
return err
}
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return err
}
}
cmd.end()
cmd.markCompressed(policy)
return nil
}
// Writes the command for touch operations
func (cmd *baseCommand) setTouch(policy *WritePolicy, key *Key) Error {
cmd.begin()
fieldCount, err := cmd.estimateKeySize(key, policy.SendKey)
if err != nil {
return err
}
predSize := 0
if policy.FilterExpression != nil {
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
cmd.estimateOperationSize()
if err := cmd.sizeBuffer(false); err != nil {
return err
}
cmd.writeHeaderWrite(policy, _INFO2_WRITE, fieldCount, 1)
if err := cmd.writeKey(key, policy.SendKey); err != nil {
return err
}
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return err
}
}
cmd.writeOperationForOperationType(_TOUCH)
cmd.end()
return nil
}
// Writes the command for exist operations
func (cmd *baseCommand) setExists(policy *BasePolicy, key *Key) Error {
cmd.begin()
fieldCount, err := cmd.estimateKeySize(key, false)
if err != nil {
return err
}
predSize := 0
if policy.FilterExpression != nil {
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
if err := cmd.sizeBuffer(false); err != nil {
return err
}
cmd.writeHeaderReadHeader(policy, _INFO1_READ|_INFO1_NOBINDATA, fieldCount, 0)
if err := cmd.writeKey(key, false); err != nil {
return err
}
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return err
}
}
cmd.end()
return nil
}
// Writes the command for get operations (all bins)
func (cmd *baseCommand) setReadForKeyOnly(policy *BasePolicy, key *Key) Error {
cmd.begin()
fieldCount, err := cmd.estimateKeySize(key, false)
if err != nil {
return err
}
predSize := 0
if policy.FilterExpression != nil {
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
cmd.writeHeaderRead(policy, _INFO1_READ|_INFO1_GET_ALL, 0, 0, fieldCount, 0)
if err := cmd.writeKey(key, false); err != nil {
return err
}
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return err
}
}
cmd.end()
cmd.markCompressed(policy)
return nil
}
// Writes the command for get operations (specified bins)
func (cmd *baseCommand) setRead(policy *BasePolicy, key *Key, binNames []string) Error {
if len(binNames) > 0 {
cmd.begin()
fieldCount, err := cmd.estimateKeySize(key, false)
if err != nil {
return err
}
predSize := 0
if policy.FilterExpression != nil {
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
for i := range binNames {
cmd.estimateOperationSizeForBinName(binNames[i])
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
attr := _INFO1_READ
if len(binNames) == 0 {
attr |= _INFO1_GET_ALL
}
cmd.writeHeaderRead(policy, attr, 0, 0, fieldCount, len(binNames))
if err := cmd.writeKey(key, false); err != nil {
return err
}
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return err
}
}
for i := range binNames {
cmd.writeOperationForBinName(binNames[i], _READ)
}
cmd.end()
cmd.markCompressed(policy)
return nil
}
return cmd.setReadForKeyOnly(policy, key)
}
// Writes the command for getting metadata operations
func (cmd *baseCommand) setReadHeader(policy *BasePolicy, key *Key) Error {
cmd.begin()
fieldCount, err := cmd.estimateKeySize(key, false)
if err != nil {
return err
}
predSize := 0
if policy.FilterExpression != nil {
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
cmd.writeHeaderReadHeader(policy, _INFO1_READ|_INFO1_NOBINDATA, fieldCount, 0)
if err := cmd.writeKey(key, false); err != nil {
return err
}
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return err
}
}
cmd.end()
cmd.markCompressed(policy)
return nil
}
// Implements different command operations
func (cmd *baseCommand) setOperate(policy *WritePolicy, key *Key, args *operateArgs) Error {
if len(args.operations) == 0 {
return newError(types.PARAMETER_ERROR, "No operations were passed.")
}
cmd.begin()
fieldCount := 0
for i := range args.operations {
if err := cmd.estimateOperationSizeForOperation(args.operations[i], false); err != nil {
return err
}
}
ksz, err := cmd.estimateKeySize(key, policy.SendKey && args.hasWrite)
if err != nil {
return err
}
fieldCount += ksz
predSize := 0
if policy.FilterExpression != nil {
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
cmd.writeHeaderReadWrite(policy, args, fieldCount)
if err := cmd.writeKey(key, policy.SendKey && args.hasWrite); err != nil {
return err
}
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return err
}
}
for _, operation := range args.operations {
if err := cmd.writeOperationForOperation(operation); err != nil {
return err
}
}
cmd.end()
cmd.markCompressed(policy)
return nil
}
func (cmd *baseCommand) setUdf(policy *WritePolicy, key *Key, packageName string, functionName string, args *ValueArray) Error {
cmd.begin()
fieldCount, err := cmd.estimateKeySize(key, policy.SendKey)
if err != nil {
return err
}
predSize := 0
if policy.FilterExpression != nil {
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
fc, err := cmd.estimateUdfSize(packageName, functionName, args)
if err != nil {
return err
}
fieldCount += fc
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
cmd.writeHeaderWrite(policy, _INFO2_WRITE, fieldCount, 0)
if err := cmd.writeKey(key, policy.SendKey); err != nil {
return err
}
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return err
}
}
cmd.writeFieldString(packageName, UDF_PACKAGE_NAME)
cmd.writeFieldString(functionName, UDF_FUNCTION)
if err := cmd.writeUdfArgs(args); err != nil {
return err
}
cmd.end()
cmd.markCompressed(policy)
return nil
}
func (cmd *baseCommand) setBatchOperateIfc(client ClientIfc, policy *BatchPolicy, records []BatchRecordIfc, batch *batchNode) (*batchAttr, Error) {
offsets := batch.offsets
max := len(batch.offsets)
// Estimate buffer size
cmd.begin()
fieldCount := 1
predSize := 0
if policy.FilterExpression != nil {
var err Error
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return nil, err
}
if predSize > 0 {
fieldCount++
}
}
cmd.dataOffset += predSize
cmd.dataOffset += int(_FIELD_HEADER_SIZE) + 5
var prev BatchRecordIfc
for i := 0; i < max; i++ {
record := records[offsets[i]]
key := record.key()
cmd.dataOffset += len(key.digest) + 4
// Try reference equality in hope that namespace/set for all keys is set from fixed variables.
if !policy.SendKey && prev != nil && prev.key().namespace == key.namespace && (prev.key().setName == key.setName) && record.equals(prev) {
// Can set repeat previous namespace/bin names to save space.
cmd.dataOffset++
} else {
// Must write full header and namespace/set/bin names.
cmd.dataOffset += 12 // header(4) + ttl(4) + fielCount(2) + opCount(2) = 12
cmd.dataOffset += len(key.namespace) + int(_FIELD_HEADER_SIZE)
cmd.dataOffset += len(key.setName) + int(_FIELD_HEADER_SIZE)
if sz, err := record.size(&policy.BasePolicy); err != nil {
return nil, err
} else {
cmd.dataOffset += sz
}
prev = record
}
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return nil, err
}
cmd.writeBatchHeader(policy, fieldCount)
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return nil, err
}
}
// Write real field size.
fieldSizeOffset := cmd.dataOffset
cmd.writeFieldHeader(0, BATCH_INDEX)
cmd.WriteUint32(uint32(max))
cmd.WriteByte(cmd.getBatchFlags(policy))
attr := &batchAttr{}
prev = nil
for i := 0; i < max; i++ {
index := offsets[i]
cmd.WriteUint32(uint32(index))
record := records[index]
key := record.key()
if _, err := cmd.Write(key.digest[:]); err != nil {
return nil, newCommonError(err)
}
// Try reference equality in hope that namespace/set for all keys is set from fixed variables.
if !policy.SendKey && prev != nil && prev.key().namespace == key.namespace && prev.key().setName == key.setName && record.equals(prev) {
// Can set repeat previous namespace/bin names to save space.
cmd.WriteByte(_BATCH_MSG_REPEAT) // repeat
} else {
// Write full message.
switch record.getType() {
case _BRT_BATCH_READ:
br := record.(*BatchRead)
attr.setBatchRead(client.getUsableBatchReadPolicy(br.Policy))
if len(br.BinNames) > 0 {
cmd.writeBatchBinNames(key, br.BinNames, attr, attr.filterExp)
} else if br.Ops != nil {
attr.adjustRead(br.Ops)
cmd.writeBatchOperations(key, br.Ops, attr, attr.filterExp)
} else {
attr.adjustReadForAllBins(br.ReadAllBins)
cmd.writeBatchRead(key, attr, attr.filterExp, 0)
}
case _BRT_BATCH_WRITE:
bw := record.(*BatchWrite)
attr.setBatchWrite(client.getUsableBatchWritePolicy(bw.Policy))
attr.adjustWrite(bw.Ops)
cmd.writeBatchOperations(key, bw.Ops, attr, attr.filterExp)
case _BRT_BATCH_UDF:
bu := record.(*BatchUDF)
attr.setBatchUDF(client.getUsableBatchUDFPolicy(bu.Policy))
cmd.writeBatchWrite(key, attr, attr.filterExp, 3, 0)
cmd.writeFieldString(bu.PackageName, UDF_PACKAGE_NAME)
cmd.writeFieldString(bu.FunctionName, UDF_FUNCTION)
cmd.writeFieldBytes(bu.argBytes, UDF_ARGLIST)
case _BRT_BATCH_DELETE:
bd := record.(*BatchDelete)
attr.setBatchDelete(client.getUsableBatchDeletePolicy(bd.Policy))
cmd.writeBatchWrite(key, attr, attr.filterExp, 0, 0)
}
prev = record
}
}
cmd.WriteUint32At(uint32(cmd.dataOffset)-uint32(_MSG_TOTAL_HEADER_SIZE)-4, fieldSizeOffset)
cmd.end()
cmd.markCompressed(policy)
return attr, nil
}
func (cmd *baseCommand) setBatchOperate(policy *BatchPolicy, keys []*Key, batch *batchNode, binNames []string, ops []*Operation, attr *batchAttr) Error {
offsets := batch.offsets
max := len(batch.offsets)
// Estimate buffer size
cmd.begin()
fieldCount := 1
predSize := 0
exp := policy.FilterExpression
if attr.filterExp != nil {
exp = attr.filterExp
}
if exp != nil {
var err Error
predSize, err = cmd.estimateExpressionSize(exp)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
cmd.dataOffset += predSize
cmd.dataOffset += int(_FIELD_HEADER_SIZE) + 5
var prev *Key
for i := 0; i < max; i++ {
key := keys[offsets[i]]
cmd.dataOffset += len(key.digest) + 4
// Try reference equality in hope that namespace/set for all keys is set from fixed variables.
if !attr.sendKey && prev != nil && prev.namespace == key.namespace && (prev.setName == key.setName) {
// Can set repeat previous namespace/bin names to save space.
cmd.dataOffset++
} else {
// Must write full header and namespace/set/bin names.
cmd.dataOffset += 12 // header(4) + ttl(4) + fielCount(2) + opCount(2) = 12
cmd.dataOffset += len(key.namespace) + int(_FIELD_HEADER_SIZE)
cmd.dataOffset += len(key.setName) + int(_FIELD_HEADER_SIZE)
if attr.sendKey {
if sz, err := key.userKey.EstimateSize(); err != nil {
return err
} else {
cmd.dataOffset += sz + int(_FIELD_HEADER_SIZE) + 1
}
}
if len(binNames) > 0 {
for _, binName := range binNames {
cmd.estimateOperationSizeForBinName(binName)
}
} else if len(ops) > 0 {
for _, op := range ops {
if op.opType.isWrite {
if !attr.hasWrite {
return newError(types.PARAMETER_ERROR, "batch operation is write but isWrite flag not set in attrs")
}
cmd.dataOffset += 2 // Extra write specific fields.
}
if err := cmd.estimateOperationSizeForOperation(op, true); err != nil {
return err
}
}
} else if (attr.writeAttr & _INFO2_DELETE) != 0 {
cmd.dataOffset += 2 // Extra write specific fields.
}
prev = key
}
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
cmd.writeBatchHeader(policy, fieldCount)
if exp != nil {
if err := cmd.writeFilterExpression(exp, predSize); err != nil {
return err
}
}
// Write real field size.
fieldSizeOffset := cmd.dataOffset
cmd.writeFieldHeader(0, BATCH_INDEX)
cmd.WriteUint32(uint32(max))
cmd.WriteByte(cmd.getBatchFlags(policy))
prev = nil
for i := 0; i < max; i++ {
index := offsets[i]
cmd.WriteUint32(uint32(index))
key := keys[index]
if _, err := cmd.Write(key.digest[:]); err != nil {
return newCommonError(err)
}
// Try reference equality in hope that namespace/set for all keys is set from fixed variables.
if !attr.sendKey && prev != nil && prev.namespace == key.namespace && prev.setName == key.setName {
// Can set repeat previous namespace/bin names to save space.
cmd.WriteByte(_BATCH_MSG_REPEAT) // repeat
} else {
// Write full header, namespace and bin names.
if len(binNames) > 0 {
cmd.writeBatchBinNames(key, binNames, attr, nil)
} else if len(ops) > 0 {
cmd.writeBatchOperations(key, ops, attr, nil)
} else if (attr.writeAttr & _INFO2_DELETE) != 0 {
cmd.writeBatchWrite(key, attr, nil, 0, 0)
} else {
cmd.writeBatchRead(key, attr, nil, 0)
}
prev = key
}
}
cmd.WriteUint32At(uint32(cmd.dataOffset)-uint32(_MSG_TOTAL_HEADER_SIZE)-4, fieldSizeOffset)
cmd.end()
cmd.markCompressed(policy)
return nil
}
func (cmd *baseCommand) setBatchUDF(policy *BatchPolicy, keys []*Key, batch *batchNode, packageName, functionName string, args ValueArray, attr *batchAttr) Error {
offsets := batch.offsets
max := len(batch.offsets)
// Estimate buffer size
cmd.begin()
fieldCount := 1
predSize := 0
if policy.FilterExpression != nil {
var err Error
predSize, err = cmd.estimateExpressionSize(policy.FilterExpression)
if err != nil {
return err
}
if predSize > 0 {
fieldCount++
}
}
cmd.dataOffset += predSize
cmd.dataOffset += int(_FIELD_HEADER_SIZE) + 5
var prev *Key
for i := 0; i < max; i++ {
key := keys[offsets[i]]
cmd.dataOffset += len(key.digest) + 4
// Try reference equality in hope that namespace/set for all keys is set from fixed variables.
if !attr.sendKey && prev != nil && prev.namespace == key.namespace && (prev.setName == key.setName) {
// Can set repeat previous namespace/bin names to save space.
cmd.dataOffset++
} else {
// Must write full header and namespace/set/bin names.
cmd.dataOffset += 12 // header(4) + ttl(4) + fieldCount(2) + opCount(2) = 12
cmd.dataOffset += len(key.namespace) + int(_FIELD_HEADER_SIZE)
cmd.dataOffset += len(key.setName) + int(_FIELD_HEADER_SIZE)
if attr.sendKey {
if sz, err := key.userKey.EstimateSize(); err != nil {
return err
} else {
cmd.dataOffset += sz + int(_FIELD_HEADER_SIZE) + 1
}
}
cmd.dataOffset += 2 // gen(2) = 2
if sz, err := cmd.estimateUdfSize(packageName, functionName, &args); err != nil {
return err
} else {
cmd.dataOffset += sz
}
prev = key
}
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
cmd.writeBatchHeader(policy, fieldCount)
if policy.FilterExpression != nil {
if err := cmd.writeFilterExpression(policy.FilterExpression, predSize); err != nil {
return err
}
}
// Write real field size.
fieldSizeOffset := cmd.dataOffset
cmd.writeFieldHeader(0, BATCH_INDEX)
cmd.WriteUint32(uint32(max))
cmd.WriteByte(cmd.getBatchFlags(policy))
prev = nil
for i := 0; i < max; i++ {
index := offsets[i]
cmd.WriteUint32(uint32(index))
key := keys[index]
if _, err := cmd.Write(key.digest[:]); err != nil {
return newCommonError(err)
}
// Try reference equality in hope that namespace/set for all keys is set from fixed variables.
if !attr.sendKey && prev != nil && prev.namespace == key.namespace && prev.setName == key.setName {
// Can set repeat previous namespace/bin names to save space.
cmd.WriteByte(_BATCH_MSG_REPEAT) // repeat
} else {
cmd.writeBatchWrite(key, attr, nil, 3, 0)
cmd.writeFieldString(packageName, UDF_PACKAGE_NAME)
cmd.writeFieldString(functionName, UDF_FUNCTION)
if err := cmd.writeUdfArgs(&args); err != nil {
return err
}
prev = key
}
}
cmd.WriteUint32At(uint32(cmd.dataOffset)-uint32(_MSG_TOTAL_HEADER_SIZE)-4, fieldSizeOffset)
cmd.end()
cmd.markCompressed(policy)
// fmt.Printf("len: %d\n", len(cmd.dataBuffer[:cmd.dataOffset]))
// fmt.Printf("%s\n", hex.Dump(cmd.dataBuffer[:cmd.dataOffset]))
return nil
}
func (cmd *baseCommand) writeBatchHeader(policy *BatchPolicy, fieldCount int) {
readAttr := _INFO1_BATCH
if policy.UseCompression {