finish 2.7

Signed-off-by: Alex Chi <iskyzh@gmail.com>
This commit is contained in:
Alex Chi
2024-01-28 14:08:08 +08:00
parent b96479384c
commit b4485f49c3
15 changed files with 165 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
use std::fs::{File, OpenOptions};
use std::hash::Hasher;
use std::io::{Read, Write};
use std::io::{BufWriter, Read, Write};
use std::path::Path;
use std::sync::Arc;
@@ -12,20 +12,20 @@ use parking_lot::Mutex;
use crate::key::{KeyBytes, KeySlice};
pub struct Wal {
file: Arc<Mutex<File>>,
file: Arc<Mutex<BufWriter<File>>>,
}
impl Wal {
pub fn create(path: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
file: Arc::new(Mutex::new(
file: Arc::new(Mutex::new(BufWriter::new(
OpenOptions::new()
.read(true)
.create_new(true)
.write(true)
.open(path)
.context("failed to create WAL")?,
)),
))),
})
}
@@ -60,7 +60,7 @@ impl Wal {
skiplist.insert(KeyBytes::from_bytes_with_ts(key, ts), value);
}
Ok(Self {
file: Arc::new(Mutex::new(file)),
file: Arc::new(Mutex::new(BufWriter::new(file))),
})
}
@@ -86,8 +86,9 @@ impl Wal {
}
pub fn sync(&self) -> Result<()> {
let file = self.file.lock();
file.sync_all()?;
let mut file = self.file.lock();
file.flush()?;
file.get_mut().sync_all()?;
Ok(())
}
}