use async_trait::async_trait;
use std::fmt::{Display, Formatter};
mod file_log;
pub use file_log::FileLog;
pub type Offset = u64;
pub type Bytes = Vec<u8>;
struct Record {
value_length: u64,
value: Bytes,
}
#[async_trait]
pub trait Log {
async fn append(&self, entry: Bytes) -> Result<Offset, LogError>;
async fn read(&self, offset: Offset) -> Result<(Bytes, Offset), LogError>;
async fn batch_read(
&self,
offset: Offset,
max_records: usize,
) -> Result<(Vec<Bytes>, Offset), LogError>;
}
#[derive(Debug)]
pub enum LogError {
IoError(std::io::Error),
IndexOutOfBounds,
}
impl Display for LogError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
LogError::IoError(e) => write!(f, "I/O error: {}", e),
LogError::IndexOutOfBounds => write!(f, "Index out of bounds"),
}
}
}
impl std::error::Error for LogError {}