IndexStore

Struct IndexStore 

Source
pub struct IndexStore(/* private fields */);
Expand description

IndexStore

Implementations§

Source§

impl IndexStore

Source

pub fn init(memory: VirtualMemory<DefaultMemoryImpl>) -> Self

Initialize an index store with the provided backing memory.

Source

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.
Source

pub fn remove_index_entry<E: EntityKind>( &mut self, entity: &E, index: &IndexSpec, )

Remove an entity’s entry for the given index if present.

Source

pub fn resolve_data_values<E: EntityKind>( &self, index: &IndexSpec, prefix: &[Value], ) -> Vec<DataKey>

Resolve data keys for a given index prefix.

Source

pub fn memory_bytes(&self) -> u64

Sum of bytes used by all index entries.

Methods from Deref<Target = BTreeMap<IndexKey, IndexEntry, VirtualMemory<DefaultMemoryImpl>>>§

Source

pub fn view(&self) -> impl Iterator<Item = (K, V)>

Source

pub fn to_vec(&self) -> Vec<(K, V)>

Collect all key/value pairs into a Vec.

Source

pub fn clear(&mut self)

clear the original clear() method in the ic-stable-structures library couldn’t be wrapped as it took ownership, so they made a new one

Methods from Deref<Target = BTreeMap<K, V, M>>§

Source

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)

Source

pub fn get(&self, key: &K) -> Option<V>

Returns the value for the given key, if it exists.

Source

pub fn contains_key(&self, key: &K) -> bool

Returns true if the key exists.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Source

pub fn len(&self) -> u64

Returns the number of elements in the map.

Source

pub fn clear_new(&mut self)

Removes all elements from the map.

Source

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.

Source

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.

Source

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.

Source

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

Source

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

Source

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());
}
Source

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());
}
Source

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.

Source

pub fn iter_upper_bound(&self, bound: &K) -> Iter<'_, K, V, M>

👎Deprecated: use iter_from_prev_key instead

Deprecated: 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.

Source

pub fn keys(&self) -> KeysIter<'_, K, V, M>

Returns an iterator over the keys of the map.

Source

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.

Source

pub fn values(&self) -> ValuesIter<'_, K, V, M>

Returns an iterator over the values of the map, sorted by key.

Source

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.

Trait Implementations§

Source§

impl Deref for IndexStore

Source§

type Target = BTreeMap<IndexKey, IndexEntry, VirtualMemory<Rc<RefCell<Vec<u8>>>>>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for IndexStore

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V