Code

store: Added matchers comparing two expressions.
[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_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
291                 sdb_store_matcher_t *filter)
293         int status;
294         assert(m->type == MATCHER_LT);
295         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
296         return (status != INT_MAX) && (status < 0);
297 } /* match_lt */
299 static int
300 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
301                 sdb_store_matcher_t *filter)
303         int status;
304         assert(m->type == MATCHER_LE);
305         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
306         return (status != INT_MAX) && (status <= 0);
307 } /* match_le */
309 static int
310 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
311                 sdb_store_matcher_t *filter)
313         int status;
314         assert(m->type == MATCHER_EQ);
315         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
316         return (status != INT_MAX) && (! status);
317 } /* match_eq */
319 static int
320 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
321                 sdb_store_matcher_t *filter)
323         int status;
324         assert(m->type == MATCHER_GE);
325         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
326         return (status != INT_MAX) && (status >= 0);
327 } /* match_ge */
329 static int
330 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
331                 sdb_store_matcher_t *filter)
333         int status;
334         assert(m->type == MATCHER_GT);
335         status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
336         return (status != INT_MAX) && (status > 0);
337 } /* match_gt */
339 /*
340  * cmp_expr:
341  * Compare the values of two expressions when evaluating them using the
342  * specified stored object and filter. Returns a value less than, equal to, or
343  * greater than zero if the value of the first expression compares less than,
344  * equal to, or greater than the value of the second expression. Returns
345  * INT_MAX if any of the expressions could not be evaluated.
346  */
347 static int
348 cmp_expr(sdb_store_expr_t *e1, sdb_store_expr_t *e2,
349                 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
351         sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
352         int status;
354         if (sdb_store_expr_eval(e1, obj, &v1, filter))
355                 return INT_MAX;
356         if (sdb_store_expr_eval(e2, obj, &v2, filter)) {
357                 sdb_data_free_datum(&v1);
358                 return INT_MAX;
359         }
361         if (v1.type == v2.type)
362                 status = sdb_data_cmp(&v1, &v2);
363         else
364                 status = sdb_data_strcmp(&v1, &v2);
366         sdb_data_free_datum(&v1);
367         sdb_data_free_datum(&v2);
368         return status;
369 } /* cmp_expr */
371 static int
372 match_cmp_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
373                 sdb_store_matcher_t *filter)
375         int status;
376         assert(m->type == MATCHER_CMP_LT);
377         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
378         return (status != INT_MAX) && (status < 0);
379 } /* match_cmp_lt */
381 static int
382 match_cmp_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
383                 sdb_store_matcher_t *filter)
385         int status;
386         assert(m->type == MATCHER_CMP_LE);
387         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
388         return (status != INT_MAX) && (status <= 0);
389 } /* match_cmp_le */
391 static int
392 match_cmp_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
393                 sdb_store_matcher_t *filter)
395         int status;
396         assert(m->type == MATCHER_CMP_EQ);
397         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
398         return (status != INT_MAX) && (! status);
399 } /* match_cmp_eq */
401 static int
402 match_cmp_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
403                 sdb_store_matcher_t *filter)
405         int status;
406         assert(m->type == MATCHER_CMP_GE);
407         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
408         return (status != INT_MAX) && (status >= 0);
409 } /* match_cmp_ge */
411 static int
412 match_cmp_gt(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_GT);
417         status = cmp_expr(CMP_M(m)->left, CMP_M(m)->right, obj, filter);
418         return (status != INT_MAX) && (status > 0);
419 } /* match_cmp_gt */
421 static int
422 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
423                 sdb_store_matcher_t *filter)
425         assert(m->type == MATCHER_ISNULL);
426         if (obj->type != SDB_HOST)
427                 return 0;
428         return attr_get(HOST(obj), ISNULL_M(m)->attr_name, filter) == NULL;
429 } /* match_isnull */
431 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
432                 sdb_store_matcher_t *);
434 /* this array needs to be indexable by the matcher types;
435  * -> update the enum in store-private.h when updating this */
436 static matcher_cb
437 matchers[] = {
438         match_logical,
439         match_logical,
440         match_unary,
441         match_name,
442         match_attr,
443         match_lt,
444         match_le,
445         match_eq,
446         match_ge,
447         match_gt,
448         match_cmp_lt,
449         match_cmp_le,
450         match_cmp_eq,
451         match_cmp_ge,
452         match_cmp_gt,
453         match_isnull,
454 };
456 /*
457  * private conditional types
458  */
460 static int
461 attr_cond_init(sdb_object_t *obj, va_list ap)
463         const char *name = va_arg(ap, const char *);
464         sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
466         if (! name)
467                 return -1;
469         SDB_STORE_COND(obj)->cmp = attr_cmp;
471         ATTR_C(obj)->name = strdup(name);
472         if (! ATTR_C(obj)->name)
473                 return -1;
474         ATTR_C(obj)->expr = expr;
475         sdb_object_ref(SDB_OBJ(expr));
476         return 0;
477 } /* attr_cond_init */
479 static void
480 attr_cond_destroy(sdb_object_t *obj)
482         if (ATTR_C(obj)->name)
483                 free(ATTR_C(obj)->name);
484         sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
485 } /* attr_cond_destroy */
487 static sdb_type_t attr_cond_type = {
488         /* size = */ sizeof(attr_cond_t),
489         /* init = */ attr_cond_init,
490         /* destroy = */ attr_cond_destroy,
491 };
493 static int
494 obj_cond_init(sdb_object_t *obj, va_list ap)
496         int field = va_arg(ap, int);
497         sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
499         SDB_STORE_COND(obj)->cmp = obj_cmp;
501         OBJ_C(obj)->field = field;
502         OBJ_C(obj)->expr = expr;
503         sdb_object_ref(SDB_OBJ(expr));
504         return 0;
505 } /* obj_cond_init */
507 static void
508 obj_cond_destroy(sdb_object_t *obj)
510         sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr));
511 } /* obj_cond_destroy */
513 static sdb_type_t obj_cond_type = {
514         /* size = */ sizeof(obj_cond_t),
515         /* init = */ obj_cond_init,
516         /* destroy = */ obj_cond_destroy,
517 };
519 /*
520  * private matcher types
521  */
523 /* initializes a string matcher consuming two elements from ap */
524 static int
525 string_matcher_init(string_matcher_t *m, va_list ap)
527         const char *name = va_arg(ap, const char *);
528         const char *name_re = va_arg(ap, const char *);
530         if (name) {
531                 m->name = strdup(name);
532                 if (! m->name)
533                         return -1;
534         }
535         if (name_re) {
536                 m->name_re = malloc(sizeof(*m->name_re));
537                 if (! m->name_re)
538                         return -1;
539                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
540                         return -1;
541         }
542         return 0;
543 } /* string_matcher_init */
545 static void
546 string_matcher_destroy(string_matcher_t *m)
548         if (m->name)
549                 free(m->name);
550         if (m->name_re) {
551                 regfree(m->name_re);
552                 free(m->name_re);
553         }
554 } /* string_matcher_destroy */
556 static char *
557 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
559         snprintf(buf, buflen, "{ %s%s%s, %p }",
560                         m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
561                         m->name_re);
562         return buf;
563 } /* string_tostring */
565 /* initializes a name matcher */
566 static int
567 name_matcher_init(sdb_object_t *obj, va_list ap)
569         name_matcher_t *m = NAME_M(obj);
570         M(obj)->type = MATCHER_NAME;
571         return string_matcher_init(&m->name, ap);
572 } /* name_matcher_init */
574 static void
575 name_matcher_destroy(sdb_object_t *obj)
577         name_matcher_t *m = NAME_M(obj);
578         string_matcher_destroy(&m->name);
579 } /* name_matcher_destroy */
581 static char *
582 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
584         char name[buflen + 1];
585         assert(m->type == MATCHER_NAME);
586         snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
587                         SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
588                         string_tostring(&NAME_M(m)->name, name, sizeof(name)));
589         return buf;
590 } /* name_tostring */
592 static int
593 attr_matcher_init(sdb_object_t *obj, va_list ap)
595         attr_matcher_t *attr = ATTR_M(obj);
596         const char *name = va_arg(ap, const char *);
598         M(obj)->type = MATCHER_ATTR;
599         if (name) {
600                 attr->name = strdup(name);
601                 if (! attr->name)
602                         return -1;
603         }
604         return string_matcher_init(&attr->value, ap);
605 } /* attr_matcher_init */
607 static void
608 attr_matcher_destroy(sdb_object_t *obj)
610         attr_matcher_t *attr = ATTR_M(obj);
611         if (attr->name)
612                 free(attr->name);
613         attr->name = NULL;
614         string_matcher_destroy(&attr->value);
615 } /* attr_matcher_destroy */
617 static char *
618 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
620         char value[buflen + 1];
622         if (! m) {
623                 snprintf(buf, buflen, "ATTR{}");
624                 return buf;
625         }
627         assert(m->type == MATCHER_ATTR);
628         snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
629                         string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
630         return buf;
631 } /* attr_tostring */
633 static int
634 cond_matcher_init(sdb_object_t *obj, va_list ap)
636         int type = va_arg(ap, int);
637         sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
639         if (! cond)
640                 return -1;
642         sdb_object_ref(SDB_OBJ(cond));
644         M(obj)->type = type;
645         COND_M(obj)->cond = cond;
646         return 0;
647 } /* cond_matcher_init */
649 static void
650 cond_matcher_destroy(sdb_object_t *obj)
652         sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
653 } /* cond_matcher_destroy */
655 static char *
656 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
658         const char *type, *id;
659         sdb_data_t value = SDB_DATA_INIT;
660         char value_str[buflen];
661         sdb_store_expr_t *expr;
663         if (COND_M(m)->cond->cmp == attr_cmp) {
664                 type = "ATTR";
665                 id = ATTR_C(COND_M(m)->cond)->name;
666                 expr = ATTR_C(COND_M(m)->cond)->expr;
667         }
668         else if (COND_M(m)->cond->cmp == obj_cmp) {
669                 type = "OBJ";
670                 id = SDB_FIELD_TO_NAME(OBJ_C(COND_M(m)->cond)->field);
671                 expr = OBJ_C(COND_M(m)->cond)->expr;
672         }
673         else {
674                 snprintf(buf, buflen, "<unknown>");
675                 return buf;
676         }
678         if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL))
679                 snprintf(value_str, sizeof(value_str), "ERR");
680         else if (sdb_data_format(&value, value_str, sizeof(value_str),
681                                 SDB_SINGLE_QUOTED) < 0)
682                 snprintf(value_str, sizeof(value_str), "ERR");
683         snprintf(buf, buflen, "%s[%s]{ %s %s }", type, id,
684                         MATCHER_SYM(m->type), value_str);
685         sdb_data_free_datum(&value);
686         return buf;
687 } /* cond_tostring */
689 static int
690 op_matcher_init(sdb_object_t *obj, va_list ap)
692         M(obj)->type = va_arg(ap, int);
693         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
694                 return -1;
696         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
697         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
698         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
699         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
701         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
702                 return -1;
703         return 0;
704 } /* op_matcher_init */
706 static void
707 op_matcher_destroy(sdb_object_t *obj)
709         if (OP_M(obj)->left)
710                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
711         if (OP_M(obj)->right)
712                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
713 } /* op_matcher_destroy */
715 static char *
716 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
718         char left[buflen + 1], right[buflen + 1];
720         if (! m) {
721                 /* this should not happen */
722                 snprintf(buf, buflen, "()");
723                 return buf;
724         }
726         assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
727         snprintf(buf, buflen, "(%s, %s, %s)",
728                         m->type == MATCHER_OR ? "OR" : "AND",
729                         sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
730                         sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
731         return buf;
732 } /* op_tostring */
734 static int
735 cmp_matcher_init(sdb_object_t *obj, va_list ap)
737         M(obj)->type = va_arg(ap, int);
739         CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
740         sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
741         CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
742         sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
744         if ((! CMP_M(obj)->left) || (! CMP_M(obj)->right))
745                 return -1;
746         return 0;
747 } /* cmp_matcher_init */
749 static void
750 cmp_matcher_destroy(sdb_object_t *obj)
752         sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
753         sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
754 } /* cmp_matcher_destroy */
756 static char *
757 cmp_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
759         if (! m) {
760                 /* this should not happen */
761                 snprintf(buf, buflen, "()");
762                 return buf;
763         }
765         /* TODO */
766         snprintf(buf, buflen, "CMP_MATCHER(%d)", m->type);
767         return buf;
768 } /* cmp_tostring */
770 static int
771 uop_matcher_init(sdb_object_t *obj, va_list ap)
773         M(obj)->type = va_arg(ap, int);
774         if (M(obj)->type != MATCHER_NOT)
775                 return -1;
777         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
778         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
780         if (! UOP_M(obj)->op)
781                 return -1;
782         return 0;
783 } /* uop_matcher_init */
785 static void
786 uop_matcher_destroy(sdb_object_t *obj)
788         if (UOP_M(obj)->op)
789                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
790 } /* uop_matcher_destroy */
792 static char *
793 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
795         char op[buflen + 1];
797         if (! m) {
798                 /* this should not happen */
799                 snprintf(buf, buflen, "()");
800                 return buf;
801         }
803         assert(m->type == MATCHER_NOT);
804         snprintf(buf, buflen, "(NOT, %s)",
805                         sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
806         return buf;
807 } /* uop_tostring */
809 static int
810 isnull_matcher_init(sdb_object_t *obj, va_list ap)
812         const char *name;
814         M(obj)->type = va_arg(ap, int);
815         if (M(obj)->type != MATCHER_ISNULL)
816                 return -1;
818         name = va_arg(ap, const char *);
819         if (! name)
820                 return -1;
821         ISNULL_M(obj)->attr_name = strdup(name);
822         if (! ISNULL_M(obj)->attr_name)
823                 return -1;
824         return 0;
825 } /* isnull_matcher_init */
827 static void
828 isnull_matcher_destroy(sdb_object_t *obj)
830         if (ISNULL_M(obj)->attr_name)
831                 free(ISNULL_M(obj)->attr_name);
832         ISNULL_M(obj)->attr_name = NULL;
833 } /* isnull_matcher_destroy */
835 static char *
836 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
838         snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
839         return buf;
840 } /* isnull_tostring */
842 static sdb_type_t name_type = {
843         /* size = */ sizeof(name_matcher_t),
844         /* init = */ name_matcher_init,
845         /* destroy = */ name_matcher_destroy,
846 };
848 static sdb_type_t attr_type = {
849         /* size = */ sizeof(attr_matcher_t),
850         /* init = */ attr_matcher_init,
851         /* destroy = */ attr_matcher_destroy,
852 };
854 static sdb_type_t cond_type = {
855         /* size = */ sizeof(cond_matcher_t),
856         /* init = */ cond_matcher_init,
857         /* destroy = */ cond_matcher_destroy,
858 };
860 static sdb_type_t op_type = {
861         /* size = */ sizeof(op_matcher_t),
862         /* init = */ op_matcher_init,
863         /* destroy = */ op_matcher_destroy,
864 };
866 static sdb_type_t uop_type = {
867         /* size = */ sizeof(uop_matcher_t),
868         /* init = */ uop_matcher_init,
869         /* destroy = */ uop_matcher_destroy,
870 };
872 static sdb_type_t cmp_type = {
873         /* size = */ sizeof(cmp_matcher_t),
874         /* init = */ cmp_matcher_init,
875         /* destroy = */ cmp_matcher_destroy,
876 };
878 static sdb_type_t isnull_type = {
879         /* size = */ sizeof(isnull_matcher_t),
880         /* init = */ isnull_matcher_init,
881         /* destroy = */ isnull_matcher_destroy,
882 };
884 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
886 /* this array needs to be indexable by the matcher types;
887  * -> update the enum in store-private.h when updating this */
888 static matcher_tostring_cb
889 matchers_tostring[] = {
890         op_tostring,
891         op_tostring,
892         uop_tostring,
893         name_tostring,
894         attr_tostring,
895         cond_tostring,
896         cond_tostring,
897         cond_tostring,
898         cond_tostring,
899         cond_tostring,
900         cmp_tostring,
901         cmp_tostring,
902         cmp_tostring,
903         cmp_tostring,
904         cmp_tostring,
905         isnull_tostring,
906 };
908 /*
909  * public API
910  */
912 sdb_store_cond_t *
913 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
915         return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
916                                 name, expr));
917 } /* sdb_store_attr_cond */
919 sdb_store_cond_t *
920 sdb_store_obj_cond(int field, sdb_store_expr_t *expr)
922         return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type,
923                                 field, expr));
924 } /* sdb_store_obj_cond */
926 sdb_store_matcher_t *
927 sdb_store_name_matcher(int type, const char *name, _Bool re)
929         sdb_store_matcher_t *m;
931         if (re)
932                 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
933         else
934                 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
936         if (! m)
937                 return NULL;
939         NAME_M(m)->obj_type = type;
940         return m;
941 } /* sdb_store_name_matcher */
943 sdb_store_matcher_t *
944 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
946         sdb_store_matcher_t *m;
948         if (! name)
949                 return NULL;
951         if (re)
952                 m = M(sdb_object_create("attr-matcher", attr_type,
953                                         name, NULL, value));
954         else
955                 m = M(sdb_object_create("attr-matcher", attr_type,
956                                         name, value, NULL));
957         return m;
958 } /* sdb_store_attr_matcher */
960 sdb_store_matcher_t *
961 sdb_store_lt_matcher(sdb_store_cond_t *cond)
963         return M(sdb_object_create("lt-matcher", cond_type,
964                                 MATCHER_LT, cond));
965 } /* sdb_store_lt_matcher */
967 sdb_store_matcher_t *
968 sdb_store_le_matcher(sdb_store_cond_t *cond)
970         return M(sdb_object_create("le-matcher", cond_type,
971                                 MATCHER_LE, cond));
972 } /* sdb_store_le_matcher */
974 sdb_store_matcher_t *
975 sdb_store_eq_matcher(sdb_store_cond_t *cond)
977         return M(sdb_object_create("eq-matcher", cond_type,
978                                 MATCHER_EQ, cond));
979 } /* sdb_store_eq_matcher */
981 sdb_store_matcher_t *
982 sdb_store_ge_matcher(sdb_store_cond_t *cond)
984         return M(sdb_object_create("ge-matcher", cond_type,
985                                 MATCHER_GE, cond));
986 } /* sdb_store_ge_matcher */
988 sdb_store_matcher_t *
989 sdb_store_gt_matcher(sdb_store_cond_t *cond)
991         return M(sdb_object_create("gt-matcher", cond_type,
992                                 MATCHER_GT, cond));
993 } /* sdb_store_gt_matcher */
995 /*
996  * TODO: Rename sdb_store_cmp_* to sdb_store_* once the old code is unused and
997  * has been removed.
998  */
1000 sdb_store_matcher_t *
1001 sdb_store_cmp_lt(sdb_store_expr_t *left, sdb_store_expr_t *right)
1003         return M(sdb_object_create("lt-matcher", cmp_type,
1004                                 MATCHER_CMP_LT, left, right));
1005 } /* sdb_store_cmp_lt */
1007 sdb_store_matcher_t *
1008 sdb_store_cmp_le(sdb_store_expr_t *left, sdb_store_expr_t *right)
1010         return M(sdb_object_create("le-matcher", cmp_type,
1011                                 MATCHER_CMP_LE, left, right));
1012 } /* sdb_store_cmp_le */
1014 sdb_store_matcher_t *
1015 sdb_store_cmp_eq(sdb_store_expr_t *left, sdb_store_expr_t *right)
1017         return M(sdb_object_create("eq-matcher", cmp_type,
1018                                 MATCHER_CMP_EQ, left, right));
1019 } /* sdb_store_cmp_eq */
1021 sdb_store_matcher_t *
1022 sdb_store_cmp_ge(sdb_store_expr_t *left, sdb_store_expr_t *right)
1024         return M(sdb_object_create("ge-matcher", cmp_type,
1025                                 MATCHER_CMP_GE, left, right));
1026 } /* sdb_store_cmp_ge */
1028 sdb_store_matcher_t *
1029 sdb_store_cmp_gt(sdb_store_expr_t *left, sdb_store_expr_t *right)
1031         return M(sdb_object_create("gt-matcher", cmp_type,
1032                                 MATCHER_CMP_GT, left, right));
1033 } /* sdb_store_cmp_gt */
1035 sdb_store_matcher_t *
1036 sdb_store_isnull_matcher(const char *attr_name)
1038         return M(sdb_object_create("isnull-matcher", isnull_type,
1039                                 MATCHER_ISNULL, attr_name));
1040 } /* sdb_store_isnull_matcher */
1042 int
1043 sdb_store_parse_object_type_plural(const char *name)
1045         if (! strcasecmp(name, "hosts"))
1046                 return SDB_HOST;
1047         else if (! strcasecmp(name, "services"))
1048                 return SDB_SERVICE;
1049         else if (! strcasecmp(name, "metrics"))
1050                 return SDB_METRIC;
1051         return -1;
1052 } /* sdb_store_parse_object_type_plural */
1054 int
1055 sdb_store_parse_field_name(const char *name)
1057         if (! strcasecmp(name, "name"))
1058                 return SDB_FIELD_NAME;
1059         else if (! strcasecmp(name, "last_update"))
1060                 return SDB_FIELD_LAST_UPDATE;
1061         else if (! strcasecmp(name, "age"))
1062                 return SDB_FIELD_AGE;
1063         else if (! strcasecmp(name, "interval"))
1064                 return SDB_FIELD_INTERVAL;
1065         else if (! strcasecmp(name, "backend"))
1066                 return SDB_FIELD_BACKEND;
1067         return -1;
1068 } /* sdb_store_parse_field_name */
1070 static sdb_store_matcher_t *
1071 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
1073         sdb_store_matcher_t *tmp;
1075         if ((! m) || (! inv))
1076                 return m;
1078         tmp = sdb_store_inv_matcher(m);
1079         /* pass ownership to the inverse matcher */
1080         sdb_object_deref(SDB_OBJ(m));
1081         return tmp;
1082 } /* maybe_inv_matcher */
1084 static int
1085 parse_cond_op(const char *op,
1086                 sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv)
1088         *inv = 0;
1089         if (! strcasecmp(op, "<"))
1090                 *matcher = sdb_store_lt_matcher;
1091         else if (! strcasecmp(op, "<="))
1092                 *matcher = sdb_store_le_matcher;
1093         else if (! strcasecmp(op, "="))
1094                 *matcher = sdb_store_eq_matcher;
1095         else if (! strcasecmp(op, ">="))
1096                 *matcher = sdb_store_ge_matcher;
1097         else if (! strcasecmp(op, ">"))
1098                 *matcher = sdb_store_gt_matcher;
1099         else if (! strcasecmp(op, "!=")) {
1100                 *matcher = sdb_store_eq_matcher;
1101                 *inv = 1;
1102         }
1103         else
1104                 return -1;
1105         return 0;
1106 } /* parse_cond_op */
1108 static sdb_store_matcher_t *
1109 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
1111         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1112         sdb_store_matcher_t *m;
1113         sdb_store_cond_t *cond;
1114         _Bool inv = 0;
1116         if (! attr)
1117                 return NULL;
1119         if (! strcasecmp(op, "IS")) {
1120                 if (! expr)
1121                         return sdb_store_isnull_matcher(attr);
1122                 else
1123                         return NULL;
1124         }
1125         else if (! expr)
1126                 return NULL;
1127         else if (parse_cond_op(op, &matcher, &inv))
1128                 return NULL;
1130         cond = sdb_store_attr_cond(attr, expr);
1131         if (! cond)
1132                 return NULL;
1134         m = matcher(cond);
1135         /* pass ownership to 'm' or destroy in case of an error */
1136         sdb_object_deref(SDB_OBJ(cond));
1137         return maybe_inv_matcher(m, inv);
1138 } /* parse_attr_cmp */
1140 sdb_store_matcher_t *
1141 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
1142                 const char *op, sdb_store_expr_t *expr)
1144         int type = -1;
1145         _Bool inv = 0;
1146         _Bool re = 0;
1148         sdb_data_t value = SDB_DATA_INIT;
1149         sdb_store_matcher_t *m = NULL;
1151         if (! strcasecmp(obj_type, "host"))
1152                 type = SDB_HOST;
1153         else if (! strcasecmp(obj_type, "service"))
1154                 type = SDB_SERVICE;
1155         else if (! strcasecmp(obj_type, "metric"))
1156                 type = SDB_METRIC;
1157         else if (! strcasecmp(obj_type, "attribute"))
1158                 type = SDB_ATTRIBUTE;
1159         else
1160                 return NULL;
1162         /* XXX: this code sucks! */
1163         if (! strcasecmp(op, "=")) {
1164                 /* nothing to do */
1165         }
1166         else if (! strcasecmp(op, "!=")) {
1167                 inv = 1;
1168         }
1169         else if (! strcasecmp(op, "=~")) {
1170                 re = 1;
1171         }
1172         else if (! strcasecmp(op, "!~")) {
1173                 inv = 1;
1174                 re = 1;
1175         }
1176         else if (type == SDB_ATTRIBUTE)
1177                 return parse_attr_cmp(attr, op, expr);
1178         else
1179                 return NULL;
1181         if (! expr)
1182                 return NULL;
1184         if (sdb_store_expr_eval(expr, /* obj */ NULL, &value, /* filter */ NULL)
1185                         || (value.type != SDB_TYPE_STRING)) {
1186                 sdb_data_free_datum(&value);
1187                 if (type != SDB_ATTRIBUTE)
1188                         return NULL;
1189                 return parse_attr_cmp(attr, op, expr);
1190         }
1192         if (! attr)
1193                 m = sdb_store_name_matcher(type, value.data.string, re);
1194         else if (type == SDB_ATTRIBUTE)
1195                 m = sdb_store_attr_matcher(attr, value.data.string, re);
1197         sdb_data_free_datum(&value);
1198         return maybe_inv_matcher(m, inv);
1199 } /* sdb_store_matcher_parse_cmp */
1201 sdb_store_matcher_t *
1202 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
1203                 sdb_store_expr_t *expr)
1205         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1206         sdb_store_matcher_t *m;
1207         sdb_store_cond_t *cond;
1208         _Bool inv = 0;
1210         int field;
1212         if (! expr)
1213                 return NULL;
1215         field = sdb_store_parse_field_name(name);
1216         if (field < 0)
1217                 return NULL;
1219         if (parse_cond_op(op, &matcher, &inv))
1220                 return NULL;
1221         cond = sdb_store_obj_cond(field, expr);
1222         if (! cond)
1223                 return NULL;
1225         assert(matcher);
1226         m = matcher(cond);
1227         /* pass ownership to 'm' or destroy in case of an error */
1228         sdb_object_deref(SDB_OBJ(cond));
1229         return maybe_inv_matcher(m, inv);
1230 } /* sdb_store_matcher_parse_field_cmp */
1232 sdb_store_matcher_t *
1233 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1235         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
1236                                 left, right));
1237 } /* sdb_store_dis_matcher */
1239 sdb_store_matcher_t *
1240 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1242         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
1243                                 left, right));
1244 } /* sdb_store_con_matcher */
1246 sdb_store_matcher_t *
1247 sdb_store_inv_matcher(sdb_store_matcher_t *m)
1249         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
1250 } /* sdb_store_inv_matcher */
1252 int
1253 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
1254                 sdb_store_matcher_t *filter)
1256         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
1257                 return 0;
1259         /* "NULL" always matches */
1260         if ((! m) || (! obj))
1261                 return 1;
1263         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1264                 return 0;
1266         return matchers[m->type](m, obj, filter);
1267 } /* sdb_store_matcher_matches */
1269 char *
1270 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1272         if (! m)
1273                 return NULL;
1275         if ((m->type < 0)
1276                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
1277                 return NULL;
1278         return matchers_tostring[m->type](m, buf, buflen);
1279 } /* sdb_store_matcher_tostring */
1281 int
1282 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1283                 sdb_store_lookup_cb cb, void *user_data)
1285         scan_iter_data_t data = { m, filter, cb, user_data };
1287         if (! cb)
1288                 return -1;
1289         return sdb_store_iterate(scan_iter, &data);
1290 } /* sdb_store_scan */
1292 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */