From 751871214184722eb0b861705cde32312cbeb0da Mon Sep 17 00:00:00 2001 From: pedrinfx Date: Tue, 24 Jan 2023 19:43:16 -0300 Subject: [PATCH] fix: replacing string type to `&path` --- README.md | 2 +- src/dustdata.rs | 6 ++---- src/lib.rs | 2 +- src/storage/lsm/filter/mod.rs | 17 ++++++++++------- src/storage/lsm/index/mod.rs | 12 ++++++------ src/storage/lsm/mod.rs | 16 +++++++--------- src/storage/lsm/sstable.rs | 10 +++++----- 7 files changed, 32 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index dfa31ea..84d9323 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Initialize a DustData interface. ```rust // DustData Configuration let config = dustdata::DustDataConfig { - path: "./test_data".to_string(), + path: std::path::Path::new("./test_data/dustdata").to_path_buf(), lsm_config: dustdata::LsmConfig { flush_threshold: dustdata::Size::Megabytes(128), } diff --git a/src/dustdata.rs b/src/dustdata.rs index f2182b3..1660fd4 100644 --- a/src/dustdata.rs +++ b/src/dustdata.rs @@ -20,7 +20,7 @@ pub struct LsmConfig { /// * `lsm_config` - The LSM configuration #[derive(Clone)] pub struct DustDataConfig { - pub path: String, + pub path: path::PathBuf, pub lsm_config: LsmConfig, } @@ -146,11 +146,9 @@ impl std::fmt::Display for ErrorCode { impl DustData { pub fn new(configuration: DustDataConfig) -> Self { - let path = path::Path::new(&configuration.path); - let lsm = storage::lsm::Lsm::new(lsm::LsmConfig { flush_threshold: size_to_usize(configuration.clone().lsm_config.flush_threshold), - sstable_path: path.to_str().unwrap().to_string(), + sstable_path: configuration.clone().path, }); Self { diff --git a/src/lib.rs b/src/lib.rs index 2b1ac9f..7cbadd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ mod dustdata_tests { fn get_default_config() -> DustDataConfig { DustDataConfig { - path: "./test_data/dustdata".to_string(), + path: std::path::Path::new("./test_data/dustdata").to_path_buf(), lsm_config: LsmConfig { flush_threshold: Size::Megabytes(128), }, diff --git a/src/storage/lsm/filter/mod.rs b/src/storage/lsm/filter/mod.rs index 7035840..77b513e 100644 --- a/src/storage/lsm/filter/mod.rs +++ b/src/storage/lsm/filter/mod.rs @@ -1,15 +1,18 @@ use crate::bloom::BloomFilter; use lz4::{Decoder, EncoderBuilder}; -use std::io::{Read, Write}; +use std::{ + io::{Read, Write}, + path, +}; -pub fn check_if_filter_exists(path: &str) -> bool { - let _path = std::path::Path::new(path).join("filter"); +pub fn check_if_filter_exists(path: &path::Path) -> bool { + let _path = path.join("filter"); _path.exists() } -pub fn write_filter(path: &str, filter: &BloomFilter) { - let _path = std::path::Path::new(path).join("filter"); +pub fn write_filter(path: &path::Path, filter: &BloomFilter) { + let _path = path.join("filter"); if !check_if_filter_exists(path) { std::fs::create_dir_all(_path.clone()).unwrap(); @@ -36,8 +39,8 @@ pub fn write_filter(path: &str, filter: &BloomFilter) { hashes_file.sync_all().unwrap(); } -pub fn read_filter(path: &str) -> BloomFilter { - let _path = std::path::Path::new(path).join("filter"); +pub fn read_filter(path: &path::Path) -> BloomFilter { + let _path = path.join("filter"); // bitvec diff --git a/src/storage/lsm/index/mod.rs b/src/storage/lsm/index/mod.rs index 9476113..a8128fe 100644 --- a/src/storage/lsm/index/mod.rs +++ b/src/storage/lsm/index/mod.rs @@ -5,14 +5,14 @@ use std::{ path, }; -pub fn check_if_index_exists(path: &str) -> bool { - let _path = path::Path::new(path).join("index"); +pub fn check_if_index_exists(path: &path::Path) -> bool { + let _path = path.join("index"); _path.exists() } -pub fn write_index(path: &str, index: &HashMap) { - let _path = path::Path::new(path).join("index"); +pub fn write_index(path: &path::Path, index: &HashMap) { + let _path = path.join("index"); if index.is_empty() { return; @@ -26,8 +26,8 @@ pub fn write_index(path: &str, index: &HashMap) { file.flush().unwrap(); } -pub fn read_index(path: &str) -> HashMap { - let _path = path::Path::new(path).join("index"); +pub fn read_index(path: &path::Path) -> HashMap { + let _path = path.join("index"); let mut file = fs::File::open(_path).unwrap(); let mut bytes_to_read: Vec = Vec::new(); diff --git a/src/storage/lsm/mod.rs b/src/storage/lsm/mod.rs index d5bab12..dcba21c 100644 --- a/src/storage/lsm/mod.rs +++ b/src/storage/lsm/mod.rs @@ -18,7 +18,7 @@ mod writer; #[derive(Clone, Debug)] pub struct LsmConfig { pub flush_threshold: usize, - pub sstable_path: String, + pub sstable_path: path::PathBuf, } #[derive(Clone)] @@ -99,7 +99,7 @@ impl Lsm { let offset = dense_index.get(&key.to_string()).unwrap(); Ok(sstable::Segment::read_with_offset( offset.to_string(), - self.lsm_config.sstable_path.to_string(), + &self.lsm_config.sstable_path, )) } } @@ -164,8 +164,7 @@ impl Lsm { let mut dense_index = self.dense_index.lock().unwrap(); - let segments = - sstable::Segment::from_tree(&memtable, self.lsm_config.sstable_path.as_str()); + let segments = sstable::Segment::from_tree(&memtable, &self.lsm_config.sstable_path); for token in segments.1 { dense_index.insert(token.0, token.1); @@ -230,9 +229,9 @@ impl Lsm { } pub fn load_snapshot(path: path::PathBuf, snapshot: Snapshot) { - sstable::Segment::from_tree(snapshot.get_memtable(), &path.display().to_string()); - index::write_index(&path.display().to_string(), snapshot.get_dense_index()); - filter::write_filter(&path.display().to_string(), snapshot.get_bloom_filter()); + sstable::Segment::from_tree(snapshot.get_memtable(), &path); + index::write_index(&path, snapshot.get_dense_index()); + filter::write_filter(&path, snapshot.get_bloom_filter()); } } @@ -245,8 +244,7 @@ impl Drop for Lsm { return; } - let segments = - sstable::Segment::from_tree(memtable.deref(), self.lsm_config.sstable_path.as_str()); + let segments = sstable::Segment::from_tree(memtable.deref(), &self.lsm_config.sstable_path); for token in segments.1 { dense_index.insert(token.0, token.1); diff --git a/src/storage/lsm/sstable.rs b/src/storage/lsm/sstable.rs index 956b82f..62ddadb 100644 --- a/src/storage/lsm/sstable.rs +++ b/src/storage/lsm/sstable.rs @@ -70,8 +70,8 @@ pub struct Segment { } impl Segment { - pub fn new(path: &str) -> Self { - let _path = path::Path::new(path).join("data"); + pub fn new(path: &path::Path) -> Self { + let _path = path.join("data"); if !_path.exists() { fs::create_dir_all(_path.clone()).unwrap(); @@ -89,12 +89,12 @@ impl Segment { } } - pub fn read_with_offset(offset: String, path: String) -> Option { + pub fn read_with_offset(offset: String, path: &path::Path) -> Option { let splited_offset = offset.split('_').collect::>(); let file_index = splited_offset[0].parse::().unwrap(); let offset = splited_offset[1].parse::().unwrap(); - let path = path::Path::new(&path).join("data"); + let path = path.join("data"); let file_path = path.join(get_file_that_starts_with_index( (*path).to_path_buf(), file_index as usize, @@ -126,7 +126,7 @@ impl Segment { pub fn from_tree( tree: &BTreeMap, - path: &str, + path: &path::Path, ) -> (Segment, Vec<(String, String)>) { let mut segment = Segment::new(path);