e2ec1b14936b3c6586bdd698bdd0c792519c61aa
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 * matcher implementations
54 */
56 /*
57 * cmp_expr:
58 * Compare two values using the specified matcher operator. If strcmp_fallback
59 * is enabled, compare the string values in case of a type mismatch.
60 */
61 static int
62 cmp_value(int op, sdb_data_t *v1, sdb_data_t *v2, _Bool strcmp_fallback)
63 {
64 int status;
66 if (sdb_data_isnull(v1) || (sdb_data_isnull(v2)))
67 status = INT_MAX;
68 else if (v1->type == v2->type)
69 status = sdb_data_cmp(v1, v2);
70 else if (! strcmp_fallback)
71 status = INT_MAX;
72 else
73 status = sdb_data_strcmp(v1, v2);
75 if (status == INT_MAX)
76 return 0;
77 else if (op == MATCHER_LT)
78 return status < 0;
79 else if (op == MATCHER_LE)
80 return status <= 0;
81 else if (op == MATCHER_EQ)
82 return status == 0;
83 else if (op == MATCHER_NE)
84 return status != 0;
85 else if (op == MATCHER_GE)
86 return status >= 0;
87 else if (op == MATCHER_GT)
88 return status > 0;
89 return 0;
90 } /* cmp_value */
92 static int
93 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
94 sdb_store_matcher_t *filter)
95 {
96 int status;
98 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
99 assert(OP_M(m)->left && OP_M(m)->right);
101 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
103 /* lazy evaluation */
104 if ((! status) && (m->type == MATCHER_AND))
105 return status;
106 else if (status && (m->type == MATCHER_OR))
107 return status;
109 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
110 } /* match_logical */
112 static int
113 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
114 sdb_store_matcher_t *filter)
115 {
116 assert(m->type == MATCHER_NOT);
117 assert(UOP_M(m)->op);
119 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
120 } /* match_unary */
122 /* iterate arrays: ANY/ALL <array> <cmp> <value> */
123 static int
124 match_iter_array(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
125 sdb_store_matcher_t *filter)
126 {
127 sdb_store_expr_t *e1, *e2;
128 sdb_data_t v1 = SDB_DATA_INIT;
129 sdb_data_t v2 = SDB_DATA_INIT;
131 int status;
133 /* TODO: fully support arbitrary operators (?) */
134 if ((ITER_M(m)->m->type < MATCHER_LT) || (MATCHER_GT < ITER_M(m)->m->type))
135 return 0;
137 e1 = CMP_M(ITER_M(m)->m)->left;
138 e2 = CMP_M(ITER_M(m)->m)->right;
140 if (sdb_store_expr_eval(e1, obj, &v1, filter))
141 return 0;
142 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
143 sdb_data_free_datum(&v1);
144 return 0;
145 }
147 if ((! (v1.type & SDB_TYPE_ARRAY)) || (v2.type & SDB_TYPE_ARRAY))
148 status = 0;
149 else if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
150 status = 0;
151 else {
152 size_t i;
153 int all = (int)(m->type == MATCHER_ALL);
155 status = all;
156 for (i = 0; i < v1.data.array.length; ++i) {
157 sdb_data_t v = SDB_DATA_INIT;
158 if (sdb_data_array_get(&v1, i, &v)) {
159 status = 0;
160 break;
161 }
163 if (cmp_value(ITER_M(m)->m->type, &v, &v2,
164 (e1->data_type) < 0 || (e2->data_type < 0))) {
165 if (! all) {
166 status = 1;
167 break;
168 }
169 }
170 else if (all) {
171 status = 0;
172 break;
173 }
174 }
175 }
177 sdb_data_free_datum(&v1);
178 sdb_data_free_datum(&v2);
179 return status;
180 } /* match_iter_array */
182 static int
183 match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
184 sdb_store_matcher_t *filter)
185 {
186 sdb_avltree_iter_t *iter = NULL;
187 int status;
188 int all = (int)(m->type == MATCHER_ALL);
190 assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL));
192 if (ITER_M(m)->type == SDB_FIELD_BACKEND)
193 return match_iter_array(m, obj, filter);
195 if (obj->type == SDB_HOST) {
196 if (ITER_M(m)->type == SDB_SERVICE)
197 iter = sdb_avltree_get_iter(HOST(obj)->services);
198 else if (ITER_M(m)->type == SDB_METRIC)
199 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
200 else if (ITER_M(m)->type == SDB_ATTRIBUTE)
201 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
202 } else if (obj->type == SDB_SERVICE) {
203 if (ITER_M(m)->type == SDB_ATTRIBUTE)
204 iter = sdb_avltree_get_iter(SVC(obj)->attributes);
205 } else if (obj->type == SDB_METRIC) {
206 if (ITER_M(m)->type == SDB_ATTRIBUTE)
207 iter = sdb_avltree_get_iter(METRIC(obj)->attributes);
208 }
210 status = all;
211 while (sdb_avltree_iter_has_next(iter)) {
212 sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
213 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
214 continue;
216 if (sdb_store_matcher_matches(ITER_M(m)->m, child, filter)) {
217 if (! all) {
218 status = 1;
219 break;
220 }
221 } else if (all) {
222 status = 0;
223 break;
224 }
225 }
226 sdb_avltree_iter_destroy(iter);
227 return status;
228 } /* match_iter */
230 static int
231 match_cmp(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
232 sdb_store_matcher_t *filter)
233 {
234 sdb_store_expr_t *e1 = CMP_M(m)->left;
235 sdb_store_expr_t *e2 = CMP_M(m)->right;
236 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
237 int status;
239 assert((m->type == MATCHER_LT)
240 || (m->type == MATCHER_LE)
241 || (m->type == MATCHER_EQ)
242 || (m->type == MATCHER_NE)
243 || (m->type == MATCHER_GE)
244 || (m->type == MATCHER_GT));
246 if (sdb_store_expr_eval(e1, obj, &v1, filter))
247 return 0;
248 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
249 sdb_data_free_datum(&v1);
250 return 0;
251 }
253 status = cmp_value(m->type, &v1, &v2,
254 (e1->data_type) < 0 || (e2->data_type < 0));
256 sdb_data_free_datum(&v1);
257 sdb_data_free_datum(&v2);
258 return status;
259 } /* match_cmp */
261 static int
262 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
263 sdb_store_matcher_t *filter)
264 {
265 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
266 int status = 1;
268 assert(m->type == MATCHER_IN);
270 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
271 || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
272 status = 0;
274 if (status)
275 status = sdb_data_inarray(&value, &array);
277 sdb_data_free_datum(&value);
278 sdb_data_free_datum(&array);
279 return status;
280 } /* match_in */
282 static int
283 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
284 sdb_store_matcher_t *filter)
285 {
286 sdb_data_t v = SDB_DATA_INIT;
287 int status = 0;
289 regex_t regex;
290 _Bool free_regex = 0;
292 assert((m->type == MATCHER_REGEX)
293 || (m->type == MATCHER_NREGEX));
295 if (! CMP_M(m)->right->type) {
296 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
297 regex = CMP_M(m)->right->data.data.re.regex;
298 }
299 else {
300 sdb_data_t tmp = SDB_DATA_INIT;
301 char *raw;
303 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
304 return 0;
306 if (tmp.type != SDB_TYPE_STRING) {
307 sdb_data_free_datum(&tmp);
308 return 0;
309 }
311 raw = tmp.data.string;
312 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
313 free(raw);
314 return 0;
315 }
317 regex = tmp.data.re.regex;
318 free_regex = 1;
319 free(tmp.data.re.raw);
320 free(raw);
321 }
323 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
324 || (sdb_data_isnull(&v)))
325 status = 0;
326 else {
327 char value[sdb_data_strlen(&v) + 1];
328 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
329 status = 0;
330 else if (! regexec(®ex, value, 0, NULL, 0))
331 status = 1;
332 }
334 if (free_regex)
335 regfree(®ex);
336 sdb_data_free_datum(&v);
337 if (m->type == MATCHER_NREGEX)
338 return !status;
339 return status;
340 } /* match_regex */
342 static int
343 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
344 sdb_store_matcher_t *filter)
345 {
346 sdb_data_t v = SDB_DATA_INIT;
347 int status;
349 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
351 /* TODO: this might hide real errors;
352 * improve error reporting and propagation */
353 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
354 || sdb_data_isnull(&v))
355 status = 1;
356 else
357 status = 0;
359 sdb_data_free_datum(&v);
360 if (m->type == MATCHER_ISNNULL)
361 return !status;
362 return status;
363 } /* match_isnull */
365 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
366 sdb_store_matcher_t *);
368 /* this array needs to be indexable by the matcher types;
369 * -> update the enum in store-private.h when updating this */
370 static matcher_cb
371 matchers[] = {
372 match_logical,
373 match_logical,
374 match_unary,
375 match_iter,
376 match_iter,
377 match_cmp,
378 match_cmp,
379 match_cmp,
380 match_cmp,
381 match_cmp,
382 match_cmp,
383 match_in,
384 match_regex,
385 match_regex,
386 match_isnull,
387 match_isnull,
388 };
390 /*
391 * private matcher types
392 */
394 static int
395 op_matcher_init(sdb_object_t *obj, va_list ap)
396 {
397 M(obj)->type = va_arg(ap, int);
398 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
399 return -1;
401 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
402 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
403 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
404 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
406 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
407 return -1;
408 return 0;
409 } /* op_matcher_init */
411 static void
412 op_matcher_destroy(sdb_object_t *obj)
413 {
414 if (OP_M(obj)->left)
415 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
416 if (OP_M(obj)->right)
417 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
418 } /* op_matcher_destroy */
420 static int
421 iter_matcher_init(sdb_object_t *obj, va_list ap)
422 {
423 M(obj)->type = va_arg(ap, int);
424 ITER_M(obj)->type = va_arg(ap, int);
425 ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
427 if (! ITER_M(obj)->m)
428 return -1;
430 sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
431 return 0;
432 } /* iter_matcher_init */
434 static void
435 iter_matcher_destroy(sdb_object_t *obj)
436 {
437 sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
438 } /* iter_matcher_destroy */
440 static int
441 cmp_matcher_init(sdb_object_t *obj, va_list ap)
442 {
443 M(obj)->type = va_arg(ap, int);
445 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
446 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
447 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
448 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
450 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
451 return -1;
452 return 0;
453 } /* cmp_matcher_init */
455 static void
456 cmp_matcher_destroy(sdb_object_t *obj)
457 {
458 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
459 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
460 } /* cmp_matcher_destroy */
462 static int
463 uop_matcher_init(sdb_object_t *obj, va_list ap)
464 {
465 M(obj)->type = va_arg(ap, int);
466 if (M(obj)->type != MATCHER_NOT)
467 return -1;
469 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
470 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
472 if (! UOP_M(obj)->op)
473 return -1;
474 return 0;
475 } /* uop_matcher_init */
477 static void
478 uop_matcher_destroy(sdb_object_t *obj)
479 {
480 if (UOP_M(obj)->op)
481 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
482 } /* uop_matcher_destroy */
484 static int
485 isnull_matcher_init(sdb_object_t *obj, va_list ap)
486 {
487 M(obj)->type = va_arg(ap, int);
488 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
489 return -1;
491 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
492 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
493 return 0;
494 } /* isnull_matcher_init */
496 static void
497 isnull_matcher_destroy(sdb_object_t *obj)
498 {
499 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
500 ISNULL_M(obj)->expr = NULL;
501 } /* isnull_matcher_destroy */
503 static sdb_type_t op_type = {
504 /* size = */ sizeof(op_matcher_t),
505 /* init = */ op_matcher_init,
506 /* destroy = */ op_matcher_destroy,
507 };
509 static sdb_type_t uop_type = {
510 /* size = */ sizeof(uop_matcher_t),
511 /* init = */ uop_matcher_init,
512 /* destroy = */ uop_matcher_destroy,
513 };
515 static sdb_type_t iter_type = {
516 /* size = */ sizeof(iter_matcher_t),
517 /* init = */ iter_matcher_init,
518 /* destroy = */ iter_matcher_destroy,
519 };
521 static sdb_type_t cmp_type = {
522 /* size = */ sizeof(cmp_matcher_t),
523 /* init = */ cmp_matcher_init,
524 /* destroy = */ cmp_matcher_destroy,
525 };
527 static sdb_type_t isnull_type = {
528 /* size = */ sizeof(isnull_matcher_t),
529 /* init = */ isnull_matcher_init,
530 /* destroy = */ isnull_matcher_destroy,
531 };
533 /*
534 * public API
535 */
537 sdb_store_matcher_t *
538 sdb_store_any_matcher(int type, sdb_store_matcher_t *m)
539 {
540 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
541 && (type != SDB_ATTRIBUTE) && (type != SDB_FIELD_BACKEND))
542 return NULL;
543 return M(sdb_object_create("any-matcher", iter_type,
544 MATCHER_ANY, type, m));
545 } /* sdb_store_any_matcher */
547 sdb_store_matcher_t *
548 sdb_store_all_matcher(int type, sdb_store_matcher_t *m)
549 {
550 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
551 && (type != SDB_ATTRIBUTE) && (type != SDB_FIELD_BACKEND))
552 return NULL;
553 return M(sdb_object_create("all-matcher", iter_type,
554 MATCHER_ALL, type, m));
555 } /* sdb_store_all_matcher */
557 sdb_store_matcher_t *
558 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
559 {
560 return M(sdb_object_create("lt-matcher", cmp_type,
561 MATCHER_LT, left, right));
562 } /* sdb_store_lt_matcher */
564 sdb_store_matcher_t *
565 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
566 {
567 return M(sdb_object_create("le-matcher", cmp_type,
568 MATCHER_LE, left, right));
569 } /* sdb_store_le_matcher */
571 sdb_store_matcher_t *
572 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
573 {
574 return M(sdb_object_create("eq-matcher", cmp_type,
575 MATCHER_EQ, left, right));
576 } /* sdb_store_eq_matcher */
578 sdb_store_matcher_t *
579 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
580 {
581 return M(sdb_object_create("ne-matcher", cmp_type,
582 MATCHER_NE, left, right));
583 } /* sdb_store_ne_matcher */
585 sdb_store_matcher_t *
586 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
587 {
588 return M(sdb_object_create("ge-matcher", cmp_type,
589 MATCHER_GE, left, right));
590 } /* sdb_store_ge_matcher */
592 sdb_store_matcher_t *
593 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
594 {
595 return M(sdb_object_create("gt-matcher", cmp_type,
596 MATCHER_GT, left, right));
597 } /* sdb_store_gt_matcher */
599 sdb_store_matcher_t *
600 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
601 {
602 return M(sdb_object_create("in-matcher", cmp_type,
603 MATCHER_IN, left, right));
604 } /* sdb_store_in_matcher */
606 sdb_store_matcher_t *
607 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
608 {
609 if (! right->type) {
610 if ((right->data.type != SDB_TYPE_STRING)
611 && (right->data.type != SDB_TYPE_REGEX))
612 return NULL;
614 if (right->data.type == SDB_TYPE_STRING) {
615 char *raw = right->data.data.string;
616 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
617 return NULL;
618 free(raw);
619 }
620 }
621 return M(sdb_object_create("regex-matcher", cmp_type,
622 MATCHER_REGEX, left, right));
623 } /* sdb_store_regex_matcher */
625 sdb_store_matcher_t *
626 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
627 {
628 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
629 if (! m)
630 return NULL;
631 m->type = MATCHER_NREGEX;
632 return m;
633 } /* sdb_store_nregex_matcher */
635 sdb_store_matcher_t *
636 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
637 {
638 return M(sdb_object_create("isnull-matcher", isnull_type,
639 MATCHER_ISNULL, expr));
640 } /* sdb_store_isnull_matcher */
642 sdb_store_matcher_t *
643 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
644 {
645 return M(sdb_object_create("isnull-matcher", isnull_type,
646 MATCHER_ISNNULL, expr));
647 } /* sdb_store_isnnull_matcher */
649 sdb_store_matcher_op_cb
650 sdb_store_parse_matcher_op(const char *op)
651 {
652 if (! strcasecmp(op, "<"))
653 return sdb_store_lt_matcher;
654 else if (! strcasecmp(op, "<="))
655 return sdb_store_le_matcher;
656 else if (! strcasecmp(op, "="))
657 return sdb_store_eq_matcher;
658 else if (! strcasecmp(op, "!="))
659 return sdb_store_ne_matcher;
660 else if (! strcasecmp(op, ">="))
661 return sdb_store_ge_matcher;
662 else if (! strcasecmp(op, ">"))
663 return sdb_store_gt_matcher;
664 else if (! strcasecmp(op, "=~"))
665 return sdb_store_regex_matcher;
666 else if (! strcasecmp(op, "!~"))
667 return sdb_store_nregex_matcher;
668 return NULL;
669 } /* sdb_store_parse_matcher_op */
671 int
672 sdb_store_parse_object_type(const char *name)
673 {
674 if (! strcasecmp(name, "host"))
675 return SDB_HOST;
676 else if (! strcasecmp(name, "service"))
677 return SDB_SERVICE;
678 else if (! strcasecmp(name, "metric"))
679 return SDB_METRIC;
680 else if (! strcasecmp(name, "attribute"))
681 return SDB_ATTRIBUTE;
682 return -1;
683 } /* sdb_store_parse_object_type */
685 int
686 sdb_store_parse_object_type_plural(const char *name)
687 {
688 if (! strcasecmp(name, "hosts"))
689 return SDB_HOST;
690 else if (! strcasecmp(name, "services"))
691 return SDB_SERVICE;
692 else if (! strcasecmp(name, "metrics"))
693 return SDB_METRIC;
694 else if (! strcasecmp(name, "attributes"))
695 return SDB_ATTRIBUTE;
696 return -1;
697 } /* sdb_store_parse_object_type_plural */
699 int
700 sdb_store_parse_field_name(const char *name)
701 {
702 if (! strcasecmp(name, "name"))
703 return SDB_FIELD_NAME;
704 else if (! strcasecmp(name, "last_update"))
705 return SDB_FIELD_LAST_UPDATE;
706 else if (! strcasecmp(name, "age"))
707 return SDB_FIELD_AGE;
708 else if (! strcasecmp(name, "interval"))
709 return SDB_FIELD_INTERVAL;
710 else if (! strcasecmp(name, "backend"))
711 return SDB_FIELD_BACKEND;
712 return -1;
713 } /* sdb_store_parse_field_name */
715 sdb_store_matcher_t *
716 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
717 {
718 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
719 left, right));
720 } /* sdb_store_dis_matcher */
722 sdb_store_matcher_t *
723 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
724 {
725 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
726 left, right));
727 } /* sdb_store_con_matcher */
729 sdb_store_matcher_t *
730 sdb_store_inv_matcher(sdb_store_matcher_t *m)
731 {
732 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
733 } /* sdb_store_inv_matcher */
735 int
736 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
737 sdb_store_matcher_t *filter)
738 {
739 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
740 return 0;
742 /* "NULL" always matches */
743 if ((! m) || (! obj))
744 return 1;
746 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
747 return 0;
749 return matchers[m->type](m, obj, filter);
750 } /* sdb_store_matcher_matches */
752 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */