From 739d378d2a0346b223c54acbb55d292335c276cb Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 13 Oct 2014 09:07:37 +0200 Subject: [PATCH] store: sdb_store_get_field: Make result parameter optional. --- src/core/store.c | 19 +++++++++++-------- src/include/core/store.h | 3 ++- t/unit/core/store_test.c | 15 +++++++++++---- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/core/store.c b/src/core/store.c index f4f2b58..94b823b 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -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 */ diff --git a/src/include/core/store.h b/src/include/core/store.h index 90416a2..7aeb287 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -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. * diff --git a/t/unit/core/store_test.c b/t/unit/core/store_test.c index 00e3c64..c1071fb 100644 --- a/t/unit/core/store_test.c +++ b/t/unit/core/store_test.c @@ -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(, 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, ) = %d; " "expected: <0"); + check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, NULL); + fail_unless(check == 0, + "sdb_store_get_field(, 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(, 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(, SDB_FIELD_NAME, ) = " -- 2.30.2