TSDB basic example
The sample code is located in , including the processes of appending, querying and status modification. The approximate code is as follows:
Let’s look at these processes separately
Append: Modify the value of the structure object
status
twice and append it to TSDB;Query: Through TSDB’s iterator API, the
query_cb
callback function will be automatically executed during each iteration to query all records in TSDB. The content of the callback function is as follows:static bool query_cb(fdb_tsl_t tsl, void *arg)
{
struct fdb_blob blob;
struct env_status status;
fdb_tsdb_t db = arg;
fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
FDB_INFO("[query_cb] queried a TSL: time: %ld, temp: %d, humi: %d\n", tsl->time, status.temp, status.humi);
return false;
Modify Status: After each TSL is added to TSDB, its status can be modified. There are 4 types of status:
FDB_TSL_WRITE
: written state, the default state after TSL is appended to TSDB;FDB_TSL_USER_STATUS1
: The status is between writing and deleting. Users can customize the meaning of the status, such as: data has been synchronized to the cloud;: Deleted state, when TSL needs to be deleted, just modify the state of TSL to this state;
FDB_TSL_USER_STATUS2
: the customized status after the status is deleted, reserved for users;
When modifying the status, it can only be modified in the order of
FDB_TSL_WRITE -> FDB_TSL_USER_STATUS1 -> FDB_TSL_DELETED -> FDB_TSL_USER_STATUS2
, and cannot be modified in reverse order. It is also possible to skip intermediate states, for example: directly change fromFDB_TSL_WRITE
toFDB_TSL_DELETED
state, and skipFDB_TSL_USER_STATUS1
state.In the example, all current TSLs are modified to the status of
FDB_TSL_USER_STATUS1
through the iterator. The callback code in the iterator is as follows:static bool set_status_cb(fdb_tsl_t tsl, void *arg)
{
fdb_tsdb_t db = arg;
FDB_INFO("set the TSL (time %ld) status from %d to %d\n", tsl->time, tsl->status, FDB_TSL_USER_STATUS1);
fdb_tsl_set_status(db, tsl, FDB_TSL_USER_STATUS1);
return false;
}
It can be seen from the log that the example first adds two TSLs, and each TSL stores different temperature and humidity records. Then obtain the TSL in TSDB through ordinary query and query by time, and finally modify its status from 2: FDB_TSL_WRITE
to 3: FDB_TSL_USER_STATUS1
.
[FlashDB][sample][tsdb] append the new status.temp (36) and status.humi (85)
[FlashDB][sample][tsdb] append the new status.temp (38) and status.humi (90)
[FlashDB][sample][tsdb] [query_cb] queried a TSL: time: 1, temp: 36, humi: 85
[FlashDB][sample][tsdb] [query_cb] queried a TSL: time: 2, temp: 38, humi: 90
[FlashDB][sample][tsdb] [query_cb] queried a TSL: time: 3, temp: 36, humi: 85
[FlashDB][sample][tsdb] [query_cb] queried a TSL: time: 4, temp: 38, humi: 90
[FlashDB][sample][tsdb] [query_by_time_cb] queried a TSL: time: 1, temp: 36, humi: 85
[FlashDB][sample][tsdb] [query_by_time_cb] queried a TSL: time: 2, temp: 38, humi: 90
[FlashDB][sample][tsdb] [query_by_time_cb] queried a TSL: time: 3, temp: 36, humi: 85
[FlashDB][sample][tsdb] [query_by_time_cb] queried a TSL: time: 4, temp: 38, humi: 90
[FlashDB][sample][tsdb] query count is: 2
[FlashDB][sample][tsdb] set the TSL (time 1) status from 3 to 3
[FlashDB][sample][tsdb] set the TSL (time 2) status from 3 to 3
[FlashDB][sample][tsdb] set the TSL (time 3) status from 2 to 3
[FlashDB][sample][tsdb] set the TSL (time 4) status from 2 to 3