2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007,2008 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>
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
27 #include "utils_threshold.h"
28 #include "utils_subst.h"
29 #include "meta_data.h"
34 typedef struct cache_entry_s
36 char name[6 * DATA_MAX_NAME_LEN];
38 gauge_t *values_gauge;
40 /* Time contained in the package
41 * (for calculating rates) */
43 /* Time according to the local clock
44 * (for purging old entries) */
46 /* Interval in which the data is collected
47 * (for purding old entries) */
53 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
54 * ! 0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! ...
55 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
56 * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
57 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
58 * ! t = 0 ! t = 1 ! t = 2 ! ...
59 * +-----------------+-----------------+-----------------+----
62 size_t history_index; /* points to the next position to write to. */
63 size_t history_length;
68 static c_avl_tree_t *cache_tree = NULL;
69 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
71 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
73 assert ((a != NULL) && (b != NULL));
74 return (strcmp (a->name, b->name));
75 } /* int cache_compare */
77 static cache_entry_t *cache_alloc (int values_num)
81 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
84 ERROR ("utils_cache: cache_alloc: malloc failed.");
87 memset (ce, '\0', sizeof (cache_entry_t));
88 ce->values_num = values_num;
90 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
91 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
92 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
94 sfree (ce->values_gauge);
95 sfree (ce->values_raw);
97 ERROR ("utils_cache: cache_alloc: calloc failed.");
102 ce->history_length = 0;
106 } /* cache_entry_t *cache_alloc */
108 static void cache_free (cache_entry_t *ce)
113 sfree (ce->values_gauge);
114 sfree (ce->values_raw);
116 if (ce->meta != NULL)
118 meta_data_destroy (ce->meta);
122 } /* void cache_free */
124 /* Send out notifications for "missing" values. */
125 static int uc_send_notification (const char *name)
127 cache_entry_t *ce = NULL;
133 char *plugin_instance;
143 name_copy = strdup (name);
144 if (name_copy == NULL)
146 ERROR ("uc_send_notification: strdup failed.");
150 status = parse_identifier (name_copy, &host,
151 &plugin, &plugin_instance,
152 &type, &type_instance);
155 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
159 memset (&ds, 0, sizeof (ds));
160 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));
171 /* Copy the associative members */
172 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
173 host, plugin, plugin_instance, type, type_instance);
176 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
179 pthread_mutex_lock (&cache_lock);
182 * Set the time _after_ getting the lock because we don't know how long
183 * acquiring the lock takes and we will use this time later to decide
184 * whether or not the state is OKAY.
186 n.time = time (NULL);
188 status = c_avl_get (cache_tree, name, (void *) &ce);
191 pthread_mutex_unlock (&cache_lock);
196 /* Check if the entry has been updated since we marked it as "missing" in
197 * uc_check_timeout() */
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 status = ut_search_threshold (&vl, &th);
210 while ((status == 0) && (th.missing_message != NULL))
212 char msg[NOTIF_MAX_MSG_LEN];
213 char temp[NOTIF_MAX_MSG_LEN];
214 char missing_str[32] = "";
216 sstrncpy (msg, th.missing_message, sizeof (msg));
217 status = ut_build_message (msg, sizeof (msg),
218 /* format = */ th.missing_message,
219 &ds, /* ds index = */ -1,
220 &vl, ce->values_gauge,
225 (void) ssnprintf (missing_str, sizeof (missing_str), "%li",
226 (long) (n.time - ce->last_update));
228 if (subst_string (temp, sizeof (temp), msg,
229 "%{missing}", missing_str) != NULL)
230 sstrncpy (msg, temp, sizeof (msg));
232 sstrncpy (n.message, msg, sizeof (n.message));
236 /* "ut_search_threshold" returned an error, there is no "missing_message" or
237 * "ut_build_message" failed. Use the generic default. */
238 if ((status != 0) || (th.missing_message == NULL))
240 ssnprintf (n.message, sizeof (n.message),
241 "\"%s\" has not been updated for %li seconds.", name,
242 (long) (n.time - ce->last_update));
245 pthread_mutex_unlock (&cache_lock);
247 plugin_dispatch_notification (&n);
250 } /* int uc_send_notification */
252 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
256 for (i = 0; i < ds->ds_num; i++)
258 if (isnan (ce->values_gauge[i]))
260 else if (ce->values_gauge[i] < ds->ds[i].min)
261 ce->values_gauge[i] = NAN;
262 else if (ce->values_gauge[i] > ds->ds[i].max)
263 ce->values_gauge[i] = NAN;
265 } /* void uc_check_range */
267 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
274 /* `cache_lock' has been locked by `uc_update' */
276 key_copy = strdup (key);
277 if (key_copy == NULL)
279 ERROR ("uc_insert: strdup failed.");
283 ce = cache_alloc (ds->ds_num);
287 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
291 sstrncpy (ce->name, key, sizeof (ce->name));
293 for (i = 0; i < ds->ds_num; i++)
295 switch (ds->ds[i].type)
297 case DS_TYPE_COUNTER:
298 ce->values_gauge[i] = NAN;
299 ce->values_raw[i].counter = vl->values[i].counter;
303 ce->values_gauge[i] = vl->values[i].gauge;
304 ce->values_raw[i].gauge = vl->values[i].gauge;
308 ce->values_gauge[i] = NAN;
309 ce->values_raw[i].derive = vl->values[i].derive;
312 case DS_TYPE_ABSOLUTE:
313 ce->values_gauge[i] = NAN;
314 if (vl->interval > 0)
315 ce->values_gauge[i] = ((double) vl->values[i].absolute)
316 / ((double) vl->interval);
317 ce->values_raw[i].absolute = vl->values[i].absolute;
321 /* This shouldn't happen. */
322 ERROR ("uc_insert: Don't know how to handle data source type %i.",
325 } /* switch (ds->ds[i].type) */
328 /* Prune invalid gauge data */
329 uc_check_range (ds, ce);
331 ce->last_time = vl->time;
332 ce->last_update = time (NULL);
333 ce->interval = vl->interval;
334 ce->state = STATE_OKAY;
336 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
339 ERROR ("uc_insert: c_avl_insert failed.");
343 DEBUG ("uc_insert: Added %s to the cache.", key);
345 } /* int uc_insert */
349 if (cache_tree == NULL)
350 cache_tree = c_avl_create ((int (*) (const void *, const void *))
356 int uc_check_timeout (void)
365 c_avl_iterator_t *iter;
368 pthread_mutex_lock (&cache_lock);
372 /* Build a list of entries to be flushed */
373 iter = c_avl_get_iterator (cache_tree);
374 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
376 /* If entry has not been updated, add to `keys' array */
377 if ((now - ce->last_update) >= (timeout_g * ce->interval))
381 tmp = (char **) realloc ((void *) keys,
382 (keys_len + 1) * sizeof (char *));
385 ERROR ("uc_check_timeout: realloc failed.");
386 c_avl_iterator_destroy (iter);
388 pthread_mutex_unlock (&cache_lock);
393 keys[keys_len] = strdup (key);
394 if (keys[keys_len] == NULL)
396 ERROR ("uc_check_timeout: strdup failed.");
401 } /* while (c_avl_iterator_next) */
405 for (i = 0; i < keys_len; i++)
409 status = ut_check_interesting (keys[i]);
413 ERROR ("uc_check_timeout: ut_check_interesting failed.");
417 else if (status == 0) /* ``service'' is uninteresting */
419 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
422 status = c_avl_remove (cache_tree, keys[i],
423 (void *) &key, (void *) &ce);
426 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
435 /* If we get here, the value is ``interesting''. Query the record from the
436 * cache and update the state field. */
437 if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
439 ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
440 /* Do not free `keys[i]' so a notification is sent further down. */
445 if (status == 2) /* persist */
447 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
449 ce->state = STATE_MISSING;
450 /* Do not free `keys[i]' so a notification is sent further down. */
452 else if (status == 1) /* do not persist */
454 if (ce->state == STATE_MISSING)
456 DEBUG ("uc_check_timeout: %s is missing but "
457 "notification has already been sent.",
459 /* Set `keys[i]' to NULL to no notification is sent. */
462 else /* (ce->state != STATE_MISSING) */
464 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
466 ce->state = STATE_MISSING;
467 /* Do not free `keys[i]' so a notification is sent further down. */
472 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
473 "invalid status %i.",
478 /* Make really sure the next iteration doesn't work with this pointer.
479 * There have been too many bugs in the past.. :/ -- octo */
481 } /* for (keys[i]) */
483 c_avl_iterator_destroy (iter);
485 pthread_mutex_unlock (&cache_lock);
487 for (i = 0; i < keys_len; i++)
492 uc_send_notification (keys[i]);
499 } /* int uc_check_timeout */
501 int uc_update (const data_set_t *ds, const value_list_t *vl)
503 char name[6 * DATA_MAX_NAME_LEN];
504 cache_entry_t *ce = NULL;
505 int send_okay_notification = 0;
506 time_t update_delay = 0;
511 if (FORMAT_VL (name, sizeof (name), vl) != 0)
513 ERROR ("uc_update: FORMAT_VL failed.");
517 pthread_mutex_lock (&cache_lock);
519 status = c_avl_get (cache_tree, name, (void *) &ce);
520 if (status != 0) /* entry does not yet exist */
522 status = uc_insert (ds, vl, name);
523 pthread_mutex_unlock (&cache_lock);
528 assert (ce->values_num == ds->ds_num);
530 if (ce->last_time >= vl->time)
532 pthread_mutex_unlock (&cache_lock);
533 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
534 "last cache update = %u;",
535 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
539 /* Send a notification (after the lock has been released) if we switch the
540 * state from something else to `okay'. */
541 if (ce->state == STATE_MISSING)
543 send_okay_notification = 1;
544 ce->state = STATE_OKAY;
545 update_delay = time (NULL) - ce->last_update;
548 for (i = 0; i < ds->ds_num; i++)
550 switch (ds->ds[i].type)
552 case DS_TYPE_COUNTER:
556 /* check if the counter has wrapped around */
557 if (vl->values[i].counter < ce->values_raw[i].counter)
559 if (ce->values_raw[i].counter <= 4294967295U)
560 diff = (4294967295U - ce->values_raw[i].counter)
561 + vl->values[i].counter;
563 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
564 + vl->values[i].counter;
566 else /* counter has NOT wrapped around */
568 diff = vl->values[i].counter - ce->values_raw[i].counter;
571 ce->values_gauge[i] = ((double) diff)
572 / ((double) (vl->time - ce->last_time));
573 ce->values_raw[i].counter = vl->values[i].counter;
578 ce->values_raw[i].gauge = vl->values[i].gauge;
579 ce->values_gauge[i] = vl->values[i].gauge;
586 diff = vl->values[i].derive - ce->values_raw[i].derive;
588 ce->values_gauge[i] = ((double) diff)
589 / ((double) (vl->time - ce->last_time));
590 ce->values_raw[i].derive = vl->values[i].derive;
594 case DS_TYPE_ABSOLUTE:
595 ce->values_gauge[i] = ((double) vl->values[i].absolute)
596 / ((double) (vl->time - ce->last_time));
597 ce->values_raw[i].absolute = vl->values[i].absolute;
601 /* This shouldn't happen. */
602 pthread_mutex_unlock (&cache_lock);
603 ERROR ("uc_update: Don't know how to handle data source type %i.",
606 } /* switch (ds->ds[i].type) */
608 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
611 /* Update the history if it exists. */
612 if (ce->history != NULL)
614 assert (ce->history_index < ce->history_length);
615 for (i = 0; i < ce->values_num; i++)
617 size_t hist_idx = (ce->values_num * ce->history_index) + i;
618 ce->history[hist_idx] = ce->values_gauge[i];
621 assert (ce->history_length > 0);
622 ce->history_index = (ce->history_index + 1) % ce->history_length;
625 /* Prune invalid gauge data */
626 uc_check_range (ds, ce);
628 ce->last_time = vl->time;
629 ce->last_update = time (NULL);
630 ce->interval = vl->interval;
632 pthread_mutex_unlock (&cache_lock);
634 if (send_okay_notification == 0)
637 /* Do not send okay notifications for uninteresting values, i. e. values for
638 * which no threshold is configured. */
639 status = ut_check_interesting (name);
643 /* Initialize the notification */
644 memset (&n, '\0', sizeof (n));
645 NOTIFICATION_INIT_VL (&n, vl, ds);
647 n.severity = NOTIF_OKAY;
650 ssnprintf (n.message, sizeof (n.message),
651 "Received a value for %s. It was missing for %u seconds.",
652 name, (unsigned int) update_delay);
654 plugin_dispatch_notification (&n);
657 } /* int uc_update */
659 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
663 cache_entry_t *ce = NULL;
666 pthread_mutex_lock (&cache_lock);
668 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
672 /* remove missing values from getval */
673 if (ce->state == STATE_MISSING)
679 ret_num = ce->values_num;
680 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
683 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
688 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
694 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
698 pthread_mutex_unlock (&cache_lock);
703 *ret_values_num = ret_num;
707 } /* gauge_t *uc_get_rate_by_name */
709 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
711 char name[6 * DATA_MAX_NAME_LEN];
716 if (FORMAT_VL (name, sizeof (name), vl) != 0)
718 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
722 status = uc_get_rate_by_name (name, &ret, &ret_num);
726 /* This is important - the caller has no other way of knowing how many
727 * values are returned. */
728 if (ret_num != (size_t) ds->ds_num)
730 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
731 "but uc_get_rate_by_name returned %zu.",
732 ds->type, ds->ds_num, ret_num);
738 } /* gauge_t *uc_get_rate */
740 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
742 c_avl_iterator_t *iter;
744 cache_entry_t *value;
747 time_t *times = NULL;
752 if ((ret_names == NULL) || (ret_number == NULL))
755 pthread_mutex_lock (&cache_lock);
757 iter = c_avl_get_iterator (cache_tree);
758 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
762 /* remove missing values when list values */
763 if (value->state == STATE_MISSING)
766 if (ret_times != NULL)
770 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
771 if (tmp_times == NULL)
777 times[number] = value->last_time;
780 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
787 names[number] = strdup (key);
788 if (names[number] == NULL)
794 } /* while (c_avl_iterator_next) */
796 c_avl_iterator_destroy (iter);
797 pthread_mutex_unlock (&cache_lock);
803 for (i = 0; i < number; i++)
813 if (ret_times != NULL)
815 *ret_number = number;
818 } /* int uc_get_names */
820 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
822 char name[6 * DATA_MAX_NAME_LEN];
823 cache_entry_t *ce = NULL;
824 int ret = STATE_ERROR;
826 if (FORMAT_VL (name, sizeof (name), vl) != 0)
828 ERROR ("uc_get_state: FORMAT_VL failed.");
829 return (STATE_ERROR);
832 pthread_mutex_lock (&cache_lock);
834 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
840 pthread_mutex_unlock (&cache_lock);
843 } /* int uc_get_state */
845 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
847 char name[6 * DATA_MAX_NAME_LEN];
848 cache_entry_t *ce = NULL;
851 if (FORMAT_VL (name, sizeof (name), vl) != 0)
853 ERROR ("uc_get_state: FORMAT_VL failed.");
854 return (STATE_ERROR);
857 pthread_mutex_lock (&cache_lock);
859 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
866 pthread_mutex_unlock (&cache_lock);
869 } /* int uc_set_state */
871 int uc_get_history_by_name (const char *name,
872 gauge_t *ret_history, size_t num_steps, size_t num_ds)
874 cache_entry_t *ce = NULL;
878 pthread_mutex_lock (&cache_lock);
880 status = c_avl_get (cache_tree, name, (void *) &ce);
883 pthread_mutex_unlock (&cache_lock);
887 if (((size_t) ce->values_num) != num_ds)
889 pthread_mutex_unlock (&cache_lock);
893 /* Check if there are enough values available. If not, increase the buffer
895 if (ce->history_length < num_steps)
900 tmp = realloc (ce->history, sizeof (*ce->history)
901 * num_steps * ce->values_num);
904 pthread_mutex_unlock (&cache_lock);
908 for (i = ce->history_length * ce->values_num;
909 i < (num_steps * ce->values_num);
914 ce->history_length = num_steps;
915 } /* if (ce->history_length < num_steps) */
917 /* Copy the values to the output buffer. */
918 for (i = 0; i < num_steps; i++)
923 if (i < ce->history_index)
924 src_index = ce->history_index - (i + 1);
926 src_index = ce->history_length + ce->history_index - (i + 1);
927 src_index = src_index * num_ds;
929 dst_index = i * num_ds;
931 memcpy (ret_history + dst_index, ce->history + src_index,
932 sizeof (*ret_history) * num_ds);
935 pthread_mutex_unlock (&cache_lock);
938 } /* int uc_get_history_by_name */
940 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
941 gauge_t *ret_history, size_t num_steps, size_t num_ds)
943 char name[6 * DATA_MAX_NAME_LEN];
945 if (FORMAT_VL (name, sizeof (name), vl) != 0)
947 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
951 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
952 } /* int uc_get_history */
954 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
956 char name[6 * DATA_MAX_NAME_LEN];
957 cache_entry_t *ce = NULL;
958 int ret = STATE_ERROR;
960 if (FORMAT_VL (name, sizeof (name), vl) != 0)
962 ERROR ("uc_get_state: FORMAT_VL failed.");
963 return (STATE_ERROR);
966 pthread_mutex_lock (&cache_lock);
968 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
974 pthread_mutex_unlock (&cache_lock);
977 } /* int uc_get_hits */
979 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
981 char name[6 * DATA_MAX_NAME_LEN];
982 cache_entry_t *ce = NULL;
985 if (FORMAT_VL (name, sizeof (name), vl) != 0)
987 ERROR ("uc_get_state: FORMAT_VL failed.");
988 return (STATE_ERROR);
991 pthread_mutex_lock (&cache_lock);
993 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
1000 pthread_mutex_unlock (&cache_lock);
1003 } /* int uc_set_hits */
1005 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
1007 char name[6 * DATA_MAX_NAME_LEN];
1008 cache_entry_t *ce = NULL;
1011 if (FORMAT_VL (name, sizeof (name), vl) != 0)
1013 ERROR ("uc_get_state: FORMAT_VL failed.");
1014 return (STATE_ERROR);
1017 pthread_mutex_lock (&cache_lock);
1019 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
1021 assert (ce != NULL);
1023 ce->hits = ret + step;
1026 pthread_mutex_unlock (&cache_lock);
1029 } /* int uc_inc_hits */
1032 * Meta data interface
1034 /* XXX: This function will acquire `cache_lock' but will not free it! */
1035 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
1037 char name[6 * DATA_MAX_NAME_LEN];
1038 cache_entry_t *ce = NULL;
1041 status = FORMAT_VL (name, sizeof (name), vl);
1044 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
1048 pthread_mutex_lock (&cache_lock);
1050 status = c_avl_get (cache_tree, name, (void *) &ce);
1053 pthread_mutex_unlock (&cache_lock);
1056 assert (ce != NULL);
1058 if (ce->meta == NULL)
1059 ce->meta = meta_data_create ();
1061 if (ce->meta == NULL)
1062 pthread_mutex_unlock (&cache_lock);
1065 } /* }}} meta_data_t *uc_get_meta */
1067 /* Sorry about this preprocessor magic, but it really makes this file much
1069 #define UC_WRAP(wrap_function) { \
1070 meta_data_t *meta; \
1072 meta = uc_get_meta (vl); \
1073 if (meta == NULL) return (-1); \
1074 status = wrap_function (meta, key); \
1075 pthread_mutex_unlock (&cache_lock); \
1078 int uc_meta_data_exists (const value_list_t *vl, const char *key)
1079 UC_WRAP (meta_data_exists)
1081 int uc_meta_data_delete (const value_list_t *vl, const char *key)
1082 UC_WRAP (meta_data_delete)
1085 /* We need a new version of this macro because the following functions take
1087 #define UC_WRAP(wrap_function) { \
1088 meta_data_t *meta; \
1090 meta = uc_get_meta (vl); \
1091 if (meta == NULL) return (-1); \
1092 status = wrap_function (meta, key, value); \
1093 pthread_mutex_unlock (&cache_lock); \
1096 int uc_meta_data_add_string (const value_list_t *vl,
1099 UC_WRAP(meta_data_add_string)
1100 int uc_meta_data_add_signed_int (const value_list_t *vl,
1103 UC_WRAP(meta_data_add_signed_int)
1104 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
1107 UC_WRAP(meta_data_add_unsigned_int)
1108 int uc_meta_data_add_double (const value_list_t *vl,
1111 UC_WRAP(meta_data_add_double)
1112 int uc_meta_data_add_boolean (const value_list_t *vl,
1115 UC_WRAP(meta_data_add_boolean)
1117 int uc_meta_data_get_string (const value_list_t *vl,
1120 UC_WRAP(meta_data_get_string)
1121 int uc_meta_data_get_signed_int (const value_list_t *vl,
1124 UC_WRAP(meta_data_get_signed_int)
1125 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
1128 UC_WRAP(meta_data_get_unsigned_int)
1129 int uc_meta_data_get_double (const value_list_t *vl,
1132 UC_WRAP(meta_data_get_double)
1133 int uc_meta_data_get_boolean (const value_list_t *vl,
1136 UC_WRAP(meta_data_get_boolean)
1139 /* vim: set sw=2 ts=8 sts=2 tw=78 : */