From: Sebastian Harl Date: Thu, 30 Oct 2014 22:34:08 +0000 (+0100) Subject: store: Let sdb_store_scan() pass on filters to callback functions. X-Git-Tag: sysdb-0.6.0~49 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=5c5ff5c501a1753314005d7988fa33ffaea41815;p=sysdb.git store: Let sdb_store_scan() pass on filters to callback functions. The callback function needs access to the filter anyway, so let's handle this centrally. --- diff --git a/src/core/store.c b/src/core/store.c index a977954..b909d00 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -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; } diff --git a/src/frontend/query.c b/src/frontend/query.c index 15e81b8..ff17022 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -43,25 +43,23 @@ 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); diff --git a/src/include/core/store.h b/src/include/core/store.h index 3c2231c..2720db4 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -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: diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index c320053..931c4f2 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -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: " ""); diff --git a/t/unit/core/store_test.c b/t/unit/core/store_test.c index d132508..1c1eac4 100644 --- a/t/unit/core/store_test.c +++ b/t/unit/core/store_test.c @@ -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: " ""); @@ -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: " "");