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"
37 int64_t mv_signed_int;
38 uint64_t mv_unsigned_int;
42 typedef union meta_value_u meta_value_t;
45 typedef struct meta_entry_s meta_entry_t;
63 static char *md_strdup (const char *orig) /* {{{ */
71 sz = strlen (orig) + 1;
76 memcpy (dest, orig, sz);
79 } /* }}} char *md_strdup */
81 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
85 e = calloc (1, sizeof (*e));
88 ERROR ("md_entry_alloc: calloc failed.");
92 e->key = md_strdup (key);
96 ERROR ("md_entry_alloc: md_strdup failed.");
104 } /* }}} meta_entry_t *md_entry_alloc */
106 /* XXX: The lock on md must be held while calling this function! */
107 static meta_entry_t *md_entry_clone_contents (const meta_entry_t *orig) /* {{{ */
112 * - we do not check that orig != NULL here. You should have done it before.
113 * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR FUNCTION
116 copy = md_entry_alloc (orig->key);
119 copy->type = orig->type;
120 if (copy->type == MD_TYPE_STRING)
121 copy->value.mv_string = strdup (orig->value.mv_string);
123 copy->value = orig->value;
126 } /* }}} meta_entry_t *md_entry_clone_contents */
128 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
135 copy = md_entry_clone_contents(orig);
137 copy->next = md_entry_clone (orig->next);
139 } /* }}} meta_entry_t *md_entry_clone */
141 static void md_entry_free (meta_entry_t *e) /* {{{ */
148 if (e->type == MD_TYPE_STRING)
149 free (e->value.mv_string);
152 md_entry_free (e->next);
155 } /* }}} void md_entry_free */
157 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
162 if ((md == NULL) || (e == NULL))
165 pthread_mutex_lock (&md->lock);
171 if (strcasecmp (e->key, this->key) == 0)
180 /* This key does not exist yet. */
181 if (md->head == NULL)
185 assert (prev != NULL);
191 else /* (this != NULL) */
198 e->next = this->next;
201 pthread_mutex_unlock (&md->lock);
206 md_entry_free (this);
210 } /* }}} int md_entry_insert */
212 /* XXX: The lock on md must be held while calling this function! */
213 static int md_entry_insert_clone (meta_data_t *md, meta_entry_t *orig) /* {{{ */
220 * - we do not check that md and e != NULL here. You should have done it before.
221 * - we do not use the lock. You should have set it before.
224 e = md_entry_clone_contents(orig);
230 if (strcasecmp (e->key, this->key) == 0)
239 /* This key does not exist yet. */
240 if (md->head == NULL)
244 assert (prev != NULL);
250 else /* (this != NULL) */
257 e->next = this->next;
263 md_entry_free (this);
267 } /* }}} int md_entry_insert_clone */
269 /* XXX: The lock on md must be held while calling this function! */
270 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
275 if ((md == NULL) || (key == NULL))
278 for (e = md->head; e != NULL; e = e->next)
279 if (strcasecmp (key, e->key) == 0)
283 } /* }}} meta_entry_t *md_entry_lookup */
288 meta_data_t *meta_data_create (void) /* {{{ */
292 md = calloc (1, sizeof (*md));
295 ERROR ("meta_data_create: calloc failed.");
299 pthread_mutex_init (&md->lock, /* attr = */ NULL);
302 } /* }}} meta_data_t *meta_data_create */
304 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
311 copy = meta_data_create ();
315 pthread_mutex_lock (&orig->lock);
316 copy->head = md_entry_clone (orig->head);
317 pthread_mutex_unlock (&orig->lock);
320 } /* }}} meta_data_t *meta_data_clone */
322 int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */
330 *dest = meta_data_clone(orig);
334 pthread_mutex_lock (&orig->lock);
335 for (e=orig->head; e != NULL; e = e->next)
337 md_entry_insert_clone((*dest), e);
339 pthread_mutex_unlock (&orig->lock);
342 } /* }}} int meta_data_clone_merge */
344 void meta_data_destroy (meta_data_t *md) /* {{{ */
349 md_entry_free (md->head);
350 pthread_mutex_destroy (&md->lock);
352 } /* }}} void meta_data_destroy */
354 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
358 if ((md == NULL) || (key == NULL))
361 pthread_mutex_lock (&md->lock);
363 for (e = md->head; e != NULL; e = e->next)
365 if (strcasecmp (key, e->key) == 0)
367 pthread_mutex_unlock (&md->lock);
372 pthread_mutex_unlock (&md->lock);
374 } /* }}} int meta_data_exists */
376 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
380 if ((md == NULL) || (key == NULL))
383 pthread_mutex_lock (&md->lock);
385 for (e = md->head; e != NULL; e = e->next)
387 if (strcasecmp (key, e->key) == 0)
389 pthread_mutex_unlock (&md->lock);
394 pthread_mutex_unlock (&md->lock);
396 } /* }}} int meta_data_type */
398 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
400 int i = 0, count = 0;
403 if ((md == NULL) || (toc == NULL))
406 pthread_mutex_lock (&md->lock);
408 for (e = md->head; e != NULL; e = e->next)
413 pthread_mutex_unlock (&md->lock);
417 *toc = calloc(count, sizeof(**toc));
418 for (e = md->head; e != NULL; e = e->next)
419 (*toc)[i++] = strdup(e->key);
421 pthread_mutex_unlock (&md->lock);
423 } /* }}} int meta_data_toc */
425 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
430 if ((md == NULL) || (key == NULL))
433 pthread_mutex_lock (&md->lock);
439 if (strcasecmp (key, this->key) == 0)
448 pthread_mutex_unlock (&md->lock);
453 md->head = this->next;
455 prev->next = this->next;
457 pthread_mutex_unlock (&md->lock);
460 md_entry_free (this);
463 } /* }}} int meta_data_delete */
468 int meta_data_add_string (meta_data_t *md, /* {{{ */
469 const char *key, const char *value)
473 if ((md == NULL) || (key == NULL) || (value == NULL))
476 e = md_entry_alloc (key);
480 e->value.mv_string = md_strdup (value);
481 if (e->value.mv_string == NULL)
483 ERROR ("meta_data_add_string: md_strdup failed.");
487 e->type = MD_TYPE_STRING;
489 return (md_entry_insert (md, e));
490 } /* }}} int meta_data_add_string */
492 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
493 const char *key, int64_t value)
497 if ((md == NULL) || (key == NULL))
500 e = md_entry_alloc (key);
504 e->value.mv_signed_int = value;
505 e->type = MD_TYPE_SIGNED_INT;
507 return (md_entry_insert (md, e));
508 } /* }}} int meta_data_add_signed_int */
510 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
511 const char *key, uint64_t value)
515 if ((md == NULL) || (key == NULL))
518 e = md_entry_alloc (key);
522 e->value.mv_unsigned_int = value;
523 e->type = MD_TYPE_UNSIGNED_INT;
525 return (md_entry_insert (md, e));
526 } /* }}} int meta_data_add_unsigned_int */
528 int meta_data_add_double (meta_data_t *md, /* {{{ */
529 const char *key, double value)
533 if ((md == NULL) || (key == NULL))
536 e = md_entry_alloc (key);
540 e->value.mv_double = value;
541 e->type = MD_TYPE_DOUBLE;
543 return (md_entry_insert (md, e));
544 } /* }}} int meta_data_add_double */
546 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
547 const char *key, _Bool value)
551 if ((md == NULL) || (key == NULL))
554 e = md_entry_alloc (key);
558 e->value.mv_boolean = value;
559 e->type = MD_TYPE_BOOLEAN;
561 return (md_entry_insert (md, e));
562 } /* }}} int meta_data_add_boolean */
567 int meta_data_get_string (meta_data_t *md, /* {{{ */
568 const char *key, char **value)
573 if ((md == NULL) || (key == NULL) || (value == NULL))
576 pthread_mutex_lock (&md->lock);
578 e = md_entry_lookup (md, key);
581 pthread_mutex_unlock (&md->lock);
585 if (e->type != MD_TYPE_STRING)
587 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
588 pthread_mutex_unlock (&md->lock);
592 temp = md_strdup (e->value.mv_string);
595 pthread_mutex_unlock (&md->lock);
596 ERROR ("meta_data_get_string: md_strdup failed.");
600 pthread_mutex_unlock (&md->lock);
605 } /* }}} int meta_data_get_string */
607 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
608 const char *key, int64_t *value)
612 if ((md == NULL) || (key == NULL) || (value == NULL))
615 pthread_mutex_lock (&md->lock);
617 e = md_entry_lookup (md, key);
620 pthread_mutex_unlock (&md->lock);
624 if (e->type != MD_TYPE_SIGNED_INT)
626 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
627 pthread_mutex_unlock (&md->lock);
631 *value = e->value.mv_signed_int;
633 pthread_mutex_unlock (&md->lock);
635 } /* }}} int meta_data_get_signed_int */
637 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
638 const char *key, uint64_t *value)
642 if ((md == NULL) || (key == NULL) || (value == NULL))
645 pthread_mutex_lock (&md->lock);
647 e = md_entry_lookup (md, key);
650 pthread_mutex_unlock (&md->lock);
654 if (e->type != MD_TYPE_UNSIGNED_INT)
656 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
657 pthread_mutex_unlock (&md->lock);
661 *value = e->value.mv_unsigned_int;
663 pthread_mutex_unlock (&md->lock);
665 } /* }}} int meta_data_get_unsigned_int */
667 int meta_data_get_double (meta_data_t *md, /* {{{ */
668 const char *key, double *value)
672 if ((md == NULL) || (key == NULL) || (value == NULL))
675 pthread_mutex_lock (&md->lock);
677 e = md_entry_lookup (md, key);
680 pthread_mutex_unlock (&md->lock);
684 if (e->type != MD_TYPE_DOUBLE)
686 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
687 pthread_mutex_unlock (&md->lock);
691 *value = e->value.mv_double;
693 pthread_mutex_unlock (&md->lock);
695 } /* }}} int meta_data_get_double */
697 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
698 const char *key, _Bool *value)
702 if ((md == NULL) || (key == NULL) || (value == NULL))
705 pthread_mutex_lock (&md->lock);
707 e = md_entry_lookup (md, key);
710 pthread_mutex_unlock (&md->lock);
714 if (e->type != MD_TYPE_BOOLEAN)
716 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
717 pthread_mutex_unlock (&md->lock);
721 *value = e->value.mv_boolean;
723 pthread_mutex_unlock (&md->lock);
725 } /* }}} int meta_data_get_boolean */
727 /* vim: set sw=2 sts=2 et fdm=marker : */