Abstract database behind generic interface and implement alternative drivers #322

Merged
lx merged 64 commits from db-abstraction into main 2022-06-08 08:01:56 +00:00
2 changed files with 24 additions and 1 deletions
Showing only changes of commit 72a87c8686 - Show all commits

View file

@ -56,7 +56,9 @@ fn open_db(path: PathBuf, engine: String) -> Result<Db> {
})?; })?;
let map_size = if u32::MAX as usize == usize::MAX { let map_size = if u32::MAX as usize == usize::MAX {
eprintln!("LMDB is not recommended on 32-bit systems, database size will be limited"); eprintln!(
"LMDB is not recommended on 32-bit systems, database size will be limited"
);
1usize << 30 // 1GB for 32-bit systems 1usize << 30 // 1GB for 32-bit systems
} else { } else {
1usize << 40 // 1TB for 64-bit systems 1usize << 40 // 1TB for 64-bit systems

View file

@ -139,40 +139,50 @@ impl Db {
#[allow(clippy::len_without_is_empty)] #[allow(clippy::len_without_is_empty)]
impl Tree { impl Tree {
#[inline]
pub fn db(&self) -> Db { pub fn db(&self) -> Db {
Db(self.0.clone()) Db(self.0.clone())
} }
#[inline]
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()) self.0.get(self.1, key.as_ref())
} }
#[inline]
pub fn len(&self) -> Result<usize> { pub fn len(&self) -> Result<usize> {
self.0.len(self.1) self.0.len(self.1)
} }
#[inline]
pub fn first(&self) -> Result<Option<(Value, Value)>> { pub fn first(&self) -> Result<Option<(Value, Value)>> {
self.iter()?.next().transpose() self.iter()?.next().transpose()
} }
#[inline]
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))? self.range((Bound::Excluded(from), Bound::Unbounded))?
.next() .next()
.transpose() .transpose()
} }
#[inline]
pub fn insert<T: AsRef<[u8]>, U: AsRef<[u8]>>(&self, key: T, value: U) -> Result<()> { pub fn insert<T: AsRef<[u8]>, U: AsRef<[u8]>>(&self, key: T, value: U) -> Result<()> {
self.0.insert(self.1, key.as_ref(), value.as_ref()) self.0.insert(self.1, key.as_ref(), value.as_ref())
} }
#[inline]
pub fn remove<T: AsRef<[u8]>>(&self, key: T) -> Result<bool> { pub fn remove<T: AsRef<[u8]>>(&self, key: T) -> Result<bool> {
self.0.remove(self.1, key.as_ref()) self.0.remove(self.1, key.as_ref())
} }
#[inline]
pub fn iter(&self) -> Result<ValueIter<'_>> { pub fn iter(&self) -> Result<ValueIter<'_>> {
self.0.iter(self.1) self.0.iter(self.1)
} }
#[inline]
pub fn iter_rev(&self) -> Result<ValueIter<'_>> { pub fn iter_rev(&self) -> Result<ValueIter<'_>> {
self.0.iter_rev(self.1) self.0.iter_rev(self.1)
} }
#[inline]
pub fn range<K, R>(&self, range: R) -> Result<ValueIter<'_>> pub fn range<K, R>(&self, range: R) -> Result<ValueIter<'_>>
where where
K: AsRef<[u8]>, K: AsRef<[u8]>,
@ -182,6 +192,7 @@ impl Tree {
let eb = range.end_bound(); let eb = range.end_bound();
self.0.range(self.1, get_bound(sb), get_bound(eb)) self.0.range(self.1, get_bound(sb), get_bound(eb))
} }
#[inline]
pub fn range_rev<K, R>(&self, range: R) -> Result<ValueIter<'_>> pub fn range_rev<K, R>(&self, range: R) -> Result<ValueIter<'_>>
where where
K: AsRef<[u8]>, K: AsRef<[u8]>,
@ -195,13 +206,16 @@ impl Tree {
#[allow(clippy::len_without_is_empty)] #[allow(clippy::len_without_is_empty)]
impl<'a> Transaction<'a> { impl<'a> Transaction<'a> {
#[inline]
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()) self.0.get(tree.1, key.as_ref())
} }
#[inline]
pub fn len(&self, tree: &Tree) -> Result<usize> { pub fn len(&self, tree: &Tree) -> Result<usize> {
self.0.len(tree.1) self.0.len(tree.1)
} }
#[inline]
pub fn insert<T: AsRef<[u8]>, U: AsRef<[u8]>>( pub fn insert<T: AsRef<[u8]>, U: AsRef<[u8]>>(
&mut self, &mut self,
tree: &Tree, tree: &Tree,
@ -210,17 +224,21 @@ impl<'a> Transaction<'a> {
) -> Result<()> { ) -> Result<()> {
self.0.insert(tree.1, key.as_ref(), value.as_ref()) self.0.insert(tree.1, key.as_ref(), value.as_ref())
} }
#[inline]
pub fn remove<T: AsRef<[u8]>>(&mut self, tree: &Tree, key: T) -> Result<bool> { pub fn remove<T: AsRef<[u8]>>(&mut self, tree: &Tree, key: T) -> Result<bool> {
self.0.remove(tree.1, key.as_ref()) self.0.remove(tree.1, key.as_ref())
} }
#[inline]
pub fn iter(&self, tree: &Tree) -> Result<ValueIter<'_>> { pub fn iter(&self, tree: &Tree) -> Result<ValueIter<'_>> {
self.0.iter(tree.1) self.0.iter(tree.1)
} }
#[inline]
pub fn iter_rev(&self, tree: &Tree) -> Result<ValueIter<'_>> { pub fn iter_rev(&self, tree: &Tree) -> Result<ValueIter<'_>> {
self.0.iter_rev(tree.1) self.0.iter_rev(tree.1)
} }
#[inline]
pub fn range<K, R>(&self, tree: &Tree, range: R) -> Result<ValueIter<'_>> pub fn range<K, R>(&self, tree: &Tree, range: R) -> Result<ValueIter<'_>>
where where
K: AsRef<[u8]>, K: AsRef<[u8]>,
@ -230,6 +248,7 @@ impl<'a> Transaction<'a> {
let eb = range.end_bound(); let eb = range.end_bound();
self.0.range(tree.1, get_bound(sb), get_bound(eb)) self.0.range(tree.1, get_bound(sb), get_bound(eb))
} }
#[inline]
pub fn range_rev<K, R>(&self, tree: &Tree, range: R) -> Result<ValueIter<'_>> pub fn range_rev<K, R>(&self, tree: &Tree, range: R) -> Result<ValueIter<'_>>
where where
K: AsRef<[u8]>, K: AsRef<[u8]>,
@ -242,10 +261,12 @@ impl<'a> Transaction<'a> {
// ---- // ----
#[inline]
pub fn abort<R, E>(self, e: E) -> TxResult<R, E> { pub fn abort<R, E>(self, e: E) -> TxResult<R, E> {
Err(TxError::Abort(e)) Err(TxError::Abort(e))
} }
#[inline]
pub fn commit<R, E>(self, r: R) -> TxResult<R, E> { pub fn commit<R, E>(self, r: R) -> TxResult<R, E> {
Ok(r) Ok(r)
} }