massa_storage/
operation_indexes.rs1use std::collections::hash_map::{self, Entry};
2
3use massa_models::{
4 address::Address,
5 operation::{OperationId, OperationPrefixId, SecureShareOperation},
6 prehash::{PreHashMap, PreHashSet},
7};
8
9#[derive(Default)]
12pub struct OperationIndexes {
13 operations: PreHashMap<OperationId, Box<SecureShareOperation>>,
15 index_by_creator: PreHashMap<Address, PreHashSet<OperationId>>,
17 index_by_prefix: PreHashMap<OperationPrefixId, PreHashSet<OperationId>>,
19}
20
21impl OperationIndexes {
22 pub(crate) fn insert(&mut self, operation: SecureShareOperation) {
26 if let Entry::Vacant(vac) = self.operations.entry(operation.id) {
27 let operation = vac.insert(Box::new(operation));
28 self.index_by_creator
30 .entry(operation.content_creator_address)
31 .or_default()
32 .insert(operation.id);
33 self.index_by_prefix
35 .entry(operation.id.prefix())
36 .or_default()
37 .insert(operation.id);
38
39 massa_metrics::set_operations_counter(self.operations.len());
40 }
41 }
42
43 pub(crate) fn remove(
47 &mut self,
48 operation_id: &OperationId,
49 ) -> Option<Box<SecureShareOperation>> {
50 if let Some(o) = self.operations.remove(operation_id) {
51 massa_metrics::set_operations_counter(self.operations.len());
52
53 if let hash_map::Entry::Occupied(mut occ) =
55 self.index_by_creator.entry(o.content_creator_address)
56 {
57 occ.get_mut().remove(&o.id);
58 if occ.get().is_empty() {
59 occ.remove();
60 }
61 }
62 if let hash_map::Entry::Occupied(mut occ) = self.index_by_prefix.entry(o.id.prefix()) {
64 occ.get_mut().remove(&o.id);
65 if occ.get().is_empty() {
66 occ.remove();
67 }
68 }
69 return Some(o);
70 }
71 None
72 }
73
74 pub fn get(&self, id: &OperationId) -> Option<&SecureShareOperation> {
76 self.operations.get(id).map(|v| v.as_ref())
77 }
78
79 pub fn contains(&self, id: &OperationId) -> bool {
81 self.operations.contains_key(id)
82 }
83
84 pub fn get_operations_created_by(&self, address: &Address) -> Option<&PreHashSet<OperationId>> {
91 self.index_by_creator.get(address)
92 }
93
94 pub fn get_operations_by_prefix(
101 &self,
102 prefix: &OperationPrefixId,
103 ) -> Option<&PreHashSet<OperationId>> {
104 self.index_by_prefix.get(prefix)
105 }
106}