Skip to content

Commit

Permalink
Implement TableHandle for Table
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Nov 7, 2023
1 parent f1dea10 commit 25d0b89
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/multimap_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::tree_store::{
RawBtree, RawLeafBuilder, TransactionalMemory, UntypedBtreeMut, BRANCH, LEAF, MAX_VALUE_LENGTH,
};
use crate::types::{RedbKey, RedbValue, TypeName};
use crate::{AccessGuard, Result, StorageError, WriteTransaction};
use crate::{AccessGuard, MultimapTableHandle, Result, StorageError, WriteTransaction};
use std::borrow::Borrow;
use std::cmp::max;
use std::convert::TryInto;
Expand Down Expand Up @@ -720,6 +720,14 @@ pub struct MultimapTable<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static>
_value_type: PhantomData<V>,
}

impl<K: RedbKey + 'static, V: RedbKey + 'static> MultimapTableHandle
for MultimapTable<'_, '_, K, V>
{
fn name(&self) -> &str {
&self.name
}
}

impl<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> MultimapTable<'db, 'txn, K, V> {
pub(crate) fn new(
name: &str,
Expand Down Expand Up @@ -1120,7 +1128,7 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTabl
}
}

impl<K: RedbKey, V: RedbKey> Sealed for MultimapTable<'_, '_, K, V> {}
impl<K: RedbKey + 'static, V: RedbKey + 'static> Sealed for MultimapTable<'_, '_, K, V> {}

impl<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> Drop
for MultimapTable<'db, 'txn, K, V>
Expand Down
8 changes: 7 additions & 1 deletion src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::tree_store::{
PageHint, PageNumber, TransactionalMemory, MAX_VALUE_LENGTH,
};
use crate::types::{RedbKey, RedbValue, RedbValueMutInPlace};
use crate::Result;
use crate::{AccessGuard, StorageError, WriteTransaction};
use crate::{Result, TableHandle};
use std::borrow::Borrow;
use std::fmt::{Debug, Formatter};
use std::ops::RangeBounds;
Expand Down Expand Up @@ -62,6 +62,12 @@ pub struct Table<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> {
tree: BtreeMut<'txn, K, V>,
}

impl<K: RedbKey + 'static, V: RedbValue + 'static> TableHandle for Table<'_, '_, K, V> {
fn name(&self) -> &str {
&self.name
}
}

impl<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> Table<'db, 'txn, K, V> {
pub(crate) fn new(
name: &str,
Expand Down
13 changes: 8 additions & 5 deletions src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,10 @@ impl<'db> WriteTransaction<'db> {
///
/// Returns a bool indicating whether the table existed
pub fn delete_table(&self, definition: impl TableHandle) -> Result<bool, TableError> {
self.tables
.lock()
.unwrap()
.delete_table(self, definition.name())
let name = definition.name().to_string();
// Drop the definition so that callers can pass in a `Table` or `MultimapTable` to delete, without getting a TableAlreadyOpen error
drop(definition);
self.tables.lock().unwrap().delete_table(self, &name)
}

/// Delete the given table
Expand All @@ -787,10 +787,13 @@ impl<'db> WriteTransaction<'db> {
&self,
definition: impl MultimapTableHandle,
) -> Result<bool, TableError> {
let name = definition.name().to_string();
// Drop the definition so that callers can pass in a `Table` or `MultimapTable` to delete, without getting a TableAlreadyOpen error
drop(definition);
self.tables
.lock()
.unwrap()
.delete_multimap_table(self, definition.name())
.delete_multimap_table(self, &name)
}

/// List all the tables
Expand Down
12 changes: 12 additions & 0 deletions tests/basic_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,18 @@ fn delete_open_table() {
write_txn.commit().unwrap();
}

#[test]
fn delete_table() {
let tmpfile = create_tempfile();
let db = Database::create(tmpfile.path()).unwrap();
let write_txn = db.begin_write().unwrap();
{
let table = write_txn.open_table(STR_TABLE).unwrap();
assert!(write_txn.delete_table(table).unwrap());
}
write_txn.commit().unwrap();
}

#[test]
fn no_dirty_reads() {
let tmpfile = create_tempfile();
Expand Down

0 comments on commit 25d0b89

Please sign in to comment.