Code

store: Removed now unused name-matcher and sdb_store_matcher_parse_cmp().
[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_child(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_SERVICE)
120                         || (m->type == MATCHER_METRIC)
121                         || (m->type == MATCHER_ATTRIBUTE));
123         /* TODO: support all object types */
124         if (obj->type != SDB_HOST)
125                 return 0;
127         /* negated matchers should only match if the respective positive matchers
128          * do not match; that is if the negated matcher matchers *all* children */
129         if ((CHILD_M(m)->m->type == MATCHER_NE)
130                         || (CHILD_M(m)->m->type == MATCHER_NREGEX))
131                 all = 1;
133         if (m->type == MATCHER_SERVICE)
134                 iter = sdb_avltree_get_iter(HOST(obj)->services);
135         else if (m->type == MATCHER_METRIC)
136                 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
137         else if (m->type == MATCHER_ATTRIBUTE)
138                 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
140         status = all;
141         while (sdb_avltree_iter_has_next(iter)) {
142                 sdb_store_obj_t *child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
143                 if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
144                         continue;
146                 if (sdb_store_matcher_matches(CHILD_M(m)->m, child, filter)) {
147                         if (! all) {
148                                 status = 1;
149                                 break;
150                         }
151                 } else if (all) {
152                         status = 0;
153                         break;
154                 }
155         }
156         sdb_avltree_iter_destroy(iter);
157         return status;
158 } /* match_child */
160 /*
161  * cmp_expr:
162  * Compare the values of two expressions when evaluating them using the
163  * specified stored object and filter. Returns a value less than, equal to, or
164  * greater than zero if the value of the first expression compares less than,
165  * equal to, or greater than the value of the second expression. Returns
166  * INT_MAX if any of the expressions could not be evaluated or if any of them
167  * evaluated to NULL.
168  */
169 static int
170 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
171                 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
173         sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
174         int status;
176         if (sdb_store_expr_eval(e1, obj, &v1, filter))
177                 return INT_MAX;
178         if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
179                 sdb_data_free_datum(&v1);
180                 return INT_MAX;
181         }
183         if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
184                 status = INT_MAX;
185         else if (v1.type == v2.type)
186                 status = sdb_data_cmp(&v1, &v2);
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_child,
370         match_child,
371         match_child,
372         match_lt,
373         match_le,
374         match_eq,
375         match_ne,
376         match_ge,
377         match_gt,
378         match_in,
379         match_regex,
380         match_regex,
381         match_isnull,
382         match_isnull,
383 };
385 /*
386  * private matcher types
387  */
389 static int
390 op_matcher_init(sdb_object_t *obj, va_list ap)
392         M(obj)->type = va_arg(ap, int);
393         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
394                 return -1;
396         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
397         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
398         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
399         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
401         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
402                 return -1;
403         return 0;
404 } /* op_matcher_init */
406 static void
407 op_matcher_destroy(sdb_object_t *obj)
409         if (OP_M(obj)->left)
410                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
411         if (OP_M(obj)->right)
412                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
413 } /* op_matcher_destroy */
415 static int
416 child_matcher_init(sdb_object_t *obj, va_list ap)
418         M(obj)->type = va_arg(ap, int);
419         CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
421         if (! CHILD_M(obj)->m)
422                 return -1;
424         sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
425         return 0;
426 } /* child_matcher_init */
428 static void
429 child_matcher_destroy(sdb_object_t *obj)
431         sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
432 } /* child_matcher_destroy */
434 static int
435 cmp_matcher_init(sdb_object_t *obj, va_list ap)
437         M(obj)->type = va_arg(ap, int);
439         CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
440         sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
441         CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
442         sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
444         if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
445                 return -1;
446         return 0;
447 } /* cmp_matcher_init */
449 static void
450 cmp_matcher_destroy(sdb_object_t *obj)
452         sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
453         sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
454 } /* cmp_matcher_destroy */
456 static int
457 uop_matcher_init(sdb_object_t *obj, va_list ap)
459         M(obj)->type = va_arg(ap, int);
460         if (M(obj)->type != MATCHER_NOT)
461                 return -1;
463         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
464         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
466         if (! UOP_M(obj)->op)
467                 return -1;
468         return 0;
469 } /* uop_matcher_init */
471 static void
472 uop_matcher_destroy(sdb_object_t *obj)
474         if (UOP_M(obj)->op)
475                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
476 } /* uop_matcher_destroy */
478 static int
479 isnull_matcher_init(sdb_object_t *obj, va_list ap)
481         M(obj)->type = va_arg(ap, int);
482         if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
483                 return -1;
485         ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
486         sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
487         return 0;
488 } /* isnull_matcher_init */
490 static void
491 isnull_matcher_destroy(sdb_object_t *obj)
493         sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
494         ISNULL_M(obj)->expr = NULL;
495 } /* isnull_matcher_destroy */
497 static sdb_type_t op_type = {
498         /* size = */ sizeof(op_matcher_t),
499         /* init = */ op_matcher_init,
500         /* destroy = */ op_matcher_destroy,
501 };
503 static sdb_type_t uop_type = {
504         /* size = */ sizeof(uop_matcher_t),
505         /* init = */ uop_matcher_init,
506         /* destroy = */ uop_matcher_destroy,
507 };
509 static sdb_type_t child_type = {
510         /* size = */ sizeof(child_matcher_t),
511         /* init = */ child_matcher_init,
512         /* destroy = */ child_matcher_destroy,
513 };
515 static sdb_type_t cmp_type = {
516         /* size = */ sizeof(cmp_matcher_t),
517         /* init = */ cmp_matcher_init,
518         /* destroy = */ cmp_matcher_destroy,
519 };
521 static sdb_type_t isnull_type = {
522         /* size = */ sizeof(isnull_matcher_t),
523         /* init = */ isnull_matcher_init,
524         /* destroy = */ isnull_matcher_destroy,
525 };
527 /*
528  * public API
529  */
531 sdb_store_matcher_t *
532 sdb_store_child_matcher(int type, sdb_store_matcher_t *m)
534         if (type == SDB_SERVICE)
535                 type = MATCHER_SERVICE;
536         else if (type == SDB_METRIC)
537                 type = MATCHER_METRIC;
538         else if (type == SDB_ATTRIBUTE)
539                 type = MATCHER_ATTRIBUTE;
540         else
541                 return NULL;
542         return M(sdb_object_create("any-matcher", child_type, type, m));
543 } /* sdb_store_child_matcher */
545 sdb_store_matcher_t *
546 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
548         return M(sdb_object_create("lt-matcher", cmp_type,
549                                 MATCHER_LT, left, right));
550 } /* sdb_store_lt_matcher */
552 sdb_store_matcher_t *
553 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
555         return M(sdb_object_create("le-matcher", cmp_type,
556                                 MATCHER_LE, left, right));
557 } /* sdb_store_le_matcher */
559 sdb_store_matcher_t *
560 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
562         return M(sdb_object_create("eq-matcher", cmp_type,
563                                 MATCHER_EQ, left, right));
564 } /* sdb_store_eq_matcher */
566 sdb_store_matcher_t *
567 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
569         return M(sdb_object_create("ne-matcher", cmp_type,
570                                 MATCHER_NE, left, right));
571 } /* sdb_store_ne_matcher */
573 sdb_store_matcher_t *
574 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
576         return M(sdb_object_create("ge-matcher", cmp_type,
577                                 MATCHER_GE, left, right));
578 } /* sdb_store_ge_matcher */
580 sdb_store_matcher_t *
581 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
583         return M(sdb_object_create("gt-matcher", cmp_type,
584                                 MATCHER_GT, left, right));
585 } /* sdb_store_gt_matcher */
587 sdb_store_matcher_t *
588 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
590         return M(sdb_object_create("in-matcher", cmp_type,
591                                 MATCHER_IN, left, right));
592 } /* sdb_store_in_matcher */
594 sdb_store_matcher_t *
595 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
597         if (! right->type) {
598                 if ((right->data.type != SDB_TYPE_STRING)
599                                 && (right->data.type != SDB_TYPE_REGEX))
600                         return NULL;
602                 if (right->data.type == SDB_TYPE_STRING) {
603                         char *raw = right->data.data.string;
604                         if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
605                                 return NULL;
606                         free(raw);
607                 }
608         }
609         return M(sdb_object_create("regex-matcher", cmp_type,
610                                 MATCHER_REGEX, left, right));
611 } /* sdb_store_regex_matcher */
613 sdb_store_matcher_t *
614 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
616         sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
617         if (! m)
618                 return NULL;
619         m->type = MATCHER_NREGEX;
620         return m;
621 } /* sdb_store_nregex_matcher */
623 sdb_store_matcher_t *
624 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
626         return M(sdb_object_create("isnull-matcher", isnull_type,
627                                 MATCHER_ISNULL, expr));
628 } /* sdb_store_isnull_matcher */
630 sdb_store_matcher_t *
631 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
633         return M(sdb_object_create("isnull-matcher", isnull_type,
634                                 MATCHER_ISNNULL, expr));
635 } /* sdb_store_isnnull_matcher */
637 sdb_store_matcher_op_cb
638 sdb_store_parse_matcher_op(const char *op)
640         if (! strcasecmp(op, "<"))
641                 return sdb_store_lt_matcher;
642         else if (! strcasecmp(op, "<="))
643                 return sdb_store_le_matcher;
644         else if (! strcasecmp(op, "="))
645                 return sdb_store_eq_matcher;
646         else if (! strcasecmp(op, "!="))
647                 return sdb_store_ne_matcher;
648         else if (! strcasecmp(op, ">="))
649                 return sdb_store_ge_matcher;
650         else if (! strcasecmp(op, ">"))
651                 return sdb_store_gt_matcher;
652         else if (! strcasecmp(op, "=~"))
653                 return sdb_store_regex_matcher;
654         else if (! strcasecmp(op, "!~"))
655                 return sdb_store_nregex_matcher;
656         return NULL;
657 } /* sdb_store_parse_matcher_op */
659 int
660 sdb_store_parse_object_type(const char *name)
662         if (! strcasecmp(name, "host"))
663                 return SDB_HOST;
664         else if (! strcasecmp(name, "service"))
665                 return SDB_SERVICE;
666         else if (! strcasecmp(name, "metric"))
667                 return SDB_METRIC;
668         else if (! strcasecmp(name, "attribute"))
669                 return SDB_ATTRIBUTE;
670         return -1;
671 } /* sdb_store_parse_object_type */
673 int
674 sdb_store_parse_object_type_plural(const char *name)
676         if (! strcasecmp(name, "hosts"))
677                 return SDB_HOST;
678         else if (! strcasecmp(name, "services"))
679                 return SDB_SERVICE;
680         else if (! strcasecmp(name, "metrics"))
681                 return SDB_METRIC;
682         else if (! strcasecmp(name, "attributes"))
683                 return SDB_ATTRIBUTE;
684         return -1;
685 } /* sdb_store_parse_object_type_plural */
687 int
688 sdb_store_parse_field_name(const char *name)
690         if (! strcasecmp(name, "name"))
691                 return SDB_FIELD_NAME;
692         else if (! strcasecmp(name, "last_update"))
693                 return SDB_FIELD_LAST_UPDATE;
694         else if (! strcasecmp(name, "age"))
695                 return SDB_FIELD_AGE;
696         else if (! strcasecmp(name, "interval"))
697                 return SDB_FIELD_INTERVAL;
698         else if (! strcasecmp(name, "backend"))
699                 return SDB_FIELD_BACKEND;
700         return -1;
701 } /* sdb_store_parse_field_name */
703 sdb_store_matcher_t *
704 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
706         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
707                                 left, right));
708 } /* sdb_store_dis_matcher */
710 sdb_store_matcher_t *
711 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
713         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
714                                 left, right));
715 } /* sdb_store_con_matcher */
717 sdb_store_matcher_t *
718 sdb_store_inv_matcher(sdb_store_matcher_t *m)
720         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
721 } /* sdb_store_inv_matcher */
723 int
724 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
725                 sdb_store_matcher_t *filter)
727         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
728                 return 0;
730         /* "NULL" always matches */
731         if ((! m) || (! obj))
732                 return 1;
734         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
735                 return 0;
737         return matchers[m->type](m, obj, filter);
738 } /* sdb_store_matcher_matches */
740 int
741 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
742                 sdb_store_lookup_cb cb, void *user_data)
744         scan_iter_data_t data = { m, filter, cb, user_data };
746         if (! cb)
747                 return -1;
748         return sdb_store_iterate(scan_iter, &data);
749 } /* sdb_store_scan */
751 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */