|
|
@@ -18,6 +18,7 @@ pub trait Query {
|
|
18
|
18
|
pub enum QueryArgs {
|
|
19
|
19
|
ValuePattern(String),
|
|
20
|
20
|
KeyValue(String, String),
|
|
|
21
|
+ KeyValueRegex(String, String),
|
|
21
|
22
|
Intersection(Vec<QueryArgs>),
|
|
22
|
23
|
}
|
|
23
|
24
|
|
|
|
@@ -25,6 +26,7 @@ pub enum QueryArgs {
|
|
25
|
26
|
pub enum QueryKind {
|
|
26
|
27
|
ValuePattern(ValuePatternQuery),
|
|
27
|
28
|
KeyValue(KeyValueQuery),
|
|
|
29
|
+ KeyValueRegex(KeyValueRegexQuery),
|
|
28
|
30
|
Intersection(Vec<QueryKind>),
|
|
29
|
31
|
}
|
|
30
|
32
|
|
|
|
@@ -37,6 +39,9 @@ impl QueryArgs {
|
|
37
|
39
|
QueryArgs::KeyValue(k, v) => {
|
|
38
|
40
|
Ok(QueryKind::KeyValue(KeyValueQuery::new(k, v)))
|
|
39
|
41
|
},
|
|
|
42
|
+ QueryArgs::KeyValueRegex(k, v) => {
|
|
|
43
|
+ Ok(QueryKind::KeyValueRegex(KeyValueRegexQuery::new(k, &v)?))
|
|
|
44
|
+ },
|
|
40
|
45
|
QueryArgs::Intersection(queries) => {
|
|
41
|
46
|
let mut subqueries = Vec::with_capacity(queries.len());
|
|
42
|
47
|
for q in queries {
|
|
|
@@ -191,6 +196,96 @@ impl Query for KeyValueQuery {
|
|
191
|
196
|
}
|
|
192
|
197
|
}
|
|
193
|
198
|
|
|
|
199
|
+#[derive(Debug)]
|
|
|
200
|
+pub struct KeyValueRegexQuery {
|
|
|
201
|
+ key: String,
|
|
|
202
|
+ value_re: Regex,
|
|
|
203
|
+}
|
|
|
204
|
+
|
|
|
205
|
+impl KeyValueRegexQuery {
|
|
|
206
|
+ pub fn new<S: Into<String>>(key: S, value_pattern: &str) -> Result<Self, String> {
|
|
|
207
|
+ let value_re = Regex::new(value_pattern)
|
|
|
208
|
+ .map_err(|e| format!("{}", e))?;
|
|
|
209
|
+ Ok(KeyValueRegexQuery {
|
|
|
210
|
+ key: key.into(),
|
|
|
211
|
+ value_re,
|
|
|
212
|
+ })
|
|
|
213
|
+ }
|
|
|
214
|
+}
|
|
|
215
|
+
|
|
|
216
|
+impl Query for KeyValueRegexQuery {
|
|
|
217
|
+ type BI = (Vec<u32>, Vec<u32>);
|
|
|
218
|
+
|
|
|
219
|
+ fn create_block_index(&self, block: &PrimitiveBlock) -> Option<(Vec<u32>, Vec<u32>)> {
|
|
|
220
|
+ let mut key_indices = vec![];
|
|
|
221
|
+ let mut value_indices = vec![];
|
|
|
222
|
+
|
|
|
223
|
+ let key_bytes = self.key.as_bytes();
|
|
|
224
|
+
|
|
|
225
|
+ for (i, string) in block.raw_stringtable().iter().enumerate() {
|
|
|
226
|
+ if string.as_slice() == key_bytes {
|
|
|
227
|
+ key_indices.push(i as u32);
|
|
|
228
|
+ }
|
|
|
229
|
+
|
|
|
230
|
+ if let Ok(s) = ::std::str::from_utf8(string) {
|
|
|
231
|
+ if self.value_re.is_match(s) {
|
|
|
232
|
+ value_indices.push(i as u32);
|
|
|
233
|
+ }
|
|
|
234
|
+ }
|
|
|
235
|
+ }
|
|
|
236
|
+
|
|
|
237
|
+ if key_indices.is_empty() || value_indices.is_empty() {
|
|
|
238
|
+ // No matches possible for this block
|
|
|
239
|
+ return None;
|
|
|
240
|
+ }
|
|
|
241
|
+
|
|
|
242
|
+ key_indices.sort();
|
|
|
243
|
+ value_indices.sort();
|
|
|
244
|
+
|
|
|
245
|
+ Some((key_indices, value_indices))
|
|
|
246
|
+ }
|
|
|
247
|
+
|
|
|
248
|
+ fn node_matches(&self, bi: &Self::BI, node: &Node) -> bool {
|
|
|
249
|
+ for (key, val) in node.raw_tags() {
|
|
|
250
|
+ if bi.0.binary_search(&key).is_ok() && bi.1.binary_search(&val).is_ok() {
|
|
|
251
|
+ return true;
|
|
|
252
|
+ }
|
|
|
253
|
+ }
|
|
|
254
|
+ return false;
|
|
|
255
|
+ }
|
|
|
256
|
+
|
|
|
257
|
+ fn dense_node_matches(&self, bi: &Self::BI, dnode: &DenseNode) -> bool {
|
|
|
258
|
+ for (key, val) in dnode.raw_tags() {
|
|
|
259
|
+ if key >= 0 &&
|
|
|
260
|
+ val >= 0 &&
|
|
|
261
|
+ bi.0.binary_search(&(key as u32)).is_ok() &&
|
|
|
262
|
+ bi.1.binary_search(&(val as u32)).is_ok()
|
|
|
263
|
+ {
|
|
|
264
|
+ return true;
|
|
|
265
|
+ }
|
|
|
266
|
+ }
|
|
|
267
|
+ return false;
|
|
|
268
|
+ }
|
|
|
269
|
+
|
|
|
270
|
+ fn way_matches(&self, bi: &Self::BI, way: &Way) -> bool {
|
|
|
271
|
+ for (key, val) in way.raw_tags() {
|
|
|
272
|
+ if bi.0.binary_search(&key).is_ok() && bi.1.binary_search(&val).is_ok() {
|
|
|
273
|
+ return true;
|
|
|
274
|
+ }
|
|
|
275
|
+ }
|
|
|
276
|
+ return false;
|
|
|
277
|
+ }
|
|
|
278
|
+
|
|
|
279
|
+ fn relation_matches(&self, bi: &Self::BI, relation: &Relation) -> bool {
|
|
|
280
|
+ for (key, val) in relation.raw_tags() {
|
|
|
281
|
+ if bi.0.binary_search(&key).is_ok() && bi.1.binary_search(&val).is_ok() {
|
|
|
282
|
+ return true;
|
|
|
283
|
+ }
|
|
|
284
|
+ }
|
|
|
285
|
+ return false;
|
|
|
286
|
+ }
|
|
|
287
|
+}
|
|
|
288
|
+
|
|
194
|
289
|
pub fn find_query_matches<Q: Query>(
|
|
195
|
290
|
block: &PrimitiveBlock,
|
|
196
|
291
|
query: &Q,
|