Code

store: Added sdb_store_get_attr().
authorSebastian Harl <sh@tokkee.org>
Mon, 13 Oct 2014 07:18:41 +0000 (09:18 +0200)
committerSebastian Harl <sh@tokkee.org>
Mon, 13 Oct 2014 07:18:41 +0000 (09:18 +0200)
src/core/store.c
src/include/core/store.h

index 94b823bfeea6ee81f5189e6646d9051f74f4347a..db8a9d9c5686f7e7c79059fd383c6014ad5329bf 100644 (file)
@@ -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)
index 7aeb2875e9cbe302ad0311fc352e824eba8435a1..df4d853106bac891ae96e887a24354e58895a726 100644 (file)
@@ -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.
  *