Code

store: Added sdb_store_get_host().
authorSebastian Harl <sh@tokkee.org>
Mon, 6 Jan 2014 10:16:52 +0000 (11:16 +0100)
committerSebastian Harl <sh@tokkee.org>
Mon, 6 Jan 2014 10:16:52 +0000 (11:16 +0100)
… querying a host by it's name.

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

index 5a36e29eef5817e2988d445accd19b352fbe6f61..e8d14a079679a12d5a76ef58735b44920065755d 100644 (file)
@@ -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)
index cf4ecbbb6f1155444a451bf57535bf536704098e..ab11a21776a193a460da5e3a8e100b4d1f25cff7 100644 (file)
@@ -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
index 4a939b64f430ef8dd43d479fdc57ed0789188805..effd05a7d5d8425f89b5f8db7d9f6d818b490940 100644 (file)
@@ -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: <host>",
+                               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: <host>",
+                               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) = <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);