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
use super::{Forest, Secrets, TreeTypes};
use crate::{
index::{CompactSeq, Index, NodeInfo},
query::Query,
store::ReadOnlyStore,
};
use anyhow::Result;
use smallvec::{smallvec, SmallVec};
#[derive(PartialEq)]
enum Mode {
Forward,
Backward,
}
pub(crate) struct IndexIter<T: TreeTypes, R, Q: Query<T>> {
forest: Forest<T, R>,
secrets: Secrets,
offset: u64,
query: Q,
stack: SmallVec<[TraverseState<T>; 5]>,
mode: Mode,
}
struct TraverseState<T: TreeTypes> {
index: Index<T>,
// If `index` points to a branch node, `position` points to the currently
// traversed child
position: isize,
// For each child, indicates whether it should be visited or not. This is
// initially empty, and initialized whenver we hit a branch.
// Branches can not have zero children, so when this is empty we know that we have
// to initialize it.
filter: SmallVec<[bool; 64]>,
}
impl<T: TreeTypes> TraverseState<T> {
fn new(index: Index<T>) -> Self {
Self {
index,
position: 0,
filter: smallvec![],
}
}
fn is_exhausted(&self, mode: &Mode) -> bool {
match mode {
Mode::Forward => !self.filter.is_empty() && self.position >= self.filter.len() as isize,
Mode::Backward => self.position < 0,
}
}
fn next_pos(&mut self, mode: &Mode) {
match mode {
Mode::Forward => self.position += 1,
Mode::Backward => self.position -= 1,
}
}
}
impl<T, R, Q> IndexIter<T, R, Q>
where
T: TreeTypes,
R: ReadOnlyStore<T::Link>,
Q: Query<T>,
{
pub(crate) fn new(forest: Forest<T, R>, secrets: Secrets, query: Q, index: Index<T>) -> Self {
let mode = Mode::Forward;
let stack = smallvec![TraverseState::new(index)];
Self {
forest,
secrets,
offset: 0,
query,
stack,
mode,
}
}
pub(crate) fn new_rev(
forest: Forest<T, R>,
secrets: Secrets,
query: Q,
index: Index<T>,
) -> Self {
let offset = index.count();
let mode = Mode::Backward;
let stack = smallvec![TraverseState::new(index)];
Self {
forest,
secrets,
offset,
query,
stack,
mode,
}
}
}
impl<T, R, Q> Iterator for IndexIter<T, R, Q>
where
T: TreeTypes,
R: ReadOnlyStore<T::Link>,
Q: Query<T>,
{
type Item = Result<Index<T>>;
fn next(&mut self) -> Option<Self::Item> {
let res = loop {
let head = match self.stack.last_mut() {
Some(i) => i,
// Nothing to do ..
_ => return None,
};
// Branch is exhausted: Ascend.
if head.is_exhausted(&self.mode) {
// Ascend to parent's node
self.stack.pop();
// increase last stack ptr, if there is still something left to
// traverse
if let Some(last) = self.stack.last_mut() {
last.next_pos(&self.mode);
}
continue;
}
match self.forest.node_info(&self.secrets, &head.index) {
NodeInfo::Branch(index, branch) => {
let branch = match branch.load_cached() {
Ok(branch) => branch,
Err(cause) => return Some(Err(cause)),
};
if head.filter.is_empty() {
// we hit this branch node for the first time. Apply the
// query on its children and store it
head.filter = smallvec![true; index.summaries.len()];
head.position = match self.mode {
Mode::Forward => 0,
Mode::Backward => branch.children.len() as isize - 1,
};
let start_offset = match self.mode {
Mode::Forward => self.offset,
Mode::Backward => self.offset - index.count,
};
self.query
.intersecting(start_offset, &index, &mut head.filter);
debug_assert_eq!(branch.children.len(), head.filter.len());
break head.index.clone();
}
let next_idx = head.position as usize;
if head.filter[next_idx] {
// Descend into next child
self.stack
.push(TraverseState::new(branch.children[next_idx].clone()));
continue;
} else {
let index = &branch.children[next_idx];
match self.mode {
Mode::Forward => {
self.offset += index.count();
}
Mode::Backward => {
self.offset -= index.count();
}
}
head.next_pos(&self.mode);
}
}
NodeInfo::Leaf(index, _) => {
match self.mode {
Mode::Forward => {
self.offset += index.keys.count();
}
Mode::Backward => {
self.offset -= index.keys.count();
}
}
// Ascend to parent's node, if it exists
let this_index = self.stack.pop().expect("not empty").index;
if let Some(last) = self.stack.last_mut() {
last.next_pos(&self.mode);
}
break this_index;
}
// even for purged leafs and branches or ignored chunks,
// produce a placeholder.
_ => {
let TraverseState { index, .. } = self.stack.pop().expect("not empty");
// Ascend to parent's node. This might be none in case the
// tree's root node is a `PurgedBranch`.
if let Some(last) = self.stack.last_mut() {
last.next_pos(&self.mode);
};
match self.mode {
Mode::Forward => {
self.offset += index.count();
}
Mode::Backward => {
self.offset -= index.count();
}
};
break index;
}
};
};
Some(Ok(res))
}
}