aboutsummaryrefslogtreecommitdiff
path: root/src/db/sled_adapter.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-06-07 17:50:10 +0200
committerAlex Auvolat <alex@adnab.me>2022-06-07 17:50:10 +0200
commit0543cb345320a15280a5af7db941bb9fbffb4cd6 (patch)
treeff5b054bc30b3ca826d5e1eece05198ae3c16f57 /src/db/sled_adapter.rs
parent1bbe0794f363eb59c56548cca672013fd78f361a (diff)
downloadgarage-0543cb345320a15280a5af7db941bb9fbffb4cd6.tar.gz
garage-0543cb345320a15280a5af7db941bb9fbffb4cd6.zip
Cleaner error management (less error-prone api)
Diffstat (limited to 'src/db/sled_adapter.rs')
-rw-r--r--src/db/sled_adapter.rs40
1 files changed, 26 insertions, 14 deletions
diff --git a/src/db/sled_adapter.rs b/src/db/sled_adapter.rs
index b07401c9..d0d9e9c0 100644
--- a/src/db/sled_adapter.rs
+++ b/src/db/sled_adapter.rs
@@ -9,7 +9,10 @@ use sled::transaction::{
UnabortableTransactionError,
};
-use crate::{Db, Error, IDb, ITx, ITxFn, Result, TxError, TxFnResult, TxResult, Value, ValueIter};
+use crate::{
+ Db, Error, IDb, ITx, ITxFn, Result, TxError, TxFnResult, TxOpError, TxOpResult, TxResult,
+ Value, ValueIter,
+};
pub use sled;
@@ -21,6 +24,12 @@ impl From<sled::Error> for Error {
}
}
+impl From<sled::Error> for TxOpError {
+ fn from(e: sled::Error) -> TxOpError {
+ TxOpError(e.into())
+ }
+}
+
// -- db
pub struct SledDb {
@@ -177,51 +186,54 @@ struct SledTx<'a> {
}
impl<'a> SledTx<'a> {
- fn get_tree(&self, i: usize) -> Result<&TransactionalTree> {
+ fn get_tree(&self, i: usize) -> TxOpResult<&TransactionalTree> {
self.trees.get(i).ok_or_else(|| {
- Error(
+ TxOpError(Error(
"invalid tree id (it might have been openned after the transaction started)".into(),
- )
+ ))
})
}
- fn save_error<R>(&self, v: std::result::Result<R, UnabortableTransactionError>) -> Result<R> {
+ fn save_error<R>(
+ &self,
+ v: std::result::Result<R, UnabortableTransactionError>,
+ ) -> TxOpResult<R> {
match v {
Ok(x) => Ok(x),
Err(e) => {
let txt = format!("{}", e);
self.err.set(Some(e));
- Err(Error(txt.into()))
+ Err(TxOpError(Error(txt.into())))
}
}
}
}
impl<'a> ITx for SledTx<'a> {
- fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>> {
+ fn get(&self, tree: usize, key: &[u8]) -> TxOpResult<Option<Value>> {
let tree = self.get_tree(tree)?;
let tmp = self.save_error(tree.get(key))?;
Ok(tmp.map(|x| x.to_vec()))
}
- fn len(&self, _tree: usize) -> Result<usize> {
+ fn len(&self, _tree: usize) -> TxOpResult<usize> {
unimplemented!(".len() in transaction not supported with Sled backend")
}
- fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> Result<Option<Value>> {
+ fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> TxOpResult<Option<Value>> {
let tree = self.get_tree(tree)?;
let old_val = self.save_error(tree.insert(key, value))?;
Ok(old_val.map(|x| x.to_vec()))
}
- fn remove(&mut self, tree: usize, key: &[u8]) -> Result<Option<Value>> {
+ fn remove(&mut self, tree: usize, key: &[u8]) -> TxOpResult<Option<Value>> {
let tree = self.get_tree(tree)?;
let old_val = self.save_error(tree.remove(key))?;
Ok(old_val.map(|x| x.to_vec()))
}
- fn iter(&self, _tree: usize) -> Result<ValueIter<'_>> {
+ fn iter(&self, _tree: usize) -> TxOpResult<ValueIter<'_>> {
unimplemented!("Iterators in transactions not supported with Sled backend");
}
- fn iter_rev(&self, _tree: usize) -> Result<ValueIter<'_>> {
+ fn iter_rev(&self, _tree: usize) -> TxOpResult<ValueIter<'_>> {
unimplemented!("Iterators in transactions not supported with Sled backend");
}
@@ -230,7 +242,7 @@ impl<'a> ITx for SledTx<'a> {
_tree: usize,
_low: Bound<&'r [u8]>,
_high: Bound<&'r [u8]>,
- ) -> Result<ValueIter<'_>> {
+ ) -> TxOpResult<ValueIter<'_>> {
unimplemented!("Iterators in transactions not supported with Sled backend");
}
fn range_rev<'r>(
@@ -238,7 +250,7 @@ impl<'a> ITx for SledTx<'a> {
_tree: usize,
_low: Bound<&'r [u8]>,
_high: Bound<&'r [u8]>,
- ) -> Result<ValueIter<'_>> {
+ ) -> TxOpResult<ValueIter<'_>> {
unimplemented!("Iterators in transactions not supported with Sled backend");
}
}