Code

store: Merged sdb_store_iterate() into sdb_store_scan().
authorSebastian Harl <sh@tokkee.org>
Tue, 28 Oct 2014 06:44:33 +0000 (07:44 +0100)
committerSebastian Harl <sh@tokkee.org>
Tue, 28 Oct 2014 06:44:33 +0000 (07:44 +0100)
These functions are really the same when using a NULL matcher and filter.

src/core/store.c
src/core/store_lookup.c
src/include/core/store.h
t/unit/core/store_test.c

index d9ca4c0eb113a7a2824249d92d83fa45f4d4e56c..6f525c31462ed74e0d4256b889373cdcc38b4ee7 100644 (file)
@@ -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 : */
 
index 513b6a64a7a824c5263eec708945c14a3812cfe6..94459cb097b1e21526f9c54f65d1cf3cd433c1ca 100644 (file)
 
 #include <limits.h>
 
-/*
- * 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 : */
 
index 9ba19b8acbeb6da1ed3513ac9676080b1ba2f0bf..76995d40a11452c8bc46881349a323eab88f33ab 100644 (file)
@@ -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
index f459b6e0afd461be0754fb64cd192928e8b3e2c9..ecbb39d93b601ebe4f2da1891f4779d379bbf368 100644 (file)
@@ -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: "
                        "<store base obj>");
        fail_unless(i != NULL,
-                       "sdb_store_iterate callback received NULL user_data; "
+                       "sdb_store_scan callback received NULL user_data; "
                        "expected: <pointer to data>");
 
        ++(*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: "
                        "<store base obj>");
        fail_unless(i != NULL,
-                       "sdb_store_iterate callback received NULL user_data; "
+                       "sdb_store_scan callback received NULL user_data; "
                        "expected: <pointer to data>");
 
        ++(*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);