diff --git a/inc/fdb_def.h b/inc/fdb_def.h index 0675e2f..0db5845 100644 --- a/inc/fdb_def.h +++ b/inc/fdb_def.h @@ -282,6 +282,9 @@ struct fdb_kvdb { struct fdb_default_kv default_kvs; /**< default KV */ bool gc_request; /**< request a GC check */ bool in_recovery_check; /**< is in recovery check status when first reboot */ + struct fdb_kv kv; + struct kvdb_sec_info sector; + bool last_is_complete_del; #ifdef FDB_KV_USING_CACHE /* KV cache table */ diff --git a/src/fdb_kvdb.c b/src/fdb_kvdb.c index bd3584f..b1964e8 100644 --- a/src/fdb_kvdb.c +++ b/src/fdb_kvdb.c @@ -849,7 +849,6 @@ static fdb_err_t del_kv(fdb_kvdb_t db, const char *key, fdb_kv_t old_kv, bool co { fdb_err_t result = FDB_NO_ERR; uint32_t dirty_status_addr; - static bool last_is_complete_del = false; uint8_t status_table[KV_STATUS_TABLE_SIZE >= FDB_DIRTY_STATUS_TABLE_SIZE ? KV_STATUS_TABLE_SIZE : FDB_DIRTY_STATUS_TABLE_SIZE]; @@ -867,11 +866,11 @@ static fdb_err_t del_kv(fdb_kvdb_t db, const char *key, fdb_kv_t old_kv, bool co /* change and save the new status */ if (!complete_del) { result = _fdb_write_status((fdb_db_t)db, old_kv->addr.start, status_table, FDB_KV_STATUS_NUM, FDB_KV_PRE_DELETE); - last_is_complete_del = true; + db->last_is_complete_del = true; } else { result = _fdb_write_status((fdb_db_t)db, old_kv->addr.start, status_table, FDB_KV_STATUS_NUM, FDB_KV_DELETED); - if (!last_is_complete_del && result == FDB_NO_ERR) { + if (!db->last_is_complete_del && result == FDB_NO_ERR) { #ifdef FDB_KV_USING_CACHE /* delete the KV in flash and cache */ if (key != NULL) { @@ -884,7 +883,7 @@ static fdb_err_t del_kv(fdb_kvdb_t db, const char *key, fdb_kv_t old_kv, bool co #endif /* FDB_KV_USING_CACHE */ } - last_is_complete_del = false; + db->last_is_complete_del = false; } dirty_status_addr = FDB_ALIGN_DOWN(old_kv->addr.start, db_sec_size(db)) + SECTOR_DIRTY_OFFSET; @@ -1185,29 +1184,27 @@ fdb_err_t fdb_kv_del(fdb_kvdb_t db, const char *key) static fdb_err_t set_kv(fdb_kvdb_t db, const char *key, const void *value_buf, size_t buf_len) { fdb_err_t result = FDB_NO_ERR; - static struct fdb_kv kv; - static struct kvdb_sec_info sector; bool kv_is_found = false; if (value_buf == NULL) { result = del_kv(db, key, NULL, true); } else { /* make sure the flash has enough space */ - if (new_kv_ex(db, §or, strlen(key), buf_len) == FAILED_ADDR) { + if (new_kv_ex(db, &db->sector, strlen(key), buf_len) == FAILED_ADDR) { return FDB_SAVED_FULL; } - kv_is_found = find_kv(db, key, &kv); + kv_is_found = find_kv(db, key, &db->kv); /* prepare to delete the old KV */ if (kv_is_found) { - result = del_kv(db, key, &kv, false); + result = del_kv(db, key, &db->kv, false); } /* create the new KV */ if (result == FDB_NO_ERR) { - result = create_kv_blob(db, §or, key, value_buf, buf_len); + result = create_kv_blob(db, &db->sector, key, value_buf, buf_len); } /* delete the old KV */ if (kv_is_found && result == FDB_NO_ERR) { - result = del_kv(db, key, &kv, true); + result = del_kv(db, key, &db->kv, true); } /* process the GC after set KV */ if (db->gc_request) { @@ -1596,6 +1593,7 @@ fdb_err_t fdb_kvdb_init(fdb_kvdb_t db, const char *name, const char *part_name, db->gc_request = false; db->in_recovery_check = false; + db->last_is_complete_del = false; db->default_kvs = *default_kv; /* there is at least one empty sector for GC. */ FDB_ASSERT((FDB_GC_EMPTY_SEC_THRESHOLD > 0 && FDB_GC_EMPTY_SEC_THRESHOLD < SECTOR_NUM))