Code

store: Fixed a typo in the type check of the child matcher.
[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 /*
78  * matcher implementations
79  */
81 static int
82 match_string(string_matcher_t *m, const char *name)
83 {
84         if ((! m->name) && (! m->name_re))
85                 return 1;
87         if (! name)
88                 name = "";
90         if (m->name && strcasecmp(m->name, name))
91                 return 0;
92         if (m->name_re && regexec(m->name_re, name,
93                                         /* matches */ 0, NULL, /* flags = */ 0))
94                 return 0;
95         return 1;
96 } /* match_string */
98 static int
99 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
100                 sdb_store_matcher_t *filter)
102         int status;
104         assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
105         assert(OP_M(m)->left && OP_M(m)->right);
107         status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
109         /* lazy evaluation */
110         if ((! status) && (m->type == MATCHER_AND))
111                 return status;
112         else if (status && (m->type == MATCHER_OR))
113                 return status;
115         return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
116 } /* match_logical */
118 static int
119 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
120                 sdb_store_matcher_t *filter)
122         assert(m->type == MATCHER_NOT);
123         assert(UOP_M(m)->op);
125         return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
126 } /* match_unary */
128 static int
129 match_name(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
130                 sdb_store_matcher_t *filter)
132         sdb_avltree_iter_t *iter = NULL;
133         int status = 0;
135         assert(m->type == MATCHER_NAME);
137         if (obj->type == NAME_M(m)->obj_type)
138                 return match_string(&NAME_M(m)->name, SDB_OBJ(obj)->name);
139         else if (obj->type != SDB_HOST)
140                 return 0;
142         switch (NAME_M(m)->obj_type) {
143                 case SDB_SERVICE:
144                         iter = sdb_avltree_get_iter(HOST(obj)->services);
145                         break;
146                 case SDB_METRIC:
147                         iter = sdb_avltree_get_iter(HOST(obj)->metrics);
148                         break;
149                 case SDB_ATTRIBUTE:
150                         iter = sdb_avltree_get_iter(HOST(obj)->attributes);
151                         break;
152         }
154         while (sdb_avltree_iter_has_next(iter)) {
155                 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
156                 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
157                                                 NULL)))
158                         continue;
159                 if (match_string(&NAME_M(m)->name, child->name)) {
160                         status = 1;
161                         break;
162                 }
163         }
164         sdb_avltree_iter_destroy(iter);
165         return status;
166 } /* match_name */
168 static int
169 match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
170                 sdb_store_matcher_t *filter)
172         sdb_avltree_iter_t *iter = NULL;
173         int status = 0;
175         assert((m->type == MATCHER_SERVICE)
176                         || (m->type == MATCHER_METRIC)
177                         || (m->type == MATCHER_ATTRIBUTE));
179         /* TODO: support all object types */
180         if (obj->type != SDB_HOST)
181                 return 0;
183         if (m->type == MATCHER_SERVICE)
184                 iter = sdb_avltree_get_iter(HOST(obj)->services);
185         else if (m->type == MATCHER_METRIC)
186                 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
187         else if (m->type == MATCHER_ATTRIBUTE)
188                 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
190         while (sdb_avltree_iter_has_next(iter)) {
191                 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
192                 if (filter && (! sdb_store_matcher_matches(filter,
193                                                 STORE_OBJ(child), NULL)))
194                         continue;
196                 if (sdb_store_matcher_matches(CHILD_M(m)->m, obj, filter)) {
197                         status = 1;
198                         break;
199                 }
200         }
201         sdb_avltree_iter_destroy(iter);
202         return status;
203 } /* match_child */
205 /*
206  * cmp_expr:
207  * Compare the values of two expressions when evaluating them using the
208  * specified stored object and filter. Returns a value less than, equal to, or
209  * greater than zero if the value of the first expression compares less than,
210  * equal to, or greater than the value of the second expression. Returns
211  * INT_MAX if any of the expressions could not be evaluated or if any of them
212  * evaluated to NULL.
213  */
214 static int
215 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
216                 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
218         sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
219         int status;
221         if (sdb_store_expr_eval(e1, obj, &v1, filter))
222                 return INT_MAX;
223         if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
224                 sdb_data_free_datum(&v1);
225                 return INT_MAX;
226         }
228         if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
229                 status = INT_MAX;
230         else if (v1.type == v2.type)
231                 status = sdb_data_cmp(&v1, &v2);
232         else
233                 status = sdb_data_strcmp(&v1, &v2);
235         sdb_data_free_datum(&v1);
236         sdb_data_free_datum(&v2);
237         return status;
238 } /* cmp_expr */
240 static int
241 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
242                 sdb_store_matcher_t *filter)
244         int status;
245         assert(m->type == MATCHER_LT);
246         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
247         return (status != INT_MAX) && (status < 0);
248 } /* match_lt */
250 static int
251 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
252                 sdb_store_matcher_t *filter)
254         int status;
255         assert(m->type == MATCHER_LE);
256         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
257         return (status != INT_MAX) && (status <= 0);
258 } /* match_le */
260 static int
261 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
262                 sdb_store_matcher_t *filter)
264         int status;
265         assert(m->type == MATCHER_EQ);
266         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
267         return (status != INT_MAX) && (! status);
268 } /* match_eq */
270 static int
271 match_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
272                 sdb_store_matcher_t *filter)
274         int status;
275         assert(m->type == MATCHER_NE);
276         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
277         return (status != INT_MAX) && status;
278 } /* match_ne */
280 static int
281 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
282                 sdb_store_matcher_t *filter)
284         int status;
285         assert(m->type == MATCHER_GE);
286         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
287         return (status != INT_MAX) && (status >= 0);
288 } /* match_ge */
290 static int
291 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
292                 sdb_store_matcher_t *filter)
294         int status;
295         assert(m->type == MATCHER_GT);
296         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
297         return (status != INT_MAX) && (status > 0);
298 } /* match_gt */
300 static int
301 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
302                 sdb_store_matcher_t *filter)
304         sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
305         int status = 1;
307         assert(m->type == MATCHER_IN);
309         if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
310                         || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
311                 status = 0;
313         if (status)
314                 status = sdb_data_inarray(&value, &array);
316         sdb_data_free_datum(&value);
317         sdb_data_free_datum(&array);
318         return status;
319 } /* match_in */
321 static int
322 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
323                 sdb_store_matcher_t *filter)
325         sdb_data_t v = SDB_DATA_INIT;
326         int status = 0;
328         regex_t regex;
329         _Bool free_regex = 0;
331         assert((m->type == MATCHER_REGEX)
332                         || (m->type == MATCHER_NREGEX));
334         if (! CMP_M(m)->right->type) {
335                 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
336                 regex = CMP_M(m)->right->data.data.re.regex;
337         }
338         else {
339                 sdb_data_t tmp = SDB_DATA_INIT;
340                 char *raw;
342                 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
343                         return 0;
345                 if (tmp.type != SDB_TYPE_STRING) {
346                         sdb_data_free_datum(&tmp);
347                         return 0;
348                 }
350                 raw = tmp.data.string;
351                 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
352                         free(raw);
353                         return 0;
354                 }
356                 regex = tmp.data.re.regex;
357                 free_regex = 1;
358                 free(tmp.data.re.raw);
359                 free(raw);
360         }
362         if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
363                         || (sdb_data_isnull(&v)))
364                 status = 0;
365         else {
366                 char value[sdb_data_strlen(&v) + 1];
367                 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
368                         status = 0;
369                 else if (! regexec(&regex, value, 0, NULL, 0))
370                         status = 1;
371         }
373         if (free_regex)
374                 regfree(&regex);
375         sdb_data_free_datum(&v);
376         if (m->type == MATCHER_NREGEX)
377                 return !status;
378         return status;
379 } /* match_regex */
381 static int
382 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
383                 sdb_store_matcher_t *filter)
385         sdb_data_t v = SDB_DATA_INIT;
386         int status;
388         assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
390         /* TODO: this might hide real errors;
391          * improve error reporting and propagation */
392         if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
393                         || sdb_data_isnull(&v))
394                 status = 1;
395         else
396                 status = 0;
398         sdb_data_free_datum(&v);
399         if (m->type == MATCHER_ISNNULL)
400                 return !status;
401         return status;
402 } /* match_isnull */
404 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
405                 sdb_store_matcher_t *);
407 /* this array needs to be indexable by the matcher types;
408  * -> update the enum in store-private.h when updating this */
409 static matcher_cb
410 matchers[] = {
411         match_logical,
412         match_logical,
413         match_unary,
414         match_name,
415         match_child,
416         match_child,
417         match_child,
418         match_lt,
419         match_le,
420         match_eq,
421         match_ne,
422         match_ge,
423         match_gt,
424         match_in,
425         match_regex,
426         match_regex,
427         match_isnull,
428         match_isnull,
429 };
431 /*
432  * private matcher types
433  */
435 /* initializes a string matcher consuming two elements from ap */
436 static int
437 string_matcher_init(string_matcher_t *m, va_list ap)
439         const char *name = va_arg(ap, const char *);
440         const char *name_re = va_arg(ap, const char *);
442         if (name) {
443                 m->name = strdup(name);
444                 if (! m->name)
445                         return -1;
446         }
447         if (name_re) {
448                 m->name_re = malloc(sizeof(*m->name_re));
449                 if (! m->name_re)
450                         return -1;
451                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
452                         return -1;
453         }
454         return 0;
455 } /* string_matcher_init */
457 static void
458 string_matcher_destroy(string_matcher_t *m)
460         if (m->name)
461                 free(m->name);
462         if (m->name_re) {
463                 regfree(m->name_re);
464                 free(m->name_re);
465         }
466 } /* string_matcher_destroy */
468 /* initializes a name matcher */
469 static int
470 name_matcher_init(sdb_object_t *obj, va_list ap)
472         name_matcher_t *m = NAME_M(obj);
473         M(obj)->type = MATCHER_NAME;
474         return string_matcher_init(&m->name, ap);
475 } /* name_matcher_init */
477 static void
478 name_matcher_destroy(sdb_object_t *obj)
480         name_matcher_t *m = NAME_M(obj);
481         string_matcher_destroy(&m->name);
482 } /* name_matcher_destroy */
484 static int
485 op_matcher_init(sdb_object_t *obj, va_list ap)
487         M(obj)->type = va_arg(ap, int);
488         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
489                 return -1;
491         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
492         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
493         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
494         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
496         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
497                 return -1;
498         return 0;
499 } /* op_matcher_init */
501 static void
502 op_matcher_destroy(sdb_object_t *obj)
504         if (OP_M(obj)->left)
505                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
506         if (OP_M(obj)->right)
507                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
508 } /* op_matcher_destroy */
510 static int
511 child_matcher_init(sdb_object_t *obj, va_list ap)
513         M(obj)->type = va_arg(ap, int);
514         CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
516         if (! CHILD_M(obj)->m)
517                 return -1;
519         sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
520         return 0;
521 } /* child_matcher_init */
523 static void
524 child_matcher_destroy(sdb_object_t *obj)
526         sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
527 } /* child_matcher_destroy */
529 static int
530 cmp_matcher_init(sdb_object_t *obj, va_list ap)
532         M(obj)->type = va_arg(ap, int);
534         CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
535         sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
536         CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
537         sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
539         if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
540                 return -1;
541         return 0;
542 } /* cmp_matcher_init */
544 static void
545 cmp_matcher_destroy(sdb_object_t *obj)
547         sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
548         sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
549 } /* cmp_matcher_destroy */
551 static int
552 uop_matcher_init(sdb_object_t *obj, va_list ap)
554         M(obj)->type = va_arg(ap, int);
555         if (M(obj)->type != MATCHER_NOT)
556                 return -1;
558         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
559         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
561         if (! UOP_M(obj)->op)
562                 return -1;
563         return 0;
564 } /* uop_matcher_init */
566 static void
567 uop_matcher_destroy(sdb_object_t *obj)
569         if (UOP_M(obj)->op)
570                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
571 } /* uop_matcher_destroy */
573 static int
574 isnull_matcher_init(sdb_object_t *obj, va_list ap)
576         M(obj)->type = va_arg(ap, int);
577         if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
578                 return -1;
580         ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
581         sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
582         return 0;
583 } /* isnull_matcher_init */
585 static void
586 isnull_matcher_destroy(sdb_object_t *obj)
588         sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
589         ISNULL_M(obj)->expr = NULL;
590 } /* isnull_matcher_destroy */
592 static sdb_type_t name_type = {
593         /* size = */ sizeof(name_matcher_t),
594         /* init = */ name_matcher_init,
595         /* destroy = */ name_matcher_destroy,
596 };
598 static sdb_type_t op_type = {
599         /* size = */ sizeof(op_matcher_t),
600         /* init = */ op_matcher_init,
601         /* destroy = */ op_matcher_destroy,
602 };
604 static sdb_type_t uop_type = {
605         /* size = */ sizeof(uop_matcher_t),
606         /* init = */ uop_matcher_init,
607         /* destroy = */ uop_matcher_destroy,
608 };
610 static sdb_type_t child_type = {
611         /* size = */ sizeof(child_matcher_t),
612         /* init = */ child_matcher_init,
613         /* destroy = */ child_matcher_destroy,
614 };
616 static sdb_type_t cmp_type = {
617         /* size = */ sizeof(cmp_matcher_t),
618         /* init = */ cmp_matcher_init,
619         /* destroy = */ cmp_matcher_destroy,
620 };
622 static sdb_type_t isnull_type = {
623         /* size = */ sizeof(isnull_matcher_t),
624         /* init = */ isnull_matcher_init,
625         /* destroy = */ isnull_matcher_destroy,
626 };
628 /*
629  * public API
630  */
632 sdb_store_matcher_t *
633 sdb_store_name_matcher(int type, const char *name, _Bool re)
635         sdb_store_matcher_t *m;
637         if (re)
638                 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
639         else
640                 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
642         if (! m)
643                 return NULL;
645         NAME_M(m)->obj_type = type;
646         return m;
647 } /* sdb_store_name_matcher */
649 sdb_store_matcher_t *
650 sdb_store_child_matcher(int type, sdb_store_matcher_t *m)
652         if (type == SDB_SERVICE)
653                 type = MATCHER_SERVICE;
654         else if (type == SDB_METRIC)
655                 type = MATCHER_METRIC;
656         else if (type == SDB_ATTRIBUTE)
657                 type = MATCHER_ATTRIBUTE;
658         else
659                 return NULL;
660         return M(sdb_object_create("any-matcher", child_type, type, m));
661 } /* sdb_store_child_matcher */
663 sdb_store_matcher_t *
664 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
666         return M(sdb_object_create("lt-matcher", cmp_type,
667                                 MATCHER_LT, left, right));
668 } /* sdb_store_lt_matcher */
670 sdb_store_matcher_t *
671 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
673         return M(sdb_object_create("le-matcher", cmp_type,
674                                 MATCHER_LE, left, right));
675 } /* sdb_store_le_matcher */
677 sdb_store_matcher_t *
678 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
680         return M(sdb_object_create("eq-matcher", cmp_type,
681                                 MATCHER_EQ, left, right));
682 } /* sdb_store_eq_matcher */
684 sdb_store_matcher_t *
685 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
687         return M(sdb_object_create("ne-matcher", cmp_type,
688                                 MATCHER_NE, left, right));
689 } /* sdb_store_ne_matcher */
691 sdb_store_matcher_t *
692 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
694         return M(sdb_object_create("ge-matcher", cmp_type,
695                                 MATCHER_GE, left, right));
696 } /* sdb_store_ge_matcher */
698 sdb_store_matcher_t *
699 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
701         return M(sdb_object_create("gt-matcher", cmp_type,
702                                 MATCHER_GT, left, right));
703 } /* sdb_store_gt_matcher */
705 sdb_store_matcher_t *
706 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
708         return M(sdb_object_create("in-matcher", cmp_type,
709                                 MATCHER_IN, left, right));
710 } /* sdb_store_in_matcher */
712 sdb_store_matcher_t *
713 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
715         if (! right->type) {
716                 if ((right->data.type != SDB_TYPE_STRING)
717                                 && (right->data.type != SDB_TYPE_REGEX))
718                         return NULL;
720                 if (right->data.type == SDB_TYPE_STRING) {
721                         char *raw = right->data.data.string;
722                         if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
723                                 return NULL;
724                         free(raw);
725                 }
726         }
727         return M(sdb_object_create("regex-matcher", cmp_type,
728                                 MATCHER_REGEX, left, right));
729 } /* sdb_store_regex_matcher */
731 sdb_store_matcher_t *
732 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
734         sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
735         if (! m)
736                 return NULL;
737         m->type = MATCHER_NREGEX;
738         return m;
739 } /* sdb_store_nregex_matcher */
741 sdb_store_matcher_t *
742 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
744         return M(sdb_object_create("isnull-matcher", isnull_type,
745                                 MATCHER_ISNULL, expr));
746 } /* sdb_store_isnull_matcher */
748 sdb_store_matcher_t *
749 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
751         return M(sdb_object_create("isnull-matcher", isnull_type,
752                                 MATCHER_ISNNULL, expr));
753 } /* sdb_store_isnnull_matcher */
755 sdb_store_matcher_op_cb
756 sdb_store_parse_matcher_op(const char *op)
758         if (! strcasecmp(op, "<"))
759                 return sdb_store_lt_matcher;
760         else if (! strcasecmp(op, "<="))
761                 return sdb_store_le_matcher;
762         else if (! strcasecmp(op, "="))
763                 return sdb_store_eq_matcher;
764         else if (! strcasecmp(op, "!="))
765                 return sdb_store_ne_matcher;
766         else if (! strcasecmp(op, ">="))
767                 return sdb_store_ge_matcher;
768         else if (! strcasecmp(op, ">"))
769                 return sdb_store_gt_matcher;
770         else if (! strcasecmp(op, "=~"))
771                 return sdb_store_regex_matcher;
772         else if (! strcasecmp(op, "!~"))
773                 return sdb_store_nregex_matcher;
774         return NULL;
775 } /* sdb_store_parse_matcher_op */
777 int
778 sdb_store_parse_object_type_plural(const char *name)
780         if (! strcasecmp(name, "hosts"))
781                 return SDB_HOST;
782         else if (! strcasecmp(name, "services"))
783                 return SDB_SERVICE;
784         else if (! strcasecmp(name, "metrics"))
785                 return SDB_METRIC;
786         return -1;
787 } /* sdb_store_parse_object_type_plural */
789 int
790 sdb_store_parse_field_name(const char *name)
792         if (! strcasecmp(name, "name"))
793                 return SDB_FIELD_NAME;
794         else if (! strcasecmp(name, "last_update"))
795                 return SDB_FIELD_LAST_UPDATE;
796         else if (! strcasecmp(name, "age"))
797                 return SDB_FIELD_AGE;
798         else if (! strcasecmp(name, "interval"))
799                 return SDB_FIELD_INTERVAL;
800         else if (! strcasecmp(name, "backend"))
801                 return SDB_FIELD_BACKEND;
802         return -1;
803 } /* sdb_store_parse_field_name */
805 static sdb_store_matcher_t *
806 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
808         sdb_store_matcher_t *tmp;
810         if ((! m) || (! inv))
811                 return m;
813         tmp = sdb_store_inv_matcher(m);
814         /* pass ownership to the inverse matcher */
815         sdb_object_deref(SDB_OBJ(m));
816         return tmp;
817 } /* maybe_inv_matcher */
819 sdb_store_matcher_t *
820 sdb_store_matcher_parse_cmp(const char *obj_type,
821                 const char *op, sdb_store_expr_t *expr)
823         int type = -1;
824         _Bool inv = 0;
825         _Bool re = 0;
827         sdb_data_t value = SDB_DATA_INIT;
828         sdb_store_matcher_t *m = NULL;
830         if (! strcasecmp(obj_type, "host"))
831                 type = SDB_HOST;
832         else if (! strcasecmp(obj_type, "service"))
833                 type = SDB_SERVICE;
834         else if (! strcasecmp(obj_type, "metric"))
835                 type = SDB_METRIC;
836         else if (! strcasecmp(obj_type, "attribute"))
837                 type = SDB_ATTRIBUTE;
838         else
839                 return NULL;
841         /* XXX: this code sucks! */
842         if (! strcasecmp(op, "=")) {
843                 /* nothing to do */
844         }
845         else if (! strcasecmp(op, "!=")) {
846                 inv = 1;
847         }
848         else if (! strcasecmp(op, "=~")) {
849                 re = 1;
850         }
851         else if (! strcasecmp(op, "!~")) {
852                 inv = 1;
853                 re = 1;
854         }
855         else
856                 return NULL;
858         if (! expr)
859                 return NULL;
861         if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
862                         || (value.type != SDB_TYPE_STRING)) {
863                 sdb_data_free_datum(&value);
864                 return NULL;
865         }
867         m = sdb_store_name_matcher(type, value.data.string, re);
868         sdb_data_free_datum(&value);
869         return maybe_inv_matcher(m, inv);
870 } /* sdb_store_matcher_parse_cmp */
872 sdb_store_matcher_t *
873 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
875         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
876                                 left, right));
877 } /* sdb_store_dis_matcher */
879 sdb_store_matcher_t *
880 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
882         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
883                                 left, right));
884 } /* sdb_store_con_matcher */
886 sdb_store_matcher_t *
887 sdb_store_inv_matcher(sdb_store_matcher_t *m)
889         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
890 } /* sdb_store_inv_matcher */
892 int
893 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
894                 sdb_store_matcher_t *filter)
896         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
897                 return 0;
899         /* "NULL" always matches */
900         if ((! m) || (! obj))
901                 return 1;
903         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
904                 return 0;
906         return matchers[m->type](m, obj, filter);
907 } /* sdb_store_matcher_matches */
909 int
910 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
911                 sdb_store_lookup_cb cb, void *user_data)
913         scan_iter_data_t data = { m, filter, cb, user_data };
915         if (! cb)
916                 return -1;
917         return sdb_store_iterate(scan_iter, &data);
918 } /* sdb_store_scan */
920 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */