180d17931d2c603fb0e295b5b06f4c9637d5cb8a
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_string(string_matcher_t *m, const char *name)
83 {
84 if ((! m->name) && (! m->name_re))
85 return 1;
87 if (! name)
88 name = "";
90 if (m->name && strcasecmp(m->name, name))
91 return 0;
92 if (m->name_re && regexec(m->name_re, name,
93 /* matches */ 0, NULL, /* flags = */ 0))
94 return 0;
95 return 1;
96 } /* match_string */
98 static int
99 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
100 sdb_store_matcher_t *filter)
101 {
102 int status;
104 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
105 assert(OP_M(m)->left && OP_M(m)->right);
107 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
109 /* lazy evaluation */
110 if ((! status) && (m->type == MATCHER_AND))
111 return status;
112 else if (status && (m->type == MATCHER_OR))
113 return status;
115 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
116 } /* match_logical */
118 static int
119 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
120 sdb_store_matcher_t *filter)
121 {
122 assert(m->type == MATCHER_NOT);
123 assert(UOP_M(m)->op);
125 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
126 } /* match_unary */
128 static int
129 match_name(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
130 sdb_store_matcher_t *filter)
131 {
132 sdb_avltree_iter_t *iter = NULL;
133 int status = 0;
135 assert(m->type == MATCHER_NAME);
137 if (obj->type == NAME_M(m)->obj_type)
138 return match_string(&NAME_M(m)->name, SDB_OBJ(obj)->name);
139 else if (obj->type != SDB_HOST)
140 return 0;
142 switch (NAME_M(m)->obj_type) {
143 case SDB_SERVICE:
144 iter = sdb_avltree_get_iter(HOST(obj)->services);
145 break;
146 case SDB_METRIC:
147 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
148 break;
149 case SDB_ATTRIBUTE:
150 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
151 break;
152 }
154 while (sdb_avltree_iter_has_next(iter)) {
155 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
156 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
157 NULL)))
158 continue;
159 if (match_string(&NAME_M(m)->name, child->name)) {
160 status = 1;
161 break;
162 }
163 }
164 sdb_avltree_iter_destroy(iter);
165 return status;
166 } /* match_name */
168 static int
169 match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
170 sdb_store_matcher_t *filter)
171 {
172 sdb_avltree_iter_t *iter = NULL;
173 int status;
174 int all = 0;
176 assert((m->type == MATCHER_SERVICE)
177 || (m->type == MATCHER_METRIC)
178 || (m->type == MATCHER_ATTRIBUTE));
180 /* TODO: support all object types */
181 if (obj->type != SDB_HOST)
182 return 0;
184 /* negated matchers should only match if the respective positive matchers
185 * do not match; that is if the negated matcher matchers *all* children */
186 if ((CHILD_M(m)->m->type == MATCHER_NE)
187 || (CHILD_M(m)->m->type == MATCHER_NREGEX))
188 all = 1;
190 if (m->type == MATCHER_SERVICE)
191 iter = sdb_avltree_get_iter(HOST(obj)->services);
192 else if (m->type == MATCHER_METRIC)
193 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
194 else if (m->type == MATCHER_ATTRIBUTE)
195 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
197 status = all;
198 while (sdb_avltree_iter_has_next(iter)) {
199 sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
200 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
201 continue;
203 if (sdb_store_matcher_matches(CHILD_M(m)->m, child, filter)) {
204 if (! all) {
205 status = 1;
206 break;
207 }
208 } else if (all) {
209 status = 0;
210 break;
211 }
212 }
213 sdb_avltree_iter_destroy(iter);
214 return status;
215 } /* match_child */
217 /*
218 * cmp_expr:
219 * Compare the values of two expressions when evaluating them using the
220 * specified stored object and filter. Returns a value less than, equal to, or
221 * greater than zero if the value of the first expression compares less than,
222 * equal to, or greater than the value of the second expression. Returns
223 * INT_MAX if any of the expressions could not be evaluated or if any of them
224 * evaluated to NULL.
225 */
226 static int
227 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
228 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
229 {
230 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
231 int status;
233 if (sdb_store_expr_eval(e1, obj, &v1, filter))
234 return INT_MAX;
235 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
236 sdb_data_free_datum(&v1);
237 return INT_MAX;
238 }
240 if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
241 status = INT_MAX;
242 else if (v1.type == v2.type)
243 status = sdb_data_cmp(&v1, &v2);
244 else
245 status = sdb_data_strcmp(&v1, &v2);
247 sdb_data_free_datum(&v1);
248 sdb_data_free_datum(&v2);
249 return status;
250 } /* cmp_expr */
252 static int
253 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
254 sdb_store_matcher_t *filter)
255 {
256 int status;
257 assert(m->type == MATCHER_LT);
258 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
259 return (status != INT_MAX) && (status < 0);
260 } /* match_lt */
262 static int
263 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
264 sdb_store_matcher_t *filter)
265 {
266 int status;
267 assert(m->type == MATCHER_LE);
268 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
269 return (status != INT_MAX) && (status <= 0);
270 } /* match_le */
272 static int
273 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
274 sdb_store_matcher_t *filter)
275 {
276 int status;
277 assert(m->type == MATCHER_EQ);
278 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
279 return (status != INT_MAX) && (! status);
280 } /* match_eq */
282 static int
283 match_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
284 sdb_store_matcher_t *filter)
285 {
286 int status;
287 assert(m->type == MATCHER_NE);
288 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
289 return (status != INT_MAX) && status;
290 } /* match_ne */
292 static int
293 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
294 sdb_store_matcher_t *filter)
295 {
296 int status;
297 assert(m->type == MATCHER_GE);
298 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
299 return (status != INT_MAX) && (status >= 0);
300 } /* match_ge */
302 static int
303 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
304 sdb_store_matcher_t *filter)
305 {
306 int status;
307 assert(m->type == MATCHER_GT);
308 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
309 return (status != INT_MAX) && (status > 0);
310 } /* match_gt */
312 static int
313 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
314 sdb_store_matcher_t *filter)
315 {
316 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
317 int status = 1;
319 assert(m->type == MATCHER_IN);
321 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
322 || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
323 status = 0;
325 if (status)
326 status = sdb_data_inarray(&value, &array);
328 sdb_data_free_datum(&value);
329 sdb_data_free_datum(&array);
330 return status;
331 } /* match_in */
333 static int
334 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
335 sdb_store_matcher_t *filter)
336 {
337 sdb_data_t v = SDB_DATA_INIT;
338 int status = 0;
340 regex_t regex;
341 _Bool free_regex = 0;
343 assert((m->type == MATCHER_REGEX)
344 || (m->type == MATCHER_NREGEX));
346 if (! CMP_M(m)->right->type) {
347 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
348 regex = CMP_M(m)->right->data.data.re.regex;
349 }
350 else {
351 sdb_data_t tmp = SDB_DATA_INIT;
352 char *raw;
354 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
355 return 0;
357 if (tmp.type != SDB_TYPE_STRING) {
358 sdb_data_free_datum(&tmp);
359 return 0;
360 }
362 raw = tmp.data.string;
363 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
364 free(raw);
365 return 0;
366 }
368 regex = tmp.data.re.regex;
369 free_regex = 1;
370 free(tmp.data.re.raw);
371 free(raw);
372 }
374 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
375 || (sdb_data_isnull(&v)))
376 status = 0;
377 else {
378 char value[sdb_data_strlen(&v) + 1];
379 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
380 status = 0;
381 else if (! regexec(®ex, value, 0, NULL, 0))
382 status = 1;
383 }
385 if (free_regex)
386 regfree(®ex);
387 sdb_data_free_datum(&v);
388 if (m->type == MATCHER_NREGEX)
389 return !status;
390 return status;
391 } /* match_regex */
393 static int
394 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
395 sdb_store_matcher_t *filter)
396 {
397 sdb_data_t v = SDB_DATA_INIT;
398 int status;
400 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
402 /* TODO: this might hide real errors;
403 * improve error reporting and propagation */
404 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
405 || sdb_data_isnull(&v))
406 status = 1;
407 else
408 status = 0;
410 sdb_data_free_datum(&v);
411 if (m->type == MATCHER_ISNNULL)
412 return !status;
413 return status;
414 } /* match_isnull */
416 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
417 sdb_store_matcher_t *);
419 /* this array needs to be indexable by the matcher types;
420 * -> update the enum in store-private.h when updating this */
421 static matcher_cb
422 matchers[] = {
423 match_logical,
424 match_logical,
425 match_unary,
426 match_name,
427 match_child,
428 match_child,
429 match_child,
430 match_lt,
431 match_le,
432 match_eq,
433 match_ne,
434 match_ge,
435 match_gt,
436 match_in,
437 match_regex,
438 match_regex,
439 match_isnull,
440 match_isnull,
441 };
443 /*
444 * private matcher types
445 */
447 /* initializes a string matcher consuming two elements from ap */
448 static int
449 string_matcher_init(string_matcher_t *m, va_list ap)
450 {
451 const char *name = va_arg(ap, const char *);
452 const char *name_re = va_arg(ap, const char *);
454 if (name) {
455 m->name = strdup(name);
456 if (! m->name)
457 return -1;
458 }
459 if (name_re) {
460 m->name_re = malloc(sizeof(*m->name_re));
461 if (! m->name_re)
462 return -1;
463 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
464 return -1;
465 }
466 return 0;
467 } /* string_matcher_init */
469 static void
470 string_matcher_destroy(string_matcher_t *m)
471 {
472 if (m->name)
473 free(m->name);
474 if (m->name_re) {
475 regfree(m->name_re);
476 free(m->name_re);
477 }
478 } /* string_matcher_destroy */
480 /* initializes a name matcher */
481 static int
482 name_matcher_init(sdb_object_t *obj, va_list ap)
483 {
484 name_matcher_t *m = NAME_M(obj);
485 M(obj)->type = MATCHER_NAME;
486 return string_matcher_init(&m->name, ap);
487 } /* name_matcher_init */
489 static void
490 name_matcher_destroy(sdb_object_t *obj)
491 {
492 name_matcher_t *m = NAME_M(obj);
493 string_matcher_destroy(&m->name);
494 } /* name_matcher_destroy */
496 static int
497 op_matcher_init(sdb_object_t *obj, va_list ap)
498 {
499 M(obj)->type = va_arg(ap, int);
500 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
501 return -1;
503 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
504 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
505 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
506 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
508 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
509 return -1;
510 return 0;
511 } /* op_matcher_init */
513 static void
514 op_matcher_destroy(sdb_object_t *obj)
515 {
516 if (OP_M(obj)->left)
517 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
518 if (OP_M(obj)->right)
519 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
520 } /* op_matcher_destroy */
522 static int
523 child_matcher_init(sdb_object_t *obj, va_list ap)
524 {
525 M(obj)->type = va_arg(ap, int);
526 CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
528 if (! CHILD_M(obj)->m)
529 return -1;
531 sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
532 return 0;
533 } /* child_matcher_init */
535 static void
536 child_matcher_destroy(sdb_object_t *obj)
537 {
538 sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
539 } /* child_matcher_destroy */
541 static int
542 cmp_matcher_init(sdb_object_t *obj, va_list ap)
543 {
544 M(obj)->type = va_arg(ap, int);
546 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
547 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
548 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
549 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
551 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
552 return -1;
553 return 0;
554 } /* cmp_matcher_init */
556 static void
557 cmp_matcher_destroy(sdb_object_t *obj)
558 {
559 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
560 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
561 } /* cmp_matcher_destroy */
563 static int
564 uop_matcher_init(sdb_object_t *obj, va_list ap)
565 {
566 M(obj)->type = va_arg(ap, int);
567 if (M(obj)->type != MATCHER_NOT)
568 return -1;
570 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
571 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
573 if (! UOP_M(obj)->op)
574 return -1;
575 return 0;
576 } /* uop_matcher_init */
578 static void
579 uop_matcher_destroy(sdb_object_t *obj)
580 {
581 if (UOP_M(obj)->op)
582 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
583 } /* uop_matcher_destroy */
585 static int
586 isnull_matcher_init(sdb_object_t *obj, va_list ap)
587 {
588 M(obj)->type = va_arg(ap, int);
589 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
590 return -1;
592 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
593 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
594 return 0;
595 } /* isnull_matcher_init */
597 static void
598 isnull_matcher_destroy(sdb_object_t *obj)
599 {
600 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
601 ISNULL_M(obj)->expr = NULL;
602 } /* isnull_matcher_destroy */
604 static sdb_type_t name_type = {
605 /* size = */ sizeof(name_matcher_t),
606 /* init = */ name_matcher_init,
607 /* destroy = */ name_matcher_destroy,
608 };
610 static sdb_type_t op_type = {
611 /* size = */ sizeof(op_matcher_t),
612 /* init = */ op_matcher_init,
613 /* destroy = */ op_matcher_destroy,
614 };
616 static sdb_type_t uop_type = {
617 /* size = */ sizeof(uop_matcher_t),
618 /* init = */ uop_matcher_init,
619 /* destroy = */ uop_matcher_destroy,
620 };
622 static sdb_type_t child_type = {
623 /* size = */ sizeof(child_matcher_t),
624 /* init = */ child_matcher_init,
625 /* destroy = */ child_matcher_destroy,
626 };
628 static sdb_type_t cmp_type = {
629 /* size = */ sizeof(cmp_matcher_t),
630 /* init = */ cmp_matcher_init,
631 /* destroy = */ cmp_matcher_destroy,
632 };
634 static sdb_type_t isnull_type = {
635 /* size = */ sizeof(isnull_matcher_t),
636 /* init = */ isnull_matcher_init,
637 /* destroy = */ isnull_matcher_destroy,
638 };
640 /*
641 * public API
642 */
644 sdb_store_matcher_t *
645 sdb_store_name_matcher(int type, const char *name, _Bool re)
646 {
647 sdb_store_matcher_t *m;
649 if (re)
650 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
651 else
652 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
654 if (! m)
655 return NULL;
657 NAME_M(m)->obj_type = type;
658 return m;
659 } /* sdb_store_name_matcher */
661 sdb_store_matcher_t *
662 sdb_store_child_matcher(int type, sdb_store_matcher_t *m)
663 {
664 if (type == SDB_SERVICE)
665 type = MATCHER_SERVICE;
666 else if (type == SDB_METRIC)
667 type = MATCHER_METRIC;
668 else if (type == SDB_ATTRIBUTE)
669 type = MATCHER_ATTRIBUTE;
670 else
671 return NULL;
672 return M(sdb_object_create("any-matcher", child_type, type, m));
673 } /* sdb_store_child_matcher */
675 sdb_store_matcher_t *
676 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
677 {
678 return M(sdb_object_create("lt-matcher", cmp_type,
679 MATCHER_LT, left, right));
680 } /* sdb_store_lt_matcher */
682 sdb_store_matcher_t *
683 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
684 {
685 return M(sdb_object_create("le-matcher", cmp_type,
686 MATCHER_LE, left, right));
687 } /* sdb_store_le_matcher */
689 sdb_store_matcher_t *
690 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
691 {
692 return M(sdb_object_create("eq-matcher", cmp_type,
693 MATCHER_EQ, left, right));
694 } /* sdb_store_eq_matcher */
696 sdb_store_matcher_t *
697 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
698 {
699 return M(sdb_object_create("ne-matcher", cmp_type,
700 MATCHER_NE, left, right));
701 } /* sdb_store_ne_matcher */
703 sdb_store_matcher_t *
704 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
705 {
706 return M(sdb_object_create("ge-matcher", cmp_type,
707 MATCHER_GE, left, right));
708 } /* sdb_store_ge_matcher */
710 sdb_store_matcher_t *
711 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
712 {
713 return M(sdb_object_create("gt-matcher", cmp_type,
714 MATCHER_GT, left, right));
715 } /* sdb_store_gt_matcher */
717 sdb_store_matcher_t *
718 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
719 {
720 return M(sdb_object_create("in-matcher", cmp_type,
721 MATCHER_IN, left, right));
722 } /* sdb_store_in_matcher */
724 sdb_store_matcher_t *
725 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
726 {
727 if (! right->type) {
728 if ((right->data.type != SDB_TYPE_STRING)
729 && (right->data.type != SDB_TYPE_REGEX))
730 return NULL;
732 if (right->data.type == SDB_TYPE_STRING) {
733 char *raw = right->data.data.string;
734 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
735 return NULL;
736 free(raw);
737 }
738 }
739 return M(sdb_object_create("regex-matcher", cmp_type,
740 MATCHER_REGEX, left, right));
741 } /* sdb_store_regex_matcher */
743 sdb_store_matcher_t *
744 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
745 {
746 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
747 if (! m)
748 return NULL;
749 m->type = MATCHER_NREGEX;
750 return m;
751 } /* sdb_store_nregex_matcher */
753 sdb_store_matcher_t *
754 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
755 {
756 return M(sdb_object_create("isnull-matcher", isnull_type,
757 MATCHER_ISNULL, expr));
758 } /* sdb_store_isnull_matcher */
760 sdb_store_matcher_t *
761 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
762 {
763 return M(sdb_object_create("isnull-matcher", isnull_type,
764 MATCHER_ISNNULL, expr));
765 } /* sdb_store_isnnull_matcher */
767 sdb_store_matcher_op_cb
768 sdb_store_parse_matcher_op(const char *op)
769 {
770 if (! strcasecmp(op, "<"))
771 return sdb_store_lt_matcher;
772 else if (! strcasecmp(op, "<="))
773 return sdb_store_le_matcher;
774 else if (! strcasecmp(op, "="))
775 return sdb_store_eq_matcher;
776 else if (! strcasecmp(op, "!="))
777 return sdb_store_ne_matcher;
778 else if (! strcasecmp(op, ">="))
779 return sdb_store_ge_matcher;
780 else if (! strcasecmp(op, ">"))
781 return sdb_store_gt_matcher;
782 else if (! strcasecmp(op, "=~"))
783 return sdb_store_regex_matcher;
784 else if (! strcasecmp(op, "!~"))
785 return sdb_store_nregex_matcher;
786 return NULL;
787 } /* sdb_store_parse_matcher_op */
789 int
790 sdb_store_parse_object_type(const char *name)
791 {
792 if (! strcasecmp(name, "host"))
793 return SDB_HOST;
794 else if (! strcasecmp(name, "service"))
795 return SDB_SERVICE;
796 else if (! strcasecmp(name, "metric"))
797 return SDB_METRIC;
798 else if (! strcasecmp(name, "attribute"))
799 return SDB_ATTRIBUTE;
800 return -1;
801 } /* sdb_store_parse_object_type */
803 int
804 sdb_store_parse_object_type_plural(const char *name)
805 {
806 if (! strcasecmp(name, "hosts"))
807 return SDB_HOST;
808 else if (! strcasecmp(name, "services"))
809 return SDB_SERVICE;
810 else if (! strcasecmp(name, "metrics"))
811 return SDB_METRIC;
812 else if (! strcasecmp(name, "attributes"))
813 return SDB_ATTRIBUTE;
814 return -1;
815 } /* sdb_store_parse_object_type_plural */
817 int
818 sdb_store_parse_field_name(const char *name)
819 {
820 if (! strcasecmp(name, "name"))
821 return SDB_FIELD_NAME;
822 else if (! strcasecmp(name, "last_update"))
823 return SDB_FIELD_LAST_UPDATE;
824 else if (! strcasecmp(name, "age"))
825 return SDB_FIELD_AGE;
826 else if (! strcasecmp(name, "interval"))
827 return SDB_FIELD_INTERVAL;
828 else if (! strcasecmp(name, "backend"))
829 return SDB_FIELD_BACKEND;
830 return -1;
831 } /* sdb_store_parse_field_name */
833 static sdb_store_matcher_t *
834 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
835 {
836 sdb_store_matcher_t *tmp;
838 if ((! m) || (! inv))
839 return m;
841 tmp = sdb_store_inv_matcher(m);
842 /* pass ownership to the inverse matcher */
843 sdb_object_deref(SDB_OBJ(m));
844 return tmp;
845 } /* maybe_inv_matcher */
847 sdb_store_matcher_t *
848 sdb_store_matcher_parse_cmp(const char *obj_type,
849 const char *op, sdb_store_expr_t *expr)
850 {
851 int type = -1;
852 _Bool inv = 0;
853 _Bool re = 0;
855 sdb_data_t value = SDB_DATA_INIT;
856 sdb_store_matcher_t *m = NULL;
858 if (! strcasecmp(obj_type, "host"))
859 type = SDB_HOST;
860 else if (! strcasecmp(obj_type, "service"))
861 type = SDB_SERVICE;
862 else if (! strcasecmp(obj_type, "metric"))
863 type = SDB_METRIC;
864 else if (! strcasecmp(obj_type, "attribute"))
865 type = SDB_ATTRIBUTE;
866 else
867 return NULL;
869 /* XXX: this code sucks! */
870 if (! strcasecmp(op, "=")) {
871 /* nothing to do */
872 }
873 else if (! strcasecmp(op, "!=")) {
874 inv = 1;
875 }
876 else if (! strcasecmp(op, "=~")) {
877 re = 1;
878 }
879 else if (! strcasecmp(op, "!~")) {
880 inv = 1;
881 re = 1;
882 }
883 else
884 return NULL;
886 if (! expr)
887 return NULL;
889 if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
890 || (value.type != SDB_TYPE_STRING)) {
891 sdb_data_free_datum(&value);
892 return NULL;
893 }
895 m = sdb_store_name_matcher(type, value.data.string, re);
896 sdb_data_free_datum(&value);
897 return maybe_inv_matcher(m, inv);
898 } /* sdb_store_matcher_parse_cmp */
900 sdb_store_matcher_t *
901 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
902 {
903 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
904 left, right));
905 } /* sdb_store_dis_matcher */
907 sdb_store_matcher_t *
908 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
909 {
910 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
911 left, right));
912 } /* sdb_store_con_matcher */
914 sdb_store_matcher_t *
915 sdb_store_inv_matcher(sdb_store_matcher_t *m)
916 {
917 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
918 } /* sdb_store_inv_matcher */
920 int
921 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
922 sdb_store_matcher_t *filter)
923 {
924 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
925 return 0;
927 /* "NULL" always matches */
928 if ((! m) || (! obj))
929 return 1;
931 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
932 return 0;
934 return matchers[m->type](m, obj, filter);
935 } /* sdb_store_matcher_matches */
937 int
938 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
939 sdb_store_lookup_cb cb, void *user_data)
940 {
941 scan_iter_data_t data = { m, filter, cb, user_data };
943 if (! cb)
944 return -1;
945 return sdb_store_iterate(scan_iter, &data);
946 } /* sdb_store_scan */
948 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */