Skip to content
This repository has been archived by the owner on Aug 4, 2024. It is now read-only.

Commit

Permalink
Feat/table multiple implementations (#28)
Browse files Browse the repository at this point in the history
* style: module classification

* refactor(ss_table): methods for abstracting SSTables

* feat(table): implement Table abstraction, improve SkipTable's Iter and remove IoType::Mem type

* feat(config): Level 0 memory implementation is configurable (disabled by default)

* style: loader directory transfer
  • Loading branch information
KKould authored Jul 1, 2023
1 parent e7ce9d4 commit db4d16d
Show file tree
Hide file tree
Showing 29 changed files with 1,139 additions and 1,131 deletions.
100 changes: 0 additions & 100 deletions src/kernel/io/mem.rs

This file was deleted.

36 changes: 0 additions & 36 deletions src/kernel/io/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
pub(crate) mod buf;
pub(crate) mod direct;
mod mem;

use crate::kernel::io::buf::{BufIoReader, BufIoWriter};
use crate::kernel::io::direct::{DirectIoReader, DirectIoWriter};
use crate::kernel::io::mem::{MemIoReader, MemIoWriter};
use crate::kernel::Result;
use crate::KernelError;
use bytes::BytesMut;
use parking_lot::Mutex;
use std::collections::HashMap;
use std::fs;
use std::io::{Read, Seek, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -40,14 +34,12 @@ impl FileExtension {
pub struct IoFactory {
dir_path: Arc<PathBuf>,
extension: Arc<FileExtension>,
mem_files: Mutex<HashMap<i64, MemIoWriter>>,
}

#[derive(PartialEq, Copy, Clone, Debug)]
pub enum IoType {
Buf,
Direct,
Mem,
}

impl IoFactory {
Expand All @@ -59,15 +51,6 @@ impl IoFactory {
Ok(match io_type {
IoType::Buf => Box::new(BufIoReader::new(dir_path, gen, extension)?),
IoType::Direct => Box::new(DirectIoReader::new(dir_path, gen, extension)?),
IoType::Mem => {
let bytes = self
.mem_files
.lock()
.get(&gen)
.ok_or(KernelError::FileNotFound)?
.bytes();
Box::new(MemIoReader::new(gen, bytes))
}
})
}

Expand All @@ -79,7 +62,6 @@ impl IoFactory {
Ok(match io_type {
IoType::Buf => Box::new(BufIoWriter::new(dir_path, gen, extension)?),
IoType::Direct => Box::new(DirectIoWriter::new(dir_path, gen, extension)?),
IoType::Mem => Box::new(self.load_mem_file(gen)),
})
}

Expand All @@ -88,46 +70,28 @@ impl IoFactory {
&self.dir_path
}

fn load_mem_file(&self, gen: i64) -> MemIoWriter {
self.mem_files
.lock()
.entry(gen)
.or_insert_with(|| MemIoWriter::new(BytesMut::new()))
.clone()
}

#[inline]
pub fn new(dir_path: impl Into<PathBuf>, extension: FileExtension) -> Result<Self> {
let path_buf = dir_path.into();
// 创建文件夹(如果他们缺失)
fs::create_dir_all(&path_buf)?;
let dir_path = Arc::new(path_buf);
let extension = Arc::new(extension);
let mem_files = Mutex::new(HashMap::new());

Ok(Self {
dir_path,
extension,
mem_files,
})
}

#[inline]
pub fn clean(&self, gen: i64) -> Result<()> {
if self.mem_files.lock().remove(&gen).is_some() {
return Ok(());
}

fs::remove_file(self.extension.path_with_gen(&self.dir_path, gen))?;
Ok(())
}

#[inline]
pub fn exists(&self, gen: i64) -> Result<bool> {
if self.mem_files.lock().contains_key(&gen) {
return Ok(true);
}

let path = self.extension.path_with_gen(&self.dir_path, gen);
Ok(fs::try_exists(path)?)
}
Expand Down
Loading

0 comments on commit db4d16d

Please sign in to comment.