pub struct SortedViewCache { /* private fields */ }Expand description
Sorted view cache maintaining entities in sort order.
§Bounding
The cache holds every entity it has been given. It shares an entity’s
fields with whoever gave it (see SharedEntity) rather than copying
them, but it keeps them alive, 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: impl Into<SharedEntity>,
) -> UpsertResult
pub fn upsert( &mut self, entity_key: String, entity: impl Into<SharedEntity>, ) -> UpsertResult
Insert or update an entity, returns the position where it was inserted
An update merges into the entity held, keeping fields the update lacks. When it lacks none, which is the case for a whole entity, the cache shares the update’s fields instead of copying them.
Sourcepub fn would_keep<E: EntityFields + ?Sized>(
&self,
entity_key: &str,
entity: &E,
max_entries: usize,
) -> bool
pub fn would_keep<E: EntityFields + ?Sized>( &self, entity_key: &str, entity: &E, max_entries: usize, ) -> bool
Whether upserting entity under entity_key into a cache bounded at
max_entries would keep it: the cache already holds the key, has
room, or the entity sorts before its current last entry.
Lets a caller skip copying an entity that Self::upsert_bounded
would evict straight away, which in a busy view is most of them.
Sourcepub fn upsert_bounded(
&mut self,
entity_key: String,
entity: impl Into<SharedEntity>,
max_entries: usize,
) -> UpsertResult
pub fn upsert_bounded( &mut self, entity_key: String, entity: impl Into<SharedEntity>, 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.
pub fn remove(&mut self, entity_key: &str) -> Option<usize>
Sourcepub fn get(&self, entity_key: &str) -> Option<&SharedEntity>
pub fn get(&self, entity_key: &str) -> Option<&SharedEntity>
Get entity by key
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, as copies.
Sourcepub fn get_all_ordered(&mut self) -> Vec<(String, Value)>
pub fn get_all_ordered(&mut self) -> Vec<(String, Value)>
Copies of every entity in deterministic sort order.
Sourcepub fn ordered_entities(&mut self) -> Vec<(String, SharedEntity)>
pub fn ordered_entities(&mut self) -> Vec<(String, SharedEntity)>
Every entity in deterministic sort order for query-side filtering, sharing their fields with the cache rather than copying them.