pub struct StableBTreeMap<K: Storable + Ord + Clone, V: Storable, M: Memory = DefaultMemoryImpl> { /* private fields */ }Expand description
StableBTreeMap Degree-six ordered B-tree over an exclusive allocation.
Implementations§
Source§impl<K: Storable + Ord + Clone, V: Storable, M: Memory> StableBTreeMap<K, V, M>
impl<K: Storable + Ord + Clone, V: Storable, M: Memory> StableBTreeMap<K, V, M>
Sourcepub fn create(memory: RuntimeMemory<M>) -> Self
pub fn create(memory: RuntimeMemory<M>) -> Self
Create a collection only in zero-page memory.
§Panics
Refuses nonempty memory before writing. Growth and codec failures trap.
Sourcepub fn create_with_options(
memory: RuntimeMemory<M>,
options: BTreeMapOptions,
) -> Self
pub fn create_with_options( memory: RuntimeMemory<M>, options: BTreeMapOptions, ) -> Self
Create an empty B-tree with per-allocation sizing options.
§Panics
Refuses invalid options or occupied memory before writing.
Sourcepub fn reset(memory: RuntimeMemory<M>) -> Self
pub fn reset(memory: RuntimeMemory<M>) -> Self
Deliberately replace any contents in this admitted allocation.
§Panics
Growth and codec failures trap.
Sourcepub fn reset_with_options(
memory: RuntimeMemory<M>,
options: BTreeMapOptions,
) -> Self
pub fn reset_with_options( memory: RuntimeMemory<M>, options: BTreeMapOptions, ) -> Self
Deliberately replace this allocation using the supplied sizing options.
§Panics
Invalid options are refused before writing. Backing failures trap.
Sourcepub fn load(memory: RuntimeMemory<M>) -> Self
pub fn load(memory: RuntimeMemory<M>) -> Self
Load retained collection state without initializing a replacement.
§Panics
Refuses empty or foreign memory and incompatible layouts/bounds without writing. Record decoding and backing failures trap.
Sourcepub fn options(&self) -> BTreeMapOptions
pub fn options(&self) -> BTreeMapOptions
Return the allocation policy recovered from this map’s header.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear entries while retaining this allocation’s exclusive lease.
§Panics
Panics on backing, record integrity, or codec failure.
Sourcepub fn reserve_pages(self, minimum: u64) -> (Self, Result<(), RuntimeGrowError>)
pub fn reserve_pages(self, minimum: u64) -> (Self, Result<(), RuntimeGrowError>)
Reserve a minimum virtual extent without exposing or duplicating the lease.
Returns the map on both success and ordinary growth refusal. The reservation preserves all entries.
minimum is an absolute virtual extent in 65,536-byte Wasm pages, not an
entry count or additional pages. Existing extents never shrink. A request
already covered performs no growth or writes. This reserves backing only:
it does not promise that any particular number or encoding of entries fits.
Callers own their byte/page budget and must qualify it for their codecs;
owned node-layout constants are not a supported capacity contract.
Sourcepub fn allocated_pages(self) -> (Self, u64)
pub fn allocated_pages(self) -> (Self, u64)
Observe the virtual extent in 65,536-byte pages while retaining the lease.
Consumes and returns the map together with its allocation size. This is allocated extent, not free space or guaranteed entry capacity.
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Insert or replace a value, returning the previous value.
§Panics
Panics on backing, record integrity, or codec failure.
Sourcepub fn visit_sorted_keys(&self, keys: &[K], visit: impl FnMut(&K, Option<V>))
pub fn visit_sorted_keys(&self, keys: &[K], visit: impl FnMut(&K, Option<V>))
Visit each sorted, distinct probe in order, including misses. At most one decoded value is passed to the visitor at a time.
§Panics
Panics on unordered or duplicate keys, backing, record integrity or codec failure.
Sourcepub fn try_visit_sorted_keys<E>(
&self,
keys: &[K],
visit: impl FnMut(&K, Option<V>) -> Result<(), E>,
) -> Result<(), E>
pub fn try_visit_sorted_keys<E>( &self, keys: &[K], visit: impl FnMut(&K, Option<V>) -> Result<(), E>, ) -> Result<(), E>
Visit sorted, distinct probes in order while sharing B-tree routing reads. The caller bounds the input. Each hit or miss is delivered exactly once until the visitor returns an error.
§Errors
Returns the first visitor error immediately, skipping subsequent probes and their subtree and payload reads.
§Panics
Panics on unordered or duplicate keys before visiting. Backing, record integrity and codec failures also panic.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Check whether the map contains a key.
§Panics
Panics on backing, record integrity, or codec failure.
Sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Remove a matching entry and return its value.
§Panics
Panics on backing, record integrity, or codec failure.
Sourcepub fn remove_many(&mut self, keys: BTreeSet<K>) -> u64
pub fn remove_many(&mut self, keys: BTreeSet<K>) -> u64
Consume a prepared set of distinct keys and return the number present. Empty sets, missing keys and retries contribute zero. The caller bounds the set during admission. Groups of keys share staged writes and root publication. A caller relying on prior existence checks must trap on a count mismatch in the same IC update to roll back its writes.
§Panics
Panics on backing, record integrity, or codec failure.
Sourcepub fn first_key_value(&self) -> Option<(K, V)>
pub fn first_key_value(&self) -> Option<(K, V)>
Read the smallest key and its value.
Sourcepub fn last_key_value(&self) -> Option<(K, V)>
pub fn last_key_value(&self) -> Option<(K, V)>
Read the largest key and its value.
Sourcepub fn range(&self, key_range: impl RangeBounds<K>) -> MapIter<'_, K, V, M>
pub fn range(&self, key_range: impl RangeBounds<K>) -> MapIter<'_, K, V, M>
Iterate within the supplied key bounds.
Sourcepub fn keys(&self) -> impl DoubleEndedIterator<Item = K> + '_
pub fn keys(&self) -> impl DoubleEndedIterator<Item = K> + '_
Iterate over keys without reading external values.
Sourcepub fn keys_range(
&self,
bounds: impl RangeBounds<K>,
) -> impl DoubleEndedIterator<Item = K> + '_
pub fn keys_range( &self, bounds: impl RangeBounds<K>, ) -> impl DoubleEndedIterator<Item = K> + '_
Iterate over keys within the bounds in either direction, skipping external values.
Sourcepub fn values(&self) -> impl DoubleEndedIterator<Item = V> + '_
pub fn values(&self) -> impl DoubleEndedIterator<Item = V> + '_
Iterate over values in key order.