redb 4.2.0

Rust Embedded DataBase
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
#[cfg(feature = "experimental-api-5")]
use redb::ReadableTable;
use redb::{ReadableDatabase, ReadableTableMetadata};

const ELEMENTS: usize = 3;

// A user-defined value type whose type name deliberately collides with the built-in `u32` and
// whose width and encoding match it. It implements both the current and the redb 2.6 `Value`
// trait so the same table can be written by one version and read by the other.
#[derive(Debug)]
#[allow(dead_code)]
struct CollidingUserType;

impl redb::Value for CollidingUserType {
    type SelfType<'a> = u32;
    type AsBytes<'a> = [u8; 4];

    fn fixed_width() -> Option<usize> {
        Some(4)
    }

    fn from_bytes<'a>(data: &'a [u8]) -> u32
    where
        Self: 'a,
    {
        u32::from_le_bytes(data.try_into().unwrap())
    }

    fn as_bytes<'a, 'b: 'a>(value: &'a u32) -> [u8; 4]
    where
        Self: 'b,
    {
        value.to_le_bytes()
    }

    fn type_name() -> redb::TypeName {
        redb::TypeName::new("u32")
    }
}

impl redb2_6::Value for CollidingUserType {
    type SelfType<'a> = u32;
    type AsBytes<'a> = [u8; 4];

    fn fixed_width() -> Option<usize> {
        Some(4)
    }

    fn from_bytes<'a>(data: &'a [u8]) -> u32
    where
        Self: 'a,
    {
        u32::from_le_bytes(data.try_into().unwrap())
    }

    fn as_bytes<'a, 'b: 'a>(value: &'a u32) -> [u8; 4]
    where
        Self: 'b,
    {
        value.to_le_bytes()
    }

    fn type_name() -> redb2_6::TypeName {
        redb2_6::TypeName::new("u32")
    }
}

trait TestData: redb::Value + redb2_6::Value {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS]
    where
        Self: 'a;

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS]
    where
        Self: 'a;
}

impl TestData for u8 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [0, 1, 2]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [0, 1, 2]
    }
}

impl TestData for u16 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [0, 1, 2]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [0, 1, 2]
    }
}

impl TestData for u32 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [0, 1, 2]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [0, 1, 2]
    }
}

impl TestData for u64 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [0, 1, 2]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [0, 1, 2]
    }
}

impl TestData for u128 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [0, 1, 2]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [0, 1, 2]
    }
}

impl TestData for i8 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [-1, 1, 2]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [-1, 1, 2]
    }
}

impl TestData for i16 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [-1, 1, 2]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [-1, 1, 2]
    }
}

impl TestData for i32 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [-1, 1, 2]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [-1, 1, 2]
    }
}

impl TestData for i64 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [-1, 1, 2]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [-1, 1, 2]
    }
}

impl TestData for i128 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [-1, 1, 2]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [-1, 1, 2]
    }
}

impl TestData for f32 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [f32::NAN, f32::INFINITY, f32::MIN_POSITIVE]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [f32::NAN, f32::INFINITY, f32::MIN_POSITIVE]
    }
}

impl TestData for f64 {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [f64::MIN, f64::NEG_INFINITY, f64::MAX]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [f64::MIN, f64::NEG_INFINITY, f64::MAX]
    }
}

impl TestData for () {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [(), (), ()]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [(), (), ()]
    }
}

impl TestData for &'static str {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        ["hello", "world1", "hi"]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        ["hello", "world1", "hi"]
    }
}

impl TestData for &'static [u8] {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [b"test", b"bytes", b"now"]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [b"test", b"bytes", b"now"]
    }
}

impl TestData for &'static [u8; 5] {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [b"test1", b"bytes", b"now12"]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [b"test1", b"bytes", b"now12"]
    }
}

impl TestData for [&str; 3] {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS]
    where
        Self: 'a,
    {
        [
            ["test1", "hi", "world"],
            ["test2", "hi", "world"],
            ["test3", "hi", "world"],
        ]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS]
    where
        Self: 'a,
    {
        [
            ["test1", "hi", "world"],
            ["test2", "hi", "world"],
            ["test3", "hi", "world"],
        ]
    }
}

impl TestData for [u128; 3] {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [[1, 2, 3], [3, 2, 1], [300, 200, 100]]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [[1, 2, 3], [3, 2, 1], [300, 200, 100]]
    }
}

impl TestData for Vec<&str> {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS]
    where
        Self: 'a,
    {
        [
            vec!["test1", "hi", "world"],
            vec!["test2", "hi", "world"],
            vec!["test3", "hi", "world"],
        ]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS]
    where
        Self: 'a,
    {
        [
            vec!["test1", "hi", "world"],
            vec!["test2", "hi", "world"],
            vec!["test3", "hi", "world"],
        ]
    }
}

impl TestData for Option<u64> {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [None, Some(0), Some(7)]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [None, Some(0), Some(7)]
    }
}

impl TestData for (u64, &'static str) {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [(0, "hi"), (1, "bye"), (2, "byte")]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [(0, "hi"), (1, "bye"), (2, "byte")]
    }
}

impl TestData for (u64, u32) {
    fn make_data_v2_6<'a>() -> [<Self as redb2_6::Value>::SelfType<'a>; ELEMENTS] {
        [(0, 3), (1, 4), (2, 5)]
    }

    fn make_data<'a>() -> [<Self as redb::Value>::SelfType<'a>; ELEMENTS] {
        [(0, 3), (1, 4), (2, 5)]
    }
}

fn create_tempfile() -> tempfile::NamedTempFile {
    if cfg!(target_os = "wasi") {
        tempfile::NamedTempFile::new_in("/tmp").unwrap()
    } else {
        tempfile::NamedTempFile::new().unwrap()
    }
}

fn test_helper<K: TestData + redb::Key + redb2_6::Key + 'static, V: TestData + 'static>() {
    {
        let tmpfile = create_tempfile();
        let db = redb2_6::Database::builder()
            .create_with_file_format_v3(true)
            .create(tmpfile.path())
            .unwrap();
        let table_def: redb2_6::TableDefinition<K, V> = redb2_6::TableDefinition::new("table");
        let write_txn = db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(table_def).unwrap();
            for i in 0..ELEMENTS {
                table
                    .insert(&K::make_data_v2_6()[i], &V::make_data_v2_6()[i])
                    .unwrap();
            }
        }
        write_txn.commit().unwrap();
        drop(db);

        let db = redb::Database::open(tmpfile.path()).unwrap();
        let read_txn = db.begin_read().unwrap();
        let table_def: redb::TableDefinition<K, V> = redb::TableDefinition::new("table");
        let table = read_txn.open_table(table_def).unwrap();
        assert_eq!(table.len().unwrap(), ELEMENTS as u64);
        for i in 0..ELEMENTS {
            let result = table.get(&K::make_data()[i]).unwrap().unwrap();
            let value = result.value();
            let bytes = <V as redb::Value>::as_bytes(&value);
            let expected = &V::make_data()[i];
            let expected_bytes = <V as redb::Value>::as_bytes(expected);
            assert_eq!(bytes.as_ref(), expected_bytes.as_ref());
        }
    }
}

#[test]
fn primitive_types() {
    test_helper::<u8, u8>();
    test_helper::<u16, u16>();
    test_helper::<u32, u32>();
    test_helper::<u64, u64>();
    test_helper::<u128, u128>();
    test_helper::<i8, i8>();
    test_helper::<i16, i16>();
    test_helper::<i32, i32>();
    test_helper::<i64, i64>();
    test_helper::<i128, i128>();
    test_helper::<i128, f32>();
    test_helper::<i128, f64>();
    test_helper::<&str, &str>();
    test_helper::<u8, ()>();
}

#[test]
fn container_types() {
    test_helper::<&[u8], &[u8]>();
    test_helper::<&[u8; 5], &[u8; 5]>();
    test_helper::<u64, Option<u64>>();
    test_helper::<(u64, u32), &str>();
    test_helper::<[&str; 3], [u128; 3]>();
    test_helper::<u64, Vec<&str>>();
}

#[test]
fn mixed_width() {
    test_helper::<u8, &[u8]>();
    test_helper::<&[u8; 5], &str>();
}

// Invariant: a persistent savepoint created by redb 2.6 (file format v3) must remain fully
// usable in the current version: restoring and deleting it must leave the allocated-pages
// bookkeeping referencing only allocated pages, so check_integrity() reports a clean database.
#[test]
fn restore_and_delete_redb2_6_persistent_savepoint() {
    let table_def: redb::TableDefinition<u64, &[u8]> = redb::TableDefinition::new("table");
    let table_def_26: redb2_6::TableDefinition<u64, &[u8]> = redb2_6::TableDefinition::new("table");
    fn value_for(i: u64, len: usize) -> Vec<u8> {
        vec![(i % 250) as u8 + 1; len]
    }

    let tmpfile = create_tempfile();
    let savepoint_id;
    {
        let db = redb2_6::Database::builder()
            .create_with_file_format_v3(true)
            .create(tmpfile.path())
            .unwrap();
        let txn = db.begin_write().unwrap();
        {
            let mut table = txn.open_table(table_def_26).unwrap();
            for i in 0..100u64 {
                table.insert(i, value_for(i, 100).as_slice()).unwrap();
            }
        }
        txn.commit().unwrap();

        let txn = db.begin_write().unwrap();
        savepoint_id = txn.persistent_savepoint().unwrap();
        txn.commit().unwrap();

        // Modify the data after the savepoint (still in redb 2.6)
        let txn = db.begin_write().unwrap();
        {
            let mut table = txn.open_table(table_def_26).unwrap();
            for i in 0..50u64 {
                table.remove(i).unwrap();
            }
            for i in 100..200u64 {
                table.insert(i, value_for(i, 100).as_slice()).unwrap();
            }
        }
        txn.commit().unwrap();
    }

    let mut db = redb::Database::open(tmpfile.path()).unwrap();
    let ids: Vec<u64> = {
        let txn = db.begin_write().unwrap();
        let ids = txn.list_persistent_savepoints().unwrap().collect();
        txn.abort().unwrap();
        ids
    };
    assert_eq!(ids, vec![savepoint_id]);

    let mut txn = db.begin_write().unwrap();
    let savepoint = txn.get_persistent_savepoint(savepoint_id).unwrap();
    txn.restore_savepoint(&savepoint).unwrap();
    drop(savepoint);
    txn.commit().unwrap();

    let txn = db.begin_write().unwrap();
    assert!(txn.delete_persistent_savepoint(savepoint_id).unwrap());
    txn.commit().unwrap();

    // Must not panic
    assert!(db.check_integrity().unwrap());

    let txn = db.begin_read().unwrap();
    let table = txn.open_table(table_def).unwrap();
    assert_eq!(table.len().unwrap(), 100);
    for i in 0..100u64 {
        assert_eq!(
            table.get(i).unwrap().unwrap().value(),
            value_for(i, 100).as_slice()
        );
    }
}

// A composite (`Option<T>`) of a user-defined type written by redb 2.6 stored its type name with
// the `Internal` classification. The current version bubbles the inner user-defined classification
// up to the composite, so opening such a table must succeed via the legacy `Internal` spelling.
#[test]
fn composite_of_user_type_created_by_redb2_6_still_opens() {
    let tmpfile = create_tempfile();
    {
        let db = redb2_6::Database::builder()
            .create_with_file_format_v3(true)
            .create(tmpfile.path())
            .unwrap();
        let def: redb2_6::TableDefinition<u64, Option<CollidingUserType>> =
            redb2_6::TableDefinition::new("table");
        let txn = db.begin_write().unwrap();
        {
            let mut table = txn.open_table(def).unwrap();
            table.insert(&1u64, &Some(7u32)).unwrap();
            table.insert(&2u64, &None).unwrap();
        }
        txn.commit().unwrap();
    }

    let db = redb::Database::open(tmpfile.path()).unwrap();
    let def: redb::TableDefinition<u64, Option<CollidingUserType>> =
        redb::TableDefinition::new("table");
    let txn = db.begin_read().unwrap();
    let table = txn.open_table(def).unwrap();
    assert_eq!(table.len().unwrap(), 2);
    assert_eq!(table.get(&1u64).unwrap().unwrap().value(), Some(7u32));
    assert_eq!(table.get(&2u64).unwrap().unwrap().value(), None);
}

// redb 2.6 wrote `Option<u32>` and `Option<user "u32">` with the same stored type name:
// `Internal + Option<u32>`. Current redb accepts that legacy spelling for built-in composites so
// old built-in tables remain readable, which means this already-ambiguous legacy table also opens
// under the built-in spelling.
#[test]
fn legacy_colliding_user_composite_can_still_open_as_builtin() {
    let tmpfile = create_tempfile();
    {
        let db = redb2_6::Database::builder()
            .create_with_file_format_v3(true)
            .create(tmpfile.path())
            .unwrap();
        let def: redb2_6::TableDefinition<u64, Option<CollidingUserType>> =
            redb2_6::TableDefinition::new("table");
        let txn = db.begin_write().unwrap();
        {
            let mut table = txn.open_table(def).unwrap();
            table.insert(&1u64, &Some(7u32)).unwrap();
            table.insert(&2u64, &None).unwrap();
        }
        txn.commit().unwrap();
    }

    let db = redb::Database::open(tmpfile.path()).unwrap();
    let def: redb::TableDefinition<u64, Option<u32>> = redb::TableDefinition::new("table");
    let txn = db.begin_read().unwrap();
    let table = txn.open_table(def).unwrap();
    assert_eq!(table.len().unwrap(), 2);
    assert_eq!(table.get(&1u64).unwrap().unwrap().value(), Some(7u32));
    assert_eq!(table.get(&2u64).unwrap().unwrap().value(), None);
}

// The bug this guards against: a composite of a user-defined type must never silently alias the
// built-in composite with the same name string. Opening an `Option<user "u32">` table under the
// built-in `Option<u32>` must now fail with a type mismatch rather than misinterpreting the data.
#[test]
fn composite_of_user_type_does_not_alias_builtin() {
    let tmpfile = create_tempfile();
    let db = redb::Database::create(tmpfile.path()).unwrap();
    {
        let def: redb::TableDefinition<u64, Option<CollidingUserType>> =
            redb::TableDefinition::new("table");
        let txn = db.begin_write().unwrap();
        {
            let mut table = txn.open_table(def).unwrap();
            table.insert(&1u64, &Some(7u32)).unwrap();
        }
        txn.commit().unwrap();
    }

    let builtin_def: redb::TableDefinition<u64, Option<u32>> = redb::TableDefinition::new("table");
    let txn = db.begin_write().unwrap();
    assert!(matches!(
        txn.open_table(builtin_def),
        Err(redb::TableError::TableTypeMismatch { .. })
    ));
    txn.abort().unwrap();
}

// Built-in composites use the new Internal3 classification, so the legacy compatibility rule for
// a user composite must not allow it to open a newly created built-in composite in reverse.
#[test]
fn builtin_composite_does_not_alias_user_type() {
    let tmpfile = create_tempfile();
    let db = redb::Database::create(tmpfile.path()).unwrap();
    {
        let def: redb::TableDefinition<u64, Option<u32>> = redb::TableDefinition::new("table");
        let txn = db.begin_write().unwrap();
        {
            let mut table = txn.open_table(def).unwrap();
            table.insert(&1u64, &Some(7u32)).unwrap();
        }
        txn.commit().unwrap();
    }

    let user_def: redb::TableDefinition<u64, Option<CollidingUserType>> =
        redb::TableDefinition::new("table");
    let txn = db.begin_write().unwrap();
    assert!(matches!(
        txn.open_table(user_def),
        Err(redb::TableError::TableTypeMismatch { .. })
    ));
    txn.abort().unwrap();
}