From: Sebastian Harl Date: Thu, 19 Jun 2014 12:52:16 +0000 (+0200) Subject: store_lookup: Let parse_cmp() use the new name matcher. X-Git-Tag: sysdb-0.2.0~54 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=77a6c3b08a4ad75673a4b6209230a52082927589;p=sysdb.git store_lookup: Let parse_cmp() use the new name matcher. --- diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 8bdb40e..b5b40e6 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -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); diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index c143a8a..efd2071 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -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" \\} " "\\} \\}\\)\\)" }, }; diff --git a/t/unit/frontend/parser_test.c b/t/unit/frontend/parser_test.c index cb3a2ad..e245c29 100644 --- a/t/unit/frontend/parser_test.c +++ b/t/unit/frontend/parser_test.c @@ -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' ", 18, MATCHER_HOST }, + { "host.name = 'localhost' -- foo", -1, MATCHER_NAME }, + { "host.name = 'host' ", 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 "