Skip to content

Commit

Permalink
[TieredStorage] HotStorageWriter::new() (#34659)
Browse files Browse the repository at this point in the history
#### 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.
  • Loading branch information
yhchiang-sol authored Jan 8, 2024
1 parent 3a2f132 commit 2082f8b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions accounts-db/src/tiered_storage/hot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
accounts_hash::AccountHash,
tiered_storage::{
byte_block,
file::TieredStorageFile,
footer::{
AccountBlockFormat, AccountMetaFormat, OwnersBlockFormat, TieredStorageFooter,
},
Expand All @@ -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;

Expand Down Expand Up @@ -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)]
pub mod tests {
use {
Expand Down Expand Up @@ -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(_));
}
}

0 comments on commit 2082f8b

Please sign in to comment.