armink / FlashDB

An ultra-lightweight database that supports key-value and time series data | 一款支持 KV 数据和时序数据的超轻量级数据库
Apache License 2.0
1.92k stars 435 forks source link

迭代器无法遍历所有键值对 #294

Open LiJingjie05 opened 6 months ago

LiJingjie05 commented 6 months ago

无法使用迭代器遍历所有键值对,添加新的键值对之后,也无法通过迭代器遍历到这个键值对,但是如果直接调用get的接口,是可以读取到数据的。 这是遍历的代码: ` void pt_check_wireless_device(uint8_t dbid, uint8_t type, uint8_t max_size) { struct fdb_kv_iterator iterator; fdb_kv_t cur_kv; struct fdb_blob blob; size_t data_size; uint8_t data_buf; int32_t i = 0; uint32_t y = 0; uint32_t wireless_base_data = dbid1000; uint32_t addr = 0;

int count = 0;

fdb_kv_iterator_init(&iterator);

while (fdb_kv_iterate(ptr_pt_global_kvdb, &iterator)) 
{
    cur_kv = &(iterator.curr_kv);
    data_size = (size_t) cur_kv->value_len;
    data_buf = (uint8_t *) malloc(data_size);
    if (data_buf == NULL)
    {
        break;
    }
    fdb_blob_read((fdb_db_t) ptr_pt_global_kvdb, fdb_kv_to_blob(cur_kv, fdb_blob_make(&blob, data_buf, data_size)));

    // 无线设备键值解析
    y = atoi(cur_kv->name);
    i = y-(wireless_base_data);
    addr = *((uint32_t *)data_buf);
    PT_LOGD("[index:%d]kv_name:%s ,addr: 0x%x", count++, cur_kv->name, addr);
    if(i > 0 && i <max_size && !(addr & 0xff00000f) && addr != 0)
    {
        cache_wireless_id[cache_wireless_count].addr = addr;
        cache_wireless_id[cache_wireless_count].id = i;
        cache_wireless_id[cache_wireless_count].type = type;
        cache_wireless_count++;
        if(cache_wireless_count >= 128+32 -1)
        {
            return;
        }
    }

    free(data_buf);
}

} `

这是读取和添加新键值对的代码: `
pt_db_data_t read_data = { .type = PT_DATA_UINT32 }; int err = pt_fdb_read(9, 1, &read_data); PT_LOGD("err:%d addr:0x%x", err, read_data.val.val_u32); rt_thread_mdelay(2000);

pt_db_data_t write_data = {
.type = PT_DATA_UINT32,
.val.val_u32 = 90,
.len = sizeof(uint32_t)
};

err = pt_fdb_write(31, 1, &write_data);
PT_LOGD("err:%d addr:0x%x", err, write_data.val.val_u32);
rt_thread_mdelay(2000);

err = pt_fdb_read(31, 1, &read_data);
PT_LOGD("err:%d addr:0x%x", err, read_data.val.val_u32);
rt_thread_mdelay(2000);

` 不管我如何添加新的键值对,迭代器都无法遍历到新的键值对,但是如果直接调用读取函数,是直接可以读取到的

armink commented 6 months ago

没啥思路,你先单步分析一下?

YanChao001 commented 4 months ago

无法使用迭代器遍历所有键值对,添加新的键值对之后,也无法通过迭代器遍历到这个键值对,但是如果直接调用get的接口,是可以读取到数据的。 这是遍历的代码: ` void pt_check_wireless_device(uint8_t dbid, uint8_t type, uint8_t max_size) { struct fdb_kv_iterator iterator; fdb_kv_t cur_kv; struct fdb_blob blob; size_t data_size; uint8_t _data_buf; int32_t i = 0; uint32_t y = 0; uint32_t wireless_base_data = dbid_1000; uint32_t addr = 0;

int count = 0;

fdb_kv_iterator_init(&iterator);

while (fdb_kv_iterate(ptr_pt_global_kvdb, &iterator)) 
{
    cur_kv = &(iterator.curr_kv);
    data_size = (size_t) cur_kv->value_len;
    data_buf = (uint8_t *) malloc(data_size);
    if (data_buf == NULL)
    {
        break;
    }
    fdb_blob_read((fdb_db_t) ptr_pt_global_kvdb, fdb_kv_to_blob(cur_kv, fdb_blob_make(&blob, data_buf, data_size)));

    // 无线设备键值解析
    y = atoi(cur_kv->name);
    i = y-(wireless_base_data);
    addr = *((uint32_t *)data_buf);
    PT_LOGD("[index:%d]kv_name:%s ,addr: 0x%x", count++, cur_kv->name, addr);
    if(i > 0 && i <max_size && !(addr & 0xff00000f) && addr != 0)
    {
        cache_wireless_id[cache_wireless_count].addr = addr;
        cache_wireless_id[cache_wireless_count].id = i;
        cache_wireless_id[cache_wireless_count].type = type;
        cache_wireless_count++;
        if(cache_wireless_count >= 128+32 -1)
        {
            return;
        }
    }

    free(data_buf);
}

} `

这是读取和添加新键值对的代码: ` pt_db_data_t read_data = { .type = PT_DATA_UINT32 }; int err = pt_fdb_read(9, 1, &read_data); PT_LOGD("err:%d addr:0x%x", err, read_data.val.val_u32); rt_thread_mdelay(2000);

pt_db_data_t write_data = {
.type = PT_DATA_UINT32,
.val.val_u32 = 90,
.len = sizeof(uint32_t)
};

err = pt_fdb_write(31, 1, &write_data);
PT_LOGD("err:%d addr:0x%x", err, write_data.val.val_u32);
rt_thread_mdelay(2000);

err = pt_fdb_read(31, 1, &read_data);
PT_LOGD("err:%d addr:0x%x", err, read_data.val.val_u32);
rt_thread_mdelay(2000);

` 不管我如何添加新的键值对,迭代器都无法遍历到新的键值对,但是如果直接调用读取函数,是直接可以读取到的

楼主解决了吗,kv_print都打印不出来键值对,size很小的都不显示,但是又能get到