1 /*
2 * SysDB - src/core/store_lookup.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 /*
29 * This module implements operators which may be used to select contents of
30 * the store by matching various attributes of the stored objects. For now, a
31 * simple full table scan is supported only.
32 */
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif /* HAVE_CONFIG_H */
38 #include "sysdb.h"
39 #include "core/store-private.h"
40 #include "core/object.h"
42 #include <assert.h>
44 #include <sys/types.h>
45 #include <regex.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #include <limits.h>
52 /*
53 * private data types
54 */
56 typedef struct {
57 sdb_store_matcher_t *m;
58 sdb_store_matcher_t *filter;
59 sdb_store_lookup_cb cb;
60 void *user_data;
61 } scan_iter_data_t;
63 /*
64 * private helper functions
65 */
67 static int
68 scan_iter(sdb_store_obj_t *obj, void *user_data)
69 {
70 scan_iter_data_t *d = user_data;
72 if (sdb_store_matcher_matches(d->m, obj, d->filter))
73 return d->cb(obj, d->user_data);
74 return 0;
75 } /* scan_iter */
77 /*
78 * matcher implementations
79 */
81 static int
82 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
83 sdb_store_matcher_t *filter)
84 {
85 int status;
87 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
88 assert(OP_M(m)->left && OP_M(m)->right);
90 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
92 /* lazy evaluation */
93 if ((! status) && (m->type == MATCHER_AND))
94 return status;
95 else if (status && (m->type == MATCHER_OR))
96 return status;
98 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
99 } /* match_logical */
101 static int
102 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
103 sdb_store_matcher_t *filter)
104 {
105 assert(m->type == MATCHER_NOT);
106 assert(UOP_M(m)->op);
108 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
109 } /* match_unary */
111 static int
112 match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
113 sdb_store_matcher_t *filter)
114 {
115 sdb_avltree_iter_t *iter = NULL;
116 int status;
117 int all = 0;
119 assert((m->type == MATCHER_SERVICE)
120 || (m->type == MATCHER_METRIC)
121 || (m->type == MATCHER_ATTRIBUTE));
123 /* TODO: support all object types */
124 if (obj->type != SDB_HOST)
125 return 0;
127 /* negated matchers should only match if the respective positive matchers
128 * do not match; that is if the negated matcher matchers *all* children */
129 if ((CHILD_M(m)->m->type == MATCHER_NE)
130 || (CHILD_M(m)->m->type == MATCHER_NREGEX))
131 all = 1;
133 if (m->type == MATCHER_SERVICE)
134 iter = sdb_avltree_get_iter(HOST(obj)->services);
135 else if (m->type == MATCHER_METRIC)
136 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
137 else if (m->type == MATCHER_ATTRIBUTE)
138 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
140 status = all;
141 while (sdb_avltree_iter_has_next(iter)) {
142 sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
143 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
144 continue;
146 if (sdb_store_matcher_matches(CHILD_M(m)->m, child, filter)) {
147 if (! all) {
148 status = 1;
149 break;
150 }
151 } else if (all) {
152 status = 0;
153 break;
154 }
155 }
156 sdb_avltree_iter_destroy(iter);
157 return status;
158 } /* match_child */
160 /*
161 * cmp_expr:
162 * Compare the values of two expressions when evaluating them using the
163 * specified stored object and filter. Returns a value less than, equal to, or
164 * greater than zero if the value of the first expression compares less than,
165 * equal to, or greater than the value of the second expression. Returns
166 * INT_MAX if any of the expressions could not be evaluated or if any of them
167 * evaluated to NULL.
168 */
169 static int
170 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
171 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
172 {
173 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
174 int status;
176 if (sdb_store_expr_eval(e1, obj, &v1, filter))
177 return INT_MAX;
178 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
179 sdb_data_free_datum(&v1);
180 return INT_MAX;
181 }
183 if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
184 status = INT_MAX;
185 else if (v1.type == v2.type)
186 status = sdb_data_cmp(&v1, &v2);
187 else if ((e1->data_type >= 0) && (e2->data_type >= 0))
188 status = INT_MAX;
189 else
190 status = sdb_data_strcmp(&v1, &v2);
192 sdb_data_free_datum(&v1);
193 sdb_data_free_datum(&v2);
194 return status;
195 } /* cmp_expr */
197 static int
198 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
199 sdb_store_matcher_t *filter)
200 {
201 int status;
202 assert(m->type == MATCHER_LT);
203 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
204 return (status != INT_MAX) && (status < 0);
205 } /* match_lt */
207 static int
208 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
209 sdb_store_matcher_t *filter)
210 {
211 int status;
212 assert(m->type == MATCHER_LE);
213 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
214 return (status != INT_MAX) && (status <= 0);
215 } /* match_le */
217 static int
218 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
219 sdb_store_matcher_t *filter)
220 {
221 int status;
222 assert(m->type == MATCHER_EQ);
223 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
224 return (status != INT_MAX) && (! status);
225 } /* match_eq */
227 static int
228 match_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
229 sdb_store_matcher_t *filter)
230 {
231 int status;
232 assert(m->type == MATCHER_NE);
233 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
234 return (status != INT_MAX) && status;
235 } /* match_ne */
237 static int
238 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
239 sdb_store_matcher_t *filter)
240 {
241 int status;
242 assert(m->type == MATCHER_GE);
243 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
244 return (status != INT_MAX) && (status >= 0);
245 } /* match_ge */
247 static int
248 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
249 sdb_store_matcher_t *filter)
250 {
251 int status;
252 assert(m->type == MATCHER_GT);
253 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
254 return (status != INT_MAX) && (status > 0);
255 } /* match_gt */
257 static int
258 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
259 sdb_store_matcher_t *filter)
260 {
261 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
262 int status = 1;
264 assert(m->type == MATCHER_IN);
266 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
267 || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
268 status = 0;
270 if (status)
271 status = sdb_data_inarray(&value, &array);
273 sdb_data_free_datum(&value);
274 sdb_data_free_datum(&array);
275 return status;
276 } /* match_in */
278 static int
279 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
280 sdb_store_matcher_t *filter)
281 {
282 sdb_data_t v = SDB_DATA_INIT;
283 int status = 0;
285 regex_t regex;
286 _Bool free_regex = 0;
288 assert((m->type == MATCHER_REGEX)
289 || (m->type == MATCHER_NREGEX));
291 if (! CMP_M(m)->right->type) {
292 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
293 regex = CMP_M(m)->right->data.data.re.regex;
294 }
295 else {
296 sdb_data_t tmp = SDB_DATA_INIT;
297 char *raw;
299 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
300 return 0;
302 if (tmp.type != SDB_TYPE_STRING) {
303 sdb_data_free_datum(&tmp);
304 return 0;
305 }
307 raw = tmp.data.string;
308 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
309 free(raw);
310 return 0;
311 }
313 regex = tmp.data.re.regex;
314 free_regex = 1;
315 free(tmp.data.re.raw);
316 free(raw);
317 }
319 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
320 || (sdb_data_isnull(&v)))
321 status = 0;
322 else {
323 char value[sdb_data_strlen(&v) + 1];
324 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
325 status = 0;
326 else if (! regexec(®ex, value, 0, NULL, 0))
327 status = 1;
328 }
330 if (free_regex)
331 regfree(®ex);
332 sdb_data_free_datum(&v);
333 if (m->type == MATCHER_NREGEX)
334 return !status;
335 return status;
336 } /* match_regex */
338 static int
339 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
340 sdb_store_matcher_t *filter)
341 {
342 sdb_data_t v = SDB_DATA_INIT;
343 int status;
345 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
347 /* TODO: this might hide real errors;
348 * improve error reporting and propagation */
349 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
350 || sdb_data_isnull(&v))
351 status = 1;
352 else
353 status = 0;
355 sdb_data_free_datum(&v);
356 if (m->type == MATCHER_ISNNULL)
357 return !status;
358 return status;
359 } /* match_isnull */
361 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
362 sdb_store_matcher_t *);
364 /* this array needs to be indexable by the matcher types;
365 * -> update the enum in store-private.h when updating this */
366 static matcher_cb
367 matchers[] = {
368 match_logical,
369 match_logical,
370 match_unary,
371 match_child,
372 match_child,
373 match_child,
374 match_lt,
375 match_le,
376 match_eq,
377 match_ne,
378 match_ge,
379 match_gt,
380 match_in,
381 match_regex,
382 match_regex,
383 match_isnull,
384 match_isnull,
385 };
387 /*
388 * private matcher types
389 */
391 static int
392 op_matcher_init(sdb_object_t *obj, va_list ap)
393 {
394 M(obj)->type = va_arg(ap, int);
395 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
396 return -1;
398 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
399 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
400 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
401 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
403 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
404 return -1;
405 return 0;
406 } /* op_matcher_init */
408 static void
409 op_matcher_destroy(sdb_object_t *obj)
410 {
411 if (OP_M(obj)->left)
412 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
413 if (OP_M(obj)->right)
414 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
415 } /* op_matcher_destroy */
417 static int
418 child_matcher_init(sdb_object_t *obj, va_list ap)
419 {
420 M(obj)->type = va_arg(ap, int);
421 CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
423 if (! CHILD_M(obj)->m)
424 return -1;
426 sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
427 return 0;
428 } /* child_matcher_init */
430 static void
431 child_matcher_destroy(sdb_object_t *obj)
432 {
433 sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
434 } /* child_matcher_destroy */
436 static int
437 cmp_matcher_init(sdb_object_t *obj, va_list ap)
438 {
439 M(obj)->type = va_arg(ap, int);
441 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
442 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
443 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
444 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
446 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
447 return -1;
448 return 0;
449 } /* cmp_matcher_init */
451 static void
452 cmp_matcher_destroy(sdb_object_t *obj)
453 {
454 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
455 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
456 } /* cmp_matcher_destroy */
458 static int
459 uop_matcher_init(sdb_object_t *obj, va_list ap)
460 {
461 M(obj)->type = va_arg(ap, int);
462 if (M(obj)->type != MATCHER_NOT)
463 return -1;
465 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
466 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
468 if (! UOP_M(obj)->op)
469 return -1;
470 return 0;
471 } /* uop_matcher_init */
473 static void
474 uop_matcher_destroy(sdb_object_t *obj)
475 {
476 if (UOP_M(obj)->op)
477 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
478 } /* uop_matcher_destroy */
480 static int
481 isnull_matcher_init(sdb_object_t *obj, va_list ap)
482 {
483 M(obj)->type = va_arg(ap, int);
484 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
485 return -1;
487 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
488 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
489 return 0;
490 } /* isnull_matcher_init */
492 static void
493 isnull_matcher_destroy(sdb_object_t *obj)
494 {
495 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
496 ISNULL_M(obj)->expr = NULL;
497 } /* isnull_matcher_destroy */
499 static sdb_type_t op_type = {
500 /* size = */ sizeof(op_matcher_t),
501 /* init = */ op_matcher_init,
502 /* destroy = */ op_matcher_destroy,
503 };
505 static sdb_type_t uop_type = {
506 /* size = */ sizeof(uop_matcher_t),
507 /* init = */ uop_matcher_init,
508 /* destroy = */ uop_matcher_destroy,
509 };
511 static sdb_type_t child_type = {
512 /* size = */ sizeof(child_matcher_t),
513 /* init = */ child_matcher_init,
514 /* destroy = */ child_matcher_destroy,
515 };
517 static sdb_type_t cmp_type = {
518 /* size = */ sizeof(cmp_matcher_t),
519 /* init = */ cmp_matcher_init,
520 /* destroy = */ cmp_matcher_destroy,
521 };
523 static sdb_type_t isnull_type = {
524 /* size = */ sizeof(isnull_matcher_t),
525 /* init = */ isnull_matcher_init,
526 /* destroy = */ isnull_matcher_destroy,
527 };
529 /*
530 * public API
531 */
533 sdb_store_matcher_t *
534 sdb_store_child_matcher(int type, sdb_store_matcher_t *m)
535 {
536 if (type == SDB_SERVICE)
537 type = MATCHER_SERVICE;
538 else if (type == SDB_METRIC)
539 type = MATCHER_METRIC;
540 else if (type == SDB_ATTRIBUTE)
541 type = MATCHER_ATTRIBUTE;
542 else
543 return NULL;
544 return M(sdb_object_create("any-matcher", child_type, type, m));
545 } /* sdb_store_child_matcher */
547 sdb_store_matcher_t *
548 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
549 {
550 return M(sdb_object_create("lt-matcher", cmp_type,
551 MATCHER_LT, left, right));
552 } /* sdb_store_lt_matcher */
554 sdb_store_matcher_t *
555 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
556 {
557 return M(sdb_object_create("le-matcher", cmp_type,
558 MATCHER_LE, left, right));
559 } /* sdb_store_le_matcher */
561 sdb_store_matcher_t *
562 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
563 {
564 return M(sdb_object_create("eq-matcher", cmp_type,
565 MATCHER_EQ, left, right));
566 } /* sdb_store_eq_matcher */
568 sdb_store_matcher_t *
569 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
570 {
571 return M(sdb_object_create("ne-matcher", cmp_type,
572 MATCHER_NE, left, right));
573 } /* sdb_store_ne_matcher */
575 sdb_store_matcher_t *
576 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
577 {
578 return M(sdb_object_create("ge-matcher", cmp_type,
579 MATCHER_GE, left, right));
580 } /* sdb_store_ge_matcher */
582 sdb_store_matcher_t *
583 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
584 {
585 return M(sdb_object_create("gt-matcher", cmp_type,
586 MATCHER_GT, left, right));
587 } /* sdb_store_gt_matcher */
589 sdb_store_matcher_t *
590 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
591 {
592 return M(sdb_object_create("in-matcher", cmp_type,
593 MATCHER_IN, left, right));
594 } /* sdb_store_in_matcher */
596 sdb_store_matcher_t *
597 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
598 {
599 if (! right->type) {
600 if ((right->data.type != SDB_TYPE_STRING)
601 && (right->data.type != SDB_TYPE_REGEX))
602 return NULL;
604 if (right->data.type == SDB_TYPE_STRING) {
605 char *raw = right->data.data.string;
606 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
607 return NULL;
608 free(raw);
609 }
610 }
611 return M(sdb_object_create("regex-matcher", cmp_type,
612 MATCHER_REGEX, left, right));
613 } /* sdb_store_regex_matcher */
615 sdb_store_matcher_t *
616 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
617 {
618 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
619 if (! m)
620 return NULL;
621 m->type = MATCHER_NREGEX;
622 return m;
623 } /* sdb_store_nregex_matcher */
625 sdb_store_matcher_t *
626 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
627 {
628 return M(sdb_object_create("isnull-matcher", isnull_type,
629 MATCHER_ISNULL, expr));
630 } /* sdb_store_isnull_matcher */
632 sdb_store_matcher_t *
633 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
634 {
635 return M(sdb_object_create("isnull-matcher", isnull_type,
636 MATCHER_ISNNULL, expr));
637 } /* sdb_store_isnnull_matcher */
639 sdb_store_matcher_op_cb
640 sdb_store_parse_matcher_op(const char *op)
641 {
642 if (! strcasecmp(op, "<"))
643 return sdb_store_lt_matcher;
644 else if (! strcasecmp(op, "<="))
645 return sdb_store_le_matcher;
646 else if (! strcasecmp(op, "="))
647 return sdb_store_eq_matcher;
648 else if (! strcasecmp(op, "!="))
649 return sdb_store_ne_matcher;
650 else if (! strcasecmp(op, ">="))
651 return sdb_store_ge_matcher;
652 else if (! strcasecmp(op, ">"))
653 return sdb_store_gt_matcher;
654 else if (! strcasecmp(op, "=~"))
655 return sdb_store_regex_matcher;
656 else if (! strcasecmp(op, "!~"))
657 return sdb_store_nregex_matcher;
658 return NULL;
659 } /* sdb_store_parse_matcher_op */
661 int
662 sdb_store_parse_object_type(const char *name)
663 {
664 if (! strcasecmp(name, "host"))
665 return SDB_HOST;
666 else if (! strcasecmp(name, "service"))
667 return SDB_SERVICE;
668 else if (! strcasecmp(name, "metric"))
669 return SDB_METRIC;
670 else if (! strcasecmp(name, "attribute"))
671 return SDB_ATTRIBUTE;
672 return -1;
673 } /* sdb_store_parse_object_type */
675 int
676 sdb_store_parse_object_type_plural(const char *name)
677 {
678 if (! strcasecmp(name, "hosts"))
679 return SDB_HOST;
680 else if (! strcasecmp(name, "services"))
681 return SDB_SERVICE;
682 else if (! strcasecmp(name, "metrics"))
683 return SDB_METRIC;
684 else if (! strcasecmp(name, "attributes"))
685 return SDB_ATTRIBUTE;
686 return -1;
687 } /* sdb_store_parse_object_type_plural */
689 int
690 sdb_store_parse_field_name(const char *name)
691 {
692 if (! strcasecmp(name, "name"))
693 return SDB_FIELD_NAME;
694 else if (! strcasecmp(name, "last_update"))
695 return SDB_FIELD_LAST_UPDATE;
696 else if (! strcasecmp(name, "age"))
697 return SDB_FIELD_AGE;
698 else if (! strcasecmp(name, "interval"))
699 return SDB_FIELD_INTERVAL;
700 else if (! strcasecmp(name, "backend"))
701 return SDB_FIELD_BACKEND;
702 return -1;
703 } /* sdb_store_parse_field_name */
705 sdb_store_matcher_t *
706 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
707 {
708 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
709 left, right));
710 } /* sdb_store_dis_matcher */
712 sdb_store_matcher_t *
713 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
714 {
715 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
716 left, right));
717 } /* sdb_store_con_matcher */
719 sdb_store_matcher_t *
720 sdb_store_inv_matcher(sdb_store_matcher_t *m)
721 {
722 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
723 } /* sdb_store_inv_matcher */
725 int
726 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
727 sdb_store_matcher_t *filter)
728 {
729 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
730 return 0;
732 /* "NULL" always matches */
733 if ((! m) || (! obj))
734 return 1;
736 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
737 return 0;
739 return matchers[m->type](m, obj, filter);
740 } /* sdb_store_matcher_matches */
742 int
743 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
744 sdb_store_lookup_cb cb, void *user_data)
745 {
746 scan_iter_data_t data = { m, filter, cb, user_data };
748 if (! cb)
749 return -1;
750 return sdb_store_iterate(scan_iter, &data);
751 } /* sdb_store_scan */
753 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */