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"
38 int64_t mv_signed_int;
39 uint64_t mv_unsigned_int;
43 typedef union meta_value_u meta_value_t;
46 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));
89 ERROR ("md_entry_alloc: calloc failed.");
93 e->key = md_strdup (key);
97 ERROR ("md_entry_alloc: md_strdup failed.");
105 } /* }}} meta_entry_t *md_entry_alloc */
107 /* XXX: The lock on md must be held while calling this function! */
108 static meta_entry_t *md_entry_clone_contents (const meta_entry_t *orig) /* {{{ */
113 * - we do not check that orig != NULL here. You should have done it before.
114 * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR FUNCTION
117 copy = md_entry_alloc (orig->key);
120 copy->type = orig->type;
121 if (copy->type == MD_TYPE_STRING)
122 copy->value.mv_string = strdup (orig->value.mv_string);
124 copy->value = orig->value;
127 } /* }}} meta_entry_t *md_entry_clone_contents */
129 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
136 copy = md_entry_clone_contents(orig);
138 copy->next = md_entry_clone (orig->next);
140 } /* }}} meta_entry_t *md_entry_clone */
142 static void md_entry_free (meta_entry_t *e) /* {{{ */
149 if (e->type == MD_TYPE_STRING)
150 free (e->value.mv_string);
153 md_entry_free (e->next);
156 } /* }}} void md_entry_free */
158 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
163 if ((md == NULL) || (e == NULL))
166 pthread_mutex_lock (&md->lock);
172 if (strcasecmp (e->key, this->key) == 0)
181 /* This key does not exist yet. */
182 if (md->head == NULL)
186 assert (prev != NULL);
192 else /* (this != NULL) */
199 e->next = this->next;
202 pthread_mutex_unlock (&md->lock);
207 md_entry_free (this);
211 } /* }}} int md_entry_insert */
213 /* XXX: The lock on md must be held while calling this function! */
214 static int md_entry_insert_clone (meta_data_t *md, meta_entry_t *orig) /* {{{ */
221 * - we do not check that md and e != NULL here. You should have done it before.
222 * - we do not use the lock. You should have set it before.
225 e = md_entry_clone_contents(orig);
231 if (strcasecmp (e->key, this->key) == 0)
240 /* This key does not exist yet. */
241 if (md->head == NULL)
245 assert (prev != NULL);
251 else /* (this != NULL) */
258 e->next = this->next;
264 md_entry_free (this);
268 } /* }}} int md_entry_insert_clone */
270 /* XXX: The lock on md must be held while calling this function! */
271 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
276 if ((md == NULL) || (key == NULL))
279 for (e = md->head; e != NULL; e = e->next)
280 if (strcasecmp (key, e->key) == 0)
284 } /* }}} meta_entry_t *md_entry_lookup */
287 * Each value_list_t*, as it is going through the system, is handled by exactly
288 * one thread. Plugins which pass a value_list_t* to another thread, e.g. the
289 * rrdtool plugin, must create a copy first. The meta data within a
290 * value_list_t* is not thread safe and doesn't need to be.
292 * The meta data associated with cache entries are a different story. There, we
293 * need to ensure exclusive locking to prevent leaks and other funky business.
294 * This is ensured by the uc_meta_data_get_*() functions.
300 meta_data_t *meta_data_create (void) /* {{{ */
304 md = calloc (1, sizeof (*md));
307 ERROR ("meta_data_create: calloc failed.");
311 pthread_mutex_init (&md->lock, /* attr = */ NULL);
314 } /* }}} meta_data_t *meta_data_create */
316 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
323 copy = meta_data_create ();
327 pthread_mutex_lock (&orig->lock);
328 copy->head = md_entry_clone (orig->head);
329 pthread_mutex_unlock (&orig->lock);
332 } /* }}} meta_data_t *meta_data_clone */
334 int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */
342 *dest = meta_data_clone(orig);
346 pthread_mutex_lock (&orig->lock);
347 for (e=orig->head; e != NULL; e = e->next)
349 md_entry_insert_clone((*dest), e);
351 pthread_mutex_unlock (&orig->lock);
354 } /* }}} int meta_data_clone_merge */
356 void meta_data_destroy (meta_data_t *md) /* {{{ */
361 md_entry_free (md->head);
362 pthread_mutex_destroy (&md->lock);
364 } /* }}} void meta_data_destroy */
366 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
370 if ((md == NULL) || (key == NULL))
373 pthread_mutex_lock (&md->lock);
375 for (e = md->head; e != NULL; e = e->next)
377 if (strcasecmp (key, e->key) == 0)
379 pthread_mutex_unlock (&md->lock);
384 pthread_mutex_unlock (&md->lock);
386 } /* }}} int meta_data_exists */
388 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
392 if ((md == NULL) || (key == NULL))
395 pthread_mutex_lock (&md->lock);
397 for (e = md->head; e != NULL; e = e->next)
399 if (strcasecmp (key, e->key) == 0)
401 pthread_mutex_unlock (&md->lock);
406 pthread_mutex_unlock (&md->lock);
408 } /* }}} int meta_data_type */
410 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
412 int i = 0, count = 0;
415 if ((md == NULL) || (toc == NULL))
418 pthread_mutex_lock (&md->lock);
420 for (e = md->head; e != NULL; e = e->next)
425 pthread_mutex_unlock (&md->lock);
429 *toc = calloc(count, sizeof(**toc));
430 for (e = md->head; e != NULL; e = e->next)
431 (*toc)[i++] = strdup(e->key);
433 pthread_mutex_unlock (&md->lock);
435 } /* }}} int meta_data_toc */
437 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
442 if ((md == NULL) || (key == NULL))
445 pthread_mutex_lock (&md->lock);
451 if (strcasecmp (key, this->key) == 0)
460 pthread_mutex_unlock (&md->lock);
465 md->head = this->next;
467 prev->next = this->next;
469 pthread_mutex_unlock (&md->lock);
472 md_entry_free (this);
475 } /* }}} int meta_data_delete */
480 int meta_data_add_string (meta_data_t *md, /* {{{ */
481 const char *key, const char *value)
485 if ((md == NULL) || (key == NULL) || (value == NULL))
488 e = md_entry_alloc (key);
492 e->value.mv_string = md_strdup (value);
493 if (e->value.mv_string == NULL)
495 ERROR ("meta_data_add_string: md_strdup failed.");
499 e->type = MD_TYPE_STRING;
501 return (md_entry_insert (md, e));
502 } /* }}} int meta_data_add_string */
504 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
505 const char *key, int64_t value)
509 if ((md == NULL) || (key == NULL))
512 e = md_entry_alloc (key);
516 e->value.mv_signed_int = value;
517 e->type = MD_TYPE_SIGNED_INT;
519 return (md_entry_insert (md, e));
520 } /* }}} int meta_data_add_signed_int */
522 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
523 const char *key, uint64_t value)
527 if ((md == NULL) || (key == NULL))
530 e = md_entry_alloc (key);
534 e->value.mv_unsigned_int = value;
535 e->type = MD_TYPE_UNSIGNED_INT;
537 return (md_entry_insert (md, e));
538 } /* }}} int meta_data_add_unsigned_int */
540 int meta_data_add_double (meta_data_t *md, /* {{{ */
541 const char *key, double value)
545 if ((md == NULL) || (key == NULL))
548 e = md_entry_alloc (key);
552 e->value.mv_double = value;
553 e->type = MD_TYPE_DOUBLE;
555 return (md_entry_insert (md, e));
556 } /* }}} int meta_data_add_double */
558 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
559 const char *key, _Bool value)
563 if ((md == NULL) || (key == NULL))
566 e = md_entry_alloc (key);
570 e->value.mv_boolean = value;
571 e->type = MD_TYPE_BOOLEAN;
573 return (md_entry_insert (md, e));
574 } /* }}} int meta_data_add_boolean */
579 int meta_data_get_string (meta_data_t *md, /* {{{ */
580 const char *key, char **value)
585 if ((md == NULL) || (key == NULL) || (value == NULL))
588 pthread_mutex_lock (&md->lock);
590 e = md_entry_lookup (md, key);
593 pthread_mutex_unlock (&md->lock);
597 if (e->type != MD_TYPE_STRING)
599 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
600 pthread_mutex_unlock (&md->lock);
604 temp = md_strdup (e->value.mv_string);
607 pthread_mutex_unlock (&md->lock);
608 ERROR ("meta_data_get_string: md_strdup failed.");
612 pthread_mutex_unlock (&md->lock);
617 } /* }}} int meta_data_get_string */
619 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
620 const char *key, int64_t *value)
624 if ((md == NULL) || (key == NULL) || (value == NULL))
627 pthread_mutex_lock (&md->lock);
629 e = md_entry_lookup (md, key);
632 pthread_mutex_unlock (&md->lock);
636 if (e->type != MD_TYPE_SIGNED_INT)
638 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
639 pthread_mutex_unlock (&md->lock);
643 *value = e->value.mv_signed_int;
645 pthread_mutex_unlock (&md->lock);
647 } /* }}} int meta_data_get_signed_int */
649 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
650 const char *key, uint64_t *value)
654 if ((md == NULL) || (key == NULL) || (value == NULL))
657 pthread_mutex_lock (&md->lock);
659 e = md_entry_lookup (md, key);
662 pthread_mutex_unlock (&md->lock);
666 if (e->type != MD_TYPE_UNSIGNED_INT)
668 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
669 pthread_mutex_unlock (&md->lock);
673 *value = e->value.mv_unsigned_int;
675 pthread_mutex_unlock (&md->lock);
677 } /* }}} int meta_data_get_unsigned_int */
679 int meta_data_get_double (meta_data_t *md, /* {{{ */
680 const char *key, double *value)
684 if ((md == NULL) || (key == NULL) || (value == NULL))
687 pthread_mutex_lock (&md->lock);
689 e = md_entry_lookup (md, key);
692 pthread_mutex_unlock (&md->lock);
696 if (e->type != MD_TYPE_DOUBLE)
698 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
699 pthread_mutex_unlock (&md->lock);
703 *value = e->value.mv_double;
705 pthread_mutex_unlock (&md->lock);
707 } /* }}} int meta_data_get_double */
709 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
710 const char *key, _Bool *value)
714 if ((md == NULL) || (key == NULL) || (value == NULL))
717 pthread_mutex_lock (&md->lock);
719 e = md_entry_lookup (md, key);
722 pthread_mutex_unlock (&md->lock);
726 if (e->type != MD_TYPE_BOOLEAN)
728 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
729 pthread_mutex_unlock (&md->lock);
733 *value = e->value.mv_boolean;
735 pthread_mutex_unlock (&md->lock);
737 } /* }}} int meta_data_get_boolean */
739 /* vim: set sw=2 sts=2 et fdm=marker : */