MessageLog

Struct MessageLog 

Source
pub struct MessageLog { /* private fields */ }
Expand description

The entry point to Selium Log.

The MessageLog struct manages creating and opening logs, along with provisioning the SegmentList, and coordinates atomic reads and writes.

The MessageLog also creates the Flusher and Cleaner asynchronous tasks, and takes ownership of them to assure that the tasks are gracefully terminated when the MessageLog instance is destroyed.

§Examples

use anyhow::Result;
use selium_log::{config::LogConfig, message::Message, MessageLog};
use std::sync::Arc;

const MESSAGE_VERSION: u32 = 1;

#[tokio::main]
async fn main() -> Result<()> {
    let config = LogConfig::from_path("path/to/segments/dir");
    let log = MessageLog::open(Arc::new(config)).await?;
    let message = Message::single(b"Hello, world!", MESSAGE_VERSION);

    log.write(message).await?;
    log.flush().await?;
    let slice = log.read_slice(0, None).await?;

    if let Some(mut iter) = slice.messages() {
        let next = iter.next().await?;
        println!("{next:?}")
    }
    Ok(())
}

Implementations§

Source§

impl MessageLog

Source

pub async fn open(config: SharedLogConfig) -> Result<Self>

Opens a message log at the segments directory configured in the provided config argument.

If the log directory does not yet exist, it will be created.

Existing segments will be loaded from the filesystem, and provisioned as a SegmentList instance. If no segments exist in the log directory, a single “hot” segment will be created.

The Flusher and Cleaner asynchronous tasks will also be started, and will run in the background.

§Errors
Source

pub async fn write(&self, message: Message) -> Result<()>

Writes the provided Message to the current hot segment.

If the hot segment is at full capacity following the write, the current hot segment will be flushed, and a new segment will be created and designated as the hot segment in its place. Otherwise, the writes_since_last_flush field is incremented by 1.

§Errors
  • Returns LogError::SegmentListEmpty if there are no segments in the list yet.
  • Returns Err if writing to the hot segment fails.
  • Returns Err if the segment is full, and the current hot segment fails to flush.
  • Returns Err if the segment is full, and the new hot segment fails to be created.
Source

pub async fn read_slice( &self, offset: u64, limit: Option<u64>, ) -> Result<MessageSlice>

Reads a range of messages from a segment identified by the provided offset.

Returns an empty MessageSlice if the provided offset is greater than the total amount of entries in the log.

§Params
  • offset - The starting offset, used to locate the segment and relative offset.
  • limit - An optional message read limit.
Source

pub async fn flush(&self) -> Result<()>

Flushes the hot segment to the filesystem. The Flusher task interval will also be interrupted and reset.

§Errors
  • Returns Err if the hot segment fails to flush.
Source

pub async fn number_of_entries(&self) -> u64

Retrieves the total number of entries in the log, based on the end_offset in the current hot segment.

Trait Implementations§

Source§

impl Debug for MessageLog

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