Code

store: Let sdb_store_scan() pass on filters to callback functions.
authorSebastian Harl <sh@tokkee.org>
Thu, 30 Oct 2014 22:34:08 +0000 (23:34 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 30 Oct 2014 22:34:08 +0000 (23:34 +0100)
The callback function needs access to the filter anyway, so let's handle this
centrally.

src/core/store.c
src/frontend/query.c
src/include/core/store.h
t/unit/core/store_lookup_test.c
t/unit/core/store_test.c

index a977954c473b171c6b09bc0bb0f815f8ccdca184..b909d001dc7a560d77c66168b30e362221bda719 100644 (file)
@@ -1125,7 +1125,7 @@ sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
                                assert(obj);
 
                                if (sdb_store_matcher_matches(m, obj, filter)) {
-                                       if (cb(obj, user_data)) {
+                                       if (cb(obj, filter, user_data)) {
                                                status = -1;
                                                break;
                                        }
@@ -1133,7 +1133,7 @@ sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
                        }
                }
                else if (sdb_store_matcher_matches(m, host, filter)) {
-                       if (cb(host, user_data))
+                       if (cb(host, filter, user_data))
                                status = -1;
                }
 
index 15e81b8f06613fcf6deb469ea92fe3d376df49d2..ff170224aac36d042367780548cee8edd44de74b 100644 (file)
 
 typedef struct {
        sdb_strbuf_t *buf;
-       sdb_store_matcher_t *filter;
-
        size_t last_len;
 } tojson_data_t;
 
 static int
-lookup_tojson(sdb_store_obj_t *obj, void *user_data)
+lookup_tojson(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
+               void *user_data)
 {
        tojson_data_t *data = user_data;
        int status;
 
-       if (data->filter && (! sdb_store_matcher_matches(data->filter, obj, NULL)))
+       if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
                return 0;
 
        if (sdb_strbuf_len(data->buf) > data->last_len)
                sdb_strbuf_append(data->buf, ",");
        data->last_len = sdb_strbuf_len(data->buf);
-       status = sdb_store_host_tojson(obj, data->buf,
-                       data->filter, /* flags = */ 0);
+       status = sdb_store_host_tojson(obj, data->buf, filter, /* flags = */ 0);
        return status;
 } /* lookup_tojson */
 
@@ -382,7 +380,7 @@ int
 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
                sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
 {
-       tojson_data_t data = { NULL, filter, 0 };
+       tojson_data_t data = { NULL, 0 };
        uint32_t res_type = htonl(CONNECTION_LOOKUP);
 
        /* XXX: support other types */
@@ -409,11 +407,8 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
        sdb_strbuf_memcpy(data.buf, &res_type, sizeof(uint32_t));
        sdb_strbuf_append(data.buf, "[");
 
-       /* Let the JSON serializer handle the filter instead of the scanner. Else,
-        * we'd have to filter twice -- once in the scanner and then again in the
-        * serializer. */
        data.last_len = sdb_strbuf_len(data.buf);
-       if (sdb_store_scan(SDB_HOST, m, /* filter */ NULL, lookup_tojson, &data)) {
+       if (sdb_store_scan(SDB_HOST, m, filter, lookup_tojson, &data)) {
                sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
                sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
                sdb_strbuf_destroy(data.buf);
index 3c2231c31bcb0c488d782805f390f1e1866cd0f1..2720db472f30abd4c853b7c1c805979749407f25 100644 (file)
@@ -540,9 +540,11 @@ sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
 /*
  * sdb_store_lookup_cb:
  * Lookup callback. It is called for each matching object when looking up data
- * in the store. The lookup aborts if the callback returns non-zero.
+ * in the store passing on the lookup filter and the specified user-data. The
+ * lookup aborts early if the callback returns non-zero.
  */
-typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
+typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter, void *user_data);
 
 /*
  * sdb_store_scan:
index c32005393a90cb730ad9204696bc8978fb0490e7..931c4f2097d75200516f8ac2e20aa2f4320eab88 100644 (file)
@@ -499,10 +499,13 @@ START_TEST(test_store_match_op)
 END_TEST
 
 static int
-scan_cb(sdb_store_obj_t *obj, void *user_data)
+scan_cb(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
 {
        int *i = user_data;
 
+       if (! sdb_store_matcher_matches(filter, obj, NULL))
+               return 0;
+
        fail_unless(obj != NULL,
                        "sdb_store_scan callback received NULL obj; expected: "
                        "<store base obj>");
index d132508537a76789ca3e9effe2240473ff8f5e88..1c1eac440d6f7f4d63ebbe973f35beeae35c4227 100644 (file)
@@ -867,10 +867,13 @@ START_TEST(test_interval)
 END_TEST
 
 static int
-scan_count(sdb_store_obj_t *obj, void *user_data)
+scan_count(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
 {
        intptr_t *i = user_data;
 
+       if (! sdb_store_matcher_matches(filter, obj, NULL))
+               return 0;
+
        fail_unless(obj != NULL,
                        "sdb_store_scan callback received NULL obj; expected: "
                        "<store base obj>");
@@ -883,10 +886,13 @@ scan_count(sdb_store_obj_t *obj, void *user_data)
 } /* scan_count */
 
 static int
-scan_error(sdb_store_obj_t *obj, void *user_data)
+scan_error(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, void *user_data)
 {
        intptr_t *i = user_data;
 
+       if (! sdb_store_matcher_matches(filter, obj, NULL))
+               return 0;
+
        fail_unless(obj != NULL,
                        "sdb_store_scan callback received NULL obj; expected: "
                        "<store base obj>");