Code

store: Introduced the concept of query filters.
[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_host_t *host, 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 (sdb_store_expr_eval(ATTR_C(cond)->expr, &value))
115                 return INT_MAX;
117         attr = attr_get(host, ATTR_C(cond)->name, filter);
118         if (! attr)
119                 status = INT_MAX;
120         else if (attr->value.type != value.type)
121                 status = INT_MAX;
122         else
123                 status = sdb_data_cmp(&attr->value, &value);
124         sdb_data_free_datum(&value);
125         return status;
126 } /* attr_cmp */
128 /*
129  * matcher implementations
130  */
132 static int
133 match_string(string_matcher_t *m, const char *name)
135         if ((! m->name) && (! m->name_re))
136                 return 1;
138         if (! name)
139                 name = "";
141         if (m->name && strcasecmp(m->name, name))
142                 return 0;
143         if (m->name_re && regexec(m->name_re, name,
144                                         /* matches */ 0, NULL, /* flags = */ 0))
145                 return 0;
146         return 1;
147 } /* match_string */
149 static int
150 match_logical(sdb_store_matcher_t *m, sdb_host_t *host,
151                 sdb_store_matcher_t *filter)
153         int status;
155         assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
156         assert(OP_M(m)->left && OP_M(m)->right);
158         status = sdb_store_matcher_matches(OP_M(m)->left, STORE_OBJ(host),
159                         filter);
161         /* lazy evaluation */
162         if ((! status) && (m->type == MATCHER_AND))
163                 return status;
164         else if (status && (m->type == MATCHER_OR))
165                 return status;
167         return sdb_store_matcher_matches(OP_M(m)->right, STORE_OBJ(host), filter);
168 } /* match_logical */
170 static int
171 match_unary(sdb_store_matcher_t *m, sdb_host_t *host,
172                 sdb_store_matcher_t *filter)
174         assert(m->type == MATCHER_NOT);
175         assert(UOP_M(m)->op);
177         return !sdb_store_matcher_matches(UOP_M(m)->op, STORE_OBJ(host), filter);
178 } /* match_unary */
180 static int
181 match_name(sdb_store_matcher_t *m, sdb_host_t *host,
182                 sdb_store_matcher_t *filter)
184         sdb_avltree_iter_t *iter = NULL;
185         int status = 0;
187         assert(m->type == MATCHER_NAME);
189         switch (NAME_M(m)->obj_type) {
190                 case SDB_HOST:
191                         return match_string(&NAME_M(m)->name, SDB_OBJ(host)->name);
192                         break;
193                 case SDB_SERVICE:
194                         iter = sdb_avltree_get_iter(host->services);
195                         break;
196                 case SDB_ATTRIBUTE:
197                         iter = sdb_avltree_get_iter(host->attributes);
198                         break;
199         }
201         while (sdb_avltree_iter_has_next(iter)) {
202                 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
203                 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
204                                                 NULL)))
205                         continue;
206                 if (match_string(&NAME_M(m)->name, child->name)) {
207                         status = 1;
208                         break;
209                 }
210         }
211         sdb_avltree_iter_destroy(iter);
212         return status;
213 } /* match_name */
215 static int
216 match_attr(sdb_store_matcher_t *m, sdb_host_t *host,
217                 sdb_store_matcher_t *filter)
219         sdb_attribute_t *attr;
221         assert(m->type == MATCHER_ATTR);
222         assert(ATTR_M(m)->name);
224         attr = attr_get(host, ATTR_M(m)->name, filter);
225         if (attr) {
226                 char buf[sdb_data_strlen(&attr->value) + 1];
227                 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
228                         return 0;
229                 if (match_string(&ATTR_M(m)->value, buf))
230                         return 1;
231         }
232         return 0;
233 } /* match_attr */
235 static int
236 match_lt(sdb_store_matcher_t *m, sdb_host_t *host,
237                 sdb_store_matcher_t *filter)
239         int status;
240         assert(m->type == MATCHER_LT);
241         status = COND_M(m)->cond->cmp(host, COND_M(m)->cond, filter);
242         return (status != INT_MAX) && (status < 0);
243 } /* match_lt */
245 static int
246 match_le(sdb_store_matcher_t *m, sdb_host_t *host,
247                 sdb_store_matcher_t *filter)
249         int status;
250         assert(m->type == MATCHER_LE);
251         status = COND_M(m)->cond->cmp(host, COND_M(m)->cond, filter);
252         return (status != INT_MAX) && (status <= 0);
253 } /* match_le */
255 static int
256 match_eq(sdb_store_matcher_t *m, sdb_host_t *host,
257                 sdb_store_matcher_t *filter)
259         int status;
260         assert(m->type == MATCHER_EQ);
261         status = COND_M(m)->cond->cmp(host, COND_M(m)->cond, filter);
262         return (status != INT_MAX) && (! status);
263 } /* match_eq */
265 static int
266 match_ge(sdb_store_matcher_t *m, sdb_host_t *host,
267                 sdb_store_matcher_t *filter)
269         int status;
270         assert(m->type == MATCHER_GE);
271         status = COND_M(m)->cond->cmp(host, COND_M(m)->cond, filter);
272         return (status != INT_MAX) && (status >= 0);
273 } /* match_ge */
275 static int
276 match_gt(sdb_store_matcher_t *m, sdb_host_t *host,
277                 sdb_store_matcher_t *filter)
279         int status;
280         assert(m->type == MATCHER_GT);
281         status = COND_M(m)->cond->cmp(host, COND_M(m)->cond, filter);
282         return (status != INT_MAX) && (status > 0);
283 } /* match_gt */
285 static int
286 match_isnull(sdb_store_matcher_t *m, sdb_host_t *host,
287                 sdb_store_matcher_t *filter)
289         assert(m->type == MATCHER_ISNULL);
290         return attr_get(host, ISNULL_M(m)->attr_name, filter) == NULL;
291 } /* match_isnull */
293 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_host_t *,
294                 sdb_store_matcher_t *);
296 /* this array needs to be indexable by the matcher types;
297  * -> update the enum in store-private.h when updating this */
298 static matcher_cb
299 matchers[] = {
300         match_logical,
301         match_logical,
302         match_unary,
303         match_name,
304         match_attr,
305         match_lt,
306         match_le,
307         match_eq,
308         match_ge,
309         match_gt,
310         match_isnull,
311 };
313 /*
314  * private conditional types
315  */
317 static int
318 attr_cond_init(sdb_object_t *obj, va_list ap)
320         const char *name = va_arg(ap, const char *);
321         sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
323         if (! name)
324                 return -1;
326         SDB_STORE_COND(obj)->cmp = attr_cmp;
328         ATTR_C(obj)->name = strdup(name);
329         if (! ATTR_C(obj)->name)
330                 return -1;
331         ATTR_C(obj)->expr = expr;
332         sdb_object_ref(SDB_OBJ(expr));
333         return 0;
334 } /* attr_cond_init */
336 static void
337 attr_cond_destroy(sdb_object_t *obj)
339         if (ATTR_C(obj)->name)
340                 free(ATTR_C(obj)->name);
341         sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
342 } /* attr_cond_destroy */
344 static sdb_type_t attr_cond_type = {
345         /* size = */ sizeof(attr_cond_t),
346         /* init = */ attr_cond_init,
347         /* destroy = */ attr_cond_destroy,
348 };
350 /*
351  * private matcher types
352  */
354 /* initializes a string matcher consuming two elements from ap */
355 static int
356 string_matcher_init(string_matcher_t *m, va_list ap)
358         const char *name = va_arg(ap, const char *);
359         const char *name_re = va_arg(ap, const char *);
361         if (name) {
362                 m->name = strdup(name);
363                 if (! m->name)
364                         return -1;
365         }
366         if (name_re) {
367                 m->name_re = malloc(sizeof(*m->name_re));
368                 if (! m->name_re)
369                         return -1;
370                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
371                         return -1;
372         }
373         return 0;
374 } /* string_matcher_init */
376 static void
377 string_matcher_destroy(string_matcher_t *m)
379         if (m->name)
380                 free(m->name);
381         if (m->name_re) {
382                 regfree(m->name_re);
383                 free(m->name_re);
384         }
385 } /* string_matcher_destroy */
387 static char *
388 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
390         snprintf(buf, buflen, "{ %s%s%s, %p }",
391                         m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
392                         m->name_re);
393         return buf;
394 } /* string_tostring */
396 /* initializes a name matcher */
397 static int
398 name_matcher_init(sdb_object_t *obj, va_list ap)
400         name_matcher_t *m = NAME_M(obj);
401         M(obj)->type = MATCHER_NAME;
402         return string_matcher_init(&m->name, ap);
403 } /* name_matcher_init */
405 static void
406 name_matcher_destroy(sdb_object_t *obj)
408         name_matcher_t *m = NAME_M(obj);
409         string_matcher_destroy(&m->name);
410 } /* name_matcher_destroy */
412 static char *
413 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
415         char name[buflen + 1];
416         assert(m->type == MATCHER_NAME);
417         snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
418                         SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
419                         string_tostring(&NAME_M(m)->name, name, sizeof(name)));
420         return buf;
421 } /* name_tostring */
423 static int
424 attr_matcher_init(sdb_object_t *obj, va_list ap)
426         attr_matcher_t *attr = ATTR_M(obj);
427         const char *name = va_arg(ap, const char *);
429         M(obj)->type = MATCHER_ATTR;
430         if (name) {
431                 attr->name = strdup(name);
432                 if (! attr->name)
433                         return -1;
434         }
435         return string_matcher_init(&attr->value, ap);
436 } /* attr_matcher_init */
438 static void
439 attr_matcher_destroy(sdb_object_t *obj)
441         attr_matcher_t *attr = ATTR_M(obj);
442         if (attr->name)
443                 free(attr->name);
444         attr->name = NULL;
445         string_matcher_destroy(&attr->value);
446 } /* attr_matcher_destroy */
448 static char *
449 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
451         char value[buflen + 1];
453         if (! m) {
454                 snprintf(buf, buflen, "ATTR{}");
455                 return buf;
456         }
458         assert(m->type == MATCHER_ATTR);
459         snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
460                         string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
461         return buf;
462 } /* attr_tostring */
464 static int
465 cond_matcher_init(sdb_object_t *obj, va_list ap)
467         int type = va_arg(ap, int);
468         sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
470         if (! cond)
471                 return -1;
473         sdb_object_ref(SDB_OBJ(cond));
475         M(obj)->type = type;
476         COND_M(obj)->cond = cond;
477         return 0;
478 } /* cond_matcher_init */
480 static void
481 cond_matcher_destroy(sdb_object_t *obj)
483         sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
484 } /* cond_matcher_destroy */
486 static char *
487 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
489         if (COND_M(m)->cond->cmp == attr_cmp) {
490                 sdb_data_t value = SDB_DATA_INIT;
491                 char value_str[buflen];
492                 if (sdb_store_expr_eval(ATTR_C(COND_M(m)->cond)->expr, &value))
493                         snprintf(value_str, sizeof(value_str), "ERR");
494                 else if (sdb_data_format(&value, value_str, sizeof(value_str),
495                                         SDB_SINGLE_QUOTED) < 0)
496                         snprintf(value_str, sizeof(value_str), "ERR");
497                 snprintf(buf, buflen, "ATTR[%s]{ %s %s }",
498                                 ATTR_C(COND_M(m)->cond)->name, MATCHER_SYM(m->type),
499                                 value_str);
500                 sdb_data_free_datum(&value);
501         }
502         return buf;
503 } /* cond_tostring */
505 static int
506 op_matcher_init(sdb_object_t *obj, va_list ap)
508         M(obj)->type = va_arg(ap, int);
509         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
510                 return -1;
512         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
513         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
514         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
515         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
517         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
518                 return -1;
519         return 0;
520 } /* op_matcher_init */
522 static void
523 op_matcher_destroy(sdb_object_t *obj)
525         if (OP_M(obj)->left)
526                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
527         if (OP_M(obj)->right)
528                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
529 } /* op_matcher_destroy */
531 static char *
532 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
534         char left[buflen + 1], right[buflen + 1];
536         if (! m) {
537                 /* this should not happen */
538                 snprintf(buf, buflen, "()");
539                 return buf;
540         }
542         assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
543         snprintf(buf, buflen, "(%s, %s, %s)",
544                         m->type == MATCHER_OR ? "OR" : "AND",
545                         sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
546                         sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
547         return buf;
548 } /* op_tostring */
550 static int
551 uop_matcher_init(sdb_object_t *obj, va_list ap)
553         M(obj)->type = va_arg(ap, int);
554         if (M(obj)->type != MATCHER_NOT)
555                 return -1;
557         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
558         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
560         if (! UOP_M(obj)->op)
561                 return -1;
562         return 0;
563 } /* uop_matcher_init */
565 static void
566 uop_matcher_destroy(sdb_object_t *obj)
568         if (UOP_M(obj)->op)
569                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
570 } /* uop_matcher_destroy */
572 static char *
573 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
575         char op[buflen + 1];
577         if (! m) {
578                 /* this should not happen */
579                 snprintf(buf, buflen, "()");
580                 return buf;
581         }
583         assert(m->type == MATCHER_NOT);
584         snprintf(buf, buflen, "(NOT, %s)",
585                         sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
586         return buf;
587 } /* uop_tostring */
589 static int
590 isnull_matcher_init(sdb_object_t *obj, va_list ap)
592         const char *name;
594         M(obj)->type = va_arg(ap, int);
595         if (M(obj)->type != MATCHER_ISNULL)
596                 return -1;
598         name = va_arg(ap, const char *);
599         if (! name)
600                 return -1;
601         ISNULL_M(obj)->attr_name = strdup(name);
602         if (! ISNULL_M(obj)->attr_name)
603                 return -1;
604         return 0;
605 } /* isnull_matcher_init */
607 static void
608 isnull_matcher_destroy(sdb_object_t *obj)
610         if (ISNULL_M(obj)->attr_name)
611                 free(ISNULL_M(obj)->attr_name);
612         ISNULL_M(obj)->attr_name = NULL;
613 } /* isnull_matcher_destroy */
615 static char *
616 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
618         snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
619         return buf;
620 } /* isnull_tostring */
622 static sdb_type_t name_type = {
623         /* size = */ sizeof(name_matcher_t),
624         /* init = */ name_matcher_init,
625         /* destroy = */ name_matcher_destroy,
626 };
628 static sdb_type_t attr_type = {
629         /* size = */ sizeof(attr_matcher_t),
630         /* init = */ attr_matcher_init,
631         /* destroy = */ attr_matcher_destroy,
632 };
634 static sdb_type_t cond_type = {
635         /* size = */ sizeof(cond_matcher_t),
636         /* init = */ cond_matcher_init,
637         /* destroy = */ cond_matcher_destroy,
638 };
640 static sdb_type_t op_type = {
641         /* size = */ sizeof(op_matcher_t),
642         /* init = */ op_matcher_init,
643         /* destroy = */ op_matcher_destroy,
644 };
646 static sdb_type_t uop_type = {
647         /* size = */ sizeof(uop_matcher_t),
648         /* init = */ uop_matcher_init,
649         /* destroy = */ uop_matcher_destroy,
650 };
652 static sdb_type_t isnull_type = {
653         /* size = */ sizeof(isnull_matcher_t),
654         /* init = */ isnull_matcher_init,
655         /* destroy = */ isnull_matcher_destroy,
656 };
658 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
660 /* this array needs to be indexable by the matcher types;
661  * -> update the enum in store-private.h when updating this */
662 static matcher_tostring_cb
663 matchers_tostring[] = {
664         op_tostring,
665         op_tostring,
666         uop_tostring,
667         name_tostring,
668         attr_tostring,
669         cond_tostring,
670         cond_tostring,
671         cond_tostring,
672         cond_tostring,
673         cond_tostring,
674         isnull_tostring,
675 };
677 /*
678  * public API
679  */
681 sdb_store_cond_t *
682 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
684         return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
685                                 name, expr));
686 } /* sdb_store_attr_cond */
688 sdb_store_matcher_t *
689 sdb_store_name_matcher(int type, const char *name, _Bool re)
691         sdb_store_matcher_t *m;
693         if (re)
694                 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
695         else
696                 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
698         if (! m)
699                 return NULL;
701         NAME_M(m)->obj_type = type;
702         return m;
703 } /* sdb_store_name_matcher */
705 sdb_store_matcher_t *
706 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
708         sdb_store_matcher_t *m;
710         if (! name)
711                 return NULL;
713         if (re)
714                 m = M(sdb_object_create("attr-matcher", attr_type,
715                                         name, NULL, value));
716         else
717                 m = M(sdb_object_create("attr-matcher", attr_type,
718                                         name, value, NULL));
719         return m;
720 } /* sdb_store_attr_matcher */
722 sdb_store_matcher_t *
723 sdb_store_lt_matcher(sdb_store_cond_t *cond)
725         return M(sdb_object_create("lt-matcher", cond_type,
726                                 MATCHER_LT, cond));
727 } /* sdb_store_lt_matcher */
729 sdb_store_matcher_t *
730 sdb_store_le_matcher(sdb_store_cond_t *cond)
732         return M(sdb_object_create("le-matcher", cond_type,
733                                 MATCHER_LE, cond));
734 } /* sdb_store_le_matcher */
736 sdb_store_matcher_t *
737 sdb_store_eq_matcher(sdb_store_cond_t *cond)
739         return M(sdb_object_create("eq-matcher", cond_type,
740                                 MATCHER_EQ, cond));
741 } /* sdb_store_eq_matcher */
743 sdb_store_matcher_t *
744 sdb_store_ge_matcher(sdb_store_cond_t *cond)
746         return M(sdb_object_create("ge-matcher", cond_type,
747                                 MATCHER_GE, cond));
748 } /* sdb_store_ge_matcher */
750 sdb_store_matcher_t *
751 sdb_store_gt_matcher(sdb_store_cond_t *cond)
753         return M(sdb_object_create("gt-matcher", cond_type,
754                                 MATCHER_GT, cond));
755 } /* sdb_store_gt_matcher */
757 sdb_store_matcher_t *
758 sdb_store_isnull_matcher(const char *attr_name)
760         return M(sdb_object_create("isnull-matcher", isnull_type,
761                                 MATCHER_ISNULL, attr_name));
762 } /* sdb_store_isnull_matcher */
764 static sdb_store_matcher_t *
765 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
767         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
768         sdb_store_matcher_t *m;
769         sdb_store_cond_t *cond;
770         _Bool inv = 0;
772         if (! attr)
773                 return NULL;
775         if (! strcasecmp(op, "IS")) {
776                 if (! expr)
777                         return sdb_store_isnull_matcher(attr);
778                 else
779                         return NULL;
780         }
781         else if (! expr)
782                 return NULL;
783         else if (! strcasecmp(op, "<"))
784                 matcher = sdb_store_lt_matcher;
785         else if (! strcasecmp(op, "<="))
786                 matcher = sdb_store_le_matcher;
787         else if (! strcasecmp(op, "="))
788                 matcher = sdb_store_eq_matcher;
789         else if (! strcasecmp(op, ">="))
790                 matcher = sdb_store_ge_matcher;
791         else if (! strcasecmp(op, ">"))
792                 matcher = sdb_store_gt_matcher;
793         else if (! strcasecmp(op, "!=")) {
794                 matcher = sdb_store_eq_matcher;
795                 inv = 1;
796         }
797         else
798                 return NULL;
800         cond = sdb_store_attr_cond(attr, expr);
801         if (! cond)
802                 return NULL;
804         m = matcher(cond);
805         /* pass ownership to 'm' or destroy in case of an error */
806         sdb_object_deref(SDB_OBJ(cond));
807         if (! m)
808                 return NULL;
810         if (inv) {
811                 sdb_store_matcher_t *tmp;
812                 tmp = sdb_store_inv_matcher(m);
813                 /* pass ownership to the inverse matcher */
814                 sdb_object_deref(SDB_OBJ(m));
815                 m = tmp;
816         }
817         return m;
818 } /* parse_attr_cmp */
820 sdb_store_matcher_t *
821 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
822                 const char *op, sdb_store_expr_t *expr)
824         int type = -1;
825         _Bool inv = 0;
826         _Bool re = 0;
828         sdb_data_t value = SDB_DATA_INIT;
829         sdb_store_matcher_t *m = NULL;
831         if (! strcasecmp(obj_type, "host"))
832                 type = SDB_HOST;
833         else if (! strcasecmp(obj_type, "service"))
834                 type = SDB_SERVICE;
835         else if (! strcasecmp(obj_type, "attribute"))
836                 type = SDB_ATTRIBUTE;
837         else
838                 return NULL;
840         /* XXX: this code sucks! */
841         if (! strcasecmp(op, "=")) {
842                 /* nothing to do */
843         }
844         else if (! strcasecmp(op, "!=")) {
845                 inv = 1;
846         }
847         else if (! strcasecmp(op, "=~")) {
848                 re = 1;
849         }
850         else if (! strcasecmp(op, "!~")) {
851                 inv = 1;
852                 re = 1;
853         }
854         else if (type == SDB_ATTRIBUTE)
855                 return parse_attr_cmp(attr, op, expr);
856         else
857                 return NULL;
859         if (! expr)
860                 return NULL;
862         if (sdb_store_expr_eval(expr, &value))
863                 return NULL;
864         if (value.type != SDB_TYPE_STRING) {
865                 sdb_data_free_datum(&value);
866                 return parse_attr_cmp(attr, op, expr);
867         }
869         if (! attr)
870                 m = sdb_store_name_matcher(type, value.data.string, re);
871         else if (type == SDB_ATTRIBUTE)
872                 m = sdb_store_attr_matcher(attr, value.data.string, re);
874         sdb_data_free_datum(&value);
876         if (! m)
877                 return NULL;
879         if (inv) {
880                 sdb_store_matcher_t *tmp;
881                 tmp = sdb_store_inv_matcher(m);
882                 /* pass ownership to the inverse matcher */
883                 sdb_object_deref(SDB_OBJ(m));
884                 m = tmp;
885         }
886         return m;
887 } /* sdb_store_matcher_parse_cmp */
889 sdb_store_matcher_t *
890 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
892         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
893                                 left, right));
894 } /* sdb_store_dis_matcher */
896 sdb_store_matcher_t *
897 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
899         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
900                                 left, right));
901 } /* sdb_store_con_matcher */
903 sdb_store_matcher_t *
904 sdb_store_inv_matcher(sdb_store_matcher_t *m)
906         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
907 } /* sdb_store_inv_matcher */
909 int
910 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
911                 sdb_store_matcher_t *filter)
913         if (obj->type != SDB_HOST)
914                 return 0;
916         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
917                 return 0;
919         /* "NULL" always matches */
920         if ((! m) || (! obj))
921                 return 1;
923         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
924                 return 0;
926         return matchers[m->type](m, HOST(obj), filter);
927 } /* sdb_store_matcher_matches */
929 char *
930 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
932         if (! m)
933                 return NULL;
935         if ((m->type < 0)
936                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
937                 return NULL;
938         return matchers_tostring[m->type](m, buf, buflen);
939 } /* sdb_store_matcher_tostring */
941 int
942 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
943                 sdb_store_lookup_cb cb, void *user_data)
945         scan_iter_data_t data = { m, filter, cb, user_data };
947         if (! cb)
948                 return -1;
949         return sdb_store_iterate(scan_iter, &data);
950 } /* sdb_store_scan */
952 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */