05e6b74d51b70117bc01a06b1b17ca9ef0f77f50
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_cmp_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_CMP_LT);
294 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
295 return (status != INT_MAX) && (status < 0);
296 } /* match_cmp_lt */
298 static int
299 match_cmp_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_CMP_LE);
304 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
305 return (status != INT_MAX) && (status <= 0);
306 } /* match_cmp_le */
308 static int
309 match_cmp_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_CMP_EQ);
314 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
315 return (status != INT_MAX) && (! status);
316 } /* match_cmp_eq */
318 static int
319 match_cmp_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_CMP_NE);
324 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
325 return (status != INT_MAX) && status;
326 } /* match_cmp_ne */
328 static int
329 match_cmp_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_CMP_GE);
334 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
335 return (status != INT_MAX) && (status >= 0);
336 } /* match_cmp_ge */
338 static int
339 match_cmp_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_CMP_GT);
344 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
345 return (status != INT_MAX) && (status > 0);
346 } /* match_cmp_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_cmp_lt,
468 match_cmp_le,
469 match_cmp_eq,
470 match_cmp_ne,
471 match_cmp_ge,
472 match_cmp_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 /*
761 * TODO: Rename sdb_store_cmp_* to sdb_store_* once the old code is unused and
762 * has been removed.
763 */
765 sdb_store_matcher_t *
766 sdb_store_cmp_lt(sdb_store_expr_t *left, sdb_store_expr_t *right)
767 {
768 return M(sdb_object_create("lt-matcher", cmp_type,
769 MATCHER_CMP_LT, left, right));
770 } /* sdb_store_cmp_lt */
772 sdb_store_matcher_t *
773 sdb_store_cmp_le(sdb_store_expr_t *left, sdb_store_expr_t *right)
774 {
775 return M(sdb_object_create("le-matcher", cmp_type,
776 MATCHER_CMP_LE, left, right));
777 } /* sdb_store_cmp_le */
779 sdb_store_matcher_t *
780 sdb_store_cmp_eq(sdb_store_expr_t *left, sdb_store_expr_t *right)
781 {
782 return M(sdb_object_create("eq-matcher", cmp_type,
783 MATCHER_CMP_EQ, left, right));
784 } /* sdb_store_cmp_eq */
786 sdb_store_matcher_t *
787 sdb_store_cmp_ne(sdb_store_expr_t *left, sdb_store_expr_t *right)
788 {
789 return M(sdb_object_create("ne-matcher", cmp_type,
790 MATCHER_CMP_NE, left, right));
791 } /* sdb_store_cmp_ne */
793 sdb_store_matcher_t *
794 sdb_store_cmp_ge(sdb_store_expr_t *left, sdb_store_expr_t *right)
795 {
796 return M(sdb_object_create("ge-matcher", cmp_type,
797 MATCHER_CMP_GE, left, right));
798 } /* sdb_store_cmp_ge */
800 sdb_store_matcher_t *
801 sdb_store_cmp_gt(sdb_store_expr_t *left, sdb_store_expr_t *right)
802 {
803 return M(sdb_object_create("gt-matcher", cmp_type,
804 MATCHER_CMP_GT, left, right));
805 } /* sdb_store_cmp_gt */
807 sdb_store_matcher_t *
808 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
809 {
810 return M(sdb_object_create("in-matcher", cmp_type,
811 MATCHER_IN, left, right));
812 } /* sdb_store_in_matcher */
814 sdb_store_matcher_t *
815 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
816 {
817 if (! right->type) {
818 if ((right->data.type != SDB_TYPE_STRING)
819 && (right->data.type != SDB_TYPE_REGEX))
820 return NULL;
822 if (right->data.type == SDB_TYPE_STRING) {
823 char *raw = right->data.data.string;
824 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
825 return NULL;
826 free(raw);
827 }
828 }
829 return M(sdb_object_create("regex-matcher", cmp_type,
830 MATCHER_REGEX, left, right));
831 } /* sdb_store_regex_matcher */
833 sdb_store_matcher_t *
834 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
835 {
836 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
837 if (! m)
838 return NULL;
839 m->type = MATCHER_NREGEX;
840 return m;
841 } /* sdb_store_nregex_matcher */
843 sdb_store_matcher_t *
844 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
845 {
846 return M(sdb_object_create("isnull-matcher", isnull_type,
847 MATCHER_ISNULL, expr));
848 } /* sdb_store_isnull_matcher */
850 sdb_store_matcher_t *
851 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
852 {
853 return M(sdb_object_create("isnull-matcher", isnull_type,
854 MATCHER_ISNNULL, expr));
855 } /* sdb_store_isnnull_matcher */
857 sdb_store_matcher_op_cb
858 sdb_store_parse_matcher_op(const char *op)
859 {
860 if (! strcasecmp(op, "<"))
861 return sdb_store_cmp_lt;
862 else if (! strcasecmp(op, "<="))
863 return sdb_store_cmp_le;
864 else if (! strcasecmp(op, "="))
865 return sdb_store_cmp_eq;
866 else if (! strcasecmp(op, "!="))
867 return sdb_store_cmp_ne;
868 else if (! strcasecmp(op, ">="))
869 return sdb_store_cmp_ge;
870 else if (! strcasecmp(op, ">"))
871 return sdb_store_cmp_gt;
872 else if (! strcasecmp(op, "=~"))
873 return sdb_store_regex_matcher;
874 else if (! strcasecmp(op, "!~"))
875 return sdb_store_nregex_matcher;
876 return NULL;
877 } /* sdb_store_parse_matcher_op */
879 int
880 sdb_store_parse_object_type_plural(const char *name)
881 {
882 if (! strcasecmp(name, "hosts"))
883 return SDB_HOST;
884 else if (! strcasecmp(name, "services"))
885 return SDB_SERVICE;
886 else if (! strcasecmp(name, "metrics"))
887 return SDB_METRIC;
888 return -1;
889 } /* sdb_store_parse_object_type_plural */
891 int
892 sdb_store_parse_field_name(const char *name)
893 {
894 if (! strcasecmp(name, "name"))
895 return SDB_FIELD_NAME;
896 else if (! strcasecmp(name, "last_update"))
897 return SDB_FIELD_LAST_UPDATE;
898 else if (! strcasecmp(name, "age"))
899 return SDB_FIELD_AGE;
900 else if (! strcasecmp(name, "interval"))
901 return SDB_FIELD_INTERVAL;
902 else if (! strcasecmp(name, "backend"))
903 return SDB_FIELD_BACKEND;
904 return -1;
905 } /* sdb_store_parse_field_name */
907 static sdb_store_matcher_t *
908 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
909 {
910 sdb_store_matcher_t *tmp;
912 if ((! m) || (! inv))
913 return m;
915 tmp = sdb_store_inv_matcher(m);
916 /* pass ownership to the inverse matcher */
917 sdb_object_deref(SDB_OBJ(m));
918 return tmp;
919 } /* maybe_inv_matcher */
921 sdb_store_matcher_t *
922 sdb_store_matcher_parse_cmp(const char *obj_type,
923 const char *op, sdb_store_expr_t *expr)
924 {
925 int type = -1;
926 _Bool inv = 0;
927 _Bool re = 0;
929 sdb_data_t value = SDB_DATA_INIT;
930 sdb_store_matcher_t *m = NULL;
932 if (! strcasecmp(obj_type, "host"))
933 type = SDB_HOST;
934 else if (! strcasecmp(obj_type, "service"))
935 type = SDB_SERVICE;
936 else if (! strcasecmp(obj_type, "metric"))
937 type = SDB_METRIC;
938 else if (! strcasecmp(obj_type, "attribute"))
939 type = SDB_ATTRIBUTE;
940 else
941 return NULL;
943 /* XXX: this code sucks! */
944 if (! strcasecmp(op, "=")) {
945 /* nothing to do */
946 }
947 else if (! strcasecmp(op, "!=")) {
948 inv = 1;
949 }
950 else if (! strcasecmp(op, "=~")) {
951 re = 1;
952 }
953 else if (! strcasecmp(op, "!~")) {
954 inv = 1;
955 re = 1;
956 }
957 else
958 return NULL;
960 if (! expr)
961 return NULL;
963 if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
964 || (value.type != SDB_TYPE_STRING)) {
965 sdb_data_free_datum(&value);
966 return NULL;
967 }
969 m = sdb_store_name_matcher(type, value.data.string, re);
970 sdb_data_free_datum(&value);
971 return maybe_inv_matcher(m, inv);
972 } /* sdb_store_matcher_parse_cmp */
974 sdb_store_matcher_t *
975 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
976 {
977 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
978 left, right));
979 } /* sdb_store_dis_matcher */
981 sdb_store_matcher_t *
982 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
983 {
984 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
985 left, right));
986 } /* sdb_store_con_matcher */
988 sdb_store_matcher_t *
989 sdb_store_inv_matcher(sdb_store_matcher_t *m)
990 {
991 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
992 } /* sdb_store_inv_matcher */
994 int
995 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
996 sdb_store_matcher_t *filter)
997 {
998 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
999 return 0;
1001 /* "NULL" always matches */
1002 if ((! m) || (! obj))
1003 return 1;
1005 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1006 return 0;
1008 return matchers[m->type](m, obj, filter);
1009 } /* sdb_store_matcher_matches */
1011 int
1012 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1013 sdb_store_lookup_cb cb, void *user_data)
1014 {
1015 scan_iter_data_t data = { m, filter, cb, user_data };
1017 if (! cb)
1018 return -1;
1019 return sdb_store_iterate(scan_iter, &data);
1020 } /* sdb_store_scan */
1022 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */