pub struct SortedViewCache { /* private fields */ }Expand description
Sorted view cache maintaining entities in sort order.
§Bounding
The cache holds a full copy of every entity it has been given, so callers
that feed it from a bounded source (the projector and snapshot restore feed
it from the LRU-capped EntityCache) should use
upsert_bounded or
trim_to_max_entries with that source’s cap.
Entries are evicted from the bottom of the sort order, not by recency.
Evicting the least-recently-updated entries (mirroring the entity cache)
would drop a top-ranked entity that simply has not updated recently and
break leaderboard-style sort + take views. Evicting everything beyond
position max_entries keeps every window with skip + take <= max_entries
exact. An evicted entity re-enters on its next update, because the
projector re-reads the full entity from the entity cache before upserting.
Known edge: after entities are removed from (or move down out of) the top of the order, a previously evicted entity that has not updated since is missing from the cache until it next updates, so a window near the cap can under-fill or show a lower-ranked entity in its place until then.
Implementations§
Source§impl SortedViewCache
impl SortedViewCache
pub fn new(view_id: String, sort_field: Vec<String>, order: SortOrder) -> Self
pub fn view_id(&self) -> &str
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn upsert(&mut self, entity_key: String, entity: Value) -> UpsertResult
pub fn upsert(&mut self, entity_key: String, entity: Value) -> UpsertResult
Insert or update an entity, returns the position where it was inserted
Sourcepub fn upsert_bounded(
&mut self,
entity_key: String,
entity: Value,
max_entries: usize,
) -> UpsertResult
pub fn upsert_bounded( &mut self, entity_key: String, entity: Value, max_entries: usize, ) -> UpsertResult
Upsert an entity, then evict from the bottom of the sort order so the
cache holds at most max_entries entities.
If the upserted entity itself sorts beyond max_entries it is evicted
immediately and the returned position is >= max_entries.
Sourcepub fn trim_to_max_entries(&mut self, max_entries: usize) -> usize
pub fn trim_to_max_entries(&mut self, max_entries: usize) -> usize
Evict entities from the bottom of the sort order until at most
max_entries remain. Returns the number of evicted entities.
Each eviction is an O(log n) pop from the end of the ordered index,
so a batch of evictions (e.g. after a bulk rebuild) costs no more than
the inserts that caused it. The ordered-keys cache is truncated in place
when it is current, so trimming does not force a full rebuild.
Sourcepub fn remove(&mut self, entity_key: &str) -> Option<usize>
pub fn remove(&mut self, entity_key: &str) -> Option<usize>
Remove an entity, returns the position it was at
Sourcepub fn ordered_keys(&mut self) -> &[String]
pub fn ordered_keys(&mut self) -> &[String]
Get ordered keys (rebuilds cache if dirty)
Sourcepub fn get_window(&mut self, skip: usize, take: usize) -> Vec<(String, Value)>
pub fn get_window(&mut self, skip: usize, take: usize) -> Vec<(String, Value)>
Get a window of entities
Sourcepub fn get_all_ordered(&mut self) -> Vec<(String, Value)>
pub fn get_all_ordered(&mut self) -> Vec<(String, Value)>
Get every entity in deterministic sort order for query-side filtering.