From e9cc9f8379b8b116ad007f9e76ec501ff93f0cc6 Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang <93241502+yhchiang-sol@users.noreply.github.com> Date: Fri, 22 Mar 2024 14:21:24 -0700 Subject: [PATCH] [TieredStorage] Refactor file_size() code path (#400) #### 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. --- accounts-db/src/tiered_storage.rs | 15 ++++++++++----- accounts-db/src/tiered_storage/hot.rs | 12 ++++++------ accounts-db/src/tiered_storage/readable.rs | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/accounts-db/src/tiered_storage.rs b/accounts-db/src/tiered_storage.rs index 20626143d7036d..a0d8eea4010b94 100644 --- a/accounts-db/src/tiered_storage.rs +++ b/accounts-db/src/tiered_storage.rs @@ -155,8 +155,13 @@ impl TieredStorage { } /// Returns the size of the underlying accounts file. - pub fn file_size(&self) -> TieredStorageResult { - 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 } } @@ -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::() + std::mem::size_of::() ); @@ -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![])); } @@ -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::() + std::mem::size_of::() ); diff --git a/accounts-db/src/tiered_storage/hot.rs b/accounts-db/src/tiered_storage/hot.rs index 4d3fabfec40a63..414d74b2eb81b7 100644 --- a/accounts-db/src/tiered_storage/hot.rs +++ b/accounts-db/src/tiered_storage/hot.rs @@ -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. @@ -1432,10 +1432,10 @@ pub mod tests { } let footer = hot_storage.footer(); - let expected_size: u64 = footer.owners_block_offset - + std::mem::size_of::() as u64 * footer.owner_count as u64 - + std::mem::size_of::() as u64 - + std::mem::size_of::() as u64; + let expected_size = footer.owners_block_offset as usize + + std::mem::size_of::() * footer.owner_count as usize + + std::mem::size_of::() + + std::mem::size_of::(); assert!(!hot_storage.is_empty()); assert_eq!(expected_size, hot_storage.len()); diff --git a/accounts-db/src/tiered_storage/readable.rs b/accounts-db/src/tiered_storage/readable.rs index cc2b6fdee0356f..008e805689df57 100644 --- a/accounts-db/src/tiered_storage/readable.rs +++ b/accounts-db/src/tiered_storage/readable.rs @@ -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(), }