From 0d1b438947deb430b9735a1800e67da2baf229e4 Mon Sep 17 00:00:00 2001 From: Valeriy Kucherenko Date: Thu, 9 Nov 2023 14:27:52 +0300 Subject: [PATCH] Add ability to append TSDB value at specific timestamp (#256) --- inc/flashdb.h | 1 + src/fdb_tsdb.c | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/inc/flashdb.h b/inc/flashdb.h index fe0ced8..2c6efa4 100644 --- a/inc/flashdb.h +++ b/inc/flashdb.h @@ -59,6 +59,7 @@ bool fdb_kv_iterate (fdb_kvdb_t db, fdb_kv_iterator_t itr); /* Time series log API like a TSDB */ fdb_err_t fdb_tsl_append (fdb_tsdb_t db, fdb_blob_t blob); +fdb_err_t fdb_tsl_append_with_ts(fdb_tsdb_t db, fdb_blob_t blob, fdb_time_t timestamp); void fdb_tsl_iter (fdb_tsdb_t db, fdb_tsl_cb cb, void *cb_arg); void fdb_tsl_iter_reverse(fdb_tsdb_t db, fdb_tsl_cb cb, void *cb_arg); void fdb_tsl_iter_by_time(fdb_tsdb_t db, fdb_time_t from, fdb_time_t to, fdb_tsl_cb cb, void *cb_arg); diff --git a/src/fdb_tsdb.c b/src/fdb_tsdb.c index 8fe4083..75e6d0d 100644 --- a/src/fdb_tsdb.c +++ b/src/fdb_tsdb.c @@ -381,10 +381,10 @@ static fdb_err_t update_sec_status(fdb_tsdb_t db, tsdb_sec_info_t sector, fdb_bl return result; } -static fdb_err_t tsl_append(fdb_tsdb_t db, fdb_blob_t blob) +static fdb_err_t tsl_append(fdb_tsdb_t db, fdb_blob_t blob, fdb_time_t *timestamp) { fdb_err_t result = FDB_NO_ERR; - fdb_time_t cur_time = db->get_time(); + fdb_time_t cur_time = timestamp == NULL ? db->get_time() : *timestamp; /* check the append length, MUST less than the db->max_len */ if(blob->size > db->max_len) @@ -441,7 +441,31 @@ fdb_err_t fdb_tsl_append(fdb_tsdb_t db, fdb_blob_t blob) } db_lock(db); - result = tsl_append(db, blob); + result = tsl_append(db, blob, NULL); + db_unlock(db); + + return result; +} + +/** + * Append a new log to TSDB with specific timestamp. + * + * @param db database object + * @param blob log blob data + * + * @return result + */ +fdb_err_t fdb_tsl_append_with_ts(fdb_tsdb_t db, fdb_blob_t blob, fdb_time_t timestamp) +{ + fdb_err_t result = FDB_NO_ERR; + + if (!db_init_ok(db)) { + FDB_INFO("Error: TSL (%s) isn't initialize OK.\n", db_name(db)); + return FDB_INIT_FAILED; + } + + db_lock(db); + result = tsl_append(db, blob, ×tamp); db_unlock(db); return result;