From a3a01141ec8f8eefe1a31162bd53fbaa2d37e601 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 7 Jun 2022 17:05:17 +0200 Subject: db abstraction: make .insert() and .remove() return the old value --- src/db/sqlite_adapter.rs | 98 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 29 deletions(-) (limited to 'src/db/sqlite_adapter.rs') diff --git a/src/db/sqlite_adapter.rs b/src/db/sqlite_adapter.rs index 32557a53..b23885bd 100644 --- a/src/db/sqlite_adapter.rs +++ b/src/db/sqlite_adapter.rs @@ -125,18 +125,6 @@ impl IDb for SqliteDb { this.internal_get(tree, key) } - fn remove(&self, tree: usize, key: &[u8]) -> Result { - trace!("remove {}: lock db", tree); - let this = self.0.lock().unwrap(); - trace!("remove {}: lock acquired", tree); - - let tree = this.get_tree(tree)?; - let res = this - .db - .execute(&format!("DELETE FROM {} WHERE k = ?1", tree), params![key])?; - Ok(res > 0) - } - fn len(&self, tree: usize) -> Result { trace!("len {}: lock db", tree); let this = self.0.lock().unwrap(); @@ -151,18 +139,50 @@ impl IDb for SqliteDb { } } - fn insert(&self, tree: usize, key: &[u8], value: &[u8]) -> Result { + fn insert(&self, tree: usize, key: &[u8], value: &[u8]) -> Result> { trace!("insert {}: lock db", tree); let this = self.0.lock().unwrap(); trace!("insert {}: lock acquired", tree); let tree = this.get_tree(tree)?; let old_val = this.internal_get(tree, key)?; - this.db.execute( - &format!("INSERT OR REPLACE INTO {} (k, v) VALUES (?1, ?2)", tree), - params![key, value], - )?; - Ok(old_val.is_none()) + + match &old_val { + Some(_) => { + let n = this.db.execute( + &format!("UPDATE {} SET v = ?2 WHERE k = ?1", tree), + params![key, value], + )?; + assert_eq!(n, 1); + } + None => { + let n = this.db.execute( + &format!("INSERT INTO {} (k, v) VALUES (?1, ?2)", tree), + params![key, value], + )?; + assert_eq!(n, 1); + } + } + + Ok(old_val) + } + + fn remove(&self, tree: usize, key: &[u8]) -> Result> { + trace!("remove {}: lock db", tree); + let this = self.0.lock().unwrap(); + trace!("remove {}: lock acquired", tree); + + let tree = this.get_tree(tree)?; + let old_val = this.internal_get(tree, key)?; + + if old_val.is_some() { + let n = this + .db + .execute(&format!("DELETE FROM {} WHERE k = ?1", tree), params![key])?; + assert_eq!(n, 1); + } + + Ok(old_val) } fn iter(&self, tree: usize) -> Result> { @@ -308,21 +328,41 @@ impl<'a> ITx for SqliteTx<'a> { } } - fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> Result { + fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> Result> { let tree = self.get_tree(tree)?; let old_val = self.internal_get(tree, key)?; - self.tx.execute( - &format!("INSERT OR REPLACE INTO {} (k, v) VALUES (?1, ?2)", tree), - params![key, value], - )?; - Ok(old_val.is_none()) + + match &old_val { + Some(_) => { + let n = self.tx.execute( + &format!("UPDATE {} SET v = ?2 WHERE k = ?1", tree), + params![key, value], + )?; + assert_eq!(n, 1); + } + None => { + let n = self.tx.execute( + &format!("INSERT INTO {} (k, v) VALUES (?1, ?2)", tree), + params![key, value], + )?; + assert_eq!(n, 1); + } + } + + Ok(old_val) } - fn remove(&mut self, tree: usize, key: &[u8]) -> Result { + fn remove(&mut self, tree: usize, key: &[u8]) -> Result> { let tree = self.get_tree(tree)?; - let res = self - .tx - .execute(&format!("DELETE FROM {} WHERE k = ?1", tree), params![key])?; - Ok(res > 0) + let old_val = self.internal_get(tree, key)?; + + if old_val.is_some() { + let n = self + .tx + .execute(&format!("DELETE FROM {} WHERE k = ?1", tree), params![key])?; + assert_eq!(n, 1); + } + + Ok(old_val) } fn iter(&self, _tree: usize) -> Result> { -- cgit v1.2.3