Code

store_lookup: Let parse_cmp() use the new name matcher.
authorSebastian Harl <sh@tokkee.org>
Thu, 19 Jun 2014 12:52:16 +0000 (14:52 +0200)
committerSebastian Harl <sh@tokkee.org>
Thu, 19 Jun 2014 12:52:16 +0000 (14:52 +0200)
src/core/store_lookup.c
t/unit/core/store_lookup_test.c
t/unit/frontend/parser_test.c

index 8bdb40e63c41f9ff8bd487390663614ede8082d2..b5b40e6f59badcc04bd2f83754bceab566a1724a 100644 (file)
@@ -700,8 +700,9 @@ sdb_store_matcher_t *
 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
                const char *op, const char *value)
 {
-       int typ = -1;
-       int inv = 0;
+       int type = -1;
+       _Bool inv = 0;
+       _Bool re = 0;
 
        sdb_store_matcher_t *m = NULL;
 
@@ -709,11 +710,11 @@ sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
        const char *matcher_re = NULL;
 
        if (! strcasecmp(obj_type, "host"))
-               typ = SDB_HOST;
+               type = SDB_HOST;
        else if (! strcasecmp(obj_type, "service"))
-               typ = SDB_SERVICE;
+               type = SDB_SERVICE;
        else if (! strcasecmp(obj_type, "attribute"))
-               typ = SDB_ATTRIBUTE;
+               type = SDB_ATTRIBUTE;
 
        /* TODO: support other operators */
        if (! strcasecmp(op, "=")) {
@@ -725,45 +726,31 @@ sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
        }
        else if (! strcasecmp(op, "=~")) {
                matcher_re = value;
+               re = 1;
        }
        else if (! strcasecmp(op, "!~")) {
                matcher_re = value;
                inv = 1;
+               re = 1;
        }
        else
                return NULL;
 
-       if (! strcasecmp(attr, "name")) {
-               /* accept */
-       }
-       else if (typ == SDB_ATTRIBUTE)
+       if (! strcasecmp(attr, "name"))
+               m = sdb_store_name_matcher(type, value, re);
+       else if (type == SDB_ATTRIBUTE) {
                m = sdb_store_host_matcher(/* name = */ NULL, NULL,
                                /* service = */ NULL,
                                sdb_store_attr_matcher(attr, NULL, matcher, matcher_re));
-       else
-               return NULL;
 
-       if (m) {
-               /* accept the attribute value matcher */
+               /* pass ownership to the host matcher */
+               if (m)
+                       sdb_object_deref(SDB_OBJ(HOST_M(m)->attr));
        }
-       else if (typ == SDB_HOST)
-               m = sdb_store_host_matcher(matcher, matcher_re, NULL, NULL);
-       else if (typ == SDB_SERVICE)
-               m = sdb_store_host_matcher(/* name = */ NULL, NULL,
-                               sdb_store_service_matcher(matcher, matcher_re, NULL),
-                               /* attr = */ NULL);
-       else if (typ == SDB_ATTRIBUTE)
-               m = sdb_store_host_matcher(/* name = */ NULL, NULL,
-                               /* service = */ NULL,
-                               sdb_store_attr_matcher(matcher, matcher_re, NULL, NULL));
 
        if (! m)
                return NULL;
 
-       /* pass ownership to the host matcher */
-       sdb_object_deref(SDB_OBJ(HOST_M(m)->service));
-       sdb_object_deref(SDB_OBJ(HOST_M(m)->attr));
-
        if (inv) {
                sdb_store_matcher_t *tmp;
                tmp = sdb_store_inv_matcher(m);
index c143a8aacc2dca51d291cb5485e34cb26454fc56..efd2071e4590ef9b6846e2b3015ef5ffd39c5366 100644 (file)
@@ -436,23 +436,23 @@ START_TEST(test_parse_cmp)
                const char *value;
                int expected;
        } golden_data[] = {
-               { "host",      "name", "=",  "hostname", MATCHER_HOST },
+               { "host",      "name", "=",  "hostname", MATCHER_NAME },
                { "host",      "name", "!=", "hostname", MATCHER_NOT },
-               { "host",      "name", "=~", "hostname", MATCHER_HOST },
+               { "host",      "name", "=~", "hostname", MATCHER_NAME },
                { "host",      "name", "!~", "hostname", MATCHER_NOT },
                { "host",      "attr", "=",  "hostname", -1 },
                { "host",      "attr", "!=", "hostname", -1 },
                { "host",      "name", "&^", "hostname", -1 },
-               { "service",   "name", "=",  "srvname",  MATCHER_HOST },
+               { "service",   "name", "=",  "srvname",  MATCHER_NAME },
                { "service",   "name", "!=", "srvname",  MATCHER_NOT },
-               { "service",   "name", "=~", "srvname",  MATCHER_HOST },
+               { "service",   "name", "=~", "srvname",  MATCHER_NAME },
                { "service",   "name", "!~", "srvname",  MATCHER_NOT },
                { "service",   "attr", "=",  "srvname",  -1 },
                { "service",   "attr", "!=", "srvname",  -1 },
                { "service",   "name", "&^", "srvname",  -1 },
-               { "attribute", "name", "=",  "attrname", MATCHER_HOST },
+               { "attribute", "name", "=",  "attrname", MATCHER_NAME },
                { "attribute", "name", "!=", "attrname", MATCHER_NOT },
-               { "attribute", "name", "=~", "attrname", MATCHER_HOST },
+               { "attribute", "name", "=~", "attrname", MATCHER_NAME },
                { "attribute", "name", "!~", "attrname", MATCHER_NOT },
                { "attribute", "attr", "=",  "attrname", MATCHER_HOST },
                { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
@@ -515,50 +515,39 @@ START_TEST(test_lookup)
                const char *tostring_re;
        } golden_data[] = {
                { "host.name = 'a'",       1,
-                       "HOST\\{ NAME\\{ 'a', \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
                { "host.name =~ 'a|b'",    2,
-                       "HOST\\{ NAME\\{ NULL, "PTR_RE" \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
                { "host.name =~ 'host'",   0,
-                       "HOST\\{ NAME\\{ NULL, "PTR_RE" \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
                { "host.name =~ '.'",      3,
-                       "HOST\\{ NAME\\{ NULL, "PTR_RE" \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
                { "service.name = 's1'",   2,
-                       "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{ "
-                                       "NAME\\{ 's1', \\(nil\\) }, ATTR\\{\\} "
-                               "\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
                { "service.name =~ 's'",   2,
-                       "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{ "
-                                       "NAME\\{ NULL, "PTR_RE" }, ATTR\\{\\} "
-                               "\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
                { "service.name !~ 's'",   1,
-                       "(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{ "
-                                       "NAME\\{ NULL, "PTR_RE" }, ATTR\\{\\} "
-                               "\\}, ATTR\\{\\} \\})" },
+                       "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
                { "attribute.name = 'k1'", 1,
-                       "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
-                                       "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ NULL, \\(nil\\) \\} "
-                               "\\} \\}" },
+                       "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
                { "attribute.name = 'x'",  0,
-                       "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
-                                       "NAME\\{ 'x', \\(nil\\) }, VALUE\\{ NULL, \\(nil\\) \\} "
-                               "\\} \\}" },
+                       "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
                { "attribute.k1 = 'v1'",   1,
                        "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
                                        "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ 'v1', \\(nil\\) \\} "
                                "\\} \\}" },
                { "attribute.k1 != 'v1'",  2,
-                       "(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
+                       "\\(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
                                        "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ 'v1', \\(nil\\) \\} "
                                "\\} \\})" },
                { "attribute.k1 != 'v2'",  3,
-                       "(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
+                       "\\(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
                                        "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ 'v2', \\(nil\\) \\} "
                                "\\} \\})" },
                { "attribute.name != 'x' "
                  "AND attribute.y !~ 'x'", 3,
-                       "\\(AND, \\(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
-                                       "NAME\\{ 'x', \\(nil\\) }, VALUE\\{ NULL, \\(nil\\) \\} "
-                               "\\} \\}\\), \\(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
+                       "\\(AND, \\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} "
+                               "\\}\\), \\(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
                                                "NAME\\{ 'y', \\(nil\\) }, VALUE\\{ NULL, "PTR_RE" \\} "
                                        "\\} \\}\\)\\)" },
        };
index cb3a2ad7d48b56ef2b15b77a75e7f37a5e5581ea..e245c29e56f1b89d279c5c7052c63208dae7b2a2 100644 (file)
@@ -141,21 +141,21 @@ START_TEST(test_parse_matcher)
                { "",                               -1, -1 },
 
                /* valid expressions */
-               { "host.name = 'localhost'",        -1,  MATCHER_HOST },
+               { "host.name = 'localhost'",        -1,  MATCHER_NAME },
                { "host.name != 'localhost'",       -1,  MATCHER_NOT },
-               { "host.name =~ 'host'",            -1,  MATCHER_HOST },
+               { "host.name =~ 'host'",            -1,  MATCHER_NAME },
                { "host.name !~ 'host'",            -1,  MATCHER_NOT },
-               { "host.name = 'localhost' -- foo", -1,  MATCHER_HOST },
-               { "host.name = 'host' <garbage>",   18,  MATCHER_HOST },
+               { "host.name = 'localhost' -- foo", -1,  MATCHER_NAME },
+               { "host.name = 'host' <garbage>",   18,  MATCHER_NAME },
                /* match hosts by service */
-               { "service.name = 'name'",          -1,  MATCHER_HOST },
+               { "service.name = 'name'",          -1,  MATCHER_NAME },
                { "service.name != 'name'",         -1,  MATCHER_NOT },
-               { "service.name =~ 'pattern'",      -1,  MATCHER_HOST },
+               { "service.name =~ 'pattern'",      -1,  MATCHER_NAME },
                { "service.name !~ 'pattern'",      -1,  MATCHER_NOT },
                /* match hosts by attribute */
-               { "attribute.name = 'name'",        -1,  MATCHER_HOST },
+               { "attribute.name = 'name'",        -1,  MATCHER_NAME },
                { "attribute.name != 'name'",       -1,  MATCHER_NOT },
-               { "attribute.name =~ 'pattern'",    -1,  MATCHER_HOST },
+               { "attribute.name =~ 'pattern'",    -1,  MATCHER_NAME },
                { "attribute.name !~ 'pattern'",    -1,  MATCHER_NOT },
                /* composite expressions */
                { "host.name =~ 'pattern' AND "