Skip to content

Commit

Permalink
fix kvdb on libc_file port
Browse files Browse the repository at this point in the history
pikasTech committed Dec 7, 2023
1 parent 0b9ed2b commit fc437ae
Showing 9 changed files with 40 additions and 30 deletions.
12 changes: 6 additions & 6 deletions port/linux/package/pikascript/_flashdb.pyi
Original file line number Diff line number Diff line change
@@ -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:...

Original file line number Diff line number Diff line change
@@ -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;
}

1 change: 1 addition & 0 deletions port/linux/package/pikascript/pikascript-lib/flashdb/fdb.c
Original file line number Diff line number Diff line change
@@ -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 */
17 changes: 9 additions & 8 deletions port/linux/package/pikascript/pikascript-lib/flashdb/fdb_def.h
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 11 additions & 6 deletions port/linux/package/pikascript/pikascript-lib/flashdb/fdb_tsdb.c
Original file line number Diff line number Diff line change
@@ -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;

5 changes: 3 additions & 2 deletions port/linux/test/module-test.cpp
Original file line number Diff line number Diff line change
@@ -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 <pthread.h>
// #include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#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 */
2 changes: 1 addition & 1 deletion port/linux/test/python/flashdb/flashdb_utest.py
Original file line number Diff line number Diff line change
@@ -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')
2 changes: 1 addition & 1 deletion src/PikaVersion.h
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit fc437ae

Please sign in to comment.