Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions demos/esp32_spi_flash/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ static struct fdb_default_kv_node default_kv_table[] = {
static struct fdb_kvdb kvdb = {0};
/* TSDB object */
struct fdb_tsdb tsdb = {0};
/* counts for simulated timestamp */
static int counts = 0;
static SemaphoreHandle_t s_lock = NULL;

extern void kvdb_basic_sample(fdb_kvdb_t kvdb);
Expand All @@ -51,14 +49,6 @@ static void unlock(fdb_db_t db)
xSemaphoreGive(s_lock);
}

static fdb_time_t get_time(void)
{
/* Using the counts instead of timestamp.
* Please change this function to return RTC time.
*/
return ++counts;
}

int flashdb_demo(void)
{
fdb_err_t result;
Expand Down Expand Up @@ -114,13 +104,11 @@ int flashdb_demo(void)
* "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.
* NULL: Function to get the current timestamp. Define if using RTC; leaving empty (NULL) uses sequential time.
* 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);
/* read last saved time for simulated timestamp */
fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
result = fdb_tsdb_init(&tsdb, "log", "fdb_tsdb1", NULL, 128, NULL);

if (result != FDB_NO_ERR)
{
Expand Down
16 changes: 2 additions & 14 deletions demos/esp8266_spi_flash/main/hello_world_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ static struct fdb_default_kv_node default_kv_table[] = {
static struct fdb_kvdb kvdb = { 0 };
/* TSDB object */
struct fdb_tsdb tsdb = { 0 };
/* counts for simulated timestamp */
static int counts = 0;
static SemaphoreHandle_t s_lock = NULL;

extern void kvdb_basic_sample(fdb_kvdb_t kvdb);
Expand All @@ -49,14 +47,6 @@ static void unlock(fdb_db_t db)
xSemaphoreGive(s_lock);
}

static fdb_time_t get_time(void)
{
/* Using the counts instead of timestamp.
* Please change this function to return RTC time.
*/
return ++counts;
}

int flashdb_demo(void)
{
fdb_err_t result;
Expand Down Expand Up @@ -110,13 +100,11 @@ int flashdb_demo(void)
* "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.
* NULL: Function to get the current timestamp. Define if using RTC; leaving empty (NULL) uses sequential time.
* 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);
/* read last saved time for simulated timestamp */
fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
result = fdb_tsdb_init(&tsdb, "log", "fdb_tsdb1", NULL, 128, NULL);

if (result != FDB_NO_ERR) {
return -1;
Expand Down
8 changes: 3 additions & 5 deletions demos/linux/applications/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ static struct fdb_default_kv_node default_kv_table[] = {
static struct fdb_kvdb kvdb = { 0 };
/* TSDB object */
struct fdb_tsdb tsdb = { 0 };
/* counts for simulated timestamp */
static int counts = 0;

extern void kvdb_basic_sample(fdb_kvdb_t kvdb);
extern void kvdb_type_string_sample(fdb_kvdb_t kvdb);
Expand All @@ -45,8 +43,10 @@ static void unlock(fdb_db_t db)
pthread_mutex_unlock((pthread_mutex_t *)db->user_data);
}

static fdb_time_t get_time(void)
static fdb_time_t get_time(fdb_tsdb_t db)
{
/* db not used to get time */
(void)db;
return time(NULL);
}

Expand Down Expand Up @@ -125,8 +125,6 @@ int main(void)
* ts_locker: The locker object.
*/
result = fdb_tsdb_init(&tsdb, "log", "fdb_tsdb1", get_time, 128, &ts_locker);
/* read last saved time for simulated timestamp */
fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);

if (result != FDB_NO_ERR) {
return -1;
Expand Down
16 changes: 2 additions & 14 deletions demos/stm32f103ve/applications/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ static struct fdb_default_kv_node default_kv_table[] = {
static struct fdb_kvdb kvdb = { 0 };
/* TSDB object */
struct fdb_tsdb tsdb = { 0 };
/* counts for simulated timestamp */
static int counts = 0;

extern void kvdb_basic_sample(fdb_kvdb_t kvdb);
extern void kvdb_type_string_sample(fdb_kvdb_t kvdb);
Expand All @@ -46,14 +44,6 @@ static void unlock(fdb_db_t db)
__enable_irq();
}

static fdb_time_t get_time(void)
{
/* Using the counts instead of timestamp.
* Please change this function to return RTC time.
*/
return ++counts;
}

int main(void)
{
fdb_err_t result;
Expand Down Expand Up @@ -102,13 +92,11 @@ int main(void)
* "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.
* NULL: Function to get the current timestamp. Define if using RTC; leaving empty (NULL) uses sequential time.
* 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);
/* read last saved time for simulated timestamp */
fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
result = fdb_tsdb_init(&tsdb, "log", "fdb_tsdb1", NULL, 128, NULL);

if (result != FDB_NO_ERR) {
return -1;
Expand Down
16 changes: 2 additions & 14 deletions demos/stm32f405rg/applications/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ static struct fdb_default_kv_node default_kv_table[] = {
static struct fdb_kvdb kvdb = { 0 };
/* TSDB object */
struct fdb_tsdb tsdb = { 0 };
/* counts for simulated timestamp */
static int counts = 0;

extern void kvdb_basic_sample(fdb_kvdb_t kvdb);
extern void kvdb_type_string_sample(fdb_kvdb_t kvdb);
Expand All @@ -46,14 +44,6 @@ static void unlock(fdb_db_t db)
__enable_irq();
}

static fdb_time_t get_time(void)
{
/* Using the counts instead of timestamp.
* Please change this function to return RTC time.
*/
return ++counts;
}

int main(void)
{
fdb_err_t result;
Expand Down Expand Up @@ -102,13 +92,11 @@ int main(void)
* "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.
* NULL: Function to get the current timestamp. Define if using RTC; leaving empty (NULL) uses sequential time.
* 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);
/* read last saved time for simulated timestamp */
fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
result = fdb_tsdb_init(&tsdb, "log", "fdb_tsdb1", NULL, 128, NULL);

if (result != FDB_NO_ERR) {
return -1;
Expand Down
16 changes: 2 additions & 14 deletions demos/stm32f405rg_spi_flash/applications/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ static struct fdb_default_kv_node default_kv_table[] = {
static struct fdb_kvdb kvdb = { 0 };
/* TSDB object */
struct fdb_tsdb tsdb = { 0 };
/* counts for simulated timestamp */
static int counts = 0;

extern void kvdb_basic_sample(fdb_kvdb_t kvdb);
extern void kvdb_type_string_sample(fdb_kvdb_t kvdb);
Expand All @@ -46,14 +44,6 @@ static void unlock(fdb_db_t db)
__enable_irq();
}

static fdb_time_t get_time(void)
{
/* Using the counts instead of timestamp.
* Please change this function to return RTC time.
*/
return ++counts;
}

int main(void)
{
fdb_err_t result;
Expand Down Expand Up @@ -102,13 +92,11 @@ int main(void)
* "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.
* NULL: Function to get the current timestamp. Define if using RTC; leaving empty (NULL) uses sequential time.
* 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);
/* read last saved time for simulated timestamp */
fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
result = fdb_tsdb_init(&tsdb, "log", "fdb_tsdb1", NULL, 128, NULL);

if (result != FDB_NO_ERR) {
return -1;
Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ Using this iterator API, all KVs in the entire KVDB can be traversed.
| db | Database Objects |
| name | Database name |
| path | FAL mode: the partition name in the partition table, file mode: the path where the database is saved |
| get_time | Function to get the current timestamp |
| get_time | Function to get the current timestamp, NULL to use the default sequential timestamps. |
| max_len | Maximum length of each TSL |
| user_data | User-defined data, NULL if not available |
| Return | Error Code |
Expand Down
10 changes: 4 additions & 6 deletions docs/demo-details.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ In the demo project, the `main function` in `main.c` is the entry function. This
* "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.
* NULL: Function to get the current timestamp. Define if using RTC; leaving empty (NULL) uses sequential time.
* 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);
/* read last saved time for simulated timestamp */
fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);
result = fdb_tsdb_init(&tsdb, "log", "fdb_tsdb1", NULL, 128, NULL);

if (result != FDB_NO_ERR) {
return -1;
Expand All @@ -86,9 +84,9 @@ For bare metal platforms, the lock and unlock callbacks are usually set to close

#### timestamp simulation

For TSDB, the timestamp in the normal project should be obtained through RTC or network clock, but here to enhance the versatility of the demonstration project, use `fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);` to get the last used timestamp of TSDB, Deposit in `counts`. Every time you use `get_time` to get the current time, it will add one to `counts` to simulate the action of moving forward in time and avoid repetition.
In a normal TSDB project, the application should define the get_time function to obtain timestamps from a RTC. In this demonstration project, to keep things simple, passing NULL as the get_time function causes TSDB to use a default timestamp function that increments the last timestamp by one for each record, simulating forward-moving time.

Therefore, the time stamp simulated by this method does not have the meaning of real-time time, just to make the time stamp inserted in each record not repeated.
Tip: If you don’t need RTC timestamps and want to reduce flash usage, you can enable FDB_TSDB_USING_SEQ_MODE in fdb_cfg_template.h

#### Example

Expand Down
7 changes: 7 additions & 0 deletions inc/fdb_cfg_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
* Warning: If defined will be incompatible with variable blob flash store or if fixed blob size is later changed */
/* #define FDB_TSDB_FIXED_BLOB_SIZE 4 */


/* Use sequential mode - eliminates per-entry timestamp storage by calculating timestamps
* on-the-fly, saving sizeof(fdb_time_t).
* In this mode, timestamps increment by 1; providing a non-NULL get_time causes an ASSERT.
* Warning: If defined will be incompatible with time flash store */
/* #define FDB_TSDB_USING_SEQ_MODE */

/* Using FAL storage mode */
#define FDB_USING_FAL_MODE

Expand Down
6 changes: 4 additions & 2 deletions inc/fdb_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ if (!(EXPR)) \
typedef int32_t fdb_time_t;
#endif /* FDB_USING_TIMESTAMP_64BIT */

typedef fdb_time_t (*fdb_get_time)(void);
struct fdb_tsdb;
typedef struct fdb_tsdb *fdb_tsdb_t;

typedef fdb_time_t (*fdb_get_time)(fdb_tsdb_t db);

struct fdb_default_kv_node {
char *key;
Expand Down Expand Up @@ -329,7 +332,6 @@ struct fdb_tsdb {

void *user_data;
};
typedef struct fdb_tsdb *fdb_tsdb_t;

/* blob structure */
struct fdb_blob {
Expand Down
Loading