• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

getdozer / dozer / 4116183752

pending completion
4116183752

push

github

GitHub
refactor: Make `LmdbRoCache` and `LmdbRwCache` `Send` and `Sync` (#821)

790 of 790 new or added lines in 44 files covered. (100.0%)

23005 of 33842 relevant lines covered (67.98%)

56312.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.72
/dozer-cache/src/cache/lmdb/cache/id_database.rs
1
use dozer_storage::{
2
    lmdb::{Database, DatabaseFlags, RwTransaction, Transaction, WriteFlags},
3
    lmdb_storage::LmdbEnvironmentManager,
4
};
5

6
use crate::{
7
    cache::lmdb::query::helper,
8
    errors::{CacheError, QueryError},
9
};
10

11
#[derive(Debug, Clone, Copy)]
×
12
pub struct IdDatabase(Database);
13

14
impl IdDatabase {
15
    pub fn new(
98✔
16
        env: &mut LmdbEnvironmentManager,
98✔
17
        create_if_not_exist: bool,
98✔
18
    ) -> Result<Self, CacheError> {
98✔
19
        let flags = if create_if_not_exist {
98✔
20
            Some(DatabaseFlags::empty())
96✔
21
        } else {
22
            None
2✔
23
        };
24
        let db = env.create_database(Some("primary_index"), flags)?;
98✔
25
        Ok(Self(db))
98✔
26
    }
98✔
27

28
    pub fn get_or_generate(
29
        &self,
30
        txn: &mut RwTransaction,
31
        key: Option<&[u8]>,
32
    ) -> Result<[u8; 8], CacheError> {
33
        if let Some(key) = key {
7,994✔
34
            match txn.get(self.0, &key) {
7,992✔
35
                Ok(id) => Ok(id
1✔
36
                    .try_into()
1✔
37
                    .expect("All values must be u64 ids in this database")),
1✔
38
                Err(dozer_storage::lmdb::Error::NotFound) => self.generate_id(txn, Some(key)),
7,991✔
39
                Err(e) => Err(CacheError::Query(QueryError::InsertValue(e))),
×
40
            }
41
        } else {
42
            self.generate_id(txn, None)
2✔
43
        }
44
    }
7,994✔
45

46
    fn generate_id(
7,993✔
47
        &self,
7,993✔
48
        txn: &mut RwTransaction,
7,993✔
49
        key: Option<&[u8]>,
7,993✔
50
    ) -> Result<[u8; 8], CacheError> {
7,993✔
51
        let id = helper::lmdb_stat(txn, self.0)
7,993✔
52
            .map_err(|e| CacheError::Internal(Box::new(e)))?
7,993✔
53
            .ms_entries as u64;
54
        let id: [u8; 8] = id.to_be_bytes();
7,993✔
55

7,993✔
56
        let key = key.unwrap_or(&id);
7,993✔
57

7,993✔
58
        txn.put(self.0, &key, &id, WriteFlags::NO_OVERWRITE)
7,993✔
59
            .map_err(|e| CacheError::Query(QueryError::InsertValue(e)))?;
7,993✔
60

61
        Ok(id)
7,993✔
62
    }
7,993✔
63

64
    pub fn get<T: Transaction>(&self, txn: &T, key: &[u8]) -> Result<[u8; 8], CacheError> {
46✔
65
        txn.get(self.0, &key)
46✔
66
            .map_err(|e| CacheError::Query(QueryError::GetValue(e)))
46✔
67
            .map(|id| {
46✔
68
                id.try_into()
46✔
69
                    .expect("All values must be u64 ids in this database")
46✔
70
            })
46✔
71
    }
46✔
72
}
73

74
#[cfg(test)]
75
mod tests {
76
    use crate::cache::{lmdb::utils::init_env, CacheOptions};
77

78
    use super::*;
79

80
    #[test]
1✔
81
    fn test_id_database() {
1✔
82
        let mut env = init_env(&CacheOptions::default()).unwrap();
1✔
83
        let writer = IdDatabase::new(&mut env, true).unwrap();
1✔
84
        let reader = IdDatabase::new(&mut env, false).unwrap();
1✔
85

1✔
86
        let key = b"key";
1✔
87

1✔
88
        let txn = env.create_txn().unwrap();
1✔
89
        let mut txn = txn.write();
1✔
90
        let id = writer.get_or_generate(txn.txn_mut(), Some(key)).unwrap();
1✔
91
        writer.get_or_generate(txn.txn_mut(), None).unwrap();
1✔
92
        txn.commit_and_renew().unwrap();
1✔
93

1✔
94
        assert_eq!(writer.get(txn.txn(), key).unwrap(), id);
1✔
95
        assert_eq!(reader.get(txn.txn(), key).unwrap(), id);
1✔
96
        txn.commit_and_renew().unwrap();
1✔
97
    }
1✔
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc