Adds AccountsIndex::get_account_info_with_and_then() (#35336)

This commit is contained in:
Brooks 2024-02-27 20:01:29 -05:00 committed by GitHub
parent da088681ba
commit a4e1a9ac98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 4 deletions

View File

@ -1143,18 +1143,30 @@ impl<T: IndexValue, U: DiskIndexValue + From<T> + Into<T>> AccountsIndex<T, U> {
ancestors: Option<&Ancestors>,
max_root: Option<Slot>,
should_add_to_in_mem_cache: bool,
mut callback: impl FnMut((Slot, T)) -> R,
callback: impl FnOnce((Slot, T)) -> R,
) -> Option<R> {
self.get_and_then(pubkey, |entry| {
let callback_result = entry.and_then(|entry| {
let slot_list = entry.slot_list.read().unwrap();
self.latest_slot(ancestors, &slot_list, max_root)
.map(|found_index| callback(slot_list[found_index]))
self.get_account_info_with_and_then(entry, ancestors, max_root, callback)
});
(should_add_to_in_mem_cache, callback_result)
})
}
/// Gets the account info (and slot) in `entry`, with `ancestors` and `max_root`,
/// and applies `callback` to it
fn get_account_info_with_and_then<R>(
&self,
entry: &AccountMapEntryInner<T>,
ancestors: Option<&Ancestors>,
max_root: Option<Slot>,
callback: impl FnOnce((Slot, T)) -> R,
) -> Option<R> {
let slot_list = entry.slot_list.read().unwrap();
self.latest_slot(ancestors, &slot_list, max_root)
.map(|found_index| callback(slot_list[found_index]))
}
/// Gets the index's entry for `pubkey` and clones it
///
/// Prefer `get_and_then()` whenever possible.