Skip to content

Commit 588e85b

Browse files
committed
add w3d6 Task 1: Track Read Set in Get and Write Set
1 parent ad85885 commit 588e85b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

mini-lsm-starter/src/mvcc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl LsmMvccInner {
6464
inner,
6565
local_storage: Arc::new(SkipMap::new()),
6666
committed: Arc::new(AtomicBool::new(false)),
67-
key_hashes: None,
67+
key_hashes: if serializable {Some(Mutex::new((HashSet::new(), HashSet::new())))} else {None},
6868
})
6969
}
7070
}

mini-lsm-starter/src/mvcc/txn.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ pub struct Transaction {
3131

3232
impl Transaction {
3333
pub fn get(&self, key: &[u8]) -> Result<Option<Bytes>> {
34+
if self.committed.load(Ordering::SeqCst) {
35+
panic!("cannot operate on committed txn!");
36+
}
37+
38+
if self.key_hashes.is_some() {
39+
let mut guard = self.key_hashes.as_ref().unwrap().lock();
40+
let (_, read_set) = & mut *guard;
41+
read_set.insert(crc32fast::hash(key));
42+
}
43+
3444
// first probe the local_sotrage
3545
if let Some(entry) = self.local_storage.get(key) {
3646
if entry.value().is_empty() {
@@ -42,6 +52,10 @@ impl Transaction {
4252
}
4353

4454
pub fn scan(self: &Arc<Self>, lower: Bound<&[u8]>, upper: Bound<&[u8]>) -> Result<TxnIterator> {
55+
if self.committed.load(Ordering::SeqCst) {
56+
panic!("cannot operate on committed txn!");
57+
}
58+
4559
let mut local_iter = TxnLocalIteratorBuilder {
4660
map: self.local_storage.clone(),
4761
iter_builder: |map| map.range((map_bound(lower), map_bound(upper))),
@@ -60,11 +74,17 @@ impl Transaction {
6074
}
6175

6276
pub fn put(&self, key: &[u8], value: &[u8]) {
77+
if self.committed.load(Ordering::SeqCst) {
78+
panic!("cannot operate on committed txn!");
79+
}
6380
self.local_storage
6481
.insert(Bytes::copy_from_slice(key), Bytes::copy_from_slice(value));
6582
}
6683

6784
pub fn delete(&self, key: &[u8]) {
85+
if self.committed.load(Ordering::SeqCst) {
86+
panic!("cannot operate on committed txn!");
87+
}
6888
self.local_storage
6989
.insert(Bytes::copy_from_slice(key), Bytes::new());
7090
}

0 commit comments

Comments
 (0)