Skip to content

Commit

Permalink
Add ability to append TSDB value at specific timestamp (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
valkuc authored Nov 9, 2023
1 parent a208b15 commit 0d1b438
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions inc/flashdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 27 additions & 3 deletions src/fdb_tsdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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, &timestamp);
db_unlock(db);

return result;
Expand Down

0 comments on commit 0d1b438

Please sign in to comment.