Skip to main content

TypedTree

Struct TypedTree 

Source
pub struct TypedTree<K: Key, T: Send + Sync, C: Codec<T>, H: TypedWriteHook<K, T> = NoHook> { /* private fields */ }
Expand description

A tree with fixed-size keys and typed values T. Values are encoded via a Codec for disk persistence but stored as T in memory — reads never touch disk and return TypedRef<T> (guard-protected reference).

Each TypedTree owns its storage engine — one tree = one database directory.

§Clone-free design

Unlike ConstTree which copies [u8; V] values (they are Copy), TypedTree returns TypedRef<T> — a guard-protected reference. The seize guard inside TypedRef prevents reclamation of the old data while the reference exists. This means put(), delete(), and update() can return the old value without requiring T: Clone.

The only method that requires T: Clone is compact() — compaction must copy the value into a new TypedData with an updated disk location.

§Write hooks

Uses TypedWriteHook<K, T> instead of WriteHook<K>. The hook receives &T directly (not encoded bytes). on_write fires on put/insert/delete/cas/update. Does not fire inside atomic(). Old value is always provided (it lives in memory) — NEEDS_OLD_VALUE is ignored.

on_init fires once per live entry during migrate() or replay_init() (enable via NEEDS_INIT = true).

§Usage

let tree = TypedTree::<16, MyValue, RapiraCodec>::open(
    "data/users",
    Config::default(),
    RapiraCodec,
)?;
tree.put(&key, value)?;
if let Some(r) = tree.get(&key) {
    println!("{:?}", &*r);  // TypedRef<MyValue> derefs to &MyValue
}
tree.close()?;

§Iteration

iter(), range(), and prefix_iter() all return TypedIter which implements Iterator + DoubleEndedIterator with Item = (K, &T). Lock-free, zero disk I/O.

for (key, value) in tree.iter() { }
let latest = tree.prefix_iter(&user_id).take(20).collect::<Vec<_>>();
let oldest = tree.iter().rev().take(5);  // DoubleEndedIterator

§Features

Requires the typed-tree feature. Codec implementations:

  • rapira-codecRapiraCodec for T: rapira::Rapira
  • bitcode-codecBitcodeCodec for T: bitcode::Encode + Decode

Implementations§

Source§

impl<K: Key, T: Send + Sync, C: Codec<T> + Sync> TypedTree<K, T, C>

Source

pub fn open(path: impl AsRef<Path>, config: Config, codec: C) -> DbResult<Self>

Open or create a TypedTree at the given path. Recovers the index from existing data files on disk.

Source§

impl<K: Key, T: Send + Sync, C: Codec<T> + Sync, H: TypedWriteHook<K, T>> TypedTree<K, T, C, H>

Source

pub fn open_hooked( path: impl AsRef<Path>, config: Config, codec: C, hook: H, ) -> DbResult<Self>

Open or create a TypedTree with a write hook for secondary index maintenance.

Source

pub fn close(self) -> DbResult<()>

Graceful shutdown: write hint files (if enabled), flush write buffers + fsync.

Source

pub fn flush_buffers(&self) -> DbResult<()>

Flush all shard write buffers to disk (without fsync).

Source

pub fn config(&self) -> &Config

Get the database configuration.

Source§

impl<K: Key, T: Send + Sync, C: Codec<T> + Sync, H: TypedWriteHook<K, T>> TypedTree<K, T, C, H>

Source

pub fn compact(&self) -> DbResult<usize>
where T: Clone,

Trigger a compaction pass across all shards.

Source

pub fn get(&self, key: &K) -> Option<TypedRef<'_, T>>

Get a guard-protected reference to a value by key. Lock-free, zero disk I/O.

Source

pub fn get_or_err(&self, key: &K) -> DbResult<TypedRef<'_, T>>

Get a guard-protected reference to a value by key, returning Err(KeyNotFound) if absent.

Source

pub fn put(&self, key: &K, value: T) -> DbResult<Option<TypedRef<'_, T>>>

Insert or update a key-value pair. Returns a TypedRef to the old value if the key existed (valid while the guard lives).

Source

pub fn insert(&self, key: &K, value: T) -> DbResult<()>

Insert a key-value pair only if the key does not exist. Returns Err(KeyExists) if the key is already present.

Source

pub fn delete(&self, key: &K) -> DbResult<Option<TypedRef<'_, T>>>

Delete a key. Returns a TypedRef to the old value if the key existed.

Source

pub fn atomic<R>( &self, shard_key: &K, f: impl FnOnce(&mut TypedShard<'_, K, T, C, H>) -> DbResult<R>, ) -> DbResult<R>

Atomically execute multiple operations on a single shard. All keys must route to the same shard as shard_key. The closure must be short — shard lock is held for its duration.

Source

pub fn cas(&self, key: &K, expected: &T, new_value: T) -> DbResult<()>
where T: PartialEq,

Compare-and-swap: if current value == expected, replace with new_value. Returns Ok(()) on success, Err(CasMismatch) if current != expected, Err(KeyNotFound) if key doesn’t exist.

Source

pub fn update( &self, key: &K, f: impl FnOnce(&T) -> T, ) -> DbResult<Option<TypedRef<'_, T>>>

Atomically read-modify-write. Returns Some(TypedRef) to the new value if key existed, None otherwise. The closure must not be heavy (shard lock is held).

Source

pub fn fetch_update( &self, key: &K, f: impl FnOnce(&T) -> T, ) -> DbResult<Option<TypedRef<'_, T>>>

Like update(), but returns Some(TypedRef) to the old value.

Source

pub fn contains(&self, key: &K) -> bool

Check if a key exists.

Source

pub fn first(&self) -> Option<(K, TypedRef<'_, T>)>

Return the first entry in index order, or None if empty. With reversed=true (default): the entry with the largest key. O(1) — follows head’s level-0 pointer, skipping marked nodes.

Source

pub fn last(&self) -> Option<(K, TypedRef<'_, T>)>

Return the last entry in index order, or None if empty. With reversed=true (default): the entry with the smallest key.

Source

pub fn prefix_iter(&self, prefix: &[u8]) -> TypedIter<'_, K, T>

Iterate entries whose keys start with prefix.

reversed=true (default): yields matching keys in DESC order. next() is O(1), next_back() is O(log n).

Source

pub fn iter(&self) -> TypedIter<'_, K, T>

Iterate all entries in index order.

reversed=true (default): DESC. reversed=false: ASC. next() is O(1), next_back() is O(log n).

Source

pub fn range(&self, start: &K, end: &K) -> TypedIter<'_, K, T>

Iterate entries in [start, end) — start inclusive, end exclusive.

reversed=true (default): DESC within range. reversed=false: ASC. next() is O(1), next_back() is O(log n).

Source

pub fn range_bounds( &self, start: Bound<&K>, end: Bound<&K>, ) -> TypedIter<'_, K, T>

Iterate entries in range defined by start and end bounds.

Unlike range(), allows Included, Excluded, or Unbounded for each bound independently.

reversed=true (default): DESC within range. reversed=false: ASC. next() is O(1), next_back() is O(log n).

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn sync_hints(&self) -> DbResult<()>

Write hint files for all active shard files. Call during graceful shutdown.

Source

pub fn migrate(&self, f: impl Fn(&K, &T) -> MigrateAction<T>) -> DbResult<usize>

Iterate all entries and optionally mutate them. Call once at startup.

The callback receives each (key, &T) and returns MigrateAction:

  • Keep — no change (fires on_init if NEEDS_INIT)
  • Update(new_value) — replace value (hook-free write, fires on_init)
  • Delete — remove entry (hook-free tombstone)

Returns the number of mutated entries.

Source

pub fn shard_for(&self, key: &K) -> usize

Trait Implementations§

Source§

impl<T, C, H> Collection for TypedTree<T::SelfId, T, C, H>
where T: CollectionMeta + Clone + Send + Sync, C: Codec<T> + Sync, H: TypedWriteHook<T::SelfId, T>, T::SelfId: Key + Ord,

Available on crate feature armour only.
Source§

fn name(&self) -> &str

Collection name (from CollectionMeta::NAME).
Source§

fn len(&self) -> usize

Number of entries in the collection.
Source§

fn compact(&self) -> DbResult<usize>

Run a compaction pass across all shards.
Source§

fn is_empty(&self) -> bool

Source§

impl<K: Key, T: Clone + Send + Sync, C: Codec<T> + Sync, H: TypedWriteHook<K, T>> CompactionIndex<K> for TypedTree<K, T, C, H>

Source§

fn update_if_match(&self, key: &K, old_loc: DiskLoc, new_loc: DiskLoc) -> bool

If the current index points to old_loc, it is updated to new_loc and returns true.
Source§

fn contains_key(&self, key: &K) -> bool

Returns true if the key currently exists in the index (i.e. has a live Put).
Source§

fn invalidate_blocks(&self, _shard_id: u8, _file_id: u32, _total_bytes: u64)

Invalidate cached blocks for a file after compaction replaces its contents.

Auto Trait Implementations§

§

impl<K, T, C, H = NoHook> !Freeze for TypedTree<K, T, C, H>

§

impl<K, T, C, H = NoHook> !RefUnwindSafe for TypedTree<K, T, C, H>

§

impl<K, T, C, H> Send for TypedTree<K, T, C, H>

§

impl<K, T, C, H> Sync for TypedTree<K, T, C, H>

§

impl<K, T, C, H> Unpin for TypedTree<K, T, C, H>
where C: Unpin, H: Unpin,

§

impl<K, T, C, H> UnsafeUnpin for TypedTree<K, T, C, H>
where C: UnsafeUnpin, H: UnsafeUnpin,

§

impl<K, T, C, H = NoHook> !UnwindSafe for TypedTree<K, T, C, H>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more