From fc437ae5183693c82c8bf7343aae94478b9509b0 Mon Sep 17 00:00:00 2001 From: lyon1998 Date: Thu, 7 Dec 2023 18:53:30 +0800 Subject: [PATCH] fix kvdb on libc_file port --- port/linux/package/pikascript/_flashdb.pyi | 12 ++++++------ .../pikascript-lib/flashdb/_flashdb_FlashDB.c | 11 ++++++----- .../pikascript/pikascript-lib/flashdb/fdb.c | 1 + .../pikascript/pikascript-lib/flashdb/fdb_def.h | 17 +++++++++-------- .../pikascript-lib/flashdb/fdb_file.c | 3 ++- .../pikascript-lib/flashdb/fdb_tsdb.c | 17 +++++++++++------ port/linux/test/module-test.cpp | 5 +++-- port/linux/test/python/flashdb/flashdb_utest.py | 2 +- src/PikaVersion.h | 2 +- 9 files changed, 40 insertions(+), 30 deletions(-) diff --git a/port/linux/package/pikascript/_flashdb.pyi b/port/linux/package/pikascript/_flashdb.pyi index 2db20c45b..352a6526a 100644 --- a/port/linux/package/pikascript/_flashdb.pyi +++ b/port/linux/package/pikascript/_flashdb.pyi @@ -17,18 +17,18 @@ class FlashDB: # def blob_read(db:any, blob:any)->any:... - def kvdb_init(name: str, path: str, default_kv: dict, + def kvdb_init(self, name: str, path: str, default_kv: dict, user_data: any) -> object: ... - def kvdb_control(kvdb: any, cmd: int, arg: any) -> int: ... + def kvdb_control(self, cmd: int, arg: any) -> int: ... - def kvdb_deinit(kvdb: any): ... + def kvdb_deinit(self): ... - def kv_set_blob(kvdb: object, key: str, blob: any) -> int: ... + def kv_set_blob(self, kvdb: object, key: str, blob: any) -> int: ... # def kv_set(kvdb:any, key:str, value:str)->int:... - def kv_get_blob(kvdb: object, key: str, size: int) -> list: ... + def kv_get_blob(self, kvdb: object, key: str, size: int) -> list: ... # def kv_get_obj(kvdb:any, key:str, kv:any)->any:... @@ -38,7 +38,7 @@ class FlashDB: # def kv_set_default(kvdb:any)->int:... - def kv_print(kvdb: object): ... + def kv_print(self, kvdb: object): ... # def kv_to_blob(kv:any, blob:any)->any:... diff --git a/port/linux/package/pikascript/pikascript-lib/flashdb/_flashdb_FlashDB.c b/port/linux/package/pikascript/pikascript-lib/flashdb/_flashdb_FlashDB.c index 80ff80d92..8b811409a 100644 --- a/port/linux/package/pikascript/pikascript-lib/flashdb/_flashdb_FlashDB.c +++ b/port/linux/package/pikascript/pikascript-lib/flashdb/_flashdb_FlashDB.c @@ -150,11 +150,14 @@ Arg* _flashdb_FlashDB_kv_to_blob(PikaObj* self, Arg* kv, Arg* blob) { return NULL; } -int _flashdb_FlashDB_kvdb_control(PikaObj* self, Arg* kvdb, int cmd, Arg* arg) { +int _flashdb_FlashDB_kvdb_control(PikaObj* self, int cmd, Arg* arg) { return 0; } -void _flashdb_FlashDB_kvdb_deinit(PikaObj* self, Arg* kvdb) {} +void _flashdb_FlashDB_kvdb_deinit(PikaObj* self) { + fdb_kvdb_deinit(&g_kvdb); +} + struct _flashdb_foreach_context { struct fdb_default_kv_node* def_kv_table; PikaObj* self; @@ -244,9 +247,7 @@ PikaObj* _flashdb_FlashDB_kvdb_init(PikaObj* self, } PikaObj* kvdb_obj = newNormalObj(New__flashdb_kvdb_t); - args_setStruct(kvdb_obj->list, "kvdb_struct", g_kvdb); - FDB_KVDB* pkvdb = args_getStruct(kvdb_obj->list, "kvdb_struct"); - obj_setPtr(kvdb_obj, "kvdb", pkvdb); + obj_setPtr(kvdb_obj, "kvdb", &g_kvdb); return kvdb_obj; } diff --git a/port/linux/package/pikascript/pikascript-lib/flashdb/fdb.c b/port/linux/package/pikascript/pikascript-lib/flashdb/fdb.c index 0b9b3caa0..eb9459271 100644 --- a/port/linux/package/pikascript/pikascript-lib/flashdb/fdb.c +++ b/port/linux/package/pikascript/pikascript-lib/flashdb/fdb.c @@ -142,6 +142,7 @@ void _fdb_deinit(fdb_db_t db) { #else if (db->cur_file != 0) { pika_platform_fclose(db->cur_file); + db->cur_file = 0; } #endif /* FDB_USING_FILE_POSIX_MODE */ #endif /* FDB_USING_FILE_MODE */ diff --git a/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_def.h b/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_def.h index ab2c15a57..353afa64b 100644 --- a/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_def.h +++ b/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_def.h @@ -181,7 +181,7 @@ typedef enum fdb_tsl_status fdb_tsl_status_t; /* key-value node object */ struct fdb_kv { fdb_kv_status_t status; /**< node status, @see fdb_kv_status_t */ - pika_bool crc_is_ok; /**< node CRC32 check is OK */ + pika_bool crc_is_ok; /**< node CRC32 check is OK */ uint8_t name_len; /**< name length */ uint32_t magic; /**< magic word(`K`, `V`, `4`, `0`) */ // uint32_t len; /**< node total length @@ -315,10 +315,11 @@ struct fdb_db { uint32_t sec_size; /**< flash section size. It's a multiple of block size */ uint32_t max_size; /**< database max size. It's a multiple of section size */ - uint32_t oldest_addr; /**< the oldest sector start address */ - pika_bool init_ok; /**< initialized successfully */ - pika_bool file_mode; /**< is file mode, default is pika_false */ - pika_bool not_formatable; /**< is can NOT be formated mode, default is pika_false */ + uint32_t oldest_addr; /**< the oldest sector start address */ + pika_bool init_ok; /**< initialized successfully */ + pika_bool file_mode; /**< is file mode, default is pika_false */ + pika_bool not_formatable; /**< is can NOT be formated mode, default is + pika_false */ #ifdef FDB_USING_FILE_MODE #if defined(FDB_USING_FILE_POSIX_MODE) int cur_file; /**< current file object */ @@ -337,7 +338,7 @@ struct fdb_db { struct fdb_kvdb { struct fdb_db parent; /**< inherit from fdb_db */ struct fdb_default_kv default_kvs; /**< default KV */ - pika_bool gc_request; /**< request a GC check */ + pika_bool gc_request; /**< request a GC check */ pika_bool in_recovery_check; /**< is in recovery check status when first reboot */ struct fdb_kv cur_kv; @@ -367,8 +368,8 @@ struct fdb_tsdb { fdb_time_t last_time; /**< last TSL timestamp */ fdb_get_time get_time; /**< the current timestamp get function */ size_t max_len; /**< the maximum length of each log */ - pika_bool rollover; /**< the oldest data will rollover by newest data, default is - pika_true */ + pika_bool rollover; /**< the oldest data will rollover by newest data, + default is pika_true */ void* user_data; }; diff --git a/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_file.c b/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_file.c index bdc92350d..125780d66 100644 --- a/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_file.c +++ b/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_file.c @@ -145,6 +145,7 @@ static FILE* open_db_file(fdb_db_t db, uint32_t addr, pika_bool clean) { if (db->cur_file) { pika_platform_fclose(db->cur_file); + db->cur_file = 0; } if (clean) { @@ -154,6 +155,7 @@ static FILE* open_db_file(fdb_db_t db, uint32_t addr, pika_bool clean) { FDB_INFO("Error: open (%s) file failed.\n", path); } else { pika_platform_fclose(db->cur_file); + db->cur_file = 0; } } @@ -179,7 +181,6 @@ fdb_err_t _fdb_file_read(fdb_db_t db, uint32_t addr, void* buf, size_t size) { printf("Error: read (%s) file failed.\n", db->name); result = FDB_READ_ERR; } - result = FDB_READ_ERR; } else { result = FDB_READ_ERR; } diff --git a/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_tsdb.c b/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_tsdb.c index 08dd50586..469c89151 100644 --- a/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_tsdb.c +++ b/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_tsdb.c @@ -319,8 +319,8 @@ static void sector_iterator(fdb_tsdb_t db, void* arg1, void* arg2, pika_bool (*callback)(tsdb_sec_info_t sector, - void* arg1, - void* arg2), + void* arg1, + void* arg2), pika_bool traversal) { uint32_t sec_addr = sector->addr, traversed_len = 0; @@ -382,7 +382,8 @@ static fdb_err_t update_sec_status(fdb_tsdb_t db, end_status, FDB_TSL_STATUS_NUM, FDB_TSL_PRE_WRITE, pika_false); FLASH_WRITE(db, cur_sec_addr + SECTOR_END0_TIME_OFFSET, - (uint32_t*)&db->last_time, sizeof(fdb_time_t), pika_false); + (uint32_t*)&db->last_time, sizeof(fdb_time_t), + pika_false); FLASH_WRITE(db, cur_sec_addr + SECTOR_END0_IDX_OFFSET, &end_index, sizeof(end_index), pika_false); _FDB_WRITE_STATUS(db, cur_sec_addr + SECTOR_END0_STATUS_OFFSET, @@ -393,7 +394,8 @@ static fdb_err_t update_sec_status(fdb_tsdb_t db, end_status, FDB_TSL_STATUS_NUM, FDB_TSL_PRE_WRITE, pika_false); FLASH_WRITE(db, cur_sec_addr + SECTOR_END1_TIME_OFFSET, - (uint32_t*)&db->last_time, sizeof(fdb_time_t), pika_false); + (uint32_t*)&db->last_time, sizeof(fdb_time_t), + pika_false); FLASH_WRITE(db, cur_sec_addr + SECTOR_END1_IDX_OFFSET, &end_index, sizeof(end_index), pika_false); _FDB_WRITE_STATUS(db, cur_sec_addr + SECTOR_END1_STATUS_OFFSET, @@ -726,7 +728,8 @@ void fdb_tsl_iter_by_time(fdb_tsdb_t db, if ((from <= to && tsl.time >= from && tsl.time <= to) || (from > to && tsl.time <= from && tsl.time >= to)) { - /* iterator is interrupted when callback return pika_true + /* iterator is interrupted when callback return + * pika_true */ if (cb(&tsl, cb_arg)) { goto __exit; @@ -822,7 +825,9 @@ fdb_blob_t fdb_tsl_to_blob(fdb_tsl_t tsl, fdb_blob_t blob) { return blob; } -static pika_bool check_sec_hdr_cb(tsdb_sec_info_t sector, void* arg1, void* arg2) { +static pika_bool check_sec_hdr_cb(tsdb_sec_info_t sector, + void* arg1, + void* arg2) { struct check_sec_hdr_cb_args* arg = arg1; fdb_tsdb_t db = arg->db; diff --git a/port/linux/test/module-test.cpp b/port/linux/test/module-test.cpp index 7bcc5ebe6..1c4ce9a9e 100644 --- a/port/linux/test/module-test.cpp +++ b/port/linux/test/module-test.cpp @@ -637,7 +637,7 @@ TEST_RUN_SINGLE_FILE_PASS(struct, unpack, "test/python/struct/unpack.py") extern "C" { #define PIKA_USING_FLASHDB 1 #if PIKA_USING_FLASHDB -//#include +// #include #include #include #include "flashdb.h" @@ -731,10 +731,11 @@ int test_flashdb(void) { kvdb_type_string_sample(&kvdb); /* run blob KV samples */ kvdb_type_blob_sample(&kvdb); + fdb_kvdb_deinit(&kvdb); } #endif /* FDB_USING_KVDB */ -//#define FDB_USING_TSDB 1 +// #define FDB_USING_TSDB 1 #ifdef FDB_USING_TSDB { /* TSDB Sample */ /* set the lock and unlock function if you want */ diff --git a/port/linux/test/python/flashdb/flashdb_utest.py b/port/linux/test/python/flashdb/flashdb_utest.py index 6b13c52e6..f44f794ed 100644 --- a/port/linux/test/python/flashdb/flashdb_utest.py +++ b/port/linux/test/python/flashdb/flashdb_utest.py @@ -38,8 +38,8 @@ def test_boot_count1(self): self.assertIsNotNone(res) new_boot_count = struct.unpack("i", res)[0] - self.assertEqual(new_boot_count, boot_count) + fdb.kvdb_deinit() def xtest_boot_count2(self): print('test boot_count increment 2') diff --git a/src/PikaVersion.h b/src/PikaVersion.h index e744b9a36..1b4f6954b 100644 --- a/src/PikaVersion.h +++ b/src/PikaVersion.h @@ -2,4 +2,4 @@ #define PIKA_VERSION_MINOR 13 #define PIKA_VERSION_MICRO 0 -#define PIKA_EDIT_TIME "2023/12/06 23:57:32" +#define PIKA_EDIT_TIME "2023/12/07 18:53:23"