Skip to content

Commit

Permalink
Rename RedbKey to Key
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Feb 3, 2024
1 parent c27584a commit 1cb0be6
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 155 deletions.
8 changes: 4 additions & 4 deletions examples/special_values.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use redb::{
Database, Error, ReadableTable, RedbKey, Table, TableDefinition, TableHandle, Value,
Database, Error, Key, ReadableTable, Table, TableDefinition, TableHandle, Value,
WriteTransaction,
};
use std::fs::{File, OpenOptions};
Expand Down Expand Up @@ -40,7 +40,7 @@ struct SpecialValuesTransaction<'db> {
}

impl<'db> SpecialValuesTransaction<'db> {
fn open_table<K: RedbKey + 'static, V: Value + 'static>(
fn open_table<K: Key + 'static, V: Value + 'static>(
&mut self,
table: TableDefinition<K, V>,
) -> SpecialValuesTable<K, V> {
Expand All @@ -58,13 +58,13 @@ impl<'db> SpecialValuesTransaction<'db> {
}
}

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

impl<'txn, K: RedbKey + 'static, V: Value + 'static> SpecialValuesTable<'txn, K, V> {
impl<'txn, K: Key + 'static, V: Value + '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
30 changes: 15 additions & 15 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::tree_store::{
InternalTableDefinition, PageHint, PageNumber, RawBtree, SerializedSavepoint, TableTreeMut,
TableType, TransactionalMemory, PAGE_SIZE,
};
use crate::types::{RedbKey, Value};
use crate::types::{Key, Value};
use crate::{
CompactionError, DatabaseError, Durability, ReadOnlyTable, SavepointError, StorageError,
};
Expand Down Expand Up @@ -108,13 +108,13 @@ impl Sealed for UntypedMultimapTableHandle {}
///
/// Note that the lifetime of the `K` and `V` type parameters does not impact the lifetimes of the data
/// that is stored or retreived from the table
pub struct TableDefinition<'a, K: RedbKey + 'static, V: Value + 'static> {
pub struct TableDefinition<'a, K: Key + 'static, V: Value + 'static> {
name: &'a str,
_key_type: PhantomData<K>,
_value_type: PhantomData<V>,
}

impl<'a, K: RedbKey + 'static, V: Value + 'static> TableDefinition<'a, K, V> {
impl<'a, K: Key + 'static, V: Value + 'static> TableDefinition<'a, K, V> {
/// Construct a new table with given `name`
///
/// ## Invariant
Expand All @@ -130,23 +130,23 @@ impl<'a, K: RedbKey + 'static, V: Value + 'static> TableDefinition<'a, K, V> {
}
}

impl<'a, K: RedbKey + 'static, V: Value + 'static> TableHandle for TableDefinition<'a, K, V> {
impl<'a, K: Key + 'static, V: Value + 'static> TableHandle for TableDefinition<'a, K, V> {
fn name(&self) -> &str {
self.name
}
}

impl<K: RedbKey, V: Value> Sealed for TableDefinition<'_, K, V> {}
impl<K: Key, V: Value> Sealed for TableDefinition<'_, K, V> {}

impl<'a, K: RedbKey + 'static, V: Value + 'static> Clone for TableDefinition<'a, K, V> {
impl<'a, K: Key + 'static, V: Value + 'static> Clone for TableDefinition<'a, K, V> {
fn clone(&self) -> Self {
*self
}
}

impl<'a, K: RedbKey + 'static, V: Value + 'static> Copy for TableDefinition<'a, K, V> {}
impl<'a, K: Key + 'static, V: Value + 'static> Copy for TableDefinition<'a, K, V> {}

impl<'a, K: RedbKey + 'static, V: Value + 'static> Display for TableDefinition<'a, K, V> {
impl<'a, K: Key + 'static, V: Value + 'static> Display for TableDefinition<'a, K, V> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
Expand All @@ -166,13 +166,13 @@ impl<'a, K: RedbKey + 'static, V: Value + 'static> Display for TableDefinition<'
///
/// Note that the lifetime of the `K` and `V` type parameters does not impact the lifetimes of the data
/// that is stored or retreived from the table
pub struct MultimapTableDefinition<'a, K: RedbKey + 'static, V: RedbKey + 'static> {
pub struct MultimapTableDefinition<'a, K: Key + 'static, V: Key + 'static> {
name: &'a str,
_key_type: PhantomData<K>,
_value_type: PhantomData<V>,
}

impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> MultimapTableDefinition<'a, K, V> {
impl<'a, K: Key + 'static, V: Key + 'static> MultimapTableDefinition<'a, K, V> {
pub const fn new(name: &'a str) -> Self {
assert!(!name.is_empty());
Self {
Expand All @@ -183,25 +183,25 @@ impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> MultimapTableDefinition<'a,
}
}

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

impl<K: RedbKey, V: RedbKey> Sealed for MultimapTableDefinition<'_, K, V> {}
impl<K: Key, V: Key> Sealed for MultimapTableDefinition<'_, K, V> {}

impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> Clone for MultimapTableDefinition<'a, K, V> {
impl<'a, K: Key + 'static, V: Key + 'static> Clone for MultimapTableDefinition<'a, K, V> {
fn clone(&self) -> Self {
*self
}
}

impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> Copy for MultimapTableDefinition<'a, K, V> {}
impl<'a, K: Key + 'static, V: Key + 'static> Copy for MultimapTableDefinition<'a, K, V> {}

impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> Display for MultimapTableDefinition<'a, K, V> {
impl<'a, K: Key + 'static, V: Key + 'static> Display for MultimapTableDefinition<'a, K, V> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub use table::{
};
pub use transactions::{DatabaseStats, Durability, ReadTransaction, WriteTransaction};
pub use tree_store::{AccessGuard, AccessGuardMut, Savepoint};
pub use types::{MutInPlaceValue, RedbKey, TypeName, Value};
pub use types::{Key, MutInPlaceValue, TypeName, Value};

type Result<T = (), E = StorageError> = std::result::Result<T, E>;

Expand Down
48 changes: 23 additions & 25 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::{
BtreeStats, CachePriority, Checksum, LeafAccessor, LeafMutator, Page, PageHint, PageNumber,
RawBtree, RawLeafBuilder, TransactionalMemory, UntypedBtreeMut, BRANCH, LEAF, MAX_VALUE_LENGTH,
};
use crate::types::{RedbKey, TypeName, Value};
use crate::types::{Key, TypeName, Value};
use crate::{AccessGuard, MultimapTableHandle, Result, StorageError, WriteTransaction};
use std::borrow::Borrow;
use std::cmp::max;
Expand Down Expand Up @@ -475,7 +475,7 @@ impl<V> DynamicCollection<V> {
}
}

impl<V: RedbKey> DynamicCollection<V> {
impl<V: Key> DynamicCollection<V> {
fn iter<'a>(
collection: AccessGuard<'a, &'static DynamicCollection<V>>,
guard: Arc<TransactionGuard>,
Expand Down Expand Up @@ -555,12 +555,12 @@ impl UntypedDynamicCollection {
}
}

enum ValueIterState<'a, V: RedbKey + 'static> {
enum ValueIterState<'a, V: Key + 'static> {
Subtree(BtreeRangeIter<V, ()>),
InlineLeaf(LeafKeyIter<'a, V>),
}

pub struct MultimapValue<'a, V: RedbKey + 'static> {
pub struct MultimapValue<'a, V: Key + 'static> {
inner: Option<ValueIterState<'a, V>>,
freed_pages: Option<Arc<Mutex<Vec<PageNumber>>>>,
free_on_drop: Vec<PageNumber>,
Expand All @@ -569,7 +569,7 @@ pub struct MultimapValue<'a, V: RedbKey + 'static> {
_value_type: PhantomData<V>,
}

impl<'a, V: RedbKey + 'static> MultimapValue<'a, V> {
impl<'a, V: Key + 'static> MultimapValue<'a, V> {
fn new_subtree(inner: BtreeRangeIter<V, ()>, guard: Arc<TransactionGuard>) -> Self {
Self {
inner: Some(ValueIterState::Subtree(inner)),
Expand Down Expand Up @@ -610,7 +610,7 @@ impl<'a, V: RedbKey + 'static> MultimapValue<'a, V> {
}
}

impl<'a, V: RedbKey + 'static> Iterator for MultimapValue<'a, V> {
impl<'a, V: Key + 'static> Iterator for MultimapValue<'a, V> {
type Item = Result<AccessGuard<'a, V>>;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -628,7 +628,7 @@ impl<'a, V: RedbKey + 'static> Iterator for MultimapValue<'a, V> {
}
}

impl<'a, V: RedbKey + 'static> DoubleEndedIterator for MultimapValue<'a, V> {
impl<'a, V: Key + 'static> DoubleEndedIterator for MultimapValue<'a, V> {
fn next_back(&mut self) -> Option<Self::Item> {
// TODO: optimize out this copy
let bytes = match self.inner.as_mut().unwrap() {
Expand All @@ -644,7 +644,7 @@ impl<'a, V: RedbKey + 'static> DoubleEndedIterator for MultimapValue<'a, V> {
}
}

impl<'a, V: RedbKey + 'static> Drop for MultimapValue<'a, V> {
impl<'a, V: Key + 'static> Drop for MultimapValue<'a, V> {
fn drop(&mut self) {
// Drop our references to the pages that are about to be freed
drop(mem::take(&mut self.inner));
Expand All @@ -659,7 +659,7 @@ impl<'a, V: RedbKey + 'static> Drop for MultimapValue<'a, V> {
}
}

pub struct MultimapRange<'a, K: RedbKey + 'static, V: RedbKey + 'static> {
pub struct MultimapRange<'a, K: Key + 'static, V: Key + 'static> {
inner: BtreeRangeIter<K, &'static DynamicCollection<V>>,
mem: Arc<TransactionalMemory>,
transaction_guard: Arc<TransactionGuard>,
Expand All @@ -668,7 +668,7 @@ pub struct MultimapRange<'a, K: RedbKey + 'static, V: RedbKey + 'static> {
_lifetime: PhantomData<&'a ()>,
}

impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> MultimapRange<'a, K, V> {
impl<'a, K: Key + 'static, V: Key + 'static> MultimapRange<'a, K, V> {
fn new(
inner: BtreeRangeIter<K, &'static DynamicCollection<V>>,
guard: Arc<TransactionGuard>,
Expand All @@ -685,7 +685,7 @@ impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> MultimapRange<'a, K, V> {
}
}

impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> Iterator for MultimapRange<'a, K, V> {
impl<'a, K: Key + 'static, V: Key + 'static> Iterator for MultimapRange<'a, K, V> {
type Item = Result<(AccessGuard<'a, K>, MultimapValue<'a, V>)>;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -708,9 +708,7 @@ impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> Iterator for MultimapRange<
}
}

impl<'a, K: RedbKey + 'static, V: RedbKey + 'static> DoubleEndedIterator
for MultimapRange<'a, K, V>
{
impl<'a, K: Key + 'static, V: Key + 'static> DoubleEndedIterator for MultimapRange<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
match self.inner.next_back()? {
Ok(entry) => {
Expand All @@ -734,7 +732,7 @@ 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<'txn, K: RedbKey + 'static, V: RedbKey + 'static> {
pub struct MultimapTable<'txn, K: Key + 'static, V: Key + 'static> {
name: String,
transaction: &'txn WriteTransaction,
freed_pages: Arc<Mutex<Vec<PageNumber>>>,
Expand All @@ -743,13 +741,13 @@ pub struct MultimapTable<'txn, K: RedbKey + 'static, V: RedbKey + 'static> {
_value_type: PhantomData<V>,
}

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

impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> MultimapTable<'txn, K, V> {
impl<'txn, K: Key + 'static, V: Key + 'static> MultimapTable<'txn, K, V> {
pub(crate) fn new(
name: &str,
table_root: Option<(PageNumber, Checksum)>,
Expand Down Expand Up @@ -1101,7 +1099,7 @@ impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> MultimapTable<'txn, K, V>
}
}

impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K, V>
impl<'txn, K: Key + 'static, V: Key + 'static> ReadableMultimapTable<K, V>
for MultimapTable<'txn, K, V>
{
/// Returns an iterator over all values for the given key. Values are in ascending order.
Expand Down Expand Up @@ -1173,15 +1171,15 @@ impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K,
}
}

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

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

pub trait ReadableMultimapTable<K: RedbKey + 'static, V: RedbKey + 'static>: Sealed {
pub trait ReadableMultimapTable<K: Key + 'static, V: Key + 'static>: Sealed {
/// 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>>
where
Expand Down Expand Up @@ -1255,14 +1253,14 @@ impl ReadOnlyUntypedMultimapTable {
}

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

impl<K: RedbKey + 'static, V: RedbKey + 'static> ReadOnlyMultimapTable<K, V> {
impl<K: Key + 'static, V: Key + 'static> ReadOnlyMultimapTable<K, V> {
pub(crate) fn new(
root_page: Option<(PageNumber, Checksum)>,
hint: PageHint,
Expand Down Expand Up @@ -1307,7 +1305,7 @@ impl<K: RedbKey + 'static, V: RedbKey + 'static> ReadOnlyMultimapTable<K, V> {
}
}

impl<K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K, V>
impl<K: Key + 'static, V: Key + 'static> ReadableMultimapTable<K, V>
for ReadOnlyMultimapTable<K, V>
{
/// Returns an iterator over all values for the given key. Values are in ascending order.
Expand Down Expand Up @@ -1375,4 +1373,4 @@ impl<K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K, V>
}
}

impl<K: RedbKey, V: RedbKey> Sealed for ReadOnlyMultimapTable<K, V> {}
impl<K: Key, V: Key> Sealed for ReadOnlyMultimapTable<K, V> {}
Loading

0 comments on commit 1cb0be6

Please sign in to comment.