v_rusty_tarantool 0.3.2

Tarantul async client based on tokio framework
Documentation
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
pub use crate::tarantool::packets::{CommandPacket, TarantoolRequest, TarantoolResponse, TarantoolSqlResponse};
pub use crate::tarantool::tools::{serialize_to_vec_u8, serialize_array};
use futures_channel::mpsc;
use futures_channel::oneshot;
use serde::Serialize;
use std::io;
use std::sync::{Arc, Mutex, RwLock};

pub mod codec;
mod dispatch;
pub mod packets;
mod tools;

use crate::tarantool::dispatch::{
    CallbackSender, Dispatch, ERROR_CLIENT_DISCONNECTED, ERROR_DISPATCH_THREAD_IS_DEAD,
};
pub use crate::tarantool::dispatch::{ClientConfig, ClientStatus};
use rmpv::Value;

impl ClientConfig {
    pub fn build(self) -> Client {
        Client::new(self)
    }
}

///iterator types
pub enum IteratorType {
    EQ = 0 , // key == x ASC order
    REQ = 1, // key == x DESC order
    ALL = 2, // all tuples
    LT = 3, // key <  x
    LE = 4, // key <= x
    GE = 5, // key >= x
    GT = 6, // key >  x
    BitsAllSet = 7, // all bits from x are set in key
    BitsAnySet = 8, // at least one x's bit is set
    BitsAllNotSet = 9, // all bits are not set
    OVERLAPS = 10, // key overlaps x
    NEIGHBOR = 11,
}

///
/// API to tarantool.
///
/// Create client by call build() on ClientConfig.
///
/// # Examples
///
/// let client = ClientConfig::new(addr, "rust", "rust").set_timeout_time_ms(1000).set_reconnect_time_ms(10000).build();
///
#[derive(Clone)]
pub struct Client {
    command_sender: mpsc::UnboundedSender<(CommandPacket, CallbackSender)>,
    dispatch: Arc<Mutex<Option<Dispatch>>>,
    status: Arc<RwLock<ClientStatus>>,
    notify_callbacks: Arc<Mutex<Vec<dispatch::ReconnectNotifySender>>>,
}

pub trait ExecWithParamaters {
    fn bind_raw(self, param: Vec<u8>) -> io::Result<Self>
        where Self: std::marker::Sized;

    ///bind named parameter
    fn bind_named_ref<T : Serialize, T1:Into<String>>(self, name: T1, param: &T) -> io::Result<Self>
        where Self: std::marker::Sized,

    {
        let mut name_s = name.into();
        name_s.insert(0,':');

        self.bind_raw(tools::serialize_one_element_map(
            name_s,
            tools::serialize_to_vec_u8(param)?)?)
    }

    ///bind parameter
    fn bind_ref<T : Serialize>(self, param: &T) -> io::Result<Self>
        where Self: std::marker::Sized
    {
        self.bind_raw(tools::serialize_to_vec_u8(param)?)
    }

    ///bind named parameter
    fn bind_named<T, T1>(self, name: T1,param: T) -> io::Result<Self>
        where T: Serialize,
              Self: std::marker::Sized,
              T1:Into<String>
    {
        self.bind_named_ref(name, &param)
    }


    ///bind parameter
    fn bind<T>(self, param: T) -> io::Result<Self>
        where T: Serialize,
              Self: std::marker::Sized
    {
        self.bind_ref(&param)
    }

    ///bind null
    fn bind_null(self) -> io::Result<Self>
        where  Self: std::marker::Sized
    {
        self.bind_ref(&Value::Nil)
    }

    ///bind named null as parameter
    fn bind_named_null<T1>(self, name: T1) -> io::Result<Self>
        where  Self: std::marker::Sized,
               T1:Into<String>
    {
        self.bind_named_ref(name, &Value::Nil)
    }
}

pub struct PreparedSql {
    client: Client,
    sql: String,
    params: Vec<Vec<u8>>
}

pub struct PreparedFunctionCall {
    client: Client,
    function: String,
    params: Vec<Vec<u8>>
}

impl Client {
    /// manually create client by consume Client Config
    pub fn new(config: ClientConfig) -> Client {
        let (command_sender, command_receiver) = mpsc::unbounded();

        let status = Arc::new(RwLock::new(ClientStatus::Init));
        let notify_callbacks = Arc::new(Mutex::new(Vec::new()));

        Client {
            command_sender,
            dispatch: Arc::new(Mutex::new(Some(Dispatch::new(
                config,
                command_receiver,
                status.clone(),
                notify_callbacks.clone(),
            )))),
            status,
            notify_callbacks,
        }
    }

    /// prepare sql call
    ///
    ///  # Examples
    ///
    /// rust code
    /// ```text
    ///     let response : TarantoolSqlResponse<(u32, String)> = client
    ///         .prepare_sql("select * from TABLE1 where COLUMN1=?")
    ///         .bind(&1)?
    ///         .execute().await?;
    ///     let rows = response.decode_result_set()?;
    pub fn prepare_sql<T>(&self, sql: T) -> PreparedSql
        where   T:Into<String>
    {
        PreparedSql {
            client : self.clone(),
            sql: sql.into(),
            params: vec![]
        }
    }

    /// prepare call to stored procedure
    ///
    ///  # Examples
    ///
    /// rust code
    /// ```text
    /// let response = client
    ///         .prepare_fn_call("test")
    ///         .bind(&("aa", "aa"))?
    ///         .bind(&1)?
    ///         .execute().await?;
    ///     let s: (Vec<String>, u64) = response.decode_pair()?;
    pub  fn prepare_fn_call<T>(&self, function_name: T) -> PreparedFunctionCall
     where T:Into<String>
    {
        PreparedFunctionCall {
            client : self.clone(),
            function: function_name.into(),
            params: vec![]
        }
    }

    /// return client status
    pub fn get_status(&self) -> ClientStatus {
        self.status.read().unwrap().clone()
    }

    /// return notify stream with connection statuses
    pub fn subscribe_to_notify_stream(&self) -> mpsc::UnboundedReceiver<ClientStatus> {
        let (callback_sender, callback_receiver) = mpsc::unbounded();
        self.notify_callbacks.lock().unwrap().push(callback_sender);
        callback_receiver
    }

    /// send any command you manually create, this method is low level and not intended to be used
    pub async fn send_command(&self, req: CommandPacket) -> io::Result<TarantoolResponse> {
        //        let dispatch = self.dispatch.clone();

        if let Some(mut extracted_dispatch) = self.dispatch.clone().lock().unwrap().take() {
            debug!("spawn coroutine!");
            //lazy spawning main coroutine in first tarantool call
            tokio::spawn(async move {
                extracted_dispatch.run().await;
            });
        }

        let (callback_sender, callback_receiver) = oneshot::channel();
        let send_res = self.command_sender.unbounded_send((req, callback_sender));
        if send_res.is_err() {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                ERROR_DISPATCH_THREAD_IS_DEAD,
            ));
        }
        match callback_receiver.await {
            Err(_) => Err(io::Error::new(
                io::ErrorKind::Other,
                ERROR_CLIENT_DISCONNECTED,
            )),
            Ok(res) => res,
        }
    }

    /// call tarantool stored procedure
    ///
    /// params mast be serializable to MsgPack tuple by Serde - rust tuple or vector or struct (by default structs serialized as tuple)
    ///
    /// order of fields in serializes tuple is order of parameters of procedure
    ///
    ///  # Examples
    ///
    /// lua function on tarantool
    /// ```lua
    /// function test(a,b)
    ///   return a,b,11
    ///  end
    /// ```
    ///
    /// rust code
    /// ```text
    ///  let response = client.call_fn("test", &(("aa", "aa"), 1)).await?;
    ///  let s: (Vec<String>, Vec<u64>) = response.decode_pair()?;
    ///  println!("resp value={:?}", s);
    ///  assert_eq!((vec!["aa".to_string(), "aa".to_string()], vec![1]), s);
    ///
    #[inline(always)]
    pub async fn call_fn<T>(&self, function: &str, params: &T) -> io::Result<TarantoolResponse>
    where
        T: Serialize,
    {
        self.send_command(CommandPacket::call(function, params).unwrap())
            .await
    }

    ///call tarantool stored procedure with one parameter
    ///
    #[inline(always)]
    pub async fn call_fn1<T1>(&self, function: &str, param1: &T1) -> io::Result<TarantoolResponse>
    where
        T1: Serialize,
    {
        self.send_command(CommandPacket::call(function, &(param1,)).unwrap())
            .await
    }

    ///call tarantool stored procedure with two parameters
    ///
    /// # Examples
    ///
    ///```text
    /// let response = client.call_fn2("test", &("param11", "param12") , &2).await?;
    ///  let s: (Vec<String>, Vec<u64>) = response.decode_pair()?;
    ///  println!("resp value={:?}", s);
    ///  assert_eq!((vec!["aa".to_string(), "aa".to_string()], vec![1]), s);
    ///
    #[inline(always)]
    pub async fn call_fn2<T1, T2>(
        &self,
        function: &str,
        param1: &T1,
        param2: &T2,
    ) -> io::Result<TarantoolResponse>
    where
        T1: Serialize,
        T2: Serialize,
    {
        self.send_command(CommandPacket::call(function, &(param1, param2)).unwrap())
            .await
    }

    ///call tarantool stored procedure with three parameters
    ///
    #[inline(always)]
    pub async fn call_fn3<T1, T2, T3>(
        &self,
        function: &str,
        param1: &T1,
        param2: &T2,
        param3: &T3,
    ) -> io::Result<TarantoolResponse>
    where
        T1: Serialize,
        T2: Serialize,
        T3: Serialize,
    {
        self.send_command(CommandPacket::call(function, &(param1, param2, param3)).unwrap())
            .await
    }

    ///call tarantool stored procedure with four parameters
    ///
    #[inline(always)]
    pub async fn call_fn4<T1, T2, T3, T4>(
        &self,
        function: &str,
        param1: &T1,
        param2: &T2,
        param3: &T3,
        param4: &T4,
    ) -> io::Result<TarantoolResponse>
    where
        T1: Serialize,
        T2: Serialize,
        T3: Serialize,
        T4: Serialize,
    {
        self.send_command(CommandPacket::call(function, &(param1, param2, param3, param4)).unwrap())
            .await
    }

    ///call tarantool stored procedure with five parameters
    ///
    #[inline(always)]
    pub async fn call_fn5<T1, T2, T3, T4, T5>(
        &self,
        function: &str,
        param1: &T1,
        param2: &T2,
        param3: &T3,
        param4: &T4,
        param5: &T5,
    ) -> io::Result<TarantoolResponse>
    where
        T1: Serialize,
        T2: Serialize,
        T3: Serialize,
        T4: Serialize,
        T5: Serialize,
    {
        self.send_command(
            CommandPacket::call(function, &(param1, param2, param3, param4, param5)).unwrap(),
        )
        .await
    }

    ///call "select" from tarantool
    /// - space - i32 space id
    /// - index - i32 index id
    /// - key - key part used for select, may be sequence (vec or tuple)
    /// - offset - i32 select offset
    /// - limit - i32 limit of rows
    /// - iterator - type of iterator
    ///
    #[inline(always)]
    pub async fn select<T>(
        &self,
        space: i32,
        index: i32,
        key: &T,
        offset: i32,
        limit: i32,
        iterator: IteratorType,
    ) -> io::Result<TarantoolResponse>
    where
        T: Serialize,
    {
        self.send_command(
            CommandPacket::select(space, index, key, offset, limit, iterator as i32).unwrap(),
        )
        .await
    }

    ///insert tuple to space
    /// - space - space id
    /// - tuple - sequence of fields(can be vec or rust tuple)
    ///
    #[inline(always)]
    pub async fn insert<T>(&self, space: i32, tuple: &T) -> io::Result<TarantoolResponse>
    where
        T: Serialize,
    {
        self.send_command(CommandPacket::insert(space, tuple).unwrap())
            .await
    }

    #[inline(always)]
    ///replace tuple in space by primary key
    /// - space - space id
    /// - tuple - sequence of fields(can be vec or rust tuple)
    ///
    /// # Examples
    /// ```text
    /// let tuple_replace= (3,"test_insert","replace");
    /// client.replace(SPACE_ID, &tuple_replace).await?;
    ///
    pub async fn replace<T>(&self, space: i32, tuple: &T) -> io::Result<TarantoolResponse>
    where
        T: Serialize,
    {
        self.send_command(CommandPacket::replace(space, tuple).unwrap())
            .await
    }

    #[inline(always)]
    ///replace tuple in space by primary key - raw method if you have already serialized msgpack
    /// - space - space id
    /// - tuple - sequence of fields stored as msgpack
    ///
    /// # Examples
    /// ```text
    /// let tuple_replace= (3,"test_insert","replace");
    /// let raw_buf = serialize_to_vec_u8(&tuple_replace).unwrap();
    /// client.replace_raw(SPACE_ID, raw_buf).await?;
    ///
    pub async fn replace_raw(
        &self,
        space: i32,
        tuple_raw: Vec<u8>,
    ) -> io::Result<TarantoolResponse> {
        self.send_command(CommandPacket::replace_raw(space, tuple_raw).unwrap())
            .await
    }

    ///update row in tarantool
    /// - space - space id
    /// - key - sequence of fields(rust tuple or vec)
    /// - args - sequence of update operations, for example (('=',2,"test_update"),)
    ///
    /// # Examples
    /// ```text
    /// let tuple= (3,"test_insert");
    /// let update_op= (('=',2,"test_update"),);
    /// client.update(SPACE_ID, &tuple, &update_op).await?;
    ///
    #[inline(always)]
    pub async fn update<T, T2>(
        &self,
        space: i32,
        key: &T2,
        args: &T,
    ) -> io::Result<TarantoolResponse>
    where
        T: Serialize,
        T2: Serialize,
    {
        self.send_command(CommandPacket::update(space, key, args).unwrap())
            .await
    }

    ///upsert row in tuple
    ///
    /// # Examples
    /// ```text
    /// let key= (4,"test_upsert");
    /// let update_op= (('=',2,"test_update_upsert"),);
    /// client.upsert(SPACE_ID,&key, &key,&update_op).await?;
    ///
    #[inline(always)]
    pub async fn upsert<T, T2, T3>(
        &self,
        space: i32,
        key: &T2,
        def: &T3,
        args: &T,
    ) -> io::Result<TarantoolResponse>
    where
        T: Serialize,
        T2: Serialize,
        T3: Serialize,
    {
        self.send_command(CommandPacket::upsert(space, key, def, args).unwrap())
            .await
    }

    ///delete row in space
    ///
    /// # Examples
    /// ```text
    /// let tuple= (3,"test_insert");
    /// client.delete(SPACE_ID,&tuple).await?;
    #[inline(always)]
    pub async fn delete<T>(&self, space: i32, key: &T) -> io::Result<TarantoolResponse>
    where
        T: Serialize,
    {
        self.send_command(CommandPacket::delete(space, key).unwrap())
            .await
    }

    ///eval expression in tarantool
    ///
    /// # Examples
    ///
    /// ```text
    /// client.eval("return ...\n".to_string(),&(1,2)).await?
    ///
    #[inline(always)]
    pub async fn eval<T, T1>(&self, expression: T1, args: &T) -> io::Result<TarantoolResponse>
    where
        T: Serialize,
        T1: Into<String>
    {
        self.send_command(CommandPacket::eval(expression.into(), args).unwrap())
            .await
    }

    ///eval sql expression in tarantool
    ///
    /// # Examples
    ///
    /// ```text
    /// client.exec_sql("select * from TABLE1 where COLUMN1=?".to_string(), &(1,)).await?;
    ///
    #[inline(always)]
    pub async fn exec_sql<T, T1>(&self, sql: T1, args: &T) -> io::Result<TarantoolResponse>
        where
            T: Serialize,
            T1: Into<String>
    {
        self.send_command(CommandPacket::exec_sql(sql.into().as_str(), args).unwrap())
            .await
    }

    // ///prepare sql expression in tarantool
    // ///
    // /// # Examples
    // ///
    // /// ```text
    // /// client.prepare_stmt("select * from TABLE1 where COLUMN1=?".to_string(), &(1,)).await?;
    // ///
    // #[inline(always)]
    // pub async fn prepare_stmt(&self, sql: String) -> io::Result<TarantoolResponse>
    // {
    //     self.send_command(CommandPacket::prepare_stmt(sql).unwrap())
    //         .await
    // }

    ///ping tarantool server, return empty response in success
    ///
    /// # Examples
    ///
    /// ```text
    /// client.ping().await?
    ///
    #[inline(always)]
    pub async fn ping(&self) -> io::Result<TarantoolResponse> {
        self.send_command(CommandPacket::ping().unwrap()).await
    }
}


impl ExecWithParamaters for PreparedSql {

    ///bind raw parameter
    fn bind_raw(mut self, param: Vec<u8>) -> io::Result<PreparedSql>  {
        self.params.push(param);
        Ok(self)
    }
}


impl PreparedSql {

    ///eval sql expression in tarantool
    ///
    /// # Examples
    ///
    /// ```text
    /// let response = client
    //         .prepare_sql("select * from TABLE1 where COLUMN1=?")
    //         .bind(1)?
    //         .execute().await?;
    ///
    #[inline(always)]
    pub async fn execute(self) -> io::Result<TarantoolSqlResponse>
    {
        self.client.send_command(CommandPacket::exec_sql_raw(&self.sql.as_str(), serialize_array(&self.params)?).unwrap())
            .await.map(|val| val.into())
    }
}

impl ExecWithParamaters for PreparedFunctionCall {

    ///bind raw parameter
    fn bind_raw(mut self, param: Vec<u8>) -> io::Result<PreparedFunctionCall>  {
        self.params.push(param);
        Ok(self)
    }
}

impl PreparedFunctionCall {
    /// call tarantool stored procedure
    ///
    /// params mast be serializable to MsgPack tuple by Serde - rust tuple or vector or struct (by default structs serialized as tuple)
    ///
    /// order of fields in serializes tuple is order od parameters of procedure
    ///
    ///  # Examples
    ///
    /// lua function on tarantool
    /// ```lua
    /// function test(a,b)
    ///   return a,b,11
    ///  end
    /// ```
    ///
    /// rust code
    /// ```text
    ///  let response = client
    ///         .prepare_fn_call("test")
    ///         .bind_ref(&("aa", "aa"))?
    ///         .bind(1)?
    ///         .execute().await?;
    ///
    #[inline(always)]
    pub async fn execute(self) -> io::Result<TarantoolResponse>
    {
        self.client.send_command(CommandPacket::call_raw(self.function.as_str(), serialize_array(&self.params)?).unwrap())
            .await
    }
}