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_unary(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_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)
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_isnull(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) || (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,
356 NULL, /* QUERY */
357 };
359 /*
360 * private matcher types
361 */
363 static int
364 op_matcher_init(sdb_object_t *obj, va_list ap)
365 {
366 M(obj)->type = va_arg(ap, int);
367 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
368 return -1;
370 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
371 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
372 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
373 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
375 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
376 return -1;
377 return 0;
378 } /* op_matcher_init */
380 static void
381 op_matcher_destroy(sdb_object_t *obj)
382 {
383 if (OP_M(obj)->left)
384 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
385 if (OP_M(obj)->right)
386 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
387 } /* op_matcher_destroy */
389 static int
390 iter_matcher_init(sdb_object_t *obj, va_list ap)
391 {
392 M(obj)->type = va_arg(ap, int);
393 ITER_M(obj)->iter = va_arg(ap, sdb_store_expr_t *);
394 ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
396 sdb_object_ref(SDB_OBJ(ITER_M(obj)->iter));
397 sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
399 if ((! ITER_M(obj)->iter) || (! ITER_M(obj)->m))
400 return -1;
401 return 0;
402 } /* iter_matcher_init */
404 static void
405 iter_matcher_destroy(sdb_object_t *obj)
406 {
407 sdb_object_deref(SDB_OBJ(ITER_M(obj)->iter));
408 sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
409 } /* iter_matcher_destroy */
411 static int
412 cmp_matcher_init(sdb_object_t *obj, va_list ap)
413 {
414 M(obj)->type = va_arg(ap, int);
416 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
417 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
418 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
419 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
421 if (! CMP_M(obj)->right)
422 return -1;
423 return 0;
424 } /* cmp_matcher_init */
426 static void
427 cmp_matcher_destroy(sdb_object_t *obj)
428 {
429 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
430 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
431 } /* cmp_matcher_destroy */
433 static int
434 uop_matcher_init(sdb_object_t *obj, va_list ap)
435 {
436 M(obj)->type = va_arg(ap, int);
437 if (M(obj)->type != MATCHER_NOT)
438 return -1;
440 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
441 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
443 if (! UOP_M(obj)->op)
444 return -1;
445 return 0;
446 } /* uop_matcher_init */
448 static void
449 uop_matcher_destroy(sdb_object_t *obj)
450 {
451 if (UOP_M(obj)->op)
452 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
453 } /* uop_matcher_destroy */
455 static int
456 isnull_matcher_init(sdb_object_t *obj, va_list ap)
457 {
458 M(obj)->type = va_arg(ap, int);
459 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
460 return -1;
462 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
463 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
464 return 0;
465 } /* isnull_matcher_init */
467 static void
468 isnull_matcher_destroy(sdb_object_t *obj)
469 {
470 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
471 ISNULL_M(obj)->expr = NULL;
472 } /* isnull_matcher_destroy */
474 static sdb_type_t op_type = {
475 /* size = */ sizeof(op_matcher_t),
476 /* init = */ op_matcher_init,
477 /* destroy = */ op_matcher_destroy,
478 };
480 static sdb_type_t uop_type = {
481 /* size = */ sizeof(uop_matcher_t),
482 /* init = */ uop_matcher_init,
483 /* destroy = */ uop_matcher_destroy,
484 };
486 static sdb_type_t iter_type = {
487 /* size = */ sizeof(iter_matcher_t),
488 /* init = */ iter_matcher_init,
489 /* destroy = */ iter_matcher_destroy,
490 };
492 static sdb_type_t cmp_type = {
493 /* size = */ sizeof(cmp_matcher_t),
494 /* init = */ cmp_matcher_init,
495 /* destroy = */ cmp_matcher_destroy,
496 };
498 static sdb_type_t isnull_type = {
499 /* size = */ sizeof(isnull_matcher_t),
500 /* init = */ isnull_matcher_init,
501 /* destroy = */ isnull_matcher_destroy,
502 };
504 /*
505 * public API
506 */
508 sdb_store_matcher_t *
509 sdb_store_any_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m)
510 {
511 if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) {
512 sdb_log(SDB_LOG_ERR, "store: Invalid ANY -> %s matcher "
513 "(invalid operator)", MATCHER_SYM(m->type));
514 return NULL;
515 }
516 if (CMP_M(m)->left) {
517 sdb_log(SDB_LOG_ERR, "store: Invalid ANY %s %s %s matcher "
518 "(invalid left operand)",
519 SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type),
520 MATCHER_SYM(m->type),
521 SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type));
522 return NULL;
523 }
524 return M(sdb_object_create("any-matcher", iter_type,
525 MATCHER_ANY, iter, m));
526 } /* sdb_store_any_matcher */
528 sdb_store_matcher_t *
529 sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m)
530 {
531 if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) {
532 sdb_log(SDB_LOG_ERR, "store: Invalid ALL -> %s matcher "
533 "(invalid operator)", MATCHER_SYM(m->type));
534 return NULL;
535 }
536 if (CMP_M(m)->left) {
537 sdb_log(SDB_LOG_ERR, "store: Invalid ALL %s %s %s matcher "
538 "(invalid left operand)",
539 SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type),
540 MATCHER_SYM(m->type),
541 SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type));
542 return NULL;
543 }
544 return M(sdb_object_create("all-matcher", iter_type,
545 MATCHER_ALL, iter, m));
546 } /* sdb_store_all_matcher */
548 sdb_store_matcher_t *
549 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
550 {
551 return M(sdb_object_create("lt-matcher", cmp_type,
552 MATCHER_LT, left, right));
553 } /* sdb_store_lt_matcher */
555 sdb_store_matcher_t *
556 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
557 {
558 return M(sdb_object_create("le-matcher", cmp_type,
559 MATCHER_LE, left, right));
560 } /* sdb_store_le_matcher */
562 sdb_store_matcher_t *
563 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
564 {
565 return M(sdb_object_create("eq-matcher", cmp_type,
566 MATCHER_EQ, left, right));
567 } /* sdb_store_eq_matcher */
569 sdb_store_matcher_t *
570 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
571 {
572 return M(sdb_object_create("ne-matcher", cmp_type,
573 MATCHER_NE, left, right));
574 } /* sdb_store_ne_matcher */
576 sdb_store_matcher_t *
577 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
578 {
579 return M(sdb_object_create("ge-matcher", cmp_type,
580 MATCHER_GE, left, right));
581 } /* sdb_store_ge_matcher */
583 sdb_store_matcher_t *
584 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
585 {
586 return M(sdb_object_create("gt-matcher", cmp_type,
587 MATCHER_GT, left, right));
588 } /* sdb_store_gt_matcher */
590 sdb_store_matcher_t *
591 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
592 {
593 return M(sdb_object_create("in-matcher", cmp_type,
594 MATCHER_IN, left, right));
595 } /* sdb_store_in_matcher */
597 sdb_store_matcher_t *
598 sdb_store_nin_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
599 {
600 return M(sdb_object_create("not-in-matcher", cmp_type,
601 MATCHER_NIN, left, right));
602 } /* sdb_store_in_matcher */
604 sdb_store_matcher_t *
605 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
606 {
607 if (! right->type) {
608 if ((right->data.type != SDB_TYPE_STRING)
609 && (right->data.type != SDB_TYPE_REGEX))
610 return NULL;
612 if (right->data.type == SDB_TYPE_STRING) {
613 char *raw = right->data.data.string;
614 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
615 return NULL;
616 free(raw);
617 }
618 }
619 return M(sdb_object_create("regex-matcher", cmp_type,
620 MATCHER_REGEX, left, right));
621 } /* sdb_store_regex_matcher */
623 sdb_store_matcher_t *
624 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
625 {
626 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
627 if (! m)
628 return NULL;
629 m->type = MATCHER_NREGEX;
630 return m;
631 } /* sdb_store_nregex_matcher */
633 sdb_store_matcher_t *
634 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
635 {
636 return M(sdb_object_create("isnull-matcher", isnull_type,
637 MATCHER_ISNULL, expr));
638 } /* sdb_store_isnull_matcher */
640 sdb_store_matcher_t *
641 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
642 {
643 return M(sdb_object_create("isnull-matcher", isnull_type,
644 MATCHER_ISNNULL, expr));
645 } /* sdb_store_isnnull_matcher */
647 sdb_store_matcher_op_cb
648 sdb_store_parse_matcher_op(const char *op)
649 {
650 if (! strcasecmp(op, "<"))
651 return sdb_store_lt_matcher;
652 else if (! strcasecmp(op, "<="))
653 return sdb_store_le_matcher;
654 else if (! strcasecmp(op, "="))
655 return sdb_store_eq_matcher;
656 else if (! strcasecmp(op, "!="))
657 return sdb_store_ne_matcher;
658 else if (! strcasecmp(op, ">="))
659 return sdb_store_ge_matcher;
660 else if (! strcasecmp(op, ">"))
661 return sdb_store_gt_matcher;
662 else if (! strcasecmp(op, "=~"))
663 return sdb_store_regex_matcher;
664 else if (! strcasecmp(op, "!~"))
665 return sdb_store_nregex_matcher;
666 return NULL;
667 } /* sdb_store_parse_matcher_op */
669 int
670 sdb_store_parse_object_type(const char *name)
671 {
672 if (! strcasecmp(name, "host"))
673 return SDB_HOST;
674 else if (! strcasecmp(name, "service"))
675 return SDB_SERVICE;
676 else if (! strcasecmp(name, "metric"))
677 return SDB_METRIC;
678 else if (! strcasecmp(name, "attribute"))
679 return SDB_ATTRIBUTE;
680 return -1;
681 } /* sdb_store_parse_object_type */
683 int
684 sdb_store_parse_object_type_plural(const char *name)
685 {
686 if (! strcasecmp(name, "hosts"))
687 return SDB_HOST;
688 else if (! strcasecmp(name, "services"))
689 return SDB_SERVICE;
690 else if (! strcasecmp(name, "metrics"))
691 return SDB_METRIC;
692 else if (! strcasecmp(name, "attributes"))
693 return SDB_ATTRIBUTE;
694 return -1;
695 } /* sdb_store_parse_object_type_plural */
697 int
698 sdb_store_parse_field_name(const char *name)
699 {
700 if (! strcasecmp(name, "name"))
701 return SDB_FIELD_NAME;
702 else if (! strcasecmp(name, "last_update"))
703 return SDB_FIELD_LAST_UPDATE;
704 else if (! strcasecmp(name, "age"))
705 return SDB_FIELD_AGE;
706 else if (! strcasecmp(name, "interval"))
707 return SDB_FIELD_INTERVAL;
708 else if (! strcasecmp(name, "backend"))
709 return SDB_FIELD_BACKEND;
710 else if (! strcasecmp(name, "value"))
711 return SDB_FIELD_VALUE;
712 return -1;
713 } /* sdb_store_parse_field_name */
715 sdb_store_matcher_t *
716 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
717 {
718 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
719 left, right));
720 } /* sdb_store_dis_matcher */
722 sdb_store_matcher_t *
723 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
724 {
725 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
726 left, right));
727 } /* sdb_store_con_matcher */
729 sdb_store_matcher_t *
730 sdb_store_inv_matcher(sdb_store_matcher_t *m)
731 {
732 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
733 } /* sdb_store_inv_matcher */
735 int
736 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
737 sdb_store_matcher_t *filter)
738 {
739 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
740 return 0;
742 /* "NULL" always matches */
743 if ((! m) || (! obj))
744 return 1;
746 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
747 return 0;
749 if (! matchers[m->type])
750 return 0;
751 return matchers[m->type](m, obj, filter);
752 } /* sdb_store_matcher_matches */
754 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */