diff --git a/accounts-db/src/tiered_storage/hot.rs b/accounts-db/src/tiered_storage/hot.rs index 1ae19bb1cd7d35..1aa69785682764 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(_)); + } }