Code

store_expr: Added support to include field values in an expression.
[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_ATTRIBUTE:
237                         iter = sdb_avltree_get_iter(HOST(obj)->attributes);
238                         break;
239         }
241         while (sdb_avltree_iter_has_next(iter)) {
242                 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
243                 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
244                                                 NULL)))
245                         continue;
246                 if (match_string(&NAME_M(m)->name, child->name)) {
247                         status = 1;
248                         break;
249                 }
250         }
251         sdb_avltree_iter_destroy(iter);
252         return status;
253 } /* match_name */
255 static int
256 match_attr(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
257                 sdb_store_matcher_t *filter)
259         sdb_attribute_t *attr;
261         assert(m->type == MATCHER_ATTR);
262         assert(ATTR_M(m)->name);
264         if (obj->type != SDB_HOST)
265                 return 0;
267         attr = attr_get(HOST(obj), ATTR_M(m)->name, filter);
268         if (attr) {
269                 char buf[sdb_data_strlen(&attr->value) + 1];
270                 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
271                         return 0;
272                 if (match_string(&ATTR_M(m)->value, buf))
273                         return 1;
274         }
275         return 0;
276 } /* match_attr */
278 static int
279 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
280                 sdb_store_matcher_t *filter)
282         int status;
283         assert(m->type == MATCHER_LT);
284         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
285         return (status != INT_MAX) && (status < 0);
286 } /* match_lt */
288 static int
289 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
290                 sdb_store_matcher_t *filter)
292         int status;
293         assert(m->type == MATCHER_LE);
294         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
295         return (status != INT_MAX) && (status <= 0);
296 } /* match_le */
298 static int
299 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
300                 sdb_store_matcher_t *filter)
302         int status;
303         assert(m->type == MATCHER_EQ);
304         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
305         return (status != INT_MAX) && (! status);
306 } /* match_eq */
308 static int
309 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
310                 sdb_store_matcher_t *filter)
312         int status;
313         assert(m->type == MATCHER_GE);
314         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
315         return (status != INT_MAX) && (status >= 0);
316 } /* match_ge */
318 static int
319 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
320                 sdb_store_matcher_t *filter)
322         int status;
323         assert(m->type == MATCHER_GT);
324         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
325         return (status != INT_MAX) && (status > 0);
326 } /* match_gt */
328 static int
329 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
330                 sdb_store_matcher_t *filter)
332         assert(m->type == MATCHER_ISNULL);
333         if (obj->type != SDB_HOST)
334                 return 0;
335         return attr_get(HOST(obj), ISNULL_M(m)->attr_name, filter) == NULL;
336 } /* match_isnull */
338 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
339                 sdb_store_matcher_t *);
341 /* this array needs to be indexable by the matcher types;
342  * -> update the enum in store-private.h when updating this */
343 static matcher_cb
344 matchers[] = {
345         match_logical,
346         match_logical,
347         match_unary,
348         match_name,
349         match_attr,
350         match_lt,
351         match_le,
352         match_eq,
353         match_ge,
354         match_gt,
355         match_isnull,
356 };
358 /*
359  * private conditional types
360  */
362 static int
363 attr_cond_init(sdb_object_t *obj, va_list ap)
365         const char *name = va_arg(ap, const char *);
366         sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
368         if (! name)
369                 return -1;
371         SDB_STORE_COND(obj)->cmp = attr_cmp;
373         ATTR_C(obj)->name = strdup(name);
374         if (! ATTR_C(obj)->name)
375                 return -1;
376         ATTR_C(obj)->expr = expr;
377         sdb_object_ref(SDB_OBJ(expr));
378         return 0;
379 } /* attr_cond_init */
381 static void
382 attr_cond_destroy(sdb_object_t *obj)
384         if (ATTR_C(obj)->name)
385                 free(ATTR_C(obj)->name);
386         sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
387 } /* attr_cond_destroy */
389 static sdb_type_t attr_cond_type = {
390         /* size = */ sizeof(attr_cond_t),
391         /* init = */ attr_cond_init,
392         /* destroy = */ attr_cond_destroy,
393 };
395 static int
396 obj_cond_init(sdb_object_t *obj, va_list ap)
398         int field = va_arg(ap, int);
399         sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
401         SDB_STORE_COND(obj)->cmp = obj_cmp;
403         OBJ_C(obj)->field = field;
404         OBJ_C(obj)->expr = expr;
405         sdb_object_ref(SDB_OBJ(expr));
406         return 0;
407 } /* obj_cond_init */
409 static void
410 obj_cond_destroy(sdb_object_t *obj)
412         sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr));
413 } /* obj_cond_destroy */
415 static sdb_type_t obj_cond_type = {
416         /* size = */ sizeof(obj_cond_t),
417         /* init = */ obj_cond_init,
418         /* destroy = */ obj_cond_destroy,
419 };
421 /*
422  * private matcher types
423  */
425 /* initializes a string matcher consuming two elements from ap */
426 static int
427 string_matcher_init(string_matcher_t *m, va_list ap)
429         const char *name = va_arg(ap, const char *);
430         const char *name_re = va_arg(ap, const char *);
432         if (name) {
433                 m->name = strdup(name);
434                 if (! m->name)
435                         return -1;
436         }
437         if (name_re) {
438                 m->name_re = malloc(sizeof(*m->name_re));
439                 if (! m->name_re)
440                         return -1;
441                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
442                         return -1;
443         }
444         return 0;
445 } /* string_matcher_init */
447 static void
448 string_matcher_destroy(string_matcher_t *m)
450         if (m->name)
451                 free(m->name);
452         if (m->name_re) {
453                 regfree(m->name_re);
454                 free(m->name_re);
455         }
456 } /* string_matcher_destroy */
458 static char *
459 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
461         snprintf(buf, buflen, "{ %s%s%s, %p }",
462                         m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
463                         m->name_re);
464         return buf;
465 } /* string_tostring */
467 /* initializes a name matcher */
468 static int
469 name_matcher_init(sdb_object_t *obj, va_list ap)
471         name_matcher_t *m = NAME_M(obj);
472         M(obj)->type = MATCHER_NAME;
473         return string_matcher_init(&m->name, ap);
474 } /* name_matcher_init */
476 static void
477 name_matcher_destroy(sdb_object_t *obj)
479         name_matcher_t *m = NAME_M(obj);
480         string_matcher_destroy(&m->name);
481 } /* name_matcher_destroy */
483 static char *
484 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
486         char name[buflen + 1];
487         assert(m->type == MATCHER_NAME);
488         snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
489                         SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
490                         string_tostring(&NAME_M(m)->name, name, sizeof(name)));
491         return buf;
492 } /* name_tostring */
494 static int
495 attr_matcher_init(sdb_object_t *obj, va_list ap)
497         attr_matcher_t *attr = ATTR_M(obj);
498         const char *name = va_arg(ap, const char *);
500         M(obj)->type = MATCHER_ATTR;
501         if (name) {
502                 attr->name = strdup(name);
503                 if (! attr->name)
504                         return -1;
505         }
506         return string_matcher_init(&attr->value, ap);
507 } /* attr_matcher_init */
509 static void
510 attr_matcher_destroy(sdb_object_t *obj)
512         attr_matcher_t *attr = ATTR_M(obj);
513         if (attr->name)
514                 free(attr->name);
515         attr->name = NULL;
516         string_matcher_destroy(&attr->value);
517 } /* attr_matcher_destroy */
519 static char *
520 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
522         char value[buflen + 1];
524         if (! m) {
525                 snprintf(buf, buflen, "ATTR{}");
526                 return buf;
527         }
529         assert(m->type == MATCHER_ATTR);
530         snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
531                         string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
532         return buf;
533 } /* attr_tostring */
535 static int
536 cond_matcher_init(sdb_object_t *obj, va_list ap)
538         int type = va_arg(ap, int);
539         sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
541         if (! cond)
542                 return -1;
544         sdb_object_ref(SDB_OBJ(cond));
546         M(obj)->type = type;
547         COND_M(obj)->cond = cond;
548         return 0;
549 } /* cond_matcher_init */
551 static void
552 cond_matcher_destroy(sdb_object_t *obj)
554         sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
555 } /* cond_matcher_destroy */
557 static char *
558 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
560         const char *type, *id;
561         sdb_data_t value = SDB_DATA_INIT;
562         char value_str[buflen];
563         sdb_store_expr_t *expr;
565         if (COND_M(m)->cond->cmp == attr_cmp) {
566                 type = "ATTR";
567                 id = ATTR_C(COND_M(m)->cond)->name;
568                 expr = ATTR_C(COND_M(m)->cond)->expr;
569         }
570         else if (COND_M(m)->cond->cmp == obj_cmp) {
571                 type = "OBJ";
572                 id = SDB_FIELD_TO_NAME(OBJ_C(COND_M(m)->cond)->field);
573                 expr = OBJ_C(COND_M(m)->cond)->expr;
574         }
575         else {
576                 snprintf(buf, buflen, "<unknown>");
577                 return buf;
578         }
580         if (sdb_store_expr_eval(expr, NULL, &value))
581                 snprintf(value_str, sizeof(value_str), "ERR");
582         else if (sdb_data_format(&value, value_str, sizeof(value_str),
583                                 SDB_SINGLE_QUOTED) < 0)
584                 snprintf(value_str, sizeof(value_str), "ERR");
585         snprintf(buf, buflen, "%s[%s]{ %s %s }", type, id,
586                         MATCHER_SYM(m->type), value_str);
587         sdb_data_free_datum(&value);
588         return buf;
589 } /* cond_tostring */
591 static int
592 op_matcher_init(sdb_object_t *obj, va_list ap)
594         M(obj)->type = va_arg(ap, int);
595         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
596                 return -1;
598         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
599         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
600         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
601         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
603         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
604                 return -1;
605         return 0;
606 } /* op_matcher_init */
608 static void
609 op_matcher_destroy(sdb_object_t *obj)
611         if (OP_M(obj)->left)
612                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
613         if (OP_M(obj)->right)
614                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
615 } /* op_matcher_destroy */
617 static char *
618 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
620         char left[buflen + 1], right[buflen + 1];
622         if (! m) {
623                 /* this should not happen */
624                 snprintf(buf, buflen, "()");
625                 return buf;
626         }
628         assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
629         snprintf(buf, buflen, "(%s, %s, %s)",
630                         m->type == MATCHER_OR ? "OR" : "AND",
631                         sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
632                         sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
633         return buf;
634 } /* op_tostring */
636 static int
637 uop_matcher_init(sdb_object_t *obj, va_list ap)
639         M(obj)->type = va_arg(ap, int);
640         if (M(obj)->type != MATCHER_NOT)
641                 return -1;
643         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
644         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
646         if (! UOP_M(obj)->op)
647                 return -1;
648         return 0;
649 } /* uop_matcher_init */
651 static void
652 uop_matcher_destroy(sdb_object_t *obj)
654         if (UOP_M(obj)->op)
655                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
656 } /* uop_matcher_destroy */
658 static char *
659 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
661         char op[buflen + 1];
663         if (! m) {
664                 /* this should not happen */
665                 snprintf(buf, buflen, "()");
666                 return buf;
667         }
669         assert(m->type == MATCHER_NOT);
670         snprintf(buf, buflen, "(NOT, %s)",
671                         sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
672         return buf;
673 } /* uop_tostring */
675 static int
676 isnull_matcher_init(sdb_object_t *obj, va_list ap)
678         const char *name;
680         M(obj)->type = va_arg(ap, int);
681         if (M(obj)->type != MATCHER_ISNULL)
682                 return -1;
684         name = va_arg(ap, const char *);
685         if (! name)
686                 return -1;
687         ISNULL_M(obj)->attr_name = strdup(name);
688         if (! ISNULL_M(obj)->attr_name)
689                 return -1;
690         return 0;
691 } /* isnull_matcher_init */
693 static void
694 isnull_matcher_destroy(sdb_object_t *obj)
696         if (ISNULL_M(obj)->attr_name)
697                 free(ISNULL_M(obj)->attr_name);
698         ISNULL_M(obj)->attr_name = NULL;
699 } /* isnull_matcher_destroy */
701 static char *
702 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
704         snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
705         return buf;
706 } /* isnull_tostring */
708 static sdb_type_t name_type = {
709         /* size = */ sizeof(name_matcher_t),
710         /* init = */ name_matcher_init,
711         /* destroy = */ name_matcher_destroy,
712 };
714 static sdb_type_t attr_type = {
715         /* size = */ sizeof(attr_matcher_t),
716         /* init = */ attr_matcher_init,
717         /* destroy = */ attr_matcher_destroy,
718 };
720 static sdb_type_t cond_type = {
721         /* size = */ sizeof(cond_matcher_t),
722         /* init = */ cond_matcher_init,
723         /* destroy = */ cond_matcher_destroy,
724 };
726 static sdb_type_t op_type = {
727         /* size = */ sizeof(op_matcher_t),
728         /* init = */ op_matcher_init,
729         /* destroy = */ op_matcher_destroy,
730 };
732 static sdb_type_t uop_type = {
733         /* size = */ sizeof(uop_matcher_t),
734         /* init = */ uop_matcher_init,
735         /* destroy = */ uop_matcher_destroy,
736 };
738 static sdb_type_t isnull_type = {
739         /* size = */ sizeof(isnull_matcher_t),
740         /* init = */ isnull_matcher_init,
741         /* destroy = */ isnull_matcher_destroy,
742 };
744 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
746 /* this array needs to be indexable by the matcher types;
747  * -> update the enum in store-private.h when updating this */
748 static matcher_tostring_cb
749 matchers_tostring[] = {
750         op_tostring,
751         op_tostring,
752         uop_tostring,
753         name_tostring,
754         attr_tostring,
755         cond_tostring,
756         cond_tostring,
757         cond_tostring,
758         cond_tostring,
759         cond_tostring,
760         isnull_tostring,
761 };
763 /*
764  * public API
765  */
767 sdb_store_cond_t *
768 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
770         return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
771                                 name, expr));
772 } /* sdb_store_attr_cond */
774 sdb_store_cond_t *
775 sdb_store_obj_cond(int field, sdb_store_expr_t *expr)
777         return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type,
778                                 field, expr));
779 } /* sdb_store_obj_cond */
781 sdb_store_matcher_t *
782 sdb_store_name_matcher(int type, const char *name, _Bool re)
784         sdb_store_matcher_t *m;
786         if (re)
787                 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
788         else
789                 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
791         if (! m)
792                 return NULL;
794         NAME_M(m)->obj_type = type;
795         return m;
796 } /* sdb_store_name_matcher */
798 sdb_store_matcher_t *
799 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
801         sdb_store_matcher_t *m;
803         if (! name)
804                 return NULL;
806         if (re)
807                 m = M(sdb_object_create("attr-matcher", attr_type,
808                                         name, NULL, value));
809         else
810                 m = M(sdb_object_create("attr-matcher", attr_type,
811                                         name, value, NULL));
812         return m;
813 } /* sdb_store_attr_matcher */
815 sdb_store_matcher_t *
816 sdb_store_lt_matcher(sdb_store_cond_t *cond)
818         return M(sdb_object_create("lt-matcher", cond_type,
819                                 MATCHER_LT, cond));
820 } /* sdb_store_lt_matcher */
822 sdb_store_matcher_t *
823 sdb_store_le_matcher(sdb_store_cond_t *cond)
825         return M(sdb_object_create("le-matcher", cond_type,
826                                 MATCHER_LE, cond));
827 } /* sdb_store_le_matcher */
829 sdb_store_matcher_t *
830 sdb_store_eq_matcher(sdb_store_cond_t *cond)
832         return M(sdb_object_create("eq-matcher", cond_type,
833                                 MATCHER_EQ, cond));
834 } /* sdb_store_eq_matcher */
836 sdb_store_matcher_t *
837 sdb_store_ge_matcher(sdb_store_cond_t *cond)
839         return M(sdb_object_create("ge-matcher", cond_type,
840                                 MATCHER_GE, cond));
841 } /* sdb_store_ge_matcher */
843 sdb_store_matcher_t *
844 sdb_store_gt_matcher(sdb_store_cond_t *cond)
846         return M(sdb_object_create("gt-matcher", cond_type,
847                                 MATCHER_GT, cond));
848 } /* sdb_store_gt_matcher */
850 sdb_store_matcher_t *
851 sdb_store_isnull_matcher(const char *attr_name)
853         return M(sdb_object_create("isnull-matcher", isnull_type,
854                                 MATCHER_ISNULL, attr_name));
855 } /* sdb_store_isnull_matcher */
857 static sdb_store_matcher_t *
858 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
860         sdb_store_matcher_t *tmp;
862         if ((! m) || (! inv))
863                 return m;
865         tmp = sdb_store_inv_matcher(m);
866         /* pass ownership to the inverse matcher */
867         sdb_object_deref(SDB_OBJ(m));
868         return tmp;
869 } /* maybe_inv_matcher */
871 static int
872 parse_cond_op(const char *op,
873                 sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv)
875         *inv = 0;
876         if (! strcasecmp(op, "<"))
877                 *matcher = sdb_store_lt_matcher;
878         else if (! strcasecmp(op, "<="))
879                 *matcher = sdb_store_le_matcher;
880         else if (! strcasecmp(op, "="))
881                 *matcher = sdb_store_eq_matcher;
882         else if (! strcasecmp(op, ">="))
883                 *matcher = sdb_store_ge_matcher;
884         else if (! strcasecmp(op, ">"))
885                 *matcher = sdb_store_gt_matcher;
886         else if (! strcasecmp(op, "!=")) {
887                 *matcher = sdb_store_eq_matcher;
888                 *inv = 1;
889         }
890         else
891                 return -1;
892         return 0;
893 } /* parse_cond_op */
895 static sdb_store_matcher_t *
896 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
898         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
899         sdb_store_matcher_t *m;
900         sdb_store_cond_t *cond;
901         _Bool inv = 0;
903         if (! attr)
904                 return NULL;
906         if (! strcasecmp(op, "IS")) {
907                 if (! expr)
908                         return sdb_store_isnull_matcher(attr);
909                 else
910                         return NULL;
911         }
912         else if (! expr)
913                 return NULL;
914         else if (parse_cond_op(op, &matcher, &inv))
915                 return NULL;
917         cond = sdb_store_attr_cond(attr, expr);
918         if (! cond)
919                 return NULL;
921         m = matcher(cond);
922         /* pass ownership to 'm' or destroy in case of an error */
923         sdb_object_deref(SDB_OBJ(cond));
924         return maybe_inv_matcher(m, inv);
925 } /* parse_attr_cmp */
927 sdb_store_matcher_t *
928 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
929                 const char *op, sdb_store_expr_t *expr)
931         int type = -1;
932         _Bool inv = 0;
933         _Bool re = 0;
935         sdb_data_t value = SDB_DATA_INIT;
936         sdb_store_matcher_t *m = NULL;
938         if (! strcasecmp(obj_type, "host"))
939                 type = SDB_HOST;
940         else if (! strcasecmp(obj_type, "service"))
941                 type = SDB_SERVICE;
942         else if (! strcasecmp(obj_type, "attribute"))
943                 type = SDB_ATTRIBUTE;
944         else
945                 return NULL;
947         /* XXX: this code sucks! */
948         if (! strcasecmp(op, "=")) {
949                 /* nothing to do */
950         }
951         else if (! strcasecmp(op, "!=")) {
952                 inv = 1;
953         }
954         else if (! strcasecmp(op, "=~")) {
955                 re = 1;
956         }
957         else if (! strcasecmp(op, "!~")) {
958                 inv = 1;
959                 re = 1;
960         }
961         else if (type == SDB_ATTRIBUTE)
962                 return parse_attr_cmp(attr, op, expr);
963         else
964                 return NULL;
966         if (! expr)
967                 return NULL;
969         if (sdb_store_expr_eval(expr, NULL, &value))
970                 return NULL;
971         if (value.type != SDB_TYPE_STRING) {
972                 sdb_data_free_datum(&value);
973                 return parse_attr_cmp(attr, op, expr);
974         }
976         if (! attr)
977                 m = sdb_store_name_matcher(type, value.data.string, re);
978         else if (type == SDB_ATTRIBUTE)
979                 m = sdb_store_attr_matcher(attr, value.data.string, re);
981         sdb_data_free_datum(&value);
982         return maybe_inv_matcher(m, inv);
983 } /* sdb_store_matcher_parse_cmp */
985 sdb_store_matcher_t *
986 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
987                 sdb_store_expr_t *expr)
989         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
990         sdb_store_matcher_t *m;
991         sdb_store_cond_t *cond;
992         _Bool inv = 0;
994         int field;
996         if (! expr)
997                 return NULL;
999         if (! strcasecmp(name, "last_update"))
1000                 field = SDB_FIELD_LAST_UPDATE;
1001         else if (! strcasecmp(name, "age"))
1002                 field = SDB_FIELD_AGE;
1003         else if (! strcasecmp(name, "interval"))
1004                 field = SDB_FIELD_INTERVAL;
1005         else if (! strcasecmp(name, "backend"))
1006                 field = SDB_FIELD_BACKEND;
1007         else
1008                 return NULL;
1010         if (parse_cond_op(op, &matcher, &inv))
1011                 return NULL;
1013         cond = sdb_store_obj_cond(field, expr);
1014         if (! cond)
1015                 return NULL;
1017         assert(matcher);
1018         m = matcher(cond);
1019         /* pass ownership to 'm' or destroy in case of an error */
1020         sdb_object_deref(SDB_OBJ(cond));
1021         return maybe_inv_matcher(m, inv);
1022 } /* sdb_store_matcher_parse_field_cmp */
1024 sdb_store_matcher_t *
1025 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1027         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
1028                                 left, right));
1029 } /* sdb_store_dis_matcher */
1031 sdb_store_matcher_t *
1032 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1034         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
1035                                 left, right));
1036 } /* sdb_store_con_matcher */
1038 sdb_store_matcher_t *
1039 sdb_store_inv_matcher(sdb_store_matcher_t *m)
1041         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
1042 } /* sdb_store_inv_matcher */
1044 int
1045 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
1046                 sdb_store_matcher_t *filter)
1048         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
1049                 return 0;
1051         /* "NULL" always matches */
1052         if ((! m) || (! obj))
1053                 return 1;
1055         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1056                 return 0;
1058         return matchers[m->type](m, obj, filter);
1059 } /* sdb_store_matcher_matches */
1061 char *
1062 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1064         if (! m)
1065                 return NULL;
1067         if ((m->type < 0)
1068                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
1069                 return NULL;
1070         return matchers_tostring[m->type](m, buf, buflen);
1071 } /* sdb_store_matcher_tostring */
1073 int
1074 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1075                 sdb_store_lookup_cb cb, void *user_data)
1077         scan_iter_data_t data = { m, filter, cb, user_data };
1079         if (! cb)
1080                 return -1;
1081         return sdb_store_iterate(scan_iter, &data);
1082 } /* sdb_store_scan */
1084 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */