2021-09-10 19:09:51 -07:00
|
|
|
use crate::accounts_index::{AccountMapEntry, IsCached};
|
2021-09-10 15:52:25 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
use std::collections::{
|
2021-09-11 13:33:04 -07:00
|
|
|
hash_map::{Entry, Keys},
|
2021-09-10 15:52:25 -07:00
|
|
|
HashMap,
|
|
|
|
};
|
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
type K = Pubkey;
|
|
|
|
|
|
|
|
// one instance of this represents one bin of the accounts index.
|
|
|
|
#[derive(Debug, Default)]
|
2021-09-12 08:25:09 -07:00
|
|
|
pub struct InMemAccountsIndex<T: IsCached> {
|
2021-09-10 15:52:25 -07:00
|
|
|
// backing store
|
2021-09-12 08:25:09 -07:00
|
|
|
map: HashMap<Pubkey, AccountMapEntry<T>>,
|
2021-09-10 15:52:25 -07:00
|
|
|
}
|
|
|
|
|
2021-09-12 08:25:09 -07:00
|
|
|
impl<T: IsCached> InMemAccountsIndex<T> {
|
2021-09-10 15:52:25 -07:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
map: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 08:25:09 -07:00
|
|
|
pub fn entry(&mut self, pubkey: Pubkey) -> Entry<K, AccountMapEntry<T>> {
|
2021-09-10 15:52:25 -07:00
|
|
|
self.map.entry(pubkey)
|
|
|
|
}
|
|
|
|
|
2021-09-12 08:25:09 -07:00
|
|
|
pub fn items(&self) -> Vec<(K, AccountMapEntry<T>)> {
|
2021-09-11 13:33:04 -07:00
|
|
|
self.map.iter().map(|(k, v)| (*k, v.clone())).collect()
|
2021-09-10 15:52:25 -07:00
|
|
|
}
|
|
|
|
|
2021-09-12 08:25:09 -07:00
|
|
|
pub fn keys(&self) -> Keys<K, AccountMapEntry<T>> {
|
2021-09-10 15:52:25 -07:00
|
|
|
self.map.keys()
|
|
|
|
}
|
|
|
|
|
2021-09-12 08:25:09 -07:00
|
|
|
pub fn get(&self, key: &K) -> Option<AccountMapEntry<T>> {
|
2021-09-11 11:28:56 -07:00
|
|
|
self.map.get(key).cloned()
|
2021-09-10 15:52:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove(&mut self, key: &K) {
|
|
|
|
self.map.remove(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.map.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
}
|