Code

82bb3d6d6cf1e50f9cc0b3c748a797e37bbb0fb6
[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_uop(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_uop */
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);
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         return status;
273 } /* match_in */
275 static int
276 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
277                 sdb_store_matcher_t *filter)
279         sdb_data_t regex = SDB_DATA_INIT, v = SDB_DATA_INIT;
280         int status = 0;
282         assert((m->type == MATCHER_REGEX)
283                         || (m->type == MATCHER_NREGEX));
284         assert(CMP_M(m)->left && CMP_M(m)->right);
286         if (expr_eval2(CMP_M(m)->left, &v, CMP_M(m)->right, &regex, obj, filter))
287                 return 0;
289         status = match_regex_value(m->type, &v, &regex);
291         expr_free_datum2(CMP_M(m)->left, &v, CMP_M(m)->right, &regex);
292         return status;
293 } /* match_regex */
295 static int
296 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
297                 sdb_store_matcher_t *filter)
299         sdb_data_t v = SDB_DATA_INIT;
300         int status;
302         assert((m->type == MATCHER_ISNULL)
303                         || (m->type == MATCHER_ISTRUE)
304                         || (m->type == MATCHER_ISFALSE));
306         if (UNARY_M(m)->expr->type) {
307                 /* TODO: this might hide real errors;
308                  * improve error reporting and propagation */
309                 if (sdb_store_expr_eval(UNARY_M(m)->expr, obj, &v, filter))
310                         return 1;
311         }
312         else
313                 v = UNARY_M(m)->expr->data;
315         if (m->type == MATCHER_ISNULL)
316                 status = sdb_data_isnull(&v) ? 1 : 0;
317         else { /* ISTRUE or ISFALSE */
318                 if ((v.type == SDB_TYPE_BOOLEAN)
319                                 && (v.data.boolean == (m->type == MATCHER_ISTRUE)))
320                         status = 1;
321                 else
322                         status = 0;
323         }
325         if (UNARY_M(m)->expr->type)
326                 sdb_data_free_datum(&v);
327         return status;
328 } /* match_unary */
330 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
331                 sdb_store_matcher_t *);
333 /* this array needs to be indexable by the matcher types;
334  * -> update the enum in store-private.h when updating this */
335 static matcher_cb
336 matchers[] = {
337         match_logical,
338         match_logical,
339         match_uop,
340         match_iter,
341         match_iter,
342         match_in,
344         /* unary operators */
345         match_unary,
346         match_unary,
347         match_unary,
349         /* ary operators */
350         match_cmp,
351         match_cmp,
352         match_cmp,
353         match_cmp,
354         match_cmp,
355         match_cmp,
356         match_regex,
357         match_regex,
359         NULL, /* QUERY */
360 };
362 /*
363  * private matcher types
364  */
366 static int
367 op_matcher_init(sdb_object_t *obj, va_list ap)
369         M(obj)->type = va_arg(ap, int);
370         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
371                 return -1;
373         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
374         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
375         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
376         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
378         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
379                 return -1;
380         return 0;
381 } /* op_matcher_init */
383 static void
384 op_matcher_destroy(sdb_object_t *obj)
386         if (OP_M(obj)->left)
387                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
388         if (OP_M(obj)->right)
389                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
390 } /* op_matcher_destroy */
392 static int
393 iter_matcher_init(sdb_object_t *obj, va_list ap)
395         M(obj)->type = va_arg(ap, int);
396         ITER_M(obj)->iter = va_arg(ap, sdb_store_expr_t *);
397         ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
399         sdb_object_ref(SDB_OBJ(ITER_M(obj)->iter));
400         sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
402         if ((! ITER_M(obj)->iter) || (! ITER_M(obj)->m))
403                 return -1;
404         return 0;
405 } /* iter_matcher_init */
407 static void
408 iter_matcher_destroy(sdb_object_t *obj)
410         sdb_object_deref(SDB_OBJ(ITER_M(obj)->iter));
411         sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
412 } /* iter_matcher_destroy */
414 static int
415 cmp_matcher_init(sdb_object_t *obj, va_list ap)
417         M(obj)->type = va_arg(ap, int);
419         CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
420         sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
421         CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
422         sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
424         if (! CMP_M(obj)->right)
425                 return -1;
426         return 0;
427 } /* cmp_matcher_init */
429 static void
430 cmp_matcher_destroy(sdb_object_t *obj)
432         sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
433         sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
434 } /* cmp_matcher_destroy */
436 static int
437 uop_matcher_init(sdb_object_t *obj, va_list ap)
439         M(obj)->type = va_arg(ap, int);
440         if (M(obj)->type != MATCHER_NOT)
441                 return -1;
443         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
444         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
446         if (! UOP_M(obj)->op)
447                 return -1;
448         return 0;
449 } /* uop_matcher_init */
451 static void
452 uop_matcher_destroy(sdb_object_t *obj)
454         if (UOP_M(obj)->op)
455                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
456 } /* uop_matcher_destroy */
458 static int
459 unary_matcher_init(sdb_object_t *obj, va_list ap)
461         M(obj)->type = va_arg(ap, int);
462         if ((M(obj)->type != MATCHER_ISNULL)
463                         && (M(obj)->type != MATCHER_ISTRUE)
464                         && (M(obj)->type != MATCHER_ISFALSE))
465                 return -1;
467         UNARY_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
468         sdb_object_ref(SDB_OBJ(UNARY_M(obj)->expr));
469         return 0;
470 } /* unary_matcher_init */
472 static void
473 unary_matcher_destroy(sdb_object_t *obj)
475         sdb_object_deref(SDB_OBJ(UNARY_M(obj)->expr));
476         UNARY_M(obj)->expr = NULL;
477 } /* unary_matcher_destroy */
479 static sdb_type_t op_type = {
480         /* size = */ sizeof(op_matcher_t),
481         /* init = */ op_matcher_init,
482         /* destroy = */ op_matcher_destroy,
483 };
485 static sdb_type_t uop_type = {
486         /* size = */ sizeof(uop_matcher_t),
487         /* init = */ uop_matcher_init,
488         /* destroy = */ uop_matcher_destroy,
489 };
491 static sdb_type_t iter_type = {
492         /* size = */ sizeof(iter_matcher_t),
493         /* init = */ iter_matcher_init,
494         /* destroy = */ iter_matcher_destroy,
495 };
497 static sdb_type_t cmp_type = {
498         /* size = */ sizeof(cmp_matcher_t),
499         /* init = */ cmp_matcher_init,
500         /* destroy = */ cmp_matcher_destroy,
501 };
503 static sdb_type_t unary_type = {
504         /* size = */ sizeof(unary_matcher_t),
505         /* init = */ unary_matcher_init,
506         /* destroy = */ unary_matcher_destroy,
507 };
509 /*
510  * public API
511  */
513 sdb_store_matcher_t *
514 sdb_store_any_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m)
516         if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) {
517                 sdb_log(SDB_LOG_ERR, "store: Invalid ANY -> %s matcher "
518                                 "(invalid operator)", MATCHER_SYM(m->type));
519                 return NULL;
520         }
521         if (CMP_M(m)->left) {
522                 sdb_log(SDB_LOG_ERR, "store: Invalid ANY %s %s %s matcher "
523                                 "(invalid left operand)",
524                                 SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type),
525                                 MATCHER_SYM(m->type),
526                                 SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type));
527                 return NULL;
528         }
529         return M(sdb_object_create("any-matcher", iter_type,
530                                 MATCHER_ANY, iter, m));
531 } /* sdb_store_any_matcher */
533 sdb_store_matcher_t *
534 sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m)
536         if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) {
537                 sdb_log(SDB_LOG_ERR, "store: Invalid ALL -> %s matcher "
538                                 "(invalid operator)", MATCHER_SYM(m->type));
539                 return NULL;
540         }
541         if (CMP_M(m)->left) {
542                 sdb_log(SDB_LOG_ERR, "store: Invalid ALL %s %s %s matcher "
543                                 "(invalid left operand)",
544                                 SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type),
545                                 MATCHER_SYM(m->type),
546                                 SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type));
547                 return NULL;
548         }
549         return M(sdb_object_create("all-matcher", iter_type,
550                                 MATCHER_ALL, iter, m));
551 } /* sdb_store_all_matcher */
553 sdb_store_matcher_t *
554 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
556         return M(sdb_object_create("lt-matcher", cmp_type,
557                                 MATCHER_LT, left, right));
558 } /* sdb_store_lt_matcher */
560 sdb_store_matcher_t *
561 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
563         return M(sdb_object_create("le-matcher", cmp_type,
564                                 MATCHER_LE, left, right));
565 } /* sdb_store_le_matcher */
567 sdb_store_matcher_t *
568 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
570         return M(sdb_object_create("eq-matcher", cmp_type,
571                                 MATCHER_EQ, left, right));
572 } /* sdb_store_eq_matcher */
574 sdb_store_matcher_t *
575 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
577         return M(sdb_object_create("ne-matcher", cmp_type,
578                                 MATCHER_NE, left, right));
579 } /* sdb_store_ne_matcher */
581 sdb_store_matcher_t *
582 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
584         return M(sdb_object_create("ge-matcher", cmp_type,
585                                 MATCHER_GE, left, right));
586 } /* sdb_store_ge_matcher */
588 sdb_store_matcher_t *
589 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
591         return M(sdb_object_create("gt-matcher", cmp_type,
592                                 MATCHER_GT, left, right));
593 } /* sdb_store_gt_matcher */
595 sdb_store_matcher_t *
596 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
598         return M(sdb_object_create("in-matcher", cmp_type,
599                                 MATCHER_IN, 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", unary_type,
635                                 MATCHER_ISNULL, expr));
636 } /* sdb_store_isnull_matcher */
638 sdb_store_matcher_t *
639 sdb_store_istrue_matcher(sdb_store_expr_t *expr)
641         return M(sdb_object_create("istrue-matcher", unary_type,
642                                 MATCHER_ISTRUE, expr));
643 } /* sdb_store_istrue_matcher */
645 sdb_store_matcher_t *
646 sdb_store_isfalse_matcher(sdb_store_expr_t *expr)
648         return M(sdb_object_create("isfalse-matcher", unary_type,
649                                 MATCHER_ISFALSE, expr));
650 } /* sdb_store_isfalse_matcher */
652 sdb_store_matcher_op_cb
653 sdb_store_parse_matcher_op(const char *op)
655         if (! strcasecmp(op, "<"))
656                 return sdb_store_lt_matcher;
657         else if (! strcasecmp(op, "<="))
658                 return sdb_store_le_matcher;
659         else if (! strcasecmp(op, "="))
660                 return sdb_store_eq_matcher;
661         else if (! strcasecmp(op, "!="))
662                 return sdb_store_ne_matcher;
663         else if (! strcasecmp(op, ">="))
664                 return sdb_store_ge_matcher;
665         else if (! strcasecmp(op, ">"))
666                 return sdb_store_gt_matcher;
667         else if (! strcasecmp(op, "=~"))
668                 return sdb_store_regex_matcher;
669         else if (! strcasecmp(op, "!~"))
670                 return sdb_store_nregex_matcher;
671         return NULL;
672 } /* sdb_store_parse_matcher_op */
674 int
675 sdb_store_parse_object_type(const char *name)
677         if (! strcasecmp(name, "host"))
678                 return SDB_HOST;
679         else if (! strcasecmp(name, "service"))
680                 return SDB_SERVICE;
681         else if (! strcasecmp(name, "metric"))
682                 return SDB_METRIC;
683         else if (! strcasecmp(name, "attribute"))
684                 return SDB_ATTRIBUTE;
685         return -1;
686 } /* sdb_store_parse_object_type */
688 int
689 sdb_store_parse_object_type_plural(const char *name)
691         if (! strcasecmp(name, "hosts"))
692                 return SDB_HOST;
693         else if (! strcasecmp(name, "services"))
694                 return SDB_SERVICE;
695         else if (! strcasecmp(name, "metrics"))
696                 return SDB_METRIC;
697         else if (! strcasecmp(name, "attributes"))
698                 return SDB_ATTRIBUTE;
699         return -1;
700 } /* sdb_store_parse_object_type_plural */
702 int
703 sdb_store_parse_field_name(const char *name)
705         if (! strcasecmp(name, "name"))
706                 return SDB_FIELD_NAME;
707         else if (! strcasecmp(name, "last_update"))
708                 return SDB_FIELD_LAST_UPDATE;
709         else if (! strcasecmp(name, "age"))
710                 return SDB_FIELD_AGE;
711         else if (! strcasecmp(name, "interval"))
712                 return SDB_FIELD_INTERVAL;
713         else if (! strcasecmp(name, "backend"))
714                 return SDB_FIELD_BACKEND;
715         else if (! strcasecmp(name, "value"))
716                 return SDB_FIELD_VALUE;
717         return -1;
718 } /* sdb_store_parse_field_name */
720 sdb_store_matcher_t *
721 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
723         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
724                                 left, right));
725 } /* sdb_store_dis_matcher */
727 sdb_store_matcher_t *
728 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
730         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
731                                 left, right));
732 } /* sdb_store_con_matcher */
734 sdb_store_matcher_t *
735 sdb_store_inv_matcher(sdb_store_matcher_t *m)
737         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
738 } /* sdb_store_inv_matcher */
740 int
741 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
742                 sdb_store_matcher_t *filter)
744         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
745                 return 0;
747         /* "NULL" always matches */
748         if ((! m) || (! obj))
749                 return 1;
751         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
752                 return 0;
754         if (! matchers[m->type])
755                 return 0;
756         return matchers[m->type](m, obj, filter);
757 } /* sdb_store_matcher_matches */
759 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */