src/daemon/utils_cache.c: Remove unnecessary cast.
[collectd.git] / src / daemon / utils_cache.c
1 /**
2  * collectd - src/utils_cache.c
3  * Copyright (C) 2007-2010  Florian octo Forster
4  * Copyright (C) 2016       Sebastian tokkee Harl
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Florian octo Forster <octo at collectd.org>
26  *   Sebastian tokkee Harl <sh at tokkee.org>
27  **/
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "meta_data.h"
33 #include "plugin.h"
34 #include "utils_avltree.h"
35 #include "utils_cache.h"
36
37 #include <assert.h>
38
39 typedef struct cache_entry_s {
40   char name[6 * DATA_MAX_NAME_LEN];
41   size_t values_num;
42   gauge_t *values_gauge;
43   value_t *values_raw;
44   /* Time contained in the package
45    * (for calculating rates) */
46   cdtime_t last_time;
47   /* Time according to the local clock
48    * (for purging old entries) */
49   cdtime_t last_update;
50   /* Interval in which the data is collected
51    * (for purging old entries) */
52   cdtime_t interval;
53   int state;
54   int hits;
55
56   /*
57    * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
58    * !  0  !  1  !  2  !  3  !  4  !  5  !  6  !  7  !  8  ! ...
59    * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
60    * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
61    * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
62    * !      t = 0      !      t = 1      !      t = 2      ! ...
63    * +-----------------+-----------------+-----------------+----
64    */
65   gauge_t *history;
66   size_t history_index; /* points to the next position to write to. */
67   size_t history_length;
68
69   meta_data_t *meta;
70 } cache_entry_t;
71
72 struct uc_iter_s {
73   c_avl_iterator_t *iter;
74
75   char *name;
76   cache_entry_t *entry;
77 };
78
79 static c_avl_tree_t *cache_tree = NULL;
80 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
81
82 static int cache_compare(const cache_entry_t *a, const cache_entry_t *b) {
83 #if COLLECT_DEBUG
84   assert((a != NULL) && (b != NULL));
85 #endif
86   return (strcmp(a->name, b->name));
87 } /* int cache_compare */
88
89 static cache_entry_t *cache_alloc(size_t values_num) {
90   cache_entry_t *ce;
91
92   ce = calloc(1, sizeof(*ce));
93   if (ce == NULL) {
94     ERROR("utils_cache: cache_alloc: calloc failed.");
95     return (NULL);
96   }
97   ce->values_num = values_num;
98
99   ce->values_gauge = calloc(values_num, sizeof(*ce->values_gauge));
100   ce->values_raw = calloc(values_num, sizeof(*ce->values_raw));
101   if ((ce->values_gauge == NULL) || (ce->values_raw == NULL)) {
102     sfree(ce->values_gauge);
103     sfree(ce->values_raw);
104     sfree(ce);
105     ERROR("utils_cache: cache_alloc: calloc failed.");
106     return (NULL);
107   }
108
109   ce->history = NULL;
110   ce->history_length = 0;
111   ce->meta = NULL;
112
113   return (ce);
114 } /* cache_entry_t *cache_alloc */
115
116 static void cache_free(cache_entry_t *ce) {
117   if (ce == NULL)
118     return;
119
120   sfree(ce->values_gauge);
121   sfree(ce->values_raw);
122   sfree(ce->history);
123   if (ce->meta != NULL) {
124     meta_data_destroy(ce->meta);
125     ce->meta = NULL;
126   }
127   sfree(ce);
128 } /* void cache_free */
129
130 static void uc_check_range(const data_set_t *ds, cache_entry_t *ce) {
131   for (size_t i = 0; i < ds->ds_num; i++) {
132     if (isnan(ce->values_gauge[i]))
133       continue;
134     else if (ce->values_gauge[i] < ds->ds[i].min)
135       ce->values_gauge[i] = NAN;
136     else if (ce->values_gauge[i] > ds->ds[i].max)
137       ce->values_gauge[i] = NAN;
138   }
139 } /* void uc_check_range */
140
141 static int uc_insert(const data_set_t *ds, const value_list_t *vl,
142                      const char *key) {
143   char *key_copy;
144   cache_entry_t *ce;
145
146   /* `cache_lock' has been locked by `uc_update' */
147
148   key_copy = strdup(key);
149   if (key_copy == NULL) {
150     ERROR("uc_insert: strdup failed.");
151     return (-1);
152   }
153
154   ce = cache_alloc(ds->ds_num);
155   if (ce == NULL) {
156     sfree(key_copy);
157     ERROR("uc_insert: cache_alloc (%zu) failed.", ds->ds_num);
158     return (-1);
159   }
160
161   sstrncpy(ce->name, key, sizeof(ce->name));
162
163   for (size_t i = 0; i < ds->ds_num; i++) {
164     switch (ds->ds[i].type) {
165     case DS_TYPE_COUNTER:
166       ce->values_gauge[i] = NAN;
167       ce->values_raw[i].counter = vl->values[i].counter;
168       break;
169
170     case DS_TYPE_GAUGE:
171       ce->values_gauge[i] = vl->values[i].gauge;
172       ce->values_raw[i].gauge = vl->values[i].gauge;
173       break;
174
175     case DS_TYPE_DERIVE:
176       ce->values_gauge[i] = NAN;
177       ce->values_raw[i].derive = vl->values[i].derive;
178       break;
179
180     case DS_TYPE_ABSOLUTE:
181       ce->values_gauge[i] = NAN;
182       if (vl->interval > 0)
183         ce->values_gauge[i] =
184             ((double)vl->values[i].absolute) / CDTIME_T_TO_DOUBLE(vl->interval);
185       ce->values_raw[i].absolute = vl->values[i].absolute;
186       break;
187
188     default:
189       /* This shouldn't happen. */
190       ERROR("uc_insert: Don't know how to handle data source type %i.",
191             ds->ds[i].type);
192       sfree(key_copy);
193       cache_free(ce);
194       return (-1);
195     } /* switch (ds->ds[i].type) */
196   }   /* for (i) */
197
198   /* Prune invalid gauge data */
199   uc_check_range(ds, ce);
200
201   ce->last_time = vl->time;
202   ce->last_update = cdtime();
203   ce->interval = vl->interval;
204   ce->state = STATE_OKAY;
205
206   if (c_avl_insert(cache_tree, key_copy, ce) != 0) {
207     sfree(key_copy);
208     ERROR("uc_insert: c_avl_insert failed.");
209     return (-1);
210   }
211
212   DEBUG("uc_insert: Added %s to the cache.", key);
213   return (0);
214 } /* int uc_insert */
215
216 int uc_init(void) {
217   if (cache_tree == NULL)
218     cache_tree =
219         c_avl_create((int (*)(const void *, const void *))cache_compare);
220
221   return (0);
222 } /* int uc_init */
223
224 int uc_check_timeout(void) {
225   struct {
226     char *key;
227     cdtime_t time;
228     cdtime_t interval;
229   } *expired = NULL;
230   size_t expired_num = 0;
231
232   pthread_mutex_lock(&cache_lock);
233   cdtime_t now = cdtime();
234
235   /* Build a list of entries to be flushed */
236   c_avl_iterator_t *iter = c_avl_get_iterator(cache_tree);
237   char *key = NULL;
238   cache_entry_t *ce = NULL;
239   while (c_avl_iterator_next(iter, (void *)&key, (void *)&ce) == 0) {
240     /* If the entry is fresh enough, continue. */
241     if ((now - ce->last_update) < (ce->interval * timeout_g))
242       continue;
243
244     void *tmp = realloc(expired, (expired_num + 1) * sizeof(*expired));
245     if (tmp == NULL) {
246       ERROR("uc_check_timeout: realloc failed.");
247       continue;
248     }
249     expired = tmp;
250
251     expired[expired_num].key = strdup(key);
252     expired[expired_num].time = ce->last_time;
253     expired[expired_num].interval = ce->interval;
254
255     if (expired[expired_num].key == NULL) {
256       ERROR("uc_check_timeout: strdup failed.");
257       continue;
258     }
259
260     expired_num++;
261   } /* while (c_avl_iterator_next) */
262
263   c_avl_iterator_destroy(iter);
264   pthread_mutex_unlock(&cache_lock);
265
266   if (expired_num == 0) {
267     sfree(expired);
268     return (0);
269   }
270
271   /* Call the "missing" callback for each value. Do this before removing the
272    * value from the cache, so that callbacks can still access the data stored,
273    * including plugin specific meta data, rates, history, …. This must be done
274    * without holding the lock, otherwise we will run into a deadlock if a
275    * plugin calls the cache interface. */
276   for (size_t i = 0; i < expired_num; i++) {
277     value_list_t vl = {
278         .time = expired[i].time, .interval = expired[i].interval,
279     };
280
281     if (parse_identifier_vl(expired[i].key, &vl) != 0) {
282       ERROR("uc_check_timeout: parse_identifier_vl (\"%s\") failed.",
283             expired[i].key);
284       continue;
285     }
286
287     plugin_dispatch_missing(&vl);
288   } /* for (i = 0; i < expired_num; i++) */
289
290   /* Now actually remove all the values from the cache. We don't re-evaluate
291    * the timestamp again, so in theory it is possible we remove a value after
292    * it is updated here. */
293   pthread_mutex_lock(&cache_lock);
294   for (size_t i = 0; i < expired_num; i++) {
295     char *key = NULL;
296     cache_entry_t *value = NULL;
297
298     if (c_avl_remove(cache_tree, expired[i].key, (void *)&key,
299                      (void *)&value) != 0) {
300       ERROR("uc_check_timeout: c_avl_remove (\"%s\") failed.", expired[i].key);
301       sfree(expired[i].key);
302       continue;
303     }
304     sfree(key);
305     cache_free(value);
306
307     sfree(expired[i].key);
308   } /* for (i = 0; i < expired_num; i++) */
309   pthread_mutex_unlock(&cache_lock);
310
311   sfree(expired);
312   return (0);
313 } /* int uc_check_timeout */
314
315 int uc_update(const data_set_t *ds, const value_list_t *vl) {
316   char name[6 * DATA_MAX_NAME_LEN];
317   cache_entry_t *ce = NULL;
318   int status;
319
320   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
321     ERROR("uc_update: FORMAT_VL failed.");
322     return (-1);
323   }
324
325   pthread_mutex_lock(&cache_lock);
326
327   status = c_avl_get(cache_tree, name, (void *)&ce);
328   if (status != 0) /* entry does not yet exist */
329   {
330     status = uc_insert(ds, vl, name);
331     pthread_mutex_unlock(&cache_lock);
332     return (status);
333   }
334
335   assert(ce != NULL);
336   assert(ce->values_num == ds->ds_num);
337
338   if (ce->last_time >= vl->time) {
339     pthread_mutex_unlock(&cache_lock);
340     NOTICE("uc_update: Value too old: name = %s; value time = %.3f; "
341            "last cache update = %.3f;",
342            name, CDTIME_T_TO_DOUBLE(vl->time),
343            CDTIME_T_TO_DOUBLE(ce->last_time));
344     return (-1);
345   }
346
347   for (size_t i = 0; i < ds->ds_num; i++) {
348     switch (ds->ds[i].type) {
349     case DS_TYPE_COUNTER: {
350       counter_t diff =
351           counter_diff(ce->values_raw[i].counter, vl->values[i].counter);
352       ce->values_gauge[i] =
353           ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
354       ce->values_raw[i].counter = vl->values[i].counter;
355     } break;
356
357     case DS_TYPE_GAUGE:
358       ce->values_raw[i].gauge = vl->values[i].gauge;
359       ce->values_gauge[i] = vl->values[i].gauge;
360       break;
361
362     case DS_TYPE_DERIVE: {
363       derive_t diff = vl->values[i].derive - ce->values_raw[i].derive;
364
365       ce->values_gauge[i] =
366           ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
367       ce->values_raw[i].derive = vl->values[i].derive;
368     } break;
369
370     case DS_TYPE_ABSOLUTE:
371       ce->values_gauge[i] = ((double)vl->values[i].absolute) /
372                             (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
373       ce->values_raw[i].absolute = vl->values[i].absolute;
374       break;
375
376     default:
377       /* This shouldn't happen. */
378       pthread_mutex_unlock(&cache_lock);
379       ERROR("uc_update: Don't know how to handle data source type %i.",
380             ds->ds[i].type);
381       return (-1);
382     } /* switch (ds->ds[i].type) */
383
384     DEBUG("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
385   } /* for (i) */
386
387   /* Update the history if it exists. */
388   if (ce->history != NULL) {
389     assert(ce->history_index < ce->history_length);
390     for (size_t i = 0; i < ce->values_num; i++) {
391       size_t hist_idx = (ce->values_num * ce->history_index) + i;
392       ce->history[hist_idx] = ce->values_gauge[i];
393     }
394
395     assert(ce->history_length > 0);
396     ce->history_index = (ce->history_index + 1) % ce->history_length;
397   }
398
399   /* Prune invalid gauge data */
400   uc_check_range(ds, ce);
401
402   ce->last_time = vl->time;
403   ce->last_update = cdtime();
404   ce->interval = vl->interval;
405
406   pthread_mutex_unlock(&cache_lock);
407
408   return (0);
409 } /* int uc_update */
410
411 int uc_get_rate_by_name(const char *name, gauge_t **ret_values,
412                         size_t *ret_values_num) {
413   gauge_t *ret = NULL;
414   size_t ret_num = 0;
415   cache_entry_t *ce = NULL;
416   int status = 0;
417
418   pthread_mutex_lock(&cache_lock);
419
420   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
421     assert(ce != NULL);
422
423     /* remove missing values from getval */
424     if (ce->state == STATE_MISSING) {
425       status = -1;
426     } else {
427       ret_num = ce->values_num;
428       ret = malloc(ret_num * sizeof(*ret));
429       if (ret == NULL) {
430         ERROR("utils_cache: uc_get_rate_by_name: malloc failed.");
431         status = -1;
432       } else {
433         memcpy(ret, ce->values_gauge, ret_num * sizeof(gauge_t));
434       }
435     }
436   } else {
437     DEBUG("utils_cache: uc_get_rate_by_name: No such value: %s", name);
438     status = -1;
439   }
440
441   pthread_mutex_unlock(&cache_lock);
442
443   if (status == 0) {
444     *ret_values = ret;
445     *ret_values_num = ret_num;
446   }
447
448   return (status);
449 } /* gauge_t *uc_get_rate_by_name */
450
451 gauge_t *uc_get_rate(const data_set_t *ds, const value_list_t *vl) {
452   char name[6 * DATA_MAX_NAME_LEN];
453   gauge_t *ret = NULL;
454   size_t ret_num = 0;
455   int status;
456
457   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
458     ERROR("utils_cache: uc_get_rate: FORMAT_VL failed.");
459     return (NULL);
460   }
461
462   status = uc_get_rate_by_name(name, &ret, &ret_num);
463   if (status != 0)
464     return (NULL);
465
466   /* This is important - the caller has no other way of knowing how many
467    * values are returned. */
468   if (ret_num != ds->ds_num) {
469     ERROR("utils_cache: uc_get_rate: ds[%s] has %zu values, "
470           "but uc_get_rate_by_name returned %zu.",
471           ds->type, ds->ds_num, ret_num);
472     sfree(ret);
473     return (NULL);
474   }
475
476   return (ret);
477 } /* gauge_t *uc_get_rate */
478
479 size_t uc_get_size(void) {
480   size_t size_arrays = 0;
481
482   pthread_mutex_lock(&cache_lock);
483   size_arrays = (size_t)c_avl_size(cache_tree);
484   pthread_mutex_unlock(&cache_lock);
485
486   return (size_arrays);
487 }
488
489 int uc_get_names(char ***ret_names, cdtime_t **ret_times, size_t *ret_number) {
490   c_avl_iterator_t *iter;
491   char *key;
492   cache_entry_t *value;
493
494   char **names = NULL;
495   cdtime_t *times = NULL;
496   size_t number = 0;
497   size_t size_arrays = 0;
498
499   int status = 0;
500
501   if ((ret_names == NULL) || (ret_number == NULL))
502     return (-1);
503
504   pthread_mutex_lock(&cache_lock);
505
506   size_arrays = (size_t)c_avl_size(cache_tree);
507   if (size_arrays < 1) {
508     /* Handle the "no values" case here, to avoid the error message when
509      * calloc() returns NULL. */
510     pthread_mutex_unlock(&cache_lock);
511     return (0);
512   }
513
514   names = calloc(size_arrays, sizeof(*names));
515   times = calloc(size_arrays, sizeof(*times));
516   if ((names == NULL) || (times == NULL)) {
517     ERROR("uc_get_names: calloc failed.");
518     sfree(names);
519     sfree(times);
520     pthread_mutex_unlock(&cache_lock);
521     return (ENOMEM);
522   }
523
524   iter = c_avl_get_iterator(cache_tree);
525   while (c_avl_iterator_next(iter, (void *)&key, (void *)&value) == 0) {
526     /* remove missing values when list values */
527     if (value->state == STATE_MISSING)
528       continue;
529
530     /* c_avl_size does not return a number smaller than the number of elements
531      * returned by c_avl_iterator_next. */
532     assert(number < size_arrays);
533
534     if (ret_times != NULL)
535       times[number] = value->last_time;
536
537     names[number] = strdup(key);
538     if (names[number] == NULL) {
539       status = -1;
540       break;
541     }
542
543     number++;
544   } /* while (c_avl_iterator_next) */
545
546   c_avl_iterator_destroy(iter);
547   pthread_mutex_unlock(&cache_lock);
548
549   if (status != 0) {
550     for (size_t i = 0; i < number; i++) {
551       sfree(names[i]);
552     }
553     sfree(names);
554     sfree(times);
555
556     return (-1);
557   }
558
559   *ret_names = names;
560   if (ret_times != NULL)
561     *ret_times = times;
562   else
563     sfree(times);
564   *ret_number = number;
565
566   return (0);
567 } /* int uc_get_names */
568
569 int uc_get_state(const data_set_t *ds, const value_list_t *vl) {
570   char name[6 * DATA_MAX_NAME_LEN];
571   cache_entry_t *ce = NULL;
572   int ret = STATE_ERROR;
573
574   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
575     ERROR("uc_get_state: FORMAT_VL failed.");
576     return (STATE_ERROR);
577   }
578
579   pthread_mutex_lock(&cache_lock);
580
581   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
582     assert(ce != NULL);
583     ret = ce->state;
584   }
585
586   pthread_mutex_unlock(&cache_lock);
587
588   return (ret);
589 } /* int uc_get_state */
590
591 int uc_set_state(const data_set_t *ds, const value_list_t *vl, int state) {
592   char name[6 * DATA_MAX_NAME_LEN];
593   cache_entry_t *ce = NULL;
594   int ret = -1;
595
596   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
597     ERROR("uc_set_state: FORMAT_VL failed.");
598     return (STATE_ERROR);
599   }
600
601   pthread_mutex_lock(&cache_lock);
602
603   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
604     assert(ce != NULL);
605     ret = ce->state;
606     ce->state = state;
607   }
608
609   pthread_mutex_unlock(&cache_lock);
610
611   return (ret);
612 } /* int uc_set_state */
613
614 int uc_get_history_by_name(const char *name, gauge_t *ret_history,
615                            size_t num_steps, size_t num_ds) {
616   cache_entry_t *ce = NULL;
617   int status = 0;
618
619   pthread_mutex_lock(&cache_lock);
620
621   status = c_avl_get(cache_tree, name, (void *)&ce);
622   if (status != 0) {
623     pthread_mutex_unlock(&cache_lock);
624     return (-ENOENT);
625   }
626
627   if (((size_t)ce->values_num) != num_ds) {
628     pthread_mutex_unlock(&cache_lock);
629     return (-EINVAL);
630   }
631
632   /* Check if there are enough values available. If not, increase the buffer
633    * size. */
634   if (ce->history_length < num_steps) {
635     gauge_t *tmp;
636
637     tmp =
638         realloc(ce->history, sizeof(*ce->history) * num_steps * ce->values_num);
639     if (tmp == NULL) {
640       pthread_mutex_unlock(&cache_lock);
641       return (-ENOMEM);
642     }
643
644     for (size_t i = ce->history_length * ce->values_num;
645          i < (num_steps * ce->values_num); i++)
646       tmp[i] = NAN;
647
648     ce->history = tmp;
649     ce->history_length = num_steps;
650   } /* if (ce->history_length < num_steps) */
651
652   /* Copy the values to the output buffer. */
653   for (size_t i = 0; i < num_steps; i++) {
654     size_t src_index;
655     size_t dst_index;
656
657     if (i < ce->history_index)
658       src_index = ce->history_index - (i + 1);
659     else
660       src_index = ce->history_length + ce->history_index - (i + 1);
661     src_index = src_index * num_ds;
662
663     dst_index = i * num_ds;
664
665     memcpy(ret_history + dst_index, ce->history + src_index,
666            sizeof(*ret_history) * num_ds);
667   }
668
669   pthread_mutex_unlock(&cache_lock);
670
671   return (0);
672 } /* int uc_get_history_by_name */
673
674 int uc_get_history(const data_set_t *ds, const value_list_t *vl,
675                    gauge_t *ret_history, size_t num_steps, size_t num_ds) {
676   char name[6 * DATA_MAX_NAME_LEN];
677
678   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
679     ERROR("utils_cache: uc_get_history: FORMAT_VL failed.");
680     return (-1);
681   }
682
683   return (uc_get_history_by_name(name, ret_history, num_steps, num_ds));
684 } /* int uc_get_history */
685
686 int uc_get_hits(const data_set_t *ds, const value_list_t *vl) {
687   char name[6 * DATA_MAX_NAME_LEN];
688   cache_entry_t *ce = NULL;
689   int ret = STATE_ERROR;
690
691   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
692     ERROR("uc_get_hits: FORMAT_VL failed.");
693     return (STATE_ERROR);
694   }
695
696   pthread_mutex_lock(&cache_lock);
697
698   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
699     assert(ce != NULL);
700     ret = ce->hits;
701   }
702
703   pthread_mutex_unlock(&cache_lock);
704
705   return (ret);
706 } /* int uc_get_hits */
707
708 int uc_set_hits(const data_set_t *ds, const value_list_t *vl, int hits) {
709   char name[6 * DATA_MAX_NAME_LEN];
710   cache_entry_t *ce = NULL;
711   int ret = -1;
712
713   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
714     ERROR("uc_set_hits: FORMAT_VL failed.");
715     return (STATE_ERROR);
716   }
717
718   pthread_mutex_lock(&cache_lock);
719
720   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
721     assert(ce != NULL);
722     ret = ce->hits;
723     ce->hits = hits;
724   }
725
726   pthread_mutex_unlock(&cache_lock);
727
728   return (ret);
729 } /* int uc_set_hits */
730
731 int uc_inc_hits(const data_set_t *ds, const value_list_t *vl, int step) {
732   char name[6 * DATA_MAX_NAME_LEN];
733   cache_entry_t *ce = NULL;
734   int ret = -1;
735
736   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
737     ERROR("uc_inc_hits: FORMAT_VL failed.");
738     return (STATE_ERROR);
739   }
740
741   pthread_mutex_lock(&cache_lock);
742
743   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
744     assert(ce != NULL);
745     ret = ce->hits;
746     ce->hits = ret + step;
747   }
748
749   pthread_mutex_unlock(&cache_lock);
750
751   return (ret);
752 } /* int uc_inc_hits */
753
754 /*
755  * Iterator interface
756  */
757 uc_iter_t *uc_get_iterator(void) {
758   uc_iter_t *iter;
759
760   iter = (uc_iter_t *)calloc(1, sizeof(*iter));
761   if (iter == NULL)
762     return (NULL);
763
764   pthread_mutex_lock(&cache_lock);
765
766   iter->iter = c_avl_get_iterator(cache_tree);
767   if (iter->iter == NULL) {
768     free(iter);
769     return (NULL);
770   }
771
772   return (iter);
773 } /* uc_iter_t *uc_get_iterator */
774
775 int uc_iterator_next(uc_iter_t *iter, char **ret_name) {
776   int status;
777
778   if (iter == NULL)
779     return (-1);
780
781   while ((status = c_avl_iterator_next(iter->iter, (void *)&iter->name,
782                                        (void *)&iter->entry)) == 0) {
783     if (iter->entry->state == STATE_MISSING)
784       continue;
785
786     break;
787   }
788   if (status != 0) {
789     iter->name = NULL;
790     iter->entry = NULL;
791     return (-1);
792   }
793
794   if (ret_name != NULL)
795     *ret_name = iter->name;
796
797   return (0);
798 } /* int uc_iterator_next */
799
800 void uc_iterator_destroy(uc_iter_t *iter) {
801   if (iter == NULL)
802     return;
803
804   c_avl_iterator_destroy(iter->iter);
805   pthread_mutex_unlock(&cache_lock);
806
807   free(iter);
808 } /* void uc_iterator_destroy */
809
810 int uc_iterator_get_time(uc_iter_t *iter, cdtime_t *ret_time) {
811   if ((iter == NULL) || (iter->entry == NULL) || (ret_time == NULL))
812     return (-1);
813
814   *ret_time = iter->entry->last_time;
815   return (0);
816 } /* int uc_iterator_get_name */
817
818 int uc_iterator_get_values(uc_iter_t *iter, value_t **ret_values,
819                            size_t *ret_num) {
820   if ((iter == NULL) || (iter->entry == NULL) || (ret_values == NULL) ||
821       (ret_num == NULL))
822     return (-1);
823
824   *ret_values =
825       calloc(iter->entry->values_num, sizeof(*iter->entry->values_raw));
826   if (*ret_values == NULL)
827     return (-1);
828   for (size_t i = 0; i < iter->entry->values_num; ++i)
829     *ret_values[i] = iter->entry->values_raw[i];
830
831   *ret_num = iter->entry->values_num;
832
833   return (0);
834 } /* int uc_iterator_get_values */
835
836 int uc_iterator_get_interval(uc_iter_t *iter, cdtime_t *ret_interval) {
837   if ((iter == NULL) || (iter->entry == NULL) || (ret_interval == NULL))
838     return (-1);
839
840   *ret_interval = iter->entry->interval;
841   return (0);
842 } /* int uc_iterator_get_name */
843
844 /*
845  * Meta data interface
846  */
847 /* XXX: This function will acquire `cache_lock' but will not free it! */
848 static meta_data_t *uc_get_meta(const value_list_t *vl) /* {{{ */
849 {
850   char name[6 * DATA_MAX_NAME_LEN];
851   cache_entry_t *ce = NULL;
852   int status;
853
854   status = FORMAT_VL(name, sizeof(name), vl);
855   if (status != 0) {
856     ERROR("utils_cache: uc_get_meta: FORMAT_VL failed.");
857     return (NULL);
858   }
859
860   pthread_mutex_lock(&cache_lock);
861
862   status = c_avl_get(cache_tree, name, (void *)&ce);
863   if (status != 0) {
864     pthread_mutex_unlock(&cache_lock);
865     return (NULL);
866   }
867   assert(ce != NULL);
868
869   if (ce->meta == NULL)
870     ce->meta = meta_data_create();
871
872   if (ce->meta == NULL)
873     pthread_mutex_unlock(&cache_lock);
874
875   return (ce->meta);
876 } /* }}} meta_data_t *uc_get_meta */
877
878 /* Sorry about this preprocessor magic, but it really makes this file much
879  * shorter.. */
880 #define UC_WRAP(wrap_function)                                                 \
881   {                                                                            \
882     meta_data_t *meta;                                                         \
883     int status;                                                                \
884     meta = uc_get_meta(vl);                                                    \
885     if (meta == NULL)                                                          \
886       return (-1);                                                             \
887     status = wrap_function(meta, key);                                         \
888     pthread_mutex_unlock(&cache_lock);                                         \
889     return (status);                                                           \
890   }
891 int uc_meta_data_exists(const value_list_t *vl,
892                         const char *key) UC_WRAP(meta_data_exists)
893
894     int uc_meta_data_delete(const value_list_t *vl,
895                             const char *key) UC_WRAP(meta_data_delete)
896 #undef UC_WRAP
897
898 /* We need a new version of this macro because the following functions take
899  * two argumetns. */
900 #define UC_WRAP(wrap_function)                                                 \
901   {                                                                            \
902     meta_data_t *meta;                                                         \
903     int status;                                                                \
904     meta = uc_get_meta(vl);                                                    \
905     if (meta == NULL)                                                          \
906       return (-1);                                                             \
907     status = wrap_function(meta, key, value);                                  \
908     pthread_mutex_unlock(&cache_lock);                                         \
909     return (status);                                                           \
910   }
911         int uc_meta_data_add_string(const value_list_t *vl, const char *key,
912                                     const char *value)
913             UC_WRAP(meta_data_add_string) int uc_meta_data_add_signed_int(
914                 const value_list_t *vl, const char *key, int64_t value)
915                 UC_WRAP(meta_data_add_signed_int) int uc_meta_data_add_unsigned_int(
916                     const value_list_t *vl, const char *key, uint64_t value)
917                     UC_WRAP(meta_data_add_unsigned_int) int uc_meta_data_add_double(
918                         const value_list_t *vl, const char *key, double value)
919                         UC_WRAP(meta_data_add_double) int uc_meta_data_add_boolean(
920                             const value_list_t *vl, const char *key,
921                             _Bool value) UC_WRAP(meta_data_add_boolean)
922
923                             int uc_meta_data_get_string(const value_list_t *vl,
924                                                         const char *key,
925                                                         char **value)
926                                 UC_WRAP(meta_data_get_string) int uc_meta_data_get_signed_int(
927                                     const value_list_t *vl, const char *key,
928                                     int64_t *value)
929                                     UC_WRAP(meta_data_get_signed_int) int uc_meta_data_get_unsigned_int(
930                                         const value_list_t *vl, const char *key,
931                                         uint64_t *value)
932                                         UC_WRAP(meta_data_get_unsigned_int) int uc_meta_data_get_double(
933                                             const value_list_t *vl,
934                                             const char *key, double *value)
935                                             UC_WRAP(meta_data_get_double) int uc_meta_data_get_boolean(
936                                                 const value_list_t *vl,
937                                                 const char *key, _Bool *value)
938                                                 UC_WRAP(meta_data_get_boolean)
939 #undef UC_WRAP
940
941     /* vim: set sw=2 ts=8 sts=2 tw=78 : */