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 if (obj->type == SDB_HOST) {
97 if (ITER_M(m)->type == SDB_SERVICE)
98 iter = sdb_avltree_get_iter(HOST(obj)->services);
99 else if (ITER_M(m)->type == SDB_METRIC)
100 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
101 else if (ITER_M(m)->type == SDB_ATTRIBUTE)
102 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
103 } else if (obj->type == SDB_SERVICE) {
104 if (ITER_M(m)->type == SDB_ATTRIBUTE)
105 iter = sdb_avltree_get_iter(SVC(obj)->attributes);
106 } else if (obj->type == SDB_METRIC) {
107 if (ITER_M(m)->type == SDB_ATTRIBUTE)
108 iter = sdb_avltree_get_iter(METRIC(obj)->attributes);
109 }
111 status = all;
112 while (sdb_avltree_iter_has_next(iter)) {
113 sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
114 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
115 continue;
117 if (sdb_store_matcher_matches(ITER_M(m)->m, child, filter)) {
118 if (! all) {
119 status = 1;
120 break;
121 }
122 } else if (all) {
123 status = 0;
124 break;
125 }
126 }
127 sdb_avltree_iter_destroy(iter);
128 return status;
129 } /* match_iter */
131 /*
132 * cmp_expr:
133 * Compare the values of two expressions when evaluating them using the
134 * specified stored object and filter. Returns a value less than, equal to, or
135 * greater than zero if the value of the first expression compares less than,
136 * equal to, or greater than the value of the second expression. Returns
137 * INT_MAX if any of the expressions could not be evaluated or if any of them
138 * evaluated to NULL.
139 */
140 static int
141 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
142 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
143 {
144 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
145 int status;
147 if (sdb_store_expr_eval(e1, obj, &v1, filter))
148 return INT_MAX;
149 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
150 sdb_data_free_datum(&v1);
151 return INT_MAX;
152 }
154 if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
155 status = INT_MAX;
156 else if (v1.type == v2.type)
157 status = sdb_data_cmp(&v1, &v2);
158 else if ((e1->data_type >= 0) && (e2->data_type >= 0))
159 status = INT_MAX;
160 else
161 status = sdb_data_strcmp(&v1, &v2);
163 sdb_data_free_datum(&v1);
164 sdb_data_free_datum(&v2);
165 return status;
166 } /* cmp_expr */
168 static int
169 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
170 sdb_store_matcher_t *filter)
171 {
172 int status;
173 assert(m->type == MATCHER_LT);
174 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
175 return (status != INT_MAX) && (status < 0);
176 } /* match_lt */
178 static int
179 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
180 sdb_store_matcher_t *filter)
181 {
182 int status;
183 assert(m->type == MATCHER_LE);
184 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
185 return (status != INT_MAX) && (status <= 0);
186 } /* match_le */
188 static int
189 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
190 sdb_store_matcher_t *filter)
191 {
192 int status;
193 assert(m->type == MATCHER_EQ);
194 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
195 return (status != INT_MAX) && (! status);
196 } /* match_eq */
198 static int
199 match_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
200 sdb_store_matcher_t *filter)
201 {
202 int status;
203 assert(m->type == MATCHER_NE);
204 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
205 return (status != INT_MAX) && status;
206 } /* match_ne */
208 static int
209 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
210 sdb_store_matcher_t *filter)
211 {
212 int status;
213 assert(m->type == MATCHER_GE);
214 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
215 return (status != INT_MAX) && (status >= 0);
216 } /* match_ge */
218 static int
219 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
220 sdb_store_matcher_t *filter)
221 {
222 int status;
223 assert(m->type == MATCHER_GT);
224 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
225 return (status != INT_MAX) && (status > 0);
226 } /* match_gt */
228 static int
229 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
230 sdb_store_matcher_t *filter)
231 {
232 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
233 int status = 1;
235 assert(m->type == MATCHER_IN);
237 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
238 || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
239 status = 0;
241 if (status)
242 status = sdb_data_inarray(&value, &array);
244 sdb_data_free_datum(&value);
245 sdb_data_free_datum(&array);
246 return status;
247 } /* match_in */
249 static int
250 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
251 sdb_store_matcher_t *filter)
252 {
253 sdb_data_t v = SDB_DATA_INIT;
254 int status = 0;
256 regex_t regex;
257 _Bool free_regex = 0;
259 assert((m->type == MATCHER_REGEX)
260 || (m->type == MATCHER_NREGEX));
262 if (! CMP_M(m)->right->type) {
263 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
264 regex = CMP_M(m)->right->data.data.re.regex;
265 }
266 else {
267 sdb_data_t tmp = SDB_DATA_INIT;
268 char *raw;
270 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
271 return 0;
273 if (tmp.type != SDB_TYPE_STRING) {
274 sdb_data_free_datum(&tmp);
275 return 0;
276 }
278 raw = tmp.data.string;
279 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
280 free(raw);
281 return 0;
282 }
284 regex = tmp.data.re.regex;
285 free_regex = 1;
286 free(tmp.data.re.raw);
287 free(raw);
288 }
290 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
291 || (sdb_data_isnull(&v)))
292 status = 0;
293 else {
294 char value[sdb_data_strlen(&v) + 1];
295 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
296 status = 0;
297 else if (! regexec(®ex, value, 0, NULL, 0))
298 status = 1;
299 }
301 if (free_regex)
302 regfree(®ex);
303 sdb_data_free_datum(&v);
304 if (m->type == MATCHER_NREGEX)
305 return !status;
306 return status;
307 } /* match_regex */
309 static int
310 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
311 sdb_store_matcher_t *filter)
312 {
313 sdb_data_t v = SDB_DATA_INIT;
314 int status;
316 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
318 /* TODO: this might hide real errors;
319 * improve error reporting and propagation */
320 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
321 || sdb_data_isnull(&v))
322 status = 1;
323 else
324 status = 0;
326 sdb_data_free_datum(&v);
327 if (m->type == MATCHER_ISNNULL)
328 return !status;
329 return status;
330 } /* match_isnull */
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_unary,
342 match_iter,
343 match_iter,
344 match_lt,
345 match_le,
346 match_eq,
347 match_ne,
348 match_ge,
349 match_gt,
350 match_in,
351 match_regex,
352 match_regex,
353 match_isnull,
354 match_isnull,
355 };
357 /*
358 * private matcher types
359 */
361 static int
362 op_matcher_init(sdb_object_t *obj, va_list ap)
363 {
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)
380 {
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)
389 {
390 M(obj)->type = va_arg(ap, int);
391 ITER_M(obj)->type = va_arg(ap, int);
392 ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
394 if (! ITER_M(obj)->m)
395 return -1;
397 sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
398 return 0;
399 } /* iter_matcher_init */
401 static void
402 iter_matcher_destroy(sdb_object_t *obj)
403 {
404 sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
405 } /* iter_matcher_destroy */
407 static int
408 cmp_matcher_init(sdb_object_t *obj, va_list ap)
409 {
410 M(obj)->type = va_arg(ap, int);
412 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
413 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
414 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
415 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
417 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
418 return -1;
419 return 0;
420 } /* cmp_matcher_init */
422 static void
423 cmp_matcher_destroy(sdb_object_t *obj)
424 {
425 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
426 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
427 } /* cmp_matcher_destroy */
429 static int
430 uop_matcher_init(sdb_object_t *obj, va_list ap)
431 {
432 M(obj)->type = va_arg(ap, int);
433 if (M(obj)->type != MATCHER_NOT)
434 return -1;
436 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
437 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
439 if (! UOP_M(obj)->op)
440 return -1;
441 return 0;
442 } /* uop_matcher_init */
444 static void
445 uop_matcher_destroy(sdb_object_t *obj)
446 {
447 if (UOP_M(obj)->op)
448 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
449 } /* uop_matcher_destroy */
451 static int
452 isnull_matcher_init(sdb_object_t *obj, va_list ap)
453 {
454 M(obj)->type = va_arg(ap, int);
455 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
456 return -1;
458 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
459 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
460 return 0;
461 } /* isnull_matcher_init */
463 static void
464 isnull_matcher_destroy(sdb_object_t *obj)
465 {
466 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
467 ISNULL_M(obj)->expr = NULL;
468 } /* isnull_matcher_destroy */
470 static sdb_type_t op_type = {
471 /* size = */ sizeof(op_matcher_t),
472 /* init = */ op_matcher_init,
473 /* destroy = */ op_matcher_destroy,
474 };
476 static sdb_type_t uop_type = {
477 /* size = */ sizeof(uop_matcher_t),
478 /* init = */ uop_matcher_init,
479 /* destroy = */ uop_matcher_destroy,
480 };
482 static sdb_type_t iter_type = {
483 /* size = */ sizeof(iter_matcher_t),
484 /* init = */ iter_matcher_init,
485 /* destroy = */ iter_matcher_destroy,
486 };
488 static sdb_type_t cmp_type = {
489 /* size = */ sizeof(cmp_matcher_t),
490 /* init = */ cmp_matcher_init,
491 /* destroy = */ cmp_matcher_destroy,
492 };
494 static sdb_type_t isnull_type = {
495 /* size = */ sizeof(isnull_matcher_t),
496 /* init = */ isnull_matcher_init,
497 /* destroy = */ isnull_matcher_destroy,
498 };
500 /*
501 * public API
502 */
504 sdb_store_matcher_t *
505 sdb_store_any_matcher(int type, sdb_store_matcher_t *m)
506 {
507 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
508 && (type != SDB_ATTRIBUTE))
509 return NULL;
510 return M(sdb_object_create("any-matcher", iter_type,
511 MATCHER_ANY, type, m));
512 } /* sdb_store_any_matcher */
514 sdb_store_matcher_t *
515 sdb_store_all_matcher(int type, sdb_store_matcher_t *m)
516 {
517 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
518 && (type != SDB_ATTRIBUTE))
519 return NULL;
520 return M(sdb_object_create("all-matcher", iter_type,
521 MATCHER_ALL, type, m));
522 } /* sdb_store_all_matcher */
524 sdb_store_matcher_t *
525 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
526 {
527 return M(sdb_object_create("lt-matcher", cmp_type,
528 MATCHER_LT, left, right));
529 } /* sdb_store_lt_matcher */
531 sdb_store_matcher_t *
532 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
533 {
534 return M(sdb_object_create("le-matcher", cmp_type,
535 MATCHER_LE, left, right));
536 } /* sdb_store_le_matcher */
538 sdb_store_matcher_t *
539 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
540 {
541 return M(sdb_object_create("eq-matcher", cmp_type,
542 MATCHER_EQ, left, right));
543 } /* sdb_store_eq_matcher */
545 sdb_store_matcher_t *
546 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
547 {
548 return M(sdb_object_create("ne-matcher", cmp_type,
549 MATCHER_NE, left, right));
550 } /* sdb_store_ne_matcher */
552 sdb_store_matcher_t *
553 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
554 {
555 return M(sdb_object_create("ge-matcher", cmp_type,
556 MATCHER_GE, left, right));
557 } /* sdb_store_ge_matcher */
559 sdb_store_matcher_t *
560 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
561 {
562 return M(sdb_object_create("gt-matcher", cmp_type,
563 MATCHER_GT, left, right));
564 } /* sdb_store_gt_matcher */
566 sdb_store_matcher_t *
567 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
568 {
569 return M(sdb_object_create("in-matcher", cmp_type,
570 MATCHER_IN, left, right));
571 } /* sdb_store_in_matcher */
573 sdb_store_matcher_t *
574 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
575 {
576 if (! right->type) {
577 if ((right->data.type != SDB_TYPE_STRING)
578 && (right->data.type != SDB_TYPE_REGEX))
579 return NULL;
581 if (right->data.type == SDB_TYPE_STRING) {
582 char *raw = right->data.data.string;
583 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
584 return NULL;
585 free(raw);
586 }
587 }
588 return M(sdb_object_create("regex-matcher", cmp_type,
589 MATCHER_REGEX, left, right));
590 } /* sdb_store_regex_matcher */
592 sdb_store_matcher_t *
593 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
594 {
595 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
596 if (! m)
597 return NULL;
598 m->type = MATCHER_NREGEX;
599 return m;
600 } /* sdb_store_nregex_matcher */
602 sdb_store_matcher_t *
603 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
604 {
605 return M(sdb_object_create("isnull-matcher", isnull_type,
606 MATCHER_ISNULL, expr));
607 } /* sdb_store_isnull_matcher */
609 sdb_store_matcher_t *
610 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
611 {
612 return M(sdb_object_create("isnull-matcher", isnull_type,
613 MATCHER_ISNNULL, expr));
614 } /* sdb_store_isnnull_matcher */
616 sdb_store_matcher_op_cb
617 sdb_store_parse_matcher_op(const char *op)
618 {
619 if (! strcasecmp(op, "<"))
620 return sdb_store_lt_matcher;
621 else if (! strcasecmp(op, "<="))
622 return sdb_store_le_matcher;
623 else if (! strcasecmp(op, "="))
624 return sdb_store_eq_matcher;
625 else if (! strcasecmp(op, "!="))
626 return sdb_store_ne_matcher;
627 else if (! strcasecmp(op, ">="))
628 return sdb_store_ge_matcher;
629 else if (! strcasecmp(op, ">"))
630 return sdb_store_gt_matcher;
631 else if (! strcasecmp(op, "=~"))
632 return sdb_store_regex_matcher;
633 else if (! strcasecmp(op, "!~"))
634 return sdb_store_nregex_matcher;
635 return NULL;
636 } /* sdb_store_parse_matcher_op */
638 int
639 sdb_store_parse_object_type(const char *name)
640 {
641 if (! strcasecmp(name, "host"))
642 return SDB_HOST;
643 else if (! strcasecmp(name, "service"))
644 return SDB_SERVICE;
645 else if (! strcasecmp(name, "metric"))
646 return SDB_METRIC;
647 else if (! strcasecmp(name, "attribute"))
648 return SDB_ATTRIBUTE;
649 return -1;
650 } /* sdb_store_parse_object_type */
652 int
653 sdb_store_parse_object_type_plural(const char *name)
654 {
655 if (! strcasecmp(name, "hosts"))
656 return SDB_HOST;
657 else if (! strcasecmp(name, "services"))
658 return SDB_SERVICE;
659 else if (! strcasecmp(name, "metrics"))
660 return SDB_METRIC;
661 else if (! strcasecmp(name, "attributes"))
662 return SDB_ATTRIBUTE;
663 return -1;
664 } /* sdb_store_parse_object_type_plural */
666 int
667 sdb_store_parse_field_name(const char *name)
668 {
669 if (! strcasecmp(name, "name"))
670 return SDB_FIELD_NAME;
671 else if (! strcasecmp(name, "last_update"))
672 return SDB_FIELD_LAST_UPDATE;
673 else if (! strcasecmp(name, "age"))
674 return SDB_FIELD_AGE;
675 else if (! strcasecmp(name, "interval"))
676 return SDB_FIELD_INTERVAL;
677 else if (! strcasecmp(name, "backend"))
678 return SDB_FIELD_BACKEND;
679 return -1;
680 } /* sdb_store_parse_field_name */
682 sdb_store_matcher_t *
683 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
684 {
685 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
686 left, right));
687 } /* sdb_store_dis_matcher */
689 sdb_store_matcher_t *
690 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
691 {
692 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
693 left, right));
694 } /* sdb_store_con_matcher */
696 sdb_store_matcher_t *
697 sdb_store_inv_matcher(sdb_store_matcher_t *m)
698 {
699 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
700 } /* sdb_store_inv_matcher */
702 int
703 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
704 sdb_store_matcher_t *filter)
705 {
706 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
707 return 0;
709 /* "NULL" always matches */
710 if ((! m) || (! obj))
711 return 1;
713 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
714 return 0;
716 return matchers[m->type](m, obj, filter);
717 } /* sdb_store_matcher_matches */
719 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */