pickledb 0.5.1

A lightweight and simple key-value store written in Rust, heavily inspired by Python's PickleDB (https://pythonhosted.org/pickleDB/)
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
#![allow(clippy::float_cmp)]

use pickledb::{PickleDb, PickleDbDumpPolicy, SerializationMethod};
use serde::{Deserialize, Serialize};

mod common;

#[cfg(test)]
extern crate rstest;

use rstest::rstest_parametrize;

#[rstest_parametrize(ser_method_int, case(0), case(1), case(2), case(3))]
fn basic_set_get(ser_method_int: i32) {
    test_setup!("basic_set_get", ser_method_int, db_name);

    let mut db = PickleDb::new(
        &db_name,
        PickleDbDumpPolicy::AutoDump,
        ser_method!(ser_method_int),
    );

    // set a number
    let num = 100;
    db.set("num", &num).unwrap();

    // set a floating point number
    let float_num = 1.224;
    db.set("float", &float_num).unwrap();

    // set a String
    let mystr = String::from("my string");
    db.set("string", &mystr).unwrap();

    // set a Vec
    let myvec = vec![1, 2, 3];
    db.set("vec", &myvec).unwrap();

    // set a struct
    #[derive(Serialize, Deserialize, Debug)]
    struct Coor {
        x: i32,
        y: i32,
    }
    let mycoor = Coor { x: 1, y: 2 };
    db.set("struct", &mycoor).unwrap();

    // read a num
    assert_eq!(db.get::<i32>("num").unwrap(), num);
    // read a floating point number
    assert_eq!(db.get::<f32>("float").unwrap(), float_num);
    // read a String
    assert_eq!(db.get::<String>("string").unwrap(), mystr);
    // read a Vec
    assert_eq!(db.get::<Vec<i32>>("vec").unwrap(), myvec);
    // read a struct
    assert_eq!(db.get::<Coor>("struct").unwrap().x, mycoor.x);
    assert_eq!(db.get::<Coor>("struct").unwrap().y, mycoor.y);
}

#[rstest_parametrize(ser_method_int, case(0), case(1), case(2), case(3))]
fn set_load_get(ser_method_int: i32) {
    test_setup!("set_load_get", ser_method_int, db_name);

    // create a db with auto_dump == false
    let mut db = PickleDb::new(
        &db_name,
        PickleDbDumpPolicy::DumpUponRequest,
        ser_method!(ser_method_int),
    );

    // set a number
    let num = 100;
    db.set("num", &num).unwrap();

    // set a floating point number
    let float_num = 1.224;
    db.set("float", &float_num).unwrap();

    // set a String
    let mystr = String::from("my string");
    db.set("string", &mystr).unwrap();

    // set a Vec
    let myvec = vec![1, 2, 3];
    db.set("vec", &myvec).unwrap();

    // set a struct
    #[derive(Serialize, Deserialize, Debug)]
    struct Coor {
        x: i32,
        y: i32,
    }
    let mycoor = Coor { x: 1, y: 2 };
    db.set("struct", &mycoor).unwrap();

    // dump db to file
    assert!(db.dump().is_ok());

    // read db from file
    let read_db = PickleDb::load_read_only(&db_name, ser_method!(ser_method_int)).unwrap();

    // read a num
    assert_eq!(read_db.get::<i32>("num").unwrap(), num);
    // read a floating point number
    assert_eq!(read_db.get::<f32>("float").unwrap(), float_num);
    // read a String
    assert_eq!(read_db.get::<String>("string").unwrap(), mystr);
    // read a Vec
    assert_eq!(read_db.get::<Vec<i32>>("vec").unwrap(), myvec);
    // read a struct
    assert_eq!(read_db.get::<Coor>("struct").unwrap().x, mycoor.x);
    assert_eq!(read_db.get::<Coor>("struct").unwrap().y, mycoor.y);
}

#[rstest_parametrize(ser_method_int, case(0), case(1), case(2), case(3))]
fn set_load_get_auto_dump(ser_method_int: i32) {
    test_setup!("set_load_get_auto_dump", ser_method_int, db_name);

    // create a db with auto_dump == true
    let mut db = PickleDb::new(
        &db_name,
        PickleDbDumpPolicy::AutoDump,
        ser_method!(ser_method_int),
    );

    // set a number
    let num = 100;
    db.set("num", &num).unwrap();

    // set a floating point number
    let float_num = 1.224;
    db.set("float", &float_num).unwrap();

    // set a String
    let mystr = String::from("my string");
    db.set("string", &mystr).unwrap();

    // set a Vec
    let myvec = vec![1, 2, 3];
    db.set("vec", &myvec).unwrap();

    // set a struct
    #[derive(Serialize, Deserialize, Debug)]
    struct Coor {
        x: i32,
        y: i32,
    }
    let mycoor = Coor { x: 1, y: 2 };
    db.set("struct", &mycoor).unwrap();

    let read_db = PickleDb::load_read_only(&db_name, ser_method!(ser_method_int)).unwrap();

    // read a num
    assert_eq!(read_db.get::<i32>("num").unwrap(), num);
    // read a floating point number
    assert_eq!(read_db.get::<f32>("float").unwrap(), float_num);
    // read a String
    assert_eq!(read_db.get::<String>("string").unwrap(), mystr);
    // read a Vec
    assert_eq!(read_db.get::<Vec<i32>>("vec").unwrap(), myvec);
    // read a struct
    assert_eq!(read_db.get::<Coor>("struct").unwrap().x, mycoor.x);
    assert_eq!(read_db.get::<Coor>("struct").unwrap().y, mycoor.y);
}

#[rstest_parametrize(ser_method_int, case(0), case(1), case(2), case(3))]
fn set_load_get_auto_dump2(ser_method_int: i32) {
    test_setup!("set_load_get_auto_dump2", ser_method_int, db_name);

    // create a db with auto_dump == true
    let mut db = PickleDb::new(
        &db_name,
        PickleDbDumpPolicy::AutoDump,
        ser_method!(ser_method_int),
    );

    // set a number
    let num = 100;
    db.set("num", &num).unwrap();

    // read this number immediately
    {
        let read_db = PickleDb::load_read_only(&db_name, ser_method!(ser_method_int)).unwrap();
        assert_eq!(read_db.get::<i32>("num").unwrap(), num);
    }

    // set another number
    let num2 = 200;
    db.set("num2", &num2).unwrap();

    // read this other number immediately
    {
        let read_db = PickleDb::load_read_only(&db_name, ser_method!(ser_method_int)).unwrap();
        assert_eq!(read_db.get::<i32>("num2").unwrap(), num2);
    }

    // set a different value for a given key
    db.set("num", &101).unwrap();

    // read the new value
    assert_eq!(db.get::<i32>("num").unwrap(), 101);
    {
        let read_db = PickleDb::load_read_only(&db_name, ser_method!(ser_method_int)).unwrap();
        assert_eq!(read_db.get::<i32>("num").unwrap(), 101);
    }

    // set a different value of a different type for a given key
    db.set("num", &vec![1, 2, 3]).unwrap();

    // read the new value
    if let SerializationMethod::Bin = ser_method!(ser_method_int) {
        // N/A
    } else {
        assert!(db.get::<i32>("num").is_none());
    }
    assert_eq!(db.get::<Vec<i32>>("num").unwrap(), vec![1, 2, 3]);
    {
        let read_db = PickleDb::load_read_only(&db_name, ser_method!(ser_method_int)).unwrap();
        assert_eq!(read_db.get::<Vec<i32>>("num").unwrap(), vec![1, 2, 3]);
    }
}

#[rstest_parametrize(ser_method_int, case(0), case(1), case(2), case(3))]
fn set_special_strings(ser_method_int: i32) {
    test_setup!("set_special_strings", ser_method_int, db_name);

    // create a db with auto_dump == true
    let mut db = PickleDb::new(
        &db_name,
        PickleDbDumpPolicy::AutoDump,
        ser_method!(ser_method_int),
    );

    db.set("string1", &String::from("\"double_quotes\""))
        .unwrap();
    db.set("string2", &String::from("\'single_quotes\'"))
        .unwrap();
    db.set("string3", &String::from("שָׁלוֹם")).unwrap();
    db.set("string4", &String::from("😻")).unwrap();
    db.set("string5", &String::from("\nescapes\t\r")).unwrap();
    db.set("string6", &String::from("my\\folder")).unwrap();

    let read_db = PickleDb::load_read_only(&db_name, ser_method!(ser_method_int)).unwrap();
    assert_eq!(
        read_db.get::<String>("string1").unwrap(),
        String::from("\"double_quotes\"")
    );
    assert_eq!(
        read_db.get::<String>("string2").unwrap(),
        String::from("\'single_quotes\'")
    );
    assert_eq!(
        read_db.get::<String>("string3").unwrap(),
        String::from("שָׁלוֹם")
    );
    assert_eq!(
        read_db.get::<String>("string4").unwrap(),
        String::from("😻")
    );
    assert_eq!(
        read_db.get::<String>("string5").unwrap(),
        String::from("\nescapes\t\r")
    );
    assert_eq!(
        read_db.get::<String>("string6").unwrap(),
        String::from("my\\folder")
    );
}

#[rstest_parametrize(ser_method_int, case(0), case(1), case(2), case(3))]
fn edge_cases(ser_method_int: i32) {
    test_setup!("edge_cases", ser_method_int, db_name);

    // create a db with auto_dump == true
    let mut db = PickleDb::new(
        &db_name,
        PickleDbDumpPolicy::AutoDump,
        ser_method!(ser_method_int),
    );

    let x = 123;
    db.set("num", &x).unwrap();

    // load a read only version of the db from file
    let read_db = PickleDb::load_read_only(&db_name, ser_method!(ser_method_int)).unwrap();

    assert_eq!(db.get::<i32>("num"), Some(x));
    assert_eq!(read_db.get::<i32>("num"), Some(x));
    if let SerializationMethod::Yaml = ser_method!(ser_method_int) {
        // N/A
    } else {
        assert_eq!(db.get::<String>("num"), None);
        assert_eq!(read_db.get::<String>("num"), None);
    }
}

#[rstest_parametrize(ser_method_int, case(0), case(1), case(2), case(3))]
fn get_all_keys(ser_method_int: i32) {
    test_setup!("get_all_keys", ser_method_int, db_name);

    // create a db with auto_dump == true
    let mut db = PickleDb::new(
        &db_name,
        PickleDbDumpPolicy::AutoDump,
        ser_method!(ser_method_int),
    );

    // insert 10 keys: key0..key9
    let num = 100;
    for i in 0..10 {
        db.set(&format!("{}{}", "key", i), &num).unwrap();
    }

    // verify we have 10 keys
    assert_eq!(db.total_keys(), 10);

    // get all keys
    let keys = db.get_all();

    // verify we got 10 keys
    assert_eq!(keys.len(), 10);

    // verify all key names are there
    for i in 0..9 {
        assert!(keys.iter().any(|key| key == &format!("{}{}", "key", i)));
    }
}

#[rstest_parametrize(ser_method_int, case(0), case(1), case(2), case(3))]
fn rem_keys(ser_method_int: i32) {
    test_setup!("rem_keys", ser_method_int, db_name);

    // create a db with auto_dump == true
    let mut db = PickleDb::new(
        &db_name,
        PickleDbDumpPolicy::AutoDump,
        ser_method!(ser_method_int),
    );

    // insert 10 keys: key0..key9
    let num = 100;
    for i in 0..10 {
        db.set(&format!("{}{}", "key", i), &num).unwrap();
    }

    // remove 2 keys
    assert!(db.rem("key5").unwrap_or(false));
    assert!(db.rem("key8").unwrap_or(false));

    // verify we only have 8 keys now
    assert_eq!(db.total_keys(), 8);

    // verify both keys were removed
    for i in vec![5, 8].iter() {
        assert!(!db.exists(&format!("{}{}", "key", i)));
    }

    // verify the other keys are still there
    for i in vec![0, 1, 2, 3, 4, 6, 7, 9].iter() {
        assert!(db.exists(&format!("{}{}", "key", i)));
    }

    // verify keys were also removed from the file
    let read_db = PickleDb::load_read_only(&db_name, ser_method!(ser_method_int)).unwrap();
    assert_eq!(read_db.total_keys(), 8);
}

#[rstest_parametrize(ser_method_int, case(0), case(1), case(2), case(3))]
fn iter_test(ser_method_int: i32) {
    test_setup!("iter_test", ser_method_int, db_name);

    // create a db with auto_dump == true
    let mut db = PickleDb::new(
        &db_name,
        PickleDbDumpPolicy::AutoDump,
        ser_method!(ser_method_int),
    );

    let keys = vec!["key1", "key2", "key3", "key4", "key5"];
    // add a few keys and values
    db.set(keys[0], &1).unwrap();
    db.set(keys[1], &1.1).unwrap();
    db.set(keys[2], &String::from("value1")).unwrap();
    db.set(keys[3], &vec![1, 2, 3]).unwrap();
    db.set(keys[4], &('a', 'b', 'c')).unwrap();

    // iterate the db
    let mut keys_seen = vec![false, false, false, false, false];
    for key_value in db.iter() {
        // find the index of the current key in the keys vec
        let index = keys.iter().position(|&k| k == key_value.get_key()).unwrap();

        // mark the key as seen
        keys_seen[index] = true;

        // verify the value
        match key_value.get_key() {
            "key1" => assert_eq!(key_value.get_value::<i32>().unwrap(), 1),
            "key2" => assert_eq!(key_value.get_value::<f64>().unwrap(), 1.1),
            "key3" => assert_eq!(
                key_value.get_value::<String>().unwrap(),
                String::from("value1")
            ),
            "key4" => assert_eq!(key_value.get_value::<Vec<i32>>().unwrap(), vec![1, 2, 3]),
            "key5" => assert_eq!(
                key_value.get_value::<(char, char, char)>().unwrap(),
                ('a', 'b', 'c')
            ),
            _ => panic!(),
        }
    }

    // verify all 5 keys were seen
    assert_eq!(keys_seen.iter().filter(|&t| *t).count(), 5);
}