pub struct BTree<P: PageStore> { /* private fields */ }Expand description
B+ Tree implementation
Implementations§
Source§impl<P: PageStore> BTree<P>
impl<P: PageStore> BTree<P>
Sourcepub fn with_root(pager: Arc<P>, root_page_id: u32) -> Self
pub fn with_root(pager: Arc<P>, root_page_id: u32) -> Self
Create a B+ tree with an existing root
Sourcepub fn root_page_id(&self) -> u32
pub fn root_page_id(&self) -> u32
Get the root page ID
Sourcepub fn new_verified(pager: Arc<P>) -> BTreeResult<Self>
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.
pub fn has_page_proofs(&self) -> bool
Sourcepub fn with_verified_root(
pager: Arc<P>,
root_page_id: u32,
proof: PageProofRoot,
) -> BTreeResult<Self>
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.
Sourcepub fn seal_page_proofs(&self) -> BTreeResult<Option<(u32, PageProofRoot)>>
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.
Sourcepub fn sorted_probe(&self) -> SortedProbe<'_, P>
pub fn sorted_probe(&self) -> SortedProbe<'_, P>
Point lookups in ascending key order; see SortedProbe.
Sourcepub fn get_with_budget(
&self,
key: &[u8],
limit_bytes: usize,
) -> BTreeResult<Option<Vec<u8>>>
pub fn get_with_budget( &self, key: &[u8], limit_bytes: usize, ) -> BTreeResult<Option<Vec<u8>>>
Bound value materialization before allocating overflow or decode buffers.
Sourcepub fn insert(&self, key: &[u8], value: &[u8]) -> BTreeResult<()>
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.
Sourcepub fn upsert(&self, key: &[u8], value: &[u8]) -> BTreeResult<()>
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).
Sourcepub fn try_upsert_inline_without_splits(
&self,
items: &[(Vec<u8>, Vec<u8>)],
) -> BTreeResult<bool>
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.
Sourcepub fn upsert_batch_sorted(
&self,
items: &[(Vec<u8>, Vec<u8>)],
) -> BTreeResult<()>
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.
Sourcepub fn insert_sorted(&self, items: &[(Vec<u8>, Vec<u8>)]) -> BTreeResult<()>
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.
Sourcepub fn bulk_insert_sorted(
&self,
items: &[(Vec<u8>, Vec<u8>)],
) -> BTreeResult<()>
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.
Sourcepub fn delete(&self, key: &[u8]) -> BTreeResult<bool>
pub fn delete(&self, key: &[u8]) -> BTreeResult<bool>
Delete a key
Sourcepub fn cursor_first_frozen(&self) -> BTreeResult<BTreeCursor<P>>
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.
Sourcepub fn cursor_first(&self) -> BTreeResult<BTreeCursor<P>>
pub fn cursor_first(&self) -> BTreeResult<BTreeCursor<P>>
Create a cursor starting at the first entry
Sourcepub fn cursor_seek(&self, key: &[u8]) -> BTreeResult<BTreeCursor<P>>
pub fn cursor_seek(&self, key: &[u8]) -> BTreeResult<BTreeCursor<P>>
Create a cursor starting at or after the given key
Sourcepub fn range(
&self,
start_key: &[u8],
end_key: &[u8],
) -> BTreeResult<Vec<(Vec<u8>, Vec<u8>)>>
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)
Sourcepub fn count(&self) -> BTreeResult<usize>
pub fn count(&self) -> BTreeResult<usize>
Count entries in the tree
Source§impl<P: PageStore> BTree<P>
impl<P: PageStore> BTree<P>
Sourcepub fn prepare_legacy_page_proofs(&self) -> BTreeResult<Self>
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.
pub fn has_retirement_readers(&self) -> bool
Sourcepub fn published_owned_pages(&self) -> BTreeResult<Option<Vec<u32>>>
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.
Sourcepub fn reclaimable_pages(&self) -> BTreeResult<Vec<u32>>
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.
Sourcepub fn retired_pages(&self) -> BTreeResult<Vec<u32>>
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.
Sourcepub fn visit_recoverable_leaves<C, R>(
&self,
claim: C,
record: R,
) -> BTreeResult<Vec<u32>>
pub fn visit_recoverable_leaves<C, R>( &self, claim: C, record: R, ) -> BTreeResult<Vec<u32>>
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>
impl<P> Sync for BTree<P>
impl<P> Unpin for BTree<P>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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