]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
Pull out add_rrds in an attempt to shorten functions to a manageable size
authorPeter Stamfest <peter@stamfest.at>
Thu, 27 Feb 2014 22:31:06 +0000 (23:31 +0100)
committerPeter Stamfest <peter@stamfest.at>
Thu, 27 Feb 2014 22:31:06 +0000 (23:31 +0100)
src/rrd_modify.c

index e30ed5805dc4a9f58dfa9f9cc79e77e460088946..8503a4a0bacb8723a09c4763248a93b3188aa79b 100644 (file)
 
 #include <locale.h>
 
-// prototype
+typedef struct {
+    /* the index of the RRA to be changed or -1 if there is no current
+       RRA */
+    int index;
+    /* what operation */
+    char op;  // '+', '-', '=', 'a'
+    /* the number originally specified with the operation (eg. rows to
+       be added) */
+    unsigned int row_count;
+    /* the resulting final row count for the RRA */
+    unsigned int final_row_count;
+    /* An RRA definition in case of an addition */
+    char *def;
+} rra_mod_op_t;
+
+// prototypes
 static int write_rrd(const char *outfilename, rrd_t *out);
+static int add_rras(rrd_t *out, rra_mod_op_t *rra_mod_ops, int rra_mod_ops_cnt);
 
 /* a convenience realloc/memcpy combo  */
 static void * copy_over_realloc(void *dest, int dest_index, 
@@ -40,22 +56,6 @@ static void * copy_over_realloc(void *dest, int dest_index,
    added.
 */
 
-typedef struct {
-    /* the index of the RRA to be changed or -1 if there is no current
-       RRA */
-    int index;
-    /* what operation */
-    char op;  // '+', '-', '=', 'a'
-    /* the number originally specified with the operation (eg. rows to
-       be added) */
-    unsigned int row_count;
-    /* the resulting final row count for the RRA */
-    unsigned int final_row_count;
-    /* An RRA definition in case of an addition */
-    char *def;
-} rra_mod_op_t;
-
-
 static int rrd_modify_r(const char *infilename,
                        const char *outfilename,
                        const char **removeDS,
@@ -560,8 +560,49 @@ static int rrd_modify_r(const char *infilename,
        out_rra++;
     }
 
+    rc = add_rras(&out, rra_mod_ops, rra_mod_ops_cnt);
+
+    if (rc != 0) goto done;
+
+    rc = write_rrd(outfilename, &out);
+
+done:
+    /* clean up */
+    if (old_locale) 
+       setlocale(LC_NUMERIC, old_locale);
+
+    if (rrd_file != NULL) {
+       rrd_close(rrd_file);
+    }
+    rrd_free(&in);
+    rrd_free(&out);
+
+    if (ops != NULL) free(ops);
+
+    return rc;
+}
+
+static int add_rras(rrd_t *out, rra_mod_op_t *rra_mod_ops, int rra_mod_ops_cnt) 
+{
+    int rc = -1;
+
     /* now add any new RRAs: */
-    unsigned int last_rra_cnt = out.stat_head->rra_cnt;
+    cdp_prep_t empty_cdp_prep;
+    int i, r;
+    unsigned int last_rra_cnt = out->stat_head->rra_cnt;
+    int total_out_rra_rows = 0;
+    int total_cnt_out = 0;
+
+    memset(&empty_cdp_prep, 0, sizeof(cdp_prep_t));
+
+
+    // first, calculate total number of rows already in rrd_value...
+
+    for (i = 0 ; i < (int) last_rra_cnt ; i++) {
+       total_out_rra_rows += out->rra_def[i].row_cnt;
+    }
+    total_cnt_out = out->stat_head->ds_cnt * total_out_rra_rows;
+
     for (r = 0 ; r < rra_mod_ops_cnt ; r++) {
        if (rra_mod_ops[r].op == 'a') {
            rra_def_t rra_def;
@@ -572,11 +613,11 @@ static int rrd_modify_r(const char *infilename,
                goto done;
            }
 
-           out.rra_def = copy_over_realloc(out.rra_def, out.stat_head->rra_cnt,
+           out->rra_def = copy_over_realloc(out->rra_def, out->stat_head->rra_cnt,
                                            &rra_def, 0,
                                            sizeof(rra_def_t));
-           if (out.rra_def == NULL) goto done;
-           out.stat_head->rra_cnt++;
+           if (out->rra_def == NULL) goto done;
+           out->stat_head->rra_cnt++;
 
            /*
            rrd.stat_head->rra_cnt++;
@@ -588,93 +629,79 @@ static int rrd_modify_r(const char *infilename,
                return -1;
            }
            */
-           out.rra_def = handle_dependent_rras(out.rra_def, &(out.stat_head->rra_cnt), 
+           out->rra_def = handle_dependent_rras(out->rra_def, &(out->stat_head->rra_cnt), 
                                                219283213712631);
-           if (out.rra_def == NULL) {
+           if (out->rra_def == NULL) {
                goto done;
            }
        }
     }
 
-    if (last_rra_cnt < out.stat_head->rra_cnt) {
+    if (last_rra_cnt < out->stat_head->rra_cnt) {
        // extend cdp_prep and rra_ptr arrays
-       out.cdp_prep = realloc(out.cdp_prep, 
-                              sizeof(cdp_prep_t) * out.stat_head->ds_cnt 
-                              * (out.stat_head->rra_cnt));
+       out->cdp_prep = realloc(out->cdp_prep, 
+                               sizeof(cdp_prep_t) * out->stat_head->ds_cnt 
+                               * (out->stat_head->rra_cnt));
 
-       if (out.cdp_prep == NULL) {
+       if (out->cdp_prep == NULL) {
            rrd_set_error("out of memory");
            goto done;
        }
        
-       out.rra_ptr = realloc(out.rra_ptr,
-                             sizeof(rra_ptr_t) * out.stat_head->rra_cnt);
+       out->rra_ptr = realloc(out->rra_ptr,
+                              sizeof(rra_ptr_t) * out->stat_head->rra_cnt);
        
-       if (out.rra_ptr == NULL) {
+       if (out->rra_ptr == NULL) {
            rrd_set_error("out of memory");
            goto done;
        }
     }
 
-    for ( ; last_rra_cnt < out.stat_head->rra_cnt ; last_rra_cnt++ ) {
+    for ( ; last_rra_cnt < out->stat_head->rra_cnt ; last_rra_cnt++ ) {
        // RRA added!!!
-       rra_def_t *rra_def = out.rra_def + last_rra_cnt;
+       rra_def_t *rra_def = out->rra_def + last_rra_cnt;
 
        // prepare CDPs + values for new RRA
-       int start_index_out = out.stat_head->ds_cnt * last_rra_cnt;
-       for (i = 0 ; i < out.stat_head->ds_cnt ; i++) {
-           cdp_prep_t *cdp_prep = out.cdp_prep + start_index_out + i;
+       int start_index_out = out->stat_head->ds_cnt * last_rra_cnt;
+       for (i = 0 ; i < (int) out->stat_head->ds_cnt ; i++) {
+           cdp_prep_t *cdp_prep = out->cdp_prep + start_index_out + i;
            memcpy(cdp_prep,
                   &empty_cdp_prep, sizeof(cdp_prep_t));
 
-           init_cdp(&out, rra_def, cdp_prep);
+           init_cdp(out, rra_def, cdp_prep);
        }
 
-       out.rra_ptr[last_rra_cnt].cur_row = 0;
+       out->rra_ptr[last_rra_cnt].cur_row = 0;
 
        // extend and fill rrd_value array...
 
        total_out_rra_rows += rra_def->row_cnt;
 
        /* prepare space for output data */
-       out.rrd_value = realloc(out.rrd_value,
-                               (total_out_rra_rows) * out.stat_head->ds_cnt
+       out->rrd_value = realloc(out->rrd_value,
+                               (total_out_rra_rows) * out->stat_head->ds_cnt
                                * sizeof(rrd_value_t));
     
-       if (out.rrd_value == NULL) {
+       if (out->rrd_value == NULL) {
            rrd_set_error("out of memory");
            goto done;
        }
 
        unsigned int oi, jj;
        for (oi = 0 ; oi < rra_def->row_cnt ; oi++) {
-           for (jj = 0 ; jj < out.stat_head->ds_cnt ; jj++) {
-               out.rrd_value[total_cnt_out + oi * out.stat_head->ds_cnt + jj] = DNAN;
+           for (jj = 0 ; jj < out->stat_head->ds_cnt ; jj++) {
+               out->rrd_value[total_cnt_out + oi * out->stat_head->ds_cnt + jj] = DNAN;
            }
        }
 
-       int rra_values_out = out.stat_head->ds_cnt * rra_def->row_cnt;
-       ssize_t rra_size_out = sizeof(rrd_value_t) * rra_values_out;
+       int rra_values_out = out->stat_head->ds_cnt * rra_def->row_cnt;
+       // ssize_t rra_size_out = sizeof(rrd_value_t) * rra_values_out;
 
-       rra_start_out += rra_size_out;
+       //      rra_start_out += rra_size_out;
        total_cnt_out += rra_values_out;
     }
-
-    rc = write_rrd(outfilename, &out);
-
+    rc = 0;
 done:
-    /* clean up */
-    if (old_locale) 
-       setlocale(LC_NUMERIC, old_locale);
-
-    if (rrd_file != NULL) {
-       rrd_close(rrd_file);
-    }
-    rrd_free(&in);
-    rrd_free(&out);
-
-    if (ops != NULL) free(ops);
-
     return rc;
 }