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))
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 = INT_MAX;
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 __attribute__((unused)) *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_get_field(obj, OBJ_C(cond)->field, &obj_value))
140 return INT_MAX;
141 if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value))
142 return INT_MAX;
144 if (obj_value.type != value.type) {
145 sdb_data_free_datum(&value);
146 return INT_MAX;
147 }
148 else if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) {
149 /* this implementation is not actually a conditional but rather checks
150 * for equality (or rather, existence) only */
151 size_t i;
152 status = INT_MAX;
153 for (i = 0; i < obj->backends_num; ++i) {
154 if (! strcasecmp(obj->backends[i], value.data.string)) {
155 status = 0;
156 break;
157 }
158 }
159 }
160 else {
161 status = sdb_data_cmp(&obj_value, &value);
162 }
163 sdb_data_free_datum(&value);
164 return status;
165 } /* obj_cmp */
167 /*
168 * matcher implementations
169 */
171 static int
172 match_string(string_matcher_t *m, const char *name)
173 {
174 if ((! m->name) && (! m->name_re))
175 return 1;
177 if (! name)
178 name = "";
180 if (m->name && strcasecmp(m->name, name))
181 return 0;
182 if (m->name_re && regexec(m->name_re, name,
183 /* matches */ 0, NULL, /* flags = */ 0))
184 return 0;
185 return 1;
186 } /* match_string */
188 static int
189 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
190 sdb_store_matcher_t *filter)
191 {
192 int status;
194 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
195 assert(OP_M(m)->left && OP_M(m)->right);
197 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
199 /* lazy evaluation */
200 if ((! status) && (m->type == MATCHER_AND))
201 return status;
202 else if (status && (m->type == MATCHER_OR))
203 return status;
205 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
206 } /* match_logical */
208 static int
209 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
210 sdb_store_matcher_t *filter)
211 {
212 assert(m->type == MATCHER_NOT);
213 assert(UOP_M(m)->op);
215 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
216 } /* match_unary */
218 static int
219 match_name(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
220 sdb_store_matcher_t *filter)
221 {
222 sdb_avltree_iter_t *iter = NULL;
223 int status = 0;
225 assert(m->type == MATCHER_NAME);
227 if (obj->type == NAME_M(m)->obj_type)
228 return match_string(&NAME_M(m)->name, SDB_OBJ(obj)->name);
229 else if (obj->type != SDB_HOST)
230 return 0;
232 switch (NAME_M(m)->obj_type) {
233 case SDB_SERVICE:
234 iter = sdb_avltree_get_iter(HOST(obj)->services);
235 break;
236 case SDB_METRIC:
237 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
238 break;
239 case SDB_ATTRIBUTE:
240 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
241 break;
242 }
244 while (sdb_avltree_iter_has_next(iter)) {
245 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
246 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
247 NULL)))
248 continue;
249 if (match_string(&NAME_M(m)->name, child->name)) {
250 status = 1;
251 break;
252 }
253 }
254 sdb_avltree_iter_destroy(iter);
255 return status;
256 } /* match_name */
258 static int
259 match_attr(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
260 sdb_store_matcher_t *filter)
261 {
262 sdb_attribute_t *attr;
264 assert(m->type == MATCHER_ATTR);
265 assert(ATTR_M(m)->name);
267 if (obj->type != SDB_HOST)
268 return 0;
270 attr = attr_get(HOST(obj), ATTR_M(m)->name, filter);
271 if (attr) {
272 char buf[sdb_data_strlen(&attr->value) + 1];
273 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
274 return 0;
275 if (match_string(&ATTR_M(m)->value, buf))
276 return 1;
277 }
278 return 0;
279 } /* match_attr */
281 static int
282 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
283 sdb_store_matcher_t *filter)
284 {
285 int status;
286 assert(m->type == MATCHER_LT);
287 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
288 return (status != INT_MAX) && (status < 0);
289 } /* match_lt */
291 static int
292 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
293 sdb_store_matcher_t *filter)
294 {
295 int status;
296 assert(m->type == MATCHER_LE);
297 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
298 return (status != INT_MAX) && (status <= 0);
299 } /* match_le */
301 static int
302 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
303 sdb_store_matcher_t *filter)
304 {
305 int status;
306 assert(m->type == MATCHER_EQ);
307 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
308 return (status != INT_MAX) && (! status);
309 } /* match_eq */
311 static int
312 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
313 sdb_store_matcher_t *filter)
314 {
315 int status;
316 assert(m->type == MATCHER_GE);
317 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
318 return (status != INT_MAX) && (status >= 0);
319 } /* match_ge */
321 static int
322 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
323 sdb_store_matcher_t *filter)
324 {
325 int status;
326 assert(m->type == MATCHER_GT);
327 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
328 return (status != INT_MAX) && (status > 0);
329 } /* match_gt */
331 static int
332 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
333 sdb_store_matcher_t *filter)
334 {
335 assert(m->type == MATCHER_ISNULL);
336 if (obj->type != SDB_HOST)
337 return 0;
338 return attr_get(HOST(obj), ISNULL_M(m)->attr_name, filter) == NULL;
339 } /* match_isnull */
341 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
342 sdb_store_matcher_t *);
344 /* this array needs to be indexable by the matcher types;
345 * -> update the enum in store-private.h when updating this */
346 static matcher_cb
347 matchers[] = {
348 match_logical,
349 match_logical,
350 match_unary,
351 match_name,
352 match_attr,
353 match_lt,
354 match_le,
355 match_eq,
356 match_ge,
357 match_gt,
358 match_isnull,
359 };
361 /*
362 * private conditional types
363 */
365 static int
366 attr_cond_init(sdb_object_t *obj, va_list ap)
367 {
368 const char *name = va_arg(ap, const char *);
369 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
371 if (! name)
372 return -1;
374 SDB_STORE_COND(obj)->cmp = attr_cmp;
376 ATTR_C(obj)->name = strdup(name);
377 if (! ATTR_C(obj)->name)
378 return -1;
379 ATTR_C(obj)->expr = expr;
380 sdb_object_ref(SDB_OBJ(expr));
381 return 0;
382 } /* attr_cond_init */
384 static void
385 attr_cond_destroy(sdb_object_t *obj)
386 {
387 if (ATTR_C(obj)->name)
388 free(ATTR_C(obj)->name);
389 sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
390 } /* attr_cond_destroy */
392 static sdb_type_t attr_cond_type = {
393 /* size = */ sizeof(attr_cond_t),
394 /* init = */ attr_cond_init,
395 /* destroy = */ attr_cond_destroy,
396 };
398 static int
399 obj_cond_init(sdb_object_t *obj, va_list ap)
400 {
401 int field = va_arg(ap, int);
402 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
404 SDB_STORE_COND(obj)->cmp = obj_cmp;
406 OBJ_C(obj)->field = field;
407 OBJ_C(obj)->expr = expr;
408 sdb_object_ref(SDB_OBJ(expr));
409 return 0;
410 } /* obj_cond_init */
412 static void
413 obj_cond_destroy(sdb_object_t *obj)
414 {
415 sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr));
416 } /* obj_cond_destroy */
418 static sdb_type_t obj_cond_type = {
419 /* size = */ sizeof(obj_cond_t),
420 /* init = */ obj_cond_init,
421 /* destroy = */ obj_cond_destroy,
422 };
424 /*
425 * private matcher types
426 */
428 /* initializes a string matcher consuming two elements from ap */
429 static int
430 string_matcher_init(string_matcher_t *m, va_list ap)
431 {
432 const char *name = va_arg(ap, const char *);
433 const char *name_re = va_arg(ap, const char *);
435 if (name) {
436 m->name = strdup(name);
437 if (! m->name)
438 return -1;
439 }
440 if (name_re) {
441 m->name_re = malloc(sizeof(*m->name_re));
442 if (! m->name_re)
443 return -1;
444 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
445 return -1;
446 }
447 return 0;
448 } /* string_matcher_init */
450 static void
451 string_matcher_destroy(string_matcher_t *m)
452 {
453 if (m->name)
454 free(m->name);
455 if (m->name_re) {
456 regfree(m->name_re);
457 free(m->name_re);
458 }
459 } /* string_matcher_destroy */
461 static char *
462 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
463 {
464 snprintf(buf, buflen, "{ %s%s%s, %p }",
465 m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
466 m->name_re);
467 return buf;
468 } /* string_tostring */
470 /* initializes a name matcher */
471 static int
472 name_matcher_init(sdb_object_t *obj, va_list ap)
473 {
474 name_matcher_t *m = NAME_M(obj);
475 M(obj)->type = MATCHER_NAME;
476 return string_matcher_init(&m->name, ap);
477 } /* name_matcher_init */
479 static void
480 name_matcher_destroy(sdb_object_t *obj)
481 {
482 name_matcher_t *m = NAME_M(obj);
483 string_matcher_destroy(&m->name);
484 } /* name_matcher_destroy */
486 static char *
487 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
488 {
489 char name[buflen + 1];
490 assert(m->type == MATCHER_NAME);
491 snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
492 SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
493 string_tostring(&NAME_M(m)->name, name, sizeof(name)));
494 return buf;
495 } /* name_tostring */
497 static int
498 attr_matcher_init(sdb_object_t *obj, va_list ap)
499 {
500 attr_matcher_t *attr = ATTR_M(obj);
501 const char *name = va_arg(ap, const char *);
503 M(obj)->type = MATCHER_ATTR;
504 if (name) {
505 attr->name = strdup(name);
506 if (! attr->name)
507 return -1;
508 }
509 return string_matcher_init(&attr->value, ap);
510 } /* attr_matcher_init */
512 static void
513 attr_matcher_destroy(sdb_object_t *obj)
514 {
515 attr_matcher_t *attr = ATTR_M(obj);
516 if (attr->name)
517 free(attr->name);
518 attr->name = NULL;
519 string_matcher_destroy(&attr->value);
520 } /* attr_matcher_destroy */
522 static char *
523 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
524 {
525 char value[buflen + 1];
527 if (! m) {
528 snprintf(buf, buflen, "ATTR{}");
529 return buf;
530 }
532 assert(m->type == MATCHER_ATTR);
533 snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
534 string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
535 return buf;
536 } /* attr_tostring */
538 static int
539 cond_matcher_init(sdb_object_t *obj, va_list ap)
540 {
541 int type = va_arg(ap, int);
542 sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
544 if (! cond)
545 return -1;
547 sdb_object_ref(SDB_OBJ(cond));
549 M(obj)->type = type;
550 COND_M(obj)->cond = cond;
551 return 0;
552 } /* cond_matcher_init */
554 static void
555 cond_matcher_destroy(sdb_object_t *obj)
556 {
557 sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
558 } /* cond_matcher_destroy */
560 static char *
561 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
562 {
563 const char *type, *id;
564 sdb_data_t value = SDB_DATA_INIT;
565 char value_str[buflen];
566 sdb_store_expr_t *expr;
568 if (COND_M(m)->cond->cmp == attr_cmp) {
569 type = "ATTR";
570 id = ATTR_C(COND_M(m)->cond)->name;
571 expr = ATTR_C(COND_M(m)->cond)->expr;
572 }
573 else if (COND_M(m)->cond->cmp == obj_cmp) {
574 type = "OBJ";
575 id = SDB_FIELD_TO_NAME(OBJ_C(COND_M(m)->cond)->field);
576 expr = OBJ_C(COND_M(m)->cond)->expr;
577 }
578 else {
579 snprintf(buf, buflen, "<unknown>");
580 return buf;
581 }
583 if (sdb_store_expr_eval(expr, NULL, &value))
584 snprintf(value_str, sizeof(value_str), "ERR");
585 else if (sdb_data_format(&value, value_str, sizeof(value_str),
586 SDB_SINGLE_QUOTED) < 0)
587 snprintf(value_str, sizeof(value_str), "ERR");
588 snprintf(buf, buflen, "%s[%s]{ %s %s }", type, id,
589 MATCHER_SYM(m->type), value_str);
590 sdb_data_free_datum(&value);
591 return buf;
592 } /* cond_tostring */
594 static int
595 op_matcher_init(sdb_object_t *obj, va_list ap)
596 {
597 M(obj)->type = va_arg(ap, int);
598 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
599 return -1;
601 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
602 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
603 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
604 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
606 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
607 return -1;
608 return 0;
609 } /* op_matcher_init */
611 static void
612 op_matcher_destroy(sdb_object_t *obj)
613 {
614 if (OP_M(obj)->left)
615 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
616 if (OP_M(obj)->right)
617 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
618 } /* op_matcher_destroy */
620 static char *
621 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
622 {
623 char left[buflen + 1], right[buflen + 1];
625 if (! m) {
626 /* this should not happen */
627 snprintf(buf, buflen, "()");
628 return buf;
629 }
631 assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
632 snprintf(buf, buflen, "(%s, %s, %s)",
633 m->type == MATCHER_OR ? "OR" : "AND",
634 sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
635 sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
636 return buf;
637 } /* op_tostring */
639 static int
640 uop_matcher_init(sdb_object_t *obj, va_list ap)
641 {
642 M(obj)->type = va_arg(ap, int);
643 if (M(obj)->type != MATCHER_NOT)
644 return -1;
646 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
647 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
649 if (! UOP_M(obj)->op)
650 return -1;
651 return 0;
652 } /* uop_matcher_init */
654 static void
655 uop_matcher_destroy(sdb_object_t *obj)
656 {
657 if (UOP_M(obj)->op)
658 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
659 } /* uop_matcher_destroy */
661 static char *
662 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
663 {
664 char op[buflen + 1];
666 if (! m) {
667 /* this should not happen */
668 snprintf(buf, buflen, "()");
669 return buf;
670 }
672 assert(m->type == MATCHER_NOT);
673 snprintf(buf, buflen, "(NOT, %s)",
674 sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
675 return buf;
676 } /* uop_tostring */
678 static int
679 isnull_matcher_init(sdb_object_t *obj, va_list ap)
680 {
681 const char *name;
683 M(obj)->type = va_arg(ap, int);
684 if (M(obj)->type != MATCHER_ISNULL)
685 return -1;
687 name = va_arg(ap, const char *);
688 if (! name)
689 return -1;
690 ISNULL_M(obj)->attr_name = strdup(name);
691 if (! ISNULL_M(obj)->attr_name)
692 return -1;
693 return 0;
694 } /* isnull_matcher_init */
696 static void
697 isnull_matcher_destroy(sdb_object_t *obj)
698 {
699 if (ISNULL_M(obj)->attr_name)
700 free(ISNULL_M(obj)->attr_name);
701 ISNULL_M(obj)->attr_name = NULL;
702 } /* isnull_matcher_destroy */
704 static char *
705 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
706 {
707 snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
708 return buf;
709 } /* isnull_tostring */
711 static sdb_type_t name_type = {
712 /* size = */ sizeof(name_matcher_t),
713 /* init = */ name_matcher_init,
714 /* destroy = */ name_matcher_destroy,
715 };
717 static sdb_type_t attr_type = {
718 /* size = */ sizeof(attr_matcher_t),
719 /* init = */ attr_matcher_init,
720 /* destroy = */ attr_matcher_destroy,
721 };
723 static sdb_type_t cond_type = {
724 /* size = */ sizeof(cond_matcher_t),
725 /* init = */ cond_matcher_init,
726 /* destroy = */ cond_matcher_destroy,
727 };
729 static sdb_type_t op_type = {
730 /* size = */ sizeof(op_matcher_t),
731 /* init = */ op_matcher_init,
732 /* destroy = */ op_matcher_destroy,
733 };
735 static sdb_type_t uop_type = {
736 /* size = */ sizeof(uop_matcher_t),
737 /* init = */ uop_matcher_init,
738 /* destroy = */ uop_matcher_destroy,
739 };
741 static sdb_type_t isnull_type = {
742 /* size = */ sizeof(isnull_matcher_t),
743 /* init = */ isnull_matcher_init,
744 /* destroy = */ isnull_matcher_destroy,
745 };
747 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
749 /* this array needs to be indexable by the matcher types;
750 * -> update the enum in store-private.h when updating this */
751 static matcher_tostring_cb
752 matchers_tostring[] = {
753 op_tostring,
754 op_tostring,
755 uop_tostring,
756 name_tostring,
757 attr_tostring,
758 cond_tostring,
759 cond_tostring,
760 cond_tostring,
761 cond_tostring,
762 cond_tostring,
763 isnull_tostring,
764 };
766 /*
767 * public API
768 */
770 sdb_store_cond_t *
771 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
772 {
773 return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
774 name, expr));
775 } /* sdb_store_attr_cond */
777 sdb_store_cond_t *
778 sdb_store_obj_cond(int field, sdb_store_expr_t *expr)
779 {
780 return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type,
781 field, expr));
782 } /* sdb_store_obj_cond */
784 sdb_store_matcher_t *
785 sdb_store_name_matcher(int type, const char *name, _Bool re)
786 {
787 sdb_store_matcher_t *m;
789 if (re)
790 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
791 else
792 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
794 if (! m)
795 return NULL;
797 NAME_M(m)->obj_type = type;
798 return m;
799 } /* sdb_store_name_matcher */
801 sdb_store_matcher_t *
802 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
803 {
804 sdb_store_matcher_t *m;
806 if (! name)
807 return NULL;
809 if (re)
810 m = M(sdb_object_create("attr-matcher", attr_type,
811 name, NULL, value));
812 else
813 m = M(sdb_object_create("attr-matcher", attr_type,
814 name, value, NULL));
815 return m;
816 } /* sdb_store_attr_matcher */
818 sdb_store_matcher_t *
819 sdb_store_lt_matcher(sdb_store_cond_t *cond)
820 {
821 return M(sdb_object_create("lt-matcher", cond_type,
822 MATCHER_LT, cond));
823 } /* sdb_store_lt_matcher */
825 sdb_store_matcher_t *
826 sdb_store_le_matcher(sdb_store_cond_t *cond)
827 {
828 return M(sdb_object_create("le-matcher", cond_type,
829 MATCHER_LE, cond));
830 } /* sdb_store_le_matcher */
832 sdb_store_matcher_t *
833 sdb_store_eq_matcher(sdb_store_cond_t *cond)
834 {
835 return M(sdb_object_create("eq-matcher", cond_type,
836 MATCHER_EQ, cond));
837 } /* sdb_store_eq_matcher */
839 sdb_store_matcher_t *
840 sdb_store_ge_matcher(sdb_store_cond_t *cond)
841 {
842 return M(sdb_object_create("ge-matcher", cond_type,
843 MATCHER_GE, cond));
844 } /* sdb_store_ge_matcher */
846 sdb_store_matcher_t *
847 sdb_store_gt_matcher(sdb_store_cond_t *cond)
848 {
849 return M(sdb_object_create("gt-matcher", cond_type,
850 MATCHER_GT, cond));
851 } /* sdb_store_gt_matcher */
853 sdb_store_matcher_t *
854 sdb_store_isnull_matcher(const char *attr_name)
855 {
856 return M(sdb_object_create("isnull-matcher", isnull_type,
857 MATCHER_ISNULL, attr_name));
858 } /* sdb_store_isnull_matcher */
860 int
861 sdb_store_parse_field_name(const char *name)
862 {
863 if (! strcasecmp(name, "last_update"))
864 return SDB_FIELD_LAST_UPDATE;
865 else if (! strcasecmp(name, "age"))
866 return SDB_FIELD_AGE;
867 else if (! strcasecmp(name, "interval"))
868 return SDB_FIELD_INTERVAL;
869 else if (! strcasecmp(name, "backend"))
870 return SDB_FIELD_BACKEND;
871 return -1;
872 } /* sdb_store_parse_field_name */
874 static sdb_store_matcher_t *
875 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
876 {
877 sdb_store_matcher_t *tmp;
879 if ((! m) || (! inv))
880 return m;
882 tmp = sdb_store_inv_matcher(m);
883 /* pass ownership to the inverse matcher */
884 sdb_object_deref(SDB_OBJ(m));
885 return tmp;
886 } /* maybe_inv_matcher */
888 static int
889 parse_cond_op(const char *op,
890 sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv)
891 {
892 *inv = 0;
893 if (! strcasecmp(op, "<"))
894 *matcher = sdb_store_lt_matcher;
895 else if (! strcasecmp(op, "<="))
896 *matcher = sdb_store_le_matcher;
897 else if (! strcasecmp(op, "="))
898 *matcher = sdb_store_eq_matcher;
899 else if (! strcasecmp(op, ">="))
900 *matcher = sdb_store_ge_matcher;
901 else if (! strcasecmp(op, ">"))
902 *matcher = sdb_store_gt_matcher;
903 else if (! strcasecmp(op, "!=")) {
904 *matcher = sdb_store_eq_matcher;
905 *inv = 1;
906 }
907 else
908 return -1;
909 return 0;
910 } /* parse_cond_op */
912 static sdb_store_matcher_t *
913 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
914 {
915 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
916 sdb_store_matcher_t *m;
917 sdb_store_cond_t *cond;
918 _Bool inv = 0;
920 if (! attr)
921 return NULL;
923 if (! strcasecmp(op, "IS")) {
924 if (! expr)
925 return sdb_store_isnull_matcher(attr);
926 else
927 return NULL;
928 }
929 else if (! expr)
930 return NULL;
931 else if (parse_cond_op(op, &matcher, &inv))
932 return NULL;
934 cond = sdb_store_attr_cond(attr, expr);
935 if (! cond)
936 return NULL;
938 m = matcher(cond);
939 /* pass ownership to 'm' or destroy in case of an error */
940 sdb_object_deref(SDB_OBJ(cond));
941 return maybe_inv_matcher(m, inv);
942 } /* parse_attr_cmp */
944 sdb_store_matcher_t *
945 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
946 const char *op, sdb_store_expr_t *expr)
947 {
948 int type = -1;
949 _Bool inv = 0;
950 _Bool re = 0;
952 sdb_data_t value = SDB_DATA_INIT;
953 sdb_store_matcher_t *m = NULL;
955 if (! strcasecmp(obj_type, "host"))
956 type = SDB_HOST;
957 else if (! strcasecmp(obj_type, "service"))
958 type = SDB_SERVICE;
959 else if (! strcasecmp(obj_type, "metric"))
960 type = SDB_METRIC;
961 else if (! strcasecmp(obj_type, "attribute"))
962 type = SDB_ATTRIBUTE;
963 else
964 return NULL;
966 /* XXX: this code sucks! */
967 if (! strcasecmp(op, "=")) {
968 /* nothing to do */
969 }
970 else if (! strcasecmp(op, "!=")) {
971 inv = 1;
972 }
973 else if (! strcasecmp(op, "=~")) {
974 re = 1;
975 }
976 else if (! strcasecmp(op, "!~")) {
977 inv = 1;
978 re = 1;
979 }
980 else if (type == SDB_ATTRIBUTE)
981 return parse_attr_cmp(attr, op, expr);
982 else
983 return NULL;
985 if (! expr)
986 return NULL;
988 if (sdb_store_expr_eval(expr, NULL, &value))
989 return NULL;
990 if (value.type != SDB_TYPE_STRING) {
991 sdb_data_free_datum(&value);
992 return parse_attr_cmp(attr, op, expr);
993 }
995 if (! attr)
996 m = sdb_store_name_matcher(type, value.data.string, re);
997 else if (type == SDB_ATTRIBUTE)
998 m = sdb_store_attr_matcher(attr, value.data.string, re);
1000 sdb_data_free_datum(&value);
1001 return maybe_inv_matcher(m, inv);
1002 } /* sdb_store_matcher_parse_cmp */
1004 sdb_store_matcher_t *
1005 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
1006 sdb_store_expr_t *expr)
1007 {
1008 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1009 sdb_store_matcher_t *m;
1010 sdb_store_cond_t *cond;
1011 _Bool inv = 0;
1013 int field;
1015 if (! expr)
1016 return NULL;
1018 field = sdb_store_parse_field_name(name);
1019 if (field < 0)
1020 return NULL;
1022 if (parse_cond_op(op, &matcher, &inv))
1023 return NULL;
1024 cond = sdb_store_obj_cond(field, expr);
1025 if (! cond)
1026 return NULL;
1028 assert(matcher);
1029 m = matcher(cond);
1030 /* pass ownership to 'm' or destroy in case of an error */
1031 sdb_object_deref(SDB_OBJ(cond));
1032 return maybe_inv_matcher(m, inv);
1033 } /* sdb_store_matcher_parse_field_cmp */
1035 sdb_store_matcher_t *
1036 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1037 {
1038 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
1039 left, right));
1040 } /* sdb_store_dis_matcher */
1042 sdb_store_matcher_t *
1043 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1044 {
1045 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
1046 left, right));
1047 } /* sdb_store_con_matcher */
1049 sdb_store_matcher_t *
1050 sdb_store_inv_matcher(sdb_store_matcher_t *m)
1051 {
1052 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
1053 } /* sdb_store_inv_matcher */
1055 int
1056 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
1057 sdb_store_matcher_t *filter)
1058 {
1059 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
1060 return 0;
1062 /* "NULL" always matches */
1063 if ((! m) || (! obj))
1064 return 1;
1066 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1067 return 0;
1069 return matchers[m->type](m, obj, filter);
1070 } /* sdb_store_matcher_matches */
1072 char *
1073 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1074 {
1075 if (! m)
1076 return NULL;
1078 if ((m->type < 0)
1079 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
1080 return NULL;
1081 return matchers_tostring[m->type](m, buf, buflen);
1082 } /* sdb_store_matcher_tostring */
1084 int
1085 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1086 sdb_store_lookup_cb cb, void *user_data)
1087 {
1088 scan_iter_data_t data = { m, filter, cb, user_data };
1090 if (! cb)
1091 return -1;
1092 return sdb_store_iterate(scan_iter, &data);
1093 } /* sdb_store_scan */
1095 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */