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 static int
53 expr_eval2(sdb_store_expr_t *e1, sdb_data_t *v1,
54 sdb_store_expr_t *e2, sdb_data_t *v2,
55 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
56 {
57 if (e1->type) {
58 if (sdb_store_expr_eval(e1, obj, v1, filter))
59 return -1;
60 }
61 else
62 *v1 = e1->data;
63 if (e2->type) {
64 if (sdb_store_expr_eval(e2, obj, v2, filter)) {
65 if (e1->type)
66 sdb_data_free_datum(v1);
67 return -1;
68 }
69 }
70 else
71 *v2 = e2->data;
72 return 0;
73 } /* expr_eval2 */
75 static void
76 expr_free_datum2(sdb_store_expr_t *e1, sdb_data_t *v1,
77 sdb_store_expr_t *e2, sdb_data_t *v2)
78 {
79 if (e1->type)
80 sdb_data_free_datum(v1);
81 if (e2->type)
82 sdb_data_free_datum(v2);
83 } /* expr_free_datum2 */
85 /*
86 * matcher implementations
87 */
89 /*
90 * cmp_expr:
91 * Compare two values using the specified matcher operator. If strcmp_fallback
92 * is enabled, compare the string values in case of a type mismatch.
93 */
94 static int
95 match_cmp_value(int op, sdb_data_t *v1, sdb_data_t *v2, _Bool strcmp_fallback)
96 {
97 int status;
99 if (sdb_data_isnull(v1) || (sdb_data_isnull(v2)))
100 status = INT_MAX;
101 else if (v1->type == v2->type)
102 status = sdb_data_cmp(v1, v2);
103 else if (! strcmp_fallback)
104 status = INT_MAX;
105 else
106 status = sdb_data_strcmp(v1, v2);
108 if (status == INT_MAX)
109 return 0;
110 else if (op == MATCHER_LT)
111 return status < 0;
112 else if (op == MATCHER_LE)
113 return status <= 0;
114 else if (op == MATCHER_EQ)
115 return status == 0;
116 else if (op == MATCHER_NE)
117 return status != 0;
118 else if (op == MATCHER_GE)
119 return status >= 0;
120 else if (op == MATCHER_GT)
121 return status > 0;
122 return 0;
123 } /* match_cmp_value */
125 static int
126 match_regex_value(int op, sdb_data_t *v, sdb_data_t *re)
127 {
128 char value[sdb_data_strlen(v) + 1];
129 int status = 0;
131 assert((op == MATCHER_REGEX)
132 || (op == MATCHER_NREGEX));
134 if (sdb_data_isnull(v) || sdb_data_isnull(re))
135 return 0;
137 if (re->type == SDB_TYPE_STRING) {
138 sdb_data_t tmp = SDB_DATA_INIT;
140 if (sdb_data_parse(re->data.string, SDB_TYPE_REGEX, &tmp))
141 return 0;
143 sdb_data_free_datum(re);
144 *re = tmp;
145 }
146 else if (re->type != SDB_TYPE_REGEX)
147 return 0;
149 if (sdb_data_format(v, value, sizeof(value), SDB_UNQUOTED) < 0)
150 status = 0;
151 else if (! regexec(&re->data.re.regex, value, 0, NULL, 0))
152 status = 1;
154 if (op == MATCHER_NREGEX)
155 return !status;
156 return status;
157 } /* match_regex_value */
159 static int
160 match_value(int op, sdb_data_t *v1, sdb_data_t *v2, _Bool strcmp_fallback)
161 {
162 if ((op == MATCHER_REGEX) || (op == MATCHER_NREGEX))
163 return match_regex_value(op, v1, v2);
164 return match_cmp_value(op, v1, v2, strcmp_fallback);
165 } /* match_value */
167 static int
168 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
169 sdb_store_matcher_t *filter)
170 {
171 int status;
173 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
174 assert(OP_M(m)->left && OP_M(m)->right);
176 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
178 /* lazy evaluation */
179 if ((! status) && (m->type == MATCHER_AND))
180 return status;
181 else if (status && (m->type == MATCHER_OR))
182 return status;
184 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
185 } /* match_logical */
187 static int
188 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
189 sdb_store_matcher_t *filter)
190 {
191 assert(m->type == MATCHER_NOT);
192 assert(UOP_M(m)->op);
194 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
195 } /* match_unary */
197 /* iterate arrays: ANY/ALL <array> <cmp> <value> */
198 static int
199 match_iter_array(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
200 sdb_store_matcher_t *filter)
201 {
202 sdb_store_expr_t *e1, *e2;
203 sdb_data_t v1 = SDB_DATA_INIT;
204 sdb_data_t v2 = SDB_DATA_INIT;
206 int status;
208 if ((ITER_M(m)->m->type < MATCHER_LT)
209 || (MATCHER_NREGEX < ITER_M(m)->m->type))
210 return 0;
212 e1 = CMP_M(ITER_M(m)->m)->left;
213 e2 = CMP_M(ITER_M(m)->m)->right;
215 if (expr_eval2(e1, &v1, e2, &v2, obj, filter))
216 return 0;
218 if ((! (v1.type & SDB_TYPE_ARRAY)) || (v2.type & SDB_TYPE_ARRAY))
219 status = 0;
220 else if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
221 status = 0;
222 else {
223 size_t i;
224 int all = (int)(m->type == MATCHER_ALL);
226 status = all;
227 for (i = 0; i < v1.data.array.length; ++i) {
228 sdb_data_t v = SDB_DATA_INIT;
229 if (sdb_data_array_get(&v1, i, &v)) {
230 status = 0;
231 break;
232 }
234 if (match_value(ITER_M(m)->m->type, &v, &v2,
235 (e1->data_type) < 0 || (e2->data_type < 0))) {
236 if (! all) {
237 status = 1;
238 break;
239 }
240 }
241 else if (all) {
242 status = 0;
243 break;
244 }
245 }
246 }
248 expr_free_datum2(e1, &v1, e2, &v2);
249 return status;
250 } /* match_iter_array */
252 static int
253 match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
254 sdb_store_matcher_t *filter)
255 {
256 sdb_avltree_iter_t *iter = NULL;
257 int status;
258 int all = (int)(m->type == MATCHER_ALL);
260 assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL));
262 if (ITER_M(m)->type == SDB_FIELD_BACKEND)
263 return match_iter_array(m, obj, filter);
265 if (obj->type == SDB_HOST) {
266 if (ITER_M(m)->type == SDB_SERVICE)
267 iter = sdb_avltree_get_iter(HOST(obj)->services);
268 else if (ITER_M(m)->type == SDB_METRIC)
269 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
270 else if (ITER_M(m)->type == SDB_ATTRIBUTE)
271 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
272 } else if (obj->type == SDB_SERVICE) {
273 if (ITER_M(m)->type == SDB_ATTRIBUTE)
274 iter = sdb_avltree_get_iter(SVC(obj)->attributes);
275 } else if (obj->type == SDB_METRIC) {
276 if (ITER_M(m)->type == SDB_ATTRIBUTE)
277 iter = sdb_avltree_get_iter(METRIC(obj)->attributes);
278 }
280 status = all;
281 while (sdb_avltree_iter_has_next(iter)) {
282 sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
283 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
284 continue;
286 if (sdb_store_matcher_matches(ITER_M(m)->m, child, filter)) {
287 if (! all) {
288 status = 1;
289 break;
290 }
291 } else if (all) {
292 status = 0;
293 break;
294 }
295 }
296 sdb_avltree_iter_destroy(iter);
297 return status;
298 } /* match_iter */
300 static int
301 match_cmp(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
302 sdb_store_matcher_t *filter)
303 {
304 sdb_store_expr_t *e1 = CMP_M(m)->left;
305 sdb_store_expr_t *e2 = CMP_M(m)->right;
306 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
307 int status;
309 assert((m->type == MATCHER_LT)
310 || (m->type == MATCHER_LE)
311 || (m->type == MATCHER_EQ)
312 || (m->type == MATCHER_NE)
313 || (m->type == MATCHER_GE)
314 || (m->type == MATCHER_GT));
316 if (expr_eval2(e1, &v1, e2, &v2, obj, filter))
317 return 0;
319 status = match_cmp_value(m->type, &v1, &v2,
320 (e1->data_type) < 0 || (e2->data_type < 0));
322 expr_free_datum2(e1, &v1, e2, &v2);
323 return status;
324 } /* match_cmp */
326 static int
327 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
328 sdb_store_matcher_t *filter)
329 {
330 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
331 int status = 1;
333 assert(m->type == MATCHER_IN);
335 if (expr_eval2(CMP_M(m)->left, &value,
336 CMP_M(m)->right, &array, obj, filter))
337 status = 0;
339 if (status)
340 status = sdb_data_inarray(&value, &array);
342 expr_free_datum2(CMP_M(m)->left, &value, CMP_M(m)->right, &array);
343 return status;
344 } /* match_in */
346 static int
347 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
348 sdb_store_matcher_t *filter)
349 {
350 sdb_data_t regex = SDB_DATA_INIT, v = SDB_DATA_INIT;
351 int status = 0;
353 assert((m->type == MATCHER_REGEX)
354 || (m->type == MATCHER_NREGEX));
356 if (expr_eval2(CMP_M(m)->left, &v, CMP_M(m)->right, ®ex, obj, filter))
357 return 0;
359 status = match_regex_value(m->type, &v, ®ex);
361 expr_free_datum2(CMP_M(m)->left, &v, CMP_M(m)->right, ®ex);
362 return status;
363 } /* match_regex */
365 static int
366 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
367 sdb_store_matcher_t *filter)
368 {
369 sdb_data_t v = SDB_DATA_INIT;
370 int status;
372 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
374 if (ISNULL_M(m)->expr->type) {
375 /* TODO: this might hide real errors;
376 * improve error reporting and propagation */
377 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter))
378 return 1;
379 }
380 else
381 v = ISNULL_M(m)->expr->data;
383 if (sdb_data_isnull(&v))
384 status = 1;
385 else
386 status = 0;
388 if (ISNULL_M(m)->expr->type)
389 sdb_data_free_datum(&v);
390 if (m->type == MATCHER_ISNNULL)
391 return !status;
392 return status;
393 } /* match_isnull */
395 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
396 sdb_store_matcher_t *);
398 /* this array needs to be indexable by the matcher types;
399 * -> update the enum in store-private.h when updating this */
400 static matcher_cb
401 matchers[] = {
402 match_logical,
403 match_logical,
404 match_unary,
405 match_iter,
406 match_iter,
407 match_in,
409 /* unary operators */
410 match_isnull,
411 match_isnull,
413 /* ary operators */
414 match_cmp,
415 match_cmp,
416 match_cmp,
417 match_cmp,
418 match_cmp,
419 match_cmp,
420 match_regex,
421 match_regex,
422 };
424 /*
425 * private matcher types
426 */
428 static int
429 op_matcher_init(sdb_object_t *obj, va_list ap)
430 {
431 M(obj)->type = va_arg(ap, int);
432 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
433 return -1;
435 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
436 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
437 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
438 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
440 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
441 return -1;
442 return 0;
443 } /* op_matcher_init */
445 static void
446 op_matcher_destroy(sdb_object_t *obj)
447 {
448 if (OP_M(obj)->left)
449 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
450 if (OP_M(obj)->right)
451 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
452 } /* op_matcher_destroy */
454 static int
455 iter_matcher_init(sdb_object_t *obj, va_list ap)
456 {
457 M(obj)->type = va_arg(ap, int);
458 ITER_M(obj)->type = va_arg(ap, int);
459 ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
461 if (! ITER_M(obj)->m)
462 return -1;
464 sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
465 return 0;
466 } /* iter_matcher_init */
468 static void
469 iter_matcher_destroy(sdb_object_t *obj)
470 {
471 sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
472 } /* iter_matcher_destroy */
474 static int
475 cmp_matcher_init(sdb_object_t *obj, va_list ap)
476 {
477 M(obj)->type = va_arg(ap, int);
479 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
480 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
481 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
482 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
484 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
485 return -1;
486 return 0;
487 } /* cmp_matcher_init */
489 static void
490 cmp_matcher_destroy(sdb_object_t *obj)
491 {
492 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
493 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
494 } /* cmp_matcher_destroy */
496 static int
497 uop_matcher_init(sdb_object_t *obj, va_list ap)
498 {
499 M(obj)->type = va_arg(ap, int);
500 if (M(obj)->type != MATCHER_NOT)
501 return -1;
503 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
504 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
506 if (! UOP_M(obj)->op)
507 return -1;
508 return 0;
509 } /* uop_matcher_init */
511 static void
512 uop_matcher_destroy(sdb_object_t *obj)
513 {
514 if (UOP_M(obj)->op)
515 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
516 } /* uop_matcher_destroy */
518 static int
519 isnull_matcher_init(sdb_object_t *obj, va_list ap)
520 {
521 M(obj)->type = va_arg(ap, int);
522 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
523 return -1;
525 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
526 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
527 return 0;
528 } /* isnull_matcher_init */
530 static void
531 isnull_matcher_destroy(sdb_object_t *obj)
532 {
533 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
534 ISNULL_M(obj)->expr = NULL;
535 } /* isnull_matcher_destroy */
537 static sdb_type_t op_type = {
538 /* size = */ sizeof(op_matcher_t),
539 /* init = */ op_matcher_init,
540 /* destroy = */ op_matcher_destroy,
541 };
543 static sdb_type_t uop_type = {
544 /* size = */ sizeof(uop_matcher_t),
545 /* init = */ uop_matcher_init,
546 /* destroy = */ uop_matcher_destroy,
547 };
549 static sdb_type_t iter_type = {
550 /* size = */ sizeof(iter_matcher_t),
551 /* init = */ iter_matcher_init,
552 /* destroy = */ iter_matcher_destroy,
553 };
555 static sdb_type_t cmp_type = {
556 /* size = */ sizeof(cmp_matcher_t),
557 /* init = */ cmp_matcher_init,
558 /* destroy = */ cmp_matcher_destroy,
559 };
561 static sdb_type_t isnull_type = {
562 /* size = */ sizeof(isnull_matcher_t),
563 /* init = */ isnull_matcher_init,
564 /* destroy = */ isnull_matcher_destroy,
565 };
567 /*
568 * public API
569 */
571 sdb_store_matcher_t *
572 sdb_store_any_matcher(int type, sdb_store_matcher_t *m)
573 {
574 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
575 && (type != SDB_ATTRIBUTE) && (type != SDB_FIELD_BACKEND))
576 return NULL;
577 return M(sdb_object_create("any-matcher", iter_type,
578 MATCHER_ANY, type, m));
579 } /* sdb_store_any_matcher */
581 sdb_store_matcher_t *
582 sdb_store_all_matcher(int type, sdb_store_matcher_t *m)
583 {
584 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
585 && (type != SDB_ATTRIBUTE) && (type != SDB_FIELD_BACKEND))
586 return NULL;
587 return M(sdb_object_create("all-matcher", iter_type,
588 MATCHER_ALL, type, m));
589 } /* sdb_store_all_matcher */
591 sdb_store_matcher_t *
592 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
593 {
594 return M(sdb_object_create("lt-matcher", cmp_type,
595 MATCHER_LT, left, right));
596 } /* sdb_store_lt_matcher */
598 sdb_store_matcher_t *
599 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
600 {
601 return M(sdb_object_create("le-matcher", cmp_type,
602 MATCHER_LE, left, right));
603 } /* sdb_store_le_matcher */
605 sdb_store_matcher_t *
606 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
607 {
608 return M(sdb_object_create("eq-matcher", cmp_type,
609 MATCHER_EQ, left, right));
610 } /* sdb_store_eq_matcher */
612 sdb_store_matcher_t *
613 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
614 {
615 return M(sdb_object_create("ne-matcher", cmp_type,
616 MATCHER_NE, left, right));
617 } /* sdb_store_ne_matcher */
619 sdb_store_matcher_t *
620 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
621 {
622 return M(sdb_object_create("ge-matcher", cmp_type,
623 MATCHER_GE, left, right));
624 } /* sdb_store_ge_matcher */
626 sdb_store_matcher_t *
627 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
628 {
629 return M(sdb_object_create("gt-matcher", cmp_type,
630 MATCHER_GT, left, right));
631 } /* sdb_store_gt_matcher */
633 sdb_store_matcher_t *
634 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
635 {
636 return M(sdb_object_create("in-matcher", cmp_type,
637 MATCHER_IN, left, right));
638 } /* sdb_store_in_matcher */
640 sdb_store_matcher_t *
641 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
642 {
643 if (! right->type) {
644 if ((right->data.type != SDB_TYPE_STRING)
645 && (right->data.type != SDB_TYPE_REGEX))
646 return NULL;
648 if (right->data.type == SDB_TYPE_STRING) {
649 char *raw = right->data.data.string;
650 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
651 return NULL;
652 free(raw);
653 }
654 }
655 return M(sdb_object_create("regex-matcher", cmp_type,
656 MATCHER_REGEX, left, right));
657 } /* sdb_store_regex_matcher */
659 sdb_store_matcher_t *
660 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
661 {
662 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
663 if (! m)
664 return NULL;
665 m->type = MATCHER_NREGEX;
666 return m;
667 } /* sdb_store_nregex_matcher */
669 sdb_store_matcher_t *
670 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
671 {
672 return M(sdb_object_create("isnull-matcher", isnull_type,
673 MATCHER_ISNULL, expr));
674 } /* sdb_store_isnull_matcher */
676 sdb_store_matcher_t *
677 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
678 {
679 return M(sdb_object_create("isnull-matcher", isnull_type,
680 MATCHER_ISNNULL, expr));
681 } /* sdb_store_isnnull_matcher */
683 sdb_store_matcher_op_cb
684 sdb_store_parse_matcher_op(const char *op)
685 {
686 if (! strcasecmp(op, "<"))
687 return sdb_store_lt_matcher;
688 else if (! strcasecmp(op, "<="))
689 return sdb_store_le_matcher;
690 else if (! strcasecmp(op, "="))
691 return sdb_store_eq_matcher;
692 else if (! strcasecmp(op, "!="))
693 return sdb_store_ne_matcher;
694 else if (! strcasecmp(op, ">="))
695 return sdb_store_ge_matcher;
696 else if (! strcasecmp(op, ">"))
697 return sdb_store_gt_matcher;
698 else if (! strcasecmp(op, "=~"))
699 return sdb_store_regex_matcher;
700 else if (! strcasecmp(op, "!~"))
701 return sdb_store_nregex_matcher;
702 return NULL;
703 } /* sdb_store_parse_matcher_op */
705 int
706 sdb_store_parse_object_type(const char *name)
707 {
708 if (! strcasecmp(name, "host"))
709 return SDB_HOST;
710 else if (! strcasecmp(name, "service"))
711 return SDB_SERVICE;
712 else if (! strcasecmp(name, "metric"))
713 return SDB_METRIC;
714 else if (! strcasecmp(name, "attribute"))
715 return SDB_ATTRIBUTE;
716 return -1;
717 } /* sdb_store_parse_object_type */
719 int
720 sdb_store_parse_object_type_plural(const char *name)
721 {
722 if (! strcasecmp(name, "hosts"))
723 return SDB_HOST;
724 else if (! strcasecmp(name, "services"))
725 return SDB_SERVICE;
726 else if (! strcasecmp(name, "metrics"))
727 return SDB_METRIC;
728 else if (! strcasecmp(name, "attributes"))
729 return SDB_ATTRIBUTE;
730 return -1;
731 } /* sdb_store_parse_object_type_plural */
733 int
734 sdb_store_parse_field_name(const char *name)
735 {
736 if (! strcasecmp(name, "name"))
737 return SDB_FIELD_NAME;
738 else if (! strcasecmp(name, "last_update"))
739 return SDB_FIELD_LAST_UPDATE;
740 else if (! strcasecmp(name, "age"))
741 return SDB_FIELD_AGE;
742 else if (! strcasecmp(name, "interval"))
743 return SDB_FIELD_INTERVAL;
744 else if (! strcasecmp(name, "backend"))
745 return SDB_FIELD_BACKEND;
746 return -1;
747 } /* sdb_store_parse_field_name */
749 sdb_store_matcher_t *
750 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
751 {
752 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
753 left, right));
754 } /* sdb_store_dis_matcher */
756 sdb_store_matcher_t *
757 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
758 {
759 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
760 left, right));
761 } /* sdb_store_con_matcher */
763 sdb_store_matcher_t *
764 sdb_store_inv_matcher(sdb_store_matcher_t *m)
765 {
766 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
767 } /* sdb_store_inv_matcher */
769 int
770 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
771 sdb_store_matcher_t *filter)
772 {
773 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
774 return 0;
776 /* "NULL" always matches */
777 if ((! m) || (! obj))
778 return 1;
780 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
781 return 0;
783 return matchers[m->type](m, obj, filter);
784 } /* sdb_store_matcher_matches */
786 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */