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
impl MessageLog
Sourcepub async fn open(config: SharedLogConfig) -> Result<Self>
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
- Returns LogError::CreateLogsDirectory if an error occurs while creating the log directory.
- Returns Err if an error occurs while constructing the SegmentList.
Sourcepub async fn write(&self, message: Message) -> Result<()>
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.
Sourcepub async fn read_slice(
&self,
offset: u64,
limit: Option<u64>,
) -> Result<MessageSlice>
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.
Sourcepub async fn flush(&self) -> Result<()>
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.
Sourcepub async fn number_of_entries(&self) -> u64
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.