pub struct IndexStore(/* private fields */);Expand description
IndexStore
Implementations§
Source§impl IndexStore
impl IndexStore
Sourcepub fn init(memory: VirtualMemory<DefaultMemoryImpl>) -> Self
pub fn init(memory: VirtualMemory<DefaultMemoryImpl>) -> Self
Initialize an index store with the provided backing memory.
Sourcepub fn insert_index_entry<E: EntityKind>(
&mut self,
entity: &E,
index: &IndexSpec,
) -> Result<(), Error>
pub fn insert_index_entry<E: EntityKind>( &mut self, entity: &E, index: &IndexSpec, ) -> Result<(), Error>
Inserts the given entity into the index defined by I.
- If
I::UNIQUE, insertion will fail if a conflicting entry already exists. - If the entity is missing required fields for this index, insertion is skipped.
- Insert an entity’s index entry, enforcing uniqueness where required.
Sourcepub fn remove_index_entry<E: EntityKind>(
&mut self,
entity: &E,
index: &IndexSpec,
)
pub fn remove_index_entry<E: EntityKind>( &mut self, entity: &E, index: &IndexSpec, )
Remove an entity’s entry for the given index if present.
Sourcepub fn resolve_data_values<E: EntityKind>(
&self,
index: &IndexSpec,
prefix: &[Value],
) -> Vec<DataKey>
pub fn resolve_data_values<E: EntityKind>( &self, index: &IndexSpec, prefix: &[Value], ) -> Vec<DataKey>
Resolve data keys for a given index prefix.
Sourcepub fn memory_bytes(&self) -> u64
pub fn memory_bytes(&self) -> u64
Sum of bytes used by all index entries.
Methods from Deref<Target = BTreeMap<IndexKey, IndexEntry, VirtualMemory<DefaultMemoryImpl>>>§
Methods from Deref<Target = BTreeMap<K, V, M>>§
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
The previous value of the key, if present, is returned.
PRECONDITION: key.to_bytes().len() <= max_size(Key) value.to_bytes().len() <= max_size(Value)
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true if the key exists.
Sourcepub fn first_key_value(&self) -> Option<(K, V)>
pub fn first_key_value(&self) -> Option<(K, V)>
Returns the first key-value pair in the map. The key in this pair is the minimum key in the map.
Sourcepub fn last_key_value(&self) -> Option<(K, V)>
pub fn last_key_value(&self) -> Option<(K, V)>
Returns the last key-value pair in the map. The key in this pair is the maximum key in the map.
Sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a key from the map, returning the previous value at the key if it exists.
Sourcepub fn pop_last(&mut self) -> Option<(K, V)>
pub fn pop_last(&mut self) -> Option<(K, V)>
Removes and returns the last element in the map. The key of this element is the maximum key that was in the map
Sourcepub fn pop_first(&mut self) -> Option<(K, V)>
pub fn pop_first(&mut self) -> Option<(K, V)>
Removes and returns the first element in the map. The key of this element is the minimum key that was in the map
Sourcepub fn iter(&self) -> Iter<'_, K, V, M>
pub fn iter(&self) -> Iter<'_, K, V, M>
Returns an iterator over the entries of the map, sorted by key.
§Example
use ic_stable_structures::{BTreeMap, DefaultMemoryImpl};
let mut map: BTreeMap<u64, String, _> = BTreeMap::init(DefaultMemoryImpl::default());
map.insert(1, "one".to_string());
map.insert(2, "two".to_string());
for entry in map.iter() {
println!("{}: {}", entry.key(), entry.value());
}Sourcepub fn range(&self, key_range: impl RangeBounds<K>) -> Iter<'_, K, V, M>
pub fn range(&self, key_range: impl RangeBounds<K>) -> Iter<'_, K, V, M>
Returns an iterator over the entries in the map where keys belong to the specified range.
§Example
use ic_stable_structures::{BTreeMap, DefaultMemoryImpl};
use std::ops::Bound;
let mut map: BTreeMap<u64, String, _> = BTreeMap::init(DefaultMemoryImpl::default());
map.insert(1, "one".to_string());
map.insert(2, "two".to_string());
map.insert(3, "three".to_string());
// Get entries with keys between 1 and 3 (inclusive)
for entry in map.range((Bound::Included(1), Bound::Included(3))) {
println!("{}: {}", entry.key(), entry.value());
}Sourcepub fn iter_from_prev_key(&self, bound: &K) -> Iter<'_, K, V, M>
pub fn iter_from_prev_key(&self, bound: &K) -> Iter<'_, K, V, M>
Returns an iterator starting just before the given key.
Finds the largest key strictly less than bound and starts from it.
Useful when range(bound..) skips the previous element.
Returns an empty iterator if no smaller key exists.
Sourcepub fn iter_upper_bound(&self, bound: &K) -> Iter<'_, K, V, M>
👎Deprecated: use iter_from_prev_key instead
pub fn iter_upper_bound(&self, bound: &K) -> Iter<'_, K, V, M>
iter_from_prev_key insteadDeprecated: use iter_from_prev_key instead.
The name iter_upper_bound was misleading — it suggested an inclusive
upper bound. In reality, it starts from the largest key strictly less
than the given bound.
The new name, iter_from_prev_key, better reflects this behavior and
improves code clarity.
Sourcepub fn keys_range(
&self,
key_range: impl RangeBounds<K>,
) -> KeysIter<'_, K, V, M>
pub fn keys_range( &self, key_range: impl RangeBounds<K>, ) -> KeysIter<'_, K, V, M>
Returns an iterator over the keys of the map which belong to the specified range.
Sourcepub fn values(&self) -> ValuesIter<'_, K, V, M>
pub fn values(&self) -> ValuesIter<'_, K, V, M>
Returns an iterator over the values of the map, sorted by key.
Sourcepub fn values_range(
&self,
key_range: impl RangeBounds<K>,
) -> ValuesIter<'_, K, V, M>
pub fn values_range( &self, key_range: impl RangeBounds<K>, ) -> ValuesIter<'_, K, V, M>
Returns an iterator over the values of the map where keys belong to the specified range.