summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 9a0ead9)
raw | patch | inline | side by side (parent: 9a0ead9)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 1 Aug 2013 23:47:39 +0000 (16:47 -0700) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 1 Aug 2013 23:47:39 +0000 (16:47 -0700) |
… rather than an attribute object. This eases memory management for the caller
and removes unnecessary string duplicates.
and removes unnecessary string duplicates.
src/backend/puppet/store-configs.c | patch | blob | history | |
src/core/store.c | patch | blob | history | |
src/include/core/store.h | patch | blob | history |
index 6f70f4940d01867645c098b762bb4416e6332066..4343920552461c5e53987062024c0cb2ca1a764d 100644 (file)
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 b96ff4e9054e46b76c45cc14be00d71e3966e62c..e8a693ce0c268b228bab54e29614a49447517e6e 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
} /* 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();
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 {
}
}
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 "
index 6c833ec6e2162673dc012e63f28947d4a201ecb7..7c6eee1c0bc801b899a8864e3baac25e6c3a1ec4 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
/*
* 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
* - 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: