Code

store_json: Base the memstore emitter on the store-writer API.
[sysdb.git] / src / core / timeseries.c
index 48da7f8dab9e47a4ad12b77f8df65bf39fc117bc..e5bff91c97a9fca413e3e5752f1c880403b7a80a 100644 (file)
 #include "core/timeseries.h"
 
 #include <stdlib.h>
+#include <string.h>
 
 /*
  * public API
  */
 
+sdb_timeseries_t *
+sdb_timeseries_create(size_t data_names_len, const char * const *data_names,
+               size_t data_len)
+{
+       sdb_timeseries_t *ts;
+       size_t i;
+
+       ts = calloc(1, sizeof(*ts));
+       if (! ts)
+               return NULL;
+
+       ts->data = calloc(data_names_len, sizeof(*ts->data));
+       if (! ts->data) {
+               sdb_timeseries_destroy(ts);
+               return NULL;
+       }
+       ts->data_names_len = data_names_len;
+       for (i = 0; i < data_names_len; ++i) {
+               ts->data[i] = calloc(data_len, sizeof(**ts->data));
+               if (! ts->data[i]) {
+                       sdb_timeseries_destroy(ts);
+                       return NULL;
+               }
+       }
+       ts->data_len = data_len;
+
+       ts->data_names = calloc(data_names_len, sizeof(*ts->data_names));
+       if (! ts->data_names) {
+               sdb_timeseries_destroy(ts);
+               return NULL;
+       }
+       for (i = 0; i < data_names_len; ++i) {
+               ts->data_names[i] = strdup(data_names[i]);
+               if (! ts->data_names[i]) {
+                       sdb_timeseries_destroy(ts);
+                       return NULL;
+               }
+       }
+       return ts;
+} /* sdb_timeseries_create */
+
 void
 sdb_timeseries_destroy(sdb_timeseries_t *ts)
 {
@@ -46,20 +88,28 @@ sdb_timeseries_destroy(sdb_timeseries_t *ts)
        if (! ts)
                return;
 
-       if (ts->data)
+       if (ts->data) {
+               for (i = 0; i < ts->data_names_len; ++i) {
+                       if (ts->data[i])
+                               free(ts->data[i]);
+                       ts->data[i] = NULL;
+               }
                free(ts->data);
+       }
        ts->data = NULL;
        ts->data_len = 0;
 
-       for (i = 0; i < ts->data_names_len; ++i) {
-               if (ts->data_names[i])
-                       free(ts->data_names[i]);
-               ts->data_names[i] = NULL;
-       }
-       if (ts->data_names)
+       if (ts->data_names) {
+               for (i = 0; i < ts->data_names_len; ++i) {
+                       if (ts->data_names[i])
+                               free(ts->data_names[i]);
+                       ts->data_names[i] = NULL;
+               }
                free(ts->data_names);
+       }
        ts->data_names = NULL;
        ts->data_names_len = 0;
+       free(ts);
 } /* sdb_timeseries_destroy */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */