Skip to main content

BTree

Struct BTree 

Source
pub struct BTree<P: PageStore> { /* private fields */ }
Expand description

B+ Tree implementation

Implementations§

Source§

impl<P: PageStore> BTree<P>

Source

pub fn new(pager: Arc<P>) -> Self

Create a new B+ tree using the given pager

Source

pub fn with_root(pager: Arc<P>, root_page_id: u32) -> Self

Create a B+ tree with an existing root

Source

pub fn root_page_id(&self) -> u32

Get the root page ID

Source

pub fn is_empty(&self) -> bool

Check if tree is empty

Source

pub fn get(&self, key: &[u8]) -> BTreeResult<Option<Vec<u8>>>

Get value for a key

Source

pub fn get_with_budget( &self, key: &[u8], limit_bytes: usize, ) -> BTreeResult<Option<Vec<u8>>>

Bound value materialization before allocating overflow or decode buffers.

Source

pub fn insert(&self, key: &[u8], value: &[u8]) -> BTreeResult<()>

Insert a key-value pair.

Applies the value-layout spill ladder before touching the tree: the 256 MB ceiling rejects here without any allocation; values at or below COMPRESSION_THRESHOLD go inline raw; larger values select raw/compressed bytes and spill beyond OVERFLOW_THRESHOLD into an crate::storage::overflow::OverflowChain. The encoded bytes are then committed through Self::insert_encoded.

Source

pub fn upsert(&self, key: &[u8], value: &[u8]) -> BTreeResult<()>

Insert or update a key. If the key already exists and the new value has the same length as the old, the value bytes are overwritten in place — no structural changes, no rebalance, one leaf page write.

Falls back to delete + insert when the key is missing or the new value has a different length. Callers like persist_entities_to_pager that re-serialize a mutated entity with a fixed-width schema almost always hit the fast path, eliminating the BTree::delete + rebalance cost that previously dominated UPDATE workloads (~50% of bulk_update CPU).

Source

pub fn try_upsert_inline_without_splits( &self, items: &[(Vec<u8>, Vec<u8>)], ) -> BTreeResult<bool>

Upsert inline values without allocating, freeing or changing tree links. The caller must make every value durable in its WAL before calling this method. Returns false without writing if any item needs a structural update or an overflow chain.

Source

pub fn upsert_batch_sorted( &self, items: &[(Vec<u8>, Vec<u8>)], ) -> BTreeResult<()>

Batch upsert for sorted keys landing in a small set of leaves.

Walks to each leaf once, applies every same-or-shrink in-place overwrite that belongs there, writes the page once, then moves on. Keys that miss or whose new value grows fall back to the generic single-key upsert path — correctness identical, structural re-layout handled there.

Callers pass lex-sorted (key, value) pairs. For the entity UPDATE path, IDs are big-endian so u64 ordering = lex ordering.

Source

pub fn bulk_insert_sorted( &self, items: &[(Vec<u8>, Vec<u8>)], ) -> BTreeResult<()>

Bulk insert for sorted key-value pairs.

Optimized for monotonically increasing keys (e.g. entity IDs):

  • Walks to the target leaf ONCE, then appends many entries before re-walking.
  • Writes each leaf only once per batch (amortized over many inserts).

Falls back to per-entity insert on splits.

Source

pub fn delete(&self, key: &[u8]) -> BTreeResult<bool>

Delete a key

Source

pub fn cursor_first(&self) -> BTreeResult<BTreeCursor<P>>

Create a cursor starting at the first entry

Source

pub fn cursor_seek(&self, key: &[u8]) -> BTreeResult<BTreeCursor<P>>

Create a cursor starting at or after the given key

Source

pub fn range( &self, start_key: &[u8], end_key: &[u8], ) -> BTreeResult<Vec<(Vec<u8>, Vec<u8>)>>

Range scan from start_key to end_key (inclusive)

Source

pub fn count(&self) -> BTreeResult<usize>

Count entries in the tree

Source§

impl<P: PageStore> BTree<P>

Source

pub fn has_retirement_readers(&self) -> bool

Source

pub fn retired_pages(&self) -> BTreeResult<Vec<u32>>

Enumerate a retired generation before changing the allocator. Never follow leaf sibling links as additional ownership edges: interior children already own every leaf. Validate overflow chains as well.

Source

pub fn visit_recoverable_leaves<C, R>( &self, claim: C, record: R, ) -> BTreeResult<Vec<u32>>
where C: FnMut(u32) -> BTreeResult<()>, R: FnMut(Vec<u8>, Vec<u8>) -> BTreeResult<()>,

Stream the verified leaves of a damaged tree. Only checksum failures at the proven leaf depth may be skipped. The caller must separately prove that no allocated pages remain unowned before publishing any output.

Auto Trait Implementations§

§

impl<P> !Freeze for BTree<P>

§

impl<P> !RefUnwindSafe for BTree<P>

§

impl<P> Forget for BTree<P>

§

impl<P> Send for BTree<P>
where Arc<P>: Send,

§

impl<P> Sync for BTree<P>
where Arc<P>: Sync,

§

impl<P> Unpin for BTree<P>
where Arc<P>: Unpin,

§

impl<P> UnsafeUnpin for BTree<P>
where Arc<P>: UnsafeUnpin,

§

impl<P> UnwindSafe for BTree<P>
where Arc<P>: UnwindSafe,

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, 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.