-
Notifications
You must be signed in to change notification settings - Fork 117
/
memory_db.rs
93 lines (86 loc) · 3.31 KB
/
memory_db.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use radix_substate_store_interface::interface::*;
use sbor::rust::prelude::*;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct InMemorySubstateDatabase {
partitions: BTreeMap<DbPartitionKey, BTreeMap<DbSortKey, DbSubstateValue>>,
}
impl InMemorySubstateDatabase {
pub fn standard() -> Self {
Self {
partitions: BTreeMap::new(),
}
}
}
impl SubstateDatabase for InMemorySubstateDatabase {
fn get_substate(
&self,
partition_key: &DbPartitionKey,
sort_key: &DbSortKey,
) -> Option<DbSubstateValue> {
self.partitions
.get(partition_key)
.and_then(|partition| partition.get(sort_key))
.cloned()
}
fn list_entries_from(
&self,
partition_key: &DbPartitionKey,
from_sort_key: Option<&DbSortKey>,
) -> Box<dyn Iterator<Item = PartitionEntry> + '_> {
let from_sort_key = from_sort_key.cloned();
let iter = self
.partitions
.get(partition_key)
.into_iter()
.flat_map(|partition| partition.iter())
.skip_while(move |(key, _substate)| Some(*key) < from_sort_key.as_ref())
.map(|(key, substate)| (key.clone(), substate.clone()));
Box::new(iter)
}
}
impl CommittableSubstateDatabase for InMemorySubstateDatabase {
fn commit(&mut self, database_updates: &DatabaseUpdates) {
for (node_key, node_updates) in &database_updates.node_updates {
for (partition_num, partition_updates) in &node_updates.partition_updates {
let partition_key = DbPartitionKey {
node_key: node_key.clone(),
partition_num: partition_num.clone(),
};
let partition = self
.partitions
.entry(partition_key.clone())
.or_insert_with(|| BTreeMap::new());
match partition_updates {
PartitionDatabaseUpdates::Delta { substate_updates } => {
for (sort_key, update) in substate_updates {
match update {
DatabaseUpdate::Set(substate_value) => {
partition.insert(sort_key.clone(), substate_value.clone())
}
DatabaseUpdate::Delete => partition.remove(sort_key),
};
}
}
PartitionDatabaseUpdates::Reset {
new_substate_values,
} => {
*partition = BTreeMap::from_iter(
new_substate_values
.iter()
.map(|(sort_key, value)| (sort_key.clone(), value.clone())),
)
}
}
if partition.is_empty() {
self.partitions.remove(&partition_key);
}
}
}
}
}
impl ListableSubstateDatabase for InMemorySubstateDatabase {
fn list_partition_keys(&self) -> Box<dyn Iterator<Item = DbPartitionKey> + '_> {
let partition_iter = self.partitions.iter().map(|(key, _)| key.clone());
Box::new(partition_iter)
}
}