cfx_storage/impls/delta_mpt/
node_ref_map.rs1#[derive(Clone, MallocSizeOfDerive)]
6pub enum TrieCacheSlotOrCacheAlgoData<CacheAlgoDataT: CacheAlgoDataTrait> {
7 TrieCacheSlot(ActualSlabIndex),
8 CacheAlgoData(CacheAlgoDataT),
9}
10
11#[derive(Clone, MallocSizeOfDerive)]
26pub struct CacheableNodeRefDeltaMpt<CacheAlgoDataT: CacheAlgoDataTrait> {
27 cached: TrieCacheSlotOrCacheAlgoData<CacheAlgoDataT>,
28}
29
30impl<CacheAlgoDataT: CacheAlgoDataTrait>
31 CacheableNodeRefDeltaMpt<CacheAlgoDataT>
32{
33 pub fn new(cached: TrieCacheSlotOrCacheAlgoData<CacheAlgoDataT>) -> Self {
34 Self { cached }
35 }
36
37 pub fn get_cache_info(
38 &self,
39 ) -> &TrieCacheSlotOrCacheAlgoData<CacheAlgoDataT> {
40 &self.cached
41 }
42
43 pub fn get_slot(&self) -> Option<&ActualSlabIndex> {
44 match &self.cached {
45 TrieCacheSlotOrCacheAlgoData::CacheAlgoData(_) => None,
46 TrieCacheSlotOrCacheAlgoData::TrieCacheSlot(slot) => Some(slot),
47 }
48 }
49}
50
51pub type DeltaMptDbKey = RowNumberUnderlyingType;
56pub type DeltaMptId = u16;
59const MPT_ID_RANGE: usize = std::u16::MAX as usize + 1;
60
61#[derive(MallocSizeOfDerive)]
67pub struct NodeRefMapDeltaMpts<CacheAlgoDataT: CacheAlgoDataTrait> {
68 node_ref_maps:
69 Vec<HashMap<DeltaMptDbKey, CacheableNodeRefDeltaMpt<CacheAlgoDataT>>>,
70 nodes: usize,
71}
72
73impl<CacheAlgoDataT: CacheAlgoDataTrait> NodeRefMapDeltaMpts<CacheAlgoDataT> {
74 pub fn new() -> Self {
75 let mut node_ref_maps = Vec::with_capacity(MPT_ID_RANGE);
76 for _i in 0..MPT_ID_RANGE {
77 node_ref_maps.push(Default::default());
78 }
79
80 Self {
81 node_ref_maps,
82 nodes: 0,
83 }
84 }
85
86 pub fn insert(
89 &mut self, key: (DeltaMptId, DeltaMptDbKey), slot: ActualSlabIndex,
90 ) -> Result<()> {
91 if self.node_ref_maps[key.0 as usize]
92 .insert(
93 key.1,
94 CacheableNodeRefDeltaMpt {
95 cached: TrieCacheSlotOrCacheAlgoData::TrieCacheSlot(slot),
96 },
97 )
98 .is_none()
99 {
100 self.nodes += 1;
101 };
102 Ok(())
103 }
104
105 pub fn get_cache_info(
109 &self, key: (DeltaMptId, DeltaMptDbKey),
110 ) -> Option<&CacheableNodeRefDeltaMpt<CacheAlgoDataT>> {
111 self.node_ref_maps[key.0 as usize].get(&key.1)
112 }
113
114 pub fn set_cache_info(
115 &mut self, key: (DeltaMptId, DeltaMptDbKey),
116 cache_info: CacheableNodeRefDeltaMpt<CacheAlgoDataT>,
117 ) {
118 if self.node_ref_maps[key.0 as usize]
119 .insert(key.1, cache_info)
120 .is_none()
121 {
122 self.nodes += 1;
123 }
124 }
125
126 pub fn delete(
127 &mut self, key: (DeltaMptId, DeltaMptDbKey),
128 ) -> Option<CacheableNodeRefDeltaMpt<CacheAlgoDataT>> {
129 let maybe_old_value = self.node_ref_maps[key.0 as usize].remove(&key.1);
130 if maybe_old_value.is_some() {
131 self.nodes -= 1;
132 }
133 maybe_old_value
134 }
135
136 pub fn get_all_cache_infos_from_mpt(
137 &self, mpt_id: DeltaMptId,
138 ) -> Vec<(DeltaMptDbKey, CacheableNodeRefDeltaMpt<CacheAlgoDataT>)> {
139 self.node_ref_maps[mpt_id as usize]
140 .iter()
141 .map(|(k, v)| (k.clone(), v.clone()))
142 .collect()
143 }
144
145 pub fn log_usage(&self) {
146 debug!("node_ref_map.BTreeMap: #nodes: {}", self.nodes,);
147 }
148}
149
150pub const DEFAULT_NODE_MAP_SIZE: u32 = 200_000_000;
151
152use super::{
153 super::errors::*, cache::algorithm::CacheAlgoDataTrait,
154 node_memory_manager::ActualSlabIndex, row_number::RowNumberUnderlyingType,
155};
156use hashbrown::HashMap;
157use malloc_size_of_derive::MallocSizeOf as MallocSizeOfDerive;