Skip to main content

libkeri/keri/db/
koming.rs

1use crate::keri::db::dbing::LMDBer;
2use crate::keri::db::errors::DBError;
3use heed::types::*;
4use heed::Database;
5use serde::{Deserialize, Serialize};
6use std::fmt::Debug;
7use std::marker::PhantomData;
8use std::sync::Arc;
9use thiserror::Error;
10
11#[allow(dead_code)]
12#[derive(Debug, Error)]
13pub enum KomerError {
14    #[error("Database error: {0}")]
15    Database(#[from] heed::Error),
16
17    #[error("LMDB error: {0}")]
18    DBError(#[from] DBError),
19
20    #[error("Serialization error: {0}")]
21    Serialization(String),
22
23    #[error("Deserialization error: {0}")]
24    Deserialization(String),
25
26    #[error("Invalid schema: expected {expected} got {got}")]
27    InvalidSchema { expected: String, got: String },
28
29    #[error("Empty keys")]
30    EmptyKeys,
31}
32
33/// Serialization types supported
34#[allow(dead_code)]
35#[derive(Debug, Clone, Copy)]
36pub enum SerialKind {
37    Json,
38    MsgPack,
39    Cbor,
40}
41
42/// KomerBase is a base struct for Komer (Keyspace Object Mapper) implementations
43/// that each use a struct (with Serialize/Deserialize) as the object mapped via
44/// serialization to an LMDB database.
45pub struct KomerBase<T>
46where
47    T: Serialize + for<'de> Deserialize<'de> + Debug,
48{
49    /// LMDB database environment
50    db: Arc<LMDBer>, // The base LMDB database
51
52    /// LMDB database instance for this Komer
53    pub sdb: Database<Bytes, Bytes>,
54
55    /// Serialization format
56    pub kind: SerialKind,
57
58    /// Separator for combining keys
59    pub sep: String,
60
61    /// Phantom data for the schema type
62    phantom: PhantomData<T>,
63}
64
65impl<T> KomerBase<T>
66where
67    T: Serialize + for<'de> Deserialize<'de> + Debug,
68{
69    /// Create a new KomerBase instance
70    pub fn new(
71        db: Arc<LMDBer>,
72        subkey: &str,
73        kind: SerialKind,
74        dupsort: bool,
75        sep: Option<&str>,
76    ) -> Result<Self, KomerError> {
77        let sdb = db.create_database(Some(subkey), Some(dupsort))?;
78
79        Ok(Self {
80            db,
81            sdb,
82            kind,
83            sep: sep.unwrap_or(".").to_string(),
84            phantom: PhantomData,
85        })
86    }
87
88    // Convert various key forms to bytes
89    pub fn to_key<K: AsRef<[u8]>>(&self, keys: &[K], topive: bool) -> Vec<u8> {
90        // Logic to convert keys to a single byte vector with separators
91        // Similar to Python's _tokey method
92        let mut result = Vec::new();
93
94        // If there's only one key and it's already bytes, return it directly
95        if keys.len() == 1 {
96            let key = keys[0].as_ref();
97            if !topive {
98                return key.to_vec();
99            }
100            // For topive=true, append separator
101            result.extend_from_slice(key);
102            result.push(self.sep.as_bytes()[0]);
103            return result;
104        }
105
106        // Join multiple keys with separators
107        for (i, key) in keys.iter().enumerate() {
108            if i > 0 {
109                result.push(self.sep.as_bytes()[0]);
110            }
111            result.extend_from_slice(key.as_ref());
112        }
113
114        // Add trailing separator for topive
115        if topive && (!result.is_empty() && result[result.len() - 1] != self.sep.as_bytes()[0]) {
116            result.push(self.sep.as_bytes()[0]);
117        }
118
119        result
120    }
121
122    /// Converts key bytes to keys tuple of strings
123    pub fn to_keys(&self, key: &[u8]) -> Vec<String> {
124        let key_str = String::from_utf8_lossy(key);
125        key_str.split(&self.sep).map(String::from).collect()
126    }
127
128    /// Serialize a value using the configured serializer
129    pub fn serialize(&self, val: &T) -> Result<Vec<u8>, KomerError> {
130        match self.kind {
131            SerialKind::Json => self.serialize_json(val),
132            SerialKind::MsgPack => self.serialize_msgpack(val),
133            SerialKind::Cbor => self.serialize_cbor(val),
134        }
135    }
136
137    /// Deserialize a value using the configured deserializer
138    pub fn deserialize(&self, val: &[u8]) -> Result<T, KomerError> {
139        match self.kind {
140            SerialKind::Json => self.deserialize_json(val),
141            SerialKind::MsgPack => self.deserialize_msgpack(val),
142            SerialKind::Cbor => self.deserialize_cbor(val),
143        }
144    }
145
146    /// Serialize to JSON
147    fn serialize_json(&self, val: &T) -> Result<Vec<u8>, KomerError> {
148        serde_json::to_vec(val)
149            .map_err(|e| KomerError::Serialization(format!("JSON serialization error: {}", e)))
150    }
151
152    /// Deserialize from JSON
153    fn deserialize_json(&self, val: &[u8]) -> Result<T, KomerError> {
154        serde_json::from_slice(val)
155            .map_err(|e| KomerError::Deserialization(format!("JSON deserialization error: {}", e)))
156    }
157
158    /// Serialize to MsgPack
159    fn serialize_msgpack(&self, val: &T) -> Result<Vec<u8>, KomerError> {
160        rmp_serde::to_vec(val)
161            .map_err(|e| KomerError::Serialization(format!("MsgPack serialization error: {}", e)))
162    }
163
164    /// Deserialize from MsgPack
165    fn deserialize_msgpack(&self, val: &[u8]) -> Result<T, KomerError> {
166        rmp_serde::from_slice(val).map_err(|e| {
167            KomerError::Deserialization(format!("MsgPack deserialization error: {}", e))
168        })
169    }
170
171    /// Serialize to CBOR
172    fn serialize_cbor(&self, val: &T) -> Result<Vec<u8>, KomerError> {
173        serde_cbor::to_vec(val)
174            .map_err(|e| KomerError::Serialization(format!("CBOR serialization error: {}", e)))
175    }
176
177    /// Deserialize from CBOR
178    fn deserialize_cbor(&self, val: &[u8]) -> Result<T, KomerError> {
179        serde_cbor::from_slice(val)
180            .map_err(|e| KomerError::Deserialization(format!("CBOR deserialization error: {}", e)))
181    }
182
183    /// Get an iterator over items with keys starting with the provided prefix
184    pub fn get_item_iter<K>(&self, keys: &[K]) -> Result<Vec<(Vec<String>, T)>, KomerError>
185    where
186        K: AsRef<[u8]>,
187    {
188        // Convert keys to a single key for database prefix search
189        let key_prefix = self.to_key(keys, false);
190
191        // Create a prefix iterator
192        let mut result = Vec::new();
193        self.db
194            .get_top_items_iter(&self.sdb, &key_prefix, |key, value| {
195                let key_strings = self.to_keys(&key);
196                let deserialized = self
197                    .deserialize(&value)
198                    .map_err(|_| DBError::ValueError("Failed to deserialize value".to_string()))?;
199                result.push((key_strings, deserialized));
200                Ok(true)
201            })?;
202
203        Ok(result)
204    }
205
206    /// Get full item iterator (same as get_item_iter in this implementation)
207    pub fn get_full_item_iter<K>(&self, keys: &[K]) -> Result<Vec<(Vec<String>, T)>, KomerError>
208    where
209        K: AsRef<[u8]>,
210    {
211        self.get_item_iter(keys)
212    }
213
214    /// Put a value into the database with the given keys
215    pub fn put<K>(&self, keys: &[K], val: &T) -> Result<bool, KomerError>
216    where
217        K: AsRef<[u8]>,
218    {
219        let key = self.to_key(keys, false);
220        if key.is_empty() {
221            Err(KomerError::EmptyKeys)
222        } else {
223            let value = self.serialize(val)?;
224            Ok(self.db.put_val(&self.sdb, &key, &value)?)
225        }
226    }
227
228    /// Get a value from the database with the given keys
229    pub fn get<K>(&self, keys: &[K]) -> Result<Option<T>, KomerError>
230    where
231        K: AsRef<[u8]>,
232    {
233        let key = self.to_key(keys, false);
234        if let Some(val) = self.db.get_val(&self.sdb, &key)? {
235            Ok(Some(self.deserialize(&val)?))
236        } else {
237            Ok(None)
238        }
239    }
240
241    /// Remove a value from the database with the given keys
242    pub fn rem<K>(&self, keys: &[K]) -> Result<bool, KomerError>
243    where
244        K: AsRef<[u8]>,
245    {
246        let key = self.to_key(keys, false);
247        let existed = self.db.del_val(&self.sdb, &key)?;
248        Ok(existed)
249    }
250
251    /// Count all items in the database
252    pub fn cnt_all(&self) -> Result<usize, KomerError> {
253        Ok(self.db.len(&self.sdb)? as usize)
254    }
255}
256
257/// Keyspace Object Mapper factory struct.
258#[allow(dead_code)]
259pub struct Komer<T>
260where
261    T: Serialize + for<'de> Deserialize<'de> + Debug,
262{
263    base: KomerBase<T>,
264}
265
266impl<T> Komer<T>
267where
268    T: Serialize + for<'de> Deserialize<'de> + Debug,
269{
270    /// Creates a new Komer instance.
271    ///
272    /// # Parameters
273    /// * `db` - Base database reference
274    /// * `subkey` - LMDB sub database key
275    /// * `kind` - Serialization/deserialization type
276    ///
277    /// # Returns
278    /// A Result containing the Komer instance or a KomerError
279    pub fn new(db: Arc<LMDBer>, subkey: &str, kind: SerialKind) -> Result<Self, KomerError> {
280        let base = KomerBase::new(db, subkey, kind, false, None)?;
281
282        Ok(Self { base })
283    }
284
285    /// Puts val at key made from keys. Does not overwrite.
286    ///
287    /// # Parameters
288    /// * `txn` - Database transaction
289    /// * `keys` - Key components to be combined to form the key
290    /// * `val` - Value to store
291    ///
292    /// # Returns
293    /// True if successful, False otherwise (e.g., if key already exists in database)
294    pub fn put<K>(&self, keys: &[K], val: &T) -> Result<bool, KomerError>
295    where
296        K: AsRef<[u8]>,
297    {
298        self.base.put(keys, val)
299    }
300
301    /// Pins (sets) val at key made from keys. Overwrites existing value.
302    ///
303    /// # Parameters
304    /// * `txn` - Database transaction
305    /// * `keys` - Key components to be combined to form the key
306    /// * `val` - Value to store
307    ///
308    /// # Returns
309    /// True if successful, False otherwise
310    pub fn pin<K>(&self, keys: &[K], val: &T) -> Result<bool, KomerError>
311    where
312        K: AsRef<[u8]>,
313    {
314        // Convert keys to a single key
315        let key = self.base.to_key(keys, false);
316
317        // Serialize value
318        let serialized = self.base.serialize(val)?;
319
320        // Set the value in the database (overwriting if it exists)
321        match self.base.db.set_val(&self.base.sdb, &key, &serialized) {
322            Ok(_) => Ok(true),
323            Err(e) => Err(KomerError::DBError(e)),
324        }
325    }
326
327    /// Gets val at keys.
328    ///
329    /// # Parameters
330    /// * `txn` - Database transaction
331    /// * `keys` - Key components to be combined to form the key
332    ///
333    /// # Returns
334    /// Option containing the value if found, None if no entry at keys
335    pub fn get<K>(&self, keys: &[K]) -> Result<Option<T>, KomerError>
336    where
337        K: AsRef<[u8]>,
338    {
339        self.base.get(keys)
340    }
341
342    /// Gets dictified val at keys.
343    /// In Rust, we'll return a serde_json::Value instead of a dict.
344    ///
345    /// # Parameters
346    /// * `txn` - Database transaction
347    /// * `keys` - Key components to be combined to form the key
348    ///
349    /// # Returns
350    /// Option containing the value as JSON if found, None if no entry at keys
351    pub fn get_json<K>(&self, keys: &[K]) -> Result<Option<serde_json::Value>, KomerError>
352    where
353        K: AsRef<[u8]>,
354    {
355        if let Some(val) = self.get(keys)? {
356            match serde_json::to_value(&val) {
357                Ok(json_val) => Ok(Some(json_val)),
358                Err(e) => Err(KomerError::Serialization(e.to_string())),
359            }
360        } else {
361            Ok(None)
362        }
363    }
364
365    pub fn get_item_iter<K>(&self, keys: &[K]) -> Result<Vec<(Vec<String>, T)>, KomerError>
366    where
367        K: AsRef<[u8]>,
368    {
369        self.base.get_item_iter(keys)
370    }
371    /// Removes entry at keys.
372    ///
373    /// # Parameters
374    /// * `txn` - Database transaction
375    /// * `keys` - Key components to be combined to form the key
376    ///
377    /// # Returns
378    /// True if key exists so delete successful, False otherwise
379    pub fn rem<K>(&self, keys: &[K]) -> Result<bool, KomerError>
380    where
381        K: AsRef<[u8]>,
382    {
383        self.base.rem(keys)
384    }
385
386    /// Removes all entries whose keys starts with the given keys prefix.
387    /// Enables removal of whole branches of db key space.
388    ///
389    /// # Parameters
390    /// * `txn` - Database transaction
391    /// * `keys` - Key components to be combined to form the prefix
392    ///
393    /// # Returns
394    /// True if operation successful, False otherwise
395    pub fn trim<K>(&self, keys: &[K]) -> Result<bool, KomerError>
396    where
397        K: AsRef<[u8]>,
398    {
399        let key = self.base.to_key(keys, true);
400        Ok(self.base.db.del_top_val(&self.base.sdb, &key)?)
401    }
402
403    /// Return the count of all items in the subdatabase.
404    ///
405    /// # Parameters
406    ///
407    /// # Returns
408    /// Number of items in the database
409    pub fn cnt_all(&self) -> Result<usize, KomerError> {
410        self.base.cnt_all()
411    }
412}
413
414#[cfg(test)]
415mod tests {
416    use super::*;
417    use serde::{Deserialize, Serialize};
418    use std::collections::HashMap;
419    use std::fmt::Debug;
420    use std::sync::Arc;
421
422    #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
423    struct Record {
424        first: String,  // first name
425        last: String,   // last name
426        street: String, // street address
427        city: String,   // city name
428        state: String,  // state code
429        zip: u32,       // zip code
430    }
431
432    #[test]
433    fn test_kom_happy_path() -> Result<(), Box<dyn std::error::Error>> {
434        // Create and serialize a Record
435        let jim = Record {
436            first: "Jim".to_string(),
437            last: "Black".to_string(),
438            street: "100 Main Street".to_string(),
439            city: "Riverton".to_string(),
440            state: "UT".to_string(),
441            zip: 84058,
442        };
443
444        let jimser = serde_json::to_string(&jim)?;
445        let jim_deserialized: Record = serde_json::from_str(&jimser)?;
446        assert_eq!(jim_deserialized, jim);
447
448        // Open database
449        let lmdber = LMDBer::builder().name("test_db").temp(true).build()?;
450        let db_ref = Arc::new(lmdber);
451
452        // Create Komer instance
453        let mydb = Komer::<Record>::new(db_ref.clone(), "records.", SerialKind::Json)?;
454
455        // Check if dupsort is not enabled
456        // assert!(!mydb.base.sdb.flags()?.dupsort);
457
458        // Create a test record
459        let sue = Record {
460            first: "Susan".to_string(),
461            last: "Black".to_string(),
462            street: "100 Main Street".to_string(),
463            city: "Riverton".to_string(),
464            state: "UT".to_string(),
465            zip: 84058,
466        };
467
468        // Test key handling
469        let keys = ["test_key", "0001"];
470        assert_eq!(mydb.base.sep, ".");
471
472        // Test to_key and to_keys
473        let key = mydb.base.to_key(&keys, false);
474        let str = std::str::from_utf8(&key)?;
475        assert_eq!(key, b"test_key.0001");
476        assert_eq!(
477            mydb.base.to_keys(&key),
478            vec!["test_key".to_string(), "0001".to_string()]
479        );
480
481        // Test put and get
482        mydb.put(&keys, &sue)?;
483        let actual = mydb.get(&keys)?.unwrap();
484
485        assert_eq!(actual.first, "Susan");
486        assert_eq!(actual.last, "Black");
487        assert_eq!(actual.street, "100 Main Street");
488        assert_eq!(actual.city, "Riverton");
489        assert_eq!(actual.state, "UT");
490        assert_eq!(actual.zip, 84058);
491
492        // Test rem
493        mydb.rem(&keys)?;
494        let actual = mydb.get(&keys)?;
495        assert!(actual.is_none());
496
497        // Test put and equality
498        let keys = ["test_key", "0001"];
499        mydb.put(&keys, &sue)?;
500        let actual = mydb.get(&keys)?.unwrap();
501        assert_eq!(actual, sue);
502
503        // Test put with existing key (should not update)
504        let kip = Record {
505            first: "Kip".to_string(),
506            last: "Thorne".to_string(),
507            street: "200 Center Street".to_string(),
508            city: "Bluffdale".to_string(),
509            state: "UT".to_string(),
510            zip: 84043,
511        };
512
513        let result = mydb.put(&keys, &kip)?;
514        assert!(!result);
515        let actual = mydb.get(&keys)?.unwrap();
516        assert_eq!(actual, sue);
517
518        // Test get_json
519        let actual_json = mydb.get_json(&keys)?.unwrap();
520        let expected_json = serde_json::to_value(&sue)?;
521        assert_eq!(actual_json, expected_json);
522
523        // Test pin (forced update)
524        let result = mydb.pin(&keys, &kip)?;
525        assert!(result);
526        let actual = mydb.get(&keys)?.unwrap();
527        assert_eq!(actual, kip);
528
529        // Test with single string key instead of slice
530        let keys = ["keystr"];
531
532        let bob = Record {
533            first: "Bob".to_string(),
534            last: "Brown".to_string(),
535            street: "100 Center Street".to_string(),
536            city: "Bluffdale".to_string(),
537            state: "UT".to_string(),
538            zip: 84043,
539        };
540
541        mydb.put(&keys, &bob)?;
542        let actual = mydb.get(&keys)?.unwrap();
543
544        assert_eq!(actual.first, "Bob");
545        assert_eq!(actual.last, "Brown");
546        assert_eq!(actual.street, "100 Center Street");
547        assert_eq!(actual.city, "Bluffdale");
548        assert_eq!(actual.state, "UT");
549        assert_eq!(actual.zip, 84043);
550
551        // Test get_json
552        let actual_json = mydb.get_json(&keys)?.unwrap();
553        let expected_json = serde_json::to_value(&bob)?;
554        assert_eq!(actual_json, expected_json);
555
556        // Test empty keys
557        let nonexistent_keys = ["bla", "bal"];
558        assert!(mydb.get_json(&nonexistent_keys)?.is_none());
559
560        // Test rem
561        mydb.rem(&keys)?;
562        let actual = mydb.get(&keys)?;
563        assert!(actual.is_none());
564
565        // Close the database (will happen when db goes out of scope)
566        drop(db_ref);
567
568        // Temporary directory should be removed when temp_dir goes out of scope
569        Ok(())
570    }
571
572    #[test]
573    fn test_komer_error_handling() -> Result<(), Box<dyn std::error::Error>> {
574        // Open database
575        let lmdber = LMDBer::builder().name("test_db").temp(true).build()?;
576        let db_ref = Arc::new(lmdber);
577
578        // Test empty keys
579        let mydb = Komer::<Record>::new(db_ref.clone(), "records.", SerialKind::Json)?;
580
581        let empty_keys: [&str; 0] = [];
582        let record = Record {
583            first: "Test".to_string(),
584            last: "User".to_string(),
585            street: "123 Test St".to_string(),
586            city: "Testville".to_string(),
587            state: "TS".to_string(),
588            zip: 12345,
589        };
590
591        // This should return an EmptyKeys error
592        let result = mydb.put(&empty_keys, &record);
593        assert!(matches!(result, Err(KomerError::EmptyKeys)));
594
595        Ok(())
596    }
597
598    #[test]
599    fn test_serialization_formats() -> Result<(), Box<dyn std::error::Error>> {
600        // Open database
601        let lmdber = LMDBer::builder().name("test_db").temp(true).build()?;
602        let db_ref = Arc::new(lmdber);
603
604        // Test record
605        let record = Record {
606            first: "Test".to_string(),
607            last: "User".to_string(),
608            street: "123 Test St".to_string(),
609            city: "Testville".to_string(),
610            state: "TS".to_string(),
611            zip: 12345,
612        };
613
614        // Test with different serialization formats
615        for serial_kind in [SerialKind::Json, SerialKind::MsgPack, SerialKind::Cbor] {
616            let mydb = Komer::<Record>::new(
617                db_ref.clone(),
618                &format!("records_{:?}.", serial_kind),
619                serial_kind,
620            )?;
621
622            let keys = ["test_key"];
623            mydb.put(&keys, &record)?;
624            let retrieved = mydb.get(&keys)?.unwrap();
625
626            assert_eq!(retrieved, record);
627        }
628
629        Ok(())
630    }
631
632    #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
633    struct Stuff {
634        a: String,
635        b: String,
636    }
637
638    impl Stuff {
639        // Similar to Python's __iter__ that returns an iterator over asdict(self)
640        pub fn to_map(&self) -> HashMap<String, String> {
641            let mut map = HashMap::new();
642            map.insert("a".to_string(), self.a.clone());
643            map.insert("b".to_string(), self.b.clone());
644            map
645        }
646    }
647
648    #[test]
649    fn test_kom_get_item_iter() -> Result<(), Box<dyn std::error::Error>> {
650        // Test data setup
651        let w = Stuff {
652            a: "Big".to_string(),
653            b: "Blue".to_string(),
654        };
655        let x = Stuff {
656            a: "Tall".to_string(),
657            b: "Red".to_string(),
658        };
659        let y = Stuff {
660            a: "Fat".to_string(),
661            b: "Green".to_string(),
662        };
663        let z = Stuff {
664            a: "Eat".to_string(),
665            b: "White".to_string(),
666        };
667
668        // Open test database
669        let lmdber = LMDBer::builder().name("test").temp(true).build()?;
670
671        let db_ref = Arc::new(lmdber);
672        assert_eq!(db_ref.name(), "test");
673        assert!(db_ref.opened());
674
675        // Create Komer instance
676        let mydb = Komer::<Stuff>::new(db_ref.clone(), "recs.", SerialKind::Json)?;
677
678        // Add initial data
679        mydb.put(&["a", "1"], &w)?;
680        mydb.put(&["a", "2"], &x)?;
681        mydb.put(&["a", "3"], &y)?;
682        mydb.put(&["a", "4"], &z)?;
683
684        // Test iteration with getItemIter
685        let items: Vec<(Vec<String>, HashMap<String, String>)> = mydb
686            .get_item_iter(&[] as &[&str])?
687            .into_iter()
688            .map(|(keys, data)| (keys, data.to_map()))
689            .collect();
690
691        assert_eq!(
692            items,
693            vec![
694                (vec!["a".to_string(), "1".to_string()], w.to_map()),
695                (vec!["a".to_string(), "2".to_string()], x.to_map()),
696                (vec!["a".to_string(), "3".to_string()], y.to_map()),
697                (vec!["a".to_string(), "4".to_string()], z.to_map()),
698            ]
699        );
700
701        // Add more data
702        mydb.put(&["b", "1"], &w)?;
703        mydb.put(&["b", "2"], &x)?;
704        mydb.put(&["bc", "3"], &y)?;
705        mydb.put(&["bc", "4"], &z)?;
706
707        // Test iteration with specific top-level keys
708        let topkeys = ["b", ""];
709        let items: Vec<(Vec<String>, HashMap<String, String>)> = mydb
710            .get_item_iter(&topkeys)?
711            .into_iter()
712            .map(|(keys, data)| (keys, data.to_map()))
713            .collect();
714
715        assert_eq!(
716            items,
717            vec![
718                (vec!["b".to_string(), "1".to_string()], w.to_map()),
719                (vec!["b".to_string(), "2".to_string()], x.to_map()),
720            ]
721        );
722
723        // Test full iteration
724        let items: Vec<(Vec<String>, HashMap<String, String>)> = mydb
725            .get_item_iter(&[] as &[&str])?
726            .into_iter()
727            .map(|(keys, data)| (keys, data.to_map()))
728            .collect();
729
730        assert_eq!(
731            items,
732            vec![
733                (vec!["a".to_string(), "1".to_string()], w.to_map()),
734                (vec!["a".to_string(), "2".to_string()], x.to_map()),
735                (vec!["a".to_string(), "3".to_string()], y.to_map()),
736                (vec!["a".to_string(), "4".to_string()], z.to_map()),
737                (vec!["b".to_string(), "1".to_string()], w.to_map()),
738                (vec!["b".to_string(), "2".to_string()], x.to_map()),
739                (vec!["bc".to_string(), "3".to_string()], y.to_map()),
740                (vec!["bc".to_string(), "4".to_string()], z.to_map()),
741            ]
742        );
743
744        // Test count all entries
745        assert_eq!(mydb.cnt_all()?, 8);
746
747        // Test trim by b prefix
748        assert!(mydb.trim(&["b", ""])?);
749
750        let items: Vec<(Vec<String>, HashMap<String, String>)> = mydb
751            .get_item_iter(&[] as &[&str])?
752            .into_iter()
753            .map(|(keys, data)| (keys, data.to_map()))
754            .collect();
755
756        assert_eq!(
757            items,
758            vec![
759                (vec!["a".to_string(), "1".to_string()], w.to_map()),
760                (vec!["a".to_string(), "2".to_string()], x.to_map()),
761                (vec!["a".to_string(), "3".to_string()], y.to_map()),
762                (vec!["a".to_string(), "4".to_string()], z.to_map()),
763                (vec!["bc".to_string(), "3".to_string()], y.to_map()),
764                (vec!["bc".to_string(), "4".to_string()], z.to_map()),
765            ]
766        );
767
768        // Test trim all
769        assert!(mydb.trim::<[u8; 0]>(&[])?);
770
771        let items: Vec<(Vec<String>, Stuff)> = mydb.get_item_iter(&[] as &[&str])?;
772
773        assert_eq!(items, vec![]);
774
775        // Drop database
776        drop(db_ref);
777
778        // Check database is closed and files are removed
779        // No need to check manually as it's handled by the LMDBer drop implementation
780
781        Ok(())
782    }
783
784    #[test]
785    fn test_kom_put_get() -> Result<(), Box<dyn std::error::Error>> {
786        // Open test database
787        let lmdber = LMDBer::builder().name("test").temp(true).build()?;
788
789        let db_ref = Arc::new(lmdber);
790
791        // Create Komer instance
792        let mydb = Komer::<Stuff>::new(db_ref.clone(), "recs.", SerialKind::Json)?;
793
794        let data = Stuff {
795            a: "Test".to_string(),
796            b: "Value".to_string(),
797        };
798
799        // Put data
800        assert!(mydb.put(&["test", "key"], &data)?);
801
802        // Get data
803        let retrieved = mydb.get(&["test", "key"])?.unwrap();
804        assert_eq!(retrieved, data);
805
806        // Verify non-existent key returns None
807        assert!(mydb.get(&["nonexistent", "key"])?.is_none());
808
809        // Test duplicate put (shouldn't overwrite)
810        let new_data = Stuff {
811            a: "Modified".to_string(),
812            b: "Data".to_string(),
813        };
814
815        assert!(!mydb.put(&["test", "key"], &new_data)?);
816
817        // Verify original data wasn't changed
818        let retrieved = mydb.get(&["test", "key"])?.unwrap();
819        assert_eq!(retrieved, data);
820
821        // Test pin (should overwrite)
822        assert!(mydb.pin(&["test", "key"], &new_data)?);
823
824        // Verify data was changed
825        let retrieved = mydb.get(&["test", "key"])?.unwrap();
826        assert_eq!(retrieved, new_data);
827
828        // Test removal
829        assert!(mydb.rem(&["test", "key"])?);
830        assert!(mydb.get(&["test", "key"])?.is_none());
831
832        // Test removal of non-existent key
833        assert!(!mydb.rem(&["test", "key"])?);
834
835        Ok(())
836    }
837
838    #[test]
839    fn test_kom_serialization_formats() -> Result<(), Box<dyn std::error::Error>> {
840        // Open test database
841        let lmdber = LMDBer::builder().name("test").temp(true).build()?;
842
843        let db_ref = Arc::new(lmdber);
844
845        let test_data = Stuff {
846            a: "Test".to_string(),
847            b: "Value".to_string(),
848        };
849
850        // Test all serialization formats
851        for format in [SerialKind::Json, SerialKind::MsgPack, SerialKind::Cbor].iter() {
852            let db_name = format!("test_{:?}", format);
853            let mydb = Komer::<Stuff>::new(db_ref.clone(), &db_name, *format)?;
854
855            // Put and get data
856            mydb.put(&["test"], &test_data)?;
857            let retrieved = mydb.get(&["test"])?.unwrap();
858
859            assert_eq!(retrieved, test_data);
860        }
861
862        Ok(())
863    }
864
865    #[test]
866    fn test_kom_empty_keys() -> Result<(), Box<dyn std::error::Error>> {
867        // Open test database
868        let lmdber = LMDBer::builder().name("test").temp(true).build()?;
869
870        let db_ref = Arc::new(lmdber);
871
872        // Create Komer instance
873        let mydb = Komer::<Stuff>::new(db_ref.clone(), "recs.", SerialKind::Json)?;
874
875        let data = Stuff {
876            a: "Test".to_string(),
877            b: "Value".to_string(),
878        };
879
880        // Test with empty keys slice
881        let empty_keys: [&str; 0] = [];
882        let result = mydb.put(&empty_keys, &data);
883
884        assert!(matches!(result, Err(KomerError::EmptyKeys)));
885
886        Ok(())
887    }
888}