Code

store: sdb_store_get_field: Make result parameter optional.
authorSebastian Harl <sh@tokkee.org>
Mon, 13 Oct 2014 07:07:37 +0000 (09:07 +0200)
committerSebastian Harl <sh@tokkee.org>
Mon, 13 Oct 2014 07:07:37 +0000 (09:07 +0200)
src/core/store.c
src/include/core/store.h
t/unit/core/store_test.c

index f4f2b58f0d700c0df8967aafddd6429261732af8..94b823bfeea6ee81f5189e6646d9051f74f4347a 100644 (file)
@@ -896,7 +896,7 @@ sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
 {
        sdb_data_t tmp;
 
-       if ((! obj) || (! res))
+       if (! obj)
                return -1;
 
        switch (field) {
@@ -905,19 +905,18 @@ sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
                        tmp.data.string = strdup(SDB_OBJ(obj)->name);
                        if (! tmp.data.string)
                                return -1;
-                       *res = tmp;
                        break;
                case SDB_FIELD_LAST_UPDATE:
-                       res->type = SDB_TYPE_DATETIME;
-                       res->data.datetime = obj->last_update;
+                       tmp.type = SDB_TYPE_DATETIME;
+                       tmp.data.datetime = obj->last_update;
                        break;
                case SDB_FIELD_AGE:
-                       res->type = SDB_TYPE_DATETIME;
-                       res->data.datetime = sdb_gettime() - obj->last_update;
+                       tmp.type = SDB_TYPE_DATETIME;
+                       tmp.data.datetime = sdb_gettime() - obj->last_update;
                        break;
                case SDB_FIELD_INTERVAL:
-                       res->type = SDB_TYPE_DATETIME;
-                       res->data.datetime = obj->interval;
+                       tmp.type = SDB_TYPE_DATETIME;
+                       tmp.data.datetime = obj->interval;
                        break;
                case SDB_FIELD_BACKEND:
                        /* TODO: add support for storing array values in a data object
@@ -925,6 +924,10 @@ sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
                default:
                        return -1;
        }
+       if (res)
+               *res = tmp;
+       else
+               sdb_data_free_datum(&tmp);
        return 0;
 } /* sdb_store_get_field */
 
index 90416a2cc0699cc3df67d50ac516a1d415a9f058..7aeb2875e9cbe302ad0311fc352e824eba8435a1 100644 (file)
@@ -241,7 +241,8 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric,
  * sdb_store_get_field:
  * Get the value of a stored object's queryable field. The caller is
  * responsible for freeing any dynamically allocated memory possibly stored in
- * the returned value.
+ * the returned value. If 'res' is NULL, the function will return whether the
+ * field exists.
  *
  * Note: Retrieving the backend this way is not currently supported.
  *
index 00e3c64389d3843321bb2fcbb396f8174fb0ed68..c1071fb584e0f18b333eb18607c1193f374a4fc1 100644 (file)
@@ -723,15 +723,22 @@ START_TEST(test_get_field)
        fail_unless(check < 0,
                        "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
                        "expected: <0");
-       check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, NULL);
-       fail_unless(check < 0,
-                       "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
-                       "expected: <0");
        check = sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, &value);
        fail_unless(check < 0,
                        "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, <value>) = %d; "
                        "expected: <0");
 
+       check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, NULL);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
+                       "expected: 0");
+       /* 'name' is dynamically allocated; make sure it's not leaked even
+        * if there is no result parameter */
+       check = sdb_store_get_field(host, SDB_FIELD_NAME, NULL);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
+                       "expected: 0");
+
        check = sdb_store_get_field(host, SDB_FIELD_NAME, &value);
        fail_unless(check == 0,
                        "sdb_store_get_field(<host>, SDB_FIELD_NAME, <value>) = "