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) /* {{{ */
340 *dest = meta_data_clone(orig);
344 pthread_mutex_lock (&orig->lock);
345 for (meta_entry_t *e=orig->head; e != NULL; e = e->next)
347 md_entry_insert_clone((*dest), e);
349 pthread_mutex_unlock (&orig->lock);
352 } /* }}} int meta_data_clone_merge */
354 void meta_data_destroy (meta_data_t *md) /* {{{ */
359 md_entry_free (md->head);
360 pthread_mutex_destroy (&md->lock);
362 } /* }}} void meta_data_destroy */
364 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
366 if ((md == NULL) || (key == NULL))
369 pthread_mutex_lock (&md->lock);
371 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
373 if (strcasecmp (key, e->key) == 0)
375 pthread_mutex_unlock (&md->lock);
380 pthread_mutex_unlock (&md->lock);
382 } /* }}} int meta_data_exists */
384 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
386 if ((md == NULL) || (key == NULL))
389 pthread_mutex_lock (&md->lock);
391 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
393 if (strcasecmp (key, e->key) == 0)
395 pthread_mutex_unlock (&md->lock);
400 pthread_mutex_unlock (&md->lock);
402 } /* }}} int meta_data_type */
404 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
406 int i = 0, count = 0;
408 if ((md == NULL) || (toc == NULL))
411 pthread_mutex_lock (&md->lock);
413 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
418 pthread_mutex_unlock (&md->lock);
422 *toc = calloc(count, sizeof(**toc));
423 for (meta_entry_t *e = md->head; e != NULL; e = e->next)
424 (*toc)[i++] = strdup(e->key);
426 pthread_mutex_unlock (&md->lock);
428 } /* }}} int meta_data_toc */
430 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
435 if ((md == NULL) || (key == NULL))
438 pthread_mutex_lock (&md->lock);
444 if (strcasecmp (key, this->key) == 0)
453 pthread_mutex_unlock (&md->lock);
458 md->head = this->next;
460 prev->next = this->next;
462 pthread_mutex_unlock (&md->lock);
465 md_entry_free (this);
468 } /* }}} int meta_data_delete */
473 int meta_data_add_string (meta_data_t *md, /* {{{ */
474 const char *key, const char *value)
478 if ((md == NULL) || (key == NULL) || (value == NULL))
481 e = md_entry_alloc (key);
485 e->value.mv_string = md_strdup (value);
486 if (e->value.mv_string == NULL)
488 ERROR ("meta_data_add_string: md_strdup failed.");
492 e->type = MD_TYPE_STRING;
494 return (md_entry_insert (md, e));
495 } /* }}} int meta_data_add_string */
497 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
498 const char *key, int64_t value)
502 if ((md == NULL) || (key == NULL))
505 e = md_entry_alloc (key);
509 e->value.mv_signed_int = value;
510 e->type = MD_TYPE_SIGNED_INT;
512 return (md_entry_insert (md, e));
513 } /* }}} int meta_data_add_signed_int */
515 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
516 const char *key, uint64_t value)
520 if ((md == NULL) || (key == NULL))
523 e = md_entry_alloc (key);
527 e->value.mv_unsigned_int = value;
528 e->type = MD_TYPE_UNSIGNED_INT;
530 return (md_entry_insert (md, e));
531 } /* }}} int meta_data_add_unsigned_int */
533 int meta_data_add_double (meta_data_t *md, /* {{{ */
534 const char *key, double value)
538 if ((md == NULL) || (key == NULL))
541 e = md_entry_alloc (key);
545 e->value.mv_double = value;
546 e->type = MD_TYPE_DOUBLE;
548 return (md_entry_insert (md, e));
549 } /* }}} int meta_data_add_double */
551 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
552 const char *key, _Bool value)
556 if ((md == NULL) || (key == NULL))
559 e = md_entry_alloc (key);
563 e->value.mv_boolean = value;
564 e->type = MD_TYPE_BOOLEAN;
566 return (md_entry_insert (md, e));
567 } /* }}} int meta_data_add_boolean */
572 int meta_data_get_string (meta_data_t *md, /* {{{ */
573 const char *key, char **value)
578 if ((md == NULL) || (key == NULL) || (value == NULL))
581 pthread_mutex_lock (&md->lock);
583 e = md_entry_lookup (md, key);
586 pthread_mutex_unlock (&md->lock);
590 if (e->type != MD_TYPE_STRING)
592 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
593 pthread_mutex_unlock (&md->lock);
597 temp = md_strdup (e->value.mv_string);
600 pthread_mutex_unlock (&md->lock);
601 ERROR ("meta_data_get_string: md_strdup failed.");
605 pthread_mutex_unlock (&md->lock);
610 } /* }}} int meta_data_get_string */
612 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
613 const char *key, int64_t *value)
617 if ((md == NULL) || (key == NULL) || (value == NULL))
620 pthread_mutex_lock (&md->lock);
622 e = md_entry_lookup (md, key);
625 pthread_mutex_unlock (&md->lock);
629 if (e->type != MD_TYPE_SIGNED_INT)
631 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
632 pthread_mutex_unlock (&md->lock);
636 *value = e->value.mv_signed_int;
638 pthread_mutex_unlock (&md->lock);
640 } /* }}} int meta_data_get_signed_int */
642 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
643 const char *key, uint64_t *value)
647 if ((md == NULL) || (key == NULL) || (value == NULL))
650 pthread_mutex_lock (&md->lock);
652 e = md_entry_lookup (md, key);
655 pthread_mutex_unlock (&md->lock);
659 if (e->type != MD_TYPE_UNSIGNED_INT)
661 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
662 pthread_mutex_unlock (&md->lock);
666 *value = e->value.mv_unsigned_int;
668 pthread_mutex_unlock (&md->lock);
670 } /* }}} int meta_data_get_unsigned_int */
672 int meta_data_get_double (meta_data_t *md, /* {{{ */
673 const char *key, double *value)
677 if ((md == NULL) || (key == NULL) || (value == NULL))
680 pthread_mutex_lock (&md->lock);
682 e = md_entry_lookup (md, key);
685 pthread_mutex_unlock (&md->lock);
689 if (e->type != MD_TYPE_DOUBLE)
691 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
692 pthread_mutex_unlock (&md->lock);
696 *value = e->value.mv_double;
698 pthread_mutex_unlock (&md->lock);
700 } /* }}} int meta_data_get_double */
702 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
703 const char *key, _Bool *value)
707 if ((md == NULL) || (key == NULL) || (value == NULL))
710 pthread_mutex_lock (&md->lock);
712 e = md_entry_lookup (md, key);
715 pthread_mutex_unlock (&md->lock);
719 if (e->type != MD_TYPE_BOOLEAN)
721 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
722 pthread_mutex_unlock (&md->lock);
726 *value = e->value.mv_boolean;
728 pthread_mutex_unlock (&md->lock);
730 } /* }}} int meta_data_get_boolean */
732 /* vim: set sw=2 sts=2 et fdm=marker : */