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 static int uc_send_notification (const char *name)
126 cache_entry_t *ce = NULL;
132 char *plugin_instance;
142 name_copy = strdup (name);
143 if (name_copy == NULL)
145 ERROR ("uc_send_notification: strdup failed.");
149 status = parse_identifier (name_copy, &host,
150 &plugin, &plugin_instance,
151 &type, &type_instance);
154 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
158 memset (&ds, '\0', sizeof (ds));
159 memset (&vl, '\0', sizeof (vl));
160 sstrncpy (vl.host, host, sizeof (vl.host));
161 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
162 if (plugin_instance != NULL)
163 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
164 sstrncpy (ds.type, type, sizeof (ds.type));
165 sstrncpy (vl.type, type, sizeof (vl.type));
166 if (type_instance != NULL)
167 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
170 /* Copy the associative members */
171 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
172 host, plugin, plugin_instance, type, type_instance);
175 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
178 pthread_mutex_lock (&cache_lock);
181 * Set the time _after_ getting the lock because we don't know how long
182 * acquiring the lock takes and we will use this time later to decide
183 * whether or not the state is OKAY.
185 n.time = time (NULL);
187 status = c_avl_get (cache_tree, name, (void *) &ce);
190 pthread_mutex_unlock (&cache_lock);
195 /* Check if the entry has been updated in the meantime */
196 if ((n.time - ce->last_update) < (2 * ce->interval))
198 ce->state = STATE_OKAY;
199 pthread_mutex_unlock (&cache_lock);
204 /* if the associated threshold has a missing message, then use custom
205 * message. FIXME: we do a threshold_search here and in uc_check_timeout
206 * (calling ut_check_interesting, but we really need to do this once */
207 if ( !ut_search_threshold(&vl, &th) &&
208 (th.missing_message != NULL) )
210 char msg[NOTIF_MAX_MSG_LEN];
211 char temp[NOTIF_MAX_MSG_LEN];
213 sstrncpy (msg, th.missing_message, sizeof (msg));
214 (void) ut_build_message (msg, NOTIF_MAX_MSG_LEN, th.missing_message,
215 &ds, 0, &vl, ce->values_gauge,
218 #define REPLACE_FIELD(t,v) \
219 if (subst_string (temp, sizeof (temp), msg, t, v) != NULL) \
220 sstrncpy (msg, temp, sizeof (msg));
222 char itoa_temp[NOTIF_MAX_MSG_LEN];
223 #define ITOA(string,i) \
224 memset(string,0x00,sizeof(string)); \
225 snprintf(string, sizeof(string), "%i", i);
227 ITOA(itoa_temp, (int)(n.time - ce->last_update))
228 REPLACE_FIELD("%{missing}", itoa_temp)
230 (void) ssnprintf (n.message, sizeof (n.message),
235 ssnprintf (n.message, sizeof (n.message),
236 "%s has not been updated for %i seconds.", name,
237 (int) (n.time - ce->last_update));
240 pthread_mutex_unlock (&cache_lock);
242 plugin_dispatch_notification (&n);
245 } /* int uc_send_notification */
247 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
251 for (i = 0; i < ds->ds_num; i++)
253 if (isnan (ce->values_gauge[i]))
255 else if (ce->values_gauge[i] < ds->ds[i].min)
256 ce->values_gauge[i] = NAN;
257 else if (ce->values_gauge[i] > ds->ds[i].max)
258 ce->values_gauge[i] = NAN;
260 } /* void uc_check_range */
262 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
269 /* `cache_lock' has been locked by `uc_update' */
271 key_copy = strdup (key);
272 if (key_copy == NULL)
274 ERROR ("uc_insert: strdup failed.");
278 ce = cache_alloc (ds->ds_num);
282 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
286 sstrncpy (ce->name, key, sizeof (ce->name));
288 for (i = 0; i < ds->ds_num; i++)
290 switch (ds->ds[i].type)
292 case DS_TYPE_COUNTER:
293 ce->values_gauge[i] = NAN;
294 ce->values_raw[i].counter = vl->values[i].counter;
298 ce->values_gauge[i] = vl->values[i].gauge;
299 ce->values_raw[i].gauge = vl->values[i].gauge;
303 ce->values_gauge[i] = NAN;
304 ce->values_raw[i].derive = vl->values[i].derive;
307 case DS_TYPE_ABSOLUTE:
308 ce->values_gauge[i] = NAN;
309 if (vl->interval > 0)
310 ce->values_gauge[i] = ((double) vl->values[i].absolute)
311 / ((double) vl->interval);
312 ce->values_raw[i].absolute = vl->values[i].absolute;
316 /* This shouldn't happen. */
317 ERROR ("uc_insert: Don't know how to handle data source type %i.",
320 } /* switch (ds->ds[i].type) */
323 /* Prune invalid gauge data */
324 uc_check_range (ds, ce);
326 ce->last_time = vl->time;
327 ce->last_update = time (NULL);
328 ce->interval = vl->interval;
329 ce->state = STATE_OKAY;
331 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
334 ERROR ("uc_insert: c_avl_insert failed.");
338 DEBUG ("uc_insert: Added %s to the cache.", key);
340 } /* int uc_insert */
344 if (cache_tree == NULL)
345 cache_tree = c_avl_create ((int (*) (const void *, const void *))
351 int uc_check_timeout (void)
360 c_avl_iterator_t *iter;
363 pthread_mutex_lock (&cache_lock);
367 /* Build a list of entries to be flushed */
368 iter = c_avl_get_iterator (cache_tree);
369 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
371 /* If entry has not been updated, add to `keys' array */
372 if ((now - ce->last_update) >= (timeout_g * ce->interval))
376 tmp = (char **) realloc ((void *) keys,
377 (keys_len + 1) * sizeof (char *));
380 ERROR ("uc_check_timeout: realloc failed.");
381 c_avl_iterator_destroy (iter);
383 pthread_mutex_unlock (&cache_lock);
388 keys[keys_len] = strdup (key);
389 if (keys[keys_len] == NULL)
391 ERROR ("uc_check_timeout: strdup failed.");
396 } /* while (c_avl_iterator_next) */
400 for (i = 0; i < keys_len; i++)
404 status = ut_check_interesting (keys[i]);
408 ERROR ("uc_check_timeout: ut_check_interesting failed.");
412 else if (status == 0) /* ``service'' is uninteresting */
414 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
417 status = c_avl_remove (cache_tree, keys[i],
418 (void *) &key, (void *) &ce);
421 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
430 /* If we get here, the value is ``interesting''. Query the record from the
431 * cache and update the state field. */
432 if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
434 ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
435 /* Do not free `keys[i]' so a notification is sent further down. */
440 if (status == 2) /* persist */
442 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
444 ce->state = STATE_MISSING;
445 /* Do not free `keys[i]' so a notification is sent further down. */
447 else if (status == 1) /* do not persist */
449 if (ce->state == STATE_MISSING)
451 DEBUG ("uc_check_timeout: %s is missing but "
452 "notification has already been sent.",
454 /* Set `keys[i]' to NULL to no notification is sent. */
457 else /* (ce->state != STATE_MISSING) */
459 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
461 ce->state = STATE_MISSING;
462 /* Do not free `keys[i]' so a notification is sent further down. */
467 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
468 "invalid status %i.",
473 /* Make really sure the next iteration doesn't work with this pointer.
474 * There have been too many bugs in the past.. :/ -- octo */
476 } /* for (keys[i]) */
478 c_avl_iterator_destroy (iter);
480 pthread_mutex_unlock (&cache_lock);
482 for (i = 0; i < keys_len; i++)
487 uc_send_notification (keys[i]);
494 } /* int uc_check_timeout */
496 int uc_update (const data_set_t *ds, const value_list_t *vl)
498 char name[6 * DATA_MAX_NAME_LEN];
499 cache_entry_t *ce = NULL;
500 int send_okay_notification = 0;
501 time_t update_delay = 0;
506 if (FORMAT_VL (name, sizeof (name), vl) != 0)
508 ERROR ("uc_update: FORMAT_VL failed.");
512 pthread_mutex_lock (&cache_lock);
514 status = c_avl_get (cache_tree, name, (void *) &ce);
515 if (status != 0) /* entry does not yet exist */
517 status = uc_insert (ds, vl, name);
518 pthread_mutex_unlock (&cache_lock);
523 assert (ce->values_num == ds->ds_num);
525 if (ce->last_time >= vl->time)
527 pthread_mutex_unlock (&cache_lock);
528 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
529 "last cache update = %u;",
530 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
534 /* Send a notification (after the lock has been released) if we switch the
535 * state from something else to `okay'. */
536 if (ce->state == STATE_MISSING)
538 send_okay_notification = 1;
539 ce->state = STATE_OKAY;
540 update_delay = time (NULL) - ce->last_update;
543 for (i = 0; i < ds->ds_num; i++)
545 switch (ds->ds[i].type)
547 case DS_TYPE_COUNTER:
551 /* check if the counter has wrapped around */
552 if (vl->values[i].counter < ce->values_raw[i].counter)
554 if (ce->values_raw[i].counter <= 4294967295U)
555 diff = (4294967295U - ce->values_raw[i].counter)
556 + vl->values[i].counter;
558 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
559 + vl->values[i].counter;
561 else /* counter has NOT wrapped around */
563 diff = vl->values[i].counter - ce->values_raw[i].counter;
566 ce->values_gauge[i] = ((double) diff)
567 / ((double) (vl->time - ce->last_time));
568 ce->values_raw[i].counter = vl->values[i].counter;
573 ce->values_raw[i].gauge = vl->values[i].gauge;
574 ce->values_gauge[i] = vl->values[i].gauge;
581 diff = vl->values[i].derive - ce->values_raw[i].derive;
583 ce->values_gauge[i] = ((double) diff)
584 / ((double) (vl->time - ce->last_time));
585 ce->values_raw[i].derive = vl->values[i].derive;
589 case DS_TYPE_ABSOLUTE:
590 ce->values_gauge[i] = ((double) vl->values[i].absolute)
591 / ((double) (vl->time - ce->last_time));
592 ce->values_raw[i].absolute = vl->values[i].absolute;
596 /* This shouldn't happen. */
597 pthread_mutex_unlock (&cache_lock);
598 ERROR ("uc_update: Don't know how to handle data source type %i.",
601 } /* switch (ds->ds[i].type) */
603 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
606 /* Update the history if it exists. */
607 if (ce->history != NULL)
609 assert (ce->history_index < ce->history_length);
610 for (i = 0; i < ce->values_num; i++)
612 size_t hist_idx = (ce->values_num * ce->history_index) + i;
613 ce->history[hist_idx] = ce->values_gauge[i];
616 assert (ce->history_length > 0);
617 ce->history_index = (ce->history_index + 1) % ce->history_length;
620 /* Prune invalid gauge data */
621 uc_check_range (ds, ce);
623 ce->last_time = vl->time;
624 ce->last_update = time (NULL);
625 ce->interval = vl->interval;
627 pthread_mutex_unlock (&cache_lock);
629 if (send_okay_notification == 0)
632 /* Do not send okay notifications for uninteresting values, i. e. values for
633 * which no threshold is configured. */
634 status = ut_check_interesting (name);
638 /* Initialize the notification */
639 memset (&n, '\0', sizeof (n));
640 NOTIFICATION_INIT_VL (&n, vl, ds);
642 n.severity = NOTIF_OKAY;
645 ssnprintf (n.message, sizeof (n.message),
646 "Received a value for %s. It was missing for %u seconds.",
647 name, (unsigned int) update_delay);
649 plugin_dispatch_notification (&n);
652 } /* int uc_update */
654 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
658 cache_entry_t *ce = NULL;
661 pthread_mutex_lock (&cache_lock);
663 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
667 /* remove missing values from getval */
668 if (ce->state == STATE_MISSING)
674 ret_num = ce->values_num;
675 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
678 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
683 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
689 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
693 pthread_mutex_unlock (&cache_lock);
698 *ret_values_num = ret_num;
702 } /* gauge_t *uc_get_rate_by_name */
704 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
706 char name[6 * DATA_MAX_NAME_LEN];
711 if (FORMAT_VL (name, sizeof (name), vl) != 0)
713 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
717 status = uc_get_rate_by_name (name, &ret, &ret_num);
721 /* This is important - the caller has no other way of knowing how many
722 * values are returned. */
723 if (ret_num != (size_t) ds->ds_num)
725 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
726 "but uc_get_rate_by_name returned %zu.",
727 ds->type, ds->ds_num, ret_num);
733 } /* gauge_t *uc_get_rate */
735 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
737 c_avl_iterator_t *iter;
739 cache_entry_t *value;
742 time_t *times = NULL;
747 if ((ret_names == NULL) || (ret_number == NULL))
750 pthread_mutex_lock (&cache_lock);
752 iter = c_avl_get_iterator (cache_tree);
753 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
757 /* remove missing values when list values */
758 if (value->state == STATE_MISSING)
761 if (ret_times != NULL)
765 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
766 if (tmp_times == NULL)
772 times[number] = value->last_time;
775 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
782 names[number] = strdup (key);
783 if (names[number] == NULL)
789 } /* while (c_avl_iterator_next) */
791 c_avl_iterator_destroy (iter);
792 pthread_mutex_unlock (&cache_lock);
798 for (i = 0; i < number; i++)
808 if (ret_times != NULL)
810 *ret_number = number;
813 } /* int uc_get_names */
815 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
817 char name[6 * DATA_MAX_NAME_LEN];
818 cache_entry_t *ce = NULL;
819 int ret = STATE_ERROR;
821 if (FORMAT_VL (name, sizeof (name), vl) != 0)
823 ERROR ("uc_get_state: FORMAT_VL failed.");
824 return (STATE_ERROR);
827 pthread_mutex_lock (&cache_lock);
829 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
835 pthread_mutex_unlock (&cache_lock);
838 } /* int uc_get_state */
840 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
842 char name[6 * DATA_MAX_NAME_LEN];
843 cache_entry_t *ce = NULL;
846 if (FORMAT_VL (name, sizeof (name), vl) != 0)
848 ERROR ("uc_get_state: FORMAT_VL failed.");
849 return (STATE_ERROR);
852 pthread_mutex_lock (&cache_lock);
854 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
861 pthread_mutex_unlock (&cache_lock);
864 } /* int uc_set_state */
866 int uc_get_history_by_name (const char *name,
867 gauge_t *ret_history, size_t num_steps, size_t num_ds)
869 cache_entry_t *ce = NULL;
873 pthread_mutex_lock (&cache_lock);
875 status = c_avl_get (cache_tree, name, (void *) &ce);
878 pthread_mutex_unlock (&cache_lock);
882 if (((size_t) ce->values_num) != num_ds)
884 pthread_mutex_unlock (&cache_lock);
888 /* Check if there are enough values available. If not, increase the buffer
890 if (ce->history_length < num_steps)
895 tmp = realloc (ce->history, sizeof (*ce->history)
896 * num_steps * ce->values_num);
899 pthread_mutex_unlock (&cache_lock);
903 for (i = ce->history_length * ce->values_num;
904 i < (num_steps * ce->values_num);
909 ce->history_length = num_steps;
910 } /* if (ce->history_length < num_steps) */
912 /* Copy the values to the output buffer. */
913 for (i = 0; i < num_steps; i++)
918 if (i < ce->history_index)
919 src_index = ce->history_index - (i + 1);
921 src_index = ce->history_length + ce->history_index - (i + 1);
922 src_index = src_index * num_ds;
924 dst_index = i * num_ds;
926 memcpy (ret_history + dst_index, ce->history + src_index,
927 sizeof (*ret_history) * num_ds);
930 pthread_mutex_unlock (&cache_lock);
933 } /* int uc_get_history_by_name */
935 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
936 gauge_t *ret_history, size_t num_steps, size_t num_ds)
938 char name[6 * DATA_MAX_NAME_LEN];
940 if (FORMAT_VL (name, sizeof (name), vl) != 0)
942 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
946 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
947 } /* int uc_get_history */
949 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
951 char name[6 * DATA_MAX_NAME_LEN];
952 cache_entry_t *ce = NULL;
953 int ret = STATE_ERROR;
955 if (FORMAT_VL (name, sizeof (name), vl) != 0)
957 ERROR ("uc_get_state: FORMAT_VL failed.");
958 return (STATE_ERROR);
961 pthread_mutex_lock (&cache_lock);
963 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
969 pthread_mutex_unlock (&cache_lock);
972 } /* int uc_get_hits */
974 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
976 char name[6 * DATA_MAX_NAME_LEN];
977 cache_entry_t *ce = NULL;
980 if (FORMAT_VL (name, sizeof (name), vl) != 0)
982 ERROR ("uc_get_state: FORMAT_VL failed.");
983 return (STATE_ERROR);
986 pthread_mutex_lock (&cache_lock);
988 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
995 pthread_mutex_unlock (&cache_lock);
998 } /* int uc_set_hits */
1000 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
1002 char name[6 * DATA_MAX_NAME_LEN];
1003 cache_entry_t *ce = NULL;
1006 if (FORMAT_VL (name, sizeof (name), vl) != 0)
1008 ERROR ("uc_get_state: FORMAT_VL failed.");
1009 return (STATE_ERROR);
1012 pthread_mutex_lock (&cache_lock);
1014 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
1016 assert (ce != NULL);
1018 ce->hits = ret + step;
1021 pthread_mutex_unlock (&cache_lock);
1024 } /* int uc_inc_hits */
1027 * Meta data interface
1029 /* XXX: This function will acquire `cache_lock' but will not free it! */
1030 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
1032 char name[6 * DATA_MAX_NAME_LEN];
1033 cache_entry_t *ce = NULL;
1036 status = FORMAT_VL (name, sizeof (name), vl);
1039 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
1043 pthread_mutex_lock (&cache_lock);
1045 status = c_avl_get (cache_tree, name, (void *) &ce);
1048 pthread_mutex_unlock (&cache_lock);
1051 assert (ce != NULL);
1053 if (ce->meta == NULL)
1054 ce->meta = meta_data_create ();
1056 if (ce->meta == NULL)
1057 pthread_mutex_unlock (&cache_lock);
1060 } /* }}} meta_data_t *uc_get_meta */
1062 /* Sorry about this preprocessor magic, but it really makes this file much
1064 #define UC_WRAP(wrap_function) { \
1065 meta_data_t *meta; \
1067 meta = uc_get_meta (vl); \
1068 if (meta == NULL) return (-1); \
1069 status = wrap_function (meta, key); \
1070 pthread_mutex_unlock (&cache_lock); \
1073 int uc_meta_data_exists (const value_list_t *vl, const char *key)
1074 UC_WRAP (meta_data_exists)
1076 int uc_meta_data_delete (const value_list_t *vl, const char *key)
1077 UC_WRAP (meta_data_delete)
1080 /* We need a new version of this macro because the following functions take
1082 #define UC_WRAP(wrap_function) { \
1083 meta_data_t *meta; \
1085 meta = uc_get_meta (vl); \
1086 if (meta == NULL) return (-1); \
1087 status = wrap_function (meta, key, value); \
1088 pthread_mutex_unlock (&cache_lock); \
1091 int uc_meta_data_add_string (const value_list_t *vl,
1094 UC_WRAP(meta_data_add_string)
1095 int uc_meta_data_add_signed_int (const value_list_t *vl,
1098 UC_WRAP(meta_data_add_signed_int)
1099 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
1102 UC_WRAP(meta_data_add_unsigned_int)
1103 int uc_meta_data_add_double (const value_list_t *vl,
1106 UC_WRAP(meta_data_add_double)
1107 int uc_meta_data_add_boolean (const value_list_t *vl,
1110 UC_WRAP(meta_data_add_boolean)
1112 int uc_meta_data_get_string (const value_list_t *vl,
1115 UC_WRAP(meta_data_get_string)
1116 int uc_meta_data_get_signed_int (const value_list_t *vl,
1119 UC_WRAP(meta_data_get_signed_int)
1120 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
1123 UC_WRAP(meta_data_get_unsigned_int)
1124 int uc_meta_data_get_double (const value_list_t *vl,
1127 UC_WRAP(meta_data_get_double)
1128 int uc_meta_data_get_boolean (const value_list_t *vl,
1131 UC_WRAP(meta_data_get_boolean)
1134 /* vim: set sw=2 ts=8 sts=2 tw=78 : */