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>
31 #include "meta_data.h"
33 #define MD_MAX_NONSTRING_CHARS 128
41 int64_t mv_signed_int;
42 uint64_t mv_unsigned_int;
46 typedef union meta_value_u meta_value_t;
49 typedef struct meta_entry_s meta_entry_t;
67 static char *md_strdup (const char *orig) /* {{{ */
75 sz = strlen (orig) + 1;
80 memcpy (dest, orig, sz);
83 } /* }}} char *md_strdup */
85 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
89 e = calloc (1, sizeof (*e));
92 ERROR ("md_entry_alloc: calloc failed.");
96 e->key = md_strdup (key);
100 ERROR ("md_entry_alloc: md_strdup failed.");
108 } /* }}} meta_entry_t *md_entry_alloc */
110 /* XXX: The lock on md must be held while calling this function! */
111 static meta_entry_t *md_entry_clone_contents (const meta_entry_t *orig) /* {{{ */
116 * - we do not check that orig != NULL here. You should have done it before.
117 * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR FUNCTION
120 copy = md_entry_alloc (orig->key);
123 copy->type = orig->type;
124 if (copy->type == MD_TYPE_STRING)
125 copy->value.mv_string = strdup (orig->value.mv_string);
127 copy->value = orig->value;
130 } /* }}} meta_entry_t *md_entry_clone_contents */
132 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
139 copy = md_entry_clone_contents(orig);
141 copy->next = md_entry_clone (orig->next);
143 } /* }}} meta_entry_t *md_entry_clone */
145 static void md_entry_free (meta_entry_t *e) /* {{{ */
152 if (e->type == MD_TYPE_STRING)
153 free (e->value.mv_string);
156 md_entry_free (e->next);
159 } /* }}} void md_entry_free */
161 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
166 if ((md == NULL) || (e == NULL))
169 pthread_mutex_lock (&md->lock);
175 if (strcasecmp (e->key, this->key) == 0)
184 /* This key does not exist yet. */
185 if (md->head == NULL)
189 assert (prev != NULL);
195 else /* (this != NULL) */
202 e->next = this->next;
205 pthread_mutex_unlock (&md->lock);
210 md_entry_free (this);
214 } /* }}} int md_entry_insert */
216 /* XXX: The lock on md must be held while calling this function! */
217 static int md_entry_insert_clone (meta_data_t *md, meta_entry_t *orig) /* {{{ */
224 * - we do not check that md and e != NULL here. You should have done it before.
225 * - we do not use the lock. You should have set it before.
228 e = md_entry_clone_contents(orig);
234 if (strcasecmp (e->key, this->key) == 0)
243 /* This key does not exist yet. */
244 if (md->head == NULL)
248 assert (prev != NULL);
254 else /* (this != NULL) */
261 e->next = this->next;
267 md_entry_free (this);
271 } /* }}} int md_entry_insert_clone */
273 /* XXX: The lock on md must be held while calling this function! */
274 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
279 if ((md == NULL) || (key == NULL))
282 for (e = md->head; e != NULL; e = e->next)
283 if (strcasecmp (key, e->key) == 0)
287 } /* }}} meta_entry_t *md_entry_lookup */
290 * Each value_list_t*, as it is going through the system, is handled by exactly
291 * one thread. Plugins which pass a value_list_t* to another thread, e.g. the
292 * rrdtool plugin, must create a copy first. The meta data within a
293 * value_list_t* is not thread safe and doesn't need to be.
295 * The meta data associated with cache entries are a different story. There, we
296 * need to ensure exclusive locking to prevent leaks and other funky business.
297 * This is ensured by the uc_meta_data_get_*() functions.
303 meta_data_t *meta_data_create (void) /* {{{ */
307 md = calloc (1, sizeof (*md));
310 ERROR ("meta_data_create: calloc failed.");
314 pthread_mutex_init (&md->lock, /* attr = */ NULL);
317 } /* }}} meta_data_t *meta_data_create */
319 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
326 copy = meta_data_create ();
330 pthread_mutex_lock (&orig->lock);
331 copy->head = md_entry_clone (orig->head);
332 pthread_mutex_unlock (&orig->lock);
335 } /* }}} meta_data_t *meta_data_clone */
337 int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */
343 *dest = meta_data_clone(orig);
347 pthread_mutex_lock (&orig->lock);
348 for (meta_entry_t *e=orig->head; e != NULL; e = e->next)
350 md_entry_insert_clone((*dest), e);
352 pthread_mutex_unlock (&orig->lock);
355 } /* }}} int meta_data_clone_merge */
357 void meta_data_destroy (meta_data_t *md) /* {{{ */
362 md_entry_free (md->head);
363 pthread_mutex_destroy (&md->lock);
365 } /* }}} void meta_data_destroy */
367 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
369 if ((md == NULL) || (key == NULL))
372 pthread_mutex_lock (&md->lock);
374 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
376 if (strcasecmp (key, e->key) == 0)
378 pthread_mutex_unlock (&md->lock);
383 pthread_mutex_unlock (&md->lock);
385 } /* }}} int meta_data_exists */
387 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
389 if ((md == NULL) || (key == NULL))
392 pthread_mutex_lock (&md->lock);
394 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
396 if (strcasecmp (key, e->key) == 0)
398 pthread_mutex_unlock (&md->lock);
403 pthread_mutex_unlock (&md->lock);
405 } /* }}} int meta_data_type */
407 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
409 int i = 0, count = 0;
411 if ((md == NULL) || (toc == NULL))
414 pthread_mutex_lock (&md->lock);
416 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
421 pthread_mutex_unlock (&md->lock);
425 *toc = calloc(count, sizeof(**toc));
426 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
427 (*toc)[i++] = strdup(e->key);
429 pthread_mutex_unlock (&md->lock);
431 } /* }}} int meta_data_toc */
433 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
438 if ((md == NULL) || (key == NULL))
441 pthread_mutex_lock (&md->lock);
447 if (strcasecmp (key, this->key) == 0)
456 pthread_mutex_unlock (&md->lock);
461 md->head = this->next;
463 prev->next = this->next;
465 pthread_mutex_unlock (&md->lock);
468 md_entry_free (this);
471 } /* }}} int meta_data_delete */
476 int meta_data_add_string (meta_data_t *md, /* {{{ */
477 const char *key, const char *value)
481 if ((md == NULL) || (key == NULL) || (value == NULL))
484 e = md_entry_alloc (key);
488 e->value.mv_string = md_strdup (value);
489 if (e->value.mv_string == NULL)
491 ERROR ("meta_data_add_string: md_strdup failed.");
495 e->type = MD_TYPE_STRING;
497 return (md_entry_insert (md, e));
498 } /* }}} int meta_data_add_string */
500 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
501 const char *key, int64_t value)
505 if ((md == NULL) || (key == NULL))
508 e = md_entry_alloc (key);
512 e->value.mv_signed_int = value;
513 e->type = MD_TYPE_SIGNED_INT;
515 return (md_entry_insert (md, e));
516 } /* }}} int meta_data_add_signed_int */
518 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
519 const char *key, uint64_t value)
523 if ((md == NULL) || (key == NULL))
526 e = md_entry_alloc (key);
530 e->value.mv_unsigned_int = value;
531 e->type = MD_TYPE_UNSIGNED_INT;
533 return (md_entry_insert (md, e));
534 } /* }}} int meta_data_add_unsigned_int */
536 int meta_data_add_double (meta_data_t *md, /* {{{ */
537 const char *key, double value)
541 if ((md == NULL) || (key == NULL))
544 e = md_entry_alloc (key);
548 e->value.mv_double = value;
549 e->type = MD_TYPE_DOUBLE;
551 return (md_entry_insert (md, e));
552 } /* }}} int meta_data_add_double */
554 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
555 const char *key, _Bool value)
559 if ((md == NULL) || (key == NULL))
562 e = md_entry_alloc (key);
566 e->value.mv_boolean = value;
567 e->type = MD_TYPE_BOOLEAN;
569 return (md_entry_insert (md, e));
570 } /* }}} int meta_data_add_boolean */
575 int meta_data_get_string (meta_data_t *md, /* {{{ */
576 const char *key, char **value)
581 if ((md == NULL) || (key == NULL) || (value == NULL))
584 pthread_mutex_lock (&md->lock);
586 e = md_entry_lookup (md, key);
589 pthread_mutex_unlock (&md->lock);
593 if (e->type != MD_TYPE_STRING)
595 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
596 pthread_mutex_unlock (&md->lock);
600 temp = md_strdup (e->value.mv_string);
603 pthread_mutex_unlock (&md->lock);
604 ERROR ("meta_data_get_string: md_strdup failed.");
608 pthread_mutex_unlock (&md->lock);
613 } /* }}} int meta_data_get_string */
615 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
616 const char *key, int64_t *value)
620 if ((md == NULL) || (key == NULL) || (value == NULL))
623 pthread_mutex_lock (&md->lock);
625 e = md_entry_lookup (md, key);
628 pthread_mutex_unlock (&md->lock);
632 if (e->type != MD_TYPE_SIGNED_INT)
634 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
635 pthread_mutex_unlock (&md->lock);
639 *value = e->value.mv_signed_int;
641 pthread_mutex_unlock (&md->lock);
643 } /* }}} int meta_data_get_signed_int */
645 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
646 const char *key, uint64_t *value)
650 if ((md == NULL) || (key == NULL) || (value == NULL))
653 pthread_mutex_lock (&md->lock);
655 e = md_entry_lookup (md, key);
658 pthread_mutex_unlock (&md->lock);
662 if (e->type != MD_TYPE_UNSIGNED_INT)
664 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
665 pthread_mutex_unlock (&md->lock);
669 *value = e->value.mv_unsigned_int;
671 pthread_mutex_unlock (&md->lock);
673 } /* }}} int meta_data_get_unsigned_int */
675 int meta_data_get_double (meta_data_t *md, /* {{{ */
676 const char *key, double *value)
680 if ((md == NULL) || (key == NULL) || (value == NULL))
683 pthread_mutex_lock (&md->lock);
685 e = md_entry_lookup (md, key);
688 pthread_mutex_unlock (&md->lock);
692 if (e->type != MD_TYPE_DOUBLE)
694 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
695 pthread_mutex_unlock (&md->lock);
699 *value = e->value.mv_double;
701 pthread_mutex_unlock (&md->lock);
703 } /* }}} int meta_data_get_double */
705 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
706 const char *key, _Bool *value)
710 if ((md == NULL) || (key == NULL) || (value == NULL))
713 pthread_mutex_lock (&md->lock);
715 e = md_entry_lookup (md, key);
718 pthread_mutex_unlock (&md->lock);
722 if (e->type != MD_TYPE_BOOLEAN)
724 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
725 pthread_mutex_unlock (&md->lock);
729 *value = e->value.mv_boolean;
731 pthread_mutex_unlock (&md->lock);
733 } /* }}} int meta_data_get_boolean */
735 int meta_data_as_string (meta_data_t *md, /* {{{ */
736 const char *key, char **value)
740 char buffer[MD_MAX_NONSTRING_CHARS]; /* For non-string types. */
744 if ((md == NULL) || (key == NULL) || (value == NULL))
747 pthread_mutex_lock (&md->lock);
749 e = md_entry_lookup (md, key);
752 pthread_mutex_unlock (&md->lock);
761 actual = e->value.mv_string;
763 case MD_TYPE_SIGNED_INT:
764 ssnprintf (buffer, sizeof (buffer), "%"PRIi64, e->value.mv_signed_int);
767 case MD_TYPE_UNSIGNED_INT:
768 ssnprintf (buffer, sizeof (buffer), "%"PRIu64, e->value.mv_unsigned_int);
772 ssnprintf (buffer, sizeof (buffer), GAUGE_FORMAT, e->value.mv_double);
775 case MD_TYPE_BOOLEAN:
776 actual = e->value.mv_boolean ? "true" : "false";
779 pthread_mutex_unlock (&md->lock);
780 ERROR ("meta_data_as_string: unknown type %d for key `%s'", type, key);
784 pthread_mutex_unlock (&md->lock);
786 temp = md_strdup (actual);
789 pthread_mutex_unlock (&md->lock);
790 ERROR ("meta_data_as_string: md_strdup failed for key `%s'.", key);
797 } /* }}} int meta_data_as_string */
799 /* vim: set sw=2 sts=2 et fdm=marker : */