Skip to content

Commit

Permalink
Fix panics opening system tables from multiple threads
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Sep 17, 2023
1 parent 5be2bfd commit b15c5bb
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 182 deletions.
1 change: 0 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,6 @@ impl Builder {
}
}

// This just makes it easier to throw `dbg` etc statements on `Result<Database>`
impl std::fmt::Debug for Database {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Database").finish()
Expand Down
6 changes: 1 addition & 5 deletions src/multimap_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,6 @@ impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> DoubleEndedIterator
/// [Multimap tables](https://en.wikipedia.org/wiki/Multimap) may have multiple values associated with each key
pub struct MultimapTable<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> {
name: String,
system: bool,
transaction: &'txn WriteTransaction<'db>,
freed_pages: Arc<Mutex<Vec<PageNumber>>>,
tree: BtreeMut<'txn, K, &'static DynamicCollection<V>>,
Expand All @@ -724,15 +723,13 @@ pub struct MultimapTable<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static>
impl<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> MultimapTable<'db, 'txn, K, V> {
pub(crate) fn new(
name: &str,
system: bool,
table_root: Option<(PageNumber, Checksum)>,
freed_pages: Arc<Mutex<Vec<PageNumber>>>,
mem: &'db TransactionalMemory,
transaction: &'txn WriteTransaction<'db>,
) -> MultimapTable<'db, 'txn, K, V> {
MultimapTable {
name: name.to_string(),
system,
transaction,
freed_pages: freed_pages.clone(),
tree: BtreeMut::new(table_root, mem, freed_pages),
Expand Down Expand Up @@ -1129,8 +1126,7 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> Drop
for MultimapTable<'db, 'txn, K, V>
{
fn drop(&mut self) {
self.transaction
.close_table(&self.name, self.system, &self.tree);
self.transaction.close_table(&self.name, &self.tree);
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,20 @@ impl TableStats {
/// A table containing key-value mappings
pub struct Table<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> {
name: String,
system: bool,
transaction: &'txn WriteTransaction<'db>,
tree: BtreeMut<'txn, K, V>,
}

impl<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> Table<'db, 'txn, K, V> {
pub(crate) fn new(
name: &str,
system: bool,
table_root: Option<(PageNumber, Checksum)>,
freed_pages: Arc<Mutex<Vec<PageNumber>>>,
mem: &'db TransactionalMemory,
transaction: &'txn WriteTransaction<'db>,
) -> Table<'db, 'txn, K, V> {
Table {
name: name.to_string(),
system,
transaction,
tree: BtreeMut::new(table_root, mem, freed_pages),
}
Expand Down Expand Up @@ -248,8 +245,7 @@ impl<K: RedbKey, V: RedbValue> Sealed for Table<'_, '_, K, V> {}

impl<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> Drop for Table<'db, 'txn, K, V> {
fn drop(&mut self) {
self.transaction
.close_table(&self.name, self.system, &self.tree);
self.transaction.close_table(&self.name, &self.tree);
}
}

Expand Down Expand Up @@ -470,7 +466,7 @@ pub struct Range<'a, K: RedbKey + 'static, V: RedbValue + 'static> {
}

impl<'a, K: RedbKey + 'static, V: RedbValue + 'static> Range<'a, K, V> {
fn new(inner: BtreeRangeIter<'a, K, V>) -> Self {
pub(super) fn new(inner: BtreeRangeIter<'a, K, V>) -> Self {
Self { inner }
}
}
Expand Down
Loading

0 comments on commit b15c5bb

Please sign in to comment.