summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 0670db1)
raw | patch | inline | side by side (parent: 0670db1)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 30 Oct 2014 22:34:08 +0000 (23:34 +0100) | ||
committer | Sebastian 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.
centrally.
diff --git a/src/core/store.c b/src/core/store.c
index a977954c473b171c6b09bc0bb0f815f8ccdca184..b909d001dc7a560d77c66168b30e362221bda719 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
assert(obj);
if (sdb_store_matcher_matches(m, obj, filter)) {
- if (cb(obj, user_data)) {
+ if (cb(obj, filter, user_data)) {
status = -1;
break;
}
}
}
else if (sdb_store_matcher_matches(m, host, filter)) {
- if (cb(host, user_data))
+ if (cb(host, filter, user_data))
status = -1;
}
diff --git a/src/frontend/query.c b/src/frontend/query.c
index 15e81b8f06613fcf6deb469ea92fe3d376df49d2..ff170224aac36d042367780548cee8edd44de74b 100644 (file)
--- a/src/frontend/query.c
+++ b/src/frontend/query.c
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 */
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 */
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)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
/*
* 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)
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)
--- a/t/unit/core/store_test.c
+++ b/t/unit/core/store_test.c
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>");
} /* 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>");