-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TieredStorage] TieredStorageFile -> TieredReadonlyFile and TieredWritableFIle #260
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,10 @@ use { | |
}; | ||
|
||
#[derive(Debug)] | ||
pub struct TieredStorageFile(pub File); | ||
pub struct TieredReadableFile(pub File); | ||
|
||
impl TieredStorageFile { | ||
pub fn new_readonly(file_path: impl AsRef<Path>) -> Self { | ||
impl TieredReadableFile { | ||
pub fn new(file_path: impl AsRef<Path>) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Github won't let me comment on line 30, which is my intent I see below there's a |
||
Self( | ||
OpenOptions::new() | ||
.read(true) | ||
|
@@ -36,30 +36,6 @@ impl TieredStorageFile { | |
)) | ||
} | ||
|
||
/// Writes `value` to the file. | ||
/// | ||
/// `value` must be plain ol' data. | ||
pub fn write_pod<T: NoUninit>(&self, value: &T) -> IoResult<usize> { | ||
// SAFETY: Since T is NoUninit, it does not contain any uninitialized bytes. | ||
unsafe { self.write_type(value) } | ||
} | ||
|
||
/// Writes `value` to the file. | ||
/// | ||
/// Prefer `write_pod` when possible, because `write_value` may cause | ||
/// undefined behavior if `value` contains uninitialized bytes. | ||
/// | ||
/// # Safety | ||
/// | ||
/// Caller must ensure casting T to bytes is safe. | ||
/// Refer to the Safety sections in std::slice::from_raw_parts() | ||
/// and bytemuck's Pod and NoUninit for more information. | ||
pub unsafe fn write_type<T>(&self, value: &T) -> IoResult<usize> { | ||
let ptr = value as *const _ as *const u8; | ||
let bytes = unsafe { std::slice::from_raw_parts(ptr, mem::size_of::<T>()) }; | ||
self.write_bytes(bytes) | ||
} | ||
|
||
/// Reads a value of type `T` from the file. | ||
/// | ||
/// Type T must be plain ol' data. | ||
|
@@ -95,13 +71,59 @@ impl TieredStorageFile { | |
(&self.0).seek(SeekFrom::End(offset)) | ||
} | ||
|
||
pub fn read_bytes(&self, buffer: &mut [u8]) -> IoResult<()> { | ||
(&self.0).read_exact(buffer) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct TieredWritableFile(pub File); | ||
|
||
impl TieredWritableFile { | ||
pub fn new(file_path: impl AsRef<Path>) -> IoResult<Self> { | ||
Ok(Self( | ||
OpenOptions::new() | ||
.create_new(true) | ||
.write(true) | ||
.open(file_path)?, | ||
)) | ||
} | ||
|
||
/// Writes `value` to the file. | ||
/// | ||
/// `value` must be plain ol' data. | ||
pub fn write_pod<T: NoUninit>(&self, value: &T) -> IoResult<usize> { | ||
// SAFETY: Since T is NoUninit, it does not contain any uninitialized bytes. | ||
unsafe { self.write_type(value) } | ||
} | ||
|
||
/// Writes `value` to the file. | ||
/// | ||
/// Prefer `write_pod` when possible, because `write_value` may cause | ||
/// undefined behavior if `value` contains uninitialized bytes. | ||
/// | ||
/// # Safety | ||
/// | ||
/// Caller must ensure casting T to bytes is safe. | ||
/// Refer to the Safety sections in std::slice::from_raw_parts() | ||
/// and bytemuck's Pod and NoUninit for more information. | ||
pub unsafe fn write_type<T>(&self, value: &T) -> IoResult<usize> { | ||
let ptr = value as *const _ as *const u8; | ||
let bytes = unsafe { std::slice::from_raw_parts(ptr, mem::size_of::<T>()) }; | ||
self.write_bytes(bytes) | ||
} | ||
|
||
pub fn seek(&self, offset: u64) -> IoResult<u64> { | ||
(&self.0).seek(SeekFrom::Start(offset)) | ||
} | ||
|
||
pub fn seek_from_end(&self, offset: i64) -> IoResult<u64> { | ||
(&self.0).seek(SeekFrom::End(offset)) | ||
} | ||
Comment on lines
+116
to
+122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's too bad these methods are duplicated. Do both the Readable and Writable have calls to these methods? I was under the impression that Writeable does everything in one pass, from top to bottom, and thus would not need to seek. |
||
|
||
pub fn write_bytes(&self, bytes: &[u8]) -> IoResult<usize> { | ||
(&self.0).write_all(bytes)?; | ||
|
||
Ok(bytes.len()) | ||
} | ||
|
||
pub fn read_bytes(&self, buffer: &mut [u8]) -> IoResult<()> { | ||
(&self.0).read_exact(buffer) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The name of the struct is now
TieredReadableFile
, but the PR title and description both use the incorrectTieredReadonlyFile
. It would be preferable for all of these to remain in-sync and correct. It's now too late for the commit message, since the PR has been merged. Please consider this for future PRs.