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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use super::{Desc, ExecutionNode, NodeSearchDesc};
use annis::db::annostorage::AnnoStorage;
use annis::db::Match;
use annis::operator::{EstimationType, Operator};
use annis::types::{AnnoKey, NodeID};
use std;
use std::iter::Peekable;
use std::sync::Arc;
/// A join that takes any iterator as left-hand-side (LHS) and an annotation condition as right-hand-side (RHS).
/// It then retrieves all matches as defined by the operator for each LHS element and checks
/// if the annotation condition is true.
pub struct IndexJoin<'a> {
lhs: Peekable<Box<ExecutionNode<Item = Vec<Match>> + 'a>>,
rhs_candidate: Option<std::iter::Peekable<Box<Iterator<Item = Match>>>>,
op: Box<Operator>,
lhs_idx: usize,
node_search_desc: Arc<NodeSearchDesc>,
node_annos: Arc<AnnoStorage<NodeID>>,
desc: Desc,
}
impl<'a> IndexJoin<'a> {
/// Create a new `IndexJoin`
/// # Arguments
///
/// * `lhs` - An iterator for a left-hand-side
/// * `lhs_idx` - The index of the element in the LHS that should be used as a source
/// * `op` - The operator that connects the LHS and RHS
/// * `anno_qname` A pair of the annotation namespace and name (both optional) to define which annotations to fetch
/// * `anno_cond` - A filter function to determine if a RHS candidate is included
pub fn new(
lhs: Box<ExecutionNode<Item = Vec<Match>> + 'a>,
lhs_idx: usize,
node_nr_lhs: usize,
node_nr_rhs: usize,
op: Box<Operator>,
node_search_desc: Arc<NodeSearchDesc>,
node_annos: Arc<AnnoStorage<NodeID>>,
rhs_desc: Option<&Desc>,
) -> IndexJoin<'a> {
let lhs_desc = lhs.get_desc().cloned();
// TODO, we
let lhs_peek = lhs.peekable();
let processed_func = |est_type: EstimationType, out_lhs: usize, out_rhs: usize| {
match est_type {
EstimationType::SELECTIVITY(op_sel) => {
// A index join processes each LHS and for each LHS the number of reachable nodes given by the operator.
// The selectivity of the operator itself an estimation how many nodes are filtered out by the cross product.
// We can use this number (without the edge annotation selectivity) to re-construct the number of reachable nodes.
// avgReachable = (sel * cross) / lhs
// = (sel * lhs * rhs) / lhs
// = sel * rhs
// processedInStep = lhs + (avgReachable * lhs)
// = lhs + (sel * rhs * lhs)
let result = (out_lhs as f64) + (op_sel * (out_rhs as f64) * (out_lhs as f64));
return result.round() as usize;
}
EstimationType::MIN => {
return out_lhs;
}
}
};
return IndexJoin {
desc: Desc::join(
&op,
lhs_desc.as_ref(),
rhs_desc,
"indexjoin",
&format!("#{} {} #{}", node_nr_lhs, op, node_nr_rhs),
&processed_func,
),
lhs: lhs_peek,
lhs_idx,
op,
node_search_desc,
node_annos,
rhs_candidate: None,
};
}
fn next_candidates(&mut self) -> Option<Box<Iterator<Item = Match>>> {
if let Some(m_lhs) = self.lhs.peek().cloned() {
let it_nodes = self.op.retrieve_matches(&m_lhs[self.lhs_idx]).fuse();
let node_annos = self.node_annos.clone();
if let Some(name) = self.node_search_desc.qname.1.clone() {
if let Some(ns) = self.node_search_desc.qname.0.clone() {
// return the only possible annotation for each node
let key = Arc::from(AnnoKey {
ns: ns.clone(),
name: name.clone(),
});
let key_id = self.node_annos.get_key_id(key.as_ref());
return Some(Box::new(it_nodes
.filter_map(move |match_node| {
if let Some(key_id) = key_id {
if node_annos.get_value_for_item_by_id(&match_node.node, key_id).is_some() {
Some(Match {
node: match_node.node,
anno_key: key_id,
})
} else {
// this annotation was not found for this node, remove it from iterator
None
}
} else {
None
}
}))
);
} else {
let keys: Vec<usize> = self
.node_annos
.get_qnames(&name)
.into_iter()
.filter_map(|k| self.node_annos.get_key_id(&k))
.collect();
// return all annotations with the correct name for each node
return Some(Box::new(it_nodes.flat_map(move |match_node| {
let mut matches: Vec<Match> = Vec::new();
matches.reserve(keys.len());
for key_id in keys.clone().into_iter() {
if node_annos
.get_value_for_item_by_id(&match_node.node, key_id)
.is_some()
{
matches.push(Match {
node: match_node.node,
anno_key: key_id,
})
}
}
matches.into_iter()
})));
}
} else {
// return all annotations for each node
return Some(Box::new(it_nodes.flat_map(move |match_node| {
let anno_keys = node_annos.get_all_keys_for_item(&match_node.node);
let mut matches: Vec<Match> = Vec::new();
matches.reserve(anno_keys.len());
for anno_key in anno_keys.into_iter() {
if let Some(key_id) = node_annos.get_key_id(&anno_key) {
matches.push(Match {
node: match_node.node,
anno_key: key_id,
});
}
}
matches.into_iter()
})));
}
}
return None;
}
}
impl<'a> ExecutionNode for IndexJoin<'a> {
fn as_iter(&mut self) -> &mut Iterator<Item = Vec<Match>> {
self
}
fn get_desc(&self) -> Option<&Desc> {
Some(&self.desc)
}
}
impl<'a> Iterator for IndexJoin<'a> {
type Item = Vec<Match>;
fn next(&mut self) -> Option<Vec<Match>> {
// lazily initialize the RHS candidates for the first LHS
if self.rhs_candidate.is_none() {
self.rhs_candidate = if let Some(rhs) = self.next_candidates() {
Some(rhs.into_iter().peekable())
} else {
None
};
}
if self.rhs_candidate.is_none() {
return None;
}
loop {
if let Some(m_lhs) = self.lhs.peek() {
let rhs_candidate = self.rhs_candidate.as_mut().unwrap();
while let Some(mut m_rhs) = rhs_candidate.next() {
// check if all filters are true
let mut filter_result = true;
for f in self.node_search_desc.cond.iter() {
if !(f)(&m_rhs) {
filter_result = false;
break;
}
}
if filter_result {
// replace the annotation with a constant value if needed
if let Some(ref const_anno) = self.node_search_desc.const_output {
m_rhs.anno_key = const_anno.clone();
}
// check if lhs and rhs are equal and if this is allowed in this query
if self.op.is_reflexive()
|| m_lhs[self.lhs_idx].node != m_rhs.node
|| m_lhs[self.lhs_idx].anno_key != m_rhs.anno_key
{
// filters have been checked, return the result
let mut result = m_lhs.clone();
let matched_node = m_rhs.node;
result.push(m_rhs);
if self.node_search_desc.const_output.is_some() {
// only return the one unique constAnno for this node and no duplicates
// skip all RHS candidates that have the same node ID
loop {
if let Some(next_match) = rhs_candidate.peek() {
if next_match.node != matched_node {
break;
}
} else {
break;
}
rhs_candidate.next();
}
}
return Some(result);
}
}
}
}
// consume next outer
if self.lhs.next().is_none() {
return None;
}
// inner was completed once, get new candidates
self.rhs_candidate = if let Some(rhs) = self.next_candidates() {
Some(rhs.into_iter().peekable())
} else {
None
};
}
}
}