From: Peter Stamfest Date: Wed, 20 Aug 2014 22:43:18 +0000 (+0200) Subject: Move custom logic row-rotation logic from write_fh, originally there to X-Git-Tag: v1.5.0-rc1~42^2~47 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=aab2ba0ff7280e5db70309bbbac92a72e8436363;p=thirdparty%2Frrdtool-1.x.git Move custom logic row-rotation logic from write_fh, originally there to support rrd_restore with its random cur_row placement to rrd_restore itself. This makes write_fh a truly reuseable function. --- diff --git a/src/rrd_create.c b/src/rrd_create.c index 2768c28f..144aaa4a 100644 --- a/src/rrd_create.c +++ b/src/rrd_create.c @@ -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; } } diff --git a/src/rrd_restore.c b/src/rrd_restore.c index 06eb4a80..df1f0725 100644 --- a/src/rrd_restore.c +++ b/src/rrd_restore.c @@ -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 */