forked from tarantool/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
1133 lines (994 loc) · 33.1 KB
/
request.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
package tarantool
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"sync"
)
func fillSearch(enc *encoder, spaceNo, indexNo uint32, key interface{}) error {
encodeUint(enc, KeySpaceNo)
encodeUint(enc, uint64(spaceNo))
encodeUint(enc, KeyIndexNo)
encodeUint(enc, uint64(indexNo))
encodeUint(enc, KeyKey)
return enc.Encode(key)
}
func fillIterator(enc *encoder, offset, limit, iterator uint32) {
encodeUint(enc, KeyIterator)
encodeUint(enc, uint64(iterator))
encodeUint(enc, KeyOffset)
encodeUint(enc, uint64(offset))
encodeUint(enc, KeyLimit)
encodeUint(enc, uint64(limit))
}
func fillInsert(enc *encoder, spaceNo uint32, tuple interface{}) error {
enc.EncodeMapLen(2)
encodeUint(enc, KeySpaceNo)
encodeUint(enc, uint64(spaceNo))
encodeUint(enc, KeyTuple)
return enc.Encode(tuple)
}
func fillSelect(enc *encoder, spaceNo, indexNo, offset, limit, iterator uint32, key interface{}) error {
enc.EncodeMapLen(6)
fillIterator(enc, offset, limit, iterator)
return fillSearch(enc, spaceNo, indexNo, key)
}
func fillUpdate(enc *encoder, spaceNo, indexNo uint32, key, ops interface{}) error {
enc.EncodeMapLen(4)
if err := fillSearch(enc, spaceNo, indexNo, key); err != nil {
return err
}
encodeUint(enc, KeyTuple)
return enc.Encode(ops)
}
func fillUpsert(enc *encoder, spaceNo uint32, tuple, ops interface{}) error {
enc.EncodeMapLen(3)
encodeUint(enc, KeySpaceNo)
encodeUint(enc, uint64(spaceNo))
encodeUint(enc, KeyTuple)
if err := enc.Encode(tuple); err != nil {
return err
}
encodeUint(enc, KeyDefTuple)
return enc.Encode(ops)
}
func fillDelete(enc *encoder, spaceNo, indexNo uint32, key interface{}) error {
enc.EncodeMapLen(3)
return fillSearch(enc, spaceNo, indexNo, key)
}
func fillCall(enc *encoder, functionName string, args interface{}) error {
enc.EncodeMapLen(2)
encodeUint(enc, KeyFunctionName)
enc.EncodeString(functionName)
encodeUint(enc, KeyTuple)
return enc.Encode(args)
}
func fillEval(enc *encoder, expr string, args interface{}) error {
enc.EncodeMapLen(2)
encodeUint(enc, KeyExpression)
enc.EncodeString(expr)
encodeUint(enc, KeyTuple)
return enc.Encode(args)
}
func fillExecute(enc *encoder, expr string, args interface{}) error {
enc.EncodeMapLen(2)
encodeUint(enc, KeySQLText)
enc.EncodeString(expr)
encodeUint(enc, KeySQLBind)
return encodeSQLBind(enc, args)
}
func fillPing(enc *encoder) error {
return enc.EncodeMapLen(0)
}
// Ping sends empty request to Tarantool to check connection.
func (conn *Connection) Ping() (resp *Response, err error) {
return conn.Do(NewPingRequest()).Get()
}
// Select performs select to box space.
//
// It is equal to conn.SelectAsync(...).Get().
func (conn *Connection) Select(space, index interface{}, offset, limit, iterator uint32, key interface{}) (resp *Response, err error) {
return conn.SelectAsync(space, index, offset, limit, iterator, key).Get()
}
// Insert performs insertion to box space.
// Tarantool will reject Insert when tuple with same primary key exists.
//
// It is equal to conn.InsertAsync(space, tuple).Get().
func (conn *Connection) Insert(space interface{}, tuple interface{}) (resp *Response, err error) {
return conn.InsertAsync(space, tuple).Get()
}
// Replace performs "insert or replace" action to box space.
// If tuple with same primary key exists, it will be replaced.
//
// It is equal to conn.ReplaceAsync(space, tuple).Get().
func (conn *Connection) Replace(space interface{}, tuple interface{}) (resp *Response, err error) {
return conn.ReplaceAsync(space, tuple).Get()
}
// Delete performs deletion of a tuple by key.
// Result will contain array with deleted tuple.
//
// It is equal to conn.DeleteAsync(space, tuple).Get().
func (conn *Connection) Delete(space, index interface{}, key interface{}) (resp *Response, err error) {
return conn.DeleteAsync(space, index, key).Get()
}
// Update performs update of a tuple by key.
// Result will contain array with updated tuple.
//
// It is equal to conn.UpdateAsync(space, tuple).Get().
func (conn *Connection) Update(space, index interface{}, key, ops interface{}) (resp *Response, err error) {
return conn.UpdateAsync(space, index, key, ops).Get()
}
// Upsert performs "update or insert" action of a tuple by key.
// Result will not contain any tuple.
//
// It is equal to conn.UpsertAsync(space, tuple, ops).Get().
func (conn *Connection) Upsert(space interface{}, tuple, ops interface{}) (resp *Response, err error) {
return conn.UpsertAsync(space, tuple, ops).Get()
}
// Call calls registered Tarantool function.
// It uses request code for Tarantool >= 1.7 if go-tarantool
// was build with go_tarantool_call_17 tag.
// Otherwise, uses request code for Tarantool 1.6.
//
// It is equal to conn.CallAsync(functionName, args).Get().
func (conn *Connection) Call(functionName string, args interface{}) (resp *Response, err error) {
return conn.CallAsync(functionName, args).Get()
}
// Call16 calls registered Tarantool function.
// It uses request code for Tarantool 1.6, so result is converted to array of arrays
// Deprecated since Tarantool 1.7.2.
//
// It is equal to conn.Call16Async(functionName, args).Get().
func (conn *Connection) Call16(functionName string, args interface{}) (resp *Response, err error) {
return conn.Call16Async(functionName, args).Get()
}
// Call17 calls registered Tarantool function.
// It uses request code for Tarantool >= 1.7, so result is not converted
// (though, keep in mind, result is always array)
//
// It is equal to conn.Call17Async(functionName, args).Get().
func (conn *Connection) Call17(functionName string, args interface{}) (resp *Response, err error) {
return conn.Call17Async(functionName, args).Get()
}
// Eval passes Lua expression for evaluation.
//
// It is equal to conn.EvalAsync(space, tuple).Get().
func (conn *Connection) Eval(expr string, args interface{}) (resp *Response, err error) {
return conn.EvalAsync(expr, args).Get()
}
// Execute passes sql expression to Tarantool for execution.
//
// It is equal to conn.ExecuteAsync(expr, args).Get().
// Since 1.6.0
func (conn *Connection) Execute(expr string, args interface{}) (resp *Response, err error) {
return conn.ExecuteAsync(expr, args).Get()
}
// single used for conn.GetTyped for decode one tuple.
type single struct {
res interface{}
found bool
}
func (s *single) DecodeMsgpack(d *decoder) error {
var err error
var len int
if len, err = d.DecodeArrayLen(); err != nil {
return err
}
if s.found = len >= 1; !s.found {
return nil
}
if len != 1 {
return errors.New("Tarantool returns unexpected value for Select(limit=1)")
}
return d.Decode(s.res)
}
// GetTyped performs select (with limit = 1 and offset = 0)
// to box space and fills typed result.
//
// It is equal to conn.SelectAsync(space, index, 0, 1, IterEq, key).GetTyped(&result)
func (conn *Connection) GetTyped(space, index interface{}, key interface{}, result interface{}) (err error) {
s := single{res: result}
err = conn.SelectAsync(space, index, 0, 1, IterEq, key).GetTyped(&s)
return
}
// SelectTyped performs select to box space and fills typed result.
//
// It is equal to conn.SelectAsync(space, index, offset, limit, iterator, key).GetTyped(&result)
func (conn *Connection) SelectTyped(space, index interface{}, offset, limit, iterator uint32, key interface{}, result interface{}) (err error) {
return conn.SelectAsync(space, index, offset, limit, iterator, key).GetTyped(result)
}
// InsertTyped performs insertion to box space.
// Tarantool will reject Insert when tuple with same primary key exists.
//
// It is equal to conn.InsertAsync(space, tuple).GetTyped(&result).
func (conn *Connection) InsertTyped(space interface{}, tuple interface{}, result interface{}) (err error) {
return conn.InsertAsync(space, tuple).GetTyped(result)
}
// ReplaceTyped performs "insert or replace" action to box space.
// If tuple with same primary key exists, it will be replaced.
//
// It is equal to conn.ReplaceAsync(space, tuple).GetTyped(&result).
func (conn *Connection) ReplaceTyped(space interface{}, tuple interface{}, result interface{}) (err error) {
return conn.ReplaceAsync(space, tuple).GetTyped(result)
}
// DeleteTyped performs deletion of a tuple by key and fills result with deleted tuple.
//
// It is equal to conn.DeleteAsync(space, tuple).GetTyped(&result).
func (conn *Connection) DeleteTyped(space, index interface{}, key interface{}, result interface{}) (err error) {
return conn.DeleteAsync(space, index, key).GetTyped(result)
}
// UpdateTyped performs update of a tuple by key and fills result with updated tuple.
//
// It is equal to conn.UpdateAsync(space, tuple, ops).GetTyped(&result).
func (conn *Connection) UpdateTyped(space, index interface{}, key, ops interface{}, result interface{}) (err error) {
return conn.UpdateAsync(space, index, key, ops).GetTyped(result)
}
// CallTyped calls registered function.
// It uses request code for Tarantool >= 1.7 if go-tarantool
// was build with go_tarantool_call_17 tag.
// Otherwise, uses request code for Tarantool 1.6.
//
// It is equal to conn.Call16Async(functionName, args).GetTyped(&result).
func (conn *Connection) CallTyped(functionName string, args interface{}, result interface{}) (err error) {
return conn.CallAsync(functionName, args).GetTyped(result)
}
// Call16Typed calls registered function.
// It uses request code for Tarantool 1.6, so result is converted to array of arrays
// Deprecated since Tarantool 1.7.2.
//
// It is equal to conn.Call16Async(functionName, args).GetTyped(&result).
func (conn *Connection) Call16Typed(functionName string, args interface{}, result interface{}) (err error) {
return conn.Call16Async(functionName, args).GetTyped(result)
}
// Call17Typed calls registered function.
// It uses request code for Tarantool >= 1.7, so result is not converted
// (though, keep in mind, result is always array)
//
// It is equal to conn.Call17Async(functionName, args).GetTyped(&result).
func (conn *Connection) Call17Typed(functionName string, args interface{}, result interface{}) (err error) {
return conn.Call17Async(functionName, args).GetTyped(result)
}
// EvalTyped passes Lua expression for evaluation.
//
// It is equal to conn.EvalAsync(space, tuple).GetTyped(&result).
func (conn *Connection) EvalTyped(expr string, args interface{}, result interface{}) (err error) {
return conn.EvalAsync(expr, args).GetTyped(result)
}
// ExecuteTyped passes sql expression to Tarantool for execution.
//
// In addition to error returns sql info and columns meta data
// Since 1.6.0
func (conn *Connection) ExecuteTyped(expr string, args interface{}, result interface{}) (SQLInfo, []ColumnMetaData, error) {
fut := conn.ExecuteAsync(expr, args)
err := fut.GetTyped(&result)
return fut.resp.SQLInfo, fut.resp.MetaData, err
}
// SelectAsync sends select request to Tarantool and returns Future.
func (conn *Connection) SelectAsync(space, index interface{}, offset, limit, iterator uint32, key interface{}) *Future {
req := NewSelectRequest(space).
Index(index).
Offset(offset).
Limit(limit).
Iterator(iterator).
Key(key)
return conn.Do(req)
}
// InsertAsync sends insert action to Tarantool and returns Future.
// Tarantool will reject Insert when tuple with same primary key exists.
func (conn *Connection) InsertAsync(space interface{}, tuple interface{}) *Future {
req := NewInsertRequest(space).Tuple(tuple)
return conn.Do(req)
}
// ReplaceAsync sends "insert or replace" action to Tarantool and returns Future.
// If tuple with same primary key exists, it will be replaced.
func (conn *Connection) ReplaceAsync(space interface{}, tuple interface{}) *Future {
req := NewReplaceRequest(space).Tuple(tuple)
return conn.Do(req)
}
// DeleteAsync sends deletion action to Tarantool and returns Future.
// Future's result will contain array with deleted tuple.
func (conn *Connection) DeleteAsync(space, index interface{}, key interface{}) *Future {
req := NewDeleteRequest(space).Index(index).Key(key)
return conn.Do(req)
}
// Update sends deletion of a tuple by key and returns Future.
// Future's result will contain array with updated tuple.
func (conn *Connection) UpdateAsync(space, index interface{}, key, ops interface{}) *Future {
req := NewUpdateRequest(space).Index(index).Key(key)
req.ops = ops
return conn.Do(req)
}
// UpsertAsync sends "update or insert" action to Tarantool and returns Future.
// Future's sesult will not contain any tuple.
func (conn *Connection) UpsertAsync(space interface{}, tuple interface{}, ops interface{}) *Future {
req := NewUpsertRequest(space).Tuple(tuple)
req.ops = ops
return conn.Do(req)
}
// CallAsync sends a call to registered Tarantool function and returns Future.
// It uses request code for Tarantool >= 1.7 if go-tarantool
// was build with go_tarantool_call_17 tag.
// Otherwise, uses request code for Tarantool 1.6.
func (conn *Connection) CallAsync(functionName string, args interface{}) *Future {
req := NewCallRequest(functionName).Args(args)
return conn.Do(req)
}
// Call16Async sends a call to registered Tarantool function and returns Future.
// It uses request code for Tarantool 1.6, so future's result is always array of arrays.
// Deprecated since Tarantool 1.7.2.
func (conn *Connection) Call16Async(functionName string, args interface{}) *Future {
req := NewCall16Request(functionName).Args(args)
return conn.Do(req)
}
// Call17Async sends a call to registered Tarantool function and returns Future.
// It uses request code for Tarantool >= 1.7, so future's result will not be converted
// (though, keep in mind, result is always array)
func (conn *Connection) Call17Async(functionName string, args interface{}) *Future {
req := NewCall17Request(functionName).Args(args)
return conn.Do(req)
}
// EvalAsync sends a Lua expression for evaluation and returns Future.
func (conn *Connection) EvalAsync(expr string, args interface{}) *Future {
req := NewEvalRequest(expr).Args(args)
return conn.Do(req)
}
// ExecuteAsync sends a sql expression for execution and returns Future.
// Since 1.6.0
func (conn *Connection) ExecuteAsync(expr string, args interface{}) *Future {
req := NewExecuteRequest(expr).Args(args)
return conn.Do(req)
}
// KeyValueBind is a type for encoding named SQL parameters
type KeyValueBind struct {
Key string
Value interface{}
}
//
// private
//
// this map is needed for caching names of struct fields in lower case
// to avoid extra allocations in heap by calling strings.ToLower()
var lowerCaseNames sync.Map
func encodeSQLBind(enc *encoder, from interface{}) error {
// internal function for encoding single map in msgpack
encodeKeyInterface := func(key string, val interface{}) error {
if err := enc.EncodeMapLen(1); err != nil {
return err
}
if err := enc.EncodeString(":" + key); err != nil {
return err
}
if err := enc.Encode(val); err != nil {
return err
}
return nil
}
encodeKeyValue := func(key string, val reflect.Value) error {
if err := enc.EncodeMapLen(1); err != nil {
return err
}
if err := enc.EncodeString(":" + key); err != nil {
return err
}
if err := enc.EncodeValue(val); err != nil {
return err
}
return nil
}
encodeNamedFromMap := func(mp map[string]interface{}) error {
if err := enc.EncodeArrayLen(len(mp)); err != nil {
return err
}
for k, v := range mp {
if err := encodeKeyInterface(k, v); err != nil {
return err
}
}
return nil
}
encodeNamedFromStruct := func(val reflect.Value) error {
if err := enc.EncodeArrayLen(val.NumField()); err != nil {
return err
}
cached, ok := lowerCaseNames.Load(val.Type())
if !ok {
fields := make([]string, val.NumField())
for i := 0; i < val.NumField(); i++ {
key := val.Type().Field(i).Name
fields[i] = strings.ToLower(key)
v := val.Field(i)
if err := encodeKeyValue(fields[i], v); err != nil {
return err
}
}
lowerCaseNames.Store(val.Type(), fields)
return nil
}
fields := cached.([]string)
for i := 0; i < val.NumField(); i++ {
k := fields[i]
v := val.Field(i)
if err := encodeKeyValue(k, v); err != nil {
return err
}
}
return nil
}
encodeSlice := func(from interface{}) error {
castedSlice, ok := from.([]interface{})
if !ok {
castedKVSlice := from.([]KeyValueBind)
t := len(castedKVSlice)
if err := enc.EncodeArrayLen(t); err != nil {
return err
}
for _, v := range castedKVSlice {
if err := encodeKeyInterface(v.Key, v.Value); err != nil {
return err
}
}
return nil
}
if err := enc.EncodeArrayLen(len(castedSlice)); err != nil {
return err
}
for i := 0; i < len(castedSlice); i++ {
if kvb, ok := castedSlice[i].(KeyValueBind); ok {
k := kvb.Key
v := kvb.Value
if err := encodeKeyInterface(k, v); err != nil {
return err
}
} else {
if err := enc.Encode(castedSlice[i]); err != nil {
return err
}
}
}
return nil
}
val := reflect.ValueOf(from)
switch val.Kind() {
case reflect.Map:
mp, ok := from.(map[string]interface{})
if !ok {
return errors.New("failed to encode map: wrong format")
}
if err := encodeNamedFromMap(mp); err != nil {
return err
}
case reflect.Struct:
if err := encodeNamedFromStruct(val); err != nil {
return err
}
case reflect.Slice, reflect.Array:
if err := encodeSlice(from); err != nil {
return err
}
}
return nil
}
// Request is an interface that provides the necessary data to create a request
// that will be sent to a tarantool instance.
type Request interface {
// Code returns a IPROTO code for the request.
Code() int32
// Body fills an encoder with a request body.
Body(resolver SchemaResolver, enc *encoder) error
// Ctx returns a context of the request.
Ctx() context.Context
// Async returns true if the request does not expect response.
Async() bool
}
// ConnectedRequest is an interface that provides the info about a Connection
// the request belongs to.
type ConnectedRequest interface {
Request
// Conn returns a Connection the request belongs to.
Conn() *Connection
}
type baseRequest struct {
requestCode int32
async bool
ctx context.Context
}
// Code returns a IPROTO code for the request.
func (req *baseRequest) Code() int32 {
return req.requestCode
}
// Async returns true if the request does not require a response.
func (req *baseRequest) Async() bool {
return req.async
}
// Ctx returns a context of the request.
func (req *baseRequest) Ctx() context.Context {
return req.ctx
}
type spaceRequest struct {
baseRequest
space interface{}
}
func (req *spaceRequest) setSpace(space interface{}) {
req.space = space
}
type spaceIndexRequest struct {
spaceRequest
index interface{}
}
func (req *spaceIndexRequest) setIndex(index interface{}) {
req.index = index
}
type authRequest struct {
baseRequest
auth Auth
user, pass string
}
func newChapSha1AuthRequest(user, salt, password string) (*authRequest, error) {
scr, err := scramble(salt, password)
if err != nil {
return nil, fmt.Errorf("scrambling failure: %w", err)
}
req := new(authRequest)
req.requestCode = AuthRequestCode
req.auth = ChapSha1Auth
req.user = user
req.pass = string(scr)
return req, nil
}
func newPapSha256AuthRequest(user, password string) *authRequest {
req := new(authRequest)
req.requestCode = AuthRequestCode
req.auth = PapSha256Auth
req.user = user
req.pass = password
return req
}
// Body fills an encoder with the auth request body.
func (req *authRequest) Body(res SchemaResolver, enc *encoder) error {
return enc.Encode(map[uint32]interface{}{
KeyUserName: req.user,
KeyTuple: []interface{}{req.auth.String(), req.pass},
})
}
// PingRequest helps you to create an execute request object for execution
// by a Connection.
type PingRequest struct {
baseRequest
}
// NewPingRequest returns a new PingRequest.
func NewPingRequest() *PingRequest {
req := new(PingRequest)
req.requestCode = PingRequestCode
return req
}
// Body fills an encoder with the ping request body.
func (req *PingRequest) Body(res SchemaResolver, enc *encoder) error {
return fillPing(enc)
}
// Context sets a passed context to the request.
//
// Pay attention that when using context with request objects,
// the timeout option for Connection does not affect the lifetime
// of the request. For those purposes use context.WithTimeout() as
// the root context.
func (req *PingRequest) Context(ctx context.Context) *PingRequest {
req.ctx = ctx
return req
}
// SelectRequest allows you to create a select request object for execution
// by a Connection.
type SelectRequest struct {
spaceIndexRequest
isIteratorSet bool
offset, limit, iterator uint32
key interface{}
}
// NewSelectRequest returns a new empty SelectRequest.
func NewSelectRequest(space interface{}) *SelectRequest {
req := new(SelectRequest)
req.requestCode = SelectRequestCode
req.setSpace(space)
req.isIteratorSet = false
req.iterator = IterAll
req.key = []interface{}{}
req.limit = 0xFFFFFFFF
return req
}
// Index sets the index for the select request.
// Note: default value is 0.
func (req *SelectRequest) Index(index interface{}) *SelectRequest {
req.setIndex(index)
return req
}
// Offset sets the offset for the select request.
// Note: default value is 0.
func (req *SelectRequest) Offset(offset uint32) *SelectRequest {
req.offset = offset
return req
}
// Limit sets the limit for the select request.
// Note: default value is 0xFFFFFFFF.
func (req *SelectRequest) Limit(limit uint32) *SelectRequest {
req.limit = limit
return req
}
// Iterator set the iterator for the select request.
// Note: default value is IterAll if key is not set or IterEq otherwise.
func (req *SelectRequest) Iterator(iterator uint32) *SelectRequest {
req.iterator = iterator
req.isIteratorSet = true
return req
}
// Key set the key for the select request.
// Note: default value is empty.
func (req *SelectRequest) Key(key interface{}) *SelectRequest {
req.key = key
if !req.isIteratorSet {
req.iterator = IterEq
}
return req
}
// Body fills an encoder with the select request body.
func (req *SelectRequest) Body(res SchemaResolver, enc *encoder) error {
spaceNo, indexNo, err := res.ResolveSpaceIndex(req.space, req.index)
if err != nil {
return err
}
return fillSelect(enc, spaceNo, indexNo, req.offset, req.limit, req.iterator, req.key)
}
// Context sets a passed context to the request.
//
// Pay attention that when using context with request objects,
// the timeout option for Connection does not affect the lifetime
// of the request. For those purposes use context.WithTimeout() as
// the root context.
func (req *SelectRequest) Context(ctx context.Context) *SelectRequest {
req.ctx = ctx
return req
}
// InsertRequest helps you to create an insert request object for execution
// by a Connection.
type InsertRequest struct {
spaceRequest
tuple interface{}
}
// NewInsertRequest returns a new empty InsertRequest.
func NewInsertRequest(space interface{}) *InsertRequest {
req := new(InsertRequest)
req.requestCode = InsertRequestCode
req.setSpace(space)
req.tuple = []interface{}{}
return req
}
// Tuple sets the tuple for insertion the insert request.
// Note: default value is nil.
func (req *InsertRequest) Tuple(tuple interface{}) *InsertRequest {
req.tuple = tuple
return req
}
// Body fills an encoder with the insert request body.
func (req *InsertRequest) Body(res SchemaResolver, enc *encoder) error {
spaceNo, _, err := res.ResolveSpaceIndex(req.space, nil)
if err != nil {
return err
}
return fillInsert(enc, spaceNo, req.tuple)
}
// Context sets a passed context to the request.
//
// Pay attention that when using context with request objects,
// the timeout option for Connection does not affect the lifetime
// of the request. For those purposes use context.WithTimeout() as
// the root context.
func (req *InsertRequest) Context(ctx context.Context) *InsertRequest {
req.ctx = ctx
return req
}
// ReplaceRequest helps you to create a replace request object for execution
// by a Connection.
type ReplaceRequest struct {
spaceRequest
tuple interface{}
}
// NewReplaceRequest returns a new empty ReplaceRequest.
func NewReplaceRequest(space interface{}) *ReplaceRequest {
req := new(ReplaceRequest)
req.requestCode = ReplaceRequestCode
req.setSpace(space)
req.tuple = []interface{}{}
return req
}
// Tuple sets the tuple for replace by the replace request.
// Note: default value is nil.
func (req *ReplaceRequest) Tuple(tuple interface{}) *ReplaceRequest {
req.tuple = tuple
return req
}
// Body fills an encoder with the replace request body.
func (req *ReplaceRequest) Body(res SchemaResolver, enc *encoder) error {
spaceNo, _, err := res.ResolveSpaceIndex(req.space, nil)
if err != nil {
return err
}
return fillInsert(enc, spaceNo, req.tuple)
}
// Context sets a passed context to the request.
//
// Pay attention that when using context with request objects,
// the timeout option for Connection does not affect the lifetime
// of the request. For those purposes use context.WithTimeout() as
// the root context.
func (req *ReplaceRequest) Context(ctx context.Context) *ReplaceRequest {
req.ctx = ctx
return req
}
// DeleteRequest helps you to create a delete request object for execution
// by a Connection.
type DeleteRequest struct {
spaceIndexRequest
key interface{}
}
// NewDeleteRequest returns a new empty DeleteRequest.
func NewDeleteRequest(space interface{}) *DeleteRequest {
req := new(DeleteRequest)
req.requestCode = DeleteRequestCode
req.setSpace(space)
req.key = []interface{}{}
return req
}
// Index sets the index for the delete request.
// Note: default value is 0.
func (req *DeleteRequest) Index(index interface{}) *DeleteRequest {
req.setIndex(index)
return req
}
// Key sets the key of tuple for the delete request.
// Note: default value is empty.
func (req *DeleteRequest) Key(key interface{}) *DeleteRequest {
req.key = key
return req
}
// Body fills an encoder with the delete request body.
func (req *DeleteRequest) Body(res SchemaResolver, enc *encoder) error {
spaceNo, indexNo, err := res.ResolveSpaceIndex(req.space, req.index)
if err != nil {
return err
}
return fillDelete(enc, spaceNo, indexNo, req.key)
}
// Context sets a passed context to the request.
//
// Pay attention that when using context with request objects,
// the timeout option for Connection does not affect the lifetime
// of the request. For those purposes use context.WithTimeout() as
// the root context.
func (req *DeleteRequest) Context(ctx context.Context) *DeleteRequest {
req.ctx = ctx
return req
}
// UpdateRequest helps you to create an update request object for execution
// by a Connection.
type UpdateRequest struct {
spaceIndexRequest
key interface{}
ops interface{}
}
// NewUpdateRequest returns a new empty UpdateRequest.
func NewUpdateRequest(space interface{}) *UpdateRequest {
req := new(UpdateRequest)
req.requestCode = UpdateRequestCode
req.setSpace(space)
req.key = []interface{}{}
req.ops = []interface{}{}
return req
}
// Index sets the index for the update request.
// Note: default value is 0.
func (req *UpdateRequest) Index(index interface{}) *UpdateRequest {
req.setIndex(index)
return req
}
// Key sets the key of tuple for the update request.
// Note: default value is empty.
func (req *UpdateRequest) Key(key interface{}) *UpdateRequest {
req.key = key
return req
}
// Operations sets operations to be performed on update.
// Note: default value is empty.
func (req *UpdateRequest) Operations(ops *Operations) *UpdateRequest {
if ops != nil {
req.ops = ops.ops
}
return req
}
// Body fills an encoder with the update request body.
func (req *UpdateRequest) Body(res SchemaResolver, enc *encoder) error {
spaceNo, indexNo, err := res.ResolveSpaceIndex(req.space, req.index)
if err != nil {
return err
}
return fillUpdate(enc, spaceNo, indexNo, req.key, req.ops)
}
// Context sets a passed context to the request.
//
// Pay attention that when using context with request objects,
// the timeout option for Connection does not affect the lifetime
// of the request. For those purposes use context.WithTimeout() as
// the root context.
func (req *UpdateRequest) Context(ctx context.Context) *UpdateRequest {
req.ctx = ctx
return req
}
// UpsertRequest helps you to create an upsert request object for execution
// by a Connection.
type UpsertRequest struct {
spaceRequest
tuple interface{}
ops interface{}
}
// NewUpsertRequest returns a new empty UpsertRequest.
func NewUpsertRequest(space interface{}) *UpsertRequest {
req := new(UpsertRequest)
req.requestCode = UpsertRequestCode
req.setSpace(space)
req.tuple = []interface{}{}
req.ops = []interface{}{}
return req
}
// Tuple sets the tuple for insertion or update by the upsert request.
// Note: default value is empty.
func (req *UpsertRequest) Tuple(tuple interface{}) *UpsertRequest {
req.tuple = tuple
return req
}
// Operations sets operations to be performed on update case by the upsert request.
// Note: default value is empty.
func (req *UpsertRequest) Operations(ops *Operations) *UpsertRequest {
if ops != nil {
req.ops = ops.ops
}
return req
}
// Body fills an encoder with the upsert request body.
func (req *UpsertRequest) Body(res SchemaResolver, enc *encoder) error {
spaceNo, _, err := res.ResolveSpaceIndex(req.space, nil)
if err != nil {
return err
}
return fillUpsert(enc, spaceNo, req.tuple, req.ops)
}
// Context sets a passed context to the request.
//
// Pay attention that when using context with request objects,
// the timeout option for Connection does not affect the lifetime
// of the request. For those purposes use context.WithTimeout() as
// the root context.
func (req *UpsertRequest) Context(ctx context.Context) *UpsertRequest {
req.ctx = ctx
return req
}
// CallRequest helps you to create a call request object for execution
// by a Connection.
type CallRequest struct {
baseRequest
function string