eab37b4787bb2ba7e752ab6c06eb4b8bc019a7ea
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 = INT_MAX;
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 (status == INT_MAX)
338 return 0;
339 if (m->type == MATCHER_NREGEX)
340 return !status;
341 return status;
342 } /* match_regex */
344 static int
345 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
346 sdb_store_matcher_t *filter)
347 {
348 sdb_data_t v = SDB_DATA_INIT;
349 int status;
351 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
353 /* TODO: this might hide real errors;
354 * improve error reporting and propagation */
355 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
356 || sdb_data_isnull(&v))
357 status = 1;
358 else
359 status = 0;
361 sdb_data_free_datum(&v);
362 if (m->type == MATCHER_ISNNULL)
363 return !status;
364 return status;
365 } /* match_isnull */
367 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
368 sdb_store_matcher_t *);
370 /* this array needs to be indexable by the matcher types;
371 * -> update the enum in store-private.h when updating this */
372 static matcher_cb
373 matchers[] = {
374 match_logical,
375 match_logical,
376 match_unary,
377 match_iter,
378 match_iter,
379 match_cmp,
380 match_cmp,
381 match_cmp,
382 match_cmp,
383 match_cmp,
384 match_cmp,
385 match_in,
386 match_regex,
387 match_regex,
388 match_isnull,
389 match_isnull,
390 };
392 /*
393 * private matcher types
394 */
396 static int
397 op_matcher_init(sdb_object_t *obj, va_list ap)
398 {
399 M(obj)->type = va_arg(ap, int);
400 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
401 return -1;
403 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
404 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
405 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
406 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
408 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
409 return -1;
410 return 0;
411 } /* op_matcher_init */
413 static void
414 op_matcher_destroy(sdb_object_t *obj)
415 {
416 if (OP_M(obj)->left)
417 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
418 if (OP_M(obj)->right)
419 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
420 } /* op_matcher_destroy */
422 static int
423 iter_matcher_init(sdb_object_t *obj, va_list ap)
424 {
425 M(obj)->type = va_arg(ap, int);
426 ITER_M(obj)->type = va_arg(ap, int);
427 ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
429 if (! ITER_M(obj)->m)
430 return -1;
432 sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
433 return 0;
434 } /* iter_matcher_init */
436 static void
437 iter_matcher_destroy(sdb_object_t *obj)
438 {
439 sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
440 } /* iter_matcher_destroy */
442 static int
443 cmp_matcher_init(sdb_object_t *obj, va_list ap)
444 {
445 M(obj)->type = va_arg(ap, int);
447 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
448 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
449 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
450 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
452 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
453 return -1;
454 return 0;
455 } /* cmp_matcher_init */
457 static void
458 cmp_matcher_destroy(sdb_object_t *obj)
459 {
460 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
461 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
462 } /* cmp_matcher_destroy */
464 static int
465 uop_matcher_init(sdb_object_t *obj, va_list ap)
466 {
467 M(obj)->type = va_arg(ap, int);
468 if (M(obj)->type != MATCHER_NOT)
469 return -1;
471 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
472 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
474 if (! UOP_M(obj)->op)
475 return -1;
476 return 0;
477 } /* uop_matcher_init */
479 static void
480 uop_matcher_destroy(sdb_object_t *obj)
481 {
482 if (UOP_M(obj)->op)
483 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
484 } /* uop_matcher_destroy */
486 static int
487 isnull_matcher_init(sdb_object_t *obj, va_list ap)
488 {
489 M(obj)->type = va_arg(ap, int);
490 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
491 return -1;
493 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
494 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
495 return 0;
496 } /* isnull_matcher_init */
498 static void
499 isnull_matcher_destroy(sdb_object_t *obj)
500 {
501 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
502 ISNULL_M(obj)->expr = NULL;
503 } /* isnull_matcher_destroy */
505 static sdb_type_t op_type = {
506 /* size = */ sizeof(op_matcher_t),
507 /* init = */ op_matcher_init,
508 /* destroy = */ op_matcher_destroy,
509 };
511 static sdb_type_t uop_type = {
512 /* size = */ sizeof(uop_matcher_t),
513 /* init = */ uop_matcher_init,
514 /* destroy = */ uop_matcher_destroy,
515 };
517 static sdb_type_t iter_type = {
518 /* size = */ sizeof(iter_matcher_t),
519 /* init = */ iter_matcher_init,
520 /* destroy = */ iter_matcher_destroy,
521 };
523 static sdb_type_t cmp_type = {
524 /* size = */ sizeof(cmp_matcher_t),
525 /* init = */ cmp_matcher_init,
526 /* destroy = */ cmp_matcher_destroy,
527 };
529 static sdb_type_t isnull_type = {
530 /* size = */ sizeof(isnull_matcher_t),
531 /* init = */ isnull_matcher_init,
532 /* destroy = */ isnull_matcher_destroy,
533 };
535 /*
536 * public API
537 */
539 sdb_store_matcher_t *
540 sdb_store_any_matcher(int type, sdb_store_matcher_t *m)
541 {
542 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
543 && (type != SDB_ATTRIBUTE) && (type != SDB_FIELD_BACKEND))
544 return NULL;
545 return M(sdb_object_create("any-matcher", iter_type,
546 MATCHER_ANY, type, m));
547 } /* sdb_store_any_matcher */
549 sdb_store_matcher_t *
550 sdb_store_all_matcher(int type, sdb_store_matcher_t *m)
551 {
552 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
553 && (type != SDB_ATTRIBUTE) && (type != SDB_FIELD_BACKEND))
554 return NULL;
555 return M(sdb_object_create("all-matcher", iter_type,
556 MATCHER_ALL, type, m));
557 } /* sdb_store_all_matcher */
559 sdb_store_matcher_t *
560 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
561 {
562 return M(sdb_object_create("lt-matcher", cmp_type,
563 MATCHER_LT, left, right));
564 } /* sdb_store_lt_matcher */
566 sdb_store_matcher_t *
567 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
568 {
569 return M(sdb_object_create("le-matcher", cmp_type,
570 MATCHER_LE, left, right));
571 } /* sdb_store_le_matcher */
573 sdb_store_matcher_t *
574 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
575 {
576 return M(sdb_object_create("eq-matcher", cmp_type,
577 MATCHER_EQ, left, right));
578 } /* sdb_store_eq_matcher */
580 sdb_store_matcher_t *
581 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
582 {
583 return M(sdb_object_create("ne-matcher", cmp_type,
584 MATCHER_NE, left, right));
585 } /* sdb_store_ne_matcher */
587 sdb_store_matcher_t *
588 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
589 {
590 return M(sdb_object_create("ge-matcher", cmp_type,
591 MATCHER_GE, left, right));
592 } /* sdb_store_ge_matcher */
594 sdb_store_matcher_t *
595 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
596 {
597 return M(sdb_object_create("gt-matcher", cmp_type,
598 MATCHER_GT, left, right));
599 } /* sdb_store_gt_matcher */
601 sdb_store_matcher_t *
602 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
603 {
604 return M(sdb_object_create("in-matcher", cmp_type,
605 MATCHER_IN, left, right));
606 } /* sdb_store_in_matcher */
608 sdb_store_matcher_t *
609 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
610 {
611 if (! right->type) {
612 if ((right->data.type != SDB_TYPE_STRING)
613 && (right->data.type != SDB_TYPE_REGEX))
614 return NULL;
616 if (right->data.type == SDB_TYPE_STRING) {
617 char *raw = right->data.data.string;
618 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
619 return NULL;
620 free(raw);
621 }
622 }
623 return M(sdb_object_create("regex-matcher", cmp_type,
624 MATCHER_REGEX, left, right));
625 } /* sdb_store_regex_matcher */
627 sdb_store_matcher_t *
628 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
629 {
630 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
631 if (! m)
632 return NULL;
633 m->type = MATCHER_NREGEX;
634 return m;
635 } /* sdb_store_nregex_matcher */
637 sdb_store_matcher_t *
638 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
639 {
640 return M(sdb_object_create("isnull-matcher", isnull_type,
641 MATCHER_ISNULL, expr));
642 } /* sdb_store_isnull_matcher */
644 sdb_store_matcher_t *
645 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
646 {
647 return M(sdb_object_create("isnull-matcher", isnull_type,
648 MATCHER_ISNNULL, expr));
649 } /* sdb_store_isnnull_matcher */
651 sdb_store_matcher_op_cb
652 sdb_store_parse_matcher_op(const char *op)
653 {
654 if (! strcasecmp(op, "<"))
655 return sdb_store_lt_matcher;
656 else if (! strcasecmp(op, "<="))
657 return sdb_store_le_matcher;
658 else if (! strcasecmp(op, "="))
659 return sdb_store_eq_matcher;
660 else if (! strcasecmp(op, "!="))
661 return sdb_store_ne_matcher;
662 else if (! strcasecmp(op, ">="))
663 return sdb_store_ge_matcher;
664 else if (! strcasecmp(op, ">"))
665 return sdb_store_gt_matcher;
666 else if (! strcasecmp(op, "=~"))
667 return sdb_store_regex_matcher;
668 else if (! strcasecmp(op, "!~"))
669 return sdb_store_nregex_matcher;
670 return NULL;
671 } /* sdb_store_parse_matcher_op */
673 int
674 sdb_store_parse_object_type(const char *name)
675 {
676 if (! strcasecmp(name, "host"))
677 return SDB_HOST;
678 else if (! strcasecmp(name, "service"))
679 return SDB_SERVICE;
680 else if (! strcasecmp(name, "metric"))
681 return SDB_METRIC;
682 else if (! strcasecmp(name, "attribute"))
683 return SDB_ATTRIBUTE;
684 return -1;
685 } /* sdb_store_parse_object_type */
687 int
688 sdb_store_parse_object_type_plural(const char *name)
689 {
690 if (! strcasecmp(name, "hosts"))
691 return SDB_HOST;
692 else if (! strcasecmp(name, "services"))
693 return SDB_SERVICE;
694 else if (! strcasecmp(name, "metrics"))
695 return SDB_METRIC;
696 else if (! strcasecmp(name, "attributes"))
697 return SDB_ATTRIBUTE;
698 return -1;
699 } /* sdb_store_parse_object_type_plural */
701 int
702 sdb_store_parse_field_name(const char *name)
703 {
704 if (! strcasecmp(name, "name"))
705 return SDB_FIELD_NAME;
706 else if (! strcasecmp(name, "last_update"))
707 return SDB_FIELD_LAST_UPDATE;
708 else if (! strcasecmp(name, "age"))
709 return SDB_FIELD_AGE;
710 else if (! strcasecmp(name, "interval"))
711 return SDB_FIELD_INTERVAL;
712 else if (! strcasecmp(name, "backend"))
713 return SDB_FIELD_BACKEND;
714 return -1;
715 } /* sdb_store_parse_field_name */
717 sdb_store_matcher_t *
718 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
719 {
720 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
721 left, right));
722 } /* sdb_store_dis_matcher */
724 sdb_store_matcher_t *
725 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
726 {
727 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
728 left, right));
729 } /* sdb_store_con_matcher */
731 sdb_store_matcher_t *
732 sdb_store_inv_matcher(sdb_store_matcher_t *m)
733 {
734 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
735 } /* sdb_store_inv_matcher */
737 int
738 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
739 sdb_store_matcher_t *filter)
740 {
741 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
742 return 0;
744 /* "NULL" always matches */
745 if ((! m) || (! obj))
746 return 1;
748 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
749 return 0;
751 return matchers[m->type](m, obj, filter);
752 } /* sdb_store_matcher_matches */
754 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */