tarantool 0.4.0

Tarantool rust bindings
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
//! Box: schema
use std::os::raw::c_int;

use serde::{Serialize, Serializer};
use serde_json::{Map, Value};

use crate::error::{Error, TarantoolError};
use crate::ffi::helper::new_c_str;
use crate::ffi::lua as ffi_lua;
use crate::index::IteratorType;
use crate::space::{Space, SystemSpace};
use crate::tuple::{AsTuple, Tuple};

/// Type of engine, used by space.
#[derive(Copy, Clone, Debug)]
pub enum SpaceEngineType {
    Memtx,
    Vinyl,
}

impl Serialize for SpaceEngineType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match *self {
            SpaceEngineType::Memtx => serializer.serialize_str("memtx"),
            SpaceEngineType::Vinyl => serializer.serialize_str("vinyl"),
        }
    }
}

/// SpaceInternal is tuple, holding space metadata in system `_space` space.
#[derive(Serialize, Debug)]
pub struct SpaceMetadata {
    pub id: u32,
    pub uid: u32,
    pub name: String,
    pub engine: SpaceEngineType,
    pub field_count: u32,
    pub options: Map<String, Value>,
    pub format: Vec<Value>,
}

impl AsTuple for SpaceMetadata {}

/// List of options for new or updated index.
///
/// For details see [space_object:create_index - options](https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/create_index/).
pub struct IndexOptions {
    pub index_type: Option<IndexType>,
    pub id: Option<u32>,
    pub unique: Option<bool>,
    pub if_not_exists: Option<bool>,
    pub parts: Option<Vec<IndexPart>>,
    pub dimension: Option<u32>,
    pub distance: Option<RtreeIndexDistanceType>,
    pub bloom_fpr: Option<f32>,
    pub page_size: Option<u32>,
    pub range_size: Option<u32>,
    pub run_count_per_level: Option<u32>,
    pub run_size_ratio: Option<f32>,
    pub sequence: Option<IndexSequenceOption>,
    pub func: Option<String>,
    // Only for Tarantool >= 2.6
    // pub hint: Option<bool>,
}

impl Default for IndexOptions {
    fn default() -> Self {
        IndexOptions {
            index_type: Some(IndexType::Tree),
            id: None,
            unique: Some(true),
            if_not_exists: Some(false),
            parts: Some(vec![IndexPart {
                field_index: 1,
                field_type: IndexFieldType::Unsigned,
                collation: None,
                is_nullable: None,
                path: None,
            }]),
            dimension: Some(2),
            distance: Some(RtreeIndexDistanceType::Euclid),
            bloom_fpr: Some(0.05),
            page_size: Some(8 * 1024),
            range_size: None,
            run_count_per_level: Some(2),
            run_size_ratio: Some(3.5),
            sequence: None,
            func: None,
            // Only for Tarantool >= 2.6
            // hint: Some(true),
        }
    }
}

/// Type of index.
#[derive(Copy, Clone, Debug)]
pub enum IndexType {
    Hash,
    Tree,
    Bitset,
    Rtree,
}

/// Type of index part.
#[derive(Copy, Clone, Debug)]
pub enum IndexFieldType {
    Unsigned,
    String,
    Integer,
    Number,
    Double,
    Decimal,
    Boolean,
    Varbinary,
    Uuid,
    Array,
    Scalar,
}

/// Index part.
pub struct IndexPart {
    pub field_index: u32,
    pub field_type: IndexFieldType,
    pub collation: Option<String>,
    pub is_nullable: Option<bool>,
    pub path: Option<String>,
}

/// Type of distance for retree index.
#[derive(Copy, Clone, Debug)]
pub enum RtreeIndexDistanceType {
    Euclid,
    Manhattan,
}

/// Sequence option for new or updated index.
///
/// For details see [specifying a sequence in create_index](https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_schema_sequence/create_index/#box-schema-sequence-create-index).
pub enum IndexSequenceOption {
    SeqId {
        seq_id: u32,
        field_index: Option<u32>,
    },
    SeqName {
        seq_name: String,
        field_index: Option<u32>,
    },
    True,
    Empty,
}

/// Create new index for space.
///
/// - `space_id`   - ID of existing space.
/// - `index_name` - name of index to create, which should conform to the rules for object names.
/// - `opts`       - see IndexOptions struct.
///
/// For details see [space_object:create_index](https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/create_index/)
pub fn create_index(space_id: u32, index_name: &str, opts: &IndexOptions) -> Result<(), Error> {
    unsafe {
        // Create new stack (just in case - in order no to mess things
        // in current stack).
        let state = ffi_lua::luaT_state();
        let ci_state = ffi_lua::lua_newthread(state);

        // Execute the following lua Code:
        // -- space = box.space._space:get(space_id)
        // -- space_name = space.name
        // -- box.space[space_name]:create_index(name, opts)

        // -- space = box.space._space:get(space_id)
        ffi_lua::lua_getglobal(ci_state, new_c_str("box").as_ptr());
        ffi_lua::lua_getfield(ci_state, -1, new_c_str("space").as_ptr());
        ffi_lua::lua_getfield(ci_state, -1, new_c_str("_space").as_ptr());
        ffi_lua::lua_getfield(ci_state, -1, new_c_str("get").as_ptr());
        ffi_lua::lua_pushvalue(ci_state, -2);
        ffi_lua::lua_pushinteger(ci_state, space_id as isize);
        if ffi_lua::luaT_call(ci_state, 2, 1) == 1 {
            return Err(TarantoolError::last().into());
        }

        // -- space_name = space.name
        ffi_lua::lua_getfield(ci_state, -1, new_c_str("name").as_ptr());
        let space_name = ffi_lua::lua_tostring(ci_state, -1);
        ffi_lua::lua_remove(ci_state, -1);

        // -- box.space[space_name].create_index(name, opts)
        ffi_lua::lua_getglobal(ci_state, new_c_str("box").as_ptr());
        ffi_lua::lua_getfield(ci_state, -1, new_c_str("space").as_ptr());
        ffi_lua::lua_getfield(ci_state, -1, space_name);
        ffi_lua::lua_getfield(ci_state, -1, new_c_str("create_index").as_ptr());

        // Put args on the stack:

        // self
        ffi_lua::lua_pushvalue(ci_state, -2);

        // name
        ffi_lua::lua_pushstring(ci_state, new_c_str(index_name).as_ptr());

        // options
        ffi_lua::lua_newtable(ci_state);

        // opts.index_type
        if let Some(index_type) = opts.index_type {
            let index_type_str = match index_type {
                IndexType::Hash => "hash",
                IndexType::Tree => "tree",
                IndexType::Bitset => "bitset",
                IndexType::Rtree => "rtree",
            };
            ffi_lua::lua_pushstring(ci_state, new_c_str(index_type_str).as_ptr());
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("type").as_ptr());
        }

        // opts.id
        if let Some(id) = opts.id {
            ffi_lua::lua_pushinteger(ci_state, id as isize);
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("id").as_ptr());
        }

        // opts.unique
        if let Some(unique) = opts.unique {
            ffi_lua::lua_pushboolean(ci_state, unique as c_int);
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("unique").as_ptr());
        }

        // opts.if_not_exists
        if let Some(if_not_exists) = opts.if_not_exists {
            ffi_lua::lua_pushboolean(ci_state, if_not_exists as c_int);
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("if_not_exists").as_ptr());
        }

        // opts.parts
        if let Some(parts) = &opts.parts {
            ffi_lua::lua_newtable(ci_state);

            for (idx, p) in parts.iter().enumerate() {
                ffi_lua::lua_pushinteger(ci_state, (idx + 1) as isize);
                ffi_lua::lua_newtable(ci_state);

                // part.field
                ffi_lua::lua_pushinteger(ci_state, p.field_index as isize);
                ffi_lua::lua_setfield(ci_state, -2, new_c_str("field").as_ptr());

                // part.type
                let field_type = match p.field_type {
                    IndexFieldType::Unsigned => "unsigned",
                    IndexFieldType::String => "string",
                    IndexFieldType::Integer => "integer",
                    IndexFieldType::Number => "number",
                    IndexFieldType::Double => "double",
                    IndexFieldType::Decimal => "decimal",
                    IndexFieldType::Boolean => "boolean",
                    IndexFieldType::Varbinary => "varbinary",
                    IndexFieldType::Uuid => "uuid",
                    IndexFieldType::Array => "array",
                    IndexFieldType::Scalar => "scalar",
                };
                ffi_lua::lua_pushstring(ci_state, new_c_str(field_type).as_ptr());
                ffi_lua::lua_setfield(ci_state, -2, new_c_str("type").as_ptr());

                // part.collation
                if let Some(collation) = &p.collation {
                    ffi_lua::lua_pushstring(ci_state, new_c_str(collation).as_ptr());
                    ffi_lua::lua_setfield(ci_state, -2, new_c_str("collation").as_ptr());
                }

                // part.is_nullable
                if let Some(is_nullable) = &p.is_nullable {
                    ffi_lua::lua_pushboolean(ci_state, if *is_nullable { 1 } else { 0 });
                    ffi_lua::lua_setfield(ci_state, -2, new_c_str("is_nullable").as_ptr());
                }

                // part.path
                if let Some(path) = &p.path {
                    ffi_lua::lua_pushstring(ci_state, new_c_str(path).as_ptr());
                    ffi_lua::lua_setfield(ci_state, -2, new_c_str("path").as_ptr());
                }

                ffi_lua::lua_settable(ci_state, -3);
            }

            ffi_lua::lua_setfield(ci_state, -2, new_c_str("parts").as_ptr())
        }

        // opts.dimension
        if let Some(dimension) = opts.dimension {
            ffi_lua::lua_pushinteger(ci_state, dimension as isize);
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("dimension").as_ptr());
        }

        // opts.distance
        if let Some(distance) = opts.distance {
            let distance_str = match distance {
                RtreeIndexDistanceType::Euclid => "euclid",
                RtreeIndexDistanceType::Manhattan => "manhattan",
            };
            ffi_lua::lua_pushstring(ci_state, new_c_str(distance_str).as_ptr());
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("distance").as_ptr());
        }

        // opts.bloom_fpr
        if let Some(bloom_fpr) = opts.bloom_fpr {
            ffi_lua::lua_pushnumber(ci_state, bloom_fpr as f64);
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("bloom_fpr").as_ptr());
        }

        // opts.page_size
        if let Some(page_size) = opts.page_size {
            ffi_lua::lua_pushinteger(ci_state, page_size as isize);
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("page_size").as_ptr());
        }

        // opts.range_size
        if let Some(range_size) = opts.range_size {
            ffi_lua::lua_pushinteger(ci_state, range_size as isize);
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("range_size").as_ptr());
        }

        // opts.run_count_per_level
        if let Some(run_count_per_level) = opts.run_count_per_level {
            ffi_lua::lua_pushinteger(ci_state, run_count_per_level as isize);
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("run_count_per_level").as_ptr());
        }

        // opts.run_size_ratio
        if let Some(run_size_ratio) = opts.run_size_ratio {
            ffi_lua::lua_pushnumber(ci_state, run_size_ratio as f64);
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("run_size_ratio").as_ptr());
        }

        // opts.sequence
        if let Some(sequence) = &opts.sequence {
            match sequence {
                // sequence = {id = sequence identifier , field = field number }
                IndexSequenceOption::SeqId {
                    seq_id,
                    field_index,
                } => {
                    ffi_lua::lua_newtable(ci_state);
                    ffi_lua::lua_pushinteger(ci_state, *seq_id as isize);
                    ffi_lua::lua_setfield(ci_state, -2, new_c_str("id").as_ptr());
                    if let Some(fi) = field_index {
                        ffi_lua::lua_pushinteger(ci_state, *fi as isize);
                        ffi_lua::lua_setfield(ci_state, -2, new_c_str("field").as_ptr());
                    }
                }
                // sequence = {id = sequence name , field = field number }
                IndexSequenceOption::SeqName {
                    seq_name,
                    field_index,
                } => {
                    ffi_lua::lua_newtable(ci_state);
                    ffi_lua::lua_pushstring(ci_state, new_c_str(seq_name).as_ptr());
                    ffi_lua::lua_setfield(ci_state, -2, new_c_str("id").as_ptr());
                    if let Some(fi) = field_index {
                        ffi_lua::lua_pushinteger(ci_state, *fi as isize);
                        ffi_lua::lua_setfield(ci_state, -2, new_c_str("field").as_ptr());
                    }
                }
                // sequence = true
                IndexSequenceOption::True => {
                    ffi_lua::lua_pushboolean(ci_state, true as c_int);
                }
                // sequence = {}
                IndexSequenceOption::Empty => {
                    ffi_lua::lua_newtable(ci_state);
                }
            }
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("sequence").as_ptr());
        }

        // opts.func
        if let Some(func) = &opts.func {
            ffi_lua::lua_pushstring(ci_state, new_c_str(func).as_ptr());
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("func").as_ptr());
        }

        // Only for Tarantool >= 2.6
        // opt.hint
        /* if let Some(hint) = opts.hint {
            ffi_lua::lua_pushboolean(ci_state, bool_as_int(hint));
            ffi_lua::lua_setfield(ci_state, -2, new_c_str("hint").as_ptr());
        }
        */

        // Call space_object:create_index.
        if ffi_lua::luaT_call(ci_state, 3, 1) == 1 {
            return Err(TarantoolError::last().into());
        }

        // No need to clean ci_state. It will be gc'ed.
    }

    Ok(())
}

/// Drop existing index.
///
/// - `space_id` - ID of existing space.
/// - `index_name` - ID of existing index.
pub fn drop_index(space_id: u32, index_id: u32) -> Result<(), Error> {
    unsafe {
        // Create new stack (just in case - in order no to mess things
        // in current stack).
        let state = ffi_lua::luaT_state();
        let drop_state = ffi_lua::lua_newthread(state);

        // Execute the following Lua code:
        // -- space = box.space._space:get(space_id)
        // -- space_name = space.name
        // -- index = box.space._index:get({space_id, index_id})
        // -- index_name = index.name
        // -- box.space.space_name.index.index_name:drop()

        // -- space = box.space._space:get({"id": space_id})
        ffi_lua::lua_getglobal(drop_state, new_c_str("box").as_ptr());
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("space").as_ptr());
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("_space").as_ptr());
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("get").as_ptr());
        ffi_lua::lua_pushvalue(drop_state, -2);
        ffi_lua::lua_pushinteger(drop_state, space_id as isize);
        if ffi_lua::luaT_call(drop_state, 2, 1) == 1 {
            return Err(TarantoolError::last().into());
        }

        // -- space_name = space.name
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("name").as_ptr());
        let space_name = ffi_lua::lua_tostring(drop_state, -1);
        ffi_lua::lua_remove(drop_state, -1);

        // -- index = box.space._index:get({space_id, index_id})
        ffi_lua::lua_getglobal(drop_state, new_c_str("box").as_ptr());
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("space").as_ptr());
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("_index").as_ptr());
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("get").as_ptr());
        ffi_lua::lua_pushvalue(drop_state, -2);
        ffi_lua::lua_newtable(drop_state);
        ffi_lua::lua_pushinteger(drop_state, 1);
        ffi_lua::lua_pushinteger(drop_state, space_id as isize);
        ffi_lua::lua_settable(drop_state, -3);
        ffi_lua::lua_pushinteger(drop_state, 2);
        ffi_lua::lua_pushinteger(drop_state, index_id as isize);
        ffi_lua::lua_settable(drop_state, -3);
        if ffi_lua::luaT_call(drop_state, 2, 1) == 1 {
            return Err(TarantoolError::last().into());
        }

        // -- index_name = index.name
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("name").as_ptr());
        let index_name = ffi_lua::lua_tostring(drop_state, -1);
        ffi_lua::lua_remove(drop_state, -1);

        // -- box.space.space_name.index.index_name:drop()
        ffi_lua::lua_getglobal(drop_state, new_c_str("box").as_ptr());
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("space").as_ptr());
        ffi_lua::lua_getfield(drop_state, -1, space_name);
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("index").as_ptr());
        ffi_lua::lua_getfield(drop_state, -1, index_name);
        ffi_lua::lua_getfield(drop_state, -1, new_c_str("drop").as_ptr());
        ffi_lua::lua_pushvalue(drop_state, -2);
        if ffi_lua::luaT_call(drop_state, 1, 1) == 1 {
            return Err(TarantoolError::last().into());
        }

        // No need to clean drop_state. It will be gc'ed.
    }

    Ok(())
}

/// Revoke all privileges associated with the given object.
///
/// - `obj_type` - string representation of object's type. Can be one of the following: "space", "sequence" or "function".
/// - `obj_id` - object's ID
pub fn revoke_object_privileges(obj_type: &str, obj_id: u32) -> Result<(), Error> {
    let sys_vpriv: Space = SystemSpace::VPriv.into();
    let mut sys_priv: Space = SystemSpace::Priv.into();

    let index_obj = sys_vpriv.index("object").unwrap();
    let privs: Vec<Tuple> = index_obj
        .select(IteratorType::Eq, &(obj_type, obj_id))?
        .collect();

    for t in privs {
        let uid = t.field::<u32>(1)?.unwrap();
        sys_priv.delete(&(uid,))?;
    }

    Ok(())
}