src/utils_cache.[ch]: Implemented `uc_get_rate_by_name'.
authorFlorian Forster <octo@huhu.verplant.org>
Wed, 27 Feb 2008 09:07:57 +0000 (10:07 +0100)
committerFlorian Forster <octo@huhu.verplant.org>
Wed, 27 Feb 2008 09:07:57 +0000 (10:07 +0100)
This functions allows to get values from the cache without creating a
`value_list_t' and `data_set_t' first. The existing function `uc_get_rate' has
been changed to use this function, too.

src/utils_cache.c
src/utils_cache.h

index 0d6961e..e7ab03c 100644 (file)
@@ -404,38 +404,76 @@ int uc_update (const data_set_t *ds, const value_list_t *vl)
   return (0);
 } /* int uc_insert */
 
-gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
+int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
 {
-  char name[6 * DATA_MAX_NAME_LEN];
   gauge_t *ret = NULL;
+  size_t ret_num = 0;
   cache_entry_t *ce = NULL;
-
-  if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
-  {
-    ERROR ("uc_insert: FORMAT_VL failed.");
-    return (NULL);
-  }
+  int status = 0;
 
   pthread_mutex_lock (&cache_lock);
 
   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
   {
     assert (ce != NULL);
-    assert (ce->values_num == ds->ds_num);
 
-    ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
+    ret_num = ce->values_num;
+    ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
     if (ret == NULL)
     {
-      ERROR ("uc_get_rate: malloc failed.");
+      ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
+      status = -1;
     }
     else
     {
-      memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
+      memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
     }
   }
+  else
+  {
+    DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
+    status = -1;
+  }
 
   pthread_mutex_unlock (&cache_lock);
 
+  if (status == 0)
+  {
+    *ret_values = ret;
+    *ret_values_num = ret_num;
+  }
+
+  return (status);
+} /* gauge_t *uc_get_rate_by_name */
+
+gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
+{
+  char name[6 * DATA_MAX_NAME_LEN];
+  gauge_t *ret = NULL;
+  size_t ret_num = 0;
+  int status;
+
+  if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
+  {
+    ERROR ("uc_insert: FORMAT_VL failed.");
+    return (NULL);
+  }
+
+  status = uc_get_rate_by_name (name, &ret, &ret_num);
+  if (status != 0)
+    return (NULL);
+
+  /* This is important - the caller has no other way of knowing how many
+   * values are returned. */
+  if (ret_num != ds->ds_num)
+  {
+    ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
+       "but uc_get_rate_by_name returned %i.",
+       ds->type, ds->ds_num, ret_num);
+    sfree (ret);
+    return (NULL);
+  }
+
   return (ret);
 } /* gauge_t *uc_get_rate */
 
index d6a56ab..ed6830b 100644 (file)
@@ -31,6 +31,7 @@
 int uc_init (void);
 int uc_check_timeout (void);
 int uc_update (const data_set_t *ds, const value_list_t *vl);
+int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num);
 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl);
 
 int uc_get_state (const data_set_t *ds, const value_list_t *vl);