避免静态变量导致多DB访问冲突

This commit is contained in:
rabbitsaviola 2021-02-02 19:30:30 +08:00
parent 119a08516c
commit b3041a1770
2 changed files with 12 additions and 11 deletions

View File

@ -282,6 +282,9 @@ struct fdb_kvdb {
struct fdb_default_kv default_kvs; /**< default KV */ struct fdb_default_kv default_kvs; /**< default KV */
bool gc_request; /**< request a GC check */ bool gc_request; /**< request a GC check */
bool in_recovery_check; /**< is in recovery check status when first reboot */ 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 #ifdef FDB_KV_USING_CACHE
/* KV cache table */ /* KV cache table */

View File

@ -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; fdb_err_t result = FDB_NO_ERR;
uint32_t dirty_status_addr; 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]; 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 */ /* change and save the new status */
if (!complete_del) { 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); 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 { } else {
result = _fdb_write_status((fdb_db_t)db, old_kv->addr.start, status_table, FDB_KV_STATUS_NUM, FDB_KV_DELETED); 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 #ifdef FDB_KV_USING_CACHE
/* delete the KV in flash and cache */ /* delete the KV in flash and cache */
if (key != NULL) { 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 */ #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; 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) 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; fdb_err_t result = FDB_NO_ERR;
static struct fdb_kv kv;
static struct kvdb_sec_info sector;
bool kv_is_found = false; bool kv_is_found = false;
if (value_buf == NULL) { if (value_buf == NULL) {
result = del_kv(db, key, NULL, true); result = del_kv(db, key, NULL, true);
} else { } else {
/* make sure the flash has enough space */ /* make sure the flash has enough space */
if (new_kv_ex(db, &sector, strlen(key), buf_len) == FAILED_ADDR) { if (new_kv_ex(db, &db->sector, strlen(key), buf_len) == FAILED_ADDR) {
return FDB_SAVED_FULL; 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 */ /* prepare to delete the old KV */
if (kv_is_found) { if (kv_is_found) {
result = del_kv(db, key, &kv, false); result = del_kv(db, key, &db->kv, false);
} }
/* create the new KV */ /* create the new KV */
if (result == FDB_NO_ERR) { if (result == FDB_NO_ERR) {
result = create_kv_blob(db, &sector, key, value_buf, buf_len); result = create_kv_blob(db, &db->sector, key, value_buf, buf_len);
} }
/* delete the old KV */ /* delete the old KV */
if (kv_is_found && result == FDB_NO_ERR) { 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 */ /* process the GC after set KV */
if (db->gc_request) { 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->gc_request = false;
db->in_recovery_check = false; db->in_recovery_check = false;
db->last_is_complete_del = false;
db->default_kvs = *default_kv; db->default_kvs = *default_kv;
/* there is at least one empty sector for GC. */ /* there is at least one empty sector for GC. */
FDB_ASSERT((FDB_GC_EMPTY_SEC_THRESHOLD > 0 && FDB_GC_EMPTY_SEC_THRESHOLD < SECTOR_NUM)) FDB_ASSERT((FDB_GC_EMPTY_SEC_THRESHOLD > 0 && FDB_GC_EMPTY_SEC_THRESHOLD < SECTOR_NUM))