[TieredStorage] HotStorageWriter::new() (#34659)
#### Problem The implementation of HotAccountWriter is missing. #### Summary of Changes This PR kicks off the implementation of HotStorageWriter by adding HotStorageWriter::new(). #### Test Plan Add a new unit-test that verifies the correctness of HotStorageWriter writing zero accounts using HotStorageReader.
This commit is contained in:
parent
3a2f1320c5
commit
2082f8b3ca
|
@ -7,6 +7,7 @@ use {
|
||||||
accounts_hash::AccountHash,
|
accounts_hash::AccountHash,
|
||||||
tiered_storage::{
|
tiered_storage::{
|
||||||
byte_block,
|
byte_block,
|
||||||
|
file::TieredStorageFile,
|
||||||
footer::{
|
footer::{
|
||||||
AccountBlockFormat, AccountMetaFormat, OwnersBlockFormat, TieredStorageFooter,
|
AccountBlockFormat, AccountMetaFormat, OwnersBlockFormat, TieredStorageFooter,
|
||||||
},
|
},
|
||||||
|
@ -33,6 +34,19 @@ pub const HOT_FORMAT: TieredStorageFormat = TieredStorageFormat {
|
||||||
account_block_format: AccountBlockFormat::AlignedRaw,
|
account_block_format: AccountBlockFormat::AlignedRaw,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// An helper function that creates a new default footer for hot
|
||||||
|
/// accounts storage.
|
||||||
|
fn new_hot_footer() -> TieredStorageFooter {
|
||||||
|
TieredStorageFooter {
|
||||||
|
account_meta_format: HOT_FORMAT.account_meta_format,
|
||||||
|
account_meta_entry_size: HOT_FORMAT.meta_entry_size as u32,
|
||||||
|
account_block_format: HOT_FORMAT.account_block_format,
|
||||||
|
index_block_format: HOT_FORMAT.index_block_format,
|
||||||
|
owners_block_format: HOT_FORMAT.owners_block_format,
|
||||||
|
..TieredStorageFooter::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The maximum number of padding bytes used in a hot account entry.
|
/// The maximum number of padding bytes used in a hot account entry.
|
||||||
const MAX_HOT_PADDING: u8 = 7;
|
const MAX_HOT_PADDING: u8 = 7;
|
||||||
|
|
||||||
|
@ -430,6 +444,21 @@ impl HotStorageReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The writer that creates a hot accounts file.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct HotStorageWriter {
|
||||||
|
storage: TieredStorageFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HotStorageWriter {
|
||||||
|
/// Create a new HotStorageWriter with the specified path.
|
||||||
|
pub fn new(file_path: impl AsRef<Path>) -> TieredStorageResult<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
storage: TieredStorageFile::new_writable(file_path)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use {
|
use {
|
||||||
|
@ -1026,4 +1055,18 @@ pub mod tests {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_hot_storage_writer_twice_on_same_path() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let path = temp_dir
|
||||||
|
.path()
|
||||||
|
.join("test_hot_storage_writer_twice_on_same_path");
|
||||||
|
|
||||||
|
// Expect the first returns Ok
|
||||||
|
assert_matches!(HotStorageWriter::new(&path), Ok(_));
|
||||||
|
// Expect the second call on the same path returns Err, as the
|
||||||
|
// HotStorageWriter only writes once.
|
||||||
|
assert_matches!(HotStorageWriter::new(&path), Err(_));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue