diff --git a/docs/api.md b/docs/api.md index 948e2bc..571d528 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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 | diff --git a/docs/sample-kvdb-traversal.md b/docs/sample-kvdb-traversal.md index 0e7d285..ca649a1 100644 --- a/docs/sample-kvdb-traversal.md +++ b/docs/sample-kvdb-traversal.md @@ -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); diff --git a/docs/zh-cn/api.md b/docs/zh-cn/api.md index c829f81..5c768c1 100644 --- a/docs/zh-cn/api.md +++ b/docs/zh-cn/api.md @@ -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 | 待初始化的迭代器对象 | | 返回 | 初始化后的迭代器对象 | diff --git a/docs/zh-cn/sample-kvdb-traversal.md b/docs/zh-cn/sample-kvdb-traversal.md index def2dbc..c366d2a 100644 --- a/docs/zh-cn/sample-kvdb-traversal.md +++ b/docs/zh-cn/sample-kvdb-traversal.md @@ -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); diff --git a/inc/fdb_def.h b/inc/fdb_def.h index 9334a8f..8008879 100644 --- a/inc/fdb_def.h +++ b/inc/fdb_def.h @@ -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; diff --git a/inc/flashdb.h b/inc/flashdb.h index f60c791..fe0ced8 100644 --- a/inc/flashdb.h +++ b/inc/flashdb.h @@ -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 */ diff --git a/src/fdb_kvdb.c b/src/fdb_kvdb.c index 160f054..e2dfc63 100644 --- a/src/fdb_kvdb.c +++ b/src/fdb_kvdb.c @@ -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, §or, 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, §or, traversed_len)) != FAILED_ADDR); + itr->traversed_len += db_sec_size(db); + } while ((itr->sector_addr = get_next_sector_addr(db, §or, itr->traversed_len)) != FAILED_ADDR); /* Finally we have iterated all the KVs. */ return false; }