2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007,2008 Florian octo Forster
4 * Copyright (C) 2010 Andrés J. Díaz
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * Andrés J. Díaz <ajdiaz at connectical.com>
27 #include "utils_avltree.h"
28 #include "utils_cache.h"
29 #include "utils_threshold.h"
30 #include "utils_subst.h"
31 #include "meta_data.h"
36 typedef struct cache_entry_s
38 char name[6 * DATA_MAX_NAME_LEN];
40 gauge_t *values_gauge;
42 /* Time contained in the package
43 * (for calculating rates) */
45 /* Time according to the local clock
46 * (for purging old entries) */
48 /* Interval in which the data is collected
49 * (for purding old entries) */
55 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
56 * ! 0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! ...
57 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
58 * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
59 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
60 * ! t = 0 ! t = 1 ! t = 2 ! ...
61 * +-----------------+-----------------+-----------------+----
64 size_t history_index; /* points to the next position to write to. */
65 size_t history_length;
70 static c_avl_tree_t *cache_tree = NULL;
71 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
73 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
75 assert ((a != NULL) && (b != NULL));
76 return (strcmp (a->name, b->name));
77 } /* int cache_compare */
79 static cache_entry_t *cache_alloc (int values_num)
83 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
86 ERROR ("utils_cache: cache_alloc: malloc failed.");
89 memset (ce, '\0', sizeof (cache_entry_t));
90 ce->values_num = values_num;
92 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
93 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
94 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
96 sfree (ce->values_gauge);
97 sfree (ce->values_raw);
99 ERROR ("utils_cache: cache_alloc: calloc failed.");
104 ce->history_length = 0;
108 } /* cache_entry_t *cache_alloc */
110 static void cache_free (cache_entry_t *ce)
115 sfree (ce->values_gauge);
116 sfree (ce->values_raw);
118 if (ce->meta != NULL)
120 meta_data_destroy (ce->meta);
124 } /* void cache_free */
126 static int uc_send_notification (const char *name)
128 cache_entry_t *ce = NULL;
134 char *plugin_instance;
144 name_copy = strdup (name);
145 if (name_copy == NULL)
147 ERROR ("uc_send_notification: strdup failed.");
151 status = parse_identifier (name_copy, &host,
152 &plugin, &plugin_instance,
153 &type, &type_instance);
156 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
160 memset (&ds, '\0', sizeof (ds));
161 memset (&vl, '\0', sizeof (vl));
162 sstrncpy (vl.host, host, sizeof (vl.host));
163 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
164 if (plugin_instance != NULL)
165 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
166 sstrncpy (ds.type, type, sizeof (ds.type));
167 sstrncpy (vl.type, type, sizeof (vl.type));
168 if (type_instance != NULL)
169 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
172 /* Copy the associative members */
173 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
174 host, plugin, plugin_instance, type, type_instance);
177 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
180 pthread_mutex_lock (&cache_lock);
183 * Set the time _after_ getting the lock because we don't know how long
184 * acquiring the lock takes and we will use this time later to decide
185 * whether or not the state is OKAY.
187 n.time = time (NULL);
189 status = c_avl_get (cache_tree, name, (void *) &ce);
192 pthread_mutex_unlock (&cache_lock);
197 /* Check if the entry has been updated in the meantime */
198 if ((n.time - ce->last_update) < (2 * ce->interval))
200 ce->state = STATE_OKAY;
201 pthread_mutex_unlock (&cache_lock);
206 /* if the associated threshold has a missing message, then use custom
207 * message. FIXME: we do a threshold_search here and in uc_check_timeout
208 * (calling ut_check_interesting, but we really need to do this once */
209 if ( !ut_search_threshold(&vl, &th) &&
210 (th.missing_message != NULL) )
212 char msg[NOTIF_MAX_MSG_LEN];
213 char temp[NOTIF_MAX_MSG_LEN];
215 sstrncpy (msg, th.missing_message, sizeof (msg));
216 (void) ut_build_message (msg, NOTIF_MAX_MSG_LEN, th.missing_message,
217 &ds, 0, &vl, ce->values_gauge,
220 #define REPLACE_FIELD(t,v) \
221 if (subst_string (temp, sizeof (temp), msg, t, v) != NULL) \
222 sstrncpy (msg, temp, sizeof (msg));
224 char itoa_temp[NOTIF_MAX_MSG_LEN];
225 #define ITOA(string,i) \
226 memset(string,0x00,sizeof(string)); \
227 snprintf(string, sizeof(string), "%i", i);
229 ITOA(itoa_temp, (int)(n.time - ce->last_update))
230 REPLACE_FIELD("%{missing}", itoa_temp)
232 (void) ssnprintf (n.message, sizeof (n.message),
237 ssnprintf (n.message, sizeof (n.message),
238 "%s has not been updated for %i seconds.", name,
239 (int) (n.time - ce->last_update));
242 pthread_mutex_unlock (&cache_lock);
244 plugin_dispatch_notification (&n);
247 } /* int uc_send_notification */
249 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
253 for (i = 0; i < ds->ds_num; i++)
255 if (isnan (ce->values_gauge[i]))
257 else if (ce->values_gauge[i] < ds->ds[i].min)
258 ce->values_gauge[i] = NAN;
259 else if (ce->values_gauge[i] > ds->ds[i].max)
260 ce->values_gauge[i] = NAN;
262 } /* void uc_check_range */
264 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
271 /* `cache_lock' has been locked by `uc_update' */
273 key_copy = strdup (key);
274 if (key_copy == NULL)
276 ERROR ("uc_insert: strdup failed.");
280 ce = cache_alloc (ds->ds_num);
284 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
288 sstrncpy (ce->name, key, sizeof (ce->name));
290 for (i = 0; i < ds->ds_num; i++)
292 switch (ds->ds[i].type)
294 case DS_TYPE_COUNTER:
295 ce->values_gauge[i] = NAN;
296 ce->values_raw[i].counter = vl->values[i].counter;
300 ce->values_gauge[i] = vl->values[i].gauge;
301 ce->values_raw[i].gauge = vl->values[i].gauge;
305 ce->values_gauge[i] = NAN;
306 ce->values_raw[i].derive = vl->values[i].derive;
309 case DS_TYPE_ABSOLUTE:
310 ce->values_gauge[i] = NAN;
311 if (vl->interval > 0)
312 ce->values_gauge[i] = ((double) vl->values[i].absolute)
313 / ((double) vl->interval);
314 ce->values_raw[i].absolute = vl->values[i].absolute;
318 /* This shouldn't happen. */
319 ERROR ("uc_insert: Don't know how to handle data source type %i.",
322 } /* switch (ds->ds[i].type) */
325 /* Prune invalid gauge data */
326 uc_check_range (ds, ce);
328 ce->last_time = vl->time;
329 ce->last_update = time (NULL);
330 ce->interval = vl->interval;
331 ce->state = STATE_OKAY;
333 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
336 ERROR ("uc_insert: c_avl_insert failed.");
340 DEBUG ("uc_insert: Added %s to the cache.", key);
342 } /* int uc_insert */
346 if (cache_tree == NULL)
347 cache_tree = c_avl_create ((int (*) (const void *, const void *))
353 int uc_check_timeout (void)
362 c_avl_iterator_t *iter;
365 pthread_mutex_lock (&cache_lock);
369 /* Build a list of entries to be flushed */
370 iter = c_avl_get_iterator (cache_tree);
371 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
373 /* If entry has not been updated, add to `keys' array */
374 if ((now - ce->last_update) >= (timeout_g * ce->interval))
378 tmp = (char **) realloc ((void *) keys,
379 (keys_len + 1) * sizeof (char *));
382 ERROR ("uc_check_timeout: realloc failed.");
383 c_avl_iterator_destroy (iter);
385 pthread_mutex_unlock (&cache_lock);
390 keys[keys_len] = strdup (key);
391 if (keys[keys_len] == NULL)
393 ERROR ("uc_check_timeout: strdup failed.");
398 } /* while (c_avl_iterator_next) */
402 for (i = 0; i < keys_len; i++)
406 status = ut_check_interesting (keys[i]);
410 ERROR ("uc_check_timeout: ut_check_interesting failed.");
414 else if (status == 0) /* ``service'' is uninteresting */
416 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
419 status = c_avl_remove (cache_tree, keys[i],
420 (void *) &key, (void *) &ce);
423 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
432 /* If we get here, the value is ``interesting''. Query the record from the
433 * cache and update the state field. */
434 if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
436 ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
437 /* Do not free `keys[i]' so a notification is sent further down. */
442 if (status == 2) /* persist */
444 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
446 ce->state = STATE_MISSING;
447 /* Do not free `keys[i]' so a notification is sent further down. */
449 else if (status == 1) /* do not persist */
451 if (ce->state == STATE_MISSING)
453 DEBUG ("uc_check_timeout: %s is missing but "
454 "notification has already been sent.",
456 /* Set `keys[i]' to NULL to no notification is sent. */
459 else /* (ce->state != STATE_MISSING) */
461 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
463 ce->state = STATE_MISSING;
464 /* Do not free `keys[i]' so a notification is sent further down. */
469 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
470 "invalid status %i.",
475 /* Make really sure the next iteration doesn't work with this pointer.
476 * There have been too many bugs in the past.. :/ -- octo */
478 } /* for (keys[i]) */
480 c_avl_iterator_destroy (iter);
482 pthread_mutex_unlock (&cache_lock);
484 for (i = 0; i < keys_len; i++)
489 uc_send_notification (keys[i]);
496 } /* int uc_check_timeout */
498 int uc_update (const data_set_t *ds, const value_list_t *vl)
500 char name[6 * DATA_MAX_NAME_LEN];
501 cache_entry_t *ce = NULL;
502 int send_okay_notification = 0;
503 time_t update_delay = 0;
508 if (FORMAT_VL (name, sizeof (name), vl) != 0)
510 ERROR ("uc_update: FORMAT_VL failed.");
514 pthread_mutex_lock (&cache_lock);
516 status = c_avl_get (cache_tree, name, (void *) &ce);
517 if (status != 0) /* entry does not yet exist */
519 status = uc_insert (ds, vl, name);
520 pthread_mutex_unlock (&cache_lock);
525 assert (ce->values_num == ds->ds_num);
527 if (ce->last_time >= vl->time)
529 pthread_mutex_unlock (&cache_lock);
530 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
531 "last cache update = %u;",
532 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
536 /* Send a notification (after the lock has been released) if we switch the
537 * state from something else to `okay'. */
538 if (ce->state == STATE_MISSING)
540 send_okay_notification = 1;
541 ce->state = STATE_OKAY;
542 update_delay = time (NULL) - ce->last_update;
545 for (i = 0; i < ds->ds_num; i++)
547 switch (ds->ds[i].type)
549 case DS_TYPE_COUNTER:
553 /* check if the counter has wrapped around */
554 if (vl->values[i].counter < ce->values_raw[i].counter)
556 if (ce->values_raw[i].counter <= 4294967295U)
557 diff = (4294967295U - ce->values_raw[i].counter)
558 + vl->values[i].counter;
560 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
561 + vl->values[i].counter;
563 else /* counter has NOT wrapped around */
565 diff = vl->values[i].counter - ce->values_raw[i].counter;
568 ce->values_gauge[i] = ((double) diff)
569 / ((double) (vl->time - ce->last_time));
570 ce->values_raw[i].counter = vl->values[i].counter;
575 ce->values_raw[i].gauge = vl->values[i].gauge;
576 ce->values_gauge[i] = vl->values[i].gauge;
583 diff = vl->values[i].derive - ce->values_raw[i].derive;
585 ce->values_gauge[i] = ((double) diff)
586 / ((double) (vl->time - ce->last_time));
587 ce->values_raw[i].derive = vl->values[i].derive;
591 case DS_TYPE_ABSOLUTE:
592 ce->values_gauge[i] = ((double) vl->values[i].absolute)
593 / ((double) (vl->time - ce->last_time));
594 ce->values_raw[i].absolute = vl->values[i].absolute;
598 /* This shouldn't happen. */
599 pthread_mutex_unlock (&cache_lock);
600 ERROR ("uc_update: Don't know how to handle data source type %i.",
603 } /* switch (ds->ds[i].type) */
605 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
608 /* Update the history if it exists. */
609 if (ce->history != NULL)
611 assert (ce->history_index < ce->history_length);
612 for (i = 0; i < ce->values_num; i++)
614 size_t hist_idx = (ce->values_num * ce->history_index) + i;
615 ce->history[hist_idx] = ce->values_gauge[i];
618 assert (ce->history_length > 0);
619 ce->history_index = (ce->history_index + 1) % ce->history_length;
622 /* Prune invalid gauge data */
623 uc_check_range (ds, ce);
625 ce->last_time = vl->time;
626 ce->last_update = time (NULL);
627 ce->interval = vl->interval;
629 pthread_mutex_unlock (&cache_lock);
631 if (send_okay_notification == 0)
634 /* Do not send okay notifications for uninteresting values, i. e. values for
635 * which no threshold is configured. */
636 status = ut_check_interesting (name);
640 /* Initialize the notification */
641 memset (&n, '\0', sizeof (n));
642 NOTIFICATION_INIT_VL (&n, vl, ds);
644 n.severity = NOTIF_OKAY;
647 ssnprintf (n.message, sizeof (n.message),
648 "Received a value for %s. It was missing for %u seconds.",
649 name, (unsigned int) update_delay);
651 plugin_dispatch_notification (&n);
654 } /* int uc_update */
656 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
660 cache_entry_t *ce = NULL;
663 pthread_mutex_lock (&cache_lock);
665 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
669 /* remove missing values from getval */
670 if (ce->state == STATE_MISSING)
676 ret_num = ce->values_num;
677 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
680 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
685 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
691 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
695 pthread_mutex_unlock (&cache_lock);
700 *ret_values_num = ret_num;
704 } /* gauge_t *uc_get_rate_by_name */
706 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
708 char name[6 * DATA_MAX_NAME_LEN];
713 if (FORMAT_VL (name, sizeof (name), vl) != 0)
715 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
719 status = uc_get_rate_by_name (name, &ret, &ret_num);
723 /* This is important - the caller has no other way of knowing how many
724 * values are returned. */
725 if (ret_num != (size_t) ds->ds_num)
727 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
728 "but uc_get_rate_by_name returned %zu.",
729 ds->type, ds->ds_num, ret_num);
735 } /* gauge_t *uc_get_rate */
737 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
739 c_avl_iterator_t *iter;
741 cache_entry_t *value;
744 time_t *times = NULL;
749 if ((ret_names == NULL) || (ret_number == NULL))
752 pthread_mutex_lock (&cache_lock);
754 iter = c_avl_get_iterator (cache_tree);
755 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
759 /* remove missing values when list values */
760 if (value->state == STATE_MISSING)
763 if (ret_times != NULL)
767 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
768 if (tmp_times == NULL)
774 times[number] = value->last_time;
777 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
784 names[number] = strdup (key);
785 if (names[number] == NULL)
791 } /* while (c_avl_iterator_next) */
793 c_avl_iterator_destroy (iter);
794 pthread_mutex_unlock (&cache_lock);
800 for (i = 0; i < number; i++)
810 if (ret_times != NULL)
812 *ret_number = number;
815 } /* int uc_get_names */
817 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
819 char name[6 * DATA_MAX_NAME_LEN];
820 cache_entry_t *ce = NULL;
821 int ret = STATE_ERROR;
823 if (FORMAT_VL (name, sizeof (name), vl) != 0)
825 ERROR ("uc_get_state: FORMAT_VL failed.");
826 return (STATE_ERROR);
829 pthread_mutex_lock (&cache_lock);
831 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
837 pthread_mutex_unlock (&cache_lock);
840 } /* int uc_get_state */
842 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
844 char name[6 * DATA_MAX_NAME_LEN];
845 cache_entry_t *ce = NULL;
848 if (FORMAT_VL (name, sizeof (name), vl) != 0)
850 ERROR ("uc_get_state: FORMAT_VL failed.");
851 return (STATE_ERROR);
854 pthread_mutex_lock (&cache_lock);
856 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
863 pthread_mutex_unlock (&cache_lock);
866 } /* int uc_set_state */
868 int uc_get_history_by_name (const char *name,
869 gauge_t *ret_history, size_t num_steps, size_t num_ds)
871 cache_entry_t *ce = NULL;
875 pthread_mutex_lock (&cache_lock);
877 status = c_avl_get (cache_tree, name, (void *) &ce);
880 pthread_mutex_unlock (&cache_lock);
884 if (((size_t) ce->values_num) != num_ds)
886 pthread_mutex_unlock (&cache_lock);
890 /* Check if there are enough values available. If not, increase the buffer
892 if (ce->history_length < num_steps)
897 tmp = realloc (ce->history, sizeof (*ce->history)
898 * num_steps * ce->values_num);
901 pthread_mutex_unlock (&cache_lock);
905 for (i = ce->history_length * ce->values_num;
906 i < (num_steps * ce->values_num);
911 ce->history_length = num_steps;
912 } /* if (ce->history_length < num_steps) */
914 /* Copy the values to the output buffer. */
915 for (i = 0; i < num_steps; i++)
920 if (i < ce->history_index)
921 src_index = ce->history_index - (i + 1);
923 src_index = ce->history_length + ce->history_index - (i + 1);
924 src_index = src_index * num_ds;
926 dst_index = i * num_ds;
928 memcpy (ret_history + dst_index, ce->history + src_index,
929 sizeof (*ret_history) * num_ds);
932 pthread_mutex_unlock (&cache_lock);
935 } /* int uc_get_history_by_name */
937 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
938 gauge_t *ret_history, size_t num_steps, size_t num_ds)
940 char name[6 * DATA_MAX_NAME_LEN];
942 if (FORMAT_VL (name, sizeof (name), vl) != 0)
944 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
948 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
949 } /* int uc_get_history */
951 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
953 char name[6 * DATA_MAX_NAME_LEN];
954 cache_entry_t *ce = NULL;
955 int ret = STATE_ERROR;
957 if (FORMAT_VL (name, sizeof (name), vl) != 0)
959 ERROR ("uc_get_state: FORMAT_VL failed.");
960 return (STATE_ERROR);
963 pthread_mutex_lock (&cache_lock);
965 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
971 pthread_mutex_unlock (&cache_lock);
974 } /* int uc_get_hits */
976 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
978 char name[6 * DATA_MAX_NAME_LEN];
979 cache_entry_t *ce = NULL;
982 if (FORMAT_VL (name, sizeof (name), vl) != 0)
984 ERROR ("uc_get_state: FORMAT_VL failed.");
985 return (STATE_ERROR);
988 pthread_mutex_lock (&cache_lock);
990 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
997 pthread_mutex_unlock (&cache_lock);
1000 } /* int uc_set_hits */
1002 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
1004 char name[6 * DATA_MAX_NAME_LEN];
1005 cache_entry_t *ce = NULL;
1008 if (FORMAT_VL (name, sizeof (name), vl) != 0)
1010 ERROR ("uc_get_state: FORMAT_VL failed.");
1011 return (STATE_ERROR);
1014 pthread_mutex_lock (&cache_lock);
1016 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
1018 assert (ce != NULL);
1020 ce->hits = ret + step;
1023 pthread_mutex_unlock (&cache_lock);
1026 } /* int uc_inc_hits */
1029 * Meta data interface
1031 /* XXX: This function will acquire `cache_lock' but will not free it! */
1032 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
1034 char name[6 * DATA_MAX_NAME_LEN];
1035 cache_entry_t *ce = NULL;
1038 status = FORMAT_VL (name, sizeof (name), vl);
1041 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
1045 pthread_mutex_lock (&cache_lock);
1047 status = c_avl_get (cache_tree, name, (void *) &ce);
1050 pthread_mutex_unlock (&cache_lock);
1053 assert (ce != NULL);
1055 if (ce->meta == NULL)
1056 ce->meta = meta_data_create ();
1058 if (ce->meta == NULL)
1059 pthread_mutex_unlock (&cache_lock);
1062 } /* }}} meta_data_t *uc_get_meta */
1064 /* Sorry about this preprocessor magic, but it really makes this file much
1066 #define UC_WRAP(wrap_function) { \
1067 meta_data_t *meta; \
1069 meta = uc_get_meta (vl); \
1070 if (meta == NULL) return (-1); \
1071 status = wrap_function (meta, key); \
1072 pthread_mutex_unlock (&cache_lock); \
1075 int uc_meta_data_exists (const value_list_t *vl, const char *key)
1076 UC_WRAP (meta_data_exists)
1078 int uc_meta_data_delete (const value_list_t *vl, const char *key)
1079 UC_WRAP (meta_data_delete)
1082 /* We need a new version of this macro because the following functions take
1084 #define UC_WRAP(wrap_function) { \
1085 meta_data_t *meta; \
1087 meta = uc_get_meta (vl); \
1088 if (meta == NULL) return (-1); \
1089 status = wrap_function (meta, key, value); \
1090 pthread_mutex_unlock (&cache_lock); \
1093 int uc_meta_data_add_string (const value_list_t *vl,
1096 UC_WRAP(meta_data_add_string)
1097 int uc_meta_data_add_signed_int (const value_list_t *vl,
1100 UC_WRAP(meta_data_add_signed_int)
1101 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
1104 UC_WRAP(meta_data_add_unsigned_int)
1105 int uc_meta_data_add_double (const value_list_t *vl,
1108 UC_WRAP(meta_data_add_double)
1109 int uc_meta_data_add_boolean (const value_list_t *vl,
1112 UC_WRAP(meta_data_add_boolean)
1114 int uc_meta_data_get_string (const value_list_t *vl,
1117 UC_WRAP(meta_data_get_string)
1118 int uc_meta_data_get_signed_int (const value_list_t *vl,
1121 UC_WRAP(meta_data_get_signed_int)
1122 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
1125 UC_WRAP(meta_data_get_unsigned_int)
1126 int uc_meta_data_get_double (const value_list_t *vl,
1129 UC_WRAP(meta_data_get_double)
1130 int uc_meta_data_get_boolean (const value_list_t *vl,
1133 UC_WRAP(meta_data_get_boolean)
1136 /* vim: set sw=2 ts=8 sts=2 tw=78 : */