From: Sebastian Harl Date: Mon, 13 Oct 2014 07:18:41 +0000 (+0200) Subject: store: Added sdb_store_get_attr(). X-Git-Tag: sysdb-0.6.0~123 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=c5168cb68478e7dff4dedb666e36b1fea2b018e2;p=sysdb.git store: Added sdb_store_get_attr(). --- diff --git a/src/core/store.c b/src/core/store.c index 94b823b..db8a9d9 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -931,6 +931,44 @@ sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res) return 0; } /* sdb_store_get_field */ +int +sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res) +{ + sdb_avltree_t *tree = NULL; + sdb_avltree_iter_t *iter = NULL; + + if ((! obj) || (! name)) + return -1; + + if (obj->type == SDB_HOST) + tree = HOST(obj)->attributes; + else if (obj->type == SDB_SERVICE) + tree = SVC(obj)->attributes; + else if (obj->type == SDB_METRIC) + tree = METRIC(obj)->attributes; + + if (! tree) + return -1; + + iter = sdb_avltree_get_iter(tree); + while (sdb_avltree_iter_has_next(iter)) { + sdb_object_t *attr = sdb_avltree_iter_get_next(iter); + + if (strcasecmp(SDB_OBJ(attr)->name, name)) + continue; + + assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE); + if (res) + sdb_data_copy(res, &ATTR(attr)->value); + sdb_avltree_iter_destroy(iter); + return 0; + } + sdb_avltree_iter_destroy(iter); + + /* not found */ + return -1; +} /* sdb_store_get_attr */ + int sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags) diff --git a/src/include/core/store.h b/src/include/core/store.h index 7aeb287..df4d853 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -253,6 +253,20 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric, int sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res); +/* + * sdb_store_get_attr: + * Get the value of a stored object's attribute. The caller is responsible for + * freeing any dynamically allocated memory possibly stored in the returned + * value.If 'res' is NULL, the function will return whether the attribute + * exists. + * + * Returns: + * - 0 if the attribute exists + * - a negative value else + */ +int +sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res); + /* * Expressions specify arithmetic expressions. *