Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary Arc<Mutex<>> #757

Merged
merged 2 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ redb1 = { version = "=1.0.0", package = "redb" }
ctrlc = "3.2.3"
fastrand = "2.0.0"
lmdb-rkv = "0.14.0"
sanakirja = "1.3.3"
sanakirja = "=1.3.3"
sanakirja-core = "=1.3.3"
sled = "0.34.7"
rocksdb = "0.21.0"
libc = "0.2.99"
Expand Down
28 changes: 11 additions & 17 deletions src/tree_store/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl UntypedBtreeMut {
pub(crate) struct BtreeMut<'a, K: Key + 'static, V: Value + 'static> {
mem: Arc<TransactionalMemory>,
transaction_guard: Arc<TransactionGuard>,
root: Arc<Mutex<Option<(PageNumber, Checksum)>>>,
root: Option<(PageNumber, Checksum)>,
freed_pages: Arc<Mutex<Vec<PageNumber>>>,
_key_type: PhantomData<K>,
_value_type: PhantomData<V>,
Expand All @@ -237,7 +237,7 @@ impl<K: Key + 'static, V: Value + 'static> BtreeMut<'_, K, V> {
Self {
mem,
transaction_guard: guard,
root: Arc::new(Mutex::new(root)),
root,
freed_pages,
_key_type: Default::default(),
_value_type: Default::default(),
Expand All @@ -264,12 +264,12 @@ impl<K: Key + 'static, V: Value + 'static> BtreeMut<'_, K, V> {
V::fixed_width(),
);
tree.finalize_dirty_checksums()?;
*self.root.lock().unwrap() = tree.get_root();
self.root = tree.get_root();
Ok(())
}

pub(crate) fn all_pages_iter(&self) -> Result<Option<AllPageNumbersBtreeIter>> {
if let Some((root, _)) = *self.root.lock().unwrap() {
if let Some((root, _)) = self.root {
Ok(Some(AllPageNumbersBtreeIter::new(
root,
K::fixed_width(),
Expand All @@ -282,7 +282,7 @@ impl<K: Key + 'static, V: Value + 'static> BtreeMut<'_, K, V> {
}

pub(crate) fn get_root(&self) -> Option<(PageNumber, Checksum)> {
*(*self.root).lock().unwrap()
self.root
}

pub(crate) fn relocate(&mut self) -> Result<bool> {
Expand All @@ -294,7 +294,7 @@ impl<K: Key + 'static, V: Value + 'static> BtreeMut<'_, K, V> {
V::fixed_width(),
);
if tree.relocate()? {
*self.root.lock().unwrap() = tree.get_root();
self.root = tree.get_root();
Ok(true)
} else {
Ok(false)
Expand All @@ -314,20 +314,18 @@ impl<K: Key + 'static, V: Value + 'static> BtreeMut<'_, K, V> {
V::as_bytes(value).as_ref().len()
);
let mut freed_pages = self.freed_pages.lock().unwrap();
let mut root = self.root.lock().unwrap();
let mut operation: MutateHelper<'_, '_, K, V> =
MutateHelper::new(&mut root, self.mem.clone(), freed_pages.as_mut());
MutateHelper::new(&mut self.root, self.mem.clone(), freed_pages.as_mut());
let (old_value, _) = operation.insert(key, value)?;
Ok(old_value)
}

pub(crate) fn remove(&mut self, key: &K::SelfType<'_>) -> Result<Option<AccessGuard<V>>> {
#[cfg(feature = "logging")]
trace!("Btree(root={:?}): Deleting {:?}", &self.root, key);
let mut root = self.root.lock().unwrap();
let mut freed_pages = self.freed_pages.lock().unwrap();
let mut operation: MutateHelper<'_, '_, K, V> =
MutateHelper::new(&mut root, self.mem.clone(), freed_pages.as_mut());
MutateHelper::new(&mut self.root, self.mem.clone(), freed_pages.as_mut());
let result = operation.delete(key)?;
Ok(result)
}
Expand Down Expand Up @@ -379,9 +377,8 @@ impl<K: Key + 'static, V: Value + 'static> BtreeMut<'_, K, V> {
let iter = self.range(range)?;
let return_iter = self.range(range)?;
let mut free_on_drop = vec![];
let mut root = self.root.lock().unwrap();
let mut operation: MutateHelper<'_, '_, K, V> =
MutateHelper::new_do_not_modify(&mut root, self.mem.clone(), &mut free_on_drop);
MutateHelper::new_do_not_modify(&mut self.root, self.mem.clone(), &mut free_on_drop);
for entry in iter {
// TODO: optimize so that we don't have to call delete in a loop
assert!(operation.delete(&entry?.key())?.is_some());
Expand Down Expand Up @@ -413,9 +410,8 @@ impl<K: Key + 'static, V: Value + 'static> BtreeMut<'_, K, V> {
let iter = self.range(range)?;
let return_iter = self.range(range)?;
let mut free_on_drop = vec![];
let mut root = self.root.lock().unwrap();
let mut operation: MutateHelper<'_, '_, K, V> =
MutateHelper::new_do_not_modify(&mut root, self.mem.clone(), &mut free_on_drop);
MutateHelper::new_do_not_modify(&mut self.root, self.mem.clone(), &mut free_on_drop);
for entry in iter {
// TODO: optimize so that we don't have to call delete in a loop
let entry = entry?;
Expand Down Expand Up @@ -456,14 +452,12 @@ impl<'a, K: Key + 'a, V: MutInPlaceValue + 'a> BtreeMut<'a, K, V> {
key,
value_length
);
let mut root = self.root.lock().unwrap();
let mut freed_pages = self.freed_pages.lock().unwrap();
let mut value = vec![0u8; value_length as usize];
V::initialize(&mut value);
let mut operation =
MutateHelper::<K, V>::new(&mut root, self.mem.clone(), freed_pages.as_mut());
MutateHelper::<K, V>::new(&mut self.root, self.mem.clone(), freed_pages.as_mut());
let (_, guard) = operation.insert(key, &V::from_bytes(&value))?;
drop(root);
Ok(guard)
}
}
Expand Down
Loading