From 63d4278caec6878e744a5bdf91f9b98a65f1ade3 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Wed, 17 Apr 2024 08:12:32 -0500 Subject: [PATCH] get_stored_account_meta_callback returns Option (#847) * get_stored_account_meta_callback returns Option * Update accounts-db/src/accounts_file.rs Co-authored-by: Brooks --------- Co-authored-by: Brooks --- accounts-db/src/accounts_file.rs | 21 ++++++++++----------- accounts-db/src/append_vec.rs | 11 +++++------ accounts-db/src/tiered_storage/hot.rs | 12 +++++------- accounts-db/src/tiered_storage/readable.rs | 6 +++--- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/accounts-db/src/accounts_file.rs b/accounts-db/src/accounts_file.rs index d9021237d..0e534fc0b 100644 --- a/accounts-db/src/accounts_file.rs +++ b/accounts-db/src/accounts_file.rs @@ -137,24 +137,23 @@ impl AccountsFile { } /// calls `callback` with the account located at the specified index offset. - pub fn get_stored_account_meta_callback<'a>( + pub fn get_stored_account_meta_callback<'a, Ret>( &'a self, offset: usize, - callback: impl FnMut(StoredAccountMeta<'a>), - ) { + callback: impl FnMut(StoredAccountMeta<'a>) -> Ret, + ) -> Option { match self { Self::AppendVec(av) => av.get_stored_account_meta_callback(offset, callback), // Note: The conversion here is needed as the AccountsDB currently // assumes all offsets are multiple of 8 while TieredStorage uses // IndexOffset that is equivalent to AccountInfo::reduced_offset. - Self::TieredStorage(ts) => { - if let Some(reader) = ts.reader() { - _ = reader.get_stored_account_meta_callback( - IndexOffset(AccountInfo::get_reduced_offset(offset)), - callback, - ); - } - } + Self::TieredStorage(ts) => ts + .reader()? + .get_stored_account_meta_callback( + IndexOffset(AccountInfo::get_reduced_offset(offset)), + callback, + ) + .ok()?, } } diff --git a/accounts-db/src/append_vec.rs b/accounts-db/src/append_vec.rs index b963dee3d..245bcff39 100644 --- a/accounts-db/src/append_vec.rs +++ b/accounts-db/src/append_vec.rs @@ -542,14 +542,13 @@ impl AppendVec { } /// calls `callback` with the account located at the specified index offset. - pub fn get_stored_account_meta_callback<'a>( + pub fn get_stored_account_meta_callback<'a, Ret>( &'a self, offset: usize, - mut callback: impl FnMut(StoredAccountMeta<'a>), - ) { - if let Some((account, _offset)) = self.get_stored_account_meta(offset) { - callback(account) - } + mut callback: impl FnMut(StoredAccountMeta<'a>) -> Ret, + ) -> Option { + self.get_stored_account_meta(offset) + .map(|(account, _offset)| callback(account)) } /// return an `AccountSharedData` for an account at `offset`. diff --git a/accounts-db/src/tiered_storage/hot.rs b/accounts-db/src/tiered_storage/hot.rs index 91d9d9ed0..1efec21a3 100644 --- a/accounts-db/src/tiered_storage/hot.rs +++ b/accounts-db/src/tiered_storage/hot.rs @@ -527,15 +527,13 @@ impl HotStorageReader { } /// calls `callback` with the account located at the specified index offset. - pub fn get_stored_account_meta_callback<'a>( + pub fn get_stored_account_meta_callback<'a, Ret>( &'a self, index_offset: IndexOffset, - mut callback: impl FnMut(StoredAccountMeta<'a>), - ) -> TieredStorageResult<()> { - if let Some((account, _offset)) = self.get_stored_account_meta(index_offset)? { - callback(account) - } - Ok(()) + mut callback: impl FnMut(StoredAccountMeta<'a>) -> Ret, + ) -> TieredStorageResult> { + let account = self.get_stored_account_meta(index_offset)?; + Ok(account.map(|(account, _offset)| callback(account))) } /// Returns the account located at the specified index offset. diff --git a/accounts-db/src/tiered_storage/readable.rs b/accounts-db/src/tiered_storage/readable.rs index e94491971..488cd6b55 100644 --- a/accounts-db/src/tiered_storage/readable.rs +++ b/accounts-db/src/tiered_storage/readable.rs @@ -86,11 +86,11 @@ impl TieredStorageReader { } /// calls `callback` with the account located at the specified index offset. - pub fn get_stored_account_meta_callback<'a>( + pub fn get_stored_account_meta_callback<'a, Ret>( &'a self, index_offset: IndexOffset, - callback: impl FnMut(StoredAccountMeta<'a>), - ) -> TieredStorageResult<()> { + callback: impl FnMut(StoredAccountMeta<'a>) -> Ret, + ) -> TieredStorageResult> { match self { Self::Hot(hot) => hot.get_stored_account_meta_callback(index_offset, callback), }