This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
1981 lines (1641 loc) · 50.5 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 2013-2020 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"
"io"
"math"
"net"
"time"
. "github.com/aerospike/aerospike-client-go/logger"
. "github.com/aerospike/aerospike-client-go/types"
ParticleType "github.com/aerospike/aerospike-client-go/internal/particle_type"
Buffer "github.com/aerospike/aerospike-client-go/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)
// 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)
// 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)
// 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
_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
)
// 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
execute(ifc command, isRead bool) error
executeAt(ifc command, policy *BasePolicy, isRead bool, deadline time.Time, iterations, commandSentCounter int) error
// Executes the command
Execute() error
}
// Holds data buffer for the command
type baseCommand struct {
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
dataBuffer []byte
dataOffset int
// 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
}
// 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 len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
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.writeHeaderWithPolicy(policy, 0, _INFO2_WRITE, fieldCount, len(bins))
} else {
cmd.writeHeaderWithPolicy(policy, 0, _INFO2_WRITE, fieldCount, len(binMap))
}
cmd.writeKey(key, policy.SendKey)
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, 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 len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
fieldCount++
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
cmd.writeHeaderWithPolicy(policy, 0, _INFO2_WRITE|_INFO2_DELETE, fieldCount, 0)
cmd.writeKey(key, false)
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, 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 len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
fieldCount++
}
cmd.estimateOperationSize()
if err := cmd.sizeBuffer(false); err != nil {
return err
}
cmd.writeHeaderWithPolicy(policy, 0, _INFO2_WRITE, fieldCount, 1)
cmd.writeKey(key, policy.SendKey)
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, 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 len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
fieldCount++
}
if err := cmd.sizeBuffer(false); err != nil {
return err
}
cmd.writeHeader(policy, _INFO1_READ|_INFO1_NOBINDATA, 0, fieldCount, 0)
cmd.writeKey(key, false)
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, 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 len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
fieldCount++
}
if err := cmd.sizeBuffer(false); err != nil {
return err
}
cmd.writeHeader(policy, _INFO1_READ|_INFO1_GET_ALL, 0, fieldCount, 0)
cmd.writeKey(key, false)
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, predSize); err != nil {
return err
}
}
cmd.end()
return nil
}
// Writes the command for get operations (specified bins)
func (cmd *baseCommand) setRead(policy *BasePolicy, key *Key, binNames []string) (err error) {
if len(binNames) > 0 {
cmd.begin()
fieldCount, err := cmd.estimateKeySize(key, false)
if err != nil {
return err
}
predSize := 0
if len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
fieldCount++
}
for i := range binNames {
cmd.estimateOperationSizeForBinName(binNames[i])
}
if err = cmd.sizeBuffer(false); err != nil {
return nil
}
cmd.writeHeader(policy, _INFO1_READ, 0, fieldCount, len(binNames))
cmd.writeKey(key, false)
if len(policy.PredExp) > 0 {
cmd.writePredExp(policy.PredExp, predSize)
}
for i := range binNames {
cmd.writeOperationForBinName(binNames[i], _READ)
}
cmd.end()
} else {
err = cmd.setReadForKeyOnly(policy, key)
}
return err
}
// 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 len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
fieldCount++
}
cmd.estimateOperationSizeForBinName("")
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
cmd.writeHeader(policy, _INFO1_READ|_INFO1_NOBINDATA, 0, fieldCount, 1)
cmd.writeKey(key, false)
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, predSize); err != nil {
return err
}
}
cmd.writeOperationForBinName("", _READ)
cmd.end()
return nil
}
// Implements different command operations
func (cmd *baseCommand) setOperate(policy *WritePolicy, key *Key, operations []*Operation) (bool, error) {
if len(operations) == 0 {
return false, NewAerospikeError(PARAMETER_ERROR, "No operations were passed.")
}
cmd.begin()
fieldCount := 0
readAttr := 0
writeAttr := 0
hasWrite := false
readBin := false
readHeader := false
RespondPerEachOp := policy.RespondPerEachOp
for i := range operations {
switch operations[i].opType {
case _BIT_READ:
fallthrough
case _HLL_READ:
fallthrough
case _MAP_READ:
// Map operations require RespondPerEachOp to be true.
RespondPerEachOp = true
// Fall through to read.
fallthrough
case _READ, _CDT_READ:
if !operations[i].headerOnly {
readAttr |= _INFO1_READ
// Read all bins if no bin is specified.
if operations[i].binName == "" {
readAttr |= _INFO1_GET_ALL
}
readBin = true
} else {
readAttr |= _INFO1_READ
readHeader = true
}
case _BIT_MODIFY:
fallthrough
case _HLL_MODIFY:
fallthrough
case _MAP_MODIFY:
// Map operations require RespondPerEachOp to be true.
RespondPerEachOp = true
// Fall through to default.
fallthrough
default:
writeAttr = _INFO2_WRITE
hasWrite = true
}
cmd.estimateOperationSizeForOperation(operations[i])
}
ksz, err := cmd.estimateKeySize(key, policy.SendKey && hasWrite)
if err != nil {
return hasWrite, err
}
fieldCount += ksz
predSize := 0
if len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
fieldCount++
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return hasWrite, err
}
if readHeader && !readBin {
readAttr |= _INFO1_NOBINDATA
}
if RespondPerEachOp {
writeAttr |= _INFO2_RESPOND_ALL_OPS
}
if writeAttr != 0 {
cmd.writeHeaderWithPolicy(policy, readAttr, writeAttr, fieldCount, len(operations))
} else {
cmd.writeHeader(&policy.BasePolicy, readAttr, writeAttr, fieldCount, len(operations))
}
cmd.writeKey(key, policy.SendKey && hasWrite)
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, predSize); err != nil {
return hasWrite, err
}
}
for _, operation := range operations {
if err := cmd.writeOperationForOperation(operation); err != nil {
return hasWrite, err
}
}
cmd.end()
cmd.markCompressed(policy)
return hasWrite, 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 len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
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.writeHeaderWithPolicy(policy, 0, _INFO2_WRITE, fieldCount, 0)
cmd.writeKey(key, policy.SendKey)
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, predSize); err != nil {
return err
}
}
cmd.writeFieldString(packageName, UDF_PACKAGE_NAME)
cmd.writeFieldString(functionName, UDF_FUNCTION)
cmd.writeUdfArgs(args)
cmd.end()
cmd.markCompressed(policy)
return nil
}
func (cmd *baseCommand) setBatchIndexReadCompat(policy *BatchPolicy, keys []*Key, batch *batchNode, binNames []string, readAttr int) error {
offsets := batch.offsets
max := len(batch.offsets)
fieldCountRow := 1
if policy.SendSetName {
fieldCountRow = 2
}
binNameSize := 0
operationCount := len(binNames)
for _, binName := range binNames {
binNameSize += len(binName) + int(_OPERATION_HEADER_SIZE)
}
// Estimate buffer size
cmd.begin()
fieldCount := 1
predSize := 0
if len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
fieldCount++
}
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 prev != nil && prev.namespace == key.namespace &&
(!policy.SendSetName || 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 += len(key.namespace) + int(_FIELD_HEADER_SIZE) + 6
if policy.SendSetName {
cmd.dataOffset += len(key.setName) + int(_FIELD_HEADER_SIZE)
}
cmd.dataOffset += binNameSize
prev = key
}
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
if policy.ReadModeAP == ReadModeAPAll {
readAttr |= _INFO1_READ_MODE_AP_ALL
}
if len(binNames) == 0 {
readAttr |= _INFO1_GET_ALL
}
cmd.writeHeader(&policy.BasePolicy, readAttr|_INFO1_BATCH, 0, fieldCount, 0)
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, predSize); err != nil {
return err
}
}
// Write real field size.
fieldSizeOffset := cmd.dataOffset
if policy.SendSetName {
cmd.writeFieldHeader(0, BATCH_INDEX_WITH_SET)
} else {
cmd.writeFieldHeader(0, BATCH_INDEX)
}
cmd.WriteUint32(uint32(max))
if policy.AllowInline {
cmd.WriteByte(1)
} else {
cmd.WriteByte(0)
}
prev = nil
for i := 0; i < max; i++ {
index := offsets[i]
cmd.WriteUint32(uint32(index))
key := keys[index]
cmd.Write(key.digest[:])
// Try reference equality in hope that namespace/set for all keys is set from fixed variables.
if prev != nil && prev.namespace == key.namespace &&
(!policy.SendSetName || prev.setName == key.setName) {
// Can set repeat previous namespace/bin names to save space.
cmd.WriteByte(1) // repeat
} else {
// Write full header, namespace and bin names.
cmd.WriteByte(0) // do not repeat
cmd.WriteByte(byte(readAttr))
cmd.WriteUint16(uint16(fieldCountRow))
cmd.WriteUint16(uint16(operationCount))
cmd.writeFieldString(key.namespace, NAMESPACE)
if policy.SendSetName {
cmd.writeFieldString(key.setName, TABLE)
}
for _, binName := range binNames {
cmd.writeOperationForBinName(binName, _READ)
}
prev = key
}
}
cmd.WriteUint32At(uint32(cmd.dataOffset)-uint32(_MSG_TOTAL_HEADER_SIZE)-4, fieldSizeOffset)
cmd.end()
cmd.markCompressed(policy)
return nil
}
func (cmd *baseCommand) setBatchIndexRead(policy *BatchPolicy, records []*BatchRead, batch *batchNode) error {
offsets := batch.offsets
max := len(batch.offsets)
fieldCountRow := 1
if policy.SendSetName {
fieldCountRow = 2
}
// Estimate buffer size
cmd.begin()
fieldCount := 1
predSize := 0
if len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
fieldCount++
}
cmd.dataOffset += int(_FIELD_HEADER_SIZE) + 5
var prev *BatchRead
for i := 0; i < max; i++ {
record := records[offsets[i]]
key := record.Key
binNames := record.BinNames
cmd.dataOffset += len(key.digest) + 4
// Try reference equality in hope that namespace/set for all keys is set from fixed variables.
if prev != nil && prev.Key.namespace == key.namespace &&
(!policy.SendSetName || prev.Key.setName == key.setName) &&
&prev.BinNames == &binNames && prev.ReadAllBins == record.ReadAllBins {
// Can set repeat previous namespace/bin names to save space.
cmd.dataOffset++
} else {
// Must write full header and namespace/set/bin names.
cmd.dataOffset += len(key.namespace) + int(_FIELD_HEADER_SIZE) + 6
if policy.SendSetName {
cmd.dataOffset += len(key.setName) + int(_FIELD_HEADER_SIZE)
}
if len(binNames) != 0 {
for _, binName := range binNames {
cmd.estimateOperationSizeForBinName(binName)
}
}
prev = record
}
}
if err := cmd.sizeBuffer(policy.compress()); err != nil {
return err
}
readAttr := _INFO1_READ
if policy.ReadModeAP == ReadModeAPAll {
readAttr |= _INFO1_READ_MODE_AP_ALL
}
cmd.writeHeader(&policy.BasePolicy, readAttr|_INFO1_BATCH, 0, fieldCount, 0)
// cmd.writeHeader(&policy.BasePolicy, _INFO1_READ|_INFO1_BATCH, 0, 1, 0)
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, predSize); err != nil {
return err
}
}
// Write real field size.
fieldSizeOffset := cmd.dataOffset
if policy.SendSetName {
cmd.writeFieldHeader(0, BATCH_INDEX_WITH_SET)
} else {
cmd.writeFieldHeader(0, BATCH_INDEX)
}
cmd.WriteUint32(uint32(max))
if policy.AllowInline {
cmd.WriteByte(1)
} else {
cmd.WriteByte(0)
}
prev = nil
for i := 0; i < max; i++ {
index := offsets[i]
cmd.WriteUint32(uint32(index))
record := records[index]
key := record.Key
binNames := record.BinNames
cmd.Write(key.digest[:])
// Try reference equality in hope that namespace/set for all keys is set from fixed variables.
if prev != nil && prev.Key.namespace == key.namespace &&
(!policy.SendSetName || prev.Key.setName == key.setName) &&
&prev.BinNames == &binNames && prev.ReadAllBins == record.ReadAllBins {
// Can set repeat previous namespace/bin names to save space.
cmd.WriteByte(1) // repeat
} else {
// Write full header, namespace and bin names.
cmd.WriteByte(0) // do not repeat
if len(binNames) > 0 {
cmd.WriteByte(byte(readAttr))
cmd.WriteUint16(uint16(fieldCountRow))
cmd.WriteUint16(uint16(len(binNames)))
cmd.writeFieldString(key.namespace, NAMESPACE)
if policy.SendSetName {
cmd.writeFieldString(key.setName, TABLE)
}
for _, binName := range binNames {
cmd.writeOperationForBinName(binName, _READ)
}
} else {
attr := byte(readAttr)
if record.ReadAllBins {
attr |= byte(_INFO1_GET_ALL)
} else {
attr |= byte(_INFO1_NOBINDATA)
}
cmd.WriteByte(attr)
cmd.WriteUint16(uint16(fieldCountRow))
cmd.WriteUint16(0)
cmd.writeFieldString(key.namespace, NAMESPACE)
if policy.SendSetName {
cmd.writeFieldString(key.setName, TABLE)
}
}
prev = record
}
}
cmd.WriteUint32At(uint32(cmd.dataOffset)-uint32(_MSG_TOTAL_HEADER_SIZE)-4, fieldSizeOffset)
cmd.end()
cmd.markCompressed(policy)
return nil
}
func (cmd *baseCommand) setScan(policy *ScanPolicy, namespace *string, setName *string, binNames []string, taskID uint64) error {
cmd.begin()
fieldCount := 0
predSize := 0
if len(policy.PredExp) > 0 {
predSize = cmd.estimatePredExpSize(policy.PredExp)
fieldCount++
}
if namespace != nil {
cmd.dataOffset += len(*namespace) + int(_FIELD_HEADER_SIZE)
fieldCount++
}
if setName != nil {
cmd.dataOffset += len(*setName) + int(_FIELD_HEADER_SIZE)
fieldCount++
}
if policy.RecordsPerSecond > 0 {
cmd.dataOffset += 4 + int(_FIELD_HEADER_SIZE)
fieldCount++
}
// Estimate scan options size.
cmd.dataOffset += 2 + int(_FIELD_HEADER_SIZE)
fieldCount++
// Estimate scan timeout size.
cmd.dataOffset += 4 + int(_FIELD_HEADER_SIZE)
fieldCount++
// Allocate space for TaskId field.
cmd.dataOffset += 8 + int(_FIELD_HEADER_SIZE)
fieldCount++
if binNames != nil {
for i := range binNames {
cmd.estimateOperationSizeForBinName(binNames[i])
}
}
if err := cmd.sizeBuffer(false); err != nil {
return err
}
readAttr := _INFO1_READ
if !policy.IncludeBinData {
readAttr |= _INFO1_NOBINDATA
}
operationCount := 0
if binNames != nil {
operationCount = len(binNames)
}
cmd.writeHeader(&policy.BasePolicy, readAttr, 0, fieldCount, operationCount)
if namespace != nil {
cmd.writeFieldString(*namespace, NAMESPACE)
}
if setName != nil {
cmd.writeFieldString(*setName, TABLE)
}
if len(policy.PredExp) > 0 {
if err := cmd.writePredExp(policy.PredExp, predSize); err != nil {
return err
}
}
if policy.RecordsPerSecond > 0 {
cmd.writeFieldInt32(int32(policy.RecordsPerSecond), RECORDS_PER_SECOND)
}
cmd.writeFieldHeader(2, SCAN_OPTIONS)
priority := byte(policy.Priority)
priority <<= 4
if policy.FailOnClusterChange {
priority |= 0x08
}
cmd.WriteByte(priority)
cmd.WriteByte(byte(policy.ScanPercent))
// Write scan timeout
cmd.writeFieldHeader(4, SCAN_TIMEOUT)
cmd.WriteInt32(int32(policy.SocketTimeout / time.Millisecond)) // in milliseconds
cmd.writeFieldHeader(8, TRAN_ID)
cmd.WriteUint64(taskID)
if binNames != nil {
for i := range binNames {
cmd.writeOperationForBinName(binNames[i], _READ)
}
}
cmd.end()
return nil
}
func (cmd *baseCommand) setQuery(policy *QueryPolicy, wpolicy *WritePolicy, statement *Statement, operations []*Operation, write bool) (err error) {
fieldCount := 0
filterSize := 0
binNameSize := 0
predSize := 0
predExp := statement.predExps
recordsPerSecond := 0
if !write {
recordsPerSecond = policy.RecordsPerSecond
}
cmd.begin()
if statement.Namespace != "" {
cmd.dataOffset += len(statement.Namespace) + int(_FIELD_HEADER_SIZE)
fieldCount++
}
if statement.IndexName != "" {
cmd.dataOffset += len(statement.IndexName) + int(_FIELD_HEADER_SIZE)
fieldCount++
}
if statement.SetName != "" {
cmd.dataOffset += len(statement.SetName) + int(_FIELD_HEADER_SIZE)
fieldCount++
}
// Allocate space for TaskId field.
cmd.dataOffset += 8 + int(_FIELD_HEADER_SIZE)
fieldCount++
if statement.Filter != nil {
idxType := statement.Filter.IndexCollectionType()
if idxType != ICT_DEFAULT {
cmd.dataOffset += int(_FIELD_HEADER_SIZE) + 1
fieldCount++
}
cmd.dataOffset += int(_FIELD_HEADER_SIZE)
filterSize++ // num filters
sz, err := statement.Filter.EstimateSize()
if err != nil {
return err
}
filterSize += sz
cmd.dataOffset += filterSize
fieldCount++
// Query bin names are specified as a field (Scan bin names are specified later as operations)
if len(statement.BinNames) > 0 {
cmd.dataOffset += int(_FIELD_HEADER_SIZE)
binNameSize++ // num bin names
for _, binName := range statement.BinNames {
binNameSize += len(binName) + 1
}
cmd.dataOffset += binNameSize
fieldCount++
}
} else {
// Calling query with no filters is more efficiently handled by a primary index scan.
// Estimate scan options size.
cmd.dataOffset += (2 + int(_FIELD_HEADER_SIZE))
fieldCount++
// Estimate scan timeout size.
cmd.dataOffset += (4 + int(_FIELD_HEADER_SIZE))
fieldCount++
// Estimate records per second size.
if recordsPerSecond > 0 {
cmd.dataOffset += 4 + int(_FIELD_HEADER_SIZE)