Code

store: Added sdb_store_parse_matcher_op().
[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 static sdb_attribute_t *
78 attr_get(sdb_host_t *host, const char *name, sdb_store_matcher_t *filter)
79 {
80         sdb_avltree_iter_t *iter = NULL;
81         sdb_attribute_t *attr = NULL;
83         iter = sdb_avltree_get_iter(host->attributes);
84         while (sdb_avltree_iter_has_next(iter)) {
85                 sdb_attribute_t *a = ATTR(sdb_avltree_iter_get_next(iter));
87                 if (strcasecmp(name, SDB_OBJ(a)->name))
88                         continue;
90                 assert(STORE_OBJ(a)->type == SDB_ATTRIBUTE);
91                 attr = a;
92                 break;
93         }
94         sdb_avltree_iter_destroy(iter);
96         if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(attr),
97                                         NULL)))
98                 return NULL;
99         return attr;
100 } /* attr_get */
102 /*
103  * conditional implementations
104  */
106 static int
107 attr_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
108                 sdb_store_matcher_t *filter)
110         sdb_attribute_t *attr;
111         sdb_data_t value = SDB_DATA_INIT;
112         int status;
114         if (obj->type != SDB_HOST)
115                 return INT_MAX;
117         if (sdb_store_expr_eval(ATTR_C(cond)->expr, obj, &value, filter))
118                 return INT_MAX;
120         attr = attr_get(HOST(obj), ATTR_C(cond)->name, filter);
121         if (! attr)
122                 status = INT_MAX;
123         else if (attr->value.type != value.type)
124                 status = sdb_data_strcmp(&attr->value, &value);
125         else
126                 status = sdb_data_cmp(&attr->value, &value);
127         sdb_data_free_datum(&value);
128         return status;
129 } /* attr_cmp */
131 static int
132 obj_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
133                 sdb_store_matcher_t *filter)
135         sdb_data_t obj_value = SDB_DATA_INIT;
136         sdb_data_t value = SDB_DATA_INIT;
137         int status;
139         if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value, filter))
140                 return INT_MAX;
142         if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) {
143                 /* this implementation is not actually a conditional but rather checks
144                  * for equality (or rather, existence) only */
145                 size_t i;
147                 if (value.type != SDB_TYPE_STRING)
148                         return INT_MAX;
150                 status = INT_MAX;
151                 for (i = 0; i < obj->backends_num; ++i) {
152                         if (! strcasecmp(obj->backends[i], value.data.string)) {
153                                 status = 0;
154                                 break;
155                         }
156                 }
157                 sdb_data_free_datum(&value);
158                 return status;
159         }
161         if (sdb_store_get_field(obj, OBJ_C(cond)->field, &obj_value))
162                 return INT_MAX;
163         if (obj_value.type != value.type) {
164                 sdb_data_free_datum(&obj_value);
165                 sdb_data_free_datum(&value);
166                 return INT_MAX;
167         }
169         status = sdb_data_cmp(&obj_value, &value);
170         sdb_data_free_datum(&obj_value);
171         sdb_data_free_datum(&value);
172         return status;
173 } /* obj_cmp */
175 /*
176  * matcher implementations
177  */
179 static int
180 match_string(string_matcher_t *m, const char *name)
182         if ((! m->name) && (! m->name_re))
183                 return 1;
185         if (! name)
186                 name = "";
188         if (m->name && strcasecmp(m->name, name))
189                 return 0;
190         if (m->name_re && regexec(m->name_re, name,
191                                         /* matches */ 0, NULL, /* flags = */ 0))
192                 return 0;
193         return 1;
194 } /* match_string */
196 static int
197 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
198                 sdb_store_matcher_t *filter)
200         int status;
202         assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
203         assert(OP_M(m)->left && OP_M(m)->right);
205         status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
207         /* lazy evaluation */
208         if ((! status) && (m->type == MATCHER_AND))
209                 return status;
210         else if (status && (m->type == MATCHER_OR))
211                 return status;
213         return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
214 } /* match_logical */
216 static int
217 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
218                 sdb_store_matcher_t *filter)
220         assert(m->type == MATCHER_NOT);
221         assert(UOP_M(m)->op);
223         return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
224 } /* match_unary */
226 static int
227 match_name(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
228                 sdb_store_matcher_t *filter)
230         sdb_avltree_iter_t *iter = NULL;
231         int status = 0;
233         assert(m->type == MATCHER_NAME);
235         if (obj->type == NAME_M(m)->obj_type)
236                 return match_string(&NAME_M(m)->name, SDB_OBJ(obj)->name);
237         else if (obj->type != SDB_HOST)
238                 return 0;
240         switch (NAME_M(m)->obj_type) {
241                 case SDB_SERVICE:
242                         iter = sdb_avltree_get_iter(HOST(obj)->services);
243                         break;
244                 case SDB_METRIC:
245                         iter = sdb_avltree_get_iter(HOST(obj)->metrics);
246                         break;
247                 case SDB_ATTRIBUTE:
248                         iter = sdb_avltree_get_iter(HOST(obj)->attributes);
249                         break;
250         }
252         while (sdb_avltree_iter_has_next(iter)) {
253                 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
254                 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
255                                                 NULL)))
256                         continue;
257                 if (match_string(&NAME_M(m)->name, child->name)) {
258                         status = 1;
259                         break;
260                 }
261         }
262         sdb_avltree_iter_destroy(iter);
263         return status;
264 } /* match_name */
266 static int
267 match_attr(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
268                 sdb_store_matcher_t *filter)
270         sdb_attribute_t *attr;
272         assert(m->type == MATCHER_ATTR);
273         assert(ATTR_M(m)->name);
275         if (obj->type != SDB_HOST)
276                 return 0;
278         attr = attr_get(HOST(obj), ATTR_M(m)->name, filter);
279         if (attr) {
280                 char buf[sdb_data_strlen(&attr->value) + 1];
281                 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
282                         return 0;
283                 if (match_string(&ATTR_M(m)->value, buf))
284                         return 1;
285         }
286         return 0;
287 } /* match_attr */
289 static int
290 match_child(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
291                 sdb_store_matcher_t *filter)
293         sdb_avltree_iter_t *iter = NULL;
294         int status = 0;
296         assert((m->type == MATCHER_SERVICE)
297                         || (m->type == MATCHER_METRIC)
298                         || (m->type == MATCHER_ATTRIBUTE));
300         /* TODO: support all object types */
301         if (obj->type != SDB_HOST)
302                 return 0;
304         if (m->type == MATCHER_SERVICE)
305                 iter = sdb_avltree_get_iter(HOST(obj)->services);
306         else if (m->type == MATCHER_METRIC)
307                 iter = sdb_avltree_get_iter(HOST(obj)->metrics);
308         else if (m->type == SDB_ATTRIBUTE)
309                 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
311         while (sdb_avltree_iter_has_next(iter)) {
312                 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
313                 if (filter && (! sdb_store_matcher_matches(filter,
314                                                 STORE_OBJ(child), NULL)))
315                         continue;
317                 if (sdb_store_matcher_matches(CHILD_M(m)->m, obj, filter)) {
318                         status = 1;
319                         break;
320                 }
321         }
322         sdb_avltree_iter_destroy(iter);
323         return status;
324 } /* match_child */
326 static int
327 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
328                 sdb_store_matcher_t *filter)
330         int status;
331         assert(m->type == MATCHER_LT);
332         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
333         return (status != INT_MAX) && (status < 0);
334 } /* match_lt */
336 static int
337 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
338                 sdb_store_matcher_t *filter)
340         int status;
341         assert(m->type == MATCHER_LE);
342         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
343         return (status != INT_MAX) && (status <= 0);
344 } /* match_le */
346 static int
347 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
348                 sdb_store_matcher_t *filter)
350         int status;
351         assert(m->type == MATCHER_EQ);
352         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
353         return (status != INT_MAX) && (! status);
354 } /* match_eq */
356 static int
357 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
358                 sdb_store_matcher_t *filter)
360         int status;
361         assert(m->type == MATCHER_GE);
362         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
363         return (status != INT_MAX) && (status >= 0);
364 } /* match_ge */
366 static int
367 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
368                 sdb_store_matcher_t *filter)
370         int status;
371         assert(m->type == MATCHER_GT);
372         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
373         return (status != INT_MAX) && (status > 0);
374 } /* match_gt */
376 /*
377  * cmp_expr:
378  * Compare the values of two expressions when evaluating them using the
379  * specified stored object and filter. Returns a value less than, equal to, or
380  * greater than zero if the value of the first expression compares less than,
381  * equal to, or greater than the value of the second expression. Returns
382  * INT_MAX if any of the expressions could not be evaluated.
383  */
384 static int
385 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
386                 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
388         sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
389         int status;
391         if (sdb_store_expr_eval(e1, obj, &v1, filter))
392                 return INT_MAX;
393         if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
394                 sdb_data_free_datum(&v1);
395                 return INT_MAX;
396         }
398         if (v1.type == v2.type)
399                 status = sdb_data_cmp(&v1, &v2);
400         else
401                 status = sdb_data_strcmp(&v1, &v2);
403         sdb_data_free_datum(&v1);
404         sdb_data_free_datum(&v2);
405         return status;
406 } /* cmp_expr */
408 static int
409 match_cmp_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
410                 sdb_store_matcher_t *filter)
412         int status;
413         assert(m->type == MATCHER_CMP_LT);
414         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
415         return (status != INT_MAX) && (status < 0);
416 } /* match_cmp_lt */
418 static int
419 match_cmp_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
420                 sdb_store_matcher_t *filter)
422         int status;
423         assert(m->type == MATCHER_CMP_LE);
424         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
425         return (status != INT_MAX) && (status <= 0);
426 } /* match_cmp_le */
428 static int
429 match_cmp_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
430                 sdb_store_matcher_t *filter)
432         int status;
433         assert(m->type == MATCHER_CMP_EQ);
434         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
435         return (status != INT_MAX) && (! status);
436 } /* match_cmp_eq */
438 static int
439 match_cmp_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
440                 sdb_store_matcher_t *filter)
442         int status;
443         assert(m->type == MATCHER_CMP_NE);
444         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
445         return (status != INT_MAX) && status;
446 } /* match_cmp_ne */
448 static int
449 match_cmp_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
450                 sdb_store_matcher_t *filter)
452         int status;
453         assert(m->type == MATCHER_CMP_GE);
454         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
455         return (status != INT_MAX) && (status >= 0);
456 } /* match_cmp_ge */
458 static int
459 match_cmp_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
460                 sdb_store_matcher_t *filter)
462         int status;
463         assert(m->type == MATCHER_CMP_GT);
464         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
465         return (status != INT_MAX) && (status > 0);
466 } /* match_cmp_gt */
468 static int
469 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
470                 sdb_store_matcher_t *filter)
472         sdb_data_t v = SDB_DATA_INIT;
473         int status = 0;
475         regex_t regex;
476         _Bool free_regex = 0;
478         assert((m->type == MATCHER_REGEX)
479                         || (m->type == MATCHER_NREGEX));
481         if (! CMP_M(m)->right->type) {
482                 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
483                 regex = CMP_M(m)->right->data.data.re.regex;
484         }
485         else {
486                 sdb_data_t tmp = SDB_DATA_INIT;
487                 char *raw;
489                 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
490                         return 0;
492                 if (tmp.type != SDB_TYPE_STRING) {
493                         sdb_data_free_datum(&tmp);
494                         return 0;
495                 }
497                 raw = tmp.data.string;
498                 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
499                         free(raw);
500                         return 0;
501                 }
503                 regex = tmp.data.re.regex;
504                 free_regex = 1;
505                 free(tmp.data.re.raw);
506                 free(raw);
507         }
509         if (sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
510                 status = 0;
511         else {
512                 char value[sdb_data_strlen(&v) + 1];
513                 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
514                         status = 0;
515                 else if (! regexec(&regex, value, 0, NULL, 0))
516                         status = 1;
517         }
519         if (free_regex)
520                 regfree(&regex);
521         sdb_data_free_datum(&v);
522         if (m->type == MATCHER_NREGEX)
523                 return !status;
524         return status;
525 } /* match_regex */
527 static int
528 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
529                 sdb_store_matcher_t *filter)
531         assert(m->type == MATCHER_ISNULL);
532         if (obj->type != SDB_HOST)
533                 return 0;
534         return attr_get(HOST(obj), ISNULL_M(m)->attr_name, filter) == NULL;
535 } /* match_isnull */
537 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
538                 sdb_store_matcher_t *);
540 /* this array needs to be indexable by the matcher types;
541  * -> update the enum in store-private.h when updating this */
542 static matcher_cb
543 matchers[] = {
544         match_logical,
545         match_logical,
546         match_unary,
547         match_name,
548         match_attr,
549         match_child,
550         match_child,
551         match_child,
552         match_lt,
553         match_le,
554         match_eq,
555         match_ge,
556         match_gt,
557         match_cmp_lt,
558         match_cmp_le,
559         match_cmp_eq,
560         match_cmp_ne,
561         match_cmp_ge,
562         match_cmp_gt,
563         match_regex,
564         match_regex,
565         match_isnull,
566 };
568 /*
569  * private conditional types
570  */
572 static int
573 attr_cond_init(sdb_object_t *obj, va_list ap)
575         const char *name = va_arg(ap, const char *);
576         sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
578         if (! name)
579                 return -1;
581         SDB_STORE_COND(obj)->cmp = attr_cmp;
583         ATTR_C(obj)->name = strdup(name);
584         if (! ATTR_C(obj)->name)
585                 return -1;
586         ATTR_C(obj)->expr = expr;
587         sdb_object_ref(SDB_OBJ(expr));
588         return 0;
589 } /* attr_cond_init */
591 static void
592 attr_cond_destroy(sdb_object_t *obj)
594         if (ATTR_C(obj)->name)
595                 free(ATTR_C(obj)->name);
596         sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
597 } /* attr_cond_destroy */
599 static sdb_type_t attr_cond_type = {
600         /* size = */ sizeof(attr_cond_t),
601         /* init = */ attr_cond_init,
602         /* destroy = */ attr_cond_destroy,
603 };
605 static int
606 obj_cond_init(sdb_object_t *obj, va_list ap)
608         int field = va_arg(ap, int);
609         sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
611         SDB_STORE_COND(obj)->cmp = obj_cmp;
613         OBJ_C(obj)->field = field;
614         OBJ_C(obj)->expr = expr;
615         sdb_object_ref(SDB_OBJ(expr));
616         return 0;
617 } /* obj_cond_init */
619 static void
620 obj_cond_destroy(sdb_object_t *obj)
622         sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr));
623 } /* obj_cond_destroy */
625 static sdb_type_t obj_cond_type = {
626         /* size = */ sizeof(obj_cond_t),
627         /* init = */ obj_cond_init,
628         /* destroy = */ obj_cond_destroy,
629 };
631 /*
632  * private matcher types
633  */
635 /* initializes a string matcher consuming two elements from ap */
636 static int
637 string_matcher_init(string_matcher_t *m, va_list ap)
639         const char *name = va_arg(ap, const char *);
640         const char *name_re = va_arg(ap, const char *);
642         if (name) {
643                 m->name = strdup(name);
644                 if (! m->name)
645                         return -1;
646         }
647         if (name_re) {
648                 m->name_re = malloc(sizeof(*m->name_re));
649                 if (! m->name_re)
650                         return -1;
651                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
652                         return -1;
653         }
654         return 0;
655 } /* string_matcher_init */
657 static void
658 string_matcher_destroy(string_matcher_t *m)
660         if (m->name)
661                 free(m->name);
662         if (m->name_re) {
663                 regfree(m->name_re);
664                 free(m->name_re);
665         }
666 } /* string_matcher_destroy */
668 static char *
669 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
671         snprintf(buf, buflen, "{ %s%s%s, %p }",
672                         m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
673                         m->name_re);
674         return buf;
675 } /* string_tostring */
677 /* initializes a name matcher */
678 static int
679 name_matcher_init(sdb_object_t *obj, va_list ap)
681         name_matcher_t *m = NAME_M(obj);
682         M(obj)->type = MATCHER_NAME;
683         return string_matcher_init(&m->name, ap);
684 } /* name_matcher_init */
686 static void
687 name_matcher_destroy(sdb_object_t *obj)
689         name_matcher_t *m = NAME_M(obj);
690         string_matcher_destroy(&m->name);
691 } /* name_matcher_destroy */
693 static char *
694 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
696         char name[buflen + 1];
697         assert(m->type == MATCHER_NAME);
698         snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
699                         SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
700                         string_tostring(&NAME_M(m)->name, name, sizeof(name)));
701         return buf;
702 } /* name_tostring */
704 static int
705 attr_matcher_init(sdb_object_t *obj, va_list ap)
707         attr_matcher_t *attr = ATTR_M(obj);
708         const char *name = va_arg(ap, const char *);
710         M(obj)->type = MATCHER_ATTR;
711         if (name) {
712                 attr->name = strdup(name);
713                 if (! attr->name)
714                         return -1;
715         }
716         return string_matcher_init(&attr->value, ap);
717 } /* attr_matcher_init */
719 static void
720 attr_matcher_destroy(sdb_object_t *obj)
722         attr_matcher_t *attr = ATTR_M(obj);
723         if (attr->name)
724                 free(attr->name);
725         attr->name = NULL;
726         string_matcher_destroy(&attr->value);
727 } /* attr_matcher_destroy */
729 static char *
730 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
732         char value[buflen + 1];
734         if (! m) {
735                 snprintf(buf, buflen, "ATTR{}");
736                 return buf;
737         }
739         assert(m->type == MATCHER_ATTR);
740         snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
741                         string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
742         return buf;
743 } /* attr_tostring */
745 static int
746 cond_matcher_init(sdb_object_t *obj, va_list ap)
748         int type = va_arg(ap, int);
749         sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
751         if (! cond)
752                 return -1;
754         sdb_object_ref(SDB_OBJ(cond));
756         M(obj)->type = type;
757         COND_M(obj)->cond = cond;
758         return 0;
759 } /* cond_matcher_init */
761 static void
762 cond_matcher_destroy(sdb_object_t *obj)
764         sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
765 } /* cond_matcher_destroy */
767 static char *
768 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
770         const char *type, *id;
771         sdb_data_t value = SDB_DATA_INIT;
772         char value_str[buflen];
773         sdb_store_expr_t *expr;
775         if (COND_M(m)->cond->cmp == attr_cmp) {
776                 type = "ATTR";
777                 id = ATTR_C(COND_M(m)->cond)->name;
778                 expr = ATTR_C(COND_M(m)->cond)->expr;
779         }
780         else if (COND_M(m)->cond->cmp == obj_cmp) {
781                 type = "OBJ";
782                 id = SDB_FIELD_TO_NAME(OBJ_C(COND_M(m)->cond)->field);
783                 expr = OBJ_C(COND_M(m)->cond)->expr;
784         }
785         else {
786                 snprintf(buf, buflen, "<unknown>");
787                 return buf;
788         }
790         if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL))
791                 snprintf(value_str, sizeof(value_str), "ERR");
792         else if (sdb_data_format(&value, value_str, sizeof(value_str),
793                                 SDB_SINGLE_QUOTED) < 0)
794                 snprintf(value_str, sizeof(value_str), "ERR");
795         snprintf(buf, buflen, "%s[%s]{ %s %s }", type, id,
796                         MATCHER_SYM(m->type), value_str);
797         sdb_data_free_datum(&value);
798         return buf;
799 } /* cond_tostring */
801 static int
802 op_matcher_init(sdb_object_t *obj, va_list ap)
804         M(obj)->type = va_arg(ap, int);
805         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
806                 return -1;
808         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
809         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
810         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
811         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
813         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
814                 return -1;
815         return 0;
816 } /* op_matcher_init */
818 static void
819 op_matcher_destroy(sdb_object_t *obj)
821         if (OP_M(obj)->left)
822                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
823         if (OP_M(obj)->right)
824                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
825 } /* op_matcher_destroy */
827 static char *
828 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
830         char left[buflen + 1], right[buflen + 1];
832         if (! m) {
833                 /* this should not happen */
834                 snprintf(buf, buflen, "()");
835                 return buf;
836         }
838         assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
839         snprintf(buf, buflen, "(%s, %s, %s)",
840                         m->type == MATCHER_OR ? "OR" : "AND",
841                         sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
842                         sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
843         return buf;
844 } /* op_tostring */
846 static int
847 child_matcher_init(sdb_object_t *obj, va_list ap)
849         M(obj)->type = va_arg(ap, int);
850         CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
852         if (! CHILD_M(obj)->m)
853                 return -1;
855         sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
856         return 0;
857 } /* child_matcher_init */
859 static void
860 child_matcher_destroy(sdb_object_t *obj)
862         sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
863 } /* child_matcher_destroy */
865 static char *
866 child_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
868         snprintf(buf, buflen, "%s:", MATCHER_SYM(m->type));
869         buf[buflen - 1] = '\0';
870         sdb_store_matcher_tostring(CHILD_M(m)->m,
871                         buf + strlen(buf), buflen - strlen(buf));
872         return buf;
873 } /* child_tostring */
875 static int
876 cmp_matcher_init(sdb_object_t *obj, va_list ap)
878         M(obj)->type = va_arg(ap, int);
880         CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
881         sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
882         CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
883         sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
885         if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
886                 return -1;
887         return 0;
888 } /* cmp_matcher_init */
890 static void
891 cmp_matcher_destroy(sdb_object_t *obj)
893         sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
894         sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
895 } /* cmp_matcher_destroy */
897 static char *
898 cmp_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
900         if (! m) {
901                 /* this should not happen */
902                 snprintf(buf, buflen, "()");
903                 return buf;
904         }
906         /* TODO */
907         snprintf(buf, buflen, "CMP_MATCHER(%d)", m->type);
908         return buf;
909 } /* cmp_tostring */
911 static int
912 uop_matcher_init(sdb_object_t *obj, va_list ap)
914         M(obj)->type = va_arg(ap, int);
915         if (M(obj)->type != MATCHER_NOT)
916                 return -1;
918         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
919         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
921         if (! UOP_M(obj)->op)
922                 return -1;
923         return 0;
924 } /* uop_matcher_init */
926 static void
927 uop_matcher_destroy(sdb_object_t *obj)
929         if (UOP_M(obj)->op)
930                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
931 } /* uop_matcher_destroy */
933 static char *
934 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
936         char op[buflen + 1];
938         if (! m) {
939                 /* this should not happen */
940                 snprintf(buf, buflen, "()");
941                 return buf;
942         }
944         assert(m->type == MATCHER_NOT);
945         snprintf(buf, buflen, "(NOT, %s)",
946                         sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
947         return buf;
948 } /* uop_tostring */
950 static int
951 isnull_matcher_init(sdb_object_t *obj, va_list ap)
953         const char *name;
955         M(obj)->type = va_arg(ap, int);
956         if (M(obj)->type != MATCHER_ISNULL)
957                 return -1;
959         name = va_arg(ap, const char *);
960         if (! name)
961                 return -1;
962         ISNULL_M(obj)->attr_name = strdup(name);
963         if (! ISNULL_M(obj)->attr_name)
964                 return -1;
965         return 0;
966 } /* isnull_matcher_init */
968 static void
969 isnull_matcher_destroy(sdb_object_t *obj)
971         if (ISNULL_M(obj)->attr_name)
972                 free(ISNULL_M(obj)->attr_name);
973         ISNULL_M(obj)->attr_name = NULL;
974 } /* isnull_matcher_destroy */
976 static char *
977 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
979         snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
980         return buf;
981 } /* isnull_tostring */
983 static sdb_type_t name_type = {
984         /* size = */ sizeof(name_matcher_t),
985         /* init = */ name_matcher_init,
986         /* destroy = */ name_matcher_destroy,
987 };
989 static sdb_type_t attr_type = {
990         /* size = */ sizeof(attr_matcher_t),
991         /* init = */ attr_matcher_init,
992         /* destroy = */ attr_matcher_destroy,
993 };
995 static sdb_type_t cond_type = {
996         /* size = */ sizeof(cond_matcher_t),
997         /* init = */ cond_matcher_init,
998         /* destroy = */ cond_matcher_destroy,
999 };
1001 static sdb_type_t op_type = {
1002         /* size = */ sizeof(op_matcher_t),
1003         /* init = */ op_matcher_init,
1004         /* destroy = */ op_matcher_destroy,
1005 };
1007 static sdb_type_t uop_type = {
1008         /* size = */ sizeof(uop_matcher_t),
1009         /* init = */ uop_matcher_init,
1010         /* destroy = */ uop_matcher_destroy,
1011 };
1013 static sdb_type_t child_type = {
1014         /* size = */ sizeof(child_matcher_t),
1015         /* init = */ child_matcher_init,
1016         /* destroy = */ child_matcher_destroy,
1017 };
1019 static sdb_type_t cmp_type = {
1020         /* size = */ sizeof(cmp_matcher_t),
1021         /* init = */ cmp_matcher_init,
1022         /* destroy = */ cmp_matcher_destroy,
1023 };
1025 static sdb_type_t isnull_type = {
1026         /* size = */ sizeof(isnull_matcher_t),
1027         /* init = */ isnull_matcher_init,
1028         /* destroy = */ isnull_matcher_destroy,
1029 };
1031 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
1033 /* this array needs to be indexable by the matcher types;
1034  * -> update the enum in store-private.h when updating this */
1035 static matcher_tostring_cb
1036 matchers_tostring[] = {
1037         op_tostring,
1038         op_tostring,
1039         uop_tostring,
1040         name_tostring,
1041         attr_tostring,
1042         child_tostring,
1043         child_tostring,
1044         child_tostring,
1045         cond_tostring,
1046         cond_tostring,
1047         cond_tostring,
1048         cond_tostring,
1049         cond_tostring,
1050         cmp_tostring,
1051         cmp_tostring,
1052         cmp_tostring,
1053         cmp_tostring,
1054         cmp_tostring,
1055         cmp_tostring,
1056         cmp_tostring,
1057         cmp_tostring,
1058         isnull_tostring,
1059 };
1061 /*
1062  * public API
1063  */
1065 sdb_store_cond_t *
1066 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
1068         return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
1069                                 name, expr));
1070 } /* sdb_store_attr_cond */
1072 sdb_store_cond_t *
1073 sdb_store_obj_cond(int field, sdb_store_expr_t *expr)
1075         return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type,
1076                                 field, expr));
1077 } /* sdb_store_obj_cond */
1079 sdb_store_matcher_t *
1080 sdb_store_name_matcher(int type, const char *name, _Bool re)
1082         sdb_store_matcher_t *m;
1084         if (re)
1085                 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
1086         else
1087                 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
1089         if (! m)
1090                 return NULL;
1092         NAME_M(m)->obj_type = type;
1093         return m;
1094 } /* sdb_store_name_matcher */
1096 sdb_store_matcher_t *
1097 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
1099         sdb_store_matcher_t *m;
1101         if (! name)
1102                 return NULL;
1104         if (re)
1105                 m = M(sdb_object_create("attr-matcher", attr_type,
1106                                         name, NULL, value));
1107         else
1108                 m = M(sdb_object_create("attr-matcher", attr_type,
1109                                         name, value, NULL));
1110         return m;
1111 } /* sdb_store_attr_matcher */
1113 sdb_store_matcher_t *
1114 sdb_store_child_matcher(int type, sdb_store_matcher_t *m)
1116         if (type == SDB_SERVICE)
1117                 type = MATCHER_SERVICE;
1118         else if (type == SDB_METRIC)
1119                 type = MATCHER_METRIC;
1120         else if (type == SDB_ATTRIBUTE)
1121                 type = MATCHER_ATTRIBUTE;
1122         else
1123                 return NULL;
1124         return M(sdb_object_create("any-matcher", child_type, type, m));
1125 } /* sdb_store_child_matcher */
1127 sdb_store_matcher_t *
1128 sdb_store_lt_matcher(sdb_store_cond_t *cond)
1130         return M(sdb_object_create("lt-matcher", cond_type,
1131                                 MATCHER_LT, cond));
1132 } /* sdb_store_lt_matcher */
1134 sdb_store_matcher_t *
1135 sdb_store_le_matcher(sdb_store_cond_t *cond)
1137         return M(sdb_object_create("le-matcher", cond_type,
1138                                 MATCHER_LE, cond));
1139 } /* sdb_store_le_matcher */
1141 sdb_store_matcher_t *
1142 sdb_store_eq_matcher(sdb_store_cond_t *cond)
1144         return M(sdb_object_create("eq-matcher", cond_type,
1145                                 MATCHER_EQ, cond));
1146 } /* sdb_store_eq_matcher */
1148 sdb_store_matcher_t *
1149 sdb_store_ge_matcher(sdb_store_cond_t *cond)
1151         return M(sdb_object_create("ge-matcher", cond_type,
1152                                 MATCHER_GE, cond));
1153 } /* sdb_store_ge_matcher */
1155 sdb_store_matcher_t *
1156 sdb_store_gt_matcher(sdb_store_cond_t *cond)
1158         return M(sdb_object_create("gt-matcher", cond_type,
1159                                 MATCHER_GT, cond));
1160 } /* sdb_store_gt_matcher */
1162 /*
1163  * TODO: Rename sdb_store_cmp_* to sdb_store_* once the old code is unused and
1164  * has been removed.
1165  */
1167 sdb_store_matcher_t *
1168 sdb_store_cmp_lt(sdb_store_expr_t *left, sdb_store_expr_t *right)
1170         return M(sdb_object_create("lt-matcher", cmp_type,
1171                                 MATCHER_CMP_LT, left, right));
1172 } /* sdb_store_cmp_lt */
1174 sdb_store_matcher_t *
1175 sdb_store_cmp_le(sdb_store_expr_t *left, sdb_store_expr_t *right)
1177         return M(sdb_object_create("le-matcher", cmp_type,
1178                                 MATCHER_CMP_LE, left, right));
1179 } /* sdb_store_cmp_le */
1181 sdb_store_matcher_t *
1182 sdb_store_cmp_eq(sdb_store_expr_t *left, sdb_store_expr_t *right)
1184         return M(sdb_object_create("eq-matcher", cmp_type,
1185                                 MATCHER_CMP_EQ, left, right));
1186 } /* sdb_store_cmp_eq */
1188 sdb_store_matcher_t *
1189 sdb_store_cmp_ne(sdb_store_expr_t *left, sdb_store_expr_t *right)
1191         return M(sdb_object_create("ne-matcher", cmp_type,
1192                                 MATCHER_CMP_NE, left, right));
1193 } /* sdb_store_cmp_ne */
1195 sdb_store_matcher_t *
1196 sdb_store_cmp_ge(sdb_store_expr_t *left, sdb_store_expr_t *right)
1198         return M(sdb_object_create("ge-matcher", cmp_type,
1199                                 MATCHER_CMP_GE, left, right));
1200 } /* sdb_store_cmp_ge */
1202 sdb_store_matcher_t *
1203 sdb_store_cmp_gt(sdb_store_expr_t *left, sdb_store_expr_t *right)
1205         return M(sdb_object_create("gt-matcher", cmp_type,
1206                                 MATCHER_CMP_GT, left, right));
1207 } /* sdb_store_cmp_gt */
1209 sdb_store_matcher_t *
1210 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1212         if (! right->type) {
1213                 if ((right->data.type != SDB_TYPE_STRING)
1214                                 && (right->data.type != SDB_TYPE_REGEX))
1215                         return NULL;
1217                 if (right->data.type == SDB_TYPE_STRING) {
1218                         char *raw = right->data.data.string;
1219                         if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
1220                                 return NULL;
1221                         free(raw);
1222                 }
1223         }
1224         return M(sdb_object_create("regex-matcher", cmp_type,
1225                                 MATCHER_REGEX, left, right));
1226 } /* sdb_store_regex_matcher */
1228 sdb_store_matcher_t *
1229 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1231         sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
1232         if (! m)
1233                 return NULL;
1234         m->type = MATCHER_NREGEX;
1235         return m;
1236 } /* sdb_store_nregex_matcher */
1238 sdb_store_matcher_t *
1239 sdb_store_isnull_matcher(const char *attr_name)
1241         return M(sdb_object_create("isnull-matcher", isnull_type,
1242                                 MATCHER_ISNULL, attr_name));
1243 } /* sdb_store_isnull_matcher */
1245 sdb_store_matcher_op_cb
1246 sdb_store_parse_matcher_op(const char *op)
1248         if (! strcasecmp(op, "<"))
1249                 return sdb_store_cmp_lt;
1250         else if (! strcasecmp(op, "<="))
1251                 return sdb_store_cmp_le;
1252         else if (! strcasecmp(op, "="))
1253                 return sdb_store_cmp_eq;
1254         else if (! strcasecmp(op, "!="))
1255                 return sdb_store_cmp_ne;
1256         else if (! strcasecmp(op, ">="))
1257                 return sdb_store_cmp_ge;
1258         else if (! strcasecmp(op, ">"))
1259                 return sdb_store_cmp_gt;
1260         else if (! strcasecmp(op, "=~"))
1261                 return sdb_store_regex_matcher;
1262         else if (! strcasecmp(op, "!~"))
1263                 return sdb_store_nregex_matcher;
1264         return NULL;
1265 } /* sdb_store_parse_matcher_op */
1267 int
1268 sdb_store_parse_object_type_plural(const char *name)
1270         if (! strcasecmp(name, "hosts"))
1271                 return SDB_HOST;
1272         else if (! strcasecmp(name, "services"))
1273                 return SDB_SERVICE;
1274         else if (! strcasecmp(name, "metrics"))
1275                 return SDB_METRIC;
1276         return -1;
1277 } /* sdb_store_parse_object_type_plural */
1279 int
1280 sdb_store_parse_field_name(const char *name)
1282         if (! strcasecmp(name, "name"))
1283                 return SDB_FIELD_NAME;
1284         else if (! strcasecmp(name, "last_update"))
1285                 return SDB_FIELD_LAST_UPDATE;
1286         else if (! strcasecmp(name, "age"))
1287                 return SDB_FIELD_AGE;
1288         else if (! strcasecmp(name, "interval"))
1289                 return SDB_FIELD_INTERVAL;
1290         else if (! strcasecmp(name, "backend"))
1291                 return SDB_FIELD_BACKEND;
1292         return -1;
1293 } /* sdb_store_parse_field_name */
1295 static sdb_store_matcher_t *
1296 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
1298         sdb_store_matcher_t *tmp;
1300         if ((! m) || (! inv))
1301                 return m;
1303         tmp = sdb_store_inv_matcher(m);
1304         /* pass ownership to the inverse matcher */
1305         sdb_object_deref(SDB_OBJ(m));
1306         return tmp;
1307 } /* maybe_inv_matcher */
1309 static int
1310 parse_cond_op(const char *op,
1311                 sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv)
1313         *inv = 0;
1314         if (! strcasecmp(op, "<"))
1315                 *matcher = sdb_store_lt_matcher;
1316         else if (! strcasecmp(op, "<="))
1317                 *matcher = sdb_store_le_matcher;
1318         else if (! strcasecmp(op, "="))
1319                 *matcher = sdb_store_eq_matcher;
1320         else if (! strcasecmp(op, ">="))
1321                 *matcher = sdb_store_ge_matcher;
1322         else if (! strcasecmp(op, ">"))
1323                 *matcher = sdb_store_gt_matcher;
1324         else if (! strcasecmp(op, "!=")) {
1325                 *matcher = sdb_store_eq_matcher;
1326                 *inv = 1;
1327         }
1328         else
1329                 return -1;
1330         return 0;
1331 } /* parse_cond_op */
1333 static sdb_store_matcher_t *
1334 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
1336         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1337         sdb_store_matcher_t *m;
1338         sdb_store_cond_t *cond;
1339         _Bool inv = 0;
1341         if (! attr)
1342                 return NULL;
1344         if (! strcasecmp(op, "IS")) {
1345                 if (! expr)
1346                         return sdb_store_isnull_matcher(attr);
1347                 else
1348                         return NULL;
1349         }
1350         else if (! expr)
1351                 return NULL;
1352         else if (parse_cond_op(op, &matcher, &inv))
1353                 return NULL;
1355         cond = sdb_store_attr_cond(attr, expr);
1356         if (! cond)
1357                 return NULL;
1359         m = matcher(cond);
1360         /* pass ownership to 'm' or destroy in case of an error */
1361         sdb_object_deref(SDB_OBJ(cond));
1362         return maybe_inv_matcher(m, inv);
1363 } /* parse_attr_cmp */
1365 sdb_store_matcher_t *
1366 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
1367                 const char *op, sdb_store_expr_t *expr)
1369         int type = -1;
1370         _Bool inv = 0;
1371         _Bool re = 0;
1373         sdb_data_t value = SDB_DATA_INIT;
1374         sdb_store_matcher_t *m = NULL;
1376         if (! strcasecmp(obj_type, "host"))
1377                 type = SDB_HOST;
1378         else if (! strcasecmp(obj_type, "service"))
1379                 type = SDB_SERVICE;
1380         else if (! strcasecmp(obj_type, "metric"))
1381                 type = SDB_METRIC;
1382         else if (! strcasecmp(obj_type, "attribute"))
1383                 type = SDB_ATTRIBUTE;
1384         else
1385                 return NULL;
1387         /* XXX: this code sucks! */
1388         if (! strcasecmp(op, "=")) {
1389                 /* nothing to do */
1390         }
1391         else if (! strcasecmp(op, "!=")) {
1392                 inv = 1;
1393         }
1394         else if (! strcasecmp(op, "=~")) {
1395                 re = 1;
1396         }
1397         else if (! strcasecmp(op, "!~")) {
1398                 inv = 1;
1399                 re = 1;
1400         }
1401         else if (type == SDB_ATTRIBUTE)
1402                 return parse_attr_cmp(attr, op, expr);
1403         else
1404                 return NULL;
1406         if (! expr)
1407                 return NULL;
1409         if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
1410                         || (value.type != SDB_TYPE_STRING)) {
1411                 sdb_data_free_datum(&value);
1412                 if (type != SDB_ATTRIBUTE)
1413                         return NULL;
1414                 return parse_attr_cmp(attr, op, expr);
1415         }
1417         if (! attr)
1418                 m = sdb_store_name_matcher(type, value.data.string, re);
1419         else if (type == SDB_ATTRIBUTE)
1420                 m = sdb_store_attr_matcher(attr, value.data.string, re);
1422         sdb_data_free_datum(&value);
1423         return maybe_inv_matcher(m, inv);
1424 } /* sdb_store_matcher_parse_cmp */
1426 sdb_store_matcher_t *
1427 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
1428                 sdb_store_expr_t *expr)
1430         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1431         sdb_store_matcher_t *m;
1432         sdb_store_cond_t *cond;
1433         _Bool inv = 0;
1435         int field;
1437         if (! expr)
1438                 return NULL;
1440         field = sdb_store_parse_field_name(name);
1441         if (field < 0)
1442                 return NULL;
1444         if (parse_cond_op(op, &matcher, &inv))
1445                 return NULL;
1446         cond = sdb_store_obj_cond(field, expr);
1447         if (! cond)
1448                 return NULL;
1450         assert(matcher);
1451         m = matcher(cond);
1452         /* pass ownership to 'm' or destroy in case of an error */
1453         sdb_object_deref(SDB_OBJ(cond));
1454         return maybe_inv_matcher(m, inv);
1455 } /* sdb_store_matcher_parse_field_cmp */
1457 sdb_store_matcher_t *
1458 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1460         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
1461                                 left, right));
1462 } /* sdb_store_dis_matcher */
1464 sdb_store_matcher_t *
1465 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1467         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
1468                                 left, right));
1469 } /* sdb_store_con_matcher */
1471 sdb_store_matcher_t *
1472 sdb_store_inv_matcher(sdb_store_matcher_t *m)
1474         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
1475 } /* sdb_store_inv_matcher */
1477 int
1478 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
1479                 sdb_store_matcher_t *filter)
1481         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
1482                 return 0;
1484         /* "NULL" always matches */
1485         if ((! m) || (! obj))
1486                 return 1;
1488         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1489                 return 0;
1491         return matchers[m->type](m, obj, filter);
1492 } /* sdb_store_matcher_matches */
1494 char *
1495 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1497         if (! m)
1498                 return NULL;
1500         if ((m->type < 0)
1501                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
1502                 return NULL;
1503         return matchers_tostring[m->type](m, buf, buflen);
1504 } /* sdb_store_matcher_tostring */
1506 int
1507 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1508                 sdb_store_lookup_cb cb, void *user_data)
1510         scan_iter_data_t data = { m, filter, cb, user_data };
1512         if (! cb)
1513                 return -1;
1514         return sdb_store_iterate(scan_iter, &data);
1515 } /* sdb_store_scan */
1517 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */