reat nan as FALSE in an IF CDEF
[rrdtool.git] / src / rrd_restore.c
index 368f7bc..b2d1193 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.2.23  Copyright by Tobi Oetiker, 1997-2007
+ * RRDtool 1.2.99907080300  Copyright by Tobi Oetiker, 1997-2007
  *****************************************************************************
  * rrd_restore.c  creates new rrd from data dumped by rrd_dump.c
  *****************************************************************************/
@@ -52,6 +52,8 @@ void      parse_FAILURES_history(
     rrd_t *rrd,
     int rra_index,
     int ds_index);
+long int  rra_random_row(
+    rra_def_t *);
 
 /* convert all occurrences of <BlaBlaBla> to <blablabla> */
 
@@ -223,14 +225,12 @@ int xml2rrd(
         rrd->stat_head = NULL;
         return -1;
     }
-    /* make sure we output the right version */
-    strcpy(rrd->stat_head->version, RRD_VERSION);
-
-    /*  if (atoi(rrd -> stat_head -> version) < 2) 
-       {
-       rrd_set_error("Can only restore version >= 2 (Not %s). Dump your old rrd using a current rrdtool dump.",  rrd -> stat_head -> version );
-       return -1;
-       } */
+    /* make sure we output the right version only go over 3 if input is over 3 too */
+    if (input_version > 3) {
+        strcpy(rrd->stat_head->version, RRD_VERSION);
+    } else {
+        strcpy(rrd->stat_head->version, RRD_VERSION3);
+    }
 
     rrd->stat_head->float_cookie = FLOAT_COOKIE;
     rrd->stat_head->ds_cnt = 0;
@@ -366,6 +366,7 @@ int xml2rrd(
             } else {
                 switch (cf_conv(rrd->rra_def[rra_index].cf_nam)) {
                 case CF_HWPREDICT:
+                case CF_MHWPREDICT:
                     read_tag(&ptr2, "hw_alpha", "%lf",
                              &(rrd->rra_def[rra_index].par[RRA_hw_alpha].
                                u_val));
@@ -384,6 +385,11 @@ int xml2rrd(
                     read_tag(&ptr2, "seasonal_smooth_idx", "%lu",
                              &(rrd->rra_def[rra_index].
                                par[RRA_seasonal_smooth_idx].u_cnt));
+                    if (atoi(rrd->stat_head->version) >= 4) {
+                        read_tag(&ptr2, "smoothing_window", "%lf",
+                                 &(rrd->rra_def[rra_index].
+                                   par[RRA_seasonal_smoothing_window].u_val));
+                    }
                     read_tag(&ptr2, "dependent_rra_idx", "%lu",
                              &(rrd->rra_def[rra_index].
                                par[RRA_dependent_rra_idx].u_cnt));
@@ -423,7 +429,12 @@ int xml2rrd(
 
         eat_tag(&ptr2, "cdp_prep");
         for (i = 0; i < (int) rrd->stat_head->ds_cnt; i++) {
-            eat_tag(&ptr2, "ds");
+            if (eat_tag(&ptr2, "ds") != 1) {
+                rrd_set_error
+                    ("expected to find %lu <ds> entries in <cdp_prep>",
+                     rrd->stat_head->ds_cnt);
+                return -1;
+            }
             /* support to read CDP parameters */
             rra_index = rrd->stat_head->rra_cnt - 1;
             skip(&ptr2);
@@ -456,6 +467,7 @@ int xml2rrd(
                                         i].scratch[CDP_secondary_val].u_val));
                     switch (cf_conv(rrd->rra_def[rra_index].cf_nam)) {
                     case CF_HWPREDICT:
+                    case CF_MHWPREDICT:
                         read_tag(&ptr2, "intercept", "%lf",
                                  &(rrd->
                                    cdp_prep[rrd->stat_head->ds_cnt *
@@ -612,12 +624,6 @@ int xml2rrd(
         return (-1);
     }
 
-    for (i = 0; i < (int) rrd->stat_head->rra_cnt; i++) {
-        /* last row in the xml file is the most recent; as
-         * rrd_update increments the current row pointer, set cur_row
-         * here to the last row. */
-        rrd->rra_ptr[i].cur_row = rrd->rra_def[i].row_cnt - 1;
-    }
     if (ptr == NULL)
         return -1;
     return 1;
@@ -634,7 +640,7 @@ int rrd_creat(
     rrd_t *rrd,
     char force_overwrite)
 {
-    unsigned long i, ii, val_cnt;
+    unsigned long i, ii, rra_offset;
     FILE     *rrd_file = NULL;
     int       fdflags;
     int       fd;
@@ -673,18 +679,31 @@ int rrd_creat(
 
     fwrite(rrd->cdp_prep, sizeof(cdp_prep_t), rrd->stat_head->rra_cnt *
            rrd->stat_head->ds_cnt, rrd_file);
+
+    for (i = 0; i < rrd->stat_head->rra_cnt; i++)
+        rrd->rra_ptr[i].cur_row = rra_random_row(&rrd->rra_def[i]);
+
     fwrite(rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt,
            rrd_file);
 
 
+    /* Dump RRD values */
+    rra_offset = 0;
+    for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
+        unsigned long num_rows = rrd->rra_def[i].row_cnt;
+        unsigned long cur_row = rrd->rra_ptr[i].cur_row;
+        unsigned long ds_cnt = rrd->stat_head->ds_cnt;
 
-    /* calculate the number of rrd_values to dump */
-    val_cnt = 0;
-    for (i = 0; i < rrd->stat_head->rra_cnt; i++)
-        for (ii = 0; ii < rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt;
-             ii++)
-            val_cnt++;
-    fwrite(rrd->rrd_value, sizeof(rrd_value_t), val_cnt, rrd_file);
+        fwrite(rrd->rrd_value +
+               (rra_offset + num_rows - 1 - cur_row) * ds_cnt,
+               sizeof(rrd_value_t), (cur_row + 1) * ds_cnt, rrd_file);
+
+        fwrite(rrd->rrd_value + rra_offset * ds_cnt,
+               sizeof(rrd_value_t), (num_rows - 1 - cur_row) * ds_cnt,
+               rrd_file);
+
+        rra_offset += num_rows;
+    }
 
     /* lets see if we had an error */
     if (ferror(rrd_file)) {
@@ -706,20 +725,19 @@ int rrd_restore(
     char     *buf;
     char      rc = 0;
     char      force_overwrite = 0;
+    struct option long_options[] = {
+        {"range-check", no_argument, 0, 'r'},
+        {"force-overwrite", no_argument, 0, 'f'},
+        {0, 0, 0, 0}
+    };
 
     /* init rrd clean */
     optind = 0;
     opterr = 0;         /* initialize getopt */
     while (1) {
-        static struct option long_options[] = {
-            {"range-check", no_argument, 0, 'r'},
-            {"force-overwrite", no_argument, 0, 'f'},
-            {0, 0, 0, 0}
-        };
         int       option_index = 0;
         int       opt;
 
-
         opt = getopt_long(argc, argv, "rf", long_options, &option_index);
 
         if (opt == EOF)