274ca027421d1540563c8d4dfdccab15c077384c
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.
383 */
384 static int
385 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
386 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
387 {
388 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
389 int status;
391 if (sdb_store_expr_eval(e1, obj, &v1, filter))
392 return INT_MAX;
393 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
394 sdb_data_free_datum(&v1);
395 return INT_MAX;
396 }
398 if (v1.type == v2.type)
399 status = sdb_data_cmp(&v1, &v2);
400 else
401 status = sdb_data_strcmp(&v1, &v2);
403 sdb_data_free_datum(&v1);
404 sdb_data_free_datum(&v2);
405 return status;
406 } /* cmp_expr */
408 static int
409 match_cmp_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
410 sdb_store_matcher_t *filter)
411 {
412 int status;
413 assert(m->type == MATCHER_CMP_LT);
414 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
415 return (status != INT_MAX) && (status < 0);
416 } /* match_cmp_lt */
418 static int
419 match_cmp_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
420 sdb_store_matcher_t *filter)
421 {
422 int status;
423 assert(m->type == MATCHER_CMP_LE);
424 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
425 return (status != INT_MAX) && (status <= 0);
426 } /* match_cmp_le */
428 static int
429 match_cmp_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
430 sdb_store_matcher_t *filter)
431 {
432 int status;
433 assert(m->type == MATCHER_CMP_EQ);
434 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
435 return (status != INT_MAX) && (! status);
436 } /* match_cmp_eq */
438 static int
439 match_cmp_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
440 sdb_store_matcher_t *filter)
441 {
442 int status;
443 assert(m->type == MATCHER_CMP_NE);
444 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
445 return (status != INT_MAX) && status;
446 } /* match_cmp_ne */
448 static int
449 match_cmp_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
450 sdb_store_matcher_t *filter)
451 {
452 int status;
453 assert(m->type == MATCHER_CMP_GE);
454 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
455 return (status != INT_MAX) && (status >= 0);
456 } /* match_cmp_ge */
458 static int
459 match_cmp_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
460 sdb_store_matcher_t *filter)
461 {
462 int status;
463 assert(m->type == MATCHER_CMP_GT);
464 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
465 return (status != INT_MAX) && (status > 0);
466 } /* match_cmp_gt */
468 static int
469 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
470 sdb_store_matcher_t *filter)
471 {
472 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
473 int status = 1;
475 assert(m->type == MATCHER_IN);
477 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
478 || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
479 status = 0;
481 if (status)
482 status = sdb_data_inarray(&value, &array);
484 sdb_data_free_datum(&value);
485 sdb_data_free_datum(&array);
486 return status;
487 } /* match_in */
489 static int
490 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
491 sdb_store_matcher_t *filter)
492 {
493 sdb_data_t v = SDB_DATA_INIT;
494 int status = 0;
496 regex_t regex;
497 _Bool free_regex = 0;
499 assert((m->type == MATCHER_REGEX)
500 || (m->type == MATCHER_NREGEX));
502 if (! CMP_M(m)->right->type) {
503 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
504 regex = CMP_M(m)->right->data.data.re.regex;
505 }
506 else {
507 sdb_data_t tmp = SDB_DATA_INIT;
508 char *raw;
510 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
511 return 0;
513 if (tmp.type != SDB_TYPE_STRING) {
514 sdb_data_free_datum(&tmp);
515 return 0;
516 }
518 raw = tmp.data.string;
519 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
520 free(raw);
521 return 0;
522 }
524 regex = tmp.data.re.regex;
525 free_regex = 1;
526 free(tmp.data.re.raw);
527 free(raw);
528 }
530 if (sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
531 status = 0;
532 else {
533 char value[sdb_data_strlen(&v) + 1];
534 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
535 status = 0;
536 else if (! regexec(®ex, value, 0, NULL, 0))
537 status = 1;
538 }
540 if (free_regex)
541 regfree(®ex);
542 sdb_data_free_datum(&v);
543 if (m->type == MATCHER_NREGEX)
544 return !status;
545 return status;
546 } /* match_regex */
548 static int
549 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
550 sdb_store_matcher_t *filter)
551 {
552 sdb_data_t v = SDB_DATA_INIT;
553 int status;
555 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
557 /* TODO: this might hide real errors;
558 * improve error reporting and propagation */
559 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
560 || sdb_data_isnull(&v))
561 status = 1;
562 else
563 status = 0;
565 sdb_data_free_datum(&v);
566 if (m->type == MATCHER_ISNNULL)
567 return !status;
568 return status;
569 } /* match_isnull */
571 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
572 sdb_store_matcher_t *);
574 /* this array needs to be indexable by the matcher types;
575 * -> update the enum in store-private.h when updating this */
576 static matcher_cb
577 matchers[] = {
578 match_logical,
579 match_logical,
580 match_unary,
581 match_name,
582 match_attr,
583 match_child,
584 match_child,
585 match_child,
586 match_lt,
587 match_le,
588 match_eq,
589 match_ge,
590 match_gt,
591 match_cmp_lt,
592 match_cmp_le,
593 match_cmp_eq,
594 match_cmp_ne,
595 match_cmp_ge,
596 match_cmp_gt,
597 match_in,
598 match_regex,
599 match_regex,
600 match_isnull,
601 match_isnull,
602 };
604 /*
605 * private conditional types
606 */
608 static int
609 attr_cond_init(sdb_object_t *obj, va_list ap)
610 {
611 const char *name = va_arg(ap, const char *);
612 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
614 if (! name)
615 return -1;
617 SDB_STORE_COND(obj)->cmp = attr_cmp;
619 ATTR_C(obj)->name = strdup(name);
620 if (! ATTR_C(obj)->name)
621 return -1;
622 ATTR_C(obj)->expr = expr;
623 sdb_object_ref(SDB_OBJ(expr));
624 return 0;
625 } /* attr_cond_init */
627 static void
628 attr_cond_destroy(sdb_object_t *obj)
629 {
630 if (ATTR_C(obj)->name)
631 free(ATTR_C(obj)->name);
632 sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
633 } /* attr_cond_destroy */
635 static sdb_type_t attr_cond_type = {
636 /* size = */ sizeof(attr_cond_t),
637 /* init = */ attr_cond_init,
638 /* destroy = */ attr_cond_destroy,
639 };
641 static int
642 obj_cond_init(sdb_object_t *obj, va_list ap)
643 {
644 int field = va_arg(ap, int);
645 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
647 SDB_STORE_COND(obj)->cmp = obj_cmp;
649 OBJ_C(obj)->field = field;
650 OBJ_C(obj)->expr = expr;
651 sdb_object_ref(SDB_OBJ(expr));
652 return 0;
653 } /* obj_cond_init */
655 static void
656 obj_cond_destroy(sdb_object_t *obj)
657 {
658 sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr));
659 } /* obj_cond_destroy */
661 static sdb_type_t obj_cond_type = {
662 /* size = */ sizeof(obj_cond_t),
663 /* init = */ obj_cond_init,
664 /* destroy = */ obj_cond_destroy,
665 };
667 /*
668 * private matcher types
669 */
671 /* initializes a string matcher consuming two elements from ap */
672 static int
673 string_matcher_init(string_matcher_t *m, va_list ap)
674 {
675 const char *name = va_arg(ap, const char *);
676 const char *name_re = va_arg(ap, const char *);
678 if (name) {
679 m->name = strdup(name);
680 if (! m->name)
681 return -1;
682 }
683 if (name_re) {
684 m->name_re = malloc(sizeof(*m->name_re));
685 if (! m->name_re)
686 return -1;
687 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
688 return -1;
689 }
690 return 0;
691 } /* string_matcher_init */
693 static void
694 string_matcher_destroy(string_matcher_t *m)
695 {
696 if (m->name)
697 free(m->name);
698 if (m->name_re) {
699 regfree(m->name_re);
700 free(m->name_re);
701 }
702 } /* string_matcher_destroy */
704 static char *
705 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
706 {
707 snprintf(buf, buflen, "{ %s%s%s, %p }",
708 m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
709 m->name_re);
710 return buf;
711 } /* string_tostring */
713 /* initializes a name matcher */
714 static int
715 name_matcher_init(sdb_object_t *obj, va_list ap)
716 {
717 name_matcher_t *m = NAME_M(obj);
718 M(obj)->type = MATCHER_NAME;
719 return string_matcher_init(&m->name, ap);
720 } /* name_matcher_init */
722 static void
723 name_matcher_destroy(sdb_object_t *obj)
724 {
725 name_matcher_t *m = NAME_M(obj);
726 string_matcher_destroy(&m->name);
727 } /* name_matcher_destroy */
729 static char *
730 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
731 {
732 char name[buflen + 1];
733 assert(m->type == MATCHER_NAME);
734 snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
735 SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
736 string_tostring(&NAME_M(m)->name, name, sizeof(name)));
737 return buf;
738 } /* name_tostring */
740 static int
741 attr_matcher_init(sdb_object_t *obj, va_list ap)
742 {
743 attr_matcher_t *attr = ATTR_M(obj);
744 const char *name = va_arg(ap, const char *);
746 M(obj)->type = MATCHER_ATTR;
747 if (name) {
748 attr->name = strdup(name);
749 if (! attr->name)
750 return -1;
751 }
752 return string_matcher_init(&attr->value, ap);
753 } /* attr_matcher_init */
755 static void
756 attr_matcher_destroy(sdb_object_t *obj)
757 {
758 attr_matcher_t *attr = ATTR_M(obj);
759 if (attr->name)
760 free(attr->name);
761 attr->name = NULL;
762 string_matcher_destroy(&attr->value);
763 } /* attr_matcher_destroy */
765 static char *
766 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
767 {
768 char value[buflen + 1];
770 if (! m) {
771 snprintf(buf, buflen, "ATTR{}");
772 return buf;
773 }
775 assert(m->type == MATCHER_ATTR);
776 snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
777 string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
778 return buf;
779 } /* attr_tostring */
781 static int
782 cond_matcher_init(sdb_object_t *obj, va_list ap)
783 {
784 int type = va_arg(ap, int);
785 sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
787 if (! cond)
788 return -1;
790 sdb_object_ref(SDB_OBJ(cond));
792 M(obj)->type = type;
793 COND_M(obj)->cond = cond;
794 return 0;
795 } /* cond_matcher_init */
797 static void
798 cond_matcher_destroy(sdb_object_t *obj)
799 {
800 sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
801 } /* cond_matcher_destroy */
803 static char *
804 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
805 {
806 const char *type, *id;
807 sdb_data_t value = SDB_DATA_INIT;
808 char value_str[buflen];
809 sdb_store_expr_t *expr;
811 if (COND_M(m)->cond->cmp == attr_cmp) {
812 type = "ATTR";
813 id = ATTR_C(COND_M(m)->cond)->name;
814 expr = ATTR_C(COND_M(m)->cond)->expr;
815 }
816 else if (COND_M(m)->cond->cmp == obj_cmp) {
817 type = "OBJ";
818 id = SDB_FIELD_TO_NAME(OBJ_C(COND_M(m)->cond)->field);
819 expr = OBJ_C(COND_M(m)->cond)->expr;
820 }
821 else {
822 snprintf(buf, buflen, "<unknown>");
823 return buf;
824 }
826 if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL))
827 snprintf(value_str, sizeof(value_str), "ERR");
828 else if (sdb_data_format(&value, value_str, sizeof(value_str),
829 SDB_SINGLE_QUOTED) < 0)
830 snprintf(value_str, sizeof(value_str), "ERR");
831 snprintf(buf, buflen, "%s[%s]{ %s %s }", type, id,
832 MATCHER_SYM(m->type), value_str);
833 sdb_data_free_datum(&value);
834 return buf;
835 } /* cond_tostring */
837 static int
838 op_matcher_init(sdb_object_t *obj, va_list ap)
839 {
840 M(obj)->type = va_arg(ap, int);
841 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
842 return -1;
844 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
845 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
846 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
847 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
849 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
850 return -1;
851 return 0;
852 } /* op_matcher_init */
854 static void
855 op_matcher_destroy(sdb_object_t *obj)
856 {
857 if (OP_M(obj)->left)
858 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
859 if (OP_M(obj)->right)
860 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
861 } /* op_matcher_destroy */
863 static char *
864 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
865 {
866 char left[buflen + 1], right[buflen + 1];
868 if (! m) {
869 /* this should not happen */
870 snprintf(buf, buflen, "()");
871 return buf;
872 }
874 assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
875 snprintf(buf, buflen, "(%s, %s, %s)",
876 m->type == MATCHER_OR ? "OR" : "AND",
877 sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
878 sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
879 return buf;
880 } /* op_tostring */
882 static int
883 child_matcher_init(sdb_object_t *obj, va_list ap)
884 {
885 M(obj)->type = va_arg(ap, int);
886 CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
888 if (! CHILD_M(obj)->m)
889 return -1;
891 sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
892 return 0;
893 } /* child_matcher_init */
895 static void
896 child_matcher_destroy(sdb_object_t *obj)
897 {
898 sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
899 } /* child_matcher_destroy */
901 static char *
902 child_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
903 {
904 snprintf(buf, buflen, "%s:", MATCHER_SYM(m->type));
905 buf[buflen - 1] = '\0';
906 sdb_store_matcher_tostring(CHILD_M(m)->m,
907 buf + strlen(buf), buflen - strlen(buf));
908 return buf;
909 } /* child_tostring */
911 static int
912 cmp_matcher_init(sdb_object_t *obj, va_list ap)
913 {
914 M(obj)->type = va_arg(ap, int);
916 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
917 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
918 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
919 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
921 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
922 return -1;
923 return 0;
924 } /* cmp_matcher_init */
926 static void
927 cmp_matcher_destroy(sdb_object_t *obj)
928 {
929 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
930 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
931 } /* cmp_matcher_destroy */
933 static char *
934 cmp_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
935 {
936 if (! m) {
937 /* this should not happen */
938 snprintf(buf, buflen, "()");
939 return buf;
940 }
942 /* TODO */
943 snprintf(buf, buflen, "CMP_MATCHER(%d)", m->type);
944 return buf;
945 } /* cmp_tostring */
947 static int
948 uop_matcher_init(sdb_object_t *obj, va_list ap)
949 {
950 M(obj)->type = va_arg(ap, int);
951 if (M(obj)->type != MATCHER_NOT)
952 return -1;
954 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
955 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
957 if (! UOP_M(obj)->op)
958 return -1;
959 return 0;
960 } /* uop_matcher_init */
962 static void
963 uop_matcher_destroy(sdb_object_t *obj)
964 {
965 if (UOP_M(obj)->op)
966 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
967 } /* uop_matcher_destroy */
969 static char *
970 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
971 {
972 char op[buflen + 1];
974 if (! m) {
975 /* this should not happen */
976 snprintf(buf, buflen, "()");
977 return buf;
978 }
980 assert(m->type == MATCHER_NOT);
981 snprintf(buf, buflen, "(NOT, %s)",
982 sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
983 return buf;
984 } /* uop_tostring */
986 static int
987 isnull_matcher_init(sdb_object_t *obj, va_list ap)
988 {
989 M(obj)->type = va_arg(ap, int);
990 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
991 return -1;
993 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
994 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
995 return 0;
996 } /* isnull_matcher_init */
998 static void
999 isnull_matcher_destroy(sdb_object_t *obj)
1000 {
1001 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
1002 ISNULL_M(obj)->expr = NULL;
1003 } /* isnull_matcher_destroy */
1005 static char *
1006 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1007 {
1008 /* XXX */
1009 if (m->type == MATCHER_ISNULL)
1010 strncpy(buf, "(IS NULL)", buflen);
1011 else
1012 strncpy(buf, "(IS NOT NULL)", buflen);
1013 return buf;
1014 } /* isnull_tostring */
1016 static sdb_type_t name_type = {
1017 /* size = */ sizeof(name_matcher_t),
1018 /* init = */ name_matcher_init,
1019 /* destroy = */ name_matcher_destroy,
1020 };
1022 static sdb_type_t attr_type = {
1023 /* size = */ sizeof(attr_matcher_t),
1024 /* init = */ attr_matcher_init,
1025 /* destroy = */ attr_matcher_destroy,
1026 };
1028 static sdb_type_t cond_type = {
1029 /* size = */ sizeof(cond_matcher_t),
1030 /* init = */ cond_matcher_init,
1031 /* destroy = */ cond_matcher_destroy,
1032 };
1034 static sdb_type_t op_type = {
1035 /* size = */ sizeof(op_matcher_t),
1036 /* init = */ op_matcher_init,
1037 /* destroy = */ op_matcher_destroy,
1038 };
1040 static sdb_type_t uop_type = {
1041 /* size = */ sizeof(uop_matcher_t),
1042 /* init = */ uop_matcher_init,
1043 /* destroy = */ uop_matcher_destroy,
1044 };
1046 static sdb_type_t child_type = {
1047 /* size = */ sizeof(child_matcher_t),
1048 /* init = */ child_matcher_init,
1049 /* destroy = */ child_matcher_destroy,
1050 };
1052 static sdb_type_t cmp_type = {
1053 /* size = */ sizeof(cmp_matcher_t),
1054 /* init = */ cmp_matcher_init,
1055 /* destroy = */ cmp_matcher_destroy,
1056 };
1058 static sdb_type_t isnull_type = {
1059 /* size = */ sizeof(isnull_matcher_t),
1060 /* init = */ isnull_matcher_init,
1061 /* destroy = */ isnull_matcher_destroy,
1062 };
1064 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
1066 /* this array needs to be indexable by the matcher types;
1067 * -> update the enum in store-private.h when updating this */
1068 static matcher_tostring_cb
1069 matchers_tostring[] = {
1070 op_tostring,
1071 op_tostring,
1072 uop_tostring,
1073 name_tostring,
1074 attr_tostring,
1075 child_tostring,
1076 child_tostring,
1077 child_tostring,
1078 cond_tostring,
1079 cond_tostring,
1080 cond_tostring,
1081 cond_tostring,
1082 cond_tostring,
1083 cmp_tostring,
1084 cmp_tostring,
1085 cmp_tostring,
1086 cmp_tostring,
1087 cmp_tostring,
1088 cmp_tostring,
1089 cmp_tostring,
1090 cmp_tostring,
1091 cmp_tostring,
1092 isnull_tostring,
1093 isnull_tostring,
1094 };
1096 /*
1097 * public API
1098 */
1100 sdb_store_cond_t *
1101 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
1102 {
1103 return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
1104 name, expr));
1105 } /* sdb_store_attr_cond */
1107 sdb_store_cond_t *
1108 sdb_store_obj_cond(int field, sdb_store_expr_t *expr)
1109 {
1110 return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type,
1111 field, expr));
1112 } /* sdb_store_obj_cond */
1114 sdb_store_matcher_t *
1115 sdb_store_name_matcher(int type, const char *name, _Bool re)
1116 {
1117 sdb_store_matcher_t *m;
1119 if (re)
1120 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
1121 else
1122 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
1124 if (! m)
1125 return NULL;
1127 NAME_M(m)->obj_type = type;
1128 return m;
1129 } /* sdb_store_name_matcher */
1131 sdb_store_matcher_t *
1132 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
1133 {
1134 sdb_store_matcher_t *m;
1136 if (! name)
1137 return NULL;
1139 if (re)
1140 m = M(sdb_object_create("attr-matcher", attr_type,
1141 name, NULL, value));
1142 else
1143 m = M(sdb_object_create("attr-matcher", attr_type,
1144 name, value, NULL));
1145 return m;
1146 } /* sdb_store_attr_matcher */
1148 sdb_store_matcher_t *
1149 sdb_store_child_matcher(int type, sdb_store_matcher_t *m)
1150 {
1151 if (type == SDB_SERVICE)
1152 type = MATCHER_SERVICE;
1153 else if (type == SDB_METRIC)
1154 type = MATCHER_METRIC;
1155 else if (type == SDB_ATTRIBUTE)
1156 type = MATCHER_ATTRIBUTE;
1157 else
1158 return NULL;
1159 return M(sdb_object_create("any-matcher", child_type, type, m));
1160 } /* sdb_store_child_matcher */
1162 sdb_store_matcher_t *
1163 sdb_store_lt_matcher(sdb_store_cond_t *cond)
1164 {
1165 return M(sdb_object_create("lt-matcher", cond_type,
1166 MATCHER_LT, cond));
1167 } /* sdb_store_lt_matcher */
1169 sdb_store_matcher_t *
1170 sdb_store_le_matcher(sdb_store_cond_t *cond)
1171 {
1172 return M(sdb_object_create("le-matcher", cond_type,
1173 MATCHER_LE, cond));
1174 } /* sdb_store_le_matcher */
1176 sdb_store_matcher_t *
1177 sdb_store_eq_matcher(sdb_store_cond_t *cond)
1178 {
1179 return M(sdb_object_create("eq-matcher", cond_type,
1180 MATCHER_EQ, cond));
1181 } /* sdb_store_eq_matcher */
1183 sdb_store_matcher_t *
1184 sdb_store_ge_matcher(sdb_store_cond_t *cond)
1185 {
1186 return M(sdb_object_create("ge-matcher", cond_type,
1187 MATCHER_GE, cond));
1188 } /* sdb_store_ge_matcher */
1190 sdb_store_matcher_t *
1191 sdb_store_gt_matcher(sdb_store_cond_t *cond)
1192 {
1193 return M(sdb_object_create("gt-matcher", cond_type,
1194 MATCHER_GT, cond));
1195 } /* sdb_store_gt_matcher */
1197 /*
1198 * TODO: Rename sdb_store_cmp_* to sdb_store_* once the old code is unused and
1199 * has been removed.
1200 */
1202 sdb_store_matcher_t *
1203 sdb_store_cmp_lt(sdb_store_expr_t *left, sdb_store_expr_t *right)
1204 {
1205 return M(sdb_object_create("lt-matcher", cmp_type,
1206 MATCHER_CMP_LT, left, right));
1207 } /* sdb_store_cmp_lt */
1209 sdb_store_matcher_t *
1210 sdb_store_cmp_le(sdb_store_expr_t *left, sdb_store_expr_t *right)
1211 {
1212 return M(sdb_object_create("le-matcher", cmp_type,
1213 MATCHER_CMP_LE, left, right));
1214 } /* sdb_store_cmp_le */
1216 sdb_store_matcher_t *
1217 sdb_store_cmp_eq(sdb_store_expr_t *left, sdb_store_expr_t *right)
1218 {
1219 return M(sdb_object_create("eq-matcher", cmp_type,
1220 MATCHER_CMP_EQ, left, right));
1221 } /* sdb_store_cmp_eq */
1223 sdb_store_matcher_t *
1224 sdb_store_cmp_ne(sdb_store_expr_t *left, sdb_store_expr_t *right)
1225 {
1226 return M(sdb_object_create("ne-matcher", cmp_type,
1227 MATCHER_CMP_NE, left, right));
1228 } /* sdb_store_cmp_ne */
1230 sdb_store_matcher_t *
1231 sdb_store_cmp_ge(sdb_store_expr_t *left, sdb_store_expr_t *right)
1232 {
1233 return M(sdb_object_create("ge-matcher", cmp_type,
1234 MATCHER_CMP_GE, left, right));
1235 } /* sdb_store_cmp_ge */
1237 sdb_store_matcher_t *
1238 sdb_store_cmp_gt(sdb_store_expr_t *left, sdb_store_expr_t *right)
1239 {
1240 return M(sdb_object_create("gt-matcher", cmp_type,
1241 MATCHER_CMP_GT, left, right));
1242 } /* sdb_store_cmp_gt */
1244 sdb_store_matcher_t *
1245 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1246 {
1247 return M(sdb_object_create("in-matcher", cmp_type,
1248 MATCHER_IN, left, right));
1249 } /* sdb_store_in_matcher */
1251 sdb_store_matcher_t *
1252 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1253 {
1254 if (! right->type) {
1255 if ((right->data.type != SDB_TYPE_STRING)
1256 && (right->data.type != SDB_TYPE_REGEX))
1257 return NULL;
1259 if (right->data.type == SDB_TYPE_STRING) {
1260 char *raw = right->data.data.string;
1261 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
1262 return NULL;
1263 free(raw);
1264 }
1265 }
1266 return M(sdb_object_create("regex-matcher", cmp_type,
1267 MATCHER_REGEX, left, right));
1268 } /* sdb_store_regex_matcher */
1270 sdb_store_matcher_t *
1271 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1272 {
1273 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
1274 if (! m)
1275 return NULL;
1276 m->type = MATCHER_NREGEX;
1277 return m;
1278 } /* sdb_store_nregex_matcher */
1280 sdb_store_matcher_t *
1281 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
1282 {
1283 return M(sdb_object_create("isnull-matcher", isnull_type,
1284 MATCHER_ISNULL, expr));
1285 } /* sdb_store_isnull_matcher */
1287 sdb_store_matcher_t *
1288 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
1289 {
1290 return M(sdb_object_create("isnull-matcher", isnull_type,
1291 MATCHER_ISNNULL, expr));
1292 } /* sdb_store_isnnull_matcher */
1294 sdb_store_matcher_op_cb
1295 sdb_store_parse_matcher_op(const char *op)
1296 {
1297 if (! strcasecmp(op, "<"))
1298 return sdb_store_cmp_lt;
1299 else if (! strcasecmp(op, "<="))
1300 return sdb_store_cmp_le;
1301 else if (! strcasecmp(op, "="))
1302 return sdb_store_cmp_eq;
1303 else if (! strcasecmp(op, "!="))
1304 return sdb_store_cmp_ne;
1305 else if (! strcasecmp(op, ">="))
1306 return sdb_store_cmp_ge;
1307 else if (! strcasecmp(op, ">"))
1308 return sdb_store_cmp_gt;
1309 else if (! strcasecmp(op, "=~"))
1310 return sdb_store_regex_matcher;
1311 else if (! strcasecmp(op, "!~"))
1312 return sdb_store_nregex_matcher;
1313 return NULL;
1314 } /* sdb_store_parse_matcher_op */
1316 int
1317 sdb_store_parse_object_type_plural(const char *name)
1318 {
1319 if (! strcasecmp(name, "hosts"))
1320 return SDB_HOST;
1321 else if (! strcasecmp(name, "services"))
1322 return SDB_SERVICE;
1323 else if (! strcasecmp(name, "metrics"))
1324 return SDB_METRIC;
1325 return -1;
1326 } /* sdb_store_parse_object_type_plural */
1328 int
1329 sdb_store_parse_field_name(const char *name)
1330 {
1331 if (! strcasecmp(name, "name"))
1332 return SDB_FIELD_NAME;
1333 else if (! strcasecmp(name, "last_update"))
1334 return SDB_FIELD_LAST_UPDATE;
1335 else if (! strcasecmp(name, "age"))
1336 return SDB_FIELD_AGE;
1337 else if (! strcasecmp(name, "interval"))
1338 return SDB_FIELD_INTERVAL;
1339 else if (! strcasecmp(name, "backend"))
1340 return SDB_FIELD_BACKEND;
1341 return -1;
1342 } /* sdb_store_parse_field_name */
1344 static sdb_store_matcher_t *
1345 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
1346 {
1347 sdb_store_matcher_t *tmp;
1349 if ((! m) || (! inv))
1350 return m;
1352 tmp = sdb_store_inv_matcher(m);
1353 /* pass ownership to the inverse matcher */
1354 sdb_object_deref(SDB_OBJ(m));
1355 return tmp;
1356 } /* maybe_inv_matcher */
1358 static int
1359 parse_cond_op(const char *op,
1360 sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv)
1361 {
1362 *inv = 0;
1363 if (! strcasecmp(op, "<"))
1364 *matcher = sdb_store_lt_matcher;
1365 else if (! strcasecmp(op, "<="))
1366 *matcher = sdb_store_le_matcher;
1367 else if (! strcasecmp(op, "="))
1368 *matcher = sdb_store_eq_matcher;
1369 else if (! strcasecmp(op, ">="))
1370 *matcher = sdb_store_ge_matcher;
1371 else if (! strcasecmp(op, ">"))
1372 *matcher = sdb_store_gt_matcher;
1373 else if (! strcasecmp(op, "!=")) {
1374 *matcher = sdb_store_eq_matcher;
1375 *inv = 1;
1376 }
1377 else
1378 return -1;
1379 return 0;
1380 } /* parse_cond_op */
1382 static sdb_store_matcher_t *
1383 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
1384 {
1385 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1386 sdb_store_matcher_t *m;
1387 sdb_store_cond_t *cond;
1388 _Bool inv = 0;
1390 if (! attr)
1391 return NULL;
1393 if (! expr)
1394 return NULL;
1395 else if (parse_cond_op(op, &matcher, &inv))
1396 return NULL;
1398 cond = sdb_store_attr_cond(attr, expr);
1399 if (! cond)
1400 return NULL;
1402 m = matcher(cond);
1403 /* pass ownership to 'm' or destroy in case of an error */
1404 sdb_object_deref(SDB_OBJ(cond));
1405 return maybe_inv_matcher(m, inv);
1406 } /* parse_attr_cmp */
1408 sdb_store_matcher_t *
1409 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
1410 const char *op, sdb_store_expr_t *expr)
1411 {
1412 int type = -1;
1413 _Bool inv = 0;
1414 _Bool re = 0;
1416 sdb_data_t value = SDB_DATA_INIT;
1417 sdb_store_matcher_t *m = NULL;
1419 if (! strcasecmp(obj_type, "host"))
1420 type = SDB_HOST;
1421 else if (! strcasecmp(obj_type, "service"))
1422 type = SDB_SERVICE;
1423 else if (! strcasecmp(obj_type, "metric"))
1424 type = SDB_METRIC;
1425 else if (! strcasecmp(obj_type, "attribute"))
1426 type = SDB_ATTRIBUTE;
1427 else
1428 return NULL;
1430 /* XXX: this code sucks! */
1431 if (! strcasecmp(op, "=")) {
1432 /* nothing to do */
1433 }
1434 else if (! strcasecmp(op, "!=")) {
1435 inv = 1;
1436 }
1437 else if (! strcasecmp(op, "=~")) {
1438 re = 1;
1439 }
1440 else if (! strcasecmp(op, "!~")) {
1441 inv = 1;
1442 re = 1;
1443 }
1444 else if (type == SDB_ATTRIBUTE)
1445 return parse_attr_cmp(attr, op, expr);
1446 else
1447 return NULL;
1449 if (! expr)
1450 return NULL;
1452 if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
1453 || (value.type != SDB_TYPE_STRING)) {
1454 sdb_data_free_datum(&value);
1455 if (type != SDB_ATTRIBUTE)
1456 return NULL;
1457 return parse_attr_cmp(attr, op, expr);
1458 }
1460 if (! attr)
1461 m = sdb_store_name_matcher(type, value.data.string, re);
1462 else if (type == SDB_ATTRIBUTE)
1463 m = sdb_store_attr_matcher(attr, value.data.string, re);
1465 sdb_data_free_datum(&value);
1466 return maybe_inv_matcher(m, inv);
1467 } /* sdb_store_matcher_parse_cmp */
1469 sdb_store_matcher_t *
1470 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
1471 sdb_store_expr_t *expr)
1472 {
1473 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1474 sdb_store_matcher_t *m;
1475 sdb_store_cond_t *cond;
1476 _Bool inv = 0;
1478 int field;
1480 if (! expr)
1481 return NULL;
1483 field = sdb_store_parse_field_name(name);
1484 if (field < 0)
1485 return NULL;
1487 if (parse_cond_op(op, &matcher, &inv))
1488 return NULL;
1489 cond = sdb_store_obj_cond(field, expr);
1490 if (! cond)
1491 return NULL;
1493 assert(matcher);
1494 m = matcher(cond);
1495 /* pass ownership to 'm' or destroy in case of an error */
1496 sdb_object_deref(SDB_OBJ(cond));
1497 return maybe_inv_matcher(m, inv);
1498 } /* sdb_store_matcher_parse_field_cmp */
1500 sdb_store_matcher_t *
1501 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1502 {
1503 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
1504 left, right));
1505 } /* sdb_store_dis_matcher */
1507 sdb_store_matcher_t *
1508 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1509 {
1510 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
1511 left, right));
1512 } /* sdb_store_con_matcher */
1514 sdb_store_matcher_t *
1515 sdb_store_inv_matcher(sdb_store_matcher_t *m)
1516 {
1517 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
1518 } /* sdb_store_inv_matcher */
1520 int
1521 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
1522 sdb_store_matcher_t *filter)
1523 {
1524 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
1525 return 0;
1527 /* "NULL" always matches */
1528 if ((! m) || (! obj))
1529 return 1;
1531 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1532 return 0;
1534 return matchers[m->type](m, obj, filter);
1535 } /* sdb_store_matcher_matches */
1537 char *
1538 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1539 {
1540 if (! m)
1541 return NULL;
1543 if ((m->type < 0)
1544 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
1545 return NULL;
1546 return matchers_tostring[m->type](m, buf, buflen);
1547 } /* sdb_store_matcher_tostring */
1549 int
1550 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1551 sdb_store_lookup_cb cb, void *user_data)
1552 {
1553 scan_iter_data_t data = { m, filter, cb, user_data };
1555 if (! cb)
1556 return -1;
1557 return sdb_store_iterate(scan_iter, &data);
1558 } /* sdb_store_scan */
1560 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */