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; NULL != e; e = e->next) {
338 md_entry_insert_clone((*dest), e);
340 pthread_mutex_unlock (&orig->lock);
343 } /* }}} int meta_data_clone_merge */
345 void meta_data_destroy (meta_data_t *md) /* {{{ */
350 md_entry_free (md->head);
351 pthread_mutex_destroy (&md->lock);
353 } /* }}} void meta_data_destroy */
355 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
359 if ((md == NULL) || (key == NULL))
362 pthread_mutex_lock (&md->lock);
364 for (e = md->head; e != NULL; e = e->next)
366 if (strcasecmp (key, e->key) == 0)
368 pthread_mutex_unlock (&md->lock);
373 pthread_mutex_unlock (&md->lock);
375 } /* }}} int meta_data_exists */
377 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
381 if ((md == NULL) || (key == NULL))
384 pthread_mutex_lock (&md->lock);
386 for (e = md->head; e != NULL; e = e->next)
388 if (strcasecmp (key, e->key) == 0)
390 pthread_mutex_unlock (&md->lock);
395 pthread_mutex_unlock (&md->lock);
397 } /* }}} int meta_data_type */
399 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
401 int i = 0, count = 0;
404 if ((md == NULL) || (toc == NULL))
407 pthread_mutex_lock (&md->lock);
409 for (e = md->head; e != NULL; e = e->next)
414 pthread_mutex_unlock (&md->lock);
418 *toc = calloc(count, sizeof(**toc));
419 for (e = md->head; e != NULL; e = e->next)
420 (*toc)[i++] = strdup(e->key);
422 pthread_mutex_unlock (&md->lock);
424 } /* }}} int meta_data_toc */
426 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
431 if ((md == NULL) || (key == NULL))
434 pthread_mutex_lock (&md->lock);
440 if (strcasecmp (key, this->key) == 0)
449 pthread_mutex_unlock (&md->lock);
454 md->head = this->next;
456 prev->next = this->next;
458 pthread_mutex_unlock (&md->lock);
461 md_entry_free (this);
464 } /* }}} int meta_data_delete */
469 int meta_data_add_string (meta_data_t *md, /* {{{ */
470 const char *key, const char *value)
474 if ((md == NULL) || (key == NULL) || (value == NULL))
477 e = md_entry_alloc (key);
481 e->value.mv_string = md_strdup (value);
482 if (e->value.mv_string == NULL)
484 ERROR ("meta_data_add_string: md_strdup failed.");
488 e->type = MD_TYPE_STRING;
490 return (md_entry_insert (md, e));
491 } /* }}} int meta_data_add_string */
493 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
494 const char *key, int64_t value)
498 if ((md == NULL) || (key == NULL))
501 e = md_entry_alloc (key);
505 e->value.mv_signed_int = value;
506 e->type = MD_TYPE_SIGNED_INT;
508 return (md_entry_insert (md, e));
509 } /* }}} int meta_data_add_signed_int */
511 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
512 const char *key, uint64_t value)
516 if ((md == NULL) || (key == NULL))
519 e = md_entry_alloc (key);
523 e->value.mv_unsigned_int = value;
524 e->type = MD_TYPE_UNSIGNED_INT;
526 return (md_entry_insert (md, e));
527 } /* }}} int meta_data_add_unsigned_int */
529 int meta_data_add_double (meta_data_t *md, /* {{{ */
530 const char *key, double value)
534 if ((md == NULL) || (key == NULL))
537 e = md_entry_alloc (key);
541 e->value.mv_double = value;
542 e->type = MD_TYPE_DOUBLE;
544 return (md_entry_insert (md, e));
545 } /* }}} int meta_data_add_double */
547 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
548 const char *key, _Bool value)
552 if ((md == NULL) || (key == NULL))
555 e = md_entry_alloc (key);
559 e->value.mv_boolean = value;
560 e->type = MD_TYPE_BOOLEAN;
562 return (md_entry_insert (md, e));
563 } /* }}} int meta_data_add_boolean */
568 int meta_data_get_string (meta_data_t *md, /* {{{ */
569 const char *key, char **value)
574 if ((md == NULL) || (key == NULL) || (value == NULL))
577 pthread_mutex_lock (&md->lock);
579 e = md_entry_lookup (md, key);
582 pthread_mutex_unlock (&md->lock);
586 if (e->type != MD_TYPE_STRING)
588 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
589 pthread_mutex_unlock (&md->lock);
593 temp = md_strdup (e->value.mv_string);
596 pthread_mutex_unlock (&md->lock);
597 ERROR ("meta_data_get_string: md_strdup failed.");
601 pthread_mutex_unlock (&md->lock);
606 } /* }}} int meta_data_get_string */
608 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
609 const char *key, int64_t *value)
613 if ((md == NULL) || (key == NULL) || (value == NULL))
616 pthread_mutex_lock (&md->lock);
618 e = md_entry_lookup (md, key);
621 pthread_mutex_unlock (&md->lock);
625 if (e->type != MD_TYPE_SIGNED_INT)
627 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
628 pthread_mutex_unlock (&md->lock);
632 *value = e->value.mv_signed_int;
634 pthread_mutex_unlock (&md->lock);
636 } /* }}} int meta_data_get_signed_int */
638 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
639 const char *key, uint64_t *value)
643 if ((md == NULL) || (key == NULL) || (value == NULL))
646 pthread_mutex_lock (&md->lock);
648 e = md_entry_lookup (md, key);
651 pthread_mutex_unlock (&md->lock);
655 if (e->type != MD_TYPE_UNSIGNED_INT)
657 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
658 pthread_mutex_unlock (&md->lock);
662 *value = e->value.mv_unsigned_int;
664 pthread_mutex_unlock (&md->lock);
666 } /* }}} int meta_data_get_unsigned_int */
668 int meta_data_get_double (meta_data_t *md, /* {{{ */
669 const char *key, double *value)
673 if ((md == NULL) || (key == NULL) || (value == NULL))
676 pthread_mutex_lock (&md->lock);
678 e = md_entry_lookup (md, key);
681 pthread_mutex_unlock (&md->lock);
685 if (e->type != MD_TYPE_DOUBLE)
687 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
688 pthread_mutex_unlock (&md->lock);
692 *value = e->value.mv_double;
694 pthread_mutex_unlock (&md->lock);
696 } /* }}} int meta_data_get_double */
698 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
699 const char *key, _Bool *value)
703 if ((md == NULL) || (key == NULL) || (value == NULL))
706 pthread_mutex_lock (&md->lock);
708 e = md_entry_lookup (md, key);
711 pthread_mutex_unlock (&md->lock);
715 if (e->type != MD_TYPE_BOOLEAN)
717 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
718 pthread_mutex_unlock (&md->lock);
722 *value = e->value.mv_boolean;
724 pthread_mutex_unlock (&md->lock);
726 } /* }}} int meta_data_get_boolean */
728 /* vim: set sw=2 sts=2 et fdm=marker : */