Skip to content

Commit

Permalink
Update commit log, use mapped file queue
Browse files Browse the repository at this point in the history
  • Loading branch information
zhihuij committed Nov 10, 2023
1 parent 8b03e0e commit a6a82c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
23 changes: 7 additions & 16 deletions src/commit_log.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
use std::path::PathBuf;
use snafu::{location, Location};
use crate::error::Error::InvalidInput;

use crate::msg_index::MessageIndexUnit;
use crate::mmap_file::MemoryMappedFile;
use crate::error::Result;
use crate::mapped_file_queue::MappedFileQueue;

pub struct CommitLog {
current_file: MemoryMappedFile,
mapped_file_queue: MappedFileQueue,
}

impl CommitLog {
pub fn open(base_path: &str, max_file_size: u64) -> Result<Self> {
let base_dir = PathBuf::from(base_path);
let msg_log_path = base_dir.join("test.log");
// Create or open the initial MemoryMappedFile
let current_file = MemoryMappedFile::open(
msg_log_path.to_str().unwrap(), 0, max_file_size)?;

Ok(CommitLog {
current_file
})
pub fn open(store_path: &str, max_file_size: u64) -> Result<Self> {
let mapped_file_queue = MappedFileQueue::open(store_path, max_file_size)?;
Ok(CommitLog { mapped_file_queue })
}

pub fn write_records(&mut self, data: &Vec<u8>) -> Result<usize> {
// Write the record to the current file
self.current_file.append(data)
self.mapped_file_queue.append(data)
}

pub fn read_records(&self, msg_index_unit: &MessageIndexUnit) -> Result<Vec<u8>> {
if msg_index_unit.size > 0 {
// Read and return records
self.current_file.read(
self.mapped_file_queue.read(
msg_index_unit.offset as usize, msg_index_unit.size as usize)
} else {
Err(InvalidInput {
Expand Down
3 changes: 2 additions & 1 deletion src/mapped_file_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ pub struct MappedFileQueue {
impl MappedFileQueue {
// Constructor: Open or create a memory-mapped file.
pub fn open(store_path: &str, max_file_size: u64) -> Result<Self> {
// TODO load exist mapped files
Ok(MappedFileQueue { store_path: store_path.to_string(), max_file_size, mapped_files: Vec::new() })
}

pub fn get_mapped_files(&self) -> &Vec<MemoryMappedFile> {
&self.mapped_files
}

fn create_mapped_file(&mut self, start_offset: usize) -> &mut MemoryMappedFile {
pub fn create_mapped_file(&mut self, start_offset: usize) -> &mut MemoryMappedFile {
let store_path_clone = self.store_path.clone();
let base_dir = PathBuf::from(store_path_clone);
let file_name = format!("{:020}", start_offset);
Expand Down

0 comments on commit a6a82c1

Please sign in to comment.