diff options
author | Alex Auvolat <alex@adnab.me> | 2022-06-06 14:01:44 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-06-06 14:01:44 +0200 |
commit | 7f2cf0b809f1fc5741990e2bfff94dc3ec41a04f (patch) | |
tree | d682dfba3c86d823b0447db21bd7740b2ab3d772 /src/db/lmdb_adapter.rs | |
parent | 4539a6c2298cfb4578261060e4a5af739a45c99f (diff) | |
download | garage-7f2cf0b809f1fc5741990e2bfff94dc3ec41a04f.tar.gz garage-7f2cf0b809f1fc5741990e2bfff94dc3ec41a04f.zip |
Safe choice: return Vec<u8> and not some fancy zero-copy type
Diffstat (limited to 'src/db/lmdb_adapter.rs')
-rw-r--r-- | src/db/lmdb_adapter.rs | 70 |
1 files changed, 11 insertions, 59 deletions
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<Option<Value<'_>>> { + fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>> { 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<bool> { @@ -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<Option<Value<'_>>> { + fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>> { 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<Box<TxAndValue<'a>>>); - -impl<'a> IValue<'a> for TxAndValuePin<'a> { - fn take_maybe(&mut self) -> Vec<u8> { - 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<( <ByteSlice as BytesDecode<'a>>::DItem, <ByteSlice as BytesDecode<'a>>::DItem, @@ -365,7 +321,7 @@ impl<'a, I> Iterator for TxAndIteratorPin<'a, I> where I: Iterator<Item = IteratorItem<'a>> + 'a, { - type Item = Result<(Value<'a>, Value<'a>)>; + type Item = Result<(Value, Value)>; fn next(&mut self) -> Option<Self::Item> { 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()))), } } } |