diff options
author | Alex Auvolat <alex@adnab.me> | 2022-06-02 18:11:32 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-06-02 18:11:32 +0200 |
commit | f29b91232fbacc0c552fbbec52f5b2cf20cdcf8d (patch) | |
tree | d022409e23a6eaefb1960396d1b5bd231fc6c0a7 /src/db/sled_adapter.rs | |
parent | fbd5b64ff3c0da95a2ae96b1f830d2ed7dc4c5a8 (diff) | |
download | garage-f29b91232fbacc0c552fbbec52f5b2cf20cdcf8d.tar.gz garage-f29b91232fbacc0c552fbbec52f5b2cf20cdcf8d.zip |
Use Cell instead of ArcSwap
Diffstat (limited to 'src/db/sled_adapter.rs')
-rw-r--r-- | src/db/sled_adapter.rs | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/src/db/sled_adapter.rs b/src/db/sled_adapter.rs index 9ee9ea58..3942317c 100644 --- a/src/db/sled_adapter.rs +++ b/src/db/sled_adapter.rs @@ -1,16 +1,17 @@ use core::ops::Bound; +use std::cell::Cell; use std::collections::HashMap; use std::sync::{Arc, RwLock}; -use arc_swap::ArcSwapOption; - use sled::transaction::{ ConflictableTransactionError, TransactionError, Transactional, TransactionalTree, UnabortableTransactionError, }; -use crate::{Db, Error, IDb, ITx, ITxFn, Result, TxError, TxFnResult, TxResult, Value, ValueIter, Exporter}; +use crate::{ + Db, Error, Exporter, IDb, ITx, ITxFn, Result, TxError, TxFnResult, TxResult, Value, ValueIter, +}; pub use sled; @@ -47,20 +48,20 @@ impl SledDb { pub fn export<'a>(&'a self) -> Result<Exporter<'a>> { let mut trees = vec![]; for name in self.db.tree_names() { - let name = std::str::from_utf8(&name).map_err(|e| Error(format!("{}", e).into()))?.to_string(); + let name = std::str::from_utf8(&name) + .map_err(|e| Error(format!("{}", e).into()))? + .to_string(); let tree = self.open_tree(&name)?; let tree = self.trees.read().unwrap().0.get(tree).unwrap().clone(); trees.push((name, tree)); } - let trees_exporter: Exporter<'a> = Box::new(trees - .into_iter() - .map(|(name, tree)| { - let iter: ValueIter<'a> = Box::new(tree.iter().map(|v| { - v.map(|(x, y)| (x.to_vec().into(), y.to_vec().into())) - .map_err(Into::into) - })); - Ok((name.to_string(), iter)) + let trees_exporter: Exporter<'a> = Box::new(trees.into_iter().map(|(name, tree)| { + let iter: ValueIter<'a> = Box::new(tree.iter().map(|v| { + v.map(|(x, y)| (x.to_vec().into(), y.to_vec().into())) + .map_err(Into::into) })); + Ok((name.to_string(), iter)) + })); Ok(trees_exporter) } @@ -172,7 +173,7 @@ impl IDb for SledDb { let res = trees.0.transaction(|txtrees| { let tx = SledTx { trees: txtrees, - err: ArcSwapOption::new(None), + err: Cell::new(None), }; match f.try_on(&tx) { TxFnResult::Ok => { @@ -184,11 +185,10 @@ impl IDb for SledDb { Err(ConflictableTransactionError::Abort(())) } TxFnResult::Err => { - let err_arc = tx + let err = tx .err .into_inner() .expect("Transaction did not store error"); - let err = Arc::try_unwrap(err_arc).ok().expect("Many refs"); Err(err.into()) } } @@ -205,14 +205,14 @@ impl IDb for SledDb { struct SledTx<'a> { trees: &'a [TransactionalTree], - err: ArcSwapOption<UnabortableTransactionError>, + err: Cell<Option<UnabortableTransactionError>>, } impl<'a> SledTx<'a> { fn get_tree(&self, i: usize) -> Result<&TransactionalTree> { - self.trees - .get(i) - .ok_or(Error("invalid tree id (it might have been openned after the transaction started)".into())) + self.trees.get(i).ok_or(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> { @@ -220,7 +220,7 @@ impl<'a> SledTx<'a> { Ok(x) => Ok(x), Err(e) => { let txt = format!("{}", e); - self.err.store(Some(Arc::new(e))); + self.err.set(Some(e)); Err(Error(txt.into())) } } |