forked from tarantool/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
1550 lines (1382 loc) · 44 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"
"io"
"reflect"
"strings"
"sync"
"github.com/tarantool/go-iproto"
"github.com/vmihailenco/msgpack/v5"
)
type spaceEncoder struct {
Id uint32
Name string
IsId bool
}
func newSpaceEncoder(res SchemaResolver, spaceInfo interface{}) (spaceEncoder, error) {
if res.NamesUseSupported() {
if spaceName, ok := spaceInfo.(string); ok {
return spaceEncoder{
Id: 0,
Name: spaceName,
IsId: false,
}, nil
}
}
spaceId, err := res.ResolveSpace(spaceInfo)
return spaceEncoder{
Id: spaceId,
IsId: true,
}, err
}
func (e spaceEncoder) Encode(enc *msgpack.Encoder) error {
if e.IsId {
if err := enc.EncodeUint(uint64(iproto.IPROTO_SPACE_ID)); err != nil {
return err
}
return enc.EncodeUint(uint64(e.Id))
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_SPACE_NAME)); err != nil {
return err
}
return enc.EncodeString(e.Name)
}
type indexEncoder struct {
Id uint32
Name string
IsId bool
}
func newIndexEncoder(res SchemaResolver, indexInfo interface{},
spaceNo uint32) (indexEncoder, error) {
if res.NamesUseSupported() {
if indexName, ok := indexInfo.(string); ok {
return indexEncoder{
Name: indexName,
IsId: false,
}, nil
}
}
indexId, err := res.ResolveIndex(indexInfo, spaceNo)
return indexEncoder{
Id: indexId,
IsId: true,
}, err
}
func (e indexEncoder) Encode(enc *msgpack.Encoder) error {
if e.IsId {
if err := enc.EncodeUint(uint64(iproto.IPROTO_INDEX_ID)); err != nil {
return err
}
return enc.EncodeUint(uint64(e.Id))
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_INDEX_NAME)); err != nil {
return err
}
return enc.EncodeString(e.Name)
}
func fillSearch(enc *msgpack.Encoder, spaceEnc spaceEncoder, indexEnc indexEncoder,
key interface{}) error {
if err := spaceEnc.Encode(enc); err != nil {
return err
}
if err := indexEnc.Encode(enc); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_KEY)); err != nil {
return err
}
return enc.Encode(key)
}
func fillIterator(enc *msgpack.Encoder, offset, limit uint32, iterator Iter) error {
if err := enc.EncodeUint(uint64(iproto.IPROTO_ITERATOR)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iterator)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_OFFSET)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(offset)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_LIMIT)); err != nil {
return err
}
return enc.EncodeUint(uint64(limit))
}
func fillInsert(enc *msgpack.Encoder, spaceEnc spaceEncoder, tuple interface{}) error {
if err := enc.EncodeMapLen(2); err != nil {
return err
}
if err := spaceEnc.Encode(enc); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_TUPLE)); err != nil {
return err
}
return enc.Encode(tuple)
}
func fillSelect(enc *msgpack.Encoder, spaceEnc spaceEncoder, indexEnc indexEncoder,
offset, limit uint32, iterator Iter, key, after interface{}, fetchPos bool) error {
mapLen := 6
if fetchPos {
mapLen += 1
}
if after != nil {
mapLen += 1
}
if err := enc.EncodeMapLen(mapLen); err != nil {
return err
}
if err := fillIterator(enc, offset, limit, iterator); err != nil {
return err
}
if err := fillSearch(enc, spaceEnc, indexEnc, key); err != nil {
return err
}
if fetchPos {
if err := enc.EncodeUint(uint64(iproto.IPROTO_FETCH_POSITION)); err != nil {
return err
}
if err := enc.EncodeBool(fetchPos); err != nil {
return err
}
}
if after != nil {
if pos, ok := after.([]byte); ok {
if err := enc.EncodeUint(uint64(iproto.IPROTO_AFTER_POSITION)); err != nil {
return err
}
if err := enc.EncodeString(string(pos)); err != nil {
return err
}
} else {
if err := enc.EncodeUint(uint64(iproto.IPROTO_AFTER_TUPLE)); err != nil {
return err
}
if err := enc.Encode(after); err != nil {
return err
}
}
}
return nil
}
func fillUpdate(enc *msgpack.Encoder, spaceEnc spaceEncoder, indexEnc indexEncoder,
key interface{}, ops *Operations) error {
enc.EncodeMapLen(4)
if err := fillSearch(enc, spaceEnc, indexEnc, key); err != nil {
return err
}
enc.EncodeUint(uint64(iproto.IPROTO_TUPLE))
if ops == nil {
return enc.Encode([]interface{}{})
}
return enc.Encode(ops)
}
func fillUpsert(enc *msgpack.Encoder, spaceEnc spaceEncoder, tuple interface{},
ops *Operations) error {
enc.EncodeMapLen(3)
if err := spaceEnc.Encode(enc); err != nil {
return err
}
enc.EncodeUint(uint64(iproto.IPROTO_TUPLE))
if err := enc.Encode(tuple); err != nil {
return err
}
enc.EncodeUint(uint64(iproto.IPROTO_OPS))
if ops == nil {
return enc.Encode([]interface{}{})
}
return enc.Encode(ops)
}
func fillDelete(enc *msgpack.Encoder, spaceEnc spaceEncoder, indexEnc indexEncoder,
key interface{}) error {
enc.EncodeMapLen(3)
return fillSearch(enc, spaceEnc, indexEnc, key)
}
func fillCall(enc *msgpack.Encoder, functionName string, args interface{}) error {
enc.EncodeMapLen(2)
enc.EncodeUint(uint64(iproto.IPROTO_FUNCTION_NAME))
enc.EncodeString(functionName)
enc.EncodeUint(uint64(iproto.IPROTO_TUPLE))
return enc.Encode(args)
}
func fillEval(enc *msgpack.Encoder, expr string, args interface{}) error {
enc.EncodeMapLen(2)
enc.EncodeUint(uint64(iproto.IPROTO_EXPR))
enc.EncodeString(expr)
enc.EncodeUint(uint64(iproto.IPROTO_TUPLE))
return enc.Encode(args)
}
func fillExecute(enc *msgpack.Encoder, expr string, args interface{}) error {
enc.EncodeMapLen(2)
enc.EncodeUint(uint64(iproto.IPROTO_SQL_TEXT))
enc.EncodeString(expr)
enc.EncodeUint(uint64(iproto.IPROTO_SQL_BIND))
return encodeSQLBind(enc, args)
}
func fillPing(enc *msgpack.Encoder) error {
return enc.EncodeMapLen(0)
}
func fillWatchOnce(enc *msgpack.Encoder, key string) error {
if err := enc.EncodeMapLen(1); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_EVENT_KEY)); err != nil {
return err
}
return enc.EncodeString(key)
}
// Ping sends empty request to Tarantool to check connection.
//
// Deprecated: the method will be removed in the next major version,
// use a PingRequest object + Do() instead.
func (conn *Connection) Ping() ([]interface{}, error) {
return conn.Do(NewPingRequest()).Get()
}
// Select performs select to box space.
//
// It is equal to conn.SelectAsync(...).Get().
//
// Deprecated: the method will be removed in the next major version,
// use a SelectRequest object + Do() instead.
func (conn *Connection) Select(space, index interface{}, offset, limit uint32, iterator Iter,
key interface{}) ([]interface{}, 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().
//
// Deprecated: the method will be removed in the next major version,
// use an InsertRequest object + Do() instead.
func (conn *Connection) Insert(space interface{}, tuple interface{}) ([]interface{}, 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().
//
// Deprecated: the method will be removed in the next major version,
// use a ReplaceRequest object + Do() instead.
func (conn *Connection) Replace(space interface{}, tuple interface{}) ([]interface{}, 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().
//
// Deprecated: the method will be removed in the next major version,
// use a DeleteRequest object + Do() instead.
func (conn *Connection) Delete(space, index interface{}, key interface{}) ([]interface{}, 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().
//
// Deprecated: the method will be removed in the next major version,
// use a UpdateRequest object + Do() instead.
func (conn *Connection) Update(space, index, key interface{},
ops *Operations) ([]interface{}, 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().
//
// Deprecated: the method will be removed in the next major version,
// use a UpsertRequest object + Do() instead.
func (conn *Connection) Upsert(space, tuple interface{}, ops *Operations) ([]interface{}, error) {
return conn.UpsertAsync(space, tuple, ops).Get()
}
// Call calls registered Tarantool function.
// It uses request code for Tarantool >= 1.7, result is an array.
//
// It is equal to conn.CallAsync(functionName, args).Get().
//
// Deprecated: the method will be removed in the next major version,
// use a CallRequest object + Do() instead.
func (conn *Connection) Call(functionName string, args interface{}) ([]interface{}, error) {
return conn.CallAsync(functionName, args).Get()
}
// Call16 calls registered Tarantool function.
// It uses request code for Tarantool 1.6, result is an array of arrays.
// Deprecated since Tarantool 1.7.2.
//
// It is equal to conn.Call16Async(functionName, args).Get().
//
// Deprecated: the method will be removed in the next major version,
// use a Call16Request object + Do() instead.
func (conn *Connection) Call16(functionName string, args interface{}) ([]interface{}, error) {
return conn.Call16Async(functionName, args).Get()
}
// Call17 calls registered Tarantool function.
// It uses request code for Tarantool >= 1.7, result is an array.
//
// It is equal to conn.Call17Async(functionName, args).Get().
//
// Deprecated: the method will be removed in the next major version,
// use a Call17Request object + Do() instead.
func (conn *Connection) Call17(functionName string, args interface{}) ([]interface{}, error) {
return conn.Call17Async(functionName, args).Get()
}
// Eval passes Lua expression for evaluation.
//
// It is equal to conn.EvalAsync(space, tuple).Get().
//
// Deprecated: the method will be removed in the next major version,
// use an EvalRequest object + Do() instead.
func (conn *Connection) Eval(expr string, args interface{}) ([]interface{}, 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
//
// Deprecated: the method will be removed in the next major version,
// use an ExecuteRequest object + Do() instead.
func (conn *Connection) Execute(expr string, args interface{}) ([]interface{}, 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 *msgpack.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)
//
// Deprecated: the method will be removed in the next major version,
// use a SelectRequest object + Do() instead.
func (conn *Connection) GetTyped(space, index interface{}, key interface{},
result interface{}) error {
s := single{res: result}
return conn.SelectAsync(space, index, 0, 1, IterEq, key).GetTyped(&s)
}
// SelectTyped performs select to box space and fills typed result.
//
// It is equal to conn.SelectAsync(space, index, offset, limit, iterator, key).GetTyped(&result)
//
// Deprecated: the method will be removed in the next major version,
// use a SelectRequest object + Do() instead.
func (conn *Connection) SelectTyped(space, index interface{}, offset, limit uint32, iterator Iter,
key interface{}, result interface{}) 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).
//
// Deprecated: the method will be removed in the next major version,
// use an InsertRequest object + Do() instead.
func (conn *Connection) InsertTyped(space interface{}, tuple interface{},
result interface{}) 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).
//
// Deprecated: the method will be removed in the next major version,
// use a ReplaceRequest object + Do() instead.
func (conn *Connection) ReplaceTyped(space interface{}, tuple interface{},
result interface{}) 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).
//
// Deprecated: the method will be removed in the next major version,
// use a DeleteRequest object + Do() instead.
func (conn *Connection) DeleteTyped(space, index interface{}, key interface{},
result interface{}) 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).
//
// Deprecated: the method will be removed in the next major version,
// use a UpdateRequest object + Do() instead.
func (conn *Connection) UpdateTyped(space, index interface{}, key interface{},
ops *Operations, result interface{}) error {
return conn.UpdateAsync(space, index, key, ops).GetTyped(result)
}
// CallTyped calls registered function.
// It uses request code for Tarantool >= 1.7, result is an array.
//
// It is equal to conn.Call16Async(functionName, args).GetTyped(&result).
//
// Deprecated: the method will be removed in the next major version,
// use a CallRequest object + Do() instead.
func (conn *Connection) CallTyped(functionName string, args interface{},
result interface{}) error {
return conn.CallAsync(functionName, args).GetTyped(result)
}
// Call16Typed calls registered function.
// It uses request code for Tarantool 1.6, result is an array of arrays.
// Deprecated since Tarantool 1.7.2.
//
// It is equal to conn.Call16Async(functionName, args).GetTyped(&result).
//
// Deprecated: the method will be removed in the next major version,
// use a Call16Request object + Do() instead.
func (conn *Connection) Call16Typed(functionName string, args interface{},
result interface{}) error {
return conn.Call16Async(functionName, args).GetTyped(result)
}
// Call17Typed calls registered function.
// It uses request code for Tarantool >= 1.7, result is an array.
//
// It is equal to conn.Call17Async(functionName, args).GetTyped(&result).
//
// Deprecated: the method will be removed in the next major version,
// use a Call17Request object + Do() instead.
func (conn *Connection) Call17Typed(functionName string, args interface{},
result interface{}) error {
return conn.Call17Async(functionName, args).GetTyped(result)
}
// EvalTyped passes Lua expression for evaluation.
//
// It is equal to conn.EvalAsync(space, tuple).GetTyped(&result).
//
// Deprecated: the method will be removed in the next major version,
// use an EvalRequest object + Do() instead.
func (conn *Connection) EvalTyped(expr string, args interface{}, result interface{}) 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
//
// Deprecated: the method will be removed in the next major version,
// use an ExecuteRequest object + Do() instead.
func (conn *Connection) ExecuteTyped(expr string, args interface{},
result interface{}) (SQLInfo, []ColumnMetaData, error) {
var (
sqlInfo SQLInfo
metaData []ColumnMetaData
)
fut := conn.ExecuteAsync(expr, args)
err := fut.GetTyped(&result)
if resp, ok := fut.resp.(*ExecuteResponse); ok {
sqlInfo = resp.sqlInfo
metaData = resp.metaData
} else if err == nil {
err = fmt.Errorf("unexpected response type %T, want: *ExecuteResponse", fut.resp)
}
return sqlInfo, metaData, err
}
// SelectAsync sends select request to Tarantool and returns Future.
//
// Deprecated: the method will be removed in the next major version,
// use a SelectRequest object + Do() instead.
func (conn *Connection) SelectAsync(space, index interface{}, offset, limit uint32, iterator Iter,
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.
//
// Deprecated: the method will be removed in the next major version,
// use an InsertRequest object + Do() instead.
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.
//
// Deprecated: the method will be removed in the next major version,
// use a ReplaceRequest object + Do() instead.
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.
//
// Deprecated: the method will be removed in the next major version,
// use a DeleteRequest object + Do() instead.
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.
//
// Deprecated: the method will be removed in the next major version,
// use a UpdateRequest object + Do() instead.
func (conn *Connection) UpdateAsync(space, index interface{}, key interface{},
ops *Operations) *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.
//
// Deprecated: the method will be removed in the next major version,
// use a UpsertRequest object + Do() instead.
func (conn *Connection) UpsertAsync(space, tuple interface{}, ops *Operations) *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, so future's result is an array.
//
// Deprecated: the method will be removed in the next major version,
// use a CallRequest object + Do() instead.
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 an array of arrays.
// Deprecated since Tarantool 1.7.2.
//
// Deprecated: the method will be removed in the next major version,
// use a Call16Request object + Do() instead.
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 is an array.
//
// Deprecated: the method will be removed in the next major version,
// use a Call17Request object + Do() instead.
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.
//
// Deprecated: the method will be removed in the next major version,
// use an EvalRequest object + Do() instead.
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
//
// Deprecated: the method will be removed in the next major version,
// use an ExecuteRequest object + Do() instead.
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 *msgpack.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 {
// Type returns a IPROTO type of the request.
Type() iproto.Type
// Body fills an msgpack.Encoder with a request body.
Body(resolver SchemaResolver, enc *msgpack.Encoder) error
// Ctx returns a context of the request.
Ctx() context.Context
// Async returns true if the request does not expect response.
Async() bool
// Response creates a response for current request type.
Response(header Header, body io.Reader) (Response, error)
}
// 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 {
rtype iproto.Type
async bool
ctx context.Context
}
// Type returns a IPROTO type for the request.
func (req *baseRequest) Type() iproto.Type {
return req.rtype
}
// 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
}
// Response creates a response for the baseRequest.
func (req *baseRequest) Response(header Header, body io.Reader) (Response, error) {
return DecodeBaseResponse(header, body)
}
type spaceRequest struct {
baseRequest
space interface{}
}
func (req *spaceRequest) setSpace(space interface{}) {
req.space = space
}
func EncodeSpace(res SchemaResolver, enc *msgpack.Encoder, space interface{}) error {
spaceEnc, err := newSpaceEncoder(res, space)
if err != nil {
return err
}
if err := spaceEnc.Encode(enc); err != nil {
return err
}
return nil
}
type spaceIndexRequest struct {
spaceRequest
index interface{}
}
func (req *spaceIndexRequest) setIndex(index interface{}) {
req.index = index
}
// authRequest implements IPROTO_AUTH request.
type authRequest struct {
auth Auth
user, pass string
}
// newChapSha1AuthRequest create a new authRequest with chap-sha1
// authentication method.
func newChapSha1AuthRequest(user, password, salt string) (authRequest, error) {
req := authRequest{}
scr, err := scramble(salt, password)
if err != nil {
return req, fmt.Errorf("scrambling failure: %w", err)
}
req.auth = ChapSha1Auth
req.user = user
req.pass = string(scr)
return req, nil
}
// newPapSha256AuthRequest create a new authRequest with pap-sha256
// authentication method.
func newPapSha256AuthRequest(user, password string) authRequest {
return authRequest{
auth: PapSha256Auth,
user: user,
pass: password,
}
}
// Type returns a IPROTO type for the request.
func (req authRequest) Type() iproto.Type {
return iproto.IPROTO_AUTH
}
// Async returns true if the request does not require a response.
func (req authRequest) Async() bool {
return false
}
// Ctx returns a context of the request.
func (req authRequest) Ctx() context.Context {
return nil
}
// Body fills an encoder with the auth request body.
func (req authRequest) Body(res SchemaResolver, enc *msgpack.Encoder) error {
if err := enc.EncodeMapLen(2); err != nil {
return err
}
if err := enc.EncodeUint32(uint32(iproto.IPROTO_USER_NAME)); err != nil {
return err
}
if err := enc.EncodeString(req.user); err != nil {
return err
}
if err := enc.EncodeUint32(uint32(iproto.IPROTO_TUPLE)); err != nil {
return err
}
if err := enc.EncodeArrayLen(2); err != nil {
return err
}
if err := enc.EncodeString(req.auth.String()); err != nil {
return err
}
if err := enc.EncodeString(req.pass); err != nil {
return err
}
return nil
}
// Response creates a response for the authRequest.
func (req authRequest) Response(header Header, body io.Reader) (Response, error) {
return DecodeBaseResponse(header, body)
}
// 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.rtype = iproto.IPROTO_PING
return req
}
// Body fills an msgpack.Encoder with the ping request body.
func (req *PingRequest) Body(res SchemaResolver, enc *msgpack.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, fetchPos bool