patch memtable and add ts for wal
Signed-off-by: Alex Chi <iskyzh@gmail.com>
This commit is contained in:
@@ -9,6 +9,8 @@ use bytes::{Buf, BufMut, Bytes};
|
||||
use crossbeam_skiplist::SkipMap;
|
||||
use parking_lot::Mutex;
|
||||
|
||||
use crate::key::{KeyBytes, KeySlice};
|
||||
|
||||
pub struct Wal {
|
||||
file: Arc<Mutex<File>>,
|
||||
}
|
||||
@@ -27,7 +29,7 @@ impl Wal {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn recover(path: impl AsRef<Path>, skiplist: &SkipMap<Bytes, Bytes>) -> Result<Self> {
|
||||
pub fn recover(path: impl AsRef<Path>, skiplist: &SkipMap<KeyBytes, Bytes>) -> Result<Self> {
|
||||
let path = path.as_ref();
|
||||
let mut file = OpenOptions::new()
|
||||
.read(true)
|
||||
@@ -44,6 +46,8 @@ impl Wal {
|
||||
let key = Bytes::copy_from_slice(&rbuf[..key_len]);
|
||||
hasher.write(&key);
|
||||
rbuf.advance(key_len);
|
||||
let ts = rbuf.get_u64();
|
||||
hasher.write_u64(ts);
|
||||
let value_len = rbuf.get_u16() as usize;
|
||||
hasher.write_u16(value_len as u16);
|
||||
let value = Bytes::copy_from_slice(&rbuf[..value_len]);
|
||||
@@ -53,22 +57,24 @@ impl Wal {
|
||||
if hasher.finalize() != checksum {
|
||||
bail!("checksum mismatch");
|
||||
}
|
||||
skiplist.insert(key, value);
|
||||
skiplist.insert(KeyBytes::from_bytes_with_ts(key, ts), value);
|
||||
}
|
||||
Ok(Self {
|
||||
file: Arc::new(Mutex::new(file)),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
pub fn put(&self, key: KeySlice, value: &[u8]) -> Result<()> {
|
||||
let mut file = self.file.lock();
|
||||
let mut buf: Vec<u8> =
|
||||
Vec::with_capacity(key.len() + value.len() + std::mem::size_of::<u16>());
|
||||
Vec::with_capacity(key.raw_len() + value.len() + std::mem::size_of::<u16>());
|
||||
let mut hasher = crc32fast::Hasher::new();
|
||||
hasher.write_u16(key.len() as u16);
|
||||
buf.put_u16(key.len() as u16);
|
||||
hasher.write(key);
|
||||
buf.put_slice(key);
|
||||
hasher.write_u16(key.key_len() as u16);
|
||||
buf.put_u16(key.key_len() as u16);
|
||||
hasher.write(key.key_ref());
|
||||
buf.put_slice(key.key_ref());
|
||||
hasher.write_u64(key.ts());
|
||||
buf.put_u64(key.ts());
|
||||
hasher.write_u16(value.len() as u16);
|
||||
buf.put_u16(value.len() as u16);
|
||||
buf.put_slice(value);
|
||||
|
||||
Reference in New Issue
Block a user