pub struct HistoryCache { /* private fields */ }Expand description
Ordered sample storage.
Internal storage: BTreeMap for O(log n) insert/lookup and an efficient
range iterator.
Note: the writing methods still need
&mut self (BTreeMap is not concurrent-safe), but stats are
available in parallel in an Arc<HistoryCacheStats> — see
stats.
Implementations§
Source§impl HistoryCache
impl HistoryCache
Sourcepub fn new_with_kind(kind: HistoryKind, max_samples: usize) -> Self
pub fn new_with_kind(kind: HistoryKind, max_samples: usize) -> Self
Creates a new cache. max_samples is the upper bound:
with KeepAll exceeding it leads to CapacityExceeded, with
KeepLast to LRU eviction of the oldest sample.
Sourcepub fn stats(&self) -> Arc<HistoryCacheStats>
pub fn stats(&self) -> Arc<HistoryCacheStats>
Shared stats handle for lock-free monitoring.
Consumers hold an Arc<HistoryCacheStats> and poll the
atomics with Acquire ordering. Values always reflect a
completed cache mutation; cross-field consistency between
len and max_sn is not guaranteed (tear risk).
Sourcepub fn new(max_samples: usize) -> Self
pub fn new(max_samples: usize) -> Self
Legacy constructor — creates a KeepAll cache with a hard
capacity limit. For new users prefer [new_with_kind].
Sourcepub fn kind(&self) -> HistoryKind
pub fn kind(&self) -> HistoryKind
History kind of this cache.
Sourcepub fn set_kind_and_max(&mut self, kind: HistoryKind, max_samples: usize)
pub fn set_kind_and_max(&mut self, kind: HistoryKind, max_samples: usize)
Expert-only: sets the history kind and max_samples cap at runtime.
Usage: short-term expansion for a backend replay burst (DurabilityService §2.2.3.5), when KeepLast(1) would collapse the replay window. The caller must restore the original kind afterwards.
This method moves no existing samples. If the
new cap is smaller than the current sample count, the
existing samples stay visible — the next insert then
evicts by KeepLast rules.
Sourcepub fn max_samples(&self) -> usize
pub fn max_samples(&self) -> usize
max_samples cap of the cache.
Sourcepub fn evicted_count(&self) -> u64
pub fn evicted_count(&self) -> u64
Number of samples discarded by KeepLast eviction since
start.
Sourcepub fn insert(&mut self, change: CacheChange) -> Result<(), CacheError>
pub fn insert(&mut self, change: CacheChange) -> Result<(), CacheError>
Inserts a change.
§Errors
CapacityExceeded: only withKeepAll, cache full.DuplicateSequenceNumber: SN already present.ZeroDepth:KeepLast { depth: 0 }.
Sourcepub fn insert_returning_evicted(
&mut self,
change: CacheChange,
) -> Result<Option<CacheChange>, CacheError>
pub fn insert_returning_evicted( &mut self, change: CacheChange, ) -> Result<Option<CacheChange>, CacheError>
Like Self::insert, but returns the evicted CacheChange
(or None if nothing was evicted). Allows the caller to
recycle the payload Arc<[u8]> instead of dropping it — see
the ReliableWriter::stage_sample hot-path pool.
§Errors
As Self::insert.
Sourcepub fn get(&self, sn: SequenceNumber) -> Option<&CacheChange>
pub fn get(&self, sn: SequenceNumber) -> Option<&CacheChange>
Fetches a change by SN.
Sourcepub fn remove_up_to(&mut self, sn: SequenceNumber) -> usize
pub fn remove_up_to(&mut self, sn: SequenceNumber) -> usize
Removes all changes with SN ≤ sn.
Returns the number of removed entries.
Sourcepub fn iter_range(
&self,
lo: SequenceNumber,
hi: SequenceNumber,
) -> impl Iterator<Item = &CacheChange> + '_
pub fn iter_range( &self, lo: SequenceNumber, hi: SequenceNumber, ) -> impl Iterator<Item = &CacheChange> + '_
Iterates in SN order over changes in the range [lo, hi]
(both inclusive).
Sourcepub fn min_sn(&self) -> Option<SequenceNumber>
pub fn min_sn(&self) -> Option<SequenceNumber>
Smallest SN in the cache.
Sourcepub fn max_sn(&self) -> Option<SequenceNumber>
pub fn max_sn(&self) -> Option<SequenceNumber>
Largest SN in the cache.