2 * collectd - src/meta_data.c
3 * Copyright (C) 2008-2011 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
24 #include "meta_data.h"
34 int64_t mv_signed_int;
35 uint64_t mv_unsigned_int;
39 typedef union meta_value_u meta_value_t;
42 typedef struct meta_entry_s meta_entry_t;
60 static char *md_strdup (const char *orig) /* {{{ */
68 sz = strlen (orig) + 1;
69 dest = (char *) malloc (sz);
73 memcpy (dest, orig, sz);
76 } /* }}} char *md_strdup */
78 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
82 e = (meta_entry_t *) malloc (sizeof (*e));
85 ERROR ("md_entry_alloc: malloc failed.");
88 memset (e, 0, sizeof (*e));
90 e->key = md_strdup (key);
94 ERROR ("md_entry_alloc: md_strdup failed.");
102 } /* }}} meta_entry_t *md_entry_alloc */
104 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
111 copy = md_entry_alloc (orig->key);
114 copy->type = orig->type;
115 if (copy->type == MD_TYPE_STRING)
116 copy->value.mv_string = strdup (orig->value.mv_string);
118 copy->value = orig->value;
120 copy->next = md_entry_clone (orig->next);
122 } /* }}} meta_entry_t *md_entry_clone */
124 static void md_entry_free (meta_entry_t *e) /* {{{ */
131 if (e->type == MD_TYPE_STRING)
132 free (e->value.mv_string);
135 md_entry_free (e->next);
138 } /* }}} void md_entry_free */
140 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
145 if ((md == NULL) || (e == NULL))
148 pthread_mutex_lock (&md->lock);
154 if (strcasecmp (e->key, this->key) == 0)
163 /* This key does not exist yet. */
164 if (md->head == NULL)
168 assert (prev != NULL);
174 else /* (this != NULL) */
181 e->next = this->next;
184 pthread_mutex_unlock (&md->lock);
189 md_entry_free (this);
193 } /* }}} int md_entry_insert */
195 /* XXX: The lock on md must be held while calling this function! */
196 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
201 if ((md == NULL) || (key == NULL))
204 for (e = md->head; e != NULL; e = e->next)
205 if (strcasecmp (key, e->key) == 0)
209 } /* }}} meta_entry_t *md_entry_lookup */
212 * Each value_list_t*, as it is going through the system, is handled by exactly
213 * one thread. Plugins which pass a value_list_t* to another thread, e.g. the
214 * rrdtool plugin, must create a copy first. The meta data within a
215 * value_list_t* is not thread safe and doesn't need to be.
217 * The meta data associated with cache entries are a different story. There, we
218 * need to ensure exclusive locking to prevent leaks and other funky business.
219 * This is ensured by the uc_meta_data_get_*() functions.
225 meta_data_t *meta_data_create (void) /* {{{ */
229 md = (meta_data_t *) malloc (sizeof (*md));
232 ERROR ("meta_data_create: malloc failed.");
235 memset (md, 0, sizeof (*md));
238 pthread_mutex_init (&md->lock, /* attr = */ NULL);
241 } /* }}} meta_data_t *meta_data_create */
243 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
250 copy = meta_data_create ();
254 pthread_mutex_lock (&orig->lock);
255 copy->head = md_entry_clone (orig->head);
256 pthread_mutex_unlock (&orig->lock);
259 } /* }}} meta_data_t *meta_data_clone */
261 void meta_data_destroy (meta_data_t *md) /* {{{ */
266 md_entry_free (md->head);
267 pthread_mutex_destroy (&md->lock);
269 } /* }}} void meta_data_destroy */
271 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
275 if ((md == NULL) || (key == NULL))
278 pthread_mutex_lock (&md->lock);
280 for (e = md->head; e != NULL; e = e->next)
282 if (strcasecmp (key, e->key) == 0)
284 pthread_mutex_unlock (&md->lock);
289 pthread_mutex_unlock (&md->lock);
291 } /* }}} int meta_data_exists */
293 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
297 if ((md == NULL) || (key == NULL))
300 pthread_mutex_lock (&md->lock);
302 for (e = md->head; e != NULL; e = e->next)
304 if (strcasecmp (key, e->key) == 0)
306 pthread_mutex_unlock (&md->lock);
311 pthread_mutex_unlock (&md->lock);
313 } /* }}} int meta_data_type */
315 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
317 int i = 0, count = 0;
320 if ((md == NULL) || (toc == NULL))
323 pthread_mutex_lock (&md->lock);
325 for (e = md->head; e != NULL; e = e->next)
330 pthread_mutex_unlock (&md->lock);
334 *toc = calloc(count, sizeof(**toc));
335 for (e = md->head; e != NULL; e = e->next)
336 (*toc)[i++] = strdup(e->key);
338 pthread_mutex_unlock (&md->lock);
340 } /* }}} int meta_data_toc */
342 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
347 if ((md == NULL) || (key == NULL))
350 pthread_mutex_lock (&md->lock);
356 if (strcasecmp (key, this->key) == 0)
365 pthread_mutex_unlock (&md->lock);
370 md->head = this->next;
372 prev->next = this->next;
374 pthread_mutex_unlock (&md->lock);
377 md_entry_free (this);
380 } /* }}} int meta_data_delete */
385 int meta_data_add_string (meta_data_t *md, /* {{{ */
386 const char *key, const char *value)
390 if ((md == NULL) || (key == NULL) || (value == NULL))
393 e = md_entry_alloc (key);
397 e->value.mv_string = md_strdup (value);
398 if (e->value.mv_string == NULL)
400 ERROR ("meta_data_add_string: md_strdup failed.");
404 e->type = MD_TYPE_STRING;
406 return (md_entry_insert (md, e));
407 } /* }}} int meta_data_add_string */
409 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
410 const char *key, int64_t value)
414 if ((md == NULL) || (key == NULL))
417 e = md_entry_alloc (key);
421 e->value.mv_signed_int = value;
422 e->type = MD_TYPE_SIGNED_INT;
424 return (md_entry_insert (md, e));
425 } /* }}} int meta_data_add_signed_int */
427 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
428 const char *key, uint64_t value)
432 if ((md == NULL) || (key == NULL))
435 e = md_entry_alloc (key);
439 e->value.mv_unsigned_int = value;
440 e->type = MD_TYPE_UNSIGNED_INT;
442 return (md_entry_insert (md, e));
443 } /* }}} int meta_data_add_unsigned_int */
445 int meta_data_add_double (meta_data_t *md, /* {{{ */
446 const char *key, double value)
450 if ((md == NULL) || (key == NULL))
453 e = md_entry_alloc (key);
457 e->value.mv_double = value;
458 e->type = MD_TYPE_DOUBLE;
460 return (md_entry_insert (md, e));
461 } /* }}} int meta_data_add_double */
463 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
464 const char *key, _Bool value)
468 if ((md == NULL) || (key == NULL))
471 e = md_entry_alloc (key);
475 e->value.mv_boolean = value;
476 e->type = MD_TYPE_BOOLEAN;
478 return (md_entry_insert (md, e));
479 } /* }}} int meta_data_add_boolean */
484 int meta_data_get_string (meta_data_t *md, /* {{{ */
485 const char *key, char **value)
490 if ((md == NULL) || (key == NULL) || (value == NULL))
493 pthread_mutex_lock (&md->lock);
495 e = md_entry_lookup (md, key);
498 pthread_mutex_unlock (&md->lock);
502 if (e->type != MD_TYPE_STRING)
504 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
505 pthread_mutex_unlock (&md->lock);
509 temp = md_strdup (e->value.mv_string);
512 pthread_mutex_unlock (&md->lock);
513 ERROR ("meta_data_get_string: md_strdup failed.");
517 pthread_mutex_unlock (&md->lock);
522 } /* }}} int meta_data_get_string */
524 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
525 const char *key, int64_t *value)
529 if ((md == NULL) || (key == NULL) || (value == NULL))
532 pthread_mutex_lock (&md->lock);
534 e = md_entry_lookup (md, key);
537 pthread_mutex_unlock (&md->lock);
541 if (e->type != MD_TYPE_SIGNED_INT)
543 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
544 pthread_mutex_unlock (&md->lock);
548 *value = e->value.mv_signed_int;
550 pthread_mutex_unlock (&md->lock);
552 } /* }}} int meta_data_get_signed_int */
554 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
555 const char *key, uint64_t *value)
559 if ((md == NULL) || (key == NULL) || (value == NULL))
562 pthread_mutex_lock (&md->lock);
564 e = md_entry_lookup (md, key);
567 pthread_mutex_unlock (&md->lock);
571 if (e->type != MD_TYPE_UNSIGNED_INT)
573 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
574 pthread_mutex_unlock (&md->lock);
578 *value = e->value.mv_unsigned_int;
580 pthread_mutex_unlock (&md->lock);
582 } /* }}} int meta_data_get_unsigned_int */
584 int meta_data_get_double (meta_data_t *md, /* {{{ */
585 const char *key, double *value)
589 if ((md == NULL) || (key == NULL) || (value == NULL))
592 pthread_mutex_lock (&md->lock);
594 e = md_entry_lookup (md, key);
597 pthread_mutex_unlock (&md->lock);
601 if (e->type != MD_TYPE_DOUBLE)
603 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
604 pthread_mutex_unlock (&md->lock);
608 *value = e->value.mv_double;
610 pthread_mutex_unlock (&md->lock);
612 } /* }}} int meta_data_get_double */
614 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
615 const char *key, _Bool *value)
619 if ((md == NULL) || (key == NULL) || (value == NULL))
622 pthread_mutex_lock (&md->lock);
624 e = md_entry_lookup (md, key);
627 pthread_mutex_unlock (&md->lock);
631 if (e->type != MD_TYPE_BOOLEAN)
633 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
634 pthread_mutex_unlock (&md->lock);
638 *value = e->value.mv_boolean;
640 pthread_mutex_unlock (&md->lock);
642 } /* }}} int meta_data_get_boolean */
644 /* vim: set sw=2 sts=2 et fdm=marker : */