Code

store_json: Base the memstore emitter on the store-writer API.
[sysdb.git] / src / core / timeseries.c
index 06c321c3f253198e47b57303bbcbe7b30df4d6ad..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)
 {
@@ -67,6 +109,7 @@ sdb_timeseries_destroy(sdb_timeseries_t *ts)
        }
        ts->data_names = NULL;
        ts->data_names_len = 0;
+       free(ts);
 } /* sdb_timeseries_destroy */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */