From: Sebastian Harl Date: Tue, 28 Oct 2014 06:44:33 +0000 (+0100) Subject: store: Merged sdb_store_iterate() into sdb_store_scan(). X-Git-Tag: sysdb-0.6.0~52 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=13997f70dfd15cceed0c12bb6ddef787bbbe04e1;p=sysdb.git store: Merged sdb_store_iterate() into sdb_store_scan(). These functions are really the same when using a NULL matcher and filter. --- diff --git a/src/core/store.c b/src/core/store.c index d9ca4c0..6f525c3 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -1091,13 +1091,16 @@ sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags) return 0; } /* sdb_store_tojson */ -/* TODO: actually support hierarchical data */ int -sdb_store_iterate(sdb_store_iter_cb cb, void *user_data) +sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter, + sdb_store_lookup_cb cb, void *user_data) { sdb_avltree_iter_t *host_iter; int status = 0; + if (! cb) + return -1; + pthread_rwlock_rdlock(&host_lock); host_iter = sdb_avltree_get_iter(hosts); @@ -1111,16 +1114,18 @@ sdb_store_iterate(sdb_store_iter_cb cb, void *user_data) host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter)); assert(host); - if (cb(host, user_data)) { - status = -1; - break; + if (sdb_store_matcher_matches(m, host, filter)) { + if (cb(host, user_data)) { + status = -1; + break; + } } } sdb_avltree_iter_destroy(host_iter); pthread_rwlock_unlock(&host_lock); return status; -} /* sdb_store_iterate */ +} /* sdb_store_scan */ /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 513b6a6..94459cb 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -49,31 +49,6 @@ #include -/* - * private data types - */ - -typedef struct { - sdb_store_matcher_t *m; - sdb_store_matcher_t *filter; - sdb_store_lookup_cb cb; - void *user_data; -} scan_iter_data_t; - -/* - * private helper functions - */ - -static int -scan_iter(sdb_store_obj_t *obj, void *user_data) -{ - scan_iter_data_t *d = user_data; - - if (sdb_store_matcher_matches(d->m, obj, d->filter)) - return d->cb(obj, d->user_data); - return 0; -} /* scan_iter */ - /* * matcher implementations */ @@ -737,16 +712,5 @@ sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj, return matchers[m->type](m, obj, filter); } /* sdb_store_matcher_matches */ -int -sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter, - sdb_store_lookup_cb cb, void *user_data) -{ - scan_iter_data_t data = { m, filter, cb, user_data }; - - if (! cb) - return -1; - return sdb_store_iterate(scan_iter, &data); -} /* sdb_store_scan */ - /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ diff --git a/src/include/core/store.h b/src/include/core/store.h index 9ba19b8..76995d4 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -610,24 +610,6 @@ int sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags); -/* - * sdb_store_iter_cb: - * Store iterator callback. Iteration stops if the callback returns non-zero. - */ -typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data); - -/* - * sdb_store_iterate: - * Iterate the entire store, calling the specified callback for each object. - * The user_data pointer is passed on to each call of the callback. - * - * Returns: - * - 0 on success - * - a negative value else - */ -int -sdb_store_iterate(sdb_store_iter_cb cb, void *user_data); - #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/t/unit/core/store_test.c b/t/unit/core/store_test.c index f459b6e..ecbb39d 100644 --- a/t/unit/core/store_test.c +++ b/t/unit/core/store_test.c @@ -852,63 +852,63 @@ START_TEST(test_interval) END_TEST static int -iter_incr(sdb_store_obj_t *obj, void *user_data) +scan_incr(sdb_store_obj_t *obj, void *user_data) { intptr_t *i = user_data; fail_unless(obj != NULL, - "sdb_store_iterate callback received NULL obj; expected: " + "sdb_store_scan callback received NULL obj; expected: " ""); fail_unless(i != NULL, - "sdb_store_iterate callback received NULL user_data; " + "sdb_store_scan callback received NULL user_data; " "expected: "); ++(*i); return 0; -} /* iter_incr */ +} /* scan_incr */ static int -iter_error(sdb_store_obj_t *obj, void *user_data) +scan_error(sdb_store_obj_t *obj, void *user_data) { intptr_t *i = user_data; fail_unless(obj != NULL, - "sdb_store_iterate callback received NULL obj; expected: " + "sdb_store_scan callback received NULL obj; expected: " ""); fail_unless(i != NULL, - "sdb_store_iterate callback received NULL user_data; " + "sdb_store_scan callback received NULL user_data; " "expected: "); ++(*i); return -1; -} /* iter_error */ +} /* scan_error */ -START_TEST(test_iterate) +START_TEST(test_scan) { intptr_t i = 0; int check; /* empty store */ - check = sdb_store_iterate(iter_incr, &i); + check = sdb_store_scan(/* m, filter = */ NULL, NULL, scan_incr, &i); fail_unless(check == -1, - "sdb_store_iterate(), empty store = %d; expected: -1", check); + "sdb_store_scan(), empty store = %d; expected: -1", check); fail_unless(i == 0, - "sdb_store_iterate called callback %d times; expected: 0", (int)i); + "sdb_store_scan called callback %d times; expected: 0", (int)i); populate(); - check = sdb_store_iterate(iter_incr, &i); + check = sdb_store_scan(/* m, filter = */ NULL, NULL, scan_incr, &i); fail_unless(check == 0, - "sdb_store_iterate() = %d; expected: 0", check); + "sdb_store_scan() = %d; expected: 0", check); fail_unless(i == 2, - "sdb_store_iterate called callback %d times; expected: 1", (int)i); + "sdb_store_scan called callback %d times; expected: 1", (int)i); i = 0; - check = sdb_store_iterate(iter_error, &i); + check = sdb_store_scan(/* m, filter = */ NULL, NULL, scan_error, &i); fail_unless(check == -1, - "sdb_store_iterate(), error callback = %d; expected: -1", check); + "sdb_store_scan(), error callback = %d; expected: -1", check); fail_unless(i == 1, - "sdb_store_iterate called callback %d times " + "sdb_store_scan called callback %d times " "(callback returned error); expected: 1", (int)i); } END_TEST @@ -930,7 +930,7 @@ core_store_suite(void) tcase_add_test(tc, test_store_service_attr); tcase_add_test(tc, test_get_field); tcase_add_test(tc, test_interval); - tcase_add_test(tc, test_iterate); + tcase_add_test(tc, test_scan); tcase_add_unchecked_fixture(tc, NULL, sdb_store_clear); suite_add_tcase(s, tc);