armink / FlashDB

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

tsdb 当日志写满了之后丢失许多旧 的日志 #189

Open byte-iot opened 1 year ago

byte-iot commented 1 year ago
struct fal_flash_dev nor_flash0 = {
    .name       = FAL_USING_NOR_FLASH_DEV_NAME,
    .addr       = 0,
    .len        = 4 * 1024 * 1024,
    .blk_size   = 4096,
    .ops        = {init, read, write, erase},
    .write_gran = 1
};

/* partition table */
#define FAL_PART_TABLE                                                               \
{                                                                                    \
    /*{FAL_PART_MAGIC_WORD,        "bl",     "stm32_onchip",         0,   64*1024, 0}, */\
    /*{FAL_PART_MAGIC_WORD,       "app",     "stm32_onchip",   64*1024,  704*1024, 0}, */\
    {FAL_PART_MAGIC_WORD, "easyflash", NOR_FLASH_DEV_NAME,         0, 12*1024, 0},/*1Mbyte*/ \
    /*{FAL_PART_MAGIC_WORD,  "download", NOR_FLASH_DEV_NAME, 1024*1024, 1024*1024, 0}, */\
}

static struct fdb_tsdb test_tsdb;
fdb_tsdb_init(&test_tsdb, "test", "easyflash", gd32_get_time, 32, NULL);
tsdb_sample(&test_tsdb);

struct speed_status {
        uint32_t longitude;
        uint32_t latitude;
    int speed;
};

static bool query_logs_cb(fdb_tsl_t tsl, void *arg){
    struct fdb_blob blob;
    struct speed_status status;
    fdb_tsdb_t db = arg;

    fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
    FDB_INFO("[query_cb] queried a TSL: time: %" __PRITS ", Long: %d, Lat: %d, Speed: %d\n", tsl->time, status.longitude, status.latitude, status.speed);

    return false;
}
struct speed_status status;
        struct fdb_blob blob;

        /* append new log to TSDB */
        status.longitude = 123456;
        status.latitude  = 654321;
        status.speed = 85;
        for(int i = 0;i < 50;i++){
            while(timedisplay == 0){;}
            timedisplay = 0;
            fdb_err_t err = fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status)));
            if(err != FDB_NO_ERR){
                FDB_INFO("append the new status err (%d)\n", err);
            }
        }
        //fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status)));
        FDB_INFO("append the new status.Long (%d) status.Lat (%d) status.Speed (%d)\n", status.longitude, status.latitude, status.speed);

        { /* QUERY the TSDB */
        /* query all TSL in TSDB by iterator */
        fdb_tsl_iter(tsdb, query_logs_cb, tsdb);
    }

以上为测试代码 当记录满了之后按理说应该循环覆盖最旧的一条日志,而实际上并没有这样做,而是丢失了许多旧的记录,大概丢失一百多条记录

请指引我应该怎么做,谢谢!

turelon commented 5 months ago

看代码循环覆盖是以扇区为单位的,当记录满了之后会格式化最旧的扇区,因此会丢失一个扇区的记录