-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
118 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ edition = "2021" | |
|
||
[dependencies] | ||
bytes = "1.4.0" | ||
anyhow = "1.0.75" | ||
anyhow = "1.0.75" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,60 @@ | ||
use crate::util::env::FileObject; | ||
use crate::memtable::logger::{LogRecordsBuilder, MemTableLogger, OperationType}; | ||
use crate::util::env::{get_global_sequence_number, FileObject}; | ||
use anyhow::Result; | ||
use bytes::Bytes; | ||
use std::collections::BTreeMap; | ||
|
||
mod logger; | ||
|
||
pub struct MemTable {} | ||
pub struct MemTable { | ||
/// table is the sorted searching data structure | ||
/// `key` is the user record key, `value` is the user record value | ||
table: BTreeMap<Bytes, Bytes>, | ||
/// memtable unique sequence number, which represents the related log | ||
/// file number, it is global unique | ||
seq: u64, | ||
logger: MemTableLogger, | ||
} | ||
|
||
impl MemTable { | ||
fn new() -> Self { | ||
todo!() | ||
pub fn new() -> Self { | ||
let seq = get_global_sequence_number(); | ||
MemTable { | ||
table: BTreeMap::new(), | ||
seq, | ||
logger: MemTableLogger::new(seq), | ||
} | ||
} | ||
fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()> { | ||
todo!() | ||
pub fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()> { | ||
// logging and flushing to disk first | ||
let mut log_record_builder = LogRecordsBuilder::new(); | ||
log_record_builder.add(OperationType::PUT, key, value)?; | ||
let ret = self | ||
.logger | ||
.log_and_sync(log_record_builder.build().as_slice()); | ||
|
||
assert!(ret.is_ok()); | ||
self.table | ||
.insert(Bytes::copy_from_slice(key), Bytes::copy_from_slice(value)); | ||
Ok(()) | ||
} | ||
fn get(&self, key: &[u8]) -> Result<Bytes> { | ||
todo!() | ||
pub fn get(&self, key: &[u8]) -> Option<Bytes> { | ||
match self.table.get(key) { | ||
None => None, | ||
Some(value) => Some(value.clone()), | ||
} | ||
} | ||
fn delete(&mut self, key: &[u8]) -> Result<()> { | ||
todo!() | ||
pub fn delete(&mut self, key: &[u8]) -> Result<()> { | ||
self.put(key,"".as_bytes()) | ||
} | ||
fn recover(file: &FileObject) -> Self { | ||
pub fn recover(file: &FileObject) -> Self { | ||
todo!() | ||
} | ||
|
||
pub fn seq_num(&self) -> u64 { | ||
self.seq | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::memtable::MemTable; | ||
use crate::util::env::logfile_path; | ||
|
||
struct RAII { | ||
pub seq: u64, | ||
} | ||
|
||
impl Drop for RAII { | ||
fn drop(&mut self) { | ||
std::fs::remove_file(logfile_path(self.seq as usize)).expect("Testing expect"); | ||
} | ||
} | ||
#[test] | ||
fn test_memtable_put() { | ||
let mut memtable = MemTable::new(); | ||
let raii = RAII { | ||
seq: memtable.seq_num(), | ||
}; | ||
let ret = memtable.put("key1".as_bytes(), "value1".as_bytes()); | ||
assert!(ret.is_ok()) | ||
} | ||
#[test] | ||
fn test_memtable_get() { | ||
let mut memtable = MemTable::new(); | ||
let raii = RAII { | ||
seq: memtable.seq_num(), | ||
}; | ||
let ret = memtable.put("key1".as_bytes(), "value1".as_bytes()); | ||
assert!(ret.is_ok()); | ||
assert_eq!(memtable.get("key1".as_bytes()),Some("value1".into())); | ||
assert_eq!(memtable.get("key".as_bytes()),None); | ||
} | ||
#[test] | ||
fn test_memtable_remove() { | ||
let mut memtable = MemTable::new(); | ||
let raii = RAII { | ||
seq: memtable.seq_num(), | ||
}; | ||
let ret = memtable.put("key1".as_bytes(), "value1".as_bytes()); | ||
assert!(ret.is_ok()); | ||
assert_eq!(memtable.get("key1".as_bytes()),Some("value1".into())); | ||
assert_eq!(memtable.get("key".as_bytes()),None); | ||
let ret = memtable.delete("key1".as_bytes()); | ||
assert!(ret.is_ok()); | ||
assert_eq!(memtable.get("key1".as_bytes()),None); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters