Code

store_lookup: Fixed parsing of invalid compare expressions.
[sysdb.git] / src / core / store_lookup.c
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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_object_type_plural(const char *name)
863         if (! strcasecmp(name, "hosts"))
864                 return SDB_HOST;
865         else if (! strcasecmp(name, "services"))
866                 return SDB_SERVICE;
867         else if (! strcasecmp(name, "metrics"))
868                 return SDB_METRIC;
869         return -1;
870 } /* sdb_store_parse_object_type_plural */
872 int
873 sdb_store_parse_field_name(const char *name)
875         if (! strcasecmp(name, "last_update"))
876                 return SDB_FIELD_LAST_UPDATE;
877         else if (! strcasecmp(name, "age"))
878                 return SDB_FIELD_AGE;
879         else if (! strcasecmp(name, "interval"))
880                 return SDB_FIELD_INTERVAL;
881         else if (! strcasecmp(name, "backend"))
882                 return SDB_FIELD_BACKEND;
883         return -1;
884 } /* sdb_store_parse_field_name */
886 static sdb_store_matcher_t *
887 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
889         sdb_store_matcher_t *tmp;
891         if ((! m) || (! inv))
892                 return m;
894         tmp = sdb_store_inv_matcher(m);
895         /* pass ownership to the inverse matcher */
896         sdb_object_deref(SDB_OBJ(m));
897         return tmp;
898 } /* maybe_inv_matcher */
900 static int
901 parse_cond_op(const char *op,
902                 sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv)
904         *inv = 0;
905         if (! strcasecmp(op, "<"))
906                 *matcher = sdb_store_lt_matcher;
907         else if (! strcasecmp(op, "<="))
908                 *matcher = sdb_store_le_matcher;
909         else if (! strcasecmp(op, "="))
910                 *matcher = sdb_store_eq_matcher;
911         else if (! strcasecmp(op, ">="))
912                 *matcher = sdb_store_ge_matcher;
913         else if (! strcasecmp(op, ">"))
914                 *matcher = sdb_store_gt_matcher;
915         else if (! strcasecmp(op, "!=")) {
916                 *matcher = sdb_store_eq_matcher;
917                 *inv = 1;
918         }
919         else
920                 return -1;
921         return 0;
922 } /* parse_cond_op */
924 static sdb_store_matcher_t *
925 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
927         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
928         sdb_store_matcher_t *m;
929         sdb_store_cond_t *cond;
930         _Bool inv = 0;
932         if (! attr)
933                 return NULL;
935         if (! strcasecmp(op, "IS")) {
936                 if (! expr)
937                         return sdb_store_isnull_matcher(attr);
938                 else
939                         return NULL;
940         }
941         else if (! expr)
942                 return NULL;
943         else if (parse_cond_op(op, &matcher, &inv))
944                 return NULL;
946         cond = sdb_store_attr_cond(attr, expr);
947         if (! cond)
948                 return NULL;
950         m = matcher(cond);
951         /* pass ownership to 'm' or destroy in case of an error */
952         sdb_object_deref(SDB_OBJ(cond));
953         return maybe_inv_matcher(m, inv);
954 } /* parse_attr_cmp */
956 sdb_store_matcher_t *
957 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
958                 const char *op, sdb_store_expr_t *expr)
960         int type = -1;
961         _Bool inv = 0;
962         _Bool re = 0;
964         sdb_data_t value = SDB_DATA_INIT;
965         sdb_store_matcher_t *m = NULL;
967         if (! strcasecmp(obj_type, "host"))
968                 type = SDB_HOST;
969         else if (! strcasecmp(obj_type, "service"))
970                 type = SDB_SERVICE;
971         else if (! strcasecmp(obj_type, "metric"))
972                 type = SDB_METRIC;
973         else if (! strcasecmp(obj_type, "attribute"))
974                 type = SDB_ATTRIBUTE;
975         else
976                 return NULL;
978         /* XXX: this code sucks! */
979         if (! strcasecmp(op, "=")) {
980                 /* nothing to do */
981         }
982         else if (! strcasecmp(op, "!=")) {
983                 inv = 1;
984         }
985         else if (! strcasecmp(op, "=~")) {
986                 re = 1;
987         }
988         else if (! strcasecmp(op, "!~")) {
989                 inv = 1;
990                 re = 1;
991         }
992         else if (type == SDB_ATTRIBUTE)
993                 return parse_attr_cmp(attr, op, expr);
994         else
995                 return NULL;
997         if (! expr)
998                 return NULL;
1000         if (sdb_store_expr_eval(expr, NULL, &value))
1001                 return NULL;
1002         if (value.type != SDB_TYPE_STRING) {
1003                 sdb_data_free_datum(&value);
1004                 if (type != SDB_ATTRIBUTE)
1005                         return NULL;
1006                 return parse_attr_cmp(attr, op, expr);
1007         }
1009         if (! attr)
1010                 m = sdb_store_name_matcher(type, value.data.string, re);
1011         else if (type == SDB_ATTRIBUTE)
1012                 m = sdb_store_attr_matcher(attr, value.data.string, re);
1014         sdb_data_free_datum(&value);
1015         return maybe_inv_matcher(m, inv);
1016 } /* sdb_store_matcher_parse_cmp */
1018 sdb_store_matcher_t *
1019 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
1020                 sdb_store_expr_t *expr)
1022         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1023         sdb_store_matcher_t *m;
1024         sdb_store_cond_t *cond;
1025         _Bool inv = 0;
1027         int field;
1029         if (! expr)
1030                 return NULL;
1032         field = sdb_store_parse_field_name(name);
1033         if (field < 0)
1034                 return NULL;
1036         if (parse_cond_op(op, &matcher, &inv))
1037                 return NULL;
1038         cond = sdb_store_obj_cond(field, expr);
1039         if (! cond)
1040                 return NULL;
1042         assert(matcher);
1043         m = matcher(cond);
1044         /* pass ownership to 'm' or destroy in case of an error */
1045         sdb_object_deref(SDB_OBJ(cond));
1046         return maybe_inv_matcher(m, inv);
1047 } /* sdb_store_matcher_parse_field_cmp */
1049 sdb_store_matcher_t *
1050 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1052         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
1053                                 left, right));
1054 } /* sdb_store_dis_matcher */
1056 sdb_store_matcher_t *
1057 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1059         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
1060                                 left, right));
1061 } /* sdb_store_con_matcher */
1063 sdb_store_matcher_t *
1064 sdb_store_inv_matcher(sdb_store_matcher_t *m)
1066         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
1067 } /* sdb_store_inv_matcher */
1069 int
1070 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
1071                 sdb_store_matcher_t *filter)
1073         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
1074                 return 0;
1076         /* "NULL" always matches */
1077         if ((! m) || (! obj))
1078                 return 1;
1080         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1081                 return 0;
1083         return matchers[m->type](m, obj, filter);
1084 } /* sdb_store_matcher_matches */
1086 char *
1087 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1089         if (! m)
1090                 return NULL;
1092         if ((m->type < 0)
1093                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
1094                 return NULL;
1095         return matchers_tostring[m->type](m, buf, buflen);
1096 } /* sdb_store_matcher_tostring */
1098 int
1099 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1100                 sdb_store_lookup_cb cb, void *user_data)
1102         scan_iter_data_t data = { m, filter, cb, user_data };
1104         if (! cb)
1105                 return -1;
1106         return sdb_store_iterate(scan_iter, &data);
1107 } /* sdb_store_scan */
1109 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */