From 7f2cf0b809f1fc5741990e2bfff94dc3ec41a04f Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 6 Jun 2022 14:01:44 +0200 Subject: Safe choice: return Vec and not some fancy zero-copy type --- src/db/lmdb_adapter.rs | 70 ++++++++------------------------------------------ 1 file changed, 11 insertions(+), 59 deletions(-) (limited to 'src/db/lmdb_adapter.rs') diff --git a/src/db/lmdb_adapter.rs b/src/db/lmdb_adapter.rs index aa365733..3d0edb38 100644 --- a/src/db/lmdb_adapter.rs +++ b/src/db/lmdb_adapter.rs @@ -11,7 +11,7 @@ use heed::types::ByteSlice; use heed::{BytesDecode, Env, RoTxn, RwTxn, UntypedDatabase as Database}; use crate::{ - Db, Error, IDb, ITx, ITxFn, IValue, Result, TxError, TxFnResult, TxResult, Value, ValueIter, + Db, Error, IDb, ITx, ITxFn, Result, TxError, TxFnResult, TxResult, Value, ValueIter, }; pub use heed; @@ -101,28 +101,15 @@ impl IDb for LmdbDb { // ---- - fn get(&self, tree: usize, key: &[u8]) -> Result>> { + fn get(&self, tree: usize, key: &[u8]) -> Result> { let tree = self.get_tree(tree)?; - let res = TxAndValue { - tx: self.db.read_txn()?, - value: NonNull::dangling(), - _pin: PhantomPinned, - }; - let mut boxed = Box::pin(res); - - unsafe { - let tx = NonNull::from(&boxed.tx); - let val = match tree.get(tx.as_ref(), &key)? { - None => return Ok(None), - Some(v) => v, - }; - - let mut_ref: Pin<&mut TxAndValue<'_>> = Pin::as_mut(&mut boxed); - Pin::get_unchecked_mut(mut_ref).value = NonNull::from(&val); + let tx = self.db.read_txn()?; + let val = tree.get(&tx, &key)?; + match val { + None => Ok(None), + Some(v) => Ok(Some(v.to_vec())), } - - Ok(Some(Value(Box::new(TxAndValuePin(boxed))))) } fn remove(&self, tree: usize, key: &[u8]) -> Result { @@ -227,13 +214,10 @@ impl<'a, 'db> LmdbTx<'a, 'db> { } impl<'a, 'db> ITx for LmdbTx<'a, 'db> { - fn get(&self, tree: usize, key: &[u8]) -> Result>> { + fn get(&self, tree: usize, key: &[u8]) -> Result> { let tree = self.get_tree(tree)?; match tree.get(&self.tx, &key)? { - Some(v) => { - let v: &'_ [u8] = v; - Ok(Some(v.into())) - } + Some(v) => Ok(Some(v.to_vec())), None => Ok(None), } } @@ -279,34 +263,6 @@ impl<'a, 'db> ITx for LmdbTx<'a, 'db> { // ---- -struct TxAndValue<'a> { - tx: RoTxn<'a>, - value: NonNull<&'a [u8]>, - _pin: PhantomPinned, -} - -struct TxAndValuePin<'a>(Pin>>); - -impl<'a> IValue<'a> for TxAndValuePin<'a> { - fn take_maybe(&mut self) -> Vec { - self.as_ref().to_vec() - } -} - -impl<'a> AsRef<[u8]> for TxAndValuePin<'a> { - fn as_ref(&self) -> &[u8] { - unsafe { self.0.value.as_ref() } - } -} - -impl<'a> std::borrow::Borrow<[u8]> for TxAndValuePin<'a> { - fn borrow(&self) -> &[u8] { - self.as_ref() - } -} - -// ---- - type IteratorItem<'a> = heed::Result<( >::DItem, >::DItem, @@ -365,7 +321,7 @@ impl<'a, I> Iterator for TxAndIteratorPin<'a, I> where I: Iterator> + 'a, { - type Item = Result<(Value<'a>, Value<'a>)>; + type Item = Result<(Value, Value)>; fn next(&mut self) -> Option { let iter_ref = unsafe { @@ -375,11 +331,7 @@ where match iter_ref.unwrap().next() { None => None, Some(Err(e)) => Some(Err(e.into())), - Some(Ok((k, v))) => { - let k: &'a [u8] = k; - let v: &'a [u8] = v; - Some(Ok((k.into(), v.into()))) - } + Some(Ok((k, v))) => Some(Ok((k.to_vec(), v.to_vec()))), } } } -- cgit v1.2.3