From: Sebastian Harl Date: Mon, 6 Jan 2014 10:16:52 +0000 (+0100) Subject: store: Added sdb_store_get_host(). X-Git-Tag: sysdb-0.1.0~252 X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=commitdiff_plain;h=bc9f267e9066b70b8e379d11da80cb6b2749d49e store: Added sdb_store_get_host(). … querying a host by it's name. --- diff --git a/src/core/store.c b/src/core/store.c index 5a36e29..e8d14a0 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -460,6 +460,22 @@ sdb_store_has_host(const char *name) return host != NULL; } /* sdb_store_has_host */ +sdb_store_base_t * +sdb_store_get_host(const char *name) +{ + sdb_store_obj_t *host; + + if (! name) + return NULL; + + host = sdb_store_lookup(SDB_HOST, name); + if (! host) + return NULL; + + sdb_object_ref(SDB_OBJ(host)); + return STORE_BASE(host); +} /* sdb_store_get_host */ + int sdb_store_attribute(const char *hostname, const char *key, const char *value, sdb_time_t last_update) diff --git a/src/include/core/store.h b/src/include/core/store.h index cf4ecbb..ab11a21 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -65,9 +65,20 @@ typedef struct sdb_store_base sdb_store_base_t; int sdb_store_host(const char *name, sdb_time_t last_update); +/* + * sdb_store_has_host: + * sdb_store_get_host: + * Query the store for a host by its (canonicalized) name. + * + * sdb_store_get_host increments the ref count of the host object. The caller + * needs to deref it when no longer using it. + */ _Bool sdb_store_has_host(const char *name); +sdb_store_base_t * +sdb_store_get_host(const char *name); + /* * sdb_store_attribute: * Add/update a host's attribute in the store. If the attribute, identified by diff --git a/t/core/store_test.c b/t/core/store_test.c index 4a939b6..effd05a 100644 --- a/t/core/store_test.c +++ b/t/core/store_test.c @@ -81,6 +81,67 @@ START_TEST(test_store_host) } END_TEST +START_TEST(test_store_get_host) +{ + char *golden_hosts[] = { "a", "b", "c" }; + char *unknown_hosts[] = { "x", "y", "z" }; + size_t i; + + for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) { + int status = sdb_store_host(golden_hosts[i], 1); + fail_unless(status >= 0, + "sdb_store_host(%s) = %d; expected: >=0", + golden_hosts[i], status); + } + + for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) { + sdb_store_base_t *sobj1, *sobj2; + int ref_cnt; + + fail_unless(sdb_store_has_host(golden_hosts[i]), + "sdb_store_has_host(%s) = FALSE; expected: TRUE", + golden_hosts[i]); + + sobj1 = sdb_store_get_host(golden_hosts[i]); + fail_unless(sobj1 != NULL, + "sdb_store_get_host(%s) = NULL; expected: ", + golden_hosts[i]); + ref_cnt = SDB_OBJ(sobj1)->ref_cnt; + + fail_unless(ref_cnt > 1, + "sdb_store_get_host(%s) did not increment ref count: " + "got: %d; expected: >1", golden_hosts[i], ref_cnt); + + sobj2 = sdb_store_get_host(golden_hosts[i]); + fail_unless(sobj2 != NULL, + "sdb_store_get_host(%s) = NULL; expected: ", + golden_hosts[i]); + + fail_unless(sobj1 == sobj2, + "sdb_store_get_host(%s) returned different objects " + "in successive calls", golden_hosts[i]); + fail_unless(SDB_OBJ(sobj2)->ref_cnt == ref_cnt + 1, + "sdb_store_get_hosts(%s) did not increment ref count " + "(first call: %d; second call: %d)", + golden_hosts[i], ref_cnt, SDB_OBJ(sobj2)->ref_cnt); + + sdb_object_deref(SDB_OBJ(sobj1)); + sdb_object_deref(SDB_OBJ(sobj2)); + } + for (i = 0; i < SDB_STATIC_ARRAY_LEN(unknown_hosts); ++i) { + sdb_store_base_t *sobj; + + fail_unless(!sdb_store_has_host(unknown_hosts[i]), + "sdb_store_has_host(%s) = TRUE; expected: FALSE", + unknown_hosts[i]); + + sobj = sdb_store_get_host(unknown_hosts[i]); + fail_unless(!sobj, "sdb_store_get_host(%s) = ; expected: NULL", + unknown_hosts[i], sobj ? SDB_OBJ(sobj)->name : "NULL"); + } +} +END_TEST + START_TEST(test_store_attr) { struct { @@ -271,6 +332,7 @@ core_store_suite(void) * even when using CK_NOFORK */ tcase_add_test(tc, test_store_tojson); tcase_add_test(tc, test_store_host); + tcase_add_test(tc, test_store_get_host); tcase_add_test(tc, test_store_attr); tcase_add_test(tc, test_store_service); suite_add_tcase(s, tc);