Skip to content

Commit 7d7c736

Browse files
authored
Merge pull request #21 from rustbase/bugfix/path
fix: replacing string type to `&path`
2 parents eb77992 + 7518712 commit 7d7c736

7 files changed

Lines changed: 32 additions & 33 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Initialize a DustData interface.
3636
```rust
3737
// DustData Configuration
3838
let config = dustdata::DustDataConfig {
39-
path: "./test_data".to_string(),
39+
path: std::path::Path::new("./test_data/dustdata").to_path_buf(),
4040
lsm_config: dustdata::LsmConfig {
4141
flush_threshold: dustdata::Size::Megabytes(128),
4242
}

src/dustdata.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct LsmConfig {
2020
/// * `lsm_config` - The LSM configuration
2121
#[derive(Clone)]
2222
pub struct DustDataConfig {
23-
pub path: String,
23+
pub path: path::PathBuf,
2424
pub lsm_config: LsmConfig,
2525
}
2626

@@ -146,11 +146,9 @@ impl std::fmt::Display for ErrorCode {
146146

147147
impl DustData {
148148
pub fn new(configuration: DustDataConfig) -> Self {
149-
let path = path::Path::new(&configuration.path);
150-
151149
let lsm = storage::lsm::Lsm::new(lsm::LsmConfig {
152150
flush_threshold: size_to_usize(configuration.clone().lsm_config.flush_threshold),
153-
sstable_path: path.to_str().unwrap().to_string(),
151+
sstable_path: configuration.clone().path,
154152
});
155153

156154
Self {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mod dustdata_tests {
2323

2424
fn get_default_config() -> DustDataConfig {
2525
DustDataConfig {
26-
path: "./test_data/dustdata".to_string(),
26+
path: std::path::Path::new("./test_data/dustdata").to_path_buf(),
2727
lsm_config: LsmConfig {
2828
flush_threshold: Size::Megabytes(128),
2929
},

src/storage/lsm/filter/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
use crate::bloom::BloomFilter;
22
use lz4::{Decoder, EncoderBuilder};
3-
use std::io::{Read, Write};
3+
use std::{
4+
io::{Read, Write},
5+
path,
6+
};
47

5-
pub fn check_if_filter_exists(path: &str) -> bool {
6-
let _path = std::path::Path::new(path).join("filter");
8+
pub fn check_if_filter_exists(path: &path::Path) -> bool {
9+
let _path = path.join("filter");
710

811
_path.exists()
912
}
1013

11-
pub fn write_filter(path: &str, filter: &BloomFilter) {
12-
let _path = std::path::Path::new(path).join("filter");
14+
pub fn write_filter(path: &path::Path, filter: &BloomFilter) {
15+
let _path = path.join("filter");
1316

1417
if !check_if_filter_exists(path) {
1518
std::fs::create_dir_all(_path.clone()).unwrap();
@@ -36,8 +39,8 @@ pub fn write_filter(path: &str, filter: &BloomFilter) {
3639
hashes_file.sync_all().unwrap();
3740
}
3841

39-
pub fn read_filter(path: &str) -> BloomFilter {
40-
let _path = std::path::Path::new(path).join("filter");
42+
pub fn read_filter(path: &path::Path) -> BloomFilter {
43+
let _path = path.join("filter");
4144

4245
// bitvec
4346

src/storage/lsm/index/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ use std::{
55
path,
66
};
77

8-
pub fn check_if_index_exists(path: &str) -> bool {
9-
let _path = path::Path::new(path).join("index");
8+
pub fn check_if_index_exists(path: &path::Path) -> bool {
9+
let _path = path.join("index");
1010

1111
_path.exists()
1212
}
1313

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

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

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

3232
let mut file = fs::File::open(_path).unwrap();
3333
let mut bytes_to_read: Vec<u8> = Vec::new();

src/storage/lsm/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod writer;
1818
#[derive(Clone, Debug)]
1919
pub struct LsmConfig {
2020
pub flush_threshold: usize,
21-
pub sstable_path: String,
21+
pub sstable_path: path::PathBuf,
2222
}
2323

2424
#[derive(Clone)]
@@ -99,7 +99,7 @@ impl Lsm {
9999
let offset = dense_index.get(&key.to_string()).unwrap();
100100
Ok(sstable::Segment::read_with_offset(
101101
offset.to_string(),
102-
self.lsm_config.sstable_path.to_string(),
102+
&self.lsm_config.sstable_path,
103103
))
104104
}
105105
}
@@ -164,8 +164,7 @@ impl Lsm {
164164

165165
let mut dense_index = self.dense_index.lock().unwrap();
166166

167-
let segments =
168-
sstable::Segment::from_tree(&memtable, self.lsm_config.sstable_path.as_str());
167+
let segments = sstable::Segment::from_tree(&memtable, &self.lsm_config.sstable_path);
169168

170169
for token in segments.1 {
171170
dense_index.insert(token.0, token.1);
@@ -230,9 +229,9 @@ impl Lsm {
230229
}
231230

232231
pub fn load_snapshot(path: path::PathBuf, snapshot: Snapshot) {
233-
sstable::Segment::from_tree(snapshot.get_memtable(), &path.display().to_string());
234-
index::write_index(&path.display().to_string(), snapshot.get_dense_index());
235-
filter::write_filter(&path.display().to_string(), snapshot.get_bloom_filter());
232+
sstable::Segment::from_tree(snapshot.get_memtable(), &path);
233+
index::write_index(&path, snapshot.get_dense_index());
234+
filter::write_filter(&path, snapshot.get_bloom_filter());
236235
}
237236
}
238237

@@ -245,8 +244,7 @@ impl Drop for Lsm {
245244
return;
246245
}
247246

248-
let segments =
249-
sstable::Segment::from_tree(memtable.deref(), self.lsm_config.sstable_path.as_str());
247+
let segments = sstable::Segment::from_tree(memtable.deref(), &self.lsm_config.sstable_path);
250248

251249
for token in segments.1 {
252250
dense_index.insert(token.0, token.1);

src/storage/lsm/sstable.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ pub struct Segment {
7070
}
7171

7272
impl Segment {
73-
pub fn new(path: &str) -> Self {
74-
let _path = path::Path::new(path).join("data");
73+
pub fn new(path: &path::Path) -> Self {
74+
let _path = path.join("data");
7575

7676
if !_path.exists() {
7777
fs::create_dir_all(_path.clone()).unwrap();
@@ -89,12 +89,12 @@ impl Segment {
8989
}
9090
}
9191

92-
pub fn read_with_offset(offset: String, path: String) -> Option<bson::Bson> {
92+
pub fn read_with_offset(offset: String, path: &path::Path) -> Option<bson::Bson> {
9393
let splited_offset = offset.split('_').collect::<Vec<&str>>();
9494
let file_index = splited_offset[0].parse::<u64>().unwrap();
9595
let offset = splited_offset[1].parse::<u64>().unwrap();
9696

97-
let path = path::Path::new(&path).join("data");
97+
let path = path.join("data");
9898
let file_path = path.join(get_file_that_starts_with_index(
9999
(*path).to_path_buf(),
100100
file_index as usize,
@@ -126,7 +126,7 @@ impl Segment {
126126

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

0 commit comments

Comments
 (0)