Skip to content

Commit

Permalink
Remove lifetimes from read-only tables
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Feb 3, 2024
1 parent 46e31c2 commit 74dee53
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 34 deletions.
8 changes: 4 additions & 4 deletions benches/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,19 @@ pub struct RedbBenchReadTransaction {
}

impl BenchReadTransaction for RedbBenchReadTransaction {
type T<'txn> = RedbBenchReader<'txn> where Self: 'txn;
type T<'txn> = RedbBenchReader where Self: 'txn;

fn get_reader(&self) -> Self::T<'_> {
let table = self.txn.open_table(X).unwrap();
RedbBenchReader { table }
}
}

pub struct RedbBenchReader<'txn> {
table: redb::ReadOnlyTable<'txn, &'static [u8], &'static [u8]>,
pub struct RedbBenchReader {
table: redb::ReadOnlyTable<&'static [u8], &'static [u8]>,
}

impl<'txn> BenchReader for RedbBenchReader<'txn> {
impl BenchReader for RedbBenchReader {
type Output<'out> = RedbAccessGuard<'out> where Self: 'out;
type Iterator<'out> = RedbBenchIterator<'out> where Self: 'out;

Expand Down
20 changes: 8 additions & 12 deletions src/multimap_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,15 +1219,14 @@ pub trait ReadableMultimapTable<K: RedbKey + 'static, V: RedbKey + 'static>: Sea
}

/// A read-only untyped multimap table
pub struct ReadOnlyUntypedMultimapTable<'txn> {
pub struct ReadOnlyUntypedMultimapTable {
tree: RawBtree,
fixed_key_size: Option<usize>,
fixed_value_size: Option<usize>,
mem: Arc<TransactionalMemory>,
_lifetime: PhantomData<&'txn ()>,
}

impl<'txn> ReadOnlyUntypedMultimapTable<'txn> {
impl ReadOnlyUntypedMultimapTable {
pub(crate) fn new(
root_page: Option<(PageNumber, Checksum)>,
fixed_key_size: Option<usize>,
Expand All @@ -1244,7 +1243,6 @@ impl<'txn> ReadOnlyUntypedMultimapTable<'txn> {
fixed_key_size,
fixed_value_size,
mem,
_lifetime: Default::default(),
}
}

Expand All @@ -1269,27 +1267,25 @@ impl<'txn> ReadOnlyUntypedMultimapTable<'txn> {
}

/// A read-only multimap table
pub struct ReadOnlyMultimapTable<'txn, K: RedbKey + 'static, V: RedbKey + 'static> {
pub struct ReadOnlyMultimapTable<K: RedbKey + 'static, V: RedbKey + 'static> {
tree: Btree<K, &'static DynamicCollection<V>>,
mem: Arc<TransactionalMemory>,
transaction_guard: Arc<TransactionGuard>,
_value_type: PhantomData<V>,
_lifetime: PhantomData<&'txn ()>,
}

impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadOnlyMultimapTable<'txn, K, V> {
impl<K: RedbKey + 'static, V: RedbKey + 'static> ReadOnlyMultimapTable<K, V> {
pub(crate) fn new(
root_page: Option<(PageNumber, Checksum)>,
hint: PageHint,
guard: Arc<TransactionGuard>,
mem: Arc<TransactionalMemory>,
) -> Result<ReadOnlyMultimapTable<'txn, K, V>> {
) -> Result<ReadOnlyMultimapTable<K, V>> {
Ok(ReadOnlyMultimapTable {
tree: Btree::new(root_page, hint, guard.clone(), mem.clone())?,
mem,
transaction_guard: guard,
_value_type: Default::default(),
_lifetime: Default::default(),
})
}

Expand Down Expand Up @@ -1323,8 +1319,8 @@ impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadOnlyMultimapTable<'tx
}
}

impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K, V>
for ReadOnlyMultimapTable<'txn, K, V>
impl<K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K, V>
for ReadOnlyMultimapTable<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 @@ -1391,4 +1387,4 @@ impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K,
}
}

impl<K: RedbKey, V: RedbKey> Sealed for ReadOnlyMultimapTable<'_, K, V> {}
impl<K: RedbKey, V: RedbKey> Sealed for ReadOnlyMultimapTable<K, V> {}
22 changes: 8 additions & 14 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,11 @@ pub trait ReadableTable<K: RedbKey + 'static, V: RedbValue + 'static>: Sealed {
}

