Skip to content

Commit 12241a0

Browse files
committed
Merge branch 'release/v1.3.1' into stable
2 parents 1454f01 + 9bbc5c1 commit 12241a0

11 files changed

Lines changed: 78 additions & 76 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dustdata"
3-
version = "1.3.0"
3+
version = "1.3.1"
44
edition = "2021"
55
description = "A data concurrency control storage engine to Rustbase"
66
repository = "https://github.com/rustbase/dustdata"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Add the following to your `Cargo.toml`:
2626

2727
```toml
2828
[dependencies]
29-
dustdata = "1.3.0"
29+
dustdata = "1.3.1"
3030
```
3131

3232
# Usage
@@ -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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
mod bloom;
1+
pub mod bloom;
22
mod dustdata;
3-
mod storage;
3+
pub mod storage;
44

55
pub use self::{
66
dustdata::DustData, dustdata::DustDataConfig, dustdata::Error, dustdata::ErrorCode,
@@ -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: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ use std::collections::{BTreeMap, HashMap};
22
use std::mem;
33
use std::ops::Deref;
44
use std::path;
5-
use std::sync::{Arc, Mutex};
5+
use std::sync::{Arc, RwLock};
66

77
use crate::bloom::BloomFilter;
88
use crate::dustdata::{Error, ErrorCode, Result};
99

1010
use self::snapshots::Snapshot;
1111

12-
mod filter;
13-
mod index;
12+
pub mod filter;
13+
pub mod index;
1414
pub mod snapshots;
15-
mod sstable;
15+
pub mod sstable;
1616
mod writer;
1717

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)]
2525
pub struct Lsm {
26-
pub memtable: Arc<Mutex<BTreeMap<String, bson::Bson>>>,
26+
pub memtable: Arc<RwLock<BTreeMap<String, bson::Bson>>>,
2727
pub memtable_size: usize,
2828
pub lsm_config: LsmConfig,
2929
pub snapshots: snapshots::SnapshotManager,
30-
pub dense_index: Arc<Mutex<HashMap<String, String>>>,
31-
pub bloom_filter: Arc<Mutex<BloomFilter>>,
30+
pub dense_index: Arc<RwLock<HashMap<String, String>>>,
31+
pub bloom_filter: Arc<RwLock<BloomFilter>>,
3232
}
3333

3434
impl Lsm {
@@ -56,9 +56,9 @@ impl Lsm {
5656
);
5757

5858
Lsm {
59-
memtable: Arc::new(Mutex::new(BTreeMap::new())),
60-
bloom_filter: Arc::new(Mutex::new(bloom_filter)),
61-
dense_index: Arc::new(Mutex::new(index)),
59+
memtable: Arc::new(RwLock::new(BTreeMap::new())),
60+
bloom_filter: Arc::new(RwLock::new(bloom_filter)),
61+
dense_index: Arc::new(RwLock::new(index)),
6262
lsm_config: config,
6363
memtable_size: 0, // The current memtable size (in bytes)
6464
snapshots,
@@ -75,8 +75,11 @@ impl Lsm {
7575

7676
self.memtable_size += mem::size_of_val(&value);
7777

78-
self.memtable.lock().unwrap().insert(key.to_string(), value);
79-
self.bloom_filter.lock().unwrap().insert(key);
78+
self.memtable
79+
.write()
80+
.unwrap()
81+
.insert(key.to_string(), value);
82+
self.bloom_filter.write().unwrap().insert(key);
8083

8184
if self.memtable_size >= self.lsm_config.flush_threshold {
8285
self.flush().unwrap();
@@ -90,16 +93,16 @@ impl Lsm {
9093
return Ok(None);
9194
}
9295

93-
let memtable = self.memtable.lock().unwrap();
96+
let memtable = self.memtable.read().unwrap();
9497

9598
match memtable.get(&key.to_string()) {
9699
Some(document) => Ok(Some(document.clone())),
97100
None => {
98-
let dense_index = self.dense_index.lock().unwrap();
101+
let dense_index = self.dense_index.read().unwrap();
99102
let offset = dense_index.get(&key.to_string()).unwrap();
100103
Ok(sstable::Segment::read_with_offset(
101104
offset.to_string(),
102-
self.lsm_config.sstable_path.to_string(),
105+
&self.lsm_config.sstable_path,
103106
))
104107
}
105108
}
@@ -113,17 +116,17 @@ impl Lsm {
113116
});
114117
}
115118

116-
let mut memtable = self.memtable.lock().unwrap();
119+
let mut memtable = self.memtable.write().unwrap();
117120

118121
if memtable.contains_key(&key.to_string()) {
119122
memtable.remove(&key.to_string());
120123

121124
drop(memtable);
122125
} else {
123-
self.dense_index.lock().unwrap().remove(&key.to_string());
126+
self.dense_index.write().unwrap().remove(&key.to_string());
124127
}
125128

126-
self.bloom_filter.lock().unwrap().delete(key);
129+
self.bloom_filter.write().unwrap().delete(key);
127130

128131
Ok(())
129132
}
@@ -136,13 +139,13 @@ impl Lsm {
136139
});
137140
}
138141

139-
let mut memtable = self.memtable.lock().unwrap();
140-
let mut bloom_filter = self.bloom_filter.lock().unwrap();
142+
let mut memtable = self.memtable.write().unwrap();
143+
let mut bloom_filter = self.bloom_filter.write().unwrap();
141144

142145
// Delete the old value from the bloom filter
143146
bloom_filter.delete(key);
144147

145-
let mut dense_index = self.dense_index.lock().unwrap();
148+
let mut dense_index = self.dense_index.write().unwrap();
146149
dense_index.remove(&key.to_string());
147150
drop(dense_index);
148151

@@ -162,10 +165,9 @@ impl Lsm {
162165
return Ok(());
163166
}
164167

165-
let mut dense_index = self.dense_index.lock().unwrap();
168+
let mut dense_index = self.dense_index.write().unwrap();
166169

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

170172
for token in segments.1 {
171173
dense_index.insert(token.0, token.1);
@@ -183,41 +185,41 @@ impl Lsm {
183185

184186
filter::write_filter(
185187
&self.lsm_config.sstable_path,
186-
self.bloom_filter.lock().unwrap().deref(),
188+
self.bloom_filter.read().unwrap().deref(),
187189
);
188190

189-
self.memtable.lock().unwrap().clear();
191+
self.memtable.write().unwrap().clear();
190192
self.memtable_size = 0;
191193

192194
Ok(())
193195
}
194196

195197
pub fn get_memtable(&self) -> BTreeMap<String, bson::Bson> {
196-
self.memtable.lock().unwrap().clone()
198+
self.memtable.read().unwrap().clone()
197199
}
198200

199201
pub fn contains(&self, key: &str) -> bool {
200-
self.bloom_filter.lock().unwrap().contains(key)
202+
self.bloom_filter.read().unwrap().contains(key)
201203
}
202204

203205
pub fn clear(&self) {
204-
self.memtable.lock().unwrap().clear();
205-
self.dense_index.lock().unwrap().clear();
206+
self.memtable.write().unwrap().clear();
207+
self.dense_index.write().unwrap().clear();
206208
}
207209

208210
pub fn update_index(&self) {
209-
let index = self.dense_index.lock().unwrap().clone();
211+
let index = self.dense_index.read().unwrap().clone();
210212
index::write_index(&self.lsm_config.sstable_path, &index);
211213
}
212214

213215
pub fn list_keys(&self) -> Vec<String> {
214216
let mut keys = Vec::new();
215217

216-
for key in self.memtable.lock().unwrap().keys() {
218+
for key in self.memtable.read().unwrap().keys() {
217219
keys.push(key.clone());
218220
}
219221

220-
for key in self.dense_index.lock().unwrap().keys() {
222+
for key in self.dense_index.read().unwrap().keys() {
221223
keys.push(key.clone());
222224
}
223225

@@ -226,27 +228,26 @@ impl Lsm {
226228

227229
pub fn drop(&mut self) {
228230
self.clear();
229-
self.bloom_filter.lock().unwrap().clear();
231+
self.bloom_filter.write().unwrap().clear();
230232
}
231233

232234
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());
235+
sstable::Segment::from_tree(snapshot.get_memtable(), &path);
236+
index::write_index(&path, snapshot.get_dense_index());
237+
filter::write_filter(&path, snapshot.get_bloom_filter());
236238
}
237239
}
238240

239241
impl Drop for Lsm {
240242
fn drop(&mut self) {
241-
let memtable = self.memtable.lock().unwrap();
242-
let mut dense_index = self.dense_index.lock().unwrap();
243+
let memtable = self.memtable.read().unwrap();
244+
let mut dense_index = self.dense_index.write().unwrap();
243245

244246
if memtable.is_empty() {
245247
return;
246248
}
247249

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

251252
for token in segments.1 {
252253
dense_index.insert(token.0, token.1);
@@ -262,7 +263,7 @@ impl Drop for Lsm {
262263

263264
filter::write_filter(
264265
&self.lsm_config.sstable_path,
265-
self.bloom_filter.lock().unwrap().deref(),
266+
self.bloom_filter.read().unwrap().deref(),
266267
);
267268
}
268269
}

src/storage/lsm/snapshots/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ impl Snapshot {
8787
}
8888

8989
let snapshot = Snapshot::new(
90-
lsm.memtable.lock().unwrap().clone(),
91-
lsm.bloom_filter.lock().unwrap().clone(),
92-
lsm.dense_index.lock().unwrap().clone(),
90+
lsm.memtable.read().unwrap().clone(),
91+
lsm.bloom_filter.read().unwrap().clone(),
92+
lsm.dense_index.read().unwrap().clone(),
9393
);
9494

9595
let now = chrono::Local::now();

0 commit comments

Comments
 (0)