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>
29 #include "meta_data.h"
39 int64_t mv_signed_int;
40 uint64_t mv_unsigned_int;
44 typedef union meta_value_u meta_value_t;
47 typedef struct meta_entry_s meta_entry_t;
65 static char *md_strdup (const char *orig) /* {{{ */
73 sz = strlen (orig) + 1;
78 memcpy (dest, orig, sz);
81 } /* }}} char *md_strdup */
83 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
87 e = calloc (1, sizeof (*e));
90 ERROR ("md_entry_alloc: calloc failed.");
94 e->key = md_strdup (key);
98 ERROR ("md_entry_alloc: md_strdup failed.");
106 } /* }}} meta_entry_t *md_entry_alloc */
108 /* XXX: The lock on md must be held while calling this function! */
109 static meta_entry_t *md_entry_clone_contents (const meta_entry_t *orig) /* {{{ */
114 * - we do not check that orig != NULL here. You should have done it before.
115 * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR FUNCTION
118 copy = md_entry_alloc (orig->key);
121 copy->type = orig->type;
122 if (copy->type == MD_TYPE_STRING)
123 copy->value.mv_string = strdup (orig->value.mv_string);
125 copy->value = orig->value;
128 } /* }}} meta_entry_t *md_entry_clone_contents */
130 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
137 copy = md_entry_clone_contents(orig);
139 copy->next = md_entry_clone (orig->next);
141 } /* }}} meta_entry_t *md_entry_clone */
143 static void md_entry_free (meta_entry_t *e) /* {{{ */
150 if (e->type == MD_TYPE_STRING)
151 free (e->value.mv_string);
154 md_entry_free (e->next);
157 } /* }}} void md_entry_free */
159 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
164 if ((md == NULL) || (e == NULL))
167 pthread_mutex_lock (&md->lock);
173 if (strcasecmp (e->key, this->key) == 0)
182 /* This key does not exist yet. */
183 if (md->head == NULL)
187 assert (prev != NULL);
193 else /* (this != NULL) */
200 e->next = this->next;
203 pthread_mutex_unlock (&md->lock);
208 md_entry_free (this);
212 } /* }}} int md_entry_insert */
214 /* XXX: The lock on md must be held while calling this function! */
215 static int md_entry_insert_clone (meta_data_t *md, meta_entry_t *orig) /* {{{ */
222 * - we do not check that md and e != NULL here. You should have done it before.
223 * - we do not use the lock. You should have set it before.
226 e = md_entry_clone_contents(orig);
232 if (strcasecmp (e->key, this->key) == 0)
241 /* This key does not exist yet. */
242 if (md->head == NULL)
246 assert (prev != NULL);
252 else /* (this != NULL) */
259 e->next = this->next;
265 md_entry_free (this);
269 } /* }}} int md_entry_insert_clone */
271 /* XXX: The lock on md must be held while calling this function! */
272 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
277 if ((md == NULL) || (key == NULL))
280 for (e = md->head; e != NULL; e = e->next)
281 if (strcasecmp (key, e->key) == 0)
285 } /* }}} meta_entry_t *md_entry_lookup */
290 meta_data_t *meta_data_create (void) /* {{{ */
294 md = calloc (1, sizeof (*md));
297 ERROR ("meta_data_create: calloc failed.");
301 pthread_mutex_init (&md->lock, /* attr = */ NULL);
304 } /* }}} meta_data_t *meta_data_create */
306 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
313 copy = meta_data_create ();
317 pthread_mutex_lock (&orig->lock);
318 copy->head = md_entry_clone (orig->head);
319 pthread_mutex_unlock (&orig->lock);
322 } /* }}} meta_data_t *meta_data_clone */
324 int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */
332 *dest = meta_data_clone(orig);
336 pthread_mutex_lock (&orig->lock);
337 for (e=orig->head; e != NULL; e = e->next)
339 md_entry_insert_clone((*dest), e);
341 pthread_mutex_unlock (&orig->lock);
344 } /* }}} int meta_data_clone_merge */
346 void meta_data_destroy (meta_data_t *md) /* {{{ */
351 md_entry_free (md->head);
352 pthread_mutex_destroy (&md->lock);
354 } /* }}} void meta_data_destroy */
356 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
360 if ((md == NULL) || (key == NULL))
363 pthread_mutex_lock (&md->lock);
365 for (e = md->head; e != NULL; e = e->next)
367 if (strcasecmp (key, e->key) == 0)
369 pthread_mutex_unlock (&md->lock);
374 pthread_mutex_unlock (&md->lock);
376 } /* }}} int meta_data_exists */
378 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
382 if ((md == NULL) || (key == NULL))
385 pthread_mutex_lock (&md->lock);
387 for (e = md->head; e != NULL; e = e->next)
389 if (strcasecmp (key, e->key) == 0)
391 pthread_mutex_unlock (&md->lock);
396 pthread_mutex_unlock (&md->lock);
398 } /* }}} int meta_data_type */
400 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
402 int i = 0, count = 0;
405 if ((md == NULL) || (toc == NULL))
408 pthread_mutex_lock (&md->lock);
410 for (e = md->head; e != NULL; e = e->next)
415 pthread_mutex_unlock (&md->lock);
419 *toc = calloc(count, sizeof(**toc));
420 for (e = md->head; e != NULL; e = e->next)
421 (*toc)[i++] = strdup(e->key);
423 pthread_mutex_unlock (&md->lock);
425 } /* }}} int meta_data_toc */
427 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
432 if ((md == NULL) || (key == NULL))
435 pthread_mutex_lock (&md->lock);
441 if (strcasecmp (key, this->key) == 0)
450 pthread_mutex_unlock (&md->lock);
455 md->head = this->next;
457 prev->next = this->next;
459 pthread_mutex_unlock (&md->lock);
462 md_entry_free (this);
465 } /* }}} int meta_data_delete */
470 int meta_data_add_string (meta_data_t *md, /* {{{ */
471 const char *key, const char *value)
475 if ((md == NULL) || (key == NULL) || (value == NULL))
478 e = md_entry_alloc (key);
482 e->value.mv_string = md_strdup (value);
483 if (e->value.mv_string == NULL)
485 ERROR ("meta_data_add_string: md_strdup failed.");
489 e->type = MD_TYPE_STRING;
491 return (md_entry_insert (md, e));
492 } /* }}} int meta_data_add_string */
494 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
495 const char *key, int64_t value)
499 if ((md == NULL) || (key == NULL))
502 e = md_entry_alloc (key);
506 e->value.mv_signed_int = value;
507 e->type = MD_TYPE_SIGNED_INT;
509 return (md_entry_insert (md, e));
510 } /* }}} int meta_data_add_signed_int */
512 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
513 const char *key, uint64_t value)
517 if ((md == NULL) || (key == NULL))
520 e = md_entry_alloc (key);
524 e->value.mv_unsigned_int = value;
525 e->type = MD_TYPE_UNSIGNED_INT;
527 return (md_entry_insert (md, e));
528 } /* }}} int meta_data_add_unsigned_int */
530 int meta_data_add_double (meta_data_t *md, /* {{{ */
531 const char *key, double value)
535 if ((md == NULL) || (key == NULL))
538 e = md_entry_alloc (key);
542 e->value.mv_double = value;
543 e->type = MD_TYPE_DOUBLE;
545 return (md_entry_insert (md, e));
546 } /* }}} int meta_data_add_double */
548 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
549 const char *key, _Bool value)
553 if ((md == NULL) || (key == NULL))
556 e = md_entry_alloc (key);
560 e->value.mv_boolean = value;
561 e->type = MD_TYPE_BOOLEAN;
563 return (md_entry_insert (md, e));
564 } /* }}} int meta_data_add_boolean */
569 int meta_data_get_string (meta_data_t *md, /* {{{ */
570 const char *key, char **value)
575 if ((md == NULL) || (key == NULL) || (value == NULL))
578 pthread_mutex_lock (&md->lock);
580 e = md_entry_lookup (md, key);
583 pthread_mutex_unlock (&md->lock);
587 if (e->type != MD_TYPE_STRING)
589 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
590 pthread_mutex_unlock (&md->lock);
594 temp = md_strdup (e->value.mv_string);
597 pthread_mutex_unlock (&md->lock);
598 ERROR ("meta_data_get_string: md_strdup failed.");
602 pthread_mutex_unlock (&md->lock);
607 } /* }}} int meta_data_get_string */
609 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
610 const char *key, int64_t *value)
614 if ((md == NULL) || (key == NULL) || (value == NULL))
617 pthread_mutex_lock (&md->lock);
619 e = md_entry_lookup (md, key);
622 pthread_mutex_unlock (&md->lock);
626 if (e->type != MD_TYPE_SIGNED_INT)
628 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
629 pthread_mutex_unlock (&md->lock);
633 *value = e->value.mv_signed_int;
635 pthread_mutex_unlock (&md->lock);
637 } /* }}} int meta_data_get_signed_int */
639 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
640 const char *key, uint64_t *value)
644 if ((md == NULL) || (key == NULL) || (value == NULL))
647 pthread_mutex_lock (&md->lock);
649 e = md_entry_lookup (md, key);
652 pthread_mutex_unlock (&md->lock);
656 if (e->type != MD_TYPE_UNSIGNED_INT)
658 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
659 pthread_mutex_unlock (&md->lock);
663 *value = e->value.mv_unsigned_int;
665 pthread_mutex_unlock (&md->lock);
667 } /* }}} int meta_data_get_unsigned_int */
669 int meta_data_get_double (meta_data_t *md, /* {{{ */
670 const char *key, double *value)
674 if ((md == NULL) || (key == NULL) || (value == NULL))
677 pthread_mutex_lock (&md->lock);
679 e = md_entry_lookup (md, key);
682 pthread_mutex_unlock (&md->lock);
686 if (e->type != MD_TYPE_DOUBLE)
688 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
689 pthread_mutex_unlock (&md->lock);
693 *value = e->value.mv_double;
695 pthread_mutex_unlock (&md->lock);
697 } /* }}} int meta_data_get_double */
699 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
700 const char *key, _Bool *value)
704 if ((md == NULL) || (key == NULL) || (value == NULL))
707 pthread_mutex_lock (&md->lock);
709 e = md_entry_lookup (md, key);
712 pthread_mutex_unlock (&md->lock);
716 if (e->type != MD_TYPE_BOOLEAN)
718 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
719 pthread_mutex_unlock (&md->lock);
723 *value = e->value.mv_boolean;
725 pthread_mutex_unlock (&md->lock);
727 } /* }}} int meta_data_get_boolean */
729 /* vim: set sw=2 sts=2 et fdm=marker : */