Code

store: Handle cname translation centrally.
[sysdb.git] / src / core / store.c
index 385cd634d9271dcd63baaaccbb5d350816d8a3e6..dba88cd2a03a70e126bf0b409e9d68b6b85e6b25 100644 (file)
@@ -88,10 +88,12 @@ typedef struct {
 enum {
        SDB_HOST = 1,
        SDB_SERVICE,
+       SDB_ATTRIBUTE,
 };
 #define TYPE_TO_NAME(t) \
        (((t) == SDB_HOST) ? "host" \
-               : ((t) == SDB_SERVICE) ? "service" : "unknown")
+               : ((t) == SDB_SERVICE) ? "service" \
+               : ((t) == SDB_ATTRIBUTE) ? "attribute" : "unknown")
 
 /* shortcuts for accessing the sdb_store_obj_t attributes
  * of inheriting objects */
@@ -304,30 +306,50 @@ sdb_store_lookup(int type, const char *name)
        return sdb_store_lookup_in_list(obj_list, type, name);
 } /* sdb_store_lookup */
 
+/* The obj_lock has to be acquired before calling this function. */
 static int
 store_obj(int parent_type, const char *parent_name,
-               int type, const char *name, sdb_time_t last_update)
+               int type, const char *name, sdb_time_t last_update,
+               store_obj_t **updated_obj)
 {
+       char *parent_cname = NULL, *cname = NULL;
+
        sdb_llist_t *parent_list;
        store_obj_t *old;
        int status = 0;
 
-       if (! name)
-               return -1;
-
        if (last_update <= 0)
                last_update = sdb_gettime();
 
        assert((parent_type == 0)
-                       || (parent_type = SDB_HOST)
+                       || (parent_type == SDB_HOST)
                        || (parent_type == SDB_SERVICE));
-       assert((type == 0) || (type = SDB_HOST) || (type == SDB_SERVICE));
-
-       pthread_rwlock_wrlock(&obj_lock);
+       assert((type == 0)
+                       || (type == SDB_HOST)
+                       || (type == SDB_SERVICE)
+                       || (type == SDB_ATTRIBUTE));
+
+       if (parent_type == SDB_HOST) {
+               parent_cname = sdb_plugin_cname(strdup(parent_name));
+               if (! parent_cname) {
+                       sdb_log(SDB_LOG_ERR, "store: strdup failed");
+                       return -1;
+               }
+               parent_name = parent_cname;
+       }
+       if (type == SDB_HOST) {
+               cname = sdb_plugin_cname(strdup(name));
+               if (! cname) {
+                       sdb_log(SDB_LOG_ERR, "store: strdup failed");
+                       return -1;
+               }
+               name = cname;
+       }
 
        if (! obj_list) {
                if (! (obj_list = sdb_llist_create())) {
-                       pthread_rwlock_unlock(&obj_lock);
+                       free(parent_cname);
+                       free(cname);
                        return -1;
                }
        }
@@ -341,15 +363,22 @@ store_obj(int parent_type, const char *parent_name,
                        sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
                                        "parent %s '%s' not found", TYPE_TO_NAME(type), name,
                                        TYPE_TO_NAME(parent_type), parent_name);
-                       pthread_rwlock_unlock(&obj_lock);
+                       free(parent_cname);
+                       free(cname);
                        return -1;
                }
 
-               parent_list = parent->children;
+               if (type == SDB_ATTRIBUTE)
+                       parent_list = parent->attributes;
+               else
+                       parent_list = parent->children;
        }
 
        /* TODO: only look into direct children? */
-       old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
+       if (type == SDB_ATTRIBUTE)
+               old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
+       else
+               old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
 
        if (old) {
                if (old->last_update > last_update) {
@@ -363,28 +392,44 @@ store_obj(int parent_type, const char *parent_name,
                else {
                        old->last_update = last_update;
                }
+
+               if (updated_obj)
+                       *updated_obj = old;
        }
        else {
-               store_obj_t *new = STORE_OBJ(sdb_object_create(name,
-                                       sdb_store_obj_type, last_update, type));
+               store_obj_t *new;
+
+               if (type == SDB_ATTRIBUTE)
+                       /* the value will be updated by the caller */
+                       new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
+                                               last_update, NULL));
+               else
+                       new = STORE_OBJ(sdb_object_create(name, sdb_store_obj_type,
+                                               last_update, type));
+
                if (! new) {
                        char errbuf[1024];
                        sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
                                        TYPE_TO_NAME(type), name,
                                        sdb_strerror(errno, errbuf, sizeof(errbuf)));
-                       pthread_rwlock_unlock(&obj_lock);
+                       free(parent_cname);
+                       free(cname);
                        return -1;
                }
 
-               /* TODO: insert type-aware */
+               /* TODO: insert type-aware; the current version works as long as we
+                * don't support to store hierarchical data */
                status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
                                sdb_object_cmp_by_name);
 
                /* pass control to the list or destroy in case of an error */
                sdb_object_deref(SDB_OBJ(new));
-       }
 
-       pthread_rwlock_unlock(&obj_lock);
+               if (updated_obj)
+                       *updated_obj = new;
+       }
+       free(parent_cname);
+       free(cname);
        return status;
 } /* sdb_store_obj */
 
@@ -395,21 +440,16 @@ store_obj(int parent_type, const char *parent_name,
 int
 sdb_store_host(const char *name, sdb_time_t last_update)
 {
-       char *cname;
-       int status = 0;
+       int status;
 
        if (! name)
                return -1;
 
-       cname = sdb_plugin_cname(strdup(name));
-       if (! cname) {
-               sdb_log(SDB_LOG_ERR, "store: strdup failed");
-               return -1;
-       }
-
+       pthread_rwlock_wrlock(&obj_lock);
        status = store_obj(/* parent = */ 0, NULL,
-                       /* stored object = */ SDB_HOST, cname, last_update);
-       free(cname);
+                       /* stored object = */ SDB_HOST, name, last_update,
+                       /* updated_obj = */ NULL);
+       pthread_rwlock_unlock(&obj_lock);
        return status;
 } /* sdb_store_host */
 
@@ -429,58 +469,23 @@ int
 sdb_store_attribute(const char *hostname, const char *key, const char *value,
                sdb_time_t last_update)
 {
-       sdb_store_obj_t *sobj;
-       sdb_attribute_t *old;
+       int status;
 
-       int status = 0;
+       store_obj_t *updated_attr = NULL;
 
        if ((! hostname) || (! key))
                return -1;
 
-       if (last_update <= 0)
-               last_update = sdb_gettime();
-
-       if (! obj_list)
-               return -1;
-
        pthread_rwlock_wrlock(&obj_lock);
-
-       sobj = sdb_store_lookup(SDB_HOST, hostname);
-       if (! sobj) {
-               pthread_rwlock_unlock(&obj_lock);
-               return -1;
-       }
-
-       old = SDB_ATTR(sdb_llist_search_by_name(sobj->attributes, key));
-       if (old) {
-               if (old->_last_update > last_update) {
-                       sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute "
-                                       "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
-                                       hostname, key, last_update, old->_last_update);
-                       status = 1;
-               }
-               else {
-                       old->_last_update = last_update;
-               }
-       }
-       else {
-               sdb_attribute_t *new = SDB_ATTR(sdb_object_create(key,
-                                       sdb_attribute_type, last_update, value));
-               if (! new) {
-                       char errbuf[1024];
-                       sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
-                                       "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
-                       pthread_rwlock_unlock(&obj_lock);
-                       return -1;
-               }
-
-               status = sdb_llist_insert_sorted(sobj->attributes, SDB_OBJ(new),
-                               sdb_object_cmp_by_name);
-
-               /* pass control to the list or destroy in case of an error */
-               sdb_object_deref(SDB_OBJ(new));
+       status = store_obj(/* parent = */ SDB_HOST, hostname,
+                       /* stored object = */ SDB_ATTRIBUTE, key, last_update,
+                       &updated_attr);
+
+       SDB_ATTR(updated_attr)->value = strdup(value);
+       if (! SDB_ATTR(updated_attr)->value) {
+               sdb_object_deref(SDB_OBJ(updated_attr));
+               status = -1;
        }
-
        pthread_rwlock_unlock(&obj_lock);
        return status;
 } /* sdb_store_attribute */
@@ -489,21 +494,16 @@ int
 sdb_store_service(const char *hostname, const char *name,
                sdb_time_t last_update)
 {
-       char *cname;
-       int status = 0;
+       int status;
 
        if ((! hostname) || (! name))
                return -1;
 
-       cname = sdb_plugin_cname(strdup(hostname));
-       if (! cname) {
-               sdb_log(SDB_LOG_ERR, "store: strdup failed");
-               return -1;
-       }
-
-       status = store_obj(/* parent = */ SDB_HOST, cname,
-                       /* stored object = */ SDB_SERVICE, name, last_update);
-       free(cname);
+       pthread_rwlock_wrlock(&obj_lock);
+       status = store_obj(/* parent = */ SDB_HOST, hostname,
+                       /* stored object = */ SDB_SERVICE, name, last_update,
+                       /* updated obj = */ NULL);
+       pthread_rwlock_unlock(&obj_lock);
        return status;
 } /* sdb_store_service */