/// A read-only untyped table
pub struct ReadOnlyUntypedTable<'txn> {
pub struct ReadOnlyUntypedTable {
tree: RawBtree,
_lifetime: PhantomData<&'txn ()>,
}

impl<'txn> ReadOnlyUntypedTable<'txn> {
impl ReadOnlyUntypedTable {
pub(crate) fn new(
root_page: Option<(PageNumber, Checksum)>,
fixed_key_size: Option<usize>,
Expand All @@ -395,7 +394,6 @@ impl<'txn> ReadOnlyUntypedTable<'txn> {
) -> Self {
Self {
tree: RawBtree::new(root_page, fixed_key_size, fixed_value_size, mem),
_lifetime: Default::default(),
}
}

Expand All @@ -415,26 +413,24 @@ impl<'txn> ReadOnlyUntypedTable<'txn> {
}

/// A read-only table
pub struct ReadOnlyTable<'txn, K: RedbKey + 'static, V: RedbValue + 'static> {
pub struct ReadOnlyTable<K: RedbKey + 'static, V: RedbValue + 'static> {
name: String,
tree: Btree<K, V>,
transaction_guard: Arc<TransactionGuard>,
_lifetime: PhantomData<&'txn ()>,
}

impl<'txn, K: RedbKey + 'static, V: RedbValue + 'static> ReadOnlyTable<'txn, K, V> {
impl<K: RedbKey + 'static, V: RedbValue + 'static> ReadOnlyTable<K, V> {
pub(crate) fn new(
name: String,
root_page: Option<(PageNumber, Checksum)>,
hint: PageHint,
guard: Arc<TransactionGuard>,
mem: Arc<TransactionalMemory>,
) -> Result<ReadOnlyTable<'txn, K, V>> {
) -> Result<ReadOnlyTable<K, V>> {
Ok(ReadOnlyTable {
name,
tree: Btree::new(root_page, hint, guard.clone(), mem)?,
transaction_guard: guard,
_lifetime: Default::default(),
})
}

Expand All @@ -450,9 +446,7 @@ impl<'txn, K: RedbKey + 'static, V: RedbValue + 'static> ReadOnlyTable<'txn, K,
}
}

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

impl<K: RedbKey, V: RedbValue> Sealed for ReadOnlyTable<'_, K, V> {}
impl<K: RedbKey, V: RedbValue> Sealed for ReadOnlyTable<K, V> {}

impl<K: RedbKey + 'static, V: RedbValue + 'static> Debug for ReadOnlyTable<'_, K, V> {
impl<K: RedbKey + 'static, V: RedbValue + 'static> Debug for ReadOnlyTable<K, V> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
debug_helper(f, &self.name, self.len(), self.first(), self.last())
}
Expand Down
8 changes: 4 additions & 4 deletions src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ impl ReadTransaction {
pub fn open_table<K: RedbKey + 'static, V: RedbValue + 'static>(
&self,
definition: TableDefinition<K, V>,
) -> Result<ReadOnlyTable<'static, K, V>, TableError> {
) -> Result<ReadOnlyTable<K, V>, TableError> {
let header = self
.tree
.get_table::<K, V>(definition.name(), TableType::Normal)?
Expand All @@ -1224,7 +1224,7 @@ impl ReadTransaction {
pub fn open_untyped_table(
&self,
handle: impl TableHandle,
) -> Result<ReadOnlyUntypedTable<'static>, TableError> {
) -> Result<ReadOnlyUntypedTable, TableError> {
let header = self
.tree
.get_table_untyped(handle.name(), TableType::Normal)?
Expand All @@ -1242,7 +1242,7 @@ impl ReadTransaction {
pub fn open_multimap_table<K: RedbKey + 'static, V: RedbKey + 'static>(
&self,
definition: MultimapTableDefinition<K, V>,
) -> Result<ReadOnlyMultimapTable<'static, K, V>, TableError> {
) -> Result<ReadOnlyMultimapTable<K, V>, TableError> {
let header = self
.tree
.get_table::<K, V>(definition.name(), TableType::Multimap)?
Expand All @@ -1260,7 +1260,7 @@ impl ReadTransaction {
pub fn open_untyped_multimap_table(
&self,
handle: impl MultimapTableHandle,
) -> Result<ReadOnlyUntypedMultimapTable<'static>, TableError> {
) -> Result<ReadOnlyUntypedMultimapTable, TableError> {
let header = self
.tree
.get_table_untyped(handle.name(), TableType::Multimap)?
Expand Down

0 comments on commit 74dee53

Please sign in to comment.