Code

When fetching data from 'past' an rra, rrd_fetch was seeking past the rrd
[rrdtool-all.git] / program / src / rrd_fetch.c
index 155bae9c01745753f504ecc860be9a93627be99b..9d4765c1f0ad82a5f0e42a18218dfbe8396c2c91 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.2.14  Copyright by Tobi Oetiker, 1997-2006
+ * RRDtool 1.2.28  Copyright by Tobi Oetiker, 1997-2008
  *****************************************************************************
  * rrd_fetch.c  read date from an rrd to use for further processing
  *****************************************************************************
@@ -53,6 +53,8 @@
  *****************************************************************************/
 
 #include "rrd_tool.h"
+
+#include "rrd_is_thread_safe.h"
 /*#define DEBUG*/
 
 int
@@ -71,7 +73,7 @@ rrd_fetch(int argc,
 
     long     step_tmp =1;
     time_t   start_tmp=0, end_tmp=0;
-    enum     cf_en cf_idx;
+    const char *cf;
 
     struct rrd_time_value start_tv, end_tv;
     char     *parsetime_error = NULL;
@@ -148,19 +150,39 @@ rrd_fetch(int argc,
        rrd_set_error("not enough arguments");
        return -1;
     }
-    
-    if ((int)(cf_idx=cf_conv(argv[optind+1])) == -1 ){
-       return -1;
-    }
 
-    if (rrd_fetch_fn(argv[optind],cf_idx,start,end,step,ds_cnt,ds_namv,data) == -1)
+    cf = argv[optind+1];
+
+    if (rrd_fetch_r(argv[optind],cf,start,end,step,ds_cnt,ds_namv,data) == -1)
        return(-1);
     return (0);
 }
 
+int
+rrd_fetch_r(
+    const char           *filename,  /* name of the rrd */
+    const char           *cf,        /* which consolidation function ?*/
+    time_t         *start,
+    time_t         *end,       /* which time frame do you want ?
+                                * will be changed to represent reality */
+    unsigned long  *step,      /* which stepsize do you want? 
+                                * will be changed to represent reality */
+    unsigned long  *ds_cnt,    /* number of data sources in file */
+    char           ***ds_namv, /* names of data_sources */
+    rrd_value_t    **data)     /* two dimensional array containing the data */
+{
+    enum     cf_en cf_idx;
+
+    if ((int)(cf_idx=cf_conv(cf)) == -1 ){
+        return -1;
+    }
+
+    return (rrd_fetch_fn(filename,cf_idx,start,end,step,ds_cnt,ds_namv,data));
+}
+
 int
 rrd_fetch_fn(
-    char           *filename,  /* name of the rrd */
+    const char     *filename,  /* name of the rrd */
     enum cf_en     cf_idx,         /* which consolidation function ?*/
     time_t         *start,
     time_t         *end,       /* which time frame do you want ?
@@ -237,8 +259,7 @@ fprintf(stderr,"Considering: start %10lu end %10lu step %5lu ",
            tmp_step_diff = labs(*step - (rrd.stat_head->pdp_step
                                           * rrd.rra_def[i].pdp_cnt));
            /* best full match */
-           if(cal_end >= *end 
-              && cal_start <= *start){
+           if(cal_start <= *start){
                if (first_full || (tmp_step_diff < best_full_step_diff)){
                    first_full=0;
                    best_full_step_diff = tmp_step_diff;
@@ -257,8 +278,6 @@ fprintf(stderr,"full match, not best\n");
                tmp_match = full_match;
                if (cal_start>*start)
                    tmp_match -= (cal_start-*start);
-               if (cal_end<*end)
-                   tmp_match -= (*end-cal_end);                
                if (first_part ||
                     (best_match < tmp_match) ||
                     (best_match == tmp_match && 
@@ -342,31 +361,34 @@ fprintf(stderr,"partial match, not best\n");
 #endif
 
     /* fill the gap at the start if needs be */
-
-    if (start_offset <= 0)
-       rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1;
-    else 
-       rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1+start_offset;
+    if (*start <= rra_end_time && *end >= rra_start_time - *step){
+        
+        if (start_offset <= 0)
+            rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1;
+        else 
+            rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1+start_offset;
     
-    if(fseek(in_file,(rra_base 
+        if(fseek(in_file,(rra_base 
                   + (rra_pointer
                      * *ds_cnt
                      * sizeof(rrd_value_t))),SEEK_SET) != 0){
-       rrd_set_error("seek error in RRA");
-       for (i=0;(unsigned)i<*ds_cnt;i++)
-             free((*ds_namv)[i]);
-       free(*ds_namv);
-       rrd_free(&rrd);
-       free(*data);
-       *data = NULL;
-       fclose(in_file);
-       return(-1);
+            rrd_set_error("seek error in RRA");
+            for (i=0;(unsigned)i<*ds_cnt;i++)
+                free((*ds_namv)[i]);
+            free(*ds_namv);
+            rrd_free(&rrd);
+            free(*data);
+            *data = NULL;
+            fclose(in_file);
+            return(-1);
 
-    }
+        }
 #ifdef DEBUG
-    fprintf(stderr,"First Seek: rra_base %lu rra_pointer %lu\n",
-           rra_base, rra_pointer);
+        fprintf(stderr,"First Seek: rra_base %lu rra_pointer %lu\n",
+                rra_base, rra_pointer);
 #endif
+    }
+    
     /* step trough the array */
 
     for (i=start_offset;