Code

store_json: Base the memstore emitter on the store-writer API.
[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"
41 #include "utils/error.h"
43 #include <assert.h>
45 #include <sys/types.h>
46 #include <regex.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include <limits.h>
53 static int
54 expr_eval2(sdb_store_expr_t *e1, sdb_data_t *v1,
55                 sdb_store_expr_t *e2, sdb_data_t *v2,
56                 sdb_store_obj_t *obj, sdb_store_matcher_t *filter)
57 {
58         if (e1->type) {
59                 if (sdb_store_expr_eval(e1, obj, v1, filter))
60                         return -1;
61         }
62         else
63                 *v1 = e1->data;
64         if (e2->type) {
65                 if (sdb_store_expr_eval(e2, obj, v2, filter)) {
66                         if (e1->type)
67                                 sdb_data_free_datum(v1);
68                         return -1;
69                 }
70         }
71         else
72                 *v2 = e2->data;
73         return 0;
74 } /* expr_eval2 */
76 static void
77 expr_free_datum2(sdb_store_expr_t *e1, sdb_data_t *v1,
78                 sdb_store_expr_t *e2, sdb_data_t *v2)
79 {
80         if (e1->type)
81                 sdb_data_free_datum(v1);
82         if (e2->type)
83                 sdb_data_free_datum(v2);
84 } /* expr_free_datum2 */
86 /*
87  * matcher implementations
88  */
90 /*
91  * cmp_expr:
92  * Compare two values using the specified matcher operator. If strcmp_fallback
93  * is enabled, compare the string values in case of a type mismatch.
94  */
95 static int
96 match_cmp_value(int op, sdb_data_t *v1, sdb_data_t *v2, bool strcmp_fallback)
97 {
98         int status;
100         if (sdb_data_isnull(v1) || (sdb_data_isnull(v2)))
101                 status = INT_MAX;
102         else if (v1->type == v2->type)
103                 status = sdb_data_cmp(v1, v2);
104         else if (! strcmp_fallback)
105                 status = INT_MAX;
106         else
107                 status = sdb_data_strcmp(v1, v2);
109         if (status == INT_MAX)
110                 return 0;
111         switch (op) {
112                 case MATCHER_LT: return status < 0;
113                 case MATCHER_LE: return status <= 0;
114                 case MATCHER_EQ: return status == 0;
115                 case MATCHER_NE: return status != 0;
116                 case MATCHER_GE: return status >= 0;
117                 case MATCHER_GT: return status > 0;
118         }
119         return 0;
120 } /* match_cmp_value */
122 static int
123 match_regex_value(int op, sdb_data_t *v, sdb_data_t *re)
125         char value[sdb_data_strlen(v) + 1];
126         int status = 0;
128         assert((op == MATCHER_REGEX)
129                         || (op == MATCHER_NREGEX));
131         if (sdb_data_isnull(v) || sdb_data_isnull(re))
132                 return 0;
134         if (re->type == SDB_TYPE_STRING) {
135                 sdb_data_t tmp = SDB_DATA_INIT;
137                 if (sdb_data_parse(re->data.string, SDB_TYPE_REGEX, &tmp))
138                         return 0;
140                 sdb_data_free_datum(re);
141                 *re = tmp;
142         }
143         else if (re->type != SDB_TYPE_REGEX)
144                 return 0;
146         if (! sdb_data_format(v, value, sizeof(value), SDB_UNQUOTED))
147                 status = 0;
148         else if (! regexec(&re->data.re.regex, value, 0, NULL, 0))
149                 status = 1;
151         if (op == MATCHER_NREGEX)
152                 return !status;
153         return status;
154 } /* match_regex_value */
156 static int
157 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
158                 sdb_store_matcher_t *filter)
160         int status;
162         assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
163         assert(OP_M(m)->left && OP_M(m)->right);
165         status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
167         /* lazy evaluation */
168         if ((! status) && (m->type == MATCHER_AND))
169                 return status;
170         else if (status && (m->type == MATCHER_OR))
171                 return status;
173         return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
174 } /* match_logical */
176 static int
177 match_uop(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
178                 sdb_store_matcher_t *filter)
180         assert(m->type == MATCHER_NOT);
181         assert(UOP_M(m)->op);
183         return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
184 } /* match_uop */
186 /* iterate: ANY/ALL <iter> <cmp> <value> */
187 static int
188 match_iter(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
189                 sdb_store_matcher_t *filter)
191         sdb_store_expr_iter_t *iter = NULL;
192         int status;
193         int all = (int)(m->type == MATCHER_ALL);
195         assert((m->type == MATCHER_ANY) || (m->type == MATCHER_ALL));
196         assert((! CMP_M(ITER_M(m)->m)->left) && CMP_M(ITER_M(m)->m)->right);
198         iter = sdb_store_expr_iter(ITER_M(m)->iter, obj, filter);
199         if (! iter) {
200                 sdb_log(SDB_LOG_WARNING, "store: Invalid iterator");
201                 return 0;
202         }
204         status = all;
205         while (sdb_store_expr_iter_has_next(iter)) {
206                 sdb_data_t v = sdb_store_expr_iter_get_next(iter);
207                 sdb_store_expr_t expr = CONST_EXPR(v);
208                 bool matches;
210                 CMP_M(ITER_M(m)->m)->left = &expr;
211                 matches = sdb_store_matcher_matches(ITER_M(m)->m, obj, filter);
212                 CMP_M(ITER_M(m)->m)->left = NULL;
213                 sdb_data_free_datum(&v);
215                 if (matches) {
216                         if (! all) {
217                                 status = 1;
218                                 break;
219                         }
220                 } else if (all) {
221                         status = 0;
222                         break;
223                 }
224         }
225         sdb_store_expr_iter_destroy(iter);
226         return status;
227 } /* match_iter */
229 static int
230 match_cmp(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
231                 sdb_store_matcher_t *filter)
233         sdb_store_expr_t *e1 = CMP_M(m)->left;
234         sdb_store_expr_t *e2 = CMP_M(m)->right;
235         sdb_data_t v1 = SDB_DATA_INIT, v2 = SDB_DATA_INIT;
236         int status;
238         assert((m->type == MATCHER_LT)
239                         || (m->type == MATCHER_LE)
240                         || (m->type == MATCHER_EQ)
241                         || (m->type == MATCHER_NE)
242                         || (m->type == MATCHER_GE)
243                         || (m->type == MATCHER_GT));
244         assert(e1 && e2);
246         if (expr_eval2(e1, &v1, e2, &v2, obj, filter))
247                 return 0;
249         status = match_cmp_value(m->type, &v1, &v2,
250                         (e1->data_type) < 0 || (e2->data_type < 0));
252         expr_free_datum2(e1, &v1, e2, &v2);
253         return status;
254 } /* match_cmp */
256 static int
257 match_in(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
258                 sdb_store_matcher_t *filter)
260         sdb_data_t value = SDB_DATA_INIT, array = SDB_DATA_INIT;
261         int status = 1;
263         assert(m->type == MATCHER_IN);
264         assert(CMP_M(m)->left && CMP_M(m)->right);
266         if (expr_eval2(CMP_M(m)->left, &value,
267                                 CMP_M(m)->right, &array, obj, filter))
268                 status = 0;
270         if (status)
271                 status = sdb_data_inarray(&value, &array);
273         expr_free_datum2(CMP_M(m)->left, &value, CMP_M(m)->right, &array);
274         return status;
275 } /* match_in */
277 static int
278 match_regex(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
279                 sdb_store_matcher_t *filter)
281         sdb_data_t regex = SDB_DATA_INIT, v = SDB_DATA_INIT;
282         int status = 0;
284         assert((m->type == MATCHER_REGEX)
285                         || (m->type == MATCHER_NREGEX));
286         assert(CMP_M(m)->left && CMP_M(m)->right);
288         if (expr_eval2(CMP_M(m)->left, &v, CMP_M(m)->right, &regex, obj, filter))
289                 return 0;
291         status = match_regex_value(m->type, &v, &regex);
293         expr_free_datum2(CMP_M(m)->left, &v, CMP_M(m)->right, &regex);
294         return status;
295 } /* match_regex */
297 static int
298 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
299                 sdb_store_matcher_t *filter)
301         sdb_data_t v = SDB_DATA_INIT;
302         int status;
304         assert((m->type == MATCHER_ISNULL)
305                         || (m->type == MATCHER_ISTRUE)
306                         || (m->type == MATCHER_ISFALSE));
308         if (UNARY_M(m)->expr->type) {
309                 /* TODO: this might hide real errors;
310                  * improve error reporting and propagation */
311                 if (sdb_store_expr_eval(UNARY_M(m)->expr, obj, &v, filter))
312                         return 1;
313         }
314         else
315                 v = UNARY_M(m)->expr->data;
317         if (m->type == MATCHER_ISNULL)
318                 status = sdb_data_isnull(&v) ? 1 : 0;
319         else { /* ISTRUE or ISFALSE */
320                 if ((v.type == SDB_TYPE_BOOLEAN)
321                                 && (v.data.boolean == (m->type == MATCHER_ISTRUE)))
322                         status = 1;
323                 else
324                         status = 0;
325         }
327         if (UNARY_M(m)->expr->type)
328                 sdb_data_free_datum(&v);
329         return status;
330 } /* match_unary */
332 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
333                 sdb_store_matcher_t *);
335 /* this array needs to be indexable by the matcher types;
336  * -> update the enum in store-private.h when updating this */
337 static matcher_cb
338 matchers[] = {
339         match_logical,
340         match_logical,
341         match_uop,
342         match_iter,
343         match_iter,
344         match_in,
346         /* unary operators */
347         match_unary,
348         match_unary,
349         match_unary,
351         /* ary operators */
352         match_cmp,
353         match_cmp,
354         match_cmp,
355         match_cmp,
356         match_cmp,
357         match_cmp,
358         match_regex,
359         match_regex,
361         NULL, /* QUERY */
362 };
364 /*
365  * private matcher types
366  */
368 static int
369 op_matcher_init(sdb_object_t *obj, va_list ap)
371         M(obj)->type = va_arg(ap, int);
372         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
373                 return -1;
375         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
376         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
377         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
378         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
380         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
381                 return -1;
382         return 0;
383 } /* op_matcher_init */
385 static void
386 op_matcher_destroy(sdb_object_t *obj)
388         if (OP_M(obj)->left)
389                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
390         if (OP_M(obj)->right)
391                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
392 } /* op_matcher_destroy */
394 static int
395 iter_matcher_init(sdb_object_t *obj, va_list ap)
397         M(obj)->type = va_arg(ap, int);
398         ITER_M(obj)->iter = va_arg(ap, sdb_store_expr_t *);
399         ITER_M(obj)->m = va_arg(ap, sdb_store_matcher_t *);
401         sdb_object_ref(SDB_OBJ(ITER_M(obj)->iter));
402         sdb_object_ref(SDB_OBJ(ITER_M(obj)->m));
404         if ((! ITER_M(obj)->iter) || (! ITER_M(obj)->m))
405                 return -1;
406         return 0;
407 } /* iter_matcher_init */
409 static void
410 iter_matcher_destroy(sdb_object_t *obj)
412         sdb_object_deref(SDB_OBJ(ITER_M(obj)->iter));
413         sdb_object_deref(SDB_OBJ(ITER_M(obj)->m));
414 } /* iter_matcher_destroy */
416 static int
417 cmp_matcher_init(sdb_object_t *obj, va_list ap)
419         M(obj)->type = va_arg(ap, int);
421         CMP_M(obj)->left = va_arg(ap, sdb_store_expr_t *);
422         sdb_object_ref(SDB_OBJ(CMP_M(obj)->left));
423         CMP_M(obj)->right = va_arg(ap, sdb_store_expr_t *);
424         sdb_object_ref(SDB_OBJ(CMP_M(obj)->right));
426         if (! CMP_M(obj)->right)
427                 return -1;
428         return 0;
429 } /* cmp_matcher_init */
431 static void
432 cmp_matcher_destroy(sdb_object_t *obj)
434         sdb_object_deref(SDB_OBJ(CMP_M(obj)->left));
435         sdb_object_deref(SDB_OBJ(CMP_M(obj)->right));
436 } /* cmp_matcher_destroy */
438 static int
439 uop_matcher_init(sdb_object_t *obj, va_list ap)
441         M(obj)->type = va_arg(ap, int);
442         if (M(obj)->type != MATCHER_NOT)
443                 return -1;
445         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
446         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
448         if (! UOP_M(obj)->op)
449                 return -1;
450         return 0;
451 } /* uop_matcher_init */
453 static void
454 uop_matcher_destroy(sdb_object_t *obj)
456         if (UOP_M(obj)->op)
457                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
458 } /* uop_matcher_destroy */
460 static int
461 unary_matcher_init(sdb_object_t *obj, va_list ap)
463         M(obj)->type = va_arg(ap, int);
464         if ((M(obj)->type != MATCHER_ISNULL)
465                         && (M(obj)->type != MATCHER_ISTRUE)
466                         && (M(obj)->type != MATCHER_ISFALSE))
467                 return -1;
469         UNARY_M(obj)->expr = va_arg(ap, sdb_store_expr_t *);
470         sdb_object_ref(SDB_OBJ(UNARY_M(obj)->expr));
471         return 0;
472 } /* unary_matcher_init */
474 static void
475 unary_matcher_destroy(sdb_object_t *obj)
477         sdb_object_deref(SDB_OBJ(UNARY_M(obj)->expr));
478         UNARY_M(obj)->expr = NULL;
479 } /* unary_matcher_destroy */
481 static sdb_type_t op_type = {
482         /* size = */ sizeof(op_matcher_t),
483         /* init = */ op_matcher_init,
484         /* destroy = */ op_matcher_destroy,
485 };
487 static sdb_type_t uop_type = {
488         /* size = */ sizeof(uop_matcher_t),
489         /* init = */ uop_matcher_init,
490         /* destroy = */ uop_matcher_destroy,
491 };
493 static sdb_type_t iter_type = {
494         /* size = */ sizeof(iter_matcher_t),
495         /* init = */ iter_matcher_init,
496         /* destroy = */ iter_matcher_destroy,
497 };
499 static sdb_type_t cmp_type = {
500         /* size = */ sizeof(cmp_matcher_t),
501         /* init = */ cmp_matcher_init,
502         /* destroy = */ cmp_matcher_destroy,
503 };
505 static sdb_type_t unary_type = {
506         /* size = */ sizeof(unary_matcher_t),
507         /* init = */ unary_matcher_init,
508         /* destroy = */ unary_matcher_destroy,
509 };
511 /*
512  * public API
513  */
515 sdb_store_matcher_t *
516 sdb_store_any_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m)
518         if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) {
519                 sdb_log(SDB_LOG_ERR, "store: Invalid ANY -> %s matcher "
520                                 "(invalid operator)", MATCHER_SYM(m->type));
521                 return NULL;
522         }
523         if (CMP_M(m)->left) {
524                 sdb_log(SDB_LOG_ERR, "store: Invalid ANY %s %s %s matcher "
525                                 "(invalid left operand)",
526                                 SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type),
527                                 MATCHER_SYM(m->type),
528                                 SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type));
529                 return NULL;
530         }
531         return M(sdb_object_create("any-matcher", iter_type,
532                                 MATCHER_ANY, iter, m));
533 } /* sdb_store_any_matcher */
535 sdb_store_matcher_t *
536 sdb_store_all_matcher(sdb_store_expr_t *iter, sdb_store_matcher_t *m)
538         if ((m->type < MATCHER_LT) || (MATCHER_NREGEX < m->type)) {
539                 sdb_log(SDB_LOG_ERR, "store: Invalid ALL -> %s matcher "
540                                 "(invalid operator)", MATCHER_SYM(m->type));
541                 return NULL;
542         }
543         if (CMP_M(m)->left) {
544                 sdb_log(SDB_LOG_ERR, "store: Invalid ALL %s %s %s matcher "
545                                 "(invalid left operand)",
546                                 SDB_TYPE_TO_STRING(CMP_M(m)->left->data_type),
547                                 MATCHER_SYM(m->type),
548                                 SDB_TYPE_TO_STRING(CMP_M(m)->right->data_type));
549                 return NULL;
550         }
551         return M(sdb_object_create("all-matcher", iter_type,
552                                 MATCHER_ALL, iter, m));
553 } /* sdb_store_all_matcher */
555 sdb_store_matcher_t *
556 sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
558         return M(sdb_object_create("lt-matcher", cmp_type,
559                                 MATCHER_LT, left, right));
560 } /* sdb_store_lt_matcher */
562 sdb_store_matcher_t *
563 sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
565         return M(sdb_object_create("le-matcher", cmp_type,
566                                 MATCHER_LE, left, right));
567 } /* sdb_store_le_matcher */
569 sdb_store_matcher_t *
570 sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
572         return M(sdb_object_create("eq-matcher", cmp_type,
573                                 MATCHER_EQ, left, right));
574 } /* sdb_store_eq_matcher */
576 sdb_store_matcher_t *
577 sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
579         return M(sdb_object_create("ne-matcher", cmp_type,
580                                 MATCHER_NE, left, right));
581 } /* sdb_store_ne_matcher */
583 sdb_store_matcher_t *
584 sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
586         return M(sdb_object_create("ge-matcher", cmp_type,
587                                 MATCHER_GE, left, right));
588 } /* sdb_store_ge_matcher */
590 sdb_store_matcher_t *
591 sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
593         return M(sdb_object_create("gt-matcher", cmp_type,
594                                 MATCHER_GT, left, right));
595 } /* sdb_store_gt_matcher */
597 sdb_store_matcher_t *
598 sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
600         return M(sdb_object_create("in-matcher", cmp_type,
601                                 MATCHER_IN, left, right));
602 } /* sdb_store_in_matcher */
604 sdb_store_matcher_t *
605 sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
607         if (! right->type) {
608                 if ((right->data.type != SDB_TYPE_STRING)
609                                 && (right->data.type != SDB_TYPE_REGEX))
610                         return NULL;
612                 if (right->data.type == SDB_TYPE_STRING) {
613                         char *raw = right->data.data.string;
614                         if (sdb_data_parse(raw, SDB_TYPE_REGEX, &right->data))
615                                 return NULL;
616                         free(raw);
617                 }
618         }
619         return M(sdb_object_create("regex-matcher", cmp_type,
620                                 MATCHER_REGEX, left, right));
621 } /* sdb_store_regex_matcher */
623 sdb_store_matcher_t *
624 sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right)
626         sdb_store_matcher_t *m = sdb_store_regex_matcher(left, right);
627         if (! m)
628                 return NULL;
629         m->type = MATCHER_NREGEX;
630         return m;
631 } /* sdb_store_nregex_matcher */
633 sdb_store_matcher_t *
634 sdb_store_isnull_matcher(sdb_store_expr_t *expr)
636         return M(sdb_object_create("isnull-matcher", unary_type,
637                                 MATCHER_ISNULL, expr));
638 } /* sdb_store_isnull_matcher */
640 sdb_store_matcher_t *
641 sdb_store_istrue_matcher(sdb_store_expr_t *expr)
643         return M(sdb_object_create("istrue-matcher", unary_type,
644                                 MATCHER_ISTRUE, expr));
645 } /* sdb_store_istrue_matcher */
647 sdb_store_matcher_t *
648 sdb_store_isfalse_matcher(sdb_store_expr_t *expr)
650         return M(sdb_object_create("isfalse-matcher", unary_type,
651                                 MATCHER_ISFALSE, expr));
652 } /* sdb_store_isfalse_matcher */
654 sdb_store_matcher_t *
655 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
657         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
658                                 left, right));
659 } /* sdb_store_dis_matcher */
661 sdb_store_matcher_t *
662 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
664         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
665                                 left, right));
666 } /* sdb_store_con_matcher */
668 sdb_store_matcher_t *
669 sdb_store_inv_matcher(sdb_store_matcher_t *m)
671         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
672 } /* sdb_store_inv_matcher */
674 int
675 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
676                 sdb_store_matcher_t *filter)
678         if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
679                 return 0;
681         /* "NULL" always matches */
682         if ((! m) || (! obj))
683                 return 1;
685         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
686                 return 0;
688         if (! matchers[m->type])
689                 return 0;
690         return matchers[m->type](m, obj, filter);
691 } /* sdb_store_matcher_matches */
693 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */