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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#![allow(clippy::implicit_hasher)]
#[cfg(feature = "asynch")]
pub mod asynch;
#[cfg(feature = "arcache")]
use crate::internals::hashtrie::cursor::Datum;
use crate::internals::lincowcell::{LinCowCell, LinCowCellReadTxn, LinCowCellWriteTxn};
include!("impl.rs");
impl<K: Hash + Eq + Clone + Debug + Sync + Send + 'static, V: Clone + Sync + Send + 'static>
HashTrie<K, V>
{
pub fn new() -> Self {
HashTrie {
inner: LinCowCell::new(unsafe { SuperBlock::new() }),
}
}
pub fn read(&self) -> HashTrieReadTxn<K, V> {
let inner = self.inner.read();
HashTrieReadTxn { inner }
}
pub fn write(&self) -> HashTrieWriteTxn<K, V> {
let inner = self.inner.write();
HashTrieWriteTxn { inner }
}
pub fn try_write(&self) -> Option<HashTrieWriteTxn<K, V>> {
self.inner
.try_write()
.map(|inner| HashTrieWriteTxn { inner })
}
}
impl<
'a,
K: Hash + Eq + Clone + Debug + Sync + Send + 'static,
V: Clone + Sync + Send + 'static,
> HashTrieWriteTxn<'a, K, V>
{
#[cfg(feature = "arcache")]
pub(crate) fn get_txid(&self) -> u64 {
self.inner.as_ref().get_txid()
}
#[cfg(feature = "arcache")]
pub(crate) fn prehash<'b, Q: ?Sized>(&'a self, k: &'b Q) -> u64
where
K: Borrow<Q>,
Q: Hash + Eq,
{
self.inner.as_ref().hash_key(k)
}
#[cfg(feature = "arcache")]
pub(crate) unsafe fn get_slot_mut(&mut self, k_hash: u64) -> Option<&mut [Datum<K, V>]> {
self.inner.as_mut().get_slot_mut_ref(k_hash)
}
pub fn commit(self) {
self.inner.commit();
}
}
impl<
'a,
K: Hash + Eq + Clone + Debug + Sync + Send + 'static,
V: Clone + Sync + Send + 'static,
> HashTrieReadTxn<'a, K, V>
{
#[cfg(feature = "arcache")]
pub(crate) fn get_txid(&self) -> u64 {
self.inner.as_ref().get_txid()
}
#[cfg(feature = "arcache")]
pub(crate) fn prehash<'b, Q: ?Sized>(&'a self, k: &'b Q) -> u64
where
K: Borrow<Q>,
Q: Hash + Eq,
{
self.inner.as_ref().hash_key(k)
}
}
#[cfg(test)]
mod tests {
use super::HashTrie;
#[test]
fn test_hashtrie_basic_write() {
let hmap: HashTrie<usize, usize> = HashTrie::new();
let mut hmap_write = hmap.write();
hmap_write.insert(10, 10);
hmap_write.insert(15, 15);
assert!(hmap_write.contains_key(&10));
assert!(hmap_write.contains_key(&15));
assert!(!hmap_write.contains_key(&20));
assert!(hmap_write.get(&10) == Some(&10));
{
let v = hmap_write.get_mut(&10).unwrap();
*v = 11;
}
assert!(hmap_write.get(&10) == Some(&11));
assert!(hmap_write.remove(&10).is_some());
assert!(!hmap_write.contains_key(&10));
assert!(hmap_write.contains_key(&15));
assert!(hmap_write.remove(&30).is_none());
hmap_write.clear();
assert!(!hmap_write.contains_key(&10));
assert!(!hmap_write.contains_key(&15));
hmap_write.commit();
}
#[test]
fn test_hashtrie_basic_read_write() {
let hmap: HashTrie<usize, usize> = HashTrie::new();
let mut hmap_w1 = hmap.write();
hmap_w1.insert(10, 10);
hmap_w1.insert(15, 15);
hmap_w1.commit();
let hmap_r1 = hmap.read();
assert!(hmap_r1.contains_key(&10));
assert!(hmap_r1.contains_key(&15));
assert!(!hmap_r1.contains_key(&20));
let mut hmap_w2 = hmap.write();
hmap_w2.insert(20, 20);
hmap_w2.commit();
assert!(hmap_r1.contains_key(&10));
assert!(hmap_r1.contains_key(&15));
assert!(!hmap_r1.contains_key(&20));
let hmap_r2 = hmap.read();
assert!(hmap_r2.contains_key(&10));
assert!(hmap_r2.contains_key(&15));
assert!(hmap_r2.contains_key(&20));
}
#[test]
fn test_hashtrie_basic_read_snapshot() {
let hmap: HashTrie<usize, usize> = HashTrie::new();
let mut hmap_w1 = hmap.write();
hmap_w1.insert(10, 10);
hmap_w1.insert(15, 15);
let snap = hmap_w1.to_snapshot();
assert!(snap.contains_key(&10));
assert!(snap.contains_key(&15));
assert!(!snap.contains_key(&20));
}
#[test]
fn test_hashtrie_basic_iter() {
let hmap: HashTrie<usize, usize> = HashTrie::new();
let mut hmap_w1 = hmap.write();
assert!(hmap_w1.iter().count() == 0);
hmap_w1.insert(10, 10);
hmap_w1.insert(15, 15);
assert!(hmap_w1.iter().count() == 2);
}
#[test]
fn test_hashtrie_from_iter() {
let hmap: HashTrie<usize, usize> = vec![(10, 10), (15, 15), (20, 20)].into_iter().collect();
let hmap_r2 = hmap.read();
assert!(hmap_r2.contains_key(&10));
assert!(hmap_r2.contains_key(&15));
assert!(hmap_r2.contains_key(&20));
}
}