1 /*
2 * SysDB - src/core/store_lookup.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 /*
29 * This module implements operators which may be used to select contents of
30 * the store by matching various attributes of the stored objects. For now, a
31 * simple full table scan is supported only.
32 */
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif /* HAVE_CONFIG_H */
38 #include "sysdb.h"
39 #include "core/store-private.h"
40 #include "core/object.h"
42 #include <assert.h>
44 #include <sys/types.h>
45 #include <regex.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #include <limits.h>
52 /*
53 * matcher implementations
54 */
56 static int
57 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
58 sdb_store_matcher_t *filter)
59 {
60 int status;
62 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
63 assert(OP_M(m)->left && OP_M(m)->right);
65 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
67 /* lazy evaluation */
68 if ((! status) && (m->type == MATCHER_AND))
69 return status;
70 else if (status && (m->type == MATCHER_OR))
71 return status;
73 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
74 } /* match_logical */
76 static int
77 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
78 sdb_store_matcher_t *filter)
79 {
80 assert(m->type == MATCHER_NOT);
81 assert(UOP_M(m)->op);
83 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
84 } /* match_unary */
86 static int
87 match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
88 sdb_store_matcher_t *filter)
89 {
90 sdb_avltree_iter_t *iter = NULL;
91 int status;
92 int all = (int)(m->type == MATCHER_ALL);
94 assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL));
96 /* TODO: support all object types */
97 if (obj->type != SDB_HOST)
98 return 0;
100 if (ITER_M(m)->type == SDB_SERVICE)
101 iter = sdb_avltree_get_iter(HOST(obj)->services);
102 else if (ITER_M(m)->type == SDB_METRIC)
103 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
104 else if (ITER_M(m)->type == SDB_ATTRIBUTE)
105 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
107 status = all;
108 while (sdb_avltree_iter_has_next(iter)) {
109 sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
110 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
111 continue;
113 if (sdb_store_matcher_matches(ITER_M(m)->m, child, filter)) {
114 if (! all) {
115 status = 1;
116 break;
117 }
118 } else if (all) {
119 status = 0;
120 break;
121 }
122 }
123 sdb_avltree_iter_destroy(iter);
124 return status;
125 } /* match_iter */
127 /*
128 * cmp_expr:
129 * Compare the values of two expressions when evaluating them using the
130 * specified stored object and filter. Returns a value less than, equal to, or
131 * greater than zero if the value of the first expression compares less than,
132 * equal to, or greater than the value of the second expression. Returns
133 * INT_MAX if any of the expressions could not be evaluated or if any of them
134 * evaluated to NULL.
135 */
136 static int
137 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
138 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
139 {
140 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
141 int status;
143 if (sdb_store_expr_eval(e1, obj, &v1, filter))
144 return INT_MAX;
145 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
146 sdb_data_free_datum(&v1);
147 return INT_MAX;
148 }
150 if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
151 status = INT_MAX;
152 else if (v1.type == v2.type)
153 status = sdb_data_cmp(&v1, &v2);
154 else if ((e1->data_type >= 0) && (e2->data_type >= 0))
155 status = INT_MAX;
156 else
157 status = sdb_data_strcmp(&v1, &v2);
159 sdb_data_free_datum(&v1);
160 sdb_data_free_datum(&v2);
161 return status;
162 } /* cmp_expr */
164 static int
165 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
166 sdb_store_matcher_t *filter)
167 {
168 int status;
169 assert(m->type == MATCHER_LT);
170 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
171 return (status != INT_MAX) && (status < 0);
172 } /* match_lt */
174 static int
175 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
176 sdb_store_matcher_t *filter)
177 {
178 int status;
179 assert(m->type == MATCHER_LE);
180 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
181 return (status != INT_MAX) && (status <= 0);
182 } /* match_le */
184 static int
185 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
186 sdb_store_matcher_t *filter)
187 {
188 int status;
189 assert(m->type == MATCHER_EQ);
190 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
191 return (status != INT_MAX) && (! status);
192 } /* match_eq */
194 static int
195 match_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
196 sdb_store_matcher_t *filter)
197 {
198 int status;
199 assert(m->type == MATCHER_NE);
200 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
201 return (status != INT_MAX) && status;
202 } /* match_ne */
204 static int
205 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
206 sdb_store_matcher_t *filter)
207 {
208 int status;
209 assert(m->type == MATCHER_GE);
210 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
211 return (status != INT_MAX) && (status >= 0);
212 } /* match_ge */
214 static int
215 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
216 sdb_store_matcher_t *filter)
217 {
218 int status;
219 assert(m->type == MATCHER_GT);
220 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
221 return (status != INT_MAX) && (status > 0);
222 } /* match_gt */
224 static int
225 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
226 sdb_store_matcher_t *filter)
227 {
228 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
229 int status = 1;
231 assert(m->type == MATCHER_IN);
233 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
234 || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
235 status = 0;
237 if (status)
238 status = sdb_data_inarray(&value, &array);
240 sdb_data_free_datum(&value);
241 sdb_data_free_datum(&array);
242 return status;
243 } /* match_in */
245 static int
246 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
247 sdb_store_matcher_t *filter)
248 {
249 sdb_data_t v = SDB_DATA_INIT;
250 int status = 0;
252 regex_t regex;
253 _Bool free_regex = 0;
255 assert((m->type == MATCHER_REGEX)
256 || (m->type == MATCHER_NREGEX));
258 if (! CMP_M(m)->right->type) {
259 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
260 regex = CMP_M(m)->right->data.data.re.regex;
261 }
262 else {
263 sdb_data_t tmp = SDB_DATA_INIT;
264 char *raw;
266 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
267 return 0;
269 if (tmp.type != SDB_TYPE_STRING) {
270 sdb_data_free_datum(&tmp);
271 return 0;
272 }
274 raw = tmp.data.string;
275 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
276 free(raw);
277 return 0;
278 }
280 regex = tmp.data.re.regex;
281 free_regex = 1;
282 free(tmp.data.re.raw);
283 free(raw);
284 }
286 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
287 || (sdb_data_isnull(&v)))
288 status = 0;
289 else {
290 char value[sdb_data_strlen(&v) + 1];
291 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
292 status = 0;
293 else if (! regexec(®ex, value, 0, NULL, 0))
294 status = 1;
295 }
297 if (free_regex)
298 regfree(®ex);
299 sdb_data_free_datum(&v);
300 if (m->type == MATCHER_NREGEX)
301 return !status;
302 return status;
303 } /* match_regex */
305 static int
306 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
307 sdb_store_matcher_t *filter)
308 {
309 sdb_data_t v = SDB_DATA_INIT;
310 int status;
312 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
314 /* TODO: this might hide real errors;
315 * improve error reporting and propagation */
316 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
317 || sdb_data_isnull(&v))
318 status = 1;
319 else
320 status = 0;
322 sdb_data_free_datum(&v);
323 if (m->type == MATCHER_ISNNULL)
324 return !status;
325 return status;
326 } /* match_isnull */
328 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
329 sdb_store_matcher_t *);
331 /* this array needs to be indexable by the matcher types;
332 * -> update the enum in store-private.h when updating this */
333 static matcher_cb
334 matchers[] = {
335 match_logical,
336 match_logical,
337 match_unary,
338 match_iter,
339 match_iter,
340 match_lt,
341 match_le,
342 match_eq,
343 match_ne,
344 match_ge,
345 match_gt,
346 match_in,
347 match_regex,
348 match_regex,
349 match_isnull,
350 match_isnull,
351 };
353 /*
354 * private matcher types
355 */
357 static int
358 op_matcher_init(sdb_object_t *obj, va_list ap)
359 {
360 M(obj)->type = va_arg(ap, int);
361 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
362 return -1;
364 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
365 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
366 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
367 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
369 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
370 return -1;
371 return 0;
372 } /* op_matcher_init */
374 static void
375 op_matcher_destroy(sdb_object_t *obj)
376 {
377 if (OP_M(obj)->left)
378 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
379 if (OP_M(obj)->right)
380 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
381 } /* op_matcher_destroy */
383 static int
384 iter_matcher_init(sdb_object_t *obj, va_list ap)
385 {
386 M(obj)->type = va_arg(ap, int);
387 ITER_M(obj)->type = va_arg(ap, int);
388 ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
390 if (! ITER_M(obj)->m)
391 return -1;
393 sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
394 return 0;
395 } /* iter_matcher_init */
397 static void
398 iter_matcher_destroy(sdb_object_t *obj)
399 {
400 sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
401 } /* iter_matcher_destroy */
403 static int
404 cmp_matcher_init(sdb_object_t *obj, va_list ap)
405 {
406 M(obj)->type = va_arg(ap, int);
408 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
409 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
410 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
411 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
413 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
414 return -1;
415 return 0;
416 } /* cmp_matcher_init */
418 static void
419 cmp_matcher_destroy(sdb_object_t *obj)
420 {
421 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
422 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
423 } /* cmp_matcher_destroy */
425 static int
426 uop_matcher_init(sdb_object_t *obj, va_list ap)
427 {
428 M(obj)->type = va_arg(ap, int);
429 if (M(obj)->type != MATCHER_NOT)
430 return -1;
432 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
433 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
435 if (! UOP_M(obj)->op)
436 return -1;
437 return 0;
438 } /* uop_matcher_init */
440 static void
441 uop_matcher_destroy(sdb_object_t *obj)
442 {
443 if (UOP_M(obj)->op)
444 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
445 } /* uop_matcher_destroy */
447 static int
448 isnull_matcher_init(sdb_object_t *obj, va_list ap)
449 {
450 M(obj)->type = va_arg(ap, int);
451 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
452 return -1;
454 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
455 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
456 return 0;
457 } /* isnull_matcher_init */
459 static void
460 isnull_matcher_destroy(sdb_object_t *obj)
461 {
462 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
463 ISNULL_M(obj)->expr = NULL;
464 } /* isnull_matcher_destroy */
466 static sdb_type_t op_type = {
467 /* size = */ sizeof(op_matcher_t),
468 /* init = */ op_matcher_init,
469 /* destroy = */ op_matcher_destroy,
470 };
472 static sdb_type_t uop_type = {
473 /* size = */ sizeof(uop_matcher_t),
474 /* init = */ uop_matcher_init,
475 /* destroy = */ uop_matcher_destroy,
476 };
478 static sdb_type_t iter_type = {
479 /* size = */ sizeof(iter_matcher_t),
480 /* init = */ iter_matcher_init,
481 /* destroy = */ iter_matcher_destroy,
482 };
484 static sdb_type_t cmp_type = {
485 /* size = */ sizeof(cmp_matcher_t),
486 /* init = */ cmp_matcher_init,
487 /* destroy = */ cmp_matcher_destroy,
488 };
490 static sdb_type_t isnull_type = {
491 /* size = */ sizeof(isnull_matcher_t),
492 /* init = */ isnull_matcher_init,
493 /* destroy = */ isnull_matcher_destroy,
494 };
496 /*
497 * public API
498 */
500 sdb_store_matcher_t *
501 sdb_store_any_matcher(int type, sdb_store_matcher_t *m)
502 {
503 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
504 && (type != SDB_ATTRIBUTE))
505 return NULL;
506 return M(sdb_object_create("any-matcher", iter_type,
507 MATCHER_ANY, type, m));
508 } /* sdb_store_any_matcher */
510 sdb_store_matcher_t *
511 sdb_store_all_matcher(int type, sdb_store_matcher_t *m)
512 {
513 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
514 && (type != SDB_ATTRIBUTE))
515 return NULL;
516 return M(sdb_object_create("all-matcher", iter_type,
517 MATCHER_ALL, type, m));
518 } /* sdb_store_all_matcher */
520 sdb_store_matcher_t *
521 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
522 {
523 return M(sdb_object_create("lt-matcher", cmp_type,
524 MATCHER_LT, left, right));
525 } /* sdb_store_lt_matcher */
527 sdb_store_matcher_t *
528 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
529 {
530 return M(sdb_object_create("le-matcher", cmp_type,
531 MATCHER_LE, left, right));
532 } /* sdb_store_le_matcher */
534 sdb_store_matcher_t *
535 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
536 {
537 return M(sdb_object_create("eq-matcher", cmp_type,
538 MATCHER_EQ, left, right));
539 } /* sdb_store_eq_matcher */
541 sdb_store_matcher_t *
542 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
543 {
544 return M(sdb_object_create("ne-matcher", cmp_type,
545 MATCHER_NE, left, right));
546 } /* sdb_store_ne_matcher */
548 sdb_store_matcher_t *
549 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
550 {
551 return M(sdb_object_create("ge-matcher", cmp_type,
552 MATCHER_GE, left, right));
553 } /* sdb_store_ge_matcher */
555 sdb_store_matcher_t *
556 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
557 {
558 return M(sdb_object_create("gt-matcher", cmp_type,
559 MATCHER_GT, left, right));
560 } /* sdb_store_gt_matcher */
562 sdb_store_matcher_t *
563 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
564 {
565 return M(sdb_object_create("in-matcher", cmp_type,
566 MATCHER_IN, left, right));
567 } /* sdb_store_in_matcher */
569 sdb_store_matcher_t *
570 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
571 {
572 if (! right->type) {
573 if ((right->data.type != SDB_TYPE_STRING)
574 && (right->data.type != SDB_TYPE_REGEX))
575 return NULL;
577 if (right->data.type == SDB_TYPE_STRING) {
578 char *raw = right->data.data.string;
579 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
580 return NULL;
581 free(raw);
582 }
583 }
584 return M(sdb_object_create("regex-matcher", cmp_type,
585 MATCHER_REGEX, left, right));
586 } /* sdb_store_regex_matcher */
588 sdb_store_matcher_t *
589 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
590 {
591 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
592 if (! m)
593 return NULL;
594 m->type = MATCHER_NREGEX;
595 return m;
596 } /* sdb_store_nregex_matcher */
598 sdb_store_matcher_t *
599 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
600 {
601 return M(sdb_object_create("isnull-matcher", isnull_type,
602 MATCHER_ISNULL, expr));
603 } /* sdb_store_isnull_matcher */
605 sdb_store_matcher_t *
606 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
607 {
608 return M(sdb_object_create("isnull-matcher", isnull_type,
609 MATCHER_ISNNULL, expr));
610 } /* sdb_store_isnnull_matcher */
612 sdb_store_matcher_op_cb
613 sdb_store_parse_matcher_op(const char *op)
614 {
615 if (! strcasecmp(op, "<"))
616 return sdb_store_lt_matcher;
617 else if (! strcasecmp(op, "<="))
618 return sdb_store_le_matcher;
619 else if (! strcasecmp(op, "="))
620 return sdb_store_eq_matcher;
621 else if (! strcasecmp(op, "!="))
622 return sdb_store_ne_matcher;
623 else if (! strcasecmp(op, ">="))
624 return sdb_store_ge_matcher;
625 else if (! strcasecmp(op, ">"))
626 return sdb_store_gt_matcher;
627 else if (! strcasecmp(op, "=~"))
628 return sdb_store_regex_matcher;
629 else if (! strcasecmp(op, "!~"))
630 return sdb_store_nregex_matcher;
631 return NULL;
632 } /* sdb_store_parse_matcher_op */
634 int
635 sdb_store_parse_object_type(const char *name)
636 {
637 if (! strcasecmp(name, "host"))
638 return SDB_HOST;
639 else if (! strcasecmp(name, "service"))
640 return SDB_SERVICE;
641 else if (! strcasecmp(name, "metric"))
642 return SDB_METRIC;
643 else if (! strcasecmp(name, "attribute"))
644 return SDB_ATTRIBUTE;
645 return -1;
646 } /* sdb_store_parse_object_type */
648 int
649 sdb_store_parse_object_type_plural(const char *name)
650 {
651 if (! strcasecmp(name, "hosts"))
652 return SDB_HOST;
653 else if (! strcasecmp(name, "services"))
654 return SDB_SERVICE;
655 else if (! strcasecmp(name, "metrics"))
656 return SDB_METRIC;
657 else if (! strcasecmp(name, "attributes"))
658 return SDB_ATTRIBUTE;
659 return -1;
660 } /* sdb_store_parse_object_type_plural */
662 int
663 sdb_store_parse_field_name(const char *name)
664 {
665 if (! strcasecmp(name, "name"))
666 return SDB_FIELD_NAME;
667 else if (! strcasecmp(name, "last_update"))
668 return SDB_FIELD_LAST_UPDATE;
669 else if (! strcasecmp(name, "age"))
670 return SDB_FIELD_AGE;
671 else if (! strcasecmp(name, "interval"))
672 return SDB_FIELD_INTERVAL;
673 else if (! strcasecmp(name, "backend"))
674 return SDB_FIELD_BACKEND;
675 return -1;
676 } /* sdb_store_parse_field_name */
678 sdb_store_matcher_t *
679 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
680 {
681 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
682 left, right));
683 } /* sdb_store_dis_matcher */
685 sdb_store_matcher_t *
686 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
687 {
688 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
689 left, right));
690 } /* sdb_store_con_matcher */
692 sdb_store_matcher_t *
693 sdb_store_inv_matcher(sdb_store_matcher_t *m)
694 {
695 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
696 } /* sdb_store_inv_matcher */
698 int
699 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
700 sdb_store_matcher_t *filter)
701 {
702 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
703 return 0;
705 /* "NULL" always matches */
706 if ((! m) || (! obj))
707 return 1;
709 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
710 return 0;
712 return matchers[m->type](m, obj, filter);
713 } /* sdb_store_matcher_matches */
715 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */