aboutsummaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/db')
-rw-r--r--src/db/lib.rs101
-rw-r--r--src/db/lmdb_adapter.rs70
-rw-r--r--src/db/sled_adapter.rs39
-rw-r--r--src/db/sqlite_adapter.rs12
4 files changed, 35 insertions, 187 deletions
diff --git a/src/db/lib.rs b/src/db/lib.rs
index 2e847f7a..b9c27d9a 100644
--- a/src/db/lib.rs
+++ b/src/db/lib.rs
@@ -21,93 +21,8 @@ pub struct Transaction<'a>(pub(crate) &'a mut dyn ITx);
#[derive(Clone)]
pub struct Tree(pub(crate) Arc<dyn IDb>, pub(crate) usize);
-pub type ValueIter<'a> = Box<dyn std::iter::Iterator<Item = Result<(Value<'a>, Value<'a>)>> + 'a>;
-
-// ----
-
-pub struct Value<'a>(pub(crate) Box<dyn IValue<'a> + 'a>);
-
-pub trait IValue<'a>: AsRef<[u8]> + core::borrow::Borrow<[u8]> {
- fn take_maybe(&mut self) -> Vec<u8>;
-}
-
-impl<'a> Value<'a> {
- #[inline]
- pub fn into_vec(mut self) -> Vec<u8> {
- self.0.take_maybe()
- }
-}
-
-impl<'a> AsRef<[u8]> for Value<'a> {
- #[inline]
- fn as_ref(&self) -> &[u8] {
- self.0.as_ref().as_ref()
- }
-}
-
-impl<'a> std::borrow::Borrow<[u8]> for Value<'a> {
- #[inline]
- fn borrow(&self) -> &[u8] {
- self.0.as_ref().borrow()
- }
-}
-
-impl<'a> core::ops::Deref for Value<'a> {
- type Target = [u8];
- #[inline]
- fn deref(&self) -> &[u8] {
- self.0.as_ref().as_ref()
- }
-}
-
-impl<'a, T> PartialEq<T> for Value<'a>
-where
- T: AsRef<[u8]>,
-{
- fn eq(&self, other: &T) -> bool {
- self.as_ref() == other.as_ref()
- }
-}
-
-impl<'a> std::fmt::Debug for Value<'a> {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- for line in hexdump::hexdump_iter(self.as_ref()) {
- f.write_str(&line)?;
- f.write_str("\n")?;
- }
- Ok(())
- }
-}
-
-impl<'a> IValue<'a> for Vec<u8> {
- fn take_maybe(&mut self) -> Vec<u8> {
- std::mem::take(self)
- }
-}
-
-impl<'a> From<Value<'a>> for Vec<u8> {
- fn from(v: Value<'a>) -> Vec<u8> {
- v.into_vec()
- }
-}
-
-impl<'a> From<Vec<u8>> for Value<'a> {
- fn from(v: Vec<u8>) -> Value<'a> {
- Value(Box::new(v))
- }
-}
-
-impl<'a> From<&'a [u8]> for Value<'a> {
- fn from(v: &'a [u8]) -> Value<'a> {
- Value(Box::new(v))
- }
-}
-
-impl<'a> IValue<'a> for &'a [u8] {
- fn take_maybe(&mut self) -> Vec<u8> {
- self.to_vec()
- }
-}
+pub type Value = Vec<u8>;
+pub type ValueIter<'a> = Box<dyn std::iter::Iterator<Item = Result<(Value, Value)>> + 'a>;
// ----
@@ -228,17 +143,17 @@ impl Tree {
Db(self.0.clone())
}
- pub fn get<T: AsRef<[u8]>>(&self, key: T) -> Result<Option<Value<'_>>> {
+ pub fn get<T: AsRef<[u8]>>(&self, key: T) -> Result<Option<Value>> {
self.0.get(self.1, key.as_ref())
}
pub fn len(&self) -> Result<usize> {
self.0.len(self.1)
}
- pub fn first(&self) -> Result<Option<(Value<'_>, Value<'_>)>> {
+ 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<'_>)>> {
+ pub fn get_gt<T: AsRef<[u8]>>(&self, from: T) -> Result<Option<(Value, Value)>> {
self.range((Bound::Excluded(from), Bound::Unbounded))?
.next()
.transpose()
@@ -280,7 +195,7 @@ impl Tree {
#[allow(clippy::len_without_is_empty)]
impl<'a> Transaction<'a> {
- pub fn get<T: AsRef<[u8]>>(&self, tree: &Tree, key: T) -> Result<Option<Value<'_>>> {
+ pub fn get<T: AsRef<[u8]>>(&self, tree: &Tree, key: T) -> Result<Option<Value>> {
self.0.get(tree.1, key.as_ref())
}
pub fn len(&self, tree: &Tree) -> Result<usize> {
@@ -342,7 +257,7 @@ pub(crate) trait IDb: Send + Sync {
fn open_tree(&self, name: &str) -> Result<usize>;
fn list_trees(&self) -> Result<Vec<String>>;
- fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>>;
+ fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>>;
fn len(&self, tree: usize) -> Result<usize>;
fn insert(&self, tree: usize, key: &[u8], value: &[u8]) -> Result<()>;
@@ -368,7 +283,7 @@ pub(crate) trait IDb: Send + Sync {
}
pub(crate) trait ITx {
- fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>>;
+ fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>>;
fn len(&self, tree: usize) -> Result<usize>;
fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> Result<()>;
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()))),
}
}
}
diff --git a/src/db/sled_adapter.rs b/src/db/sled_adapter.rs
index 2953785e..489fae61 100644
--- a/src/db/sled_adapter.rs
+++ b/src/db/sled_adapter.rs
@@ -10,7 +10,7 @@ use sled::transaction::{
};
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 sled;
@@ -23,26 +23,6 @@ impl From<sled::Error> for Error {
}
}
-// -- val
-
-impl<'a> IValue<'a> for sled::IVec {
- fn take_maybe(&mut self) -> Vec<u8> {
- self.to_vec()
- }
-}
-
-impl<'a> From<Value<'a>> for sled::IVec {
- fn from(v: Value<'a>) -> sled::IVec {
- sled::IVec::from(v.into_vec())
- }
-}
-
-impl<'a> From<sled::IVec> for Value<'a> {
- fn from(v: sled::IVec) -> Value<'a> {
- Value(Box::new(v))
- }
-}
-
// -- db
pub struct SledDb {
@@ -99,9 +79,10 @@ impl IDb for SledDb {
// ----
- 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)?;
- Ok(tree.get(key)?.map(From::from))
+ let val = tree.get(key)?;
+ Ok(val.map(|x| x.to_vec()))
}
fn remove(&self, tree: usize, key: &[u8]) -> Result<bool> {
@@ -123,14 +104,14 @@ impl IDb for SledDb {
fn iter(&self, tree: usize) -> Result<ValueIter<'_>> {
let tree = self.get_tree(tree)?;
Ok(Box::new(tree.iter().map(|v| {
- v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into)
+ v.map(|(x, y)| (x.to_vec(), y.to_vec())).map_err(Into::into)
})))
}
fn iter_rev(&self, tree: usize) -> Result<ValueIter<'_>> {
let tree = self.get_tree(tree)?;
Ok(Box::new(tree.iter().rev().map(|v| {
- v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into)
+ v.map(|(x, y)| (x.to_vec(), y.to_vec())).map_err(Into::into)
})))
}
@@ -142,7 +123,7 @@ impl IDb for SledDb {
) -> Result<ValueIter<'_>> {
let tree = self.get_tree(tree)?;
Ok(Box::new(tree.range::<&'r [u8], _>((low, high)).map(|v| {
- v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into)
+ v.map(|(x, y)| (x.to_vec(), y.to_vec())).map_err(Into::into)
})))
}
fn range_rev<'r>(
@@ -153,7 +134,7 @@ impl IDb for SledDb {
) -> Result<ValueIter<'_>> {
let tree = self.get_tree(tree)?;
Ok(Box::new(tree.range::<&'r [u8], _>((low, high)).rev().map(
- |v| v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into),
+ |v| v.map(|(x, y)| (x.to_vec(), y.to_vec())).map_err(Into::into),
)))
}
@@ -218,10 +199,10 @@ impl<'a> SledTx<'a> {
}
impl<'a> ITx for SledTx<'a> {
- 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 tmp = self.save_error(tree.get(key))?;
- Ok(tmp.map(From::from))
+ Ok(tmp.map(|x| x.to_vec()))
}
fn len(&self, _tree: usize) -> Result<usize> {
unimplemented!(".len() in transaction not supported with Sled backend")
diff --git a/src/db/sqlite_adapter.rs b/src/db/sqlite_adapter.rs
index e945c31c..f0bca257 100644
--- a/src/db/sqlite_adapter.rs
+++ b/src/db/sqlite_adapter.rs
@@ -105,7 +105,7 @@ impl IDb for SqliteDb {
// ----
- fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>> {
+ fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>> {
trace!("get {}: lock db", tree);
let this = self.0.lock().unwrap();
trace!("get {}: lock acquired", tree);
@@ -118,7 +118,7 @@ impl IDb for SqliteDb {
let mut res_iter = stmt.query([key])?;
match res_iter.next()? {
None => Ok(None),
- Some(v) => Ok(Some(v.get::<_, Vec<u8>>(0)?.into())),
+ Some(v) => Ok(Some(v.get::<_, Vec<u8>>(0)?)),
}
}
@@ -279,7 +279,7 @@ impl<'a> SqliteTx<'a> {
}
impl<'a> ITx for SqliteTx<'a> {
- 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 mut stmt = self
.tx
@@ -287,7 +287,7 @@ impl<'a> ITx for SqliteTx<'a> {
let mut res_iter = stmt.query([key])?;
match res_iter.next()? {
None => Ok(None),
- Some(v) => Ok(Some(v.get::<_, Vec<u8>>(0)?.into())),
+ Some(v) => Ok(Some(v.get::<_, Vec<u8>>(0)?)),
}
}
fn len(&self, tree: usize) -> Result<usize> {
@@ -394,7 +394,7 @@ impl<'a> Drop for DbValueIterator<'a> {
struct DbValueIteratorPin<'a>(Pin<Box<DbValueIterator<'a>>>);
impl<'a> Iterator for DbValueIteratorPin<'a> {
- type Item = Result<(Value<'a>, Value<'a>)>;
+ type Item = Result<(Value, Value)>;
fn next(&mut self) -> Option<Self::Item> {
let next = unsafe {
@@ -414,7 +414,7 @@ impl<'a> Iterator for DbValueIteratorPin<'a> {
Err(e) => return Some(Err(e.into())),
Ok(y) => y,
};
- Some(Ok((k.into(), v.into())))
+ Some(Ok((k, v)))
}
}