2 * collectd - src/meta_data.c
3 * Copyright (C) 2008-2011 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
29 #include "meta_data.h"
37 int64_t mv_signed_int;
38 uint64_t mv_unsigned_int;
42 typedef union meta_value_u meta_value_t;
45 typedef struct meta_entry_s meta_entry_t;
61 static char *md_strdup(const char *orig) /* {{{ */
69 sz = strlen(orig) + 1;
74 memcpy(dest, orig, sz);
77 } /* }}} char *md_strdup */
79 static meta_entry_t *md_entry_alloc(const char *key) /* {{{ */
83 e = calloc(1, sizeof(*e));
85 ERROR("md_entry_alloc: calloc failed.");
89 e->key = md_strdup(key);
92 ERROR("md_entry_alloc: md_strdup failed.");
100 } /* }}} meta_entry_t *md_entry_alloc */
102 /* XXX: The lock on md must be held while calling this function! */
103 static meta_entry_t *md_entry_clone_contents(const meta_entry_t *orig) /* {{{ */
108 * - we do not check that orig != NULL here. You should have done it before.
109 * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR
113 copy = md_entry_alloc(orig->key);
116 copy->type = orig->type;
117 if (copy->type == MD_TYPE_STRING)
118 copy->value.mv_string = strdup(orig->value.mv_string);
120 copy->value = orig->value;
123 } /* }}} meta_entry_t *md_entry_clone_contents */
125 static meta_entry_t *md_entry_clone(const meta_entry_t *orig) /* {{{ */
132 copy = md_entry_clone_contents(orig);
134 copy->next = md_entry_clone(orig->next);
136 } /* }}} meta_entry_t *md_entry_clone */
138 static void md_entry_free(meta_entry_t *e) /* {{{ */
145 if (e->type == MD_TYPE_STRING)
146 free(e->value.mv_string);
149 md_entry_free(e->next);
152 } /* }}} void md_entry_free */
154 static int md_entry_insert(meta_data_t *md, meta_entry_t *e) /* {{{ */
159 if ((md == NULL) || (e == NULL))
162 pthread_mutex_lock(&md->lock);
166 while (this != NULL) {
167 if (strcasecmp(e->key, this->key) == 0)
175 /* This key does not exist yet. */
176 if (md->head == NULL)
179 assert(prev != NULL);
184 } else /* (this != NULL) */
191 e->next = this->next;
194 pthread_mutex_unlock(&md->lock);
202 } /* }}} int md_entry_insert */
204 /* XXX: The lock on md must be held while calling this function! */
205 static int md_entry_insert_clone(meta_data_t *md, meta_entry_t *orig) /* {{{ */
212 * - we do not check that md and e != NULL here. You should have done it
214 * - we do not use the lock. You should have set it before.
217 e = md_entry_clone_contents(orig);
221 while (this != NULL) {
222 if (strcasecmp(e->key, this->key) == 0)
230 /* This key does not exist yet. */
231 if (md->head == NULL)
234 assert(prev != NULL);
239 } else /* (this != NULL) */
246 e->next = this->next;
255 } /* }}} int md_entry_insert_clone */
257 /* XXX: The lock on md must be held while calling this function! */
258 static meta_entry_t *md_entry_lookup(meta_data_t *md, /* {{{ */
262 if ((md == NULL) || (key == NULL))
265 for (e = md->head; e != NULL; e = e->next)
266 if (strcasecmp(key, e->key) == 0)
270 } /* }}} meta_entry_t *md_entry_lookup */
273 * Each value_list_t*, as it is going through the system, is handled by exactly
274 * one thread. Plugins which pass a value_list_t* to another thread, e.g. the
275 * rrdtool plugin, must create a copy first. The meta data within a
276 * value_list_t* is not thread safe and doesn't need to be.
278 * The meta data associated with cache entries are a different story. There, we
279 * need to ensure exclusive locking to prevent leaks and other funky business.
280 * This is ensured by the uc_meta_data_get_*() functions.
286 meta_data_t *meta_data_create(void) /* {{{ */
290 md = calloc(1, sizeof(*md));
292 ERROR("meta_data_create: calloc failed.");
296 pthread_mutex_init(&md->lock, /* attr = */ NULL);
299 } /* }}} meta_data_t *meta_data_create */
301 meta_data_t *meta_data_clone(meta_data_t *orig) /* {{{ */
308 copy = meta_data_create();
312 pthread_mutex_lock(&orig->lock);
313 copy->head = md_entry_clone(orig->head);
314 pthread_mutex_unlock(&orig->lock);
317 } /* }}} meta_data_t *meta_data_clone */
319 int meta_data_clone_merge(meta_data_t **dest, meta_data_t *orig) /* {{{ */
325 *dest = meta_data_clone(orig);
329 pthread_mutex_lock(&orig->lock);
330 for (meta_entry_t *e = orig->head; e != NULL; e = e->next) {
331 md_entry_insert_clone((*dest), e);
333 pthread_mutex_unlock(&orig->lock);
336 } /* }}} int meta_data_clone_merge */
338 void meta_data_destroy(meta_data_t *md) /* {{{ */
343 md_entry_free(md->head);
344 pthread_mutex_destroy(&md->lock);
346 } /* }}} void meta_data_destroy */
348 int meta_data_exists(meta_data_t *md, const char *key) /* {{{ */
350 if ((md == NULL) || (key == NULL))
353 pthread_mutex_lock(&md->lock);
355 for (meta_entry_t *e = md->head; e != NULL; e = e->next) {
356 if (strcasecmp(key, e->key) == 0) {
357 pthread_mutex_unlock(&md->lock);
362 pthread_mutex_unlock(&md->lock);
364 } /* }}} int meta_data_exists */
366 int meta_data_type(meta_data_t *md, const char *key) /* {{{ */
368 if ((md == NULL) || (key == NULL))
371 pthread_mutex_lock(&md->lock);
373 for (meta_entry_t *e = md->head; e != NULL; e = e->next) {
374 if (strcasecmp(key, e->key) == 0) {
375 pthread_mutex_unlock(&md->lock);
380 pthread_mutex_unlock(&md->lock);
382 } /* }}} int meta_data_type */
384 int meta_data_toc(meta_data_t *md, char ***toc) /* {{{ */
386 int i = 0, count = 0;
388 if ((md == NULL) || (toc == NULL))
391 pthread_mutex_lock(&md->lock);
393 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
397 pthread_mutex_unlock(&md->lock);
401 *toc = calloc(count, sizeof(**toc));
402 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
403 (*toc)[i++] = strdup(e->key);
405 pthread_mutex_unlock(&md->lock);
407 } /* }}} int meta_data_toc */
409 int meta_data_delete(meta_data_t *md, const char *key) /* {{{ */
414 if ((md == NULL) || (key == NULL))
417 pthread_mutex_lock(&md->lock);
421 while (this != NULL) {
422 if (strcasecmp(key, this->key) == 0)
430 pthread_mutex_unlock(&md->lock);
435 md->head = this->next;
437 prev->next = this->next;
439 pthread_mutex_unlock(&md->lock);
445 } /* }}} int meta_data_delete */
450 int meta_data_add_string(meta_data_t *md, /* {{{ */
451 const char *key, const char *value) {
454 if ((md == NULL) || (key == NULL) || (value == NULL))
457 e = md_entry_alloc(key);
461 e->value.mv_string = md_strdup(value);
462 if (e->value.mv_string == NULL) {
463 ERROR("meta_data_add_string: md_strdup failed.");
467 e->type = MD_TYPE_STRING;
469 return (md_entry_insert(md, e));
470 } /* }}} int meta_data_add_string */
472 int meta_data_add_signed_int(meta_data_t *md, /* {{{ */
473 const char *key, int64_t value) {
476 if ((md == NULL) || (key == NULL))
479 e = md_entry_alloc(key);
483 e->value.mv_signed_int = value;
484 e->type = MD_TYPE_SIGNED_INT;
486 return (md_entry_insert(md, e));
487 } /* }}} int meta_data_add_signed_int */
489 int meta_data_add_unsigned_int(meta_data_t *md, /* {{{ */
490 const char *key, uint64_t value) {
493 if ((md == NULL) || (key == NULL))
496 e = md_entry_alloc(key);
500 e->value.mv_unsigned_int = value;
501 e->type = MD_TYPE_UNSIGNED_INT;
503 return (md_entry_insert(md, e));
504 } /* }}} int meta_data_add_unsigned_int */
506 int meta_data_add_double(meta_data_t *md, /* {{{ */
507 const char *key, double value) {
510 if ((md == NULL) || (key == NULL))
513 e = md_entry_alloc(key);
517 e->value.mv_double = value;
518 e->type = MD_TYPE_DOUBLE;
520 return (md_entry_insert(md, e));
521 } /* }}} int meta_data_add_double */
523 int meta_data_add_boolean(meta_data_t *md, /* {{{ */
524 const char *key, _Bool value) {
527 if ((md == NULL) || (key == NULL))
530 e = md_entry_alloc(key);
534 e->value.mv_boolean = value;
535 e->type = MD_TYPE_BOOLEAN;
537 return (md_entry_insert(md, e));
538 } /* }}} int meta_data_add_boolean */
543 int meta_data_get_string(meta_data_t *md, /* {{{ */
544 const char *key, char **value) {
548 if ((md == NULL) || (key == NULL) || (value == NULL))
551 pthread_mutex_lock(&md->lock);
553 e = md_entry_lookup(md, key);
555 pthread_mutex_unlock(&md->lock);
559 if (e->type != MD_TYPE_STRING) {
560 ERROR("meta_data_get_string: Type mismatch for key `%s'", e->key);
561 pthread_mutex_unlock(&md->lock);
565 temp = md_strdup(e->value.mv_string);
567 pthread_mutex_unlock(&md->lock);
568 ERROR("meta_data_get_string: md_strdup failed.");
572 pthread_mutex_unlock(&md->lock);
577 } /* }}} int meta_data_get_string */
579 int meta_data_get_signed_int(meta_data_t *md, /* {{{ */
580 const char *key, int64_t *value) {
583 if ((md == NULL) || (key == NULL) || (value == NULL))
586 pthread_mutex_lock(&md->lock);
588 e = md_entry_lookup(md, key);
590 pthread_mutex_unlock(&md->lock);
594 if (e->type != MD_TYPE_SIGNED_INT) {
595 ERROR("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
596 pthread_mutex_unlock(&md->lock);
600 *value = e->value.mv_signed_int;
602 pthread_mutex_unlock(&md->lock);
604 } /* }}} int meta_data_get_signed_int */
606 int meta_data_get_unsigned_int(meta_data_t *md, /* {{{ */
607 const char *key, uint64_t *value) {
610 if ((md == NULL) || (key == NULL) || (value == NULL))
613 pthread_mutex_lock(&md->lock);
615 e = md_entry_lookup(md, key);
617 pthread_mutex_unlock(&md->lock);
621 if (e->type != MD_TYPE_UNSIGNED_INT) {
622 ERROR("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
623 pthread_mutex_unlock(&md->lock);
627 *value = e->value.mv_unsigned_int;
629 pthread_mutex_unlock(&md->lock);
631 } /* }}} int meta_data_get_unsigned_int */
633 int meta_data_get_double(meta_data_t *md, /* {{{ */
634 const char *key, double *value) {
637 if ((md == NULL) || (key == NULL) || (value == NULL))
640 pthread_mutex_lock(&md->lock);
642 e = md_entry_lookup(md, key);
644 pthread_mutex_unlock(&md->lock);
648 if (e->type != MD_TYPE_DOUBLE) {
649 ERROR("meta_data_get_double: Type mismatch for key `%s'", e->key);
650 pthread_mutex_unlock(&md->lock);
654 *value = e->value.mv_double;
656 pthread_mutex_unlock(&md->lock);
658 } /* }}} int meta_data_get_double */
660 int meta_data_get_boolean(meta_data_t *md, /* {{{ */
661 const char *key, _Bool *value) {
664 if ((md == NULL) || (key == NULL) || (value == NULL))
667 pthread_mutex_lock(&md->lock);
669 e = md_entry_lookup(md, key);
671 pthread_mutex_unlock(&md->lock);
675 if (e->type != MD_TYPE_BOOLEAN) {
676 ERROR("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
677 pthread_mutex_unlock(&md->lock);
681 *value = e->value.mv_boolean;
683 pthread_mutex_unlock(&md->lock);
685 } /* }}} int meta_data_get_boolean */
687 /* vim: set sw=2 sts=2 et fdm=marker : */