]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
Move custom logic row-rotation logic from write_fh, originally there to
authorPeter Stamfest <peter@stamfest.at>
Wed, 20 Aug 2014 22:43:18 +0000 (00:43 +0200)
committerPeter Stamfest <peter@stamfest.at>
Sun, 31 Aug 2014 20:15:32 +0000 (22:15 +0200)
support rrd_restore with its random cur_row placement to
rrd_restore itself.  This makes write_fh a truly reuseable function.

src/rrd_create.c
src/rrd_restore.c

index 2768c28f90080d03c0b6f612064b297995f89a04..144aaa4a7c51b6381e52bcd958a886d6e7448540 100644 (file)
@@ -1157,13 +1157,10 @@ int write_fh(
         unsigned long cur_row = rrd->rra_ptr[i].cur_row;
         unsigned long ds_cnt = rrd->stat_head->ds_cnt;
         if (num_rows > 0){
-            fwrite(rrd->rrd_value +
-                (rra_offset + num_rows - 1 - cur_row) * ds_cnt,
-                sizeof(rrd_value_t), (cur_row + 1) * ds_cnt, fh);
-
             fwrite(rrd->rrd_value + rra_offset * ds_cnt,
-                sizeof(rrd_value_t), (num_rows - 1 - cur_row) * ds_cnt, fh);
-
+                    sizeof(rrd_value_t), 
+                    num_rows * ds_cnt, fh);
+                    
             rra_offset += num_rows;
         }
     }
index 06eb4a80e30c1256fdac6e09bbd1ffdf2f75afe9..df1f0725b4b741ec240c527929a927f9d8043f63 100644 (file)
@@ -400,17 +400,24 @@ static int parse_tag_rra_database(
     rrd_t *rrd )
 {
     rra_def_t *cur_rra_def;
+    rra_ptr_t *cur_rra_ptr;
     unsigned int total_row_cnt;
     int       status;
     int       i;
     xmlChar *element;
-
+    unsigned int start_row_cnt;
+    int       ds_cnt;
+    
+    ds_cnt = rrd->stat_head->ds_cnt;
+    
     total_row_cnt = 0;
     for (i = 0; i < (((int) rrd->stat_head->rra_cnt) - 1); i++)
         total_row_cnt += rrd->rra_def[i].row_cnt;
 
     cur_rra_def = rrd->rra_def + i;
-
+    cur_rra_ptr = rrd->rra_ptr + i;
+    start_row_cnt = total_row_cnt;
+    
     status = 0;
     while ((element = get_xml_element(reader)) != NULL){        
         if (xmlStrcasecmp(element,(const xmlChar *)"row") == 0){
@@ -456,6 +463,63 @@ static int parse_tag_rra_database(
         if (status != 0)
             break;        
     }
+    
+    /* Set the RRA pointer to a random location */
+    cur_rra_ptr->cur_row = rrd_random() % cur_rra_def->row_cnt;
+    
+    /*
+     * rotate rows to match cur_row...
+     * 
+     * this will require some extra temp. memory. We can do this rather 
+     * brainlessly, because we have done all kinds of realloc before, 
+     * so we messed around with memory a lot already.
+     */
+    
+    /*
+        
+     What we want:
+     
+     +-start_row_cnt
+     |           +-cur_rra_def->row_cnt
+     |           |
+     |a---------n012-------------------------|
+    
+   (cur_rra_def->row_cnt slots of ds_cnt width)
+   
+     What we have 
+      
+     |   
+     |012-------------------------a---------n|
+     
+     Do this by:
+     copy away 0..(a-1) to a temp buffer
+     move a..n to start of buffer
+     copy temp buffer to position after where we moved n to
+     */
+    
+    int a = cur_rra_def->row_cnt - cur_rra_ptr->cur_row - 1;
+    
+    rrd_value_t *temp = malloc(ds_cnt * sizeof(rrd_value_t) * a);
+    if (temp == NULL) {
+        rrd_set_error("parse_tag_rra: malloc failed.");
+        return -1;
+    }
+
+    rrd_value_t *start = rrd->rrd_value + start_row_cnt * ds_cnt;
+    /* */            
+    memcpy(temp, start,
+            a * ds_cnt * sizeof(rrd_value_t));
+    
+    memmove(start,
+            start + a * ds_cnt,
+            (cur_rra_ptr->cur_row + 1) * ds_cnt * sizeof(rrd_value_t));
+            
+    memcpy(start + (cur_rra_ptr->cur_row + 1) * ds_cnt,
+           temp,
+           a * ds_cnt * sizeof(rrd_value_t));
+            
+    free(temp);
+
     return (status);
 }                       /* int parse_tag_rra_database */
 
@@ -871,9 +935,6 @@ static int parse_tag_rra(
             return status;
         }        
     }    
-    /* Set the RRA pointer to a random location */
-    cur_rra_ptr->cur_row = rrd_random() % cur_rra_def->row_cnt;
-
     return (status);
 }                       /* int parse_tag_rra */