move insert_new_entry_if_missing_with_lock into InMemAccountsIndex (#19800)

This commit is contained in:
Jeff Washington (jwash)
2021-09-12 13:39:29 -05:00
committed by GitHub
parent a295febf04
commit c9a3b8941f
2 changed files with 25 additions and 29 deletions

View File

@@ -1,4 +1,4 @@
use crate::accounts_index::{AccountMapEntry, IsCached};
use crate::accounts_index::{AccountMapEntry, IsCached, WriteAccountMapEntry};
use solana_sdk::pubkey::Pubkey;
use std::collections::{
hash_map::{Entry, Keys},
@@ -61,4 +61,26 @@ impl<T: IsCached> InMemAccountsIndex<T> {
pub fn is_empty(&self) -> bool {
self.len() == 0
}
// return None if item was created new
// if entry for pubkey already existed, return Some(entry). Caller needs to call entry.update.
pub fn insert_new_entry_if_missing_with_lock(
&mut self,
pubkey: Pubkey,
new_entry: AccountMapEntry<T>,
) -> Option<(WriteAccountMapEntry<T>, T, Pubkey)> {
let account_entry = self.map.entry(pubkey);
match account_entry {
Entry::Occupied(account_entry) => Some((
WriteAccountMapEntry::from_account_map_entry(account_entry.get().clone()),
// extract the new account_info from the unused 'new_entry'
new_entry.slot_list.write().unwrap().remove(0).1,
*account_entry.key(),
)),
Entry::Vacant(account_entry) => {
account_entry.insert(new_entry);
None
}
}
}
}