Skip to main content

libkeri/keri/db/subing/
iodup.rs

1use crate::keri::db::dbing::{BytesDatabase, LMDBer};
2use crate::keri::db::subing::dup::DupSuber;
3use crate::keri::db::subing::{SuberError, Utf8Codec, ValueCodec};
4use std::marker::PhantomData;
5use std::sync::Arc;
6
7/// IoDupSuber - Sub class of DupSuber that supports Insertion Ordering (IoDup) of duplicates
8///
9/// By automagically prepending and stripping ordinal proem to/from each
10/// duplicate value at a given key.
11///
12/// IoDupSuber supports insertion ordered multiple entries at each key
13/// (duplicates) with dupsort==True
14///
15/// Do not use if serialized length key + proem + value, is greater than 511 bytes.
16/// This is a limitation of dupsort==True sub dbs in LMDB
17///
18/// IoDupSuber may be more performant then IoSetSuber for values that are indices
19/// to other sub dbs that fit the size constraint because LMDB support for
20/// duplicates is more space efficient and code performant.
21///
22/// Duplicates at a given key preserve insertion order of duplicate.
23/// Because lmdb is lexocographic an insertion ordering proem is prepended to
24/// all values that makes lexocographic order that same as insertion order.
25///
26/// Duplicates are ordered as a pair of key plus value so prepending proem
27/// to each value changes duplicate ordering. Proem is 33 characters long.
28/// With 32 character hex string followed by '.' for essentially unlimited
29/// number of values which will be limited by memory.
30///
31/// With prepended proem ordinal must explicitly check for duplicate values
32/// before insertion. Uses a set for the duplicate inclusion test.
33pub struct IoDupSuber<C: ValueCodec = Utf8Codec> {
34    base: DupSuber<C>,
35}
36
37impl<C: ValueCodec> IoDupSuber<C> {
38    /// Creates a new `IoDupSuber` instance.
39    pub fn new(
40        db: Arc<LMDBer>,
41        subkey: &str,
42        sep: Option<u8>,
43        verify: bool,
44    ) -> Result<Self, SuberError> {
45        let base = DupSuber::new(db, subkey, sep, verify)?;
46
47        Ok(Self { base })
48    }
49
50    /// Puts all vals idempotently at key made from keys in insertion order using
51    /// hidden ordinal proem. Idempotently means do not put any val in vals that is
52    /// already in dup vals at key. Does not overwrite.
53    ///
54    /// # Arguments
55    /// * `keys` - Slice of key parts to be combined to form the key
56    /// * `vals` - Slice of values to be stored
57    ///
58    /// # Returns
59    /// * `Result<bool, SuberError>` - True if successful, otherwise an error
60    pub fn put<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
61        &self,
62        keys: &[K],
63        vals: &[&V],
64    ) -> Result<bool, SuberError> {
65        let key = self.base.base.to_key(keys, false);
66
67        // Serialize all values
68        let serialized_vals: Vec<Vec<u8>> = vals
69            .iter()
70            .map(|v| self.base.base.ser(*v))
71            .collect::<Result<Vec<Vec<u8>>, _>>()?;
72
73        // Create Vec<&[u8]> by borrowing from serialized_vals
74        let val_slices: Vec<&[u8]> = serialized_vals.iter().map(|v| v.as_slice()).collect();
75
76        // Use the put_io_dup_vals method for insertion ordered duplicates
77        self.base
78            .base
79            .db
80            .put_io_dup_vals(&self.base.base.sdb, &key, &val_slices)
81            .map_err(SuberError::DBError)
82    }
83
84    /// Add val idempotently at key made from keys in insertion order using hidden
85    /// ordinal proem. Idempotently means do not add val that is already in
86    /// dup vals at key. Does not overwrite.
87    ///
88    /// # Arguments
89    /// * `keys` - Slice of key parts to be combined to form the key
90    /// * `val` - Value to be stored
91    ///
92    /// # Returns
93    /// * `Result<bool, SuberError>` - True means unique value added among duplications,
94    ///   False means duplicate of same value already exists.
95    pub fn add<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
96        &self,
97        keys: &[K],
98        val: &V,
99    ) -> Result<bool, SuberError> {
100        let key = self.base.base.to_key(keys, false);
101        let sval = self.base.base.ser(val)?;
102
103        self.base
104            .base
105            .db
106            .add_io_dup_val(&self.base.base.sdb, &key, &sval)
107            .map_err(SuberError::DBError)
108    }
109
110    /// Pins (sets) vals at key made from keys in insertion order using hidden
111    /// ordinal proem. Overwrites. Removes all pre-existing vals that share
112    /// same keys and replaces them with vals
113    ///
114    /// # Arguments
115    /// * `keys` - Slice of key parts to be combined to form the key
116    /// * `vals` - Slice of values to be stored
117    ///
118    /// # Returns
119    /// * `Result<bool, SuberError>` - True if successful, otherwise an error
120    pub fn pin<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
121        &self,
122        keys: &[K],
123        vals: &[&V],
124    ) -> Result<bool, SuberError> {
125        let key = self.base.base.to_key(keys, false);
126
127        // Delete all values at the key first
128        self.base
129            .base
130            .db
131            .del_io_dup_vals(&self.base.base.sdb, &key)
132            .map_err(SuberError::DBError)?;
133
134        // If there are no values to add, return true (successful operation)
135        if vals.is_empty() {
136            return Ok(true);
137        }
138
139        // Serialize all values
140        let serialized_vals: Vec<Vec<u8>> = vals
141            .iter()
142            .map(|v| self.base.base.ser(*v))
143            .collect::<Result<Vec<Vec<u8>>, _>>()?;
144
145        // Create Vec<&[u8]> by borrowing from serialized_vals
146        let val_slices: Vec<&[u8]> = serialized_vals.iter().map(|v| v.as_slice()).collect();
147
148        // Add the new values
149        self.base
150            .base
151            .db
152            .put_io_dup_vals(&self.base.base.sdb, &key, &val_slices)
153            .map_err(SuberError::DBError)
154    }
155
156    /// Gets vals dup list in insertion order using key made from keys and
157    /// hidden ordinal proem on dups.
158    ///
159    /// # Arguments
160    /// * `keys` - Slice of key parts to be combined to form the key
161    ///
162    /// # Returns
163    /// * `Result<Vec<R>, SuberError>` - Vector of deserialized values, empty if none found
164    pub fn get<K: AsRef<[u8]>, R: TryFrom<Vec<u8>>>(&self, keys: &[K]) -> Result<Vec<R>, SuberError>
165    where
166        <R as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
167    {
168        let key = self.base.base.to_key(keys, false);
169        let values = self
170            .base
171            .base
172            .db
173            .get_io_dup_vals(&self.base.base.sdb, &key)
174            .map_err(SuberError::DBError)?;
175
176        values.iter().map(|val| self.base.base.des(val)).collect()
177    }
178
179    /// Gets vals dup iterator in insertion order using key made from keys and
180    /// hidden ordinal proem on dups.
181    /// All vals in dups that share same key are retrieved in insertion order.
182    ///
183    /// # Arguments
184    /// * `keys` - Slice of key parts to be combined to form the key
185    ///
186    /// # Returns
187    /// * `Result<impl Iterator<Item = Result<R, SuberError>>, SuberError>` - Iterator of results
188    pub fn get_iter<K: AsRef<[u8]>, R: TryFrom<Vec<u8>> + 'static>(
189        &self,
190        keys: &[K],
191    ) -> Result<impl Iterator<Item = Result<R, SuberError>>, SuberError>
192    where
193        <R as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
194        SuberError: From<<R as TryFrom<Vec<u8>>>::Error>,
195    {
196        let key = self.base.base.to_key(keys, false);
197        let mut results: Vec<Result<R, SuberError>> = Vec::new();
198
199        self.base
200            .base
201            .db
202            .get_io_dup_vals_iter(&self.base.base.sdb, &key, |val| {
203                let result = self.base.base.des(val);
204                results.push(result);
205                Ok(true)
206            })
207            .map_err(SuberError::DBError)?;
208
209        Ok(results.into_iter())
210    }
211
212    /// Gets last val inserted at key made from keys in insertion order using
213    /// hidden ordinal proem.
214    ///
215    /// # Arguments
216    /// * `keys` - Slice of key parts to be combined to form the key
217    ///
218    /// # Returns
219    /// * `Result<Option<R>, SuberError>` - The last value if found, None otherwise
220    pub fn get_last<K: AsRef<[u8]>, R: TryFrom<Vec<u8>>>(
221        &self,
222        keys: &[K],
223    ) -> Result<Option<R>, SuberError>
224    where
225        <R as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
226    {
227        let key = self.base.base.to_key(keys, false);
228        let raw_val_opt = self
229            .base
230            .base
231            .db
232            .get_io_dup_val_last(&self.base.base.sdb, &key)
233            .map_err(SuberError::DBError)?;
234
235        match raw_val_opt {
236            Some(val) => self.base.base.des(&val).map(Some),
237            None => Ok(None),
238        }
239    }
240
241    /// Removes entry at key made from keys and dup val that matches val if any,
242    /// notwithstanding hidden ordinal proem. Otherwise deletes all dup values
243    /// at key if val is None.
244    ///
245    /// # Arguments
246    /// * `keys` - Slice of key parts to be combined to form the key
247    /// * `val` - Optional value to remove; if None, removes all values at the key
248    ///
249    /// # Returns
250    /// * `Result<bool, SuberError>` - True if successful, False otherwise
251    pub fn rem<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
252        &self,
253        keys: &[K],
254        val: Option<&V>,
255    ) -> Result<bool, SuberError> {
256        let key = self.base.base.to_key(keys, false);
257
258        match val {
259            Some(v) => {
260                let sval = self.base.base.ser(v)?;
261                self.base
262                    .base
263                    .db
264                    .del_io_dup_val(&self.base.base.sdb, &key, &sval)
265                    .map_err(SuberError::DBError)
266            }
267            None => self
268                .base
269                .base
270                .db
271                .del_io_dup_vals(&self.base.base.sdb, &key)
272                .map_err(SuberError::DBError),
273        }
274    }
275
276    /// Return count of dup values at key made from keys with hidden ordinal
277    /// proem. Zero otherwise
278    ///
279    /// # Arguments
280    /// * `keys` - Slice of key parts to be combined to form the key
281    ///
282    /// # Returns
283    /// * `Result<usize, SuberError>` - Count of values
284    pub fn cnt<K: AsRef<[u8]>>(&self, keys: &[K]) -> Result<usize, SuberError> {
285        let key = self.base.base.to_key(keys, false);
286        self.base
287            .base
288            .db
289            .cnt_io_dup_vals(&self.base.base.sdb, &key)
290            .map_err(SuberError::DBError)
291    }
292
293    /// Return iterator over all the items including dup items for all keys
294    /// in top branch defined by keys where keys may be truncation of full branch.
295    ///
296    /// # Arguments
297    /// * `keys` - Slice of key parts, potentially a partial key
298    /// * `topive` - If true, treat as partial key tuple ending with separator
299    ///
300    /// # Returns
301    /// * `Result<Vec<(Vec<Vec<u8>>, Vec<u8>)>, SuberError>` - Vector of key-value pairs
302    pub fn get_item_iter<K: AsRef<[u8]>>(
303        &self,
304        keys: &[K],
305        topive: bool,
306    ) -> Result<Vec<(Vec<Vec<u8>>, Vec<u8>)>, SuberError> {
307        let key = self.base.base.to_key(keys, topive);
308
309        let mut result = Vec::new();
310        self.base
311            .base
312            .db
313            .get_top_io_dup_item_iter(&self.base.base.sdb, &key, |k, v| {
314                result.push((self.base.base.to_keys(k), v.to_vec()));
315                Ok(true)
316            })
317            .map_err(SuberError::DBError)?;
318
319        Ok(result)
320    }
321
322    /// Returns an iterator over all the items including dup items for all keys
323    /// in top branch defined by keys where keys may be truncation of full branch.
324    /// Returns full raw values with ordinal proems.
325    ///
326    /// # Arguments
327    /// * `keys` - Slice of key parts, potentially a partial key
328    /// * `topive` - If true, treat as partial key tuple ending with separator
329    ///
330    /// # Returns
331    /// * `Result<Vec<(Vec<Vec<u8>>, Vec<u8>)>, SuberError>` - Vector of key-value pairs with raw values
332    pub fn get_full_item_iter<K: AsRef<[u8]>>(
333        &self,
334        keys: &[K],
335        topive: bool,
336    ) -> Result<Vec<(Vec<Vec<u8>>, Vec<u8>)>, SuberError> {
337        self.base.base.get_full_item_iter(keys, topive)
338    }
339
340    /// Removes all entries at keys that are in top branch with key prefix matching
341    /// keys where keys may be truncation of full branch.
342    ///
343    /// # Arguments
344    /// * `keys` - Slice of key parts, potentially a partial key
345    /// * `topive` - If true, treat as partial key tuple ending with separator
346    ///
347    /// # Returns
348    /// * `Result<bool, SuberError>` - True if successful
349    pub fn trim<K: AsRef<[u8]>>(&self, keys: &[K], topive: bool) -> Result<bool, SuberError> {
350        self.base.base.trim(keys, topive)
351    }
352
353    /// Returns whether this SuberBase is configured to support duplicate values for keys.
354    ///
355    /// # Returns
356    /// * `bool` - True if duplicates are allowed
357    pub fn is_dupsort(&self) -> bool {
358        self.base.is_dupsort()
359    }
360}
361
362#[cfg(test)]
363mod tests {
364    use super::*;
365    use crate::keri::db::dbing::LMDBer;
366    use std::sync::Arc;
367
368    // Helper function to convert keys and value to string representation
369    fn key_val_to_string(key_val: &(Vec<Vec<u8>>, Vec<u8>)) -> (Vec<String>, String) {
370        let (keys, val) = key_val;
371        let string_keys = keys
372            .iter()
373            .map(|k| String::from_utf8(k.clone()).unwrap())
374            .collect();
375        let string_val = String::from_utf8(val.clone()).unwrap();
376        (string_keys, string_val)
377    }
378
379    // Helper function to convert items to string tuples for easier assertions
380    fn items_to_string_tuples(items: &[(Vec<Vec<u8>>, Vec<u8>)]) -> Vec<(Vec<String>, String)> {
381        items.iter().map(key_val_to_string).collect()
382    }
383
384    #[test]
385    fn test_io_dup_suber() -> Result<(), SuberError> {
386        // Create a temporary database for the test
387        let lmdber = LMDBer::builder().name("test").temp(true).build()?;
388
389        assert_eq!(lmdber.name(), "test");
390        assert!(lmdber.opened());
391
392        // Create IoDupSuber
393        let ioduber: IoDupSuber<Utf8Codec> =
394            IoDupSuber::new(Arc::new(lmdber), "bags.", None, false)?;
395        assert!(ioduber.is_dupsort());
396
397        // Test data
398        let sue = "Hello sailer!";
399        let sal = "Not my type.";
400
401        let keys0 = &["test_key", "0001"];
402        let keys1 = &["test_key", "0002"];
403
404        // Test put and get methods with string conversion for direct comparison
405        assert!(ioduber.put(keys0, &[&sal, &sue])?);
406        let bytes: Vec<Vec<u8>> = ioduber.get(keys0)?;
407        let actuals: Vec<String> = bytes
408            .into_iter()
409            .map(|b| String::from_utf8(b).unwrap())
410            .collect();
411        assert_eq!(actuals, vec![sal.to_string(), sue.to_string()]); // insertion order not lexicographic
412        assert_eq!(ioduber.cnt(keys0)?, 2);
413
414        // Test getLast method
415        let last_bytes: Option<Vec<u8>> = ioduber.get_last(keys0)?;
416        let actual = last_bytes.map(|b| String::from_utf8(b).unwrap());
417        assert_eq!(actual, Some(sue.to_string()));
418
419        // Test remove method
420        assert!(ioduber.rem(keys0, None::<&String>)?);
421        let bytes: Vec<Vec<u8>> = ioduber.get(keys0)?;
422        let actuals: Vec<String> = bytes
423            .into_iter()
424            .map(|b| String::from_utf8(b).unwrap())
425            .collect();
426        assert!(actuals.is_empty());
427        assert_eq!(actuals, Vec::<String>::new());
428        assert_eq!(ioduber.cnt(keys0)?, 0);
429
430        // Test put and get again
431        assert!(ioduber.put(keys0, &[&sue, &sal])?);
432        let bytes: Vec<Vec<u8>> = ioduber.get(keys0)?;
433        let actuals: Vec<String> = bytes
434            .into_iter()
435            .map(|b| String::from_utf8(b).unwrap())
436            .collect();
437        assert_eq!(actuals, vec![sue.to_string(), sal.to_string()]); // insertion order
438
439        let last_bytes: Option<Vec<u8>> = ioduber.get_last(keys0)?;
440        let actual = last_bytes.map(|b| String::from_utf8(b).unwrap());
441        assert_eq!(actual, Some(sal.to_string()));
442
443        // Test add method
444        let sam = "A real charmer!";
445        let result = ioduber.add(keys0, &sam)?;
446        assert!(result);
447        let bytes: Vec<Vec<u8>> = ioduber.get(keys0)?;
448        let actuals: Vec<String> = bytes
449            .into_iter()
450            .map(|b| String::from_utf8(b).unwrap())
451            .collect();
452        assert_eq!(
453            actuals,
454            vec![sue.to_string(), sal.to_string(), sam.to_string()]
455        ); // insertion order
456
457        // Test pin method
458        let zoe = "See ya later.";
459        let zia = "Hey gorgeous!";
460        let result = ioduber.pin(keys0, &[&zoe, &zia])?;
461        assert!(result);
462        let bytes: Vec<Vec<u8>> = ioduber.get(keys0)?;
463        let actuals: Vec<String> = bytes
464            .into_iter()
465            .map(|b| String::from_utf8(b).unwrap())
466            .collect();
467        assert_eq!(actuals, vec![zoe.to_string(), zia.to_string()]); // insertion order
468
469        // Test with multiple keys
470        assert!(ioduber.put(keys1, &[&sal, &sue, &sam])?);
471        let bytes: Vec<Vec<u8>> = ioduber.get(keys1)?;
472        let actuals: Vec<String> = bytes
473            .into_iter()
474            .map(|b| String::from_utf8(b).unwrap())
475            .collect();
476        assert_eq!(
477            actuals,
478            vec![sal.to_string(), sue.to_string(), sam.to_string()]
479        );
480
481        // Test getIter - equivalent to Python's getIter
482        let mut i = 0;
483        for val_result in ioduber.get_iter::<_, Vec<u8>>(keys1)? {
484            let val = String::from_utf8(val_result?).unwrap();
485            assert_eq!(val, actuals[i]);
486            i += 1;
487        }
488
489        // Test getItemIter - equivalent to Python's getItemIter
490        let empty_key: &[&str] = &[];
491        let items = ioduber.get_item_iter(empty_key, false)?;
492        let string_items = items_to_string_tuples(&items);
493
494        // Direct assertion of expected key-value structures
495        assert_eq!(
496            string_items,
497            vec![
498                (
499                    vec!["test_key".to_string(), "0001".to_string()],
500                    "See ya later.".to_string()
501                ),
502                (
503                    vec!["test_key".to_string(), "0001".to_string()],
504                    "Hey gorgeous!".to_string()
505                ),
506                (
507                    vec!["test_key".to_string(), "0002".to_string()],
508                    "Not my type.".to_string()
509                ),
510                (
511                    vec!["test_key".to_string(), "0002".to_string()],
512                    "Hello sailer!".to_string()
513                ),
514                (
515                    vec!["test_key".to_string(), "0002".to_string()],
516                    "A real charmer!".to_string()
517                ),
518            ]
519        );
520
521        // Test getFullItemIter - equivalent to Python's getFullItemIter
522        let items = ioduber.get_full_item_iter(empty_key, false)?;
523        let string_items = items_to_string_tuples(&items);
524
525        // Check full items with proem
526        assert_eq!(
527            string_items,
528            vec![
529                (
530                    vec!["test_key".to_string(), "0001".to_string()],
531                    "00000000000000000000000000000000.See ya later.".to_string()
532                ),
533                (
534                    vec!["test_key".to_string(), "0001".to_string()],
535                    "00000000000000000000000000000001.Hey gorgeous!".to_string()
536                ),
537                (
538                    vec!["test_key".to_string(), "0002".to_string()],
539                    "00000000000000000000000000000000.Not my type.".to_string()
540                ),
541                (
542                    vec!["test_key".to_string(), "0002".to_string()],
543                    "00000000000000000000000000000001.Hello sailer!".to_string()
544                ),
545                (
546                    vec!["test_key".to_string(), "0002".to_string()],
547                    "00000000000000000000000000000002.A real charmer!".to_string()
548                ),
549            ]
550        );
551
552        // Test getItemIter with specific keys = keys1
553        let items = ioduber.get_item_iter(keys1, false)?;
554        let string_items = items_to_string_tuples(&items);
555
556        assert_eq!(
557            string_items,
558            vec![
559                (
560                    vec!["test_key".to_string(), "0002".to_string()],
561                    "Not my type.".to_string()
562                ),
563                (
564                    vec!["test_key".to_string(), "0002".to_string()],
565                    "Hello sailer!".to_string()
566                ),
567                (
568                    vec!["test_key".to_string(), "0002".to_string()],
569                    "A real charmer!".to_string()
570                ),
571            ]
572        );
573
574        // Test getItemIter with specific keys = keys0
575        let items = ioduber.get_item_iter(keys0, false)?;
576        let string_items = items_to_string_tuples(&items);
577
578        assert_eq!(
579            string_items,
580            vec![
581                (
582                    vec!["test_key".to_string(), "0001".to_string()],
583                    "See ya later.".to_string()
584                ),
585                (
586                    vec!["test_key".to_string(), "0001".to_string()],
587                    "Hey gorgeous!".to_string()
588                ),
589            ]
590        );
591
592        // Test with top keys
593        let test_pop = &["test", "pop"];
594        assert!(ioduber.put(test_pop, &[&sal, &sue, &sam])?);
595        let topkeys = &["test", ""];
596        let items = ioduber.get_item_iter(topkeys, false)?;
597        let string_items = items_to_string_tuples(&items);
598
599        assert_eq!(
600            string_items,
601            vec![
602                (
603                    vec!["test".to_string(), "pop".to_string()],
604                    "Not my type.".to_string()
605                ),
606                (
607                    vec!["test".to_string(), "pop".to_string()],
608                    "Hello sailer!".to_string()
609                ),
610                (
611                    vec!["test".to_string(), "pop".to_string()],
612                    "A real charmer!".to_string()
613                ),
614            ]
615        );
616
617        // Test with top parameter
618        let keys = &["test"];
619        let items = ioduber.get_item_iter(keys, true)?;
620        let string_items = items_to_string_tuples(&items);
621
622        assert_eq!(
623            string_items,
624            vec![
625                (
626                    vec!["test".to_string(), "pop".to_string()],
627                    "Not my type.".to_string()
628                ),
629                (
630                    vec!["test".to_string(), "pop".to_string()],
631                    "Hello sailer!".to_string()
632                ),
633                (
634                    vec!["test".to_string(), "pop".to_string()],
635                    "A real charmer!".to_string()
636                ),
637            ]
638        );
639
640        // Test IoItems (getFullItemIter with keys)
641        let items = ioduber.get_full_item_iter(topkeys, false)?;
642        let string_items = items_to_string_tuples(&items);
643
644        assert_eq!(
645            string_items,
646            vec![
647                (
648                    vec!["test".to_string(), "pop".to_string()],
649                    "00000000000000000000000000000000.Not my type.".to_string()
650                ),
651                (
652                    vec!["test".to_string(), "pop".to_string()],
653                    "00000000000000000000000000000001.Hello sailer!".to_string()
654                ),
655                (
656                    vec!["test".to_string(), "pop".to_string()],
657                    "00000000000000000000000000000002.A real charmer!".to_string()
658                ),
659            ]
660        );
661
662        // Test remove with a specific val
663        assert!(ioduber.rem(keys1, Some(&sue))?);
664
665        // Sequence of state validation after remove operation
666        let items = ioduber.get_item_iter(empty_key, false)?;
667        let string_items = items_to_string_tuples(&items);
668
669        assert_eq!(
670            string_items,
671            vec![
672                (
673                    vec!["test".to_string(), "pop".to_string()],
674                    "Not my type.".to_string()
675                ),
676                (
677                    vec!["test".to_string(), "pop".to_string()],
678                    "Hello sailer!".to_string()
679                ),
680                (
681                    vec!["test".to_string(), "pop".to_string()],
682                    "A real charmer!".to_string()
683                ),
684                (
685                    vec!["test_key".to_string(), "0001".to_string()],
686                    "See ya later.".to_string()
687                ),
688                (
689                    vec!["test_key".to_string(), "0001".to_string()],
690                    "Hey gorgeous!".to_string()
691                ),
692                (
693                    vec!["test_key".to_string(), "0002".to_string()],
694                    "Not my type.".to_string()
695                ),
696                (
697                    vec!["test_key".to_string(), "0002".to_string()],
698                    "A real charmer!".to_string()
699                ),
700            ]
701        );
702
703        // Test trim with specific keys
704        assert!(ioduber.trim(topkeys, false)?);
705
706        // Sequence of state validation after trim operation
707        let items = ioduber.get_item_iter(empty_key, false)?;
708        let string_items = items_to_string_tuples(&items);
709
710        assert_eq!(
711            string_items,
712            vec![
713                (
714                    vec!["test_key".to_string(), "0001".to_string()],
715                    "See ya later.".to_string()
716                ),
717                (
718                    vec!["test_key".to_string(), "0001".to_string()],
719                    "Hey gorgeous!".to_string()
720                ),
721                (
722                    vec!["test_key".to_string(), "0002".to_string()],
723                    "Not my type.".to_string()
724                ),
725                (
726                    vec!["test_key".to_string(), "0002".to_string()],
727                    "A real charmer!".to_string()
728                ),
729            ]
730        );
731
732        assert_eq!(ioduber.cnt(keys0)?, 2);
733        assert_eq!(ioduber.cnt(keys1)?, 2);
734
735        // Test with keys as string not tuple
736        let keys2 = &["keystr"];
737        let bob = "Shove off!";
738        assert!(ioduber.put(keys2, &[&bob])?);
739        let bytes: Vec<Vec<u8>> = ioduber.get(keys2)?;
740        let actuals: Vec<String> = bytes
741            .into_iter()
742            .map(|b| String::from_utf8(b).unwrap())
743            .collect();
744        assert_eq!(actuals, vec![bob.to_string()]);
745        assert_eq!(ioduber.cnt(keys2)?, 1);
746
747        assert!(ioduber.rem(keys2, None::<&String>)?);
748        let bytes: Vec<Vec<u8>> = ioduber.get(keys2)?;
749        let actuals: Vec<String> = bytes
750            .into_iter()
751            .map(|b| String::from_utf8(b).unwrap())
752            .collect();
753        assert!(actuals.is_empty());
754        assert_eq!(ioduber.cnt(keys2)?, 0);
755
756        assert!(ioduber.put(keys2, &[&bob])?);
757        let bytes: Vec<Vec<u8>> = ioduber.get(keys2)?;
758        let actuals: Vec<String> = bytes
759            .into_iter()
760            .map(|b| String::from_utf8(b).unwrap())
761            .collect();
762        assert_eq!(actuals, vec![bob.to_string()]);
763
764        let bil = "Go away.";
765        assert!(ioduber.pin(keys2, &[&bil])?);
766        let bytes: Vec<Vec<u8>> = ioduber.get(keys2)?;
767        let actuals: Vec<String> = bytes
768            .into_iter()
769            .map(|b| String::from_utf8(b).unwrap())
770            .collect();
771        assert_eq!(actuals, vec![bil.to_string()]);
772
773        assert!(ioduber.add(keys2, &bob)?);
774        let bytes: Vec<Vec<u8>> = ioduber.get(keys2)?;
775        let actuals: Vec<String> = bytes
776            .into_iter()
777            .map(|b| String::from_utf8(b).unwrap())
778            .collect();
779        assert_eq!(actuals, vec![bil.to_string(), bob.to_string()]);
780
781        // Test trim of entire database
782        assert!(ioduber.trim(&[] as &[&str], false)?); // default trims whole database
783        assert!(ioduber.put(keys1, &[&bob, &bil])?);
784        let bytes: Vec<Vec<u8>> = ioduber.get(keys1)?;
785        let actuals: Vec<String> = bytes
786            .into_iter()
787            .map(|b| String::from_utf8(b).unwrap())
788            .collect();
789        assert_eq!(actuals, vec![bob.to_string(), bil.to_string()]);
790
791        // Check database is auto-closed when test ends (similar to Python's context manager)
792        // This happens automatically when lmdber goes out of scope
793
794        Ok(())
795    }
796}