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 new_verified(pager: Arc<P>) -> BTreeResult<Self>

Start an authenticated tree. Retired proof paths remain owned until the host publishes its replacement catalog through the pager fence.

Source

pub fn has_page_proofs(&self) -> bool

Source

pub fn with_verified_root( pager: Arc<P>, root_page_id: u32, proof: PageProofRoot, ) -> BTreeResult<Self>

Restore independently sealed page-image authority without scanning data.

Source

pub fn seal_page_proofs(&self) -> BTreeResult<Option<(u32, PageProofRoot)>>

The host must sync before publishing this pair in its catalog. Sealing writes the proof entries the generation set, so it can allocate and free proof pages: take page inventories after it, not before.

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 sorted_probe(&self) -> SortedProbe<'_, P>

Point lookups in ascending key order; see SortedProbe.

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 insert_sorted(&self, items: &[(Vec<u8>, Vec<u8>)]) -> BTreeResult<()>

Insert keys given in ascending order, wherever in the tree they belong.

Walks to a leaf once, puts into it every following key that belongs there and fits, and writes it once. insert walks from the root and rewrites the leaf for each key, and bulk_insert_sorted batches only keys past the end of a leaf. A key that does not fit, or whose value leaves the leaf, takes the single-key path, which splits.

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_frozen(&self) -> BTreeResult<BTreeCursor<P>>

Create a cursor at the first entry of a tree that nothing writes while the cursor lives, such as a sorted run about to be merged. It keeps the leaf it is on; a cursor from Self::cursor_first reads its leaf again, and proves it again, for every entry.

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 prepare_legacy_page_proofs(&self) -> BTreeResult<Self>

Authenticate a frozen legacy generation without moving or rewriting its data. The host must fence the reader capability and deferred reclamation, sync the new proof pages, and publish their root before allowing reuse. Existing cursors retain their retirement pin across this transition.

Source

pub fn has_retirement_readers(&self) -> bool

Source

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

Enumerate a sealed generation’s authenticated ownership without reading its data. Recovery may discard these pages even when their images fail verification. This does not certify the contents of any data page.

Source

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

Every page of a retired generation, for the allocator. A sealed proof already lists them, one proof node for 256 pages, so the generation is not read page by page; without one the tree is walked.

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> !UnwindSafe for BTree<P>

§

impl<P> Forget for BTree<P>

§

impl<P> Move for BTree<P>

§

impl<P> Send for BTree<P>
where Arc<()>: Send, Arc<TreePageStore<P>>: Send, RwLock<u32>: Send, RwLock<Option<(u32, Vec<u8>)>>: Send, ReentrantMutex<RawMutex, RawThreadId, ()>: Send,

§

impl<P> Sync for BTree<P>
where Arc<()>: Sync, Arc<TreePageStore<P>>: Sync, RwLock<u32>: Sync, RwLock<Option<(u32, Vec<u8>)>>: Sync, ReentrantMutex<RawMutex, RawThreadId, ()>: Sync,

§

impl<P> Unpin for BTree<P>
where Arc<()>: Unpin, Arc<TreePageStore<P>>: Unpin, Align4<u32>: Unpin, Align1<u8>: Unpin, *const u8 is !null: Unpin, Align8<usize>: Unpin,

§

impl<P> UnsafeUnpin for BTree<P>
where *const ArcInner<()> is !null: UnsafeUnpin, PhantomData<ArcInner<()>>: UnsafeUnpin, *const ArcInner<TreePageStore<P>> is !null: UnsafeUnpin, PhantomData<ArcInner<TreePageStore<P>>>: UnsafeUnpin, Align4<u32>: UnsafeUnpin, Align1<u8>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, Align8<usize>: UnsafeUnpin,

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.