Skip to content

Commit

Permalink
fix: replacing string type to &path
Browse files Browse the repository at this point in the history
  • Loading branch information
peeeuzin committed Jan 24, 2023
1 parent 4be3488 commit 7518712
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
6 changes: 2 additions & 4 deletions src/dustdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
17 changes: 10 additions & 7 deletions src/storage/lsm/filter/mod.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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

Expand Down
12 changes: 6 additions & 6 deletions src/storage/lsm/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>) {
let _path = path::Path::new(path).join("index");
pub fn write_index(path: &path::Path, index: &HashMap<String, String>) {
let _path = path.join("index");

if index.is_empty() {
return;
Expand All @@ -26,8 +26,8 @@ pub fn write_index(path: &str, index: &HashMap<String, String>) {
file.flush().unwrap();
}

pub fn read_index(path: &str) -> HashMap<String, String> {
let _path = path::Path::new(path).join("index");
pub fn read_index(path: &path::Path) -> HashMap<String, String> {
let _path = path.join("index");

let mut file = fs::File::open(_path).unwrap();
let mut bytes_to_read: Vec<u8> = Vec::new();
Expand Down
16 changes: 7 additions & 9 deletions src/storage/lsm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
))
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/storage/lsm/sstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -89,12 +89,12 @@ impl Segment {
}
}

pub fn read_with_offset(offset: String, path: String) -> Option<bson::Bson> {
pub fn read_with_offset(offset: String, path: &path::Path) -> Option<bson::Bson> {
let splited_offset = offset.split('_').collect::<Vec<&str>>();
let file_index = splited_offset[0].parse::<u64>().unwrap();
let offset = splited_offset[1].parse::<u64>().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,
Expand Down Expand Up @@ -126,7 +126,7 @@ impl Segment {

pub fn from_tree(
tree: &BTreeMap<String, bson::Bson>,
path: &str,
path: &path::Path,
) -> (Segment, Vec<(String, String)>) {
let mut segment = Segment::new(path);

Expand Down

0 comments on commit 7518712

Please sign in to comment.