11d85fea0a5ead05b42f6fffd089b6b135b0bcb9
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 * private data types
54 */
56 typedef struct {
57 sdb_store_matcher_t *m;
58 sdb_store_matcher_t *filter;
59 sdb_store_lookup_cb cb;
60 void *user_data;
61 } scan_iter_data_t;
63 /*
64 * private helper functions
65 */
67 static int
68 scan_iter(sdb_store_obj_t *obj, void *user_data)
69 {
70 scan_iter_data_t *d = user_data;
72 if (sdb_store_matcher_matches(d->m, obj, d->filter))
73 return d->cb(obj, d->user_data);
74 return 0;
75 } /* scan_iter */
77 /*
78 * matcher implementations
79 */
81 static int
82 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
83 sdb_store_matcher_t *filter)
84 {
85 int status;
87 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
88 assert(OP_M(m)->left && OP_M(m)->right);
90 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
92 /* lazy evaluation */
93 if ((! status) && (m->type == MATCHER_AND))
94 return status;
95 else if (status && (m->type == MATCHER_OR))
96 return status;
98 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
99 } /* match_logical */
101 static int
102 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
103 sdb_store_matcher_t *filter)
104 {
105 assert(m->type == MATCHER_NOT);
106 assert(UOP_M(m)->op);
108 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
109 } /* match_unary */
111 static int
112 match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
113 sdb_store_matcher_t *filter)
114 {
115 sdb_avltree_iter_t *iter = NULL;
116 int status;
117 int all = 0;
119 assert(m->type == MATCHER_ANY);
121 /* TODO: support all object types */
122 if (obj->type != SDB_HOST)
123 return 0;
125 if (ITER_M(m)->type == SDB_SERVICE)
126 iter = sdb_avltree_get_iter(HOST(obj)->services);
127 else if (ITER_M(m)->type == SDB_METRIC)
128 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
129 else if (ITER_M(m)->type == SDB_ATTRIBUTE)
130 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
132 status = all;
133 while (sdb_avltree_iter_has_next(iter)) {
134 sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
135 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
136 continue;
138 if (sdb_store_matcher_matches(ITER_M(m)->m, child, filter)) {
139 if (! all) {
140 status = 1;
141 break;
142 }
143 } else if (all) {
144 status = 0;
145 break;
146 }
147 }
148 sdb_avltree_iter_destroy(iter);
149 return status;
150 } /* match_iter */
152 /*
153 * cmp_expr:
154 * Compare the values of two expressions when evaluating them using the
155 * specified stored object and filter. Returns a value less than, equal to, or
156 * greater than zero if the value of the first expression compares less than,
157 * equal to, or greater than the value of the second expression. Returns
158 * INT_MAX if any of the expressions could not be evaluated or if any of them
159 * evaluated to NULL.
160 */
161 static int
162 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
163 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
164 {
165 sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
166 int status;
168 if (sdb_store_expr_eval(e1, obj, &v1, filter))
169 return INT_MAX;
170 if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
171 sdb_data_free_datum(&v1);
172 return INT_MAX;
173 }
175 if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
176 status = INT_MAX;
177 else if (v1.type == v2.type)
178 status = sdb_data_cmp(&v1, &v2);
179 else if ((e1->data_type >= 0) && (e2->data_type >= 0))
180 status = INT_MAX;
181 else
182 status = sdb_data_strcmp(&v1, &v2);
184 sdb_data_free_datum(&v1);
185 sdb_data_free_datum(&v2);
186 return status;
187 } /* cmp_expr */
189 static int
190 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
191 sdb_store_matcher_t *filter)
192 {
193 int status;
194 assert(m->type == MATCHER_LT);
195 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
196 return (status != INT_MAX) && (status < 0);
197 } /* match_lt */
199 static int
200 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
201 sdb_store_matcher_t *filter)
202 {
203 int status;
204 assert(m->type == MATCHER_LE);
205 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
206 return (status != INT_MAX) && (status <= 0);
207 } /* match_le */
209 static int
210 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
211 sdb_store_matcher_t *filter)
212 {
213 int status;
214 assert(m->type == MATCHER_EQ);
215 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
216 return (status != INT_MAX) && (! status);
217 } /* match_eq */
219 static int
220 match_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
221 sdb_store_matcher_t *filter)
222 {
223 int status;
224 assert(m->type == MATCHER_NE);
225 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
226 return (status != INT_MAX) && status;
227 } /* match_ne */
229 static int
230 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
231 sdb_store_matcher_t *filter)
232 {
233 int status;
234 assert(m->type == MATCHER_GE);
235 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
236 return (status != INT_MAX) && (status >= 0);
237 } /* match_ge */
239 static int
240 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
241 sdb_store_matcher_t *filter)
242 {
243 int status;
244 assert(m->type == MATCHER_GT);
245 status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
246 return (status != INT_MAX) && (status > 0);
247 } /* match_gt */
249 static int
250 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
251 sdb_store_matcher_t *filter)
252 {
253 sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
254 int status = 1;
256 assert(m->type == MATCHER_IN);
258 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
259 || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
260 status = 0;
262 if (status)
263 status = sdb_data_inarray(&value, &array);
265 sdb_data_free_datum(&value);
266 sdb_data_free_datum(&array);
267 return status;
268 } /* match_in */
270 static int
271 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
272 sdb_store_matcher_t *filter)
273 {
274 sdb_data_t v = SDB_DATA_INIT;
275 int status = 0;
277 regex_t regex;
278 _Bool free_regex = 0;
280 assert((m->type == MATCHER_REGEX)
281 || (m->type == MATCHER_NREGEX));
283 if (! CMP_M(m)->right->type) {
284 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
285 regex = CMP_M(m)->right->data.data.re.regex;
286 }
287 else {
288 sdb_data_t tmp = SDB_DATA_INIT;
289 char *raw;
291 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
292 return 0;
294 if (tmp.type != SDB_TYPE_STRING) {
295 sdb_data_free_datum(&tmp);
296 return 0;
297 }
299 raw = tmp.data.string;
300 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
301 free(raw);
302 return 0;
303 }
305 regex = tmp.data.re.regex;
306 free_regex = 1;
307 free(tmp.data.re.raw);
308 free(raw);
309 }
311 if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
312 || (sdb_data_isnull(&v)))
313 status = 0;
314 else {
315 char value[sdb_data_strlen(&v) + 1];
316 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
317 status = 0;
318 else if (! regexec(®ex, value, 0, NULL, 0))
319 status = 1;
320 }
322 if (free_regex)
323 regfree(®ex);
324 sdb_data_free_datum(&v);
325 if (m->type == MATCHER_NREGEX)
326 return !status;
327 return status;
328 } /* match_regex */
330 static int
331 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
332 sdb_store_matcher_t *filter)
333 {
334 sdb_data_t v = SDB_DATA_INIT;
335 int status;
337 assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
339 /* TODO: this might hide real errors;
340 * improve error reporting and propagation */
341 if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
342 || sdb_data_isnull(&v))
343 status = 1;
344 else
345 status = 0;
347 sdb_data_free_datum(&v);
348 if (m->type == MATCHER_ISNNULL)
349 return !status;
350 return status;
351 } /* match_isnull */
353 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
354 sdb_store_matcher_t *);
356 /* this array needs to be indexable by the matcher types;
357 * -> update the enum in store-private.h when updating this */
358 static matcher_cb
359 matchers[] = {
360 match_logical,
361 match_logical,
362 match_unary,
363 match_iter,
364 match_lt,
365 match_le,
366 match_eq,
367 match_ne,
368 match_ge,
369 match_gt,
370 match_in,
371 match_regex,
372 match_regex,
373 match_isnull,
374 match_isnull,
375 };
377 /*
378 * private matcher types
379 */
381 static int
382 op_matcher_init(sdb_object_t *obj, va_list ap)
383 {
384 M(obj)->type = va_arg(ap, int);
385 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
386 return -1;
388 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
389 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
390 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
391 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
393 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
394 return -1;
395 return 0;
396 } /* op_matcher_init */
398 static void
399 op_matcher_destroy(sdb_object_t *obj)
400 {
401 if (OP_M(obj)->left)
402 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
403 if (OP_M(obj)->right)
404 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
405 } /* op_matcher_destroy */
407 static int
408 iter_matcher_init(sdb_object_t *obj, va_list ap)
409 {
410 M(obj)->type = va_arg(ap, int);
411 ITER_M(obj)->type = va_arg(ap, int);
412 ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
414 if (! ITER_M(obj)->m)
415 return -1;
417 sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
418 return 0;
419 } /* iter_matcher_init */
421 static void
422 iter_matcher_destroy(sdb_object_t *obj)
423 {
424 sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
425 } /* iter_matcher_destroy */
427 static int
428 cmp_matcher_init(sdb_object_t *obj, va_list ap)
429 {
430 M(obj)->type = va_arg(ap, int);
432 CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
433 sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
434 CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
435 sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
437 if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
438 return -1;
439 return 0;
440 } /* cmp_matcher_init */
442 static void
443 cmp_matcher_destroy(sdb_object_t *obj)
444 {
445 sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
446 sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
447 } /* cmp_matcher_destroy */
449 static int
450 uop_matcher_init(sdb_object_t *obj, va_list ap)
451 {
452 M(obj)->type = va_arg(ap, int);
453 if (M(obj)->type != MATCHER_NOT)
454 return -1;
456 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
457 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
459 if (! UOP_M(obj)->op)
460 return -1;
461 return 0;
462 } /* uop_matcher_init */
464 static void
465 uop_matcher_destroy(sdb_object_t *obj)
466 {
467 if (UOP_M(obj)->op)
468 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
469 } /* uop_matcher_destroy */
471 static int
472 isnull_matcher_init(sdb_object_t *obj, va_list ap)
473 {
474 M(obj)->type = va_arg(ap, int);
475 if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
476 return -1;
478 ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
479 sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
480 return 0;
481 } /* isnull_matcher_init */
483 static void
484 isnull_matcher_destroy(sdb_object_t *obj)
485 {
486 sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
487 ISNULL_M(obj)->expr = NULL;
488 } /* isnull_matcher_destroy */
490 static sdb_type_t op_type = {
491 /* size = */ sizeof(op_matcher_t),
492 /* init = */ op_matcher_init,
493 /* destroy = */ op_matcher_destroy,
494 };
496 static sdb_type_t uop_type = {
497 /* size = */ sizeof(uop_matcher_t),
498 /* init = */ uop_matcher_init,
499 /* destroy = */ uop_matcher_destroy,
500 };
502 static sdb_type_t iter_type = {
503 /* size = */ sizeof(iter_matcher_t),
504 /* init = */ iter_matcher_init,
505 /* destroy = */ iter_matcher_destroy,
506 };
508 static sdb_type_t cmp_type = {
509 /* size = */ sizeof(cmp_matcher_t),
510 /* init = */ cmp_matcher_init,
511 /* destroy = */ cmp_matcher_destroy,
512 };
514 static sdb_type_t isnull_type = {
515 /* size = */ sizeof(isnull_matcher_t),
516 /* init = */ isnull_matcher_init,
517 /* destroy = */ isnull_matcher_destroy,
518 };
520 /*
521 * public API
522 */
524 sdb_store_matcher_t *
525 sdb_store_any_matcher(int type, sdb_store_matcher_t *m)
526 {
527 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
528 && (type != SDB_ATTRIBUTE))
529 return NULL;
530 return M(sdb_object_create("any-matcher", iter_type,
531 MATCHER_ANY, type, m));
532 } /* sdb_store_iter_matcher */
534 sdb_store_matcher_t *
535 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
536 {
537 return M(sdb_object_create("lt-matcher", cmp_type,
538 MATCHER_LT, left, right));
539 } /* sdb_store_lt_matcher */
541 sdb_store_matcher_t *
542 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
543 {
544 return M(sdb_object_create("le-matcher", cmp_type,
545 MATCHER_LE, left, right));
546 } /* sdb_store_le_matcher */
548 sdb_store_matcher_t *
549 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
550 {
551 return M(sdb_object_create("eq-matcher", cmp_type,
552 MATCHER_EQ, left, right));
553 } /* sdb_store_eq_matcher */
555 sdb_store_matcher_t *
556 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
557 {
558 return M(sdb_object_create("ne-matcher", cmp_type,
559 MATCHER_NE, left, right));
560 } /* sdb_store_ne_matcher */
562 sdb_store_matcher_t *
563 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
564 {
565 return M(sdb_object_create("ge-matcher", cmp_type,
566 MATCHER_GE, left, right));
567 } /* sdb_store_ge_matcher */
569 sdb_store_matcher_t *
570 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
571 {
572 return M(sdb_object_create("gt-matcher", cmp_type,
573 MATCHER_GT, left, right));
574 } /* sdb_store_gt_matcher */
576 sdb_store_matcher_t *
577 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
578 {
579 return M(sdb_object_create("in-matcher", cmp_type,
580 MATCHER_IN, left, right));
581 } /* sdb_store_in_matcher */
583 sdb_store_matcher_t *
584 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
585 {
586 if (! right->type) {
587 if ((right->data.type != SDB_TYPE_STRING)
588 && (right->data.type != SDB_TYPE_REGEX))
589 return NULL;
591 if (right->data.type == SDB_TYPE_STRING) {
592 char *raw = right->data.data.string;
593 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
594 return NULL;
595 free(raw);
596 }
597 }
598 return M(sdb_object_create("regex-matcher", cmp_type,
599 MATCHER_REGEX, left, right));
600 } /* sdb_store_regex_matcher */
602 sdb_store_matcher_t *
603 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
604 {
605 sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
606 if (! m)
607 return NULL;
608 m->type = MATCHER_NREGEX;
609 return m;
610 } /* sdb_store_nregex_matcher */
612 sdb_store_matcher_t *
613 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
614 {
615 return M(sdb_object_create("isnull-matcher", isnull_type,
616 MATCHER_ISNULL, expr));
617 } /* sdb_store_isnull_matcher */
619 sdb_store_matcher_t *
620 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
621 {
622 return M(sdb_object_create("isnull-matcher", isnull_type,
623 MATCHER_ISNNULL, expr));
624 } /* sdb_store_isnnull_matcher */
626 sdb_store_matcher_op_cb
627 sdb_store_parse_matcher_op(const char *op)
628 {
629 if (! strcasecmp(op, "<"))
630 return sdb_store_lt_matcher;
631 else if (! strcasecmp(op, "<="))
632 return sdb_store_le_matcher;
633 else if (! strcasecmp(op, "="))
634 return sdb_store_eq_matcher;
635 else if (! strcasecmp(op, "!="))
636 return sdb_store_ne_matcher;
637 else if (! strcasecmp(op, ">="))
638 return sdb_store_ge_matcher;
639 else if (! strcasecmp(op, ">"))
640 return sdb_store_gt_matcher;
641 else if (! strcasecmp(op, "=~"))
642 return sdb_store_regex_matcher;
643 else if (! strcasecmp(op, "!~"))
644 return sdb_store_nregex_matcher;
645 return NULL;
646 } /* sdb_store_parse_matcher_op */
648 int
649 sdb_store_parse_object_type(const char *name)
650 {
651 if (! strcasecmp(name, "host"))
652 return SDB_HOST;
653 else if (! strcasecmp(name, "service"))
654 return SDB_SERVICE;
655 else if (! strcasecmp(name, "metric"))
656 return SDB_METRIC;
657 else if (! strcasecmp(name, "attribute"))
658 return SDB_ATTRIBUTE;
659 return -1;
660 } /* sdb_store_parse_object_type */
662 int
663 sdb_store_parse_object_type_plural(const char *name)
664 {
665 if (! strcasecmp(name, "hosts"))
666 return SDB_HOST;
667 else if (! strcasecmp(name, "services"))
668 return SDB_SERVICE;
669 else if (! strcasecmp(name, "metrics"))
670 return SDB_METRIC;
671 else if (! strcasecmp(name, "attributes"))
672 return SDB_ATTRIBUTE;
673 return -1;
674 } /* sdb_store_parse_object_type_plural */
676 int
677 sdb_store_parse_field_name(const char *name)
678 {
679 if (! strcasecmp(name, "name"))
680 return SDB_FIELD_NAME;
681 else if (! strcasecmp(name, "last_update"))
682 return SDB_FIELD_LAST_UPDATE;
683 else if (! strcasecmp(name, "age"))
684 return SDB_FIELD_AGE;
685 else if (! strcasecmp(name, "interval"))
686 return SDB_FIELD_INTERVAL;
687 else if (! strcasecmp(name, "backend"))
688 return SDB_FIELD_BACKEND;
689 return -1;
690 } /* sdb_store_parse_field_name */
692 sdb_store_matcher_t *
693 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
694 {
695 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
696 left, right));
697 } /* sdb_store_dis_matcher */
699 sdb_store_matcher_t *
700 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
701 {
702 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
703 left, right));
704 } /* sdb_store_con_matcher */
706 sdb_store_matcher_t *
707 sdb_store_inv_matcher(sdb_store_matcher_t *m)
708 {
709 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
710 } /* sdb_store_inv_matcher */
712 int
713 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
714 sdb_store_matcher_t *filter)
715 {
716 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
717 return 0;
719 /* "NULL" always matches */
720 if ((! m) || (! obj))
721 return 1;
723 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
724 return 0;
726 return matchers[m->type](m, obj, filter);
727 } /* sdb_store_matcher_matches */
729 int
730 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
731 sdb_store_lookup_cb cb, void *user_data)
732 {
733 scan_iter_data_t data = { m, filter, cb, user_data };
735 if (! cb)
736 return -1;
737 return sdb_store_iterate(scan_iter, &data);
738 } /* sdb_store_scan */
740 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */