Skip to content

Commit

Permalink
extract map to map() (#19883)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington authored Sep 15, 2021
1 parent 4ff5051 commit 691bea8
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions runtime/src/in_mem_accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ type K = Pubkey;
#[derive(Debug)]
pub struct InMemAccountsIndex<T: IndexValue> {
// backing store
map: RwLock<HashMap<Pubkey, AccountMapEntry<T>>>,
map_internal: RwLock<HashMap<Pubkey, AccountMapEntry<T>>>,
storage: Arc<BucketMapHolder<T>>,
bin: usize,
}

impl<T: IndexValue> InMemAccountsIndex<T> {
pub fn new(storage: &AccountsIndexStorage<T>, bin: usize) -> Self {
Self {
map: RwLock::default(),
map_internal: RwLock::default(),
storage: storage.storage().clone(),
bin,
}
}

fn map(&self) -> &RwLock<HashMap<Pubkey, AccountMapEntry<T>>> {
&self.map_internal
}

pub fn new_bucket_map_holder() -> Arc<BucketMapHolder<T>> {
Arc::new(BucketMapHolder::new())
}
Expand All @@ -41,7 +45,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
R: RangeBounds<Pubkey> + std::fmt::Debug,
{
Self::update_stat(&self.stats().items, 1);
let map = self.map.read().unwrap();
let map = self.map().read().unwrap();
let mut result = Vec::with_capacity(map.len());
map.iter().for_each(|(k, v)| {
if range.map(|range| range.contains(k)).unwrap_or(true) {
Expand All @@ -53,12 +57,12 @@ impl<T: IndexValue> InMemAccountsIndex<T> {

pub fn keys(&self) -> Vec<Pubkey> {
Self::update_stat(&self.stats().keys, 1);
self.map.read().unwrap().keys().cloned().collect()
self.map().read().unwrap().keys().cloned().collect()
}

pub fn get(&self, key: &K) -> Option<AccountMapEntry<T>> {
let m = Measure::start("get");
let result = self.map.read().unwrap().get(key).cloned();
let result = self.map().read().unwrap().get(key).cloned();
let stats = self.stats();
let (count, time) = if result.is_some() {
(&stats.gets_from_mem, &stats.get_mem_us)
Expand All @@ -74,7 +78,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
// Return false otherwise.
pub fn remove_if_slot_list_empty(&mut self, pubkey: Pubkey) -> bool {
let m = Measure::start("entry");
let mut map = self.map.write().unwrap();
let mut map = self.map().write().unwrap();
let entry = map.entry(pubkey);
let stats = &self.storage.stats;
let (count, time) = if matches!(entry, Entry::Occupied(_)) {
Expand Down Expand Up @@ -103,7 +107,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
previous_slot_entry_was_cached: bool,
) {
let m = Measure::start("entry");
let mut map = self.map.write().unwrap();
let mut map = self.map().write().unwrap();
let entry = map.entry(*pubkey);
let stats = &self.storage.stats;
let (count, time) = if matches!(entry, Entry::Occupied(_)) {
Expand Down Expand Up @@ -197,7 +201,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
reclaims: &mut SlotList<T>,
previous_slot_entry_was_cached: bool,
) -> bool {
if let Some(current) = self.map.read().unwrap().get(pubkey) {
if let Some(current) = self.map().read().unwrap().get(pubkey) {
Self::lock_and_update_slot_list(
current,
new_value,
Expand All @@ -211,7 +215,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
}

pub fn len(&self) -> usize {
self.map.read().unwrap().len()
self.map().read().unwrap().len()
}

pub fn is_empty(&self) -> bool {
Expand All @@ -226,7 +230,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
new_entry: AccountMapEntry<T>,
) -> Option<(WriteAccountMapEntry<T>, T, Pubkey)> {
let m = Measure::start("entry");
let mut map = self.map.write().unwrap();
let mut map = self.map().write().unwrap();
let entry = map.entry(pubkey);
let stats = &self.storage.stats;
let (count, time) = if matches!(entry, Entry::Occupied(_)) {
Expand Down

0 comments on commit 691bea8

Please sign in to comment.