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 = 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 __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_expr_eval(OBJ_C(cond)->expr, obj, &value))
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_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
291 sdb_store_matcher_t *filter)
292 {
293 int status;
294 assert(m->type == MATCHER_LT);
295 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
296 return (status != INT_MAX) && (status < 0);
297 } /* match_lt */
299 static int
300 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
301 sdb_store_matcher_t *filter)
302 {
303 int status;
304 assert(m->type == MATCHER_LE);
305 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
306 return (status != INT_MAX) && (status <= 0);
307 } /* match_le */
309 static int
310 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
311 sdb_store_matcher_t *filter)
312 {
313 int status;
314 assert(m->type == MATCHER_EQ);
315 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
316 return (status != INT_MAX) && (! status);
317 } /* match_eq */
319 static int
320 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
321 sdb_store_matcher_t *filter)
322 {
323 int status;
324 assert(m->type == MATCHER_GE);
325 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
326 return (status != INT_MAX) && (status >= 0);
327 } /* match_ge */
329 static int
330 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
331 sdb_store_matcher_t *filter)
332 {
333 int status;
334 assert(m->type == MATCHER_GT);
335 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
336 return (status != INT_MAX) && (status > 0);
337 } /* match_gt */
339 static int
340 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
341 sdb_store_matcher_t *filter)
342 {
343 assert(m->type == MATCHER_ISNULL);
344 if (obj->type != SDB_HOST)
345 return 0;
346 return attr_get(HOST(obj), ISNULL_M(m)->attr_name, filter) == NULL;
347 } /* match_isnull */
349 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
350 sdb_store_matcher_t *);
352 /* this array needs to be indexable by the matcher types;
353 * -> update the enum in store-private.h when updating this */
354 static matcher_cb
355 matchers[] = {
356 match_logical,
357 match_logical,
358 match_unary,
359 match_name,
360 match_attr,
361 match_lt,
362 match_le,
363 match_eq,
364 match_ge,
365 match_gt,
366 match_isnull,
367 };
369 /*
370 * private conditional types
371 */
373 static int
374 attr_cond_init(sdb_object_t *obj, va_list ap)
375 {
376 const char *name = va_arg(ap, const char *);
377 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
379 if (! name)
380 return -1;
382 SDB_STORE_COND(obj)->cmp = attr_cmp;
384 ATTR_C(obj)->name = strdup(name);
385 if (! ATTR_C(obj)->name)
386 return -1;
387 ATTR_C(obj)->expr = expr;
388 sdb_object_ref(SDB_OBJ(expr));
389 return 0;
390 } /* attr_cond_init */
392 static void
393 attr_cond_destroy(sdb_object_t *obj)
394 {
395 if (ATTR_C(obj)->name)
396 free(ATTR_C(obj)->name);
397 sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
398 } /* attr_cond_destroy */
400 static sdb_type_t attr_cond_type = {
401 /* size = */ sizeof(attr_cond_t),
402 /* init = */ attr_cond_init,
403 /* destroy = */ attr_cond_destroy,
404 };
406 static int
407 obj_cond_init(sdb_object_t *obj, va_list ap)
408 {
409 int field = va_arg(ap, int);
410 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
412 SDB_STORE_COND(obj)->cmp = obj_cmp;
414 OBJ_C(obj)->field = field;
415 OBJ_C(obj)->expr = expr;
416 sdb_object_ref(SDB_OBJ(expr));
417 return 0;
418 } /* obj_cond_init */
420 static void
421 obj_cond_destroy(sdb_object_t *obj)
422 {
423 sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr));
424 } /* obj_cond_destroy */
426 static sdb_type_t obj_cond_type = {
427 /* size = */ sizeof(obj_cond_t),
428 /* init = */ obj_cond_init,
429 /* destroy = */ obj_cond_destroy,
430 };
432 /*
433 * private matcher types
434 */
436 /* initializes a string matcher consuming two elements from ap */
437 static int
438 string_matcher_init(string_matcher_t *m, va_list ap)
439 {
440 const char *name = va_arg(ap, const char *);
441 const char *name_re = va_arg(ap, const char *);
443 if (name) {
444 m->name = strdup(name);
445 if (! m->name)
446 return -1;
447 }
448 if (name_re) {
449 m->name_re = malloc(sizeof(*m->name_re));
450 if (! m->name_re)
451 return -1;
452 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
453 return -1;
454 }
455 return 0;
456 } /* string_matcher_init */
458 static void
459 string_matcher_destroy(string_matcher_t *m)
460 {
461 if (m->name)
462 free(m->name);
463 if (m->name_re) {
464 regfree(m->name_re);
465 free(m->name_re);
466 }
467 } /* string_matcher_destroy */
469 static char *
470 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
471 {
472 snprintf(buf, buflen, "{ %s%s%s, %p }",
473 m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
474 m->name_re);
475 return buf;
476 } /* string_tostring */
478 /* initializes a name matcher */
479 static int
480 name_matcher_init(sdb_object_t *obj, va_list ap)
481 {
482 name_matcher_t *m = NAME_M(obj);
483 M(obj)->type = MATCHER_NAME;
484 return string_matcher_init(&m->name, ap);
485 } /* name_matcher_init */
487 static void
488 name_matcher_destroy(sdb_object_t *obj)
489 {
490 name_matcher_t *m = NAME_M(obj);
491 string_matcher_destroy(&m->name);
492 } /* name_matcher_destroy */
494 static char *
495 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
496 {
497 char name[buflen + 1];
498 assert(m->type == MATCHER_NAME);
499 snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
500 SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
501 string_tostring(&NAME_M(m)->name, name, sizeof(name)));
502 return buf;
503 } /* name_tostring */
505 static int
506 attr_matcher_init(sdb_object_t *obj, va_list ap)
507 {
508 attr_matcher_t *attr = ATTR_M(obj);
509 const char *name = va_arg(ap, const char *);
511 M(obj)->type = MATCHER_ATTR;
512 if (name) {
513 attr->name = strdup(name);
514 if (! attr->name)
515 return -1;
516 }
517 return string_matcher_init(&attr->value, ap);
518 } /* attr_matcher_init */
520 static void
521 attr_matcher_destroy(sdb_object_t *obj)
522 {
523 attr_matcher_t *attr = ATTR_M(obj);
524 if (attr->name)
525 free(attr->name);
526 attr->name = NULL;
527 string_matcher_destroy(&attr->value);
528 } /* attr_matcher_destroy */
530 static char *
531 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
532 {
533 char value[buflen + 1];
535 if (! m) {
536 snprintf(buf, buflen, "ATTR{}");
537 return buf;
538 }
540 assert(m->type == MATCHER_ATTR);
541 snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
542 string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
543 return buf;
544 } /* attr_tostring */
546 static int
547 cond_matcher_init(sdb_object_t *obj, va_list ap)
548 {
549 int type = va_arg(ap, int);
550 sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
552 if (! cond)
553 return -1;
555 sdb_object_ref(SDB_OBJ(cond));
557 M(obj)->type = type;
558 COND_M(obj)->cond = cond;
559 return 0;
560 } /* cond_matcher_init */
562 static void
563 cond_matcher_destroy(sdb_object_t *obj)
564 {
565 sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
566 } /* cond_matcher_destroy */
568 static char *
569 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
570 {
571 const char *type, *id;
572 sdb_data_t value = SDB_DATA_INIT;
573 char value_str[buflen];
574 sdb_store_expr_t *expr;
576 if (COND_M(m)->cond->cmp == attr_cmp) {
577 type = "ATTR";
578 id = ATTR_C(COND_M(m)->cond)->name;
579 expr = ATTR_C(COND_M(m)->cond)->expr;
580 }
581 else if (COND_M(m)->cond->cmp == obj_cmp) {
582 type = "OBJ";
583 id = SDB_FIELD_TO_NAME(OBJ_C(COND_M(m)->cond)->field);
584 expr = OBJ_C(COND_M(m)->cond)->expr;
585 }
586 else {
587 snprintf(buf, buflen, "<unknown>");
588 return buf;
589 }
591 if (sdb_store_expr_eval(expr, NULL, &value))
592 snprintf(value_str, sizeof(value_str), "ERR");
593 else if (sdb_data_format(&value, value_str, sizeof(value_str),
594 SDB_SINGLE_QUOTED) < 0)
595 snprintf(value_str, sizeof(value_str), "ERR");
596 snprintf(buf, buflen, "%s[%s]{ %s %s }", type, id,
597 MATCHER_SYM(m->type), value_str);
598 sdb_data_free_datum(&value);
599 return buf;
600 } /* cond_tostring */
602 static int
603 op_matcher_init(sdb_object_t *obj, va_list ap)
604 {
605 M(obj)->type = va_arg(ap, int);
606 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
607 return -1;
609 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
610 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
611 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
612 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
614 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
615 return -1;
616 return 0;
617 } /* op_matcher_init */
619 static void
620 op_matcher_destroy(sdb_object_t *obj)
621 {
622 if (OP_M(obj)->left)
623 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
624 if (OP_M(obj)->right)
625 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
626 } /* op_matcher_destroy */
628 static char *
629 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
630 {
631 char left[buflen + 1], right[buflen + 1];
633 if (! m) {
634 /* this should not happen */
635 snprintf(buf, buflen, "()");
636 return buf;
637 }
639 assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
640 snprintf(buf, buflen, "(%s, %s, %s)",
641 m->type == MATCHER_OR ? "OR" : "AND",
642 sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
643 sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
644 return buf;
645 } /* op_tostring */
647 static int
648 uop_matcher_init(sdb_object_t *obj, va_list ap)
649 {
650 M(obj)->type = va_arg(ap, int);
651 if (M(obj)->type != MATCHER_NOT)
652 return -1;
654 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
655 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
657 if (! UOP_M(obj)->op)
658 return -1;
659 return 0;
660 } /* uop_matcher_init */
662 static void
663 uop_matcher_destroy(sdb_object_t *obj)
664 {
665 if (UOP_M(obj)->op)
666 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
667 } /* uop_matcher_destroy */
669 static char *
670 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
671 {
672 char op[buflen + 1];
674 if (! m) {
675 /* this should not happen */
676 snprintf(buf, buflen, "()");
677 return buf;
678 }
680 assert(m->type == MATCHER_NOT);
681 snprintf(buf, buflen, "(NOT, %s)",
682 sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
683 return buf;
684 } /* uop_tostring */
686 static int
687 isnull_matcher_init(sdb_object_t *obj, va_list ap)
688 {
689 const char *name;
691 M(obj)->type = va_arg(ap, int);
692 if (M(obj)->type != MATCHER_ISNULL)
693 return -1;
695 name = va_arg(ap, const char *);
696 if (! name)
697 return -1;
698 ISNULL_M(obj)->attr_name = strdup(name);
699 if (! ISNULL_M(obj)->attr_name)
700 return -1;
701 return 0;
702 } /* isnull_matcher_init */
704 static void
705 isnull_matcher_destroy(sdb_object_t *obj)
706 {
707 if (ISNULL_M(obj)->attr_name)
708 free(ISNULL_M(obj)->attr_name);
709 ISNULL_M(obj)->attr_name = NULL;
710 } /* isnull_matcher_destroy */
712 static char *
713 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
714 {
715 snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
716 return buf;
717 } /* isnull_tostring */
719 static sdb_type_t name_type = {
720 /* size = */ sizeof(name_matcher_t),
721 /* init = */ name_matcher_init,
722 /* destroy = */ name_matcher_destroy,
723 };
725 static sdb_type_t attr_type = {
726 /* size = */ sizeof(attr_matcher_t),
727 /* init = */ attr_matcher_init,
728 /* destroy = */ attr_matcher_destroy,
729 };
731 static sdb_type_t cond_type = {
732 /* size = */ sizeof(cond_matcher_t),
733 /* init = */ cond_matcher_init,
734 /* destroy = */ cond_matcher_destroy,
735 };
737 static sdb_type_t op_type = {
738 /* size = */ sizeof(op_matcher_t),
739 /* init = */ op_matcher_init,
740 /* destroy = */ op_matcher_destroy,
741 };
743 static sdb_type_t uop_type = {
744 /* size = */ sizeof(uop_matcher_t),
745 /* init = */ uop_matcher_init,
746 /* destroy = */ uop_matcher_destroy,
747 };
749 static sdb_type_t isnull_type = {
750 /* size = */ sizeof(isnull_matcher_t),
751 /* init = */ isnull_matcher_init,
752 /* destroy = */ isnull_matcher_destroy,
753 };
755 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
757 /* this array needs to be indexable by the matcher types;
758 * -> update the enum in store-private.h when updating this */
759 static matcher_tostring_cb
760 matchers_tostring[] = {
761 op_tostring,
762 op_tostring,
763 uop_tostring,
764 name_tostring,
765 attr_tostring,
766 cond_tostring,
767 cond_tostring,
768 cond_tostring,
769 cond_tostring,
770 cond_tostring,
771 isnull_tostring,
772 };
774 /*
775 * public API
776 */
778 sdb_store_cond_t *
779 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
780 {
781 return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
782 name, expr));
783 } /* sdb_store_attr_cond */
785 sdb_store_cond_t *
786 sdb_store_obj_cond(int field, sdb_store_expr_t *expr)
787 {
788 return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type,
789 field, expr));
790 } /* sdb_store_obj_cond */
792 sdb_store_matcher_t *
793 sdb_store_name_matcher(int type, const char *name, _Bool re)
794 {
795 sdb_store_matcher_t *m;
797 if (re)
798 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
799 else
800 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
802 if (! m)
803 return NULL;
805 NAME_M(m)->obj_type = type;
806 return m;
807 } /* sdb_store_name_matcher */
809 sdb_store_matcher_t *
810 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
811 {
812 sdb_store_matcher_t *m;
814 if (! name)
815 return NULL;
817 if (re)
818 m = M(sdb_object_create("attr-matcher", attr_type,
819 name, NULL, value));
820 else
821 m = M(sdb_object_create("attr-matcher", attr_type,
822 name, value, NULL));
823 return m;
824 } /* sdb_store_attr_matcher */
826 sdb_store_matcher_t *
827 sdb_store_lt_matcher(sdb_store_cond_t *cond)
828 {
829 return M(sdb_object_create("lt-matcher", cond_type,
830 MATCHER_LT, cond));
831 } /* sdb_store_lt_matcher */
833 sdb_store_matcher_t *
834 sdb_store_le_matcher(sdb_store_cond_t *cond)
835 {
836 return M(sdb_object_create("le-matcher", cond_type,
837 MATCHER_LE, cond));
838 } /* sdb_store_le_matcher */
840 sdb_store_matcher_t *
841 sdb_store_eq_matcher(sdb_store_cond_t *cond)
842 {
843 return M(sdb_object_create("eq-matcher", cond_type,
844 MATCHER_EQ, cond));
845 } /* sdb_store_eq_matcher */
847 sdb_store_matcher_t *
848 sdb_store_ge_matcher(sdb_store_cond_t *cond)
849 {
850 return M(sdb_object_create("ge-matcher", cond_type,
851 MATCHER_GE, cond));
852 } /* sdb_store_ge_matcher */
854 sdb_store_matcher_t *
855 sdb_store_gt_matcher(sdb_store_cond_t *cond)
856 {
857 return M(sdb_object_create("gt-matcher", cond_type,
858 MATCHER_GT, cond));
859 } /* sdb_store_gt_matcher */
861 sdb_store_matcher_t *
862 sdb_store_isnull_matcher(const char *attr_name)
863 {
864 return M(sdb_object_create("isnull-matcher", isnull_type,
865 MATCHER_ISNULL, attr_name));
866 } /* sdb_store_isnull_matcher */
868 int
869 sdb_store_parse_object_type_plural(const char *name)
870 {
871 if (! strcasecmp(name, "hosts"))
872 return SDB_HOST;
873 else if (! strcasecmp(name, "services"))
874 return SDB_SERVICE;
875 else if (! strcasecmp(name, "metrics"))
876 return SDB_METRIC;
877 return -1;
878 } /* sdb_store_parse_object_type_plural */
880 int
881 sdb_store_parse_field_name(const char *name)
882 {
883 if (! strcasecmp(name, "last_update"))
884 return SDB_FIELD_LAST_UPDATE;
885 else if (! strcasecmp(name, "age"))
886 return SDB_FIELD_AGE;
887 else if (! strcasecmp(name, "interval"))
888 return SDB_FIELD_INTERVAL;
889 else if (! strcasecmp(name, "backend"))
890 return SDB_FIELD_BACKEND;
891 return -1;
892 } /* sdb_store_parse_field_name */
894 static sdb_store_matcher_t *
895 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
896 {
897 sdb_store_matcher_t *tmp;
899 if ((! m) || (! inv))
900 return m;
902 tmp = sdb_store_inv_matcher(m);
903 /* pass ownership to the inverse matcher */
904 sdb_object_deref(SDB_OBJ(m));
905 return tmp;
906 } /* maybe_inv_matcher */
908 static int
909 parse_cond_op(const char *op,
910 sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv)
911 {
912 *inv = 0;
913 if (! strcasecmp(op, "<"))
914 *matcher = sdb_store_lt_matcher;
915 else if (! strcasecmp(op, "<="))
916 *matcher = sdb_store_le_matcher;
917 else if (! strcasecmp(op, "="))
918 *matcher = sdb_store_eq_matcher;
919 else if (! strcasecmp(op, ">="))
920 *matcher = sdb_store_ge_matcher;
921 else if (! strcasecmp(op, ">"))
922 *matcher = sdb_store_gt_matcher;
923 else if (! strcasecmp(op, "!=")) {
924 *matcher = sdb_store_eq_matcher;
925 *inv = 1;
926 }
927 else
928 return -1;
929 return 0;
930 } /* parse_cond_op */
932 static sdb_store_matcher_t *
933 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
934 {
935 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
936 sdb_store_matcher_t *m;
937 sdb_store_cond_t *cond;
938 _Bool inv = 0;
940 if (! attr)
941 return NULL;
943 if (! strcasecmp(op, "IS")) {
944 if (! expr)
945 return sdb_store_isnull_matcher(attr);
946 else
947 return NULL;
948 }
949 else if (! expr)
950 return NULL;
951 else if (parse_cond_op(op, &matcher, &inv))
952 return NULL;
954 cond = sdb_store_attr_cond(attr, expr);
955 if (! cond)
956 return NULL;
958 m = matcher(cond);
959 /* pass ownership to 'm' or destroy in case of an error */
960 sdb_object_deref(SDB_OBJ(cond));
961 return maybe_inv_matcher(m, inv);
962 } /* parse_attr_cmp */
964 sdb_store_matcher_t *
965 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
966 const char *op, sdb_store_expr_t *expr)
967 {
968 int type = -1;
969 _Bool inv = 0;
970 _Bool re = 0;
972 sdb_data_t value = SDB_DATA_INIT;
973 sdb_store_matcher_t *m = NULL;
975 if (! strcasecmp(obj_type, "host"))
976 type = SDB_HOST;
977 else if (! strcasecmp(obj_type, "service"))
978 type = SDB_SERVICE;
979 else if (! strcasecmp(obj_type, "metric"))
980 type = SDB_METRIC;
981 else if (! strcasecmp(obj_type, "attribute"))
982 type = SDB_ATTRIBUTE;
983 else
984 return NULL;
986 /* XXX: this code sucks! */
987 if (! strcasecmp(op, "=")) {
988 /* nothing to do */
989 }
990 else if (! strcasecmp(op, "!=")) {
991 inv = 1;
992 }
993 else if (! strcasecmp(op, "=~")) {
994 re = 1;
995 }
996 else if (! strcasecmp(op, "!~")) {
997 inv = 1;
998 re = 1;
999 }
1000 else if (type == SDB_ATTRIBUTE)
1001 return parse_attr_cmp(attr, op, expr);
1002 else
1003 return NULL;
1005 if (! expr)
1006 return NULL;
1008 if (sdb_store_expr_eval(expr, NULL, &value))
1009 return NULL;
1010 if (value.type != SDB_TYPE_STRING) {
1011 sdb_data_free_datum(&value);
1012 if (type != SDB_ATTRIBUTE)
1013 return NULL;
1014 return parse_attr_cmp(attr, op, expr);
1015 }
1017 if (! attr)
1018 m = sdb_store_name_matcher(type, value.data.string, re);
1019 else if (type == SDB_ATTRIBUTE)
1020 m = sdb_store_attr_matcher(attr, value.data.string, re);
1022 sdb_data_free_datum(&value);
1023 return maybe_inv_matcher(m, inv);
1024 } /* sdb_store_matcher_parse_cmp */
1026 sdb_store_matcher_t *
1027 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
1028 sdb_store_expr_t *expr)
1029 {
1030 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1031 sdb_store_matcher_t *m;
1032 sdb_store_cond_t *cond;
1033 _Bool inv = 0;
1035 int field;
1037 if (! expr)
1038 return NULL;
1040 field = sdb_store_parse_field_name(name);
1041 if (field < 0)
1042 return NULL;
1044 if (parse_cond_op(op, &matcher, &inv))
1045 return NULL;
1046 cond = sdb_store_obj_cond(field, expr);
1047 if (! cond)
1048 return NULL;
1050 assert(matcher);
1051 m = matcher(cond);
1052 /* pass ownership to 'm' or destroy in case of an error */
1053 sdb_object_deref(SDB_OBJ(cond));
1054 return maybe_inv_matcher(m, inv);
1055 } /* sdb_store_matcher_parse_field_cmp */
1057 sdb_store_matcher_t *
1058 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1059 {
1060 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
1061 left, right));
1062 } /* sdb_store_dis_matcher */
1064 sdb_store_matcher_t *
1065 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1066 {
1067 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
1068 left, right));
1069 } /* sdb_store_con_matcher */
1071 sdb_store_matcher_t *
1072 sdb_store_inv_matcher(sdb_store_matcher_t *m)
1073 {
1074 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
1075 } /* sdb_store_inv_matcher */
1077 int
1078 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
1079 sdb_store_matcher_t *filter)
1080 {
1081 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
1082 return 0;
1084 /* "NULL" always matches */
1085 if ((! m) || (! obj))
1086 return 1;
1088 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1089 return 0;
1091 return matchers[m->type](m, obj, filter);
1092 } /* sdb_store_matcher_matches */
1094 char *
1095 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1096 {
1097 if (! m)
1098 return NULL;
1100 if ((m->type < 0)
1101 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
1102 return NULL;
1103 return matchers_tostring[m->type](m, buf, buflen);
1104 } /* sdb_store_matcher_tostring */
1106 int
1107 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1108 sdb_store_lookup_cb cb, void *user_data)
1109 {
1110 scan_iter_data_t data = { m, filter, cb, user_data };
1112 if (! cb)
1113 return -1;
1114 return sdb_store_iterate(scan_iter, &data);
1115 } /* sdb_store_scan */
1117 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */