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
#![allow(non_camel_case_types)]
use std::io;
use std::str;

use crate::tarantool::tools;
use rmpv::Value;
use serde::{Deserialize, Serialize};

use bytes::{Bytes, IntoBuf};

/// tarantool auth packet
#[derive(Debug)]
pub struct AuthPacket {
    pub login: String,
    pub password: String,
}

/// tarantool packet intended for serialize and cross thread send
#[derive(Debug, Clone)]
pub struct CommandPacket {
    pub code: Code,
    pub internal_fields: Vec<(Key, Value)>,
    pub command_field: Vec<(Key, Vec<u8>)>,
}

/// Tarantool request enum (auth or ordinary packet)
#[derive(Debug)]
pub enum TarantoolRequest {
    Auth(AuthPacket),
    Command(CommandPacket),
}

/// Tarantool response struct
///
/// use any decode method to decode tarantool response to custom struct by serde
/// please look examples
/// https://github.com/zheludkovm/RustyTarantool/tree/master/examples
///
#[derive(Debug)]
pub struct TarantoolResponse {
    pub code: u64,
    pub data: Bytes,
}

#[derive(Debug, Clone)]
pub enum Code {
    SELECT = 0x01,
    INSERT = 0x02,
    REPLACE = 0x03,
    UPDATE = 0x04,
    DELETE = 0x05,
    OLD_CALL = 0x06,
    AUTH = 0x07,
    EVAL = 0x08,
    UPSERT = 0x09,
    CALL = 0x010,
    PING = 0x040,
    SUBSCRIBE = 0x066,
}

#[derive(Debug, Copy, Clone)]
pub enum Key {
    //header
    CODE = 0x00,
    SYNC = 0x01,
    SCHEMA_ID = 0x05,

    //body
    SPACE = 0x10,
    INDEX = 0x11,
    LIMIT = 0x12,
    OFFSET = 0x13,
    ITERATOR = 0x14,
    KEY = 0x20,
    TUPLE = 0x21,
    FUNCTION = 0x22,
    USER_NAME = 0x23,
    EXPRESSION = 0x27,
    UPSERT_OPS = 0x28,
    DATA = 0x30,
    ERROR = 0x31,
}

impl TarantoolResponse {
    pub fn new(code: u64, data: Bytes) -> TarantoolResponse {
        TarantoolResponse { code, data }
    }

    /// decode tarantool response to any serder deserializable struct
    pub fn decode<'de, T>(self) -> io::Result<T>
    where
        T: Deserialize<'de>,
    {
        tools::decode_serde(self.data.into_buf())
    }

    /// decode tarantool response to tuple wih one element and return this element
    pub fn decode_single<'de, T>(self) -> io::Result<T>
    where
        T: Deserialize<'de>,
    {
        let (res,) = tools::decode_serde(self.data.into_buf())?;
        Ok(res)
    }

    /// decode tarantool response to tuple of two elements
    pub fn decode_pair<'de, T1, T2>(self) -> io::Result<(T1, T2)>
    where
        T1: Deserialize<'de>,
        T2: Deserialize<'de>,
    {
        Ok(tools::decode_serde(self.data.into_buf())?)
    }

    ///decode tarantool response to three elements
    pub fn decode_trio<'de, T1, T2, T3>(self) -> io::Result<(T1, T2, T3)>
    where
        T1: Deserialize<'de>,
        T2: Deserialize<'de>,
        T3: Deserialize<'de>,
    {
        let (r1, r2, r3) = tools::decode_serde(self.data.into_buf())?;
        Ok((r1, r2, r3))
    }
}

impl CommandPacket {
    pub fn call<T>(function: &str, params: &T) -> io::Result<CommandPacket>
    where
        T: Serialize,
    {
        Ok(CommandPacket {
            code: Code::OLD_CALL,
            internal_fields: vec![(Key::FUNCTION, Value::from(function))],
            command_field: vec![(Key::TUPLE, tools::serialize_to_vec_u8(params)?)],
        })
    }

    pub fn select<T>(
        space: i32,
        index: i32,
        key: &T,
        offset: i32,
        limit: i32,
        iterator: i32,
    ) -> io::Result<CommandPacket>
    where
        T: Serialize,
    {
        Ok(CommandPacket {
            code: Code::SELECT,
            internal_fields: vec![
                (Key::SPACE, Value::from(space)),
                (Key::INDEX, Value::from(index)),
                (Key::ITERATOR, Value::from(iterator)),
                (Key::LIMIT, Value::from(limit)),
                (Key::OFFSET, Value::from(offset)),
            ],
            command_field: vec![(Key::KEY, tools::serialize_to_vec_u8(key)?)],
        })
    }

    pub fn insert<T>(space: i32, tuple: &T) -> io::Result<CommandPacket>
    where
        T: Serialize,
    {
        Ok(CommandPacket {
            code: Code::INSERT,
            internal_fields: vec![(Key::SPACE, Value::from(space))],
            command_field: vec![(Key::TUPLE, tools::serialize_to_vec_u8(tuple)?)],
        })
    }

    pub fn replace<T>(space: i32, tuple: &T) -> io::Result<CommandPacket>
    where
        T: Serialize,
    {
        Ok(CommandPacket {
            code: Code::REPLACE,
            internal_fields: vec![(Key::SPACE, Value::from(space))],
            command_field: vec![(Key::TUPLE, tools::serialize_to_vec_u8(tuple)?)],
        })
    }

    pub fn replace_raw(space: i32, tuple_raw: Vec<u8>) -> io::Result<CommandPacket> {
        Ok(CommandPacket {
            code: Code::REPLACE,
            internal_fields: vec![(Key::SPACE, Value::from(space))],
            command_field: vec![(Key::TUPLE, tuple_raw)],
        })
    }

    pub fn update<T, T2>(space: i32, key: &T2, args: &T) -> io::Result<CommandPacket>
    where
        T: Serialize,
        T2: Serialize,
    {
        Ok(CommandPacket {
            code: Code::UPDATE,
            internal_fields: vec![(Key::SPACE, Value::from(space))],
            command_field: vec![
                (Key::KEY, tools::serialize_to_vec_u8(key)?),
                (Key::TUPLE, tools::serialize_to_vec_u8(args)?),
            ],
        })
    }

    pub fn upsert<T, T2, T3>(space: i32, key: &T2, def: &T3, args: &T) -> io::Result<CommandPacket>
    where
        T: Serialize,
        T2: Serialize,
        T3: Serialize,
    {
        Ok(CommandPacket {
            code: Code::UPSERT,
            internal_fields: vec![(Key::SPACE, Value::from(space))],
            command_field: vec![
                (Key::KEY, tools::serialize_to_vec_u8(key)?),
                (Key::TUPLE, tools::serialize_to_vec_u8(def)?),
                (Key::UPSERT_OPS, tools::serialize_to_vec_u8(args)?),
            ],
        })
    }

    pub fn delete<T>(space: i32, key: &T) -> io::Result<CommandPacket>
    where
        T: Serialize,
    {
        Ok(CommandPacket {
            code: Code::DELETE,
            internal_fields: vec![(Key::SPACE, Value::from(space))],
            command_field: vec![(Key::KEY, tools::serialize_to_vec_u8(key)?)],
        })
    }

    pub fn eval<T>(expression: String, args: &T) -> io::Result<CommandPacket>
    where
        T: Serialize,
    {
        Ok(CommandPacket {
            code: Code::EVAL,
            internal_fields: vec![(Key::EXPRESSION, Value::from(expression))],
            command_field: vec![(Key::TUPLE, tools::serialize_to_vec_u8(args)?)],
        })
    }

    pub fn ping() -> io::Result<CommandPacket> {
        Ok(CommandPacket {
            code: Code::PING,
            internal_fields: vec![],
            command_field: vec![],
        })
    }
}