Skip to content
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] HotStorageWriter::new() #34659

Merged
merged 7 commits into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(_));
}
}