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_iter(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 = (int)(m->type == MATCHER_ALL);
119 assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL));
121 /* TODO: support all object types */
122 if (obj->type != SDB_HOST)
123 return 0;
125 if (ITER_M(m)->type == SDB_SERVICE)
126 iter = sdb_avltree_get_iter(HOST(obj)->services);
127 else if (ITER_M(m)->type == SDB_METRIC)
128 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
129 else if (ITER_M(m)->type == SDB_ATTRIBUTE)
130 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
132 status = all;
133 while (sdb_avltree_iter_has_next(iter)) {
134 sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
135 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
136 continue;
138 if (sdb_store_matcher_matches(ITER_M(m)->m, child, filter)) {
139 if (! all) {
140 status = 1;
141 break;
142 }
143 } else if (all) {
144 status = 0;
145 break;
146 }
147 }
148 sdb_avltree_iter_destroy(iter);
149 return status;
150 } /* match_iter */
152 /*
153 * cmp_expr:
154 * Compare the values of two expressions when evaluating them using the
155 * specified stored object and filter. Returns a value less than, equal to, or
156 * greater than zero if the value of the first expression compares less than,
157 * equal to, or greater than the value of the second expression. Returns
158 * INT_MAX if any of the expressions could not be evaluated or if any of them
159 * evaluated to NULL.
160 */
161 static int
162 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
163 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
164 {
165 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
166 int status;
168 if (sdb_store_expr_eval(e1, obj, &v1, filter))
169 return INT_MAX;
170 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
171 sdb_data_free_datum(&v1);
172 return INT_MAX;
173 }
175 if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
176 status = INT_MAX;
177 else if (v1.type == v2.type)
178 status = sdb_data_cmp(&v1, &v2);
179 else if ((e1->data_type >= 0) && (e2->data_type >= 0))
180 status = INT_MAX;
181 else
182 status = sdb_data_strcmp(&v1, &v2);
184 sdb_data_free_datum(&v1);
185 sdb_data_free_datum(&v2);
186 return status;
187 } /* cmp_expr */
189 static int
190 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
191 sdb_store_matcher_t *filter)
192 {
193 int status;
194 assert(m->type == MATCHER_LT);
195 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
196 return (status != INT_MAX) && (status < 0);
197 } /* match_lt */
199 static int
200 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
201 sdb_store_matcher_t *filter)
202 {
203 int status;
204 assert(m->type == MATCHER_LE);
205 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
206 return (status != INT_MAX) && (status <= 0);
207 } /* match_le */
209 static int
210 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
211 sdb_store_matcher_t *filter)
212 {
213 int status;
214 assert(m->type == MATCHER_EQ);
215 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
216 return (status != INT_MAX) && (! status);
217 } /* match_eq */
219 static int
220 match_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
221 sdb_store_matcher_t *filter)
222 {
223 int status;
224 assert(m->type == MATCHER_NE);
225 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
226 return (status != INT_MAX) && status;
227 } /* match_ne */
229 static int
230 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
231 sdb_store_matcher_t *filter)
232 {
233 int status;
234 assert(m->type == MATCHER_GE);
235 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
236 return (status != INT_MAX) && (status >= 0);
237 } /* match_ge */
239 static int
240 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
241 sdb_store_matcher_t *filter)
242 {
243 int status;
244 assert(m->type == MATCHER_GT);
245 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
246 return (status != INT_MAX) && (status > 0);
247 } /* match_gt */
249 static int
250 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
251 sdb_store_matcher_t *filter)
252 {
253 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
254 int status = 1;
256 assert(m->type == MATCHER_IN);
258 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
259 || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
260 status = 0;
262 if (status)
263 status = sdb_data_inarray(&value, &array);
265 sdb_data_free_datum(&value);
266 sdb_data_free_datum(&array);
267 return status;
268 } /* match_in */
270 static int
271 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
272 sdb_store_matcher_t *filter)
273 {
274 sdb_data_t v = SDB_DATA_INIT;
275 int status = 0;
277 regex_t regex;
278 _Bool free_regex = 0;
280 assert((m->type == MATCHER_REGEX)
281 || (m->type == MATCHER_NREGEX));
283 if (! CMP_M(m)->right->type) {
284 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
285 regex = CMP_M(m)->right->data.data.re.regex;
286 }
287 else {
288 sdb_data_t tmp = SDB_DATA_INIT;
289 char *raw;
291 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
292 return 0;
294 if (tmp.type != SDB_TYPE_STRING) {
295 sdb_data_free_datum(&tmp);
296 return 0;
297 }
299 raw = tmp.data.string;
300 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
301 free(raw);
302 return 0;
303 }
305 regex = tmp.data.re.regex;
306 free_regex = 1;
307 free(tmp.data.re.raw);
308 free(raw);
309 }
311 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
312 || (sdb_data_isnull(&v)))
313 status = 0;
314 else {
315 char value[sdb_data_strlen(&v) + 1];
316 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
317 status = 0;
318 else if (! regexec(®ex, value, 0, NULL, 0))
319 status = 1;
320 }
322 if (free_regex)
323 regfree(®ex);
324 sdb_data_free_datum(&v);
325 if (m->type == MATCHER_NREGEX)
326 return !status;
327 return status;
328 } /* match_regex */
330 static int
331 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
332 sdb_store_matcher_t *filter)
333 {
334 sdb_data_t v = SDB_DATA_INIT;
335 int status;
337 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
339 /* TODO: this might hide real errors;
340 * improve error reporting and propagation */
341 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
342 || sdb_data_isnull(&v))
343 status = 1;
344 else
345 status = 0;
347 sdb_data_free_datum(&v);
348 if (m->type == MATCHER_ISNNULL)
349 return !status;
350 return status;
351 } /* match_isnull */
353 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
354 sdb_store_matcher_t *);
356 /* this array needs to be indexable by the matcher types;
357 * -> update the enum in store-private.h when updating this */
358 static matcher_cb
359 matchers[] = {
360 match_logical,
361 match_logical,
362 match_unary,
363 match_iter,
364 match_iter,
365 match_lt,
366 match_le,
367 match_eq,
368 match_ne,
369 match_ge,
370 match_gt,
371 match_in,
372 match_regex,
373 match_regex,
374 match_isnull,
375 match_isnull,
376 };
378 /*
379 * private matcher types
380 */
382 static int
383 op_matcher_init(sdb_object_t *obj, va_list ap)
384 {
385 M(obj)->type = va_arg(ap, int);
386 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
387 return -1;
389 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
390 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
391 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
392 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
394 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
395 return -1;
396 return 0;
397 } /* op_matcher_init */
399 static void
400 op_matcher_destroy(sdb_object_t *obj)
401 {
402 if (OP_M(obj)->left)
403 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
404 if (OP_M(obj)->right)
405 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
406 } /* op_matcher_destroy */
408 static int
409 iter_matcher_init(sdb_object_t *obj, va_list ap)
410 {
411 M(obj)->type = va_arg(ap, int);
412 ITER_M(obj)->type = va_arg(ap, int);
413 ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
415 if (! ITER_M(obj)->m)
416 return -1;
418 sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
419 return 0;
420 } /* iter_matcher_init */
422 static void
423 iter_matcher_destroy(sdb_object_t *obj)
424 {
425 sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
426 } /* iter_matcher_destroy */
428 static int
429 cmp_matcher_init(sdb_object_t *obj, va_list ap)
430 {
431 M(obj)->type = va_arg(ap, int);
433 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
434 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
435 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
436 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
438 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
439 return -1;
440 return 0;
441 } /* cmp_matcher_init */
443 static void
444 cmp_matcher_destroy(sdb_object_t *obj)
445 {
446 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
447 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
448 } /* cmp_matcher_destroy */
450 static int
451 uop_matcher_init(sdb_object_t *obj, va_list ap)
452 {
453 M(obj)->type = va_arg(ap, int);
454 if (M(obj)->type != MATCHER_NOT)
455 return -1;
457 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
458 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
460 if (! UOP_M(obj)->op)
461 return -1;
462 return 0;
463 } /* uop_matcher_init */
465 static void
466 uop_matcher_destroy(sdb_object_t *obj)
467 {
468 if (UOP_M(obj)->op)
469 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
470 } /* uop_matcher_destroy */
472 static int
473 isnull_matcher_init(sdb_object_t *obj, va_list ap)
474 {
475 M(obj)->type = va_arg(ap, int);
476 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
477 return -1;
479 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
480 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
481 return 0;
482 } /* isnull_matcher_init */
484 static void
485 isnull_matcher_destroy(sdb_object_t *obj)
486 {
487 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
488 ISNULL_M(obj)->expr = NULL;
489 } /* isnull_matcher_destroy */
491 static sdb_type_t op_type = {
492 /* size = */ sizeof(op_matcher_t),
493 /* init = */ op_matcher_init,
494 /* destroy = */ op_matcher_destroy,
495 };
497 static sdb_type_t uop_type = {
498 /* size = */ sizeof(uop_matcher_t),
499 /* init = */ uop_matcher_init,
500 /* destroy = */ uop_matcher_destroy,
501 };
503 static sdb_type_t iter_type = {
504 /* size = */ sizeof(iter_matcher_t),
505 /* init = */ iter_matcher_init,
506 /* destroy = */ iter_matcher_destroy,
507 };
509 static sdb_type_t cmp_type = {
510 /* size = */ sizeof(cmp_matcher_t),
511 /* init = */ cmp_matcher_init,
512 /* destroy = */ cmp_matcher_destroy,
513 };
515 static sdb_type_t isnull_type = {
516 /* size = */ sizeof(isnull_matcher_t),
517 /* init = */ isnull_matcher_init,
518 /* destroy = */ isnull_matcher_destroy,
519 };
521 /*
522 * public API
523 */
525 sdb_store_matcher_t *
526 sdb_store_any_matcher(int type, sdb_store_matcher_t *m)
527 {
528 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
529 && (type != SDB_ATTRIBUTE))
530 return NULL;
531 return M(sdb_object_create("any-matcher", iter_type,
532 MATCHER_ANY, type, m));
533 } /* sdb_store_any_matcher */
535 sdb_store_matcher_t *
536 sdb_store_all_matcher(int type, sdb_store_matcher_t *m)
537 {
538 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
539 && (type != SDB_ATTRIBUTE))
540 return NULL;
541 return M(sdb_object_create("all-matcher", iter_type,
542 MATCHER_ALL, type, m));
543 } /* sdb_store_all_matcher */
545 sdb_store_matcher_t *
546 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
547 {
548 return M(sdb_object_create("lt-matcher", cmp_type,
549 MATCHER_LT, left, right));
550 } /* sdb_store_lt_matcher */
552 sdb_store_matcher_t *
553 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
554 {
555 return M(sdb_object_create("le-matcher", cmp_type,
556 MATCHER_LE, left, right));
557 } /* sdb_store_le_matcher */
559 sdb_store_matcher_t *
560 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
561 {
562 return M(sdb_object_create("eq-matcher", cmp_type,
563 MATCHER_EQ, left, right));
564 } /* sdb_store_eq_matcher */
566 sdb_store_matcher_t *
567 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
568 {
569 return M(sdb_object_create("ne-matcher", cmp_type,
570 MATCHER_NE, left, right));
571 } /* sdb_store_ne_matcher */
573 sdb_store_matcher_t *
574 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
575 {
576 return M(sdb_object_create("ge-matcher", cmp_type,
577 MATCHER_GE, left, right));
578 } /* sdb_store_ge_matcher */
580 sdb_store_matcher_t *
581 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
582 {
583 return M(sdb_object_create("gt-matcher", cmp_type,
584 MATCHER_GT, left, right));
585 } /* sdb_store_gt_matcher */
587 sdb_store_matcher_t *
588 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
589 {
590 return M(sdb_object_create("in-matcher", cmp_type,
591 MATCHER_IN, left, right));
592 } /* sdb_store_in_matcher */
594 sdb_store_matcher_t *
595 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
596 {
597 if (! right->type) {
598 if ((right->data.type != SDB_TYPE_STRING)
599 && (right->data.type != SDB_TYPE_REGEX))
600 return NULL;
602 if (right->data.type == SDB_TYPE_STRING) {
603 char *raw = right->data.data.string;
604 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
605 return NULL;
606 free(raw);
607 }
608 }
609 return M(sdb_object_create("regex-matcher", cmp_type,
610 MATCHER_REGEX, left, right));
611 } /* sdb_store_regex_matcher */
613 sdb_store_matcher_t *
614 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
615 {
616 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
617 if (! m)
618 return NULL;
619 m->type = MATCHER_NREGEX;
620 return m;
621 } /* sdb_store_nregex_matcher */
623 sdb_store_matcher_t *
624 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
625 {
626 return M(sdb_object_create("isnull-matcher", isnull_type,
627 MATCHER_ISNULL, expr));
628 } /* sdb_store_isnull_matcher */
630 sdb_store_matcher_t *
631 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
632 {
633 return M(sdb_object_create("isnull-matcher", isnull_type,
634 MATCHER_ISNNULL, expr));
635 } /* sdb_store_isnnull_matcher */
637 sdb_store_matcher_op_cb
638 sdb_store_parse_matcher_op(const char *op)
639 {
640 if (! strcasecmp(op, "<"))
641 return sdb_store_lt_matcher;
642 else if (! strcasecmp(op, "<="))
643 return sdb_store_le_matcher;
644 else if (! strcasecmp(op, "="))
645 return sdb_store_eq_matcher;
646 else if (! strcasecmp(op, "!="))
647 return sdb_store_ne_matcher;
648 else if (! strcasecmp(op, ">="))
649 return sdb_store_ge_matcher;
650 else if (! strcasecmp(op, ">"))
651 return sdb_store_gt_matcher;
652 else if (! strcasecmp(op, "=~"))
653 return sdb_store_regex_matcher;
654 else if (! strcasecmp(op, "!~"))
655 return sdb_store_nregex_matcher;
656 return NULL;
657 } /* sdb_store_parse_matcher_op */
659 int
660 sdb_store_parse_object_type(const char *name)
661 {
662 if (! strcasecmp(name, "host"))
663 return SDB_HOST;
664 else if (! strcasecmp(name, "service"))
665 return SDB_SERVICE;
666 else if (! strcasecmp(name, "metric"))
667 return SDB_METRIC;
668 else if (! strcasecmp(name, "attribute"))
669 return SDB_ATTRIBUTE;
670 return -1;
671 } /* sdb_store_parse_object_type */
673 int
674 sdb_store_parse_object_type_plural(const char *name)
675 {
676 if (! strcasecmp(name, "hosts"))
677 return SDB_HOST;
678 else if (! strcasecmp(name, "services"))
679 return SDB_SERVICE;
680 else if (! strcasecmp(name, "metrics"))
681 return SDB_METRIC;
682 else if (! strcasecmp(name, "attributes"))
683 return SDB_ATTRIBUTE;
684 return -1;
685 } /* sdb_store_parse_object_type_plural */
687 int
688 sdb_store_parse_field_name(const char *name)
689 {
690 if (! strcasecmp(name, "name"))
691 return SDB_FIELD_NAME;
692 else if (! strcasecmp(name, "last_update"))
693 return SDB_FIELD_LAST_UPDATE;
694 else if (! strcasecmp(name, "age"))
695 return SDB_FIELD_AGE;
696 else if (! strcasecmp(name, "interval"))
697 return SDB_FIELD_INTERVAL;
698 else if (! strcasecmp(name, "backend"))
699 return SDB_FIELD_BACKEND;
700 return -1;
701 } /* sdb_store_parse_field_name */
703 sdb_store_matcher_t *
704 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
705 {
706 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
707 left, right));
708 } /* sdb_store_dis_matcher */
710 sdb_store_matcher_t *
711 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
712 {
713 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
714 left, right));
715 } /* sdb_store_con_matcher */
717 sdb_store_matcher_t *
718 sdb_store_inv_matcher(sdb_store_matcher_t *m)
719 {
720 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
721 } /* sdb_store_inv_matcher */
723 int
724 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
725 sdb_store_matcher_t *filter)
726 {
727 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
728 return 0;
730 /* "NULL" always matches */
731 if ((! m) || (! obj))
732 return 1;
734 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
735 return 0;
737 return matchers[m->type](m, obj, filter);
738 } /* sdb_store_matcher_matches */
740 int
741 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
742 sdb_store_lookup_cb cb, void *user_data)
743 {
744 scan_iter_data_t data = { m, filter, cb, user_data };
746 if (! cb)
747 return -1;
748 return sdb_store_iterate(scan_iter, &data);
749 } /* sdb_store_scan */
751 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */