pub struct StateChangelogBuffer { /* private fields */ }Expand description
Ring 0 SPSC changelog buffer for state mutations.
This buffer is designed for the hot path and must never allocate after initial warmup. It provides backpressure signaling when full.
§Thread Safety
The buffer is designed for single-producer single-consumer access:
- Ring 0 (producer): pushes entries via
push() - Ring 1 (consumer): drains entries via
drain()
Use atomic indices for thread-safe access when needed.
§Example
use laminar_storage::incremental::{StateChangelogBuffer, StateChangelogEntry};
// Pre-allocate buffer for 1024 entries
let mut buffer = StateChangelogBuffer::with_capacity(1024);
// Ring 0: Push state mutations (no allocation)
let entry = StateChangelogEntry::put(1, 12345, 0, 100);
if !buffer.push(entry) {
// Buffer full - apply backpressure
}
// Ring 1: Drain for WAL writes
let entries: Vec<_> = buffer.drain_all().collect();Implementations§
Source§impl StateChangelogBuffer
impl StateChangelogBuffer
Sourcepub const DEFAULT_CAPACITY: usize
pub const DEFAULT_CAPACITY: usize
Default buffer capacity (16K entries = 512KB).
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new changelog buffer with the given capacity.
The buffer is pre-allocated to avoid allocation on the hot path.
§Panics
Panics if capacity is 0.
Sourcepub fn advance_epoch(&mut self) -> u64
pub fn advance_epoch(&mut self) -> u64
Advances to the next epoch.
Sourcepub fn push(&self, entry: StateChangelogEntry) -> bool
pub fn push(&self, entry: StateChangelogEntry) -> bool
Pushes an entry to the buffer (zero allocation).
Returns true if successful, false if buffer is full (backpressure).
Sourcepub fn push_put(&self, key: &[u8], mmap_offset: u64, value_len: u32) -> bool
pub fn push_put(&self, key: &[u8], mmap_offset: u64, value_len: u32) -> bool
Pushes a Put operation for the given key.
Sourcepub fn push_delete(&self, key: &[u8]) -> bool
pub fn push_delete(&self, key: &[u8]) -> bool
Pushes a Delete operation for the given key.
Sourcepub fn pop(&self) -> Option<StateChangelogEntry>
pub fn pop(&self) -> Option<StateChangelogEntry>
Attempts to pop a single entry (for consumer).
Sourcepub fn drain(
&self,
max_count: usize,
) -> impl Iterator<Item = StateChangelogEntry> + '_
pub fn drain( &self, max_count: usize, ) -> impl Iterator<Item = StateChangelogEntry> + '_
Drains up to max_count entries from the buffer.
Returns an iterator over the drained entries.
Sourcepub fn drain_all(&self) -> impl Iterator<Item = StateChangelogEntry> + '_
pub fn drain_all(&self) -> impl Iterator<Item = StateChangelogEntry> + '_
Drains all available entries from the buffer.
Sourcepub fn total_pushed(&self) -> usize
pub fn total_pushed(&self) -> usize
Returns the total number of entries pushed (including overflows).
Sourcepub fn total_drained(&self) -> usize
pub fn total_drained(&self) -> usize
Returns the total number of entries drained.
Sourcepub fn overflow_count(&self) -> usize
pub fn overflow_count(&self) -> usize
Returns the number of overflow events (backpressure signals).
Sourcepub fn checkpoint_barrier(&self) -> (u64, usize)
pub fn checkpoint_barrier(&self) -> (u64, usize)
Creates a checkpoint barrier at the current position.
Returns the current epoch and write position for recovery.
Trait Implementations§
Source§impl Default for StateChangelogBuffer
impl Default for StateChangelogBuffer
impl Send for StateChangelogBuffer
impl Sync for StateChangelogBuffer
Auto Trait Implementations§
impl !Freeze for StateChangelogBuffer
impl !RefUnwindSafe for StateChangelogBuffer
impl Unpin for StateChangelogBuffer
impl UnsafeUnpin for StateChangelogBuffer
impl UnwindSafe for StateChangelogBuffer
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more