[kvdb] fix #211 , fdb kv iterate from the oldest sector.

This commit is contained in:
朱天龙 (Armink) 2023-04-29 16:11:35 +08:00
parent 6e8e9e7780
commit c829b3d8e2
7 changed files with 14 additions and 10 deletions

View File

@ -221,10 +221,11 @@ Reset the KV in KVDB to the **first initial** default value
| Return | Converted blob object |
### Initialize KV iterator
`fdb_kv_iterator_t fdb_kv_iterator_init(fdb_kv_iterator_t itr)`
`fdb_kv_iterator_t fdb_kv_iterator_init(fdb_kvdb_t db, fdb_kv_iterator_t itr)`
| Parameters | Description |
| ---- | -------------------- |
| db | Database Objects |
| itr | Iterator object to be initialized |
| Return | Iterator object after initialization |

View File

@ -17,7 +17,7 @@ void kvdb_tarversal_sample(fdb_kvdb_t kvdb)
size_t data_size;
uint8_t *data_buf;
fdb_kv_iterator_init(&iterator);
fdb_kv_iterator_init(kvdb, &iterator);
while (fdb_kv_iterate(kvdb, &iterator)) {
cur_kv = &(iterator.curr_kv);

View File

@ -221,10 +221,11 @@ if (blob.saved.len > 0) {
| 返回 | 转换后的 blob 对象 |
### 初始化 KV 迭代器
`fdb_kv_iterator_t fdb_kv_iterator_init(fdb_kv_iterator_t itr)`
`fdb_kv_iterator_t fdb_kv_iterator_init(fdb_kvdb_t db, fdb_kv_iterator_t itr)`
| 参数 | 描述 |
| ---- | -------------------- |
| db | 数据库对象 |
| itr | 待初始化的迭代器对象 |
| 返回 | 初始化后的迭代器对象 |

View File

@ -17,7 +17,7 @@ void kvdb_tarversal_sample(fdb_kvdb_t kvdb)
size_t data_size;
uint8_t *data_buf;
fdb_kv_iterator_init(&iterator);
fdb_kv_iterator_init(kvdb, &iterator);
while (fdb_kv_iterate(kvdb, &iterator)) {
cur_kv = &(iterator.curr_kv);

View File

@ -164,6 +164,7 @@ struct fdb_kv_iterator {
size_t iterated_obj_bytes; /**< Total storage size of KVs we have iterated. */
size_t iterated_value_bytes; /**< Total value size of KVs we have iterated. */
uint32_t sector_addr; /**< Current sector address we're iterating. DO NOT touch it. */
uint32_t traversed_len; /**< Traversed sector total length. */
};
typedef struct fdb_kv_iterator *fdb_kv_iterator_t;

View File

@ -54,7 +54,7 @@ fdb_kv_t fdb_kv_get_obj (fdb_kvdb_t db, const char *key, fdb_kv_t
fdb_blob_t fdb_kv_to_blob (fdb_kv_t kv, fdb_blob_t blob);
fdb_err_t fdb_kv_set_default (fdb_kvdb_t db);
void fdb_kv_print (fdb_kvdb_t db);
fdb_kv_iterator_t fdb_kv_iterator_init(fdb_kv_iterator_t itr);
fdb_kv_iterator_t fdb_kv_iterator_init(fdb_kvdb_t db, fdb_kv_iterator_t itr);
bool fdb_kv_iterate (fdb_kvdb_t db, fdb_kv_iterator_t itr);
/* Time series log API like a TSDB */

View File

@ -1789,11 +1789,12 @@ fdb_err_t fdb_kvdb_deinit(fdb_kvdb_t db)
/**
* The KV database initialization.
*
* @param db database object
* @param itr iterator structure to be initialized
*
* @return pointer to the iterator initialized.
*/
fdb_kv_iterator_t fdb_kv_iterator_init(fdb_kv_iterator_t itr)
fdb_kv_iterator_t fdb_kv_iterator_init(fdb_kvdb_t db, fdb_kv_iterator_t itr)
{
itr->curr_kv.addr.start = 0;
@ -1801,8 +1802,9 @@ fdb_kv_iterator_t fdb_kv_iterator_init(fdb_kv_iterator_t itr)
itr->iterated_cnt = 0;
itr->iterated_obj_bytes = 0;
itr->iterated_value_bytes = 0;
itr->traversed_len = 0;
/* Start from sector head */
itr->sector_addr = 0;
itr->sector_addr = db_oldest_addr(db);
return itr;
}
@ -1818,10 +1820,8 @@ bool fdb_kv_iterate(fdb_kvdb_t db, fdb_kv_iterator_t itr)
{
struct kvdb_sec_info sector;
fdb_kv_t kv = &(itr->curr_kv);
uint32_t traversed_len = 0;
do {
traversed_len += db_sec_size(db);
if (read_sector_info(db, itr->sector_addr, &sector, false) == FDB_NO_ERR) {
if (sector.status.store == FDB_SECTOR_STORE_USING || sector.status.store == FDB_SECTOR_STORE_FULL) {
if (kv->addr.start == 0) {
@ -1847,7 +1847,8 @@ bool fdb_kv_iterate(fdb_kvdb_t db, fdb_kv_iterator_t itr)
* the kv->addr.start is set to the new sector.addr + SECTOR_HDR_DATA_SIZE.
*/
kv->addr.start = 0;
} while ((itr->sector_addr = get_next_sector_addr(db, &sector, traversed_len)) != FAILED_ADDR);
itr->traversed_len += db_sec_size(db);
} while ((itr->sector_addr = get_next_sector_addr(db, &sector, itr->traversed_len)) != FAILED_ADDR);
/* Finally we have iterated all the KVs. */
return false;
}