Code

store: Let sdb_store_matcher_matches return true if it matches.
authorSebastian Harl <sh@tokkee.org>
Tue, 1 Apr 2014 20:09:26 +0000 (22:09 +0200)
committerSebastian Harl <sh@tokkee.org>
Tue, 1 Apr 2014 20:09:26 +0000 (22:09 +0200)
Rather than using 0 for matches and -1 else.

src/core/store_lookup.c
src/include/core/store.h
t/core/store_lookup_test.c

index 35c53e5b0b84c9818ac11a7a4004483a4ff27ac3..b0b7bf8f9345644e037264a2abd50b6d14b697f8 100644 (file)
@@ -126,7 +126,7 @@ lookup_iter(sdb_store_base_t *obj, void *user_data)
 {
        lookup_iter_data_t *d = user_data;
 
-       if (sdb_store_matcher_matches(d->m, obj))
+       if (sdb_store_matcher_matches(d->m, obj))
                return d->cb(obj, d->user_data);
        return 0;
 } /* lookup_iter */
@@ -148,17 +148,17 @@ match_name(name_matcher_t *m, const char *name)
        assert(m);
 
        if ((! m->name) && (! m->name_re))
-               return 0;
+               return 1;
 
        if (! name)
                name = "";
 
        if (m->name && strcasecmp(m->name, name))
-               return -1;
+               return 0;
        if (m->name_re && regexec(m->name_re, name,
                                        /* matches */ 0, NULL, /* flags = */ 0))
-               return -1;
-       return 0;
+               return 0;
+       return 1;
 } /* match_name */
 
 /* match attribute specific values;
@@ -169,14 +169,14 @@ match_attr(attr_matcher_t *m, sdb_store_base_t *obj)
        assert(m && obj);
 
        if (obj->type != SDB_ATTRIBUTE)
-               return -1;
+               return 0;
 
        {
                sdb_attribute_t *attr = SDB_ATTR(obj);
                char buf[sdb_data_strlen(&attr->value) + 1];
 
                if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
-                       return -1;
+                       return 0;
                return match_name(&m->value, buf);
        }
 } /* match_attr */
@@ -191,23 +191,23 @@ match_service(service_matcher_t *m, sdb_store_base_t *obj)
        assert(m && obj);
 
        if (obj->type != SDB_SERVICE)
-               return -1;
+               return 0;
 
        if (! m->attr)
-               return 0;
+               return 1;
 
        iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
        while (sdb_llist_iter_has_next(iter)) {
                sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter));
 
                /* if any of the attributes matches we found a matching service */
-               if (match_obj(M(m->attr), attr)) {
+               if (match_obj(M(m->attr), attr)) {
                        sdb_llist_iter_destroy(iter);
-                       return 0;
+                       return 1;
                }
        }
        sdb_llist_iter_destroy(iter);
-       return -1;
+       return 0;
 } /* match_service */
 
 /* match host specific values;
@@ -221,44 +221,44 @@ match_host(host_matcher_t *m, sdb_store_base_t *obj)
        assert(m && obj);
 
        if (obj->type != SDB_HOST)
-               return -1;
+               return 0;
 
        if (m->service) {
                iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
-               status = -1;
+               status = 0;
        }
        else {
                iter = NULL;
-               status = 0;
+               status = 1;
        }
        while (sdb_llist_iter_has_next(iter)) {
                sdb_store_base_t *service = STORE_BASE(sdb_llist_iter_get_next(iter));
 
                /* found a matching service */
-               if (match_obj(M(m->service), service)) {
-                       status = 0;
+               if (match_obj(M(m->service), service)) {
+                       status = 1;
                        break;
                }
        }
        sdb_llist_iter_destroy(iter);
 
-       if (status)
+       if (status)
                return status;
        else if (! m->attr)
-               return 0;
+               return 1;
 
        iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
        while (sdb_llist_iter_has_next(iter)) {
                sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter));
 
                /* if any attribute matches, we found a matching host */
-               if (match_obj(M(m->attr), attr)) {
+               if (match_obj(M(m->attr), attr)) {
                        sdb_llist_iter_destroy(iter);
-                       return 0;
+                       return 1;
                }
        }
        sdb_llist_iter_destroy(iter);
-       return -1;
+       return 0;
 } /* match_host */
 
 /* generic matchers */
@@ -292,9 +292,9 @@ match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
 
        status = sdb_store_matcher_matches(OP_M(m)->left, obj);
        /* lazy evaluation */
-       if (status && (m->type == MATCHER_AND))
+       if ((! status) && (m->type == MATCHER_AND))
                return status;
-       else if ((! status) && (m->type == MATCHER_OR))
+       else if (status && (m->type == MATCHER_OR))
                return status;
 
        return sdb_store_matcher_matches(OP_M(m)->right, obj);
@@ -308,7 +308,7 @@ match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj)
        assert(m && obj);
 
        status = match_name(&OBJ_M(m)->name, obj->super.name);
-       if (status)
+       if (status)
                return status;
 
        switch (m->type) {
@@ -322,7 +322,7 @@ match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj)
                        return match_host(HOST_M(m), obj);
                        break;
        }
-       return -1;
+       return 0;
 } /* match_obj */
 
 /*
@@ -555,10 +555,10 @@ sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
 {
        /* "NULL" always matches */
        if ((! m) || (! obj))
-               return 0;
+               return 1;
 
        if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
-               return -1;
+               return 0;
 
        return matchers[m->type](m, obj);
 } /* sdb_store_matcher_matches */
index 25bd91a9393daee17638ad3063c5a0259229df1b..e09483ff7ed283f0ec65c8ac89823d6323c3cc1b 100644 (file)
@@ -188,8 +188,8 @@ sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
  * Check whether the specified matcher matches the specified store object.
  *
  * Returns:
- *  - 0 if the object matches
- *  - a negative value else
+ *  - 1 if the object matches
+ *  - 0 else
  */
 int
 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj);
index 465bb08ca079004604ddc08d694d0192d509043c..f95598e27dcf222be6ef0dfe42f80e3e9f137d7c 100644 (file)
@@ -101,107 +101,107 @@ START_TEST(test_store_match)
                {
                        /* host */ NULL, NULL,
                        /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, NULL, NULL, 1
                },
                {
                        /* host */ "a", NULL,
                        /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, NULL, NULL, 1
                },
                {
                        /* host */ "b", NULL,
                        /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, -1
+                       /* attr */ NULL, NULL, NULL, NULL, 0
                },
                {
                        /* host */ NULL, "^a$",
                        /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, NULL, NULL, 1
                },
                {
                        /* host */ NULL, "^b$",
                        /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, -1
+                       /* attr */ NULL, NULL, NULL, NULL, 0
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, NULL, NULL, 1
                },
                {
                        /* host */ "a", "^b$",
                        /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, -1
+                       /* attr */ NULL, NULL, NULL, NULL, 0
                },
                {
                        /* host */ "b", "^a$",
                        /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, -1
+                       /* attr */ NULL, NULL, NULL, NULL, 0
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "s1", NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, NULL, NULL, 1
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ NULL, "^s1$",
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, NULL, NULL, 1
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "s1", "^s1$",
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, NULL, NULL, 1
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "x1", NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, -1
+                       /* attr */ NULL, NULL, NULL, NULL, 0
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ NULL, "x",
-                       /* attr */ NULL, NULL, NULL, NULL, -1
+                       /* attr */ NULL, NULL, NULL, NULL, 0
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "x1", "x",
-                       /* attr */ NULL, NULL, NULL, NULL, -1
+                       /* attr */ NULL, NULL, NULL, NULL, 0
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "s1", "x",
-                       /* attr */ NULL, NULL, NULL, NULL, -1
+                       /* attr */ NULL, NULL, NULL, NULL, 0
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "x1", "s",
-                       /* attr */ NULL, NULL, NULL, NULL, -1
+                       /* attr */ NULL, NULL, NULL, NULL, 0
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "s1", "^s1$",
-                       /* attr */ "k1", NULL, NULL, NULL, 0
+                       /* attr */ "k1", NULL, NULL, NULL, 1
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "s1", "^s1$",
-                       /* attr */ NULL, "^k", NULL, NULL, 0
+                       /* attr */ NULL, "^k", NULL, NULL, 1
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "s1", "^s1$",
-                       /* attr */ NULL, NULL, "v1", NULL, 0
+                       /* attr */ NULL, NULL, "v1", NULL, 1
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "s1", "^s1$",
-                       /* attr */ NULL, NULL, NULL, "^v1$", 0
+                       /* attr */ NULL, NULL, NULL, "^v1$", 1
                },
                {
                        /* host */ "a", "^a$",
                        /* svc  */ "s1", "^s1$",
-                       /* attr */ "k1", "1", "v1", "1", 0
+                       /* attr */ "k1", "1", "v1", "1", 1
                },
        };
 
@@ -262,14 +262,14 @@ START_TEST(test_store_match_op)
                sdb_store_matcher_t *right;
                int expected;
        } golden_data[] = {
-               { "OR",  always, always,  0 },
-               { "OR",  always, never,   0 },
-               { "OR",  never,  always,  0 },
-               { "OR",  never,  never,  -1 },
-               { "AND", always, always,  0 },
-               { "AND", always, never,  -1 },
-               { "AND", never,  always, -1 },
-               { "AND", never,  never,  -1 },
+               { "OR",  always, always, 1 },
+               { "OR",  always, never,  1 },
+               { "OR",  never,  always, 1 },
+               { "OR",  never,  never,  0 },
+               { "AND", always, always, 1 },
+               { "AND", always, never,  0 },
+               { "AND", never,  always, 0 },
+               { "AND", never,  never,  0 },
        };
 
        int status;
@@ -278,10 +278,10 @@ START_TEST(test_store_match_op)
        obj = sdb_store_get_host("a");
 
        status = sdb_store_matcher_matches(always, obj);
-       fail_unless(status == 0,
+       fail_unless(status == 1,
                        "INTERNAL ERROR: 'always' did not match host");
        status = sdb_store_matcher_matches(never, obj);
-       fail_unless(status < 0,
+       fail_unless(status == 0,
                        "INTERNAL ERROR: 'never' matches host");
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {