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:

      1. static bool query_cb(fdb_tsl_t tsl, void *arg)
      2. {
      3. struct fdb_blob blob;
      4. struct env_status status;
      5. fdb_tsdb_t db = arg;
      6. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
      7. FDB_INFO("[query_cb] queried a TSL: time: %ld, temp: %d, humi: %d\n", tsl->time, status.temp, status.humi);
      8. 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 from FDB_TSL_WRITE to FDB_TSL_DELETED state, and skip FDB_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:

      1. static bool set_status_cb(fdb_tsl_t tsl, void *arg)
      2. {
      3. fdb_tsdb_t db = arg;
      4. FDB_INFO("set the TSL (time %ld) status from %d to %d\n", tsl->time, tsl->status, FDB_TSL_USER_STATUS1);
      5. fdb_tsl_set_status(db, tsl, FDB_TSL_USER_STATUS1);
      6. return false;
      7. }

    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.

    1. [FlashDB][sample][tsdb] append the new status.temp (36) and status.humi (85)
    2. [FlashDB][sample][tsdb] append the new status.temp (38) and status.humi (90)
    3. [FlashDB][sample][tsdb] [query_cb] queried a TSL: time: 1, temp: 36, humi: 85
    4. [FlashDB][sample][tsdb] [query_cb] queried a TSL: time: 2, temp: 38, humi: 90
    5. [FlashDB][sample][tsdb] [query_cb] queried a TSL: time: 3, temp: 36, humi: 85
    6. [FlashDB][sample][tsdb] [query_cb] queried a TSL: time: 4, temp: 38, humi: 90
    7. [FlashDB][sample][tsdb] [query_by_time_cb] queried a TSL: time: 1, temp: 36, humi: 85
    8. [FlashDB][sample][tsdb] [query_by_time_cb] queried a TSL: time: 2, temp: 38, humi: 90
    9. [FlashDB][sample][tsdb] [query_by_time_cb] queried a TSL: time: 3, temp: 36, humi: 85
    10. [FlashDB][sample][tsdb] [query_by_time_cb] queried a TSL: time: 4, temp: 38, humi: 90
    11. [FlashDB][sample][tsdb] query count is: 2
    12. [FlashDB][sample][tsdb] set the TSL (time 1) status from 3 to 3
    13. [FlashDB][sample][tsdb] set the TSL (time 2) status from 3 to 3
    14. [FlashDB][sample][tsdb] set the TSL (time 3) status from 2 to 3
    15. [FlashDB][sample][tsdb] set the TSL (time 4) status from 2 to 3