@@ -5,29 +5,17 @@ import (
5
5
"reflect"
6
6
"strings"
7
7
"sync"
8
- "time"
9
8
10
9
"gopkg.in/vmihailenco/msgpack.v2"
11
10
)
12
11
13
- // Future is a handle for asynchronous request.
14
- type Future struct {
15
- requestId uint32
16
- requestCode int32
17
- timeout time.Duration
18
- resp * Response
19
- err error
20
- ready chan struct {}
21
- next * Future
22
- }
23
-
24
12
// Ping sends empty request to Tarantool to check connection.
25
13
func (conn * Connection ) Ping () (resp * Response , err error ) {
26
14
future := conn .newFuture (PingRequest )
27
15
return future .send (conn , func (enc * msgpack.Encoder ) error { enc .EncodeMapLen (0 ); return nil }).Get ()
28
16
}
29
17
30
- func ( req * Future ) fillSearch (enc * msgpack.Encoder , spaceNo , indexNo uint32 , key interface {}) error {
18
+ func fillSearch (enc * msgpack.Encoder , spaceNo , indexNo uint32 , key interface {}) error {
31
19
enc .EncodeUint64 (KeySpaceNo )
32
20
enc .EncodeUint64 (uint64 (spaceNo ))
33
21
enc .EncodeUint64 (KeyIndexNo )
@@ -36,7 +24,7 @@ func (req *Future) fillSearch(enc *msgpack.Encoder, spaceNo, indexNo uint32, key
36
24
return enc .Encode (key )
37
25
}
38
26
39
- func ( req * Future ) fillIterator (enc * msgpack.Encoder , offset , limit , iterator uint32 ) {
27
+ func fillIterator (enc * msgpack.Encoder , offset , limit , iterator uint32 ) {
40
28
enc .EncodeUint64 (KeyIterator )
41
29
enc .EncodeUint64 (uint64 (iterator ))
42
30
enc .EncodeUint64 (KeyOffset )
@@ -45,7 +33,7 @@ func (req *Future) fillIterator(enc *msgpack.Encoder, offset, limit, iterator ui
45
33
enc .EncodeUint64 (uint64 (limit ))
46
34
}
47
35
48
- func ( req * Future ) fillInsert (enc * msgpack.Encoder , spaceNo uint32 , tuple interface {}) error {
36
+ func fillInsert (enc * msgpack.Encoder , spaceNo uint32 , tuple interface {}) error {
49
37
enc .EncodeUint64 (KeySpaceNo )
50
38
enc .EncodeUint64 (uint64 (spaceNo ))
51
39
enc .EncodeUint64 (KeyTuple )
@@ -242,8 +230,8 @@ func (conn *Connection) SelectAsync(space, index interface{}, offset, limit, ite
242
230
}
243
231
return future .send (conn , func (enc * msgpack.Encoder ) error {
244
232
enc .EncodeMapLen (6 )
245
- future . fillIterator (enc , offset , limit , iterator )
246
- return future . fillSearch (enc , spaceNo , indexNo , key )
233
+ fillIterator (enc , offset , limit , iterator )
234
+ return fillSearch (enc , spaceNo , indexNo , key )
247
235
})
248
236
}
249
237
@@ -257,7 +245,7 @@ func (conn *Connection) InsertAsync(space interface{}, tuple interface{}) *Futur
257
245
}
258
246
return future .send (conn , func (enc * msgpack.Encoder ) error {
259
247
enc .EncodeMapLen (2 )
260
- return future . fillInsert (enc , spaceNo , tuple )
248
+ return fillInsert (enc , spaceNo , tuple )
261
249
})
262
250
}
263
251
@@ -271,7 +259,7 @@ func (conn *Connection) ReplaceAsync(space interface{}, tuple interface{}) *Futu
271
259
}
272
260
return future .send (conn , func (enc * msgpack.Encoder ) error {
273
261
enc .EncodeMapLen (2 )
274
- return future . fillInsert (enc , spaceNo , tuple )
262
+ return fillInsert (enc , spaceNo , tuple )
275
263
})
276
264
}
277
265
@@ -285,7 +273,7 @@ func (conn *Connection) DeleteAsync(space, index interface{}, key interface{}) *
285
273
}
286
274
return future .send (conn , func (enc * msgpack.Encoder ) error {
287
275
enc .EncodeMapLen (3 )
288
- return future . fillSearch (enc , spaceNo , indexNo , key )
276
+ return fillSearch (enc , spaceNo , indexNo , key )
289
277
})
290
278
}
291
279
@@ -299,7 +287,7 @@ func (conn *Connection) UpdateAsync(space, index interface{}, key, ops interface
299
287
}
300
288
return future .send (conn , func (enc * msgpack.Encoder ) error {
301
289
enc .EncodeMapLen (4 )
302
- if err := future . fillSearch (enc , spaceNo , indexNo , key ); err != nil {
290
+ if err := fillSearch (enc , spaceNo , indexNo , key ); err != nil {
303
291
return err
304
292
}
305
293
enc .EncodeUint64 (KeyTuple )
@@ -520,116 +508,3 @@ func encodeSQLBind(enc *msgpack.Encoder, from interface{}) error {
520
508
}
521
509
return nil
522
510
}
523
-
524
- func (fut * Future ) pack (h * smallWBuf , enc * msgpack.Encoder , body func (* msgpack.Encoder ) error ) (err error ) {
525
- rid := fut .requestId
526
- hl := h .Len ()
527
- h .Write ([]byte {
528
- 0xce , 0 , 0 , 0 , 0 , // length
529
- 0x82 , // 2 element map
530
- KeyCode , byte (fut .requestCode ), // request code
531
- KeySync , 0xce ,
532
- byte (rid >> 24 ), byte (rid >> 16 ),
533
- byte (rid >> 8 ), byte (rid ),
534
- })
535
-
536
- if err = body (enc ); err != nil {
537
- return
538
- }
539
-
540
- l := uint32 (h .Len () - 5 - hl )
541
- h .b [hl + 1 ] = byte (l >> 24 )
542
- h .b [hl + 2 ] = byte (l >> 16 )
543
- h .b [hl + 3 ] = byte (l >> 8 )
544
- h .b [hl + 4 ] = byte (l )
545
-
546
- return
547
- }
548
-
549
- func (fut * Future ) send (conn * Connection , body func (* msgpack.Encoder ) error ) * Future {
550
- if fut .ready == nil {
551
- return fut
552
- }
553
- conn .putFuture (fut , body )
554
- return fut
555
- }
556
-
557
- func (fut * Future ) markReady (conn * Connection ) {
558
- close (fut .ready )
559
- if conn .rlimit != nil {
560
- <- conn .rlimit
561
- }
562
- }
563
-
564
- func (fut * Future ) fail (conn * Connection , err error ) * Future {
565
- if f := conn .fetchFuture (fut .requestId ); f == fut {
566
- f .err = err
567
- fut .markReady (conn )
568
- }
569
- return fut
570
- }
571
-
572
- func (fut * Future ) wait () {
573
- if fut .ready == nil {
574
- return
575
- }
576
- <- fut .ready
577
- }
578
-
579
- // Get waits for Future to be filled and returns Response and error.
580
- //
581
- // Response will contain deserialized result in Data field.
582
- // It will be []interface{}, so if you want more performace, use GetTyped method.
583
- //
584
- // Note: Response could be equal to nil if ClientError is returned in error.
585
- //
586
- // "error" could be Error, if it is error returned by Tarantool,
587
- // or ClientError, if something bad happens in a client process.
588
- func (fut * Future ) Get () (* Response , error ) {
589
- fut .wait ()
590
- if fut .err != nil {
591
- return fut .resp , fut .err
592
- }
593
- fut .err = fut .resp .decodeBody ()
594
- return fut .resp , fut .err
595
- }
596
-
597
- // GetTyped waits for Future and calls msgpack.Decoder.Decode(result) if no error happens.
598
- // It is could be much faster than Get() function.
599
- //
600
- // Note: Tarantool usually returns array of tuples (except for Eval and Call17 actions)
601
- func (fut * Future ) GetTyped (result interface {}) error {
602
- fut .wait ()
603
- if fut .err != nil {
604
- return fut .err
605
- }
606
- fut .err = fut .resp .decodeBodyTyped (result )
607
- return fut .err
608
- }
609
-
610
- var closedChan = make (chan struct {})
611
-
612
- func init () {
613
- close (closedChan )
614
- }
615
-
616
- // WaitChan returns channel which becomes closed when response arrived or error occured.
617
- func (fut * Future ) WaitChan () <- chan struct {} {
618
- if fut .ready == nil {
619
- return closedChan
620
- }
621
- return fut .ready
622
- }
623
-
624
- // Err returns error set on Future.
625
- // It waits for future to be set.
626
- // Note: it doesn't decode body, therefore decoding error are not set here.
627
- func (fut * Future ) Err () error {
628
- fut .wait ()
629
- return fut .err
630
- }
631
-
632
- // NewErrorFuture returns new set empty Future with filled error field.
633
- func NewErrorFuture (err error ) * Future {
634
- return & Future {err : err }
635
- }
0 commit comments