X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fstore.c;h=6deb699024d9b1f90052834efb55d4bcb3abd83a;hb=0f606423bfbb9cfd30d2e55ca9b0f231ded24cfe;hp=b9335818c203a753cd3fa84ae2db18b270e7cfe7;hpb=045e522816461b58e471a6ccddf4ada51236c478;p=sysdb.git diff --git a/src/core/store.c b/src/core/store.c index b933581..6deb699 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -49,9 +49,50 @@ static sdb_llist_t *host_list = NULL; static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER; /* - * public types + * private types */ +static sdb_type_t sdb_host_type; +static sdb_type_t sdb_attribute_type; +static sdb_type_t sdb_service_type; + +typedef struct { + sdb_object_t super; + sdb_time_t last_update; +} 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)) + +typedef struct { + sdb_store_obj_t super; + + char *hostname; +} sdb_service_t; +#define SDB_SVC(obj) ((sdb_service_t *)(obj)) +#define SDB_CONST_SVC(obj) ((const sdb_service_t *)(obj)) + +typedef struct { + sdb_store_obj_t super; + + char *attr_value; + char *hostname; +} sdb_attribute_t; +#define SDB_ATTR(obj) ((sdb_attribute_t *)(obj)) +#define SDB_CONST_ATTR(obj) ((const sdb_attribute_t *)(obj)) + +typedef struct { + sdb_store_obj_t super; + + sdb_llist_t *attributes; + sdb_llist_t *services; +} sdb_host_t; +#define SDB_HOST(obj) ((sdb_host_t *)(obj)) +#define SDB_CONST_HOST(obj) ((const sdb_host_t *)(obj)) + +/* shortcuts for accessing the sdb_store_obj_t attributes of inheriting + * objects */ +#define _last_update super.last_update + static int sdb_host_init(sdb_object_t *obj, va_list __attribute__((unused)) ap) { @@ -79,7 +120,7 @@ sdb_host_destroy(sdb_object_t *obj) } /* sdb_host_destroy */ static sdb_object_t * -sdb_host_do_clone(const sdb_object_t *obj) +sdb_host_clone(const sdb_object_t *obj) { const sdb_host_t *host = (const sdb_host_t *)obj; sdb_host_t *new; @@ -109,7 +150,7 @@ sdb_host_do_clone(const sdb_object_t *obj) } } return SDB_OBJ(new); -} /* sdb_host_do_clone */ +} /* sdb_host_clone */ static int sdb_attr_init(sdb_object_t *obj, va_list ap) @@ -143,8 +184,8 @@ sdb_attr_clone(const sdb_object_t *obj) const sdb_attribute_t *attr = (const sdb_attribute_t *)obj; sdb_attribute_t *new; - new = sdb_attribute_create(attr->hostname, - obj->name, attr->attr_value); + new = SDB_ATTR(sdb_object_create(obj->name, sdb_attribute_type, + attr->hostname, attr->attr_value)); if (! new) return NULL; @@ -181,7 +222,8 @@ sdb_svc_clone(const sdb_object_t *obj) const sdb_service_t *svc = (const sdb_service_t *)obj; sdb_service_t *new; - new = sdb_service_create(svc->hostname, obj->name); + new = SDB_SVC(sdb_object_create(obj->name, sdb_service_type, + svc->hostname)); if (! new) return NULL; @@ -189,15 +231,15 @@ sdb_svc_clone(const sdb_object_t *obj) return SDB_OBJ(new); } /* sdb_svc_clone */ -const sdb_type_t sdb_host_type = { +static sdb_type_t sdb_host_type = { sizeof(sdb_host_t), sdb_host_init, sdb_host_destroy, - sdb_host_do_clone + sdb_host_clone }; -const sdb_type_t sdb_attribute_type = { +static sdb_type_t sdb_attribute_type = { sizeof(sdb_attribute_t), sdb_attr_init, @@ -205,7 +247,7 @@ const sdb_type_t sdb_attribute_type = { sdb_attr_clone }; -const sdb_type_t sdb_service_type = { +static sdb_type_t sdb_service_type = { sizeof(sdb_service_t), sdb_svc_init, @@ -234,7 +276,6 @@ sdb_store_host(const char *name, sdb_time_t last_update) return -1; } - last_update = last_update; if (last_update <= 0) last_update = sdb_gettime(); @@ -275,30 +316,6 @@ sdb_store_host(const char *name, sdb_time_t last_update) free(SDB_OBJ(new)->name); SDB_OBJ(new)->name = cname; - if (! new->attributes) { - if (! (new->attributes = sdb_llist_create())) { - char errbuf[1024]; - sdb_log(SDB_LOG_ERR, "store: Failed to initialize " - "host object '%s': %s", SDB_OBJ(new)->name, - sdb_strerror(errno, errbuf, sizeof(errbuf))); - sdb_object_deref(SDB_OBJ(new)); - pthread_rwlock_unlock(&host_lock); - return -1; - } - } - - if (! new->services) { - if (! (new->services = sdb_llist_create())) { - char errbuf[1024]; - sdb_log(SDB_LOG_ERR, "store: Failed to initialize " - "host object '%s': %s", SDB_OBJ(new)->name, - sdb_strerror(errno, errbuf, sizeof(errbuf))); - sdb_object_deref(SDB_OBJ(new)); - pthread_rwlock_unlock(&host_lock); - return -1; - } - } - status = sdb_llist_insert_sorted(host_list, SDB_OBJ(new), sdb_object_cmp_by_name); @@ -322,34 +339,18 @@ sdb_store_has_host(const char *name) return host != NULL; } /* sdb_store_has_host */ -sdb_attribute_t * -sdb_attribute_create(const char *hostname, - const char *name, const char *value) -{ - sdb_object_t *obj; - - if ((! hostname) || (! name) || (! value)) - return NULL; - - obj = sdb_object_create(name, sdb_attribute_type, hostname, value); - if (! obj) - return NULL; - return SDB_ATTR(obj); -} /* sdb_attribute_create */ - 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(); @@ -358,20 +359,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 { @@ -379,7 +378,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 " @@ -399,33 +399,18 @@ sdb_store_attribute(const sdb_attribute_t *attr) return status; } /* sdb_store_attribute */ -sdb_service_t * -sdb_service_create(const char *hostname, const char *name) -{ - sdb_object_t *obj; - - if ((! hostname) || (! name)) - return NULL; - - obj = sdb_object_create(name, sdb_service_type, hostname); - if (! obj) - return NULL; - return SDB_SVC(obj); -} /* sdb_service_create */ - int -sdb_store_service(const sdb_service_t *svc) +sdb_store_service(const char *hostname, const char *name, + sdb_time_t last_update) { sdb_host_t *host; sdb_service_t *old; - sdb_time_t last_update; int status = 0; - if (! svc) + if ((! hostname) || (! name)) return -1; - last_update = svc->_last_update; if (last_update <= 0) last_update = sdb_gettime(); @@ -434,20 +419,18 @@ sdb_store_service(const sdb_service_t *svc) pthread_rwlock_wrlock(&host_lock); - host = SDB_HOST(sdb_llist_search_by_name(host_list, svc->hostname)); + host = SDB_HOST(sdb_llist_search_by_name(host_list, hostname)); if (! host) { pthread_rwlock_unlock(&host_lock); return -1; } - old = SDB_SVC(sdb_llist_search_by_name(host->services, - SDB_CONST_OBJ(svc)->name)); + old = SDB_SVC(sdb_llist_search_by_name(host->services, name)); if (old) { if (old->_last_update > last_update) { sdb_log(SDB_LOG_DEBUG, "store: Cannot update service " "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")", - svc->hostname, SDB_CONST_OBJ(svc)->name, last_update, - old->_last_update); + hostname, name, last_update, old->_last_update); status = 1; } else { @@ -455,7 +438,8 @@ sdb_store_service(const sdb_service_t *svc) } } else { - sdb_service_t *new = SDB_SVC(sdb_object_clone(SDB_CONST_OBJ(svc))); + sdb_service_t *new = SDB_SVC(sdb_object_create(name, sdb_service_type, + hostname)); if (! new) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "store: Failed to clone service " @@ -475,20 +459,6 @@ sdb_store_service(const sdb_service_t *svc) return status; } /* sdb_store_service */ -const sdb_service_t * -sdb_store_get_service(const sdb_host_t *host, const char *name) -{ - sdb_service_t *svc; - - if ((! host) || (! name)) - return NULL; - - svc = SDB_SVC(sdb_llist_search_by_name(host->services, name)); - if (! svc) - return NULL; - return svc; -} /* sdb_store_get_service */ - int sdb_store_dump(FILE *fh) {