From 0aea1ddff44c4c12b55b0ad110c20eb09c408da9 Mon Sep 17 00:00:00 2001 From: armink Date: Sun, 31 May 2020 19:02:13 +0800 Subject: [PATCH] Update the samples. --- samples/kvdb_type_blob_sample.c | 63 ++++++---------------------- samples/kvdb_type_string_sample.c | 70 +++++++------------------------ samples/tsdb_sample.c | 65 ++++++++-------------------- 3 files changed, 47 insertions(+), 151 deletions(-) diff --git a/samples/kvdb_type_blob_sample.c b/samples/kvdb_type_blob_sample.c index f5add7e..8ce7f0b 100644 --- a/samples/kvdb_type_blob_sample.c +++ b/samples/kvdb_type_blob_sample.c @@ -13,73 +13,32 @@ #include -static uint32_t boot_count = 0; -static time_t boot_time[10] = {0, 1, 2, 3}; -/* default KV nodes */ -static struct fdb_default_kv_node default_kv_table[] = { - {"username", "armink", 0}, /* string KV */ - {"password", "123456", 0}, /* string KV */ - {"boot_count", &boot_count, sizeof(boot_count)}, /* int type KV */ - {"boot_time", &boot_time, sizeof(boot_time)}, /* int array type KV */ -}; -/* KVDB object */ -static struct fdb_kvdb kvdb = { 0 }; +#ifdef FDB_USING_KVDB -static void lock(fdb_db_t db) -{ - /* YOUR CODE HERE */ -} +#define FDB_LOG_TAG "[sample][kvdb][blob]" -static void unlock(fdb_db_t db) +void kvdb_type_blob_sample(fdb_kvdb_t kvdb) { - /* YOUR CODE HERE */ -} - -void kvdb_type_blob_sample(void) -{ - fdb_err_t result; struct fdb_blob blob; - { /* database initialization */ - struct fdb_default_kv default_kv; - - default_kv.kvs = default_kv_table; - default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]); - /* set the lock and unlock function if you want */ - fdb_lock_set((fdb_db_t)&kvdb, lock, unlock); - /* Key-Value database initialization - * - * &kvdb: database object - * "env": database name - * "fdb_kvdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table. - * Please change to YOUR partition name. - * &default_kv: The default KV nodes. It will auto add to KVDB when first initialize successfully. - * NULL: The user data if you need, now is empty. - */ - result = fdb_kvdb_init(&kvdb, "env", "fdb_kvdb1", &default_kv, NULL); - - if (result != FDB_NO_ERR) { - return; - } - } - { /* CREATE new Key-Value */ int temp_data = 36; /* It will create new KV node when "temp" KV not in database. * fdb_blob_make: It's a blob make function, and it will return the blob when make finish. */ - fdb_kv_set_blob(&kvdb, "temp", fdb_blob_make(&blob, &temp_data, sizeof(temp_data))); + fdb_kv_set_blob(kvdb, "temp", fdb_blob_make(&blob, &temp_data, sizeof(temp_data))); + FDB_INFO("create the 'temp' blob KV, value is: %d\n", temp_data); } { /* GET the KV value */ int temp_data = 0; /* get the "temp" KV value */ - fdb_kv_get_blob(&kvdb, "temp", fdb_blob_make(&blob, &temp_data, sizeof(temp_data))); + fdb_kv_get_blob(kvdb, "temp", fdb_blob_make(&blob, &temp_data, sizeof(temp_data))); /* the blob.saved.len is more than 0 when get the value successful */ if (blob.saved.len > 0) { - FDB_PRINT("temp_data: %d\n", temp_data); + FDB_INFO("get the 'temp' value is: %d\n", temp_data); } } @@ -87,10 +46,14 @@ void kvdb_type_blob_sample(void) int temp_data = 38; /* change the "temp" KV's value to 38.1 */ - fdb_kv_set_blob(&kvdb, "temp", fdb_blob_make(&blob, &temp_data, sizeof(temp_data))); + fdb_kv_set_blob(kvdb, "temp", fdb_blob_make(&blob, &temp_data, sizeof(temp_data))); + FDB_INFO("set 'temp' value to %d\n", temp_data); } { /* DELETE the KV by name */ - fdb_kv_del(&kvdb, "temp"); + fdb_kv_del(kvdb, "temp"); + FDB_INFO("delete the 'temp' finish\n"); } } + +#endif /* FDB_USING_KVDB */ diff --git a/samples/kvdb_type_string_sample.c b/samples/kvdb_type_string_sample.c index e3aedac..cdc45b5 100644 --- a/samples/kvdb_type_string_sample.c +++ b/samples/kvdb_type_string_sample.c @@ -14,60 +14,18 @@ #include #include -static uint32_t boot_count = 0; -static time_t boot_time[10] = {0, 1, 2, 3}; -/* default KV nodes */ -static struct fdb_default_kv_node default_kv_table[] = { - {"username", "armink", 0}, /* string KV */ - {"password", "123456", 0}, /* string KV */ - {"boot_count", &boot_count, sizeof(boot_count)}, /* int type KV */ - {"boot_time", &boot_time, sizeof(boot_time)}, /* int array type KV */ -}; -/* KVDB object */ -static struct fdb_kvdb kvdb = { 0 }; +#ifdef FDB_USING_KVDB -static void lock(fdb_db_t db) +#define FDB_LOG_TAG "[sample][kvdb][string]" + +void kvdb_type_string_sample(fdb_kvdb_t kvdb) { - /* YOUR CODE HERE */ -} - -static void unlock(fdb_db_t db) -{ - /* YOUR CODE HERE */ -} - -void kvdb_type_string_sample(void) -{ - fdb_err_t result; - - { /* database initialization */ - struct fdb_default_kv default_kv; - - default_kv.kvs = default_kv_table; - default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]); - /* set the lock and unlock function if you want */ - fdb_lock_set((fdb_db_t)&kvdb, lock, unlock); - /* Key-Value database initialization - * - * &kvdb: database object - * "env": database name - * "fdb_kvdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table. - * Please change to YOUR partition name. - * &default_kv: The default KV nodes. It will auto add to KVDB when first initialize successfully. - * NULL: The user data if you need, now is empty. - */ - result = fdb_kvdb_init(&kvdb, "env", "fdb_kvdb1", &default_kv, NULL); - - if (result != FDB_NO_ERR) { - return; - } - } - { /* CREATE new Key-Value */ - char temp_data[10] = "36"; + char temp_data[10] = "36C"; /* It will create new KV node when "temp" KV not in database. */ - fdb_kv_set(&kvdb, "temp", temp_data); + fdb_kv_set(kvdb, "temp", temp_data); + FDB_INFO("create the 'temp' string KV, value is: %s\n", temp_data); } { /* GET the KV value */ @@ -76,22 +34,26 @@ void kvdb_type_string_sample(void) /* Get the "temp" KV value. * NOTE: The return value saved in fdb_kv_get's buffer. Please copy away as soon as possible. */ - return_value = fdb_kv_get(&kvdb, "temp"); + return_value = fdb_kv_get(kvdb, "temp"); /* the return value is NULL when get the value failed */ if (return_value != NULL) { strncpy(temp_data, return_value, sizeof(temp_data)); - FDB_PRINT("temp_data: %s\n", temp_data); + FDB_INFO("get the 'temp' value is: %s\n", temp_data); } } { /* CHANGE the KV value */ - char temp_data[10] = "38"; + char temp_data[10] = "38C"; /* change the "temp" KV's value to "38.1" */ - fdb_kv_set(&kvdb, "temp", temp_data); + fdb_kv_set(kvdb, "temp", temp_data); + FDB_INFO("set 'temp' value to %s\n", temp_data); } { /* DELETE the KV by name */ - fdb_kv_del(&kvdb, "temp"); + fdb_kv_del(kvdb, "temp"); + FDB_INFO("delete the 'temp' finish\n"); } } + +#endif /* FDB_USING_KVDB */ diff --git a/samples/tsdb_sample.c b/samples/tsdb_sample.c index 2ae3e91..9b7c41f 100644 --- a/samples/tsdb_sample.c +++ b/samples/tsdb_sample.c @@ -16,72 +16,40 @@ #include #include +#ifdef FDB_USING_TSDB + +#define FDB_LOG_TAG "[sample][tsdb]" + struct env_status { int temp; int humi; }; -/* TSDB object */ -static struct fdb_tsdb tsdb = { 0 }; - static bool query_cb(fdb_tsl_t tsl, void *arg); static bool set_status_cb(fdb_tsl_t tsl, void *arg); -static void lock(fdb_db_t db) +void tsdb_sample(fdb_tsdb_t tsdb) { - /* YOUR CODE HERE */ -} - -static void unlock(fdb_db_t db) -{ - /* YOUR CODE HERE */ -} -static fdb_time_t get_time(void) -{ - return time(NULL); -} - -void tsdb_sample(void) -{ - fdb_err_t result; struct fdb_blob blob; - { /* database initialization */ - /* set the lock and unlock function if you want */ - fdb_lock_set((fdb_db_t)&tsdb, lock, unlock); - /* Time series database initialization - * - * &tsdb: database object - * "log": database name - * "fdb_tsdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table. - * Please change to YOUR partition name. - * get_time: The get current timestamp function. - * 128: maximum length of each log - * NULL: The user data if you need, now is empty. - */ - result = fdb_tsdb_init(&tsdb, "log", "fdb_tsdb1", get_time, 128, NULL); - - if (result != FDB_NO_ERR) { - return; - } - } - { /* APPEND new TSL (time series log) */ struct env_status status; /* append new log to TSDB */ status.temp = 36; status.humi = 85; - fdb_tsl_append(&tsdb, fdb_blob_make(&blob, &status, sizeof(status))); + fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status))); + FDB_INFO("append the new status.temp (%d) and status.humi (%d)\n", status.temp, status.humi); status.temp = 38; status.humi = 90; - fdb_tsl_append(&tsdb, fdb_blob_make(&blob, &status, sizeof(status))); + fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status))); + FDB_INFO("append the new status.temp (%d) and status.humi (%d)\n", status.temp, status.humi); } { /* QUERY the TSDB */ /* query all TSL in TSDB by iterator */ - fdb_tsl_iter(&tsdb, query_cb, &tsdb); + fdb_tsl_iter(tsdb, query_cb, tsdb); } { /* QUERY the TSDB by time */ @@ -91,10 +59,10 @@ void tsdb_sample(void) time_t from_time = mktime(&tm_from), to_time = mktime(&tm_to); size_t count; /* query all TSL in TSDB by time */ - fdb_tsl_iter_by_time(&tsdb, from_time, to_time, query_cb, &tsdb); + fdb_tsl_iter_by_time(tsdb, from_time, to_time, query_cb, tsdb); /* query all FDB_TSL_WRITE status TSL's count in TSDB by time */ - count = fdb_tsl_query_count(&tsdb, from_time, to_time, FDB_TSL_WRITE); - FDB_PRINT("query count: %lu\n", count); + count = fdb_tsl_query_count(tsdb, from_time, to_time, FDB_TSL_WRITE); + FDB_INFO("query count is: %u\n", count); } { /* SET the TSL status */ @@ -104,7 +72,7 @@ void tsdb_sample(void) * NOTE: The actions to modify the state must be in order. * FDB_TSL_WRITE -> FDB_TSL_USER_STATUS1 -> FDB_TSL_DELETED -> FDB_TSL_USER_STATUS2 */ - fdb_tsl_iter(&tsdb, set_status_cb, &tsdb); + fdb_tsl_iter(tsdb, set_status_cb, tsdb); } } @@ -115,7 +83,7 @@ static bool query_cb(fdb_tsl_t tsl, void *arg) 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_PRINT("time: %d, temp: %d, humi: %d\n", tsl->time, status.temp, status.humi); + FDB_INFO("queried a TSL: time: %ld, temp: %d, humi: %d\n", tsl->time, status.temp, status.humi); return false; } @@ -124,7 +92,10 @@ 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; } + +#endif /* FDB_USING_TSDB */