From 2082f8b3cab9a5ffc07f2a4e6efaadb532180320 Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang <93241502+yhchiang-sol@users.noreply.github.com> Date: Mon, 8 Jan 2024 14:14:30 -0800 Subject: [PATCH] [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. --- accounts-db/src/tiered_storage/hot.rs | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/accounts-db/src/tiered_storage/hot.rs b/accounts-db/src/tiered_storage/hot.rs index 9adfe08541..4d42cbb4bc 100644 --- a/accounts-db/src/tiered_storage/hot.rs +++ b/accounts-db/src/tiered_storage/hot.rs @@ -7,6 +7,7 @@ use { accounts_hash::AccountHash, tiered_storage::{ byte_block, + file::TieredStorageFile, footer::{ AccountBlockFormat, AccountMetaFormat, OwnersBlockFormat, TieredStorageFooter, }, @@ -33,6 +34,19 @@ pub const HOT_FORMAT: TieredStorageFormat = TieredStorageFormat { 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. 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) -> TieredStorageResult { + Ok(Self { + storage: TieredStorageFile::new_writable(file_path)?, + }) + } +} + #[cfg(test)] pub mod tests { use { @@ -1026,4 +1055,18 @@ pub mod tests { 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(_)); + } }