[rm-sled] Implement some missing functionality in garage_db

This commit is contained in:
Alex Auvolat 2024-03-08 16:02:58 +01:00
parent 05c92204ec
commit 66c23890c1
No known key found for this signature in database
GPG key ID: 0E496D15096376BE
3 changed files with 19 additions and 2 deletions

View file

@ -274,6 +274,11 @@ impl<'a> Transaction<'a> {
pub fn remove<T: AsRef<[u8]>>(&mut self, tree: &Tree, key: T) -> TxOpResult<Option<Value>> { pub fn remove<T: AsRef<[u8]>>(&mut self, tree: &Tree, key: T) -> TxOpResult<Option<Value>> {
self.tx.remove(tree.1, key.as_ref()) self.tx.remove(tree.1, key.as_ref())
} }
/// Clears all values in a tree
#[inline]
pub fn clear(&mut self, tree: &Tree) -> TxOpResult<()> {
self.tx.clear(tree.1)
}
#[inline] #[inline]
pub fn iter(&self, tree: &Tree) -> TxOpResult<TxValueIter<'_>> { pub fn iter(&self, tree: &Tree) -> TxOpResult<TxValueIter<'_>> {
@ -350,6 +355,7 @@ pub(crate) trait ITx {
fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> TxOpResult<Option<Value>>; fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> TxOpResult<Option<Value>>;
fn remove(&mut self, tree: usize, key: &[u8]) -> TxOpResult<Option<Value>>; fn remove(&mut self, tree: usize, key: &[u8]) -> TxOpResult<Option<Value>>;
fn clear(&mut self, tree: usize) -> TxOpResult<()>;
fn iter(&self, tree: usize) -> TxOpResult<TxValueIter<'_>>; fn iter(&self, tree: usize) -> TxOpResult<TxValueIter<'_>>;
fn iter_rev(&self, tree: usize) -> TxOpResult<TxValueIter<'_>>; fn iter_rev(&self, tree: usize) -> TxOpResult<TxValueIter<'_>>;

View file

@ -238,8 +238,9 @@ impl<'a> ITx for LmdbTx<'a> {
None => Ok(None), None => Ok(None),
} }
} }
fn len(&self, _tree: usize) -> TxOpResult<usize> { fn len(&self, tree: usize) -> TxOpResult<usize> {
unimplemented!(".len() in transaction not supported with LMDB backend") let tree = self.get_tree(tree)?;
Ok(tree.len(&self.tx)? as usize)
} }
fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> TxOpResult<Option<Value>> { fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> TxOpResult<Option<Value>> {
@ -254,6 +255,11 @@ impl<'a> ITx for LmdbTx<'a> {
tree.delete(&mut self.tx, key)?; tree.delete(&mut self.tx, key)?;
Ok(old_val) Ok(old_val)
} }
fn clear(&mut self, tree: usize) -> TxOpResult<()> {
let tree = *self.get_tree(tree)?;
tree.clear(&mut self.tx)?;
Ok(())
}
fn iter(&self, _tree: usize) -> TxOpResult<TxValueIter<'_>> { fn iter(&self, _tree: usize) -> TxOpResult<TxValueIter<'_>> {
unimplemented!("Iterators in transactions not supported with LMDB backend"); unimplemented!("Iterators in transactions not supported with LMDB backend");

View file

@ -363,6 +363,11 @@ impl<'a> ITx for SqliteTx<'a> {
Ok(old_val) Ok(old_val)
} }
fn clear(&mut self, tree: usize) -> TxOpResult<()> {
let tree = self.get_tree(tree)?;
self.tx.execute(&format!("DELETE FROM {}", tree), [])?;
Ok(())
}
fn iter(&self, _tree: usize) -> TxOpResult<TxValueIter<'_>> { fn iter(&self, _tree: usize) -> TxOpResult<TxValueIter<'_>> {
unimplemented!(); unimplemented!();