Skip to content

Commit 4cea1a9

Browse files
authored
Merge pull request #22 from rustbase/feature/rwlock
feat: replacing mutex to rwlock
2 parents 7d7c736 + a199882 commit 4cea1a9

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

src/storage/lsm/mod.rs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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};
@@ -23,12 +23,12 @@ pub struct LsmConfig {
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,12 +93,12 @@ 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(),
@@ -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,7 +165,7 @@ 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

167170
let segments = sstable::Segment::from_tree(&memtable, &self.lsm_config.sstable_path);
168171

@@ -182,41 +185,41 @@ impl Lsm {
182185

183186
filter::write_filter(
184187
&self.lsm_config.sstable_path,
185-
self.bloom_filter.lock().unwrap().deref(),
188+
self.bloom_filter.read().unwrap().deref(),
186189
);
187190

188-
self.memtable.lock().unwrap().clear();
191+
self.memtable.write().unwrap().clear();
189192
self.memtable_size = 0;
190193

191194
Ok(())
192195
}
193196

194197
pub fn get_memtable(&self) -> BTreeMap<String, bson::Bson> {
195-
self.memtable.lock().unwrap().clone()
198+
self.memtable.read().unwrap().clone()
196199
}
197200

198201
pub fn contains(&self, key: &str) -> bool {
199-
self.bloom_filter.lock().unwrap().contains(key)
202+
self.bloom_filter.read().unwrap().contains(key)
200203
}
201204

202205
pub fn clear(&self) {
203-
self.memtable.lock().unwrap().clear();
204-
self.dense_index.lock().unwrap().clear();
206+
self.memtable.write().unwrap().clear();
207+
self.dense_index.write().unwrap().clear();
205208
}
206209

207210
pub fn update_index(&self) {
208-
let index = self.dense_index.lock().unwrap().clone();
211+
let index = self.dense_index.read().unwrap().clone();
209212
index::write_index(&self.lsm_config.sstable_path, &index);
210213
}
211214

212215
pub fn list_keys(&self) -> Vec<String> {
213216
let mut keys = Vec::new();
214217

215-
for key in self.memtable.lock().unwrap().keys() {
218+
for key in self.memtable.read().unwrap().keys() {
216219
keys.push(key.clone());
217220
}
218221

219-
for key in self.dense_index.lock().unwrap().keys() {
222+
for key in self.dense_index.read().unwrap().keys() {
220223
keys.push(key.clone());
221224
}
222225

@@ -225,7 +228,7 @@ impl Lsm {
225228

226229
pub fn drop(&mut self) {
227230
self.clear();
228-
self.bloom_filter.lock().unwrap().clear();
231+
self.bloom_filter.write().unwrap().clear();
229232
}
230233

231234
pub fn load_snapshot(path: path::PathBuf, snapshot: Snapshot) {
@@ -237,8 +240,8 @@ impl Lsm {
237240

238241
impl Drop for Lsm {
239242
fn drop(&mut self) {
240-
let memtable = self.memtable.lock().unwrap();
241-
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();
242245

243246
if memtable.is_empty() {
244247
return;
@@ -260,7 +263,7 @@ impl Drop for Lsm {
260263

261264
filter::write_filter(
262265
&self.lsm_config.sstable_path,
263-
self.bloom_filter.lock().unwrap().deref(),
266+
self.bloom_filter.read().unwrap().deref(),
264267
);
265268
}
266269
}

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)