Skip to content

Commit

Permalink
Remove lifetime from WriteTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Feb 3, 2024
1 parent 2a36266 commit 207a9e9
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 82 deletions.
16 changes: 8 additions & 8 deletions benches/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<'a> RedbBenchDatabase<'a> {
}

impl<'a> BenchDatabase for RedbBenchDatabase<'a> {
type W<'db> = RedbBenchWriteTransaction<'db> where Self: 'db;
type W<'db> = RedbBenchWriteTransaction where Self: 'db;
type R<'db> = RedbBenchReadTransaction<'db> where Self: 'db;

fn db_type_name() -> &'static str {
Expand Down Expand Up @@ -165,12 +165,12 @@ impl<'a> AsRef<[u8]> for RedbAccessGuard<'a> {
}
}

pub struct RedbBenchWriteTransaction<'db> {
txn: redb::WriteTransaction<'db>,
pub struct RedbBenchWriteTransaction {
txn: redb::WriteTransaction,
}

impl<'db> BenchWriteTransaction for RedbBenchWriteTransaction<'db> {
type W<'txn> = RedbBenchInserter<'db, 'txn> where Self: 'txn;
impl BenchWriteTransaction for RedbBenchWriteTransaction {
type W<'txn> = RedbBenchInserter<'txn> where Self: 'txn;

fn get_inserter(&mut self) -> Self::W<'_> {
let table = self.txn.open_table(X).unwrap();
Expand All @@ -182,11 +182,11 @@ impl<'db> BenchWriteTransaction for RedbBenchWriteTransaction<'db> {
}
}

pub struct RedbBenchInserter<'db, 'txn> {
table: redb::Table<'db, 'txn, &'static [u8], &'static [u8]>,
pub struct RedbBenchInserter<'txn> {
table: redb::Table<'txn, &'static [u8], &'static [u8]>,
}

impl BenchInserter for RedbBenchInserter<'_, '_> {
impl BenchInserter for RedbBenchInserter<'_> {
fn insert(&mut self, key: &[u8], value: &[u8]) -> Result<(), ()> {
self.table.insert(key, value).map(|_| ()).map_err(|_| ())
}
Expand Down
14 changes: 7 additions & 7 deletions examples/special_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ impl SpecialValuesDb {
}

struct SpecialValuesTransaction<'db> {
inner: WriteTransaction<'db>,
inner: WriteTransaction,
file: &'db mut File,
}

impl<'db> SpecialValuesTransaction<'db> {
fn open_table<'txn, K: RedbKey + 'static, V: RedbValue + 'static>(
&'txn mut self,
fn open_table<K: RedbKey + 'static, V: RedbValue + 'static>(
&mut self,
table: TableDefinition<K, V>,
) -> SpecialValuesTable<'db, 'txn, K, V> {
) -> SpecialValuesTable<K, V> {
let def: TableDefinition<K, (u64, u64)> = TableDefinition::new(table.name());
SpecialValuesTable {
inner: self.inner.open_table(def).unwrap(),
Expand All @@ -58,13 +58,13 @@ impl<'db> SpecialValuesTransaction<'db> {
}
}

struct SpecialValuesTable<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> {
inner: Table<'db, 'txn, K, (u64, u64)>,
struct SpecialValuesTable<'txn, K: RedbKey + 'static, V: RedbValue + 'static> {
inner: Table<'txn, K, (u64, u64)>,
file: &'txn mut File,
_value_type: PhantomData<V>,
}

impl<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> SpecialValuesTable<'db, 'txn, K, V> {
impl<'txn, K: RedbKey + 'static, V: RedbValue + 'static> SpecialValuesTable<'txn, K, V> {
fn insert(&mut self, key: K::SelfType<'_>, value: V::SelfType<'_>) {
// Append to end of file
let offset = self.file.seek(SeekFrom::End(0)).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/fuzz_redb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ fn is_simulated_io_error(err: &redb::Error) -> bool {
}
}

fn exec_table_crash_support<T: Clone>(config: &FuzzConfig, apply: fn(WriteTransaction<'_>, &mut BTreeMap<u64, T>, &FuzzTransaction, &mut SavepointManager<T>) -> Result<(), redb::Error>) -> Result<(), redb::Error> {
fn exec_table_crash_support<T: Clone>(config: &FuzzConfig, apply: fn(WriteTransaction, &mut BTreeMap<u64, T>, &FuzzTransaction, &mut SavepointManager<T>) -> Result<(), redb::Error>) -> Result<(), redb::Error> {
let mut redb_file: NamedTempFile = NamedTempFile::new().unwrap();
let backend = FuzzerBackend::new(FileBackend::new(redb_file.as_file().try_clone().unwrap())?);
let countdown = backend.countdown.clone();
Expand Down Expand Up @@ -642,7 +642,7 @@ fn handle_savepoints<T: Clone>(mut txn: WriteTransaction, reference: &mut BTreeM

}

fn apply_crashable_transaction_multimap(txn: WriteTransaction<'_>, uncommitted_reference: &mut BTreeMap<u64, BTreeSet<usize>>, transaction: &FuzzTransaction, savepoints: &mut SavepointManager<BTreeSet<usize>>) -> Result<(), redb::Error> {
fn apply_crashable_transaction_multimap(txn: WriteTransaction, uncommitted_reference: &mut BTreeMap<u64, BTreeSet<usize>>, transaction: &FuzzTransaction, savepoints: &mut SavepointManager<BTreeSet<usize>>) -> Result<(), redb::Error> {
{
let mut table = txn.open_multimap_table(MULTIMAP_TABLE_DEF)?;
for op in transaction.ops.iter() {
Expand All @@ -662,7 +662,7 @@ fn apply_crashable_transaction_multimap(txn: WriteTransaction<'_>, uncommitted_r
Ok(())
}

fn apply_crashable_transaction(txn: WriteTransaction<'_>, uncommitted_reference: &mut BTreeMap<u64, usize>, transaction: &FuzzTransaction, savepoints: &mut SavepointManager<usize>) -> Result<(), redb::Error> {
fn apply_crashable_transaction(txn: WriteTransaction, uncommitted_reference: &mut BTreeMap<u64, usize>, transaction: &FuzzTransaction, savepoints: &mut SavepointManager<usize>) -> Result<(), redb::Error> {
{
let mut table = txn.open_table(TABLE_DEF)?;
for op in transaction.ops.iter() {
Expand Down
10 changes: 3 additions & 7 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl TransactionGuard {
self.transaction_id.unwrap()
}

fn leak(mut self) -> TransactionId {
pub(crate) fn leak(mut self) -> TransactionId {
self.transaction_id.take().unwrap()
}
}
Expand Down Expand Up @@ -768,11 +768,6 @@ impl Database {
))
}

pub(crate) fn allocate_savepoint(&self) -> Result<(SavepointId, TransactionId)> {
let id = self.transaction_tracker.allocate_savepoint();
Ok((id, self.allocate_read_transaction()?.leak()))
}

/// Convenience method for [`Builder::new`]
pub fn builder() -> Builder {
Builder::new()
Expand All @@ -788,7 +783,8 @@ impl Database {
self.transaction_tracker.start_write_transaction(),
self.transaction_tracker.clone(),
);
WriteTransaction::new(self, guard, self.transaction_tracker.clone()).map_err(|e| e.into())
WriteTransaction::new(guard, self.transaction_tracker.clone(), self.mem.clone())
.map_err(|e| e.into())
}

/// Begins a read transaction
Expand Down
24 changes: 10 additions & 14 deletions src/multimap_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,31 +740,29 @@ impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> DoubleEndedIterator
/// A multimap table
///
/// [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> {
pub struct MultimapTable<'txn, K: RedbKey + 'static, V: RedbKey + 'static> {
name: String,
transaction: &'txn WriteTransaction<'db>,
transaction: &'txn WriteTransaction,
freed_pages: Arc<Mutex<Vec<PageNumber>>>,
tree: BtreeMut<'txn, K, &'static DynamicCollection<V>>,
mem: Arc<TransactionalMemory>,
_value_type: PhantomData<V>,
}

impl<K: RedbKey + 'static, V: RedbKey + 'static> MultimapTableHandle
for MultimapTable<'_, '_, K, 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> {
impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> MultimapTable<'txn, K, V> {
pub(crate) fn new(
name: &str,
table_root: Option<(PageNumber, Checksum)>,
freed_pages: Arc<Mutex<Vec<PageNumber>>>,
mem: Arc<TransactionalMemory>,
transaction: &'txn WriteTransaction<'db>,
) -> MultimapTable<'db, 'txn, K, V> {
transaction: &'txn WriteTransaction,
) -> MultimapTable<'txn, K, V> {
MultimapTable {
name: name.to_string(),
transaction,
Expand Down Expand Up @@ -1115,8 +1113,8 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> MultimapTable<'db, '
}
}

impl<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K, V>
for MultimapTable<'db, 'txn, K, V>
impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K, V>
for MultimapTable<'txn, K, V>
{
/// Returns an iterator over all values for the given key. Values are in ascending order.
fn get<'a>(&self, key: impl Borrow<K::SelfType<'a>>) -> Result<MultimapValue<V>>
Expand Down Expand Up @@ -1187,11 +1185,9 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTabl
}
}

impl<K: RedbKey + 'static, V: RedbKey + 'static> 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>
{
impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> Drop for MultimapTable<'txn, K, V> {
fn drop(&mut self) {
self.transaction.close_table(&self.name, &self.tree);
}
Expand Down
24 changes: 11 additions & 13 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,26 @@ impl TableStats {
}

/// A table containing key-value mappings
pub struct Table<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> {
pub struct Table<'txn, K: RedbKey + 'static, V: RedbValue + 'static> {
name: String,
transaction: &'txn WriteTransaction<'db>,
transaction: &'txn WriteTransaction,
tree: BtreeMut<'txn, K, V>,
}

impl<K: RedbKey + 'static, V: RedbValue + 'static> TableHandle for Table<'_, '_, 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> {
impl<'txn, K: RedbKey + 'static, V: RedbValue + 'static> Table<'txn, K, V> {
pub(crate) fn new(
name: &str,
table_root: Option<(PageNumber, Checksum)>,
freed_pages: Arc<Mutex<Vec<PageNumber>>>,
mem: Arc<TransactionalMemory>,
transaction: &'txn WriteTransaction<'db>,
) -> Table<'db, 'txn, K, V> {
transaction: &'txn WriteTransaction,
) -> Table<'txn, K, V> {
Table {
name: name.to_string(),
transaction,
Expand Down Expand Up @@ -193,7 +193,7 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> Table<'db, 'txn, K
}
}

impl<'db, 'txn, K: RedbKey + 'static, V: MutInPlaceValue + 'static> Table<'db, 'txn, K, V> {
impl<'txn, K: RedbKey + 'static, V: MutInPlaceValue + 'static> Table<'txn, K, V> {
/// Reserve space to insert a key-value pair
/// The returned reference will have length equal to value_length
pub fn insert_reserve<'a>(
Expand All @@ -215,9 +215,7 @@ impl<'db, 'txn, K: RedbKey + 'static, V: MutInPlaceValue + 'static> Table<'db, '
}
}

impl<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> ReadableTable<K, V>
for Table<'db, 'txn, K, V>
{
impl<'txn, K: RedbKey + 'static, V: RedbValue + 'static> ReadableTable<K, V> for Table<'txn, K, V> {
fn get<'a>(&self, key: impl Borrow<K::SelfType<'a>>) -> Result<Option<AccessGuard<V>>>
where
K: 'a,
Expand Down Expand Up @@ -257,9 +255,9 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> ReadableTable<K, V
}
}

impl<K: RedbKey, V: RedbValue> Sealed for Table<'_, '_, K, V> {}
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> {
impl<'txn, K: RedbKey + 'static, V: RedbValue + 'static> Drop for Table<'txn, K, V> {
fn drop(&mut self) {
self.transaction.close_table(&self.name, &self.tree);
}
Expand Down Expand Up @@ -308,7 +306,7 @@ fn debug_helper<K: RedbKey + 'static, V: RedbValue + 'static>(
Ok(())
}

impl<K: RedbKey + 'static, V: RedbValue + 'static> Debug for Table<'_, '_, K, V> {
impl<K: RedbKey + 'static, V: RedbValue + 'static> Debug for Table<'_, K, V> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
debug_helper(f, &self.name, self.len(), self.first(), self.last())
}
Expand Down
Loading

0 comments on commit 207a9e9

Please sign in to comment.