pub struct Journal<E: Context, A> { /* private fields */ }Expand description
Implementation of super::Mutable for fixed-size value journals.
§Repair
Like sqlite and rocksdb, the first invalid data read will be considered the new end of the journal (and the underlying blob will be truncated to the last valid item). Repair is performed during init.
Implementations§
Source§impl<E: Context, A: CodecFixedShared> Journal<E, A>
impl<E: Context, A: CodecFixedShared> Journal<E, A>
Sourcepub const CHUNK_SIZE: NonZeroUsize
pub const CHUNK_SIZE: NonZeroUsize
Size of each entry in bytes. Evaluating this rejects zero-size item types at compile time, which would otherwise divide by zero in the chunk math.
Sourcepub const CHUNK_SIZE_U64: u64
pub const CHUNK_SIZE_U64: u64
Size of each entry in bytes (as u64).
Sourcepub async fn init(context: E, cfg: Config) -> Result<Self, Error>
pub async fn init(context: E, cfg: Config) -> Result<Self, Error>
Initialize a new Journal instance.
All backing blobs are opened but not read during initialization. The replay method can be
used to iterate over all items in the Journal.
Sourcepub async fn init_at_size(
context: E,
cfg: Config,
size: u64,
) -> Result<Self, Error>
pub async fn init_at_size( context: E, cfg: Config, size: u64, ) -> Result<Self, Error>
Initialize a Journal in a fully-pruned state at size: existing data is cleared and the
journal behaves as if size items were appended then pruned. It is empty (bounds is
size..size) and the next append writes at position size. Used for state sync.
§Crash Safety
In the event of a crash during this call, upon restart recovery will ensure the journal is
either still in its prior state, or has bounds size..size.
Sourcepub async fn commit(&mut self) -> Result<(), Error>
pub async fn commit(&mut self) -> Result<(), Error>
Durably persists the current state of the structure.
Does not advance the recovery watermark, so external consumers may need to replay entries
beyond the previous sync(). Use sync() to advance the watermark and to ensure that a
crash after this call doesn’t require any recovery.
Sourcepub async fn sync(&mut self) -> Result<(), Error>
pub async fn sync(&mut self) -> Result<(), Error>
Durably persist the current state of the structure, ensuring no recovery is required in the event of a crash following this call.
Advances the recovery watermark to the current size.
Sourcepub async fn snapshot(&mut self) -> Result<Reader<'static, E, A>, Error>
pub async fn snapshot(&mut self) -> Result<Reader<'static, E, A>, Error>
Capture an owned snapshot (Reader) over the current journal. Bounds are frozen at
creation, and the snapshot stays readable across concurrent appends and prunes.
If the journal later rewinds or truncates into the returned reader’s range, subsequent reads from that range may observe unspecified contents.
Sourcepub const fn size(&self) -> u64
pub const fn size(&self) -> u64
Return the total number of items in the journal, irrespective of pruning. The next value appended to the journal will be at this position.
Sourcepub async fn append(&mut self, item: &A) -> Result<u64, Error>
pub async fn append(&mut self, item: &A) -> Result<u64, Error>
Append a new item to the journal, returning its position.
§Errors
Returns an error if the underlying storage operation fails.
Sourcepub async fn append_many<'a>(
&'a mut self,
items: Many<'a, A>,
) -> Result<u64, Error>
pub async fn append_many<'a>( &'a mut self, items: Many<'a, A>, ) -> Result<u64, Error>
Append items to the journal, returning the position of the last item appended.
Returns Error::EmptyAppend if items is empty.
Sourcepub fn prepare_append(&self, items: Many<'_, A>) -> PreparedAppend<A>
pub fn prepare_append(&self, items: Many<'_, A>) -> PreparedAppend<A>
Encode items into a buffer that can be appended later with Self::append_prepared.
This lets callers serialize borrowed items synchronously, release those borrows, and perform the append without holding unrelated locks across journal I/O.
Sourcepub async fn append_prepared(
&mut self,
prepared: PreparedAppend<A>,
) -> Result<u64, Error>
pub async fn append_prepared( &mut self, prepared: PreparedAppend<A>, ) -> Result<u64, Error>
Append items encoded by Self::prepare_append, returning the position of the last item
appended.
Returns Error::EmptyAppend if prepared contains no items.
Sourcepub async fn rewind(&mut self, size: u64) -> Result<(), Error>
pub async fn rewind(&mut self, size: u64) -> Result<(), Error>
Rewind the journal to the given size. Returns Error::InvalidRewind if size is beyond
the current size, or Error::ItemPruned if it precedes the pruning boundary. The journal
is not synced after rewinding.
§Warnings
- This operation is not guaranteed to survive restarts until
commit()orsync()is called. - This operation is not atomic. Its on-disk updates are ordered (blobs removed newest-to-oldest) so that restart recovery always rebuilds a contiguous retained prefix.
- Readers returned by
snapshotmay observe unspecified contents if this rewind truncates into their range.
Sourcepub const fn pruning_boundary(&self) -> u64
pub const fn pruning_boundary(&self) -> u64
Return the location before which all items have been pruned.
Sourcepub async fn prune(&mut self, min_item_pos: u64) -> Result<bool, Error>
pub async fn prune(&mut self, min_item_pos: u64) -> Result<bool, Error>
Allow the journal to prune items older than min_item_pos. The journal may not prune all
such items in order to preserve blob boundaries, but the amount of such items will always be
less than the configured number of items per blob. Returns true if any items were pruned.
Readers holding earlier snapshots keep reading pruned blobs through their own handles; later snapshots observe Error::ItemPruned.
Note that this operation may NOT be atomic, however it’s guaranteed not to leave gaps in the event of failure as items are always pruned in order from oldest to newest.
Sourcepub async fn destroy(self) -> Result<(), Error>
pub async fn destroy(self) -> Result<(), Error>
Remove any persisted data created by the journal.
§Crash Safety
This operation is intended for final teardown and is not crash-safe. If interrupted, reopening the same partition may observe partially removed state. Use Self::init_at_size for a recoverable reset.
Trait Implementations§
Source§impl<E: Context, A: CodecFixedShared> Contiguous for Journal<E, A>
impl<E: Context, A: CodecFixedShared> Contiguous for Journal<E, A>
Source§fn bounds(&self) -> Range<u64>
fn bounds(&self) -> Range<u64>
Source§async fn read(&self, pos: u64) -> Result<A, Error>
async fn read(&self, pos: u64) -> Result<A, Error>
Source§async fn read_many(&self, positions: &[u64]) -> Result<Vec<A>, Error>
async fn read_many(&self, positions: &[u64]) -> Result<Vec<A>, Error>
Source§fn try_read_sync(&self, pos: u64) -> Option<A>
fn try_read_sync(&self, pos: u64) -> Option<A>
None
otherwise. Decode failures surface as None and the async read path reports the error.Source§fn try_read_many_sync(&self, positions: &[u64]) -> Vec<Option<A>>
fn try_read_many_sync(&self, positions: &[u64]) -> Vec<Option<A>>
bounds() decline to None. The
async read paths are the sole error authority for declined positions.Source§impl<E: Context, A: CodecFixedShared> Inner<E> for Journal<E, A>
Available on neither commonware_stability_BETA nor commonware_stability_DELTA nor commonware_stability_EPSILON nor commonware_stability_GAMMA nor commonware_stability_RESERVED.
impl<E: Context, A: CodecFixedShared> Inner<E> for Journal<E, A>
commonware_stability_BETA nor commonware_stability_DELTA nor commonware_stability_EPSILON nor commonware_stability_GAMMA nor commonware_stability_RESERVED.Source§impl<E: Context, A: CodecFixedShared> Mutable for Journal<E, A>
impl<E: Context, A: CodecFixedShared> Mutable for Journal<E, A>
Source§async fn append(&mut self, item: &Self::Item) -> Result<u64, Error>
async fn append(&mut self, item: &Self::Item) -> Result<u64, Error>
Source§async fn append_many<'a>(
&'a mut self,
items: Many<'a, Self::Item>,
) -> Result<u64, Error>
async fn append_many<'a>( &'a mut self, items: Many<'a, Self::Item>, ) -> Result<u64, Error>
Source§async fn prune(&mut self, min_position: u64) -> Result<bool, Error>
async fn prune(&mut self, min_position: u64) -> Result<bool, Error>
min_position. Read moreSource§async fn rewind(&mut self, size: u64) -> Result<(), Error>
async fn rewind(&mut self, size: u64) -> Result<(), Error>
Source§async fn commit(&mut self) -> Result<(), Error>
async fn commit(&mut self) -> Result<(), Error>
Source§async fn sync(&mut self) -> Result<(), Error>
async fn sync(&mut self) -> Result<(), Error>
Source§async fn destroy(self) -> Result<(), Error>
async fn destroy(self) -> Result<(), Error>
Auto Trait Implementations§
impl<E, A> !Freeze for Journal<E, A>
impl<E, A> !RefUnwindSafe for Journal<E, A>
impl<E, A> !UnwindSafe for Journal<E, A>
impl<E, A> Send for Journal<E, A>where
A: Send,
impl<E, A> Sync for Journal<E, A>where
A: Sync,
impl<E, A> Unpin for Journal<E, A>
impl<E, A> UnsafeUnpin for Journal<E, A>
Blanket Implementations§
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
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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