KVDB basic example

This example mainly demonstrates the basic functions of KVDB, including KV acquisition and setting modification functions.

Code description

The sample code is located in samples/kvdb_basic_sample.c, the default KV table is defined in main.c, and there is boot_count KV in it. The KV is used to record the current system startup times. Each time the power is turned off and restarted, the KV will be automatically incremented by one and saved to KVDB. The general content is as follows:

  1. void kvdb_basic_sample(fdb_kvdb_t kvdb)
  2. {
  3. struct fdb_blob blob;
  4. int boot_count = 0;
  5. FDB_INFO("==================== kvdb_basic_sample ====================\n");
  6. { /* GET the KV value */
  7. /* get the "boot_count" KV value */
  8. fdb_kv_get_blob(kvdb, "boot_count", fdb_blob_make(&blob, &boot_count, sizeof(boot_count)));
  9. /* the blob.saved.len is more than 0 when get the value successful */
  10. if (blob.saved.len > 0) {
  11. FDB_INFO("get the 'boot_count' value is %d\n", boot_count);
  12. } else {
  13. FDB_INFO("get the 'boot_count' failed\n");
  14. }
  15. }
  16. { /* CHANGE the KV value */
  17. /* increase the boot count */
  18. boot_count ++;
  19. /* change the "boot_count" KV's value */
  20. fdb_kv_set_blob(kvdb, "boot_count", fdb_blob_make(&blob, &boot_count, sizeof(boot_count)));
  21. FDB_INFO("set the 'boot_count' value to %d\n", boot_count);
  22. }
  23. FDB_INFO("===========================================================\n");
  24. }

First run log

The current boot_count is 0, after adding one, it is stored in the database.

  1. [FlashDB][sample][kvdb][basic] ==================== kvdb_basic_sample ====================
  2. [FlashDB][sample][kvdb][basic] get the 'boot_count' value is 0
  3. [FlashDB][sample][kvdb][basic] set the 'boot_count' value to 1
  4. [FlashDB][sample][kvdb][basic] ===========================================================

Secondary run log

The current boot_count is 1, indicating that the last save is effective, add one to it and save it for the next visit.

  1. [FlashDB][sample][kvdb][basic] ==================== kvdb_basic_sample ====================
  2. [FlashDB][sample][kvdb][basic] get the 'boot_count' value is 1
  3. [FlashDB][sample][kvdb][basic] set the 'boot_count' value to 2
  4. [FlashDB][sample][kvdb][basic] ===========================================================