Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
authorSebastian Harl <sh@tokkee.org>
Sun, 27 Jul 2014 17:09:01 +0000 (19:09 +0200)
committerSebastian Harl <sh@tokkee.org>
Sun, 27 Jul 2014 17:09:01 +0000 (19:09 +0200)
Conflicts:
src/frontend/grammar.y

doc/sysdbql.7.txt
src/core/store_lookup.c
src/frontend/grammar.y
src/frontend/query.c
src/frontend/scanner.l
src/include/core/store.h
t/integration/simple_query.sh
t/unit/core/store_lookup_test.c
t/unit/frontend/parser_test.c

index c4f52a9a8c1110ac3880b7c03b849eeb6b75d991..447950d37ec00ad11c8dbd403212895e2be346a1 100644 (file)
@@ -11,7 +11,7 @@ SYNOPSIS
 
   LIST;
 
-  QUERY hosts WHERE attribute.architecture = 'amd64';
+  LOOKUP hosts MATCHING attribute.architecture = 'amd64';
 
 DESCRIPTION
 -----------
@@ -39,20 +39,20 @@ value includes the hostname, a list of services referenced by the host, and a
 list of attributes for the host and each service. If the host does not exist,
 an error is returned.
 
-*LOOKUP* hosts *WHERE* '<search_condition>'::
+*LOOKUP* hosts *MATCHING* '<search_condition>'::
 Retrieve detailed information about all host objects matching the specified
 search condition. The return value is a list of detailed information for each
 matching host providing the same details as returned by the *FETCH* command.
-See the section "WHERE clause" for more details about how to specify the
+See the section "MATCHING clause" for more details about how to specify the
 search condition.
 
-WHERE clause
-~~~~~~~~~~~~
-The *WHERE* clause in a query specifies a boolean expression which is used to
-match host objects based on their names, their attributes, or services
-referenced by the host. Each *WHERE* clause may be made up of one or multiple
-subexpressions each matching on one criteria. The following subexpressions
-are supported by SysDB:
+MATCHING clause
+~~~~~~~~~~~~~~~
+The *MATCHING* clause in a query specifies a boolean expression which is used
+to match host objects based on their names, their attributes, or services
+referenced by the host. Each *MATCHING* clause may be made up of one or
+multiple subexpressions each matching on one criteria. The following
+subexpressions are supported by SysDB:
 
 '<field>' '<operator>' '<value>'::
        Match a named field against the specified value. See below for what fields
@@ -158,7 +158,7 @@ replies look like. The replies are pretty-printed to more easily follow them.
           ...
         }]}
 
-  LOOKUP hosts WHERE attribute.architecture = 'amd64';
+  LOOKUP hosts MATCHING attribute.architecture = 'amd64';
   [{
       "name": "host1.example.com",
       "last_update": "2001-02-03 04:05:06 +0700",
index a3efb4669c182ee66938ac3554ba79c10d577d84..0bb157c76e7a49162e3832f5b9ae0e013372485a 100644 (file)
@@ -57,21 +57,21 @@ typedef struct {
        sdb_store_matcher_t *m;
        sdb_store_lookup_cb  cb;
        void *user_data;
-} lookup_iter_data_t;
+} scan_iter_data_t;
 
 /*
  * private helper functions
  */
 
 static int
-lookup_iter(sdb_store_obj_t *obj, void *user_data)
+scan_iter(sdb_store_obj_t *obj, void *user_data)
 {
-       lookup_iter_data_t *d = user_data;
+       scan_iter_data_t *d = user_data;
 
        if (sdb_store_matcher_matches(d->m, obj))
                return d->cb(obj, d->user_data);
        return 0;
-} /* lookup_iter */
+} /* scan_iter */
 
 static sdb_attribute_t *
 attr_get(sdb_host_t *host, const char *name)
@@ -891,15 +891,15 @@ sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
 } /* sdb_store_matcher_tostring */
 
 int
-sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
+sdb_store_scan(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
                void *user_data)
 {
-       lookup_iter_data_t data = { m, cb, user_data };
+       scan_iter_data_t data = { m, cb, user_data };
 
        if (! cb)
                return -1;
-       return sdb_store_iterate(lookup_iter, &data);
-} /* sdb_store_lookup */
+       return sdb_store_iterate(scan_iter, &data);
+} /* sdb_store_scan */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 
index a607ee0aba96bd0698147b81e0d441ab55182451..e0d8e7ecc46136b01cb84546ba1d3e11d7fb9e2b 100644 (file)
@@ -81,7 +81,7 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 
 %token SCANNER_ERROR
 
-%token AND OR IS NOT WHERE
+%token AND OR IS NOT MATCHING
 %token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
 %token CMP_LT CMP_LE CMP_GE CMP_GT
 
@@ -221,12 +221,12 @@ list_statement:
        ;
 
 /*
- * LOOKUP <type> WHERE <condition>;
+ * LOOKUP <type> MATCHING <condition>;
  *
  * Returns detailed information about <type> matching condition.
  */
 lookup_statement:
