From: Sebastian Harl Date: Thu, 1 Aug 2013 23:47:39 +0000 (-0700) Subject: store: Let sdb_store_attribute() accept const arguments. X-Git-Tag: sysdb-0.1.0~389 X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=commitdiff_plain;h=43ad58582d8be53eae16e9c2eb8601add8af0350 store: Let sdb_store_attribute() accept const arguments. … rather than an attribute object. This eases memory management for the caller and removes unnecessary string duplicates. --- diff --git a/src/backend/puppet/store-configs.c b/src/backend/puppet/store-configs.c index 6f70f49..4343920 100644 --- a/src/backend/puppet/store-configs.c +++ b/src/backend/puppet/store-configs.c @@ -83,36 +83,33 @@ sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client, size_t n, sdb_data_t *data, sdb_object_t __attribute__((unused)) *user_data) { - sdb_attribute_t attr = SDB_ATTR_INIT; - int status; + const char *hostname; + const char *key; + const char *value; + sdb_time_t last_update; + assert(n == 4); assert((data[0].type == SDB_TYPE_STRING) && (data[1].type == SDB_TYPE_STRING) && (data[2].type == SDB_TYPE_STRING) && (data[3].type == SDB_TYPE_DATETIME)); - attr.hostname = strdup(data[0].data.string); - SDB_OBJ(&attr)->name = strdup(data[1].data.string); - attr.attr_value = strdup(data[2].data.string); - attr._last_update = data[3].data.datetime; + hostname = data[0].data.string; + key = data[1].data.string; + value = data[2].data.string; + last_update = data[3].data.datetime; - status = sdb_store_attribute(&attr); + status = sdb_store_attribute(hostname, key, value, last_update); if (status < 0) { sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to " "store/update host attribute '%s' for host '%s'.", - SDB_OBJ(&attr)->name, attr.hostname); - free(attr.hostname); - free(SDB_OBJ(&attr)->name); - free(attr.attr_value); + key, hostname); return -1; } - free(attr.hostname); - free(SDB_OBJ(&attr)->name); - free(attr.attr_value); return 0; } /* sdb_puppet_stcfg_get_attrs */ diff --git a/src/core/store.c b/src/core/store.c index b96ff4e..e8a693c 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -324,18 +324,17 @@ sdb_store_has_host(const char *name) } /* sdb_store_has_host */ int -sdb_store_attribute(const sdb_attribute_t *attr) +sdb_store_attribute(const char *hostname, const char *key, const char *value, + sdb_time_t last_update) { sdb_host_t *host; sdb_attribute_t *old; - sdb_time_t last_update; int status = 0; - if (! attr) + if ((! hostname) || (! key)) return -1; - last_update = attr->_last_update; if (last_update <= 0) last_update = sdb_gettime(); @@ -344,20 +343,18 @@ sdb_store_attribute(const sdb_attribute_t *attr) pthread_rwlock_wrlock(&host_lock); - host = SDB_HOST(sdb_llist_search_by_name(host_list, attr->hostname)); + host = SDB_HOST(sdb_llist_search_by_name(host_list, hostname)); if (! host) { pthread_rwlock_unlock(&host_lock); return -1; } - old = SDB_ATTR(sdb_llist_search_by_name(host->attributes, - SDB_CONST_OBJ(attr)->name)); + old = SDB_ATTR(sdb_llist_search_by_name(host->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")", - attr->hostname, SDB_CONST_OBJ(attr)->name, last_update, - old->_last_update); + hostname, key, last_update, old->_last_update); status = 1; } else { @@ -365,7 +362,8 @@ sdb_store_attribute(const sdb_attribute_t *attr) } } else { - sdb_attribute_t *new = SDB_ATTR(sdb_object_clone(SDB_CONST_OBJ(attr))); + sdb_attribute_t *new = SDB_ATTR(sdb_object_create(key, + sdb_attribute_type, hostname, value)); if (! new) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute " diff --git a/src/include/core/store.h b/src/include/core/store.h index 6c833ec..7c6eee1 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -107,12 +107,11 @@ sdb_store_has_host(const char *name); /* * sdb_store_attribute: * Add/update a host's attribute in the store. If the attribute, identified by - * its name, already exists for the specified host, it will be updated - * according to the specified 'attr' object. If the referenced host does not - * exist, an error will be reported. Else, a new entry will be created in the - * store. Any memory required for storing the entry will be allocated and - * managed by the store itself. The specified attribute-object will not be - * referenced or further accessed. + * its key, already exists for the specified host, it will be updated to the + * specified values. If the referenced host does not exist, an error will be + * reported. Else, a new entry will be created in the store. Any memory + * required for storing the entry will be allocated and managed by the store + * itself. * * Returns: * - 0 on success @@ -121,7 +120,8 @@ sdb_store_has_host(const char *name); * - a negative value on error */ int -sdb_store_attribute(const sdb_attribute_t *attr); +sdb_store_attribute(const char *hostname, const char *key, const char *value, + sdb_time_t last_update); /* * sdb_store_service: