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 * conditional implementations
104 */
106 static int
107 attr_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
108 sdb_store_matcher_t *filter)
109 {
110 sdb_attribute_t *attr;
111 sdb_data_t value = SDB_DATA_INIT;
112 int status;
114 if (obj->type != SDB_HOST)
115 return INT_MAX;
117 if (sdb_store_expr_eval(ATTR_C(cond)->expr, obj, &value, filter))
118 return INT_MAX;
120 attr = attr_get(HOST(obj), ATTR_C(cond)->name, filter);
121 if (! attr)
122 status = INT_MAX;
123 else if (attr->value.type != value.type)
124 status = sdb_data_strcmp(&attr->value, &value);
125 else
126 status = sdb_data_cmp(&attr->value, &value);
127 sdb_data_free_datum(&value);
128 return status;
129 } /* attr_cmp */
131 static int
132 obj_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
133 sdb_store_matcher_t *filter)
134 {
135 sdb_data_t obj_value = SDB_DATA_INIT;
136 sdb_data_t value = SDB_DATA_INIT;
137 int status;
139 if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value, filter))
140 return INT_MAX;
142 if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) {
143 /* this implementation is not actually a conditional but rather checks
144 * for equality (or rather, existence) only */
145 size_t i;
147 if (value.type != SDB_TYPE_STRING)
148 return INT_MAX;
150 status = INT_MAX;
151 for (i = 0; i < obj->backends_num; ++i) {
152 if (! strcasecmp(obj->backends[i], value.data.string)) {
153 status = 0;
154 break;
155 }
156 }
157 sdb_data_free_datum(&value);
158 return status;
159 }
161 if (sdb_store_get_field(obj, OBJ_C(cond)->field, &obj_value))
162 return INT_MAX;
163 if (obj_value.type != value.type) {
164 sdb_data_free_datum(&obj_value);
165 sdb_data_free_datum(&value);
166 return INT_MAX;
167 }
169 status = sdb_data_cmp(&obj_value, &value);
170 sdb_data_free_datum(&obj_value);
171 sdb_data_free_datum(&value);
172 return status;
173 } /* obj_cmp */
175 /*
176 * matcher implementations
177 */
179 static int
180 match_string(string_matcher_t *m, const char *name)
181 {
182 if ((! m->name) && (! m->name_re))
183 return 1;
185 if (! name)
186 name = "";
188 if (m->name && strcasecmp(m->name, name))
189 return 0;
190 if (m->name_re && regexec(m->name_re, name,
191 /* matches */ 0, NULL, /* flags = */ 0))
192 return 0;
193 return 1;
194 } /* match_string */
196 static int
197 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
198 sdb_store_matcher_t *filter)
199 {
200 int status;
202 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
203 assert(OP_M(m)->left && OP_M(m)->right);
205 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
207 /* lazy evaluation */
208 if ((! status) && (m->type == MATCHER_AND))
209 return status;
210 else if (status && (m->type == MATCHER_OR))
211 return status;
213 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
214 } /* match_logical */
216 static int
217 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
218 sdb_store_matcher_t *filter)
219 {
220 assert(m->type == MATCHER_NOT);
221 assert(UOP_M(m)->op);
223 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
224 } /* match_unary */
226 static int
227 match_name(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
228 sdb_store_matcher_t *filter)
229 {
230 sdb_avltree_iter_t *iter = NULL;
231 int status = 0;
233 assert(m->type == MATCHER_NAME);
235 if (obj->type == NAME_M(m)->obj_type)
236 return match_string(&NAME_M(m)->name, SDB_OBJ(obj)->name);
237 else if (obj->type != SDB_HOST)
238 return 0;
240 switch (NAME_M(m)->obj_type) {
241 case SDB_SERVICE:
242 iter = sdb_avltree_get_iter(HOST(obj)->services);
243 break;
244 case SDB_METRIC:
245 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
246 break;
247 case SDB_ATTRIBUTE:
248 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
249 break;
250 }
252 while (sdb_avltree_iter_has_next(iter)) {
253 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
254 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
255 NULL)))
256 continue;
257 if (match_string(&NAME_M(m)->name, child->name)) {
258 status = 1;
259 break;
260 }
261 }
262 sdb_avltree_iter_destroy(iter);
263 return status;
264 } /* match_name */
266 static int
267 match_attr(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
268 sdb_store_matcher_t *filter)
269 {
270 sdb_attribute_t *attr;
272 assert(m->type == MATCHER_ATTR);
273 assert(ATTR_M(m)->name);
275 if (obj->type != SDB_HOST)
276 return 0;
278 attr = attr_get(HOST(obj), ATTR_M(m)->name, filter);
279 if (attr) {
280 char buf[sdb_data_strlen(&attr->value) + 1];
281 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
282 return 0;
283 if (match_string(&ATTR_M(m)->value, buf))
284 return 1;
285 }
286 return 0;
287 } /* match_attr */
289 static int
290 match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
291 sdb_store_matcher_t *filter)
292 {
293 sdb_avltree_iter_t *iter = NULL;
294 int status = 0;
296 assert((m->type == MATCHER_SERVICE)
297 || (m->type == MATCHER_METRIC)
298 || (m->type == MATCHER_ATTRIBUTE));
300 /* TODO: support all object types */
301 if (obj->type != SDB_HOST)
302 return 0;
304 if (m->type == MATCHER_SERVICE)
305 iter = sdb_avltree_get_iter(HOST(obj)->services);
306 else if (m->type == MATCHER_METRIC)
307 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
308 else if (m->type == SDB_ATTRIBUTE)
309 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
311 while (sdb_avltree_iter_has_next(iter)) {
312 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
313 if (filter && (! sdb_store_matcher_matches(filter,
314 STORE_OBJ(child), NULL)))
315 continue;
317 if (sdb_store_matcher_matches(CHILD_M(m)->m, obj, filter)) {
318 status = 1;
319 break;
320 }
321 }
322 sdb_avltree_iter_destroy(iter);
323 return status;
324 } /* match_child */
326 static int
327 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
328 sdb_store_matcher_t *filter)
329 {
330 int status;
331 assert(m->type == MATCHER_LT);
332 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
333 return (status != INT_MAX) && (status < 0);
334 } /* match_lt */
336 static int
337 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
338 sdb_store_matcher_t *filter)
339 {
340 int status;
341 assert(m->type == MATCHER_LE);
342 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
343 return (status != INT_MAX) && (status <= 0);
344 } /* match_le */
346 static int
347 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
348 sdb_store_matcher_t *filter)
349 {
350 int status;
351 assert(m->type == MATCHER_EQ);
352 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
353 return (status != INT_MAX) && (! status);
354 } /* match_eq */
356 static int
357 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
358 sdb_store_matcher_t *filter)
359 {
360 int status;
361 assert(m->type == MATCHER_GE);
362 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
363 return (status != INT_MAX) && (status >= 0);
364 } /* match_ge */
366 static int
367 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
368 sdb_store_matcher_t *filter)
369 {
370 int status;
371 assert(m->type == MATCHER_GT);
372 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
373 return (status != INT_MAX) && (status > 0);
374 } /* match_gt */
376 /*
377 * cmp_expr:
378 * Compare the values of two expressions when evaluating them using the
379 * specified stored object and filter. Returns a value less than, equal to, or
380 * greater than zero if the value of the first expression compares less than,
381 * equal to, or greater than the value of the second expression. Returns
382 * INT_MAX if any of the expressions could not be evaluated or if any of them
383 * evaluated to NULL.
384 */
385 static int
386 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
387 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
388 {
389 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
390 int status;
392 if (sdb_store_expr_eval(e1, obj, &v1, filter))
393 return INT_MAX;
394 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
395 sdb_data_free_datum(&v1);
396 return INT_MAX;
397 }
399 if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
400 status = INT_MAX;
401 else if (v1.type == v2.type)
402 status = sdb_data_cmp(&v1, &v2);
403 else
404 status = sdb_data_strcmp(&v1, &v2);
406 sdb_data_free_datum(&v1);
407 sdb_data_free_datum(&v2);
408 return status;
409 } /* cmp_expr */
411 static int
412 match_cmp_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
413 sdb_store_matcher_t *filter)
414 {
415 int status;
416 assert(m->type == MATCHER_CMP_LT);
417 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
418 return (status != INT_MAX) && (status < 0);
419 } /* match_cmp_lt */
421 static int
422 match_cmp_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
423 sdb_store_matcher_t *filter)
424 {
425 int status;
426 assert(m->type == MATCHER_CMP_LE);
427 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
428 return (status != INT_MAX) && (status <= 0);
429 } /* match_cmp_le */
431 static int
432 match_cmp_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
433 sdb_store_matcher_t *filter)
434 {
435 int status;
436 assert(m->type == MATCHER_CMP_EQ);
437 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
438 return (status != INT_MAX) && (! status);
439 } /* match_cmp_eq */
441 static int
442 match_cmp_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
443 sdb_store_matcher_t *filter)
444 {
445 int status;
446 assert(m->type == MATCHER_CMP_NE);
447 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
448 return (status != INT_MAX) && status;
449 } /* match_cmp_ne */
451 static int
452 match_cmp_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
453 sdb_store_matcher_t *filter)
454 {
455 int status;
456 assert(m->type == MATCHER_CMP_GE);
457 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
458 return (status != INT_MAX) && (status >= 0);
459 } /* match_cmp_ge */
461 static int
462 match_cmp_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
463 sdb_store_matcher_t *filter)
464 {
465 int status;
466 assert(m->type == MATCHER_CMP_GT);
467 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
468 return (status != INT_MAX) && (status > 0);
469 } /* match_cmp_gt */
471 static int
472 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
473 sdb_store_matcher_t *filter)
474 {
475 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
476 int status = 1;
478 assert(m->type == MATCHER_IN);
480 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
481 || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
482 status = 0;
484 if (status)
485 status = sdb_data_inarray(&value, &array);
487 sdb_data_free_datum(&value);
488 sdb_data_free_datum(&array);
489 return status;
490 } /* match_in */
492 static int
493 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
494 sdb_store_matcher_t *filter)
495 {
496 sdb_data_t v = SDB_DATA_INIT;
497 int status = 0;
499 regex_t regex;
500 _Bool free_regex = 0;
502 assert((m->type == MATCHER_REGEX)
503 || (m->type == MATCHER_NREGEX));
505 if (! CMP_M(m)->right->type) {
506 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
507 regex = CMP_M(m)->right->data.data.re.regex;
508 }
509 else {
510 sdb_data_t tmp = SDB_DATA_INIT;
511 char *raw;
513 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
514 return 0;
516 if (tmp.type != SDB_TYPE_STRING) {
517 sdb_data_free_datum(&tmp);
518 return 0;
519 }
521 raw = tmp.data.string;
522 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
523 free(raw);
524 return 0;
525 }
527 regex = tmp.data.re.regex;
528 free_regex = 1;
529 free(tmp.data.re.raw);
530 free(raw);
531 }
533 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
534 || (sdb_data_isnull(&v)))
535 status = 0;
536 else {
537 char value[sdb_data_strlen(&v) + 1];
538 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
539 status = 0;
540 else if (! regexec(®ex, value, 0, NULL, 0))
541 status = 1;
542 }
544 if (free_regex)
545 regfree(®ex);
546 sdb_data_free_datum(&v);
547 if (m->type == MATCHER_NREGEX)
548 return !status;
549 return status;
550 } /* match_regex */
552 static int
553 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
554 sdb_store_matcher_t *filter)
555 {
556 sdb_data_t v = SDB_DATA_INIT;
557 int status;
559 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
561 /* TODO: this might hide real errors;
562 * improve error reporting and propagation */
563 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
564 || sdb_data_isnull(&v))
565 status = 1;
566 else
567 status = 0;
569 sdb_data_free_datum(&v);
570 if (m->type == MATCHER_ISNNULL)
571 return !status;
572 return status;
573 } /* match_isnull */
575 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
576 sdb_store_matcher_t *);
578 /* this array needs to be indexable by the matcher types;
579 * -> update the enum in store-private.h when updating this */
580 static matcher_cb
581 matchers[] = {
582 match_logical,
583 match_logical,
584 match_unary,
585 match_name,
586 match_attr,
587 match_child,
588 match_child,
589 match_child,
590 match_lt,
591 match_le,
592 match_eq,
593 match_ge,
594 match_gt,
595 match_cmp_lt,
596 match_cmp_le,
597 match_cmp_eq,
598 match_cmp_ne,
599 match_cmp_ge,
600 match_cmp_gt,
601 match_in,
602 match_regex,
603 match_regex,
604 match_isnull,
605 match_isnull,
606 };
608 /*
609 * private conditional types
610 */
612 static int
613 attr_cond_init(sdb_object_t *obj, va_list ap)
614 {
615 const char *name = va_arg(ap, const char *);
616 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
618 if (! name)
619 return -1;
621 SDB_STORE_COND(obj)->cmp = attr_cmp;
623 ATTR_C(obj)->name = strdup(name);
624 if (! ATTR_C(obj)->name)
625 return -1;
626 ATTR_C(obj)->expr = expr;
627 sdb_object_ref(SDB_OBJ(expr));
628 return 0;
629 } /* attr_cond_init */
631 static void
632 attr_cond_destroy(sdb_object_t *obj)
633 {
634 if (ATTR_C(obj)->name)
635 free(ATTR_C(obj)->name);
636 sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
637 } /* attr_cond_destroy */
639 static sdb_type_t attr_cond_type = {
640 /* size = */ sizeof(attr_cond_t),
641 /* init = */ attr_cond_init,
642 /* destroy = */ attr_cond_destroy,
643 };
645 static int
646 obj_cond_init(sdb_object_t *obj, va_list ap)
647 {
648 int field = va_arg(ap, int);
649 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
651 SDB_STORE_COND(obj)->cmp = obj_cmp;
653 OBJ_C(obj)->field = field;
654 OBJ_C(obj)->expr = expr;
655 sdb_object_ref(SDB_OBJ(expr));
656 return 0;
657 } /* obj_cond_init */
659 static void
660 obj_cond_destroy(sdb_object_t *obj)
661 {
662 sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr));
663 } /* obj_cond_destroy */
665 static sdb_type_t obj_cond_type = {
666 /* size = */ sizeof(obj_cond_t),
667 /* init = */ obj_cond_init,
668 /* destroy = */ obj_cond_destroy,
669 };
671 /*
672 * private matcher types
673 */
675 /* initializes a string matcher consuming two elements from ap */
676 static int
677 string_matcher_init(string_matcher_t *m, va_list ap)
678 {
679 const char *name = va_arg(ap, const char *);
680 const char *name_re = va_arg(ap, const char *);
682 if (name) {
683 m->name = strdup(name);
684 if (! m->name)
685 return -1;
686 }
687 if (name_re) {
688 m->name_re = malloc(sizeof(*m->name_re));
689 if (! m->name_re)
690 return -1;
691 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
692 return -1;
693 }
694 return 0;
695 } /* string_matcher_init */
697 static void
698 string_matcher_destroy(string_matcher_t *m)
699 {
700 if (m->name)
701 free(m->name);
702 if (m->name_re) {
703 regfree(m->name_re);
704 free(m->name_re);
705 }
706 } /* string_matcher_destroy */
708 /* initializes a name matcher */
709 static int
710 name_matcher_init(sdb_object_t *obj, va_list ap)
711 {
712 name_matcher_t *m = NAME_M(obj);
713 M(obj)->type = MATCHER_NAME;
714 return string_matcher_init(&m->name, ap);
715 } /* name_matcher_init */
717 static void
718 name_matcher_destroy(sdb_object_t *obj)
719 {
720 name_matcher_t *m = NAME_M(obj);
721 string_matcher_destroy(&m->name);
722 } /* name_matcher_destroy */
724 static int
725 attr_matcher_init(sdb_object_t *obj, va_list ap)
726 {
727 attr_matcher_t *attr = ATTR_M(obj);
728 const char *name = va_arg(ap, const char *);
730 M(obj)->type = MATCHER_ATTR;
731 if (name) {
732 attr->name = strdup(name);
733 if (! attr->name)
734 return -1;
735 }
736 return string_matcher_init(&attr->value, ap);
737 } /* attr_matcher_init */
739 static void
740 attr_matcher_destroy(sdb_object_t *obj)
741 {
742 attr_matcher_t *attr = ATTR_M(obj);
743 if (attr->name)
744 free(attr->name);
745 attr->name = NULL;
746 string_matcher_destroy(&attr->value);
747 } /* attr_matcher_destroy */
749 static int
750 cond_matcher_init(sdb_object_t *obj, va_list ap)
751 {
752 int type = va_arg(ap, int);
753 sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
755 if (! cond)
756 return -1;
758 sdb_object_ref(SDB_OBJ(cond));
760 M(obj)->type = type;
761 COND_M(obj)->cond = cond;
762 return 0;
763 } /* cond_matcher_init */
765 static void
766 cond_matcher_destroy(sdb_object_t *obj)
767 {
768 sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
769 } /* cond_matcher_destroy */
771 static int
772 op_matcher_init(sdb_object_t *obj, va_list ap)
773 {
774 M(obj)->type = va_arg(ap, int);
775 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
776 return -1;
778 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
779 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
780 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
781 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
783 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
784 return -1;
785 return 0;
786 } /* op_matcher_init */
788 static void
789 op_matcher_destroy(sdb_object_t *obj)
790 {
791 if (OP_M(obj)->left)
792 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
793 if (OP_M(obj)->right)
794 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
795 } /* op_matcher_destroy */
797 static int
798 child_matcher_init(sdb_object_t *obj, va_list ap)
799 {
800 M(obj)->type = va_arg(ap, int);
801 CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
803 if (! CHILD_M(obj)->m)
804 return -1;
806 sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
807 return 0;
808 } /* child_matcher_init */
810 static void
811 child_matcher_destroy(sdb_object_t *obj)
812 {
813 sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
814 } /* child_matcher_destroy */
816 static int
817 cmp_matcher_init(sdb_object_t *obj, va_list ap)
818 {
819 M(obj)->type = va_arg(ap, int);
821 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
822 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
823 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
824 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
826 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
827 return -1;
828 return 0;
829 } /* cmp_matcher_init */
831 static void
832 cmp_matcher_destroy(sdb_object_t *obj)
833 {
834 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
835 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
836 } /* cmp_matcher_destroy */
838 static int
839 uop_matcher_init(sdb_object_t *obj, va_list ap)
840 {
841 M(obj)->type = va_arg(ap, int);
842 if (M(obj)->type != MATCHER_NOT)
843 return -1;
845 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
846 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
848 if (! UOP_M(obj)->op)
849 return -1;
850 return 0;
851 } /* uop_matcher_init */
853 static void
854 uop_matcher_destroy(sdb_object_t *obj)
855 {
856 if (UOP_M(obj)->op)
857 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
858 } /* uop_matcher_destroy */
860 static int
861 isnull_matcher_init(sdb_object_t *obj, va_list ap)
862 {
863 M(obj)->type = va_arg(ap, int);
864 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
865 return -1;
867 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
868 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
869 return 0;
870 } /* isnull_matcher_init */
872 static void
873 isnull_matcher_destroy(sdb_object_t *obj)
874 {
875 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
876 ISNULL_M(obj)->expr = NULL;
877 } /* isnull_matcher_destroy */
879 static sdb_type_t name_type = {
880 /* size = */ sizeof(name_matcher_t),
881 /* init = */ name_matcher_init,
882 /* destroy = */ name_matcher_destroy,
883 };
885 static sdb_type_t attr_type = {
886 /* size = */ sizeof(attr_matcher_t),
887 /* init = */ attr_matcher_init,
888 /* destroy = */ attr_matcher_destroy,
889 };
891 static sdb_type_t cond_type = {
892 /* size = */ sizeof(cond_matcher_t),
893 /* init = */ cond_matcher_init,
894 /* destroy = */ cond_matcher_destroy,
895 };
897 static sdb_type_t op_type = {
898 /* size = */ sizeof(op_matcher_t),
899 /* init = */ op_matcher_init,
900 /* destroy = */ op_matcher_destroy,
901 };
903 static sdb_type_t uop_type = {
904 /* size = */ sizeof(uop_matcher_t),
905 /* init = */ uop_matcher_init,
906 /* destroy = */ uop_matcher_destroy,
907 };
909 static sdb_type_t child_type = {
910 /* size = */ sizeof(child_matcher_t),
911 /* init = */ child_matcher_init,
912 /* destroy = */ child_matcher_destroy,
913 };
915 static sdb_type_t cmp_type = {
916 /* size = */ sizeof(cmp_matcher_t),
917 /* init = */ cmp_matcher_init,
918 /* destroy = */ cmp_matcher_destroy,
919 };
921 static sdb_type_t isnull_type = {
922 /* size = */ sizeof(isnull_matcher_t),
923 /* init = */ isnull_matcher_init,
924 /* destroy = */ isnull_matcher_destroy,
925 };
927 /*
928 * public API
929 */
931 sdb_store_cond_t *
932 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
933 {
934 return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
935 name, expr));
936 } /* sdb_store_attr_cond */
938 sdb_store_cond_t *
939 sdb_store_obj_cond(int field, sdb_store_expr_t *expr)
940 {
941 return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type,
942 field, expr));
943 } /* sdb_store_obj_cond */
945 sdb_store_matcher_t *
946 sdb_store_name_matcher(int type, const char *name, _Bool re)
947 {
948 sdb_store_matcher_t *m;
950 if (re)
951 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
952 else
953 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
955 if (! m)
956 return NULL;
958 NAME_M(m)->obj_type = type;
959 return m;
960 } /* sdb_store_name_matcher */
962 sdb_store_matcher_t *
963 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
964 {
965 sdb_store_matcher_t *m;
967 if (! name)
968 return NULL;
970 if (re)
971 m = M(sdb_object_create("attr-matcher", attr_type,
972 name, NULL, value));
973 else
974 m = M(sdb_object_create("attr-matcher", attr_type,
975 name, value, NULL));
976 return m;
977 } /* sdb_store_attr_matcher */
979 sdb_store_matcher_t *
980 sdb_store_child_matcher(int type, sdb_store_matcher_t *m)
981 {
982 if (type == SDB_SERVICE)
983 type = MATCHER_SERVICE;
984 else if (type == SDB_METRIC)
985 type = MATCHER_METRIC;
986 else if (type == SDB_ATTRIBUTE)
987 type = MATCHER_ATTRIBUTE;
988 else
989 return NULL;
990 return M(sdb_object_create("any-matcher", child_type, type, m));
991 } /* sdb_store_child_matcher */
993 sdb_store_matcher_t *
994 sdb_store_lt_matcher(sdb_store_cond_t *cond)
995 {
996 return M(sdb_object_create("lt-matcher", cond_type,
997 MATCHER_LT, cond));
998 } /* sdb_store_lt_matcher */
1000 sdb_store_matcher_t *
1001 sdb_store_le_matcher(sdb_store_cond_t *cond)
1002 {
1003 return M(sdb_object_create("le-matcher", cond_type,
1004 MATCHER_LE, cond));
1005 } /* sdb_store_le_matcher */
1007 sdb_store_matcher_t *
1008 sdb_store_eq_matcher(sdb_store_cond_t *cond)
1009 {
1010 return M(sdb_object_create("eq-matcher", cond_type,
1011 MATCHER_EQ, cond));
1012 } /* sdb_store_eq_matcher */
1014 sdb_store_matcher_t *
1015 sdb_store_ge_matcher(sdb_store_cond_t *cond)
1016 {
1017 return M(sdb_object_create("ge-matcher", cond_type,
1018 MATCHER_GE, cond));
1019 } /* sdb_store_ge_matcher */
1021 sdb_store_matcher_t *
1022 sdb_store_gt_matcher(sdb_store_cond_t *cond)
1023 {
1024 return M(sdb_object_create("gt-matcher", cond_type,
1025 MATCHER_GT, cond));
1026 } /* sdb_store_gt_matcher */
1028 /*
1029 * TODO: Rename sdb_store_cmp_* to sdb_store_* once the old code is unused and
1030 * has been removed.
1031 */
1033 sdb_store_matcher_t *
1034 sdb_store_cmp_lt(sdb_store_expr_t *left, sdb_store_expr_t *right)
1035 {
1036 return M(sdb_object_create("lt-matcher", cmp_type,
1037 MATCHER_CMP_LT, left, right));
1038 } /* sdb_store_cmp_lt */
1040 sdb_store_matcher_t *
1041 sdb_store_cmp_le(sdb_store_expr_t *left, sdb_store_expr_t *right)
1042 {
1043 return M(sdb_object_create("le-matcher", cmp_type,
1044 MATCHER_CMP_LE, left, right));
1045 } /* sdb_store_cmp_le */
1047 sdb_store_matcher_t *
1048 sdb_store_cmp_eq(sdb_store_expr_t *left, sdb_store_expr_t *right)
1049 {
1050 return M(sdb_object_create("eq-matcher", cmp_type,
1051 MATCHER_CMP_EQ, left, right));
1052 } /* sdb_store_cmp_eq */
1054 sdb_store_matcher_t *
1055 sdb_store_cmp_ne(sdb_store_expr_t *left, sdb_store_expr_t *right)
1056 {
1057 return M(sdb_object_create("ne-matcher", cmp_type,
1058 MATCHER_CMP_NE, left, right));
1059 } /* sdb_store_cmp_ne */
1061 sdb_store_matcher_t *
1062 sdb_store_cmp_ge(sdb_store_expr_t *left, sdb_store_expr_t *right)
1063 {
1064 return M(sdb_object_create("ge-matcher", cmp_type,
1065 MATCHER_CMP_GE, left, right));
1066 } /* sdb_store_cmp_ge */
1068 sdb_store_matcher_t *
1069 sdb_store_cmp_gt(sdb_store_expr_t *left, sdb_store_expr_t *right)
1070 {
1071 return M(sdb_object_create("gt-matcher", cmp_type,
1072 MATCHER_CMP_GT, left, right));
1073 } /* sdb_store_cmp_gt */
1075 sdb_store_matcher_t *
1076 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1077 {
1078 return M(sdb_object_create("in-matcher", cmp_type,
1079 MATCHER_IN, left, right));
1080 } /* sdb_store_in_matcher */
1082 sdb_store_matcher_t *
1083 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1084 {
1085 if (! right->type) {
1086 if ((right->data.type != SDB_TYPE_STRING)
1087 && (right->data.type != SDB_TYPE_REGEX))
1088 return NULL;
1090 if (right->data.type == SDB_TYPE_STRING) {
1091 char *raw = right->data.data.string;
1092 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
1093 return NULL;
1094 free(raw);
1095 }
1096 }
1097 return M(sdb_object_create("regex-matcher", cmp_type,
1098 MATCHER_REGEX, left, right));
1099 } /* sdb_store_regex_matcher */
1101 sdb_store_matcher_t *
1102 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1103 {
1104 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
1105 if (! m)
1106 return NULL;
1107 m->type = MATCHER_NREGEX;
1108 return m;
1109 } /* sdb_store_nregex_matcher */
1111 sdb_store_matcher_t *
1112 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
1113 {
1114 return M(sdb_object_create("isnull-matcher", isnull_type,
1115 MATCHER_ISNULL, expr));
1116 } /* sdb_store_isnull_matcher */
1118 sdb_store_matcher_t *
1119 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
1120 {
1121 return M(sdb_object_create("isnull-matcher", isnull_type,
1122 MATCHER_ISNNULL, expr));
1123 } /* sdb_store_isnnull_matcher */
1125 sdb_store_matcher_op_cb
1126 sdb_store_parse_matcher_op(const char *op)
1127 {
1128 if (! strcasecmp(op, "<"))
1129 return sdb_store_cmp_lt;
1130 else if (! strcasecmp(op, "<="))
1131 return sdb_store_cmp_le;
1132 else if (! strcasecmp(op, "="))
1133 return sdb_store_cmp_eq;
1134 else if (! strcasecmp(op, "!="))
1135 return sdb_store_cmp_ne;
1136 else if (! strcasecmp(op, ">="))
1137 return sdb_store_cmp_ge;
1138 else if (! strcasecmp(op, ">"))
1139 return sdb_store_cmp_gt;
1140 else if (! strcasecmp(op, "=~"))
1141 return sdb_store_regex_matcher;
1142 else if (! strcasecmp(op, "!~"))
1143 return sdb_store_nregex_matcher;
1144 return NULL;
1145 } /* sdb_store_parse_matcher_op */
1147 int
1148 sdb_store_parse_object_type_plural(const char *name)
1149 {
1150 if (! strcasecmp(name, "hosts"))
1151 return SDB_HOST;
1152 else if (! strcasecmp(name, "services"))
1153 return SDB_SERVICE;
1154 else if (! strcasecmp(name, "metrics"))
1155 return SDB_METRIC;
1156 return -1;
1157 } /* sdb_store_parse_object_type_plural */
1159 int
1160 sdb_store_parse_field_name(const char *name)
1161 {
1162 if (! strcasecmp(name, "name"))
1163 return SDB_FIELD_NAME;
1164 else if (! strcasecmp(name, "last_update"))
1165 return SDB_FIELD_LAST_UPDATE;
1166 else if (! strcasecmp(name, "age"))
1167 return SDB_FIELD_AGE;
1168 else if (! strcasecmp(name, "interval"))
1169 return SDB_FIELD_INTERVAL;
1170 else if (! strcasecmp(name, "backend"))
1171 return SDB_FIELD_BACKEND;
1172 return -1;
1173 } /* sdb_store_parse_field_name */
1175 static sdb_store_matcher_t *
1176 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
1177 {
1178 sdb_store_matcher_t *tmp;
1180 if ((! m) || (! inv))
1181 return m;
1183 tmp = sdb_store_inv_matcher(m);
1184 /* pass ownership to the inverse matcher */
1185 sdb_object_deref(SDB_OBJ(m));
1186 return tmp;
1187 } /* maybe_inv_matcher */
1189 static int
1190 parse_cond_op(const char *op,
1191 sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv)
1192 {
1193 *inv = 0;
1194 if (! strcasecmp(op, "<"))
1195 *matcher = sdb_store_lt_matcher;
1196 else if (! strcasecmp(op, "<="))
1197 *matcher = sdb_store_le_matcher;
1198 else if (! strcasecmp(op, "="))
1199 *matcher = sdb_store_eq_matcher;
1200 else if (! strcasecmp(op, ">="))
1201 *matcher = sdb_store_ge_matcher;
1202 else if (! strcasecmp(op, ">"))
1203 *matcher = sdb_store_gt_matcher;
1204 else if (! strcasecmp(op, "!=")) {
1205 *matcher = sdb_store_eq_matcher;
1206 *inv = 1;
1207 }
1208 else
1209 return -1;
1210 return 0;
1211 } /* parse_cond_op */
1213 static sdb_store_matcher_t *
1214 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
1215 {
1216 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1217 sdb_store_matcher_t *m;
1218 sdb_store_cond_t *cond;
1219 _Bool inv = 0;
1221 if (! attr)
1222 return NULL;
1224 if (! expr)
1225 return NULL;
1226 else if (parse_cond_op(op, &matcher, &inv))
1227 return NULL;
1229 cond = sdb_store_attr_cond(attr, expr);
1230 if (! cond)
1231 return NULL;
1233 m = matcher(cond);
1234 /* pass ownership to 'm' or destroy in case of an error */
1235 sdb_object_deref(SDB_OBJ(cond));
1236 return maybe_inv_matcher(m, inv);
1237 } /* parse_attr_cmp */
1239 sdb_store_matcher_t *
1240 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
1241 const char *op, sdb_store_expr_t *expr)
1242 {
1243 int type = -1;
1244 _Bool inv = 0;
1245 _Bool re = 0;
1247 sdb_data_t value = SDB_DATA_INIT;
1248 sdb_store_matcher_t *m = NULL;
1250 if (! strcasecmp(obj_type, "host"))
1251 type = SDB_HOST;
1252 else if (! strcasecmp(obj_type, "service"))
1253 type = SDB_SERVICE;
1254 else if (! strcasecmp(obj_type, "metric"))
1255 type = SDB_METRIC;
1256 else if (! strcasecmp(obj_type, "attribute"))
1257 type = SDB_ATTRIBUTE;
1258 else
1259 return NULL;
1261 /* XXX: this code sucks! */
1262 if (! strcasecmp(op, "=")) {
1263 /* nothing to do */
1264 }
1265 else if (! strcasecmp(op, "!=")) {
1266 inv = 1;
1267 }
1268 else if (! strcasecmp(op, "=~")) {
1269 re = 1;
1270 }
1271 else if (! strcasecmp(op, "!~")) {
1272 inv = 1;
1273 re = 1;
1274 }
1275 else if (type == SDB_ATTRIBUTE)
1276 return parse_attr_cmp(attr, op, expr);
1277 else
1278 return NULL;
1280 if (! expr)
1281 return NULL;
1283 if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
1284 || (value.type != SDB_TYPE_STRING)) {
1285 sdb_data_free_datum(&value);
1286 if (type != SDB_ATTRIBUTE)
1287 return NULL;
1288 return parse_attr_cmp(attr, op, expr);
1289 }
1291 if (! attr)
1292 m = sdb_store_name_matcher(type, value.data.string, re);
1293 else if (type == SDB_ATTRIBUTE)
1294 m = sdb_store_attr_matcher(attr, value.data.string, re);
1296 sdb_data_free_datum(&value);
1297 return maybe_inv_matcher(m, inv);
1298 } /* sdb_store_matcher_parse_cmp */
1300 sdb_store_matcher_t *
1301 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
1302 sdb_store_expr_t *expr)
1303 {
1304 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1305 sdb_store_matcher_t *m;
1306 sdb_store_cond_t *cond;
1307 _Bool inv = 0;
1309 int field;
1311 if (! expr)
1312 return NULL;
1314 field = sdb_store_parse_field_name(name);
1315 if (field < 0)
1316 return NULL;
1318 if (parse_cond_op(op, &matcher, &inv))
1319 return NULL;
1320 cond = sdb_store_obj_cond(field, expr);
1321 if (! cond)
1322 return NULL;
1324 assert(matcher);
1325 m = matcher(cond);
1326 /* pass ownership to 'm' or destroy in case of an error */
1327 sdb_object_deref(SDB_OBJ(cond));
1328 return maybe_inv_matcher(m, inv);
1329 } /* sdb_store_matcher_parse_field_cmp */
1331 sdb_store_matcher_t *
1332 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1333 {
1334 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
1335 left, right));
1336 } /* sdb_store_dis_matcher */
1338 sdb_store_matcher_t *
1339 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1340 {
1341 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
1342 left, right));
1343 } /* sdb_store_con_matcher */
1345 sdb_store_matcher_t *
1346 sdb_store_inv_matcher(sdb_store_matcher_t *m)
1347 {
1348 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
1349 } /* sdb_store_inv_matcher */
1351 int
1352 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
1353 sdb_store_matcher_t *filter)
1354 {
1355 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
1356 return 0;
1358 /* "NULL" always matches */
1359 if ((! m) || (! obj))
1360 return 1;
1362 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1363 return 0;
1365 return matchers[m->type](m, obj, filter);
1366 } /* sdb_store_matcher_matches */
1368 int
1369 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1370 sdb_store_lookup_cb cb, void *user_data)
1371 {
1372 scan_iter_data_t data = { m, filter, cb, user_data };
1374 if (! cb)
1375 return -1;
1376 return sdb_store_iterate(scan_iter, &data);
1377 } /* sdb_store_scan */
1379 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */