Code

store: Let compare matchers fail if any of the operands is NULL.
[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 or if any of them
383  * evaluated to NULL.
384  */
385 static int
386 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
387                 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
389         sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
390         int status;
392         if (sdb_store_expr_eval(e1, obj, &v1, filter))
393                 return INT_MAX;
394         if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
395                 sdb_data_free_datum(&v1);
396                 return INT_MAX;
397         }
399         if (sdb_data_isnull(&v1) || (sdb_data_isnull(&v2)))
400                 status = INT_MAX;
401         else if (v1.type == v2.type)
402                 status = sdb_data_cmp(&v1, &v2);
403         else
404                 status = sdb_data_strcmp(&v1, &v2);
406         sdb_data_free_datum(&v1);
407         sdb_data_free_datum(&v2);
408         return status;
409 } /* cmp_expr */
411 static int
412 match_cmp_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
413                 sdb_store_matcher_t *filter)
415         int status;
416         assert(m->type == MATCHER_CMP_LT);
417         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
418         return (status != INT_MAX) && (status < 0);
419 } /* match_cmp_lt */
421 static int
422 match_cmp_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
423                 sdb_store_matcher_t *filter)
425         int status;
426         assert(m->type == MATCHER_CMP_LE);
427         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
428         return (status != INT_MAX) && (status <= 0);
429 } /* match_cmp_le */
431 static int
432 match_cmp_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
433                 sdb_store_matcher_t *filter)
435         int status;
436         assert(m->type == MATCHER_CMP_EQ);
437         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
438         return (status != INT_MAX) && (! status);
439 } /* match_cmp_eq */
441 static int
442 match_cmp_ne(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
443                 sdb_store_matcher_t *filter)
445         int status;
446         assert(m->type == MATCHER_CMP_NE);
447         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
448         return (status != INT_MAX) && status;
449 } /* match_cmp_ne */
451 static int
452 match_cmp_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
453                 sdb_store_matcher_t *filter)
455         int status;
456         assert(m->type == MATCHER_CMP_GE);
457         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
458         return (status != INT_MAX) && (status >= 0);
459 } /* match_cmp_ge */
461 static int
462 match_cmp_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
463                 sdb_store_matcher_t *filter)
465         int status;
466         assert(m->type == MATCHER_CMP_GT);
467         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
468         return (status != INT_MAX) && (status > 0);
469 } /* match_cmp_gt */
471 static int
472 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
473                 sdb_store_matcher_t *filter)
475         sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
476         int status = 1;
478         assert(m->type == MATCHER_IN);
480         if ((sdb_store_expr_eval(CMP_M(m)->left, obj, &value, filter))
481                         || (sdb_store_expr_eval(CMP_M(m)->right, obj, &array, filter)))
482                 status = 0;
484         if (status)
485                 status = sdb_data_inarray(&value, &array);
487         sdb_data_free_datum(&value);
488         sdb_data_free_datum(&array);
489         return status;
490 } /* match_in */
492 static int
493 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
494                 sdb_store_matcher_t *filter)
496         sdb_data_t v = SDB_DATA_INIT;
497         int status = 0;
499         regex_t regex;
500         _Bool free_regex = 0;
502         assert((m->type == MATCHER_REGEX)
503                         || (m->type == MATCHER_NREGEX));
505         if (! CMP_M(m)->right->type) {
506                 assert(CMP_M(m)->right->data.type == SDB_TYPE_REGEX);
507                 regex = CMP_M(m)->right->data.data.re.regex;
508         }
509         else {
510                 sdb_data_t tmp = SDB_DATA_INIT;
511                 char *raw;
513                 if (sdb_store_expr_eval(CMP_M(m)->right, obj, &tmp, filter))
514                         return 0;
516                 if (tmp.type != SDB_TYPE_STRING) {
517                         sdb_data_free_datum(&tmp);
518                         return 0;
519                 }
521                 raw = tmp.data.string;
522                 if (sdb_data_parse(raw, SDB_TYPE_REGEX, &tmp)) {
523                         free(raw);
524                         return 0;
525                 }
527                 regex = tmp.data.re.regex;
528                 free_regex = 1;
529                 free(tmp.data.re.raw);
530                 free(raw);
531         }
533         if (sdb_store_expr_eval(CMP_M(m)->left, obj, &v, filter))
534                 status = 0;
535         else {
536                 char value[sdb_data_strlen(&v) + 1];
537                 if (sdb_data_format(&v, value, sizeof(value), SDB_UNQUOTED) < 0)
538                         status = 0;
539                 else if (! regexec(&regex, value, 0, NULL, 0))
540                         status = 1;
541         }
543         if (free_regex)
544                 regfree(&regex);
545         sdb_data_free_datum(&v);
546         if (m->type == MATCHER_NREGEX)
547                 return !status;
548         return status;
549 } /* match_regex */
551 static int
552 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
553                 sdb_store_matcher_t *filter)
555         sdb_data_t v = SDB_DATA_INIT;
556         int status;
558         assert((m->type == MATCHER_ISNULL) || (m->type == MATCHER_ISNNULL));
560         /* TODO: this might hide real errors;
561          * improve error reporting and propagation */
562         if (sdb_store_expr_eval(ISNULL_M(m)->expr, obj, &v, filter)
563                         || sdb_data_isnull(&v))
564                 status = 1;
565         else
566                 status = 0;
568         sdb_data_free_datum(&v);
569         if (m->type == MATCHER_ISNNULL)
570                 return !status;
571         return status;
572 } /* match_isnull */
574 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
575                 sdb_store_matcher_t *);
577 /* this array needs to be indexable by the matcher types;
578  * -> update the enum in store-private.h when updating this */
579 static matcher_cb
580 matchers[] = {
581         match_logical,
582         match_logical,
583         match_unary,
584         match_name,
585         match_attr,
586         match_child,
587         match_child,
588         match_child,
589         match_lt,
590         match_le,
591         match_eq,
592         match_ge,
593         match_gt,
594         match_cmp_lt,
595         match_cmp_le,
596         match_cmp_eq,
597         match_cmp_ne,
598         match_cmp_ge,
599         match_cmp_gt,
600         match_in,
601         match_regex,
602         match_regex,
603         match_isnull,
604         match_isnull,
605 };
607 /*
608  * private conditional types
609  */
611 static int
612 attr_cond_init(sdb_object_t *obj, va_list ap)
614         const char *name = va_arg(ap, const char *);
615         sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
617         if (! name)
618                 return -1;
620         SDB_STORE_COND(obj)->cmp = attr_cmp;
622         ATTR_C(obj)->name = strdup(name);
623         if (! ATTR_C(obj)->name)
624                 return -1;
625         ATTR_C(obj)->expr = expr;
626         sdb_object_ref(SDB_OBJ(expr));
627         return 0;
628 } /* attr_cond_init */
630 static void
631 attr_cond_destroy(sdb_object_t *obj)
633         if (ATTR_C(obj)->name)
634                 free(ATTR_C(obj)->name);
635         sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
636 } /* attr_cond_destroy */
638 static sdb_type_t attr_cond_type = {
639         /* size = */ sizeof(attr_cond_t),
640         /* init = */ attr_cond_init,
641         /* destroy = */ attr_cond_destroy,
642 };
644 static int
645 obj_cond_init(sdb_object_t *obj, va_list ap)
647         int field = va_arg(ap, int);
648         sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
650         SDB_STORE_COND(obj)->cmp = obj_cmp;
652         OBJ_C(obj)->field = field;
653         OBJ_C(obj)->expr = expr;
654         sdb_object_ref(SDB_OBJ(expr));
655         return 0;
656 } /* obj_cond_init */
658 static void
659 obj_cond_destroy(sdb_object_t *obj)
661         sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr));
662 } /* obj_cond_destroy */
664 static sdb_type_t obj_cond_type = {
665         /* size = */ sizeof(obj_cond_t),
666         /* init = */ obj_cond_init,
667         /* destroy = */ obj_cond_destroy,
668 };
670 /*
671  * private matcher types
672  */
674 /* initializes a string matcher consuming two elements from ap */
675 static int
676 string_matcher_init(string_matcher_t *m, va_list ap)
678         const char *name = va_arg(ap, const char *);
679         const char *name_re = va_arg(ap, const char *);
681         if (name) {
682                 m->name = strdup(name);
683                 if (! m->name)
684                         return -1;
685         }
686         if (name_re) {
687                 m->name_re = malloc(sizeof(*m->name_re));
688                 if (! m->name_re)
689                         return -1;
690                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
691                         return -1;
692         }
693         return 0;
694 } /* string_matcher_init */
696 static void
697 string_matcher_destroy(string_matcher_t *m)
699         if (m->name)
700                 free(m->name);
701         if (m->name_re) {
702                 regfree(m->name_re);
703                 free(m->name_re);
704         }
705 } /* string_matcher_destroy */
707 static char *
708 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
710         snprintf(buf, buflen, "{ %s%s%s, %p }",
711                         m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
712                         m->name_re);
713         return buf;
714 } /* string_tostring */
716 /* initializes a name matcher */
717 static int
718 name_matcher_init(sdb_object_t *obj, va_list ap)
720         name_matcher_t *m = NAME_M(obj);
721         M(obj)->type = MATCHER_NAME;
722         return string_matcher_init(&m->name, ap);
723 } /* name_matcher_init */
725 static void
726 name_matcher_destroy(sdb_object_t *obj)
728         name_matcher_t *m = NAME_M(obj);
729         string_matcher_destroy(&m->name);
730 } /* name_matcher_destroy */
732 static char *
733 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
735         char name[buflen + 1];
736         assert(m->type == MATCHER_NAME);
737         snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
738                         SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
739                         string_tostring(&NAME_M(m)->name, name, sizeof(name)));
740         return buf;
741 } /* name_tostring */
743 static int
744 attr_matcher_init(sdb_object_t *obj, va_list ap)
746         attr_matcher_t *attr = ATTR_M(obj);
747         const char *name = va_arg(ap, const char *);
749         M(obj)->type = MATCHER_ATTR;
750         if (name) {
751                 attr->name = strdup(name);
752                 if (! attr->name)
753                         return -1;
754         }
755         return string_matcher_init(&attr->value, ap);
756 } /* attr_matcher_init */
758 static void
759 attr_matcher_destroy(sdb_object_t *obj)
761         attr_matcher_t *attr = ATTR_M(obj);
762         if (attr->name)
763                 free(attr->name);
764         attr->name = NULL;
765         string_matcher_destroy(&attr->value);
766 } /* attr_matcher_destroy */
768 static char *
769 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
771         char value[buflen + 1];
773         if (! m) {
774                 snprintf(buf, buflen, "ATTR{}");
775                 return buf;
776         }
778         assert(m->type == MATCHER_ATTR);
779         snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
780                         string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
781         return buf;
782 } /* attr_tostring */
784 static int
785 cond_matcher_init(sdb_object_t *obj, va_list ap)
787         int type = va_arg(ap, int);
788         sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
790         if (! cond)
791                 return -1;
793         sdb_object_ref(SDB_OBJ(cond));
795         M(obj)->type = type;
796         COND_M(obj)->cond = cond;
797         return 0;
798 } /* cond_matcher_init */
800 static void
801 cond_matcher_destroy(sdb_object_t *obj)
803         sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
804 } /* cond_matcher_destroy */
806 static char *
807 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
809         const char *type, *id;
810         sdb_data_t value = SDB_DATA_INIT;
811         char value_str[buflen];
812         sdb_store_expr_t *expr;
814         if (COND_M(m)->cond->cmp == attr_cmp) {
815                 type = "ATTR";
816                 id = ATTR_C(COND_M(m)->cond)->name;
817                 expr = ATTR_C(COND_M(m)->cond)->expr;
818         }
819         else if (COND_M(m)->cond->cmp == obj_cmp) {
820                 type = "OBJ";
821                 id = SDB_FIELD_TO_NAME(OBJ_C(COND_M(m)->cond)->field);
822                 expr = OBJ_C(COND_M(m)->cond)->expr;
823         }
824         else {
825                 snprintf(buf, buflen, "<unknown>");
826                 return buf;
827         }
829         if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL))
830                 snprintf(value_str, sizeof(value_str), "ERR");
831         else if (sdb_data_format(&value, value_str, sizeof(value_str),
832                                 SDB_SINGLE_QUOTED) < 0)
833                 snprintf(value_str, sizeof(value_str), "ERR");
834         snprintf(buf, buflen, "%s[%s]{ %s %s }", type, id,
835                         MATCHER_SYM(m->type), value_str);
836         sdb_data_free_datum(&value);
837         return buf;
838 } /* cond_tostring */
840 static int
841 op_matcher_init(sdb_object_t *obj, va_list ap)
843         M(obj)->type = va_arg(ap, int);
844         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
845                 return -1;
847         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
848         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
849         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
850         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
852         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
853                 return -1;
854         return 0;
855 } /* op_matcher_init */
857 static void
858 op_matcher_destroy(sdb_object_t *obj)
860         if (OP_M(obj)->left)
861                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
862         if (OP_M(obj)->right)
863                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
864 } /* op_matcher_destroy */
866 static char *
867 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
869         char left[buflen + 1], right[buflen + 1];
871         if (! m) {
872                 /* this should not happen */
873                 snprintf(buf, buflen, "()");
874                 return buf;
875         }
877         assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
878         snprintf(buf, buflen, "(%s, %s, %s)",
879                         m->type == MATCHER_OR ? "OR" : "AND",
880                         sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
881                         sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
882         return buf;
883 } /* op_tostring */
885 static int
886 child_matcher_init(sdb_object_t *obj, va_list ap)
888         M(obj)->type = va_arg(ap, int);
889         CHILD_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
891         if (! CHILD_M(obj)->m)
892                 return -1;
894         sdb_object_ref(SDB_OBJ(CHILD_M(obj)->m));
895         return 0;
896 } /* child_matcher_init */
898 static void
899 child_matcher_destroy(sdb_object_t *obj)
901         sdb_object_deref(SDB_OBJ(CHILD_M(obj)->m));
902 } /* child_matcher_destroy */
904 static char *
905 child_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
907         snprintf(buf, buflen, "%s:", MATCHER_SYM(m->type));
908         buf[buflen - 1] = '\0';
909         sdb_store_matcher_tostring(CHILD_M(m)->m,
910                         buf + strlen(buf), buflen - strlen(buf));
911         return buf;
912 } /* child_tostring */
914 static int
915 cmp_matcher_init(sdb_object_t *obj, va_list ap)
917         M(obj)->type = va_arg(ap, int);
919         CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
920         sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
921         CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
922         sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
924         if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
925                 return -1;
926         return 0;
927 } /* cmp_matcher_init */
929 static void
930 cmp_matcher_destroy(sdb_object_t *obj)
932         sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
933         sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
934 } /* cmp_matcher_destroy */
936 static char *
937 cmp_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
939         if (! m) {
940                 /* this should not happen */
941                 snprintf(buf, buflen, "()");
942                 return buf;
943         }
945         /* TODO */
946         snprintf(buf, buflen, "CMP_MATCHER(%d)", m->type);
947         return buf;
948 } /* cmp_tostring */
950 static int
951 uop_matcher_init(sdb_object_t *obj, va_list ap)
953         M(obj)->type = va_arg(ap, int);
954         if (M(obj)->type != MATCHER_NOT)
955                 return -1;
957         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
958         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
960         if (! UOP_M(obj)->op)
961                 return -1;
962         return 0;
963 } /* uop_matcher_init */
965 static void
966 uop_matcher_destroy(sdb_object_t *obj)
968         if (UOP_M(obj)->op)
969                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
970 } /* uop_matcher_destroy */
972 static char *
973 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
975         char op[buflen + 1];
977         if (! m) {
978                 /* this should not happen */
979                 snprintf(buf, buflen, "()");
980                 return buf;
981         }
983         assert(m->type == MATCHER_NOT);
984         snprintf(buf, buflen, "(NOT, %s)",
985                         sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
986         return buf;
987 } /* uop_tostring */
989 static int
990 isnull_matcher_init(sdb_object_t *obj, va_list ap)
992         M(obj)->type = va_arg(ap, int);
993         if ((M(obj)->type != MATCHER_ISNULL) && (M(obj)->type != MATCHER_ISNNULL))
994                 return -1;
996         ISNULL_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
997         sdb_object_ref(SDB_OBJ(ISNULL_M(obj)->expr));
998         return 0;
999 } /* isnull_matcher_init */
1001 static void
1002 isnull_matcher_destroy(sdb_object_t *obj)
1004         sdb_object_deref(SDB_OBJ(ISNULL_M(obj)->expr));
1005         ISNULL_M(obj)->expr = NULL;
1006 } /* isnull_matcher_destroy */
1008 static char *
1009 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1011         /* XXX */
1012         if (m->type == MATCHER_ISNULL)
1013                 strncpy(buf, "(IS NULL)", buflen);
1014         else
1015                 strncpy(buf, "(IS NOT NULL)", buflen);
1016         return buf;
1017 } /* isnull_tostring */
1019 static sdb_type_t name_type = {
1020         /* size = */ sizeof(name_matcher_t),
1021         /* init = */ name_matcher_init,
1022         /* destroy = */ name_matcher_destroy,
1023 };
1025 static sdb_type_t attr_type = {
1026         /* size = */ sizeof(attr_matcher_t),
1027         /* init = */ attr_matcher_init,
1028         /* destroy = */ attr_matcher_destroy,
1029 };
1031 static sdb_type_t cond_type = {
1032         /* size = */ sizeof(cond_matcher_t),
1033         /* init = */ cond_matcher_init,
1034         /* destroy = */ cond_matcher_destroy,
1035 };
1037 static sdb_type_t op_type = {
1038         /* size = */ sizeof(op_matcher_t),
1039         /* init = */ op_matcher_init,
1040         /* destroy = */ op_matcher_destroy,
1041 };
1043 static sdb_type_t uop_type = {
1044         /* size = */ sizeof(uop_matcher_t),
1045         /* init = */ uop_matcher_init,
1046         /* destroy = */ uop_matcher_destroy,
1047 };
1049 static sdb_type_t child_type = {
1050         /* size = */ sizeof(child_matcher_t),
1051         /* init = */ child_matcher_init,
1052         /* destroy = */ child_matcher_destroy,
1053 };
1055 static sdb_type_t cmp_type = {
1056         /* size = */ sizeof(cmp_matcher_t),
1057         /* init = */ cmp_matcher_init,
1058         /* destroy = */ cmp_matcher_destroy,
1059 };
1061 static sdb_type_t isnull_type = {
1062         /* size = */ sizeof(isnull_matcher_t),
1063         /* init = */ isnull_matcher_init,
1064         /* destroy = */ isnull_matcher_destroy,
1065 };
1067 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
1069 /* this array needs to be indexable by the matcher types;
1070  * -> update the enum in store-private.h when updating this */
1071 static matcher_tostring_cb
1072 matchers_tostring[] = {
1073         op_tostring,
1074         op_tostring,
1075         uop_tostring,
1076         name_tostring,
1077         attr_tostring,
1078         child_tostring,
1079         child_tostring,
1080         child_tostring,
1081         cond_tostring,
1082         cond_tostring,
1083         cond_tostring,
1084         cond_tostring,
1085         cond_tostring,
1086         cmp_tostring,
1087         cmp_tostring,
1088         cmp_tostring,
1089         cmp_tostring,
1090         cmp_tostring,
1091         cmp_tostring,
1092         cmp_tostring,
1093         cmp_tostring,
1094         cmp_tostring,
1095         isnull_tostring,
1096         isnull_tostring,
1097 };
1099 /*
1100  * public API
1101  */
1103 sdb_store_cond_t *
1104 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
1106         return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
1107                                 name, expr));
1108 } /* sdb_store_attr_cond */
1110 sdb_store_cond_t *
1111 sdb_store_obj_cond(int field, sdb_store_expr_t *expr)
1113         return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type,
1114                                 field, expr));
1115 } /* sdb_store_obj_cond */
1117 sdb_store_matcher_t *
1118 sdb_store_name_matcher(int type, const char *name, _Bool re)
1120         sdb_store_matcher_t *m;
1122         if (re)
1123                 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
1124         else
1125                 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
1127         if (! m)
1128                 return NULL;
1130         NAME_M(m)->obj_type = type;
1131         return m;
1132 } /* sdb_store_name_matcher */
1134 sdb_store_matcher_t *
1135 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
1137         sdb_store_matcher_t *m;
1139         if (! name)
1140                 return NULL;
1142         if (re)
1143                 m = M(sdb_object_create("attr-matcher", attr_type,
1144                                         name, NULL, value));
1145         else
1146                 m = M(sdb_object_create("attr-matcher", attr_type,
1147                                         name, value, NULL));
1148         return m;
1149 } /* sdb_store_attr_matcher */
1151 sdb_store_matcher_t *
1152 sdb_store_child_matcher(int type, sdb_store_matcher_t *m)
1154         if (type == SDB_SERVICE)
1155                 type = MATCHER_SERVICE;
1156         else if (type == SDB_METRIC)
1157                 type = MATCHER_METRIC;
1158         else if (type == SDB_ATTRIBUTE)
1159                 type = MATCHER_ATTRIBUTE;
1160         else
1161                 return NULL;
1162         return M(sdb_object_create("any-matcher", child_type, type, m));
1163 } /* sdb_store_child_matcher */
1165 sdb_store_matcher_t *
1166 sdb_store_lt_matcher(sdb_store_cond_t *cond)
1168         return M(sdb_object_create("lt-matcher", cond_type,
1169                                 MATCHER_LT, cond));
1170 } /* sdb_store_lt_matcher */
1172 sdb_store_matcher_t *
1173 sdb_store_le_matcher(sdb_store_cond_t *cond)
1175         return M(sdb_object_create("le-matcher", cond_type,
1176                                 MATCHER_LE, cond));
1177 } /* sdb_store_le_matcher */
1179 sdb_store_matcher_t *
1180 sdb_store_eq_matcher(sdb_store_cond_t *cond)
1182         return M(sdb_object_create("eq-matcher", cond_type,
1183                                 MATCHER_EQ, cond));
1184 } /* sdb_store_eq_matcher */
1186 sdb_store_matcher_t *
1187 sdb_store_ge_matcher(sdb_store_cond_t *cond)
1189         return M(sdb_object_create("ge-matcher", cond_type,
1190                                 MATCHER_GE, cond));
1191 } /* sdb_store_ge_matcher */
1193 sdb_store_matcher_t *
1194 sdb_store_gt_matcher(sdb_store_cond_t *cond)
1196         return M(sdb_object_create("gt-matcher", cond_type,
1197                                 MATCHER_GT, cond));
1198 } /* sdb_store_gt_matcher */
1200 /*
1201  * TODO: Rename sdb_store_cmp_* to sdb_store_* once the old code is unused and
1202  * has been removed.
1203  */
1205 sdb_store_matcher_t *
1206 sdb_store_cmp_lt(sdb_store_expr_t *left, sdb_store_expr_t *right)
1208         return M(sdb_object_create("lt-matcher", cmp_type,
1209                                 MATCHER_CMP_LT, left, right));
1210 } /* sdb_store_cmp_lt */
1212 sdb_store_matcher_t *
1213 sdb_store_cmp_le(sdb_store_expr_t *left, sdb_store_expr_t *right)
1215         return M(sdb_object_create("le-matcher", cmp_type,
1216                                 MATCHER_CMP_LE, left, right));
1217 } /* sdb_store_cmp_le */
1219 sdb_store_matcher_t *
1220 sdb_store_cmp_eq(sdb_store_expr_t *left, sdb_store_expr_t *right)
1222         return M(sdb_object_create("eq-matcher", cmp_type,
1223                                 MATCHER_CMP_EQ, left, right));
1224 } /* sdb_store_cmp_eq */
1226 sdb_store_matcher_t *
1227 sdb_store_cmp_ne(sdb_store_expr_t *left, sdb_store_expr_t *right)
1229         return M(sdb_object_create("ne-matcher", cmp_type,
1230                                 MATCHER_CMP_NE, left, right));
1231 } /* sdb_store_cmp_ne */
1233 sdb_store_matcher_t *
1234 sdb_store_cmp_ge(sdb_store_expr_t *left, sdb_store_expr_t *right)
1236         return M(sdb_object_create("ge-matcher", cmp_type,
1237                                 MATCHER_CMP_GE, left, right));
1238 } /* sdb_store_cmp_ge */
1240 sdb_store_matcher_t *
1241 sdb_store_cmp_gt(sdb_store_expr_t *left, sdb_store_expr_t *right)
1243         return M(sdb_object_create("gt-matcher", cmp_type,
1244                                 MATCHER_CMP_GT, left, right));
1245 } /* sdb_store_cmp_gt */
1247 sdb_store_matcher_t *
1248 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1250         return M(sdb_object_create("in-matcher", cmp_type,
1251                                 MATCHER_IN, left, right));
1252 } /* sdb_store_in_matcher */
1254 sdb_store_matcher_t *
1255 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1257         if (! right->type) {
1258                 if ((right->data.type != SDB_TYPE_STRING)
1259                                 && (right->data.type != SDB_TYPE_REGEX))
1260                         return NULL;
1262                 if (right->data.type == SDB_TYPE_STRING) {
1263                         char *raw = right->data.data.string;
1264                         if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
1265                                 return NULL;
1266                         free(raw);
1267                 }
1268         }
1269         return M(sdb_object_create("regex-matcher", cmp_type,
1270                                 MATCHER_REGEX, left, right));
1271 } /* sdb_store_regex_matcher */
1273 sdb_store_matcher_t *
1274 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
1276         sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
1277         if (! m)
1278                 return NULL;
1279         m->type = MATCHER_NREGEX;
1280         return m;
1281 } /* sdb_store_nregex_matcher */
1283 sdb_store_matcher_t *
1284 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
1286         return M(sdb_object_create("isnull-matcher", isnull_type,
1287                                 MATCHER_ISNULL, expr));
1288 } /* sdb_store_isnull_matcher */
1290 sdb_store_matcher_t *
1291 sdb_store_isnnull_matcher(sdb_store_expr_t *expr)
1293         return M(sdb_object_create("isnull-matcher", isnull_type,
1294                                 MATCHER_ISNNULL, expr));
1295 } /* sdb_store_isnnull_matcher */
1297 sdb_store_matcher_op_cb
1298 sdb_store_parse_matcher_op(const char *op)
1300         if (! strcasecmp(op, "<"))
1301                 return sdb_store_cmp_lt;
1302         else if (! strcasecmp(op, "<="))
1303                 return sdb_store_cmp_le;
1304         else if (! strcasecmp(op, "="))
1305                 return sdb_store_cmp_eq;
1306         else if (! strcasecmp(op, "!="))
1307                 return sdb_store_cmp_ne;
1308         else if (! strcasecmp(op, ">="))
1309                 return sdb_store_cmp_ge;
1310         else if (! strcasecmp(op, ">"))
1311                 return sdb_store_cmp_gt;
1312         else if (! strcasecmp(op, "=~"))
1313                 return sdb_store_regex_matcher;
1314         else if (! strcasecmp(op, "!~"))
1315                 return sdb_store_nregex_matcher;
1316         return NULL;
1317 } /* sdb_store_parse_matcher_op */
1319 int
1320 sdb_store_parse_object_type_plural(const char *name)
1322         if (! strcasecmp(name, "hosts"))
1323                 return SDB_HOST;
1324         else if (! strcasecmp(name, "services"))
1325                 return SDB_SERVICE;
1326         else if (! strcasecmp(name, "metrics"))
1327                 return SDB_METRIC;
1328         return -1;
1329 } /* sdb_store_parse_object_type_plural */
1331 int
1332 sdb_store_parse_field_name(const char *name)
1334         if (! strcasecmp(name, "name"))
1335                 return SDB_FIELD_NAME;
1336         else if (! strcasecmp(name, "last_update"))
1337                 return SDB_FIELD_LAST_UPDATE;
1338         else if (! strcasecmp(name, "age"))
1339                 return SDB_FIELD_AGE;
1340         else if (! strcasecmp(name, "interval"))
1341                 return SDB_FIELD_INTERVAL;
1342         else if (! strcasecmp(name, "backend"))
1343                 return SDB_FIELD_BACKEND;
1344         return -1;
1345 } /* sdb_store_parse_field_name */
1347 static sdb_store_matcher_t *
1348 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
1350         sdb_store_matcher_t *tmp;
1352         if ((! m) || (! inv))
1353                 return m;
1355         tmp = sdb_store_inv_matcher(m);
1356         /* pass ownership to the inverse matcher */
1357         sdb_object_deref(SDB_OBJ(m));
1358         return tmp;
1359 } /* maybe_inv_matcher */
1361 static int
1362 parse_cond_op(const char *op,
1363                 sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv)
1365         *inv = 0;
1366         if (! strcasecmp(op, "<"))
1367                 *matcher = sdb_store_lt_matcher;
1368         else if (! strcasecmp(op, "<="))
1369                 *matcher = sdb_store_le_matcher;
1370         else if (! strcasecmp(op, "="))
1371                 *matcher = sdb_store_eq_matcher;
1372         else if (! strcasecmp(op, ">="))
1373                 *matcher = sdb_store_ge_matcher;
1374         else if (! strcasecmp(op, ">"))
1375                 *matcher = sdb_store_gt_matcher;
1376         else if (! strcasecmp(op, "!=")) {
1377                 *matcher = sdb_store_eq_matcher;
1378                 *inv = 1;
1379         }
1380         else
1381                 return -1;
1382         return 0;
1383 } /* parse_cond_op */
1385 static sdb_store_matcher_t *
1386 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
1388         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1389         sdb_store_matcher_t *m;
1390         sdb_store_cond_t *cond;
1391         _Bool inv = 0;
1393         if (! attr)
1394                 return NULL;
1396         if (! expr)
1397                 return NULL;
1398         else if (parse_cond_op(op, &matcher, &inv))
1399                 return NULL;
1401         cond = sdb_store_attr_cond(attr, expr);
1402         if (! cond)
1403                 return NULL;
1405         m = matcher(cond);
1406         /* pass ownership to 'm' or destroy in case of an error */
1407         sdb_object_deref(SDB_OBJ(cond));
1408         return maybe_inv_matcher(m, inv);
1409 } /* parse_attr_cmp */
1411 sdb_store_matcher_t *
1412 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
1413                 const char *op, sdb_store_expr_t *expr)
1415         int type = -1;
1416         _Bool inv = 0;
1417         _Bool re = 0;
1419         sdb_data_t value = SDB_DATA_INIT;
1420         sdb_store_matcher_t *m = NULL;
1422         if (! strcasecmp(obj_type, "host"))
1423                 type = SDB_HOST;
1424         else if (! strcasecmp(obj_type, "service"))
1425                 type = SDB_SERVICE;
1426         else if (! strcasecmp(obj_type, "metric"))
1427                 type = SDB_METRIC;
1428         else if (! strcasecmp(obj_type, "attribute"))
1429                 type = SDB_ATTRIBUTE;
1430         else
1431                 return NULL;
1433         /* XXX: this code sucks! */
1434         if (! strcasecmp(op, "=")) {
1435                 /* nothing to do */
1436         }
1437         else if (! strcasecmp(op, "!=")) {
1438                 inv = 1;
1439         }
1440         else if (! strcasecmp(op, "=~")) {
1441                 re = 1;
1442         }
1443         else if (! strcasecmp(op, "!~")) {
1444                 inv = 1;
1445                 re = 1;
1446         }
1447         else if (type == SDB_ATTRIBUTE)
1448                 return parse_attr_cmp(attr, op, expr);
1449         else
1450                 return NULL;
1452         if (! expr)
1453                 return NULL;
1455         if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
1456                         || (value.type != SDB_TYPE_STRING)) {
1457                 sdb_data_free_datum(&value);
1458                 if (type != SDB_ATTRIBUTE)
1459                         return NULL;
1460                 return parse_attr_cmp(attr, op, expr);
1461         }
1463         if (! attr)
1464                 m = sdb_store_name_matcher(type, value.data.string, re);
1465         else if (type == SDB_ATTRIBUTE)
1466                 m = sdb_store_attr_matcher(attr, value.data.string, re);
1468         sdb_data_free_datum(&value);
1469         return maybe_inv_matcher(m, inv);
1470 } /* sdb_store_matcher_parse_cmp */
1472 sdb_store_matcher_t *
1473 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
1474                 sdb_store_expr_t *expr)
1476         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1477         sdb_store_matcher_t *m;
1478         sdb_store_cond_t *cond;
1479         _Bool inv = 0;
1481         int field;
1483         if (! expr)
1484                 return NULL;
1486         field = sdb_store_parse_field_name(name);
1487         if (field < 0)
1488                 return NULL;
1490         if (parse_cond_op(op, &matcher, &inv))
1491                 return NULL;
1492         cond = sdb_store_obj_cond(field, expr);
1493         if (! cond)
1494                 return NULL;
1496         assert(matcher);
1497         m = matcher(cond);
1498         /* pass ownership to 'm' or destroy in case of an error */
1499         sdb_object_deref(SDB_OBJ(cond));
1500         return maybe_inv_matcher(m, inv);
1501 } /* sdb_store_matcher_parse_field_cmp */
1503 sdb_store_matcher_t *
1504 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1506         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
1507                                 left, right));
1508 } /* sdb_store_dis_matcher */
1510 sdb_store_matcher_t *
1511 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1513         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
1514                                 left, right));
1515 } /* sdb_store_con_matcher */
1517 sdb_store_matcher_t *
1518 sdb_store_inv_matcher(sdb_store_matcher_t *m)
1520         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
1521 } /* sdb_store_inv_matcher */
1523 int
1524 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
1525                 sdb_store_matcher_t *filter)
1527         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
1528                 return 0;
1530         /* "NULL" always matches */
1531         if ((! m) || (! obj))
1532                 return 1;
1534         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1535                 return 0;
1537         return matchers[m->type](m, obj, filter);
1538 } /* sdb_store_matcher_matches */
1540 char *
1541 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1543         if (! m)
1544                 return NULL;
1546         if ((m->type < 0)
1547                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
1548                 return NULL;
1549         return matchers_tostring[m->type](m, buf, buflen);
1550 } /* sdb_store_matcher_tostring */
1552 int
1553 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1554                 sdb_store_lookup_cb cb, void *user_data)
1556         scan_iter_data_t data = { m, filter, cb, user_data };
1558         if (! cb)
1559                 return -1;
1560         return sdb_store_iterate(scan_iter, &data);
1561 } /* sdb_store_scan */
1563 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */