fix: ensure WAL is atomic for each write batch (#84)
Signed-off-by: Alex Chi <iskyzh@gmail.com>
This commit is contained in:
@@ -122,16 +122,25 @@ impl MemTable {
|
||||
///
|
||||
/// In week 1, day 1, simply put the key-value pair into the skipmap.
|
||||
/// In week 2, day 6, also flush the data to WAL.
|
||||
/// In week 3, day 5, modify the function to use the batch API.
|
||||
pub fn put(&self, key: KeySlice, value: &[u8]) -> Result<()> {
|
||||
let estimated_size = key.raw_len() + value.len();
|
||||
self.map.insert(
|
||||
key.to_key_vec().into_key_bytes(),
|
||||
Bytes::copy_from_slice(value),
|
||||
);
|
||||
self.put_batch(&[(key, value)])
|
||||
}
|
||||
|
||||
/// Implement this in week 3, day 5.
|
||||
pub fn put_batch(&self, data: &[(KeySlice, &[u8])]) -> Result<()> {
|
||||
let mut estimated_size = 0;
|
||||
for (key, value) in data {
|
||||
estimated_size += key.raw_len() + value.len();
|
||||
self.map.insert(
|
||||
key.to_key_vec().into_key_bytes(),
|
||||
Bytes::copy_from_slice(value),
|
||||
);
|
||||
}
|
||||
self.approximate_size
|
||||
.fetch_add(estimated_size, std::sync::atomic::Ordering::Relaxed);
|
||||
if let Some(ref wal) = self.wal {
|
||||
wal.put(key, value)?;
|
||||
wal.put_batch(data)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user