pub struct Journal<E: Storage + Metrics, A: CodecFixed> { /* private fields */ }Expand description
A segmented journal with fixed-size entries.
Each section is stored in a separate blob. Within each blob, items are fixed-size.
§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 occurs during init by checking each blob’s size.
Implementations§
Source§impl<E: Storage + Metrics, A: CodecFixedShared> Journal<E, A>
impl<E: Storage + Metrics, A: CodecFixedShared> Journal<E, A>
Sourcepub const CHUNK_SIZE: usize = A::SIZE
pub const CHUNK_SIZE: usize = A::SIZE
Size of each entry.
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. Use replay
to iterate over all items.
Sourcepub async fn append(&mut self, section: u64, item: A) -> Result<u64, Error>
pub async fn append(&mut self, section: u64, item: A) -> Result<u64, Error>
Append a new item to the journal in the given section.
Returns the position of the item within the section (0-indexed).
Sourcepub async fn get(&self, section: u64, position: u64) -> Result<A, Error>
pub async fn get(&self, section: u64, position: u64) -> Result<A, Error>
Read the item at the given section and position.
§Errors
- Error::AlreadyPrunedToSection if the section has been pruned.
- Error::SectionOutOfRange if the section doesn’t exist.
- Error::ItemOutOfRange if the position is beyond the blob size.
Sourcepub async fn last(&self, section: u64) -> Result<Option<A>, Error>
pub async fn last(&self, section: u64) -> Result<Option<A>, Error>
Read the last item in a section, if any.
Sourcepub async fn replay(
&self,
start_section: u64,
start_position: u64,
buffer: NonZeroUsize,
) -> Result<impl Stream<Item = Result<(u64, u64, A), Error>> + Send + '_, Error>
pub async fn replay( &self, start_section: u64, start_position: u64, buffer: NonZeroUsize, ) -> Result<impl Stream<Item = Result<(u64, u64, A), Error>> + Send + '_, Error>
Returns a stream of all items starting from the given section.
Each item is returned as (section, position, item).
Sourcepub async fn prune(&mut self, min: u64) -> Result<bool, Error>
pub async fn prune(&mut self, min: u64) -> Result<bool, Error>
Prune all sections less than min. Returns true if any were pruned.
Sourcepub fn oldest_section(&self) -> Option<u64>
pub fn oldest_section(&self) -> Option<u64>
Returns the oldest section number, if any blobs exist.
Sourcepub fn newest_section(&self) -> Option<u64>
pub fn newest_section(&self) -> Option<u64>
Returns the newest section number, if any blobs exist.
Sourcepub fn sections(&self) -> impl Iterator<Item = u64> + '_
pub fn sections(&self) -> impl Iterator<Item = u64> + '_
Returns an iterator over all section numbers.
Sourcepub async fn section_len(&self, section: u64) -> Result<u64, Error>
pub async fn section_len(&self, section: u64) -> Result<u64, Error>
Returns the number of items in the given section.
Sourcepub async fn size(&self, section: u64) -> Result<u64, Error>
pub async fn size(&self, section: u64) -> Result<u64, Error>
Returns the byte size of the given section.
Sourcepub async fn rewind(&mut self, section: u64, offset: u64) -> Result<(), Error>
pub async fn rewind(&mut self, section: u64, offset: u64) -> Result<(), Error>
Rewind the journal to a specific section and byte offset.
This truncates the section to the given size. All sections
after section are removed.
Auto Trait Implementations§
impl<E, A> Freeze for Journal<E, A>where
E: Freeze,
impl<E, A> !RefUnwindSafe 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> !UnwindSafe 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>
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