Skip to main content

libkeri/keri/db/subing/
ioset.rs

1use crate::keri::db::dbing::LMDBer;
2use crate::keri::db::errors::DBError;
3use crate::keri::db::subing::{SuberBase, SuberError, Utf8Codec, ValueCodec};
4use std::sync::Arc;
5
6/// Represents an Insertion Ordered Set Suber.
7
8pub struct IoSetSuber<C: ValueCodec = Utf8Codec> {
9    pub base: SuberBase<C>,
10}
11
12impl<C: ValueCodec> IoSetSuber<C> {
13    /// Creates a new `IoSetSuber`.
14    pub fn new(
15        db: Arc<LMDBer>,
16        subkey: &str,
17        sep: Option<u8>,
18        verify: bool,
19    ) -> Result<Self, SuberError> {
20        let base = SuberBase::new(db, subkey, sep, verify, Some(false))?;
21        Ok(Self { base })
22    }
23
24    /// Puts multiple values into the set associated with the given keys.
25    ///
26    /// Adds only values that are not already present in the set for the effective key.
27    /// Does not overwrite existing identical values. Values are appended in insertion order.
28    ///
29    /// # Arguments
30    ///
31    /// * `keys`: An array slice of key parts.
32    /// * `vals`: An array slice of values to put.
33    ///
34    /// # Returns
35    ///
36    /// `Ok(true)` if the operation was successful (note: underlying LMDB operation might always return true here).
37    /// `Ok(false)` if the operation failed at the LMDB level.
38    /// Returns `Err(SuberError)` on key/value processing or database errors.
39    pub fn put<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
40        &self,
41        keys: &[K],
42        vals: &[&V],
43    ) -> Result<bool, SuberError> {
44        let key = self.base.to_key(keys, false);
45
46        // 1. Serialize into owned Vec<Vec<u8>> and handle potential error
47        let serialized_vals: Vec<Vec<u8>> = vals
48            .iter()
49            .map(|v| self.base.ser(*v))
50            .collect::<Result<Vec<Vec<u8>>, _>>()?; // Use turbofish for clarity and collect the Result
51
52        // 2. Create Vec<&[u8]> by borrowing from the now longer-lived 'serialized_vals'
53        let val_slices: Vec<&[u8]> = serialized_vals
54            .iter() // Borrow serialized_vals
55            .map(|v| v.as_slice()) // Create slices borrowing from elements of serialized_vals
56            .collect();
57
58        // 3. Use the Vec<&[u8]>
59        self.base
60            .db
61            .put_io_set_vals(&self.base.sdb, &key, &val_slices, Some([self.base.sep]))
62            .map_err(SuberError::DBError)
63    }
64
65    /// Adds a single value idempotently to the set associated with the given keys.
66    ///
67    /// If the value already exists in the set for the effective key, it is not added again.
68    ///
69    /// # Arguments
70    ///
71    /// * `keys`: An array slice of key parts.
72    /// * `val`: The value to add.
73    ///
74    /// # Returns
75    ///
76    /// `Ok(true)` if the value was added (i.e., it was not already present).
77    /// `Ok(false)` if the value was already present.
78    /// Returns `Err(SuberError)` on key/value processing or database errors.
79    pub fn add<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
80        &self,
81        keys: &[K],
82        val: &V,
83    ) -> Result<bool, SuberError> {
84        let key = self.base.to_key(keys, false);
85        let sval = self.base.ser(val)?;
86        self.base
87            .db
88            .add_io_set_val(&self.base.sdb, &key, &sval, Some([self.base.sep]))
89            .map_err(SuberError::DBError)
90    }
91
92    /// Pins (sets/replaces) the values associated with the given keys.
93    ///
94    /// Removes all pre-existing values for the effective key and replaces them with the provided `vals`.
95    ///
96    /// # Arguments
97    ///
98    /// * `keys`: An array slice of key parts.
99    /// * `vals`: An array slice of the new values to set.
100    ///
101    /// # Returns
102    ///
103    /// `Ok(true)` if the operation was successful.
104    /// `Ok(false)` if the operation failed at the LMDB level.
105    /// Returns `Err(SuberError)` on key/value processing or database errors.
106    /// Pins (sets/replaces) the values associated with the given keys.
107    ///
108    /// Removes all pre-existing values for the effective key and replaces them with the provided `vals`.
109    /// Each value is serialized using the configured ValueCodec before storage.
110    ///
111    /// # Arguments
112    ///
113    /// * `keys`: An array slice of key parts.
114    /// * `vals`: An array slice of the new values to set.
115    ///
116    /// # Returns
117    ///
118    /// `Ok(true)` if the operation was successful.
119    /// `Ok(false)` if the operation failed at the LMDB level.
120    /// Returns `Err(SuberError)` on key/value processing or database errors.
121    pub fn pin<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
122        &self,
123        keys: &[K],
124        vals: &[&V],
125    ) -> Result<bool, SuberError> {
126        let key = self.base.to_key(keys, false);
127
128        // 1. Serialize into owned Vec<Vec<u8>> and handle potential error
129        let serialized_vals: Vec<Vec<u8>> = vals
130            .iter()
131            .map(|v| self.base.ser(*v))
132            .collect::<Result<Vec<Vec<u8>>, _>>()?;
133
134        // 2. Create Vec<&[u8]> by borrowing from the serialized_vals
135        let val_slices: Vec<&[u8]> = serialized_vals.iter().map(|v| v.as_slice()).collect();
136
137        // 3. Use the Vec<&[u8]> with the underlying database operation
138        self.base
139            .db
140            .set_io_set_vals(&self.base.sdb, &key, &val_slices, Some([self.base.sep]))
141            .map_err(SuberError::DBError)
142    }
143
144    /// Gets all values in the set associated with the given keys, in insertion order.
145    ///
146    /// # Arguments
147    ///
148    /// * `keys`: An array slice of key parts.
149    ///
150    /// # Returns
151    ///
152    /// `Ok(Vec<R>)` containing the deserialized values. Returns an empty Vec if the key doesn't exist.
153    /// `Err(SuberError)` on key processing or database/deserialization errors.
154    pub fn get<K: AsRef<[u8]>, R: TryFrom<Vec<u8>>>(&self, keys: &[K]) -> Result<Vec<R>, SuberError>
155    where
156        // Add bound to handle potential errors from TryFrom
157        <R as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
158    {
159        let key = self.base.to_key(keys, false);
160        let raw_vals = self
161            .base
162            .db
163            .get_io_set_vals(&self.base.sdb, &key, None, Some([self.base.sep]))
164            .map_err(SuberError::DBError)?;
165
166        raw_vals
167            .iter()
168            .map(|raw_val| self.base.des(raw_val))
169            .collect() // Collects into Result<Vec<R>, SuberError>
170    }
171
172    /// Gets an iterator over the values in the set associated with the given keys.
173    ///
174    /// Values are yielded in insertion order.
175    /// Note: This implementation collects into a Vec first due to the underlying callback mechanism.
176    /// A true iterator might require a more complex setup or modification to LMDBer.
177    ///
178    /// # Arguments
179    ///
180    /// * `keys`: An array slice of key parts.
181    ///
182    /// # Returns
183    ///
184    /// `Ok(impl Iterator<Item = Result<R, SuberError>>)` yielding deserialized values.
185    /// `Err(SuberError)` on key processing or initial database errors.
186    pub fn get_iter<K: AsRef<[u8]>, R: TryFrom<Vec<u8>> + 'static>(
187        &self,
188        keys: &[K],
189    ) -> Result<impl Iterator<Item = Result<R, SuberError>>, SuberError>
190    where
191        <R as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
192        SuberError: From<<R as TryFrom<Vec<u8>>>::Error>,
193    {
194        let key = self.base.to_key(keys, false);
195        let mut results: Vec<Result<R, SuberError>> = Vec::new();
196        let sep = Some([self.base.sep]);
197
198        self.base
199            .db
200            .get_io_set_vals_iter(&self.base.sdb, &key, None, sep, |val| {
201                let result = self.base.des(val);
202                results.push(result);
203                Ok(true)
204            })
205            .map_err(SuberError::DBError)?;
206
207        Ok(results.into_iter())
208    }
209
210    /// Gets the last value inserted into the set for the given keys.
211    ///
212    /// # Arguments
213    ///
214    /// * `keys`: An array slice of key parts.
215    ///
216    /// # Returns
217    ///
218    /// `Ok(Some(R))` containing the deserialized last value if found.
219    /// `Ok(None)` if the key does not exist or the set is empty.
220    /// `Err(SuberError)` on key processing or database/deserialization errors.
221    pub fn get_last<K: AsRef<[u8]>, R: TryFrom<Vec<u8>>>(
222        &self,
223        keys: &[K],
224    ) -> Result<Option<R>, SuberError>
225    where
226        <R as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
227    {
228        let key = self.base.to_key(keys, false);
229        let raw_val_opt = self
230            .base
231            .db
232            .get_io_set_val_last(&self.base.sdb, &key, Some([self.base.sep]))
233            .map_err(SuberError::DBError)?;
234
235        match raw_val_opt {
236            Some(raw_val) => self.base.des(&raw_val).map(Some),
237            None => Ok(None),
238        }
239    }
240
241    /// Removes entries associated with the given keys.
242    ///
243    /// If `val` is `Some`, removes the specific value from the set if it exists.
244    /// If `val` is `None`, removes *all* values associated with the effective key.
245    ///
246    /// # Arguments
247    ///
248    /// * `keys`: An array slice of key parts.
249    /// * `val`: An `Option` containing the specific value to remove, or `None` to remove all.
250    ///
251    /// # Returns
252    ///
253    /// `Ok(true)` if the specified value or the entire key entry was found and removed.
254    /// `Ok(false)` if the specified value or key was not found.
255    /// `Err(SuberError)` on key/value processing or database errors.
256    pub fn rem<K: AsRef<[u8]>, V: ?Sized + Clone + Into<Vec<u8>>>(
257        &self,
258        keys: &[K],
259        val: Option<&V>,
260    ) -> Result<bool, SuberError> {
261        let key = self.base.to_key(keys, false);
262        let sep = Some([self.base.sep]);
263        match val {
264            Some(v) => {
265                let sval = self.base.ser(v)?;
266                self.base
267                    .db
268                    .del_io_set_val(&self.base.sdb, &key, &sval, sep)
269                    .map_err(SuberError::DBError)
270            }
271            None => self
272                .base
273                .db
274                .del_io_set_vals(&self.base.sdb, &key, sep)
275                .map_err(SuberError::DBError),
276        }
277    }
278
279    /// Counts the number of values in the set associated with the given keys.
280    ///
281    /// # Arguments
282    ///
283    /// * `keys`: An array slice of key parts.
284    ///
285    /// # Returns
286    ///
287    /// `Ok(usize)` containing the count of values (0 if the key doesn't exist).
288    /// `Err(SuberError)` on key processing or database errors.
289    pub fn cnt<K: AsRef<[u8]>>(&self, keys: &[K]) -> Result<usize, SuberError> {
290        let key = self.base.to_key(keys, false);
291        self.base
292            .db
293            .cnt_io_set_vals(&self.base.sdb, &key, Some([self.base.sep]))
294            .map_err(SuberError::DBError)
295    }
296
297    /// Returns an iterator over (keys, value) items whose effective key starts with the prefix derived from `keys`.
298    ///
299    /// Note: Similar to `get_iter`, this collects results first due to the callback mechanism.
300    /// The keys returned in the tuple are split based on the separator.
301    ///
302    /// # Arguments
303    ///
304    /// * `keys`: An array slice of key parts forming the prefix.
305    /// * `topive`: If true, treat `keys` as a partial prefix (appends separator).
306    ///
307    /// # Returns
308    ///
309    /// `Ok(Vec<(Vec<Vec<u8>>, R)>)` containing tuples of (split key parts, deserialized value).
310    /// `Err(SuberError)` on key processing or database/deserialization errors.
311    pub fn get_item_iter<K: AsRef<[u8]>, R: TryFrom<Vec<u8>>>(
312        &self,
313        keys: &[K],
314        topive: bool,
315    ) -> Result<Vec<(Vec<Vec<u8>>, R)>, SuberError>
316    where
317        <R as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
318        // Ensure DBError can be converted into SuberError
319        SuberError: From<DBError>,
320    {
321        let key_prefix = self.base.to_key(keys, topive);
322        let mut raw_items = Vec::new(); // Collect raw key-value pairs first
323        let sep = Some([self.base.sep]);
324
325        // --- Stage 1: Iterate and collect raw bytes, handling DBError ---
326        self.base
327            .db
328            .get_top_io_set_items_iter(&self.base.sdb, &key_prefix, sep, |k, v| {
329                // k should have the ordinal suffix removed by the LMDBer method
330                // Collect raw bytes, don't deserialize yet.
331                raw_items.push((k.to_vec(), v.to_vec()));
332                Ok(true) // Continue iteration, return type is Result<bool, DBError>
333            })?; // Use ? here: If DB iteration fails, convert DBError to SuberError and return
334
335        // --- Stage 2: Process collected raw items, handling SuberError ---
336        let mut results = Vec::with_capacity(raw_items.len());
337        for (raw_key, raw_val) in raw_items {
338            let item_keys = self.base.to_keys(&raw_key); // Split the effective key
339            let item_val = self.base.des(&raw_val)?; // Deserialize value. ? now works because we're in the main function scope expecting SuberError.
340            results.push((item_keys, item_val));
341        }
342
343        Ok(results)
344    }
345
346    /// Returns an iterator over all the items including all raw items for all keys
347    /// in top branch defined by keys where keys may be truncation of full branch.
348    /// Returns full raw values with ordinal suffixes.
349    ///
350    /// # Arguments
351    /// * `keys` - Slice of key parts, potentially a partial key
352    /// * `topive` - If true, treat as partial key tuple ending with separator
353    ///
354    /// # Returns
355    /// * `Result<Vec<(Vec<Vec<u8>>, Vec<u8>)>, SuberError>` - Vector of key-value pairs with raw values
356    pub fn get_full_item_iter<K: AsRef<[u8]>>(
357        &self,
358        keys: &[K],
359        topive: bool,
360    ) -> Result<Vec<(Vec<Vec<u8>>, Vec<u8>)>, SuberError> {
361        self.base.get_full_item_iter(keys, topive)
362    }
363
364    /// Removes all entries at keys that are in top branch with key prefix matching
365    /// keys where keys may be truncation of full branch.
366    pub fn trim<K: AsRef<[u8]>>(&self, keys: &[K], topive: bool) -> Result<bool, SuberError> {
367        self.base.trim(keys, topive)
368    }
369
370    /// Converts a collection of keys to a single key byte vector
371    pub fn _tokey<K: AsRef<[u8]>>(&self, keys: &[K]) -> Vec<u8> {
372        self.base.to_key(keys, false)
373    }
374
375    /// Converts a key to a vector of key parts
376    pub fn _tokeys(&self, key: &[u8]) -> Vec<Vec<u8>> {
377        self.base.to_keys(key)
378    }
379
380    /// Serialize a value to bytes
381    pub fn _ser<T: ?Sized + Clone + Into<Vec<u8>>>(&self, val: &T) -> Result<Vec<u8>, SuberError> {
382        self.base.ser(val)
383    }
384
385    /// Deserialize bytes to a value
386    pub fn _des<T: TryFrom<Vec<u8>>>(&self, val: &[u8]) -> Result<T, SuberError> {
387        self.base.des(val)
388    }
389
390    /// Count all items in the database
391    pub fn cntAll(&self) -> Result<usize, SuberError> {
392        self.base.cnt_all()
393    }
394}
395
396mod tests {
397    use crate::keri::db::dbing::{LMDBer, LMDBerBuilder};
398    use crate::keri::db::subing::ioset::IoSetSuber;
399    use crate::keri::db::subing::{SuberError, Utf8Codec};
400    use std::sync::Arc;
401    use tempfile::tempdir;
402
403    #[test]
404    fn test_ioset_suber() -> Result<(), SuberError> {
405        // Create a tempdir to ensure test isolation
406        let temp_dir = tempdir().unwrap();
407        let _dir_path = temp_dir.path().to_path_buf();
408
409        // Open a test database
410        let db = LMDBerBuilder::default()
411            .name("test")
412            .temp(true)
413            .build()
414            .unwrap();
415
416        assert_eq!(db.name(), "test");
417        assert!(db.opened());
418
419        // Create IoSetSuber
420        let db_ref = Arc::new(db);
421        let iosuber = IoSetSuber::<Utf8Codec>::new(db_ref.clone(), "bags.", None, false)?;
422
423        let sue = "Hello sailer!";
424        let sal = "Not my type.";
425        let sam = "A real charmer!";
426        let zoe = "See ya later.";
427        let zia = "Hey gorgeous!";
428        let bob = "Shove off!";
429        let bil = "Go away.";
430
431        let keys0 = &["test_key", "0001"];
432        let keys1 = &["test_key", "0002"];
433        let keys2 = &["keystr"];
434
435        // Test put and get
436        assert!(iosuber.put(keys0, &[&sal, &sue])?);
437        let bytes: Vec<Vec<u8>> = iosuber.get(keys0)?;
438        assert_eq!(bytes.len(), 2);
439        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), sal);
440        assert_eq!(String::from_utf8(bytes[1].clone()).unwrap(), sue);
441        assert_eq!(iosuber.cnt(keys0)?, 2);
442
443        // Test getLast - manual conversion
444        let last_bytes = iosuber.get_last(keys0)?;
445        assert!(last_bytes.is_some());
446        assert_eq!(String::from_utf8(last_bytes.unwrap()).unwrap(), sue);
447
448        // Test rem
449        assert!(iosuber.rem(keys0, None::<&String>)?);
450        let bytes: Vec<Vec<u8>> = iosuber.get(keys0)?;
451        assert!(bytes.is_empty());
452        assert_eq!(iosuber.cnt(keys0)?, 0);
453
454        // Test put again with different order
455        assert!(iosuber.put(keys0, &[&sue, &sal])?);
456        let bytes: Vec<Vec<u8>> = iosuber.get(keys0)?;
457        assert_eq!(bytes.len(), 2);
458        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), sue);
459        assert_eq!(String::from_utf8(bytes[1].clone()).unwrap(), sal);
460
461        // Test getLast again
462        let last_bytes = iosuber.get_last(keys0)?;
463        assert!(last_bytes.is_some());
464        assert_eq!(String::from_utf8(last_bytes.unwrap()).unwrap(), sal);
465
466        // Test add
467        assert!(iosuber.add(keys0, &sam)?);
468        let bytes: Vec<Vec<u8>> = iosuber.get(keys0)?;
469        assert_eq!(bytes.len(), 3);
470        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), sue);
471        assert_eq!(String::from_utf8(bytes[1].clone()).unwrap(), sal);
472        assert_eq!(String::from_utf8(bytes[2].clone()).unwrap(), sam);
473
474        // Test pin
475        assert!(iosuber.pin(keys0, &[&zoe, &zia])?);
476        let bytes: Vec<Vec<u8>> = iosuber.get(keys0)?;
477        assert_eq!(bytes.len(), 2);
478        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), zoe);
479        assert_eq!(String::from_utf8(bytes[1].clone()).unwrap(), zia);
480
481        // Test put on a different key
482        assert!(iosuber.put(keys1, &[&sal, &sue, &sam])?);
483        let bytes: Vec<Vec<u8>> = iosuber.get(keys1)?;
484        assert_eq!(bytes.len(), 3);
485        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), sal);
486        assert_eq!(String::from_utf8(bytes[1].clone()).unwrap(), sue);
487        assert_eq!(String::from_utf8(bytes[2].clone()).unwrap(), sam);
488
489        // Test getIter with manual conversion
490        let mut iter_vals = Vec::new();
491        for val_result in iosuber.get_iter::<_, Vec<u8>>(keys1)? {
492            iter_vals.push(String::from_utf8(val_result?).unwrap());
493        }
494        assert_eq!(iter_vals.len(), 3);
495        assert_eq!(iter_vals[0], sal);
496        assert_eq!(iter_vals[1], sue);
497        assert_eq!(iter_vals[2], sam);
498
499        // Test getItemIter (for all items)
500        let items: Vec<(Vec<Vec<u8>>, Vec<u8>)> = iosuber.get_item_iter(&[] as &[&str], false)?;
501        assert_eq!(items.len(), 5);
502
503        // Test values exist in expected orders
504        let mut found_zoe = false;
505        let mut found_zia = false;
506        let mut found_sal_at_keys1 = false;
507        let mut found_sue_at_keys1 = false;
508        let mut found_sam_at_keys1 = false;
509
510        for (keys, val) in &items {
511            let key1 = String::from_utf8(keys[0].clone()).unwrap();
512            let key2 = String::from_utf8(keys[1].clone()).unwrap();
513            let val_str = String::from_utf8(val.clone()).unwrap();
514
515            if key1 == "test_key" && key2 == "0001" && val_str == zoe {
516                found_zoe = true;
517            } else if key1 == "test_key" && key2 == "0001" && val_str == zia {
518                found_zia = true;
519            } else if key1 == "test_key" && key2 == "0002" && val_str == sal {
520                found_sal_at_keys1 = true;
521            } else if key1 == "test_key" && key2 == "0002" && val_str == sue {
522                found_sue_at_keys1 = true;
523            } else if key1 == "test_key" && key2 == "0002" && val_str == sam {
524                found_sam_at_keys1 = true;
525            }
526        }
527
528        assert!(found_zoe);
529        assert!(found_zia);
530        assert!(found_sal_at_keys1);
531        assert!(found_sue_at_keys1);
532        assert!(found_sam_at_keys1);
533
534        // Test getFullItemIter
535        let full_items = iosuber.get_full_item_iter(&[] as &[&str], false)?;
536        assert_eq!(full_items.len(), 5);
537
538        // Verify structure of first item
539        let key1 = String::from_utf8(full_items[0].0[0].clone()).unwrap();
540        let key2 = String::from_utf8(full_items[0].0[1].clone()).unwrap();
541        let key3 = String::from_utf8(full_items[0].0[2].clone()).unwrap();
542
543        assert_eq!(key1, "test_key");
544        assert!(key2 == "0001" || key2 == "0002");
545        assert!(key3.starts_with("0000000000000000"));
546
547        // Test getItemIter with specific keys
548        let items: Vec<(Vec<Vec<u8>>, Vec<u8>)> = iosuber.get_item_iter(keys0, false)?;
549        assert_eq!(items.len(), 2);
550
551        // Check that both keys0 items are returned with correct values
552        let mut has_zoe = false;
553        let mut has_zia = false;
554
555        for (keys, val) in &items {
556            let key1 = String::from_utf8(keys[0].clone()).unwrap();
557            let key2 = String::from_utf8(keys[1].clone()).unwrap();
558            let val_str = String::from_utf8(val.clone()).unwrap();
559
560            assert_eq!(key1, "test_key");
561            assert_eq!(key2, "0001");
562
563            if val_str == zoe {
564                has_zoe = true;
565            } else if val_str == zia {
566                has_zia = true;
567            }
568        }
569
570        assert!(has_zoe);
571        assert!(has_zia);
572
573        // Test with keys1
574        let items: Vec<(Vec<Vec<u8>>, Vec<u8>)> = iosuber.get_item_iter(keys1, false)?;
575        assert_eq!(items.len(), 3);
576
577        let mut has_sal = false;
578        let mut has_sue = false;
579        let mut has_sam = false;
580
581        for (keys, val) in &items {
582            let key1 = String::from_utf8(keys[0].clone()).unwrap();
583            let key2 = String::from_utf8(keys[1].clone()).unwrap();
584            let val_str = String::from_utf8(val.clone()).unwrap();
585
586            assert_eq!(key1, "test_key");
587            assert_eq!(key2, "0002");
588
589            if val_str == sal {
590                has_sal = true;
591            } else if val_str == sue {
592                has_sue = true;
593            } else if val_str == sam {
594                has_sam = true;
595            }
596        }
597
598        assert!(has_sal);
599        assert!(has_sue);
600        assert!(has_sam);
601
602        // Test with top keys
603        assert!(iosuber.put(&["test", "pop"], &[&sal, &sue, &sam])?);
604        let topkeys = &["test", ""];
605
606        let items: Vec<(Vec<Vec<u8>>, Vec<u8>)> = iosuber.get_item_iter(topkeys, false)?;
607        assert_eq!(items.len(), 3);
608
609        // Verify test/pop entries
610        let mut count = 0;
611        for (keys, val) in &items {
612            let key1 = String::from_utf8(keys[0].clone()).unwrap();
613            let key2 = String::from_utf8(keys[1].clone()).unwrap();
614            let val_str = String::from_utf8(val.clone()).unwrap();
615
616            assert_eq!(key1, "test");
617            assert_eq!(key2, "pop");
618            assert!(val_str == sal || val_str == sue || val_str == sam);
619            count += 1;
620        }
621        assert_eq!(count, 3);
622
623        // Test with top parameter
624        let keys = &["test"];
625
626        let items: Vec<(Vec<Vec<u8>>, Vec<u8>)> = iosuber.get_item_iter(keys, true)?;
627        assert_eq!(items.len(), 3);
628
629        // Verify with top parameter
630        for (keys, val) in &items {
631            let key1 = String::from_utf8(keys[0].clone()).unwrap();
632            let key2 = String::from_utf8(keys[1].clone()).unwrap();
633
634            assert_eq!(key1, "test");
635            assert_eq!(key2, "pop");
636        }
637
638        // Test remove with a specific val
639        assert!(iosuber.rem(keys1, Some(&sue))?);
640
641        let bytes: Vec<Vec<u8>> = iosuber.get(keys1)?;
642        assert_eq!(bytes.len(), 2); // sue was removed
643        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), sal);
644        assert_eq!(String::from_utf8(bytes[1].clone()).unwrap(), sam);
645
646        // Test trim
647        assert!(iosuber.trim(&["test", ""], true)?);
648
649        // Verify test entries are gone
650        let items: Vec<(Vec<Vec<u8>>, Vec<u8>)> = iosuber.get_item_iter(&["test"], true)?;
651        assert_eq!(items.len(), 0);
652
653        // Verify remaining entries
654        let items: Vec<(Vec<Vec<u8>>, Vec<u8>)> = iosuber.get_item_iter(&[] as &[&str], true)?;
655        assert_eq!(items.len(), 4); // keys0 (2) + keys1 (2)
656
657        // Test with keys as string not tuple
658        assert!(iosuber.put(keys2, &[&bob])?);
659        let bytes: Vec<Vec<u8>> = iosuber.get(keys2)?;
660        assert_eq!(bytes.len(), 1);
661        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), bob);
662        assert_eq!(iosuber.cnt(keys2)?, 1);
663
664        assert!(iosuber.rem(keys2, None::<&String>)?);
665        let bytes: Vec<Vec<u8>> = iosuber.get(keys2)?;
666        assert!(bytes.is_empty());
667        assert_eq!(iosuber.cnt(keys2)?, 0);
668
669        assert!(iosuber.put(keys2, &[&bob])?);
670        let bytes: Vec<Vec<u8>> = iosuber.get(keys2)?;
671        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), bob);
672
673        assert!(iosuber.pin(keys2, &[&bil])?);
674        let bytes: Vec<Vec<u8>> = iosuber.get(keys2)?;
675        assert_eq!(bytes.len(), 1);
676        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), bil);
677
678        assert!(iosuber.add(keys2, &bob)?);
679        let bytes: Vec<Vec<u8>> = iosuber.get(keys2)?;
680        assert_eq!(bytes.len(), 2);
681        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), bil);
682        assert_eq!(String::from_utf8(bytes[1].clone()).unwrap(), bob);
683
684        // Test trim and append
685        assert!(iosuber.trim(&[] as &[&str], false)?);
686        assert!(iosuber.put(keys1, &[&bob, &bil])?);
687        let bytes: Vec<Vec<u8>> = iosuber.get(keys1)?;
688        assert_eq!(bytes.len(), 2);
689        assert_eq!(String::from_utf8(bytes[0].clone()).unwrap(), bob);
690        assert_eq!(String::from_utf8(bytes[1].clone()).unwrap(), bil);
691
692        // The database should be closed and removed when db goes out of scope
693        drop(db_ref);
694
695        Ok(())
696    }
697}