Skip to main content

HistoryCache

Struct HistoryCache 

Source
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

Source

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.

Source

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

Source

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

Source

pub fn kind(&self) -> HistoryKind

History kind of this cache.

Source

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.

Source

pub fn max_samples(&self) -> usize

max_samples cap of the cache.

Source

pub fn evicted_count(&self) -> u64

Number of samples discarded by KeepLast eviction since start.

Source

pub fn insert(&mut self, change: CacheChange) -> Result<(), CacheError>

Inserts a change.

§Errors
  • CapacityExceeded: only with KeepAll, cache full.
  • DuplicateSequenceNumber: SN already present.
  • ZeroDepth: KeepLast { depth: 0 }.
Source

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.

Source

pub fn get(&self, sn: SequenceNumber) -> Option<&CacheChange>

Fetches a change by SN.

Source

pub fn remove_up_to(&mut self, sn: SequenceNumber) -> usize

Removes all changes with SN ≤ sn. Returns the number of removed entries.

Source

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

Source

pub fn min_sn(&self) -> Option<SequenceNumber>

Smallest SN in the cache.

Source

pub fn max_sn(&self) -> Option<SequenceNumber>

Largest SN in the cache.

Source

pub fn len(&self) -> usize

Number of changes.

Source

pub fn is_empty(&self) -> bool

True if no changes.

Source

pub fn capacity(&self) -> usize

Maximum capacity.

Trait Implementations§

Source§

impl Clone for HistoryCache

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HistoryCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.