Code

9192618dd069442517da83e1461aae06d49e8031
[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"
41 #include "utils/error.h"
43 #include <assert.h>
45 #include <sys/types.h>
46 #include <regex.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include <limits.h>
53 static int
54 expr_eval2(sdb_store_expr_t *e1, sdb_data_t *v1,
55                 sdb_store_expr_t *e2, sdb_data_t *v2,
56                 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
57 {
58         if (e1->type) {
59                 if (sdb_store_expr_eval(e1, obj, v1, filter))
60                         return -1;
61         }
62         else
63                 *v1 = e1->data;
64         if (e2->type) {
65                 if (sdb_store_expr_eval(e2, obj, v2, filter)) {
66                         if (e1->type)
67                                 sdb_data_free_datum(v1);
68                         return -1;
69                 }
70         }
71         else
72                 *v2 = e2->data;
73         return 0;
74 } /* expr_eval2 */
76 static void
77 expr_free_datum2(sdb_store_expr_t *e1, sdb_data_t *v1,
78                 sdb_store_expr_t *e2, sdb_data_t *v2)
79 {
80         if (e1->type)
81                 sdb_data_free_datum(v1);
82         if (e2->type)
83                 sdb_data_free_datum(v2);
84 } /* expr_free_datum2 */
86 /*
87  * matcher implementations
88  */
90 /*
91  * cmp_expr:
92  * Compare two values using the specified matcher operator. If strcmp_fallback
93  * is enabled, compare the string values in case of a type mismatch.
94  */
95 static int
96 match_cmp_value(int op, sdb_data_t *v1, sdb_data_t *v2, bool strcmp_fallback)
97 {
98         int status;
100         if (sdb_data_isnull(v1) || (sdb_data_isnull(v2)))
101                 status = INT_MAX;
102         else if (v1->type == v2->type)
103                 status = sdb_data_cmp(v1, v2);
104         else if (! strcmp_fallback)
105                 status = INT_MAX;
106         else
107                 status = sdb_data_strcmp(v1, v2);
109         if (status == INT_MAX)
110                 return 0;
111         switch (op) {
112                 case MATCHER_LT: return status < 0;
113                 case MATCHER_LE: return status <= 0;
114                 case MATCHER_EQ: return status == 0;
115                 case MATCHER_NE: return status != 0;
116                 case MATCHER_GE: return status >= 0;
117                 case MATCHER_GT: return status > 0;
118         }
119         return 0;
120 } /* match_cmp_value */
122 static int
123 match_regex_value(int op, sdb_data_t *v, sdb_data_t *re)
125         char value[sdb_data_strlen(v) + 1];
126         int status = 0;
128         assert((op == MATCHER_REGEX)
129                         || (op == MATCHER_NREGEX));
131         if (sdb_data_isnull(v) || sdb_data_isnull(re))
132                 return 0;
134         if (re->type == SDB_TYPE_STRING) {
135                 sdb_data_t tmp = SDB_DATA_INIT;
137                 if (sdb_data_parse(re->data.string, SDB_TYPE_REGEX, &tmp))
138                         return 0;
140                 sdb_data_free_datum(re);
141                 *re = tmp;
142         }
143         else if (re->type != SDB_TYPE_REGEX)
144                 return 0;
146         if (! sdb_data_format(v, value, sizeof(value), SDB_UNQUOTED))
147                 status = 0;
148         else if (! regexec(&re->data.re.regex, value, 0, NULL, 0))
149                 status = 1;
151         if (op == MATCHER_NREGEX)
152                 return !status;
153         return status;
154 } /* match_regex_value */
156 static int
157 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
158                 sdb_store_matcher_t *filter)
160         int status;
162         assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
163         assert(OP_M(m)->left && OP_M(m)->right);
165         status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
167         /* lazy evaluation */
168         if ((! status) && (m->type == MATCHER_AND))
169                 return status;
170         else if (status && (m->type == MATCHER_OR))
171                 return status;
173         return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
174 } /* match_logical */
176 static int
177 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
178                 sdb_store_matcher_t *filter)
180         assert(m->type == MATCHER_NOT);
181         assert(UOP_M(m)->op);
183         return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
184 } /* match_unary */
186 /* iterate: ANY/ALL <iter> <cmp> <value> */
187 static int
188 match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
189                 sdb_store_matcher_t *filter)
191         sdb_store_expr_iter_t *iter = NULL;
192         int status;
193         int all = (int)(m->type == MATCHER_ALL);
195         assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL));
196         assert((! CMP_M(ITER_M(m)->m)->left) && CMP_M(ITER_M(m)->m)->right);
198         iter = sdb_store_expr_iter(ITER_M(m)->iter, obj, filter);
199         if (! iter)
200                 return 0;
202         status = all;
203         while (sdb_store_expr_iter_has_next(iter)) {
204                 sdb_data_t v = sdb_store_expr_iter_get_next(iter);
205                 sdb_store_expr_t expr = CONST_EXPR(v);
206                 bool matches;
208                 CMP_M(ITER_M(m)->m)->left = &expr;
209                 matches = sdb_store_matcher_matches(ITER_M(m)->m, obj, filter);
210                 CMP_M(ITER_M(m)->m)->left = NULL;
211                 sdb_data_free_datum(&v);
213                 if (matches) {
214                         if (! all) {
215                                 status = 1;
216                                 break;
217                         }
218                 } else if (all) {
219                         status = 0;
220                         break;
221                 }
222         }
223         sdb_store_expr_iter_destroy(iter);
224         return status;
225 } /* match_iter */
227 static int
228 match_cmp(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
229                 sdb_store_matcher_t *filter)
231         sdb_store_expr_t *e1 = CMP_M(m)->left;
232         sdb_store_expr_t *e2 = CMP_M(m)->right;
233         sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
234         int status;
236         assert((m->type == MATCHER_LT)
237                         || (m->type == MATCHER_LE)
238                         || (m->type == MATCHER_EQ)
239                         || (m->type == MATCHER_NE)
240                         || (m->type == MATCHER_GE)
241                         || (m->type == MATCHER_GT));
242         assert(e1 && e2);
244         if (expr_eval2(e1, &v1, e2, &v2, obj, filter))
245                 return 0;
247         status = match_cmp_value(m->type, &v1, &v2,
248                         (e1->data_type) < 0 || (e2->data_type < 0));
250         expr_free_datum2(e1, &v1, e2, &v2);
251         return status;
252 } /* match_cmp */
254 static int
255 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
256                 sdb_store_matcher_t *filter)
258         sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
259         int status = 1;
261         assert((m->type == MATCHER_IN) || (m->type == MATCHER_NIN));
262         assert(CMP_M(m)->left && CMP_M(m)->right);
264         if (expr_eval2(CMP_M(m)->left, &value,
265                                 CMP_M(m)->right, &array, obj, filter))
266                 status = 0;
268         if (status)
269                 status = sdb_data_inarray(&value, &array);
271         expr_free_datum2(CMP_M(m)->left, &value, CMP_M(m)->right, &array);
272         if (m->type == MATCHER_NIN)
273                 return !status;
274         return status;
275 } /* match_in */
277 static int
278 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
279                 sdb_store_matcher_t *filter)
281         sdb_data_t regex = SDB_DATA_INIT, v = SDB_DATA_INIT;
282         int status = 0;
284         assert((m->type == MATCHER_REGEX)
285                         || (m->type == MATCHER_NREGEX));
286         assert(CMP_M(m)->left && CMP_M(m)->right);
288         if (expr_eval2(CMP_M(m)->left, &v, CMP_M(m)->right, &regex, obj, filter))
289                 return 0;
291         status = match_regex_value(m->type, &v, &regex);
293         expr_free_datum2(CMP_M(m)->left, &v, CMP_M(m)->right, &regex);
294         return status;
295 } /* match_regex */
297 static int
298 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
299                 sdb_store_matcher_t *filter)
301         sdb_data_t v = SDB_DATA_INIT;
302         int status;
304         assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
306         if (ISNULL_M(m)->expr->type) {
307                 /* TODO: this might hide real errors;
308                  * improve error reporting and propagation */
309                 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter))
310                         return 1;
311         }
312         else
313                 v = ISNULL_M(m)->expr->data;
315         if (sdb_data_isnull(&v))
316                 status = 1;
317         else
318                 status = 0;
320         if (ISNULL_M(m)->expr->type)
321                 sdb_data_free_datum(&v);
322         if (m->type == MATCHER_ISNNULL)
323                 return !status;
324         return status;
325 } /* match_isnull */
327 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
328                 sdb_store_matcher_t *);
330 /* this array needs to be indexable by the matcher types;
331  * -> update the enum in store-private.h when updating this */
332 static matcher_cb
333 matchers[] = {
334         match_logical,
335         match_logical,
336         match_unary,
337         match_iter,
338         match_iter,
339         match_in,
340         match_in,
342         /* unary operators */
343         match_isnull,
344         match_isnull,
346         /* ary operators */
347         match_cmp,
348         match_cmp,
349         match_cmp,
350         match_cmp,
351         match_cmp,
352         match_cmp,
353         match_regex,
354         match_regex,
355 };
357 /*
358  * private matcher types
359  */
361 static int
362 op_matcher_init(sdb_object_t *obj, va_list ap)
364         M(obj)->type = va_arg(ap, int);
365         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
366                 return -1;
368         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
369         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
370         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
371         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
373         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
374                 return -1;
375         return 0;
376 } /* op_matcher_init */
378 static void
379 op_matcher_destroy(sdb_object_t *obj)
381         if (OP_M(obj)->left)
382                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
383         if (OP_M(obj)->right)
384                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
385 } /* op_matcher_destroy */
387 static int
388 iter_matcher_init(sdb_object_t *obj, va_list ap)
390         M(obj)->type = va_arg(ap, int);
391         ITER_M(obj)->iter = va_arg(ap, sdb_store_expr_t *);
392         ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
394         sdb_object_ref(SDB_OBJ(ITER_M(obj)->iter));
395         sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
397         if ((! ITER_M(obj)->iter) || (! ITER_M(obj)->m))
398                 return -1;
399         return 0;
400 } /* iter_matcher_init */
402 static void
403 iter_matcher_destroy(sdb_object_t *obj)
405         sdb_object_deref(SDB_OBJ(ITER_M(obj)->iter));
406         sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
407 } /* iter_matcher_destroy */
409 static int
410 cmp_matcher_init(sdb_object_t *obj, va_list ap)
412         M(obj)->type = va_arg(ap, int);
414         CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
415         sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
416         CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
417         sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
419         if (! CMP_M(obj)->right)
420                 return -1;
421         return 0;
422 } /* cmp_matcher_init */
424 static void
425 cmp_matcher_destroy(sdb_object_t *obj)
427         sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
428         sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
429 } /* cmp_matcher_destroy */
431 static int
432 uop_matcher_init(sdb_object_t *obj, va_list ap)
434         M(obj)->type = va_arg(ap, int);
435         if (M(obj)->type != MATCHER_NOT)
436                 return -1;
438         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
439         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
441         if (! UOP_M(obj)->op)
442                 return -1;
443         return 0;
444 } /* uop_matcher_init */
446 static void
447 uop_matcher_destroy(sdb_object_t *obj)
449         if (UOP_M(obj)->op)
450                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
451 } /* uop_matcher_destroy */
453 static int
454 isnull_matcher_init(sdb_object_t *obj, va_list ap)
456         M(obj)->type = va_arg(ap, int);
457         if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
458                 return -1;
460         ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
461         sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
462         return 0;
463 } /* isnull_matcher_init */
465 static void
466 isnull_matcher_destroy(sdb_object_t *obj)
468         sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
469         ISNULL_M(obj)->expr = NULL;
470 } /* isnull_matcher_destroy */
472 static sdb_type_t op_type = {
473         /* size = */ sizeof(op_matcher_t),
474         /* init = */ op_matcher_init,
475         /* destroy = */ op_matcher_destroy,
476 };
478 static sdb_type_t uop_type = {
479         /* size = */ sizeof(uop_matcher_t),
480         /* init = */ uop_matcher_init,
481         /* destroy = */ uop_matcher_destroy,
482 };
484 static sdb_type_t iter_type = {
485         /* size = */ sizeof(iter_matcher_t),
486         /* init = */ iter_matcher_init,
487         /* destroy = */ iter_matcher_destroy,
488 };
490 static sdb_type_t cmp_type = {
491         /* size = */ sizeof(cmp_matcher_t),
492         /* init = */ cmp_matcher_init,
493         /* destroy = */ cmp_matcher_destroy,
494 };
496 static sdb_type_t isnull_type = {
497         /* size = */ sizeof(isnull_matcher_t),
498         /* init = */ isnull_matcher_init,
499         /* destroy = */ isnull_matcher_destroy,
500 };
502 /*
503  * public API
504  */
506 sdb_store_matcher_t *
507 sdb_store_any_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m)
509         if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) {
510                 sdb_log(SDB_LOG_ERR, "store: Invalid ANY -> %s matcher "
511                                 "(invalid operator)", MATCHER_SYM(m->type));
512                 return NULL;
513         }
514         if (CMP_M(m)->left) {
515                 sdb_log(SDB_LOG_ERR, "store: Invalid ANY %s %s %s matcher "
516                                 "(invalid left operand)",
517                                 SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type),
518                                 MATCHER_SYM(m->type),
519                                 SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type));
520                 return NULL;
521         }
522         return M(sdb_object_create("any-matcher", iter_type,
523                                 MATCHER_ANY, iter, m));
524 } /* sdb_store_any_matcher */
526 sdb_store_matcher_t *
527 sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m)
529         if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) {
530                 sdb_log(SDB_LOG_ERR, "store: Invalid ALL -> %s matcher "
531                                 "(invalid operator)", MATCHER_SYM(m->type));
532                 return NULL;
533         }
534         if (CMP_M(m)->left) {
535                 sdb_log(SDB_LOG_ERR, "store: Invalid ALL %s %s %s matcher "
536                                 "(invalid left operand)",
537                                 SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type),
538                                 MATCHER_SYM(m->type),
539                                 SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type));
540                 return NULL;
541         }
542         return M(sdb_object_create("all-matcher", iter_type,
543                                 MATCHER_ALL, iter, m));
544 } /* sdb_store_all_matcher */
546 sdb_store_matcher_t *
547 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
549         return M(sdb_object_create("lt-matcher", cmp_type,
550                                 MATCHER_LT, left, right));
551 } /* sdb_store_lt_matcher */
553 sdb_store_matcher_t *
554 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
556         return M(sdb_object_create("le-matcher", cmp_type,
557                                 MATCHER_LE, left, right));
558 } /* sdb_store_le_matcher */
560 sdb_store_matcher_t *
561 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
563         return M(sdb_object_create("eq-matcher", cmp_type,
564                                 MATCHER_EQ, left, right));
565 } /* sdb_store_eq_matcher */
567 sdb_store_matcher_t *
568 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
570         return M(sdb_object_create("ne-matcher", cmp_type,
571                                 MATCHER_NE, left, right));
572 } /* sdb_store_ne_matcher */
574 sdb_store_matcher_t *
575 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
577         return M(sdb_object_create("ge-matcher", cmp_type,
578                                 MATCHER_GE, left, right));
579 } /* sdb_store_ge_matcher */
581 sdb_store_matcher_t *
582 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
584         return M(sdb_object_create("gt-matcher", cmp_type,
585                                 MATCHER_GT, left, right));
586 } /* sdb_store_gt_matcher */
588 sdb_store_matcher_t *
589 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
591         return M(sdb_object_create("in-matcher", cmp_type,
592                                 MATCHER_IN, left, right));
593 } /* sdb_store_in_matcher */
595 sdb_store_matcher_t *
596 sdb_store_nin_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
598         return M(sdb_object_create("not-in-matcher", cmp_type,
599                                 MATCHER_NIN, left, right));
600 } /* sdb_store_in_matcher */
602 sdb_store_matcher_t *
603 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
605         if (! right->type) {
606                 if ((right->data.type != SDB_TYPE_STRING)
607                                 && (right->data.type != SDB_TYPE_REGEX))
608                         return NULL;
610                 if (right->data.type == SDB_TYPE_STRING) {
611                         char *raw = right->data.data.string;
612                         if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
613                                 return NULL;
614                         free(raw);
615                 }
616         }
617         return M(sdb_object_create("regex-matcher", cmp_type,
618                                 MATCHER_REGEX, left, right));
619 } /* sdb_store_regex_matcher */
621 sdb_store_matcher_t *
622 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
624         sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
625         if (! m)
626                 return NULL;
627         m->type = MATCHER_NREGEX;
628         return m;
629 } /* sdb_store_nregex_matcher */
631 sdb_store_matcher_t *
632 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
634         return M(sdb_object_create("isnull-matcher", isnull_type,
635                                 MATCHER_ISNULL, expr));
636 } /* sdb_store_isnull_matcher */
638 sdb_store_matcher_t *
639 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
641         return M(sdb_object_create("isnull-matcher", isnull_type,
642                                 MATCHER_ISNNULL, expr));
643 } /* sdb_store_isnnull_matcher */
645 sdb_store_matcher_op_cb
646 sdb_store_parse_matcher_op(const char *op)
648         if (! strcasecmp(op, "<"))
649                 return sdb_store_lt_matcher;
650         else if (! strcasecmp(op, "<="))
651                 return sdb_store_le_matcher;
652         else if (! strcasecmp(op, "="))
653                 return sdb_store_eq_matcher;
654         else if (! strcasecmp(op, "!="))
655                 return sdb_store_ne_matcher;
656         else if (! strcasecmp(op, ">="))
657                 return sdb_store_ge_matcher;
658         else if (! strcasecmp(op, ">"))
659                 return sdb_store_gt_matcher;
660         else if (! strcasecmp(op, "=~"))
661                 return sdb_store_regex_matcher;
662         else if (! strcasecmp(op, "!~"))
663                 return sdb_store_nregex_matcher;
664         return NULL;
665 } /* sdb_store_parse_matcher_op */
667 int
668 sdb_store_parse_object_type(const char *name)
670         if (! strcasecmp(name, "host"))
671                 return SDB_HOST;
672         else if (! strcasecmp(name, "service"))
673                 return SDB_SERVICE;
674         else if (! strcasecmp(name, "metric"))
675                 return SDB_METRIC;
676         else if (! strcasecmp(name, "attribute"))
677                 return SDB_ATTRIBUTE;
678         return -1;
679 } /* sdb_store_parse_object_type */
681 int
682 sdb_store_parse_object_type_plural(const char *name)
684         if (! strcasecmp(name, "hosts"))
685                 return SDB_HOST;
686         else if (! strcasecmp(name, "services"))
687                 return SDB_SERVICE;
688         else if (! strcasecmp(name, "metrics"))
689                 return SDB_METRIC;
690         else if (! strcasecmp(name, "attributes"))
691                 return SDB_ATTRIBUTE;
692         return -1;
693 } /* sdb_store_parse_object_type_plural */
695 int
696 sdb_store_parse_field_name(const char *name)
698         if (! strcasecmp(name, "name"))
699                 return SDB_FIELD_NAME;
700         else if (! strcasecmp(name, "last_update"))
701                 return SDB_FIELD_LAST_UPDATE;
702         else if (! strcasecmp(name, "age"))
703                 return SDB_FIELD_AGE;
704         else if (! strcasecmp(name, "interval"))
705                 return SDB_FIELD_INTERVAL;
706         else if (! strcasecmp(name, "backend"))
707                 return SDB_FIELD_BACKEND;
708         return -1;
709 } /* sdb_store_parse_field_name */
711 sdb_store_matcher_t *
712 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
714         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
715                                 left, right));
716 } /* sdb_store_dis_matcher */
718 sdb_store_matcher_t *
719 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
721         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
722                                 left, right));
723 } /* sdb_store_con_matcher */
725 sdb_store_matcher_t *
726 sdb_store_inv_matcher(sdb_store_matcher_t *m)
728         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
729 } /* sdb_store_inv_matcher */
731 int
732 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
733                 sdb_store_matcher_t *filter)
735         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
736                 return 0;
738         /* "NULL" always matches */
739         if ((! m) || (! obj))
740                 return 1;
742         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
743                 return 0;
745         return matchers[m->type](m, obj, filter);
746 } /* sdb_store_matcher_matches */
748 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */