[tsdb] add get last save time control command.

This commit is contained in:
armink 2020-07-05 23:59:50 +08:00
parent 165d37d531
commit 65a4b8968a
2 changed files with 37 additions and 17 deletions

View File

@ -72,6 +72,7 @@ if (!(EXPR)) \
#define FDB_TSDB_CTRL_SET_UNLOCK 0x3 /**< set unlock function control command */ #define FDB_TSDB_CTRL_SET_UNLOCK 0x3 /**< set unlock function control command */
#define FDB_TSDB_CTRL_SET_ROLLOVER 0x4 /**< set rollover control command */ #define FDB_TSDB_CTRL_SET_ROLLOVER 0x4 /**< set rollover control command */
#define FDB_TSDB_CTRL_GET_ROLLOVER 0x5 /**< get rollover control command */ #define FDB_TSDB_CTRL_GET_ROLLOVER 0x5 /**< get rollover control command */
#define FDB_TSDB_CTRL_GET_LAST_TIME 0x6 /**< get last save time control command */
typedef time_t fdb_time_t; typedef time_t fdb_time_t;
#ifdef FDB_USING_TIMESTAMP_64BIT #ifdef FDB_USING_TIMESTAMP_64BIT

View File

@ -75,9 +75,9 @@ struct sector_hdr_data {
uint32_t magic; /**< magic word(`T`, `S`, `L`, `0`) */ uint32_t magic; /**< magic word(`T`, `S`, `L`, `0`) */
fdb_time_t start_time; /**< the first start node's timestamp */ fdb_time_t start_time; /**< the first start node's timestamp */
struct { struct {
fdb_time_t time; /**< the last end node's timestamp */ fdb_time_t time; /**< the last end node's timestamp */
uint32_t index; /**< the last end node's index */ uint32_t index; /**< the last end node's index */
uint8_t status[TSL_STATUS_TABLE_SIZE]; /**< end node status, @see fdb_tsl_status_t */ uint8_t status[TSL_STATUS_TABLE_SIZE]; /**< end node status, @see fdb_tsl_status_t */
} end_info[2]; } end_info[2];
uint32_t reserved; uint32_t reserved;
}; };
@ -98,7 +98,7 @@ struct query_count_args {
}; };
struct check_sec_hdr_cb_args { struct check_sec_hdr_cb_args {
fdb_tsdb_t db; fdb_tsdb_t db;
bool check_failed; bool check_failed;
size_t empty_num; size_t empty_num;
uint32_t empty_addr; uint32_t empty_addr;
@ -283,10 +283,10 @@ static fdb_err_t write_tsl(fdb_tsdb_t db, fdb_blob_t blob, fdb_time_t time)
static fdb_err_t update_sec_status(fdb_tsdb_t db, tsdb_sec_info_t sector, fdb_blob_t blob, fdb_time_t cur_time) static fdb_err_t update_sec_status(fdb_tsdb_t db, tsdb_sec_info_t sector, fdb_blob_t blob, fdb_time_t cur_time)
{ {
fdb_err_t result = FDB_NO_ERR; fdb_err_t result = FDB_NO_ERR;
uint8_t status[FDB_STORE_STATUS_TABLE_SIZE]; uint8_t status[FDB_STORE_STATUS_TABLE_SIZE];
if (sector->status == FDB_SECTOR_STORE_USING && sector->remain < LOG_IDX_DATA_SIZE + FDB_WG_ALIGN(blob->size)) { if (sector->status == FDB_SECTOR_STORE_USING && sector->remain < LOG_IDX_DATA_SIZE + FDB_WG_ALIGN(blob->size)) {
if (db->rollover) { if (db->rollover) {
uint8_t end_status[TSL_STATUS_TABLE_SIZE]; uint8_t end_status[TSL_STATUS_TABLE_SIZE];
uint32_t end_index = sector->empty_idx - LOG_IDX_DATA_SIZE, new_sec_addr, cur_sec_addr = sector->addr; uint32_t end_index = sector->empty_idx - LOG_IDX_DATA_SIZE, new_sec_addr, cur_sec_addr = sector->addr;
@ -511,9 +511,9 @@ void fdb_tsl_iter_by_time(fdb_tsdb_t db, fdb_time_t from, fdb_time_t to, fdb_tsl
} }
} while ((tsl.addr.index = get_next_tsl_addr(&sector, &tsl)) != FAILED_ADDR); } while ((tsl.addr.index = get_next_tsl_addr(&sector, &tsl)) != FAILED_ADDR);
} }
} else if (sector.status == FDB_SECTOR_STORE_EMPTY) { } else if (sector.status == FDB_SECTOR_STORE_EMPTY) {
return; return;
} }
traversed_len += db_sec_size(db); traversed_len += db_sec_size(db);
} while ((sec_addr = get_next_sector_addr(db, &sector, traversed_len)) != FAILED_ADDR); } while ((sec_addr = get_next_sector_addr(db, &sector, traversed_len)) != FAILED_ADDR);
} }
@ -620,7 +620,7 @@ static bool check_sec_hdr_cb(tsdb_sec_info_t sector, void *arg1, void *arg2)
} }
static bool format_all_cb(tsdb_sec_info_t sector, void *arg1, void *arg2) static bool format_all_cb(tsdb_sec_info_t sector, void *arg1, void *arg2)
{ {
fdb_tsdb_t db = arg1; fdb_tsdb_t db = arg1;
format_sector(db, sector->addr); format_sector(db, sector->addr);
@ -635,6 +635,7 @@ static void tsl_format_all(fdb_tsdb_t db)
sector_iterator(db, &sector, FDB_SECTOR_STORE_UNUSED, db, NULL, format_all_cb, false); sector_iterator(db, &sector, FDB_SECTOR_STORE_UNUSED, db, NULL, format_all_cb, false);
db->oldest_addr = 0; db->oldest_addr = 0;
db->cur_sec.addr = 0; db->cur_sec.addr = 0;
db->last_time = 0;
/* read the current using sector info */ /* read the current using sector info */
read_sector_info(db, db->cur_sec.addr, &db->cur_sec, false); read_sector_info(db, db->cur_sec.addr, &db->cur_sec, false);
@ -667,12 +668,6 @@ void fdb_tsdb_control(fdb_tsdb_t db, int cmd, void *arg)
FDB_ASSERT(db); FDB_ASSERT(db);
switch (cmd) { switch (cmd) {
case FDB_TSDB_CTRL_SET_ROLLOVER:
db->rollover = *(bool *)arg;
break;
case FDB_TSDB_CTRL_GET_ROLLOVER:
*(bool *)arg = db->rollover;
break;
case FDB_TSDB_CTRL_SET_SEC_SIZE: case FDB_TSDB_CTRL_SET_SEC_SIZE:
/* the sector size change MUST before database initialization */ /* the sector size change MUST before database initialization */
FDB_ASSERT(db->parent.init_ok == false); FDB_ASSERT(db->parent.init_ok == false);
@ -687,6 +682,15 @@ void fdb_tsdb_control(fdb_tsdb_t db, int cmd, void *arg)
case FDB_TSDB_CTRL_SET_UNLOCK: case FDB_TSDB_CTRL_SET_UNLOCK:
db->parent.unlock = (void (*)(fdb_db_t db))arg; db->parent.unlock = (void (*)(fdb_db_t db))arg;
break; break;
case FDB_TSDB_CTRL_SET_ROLLOVER:
db->rollover = *(bool *)arg;
break;
case FDB_TSDB_CTRL_GET_ROLLOVER:
*(bool *)arg = db->rollover;
break;
case FDB_TSDB_CTRL_GET_LAST_TIME:
*(fdb_time_t *)arg = db->last_time;
break;
} }
} }
@ -753,6 +757,21 @@ fdb_err_t fdb_tsdb_init(fdb_tsdb_t db, const char *name, const char *part_name,
db->cur_sec.addr); db->cur_sec.addr);
/* read the current using sector info */ /* read the current using sector info */
read_sector_info(db, db->cur_sec.addr, &db->cur_sec, true); read_sector_info(db, db->cur_sec.addr, &db->cur_sec, true);
/* get last save time */
if (db->cur_sec.status == FDB_SECTOR_STORE_USING) {
db->last_time = db->cur_sec.end_time;
} else if (db->cur_sec.status == FDB_SECTOR_STORE_EMPTY && db->oldest_addr != db->cur_sec.addr) {
struct tsdb_sec_info sec;
uint32_t addr = db->cur_sec.addr;
if (addr == 0) {
addr = db_part_size(db) - db_sec_size(db);
} else {
addr -= db_sec_size(db);
}
read_sector_info(db, addr, &sec, false);
db->last_time = sec.end_time;
}
__exit: __exit: