libkeri 0.1.0

A Rust library for KERI (Key Event Receipt Infrastructure)
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
use crate::keri::db::dbing::{BytesDatabase, LMDBer};
use crate::keri::db::subing::dup::DupSuber;
use crate::keri::db::subing::{SuberError, Utf8Codec, ValueCodec};
use std::marker::PhantomData;
use std::sync::Arc;

/// IoDupSuber - Sub class of DupSuber that supports Insertion Ordering (IoDup) of duplicates
///
/// By automagically prepending and stripping ordinal proem to/from each
/// duplicate value at a given key.
///
/// IoDupSuber supports insertion ordered multiple entries at each key
/// (duplicates) with dupsort==True
///
/// Do not use if serialized length key + proem + value, is greater than 511 bytes.
/// This is a limitation of dupsort==True sub dbs in LMDB
///
/// IoDupSuber may be more performant then IoSetSuber for values that are indices
/// to other sub dbs that fit the size constraint because LMDB support for
/// duplicates is more space efficient and code performant.
///
/// Duplicates at a given key preserve insertion order of duplicate.
/// Because lmdb is lexocographic an insertion ordering proem is prepended to
/// all values that makes lexocographic order that same as insertion order.
///
/// Duplicates are ordered as a pair of key plus value so prepending proem
/// to each value changes duplicate ordering. Proem is 33 characters long.
/// With 32 character hex string followed by '.' for essentially unlimited
/// number of values which will be limited by memory.
///
/// With prepended proem ordinal must explicitly check for duplicate values
/// before insertion. Uses a set for the duplicate inclusion test.
pub struct IoDupSuber<'db, C: ValueCodec = Utf8Codec> {
    base: DupSuber<'db, C>,
}

impl<'db, C: ValueCodec> IoDupSuber<'db, C> {
    /// Creates a new `IoDupSuber` instance.
    pub fn new(
        db: Arc<&'db LMDBer>,
        subkey: &str,
        sep: Option<u8>,
        verify: bool,
    ) -> Result<Self, SuberError> {
        let base = DupSuber::new(db, subkey, sep, verify)?;

        Ok(Self { base })
    }

    /// Puts all vals idempotently at key made from keys in insertion order using
    /// hidden ordinal proem. Idempotently means do not put any val in vals that is
    /// already in dup vals at key. Does not overwrite.
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts to be combined to form the key
    /// * `vals` - Slice of values to be stored
    ///
    /// # Returns
    /// * `Result<bool, SuberError>` - True if successful, otherwise an error
    pub fn put<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
        &self,
        keys: &[K],
        vals: &[&V],
    ) -> Result<bool, SuberError> {
        let key = self.base.base.to_key(keys, false);

        // Serialize all values
        let serialized_vals: Vec<Vec<u8>> = vals
            .iter()
            .map(|v| self.base.base.ser(*v))
            .collect::<Result<Vec<Vec<u8>>, _>>()?;

        // Create Vec<&[u8]> by borrowing from serialized_vals
        let val_slices: Vec<&[u8]> = serialized_vals.iter().map(|v| v.as_slice()).collect();

        // Use the put_io_dup_vals method for insertion ordered duplicates
        self.base
            .base
            .db
            .put_io_dup_vals(&self.base.base.sdb, &key, &val_slices)
            .map_err(SuberError::DBError)
    }

    /// Add val idempotently at key made from keys in insertion order using hidden
    /// ordinal proem. Idempotently means do not add val that is already in
    /// dup vals at key. Does not overwrite.
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts to be combined to form the key
    /// * `val` - Value to be stored
    ///
    /// # Returns
    /// * `Result<bool, SuberError>` - True means unique value added among duplications,
    ///   False means duplicate of same value already exists.
    pub fn add<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
        &self,
        keys: &[K],
        val: &V,
    ) -> Result<bool, SuberError> {
        let key = self.base.base.to_key(keys, false);
        let sval = self.base.base.ser(val)?;

        self.base
            .base
            .db
            .add_io_dup_val(&self.base.base.sdb, &key, &sval)
            .map_err(SuberError::DBError)
    }

    /// Pins (sets) vals at key made from keys in insertion order using hidden
    /// ordinal proem. Overwrites. Removes all pre-existing vals that share
    /// same keys and replaces them with vals
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts to be combined to form the key
    /// * `vals` - Slice of values to be stored
    ///
    /// # Returns
    /// * `Result<bool, SuberError>` - True if successful, otherwise an error
    pub fn pin<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
        &self,
        keys: &[K],
        vals: &[&V],
    ) -> Result<bool, SuberError> {
        let key = self.base.base.to_key(keys, false);

        // Delete all values at the key first
        self.base
            .base
            .db
            .del_io_dup_vals(&self.base.base.sdb, &key)
            .map_err(SuberError::DBError)?;

        // If there are no values to add, return true (successful operation)
        if vals.is_empty() {
            return Ok(true);
        }

        // Serialize all values
        let serialized_vals: Vec<Vec<u8>> = vals
            .iter()
            .map(|v| self.base.base.ser(*v))
            .collect::<Result<Vec<Vec<u8>>, _>>()?;

        // Create Vec<&[u8]> by borrowing from serialized_vals
        let val_slices: Vec<&[u8]> = serialized_vals.iter().map(|v| v.as_slice()).collect();

        // Add the new values
        self.base
            .base
            .db
            .put_io_dup_vals(&self.base.base.sdb, &key, &val_slices)
            .map_err(SuberError::DBError)
    }

    /// Gets vals dup list in insertion order using key made from keys and
    /// hidden ordinal proem on dups.
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts to be combined to form the key
    ///
    /// # Returns
    /// * `Result<Vec<R>, SuberError>` - Vector of deserialized values, empty if none found
    pub fn get<K: AsRef<[u8]>, R: TryFrom<Vec<u8>>>(&self, keys: &[K]) -> Result<Vec<R>, SuberError>
    where
        <R as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
    {
        let key = self.base.base.to_key(keys, false);
        let values = self
            .base
            .base
            .db
            .get_io_dup_vals(&self.base.base.sdb, &key)
            .map_err(SuberError::DBError)?;

        values.iter().map(|val| self.base.base.des(val)).collect()
    }

    /// Gets vals dup iterator in insertion order using key made from keys and
    /// hidden ordinal proem on dups.
    /// All vals in dups that share same key are retrieved in insertion order.
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts to be combined to form the key
    ///
    /// # Returns
    /// * `Result<impl Iterator<Item = Result<R, SuberError>>, SuberError>` - Iterator of results
    pub fn get_iter<K: AsRef<[u8]>, R: TryFrom<Vec<u8>> + 'static>(
        &self,
        keys: &[K],
    ) -> Result<impl Iterator<Item = Result<R, SuberError>>, SuberError>
    where
        <R as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
        SuberError: From<<R as TryFrom<Vec<u8>>>::Error>,
    {
        let key = self.base.base.to_key(keys, false);
        let mut results: Vec<Result<R, SuberError>> = Vec::new();

        self.base
            .base
            .db
            .get_io_dup_vals_iter(&self.base.base.sdb, &key, |val| {
                let result = self.base.base.des(val);
                results.push(result);
                Ok(true)
            })
            .map_err(SuberError::DBError)?;

        Ok(results.into_iter())
    }

    /// Gets last val inserted at key made from keys in insertion order using
    /// hidden ordinal proem.
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts to be combined to form the key
    ///
    /// # Returns
    /// * `Result<Option<R>, SuberError>` - The last value if found, None otherwise
    pub fn get_last<K: AsRef<[u8]>, R: TryFrom<Vec<u8>>>(
        &self,
        keys: &[K],
    ) -> Result<Option<R>, SuberError>
    where
        <R as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
    {
        let key = self.base.base.to_key(keys, false);
        let raw_val_opt = self
            .base
            .base
            .db
            .get_io_dup_val_last(&self.base.base.sdb, &key)
            .map_err(SuberError::DBError)?;

        match raw_val_opt {
            Some(val) => self.base.base.des(&val).map(Some),
            None => Ok(None),
        }
    }

    /// Removes entry at key made from keys and dup val that matches val if any,
    /// notwithstanding hidden ordinal proem. Otherwise deletes all dup values
    /// at key if val is None.
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts to be combined to form the key
    /// * `val` - Optional value to remove; if None, removes all values at the key
    ///
    /// # Returns
    /// * `Result<bool, SuberError>` - True if successful, False otherwise
    pub fn rem<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
        &self,
        keys: &[K],
        val: Option<&V>,
    ) -> Result<bool, SuberError> {
        let key = self.base.base.to_key(keys, false);

        match val {
            Some(v) => {
                let sval = self.base.base.ser(v)?;
                self.base
                    .base
                    .db
                    .del_io_dup_val(&self.base.base.sdb, &key, &sval)
                    .map_err(SuberError::DBError)
            }
            None => self
                .base
                .base
                .db
                .del_io_dup_vals(&self.base.base.sdb, &key)
                .map_err(SuberError::DBError),
        }
    }

    /// Return count of dup values at key made from keys with hidden ordinal
    /// proem. Zero otherwise
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts to be combined to form the key
    ///
    /// # Returns
    /// * `Result<usize, SuberError>` - Count of values
    pub fn cnt<K: AsRef<[u8]>>(&self, keys: &[K]) -> Result<usize, SuberError> {
        let key = self.base.base.to_key(keys, false);
        self.base
            .base
            .db
            .cnt_io_dup_vals(&self.base.base.sdb, &key)
            .map_err(SuberError::DBError)
    }

    /// Return iterator over all the items including dup items for all keys
    /// in top branch defined by keys where keys may be truncation of full branch.
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts, potentially a partial key
    /// * `topive` - If true, treat as partial key tuple ending with separator
    ///
    /// # Returns
    /// * `Result<Vec<(Vec<Vec<u8>>, Vec<u8>)>, SuberError>` - Vector of key-value pairs
    pub fn get_item_iter<K: AsRef<[u8]>>(
        &self,
        keys: &[K],
        topive: bool,
    ) -> Result<Vec<(Vec<Vec<u8>>, Vec<u8>)>, SuberError> {
        let key = self.base.base.to_key(keys, topive);

        let mut result = Vec::new();
        self.base
            .base
            .db
            .get_top_io_dup_item_iter(&self.base.base.sdb, &key, |k, v| {
                result.push((self.base.base.to_keys(k), v.to_vec()));
                Ok(true)
            })
            .map_err(SuberError::DBError)?;

        Ok(result)
    }

    /// Returns an iterator over all the items including dup items for all keys
    /// in top branch defined by keys where keys may be truncation of full branch.
    /// Returns full raw values with ordinal proems.
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts, potentially a partial key
    /// * `topive` - If true, treat as partial key tuple ending with separator
    ///
    /// # Returns
    /// * `Result<Vec<(Vec<Vec<u8>>, Vec<u8>)>, SuberError>` - Vector of key-value pairs with raw values
    pub fn get_full_item_iter<K: AsRef<[u8]>>(
        &self,
        keys: &[K],
        topive: bool,
    ) -> Result<Vec<(Vec<Vec<u8>>, Vec<u8>)>, SuberError> {
        self.base.base.get_full_item_iter(keys, topive)
    }

    /// Removes all entries at keys that are in top branch with key prefix matching
    /// keys where keys may be truncation of full branch.
    ///
    /// # Arguments
    /// * `keys` - Slice of key parts, potentially a partial key
    /// * `topive` - If true, treat as partial key tuple ending with separator
    ///
    /// # Returns
    /// * `Result<bool, SuberError>` - True if successful
    pub fn trim<K: AsRef<[u8]>>(&self, keys: &[K], topive: bool) -> Result<bool, SuberError> {
        self.base.base.trim(keys, topive)
    }

    /// Returns whether this SuberBase is configured to support duplicate values for keys.
    ///
    /// # Returns
    /// * `bool` - True if duplicates are allowed
    pub fn is_dupsort(&self) -> bool {
        self.base.is_dupsort()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::keri::db::dbing::LMDBer;
    use std::sync::Arc;

    // Helper function to convert keys and value to string representation
    fn key_val_to_string(key_val: &(Vec<Vec<u8>>, Vec<u8>)) -> (Vec<String>, String) {
        let (keys, val) = key_val;
        let string_keys = keys
            .iter()
            .map(|k| String::from_utf8(k.clone()).unwrap())
            .collect();
        let string_val = String::from_utf8(val.clone()).unwrap();
        (string_keys, string_val)
    }

    // Helper function to convert items to string tuples for easier assertions
    fn items_to_string_tuples(items: &[(Vec<Vec<u8>>, Vec<u8>)]) -> Vec<(Vec<String>, String)> {
        items.iter().map(key_val_to_string).collect()
    }

    #[test]
    fn test_io_dup_suber() -> Result<(), SuberError> {
        // Create a temporary database for the test
        let lmdber = LMDBer::builder().name("test").temp(true).build()?;

        assert_eq!(lmdber.name(), "test");
        assert!(lmdber.opened());

        // Create IoDupSuber
        let ioduber: IoDupSuber<Utf8Codec> =
            IoDupSuber::new(Arc::new(&lmdber), "bags.", None, false)?;
        assert!(ioduber.is_dupsort());

        // Test data
        let sue = "Hello sailer!";
        let sal = "Not my type.";

        let keys0 = &["test_key", "0001"];
        let keys1 = &["test_key", "0002"];

        // Test put and get methods with string conversion for direct comparison
        assert!(ioduber.put(keys0, &[&sal, &sue])?);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys0)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert_eq!(actuals, vec![sal.to_string(), sue.to_string()]); // insertion order not lexicographic
        assert_eq!(ioduber.cnt(keys0)?, 2);

        // Test getLast method
        let last_bytes: Option<Vec<u8>> = ioduber.get_last(keys0)?;
        let actual = last_bytes.map(|b| String::from_utf8(b).unwrap());
        assert_eq!(actual, Some(sue.to_string()));

        // Test remove method
        assert!(ioduber.rem(keys0, None::<&String>)?);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys0)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert!(actuals.is_empty());
        assert_eq!(actuals, Vec::<String>::new());
        assert_eq!(ioduber.cnt(keys0)?, 0);

        // Test put and get again
        assert!(ioduber.put(keys0, &[&sue, &sal])?);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys0)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert_eq!(actuals, vec![sue.to_string(), sal.to_string()]); // insertion order

        let last_bytes: Option<Vec<u8>> = ioduber.get_last(keys0)?;
        let actual = last_bytes.map(|b| String::from_utf8(b).unwrap());
        assert_eq!(actual, Some(sal.to_string()));

        // Test add method
        let sam = "A real charmer!";
        let result = ioduber.add(keys0, &sam)?;
        assert!(result);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys0)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert_eq!(
            actuals,
            vec![sue.to_string(), sal.to_string(), sam.to_string()]
        ); // insertion order

        // Test pin method
        let zoe = "See ya later.";
        let zia = "Hey gorgeous!";
        let result = ioduber.pin(keys0, &[&zoe, &zia])?;
        assert!(result);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys0)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert_eq!(actuals, vec![zoe.to_string(), zia.to_string()]); // insertion order

        // Test with multiple keys
        assert!(ioduber.put(keys1, &[&sal, &sue, &sam])?);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys1)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert_eq!(
            actuals,
            vec![sal.to_string(), sue.to_string(), sam.to_string()]
        );

        // Test getIter - equivalent to Python's getIter
        let mut i = 0;
        for val_result in ioduber.get_iter::<_, Vec<u8>>(keys1)? {
            let val = String::from_utf8(val_result?).unwrap();
            assert_eq!(val, actuals[i]);
            i += 1;
        }

        // Test getItemIter - equivalent to Python's getItemIter
        let empty_key: &[&str] = &[];
        let items = ioduber.get_item_iter(empty_key, false)?;
        let string_items = items_to_string_tuples(&items);

        // Direct assertion of expected key-value structures
        assert_eq!(
            string_items,
            vec![
                (
                    vec!["test_key".to_string(), "0001".to_string()],
                    "See ya later.".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0001".to_string()],
                    "Hey gorgeous!".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "Not my type.".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "Hello sailer!".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "A real charmer!".to_string()
                ),
            ]
        );

        // Test getFullItemIter - equivalent to Python's getFullItemIter
        let items = ioduber.get_full_item_iter(empty_key, false)?;
        let string_items = items_to_string_tuples(&items);

        // Check full items with proem
        assert_eq!(
            string_items,
            vec![
                (
                    vec!["test_key".to_string(), "0001".to_string()],
                    "00000000000000000000000000000000.See ya later.".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0001".to_string()],
                    "00000000000000000000000000000001.Hey gorgeous!".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "00000000000000000000000000000000.Not my type.".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "00000000000000000000000000000001.Hello sailer!".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "00000000000000000000000000000002.A real charmer!".to_string()
                ),
            ]
        );

        // Test getItemIter with specific keys = keys1
        let items = ioduber.get_item_iter(keys1, false)?;
        let string_items = items_to_string_tuples(&items);

        assert_eq!(
            string_items,
            vec![
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "Not my type.".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "Hello sailer!".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "A real charmer!".to_string()
                ),
            ]
        );

        // Test getItemIter with specific keys = keys0
        let items = ioduber.get_item_iter(keys0, false)?;
        let string_items = items_to_string_tuples(&items);

        assert_eq!(
            string_items,
            vec![
                (
                    vec!["test_key".to_string(), "0001".to_string()],
                    "See ya later.".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0001".to_string()],
                    "Hey gorgeous!".to_string()
                ),
            ]
        );

        // Test with top keys
        let test_pop = &["test", "pop"];
        assert!(ioduber.put(test_pop, &[&sal, &sue, &sam])?);
        let topkeys = &["test", ""];
        let items = ioduber.get_item_iter(topkeys, false)?;
        let string_items = items_to_string_tuples(&items);

        assert_eq!(
            string_items,
            vec![
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "Not my type.".to_string()
                ),
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "Hello sailer!".to_string()
                ),
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "A real charmer!".to_string()
                ),
            ]
        );

        // Test with top parameter
        let keys = &["test"];
        let items = ioduber.get_item_iter(keys, true)?;
        let string_items = items_to_string_tuples(&items);

        assert_eq!(
            string_items,
            vec![
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "Not my type.".to_string()
                ),
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "Hello sailer!".to_string()
                ),
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "A real charmer!".to_string()
                ),
            ]
        );

        // Test IoItems (getFullItemIter with keys)
        let items = ioduber.get_full_item_iter(topkeys, false)?;
        let string_items = items_to_string_tuples(&items);

        assert_eq!(
            string_items,
            vec![
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "00000000000000000000000000000000.Not my type.".to_string()
                ),
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "00000000000000000000000000000001.Hello sailer!".to_string()
                ),
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "00000000000000000000000000000002.A real charmer!".to_string()
                ),
            ]
        );

        // Test remove with a specific val
        assert!(ioduber.rem(keys1, Some(&sue))?);

        // Sequence of state validation after remove operation
        let items = ioduber.get_item_iter(empty_key, false)?;
        let string_items = items_to_string_tuples(&items);

        assert_eq!(
            string_items,
            vec![
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "Not my type.".to_string()
                ),
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "Hello sailer!".to_string()
                ),
                (
                    vec!["test".to_string(), "pop".to_string()],
                    "A real charmer!".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0001".to_string()],
                    "See ya later.".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0001".to_string()],
                    "Hey gorgeous!".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "Not my type.".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "A real charmer!".to_string()
                ),
            ]
        );

        // Test trim with specific keys
        assert!(ioduber.trim(topkeys, false)?);

        // Sequence of state validation after trim operation
        let items = ioduber.get_item_iter(empty_key, false)?;
        let string_items = items_to_string_tuples(&items);

        assert_eq!(
            string_items,
            vec![
                (
                    vec!["test_key".to_string(), "0001".to_string()],
                    "See ya later.".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0001".to_string()],
                    "Hey gorgeous!".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "Not my type.".to_string()
                ),
                (
                    vec!["test_key".to_string(), "0002".to_string()],
                    "A real charmer!".to_string()
                ),
            ]
        );

        assert_eq!(ioduber.cnt(keys0)?, 2);
        assert_eq!(ioduber.cnt(keys1)?, 2);

        // Test with keys as string not tuple
        let keys2 = &["keystr"];
        let bob = "Shove off!";
        assert!(ioduber.put(keys2, &[&bob])?);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys2)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert_eq!(actuals, vec![bob.to_string()]);
        assert_eq!(ioduber.cnt(keys2)?, 1);

        assert!(ioduber.rem(keys2, None::<&String>)?);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys2)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert!(actuals.is_empty());
        assert_eq!(ioduber.cnt(keys2)?, 0);

        assert!(ioduber.put(keys2, &[&bob])?);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys2)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert_eq!(actuals, vec![bob.to_string()]);

        let bil = "Go away.";
        assert!(ioduber.pin(keys2, &[&bil])?);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys2)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert_eq!(actuals, vec![bil.to_string()]);

        assert!(ioduber.add(keys2, &bob)?);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys2)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert_eq!(actuals, vec![bil.to_string(), bob.to_string()]);

        // Test trim of entire database
        assert!(ioduber.trim(&[] as &[&str], false)?); // default trims whole database
        assert!(ioduber.put(keys1, &[&bob, &bil])?);
        let bytes: Vec<Vec<u8>> = ioduber.get(keys1)?;
        let actuals: Vec<String> = bytes
            .into_iter()
            .map(|b| String::from_utf8(b).unwrap())
            .collect();
        assert_eq!(actuals, vec![bob.to_string(), bil.to_string()]);

        // Check database is auto-closed when test ends (similar to Python's context manager)
        // This happens automatically when lmdber goes out of scope

        Ok(())
    }
}