-       LOOKUP IDENTIFIER WHERE condition
+       LOOKUP IDENTIFIER MATCHING condition
                {
                        /* TODO: support other types as well */
                        if (strcasecmp($2, "hosts")) {
index d7476d345d48791f7cac512daca7ebfcfae49782..2a6aecff6b1513856f22c9929646bb64c34367c7 100644 (file)
@@ -166,7 +166,7 @@ sdb_fe_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m)
 
        sdb_strbuf_append(buf, "[");
 
-       if (sdb_store_lookup(m, lookup_tojson, buf)) {
+       if (sdb_store_scan(m, lookup_tojson, buf)) {
                sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
                sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
                sdb_strbuf_destroy(buf);
index a7b72b43794541985e722ca2f2c6ea3a61848e12..ef30682b45b837f8cb931f1e7257f7568ddd3f2b 100644 (file)
@@ -114,14 +114,14 @@ float             ({float1}|{float2}|{float3}|{float4}|{float5})
                        return LIST;
                else if (! strcasecmp(yytext, "LOOKUP"))
                        return LOOKUP;
+               else if (! strcasecmp(yytext, "MATCHING"))
+                       return MATCHING;
                else if (! strcasecmp(yytext, "NOT"))
                        return NOT;
                else if (! strcasecmp(yytext, "NULL"))
                        return NULL_T;
                else if (! strcasecmp(yytext, "OR"))
                        return OR;
-               else if (! strcasecmp(yytext, "WHERE"))
-                       return WHERE;
 
                yylval->str = strdup(yytext);
                return IDENTIFIER;
index 7a9dc44bbc887cd00583675ed841d8c9c70ec30f..4e8d468c4263f0e57cd2f26d301aedf0006102a7 100644 (file)
@@ -295,16 +295,17 @@ sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
 
 /*
- * sdb_store_lookup:
+ * sdb_store_scan:
  * Look up objects in the store. The specified callback function is called for
- * each object in the store matching 'm'.
+ * each object in the store matching 'm'. The function performs a full scan of
+ * all hosts stored in the database.
  *
  * Returns:
  *  - 0 on success
  *  - a negative value else
  */
 int
-sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
+sdb_store_scan(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
                void *user_data);
 
 /*
index ef12bf8f2dbf1a735ff284c7cfb8b084b19098c9..6d7f5e5d9af9228b2b20e40e66b4bb0d62b976bb 100755 (executable)
@@ -89,7 +89,7 @@ output="$( run_sysdb -H "$SOCKET_FILE" -c "FETCH 'does.not.exist'" )" \
 echo "$output" | grep -F 'not found'
 
 output="$( run_sysdb -H "$SOCKET_FILE" \
-       -c "LOOKUP hosts WHERE attribute.architecture = 'x42'" )"
+       -c "LOOKUP hosts MATCHING attribute.architecture = 'x42'" )"
 echo "$output" \
        | grep -F '"host1.example.com"' \
        | grep -F '"host2.example.com"'
@@ -98,7 +98,7 @@ echo "$output" | grep -F 'other.host.name' && exit 1
 echo "$output" | grep -F 'some.host.name' && exit 1
 
 output="$( run_sysdb -H "$SOCKET_FILE" \
-       -c "LOOKUP hosts WHERE attribute != 'architecture'" )"
+       -c "LOOKUP hosts MATCHING attribute != 'architecture'" )"
 echo "$output" \
        | grep -F '"some.host.name"' \
        | grep -F '"localhost"'
@@ -107,7 +107,7 @@ echo "$output" | grep -F 'host1.example.com' && exit 1
 echo "$output" | grep -F 'host2.example.com' && exit 1
 
 output="$( run_sysdb -H "$SOCKET_FILE" \
-       -c "LOOKUP hosts WHERE service = 'sysdbd'" )"
+       -c "LOOKUP hosts MATCHING service = 'sysdbd'" )"
 echo "$output" | grep -F '"localhost"'
 echo "$output" | grep -F 'some.host.name' && exit 1
 echo "$output" | grep -F 'other.host.name' && exit 1
@@ -115,7 +115,7 @@ echo "$output" | grep -F 'host1.example.com' && exit 1
 echo "$output" | grep -F 'host2.example.com' && exit 1
 
 output="$( run_sysdb -H "$SOCKET_FILE" \
-       -c "LOOKUP hosts WHERE host =~ 'example.com'" )"
+       -c "LOOKUP hosts MATCHING host =~ 'example.com'" )"
 echo "$output" \
        | grep -F '"host1.example.com"' \
        | grep -F '"host2.example.com"'
@@ -125,7 +125,7 @@ echo "$output" | grep -F 'localhost' && exit 1
 
 # When querying hosts that don't exist, expect a zero exit code.
 output="$( run_sysdb -H "$SOCKET_FILE" \
-       -c "LOOKUP hosts WHERE attribute.invalid = 'none'" )"
+       -c "LOOKUP hosts MATCHING attribute.invalid = 'none'" )"
 echo $output | grep -E '^\[\]$'
 
 stop_sysdbd
index 7ecaaf0c3706fb535909cf8488c23dc158234e99..351ae18da7dfd899c918c897da267503597d644b 100644 (file)
@@ -491,22 +491,22 @@ START_TEST(test_parse_cmp)
 END_TEST
 
 static int
-lookup_cb(sdb_store_obj_t *obj, void *user_data)
+scan_cb(sdb_store_obj_t *obj, void *user_data)
 {
        int *i = user_data;
 
        fail_unless(obj != NULL,
-                       "sdb_store_lookup callback received NULL obj; expected: "
+                       "sdb_store_scan callback received NULL obj; expected: "
                        "<store base obj>");
        fail_unless(i != NULL,
-                       "sdb_store_lookup callback received NULL user_data; "
+                       "sdb_store_scan callback received NULL user_data; "
                        "expected: <pointer to data>");
 
        ++(*i);
        return 0;
-} /* lookup_cb */
+} /* scan_cb */
 
-START_TEST(test_lookup)
+START_TEST(test_scan)
 {
 #define PTR_RE "0x[0-9a-f]+"
        struct {
@@ -569,11 +569,11 @@ START_TEST(test_lookup)
        size_t i;
 
        n = 0;
-       check = sdb_store_lookup(NULL, lookup_cb, &n);
+       check = sdb_store_scan(NULL, scan_cb, &n);
        fail_unless(check == 0,
-                       "sdb_store_lookup() = %d; expected: 0", check);
+                       "sdb_store_scan() = %d; expected: 0", check);
        fail_unless(n == 3,
-                       "sdb_store_lookup called callback %d times; expected: 3", (int)n);
+                       "sdb_store_scan called callback %d times; expected: 3", (int)n);
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
                sdb_store_matcher_t *m;
@@ -591,9 +591,9 @@ START_TEST(test_lookup)
                                golden_data[i].tostring_re);
 
                n = 0;
-               sdb_store_lookup(m, lookup_cb, &n);
+               sdb_store_scan(m, scan_cb, &n);
                fail_unless(n == golden_data[i].expected,
-                               "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
+                               "sdb_store_scan(matcher{%s}) found %d hosts; expected: %d",
                                golden_data[i].query, n, golden_data[i].expected);
                sdb_object_deref(SDB_OBJ(m));
        }
@@ -613,7 +613,7 @@ core_store_lookup_suite(void)
        tcase_add_test(tc, test_store_cond);
        tcase_add_test(tc, test_store_match_op);
        tcase_add_test(tc, test_parse_cmp);
-       tcase_add_test(tc, test_lookup);
+       tcase_add_test(tc, test_scan);
        suite_add_tcase(s, tc);
 
        return s;
index f1e656982cbb21de211fd850b64e456da68ee84f..3ca27ba19ffb47ec637575469b398c7d85d10961 100644 (file)
@@ -58,84 +58,84 @@ START_TEST(test_parse)
                { "LIST;",               -1,  1, CONNECTION_LIST   },
                { "LIST; INVALID",        5,  1, CONNECTION_LIST   },
 
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "host = 'host'",       -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE NOT "
+               { "LOOKUP hosts MATCHING NOT "
                  "host = 'host'",       -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "host =~ 'p' AND "
                  "service =~ 'p'",      -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE NOT "
+               { "LOOKUP hosts MATCHING NOT "
                  "host =~ 'p' AND "
                  "service =~ 'p'",      -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "host =~ 'p' AND "
                  "service =~ 'p' OR "
                  "service =~ 'r'",      -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE NOT "
+               { "LOOKUP hosts MATCHING NOT "
                  "host =~ 'p' AND "
                  "service =~ 'p' OR "
                  "service =~ 'r'",      -1,  1, CONNECTION_LOOKUP },
 
                /* numeric constants */
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo = "
                  "1234",                -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo != "
                  "+234",                -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo < "
                  "-234",                -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo > "
                  "12.4",                -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo <= "
                  "12.",                 -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo >= "
                  ".4",                  -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo = "
                  "+12e3",               -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo = "
                  "+12e-3",              -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo = "
                  "-12e+3",              -1,  1, CONNECTION_LOOKUP },
 
                /* NULL */
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo "
                  "IS NULL",             -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo "
                  "IS NOT NULL",         -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "NOT attribute.foo "
                  "IS NULL",             -1,  1, CONNECTION_LOOKUP },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "host IS NULL",        -1, -1, 0 },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "service IS NULL",     -1, -1, 0 },
 
                /* invalid numeric constants */
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo = "
                  "+-12e+3",             -1, -1, 0 },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo = "
                  "-12e-+3",             -1, -1, 0 },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo = "
                  "e+3",                 -1, -1, 0 },
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo = "
                  "3e",                  -1, -1, 0 },
                /* following SQL standard, we don't support hex numbers */
-               { "LOOKUP hosts WHERE "
+               { "LOOKUP hosts MATCHING "
                  "attribute.foo = "
                  "0x12",                -1, -1, 0 },
 
@@ -150,7 +150,7 @@ START_TEST(test_parse)
                { "/* some incomplete",  -1, -1, 0 },
 
                { "LOOKUP hosts",        -1, -1, 0 },
-               { "LOOKUP foo WHERE "
+               { "LOOKUP foo MATCHING "
                  "host = 'host'",       -1, -1, 0 },
        };