Code

store: Split store_obj into separate host and service objects.
authorSebastian Harl <sh@tokkee.org>
Fri, 27 Jun 2014 16:46:57 +0000 (18:46 +0200)
committerSebastian Harl <sh@tokkee.org>
Fri, 27 Jun 2014 16:46:57 +0000 (18:46 +0200)
src/core/store-private.h
src/core/store.c
src/core/store_lookup.c

index e501b2cd830169b2b7de01660207c976ca66f089..603648ca057b51ced94b5a7ecd05bdadd615acbc 100644 (file)
@@ -60,20 +60,27 @@ typedef struct {
 
        sdb_data_t value;
 } sdb_attribute_t;
-#define SDB_ATTR(obj) ((sdb_attribute_t *)(obj))
-#define SDB_CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
+#define ATTR(obj) ((sdb_attribute_t *)(obj))
+#define CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
+
+typedef struct {
+       sdb_store_base_t super;
+
+       sdb_llist_t *attributes;
+} sdb_service_t;
+#define SVC(obj) ((sdb_service_t *)(obj))
+#define CONST_SVC(obj) ((const sdb_service_t *)(obj))
 
 typedef struct {
        sdb_store_base_t super;
 
        sdb_llist_t *services;
        sdb_llist_t *attributes;
-} sdb_store_obj_t;
-#define SDB_STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
-#define SDB_CONST_STORE_OBJ(obj) ((const sdb_store_obj_t *)(obj))
+} sdb_host_t;
+#define HOST(obj) ((sdb_host_t *)(obj))
+#define CONST_HOST(obj) ((const sdb_host_t *)(obj))
 
-/* shortcuts for accessing the sdb_store_obj_t attributes
- * of inheriting objects */
+/* shortcuts for accessing service/host attributes */
 #define _last_update super.last_update
 #define _interval super.interval
 
index bb37e26a7d9e074466f70d724a8943acabd03747..ccb5f0a954d97e0f867bc2bb2067573245e30133 100644 (file)
@@ -56,7 +56,8 @@ static pthread_rwlock_t obj_lock = PTHREAD_RWLOCK_INITIALIZER;
  * private types
  */
 
-static sdb_type_t sdb_store_obj_type;
+static sdb_type_t sdb_host_type;
+static sdb_type_t sdb_service_type;
 static sdb_type_t sdb_attribute_type;
 
 static int
@@ -82,9 +83,9 @@ store_base_destroy(sdb_object_t *obj)
 } /* store_base_destroy */
 
 static int
-sdb_store_obj_init(sdb_object_t *obj, va_list ap)
+sdb_host_init(sdb_object_t *obj, va_list ap)
 {
-       sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
+       sdb_host_t *sobj = HOST(obj);
        int ret;
 
        /* this will consume the first argument (type) of ap */
@@ -99,13 +100,12 @@ sdb_store_obj_init(sdb_object_t *obj, va_list ap)
        if (! sobj->attributes)
                return -1;
        return 0;
-} /* sdb_store_obj_init */
+} /* sdb_host_init */
 
 static void
-sdb_store_obj_destroy(sdb_object_t *obj)
+sdb_host_destroy(sdb_object_t *obj)
 {
-       sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
-
+       sdb_host_t *sobj = HOST(obj);
        assert(obj);
 
        store_base_destroy(obj);
@@ -114,7 +114,36 @@ sdb_store_obj_destroy(sdb_object_t *obj)
                sdb_llist_destroy(sobj->services);
        if (sobj->attributes)
                sdb_llist_destroy(sobj->attributes);
-} /* sdb_store_obj_destroy */
+} /* sdb_host_destroy */
+
+static int
+sdb_service_init(sdb_object_t *obj, va_list ap)
+{
+       sdb_service_t *sobj = SVC(obj);
+       int ret;
+
+       /* this will consume the first argument (type) of ap */
+       ret = store_base_init(obj, ap);
+       if (ret)
+               return ret;
+
+       sobj->attributes = sdb_llist_create();
+       if (! sobj->attributes)
+               return -1;
+       return 0;
+} /* sdb_service_init */
+
+static void
+sdb_service_destroy(sdb_object_t *obj)
+{
+       sdb_service_t *sobj = SVC(obj);
+       assert(obj);
+
+       store_base_destroy(obj);
+
+       if (sobj->attributes)
+               sdb_llist_destroy(sobj->attributes);
+} /* sdb_service_destroy */
 
 static int
 sdb_attr_init(sdb_object_t *obj, va_list ap)
@@ -130,7 +159,7 @@ sdb_attr_init(sdb_object_t *obj, va_list ap)
        value = va_arg(ap, const sdb_data_t *);
 
        if (value)
-               if (sdb_data_copy(&SDB_ATTR(obj)->value, value))
+               if (sdb_data_copy(&ATTR(obj)->value, value))
                        return -1;
        return 0;
 } /* sdb_attr_init */
@@ -141,19 +170,23 @@ sdb_attr_destroy(sdb_object_t *obj)
        assert(obj);
 
        store_base_destroy(obj);
-       sdb_data_free_datum(&SDB_ATTR(obj)->value);
+       sdb_data_free_datum(&ATTR(obj)->value);
 } /* sdb_attr_destroy */
 
-static sdb_type_t sdb_store_obj_type = {
-       sizeof(sdb_store_obj_t),
+static sdb_type_t sdb_host_type = {
+       sizeof(sdb_host_t),
+       sdb_host_init,
+       sdb_host_destroy
+};
 
-       sdb_store_obj_init,
-       sdb_store_obj_destroy
+static sdb_type_t sdb_service_type = {
+       sizeof(sdb_service_t),
+       sdb_service_init,
+       sdb_service_destroy
 };
 
 static sdb_type_t sdb_attribute_type = {
        sizeof(sdb_attribute_t),
-
        sdb_attr_init,
        sdb_attr_destroy
 };
@@ -162,10 +195,10 @@ static sdb_type_t sdb_attribute_type = {
  * private helper functions
  */
 
-static sdb_store_obj_t *
+static sdb_host_t *
 lookup_host(const char *name)
 {
-       return SDB_STORE_OBJ(sdb_llist_search_by_name(host_list, name));
+       return HOST(sdb_llist_search_by_name(host_list, name));
 } /* lookup_host */
 
 /* The obj_lock has to be acquired before calling this function. */
@@ -207,7 +240,7 @@ store_obj(const char *hostname, int type, const char *name,
        }
 
        if (hostname) {
-               sdb_store_obj_t *host;
+               sdb_host_t *host;
 
                host_cname = sdb_plugin_cname(strdup(hostname));
                if (! host_cname) {
@@ -266,13 +299,16 @@ store_obj(const char *hostname, int type, const char *name,
        else {
                sdb_store_base_t *new;
 
-               if (type == SDB_ATTRIBUTE)
+               if (type == SDB_ATTRIBUTE) {
                        /* the value will be updated by the caller */
                        new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
                                                type, last_update, NULL));
-               else
-                       new = STORE_BASE(sdb_object_create(name, sdb_store_obj_type,
-                                               type, last_update));
+               }
+               else {
+                       sdb_type_t t;
+                       t = type == SDB_HOST ? sdb_host_type : sdb_service_type;
+                       new = STORE_BASE(sdb_object_create(name, t, type, last_update));
+               }
 
                if (! new) {
                        char errbuf[1024];
@@ -342,8 +378,8 @@ store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
 
                sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
                if (type == SDB_ATTRIBUTE) {
-                       char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
-                       sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
+                       char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
+                       sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
                                        SDB_DOUBLE_QUOTED);
                        sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
                                        "\"update_interval\": \"%s\"}", tmp, time_str,
@@ -391,7 +427,7 @@ sdb_store_host(const char *name, sdb_time_t last_update)
 _Bool
 sdb_store_has_host(const char *name)
 {
-       sdb_store_obj_t *host;
+       sdb_host_t *host;
 
        if (! name)
                return NULL;
@@ -403,7 +439,7 @@ sdb_store_has_host(const char *name)
 sdb_store_base_t *
 sdb_store_get_host(const char *name)
 {
-       sdb_store_obj_t *host;
+       sdb_host_t *host;
 
        if (! name)
                return NULL;
@@ -435,8 +471,8 @@ sdb_store_attribute(const char *hostname,
 
        if (status >= 0) {
                assert(updated_attr);
-               sdb_data_free_datum(&SDB_ATTR(updated_attr)->value);
-               if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
+               sdb_data_free_datum(&ATTR(updated_attr)->value);
+               if (sdb_data_copy(&ATTR(updated_attr)->value, value)) {
                        sdb_object_deref(SDB_OBJ(updated_attr));
                        status = -1;
                }
@@ -466,14 +502,14 @@ sdb_store_service(const char *hostname, const char *name,
 int
 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
 {
-       sdb_store_obj_t *host;
+       sdb_host_t *host;
        char time_str[64];
        char interval_str[64];
 
        if ((! h) || (h->type != SDB_HOST) || (! buf))
                return -1;
 
-       host = SDB_STORE_OBJ(h);
+       host = HOST(h);
 
        if (! sdb_strftime(time_str, sizeof(time_str),
                                "%F %T %z", host->_last_update))
index f57cb05cab292fa53d86b56c936d35403fc77351..66f3f22dcfb9b5ec7a9fd447135ab493c7360f92 100644 (file)
@@ -74,16 +74,14 @@ lookup_iter(sdb_store_base_t *obj, void *user_data)
 } /* lookup_iter */
 
 static sdb_store_base_t *
-attr_get(sdb_store_base_t *host, const char *name)
+attr_get(sdb_host_t *host, const char *name)
 {
        sdb_llist_iter_t *iter = NULL;
        sdb_store_base_t *attr = NULL;
 
-       assert(host->type == SDB_HOST);
-
-       iter = sdb_llist_get_iter(SDB_STORE_OBJ(host)->attributes);
+       iter = sdb_llist_get_iter(host->attributes);
        while (sdb_llist_iter_has_next(iter)) {
-               sdb_attribute_t *a = SDB_ATTR(sdb_llist_iter_get_next(iter));
+               sdb_attribute_t *a = ATTR(sdb_llist_iter_get_next(iter));
 
                if (strcasecmp(name, SDB_OBJ(a)->name))
                        continue;
@@ -103,7 +101,7 @@ attr_cmp(sdb_store_base_t *obj, sdb_store_cond_t *cond)
 {
        sdb_attribute_t *attr;
 
-       attr = SDB_ATTR(attr_get(obj, ATTR_C(cond)->name));
+       attr = ATTR(attr_get(HOST(obj), ATTR_C(cond)->name));
        if (! attr)
                return INT_MAX;
        if (attr->value.type != ATTR_C(cond)->value.type)
@@ -172,10 +170,10 @@ match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
                        return match_string(&NAME_M(m)->name, obj->super.name);
                        break;
                case SDB_SERVICE:
-                       iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->services);
+                       iter = sdb_llist_get_iter(HOST(obj)->services);
                        break;
                case SDB_ATTRIBUTE:
-                       iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
+                       iter = sdb_llist_get_iter(HOST(obj)->attributes);
                        break;
        }
 
@@ -198,7 +196,7 @@ match_attr(sdb_store_matcher_t *m, sdb_store_base_t *obj)
        assert(m->type == MATCHER_ATTR);
        assert(ATTR_M(m)->name);
 
-       attr = SDB_ATTR(attr_get(obj, ATTR_M(m)->name));
+       attr = ATTR(attr_get(HOST(obj), ATTR_M(m)->name));
        if (attr) {
                char buf[sdb_data_strlen(&attr->value) + 1];
                if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)