From 4e72c713f157ae9d5103a461c4c213b2aa6a84b9 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 3 Jun 2022 15:31:07 +0200 Subject: Start LMDB adapter, with fixed semantics --- src/table/data.rs | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'src/table/data.rs') diff --git a/src/table/data.rs b/src/table/data.rs index 17402bb6..cca96f68 100644 --- a/src/table/data.rs +++ b/src/table/data.rs @@ -182,7 +182,7 @@ where tree_key: &[u8], f: impl Fn(Option) -> F::E, ) -> Result, Error> { - let changed = self.store.db().transaction(|tx| { + let changed = self.store.db().transaction(|mut tx| { let (old_entry, old_bytes, new_entry) = match tx.get(&self.store, tree_key)? { Some(old_bytes) => { let old_entry = self.decode_entry(&old_bytes).map_err(db::TxError::Abort)?; @@ -203,6 +203,7 @@ where .map_err(Error::RmpEncode) .map_err(db::TxError::Abort)?; let encoding_changed = Some(&new_bytes[..]) != old_bytes.as_ref().map(|x| &x[..]); + drop(old_bytes); if value_changed || encoding_changed { let new_bytes_hash = blake2sum(&new_bytes[..]); @@ -241,15 +242,16 @@ where } pub(crate) fn delete_if_equal(self: &Arc, k: &[u8], v: &[u8]) -> Result { - let removed = self.store.db().transaction(|tx| { - if let Some(cur_v) = tx.get(&self.store, k)? { - if cur_v == v { - tx.remove(&self.store, k)?; - tx.insert(&self.merkle_todo, k, vec![])?; - return Ok(true); - } + let removed = self.store.db().transaction(|mut tx| { + let remove = match tx.get(&self.store, k)? { + Some(cur_v) if cur_v == v => true, + _ => false, + }; + if remove { + tx.remove(&self.store, k)?; + tx.insert(&self.merkle_todo, k, vec![])?; } - Ok(false) + Ok(remove) })?; if removed { @@ -267,15 +269,16 @@ where k: &[u8], vhash: Hash, ) -> Result { - let removed = self.store.db().transaction(|tx| { - if let Some(cur_v) = tx.get(&self.store, k)? { - if blake2sum(&cur_v[..]) == vhash { - tx.remove(&self.store, k)?; - tx.insert(&self.merkle_todo, k, vec![])?; - return Ok(Some(cur_v.into_vec())); - } + let removed = self.store.db().transaction(|mut tx| { + let remove_v = match tx.get(&self.store, k)? { + Some(cur_v) if blake2sum(&cur_v[..]) == vhash => Some(cur_v.into_vec()), + _ => None, + }; + if remove_v.is_some() { + tx.remove(&self.store, k)?; + tx.insert(&self.merkle_todo, k, vec![])?; } - Ok(None) + Ok(remove_v) })?; if let Some(old_v) = removed { -- cgit v1.2.3