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>
30 #include "meta_data.h"
33 #define MD_MAX_NONSTRING_CHARS 128
40 int64_t mv_signed_int;
41 uint64_t mv_unsigned_int;
45 typedef union meta_value_u meta_value_t;
48 typedef struct meta_entry_s meta_entry_t;
64 static char *md_strdup(const char *orig) /* {{{ */
72 sz = strlen(orig) + 1;
77 memcpy(dest, orig, sz);
80 } /* }}} char *md_strdup */
82 static meta_entry_t *md_entry_alloc(const char *key) /* {{{ */
86 e = calloc(1, sizeof(*e));
88 ERROR("md_entry_alloc: calloc failed.");
92 e->key = md_strdup(key);
95 ERROR("md_entry_alloc: md_strdup failed.");
103 } /* }}} meta_entry_t *md_entry_alloc */
105 /* XXX: The lock on md must be held while calling this function! */
106 static meta_entry_t *md_entry_clone_contents(const meta_entry_t *orig) /* {{{ */
111 * - we do not check that orig != NULL here. You should have done it before.
112 * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR
116 copy = md_entry_alloc(orig->key);
119 copy->type = orig->type;
120 if (copy->type == MD_TYPE_STRING)
121 copy->value.mv_string = strdup(orig->value.mv_string);
123 copy->value = orig->value;
126 } /* }}} meta_entry_t *md_entry_clone_contents */
128 static meta_entry_t *md_entry_clone(const meta_entry_t *orig) /* {{{ */
135 copy = md_entry_clone_contents(orig);
137 copy->next = md_entry_clone(orig->next);
139 } /* }}} meta_entry_t *md_entry_clone */
141 static void md_entry_free(meta_entry_t *e) /* {{{ */
148 if (e->type == MD_TYPE_STRING)
149 free(e->value.mv_string);
152 md_entry_free(e->next);
155 } /* }}} void md_entry_free */
157 static int md_entry_insert(meta_data_t *md, meta_entry_t *e) /* {{{ */
162 if ((md == NULL) || (e == NULL))
165 pthread_mutex_lock(&md->lock);
169 while (this != NULL) {
170 if (strcasecmp(e->key, this->key) == 0)
178 /* This key does not exist yet. */
179 if (md->head == NULL)
182 assert(prev != NULL);
187 } else /* (this != NULL) */
194 e->next = this->next;
197 pthread_mutex_unlock(&md->lock);
205 } /* }}} int md_entry_insert */
207 /* XXX: The lock on md must be held while calling this function! */
208 static int md_entry_insert_clone(meta_data_t *md, meta_entry_t *orig) /* {{{ */
215 * - we do not check that md and e != NULL here. You should have done it
217 * - we do not use the lock. You should have set it before.
220 e = md_entry_clone_contents(orig);
224 while (this != NULL) {
225 if (strcasecmp(e->key, this->key) == 0)
233 /* This key does not exist yet. */
234 if (md->head == NULL)
237 assert(prev != NULL);
242 } else /* (this != NULL) */
249 e->next = this->next;
258 } /* }}} int md_entry_insert_clone */
260 /* XXX: The lock on md must be held while calling this function! */
261 static meta_entry_t *md_entry_lookup(meta_data_t *md, /* {{{ */
265 if ((md == NULL) || (key == NULL))
268 for (e = md->head; e != NULL; e = e->next)
269 if (strcasecmp(key, e->key) == 0)
273 } /* }}} meta_entry_t *md_entry_lookup */
276 * Each value_list_t*, as it is going through the system, is handled by exactly
277 * one thread. Plugins which pass a value_list_t* to another thread, e.g. the
278 * rrdtool plugin, must create a copy first. The meta data within a
279 * value_list_t* is not thread safe and doesn't need to be.
281 * The meta data associated with cache entries are a different story. There, we
282 * need to ensure exclusive locking to prevent leaks and other funky business.
283 * This is ensured by the uc_meta_data_get_*() functions.
289 meta_data_t *meta_data_create(void) /* {{{ */
293 md = calloc(1, sizeof(*md));
295 ERROR("meta_data_create: calloc failed.");
299 pthread_mutex_init(&md->lock, /* attr = */ NULL);
302 } /* }}} meta_data_t *meta_data_create */
304 meta_data_t *meta_data_clone(meta_data_t *orig) /* {{{ */
311 copy = meta_data_create();
315 pthread_mutex_lock(&orig->lock);
316 copy->head = md_entry_clone(orig->head);
317 pthread_mutex_unlock(&orig->lock);
320 } /* }}} meta_data_t *meta_data_clone */
322 int meta_data_clone_merge(meta_data_t **dest, meta_data_t *orig) /* {{{ */
328 *dest = meta_data_clone(orig);
332 pthread_mutex_lock(&orig->lock);
333 for (meta_entry_t *e = orig->head; e != NULL; e = e->next) {
334 md_entry_insert_clone((*dest), e);
336 pthread_mutex_unlock(&orig->lock);
339 } /* }}} int meta_data_clone_merge */
341 void meta_data_destroy(meta_data_t *md) /* {{{ */
346 md_entry_free(md->head);
347 pthread_mutex_destroy(&md->lock);
349 } /* }}} void meta_data_destroy */
351 int meta_data_exists(meta_data_t *md, const char *key) /* {{{ */
353 if ((md == NULL) || (key == NULL))
356 pthread_mutex_lock(&md->lock);
358 for (meta_entry_t *e = md->head; e != NULL; e = e->next) {
359 if (strcasecmp(key, e->key) == 0) {
360 pthread_mutex_unlock(&md->lock);
365 pthread_mutex_unlock(&md->lock);
367 } /* }}} int meta_data_exists */
369 int meta_data_type(meta_data_t *md, const char *key) /* {{{ */
371 if ((md == NULL) || (key == NULL))
374 pthread_mutex_lock(&md->lock);
376 for (meta_entry_t *e = md->head; e != NULL; e = e->next) {
377 if (strcasecmp(key, e->key) == 0) {
378 pthread_mutex_unlock(&md->lock);
383 pthread_mutex_unlock(&md->lock);
385 } /* }}} int meta_data_type */
387 int meta_data_toc(meta_data_t *md, char ***toc) /* {{{ */
389 int i = 0, count = 0;
391 if ((md == NULL) || (toc == NULL))
394 pthread_mutex_lock(&md->lock);
396 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
400 pthread_mutex_unlock(&md->lock);
404 *toc = calloc(count, sizeof(**toc));
405 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
406 (*toc)[i++] = strdup(e->key);
408 pthread_mutex_unlock(&md->lock);
410 } /* }}} int meta_data_toc */
412 int meta_data_delete(meta_data_t *md, const char *key) /* {{{ */
417 if ((md == NULL) || (key == NULL))
420 pthread_mutex_lock(&md->lock);
424 while (this != NULL) {
425 if (strcasecmp(key, this->key) == 0)
433 pthread_mutex_unlock(&md->lock);
438 md->head = this->next;
440 prev->next = this->next;
442 pthread_mutex_unlock(&md->lock);
448 } /* }}} int meta_data_delete */
453 int meta_data_add_string(meta_data_t *md, /* {{{ */
454 const char *key, const char *value) {
457 if ((md == NULL) || (key == NULL) || (value == NULL))
460 e = md_entry_alloc(key);
464 e->value.mv_string = md_strdup(value);
465 if (e->value.mv_string == NULL) {
466 ERROR("meta_data_add_string: md_strdup failed.");
470 e->type = MD_TYPE_STRING;
472 return md_entry_insert(md, e);
473 } /* }}} int meta_data_add_string */
475 int meta_data_add_signed_int(meta_data_t *md, /* {{{ */
476 const char *key, int64_t value) {
479 if ((md == NULL) || (key == NULL))
482 e = md_entry_alloc(key);
486 e->value.mv_signed_int = value;
487 e->type = MD_TYPE_SIGNED_INT;
489 return md_entry_insert(md, e);
490 } /* }}} int meta_data_add_signed_int */
492 int meta_data_add_unsigned_int(meta_data_t *md, /* {{{ */
493 const char *key, uint64_t value) {
496 if ((md == NULL) || (key == NULL))
499 e = md_entry_alloc(key);
503 e->value.mv_unsigned_int = value;
504 e->type = MD_TYPE_UNSIGNED_INT;
506 return md_entry_insert(md, e);
507 } /* }}} int meta_data_add_unsigned_int */
509 int meta_data_add_double(meta_data_t *md, /* {{{ */
510 const char *key, double value) {
513 if ((md == NULL) || (key == NULL))
516 e = md_entry_alloc(key);
520 e->value.mv_double = value;
521 e->type = MD_TYPE_DOUBLE;
523 return md_entry_insert(md, e);
524 } /* }}} int meta_data_add_double */
526 int meta_data_add_boolean(meta_data_t *md, /* {{{ */
527 const char *key, _Bool value) {
530 if ((md == NULL) || (key == NULL))
533 e = md_entry_alloc(key);
537 e->value.mv_boolean = value;
538 e->type = MD_TYPE_BOOLEAN;
540 return md_entry_insert(md, e);
541 } /* }}} int meta_data_add_boolean */
546 int meta_data_get_string(meta_data_t *md, /* {{{ */
547 const char *key, char **value) {
551 if ((md == NULL) || (key == NULL) || (value == NULL))
554 pthread_mutex_lock(&md->lock);
556 e = md_entry_lookup(md, key);
558 pthread_mutex_unlock(&md->lock);
562 if (e->type != MD_TYPE_STRING) {
563 ERROR("meta_data_get_string: Type mismatch for key `%s'", e->key);
564 pthread_mutex_unlock(&md->lock);
568 temp = md_strdup(e->value.mv_string);
570 pthread_mutex_unlock(&md->lock);
571 ERROR("meta_data_get_string: md_strdup failed.");
575 pthread_mutex_unlock(&md->lock);
580 } /* }}} int meta_data_get_string */
582 int meta_data_get_signed_int(meta_data_t *md, /* {{{ */
583 const char *key, int64_t *value) {
586 if ((md == NULL) || (key == NULL) || (value == NULL))
589 pthread_mutex_lock(&md->lock);
591 e = md_entry_lookup(md, key);
593 pthread_mutex_unlock(&md->lock);
597 if (e->type != MD_TYPE_SIGNED_INT) {
598 ERROR("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
599 pthread_mutex_unlock(&md->lock);
603 *value = e->value.mv_signed_int;
605 pthread_mutex_unlock(&md->lock);
607 } /* }}} int meta_data_get_signed_int */
609 int meta_data_get_unsigned_int(meta_data_t *md, /* {{{ */
610 const char *key, uint64_t *value) {
613 if ((md == NULL) || (key == NULL) || (value == NULL))
616 pthread_mutex_lock(&md->lock);
618 e = md_entry_lookup(md, key);
620 pthread_mutex_unlock(&md->lock);
624 if (e->type != MD_TYPE_UNSIGNED_INT) {
625 ERROR("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
626 pthread_mutex_unlock(&md->lock);
630 *value = e->value.mv_unsigned_int;
632 pthread_mutex_unlock(&md->lock);
634 } /* }}} int meta_data_get_unsigned_int */
636 int meta_data_get_double(meta_data_t *md, /* {{{ */
637 const char *key, double *value) {
640 if ((md == NULL) || (key == NULL) || (value == NULL))
643 pthread_mutex_lock(&md->lock);
645 e = md_entry_lookup(md, key);
647 pthread_mutex_unlock(&md->lock);
651 if (e->type != MD_TYPE_DOUBLE) {
652 ERROR("meta_data_get_double: Type mismatch for key `%s'", e->key);
653 pthread_mutex_unlock(&md->lock);
657 *value = e->value.mv_double;
659 pthread_mutex_unlock(&md->lock);
661 } /* }}} int meta_data_get_double */
663 int meta_data_get_boolean(meta_data_t *md, /* {{{ */
664 const char *key, _Bool *value) {
667 if ((md == NULL) || (key == NULL) || (value == NULL))
670 pthread_mutex_lock(&md->lock);
672 e = md_entry_lookup(md, key);
674 pthread_mutex_unlock(&md->lock);
678 if (e->type != MD_TYPE_BOOLEAN) {
679 ERROR("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
680 pthread_mutex_unlock(&md->lock);
684 *value = e->value.mv_boolean;
686 pthread_mutex_unlock(&md->lock);
688 } /* }}} int meta_data_get_boolean */
690 int meta_data_as_string(meta_data_t *md, /* {{{ */
691 const char *key, char **value) {
694 char buffer[MD_MAX_NONSTRING_CHARS]; /* For non-string types. */
698 if ((md == NULL) || (key == NULL) || (value == NULL))
701 pthread_mutex_lock(&md->lock);
703 e = md_entry_lookup(md, key);
705 pthread_mutex_unlock(&md->lock);
713 actual = e->value.mv_string;
715 case MD_TYPE_SIGNED_INT:
716 snprintf(buffer, sizeof(buffer), "%" PRIi64, e->value.mv_signed_int);
719 case MD_TYPE_UNSIGNED_INT:
720 snprintf(buffer, sizeof(buffer), "%" PRIu64, e->value.mv_unsigned_int);
724 snprintf(buffer, sizeof(buffer), GAUGE_FORMAT, e->value.mv_double);
727 case MD_TYPE_BOOLEAN:
728 actual = e->value.mv_boolean ? "true" : "false";
731 pthread_mutex_unlock(&md->lock);
732 ERROR("meta_data_as_string: unknown type %d for key `%s'", type, key);
736 pthread_mutex_unlock(&md->lock);
738 temp = md_strdup(actual);
740 pthread_mutex_unlock(&md->lock);
741 ERROR("meta_data_as_string: md_strdup failed for key `%s'.", key);
748 } /* }}} int meta_data_as_string */