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 static sdb_attribute_t *
78 attr_get(sdb_host_t *host, const char *name, sdb_store_matcher_t *filter)
79 {
80 sdb_avltree_iter_t *iter = NULL;
81 sdb_attribute_t *attr = NULL;
83 iter = sdb_avltree_get_iter(host->attributes);
84 while (sdb_avltree_iter_has_next(iter)) {
85 sdb_attribute_t *a = ATTR(sdb_avltree_iter_get_next(iter));
87 if (strcasecmp(name, SDB_OBJ(a)->name))
88 continue;
90 assert(STORE_OBJ(a)->type == SDB_ATTRIBUTE);
91 attr = a;
92 break;
93 }
94 sdb_avltree_iter_destroy(iter);
96 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(attr),
97 NULL)))
98 return NULL;
99 return attr;
100 } /* attr_get */
102 /*
103 * matcher implementations
104 */
106 static int
107 match_string(string_matcher_t *m, const char *name)
108 {
109 if ((! m->name) && (! m->name_re))
110 return 1;
112 if (! name)
113 name = "";
115 if (m->name && strcasecmp(m->name, name))
116 return 0;
117 if (m->name_re && regexec(m->name_re, name,
118 /* matches */ 0, NULL, /* flags = */ 0))
119 return 0;
120 return 1;
121 } /* match_string */
123 static int
124 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
125 sdb_store_matcher_t *filter)
126 {
127 int status;
129 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
130 assert(OP_M(m)->left && OP_M(m)->right);
132 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
134 /* lazy evaluation */
135 if ((! status) && (m->type == MATCHER_AND))
136 return status;
137 else if (status && (m->type == MATCHER_OR))
138 return status;
140 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
141 } /* match_logical */
143 static int
144 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
145 sdb_store_matcher_t *filter)
146 {
147 assert(m->type == MATCHER_NOT);
148 assert(UOP_M(m)->op);
150 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
151 } /* match_unary */
153 static int
154 match_name(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
155 sdb_store_matcher_t *filter)
156 {
157 sdb_avltree_iter_t *iter = NULL;
158 int status = 0;
160 assert(m->type == MATCHER_NAME);
162 if (obj->type == NAME_M(m)->obj_type)
163 return match_string(&NAME_M(m)->name, SDB_OBJ(obj)->name);
164 else if (obj->type != SDB_HOST)
165 return 0;
167 switch (NAME_M(m)->obj_type) {
168 case SDB_SERVICE:
169 iter = sdb_avltree_get_iter(HOST(obj)->services);
170 break;
171 case SDB_METRIC:
172 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
173 break;
174 case SDB_ATTRIBUTE:
175 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
176 break;
177 }
179 while (sdb_avltree_iter_has_next(iter)) {
180 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
181 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
182 NULL)))
183 continue;
184 if (match_string(&NAME_M(m)->name, child->name)) {
185 status = 1;
186 break;
187 }
188 }
189 sdb_avltree_iter_destroy(iter);
190 return status;
191 } /* match_name */
193 static int
194 match_attr(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
195 sdb_store_matcher_t *filter)
196 {
197 sdb_attribute_t *attr;
199 assert(m->type == MATCHER_ATTR);
200 assert(ATTR_M(m)->name);
202 if (obj->type != SDB_HOST)
203 return 0;
205 attr = attr_get(HOST(obj), ATTR_M(m)->name, filter);
206 if (attr) {
207 char buf[sdb_data_strlen(&attr->value) + 1];
208 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
209 return 0;
210 if (match_string(&ATTR_M(m)->value, buf))
211 return 1;
212 }
213 return 0;
214 } /* match_attr */
216 static int
217 match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
218 sdb_store_matcher_t *filter)
219 {
220 sdb_avltree_iter_t *iter = NULL;
221 int status = 0;
223 assert((m->type == MATCHER_SERVICE)
224 || (m->type == MATCHER_METRIC)
225 || (m->type == MATCHER_ATTRIBUTE));
227 /* TODO: support all object types */
228 if (obj->type != SDB_HOST)
229 return 0;
231 if (m->type == MATCHER_SERVICE)
232 iter = sdb_avltree_get_iter(HOST(obj)->services);
233 else if (m->type == MATCHER_METRIC)
234 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
235 else if (m->type == SDB_ATTRIBUTE)
236 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
238 while (sdb_avltree_iter_has_next(iter)) {
239 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
240 if (filter && (! sdb_store_matcher_matches(filter,
241 STORE_OBJ(child), NULL)))
242 continue;
244 if (sdb_store_matcher_matches(CHILD_M(m)->m, obj, filter)) {
245 status = 1;
246 break;
247 }
248 }
249 sdb_avltree_iter_destroy(iter);
250 return status;
251 } /* match_child */
253 /*
254 * cmp_expr:
255 * Compare the values of two expressions when evaluating them using the
256 * specified stored object and filter. Returns a value less than, equal to, or
257 * greater than zero if the value of the first expression compares less than,
258 * equal to, or greater than the value of the second expression. Returns
259 * INT_MAX if any of the expressions could not be evaluated or if any of them
260 * evaluated to NULL.
261 */
262 static int
263 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
264 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
265 {
266 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
267 int status;
269 if (sdb_store_expr_eval(e1, obj, &v1, filter))
270 return INT_MAX;
271 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
272 sdb_data_free_datum(&v1);
273 return INT_MAX;
274 }
276 if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
277 status = INT_MAX;
278 else if (v1.type == v2.type)
279 status = sdb_data_cmp(&v1, &v2);
280 else
281 status = sdb_data_strcmp(&v1, &v2);
283 sdb_data_free_datum(&v1);
284 sdb_data_free_datum(&v2);
285 return status;
286 } /* cmp_expr */
288 static int
289 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
290 sdb_store_matcher_t *filter)
291 {
292 int status;
293 assert(m->type == MATCHER_LT);
294 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
295 return (status != INT_MAX) && (status < 0);
296 } /* match_lt */
298 static int
299 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
300 sdb_store_matcher_t *filter)
301 {
302 int status;
303 assert(m->type == MATCHER_LE);
304 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
305 return (status != INT_MAX) && (status <= 0);
306 } /* match_le */
308 static int
309 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
310 sdb_store_matcher_t *filter)
311 {
312 int status;
313 assert(m->type == MATCHER_EQ);
314 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
315 return (status != INT_MAX) && (! status);
316 } /* match_eq */
318 static int
319 match_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
320 sdb_store_matcher_t *filter)
321 {
322 int status;
323 assert(m->type == MATCHER_NE);
324 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
325 return (status != INT_MAX) && status;
326 } /* match_ne */
328 static int
329 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
330 sdb_store_matcher_t *filter)
331 {
332 int status;
333 assert(m->type == MATCHER_GE);
334 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
335 return (status != INT_MAX) && (status >= 0);
336 } /* match_ge */
338 static int
339 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
340 sdb_store_matcher_t *filter)
341 {
342 int status;
343 assert(m->type == MATCHER_GT);
344 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
345 return (status != INT_MAX) && (status > 0);
346 } /* match_gt */
348 static int
349 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
350 sdb_store_matcher_t *filter)
351 {
352 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
353 int status = 1;
355 assert(m->type == MATCHER_IN);
357 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
358 || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
359 status = 0;
361 if (status)
362 status = sdb_data_inarray(&value, &array);
364 sdb_data_free_datum(&value);
365 sdb_data_free_datum(&array);
366 return status;
367 } /* match_in */
369 static int
370 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
371 sdb_store_matcher_t *filter)
372 {
373 sdb_data_t v = SDB_DATA_INIT;
374 int status = 0;
376 regex_t regex;
377 _Bool free_regex = 0;
379 assert((m->type == MATCHER_REGEX)
380 || (m->type == MATCHER_NREGEX));
382 if (! CMP_M(m)->right->type) {
383 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
384 regex = CMP_M(m)->right->data.data.re.regex;
385 }
386 else {
387 sdb_data_t tmp = SDB_DATA_INIT;
388 char *raw;
390 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
391 return 0;
393 if (tmp.type != SDB_TYPE_STRING) {
394 sdb_data_free_datum(&tmp);
395 return 0;
396 }
398 raw = tmp.data.string;
399 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
400 free(raw);
401 return 0;
402 }
404 regex = tmp.data.re.regex;
405 free_regex = 1;
406 free(tmp.data.re.raw);
407 free(raw);
408 }
410 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
411 || (sdb_data_isnull(&v)))
412 status = 0;
413 else {
414 char value[sdb_data_strlen(&v) + 1];
415 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
416 status = 0;
417 else if (! regexec(®ex, value, 0, NULL, 0))
418 status = 1;
419 }
421 if (free_regex)
422 regfree(®ex);
423 sdb_data_free_datum(&v);
424 if (m->type == MATCHER_NREGEX)
425 return !status;
426 return status;
427 } /* match_regex */
429 static int
430 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
431 sdb_store_matcher_t *filter)
432 {
433 sdb_data_t v = SDB_DATA_INIT;
434 int status;
436 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
438 /* TODO: this might hide real errors;
439 * improve error reporting and propagation */
440 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
441 || sdb_data_isnull(&v))
442 status = 1;
443 else
444 status = 0;
446 sdb_data_free_datum(&v);
447 if (m->type == MATCHER_ISNNULL)
448 return !status;
449 return status;
450 } /* match_isnull */
452 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
453 sdb_store_matcher_t *);
455 /* this array needs to be indexable by the matcher types;
456 * -> update the enum in store-private.h when updating this */
457 static matcher_cb
458 matchers[] = {
459 match_logical,
460 match_logical,
461 match_unary,
462 match_name,
463 match_attr,
464 match_child,
465 match_child,
466 match_child,
467 match_lt,
468 match_le,
469 match_eq,
470 match_ne,
471 match_ge,
472 match_gt,
473 match_in,
474 match_regex,
475 match_regex,
476 match_isnull,
477 match_isnull,
478 };
480 /*
481 * private matcher types
482 */
484 /* initializes a string matcher consuming two elements from ap */
485 static int
486 string_matcher_init(string_matcher_t *m, va_list ap)
487 {
488 const char *name = va_arg(ap, const char *);
489 const char *name_re = va_arg(ap, const char *);
491 if (name) {
492 m->name = strdup(name);
493 if (! m->name)
494 return -1;
495 }
496 if (name_re) {
497 m->name_re = malloc(sizeof(*m->name_re));
498 if (! m->name_re)
499 return -1;
500 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
501 return -1;
502 }
503 return 0;
504 } /* string_matcher_init */
506 static void
507 string_matcher_destroy(string_matcher_t *m)
508 {
509 if (m->name)
510 free(m->name);
511 if (m->name_re) {
512 regfree(m->name_re);
513 free(m->name_re);
514 }
515 } /* string_matcher_destroy */
517 /* initializes a name matcher */
518 static int
519 name_matcher_init(sdb_object_t *obj, va_list ap)
520 {
521 name_matcher_t *m = NAME_M(obj);
522 M(obj)->type = MATCHER_NAME;
523 return string_matcher_init(&m->name, ap);
524 } /* name_matcher_init */
526 static void
527 name_matcher_destroy(sdb_object_t *obj)
528 {
529 name_matcher_t *m = NAME_M(obj);
530 string_matcher_destroy(&m->name);
531 } /* name_matcher_destroy */
533 static int
534 attr_matcher_init(sdb_object_t *obj, va_list ap)
535 {
536 attr_matcher_t *attr = ATTR_M(obj);
537 const char *name = va_arg(ap, const char *);
539 M(obj)->type = MATCHER_ATTR;
540 if (name) {
541 attr->name = strdup(name);
542 if (! attr->name)
543 return -1;
544 }
545 return string_matcher_init(&attr->value, ap);
546 } /* attr_matcher_init */
548 static void
549 attr_matcher_destroy(sdb_object_t *obj)
550 {
551 attr_matcher_t *attr = ATTR_M(obj);
552 if (attr->name)
553 free(attr->name);
554 attr->name = NULL;
555 string_matcher_destroy(&attr->value);
556 } /* attr_matcher_destroy */
558 static int
559 op_matcher_init(sdb_object_t *obj, va_list ap)
560 {
561 M(obj)->type = va_arg(ap, int);
562 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
563 return -1;
565 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
566 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
567 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
568 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
570 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
571 return -1;
572 return 0;
573 } /* op_matcher_init */
575 static void
576 op_matcher_destroy(sdb_object_t *obj)
577 {
578 if (OP_M(obj)->left)
579 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
580 if (OP_M(obj)->right)
581 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
582 } /* op_matcher_destroy */
584 static int
585 child_matcher_init(sdb_object_t *obj, va_list ap)
586 {
587 M(obj)->type = va_arg(ap, int);
588 CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
590 if (! CHILD_M(obj)->m)
591 return -1;
593 sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
594 return 0;
595 } /* child_matcher_init */
597 static void
598 child_matcher_destroy(sdb_object_t *obj)
599 {
600 sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
601 } /* child_matcher_destroy */
603 static int
604 cmp_matcher_init(sdb_object_t *obj, va_list ap)
605 {
606 M(obj)->type = va_arg(ap, int);
608 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
609 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
610 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
611 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
613 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
614 return -1;
615 return 0;
616 } /* cmp_matcher_init */
618 static void
619 cmp_matcher_destroy(sdb_object_t *obj)
620 {
621 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
622 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
623 } /* cmp_matcher_destroy */
625 static int
626 uop_matcher_init(sdb_object_t *obj, va_list ap)
627 {
628 M(obj)->type = va_arg(ap, int);
629 if (M(obj)->type != MATCHER_NOT)
630 return -1;
632 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
633 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
635 if (! UOP_M(obj)->op)
636 return -1;
637 return 0;
638 } /* uop_matcher_init */
640 static void
641 uop_matcher_destroy(sdb_object_t *obj)
642 {
643 if (UOP_M(obj)->op)
644 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
645 } /* uop_matcher_destroy */
647 static int
648 isnull_matcher_init(sdb_object_t *obj, va_list ap)
649 {
650 M(obj)->type = va_arg(ap, int);
651 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
652 return -1;
654 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
655 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
656 return 0;
657 } /* isnull_matcher_init */
659 static void
660 isnull_matcher_destroy(sdb_object_t *obj)
661 {
662 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
663 ISNULL_M(obj)->expr = NULL;
664 } /* isnull_matcher_destroy */
666 static sdb_type_t name_type = {
667 /* size = */ sizeof(name_matcher_t),
668 /* init = */ name_matcher_init,
669 /* destroy = */ name_matcher_destroy,
670 };
672 static sdb_type_t attr_type = {
673 /* size = */ sizeof(attr_matcher_t),
674 /* init = */ attr_matcher_init,
675 /* destroy = */ attr_matcher_destroy,
676 };
678 static sdb_type_t op_type = {
679 /* size = */ sizeof(op_matcher_t),
680 /* init = */ op_matcher_init,
681 /* destroy = */ op_matcher_destroy,
682 };
684 static sdb_type_t uop_type = {
685 /* size = */ sizeof(uop_matcher_t),
686 /* init = */ uop_matcher_init,
687 /* destroy = */ uop_matcher_destroy,
688 };
690 static sdb_type_t child_type = {
691 /* size = */ sizeof(child_matcher_t),
692 /* init = */ child_matcher_init,
693 /* destroy = */ child_matcher_destroy,
694 };
696 static sdb_type_t cmp_type = {
697 /* size = */ sizeof(cmp_matcher_t),
698 /* init = */ cmp_matcher_init,
699 /* destroy = */ cmp_matcher_destroy,
700 };
702 static sdb_type_t isnull_type = {
703 /* size = */ sizeof(isnull_matcher_t),
704 /* init = */ isnull_matcher_init,
705 /* destroy = */ isnull_matcher_destroy,
706 };
708 /*
709 * public API
710 */
712 sdb_store_matcher_t *
713 sdb_store_name_matcher(int type, const char *name, _Bool re)
714 {
715 sdb_store_matcher_t *m;
717 if (re)
718 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
719 else
720 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
722 if (! m)
723 return NULL;
725 NAME_M(m)->obj_type = type;
726 return m;
727 } /* sdb_store_name_matcher */
729 sdb_store_matcher_t *
730 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
731 {
732 sdb_store_matcher_t *m;
734 if (! name)
735 return NULL;
737 if (re)
738 m = M(sdb_object_create("attr-matcher", attr_type,
739 name, NULL, value));
740 else
741 m = M(sdb_object_create("attr-matcher", attr_type,
742 name, value, NULL));
743 return m;
744 } /* sdb_store_attr_matcher */
746 sdb_store_matcher_t *
747 sdb_store_child_matcher(int type, sdb_store_matcher_t *m)
748 {
749 if (type == SDB_SERVICE)
750 type = MATCHER_SERVICE;
751 else if (type == SDB_METRIC)
752 type = MATCHER_METRIC;
753 else if (type == SDB_ATTRIBUTE)
754 type = MATCHER_ATTRIBUTE;
755 else
756 return NULL;
757 return M(sdb_object_create("any-matcher", child_type, type, m));
758 } /* sdb_store_child_matcher */
760 sdb_store_matcher_t *
761 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
762 {
763 return M(sdb_object_create("lt-matcher", cmp_type,
764 MATCHER_LT, left, right));
765 } /* sdb_store_lt_matcher */
767 sdb_store_matcher_t *
768 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
769 {
770 return M(sdb_object_create("le-matcher", cmp_type,
771 MATCHER_LE, left, right));
772 } /* sdb_store_le_matcher */
774 sdb_store_matcher_t *
775 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
776 {
777 return M(sdb_object_create("eq-matcher", cmp_type,
778 MATCHER_EQ, left, right));
779 } /* sdb_store_eq_matcher */
781 sdb_store_matcher_t *
782 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
783 {
784 return M(sdb_object_create("ne-matcher", cmp_type,
785 MATCHER_NE, left, right));
786 } /* sdb_store_ne_matcher */
788 sdb_store_matcher_t *
789 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
790 {
791 return M(sdb_object_create("ge-matcher", cmp_type,
792 MATCHER_GE, left, right));
793 } /* sdb_store_ge_matcher */
795 sdb_store_matcher_t *
796 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
797 {
798 return M(sdb_object_create("gt-matcher", cmp_type,
799 MATCHER_GT, left, right));
800 } /* sdb_store_gt_matcher */
802 sdb_store_matcher_t *
803 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
804 {
805 return M(sdb_object_create("in-matcher", cmp_type,
806 MATCHER_IN, left, right));
807 } /* sdb_store_in_matcher */
809 sdb_store_matcher_t *
810 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
811 {
812 if (! right->type) {
813 if ((right->data.type != SDB_TYPE_STRING)
814 && (right->data.type != SDB_TYPE_REGEX))
815 return NULL;
817 if (right->data.type == SDB_TYPE_STRING) {
818 char *raw = right->data.data.string;
819 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
820 return NULL;
821 free(raw);
822 }
823 }
824 return M(sdb_object_create("regex-matcher", cmp_type,
825 MATCHER_REGEX, left, right));
826 } /* sdb_store_regex_matcher */
828 sdb_store_matcher_t *
829 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
830 {
831 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
832 if (! m)
833 return NULL;
834 m->type = MATCHER_NREGEX;
835 return m;
836 } /* sdb_store_nregex_matcher */
838 sdb_store_matcher_t *
839 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
840 {
841 return M(sdb_object_create("isnull-matcher", isnull_type,
842 MATCHER_ISNULL, expr));
843 } /* sdb_store_isnull_matcher */
845 sdb_store_matcher_t *
846 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
847 {
848 return M(sdb_object_create("isnull-matcher", isnull_type,
849 MATCHER_ISNNULL, expr));
850 } /* sdb_store_isnnull_matcher */
852 sdb_store_matcher_op_cb
853 sdb_store_parse_matcher_op(const char *op)
854 {
855 if (! strcasecmp(op, "<"))
856 return sdb_store_lt_matcher;
857 else if (! strcasecmp(op, "<="))
858 return sdb_store_le_matcher;
859 else if (! strcasecmp(op, "="))
860 return sdb_store_eq_matcher;
861 else if (! strcasecmp(op, "!="))
862 return sdb_store_ne_matcher;
863 else if (! strcasecmp(op, ">="))
864 return sdb_store_ge_matcher;
865 else if (! strcasecmp(op, ">"))
866 return sdb_store_gt_matcher;
867 else if (! strcasecmp(op, "=~"))
868 return sdb_store_regex_matcher;
869 else if (! strcasecmp(op, "!~"))
870 return sdb_store_nregex_matcher;
871 return NULL;
872 } /* sdb_store_parse_matcher_op */
874 int
875 sdb_store_parse_object_type_plural(const char *name)
876 {
877 if (! strcasecmp(name, "hosts"))
878 return SDB_HOST;
879 else if (! strcasecmp(name, "services"))
880 return SDB_SERVICE;
881 else if (! strcasecmp(name, "metrics"))
882 return SDB_METRIC;
883 return -1;
884 } /* sdb_store_parse_object_type_plural */
886 int
887 sdb_store_parse_field_name(const char *name)
888 {
889 if (! strcasecmp(name, "name"))
890 return SDB_FIELD_NAME;
891 else if (! strcasecmp(name, "last_update"))
892 return SDB_FIELD_LAST_UPDATE;
893 else if (! strcasecmp(name, "age"))
894 return SDB_FIELD_AGE;
895 else if (! strcasecmp(name, "interval"))
896 return SDB_FIELD_INTERVAL;
897 else if (! strcasecmp(name, "backend"))
898 return SDB_FIELD_BACKEND;
899 return -1;
900 } /* sdb_store_parse_field_name */
902 static sdb_store_matcher_t *
903 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
904 {
905 sdb_store_matcher_t *tmp;
907 if ((! m) || (! inv))
908 return m;
910 tmp = sdb_store_inv_matcher(m);
911 /* pass ownership to the inverse matcher */
912 sdb_object_deref(SDB_OBJ(m));
913 return tmp;
914 } /* maybe_inv_matcher */
916 sdb_store_matcher_t *
917 sdb_store_matcher_parse_cmp(const char *obj_type,
918 const char *op, sdb_store_expr_t *expr)
919 {
920 int type = -1;
921 _Bool inv = 0;
922 _Bool re = 0;
924 sdb_data_t value = SDB_DATA_INIT;
925 sdb_store_matcher_t *m = NULL;
927 if (! strcasecmp(obj_type, "host"))
928 type = SDB_HOST;
929 else if (! strcasecmp(obj_type, "service"))
930 type = SDB_SERVICE;
931 else if (! strcasecmp(obj_type, "metric"))
932 type = SDB_METRIC;
933 else if (! strcasecmp(obj_type, "attribute"))
934 type = SDB_ATTRIBUTE;
935 else
936 return NULL;
938 /* XXX: this code sucks! */
939 if (! strcasecmp(op, "=")) {
940 /* nothing to do */
941 }
942 else if (! strcasecmp(op, "!=")) {
943 inv = 1;
944 }
945 else if (! strcasecmp(op, "=~")) {
946 re = 1;
947 }
948 else if (! strcasecmp(op, "!~")) {
949 inv = 1;
950 re = 1;
951 }
952 else
953 return NULL;
955 if (! expr)
956 return NULL;
958 if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
959 || (value.type != SDB_TYPE_STRING)) {
960 sdb_data_free_datum(&value);
961 return NULL;
962 }
964 m = sdb_store_name_matcher(type, value.data.string, re);
965 sdb_data_free_datum(&value);
966 return maybe_inv_matcher(m, inv);
967 } /* sdb_store_matcher_parse_cmp */
969 sdb_store_matcher_t *
970 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
971 {
972 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
973 left, right));
974 } /* sdb_store_dis_matcher */
976 sdb_store_matcher_t *
977 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
978 {
979 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
980 left, right));
981 } /* sdb_store_con_matcher */
983 sdb_store_matcher_t *
984 sdb_store_inv_matcher(sdb_store_matcher_t *m)
985 {
986 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
987 } /* sdb_store_inv_matcher */
989 int
990 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
991 sdb_store_matcher_t *filter)
992 {
993 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
994 return 0;
996 /* "NULL" always matches */
997 if ((! m) || (! obj))
998 return 1;
1000 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1001 return 0;
1003 return matchers[m->type](m, obj, filter);
1004 } /* sdb_store_matcher_matches */
1006 int
1007 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1008 sdb_store_lookup_cb cb, void *user_data)
1009 {
1010 scan_iter_data_t data = { m, filter, cb, user_data };
1012 if (! cb)
1013 return -1;
1014 return sdb_store_iterate(scan_iter, &data);
1015 } /* sdb_store_scan */
1017 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */