Code

store: Renamed child-matcher to any-matcher.
[sysdb.git] / src / core / store_lookup.c
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)
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)
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         /* negated matchers should only match if the respective positive matchers
126          * do not match; that is if the negated matcher matchers *all* children */
127         if ((ITER_M(m)->m->type == MATCHER_NE)
128                         || (ITER_M(m)->m->type == MATCHER_NREGEX))
129                 all = 1;
131         if (ITER_M(m)->type == SDB_SERVICE)
132                 iter = sdb_avltree_get_iter(HOST(obj)->services);
133         else if (ITER_M(m)->type == SDB_METRIC)
134                 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
135         else if (ITER_M(m)->type == SDB_ATTRIBUTE)
136                 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
138         status = all;
139         while (sdb_avltree_iter_has_next(iter)) {
140                 sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
141                 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
142                         continue;
144                 if (sdb_store_matcher_matches(ITER_M(m)->m, child, filter)) {
145                         if (! all) {
146                                 status = 1;
147                                 break;
148                         }
149                 } else if (all) {
150                         status = 0;
151                         break;
152                 }
153         }
154         sdb_avltree_iter_destroy(iter);
155         return status;
156 } /* match_iter */
158 /*
159  * cmp_expr:
160  * Compare the values of two expressions when evaluating them using the
161  * specified stored object and filter. Returns a value less than, equal to, or
162  * greater than zero if the value of the first expression compares less than,
163  * equal to, or greater than the value of the second expression. Returns
164  * INT_MAX if any of the expressions could not be evaluated or if any of them
165  * evaluated to NULL.
166  */
167 static int
168 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
169                 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
171         sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
172         int status;
174         if (sdb_store_expr_eval(e1, obj, &v1, filter))
175                 return INT_MAX;
176         if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
177                 sdb_data_free_datum(&v1);
178                 return INT_MAX;
179         }
181         if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
182                 status = INT_MAX;
183         else if (v1.type == v2.type)
184                 status = sdb_data_cmp(&v1, &v2);
185         else if ((e1->data_type >= 0) && (e2->data_type >= 0))
186                 status = INT_MAX;
187         else
188                 status = sdb_data_strcmp(&v1, &v2);
190         sdb_data_free_datum(&v1);
191         sdb_data_free_datum(&v2);
192         return status;
193 } /* cmp_expr */
195 static int
196 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
197                 sdb_store_matcher_t *filter)
199         int status;
200         assert(m->type == MATCHER_LT);
201         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
202         return (status != INT_MAX) && (status < 0);
203 } /* match_lt */
205 static int
206 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
207                 sdb_store_matcher_t *filter)
209         int status;
210         assert(m->type == MATCHER_LE);
211         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
212         return (status != INT_MAX) && (status <= 0);
213 } /* match_le */
215 static int
216 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
217                 sdb_store_matcher_t *filter)
219         int status;
220         assert(m->type == MATCHER_EQ);
221         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
222         return (status != INT_MAX) && (! status);
223 } /* match_eq */
225 static int
226 match_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
227                 sdb_store_matcher_t *filter)
229         int status;
230         assert(m->type == MATCHER_NE);
231         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
232         return (status != INT_MAX) && status;
233 } /* match_ne */
235 static int
236 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
237                 sdb_store_matcher_t *filter)
239         int status;
240         assert(m->type == MATCHER_GE);
241         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
242         return (status != INT_MAX) && (status >= 0);
243 } /* match_ge */
245 static int
246 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
247                 sdb_store_matcher_t *filter)
249         int status;
250         assert(m->type == MATCHER_GT);
251         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
252         return (status != INT_MAX) && (status > 0);
253 } /* match_gt */
255 static int
256 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
257                 sdb_store_matcher_t *filter)
259         sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
260         int status = 1;
262         assert(m->type == MATCHER_IN);
264         if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
265                         || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
266                 status = 0;
268         if (status)
269                 status = sdb_data_inarray(&value, &array);
271         sdb_data_free_datum(&value);
272         sdb_data_free_datum(&array);
273         return status;
274 } /* match_in */
276 static int
277 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
278                 sdb_store_matcher_t *filter)
280         sdb_data_t v = SDB_DATA_INIT;
281         int status = 0;
283         regex_t regex;
284         _Bool free_regex = 0;
286         assert((m->type == MATCHER_REGEX)
287                         || (m->type == MATCHER_NREGEX));
289         if (! CMP_M(m)->right->type) {
290                 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
291                 regex = CMP_M(m)->right->data.data.re.regex;
292         }
293         else {
294                 sdb_data_t tmp = SDB_DATA_INIT;
295                 char *raw;
297                 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
298                         return 0;
300                 if (tmp.type != SDB_TYPE_STRING) {
301                         sdb_data_free_datum(&tmp);
302                         return 0;
303                 }
305                 raw = tmp.data.string;
306                 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
307                         free(raw);
308                         return 0;
309                 }
311                 regex = tmp.data.re.regex;
312                 free_regex = 1;
313                 free(tmp.data.re.raw);
314                 free(raw);
315         }
317         if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
318                         || (sdb_data_isnull(&v)))
319                 status = 0;
320         else {
321                 char value[sdb_data_strlen(&v) + 1];
322                 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
323                         status = 0;
324                 else if (! regexec(&regex, value, 0, NULL, 0))
325                         status = 1;
326         }
328         if (free_regex)
329                 regfree(&regex);
330         sdb_data_free_datum(&v);
331         if (m->type == MATCHER_NREGEX)
332                 return !status;
333         return status;
334 } /* match_regex */
336 static int
337 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
338                 sdb_store_matcher_t *filter)
340         sdb_data_t v = SDB_DATA_INIT;
341         int status;
343         assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
345         /* TODO: this might hide real errors;
346          * improve error reporting and propagation */
347         if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
348                         || sdb_data_isnull(&v))
349                 status = 1;
350         else
351                 status = 0;
353         sdb_data_free_datum(&v);
354         if (m->type == MATCHER_ISNNULL)
355                 return !status;
356         return status;
357 } /* match_isnull */
359 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
360                 sdb_store_matcher_t *);
362 /* this array needs to be indexable by the matcher types;
363  * -> update the enum in store-private.h when updating this */
364 static matcher_cb
365 matchers[] = {
366         match_logical,
367         match_logical,
368         match_unary,
369         match_iter,
370         match_lt,
371         match_le,
372         match_eq,
373         match_ne,
374         match_ge,
375         match_gt,
376         match_in,
377         match_regex,
378         match_regex,
379         match_isnull,
380         match_isnull,
381 };
383 /*
384  * private matcher types
385  */
387 static int
388 op_matcher_init(sdb_object_t *obj, va_list ap)
390         M(obj)->type = va_arg(ap, int);
391         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
392                 return -1;
394         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
395         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
396         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
397         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
399         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
400                 return -1;
401         return 0;
402 } /* op_matcher_init */
404 static void
405 op_matcher_destroy(sdb_object_t *obj)
407         if (OP_M(obj)->left)
408                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
409         if (OP_M(obj)->right)
410                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
411 } /* op_matcher_destroy */
413 static int
414 iter_matcher_init(sdb_object_t *obj, va_list ap)
416         M(obj)->type = va_arg(ap, int);
417         ITER_M(obj)->type = va_arg(ap, int);
418         ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
420         if (! ITER_M(obj)->m)
421                 return -1;
423         sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
424         return 0;
425 } /* iter_matcher_init */
427 static void
428 iter_matcher_destroy(sdb_object_t *obj)
430         sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
431 } /* iter_matcher_destroy */
433 static int
434 cmp_matcher_init(sdb_object_t *obj, va_list ap)
436         M(obj)->type = va_arg(ap, int);
438         CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
439         sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
440         CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
441         sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
443         if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
444                 return -1;
445         return 0;
446 } /* cmp_matcher_init */
448 static void
449 cmp_matcher_destroy(sdb_object_t *obj)
451         sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
452         sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
453 } /* cmp_matcher_destroy */
455 static int
456 uop_matcher_init(sdb_object_t *obj, va_list ap)
458         M(obj)->type = va_arg(ap, int);
459         if (M(obj)->type != MATCHER_NOT)
460                 return -1;
462         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
463         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
465         if (! UOP_M(obj)->op)
466                 return -1;
467         return 0;
468 } /* uop_matcher_init */
470 static void
471 uop_matcher_destroy(sdb_object_t *obj)
473         if (UOP_M(obj)->op)
474                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
475 } /* uop_matcher_destroy */
477 static int
478 isnull_matcher_init(sdb_object_t *obj, va_list ap)
480         M(obj)->type = va_arg(ap, int);
481         if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
482                 return -1;
484         ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
485         sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
486         return 0;
487 } /* isnull_matcher_init */
489 static void
490 isnull_matcher_destroy(sdb_object_t *obj)
492         sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
493         ISNULL_M(obj)->expr = NULL;
494 } /* isnull_matcher_destroy */
496 static sdb_type_t op_type = {
497         /* size = */ sizeof(op_matcher_t),
498         /* init = */ op_matcher_init,
499         /* destroy = */ op_matcher_destroy,
500 };
502 static sdb_type_t uop_type = {
503         /* size = */ sizeof(uop_matcher_t),
504         /* init = */ uop_matcher_init,
505         /* destroy = */ uop_matcher_destroy,
506 };
508 static sdb_type_t iter_type = {
509         /* size = */ sizeof(iter_matcher_t),
510         /* init = */ iter_matcher_init,
511         /* destroy = */ iter_matcher_destroy,
512 };
514 static sdb_type_t cmp_type = {
515         /* size = */ sizeof(cmp_matcher_t),
516         /* init = */ cmp_matcher_init,
517         /* destroy = */ cmp_matcher_destroy,
518 };
520 static sdb_type_t isnull_type = {
521         /* size = */ sizeof(isnull_matcher_t),
522         /* init = */ isnull_matcher_init,
523         /* destroy = */ isnull_matcher_destroy,
524 };
526 /*
527  * public API
528  */
530 sdb_store_matcher_t *
531 sdb_store_any_matcher(int type, sdb_store_matcher_t *m)
533         if ((type != SDB_SERVICE) && (type != SDB_METRIC)
534                         && (type != SDB_ATTRIBUTE))
535                 return NULL;
536         return M(sdb_object_create("any-matcher", iter_type,
537                                 MATCHER_ANY, type, m));
538 } /* sdb_store_iter_matcher */
540 sdb_store_matcher_t *
541 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
543         return M(sdb_object_create("lt-matcher", cmp_type,
544                                 MATCHER_LT, left, right));
545 } /* sdb_store_lt_matcher */
547 sdb_store_matcher_t *
548 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
550         return M(sdb_object_create("le-matcher", cmp_type,
551                                 MATCHER_LE, left, right));
552 } /* sdb_store_le_matcher */
554 sdb_store_matcher_t *
555 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
557         return M(sdb_object_create("eq-matcher", cmp_type,
558                                 MATCHER_EQ, left, right));
559 } /* sdb_store_eq_matcher */
561 sdb_store_matcher_t *
562 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
564         return M(sdb_object_create("ne-matcher", cmp_type,
565                                 MATCHER_NE, left, right));
566 } /* sdb_store_ne_matcher */
568 sdb_store_matcher_t *
569 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
571         return M(sdb_object_create("ge-matcher", cmp_type,
572                                 MATCHER_GE, left, right));
573 } /* sdb_store_ge_matcher */
575 sdb_store_matcher_t *
576 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
578         return M(sdb_object_create("gt-matcher", cmp_type,
579                                 MATCHER_GT, left, right));
580 } /* sdb_store_gt_matcher */
582 sdb_store_matcher_t *
583 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
585         return M(sdb_object_create("in-matcher", cmp_type,
586                                 MATCHER_IN, left, right));
587 } /* sdb_store_in_matcher */
589 sdb_store_matcher_t *
590 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
592         if (! right->type) {
593                 if ((right->data.type != SDB_TYPE_STRING)
594                                 && (right->data.type != SDB_TYPE_REGEX))
595                         return NULL;
597                 if (right->data.type == SDB_TYPE_STRING) {
598                         char *raw = right->data.data.string;
599                         if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
600                                 return NULL;
601                         free(raw);
602                 }
603         }
604         return M(sdb_object_create("regex-matcher", cmp_type,
605                                 MATCHER_REGEX, left, right));
606 } /* sdb_store_regex_matcher */
608 sdb_store_matcher_t *
609 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
611         sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
612         if (! m)
613                 return NULL;
614         m->type = MATCHER_NREGEX;
615         return m;
616 } /* sdb_store_nregex_matcher */
618 sdb_store_matcher_t *
619 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
621         return M(sdb_object_create("isnull-matcher", isnull_type,
622                                 MATCHER_ISNULL, expr));
623 } /* sdb_store_isnull_matcher */
625 sdb_store_matcher_t *
626 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
628         return M(sdb_object_create("isnull-matcher", isnull_type,
629                                 MATCHER_ISNNULL, expr));
630 } /* sdb_store_isnnull_matcher */
632 sdb_store_matcher_op_cb
633 sdb_store_parse_matcher_op(const char *op)
635         if (! strcasecmp(op, "<"))
636                 return sdb_store_lt_matcher;
637         else if (! strcasecmp(op, "<="))
638                 return sdb_store_le_matcher;
639         else if (! strcasecmp(op, "="))
640                 return sdb_store_eq_matcher;
641         else if (! strcasecmp(op, "!="))
642                 return sdb_store_ne_matcher;
643         else if (! strcasecmp(op, ">="))
644                 return sdb_store_ge_matcher;
645         else if (! strcasecmp(op, ">"))
646                 return sdb_store_gt_matcher;
647         else if (! strcasecmp(op, "=~"))
648                 return sdb_store_regex_matcher;
649         else if (! strcasecmp(op, "!~"))
650                 return sdb_store_nregex_matcher;
651         return NULL;
652 } /* sdb_store_parse_matcher_op */
654 int
655 sdb_store_parse_object_type(const char *name)
657         if (! strcasecmp(name, "host"))
658                 return SDB_HOST;
659         else if (! strcasecmp(name, "service"))
660                 return SDB_SERVICE;
661         else if (! strcasecmp(name, "metric"))
662                 return SDB_METRIC;
663         else if (! strcasecmp(name, "attribute"))
664                 return SDB_ATTRIBUTE;
665         return -1;
666 } /* sdb_store_parse_object_type */
668 int
669 sdb_store_parse_object_type_plural(const char *name)
671         if (! strcasecmp(name, "hosts"))
672                 return SDB_HOST;
673         else if (! strcasecmp(name, "services"))
674                 return SDB_SERVICE;
675         else if (! strcasecmp(name, "metrics"))
676                 return SDB_METRIC;
677         else if (! strcasecmp(name, "attributes"))
678                 return SDB_ATTRIBUTE;
679         return -1;
680 } /* sdb_store_parse_object_type_plural */
682 int
683 sdb_store_parse_field_name(const char *name)
685         if (! strcasecmp(name, "name"))
686                 return SDB_FIELD_NAME;
687         else if (! strcasecmp(name, "last_update"))
688                 return SDB_FIELD_LAST_UPDATE;
689         else if (! strcasecmp(name, "age"))
690                 return SDB_FIELD_AGE;
691         else if (! strcasecmp(name, "interval"))
692                 return SDB_FIELD_INTERVAL;
693         else if (! strcasecmp(name, "backend"))
694                 return SDB_FIELD_BACKEND;
695         return -1;
696 } /* sdb_store_parse_field_name */
698 sdb_store_matcher_t *
699 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
701         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
702                                 left, right));
703 } /* sdb_store_dis_matcher */
705 sdb_store_matcher_t *
706 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
708         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
709                                 left, right));
710 } /* sdb_store_con_matcher */
712 sdb_store_matcher_t *
713 sdb_store_inv_matcher(sdb_store_matcher_t *m)
715         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
716 } /* sdb_store_inv_matcher */
718 int
719 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
720                 sdb_store_matcher_t *filter)
722         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
723                 return 0;
725         /* "NULL" always matches */
726         if ((! m) || (! obj))
727                 return 1;
729         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
730                 return 0;
732         return matchers[m->type](m, obj, filter);
733 } /* sdb_store_matcher_matches */
735 int
736 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
737                 sdb_store_lookup_cb cb, void *user_data)
739         scan_iter_data_t data = { m, filter, cb, user_data };
741         if (! cb)
742                 return -1;
743         return sdb_store_iterate(scan_iter, &data);
744 } /* sdb_store_scan */
746 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */