diff options
author | Alex Auvolat <alex@adnab.me> | 2022-06-03 12:35:12 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-06-03 12:35:12 +0200 |
commit | 4bf706b170f149f54fd701576692cd5cd5bc9779 (patch) | |
tree | 9f85ddd71c76edfdbc7cfd0d016ae059bc5326e5 /src/db | |
parent | bd9ff432d716020d7235ece76257b60dc3ead95a (diff) | |
download | garage-4bf706b170f149f54fd701576692cd5cd5bc9779.tar.gz garage-4bf706b170f149f54fd701576692cd5cd5bc9779.zip |
Slightly prettier code
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/lib.rs | 9 | ||||
-rw-r--r-- | src/db/sqlite_adapter.rs | 16 |
2 files changed, 17 insertions, 8 deletions
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<Option<(Value<'_>, Value<'_>)>> { + self.iter()?.next().transpose() + } + pub fn get_gt<T: AsRef<[u8]>>(&self, from: T) -> Result<Option<(Value<'_>, Value<'_>)>> { + self.range((Bound::Excluded(from), Bound::Unbounded))? + .next() + .transpose() + } + pub fn insert<T: AsRef<[u8]>, 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<Option<Value<'_>>> { 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<bool> { 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<usize> { 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), |