Skip to content

Commit

Permalink
[TieredStorage] Refactor file_size() code path (#400)
Browse files Browse the repository at this point in the history
#### Problem
TieredStorage::file_size() essentially supports AccountsFile::len(),
but its API is inconsistent with AccountsFile's.

#### Summary of Changes
Refactor TieredStorage::file_size() to ::len() and share the same API
as AccountsFile's.

#### Test Plan
Build
Existing unit-tests.
  • Loading branch information
yhchiang-sol authored Mar 22, 2024
1 parent 9a447ab commit e9cc9f8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
15 changes: 10 additions & 5 deletions accounts-db/src/tiered_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,13 @@ impl TieredStorage {
}

/// Returns the size of the underlying accounts file.
pub fn file_size(&self) -> TieredStorageResult<u64> {
Ok(self.reader().map_or(0, |reader| reader.len()))
pub fn len(&self) -> usize {
self.reader().map_or(0, |reader| reader.len())
}

/// Returns whether the underlying storage is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

Expand Down Expand Up @@ -220,7 +225,7 @@ mod tests {

assert!(tiered_storage.is_read_only());
assert_eq!(
tiered_storage.file_size().unwrap() as usize,
tiered_storage.len(),
std::mem::size_of::<TieredStorageFooter>()
+ std::mem::size_of::<TieredStorageMagicNumber>()
);
Expand All @@ -238,7 +243,7 @@ mod tests {

assert!(!tiered_storage.is_read_only());
assert_eq!(tiered_storage.path(), tiered_storage_path);
assert_eq!(tiered_storage.file_size().unwrap(), 0);
assert_eq!(tiered_storage.len(), 0);

write_zero_accounts(&tiered_storage, Ok(vec![]));
}
Expand All @@ -252,7 +257,7 @@ mod tests {
assert_eq!(footer.index_block_format, HOT_FORMAT.index_block_format);
assert_eq!(footer.account_block_format, HOT_FORMAT.account_block_format);
assert_eq!(
tiered_storage_readonly.file_size().unwrap() as usize,
tiered_storage_readonly.len(),
std::mem::size_of::<TieredStorageFooter>()
+ std::mem::size_of::<TieredStorageMagicNumber>()
);
Expand Down
12 changes: 6 additions & 6 deletions accounts-db/src/tiered_storage/hot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ impl HotStorageReader {
}

/// Returns the size of the underlying storage.
pub fn len(&self) -> u64 {
self.mmap.len() as u64
pub fn len(&self) -> usize {
self.mmap.len()
}

/// Returns whether the nderlying storage is empty.
Expand Down Expand Up @@ -1432,10 +1432,10 @@ pub mod tests {
}
let footer = hot_storage.footer();

let expected_size: u64 = footer.owners_block_offset
+ std::mem::size_of::<Pubkey>() as u64 * footer.owner_count as u64
+ std::mem::size_of::<TieredStorageFooter>() as u64
+ std::mem::size_of::<TieredStorageMagicNumber>() as u64;
let expected_size = footer.owners_block_offset as usize
+ std::mem::size_of::<Pubkey>() * footer.owner_count as usize
+ std::mem::size_of::<TieredStorageFooter>()
+ std::mem::size_of::<TieredStorageMagicNumber>();

assert!(!hot_storage.is_empty());
assert_eq!(expected_size, hot_storage.len());
Expand Down
2 changes: 1 addition & 1 deletion accounts-db/src/tiered_storage/readable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl TieredStorageReader {
}

/// Returns the size of the underlying storage.
pub fn len(&self) -> u64 {
pub fn len(&self) -> usize {
match self {
Self::Hot(hot) => hot.len(),
}
Expand Down

0 comments on commit e9cc9f8

Please sign in to comment.