-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from Gifted-s/ft/file_sync
[Feat] Data Block, Compression, LSM Entry
- Loading branch information
Showing
14 changed files
with
500 additions
and
57 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
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,30 @@ | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
#[allow(clippy::module_name_repetitions)] | ||
pub enum CompressionType { | ||
Lz4, | ||
} | ||
|
||
impl From<CompressionType> for u8 { | ||
fn from(val: CompressionType) -> Self { | ||
match val { | ||
CompressionType::Lz4 => 1, | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<u8> for CompressionType { | ||
type Error = (); | ||
|
||
fn try_from(value: u8) -> Result<Self, Self::Error> { | ||
match value { | ||
1 => Ok(Self::Lz4), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for CompressionType { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "lz4") | ||
} | ||
} |
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,23 @@ | ||
#[derive(Clone, Debug)] | ||
pub enum Either<L, R> { | ||
Left(L), | ||
Right(R), | ||
} | ||
|
||
use Either::{Left, Right}; | ||
|
||
impl<L, R> Either<L, R> { | ||
pub fn left(self) -> L { | ||
match self { | ||
Left(value) => value, | ||
Right(_) => panic!("Accessed Right on Left value"), | ||
} | ||
} | ||
|
||
pub fn right(self) -> R { | ||
match self { | ||
Right(value) => value, | ||
Left(_) => panic!("Accessed Right on Left value"), | ||
} | ||
} | ||
} |
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,58 @@ | ||
use std::{fs::File, io::Write, path::Path}; | ||
|
||
pub const LSM_MARKER: &str = "version"; | ||
pub const CONFIG_FILE: &str = "config"; | ||
pub const SEGMENTS_FOLDER: &str = "segments"; | ||
pub const LEVELS_MANIFEST_FILE: &str = "levels"; | ||
|
||
pub fn rewrite_atomic<P: AsRef<Path>>(path: P, content: &[u8]) -> std::io::Result<()> { | ||
let path = path.as_ref(); | ||
let folder = path.parent().expect("should have a parent"); | ||
let mut temp_file = tempfile::NamedTempFile::new_in(folder)?; | ||
temp_file.write_all(content)?; | ||
temp_file.persist(path)?; | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
{ | ||
let file = File::open(path)?; | ||
file.sync_all()?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
pub fn fsync_directory<P: AsRef<Path>>(path: P) -> std::io::Result<()> { | ||
let file = File::open(path)?; | ||
debug_assert!(file.metadata()?.is_dir()); | ||
file.sync_all() | ||
} | ||
|
||
#[cfg(target_os = "windows")] | ||
pub fn fsync_directory<P: AsRef<Path>>(path: P) -> std::io::Result<()> { | ||
// Cannot fsync directory on Windows | ||
Ok() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::fs::File; | ||
use std::io::Write; | ||
use test_log::test; | ||
|
||
use super::rewrite_atomic; | ||
#[test] | ||
fn atomic_write() -> std::io::Result<()> { | ||
let dir = tempfile::tempdir()?; | ||
let path = dir.path().join("test.txt"); | ||
|
||
let mut file = File::create(&path)?; | ||
write!(file, "abcdefghijklmnop")?; | ||
|
||
rewrite_atomic(&path, b"newcontent")?; | ||
|
||
let content = std::fs::read_to_string(&path)?; | ||
assert_eq!("newcontent", content); | ||
Ok(()) | ||
} | ||
} |
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,15 +1,19 @@ | ||
mod bloom; | ||
mod error; | ||
mod serde; | ||
mod value; | ||
mod lsm_entry; | ||
mod range; | ||
mod memtable; | ||
mod stop_signal; | ||
mod time; | ||
mod version; | ||
mod file; | ||
mod either; | ||
mod sst; | ||
mod compression; | ||
|
||
pub use { | ||
error::{Error, Result}, | ||
serde::{DeserializeError, SerializeError}, | ||
value::{SeqNo, UserKey, UserValue, Value, ValueType}, | ||
lsm_entry::{SeqNo, UserKey, UserValue, LSMEntry, ValueType}, | ||
}; |
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
Oops, something went wrong.