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)
124 {
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)
159 {
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)
179 {
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)
190 {
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)
230 {
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)
257 {
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)
280 {
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, ®ex, obj, filter))
289 return 0;
291 status = match_regex_value(m->type, &v, ®ex);
293 expr_free_datum2(CMP_M(m)->left, &v, CMP_M(m)->right, ®ex);
294 return status;
295 } /* match_regex */
297 static int
298 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
299 sdb_store_matcher_t *filter)
300 {
301 sdb_data_t v = SDB_DATA_INIT;
302 int status;
304 assert((m->type == MATCHER_ISNULL)
305 || (m->type == MATCHER_ISTRUE)
306 || (m->type == MATCHER_ISFALSE));
308 if (UNARY_M(m)->expr->type) {
309 /* TODO: this might hide real errors;
310 * improve error reporting and propagation */
311 if (sdb_store_expr_eval(UNARY_M(m)->expr, obj, &v, filter))
312 return 1;
313 }
314 else
315 v = UNARY_M(m)->expr->data;
317 if (m->type == MATCHER_ISNULL)
318 status = sdb_data_isnull(&v) ? 1 : 0;
319 else { /* ISTRUE or ISFALSE */
320 if ((v.type == SDB_TYPE_BOOLEAN)
321 && (v.data.boolean == (m->type == MATCHER_ISTRUE)))
322 status = 1;
323 else
324 status = 0;
325 }
327 if (UNARY_M(m)->expr->type)
328 sdb_data_free_datum(&v);
329 return status;
330 } /* match_unary */
332 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
333 sdb_store_matcher_t *);
335 /* this array needs to be indexable by the matcher types;
336 * -> update the enum in store-private.h when updating this */
337 static matcher_cb
338 matchers[] = {
339 match_logical,
340 match_logical,
341 match_uop,
342 match_iter,
343 match_iter,
344 match_in,
345 match_in,
347 /* unary operators */
348 match_unary,
349 match_unary,
350 match_unary,
352 /* ary operators */
353 match_cmp,
354 match_cmp,
355 match_cmp,
356 match_cmp,
357 match_cmp,
358 match_cmp,
359 match_regex,
360 match_regex,
362 NULL, /* QUERY */
363 };
365 /*
366 * private matcher types
367 */
369 static int
370 op_matcher_init(sdb_object_t *obj, va_list ap)
371 {
372 M(obj)->type = va_arg(ap, int);
373 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
374 return -1;
376 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
377 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
378 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
379 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
381 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
382 return -1;
383 return 0;
384 } /* op_matcher_init */
386 static void
387 op_matcher_destroy(sdb_object_t *obj)
388 {
389 if (OP_M(obj)->left)
390 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
391 if (OP_M(obj)->right)
392 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
393 } /* op_matcher_destroy */
395 static int
396 iter_matcher_init(sdb_object_t *obj, va_list ap)
397 {
398 M(obj)->type = va_arg(ap, int);
399 ITER_M(obj)->iter = va_arg(ap, sdb_store_expr_t *);
400 ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
402 sdb_object_ref(SDB_OBJ(ITER_M(obj)->iter));
403 sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
405 if ((! ITER_M(obj)->iter) || (! ITER_M(obj)->m))
406 return -1;
407 return 0;
408 } /* iter_matcher_init */
410 static void
411 iter_matcher_destroy(sdb_object_t *obj)
412 {
413 sdb_object_deref(SDB_OBJ(ITER_M(obj)->iter));
414 sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
415 } /* iter_matcher_destroy */
417 static int
418 cmp_matcher_init(sdb_object_t *obj, va_list ap)
419 {
420 M(obj)->type = va_arg(ap, int);
422 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
423 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
424 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
425 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
427 if (! CMP_M(obj)->right)
428 return -1;
429 return 0;
430 } /* cmp_matcher_init */
432 static void
433 cmp_matcher_destroy(sdb_object_t *obj)
434 {
435 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
436 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
437 } /* cmp_matcher_destroy */
439 static int
440 uop_matcher_init(sdb_object_t *obj, va_list ap)
441 {
442 M(obj)->type = va_arg(ap, int);
443 if (M(obj)->type != MATCHER_NOT)
444 return -1;
446 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
447 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
449 if (! UOP_M(obj)->op)
450 return -1;
451 return 0;
452 } /* uop_matcher_init */
454 static void
455 uop_matcher_destroy(sdb_object_t *obj)
456 {
457 if (UOP_M(obj)->op)
458 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
459 } /* uop_matcher_destroy */
461 static int
462 unary_matcher_init(sdb_object_t *obj, va_list ap)
463 {
464 M(obj)->type = va_arg(ap, int);
465 if ((M(obj)->type != MATCHER_ISNULL)
466 && (M(obj)->type != MATCHER_ISTRUE)
467 && (M(obj)->type != MATCHER_ISFALSE))
468 return -1;
470 UNARY_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
471 sdb_object_ref(SDB_OBJ(UNARY_M(obj)->expr));
472 return 0;
473 } /* unary_matcher_init */
475 static void
476 unary_matcher_destroy(sdb_object_t *obj)
477 {
478 sdb_object_deref(SDB_OBJ(UNARY_M(obj)->expr));
479 UNARY_M(obj)->expr = NULL;
480 } /* unary_matcher_destroy */
482 static sdb_type_t op_type = {
483 /* size = */ sizeof(op_matcher_t),
484 /* init = */ op_matcher_init,
485 /* destroy = */ op_matcher_destroy,
486 };
488 static sdb_type_t uop_type = {
489 /* size = */ sizeof(uop_matcher_t),
490 /* init = */ uop_matcher_init,
491 /* destroy = */ uop_matcher_destroy,
492 };
494 static sdb_type_t iter_type = {
495 /* size = */ sizeof(iter_matcher_t),
496 /* init = */ iter_matcher_init,
497 /* destroy = */ iter_matcher_destroy,
498 };
500 static sdb_type_t cmp_type = {
501 /* size = */ sizeof(cmp_matcher_t),
502 /* init = */ cmp_matcher_init,
503 /* destroy = */ cmp_matcher_destroy,
504 };
506 static sdb_type_t unary_type = {
507 /* size = */ sizeof(unary_matcher_t),
508 /* init = */ unary_matcher_init,
509 /* destroy = */ unary_matcher_destroy,
510 };
512 /*
513 * public API
514 */
516 sdb_store_matcher_t *
517 sdb_store_any_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m)
518 {
519 if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) {
520 sdb_log(SDB_LOG_ERR, "store: Invalid ANY -> %s matcher "
521 "(invalid operator)", MATCHER_SYM(m->type));
522 return NULL;
523 }
524 if (CMP_M(m)->left) {
525 sdb_log(SDB_LOG_ERR, "store: Invalid ANY %s %s %s matcher "
526 "(invalid left operand)",
527 SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type),
528 MATCHER_SYM(m->type),
529 SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type));
530 return NULL;
531 }
532 return M(sdb_object_create("any-matcher", iter_type,
533 MATCHER_ANY, iter, m));
534 } /* sdb_store_any_matcher */
536 sdb_store_matcher_t *
537 sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m)
538 {
539 if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) {
540 sdb_log(SDB_LOG_ERR, "store: Invalid ALL -> %s matcher "
541 "(invalid operator)", MATCHER_SYM(m->type));
542 return NULL;
543 }
544 if (CMP_M(m)->left) {
545 sdb_log(SDB_LOG_ERR, "store: Invalid ALL %s %s %s matcher "
546 "(invalid left operand)",
547 SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type),
548 MATCHER_SYM(m->type),
549 SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type));
550 return NULL;
551 }
552 return M(sdb_object_create("all-matcher", iter_type,
553 MATCHER_ALL, iter, m));
554 } /* sdb_store_all_matcher */
556 sdb_store_matcher_t *
557 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
558 {
559 return M(sdb_object_create("lt-matcher", cmp_type,
560 MATCHER_LT, left, right));
561 } /* sdb_store_lt_matcher */
563 sdb_store_matcher_t *
564 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
565 {
566 return M(sdb_object_create("le-matcher", cmp_type,
567 MATCHER_LE, left, right));
568 } /* sdb_store_le_matcher */
570 sdb_store_matcher_t *
571 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
572 {
573 return M(sdb_object_create("eq-matcher", cmp_type,
574 MATCHER_EQ, left, right));
575 } /* sdb_store_eq_matcher */
577 sdb_store_matcher_t *
578 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
579 {
580 return M(sdb_object_create("ne-matcher", cmp_type,
581 MATCHER_NE, left, right));
582 } /* sdb_store_ne_matcher */
584 sdb_store_matcher_t *
585 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
586 {
587 return M(sdb_object_create("ge-matcher", cmp_type,
588 MATCHER_GE, left, right));
589 } /* sdb_store_ge_matcher */
591 sdb_store_matcher_t *
592 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
593 {
594 return M(sdb_object_create("gt-matcher", cmp_type,
595 MATCHER_GT, left, right));
596 } /* sdb_store_gt_matcher */
598 sdb_store_matcher_t *
599 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
600 {
601 return M(sdb_object_create("in-matcher", cmp_type,
602 MATCHER_IN, left, right));
603 } /* sdb_store_in_matcher */
605 sdb_store_matcher_t *
606 sdb_store_nin_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
607 {
608 return M(sdb_object_create("not-in-matcher", cmp_type,
609 MATCHER_NIN, left, right));
610 } /* sdb_store_in_matcher */
612 sdb_store_matcher_t *
613 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
614 {
615 if (! right->type) {
616 if ((right->data.type != SDB_TYPE_STRING)
617 && (right->data.type != SDB_TYPE_REGEX))
618 return NULL;
620 if (right->data.type == SDB_TYPE_STRING) {
621 char *raw = right->data.data.string;
622 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
623 return NULL;
624 free(raw);
625 }
626 }
627 return M(sdb_object_create("regex-matcher", cmp_type,
628 MATCHER_REGEX, left, right));
629 } /* sdb_store_regex_matcher */
631 sdb_store_matcher_t *
632 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
633 {
634 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
635 if (! m)
636 return NULL;
637 m->type = MATCHER_NREGEX;
638 return m;
639 } /* sdb_store_nregex_matcher */
641 sdb_store_matcher_t *
642 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
643 {
644 return M(sdb_object_create("isnull-matcher", unary_type,
645 MATCHER_ISNULL, expr));
646 } /* sdb_store_isnull_matcher */
648 sdb_store_matcher_t *
649 sdb_store_istrue_matcher(sdb_store_expr_t *expr)
650 {
651 return M(sdb_object_create("istrue-matcher", unary_type,
652 MATCHER_ISTRUE, expr));
653 } /* sdb_store_istrue_matcher */
655 sdb_store_matcher_t *
656 sdb_store_isfalse_matcher(sdb_store_expr_t *expr)
657 {
658 return M(sdb_object_create("isfalse-matcher", unary_type,
659 MATCHER_ISFALSE, expr));
660 } /* sdb_store_isfalse_matcher */
662 sdb_store_matcher_op_cb
663 sdb_store_parse_matcher_op(const char *op)
664 {
665 if (! strcasecmp(op, "<"))
666 return sdb_store_lt_matcher;
667 else if (! strcasecmp(op, "<="))
668 return sdb_store_le_matcher;
669 else if (! strcasecmp(op, "="))
670 return sdb_store_eq_matcher;
671 else if (! strcasecmp(op, "!="))
672 return sdb_store_ne_matcher;
673 else if (! strcasecmp(op, ">="))
674 return sdb_store_ge_matcher;
675 else if (! strcasecmp(op, ">"))
676 return sdb_store_gt_matcher;
677 else if (! strcasecmp(op, "=~"))
678 return sdb_store_regex_matcher;
679 else if (! strcasecmp(op, "!~"))
680 return sdb_store_nregex_matcher;
681 return NULL;
682 } /* sdb_store_parse_matcher_op */
684 int
685 sdb_store_parse_object_type(const char *name)
686 {
687 if (! strcasecmp(name, "host"))
688 return SDB_HOST;
689 else if (! strcasecmp(name, "service"))
690 return SDB_SERVICE;
691 else if (! strcasecmp(name, "metric"))
692 return SDB_METRIC;
693 else if (! strcasecmp(name, "attribute"))
694 return SDB_ATTRIBUTE;
695 return -1;
696 } /* sdb_store_parse_object_type */
698 int
699 sdb_store_parse_object_type_plural(const char *name)
700 {
701 if (! strcasecmp(name, "hosts"))
702 return SDB_HOST;
703 else if (! strcasecmp(name, "services"))
704 return SDB_SERVICE;
705 else if (! strcasecmp(name, "metrics"))
706 return SDB_METRIC;
707 else if (! strcasecmp(name, "attributes"))
708 return SDB_ATTRIBUTE;
709 return -1;
710 } /* sdb_store_parse_object_type_plural */
712 int
713 sdb_store_parse_field_name(const char *name)
714 {
715 if (! strcasecmp(name, "name"))
716 return SDB_FIELD_NAME;
717 else if (! strcasecmp(name, "last_update"))
718 return SDB_FIELD_LAST_UPDATE;
719 else if (! strcasecmp(name, "age"))
720 return SDB_FIELD_AGE;
721 else if (! strcasecmp(name, "interval"))
722 return SDB_FIELD_INTERVAL;
723 else if (! strcasecmp(name, "backend"))
724 return SDB_FIELD_BACKEND;
725 else if (! strcasecmp(name, "value"))
726 return SDB_FIELD_VALUE;
727 return -1;
728 } /* sdb_store_parse_field_name */
730 sdb_store_matcher_t *
731 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
732 {
733 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
734 left, right));
735 } /* sdb_store_dis_matcher */
737 sdb_store_matcher_t *
738 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
739 {
740 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
741 left, right));
742 } /* sdb_store_con_matcher */
744 sdb_store_matcher_t *
745 sdb_store_inv_matcher(sdb_store_matcher_t *m)
746 {
747 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
748 } /* sdb_store_inv_matcher */
750 int
751 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
752 sdb_store_matcher_t *filter)
753 {
754 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
755 return 0;
757 /* "NULL" always matches */
758 if ((! m) || (! obj))
759 return 1;
761 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
762 return 0;
764 if (! matchers[m->type])
765 return 0;
766 return matchers[m->type](m, obj, filter);
767 } /* sdb_store_matcher_matches */
769 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */