From 4bf706b170f149f54fd701576692cd5cd5bc9779 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 3 Jun 2022 12:35:12 +0200 Subject: Slightly prettier code --- src/db/lib.rs | 9 +++++++++ src/db/sqlite_adapter.rs | 16 ++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'src/db') diff --git a/src/db/lib.rs b/src/db/lib.rs index 1b31df43..045c16c5 100644 --- a/src/db/lib.rs +++ b/src/db/lib.rs @@ -226,6 +226,15 @@ impl Tree { self.0.len(self.1) } + pub fn first(&self) -> Result, Value<'_>)>> { + self.iter()?.next().transpose() + } + pub fn get_gt>(&self, from: T) -> Result, Value<'_>)>> { + self.range((Bound::Excluded(from), Bound::Unbounded))? + .next() + .transpose() + } + pub fn insert, U: AsRef<[u8]>>(&self, key: T, value: U) -> Result<()> { self.0.insert(self.1, key.as_ref(), value.as_ref()) } diff --git a/src/db/sqlite_adapter.rs b/src/db/sqlite_adapter.rs index 49c07562..ab7efbf8 100644 --- a/src/db/sqlite_adapter.rs +++ b/src/db/sqlite_adapter.rs @@ -107,9 +107,9 @@ impl IDb for SqliteDb { fn get(&self, tree: usize, key: &[u8]) -> Result>> { let tree = self.get_tree(tree)?; - trace!("get: lock db"); + trace!("get {}: lock db", tree); let db = self.db.lock().unwrap(); - trace!("get: lock acquired"); + trace!("get {}: lock acquired", tree); let mut stmt = db.prepare(&format!("SELECT v FROM {} WHERE k = ?1", tree))?; let mut res_iter = stmt.query([key])?; @@ -122,9 +122,9 @@ impl IDb for SqliteDb { fn remove(&self, tree: usize, key: &[u8]) -> Result { let tree = self.get_tree(tree)?; - trace!("remove: lock db"); + trace!("remove {}: lock db", tree); let db = self.db.lock().unwrap(); - trace!("remove: lock acquired"); + trace!("remove {}: lock acquired", tree); let res = db.execute(&format!("DELETE FROM {} WHERE k = ?1", tree), params![key])?; Ok(res > 0) @@ -133,9 +133,9 @@ impl IDb for SqliteDb { fn len(&self, tree: usize) -> Result { let tree = self.get_tree(tree)?; - trace!("len: lock db"); + trace!("len {}: lock db", tree); let db = self.db.lock().unwrap(); - trace!("len: lock acquired"); + trace!("len {}: lock acquired", tree); let mut stmt = db.prepare(&format!("SELECT COUNT(*) FROM {}", tree))?; let mut res_iter = stmt.query([])?; @@ -148,9 +148,9 @@ impl IDb for SqliteDb { fn insert(&self, tree: usize, key: &[u8], value: &[u8]) -> Result<()> { let tree = self.get_tree(tree)?; - trace!("insert: lock db"); + trace!("insert {}: lock db", tree); let db = self.db.lock().unwrap(); - trace!("insert: lock acquired"); + trace!("insert {}: lock acquired", tree); db.execute( &format!("INSERT OR REPLACE INTO {} (k, v) VALUES (?1, ?2)", tree), -- cgit v1.2.3