Skip to content

Commit

Permalink
storage: refactor MemStorage to support fault injection (#36)
Browse files Browse the repository at this point in the history
* storage: refactor the MemStorage to support fault injection

Signed-off-by: Fullstop000 <[email protected]>

* fix issues

Signed-off-by: Fullstop000 <[email protected]>

* fix test

Signed-off-by: Fullstop000 <[email protected]>

* remove un-compatible code

Signed-off-by: Fullstop000 <[email protected]>
  • Loading branch information
Fullstop000 authored Jan 15, 2020
1 parent 4654160 commit 29784a0
Show file tree
Hide file tree
Showing 8 changed files with 639 additions and 132 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ crc32c = "0.4.0"
fs2 = "0.4.3"
fxhash = "0.2.1"
quick-error = "1.2.3"
num-traits = "0.2"
num-derive = "0.3"

[dev-dependencies]
criterion = "0.3.0"
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ extern crate log;
extern crate crc32c;
extern crate crossbeam_channel;
extern crate crossbeam_utils;
#[macro_use]
extern crate num_derive;
extern crate quick_error;
extern crate rand;
extern crate snap;
Expand Down
11 changes: 3 additions & 8 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::cache::Cache;
use crate::db::filename::{generate_filename, FileType};
use crate::filter::FilterPolicy;
use crate::logger::Logger;
use crate::options::CompressionType::{NoCompression, SnappyCompression, Unknown};
use crate::snapshot::Snapshot;
use crate::sstable::block::Block;
use crate::storage::{File, Storage};
Expand All @@ -30,7 +29,7 @@ use crate::Log;
use std::rc::Rc;
use std::sync::Arc;

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, FromPrimitive)]
pub enum CompressionType {
NoCompression = 0,
SnappyCompression = 1,
Expand All @@ -39,11 +38,7 @@ pub enum CompressionType {

impl From<u8> for CompressionType {
fn from(i: u8) -> Self {
match i {
0 => NoCompression,
1 => SnappyCompression,
_ => Unknown,
}
num_traits::FromPrimitive::from_u8(i).unwrap()
}
}

Expand Down Expand Up @@ -273,7 +268,7 @@ impl Default for Options {
block_size: 4 * 1024, // 4KB
block_restart_interval: 16,
max_file_size: 2 * 1024 * 1024, // 2MB
compression: SnappyCompression,
compression: CompressionType::SnappyCompression,
reuse_logs: true,
filter_policy: None,
logger: None,
Expand Down
2 changes: 1 addition & 1 deletion src/sstable/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<F: File> Table<F> {
let mut cache_key_buffer = vec![0; 16];
put_fixed_64(&mut cache_key_buffer, self.cache_id);
put_fixed_64(&mut cache_key_buffer, data_block_handle.offset);
if let Some(cache_handle) = cache.look_up(&cache_key_buffer.as_slice()) {
if let Some(cache_handle) = cache.look_up(&cache_key_buffer) {
let b = cache_handle.value().unwrap();
cache.release(cache_handle);
b
Expand Down
23 changes: 11 additions & 12 deletions src/storage/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct FileStorage;

impl Storage for FileStorage {
type F = SysFile;
fn create(&self, name: &str) -> Result<Self::F> {
fn create<P: AsRef<Path>>(&self, name: P) -> Result<Self::F> {
match OpenOptions::new()
.write(true)
.read(true)
Expand All @@ -43,19 +43,19 @@ impl Storage for FileStorage {
}
}

fn open(&self, name: &str) -> Result<Self::F> {
fn open<P: AsRef<Path>>(&self, name: P) -> Result<Self::F> {
match OpenOptions::new().write(true).read(true).open(name) {
Ok(f) => Ok(f),
Err(e) => Err(Error::IO(e)),
}
}

fn remove(&self, name: &str) -> Result<()> {
fn remove<P: AsRef<Path>>(&self, name: P) -> Result<()> {
let r = remove_file(name);
map_io_res!(r)
}

fn remove_dir(&self, dir: &str, recursively: bool) -> Result<()> {
fn remove_dir<P: AsRef<Path>>(&self, dir: P, recursively: bool) -> Result<()> {
let r = if recursively {
remove_dir_all(dir)
} else {
Expand All @@ -64,24 +64,23 @@ impl Storage for FileStorage {
map_io_res!(r)
}

fn exists(&self, name: &str) -> bool {
Path::new(name).exists()
fn exists<P: AsRef<Path>>(&self, name: P) -> bool {
name.as_ref().exists()
}

fn rename(&self, old: &str, new: &str) -> Result<()> {
fn rename<P: AsRef<Path>>(&self, old: P, new: P) -> Result<()> {
map_io_res!(rename(old, new))
}

fn mkdir_all(&self, dir: &str) -> Result<()> {
fn mkdir_all<P: AsRef<Path>>(&self, dir: P) -> Result<()> {
let r = create_dir_all(dir);
map_io_res!(r)
}

fn list(&self, dir: &str) -> Result<Vec<PathBuf>> {
let path = Path::new(dir);
if path.is_dir() {
fn list<P: AsRef<Path>>(&self, dir: P) -> Result<Vec<PathBuf>> {
if dir.as_ref().is_dir() {
let mut v = vec![];
match read_dir(path) {
match read_dir(dir) {
Ok(rd) => {
for entry in rd {
match entry {
Expand Down
Loading

0 comments on commit 29784a0

Please sign in to comment.