//! A memory-efficient index that uses an unordered map internally to map translated keys to //! arbitrary values. If you require ordering over the map's keys, consider //! [crate::index::ordered::Index] instead. use crate::{ index::{ storage::{insert_front, Cursor as CursorImpl, ImmutableCursor, IndexEntry, Record}, Cursor as CursorTrait, Unordered, }, translator::Translator, }; use commonware_runtime::{ telemetry::metrics::{Counter, Gauge, MetricsExt as _}, Metrics, }; use std::collections::{ hash_map::{Entry, OccupiedEntry, VacantEntry}, HashMap, }; /// The initial capacity of the internal hashmap. This is a guess at the number of unique keys we /// will encounter. The hashmap will grow as needed, but this is a good starting point (covering the /// entire [crate::translator::OneCap] range). const INITIAL_CAPACITY: usize = 256; /// Implementation of [IndexEntry] for [OccupiedEntry]. impl IndexEntry for OccupiedEntry<'_, K, Record> { fn get_mut(&mut self) -> &mut Record { self.get_mut() } fn remove(self) { OccupiedEntry::remove(self); } } /// A [crate::index::Cursor] over the values associated with a translated key. pub type Cursor<'a, K, V> = CursorImpl<'a, V, OccupiedEntry<'a, K, Record>>; /// A memory-efficient index that uses an unordered map internally to map translated keys to /// arbitrary values. pub struct Index { translator: T, map: HashMap, T>, keys: Gauge, items: Gauge, pruned: Counter, } impl Index { /// Create a new entry in the index. fn create(keys: &Gauge, items: &Gauge, vacant: VacantEntry<'_, T::Key, Record>, v: V) { keys.inc(); items.inc(); vacant.insert(Record { value: v, next: None, }); } /// Create a new index with the given translator and metrics registry. pub fn new(ctx: impl Metrics, translator: T) -> Self { Self { translator: translator.clone(), map: HashMap::with_capacity_and_hasher(INITIAL_CAPACITY, translator), keys: ctx.gauge("keys", "Number of translated keys in the index"), items: ctx.gauge("items", "Number of items in the index"), pruned: ctx.counter("pruned", "Number of items pruned"), } } } impl super::Factory for Index { fn new(ctx: impl commonware_runtime::Metrics, translator: T) -> Self { Self::new(ctx, translator) } } impl Unordered for Index { type Value = V; type Cursor<'a> = Cursor<'a, T::Key, V> where Self: 'a; fn get<'a>(&'a self, key: &[u8]) -> impl Iterator + 'a where V: 'a, { let k = self.translator.transform(key); self.map .get(&k) .map(|record| ImmutableCursor::new(record)) .into_iter() .flatten() } fn get_mut<'a>(&'a mut self, key: &[u8]) -> Option> { let k = self.translator.transform(key); match self.map.entry(k) { Entry::Occupied(entry) => Some(Cursor::<'_, T::Key, V>::new( entry, &self.keys, &self.items, &self.pruned, )), Entry::Vacant(_) => None, } } fn get_mut_or_insert<'a>(&'a mut self, key: &[u8], value: V) -> Option> { let k = self.translator.transform(key); match self.map.entry(k) { Entry::Occupied(entry) => Some(Cursor::<'_, T::Key, V>::new( entry, &self.keys, &self.items, &self.pruned, )), Entry::Vacant(entry) => { Self::create(&self.keys, &self.items, entry, value); None } } } fn insert(&mut self, key: &[u8], v: V) { let k = self.translator.transform(key); match self.map.entry(k) { Entry::Occupied(mut entry) => { insert_front(entry.get_mut(), v); self.items.inc(); } Entry::Vacant(entry) => { Self::create(&self.keys, &self.items, entry, v); } } } fn insert_and_retain(&mut self, key: &[u8], value: V, should_retain: impl Fn(&V) -> bool) { let k = self.translator.transform(key); match self.map.entry(k) { Entry::Occupied(entry) => { let mut cursor = Cursor::<'_, T::Key, V>::new(entry, &self.keys, &self.items, &self.pruned); // Drop anything that should not be retained. cursor.retain(&should_retain); // Add the new value only if it should be retained. if should_retain(&value) { cursor.insert(value); } } Entry::Vacant(entry) => { // Create the entry only if the value should be retained. if should_retain(&value) { Self::create(&self.keys, &self.items, entry, value); } } } } fn remove(&mut self, key: &[u8]) { let k = self.translator.transform(key); if let Some(mut record) = self.map.remove(&k) { // To ensure metrics are accurate, account for all conflicting values in the chain. self.keys.dec(); self.items.dec(); self.pruned.inc(); while let Some(next) = record.next.take() { self.items.dec(); self.pruned.inc(); record = *next; } } } #[cfg(test)] fn keys(&self) -> usize { self.map.len() } #[cfg(test)] fn items(&self) -> usize { self.items.get() as usize } #[cfg(test)] fn pruned(&self) -> usize { self.pruned.get() as usize } } impl Drop for Index { /// To avoid stack overflow on keys with many collisions, we implement an iterative drop (in /// lieu of Rust's default recursive drop). fn drop(&mut self) { for (_, mut record) in self.map.drain() { let mut next = record.next.take(); while let Some(mut record) = next { next = record.next.take(); } } } }