Skip to content

Commit

Permalink
fix mem lack, get_blob for default_kvs not work
Browse files Browse the repository at this point in the history
  • Loading branch information
pikasTech committed Dec 6, 2023
1 parent c8b0ffb commit eea7dbd
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 50 deletions.
1 change: 0 additions & 1 deletion examples/flashdb/flashdb_utest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def test_boot_count1(self):
}
#print(default_kv)
kvdb = fdb.kvdb_init("env", DB_PATH, default_kv, None)
return

res = fdb.kv_get_blob(kvdb, "boot_count", len(boot_count_blob))
self.assertIsNotNone(res)
Expand Down
30 changes: 16 additions & 14 deletions package/flashdb/_flashdb_FlashDB.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include "_flashdb_FlashDB.h"
#include <stdio.h>
#include "_flashdb_kvdb_t.h"
#include "flashdb.h"
#include "pikaScript.h"
#include "_flashdb_kvdb_t.h"

//#include "fdb_def.h"
// #include "fdb_def.h"
#define PIKA_USING_FLASHDB1 1
#if PIKA_USING_FLASHDB1
//#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
// #include <pthread.h>
#include "flashdb.h"
#define FDB_LOG_TAG "[main]"

Expand Down Expand Up @@ -40,7 +38,8 @@ static void unlock(fdb_db_t db)
}
*/
static fdb_time_t get_time(void) {
return time(NULL);
// ms to s
return pika_platform_get_tick() / 1000;
}
#endif

Expand Down Expand Up @@ -81,13 +80,13 @@ PikaObj* _flashdb_FlashDB_kv_get_blob(PikaObj* self,
blob.size = size;
uint8_t* buf = (uint8_t*)pikaMalloc(size + 1);
if (!buf) {
printf("alloc fail\n");
pika_platform_printf("alloc fail\n");
return NULL;
}
blob.buf = buf;
size_t len = fdb_kv_get_blob(kvdb, key, &blob);
if (len != size) {
printf("size error\n");
pika_platform_printf("size error\n");
pikaFree(buf, size + 1);
return NULL;
}
Expand Down Expand Up @@ -129,7 +128,7 @@ int _flashdb_FlashDB_kv_set_blob(PikaObj* self,

ArgType argt_blob_in = arg_getType(blob_in);
if (argt_blob_in != ARG_TYPE_BYTES) {
printf("blob must be bytes but got:%d", argt_blob_in);
pika_platform_printf("blob must be bytes but got:%d", argt_blob_in);
}
size_t len = arg_getBytesSize(blob_in);
uint8_t* bytes = (uint8_t*)arg_getBytes(blob_in);
Expand Down Expand Up @@ -187,9 +186,9 @@ int32_t _flashdb_foreach(PikaObj* self_dict,
memcpy(pbytes, bytes, bytes_size);
/*
for (size_t i=0; i < bytes_size; i++) {
printf("%02x", bytes[i]);
pika_platform_printf("%02x", bytes[i]);
}
printf("\n");
pika_platform_printf("\n");
*/
def_kv_table[g_def_kv_table_idx].key = strdup(key);
def_kv_table[g_def_kv_table_idx].value = pbytes;
Expand All @@ -204,7 +203,7 @@ PikaObj* _flashdb_FlashDB_kvdb_init(PikaObj* self,
char* path,
PikaObj* default_kv_in,
Arg* user_data) {
printf("kvdb_init \n");
pika_platform_printf("kvdb_init \n");

fdb_err_t result;
if (!g_kvdb_path_inited) {
Expand All @@ -216,7 +215,7 @@ PikaObj* _flashdb_FlashDB_kvdb_init(PikaObj* self,
/* enable file mode */
fdb_kvdb_control(&g_kvdb, FDB_KVDB_CTRL_SET_FILE_MODE, &file_mode);
/* create database directory */
mkdir(path, 0777);
pika_platform_mkdir(path, 0777);
g_kvdb_path_inited = true;
}
// int len =pikaDict_getSize(default_kv_in);
Expand Down Expand Up @@ -252,7 +251,10 @@ PikaObj* _flashdb_FlashDB_kvdb_init(PikaObj* self,
}

void _flashdb_FlashDB___del__(PikaObj* self) {
args_deinit(_FDBBUFFS);
Args* buffs = _FDBBUFFS;
if (NULL != buffs) {
args_deinit(_FDBBUFFS);
}
}

#undef strudp
2 changes: 1 addition & 1 deletion package/flashdb/fdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void _fdb_deinit(fdb_db_t db) {
}
#else
if (db->cur_file != 0) {
fclose(db->cur_file);
pika_platform_fclose(db->cur_file);
}
#endif /* FDB_USING_FILE_POSIX_MODE */
#endif /* FDB_USING_FILE_MODE */
Expand Down
6 changes: 4 additions & 2 deletions package/flashdb/fdb_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
#ifdef FDB_USING_KVDB
/* Auto update KV to latest default when current KVDB version number is changed.
* @see fdb_kvdb.ver_num */
/* #define FDB_KV_AUTO_UPDATE */
#define FDB_KV_AUTO_UPDATE
#endif

/* using TSDB (Time series database) feature */
#define FDB_USING_TSDB

/* Using file storage mode by POSIX file API, like open/read/write/close */
#define FDB_USING_FILE_POSIX_MODE
// #define FDB_USING_FILE_POSIX_MODE

#define FDB_USING_FILE_LIBC_MODE

/* log print macro. default EF_PRINT macro is printf() */
/* #define FDB_PRINT(...) my_printf(__VA_ARGS__) */
Expand Down
11 changes: 11 additions & 0 deletions package/flashdb/fdb_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
#define _FDB_DEF_H_

#include "PikaObj.h"
#ifndef bool
#define bool pika_bool
#endif

#ifndef true
#define true pika_true
#endif

#ifndef false
#define false pika_false
#endif

#ifdef __cplusplus
extern "C" {
Expand Down
26 changes: 13 additions & 13 deletions package/flashdb/fdb_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,21 @@ static FILE* open_db_file(fdb_db_t db, uint32_t addr, bool clean) {
get_db_file_path(db, addr, path, DB_PATH_MAX);

if (db->cur_file) {
fclose(db->cur_file);
pika_platform_fclose(db->cur_file);
}

if (clean) {
/* clean the old file */
db->cur_file = fopen(path, "wb+");
db->cur_file = pika_platform_fopen(path, "wb+");
if (db->cur_file == NULL) {
FDB_INFO("Error: open (%s) file failed.\n", path);
} else {
fclose(db->cur_file);
pika_platform_fclose(db->cur_file);
}
}

/* open the database file */
db->cur_file = fopen(path, "rb+");
db->cur_file = pika_platform_fopen(path, "rb+");
db->cur_sec = sec_addr;
}

Expand All @@ -170,8 +170,8 @@ fdb_err_t _fdb_file_read(fdb_db_t db, uint32_t addr, void* buf, size_t size) {
FILE* fp = open_db_file(db, addr, false);
if (fp) {
addr = addr % db->sec_size;
if ((fseek(fp, addr, SEEK_SET) != 0) ||
(fread(buf, size, 1, fp) != size))
if ((pika_platform_fseek(fp, addr, SEEK_SET) != 0) ||
(pika_platform_fread(buf, size, 1, fp) != size))
result = FDB_READ_ERR;
} else {
result = FDB_READ_ERR;
Expand All @@ -188,11 +188,11 @@ fdb_err_t _fdb_file_write(fdb_db_t db,
FILE* fp = open_db_file(db, addr, false);
if (fp) {
addr = addr % db->sec_size;
if ((fseek(fp, addr, SEEK_SET) != 0) ||
(fwrite(buf, size, 1, fp) != size))
if ((pika_platform_fseek(fp, addr, SEEK_SET) != 0) ||
(pika_platform_fwrite(buf, size, 1, fp) != size))
result = FDB_READ_ERR;
if (sync) {
fflush(fp);
pika_platform_fflush(fp);
}
} else {
result = FDB_READ_ERR;
Expand All @@ -209,14 +209,14 @@ fdb_err_t _fdb_file_erase(fdb_db_t db, uint32_t addr, size_t size) {
#define BUF_SIZE 32
uint8_t buf[BUF_SIZE];
size_t i;
fseek(fp, 0, SEEK_SET);
pika_platform_fseek(fp, 0, SEEK_SET);
for (i = 0; i * BUF_SIZE < size; i++) {
memset(buf, 0xFF, BUF_SIZE);
fwrite(buf, BUF_SIZE, 1, fp);
pika_platform_fwrite(buf, BUF_SIZE, 1, fp);
}
memset(buf, 0xFF, BUF_SIZE);
fwrite(buf, size - i * BUF_SIZE, 1, fp);
fflush(fp);
pika_platform_fwrite(buf, size - i * BUF_SIZE, 1, fp);
pika_platform_fflush(fp);
} else {
result = FDB_ERASE_ERR;
}
Expand Down
2 changes: 1 addition & 1 deletion package/flashdb/flashdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FDB(_flashdb.FlashDB):

def kv_get_blob(self, kvdb, key, size):
res = super().kv_get_blob(kvdb, key, size)
if type(res) == type([]):
if type(res) == list:
return bytes(res)
return None

Expand Down
3 changes: 2 additions & 1 deletion port/linux/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// "--gtest_filter=vm.run_file_subsrc"
// "--gtest_filter=vm.run_file"
// "--gtest_filter=stddata.encode_decode"
"--gtest_filter=packtool.packfiles_txt"
// "--gtest_filter=packtool.packfiles_txt"
"--gtest_filter=flashdb.*"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand Down
2 changes: 1 addition & 1 deletion port/linux/package/pikascript/flashdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FDB(_flashdb.FlashDB):

def kv_get_blob(self, kvdb, key, size):
res = super().kv_get_blob(kvdb, key, size)
if type(res) == type([]):
if type(res) == list:
return bytes(res)
return None

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "_flashdb_FlashDB.h"
#include <stdio.h>
#include "_flashdb_kvdb_t.h"
#include "flashdb.h"
#include "pikaScript.h"
#include "_flashdb_kvdb_t.h"

//#include "fdb_def.h"
// #include "fdb_def.h"
#define PIKA_USING_FLASHDB1 1
#if PIKA_USING_FLASHDB1
//#include <pthread.h>
// #include <pthread.h>
#include "flashdb.h"
#define FDB_LOG_TAG "[main]"

Expand Down Expand Up @@ -251,7 +251,10 @@ PikaObj* _flashdb_FlashDB_kvdb_init(PikaObj* self,
}

void _flashdb_FlashDB___del__(PikaObj* self) {
args_deinit(_FDBBUFFS);
Args* buffs = _FDBBUFFS;
if (NULL != buffs) {
args_deinit(_FDBBUFFS);
}
}

#undef strudp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifdef FDB_USING_KVDB
/* Auto update KV to latest default when current KVDB version number is changed.
* @see fdb_kvdb.ver_num */
/* #define FDB_KV_AUTO_UPDATE */
#define FDB_KV_AUTO_UPDATE
#endif

/* using TSDB (Time series database) feature */
Expand Down
3 changes: 3 additions & 0 deletions port/linux/test/module-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,9 @@ int test_flashdb(void) {

/* run TSDB sample */
tsdb_sample(&tsdb);

/* close TSDB */
fdb_tsdb_deinit(&tsdb);
}
#endif /* FDB_USING_TSDB */
#else
Expand Down
1 change: 0 additions & 1 deletion port/linux/test/python/flashdb/flashdb_utest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def test_boot_count1(self):
}
#print(default_kv)
kvdb = fdb.kvdb_init("env", DB_PATH, default_kv, None)
return

res = fdb.kv_get_blob(kvdb, "boot_count", len(boot_count_blob))
self.assertIsNotNone(res)
Expand Down
19 changes: 10 additions & 9 deletions src/PikaPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@
#include <io.h>
#endif

#if defined(_WIN32)
#include <dirent.h>
#endif

#if defined(__linux) || PIKA_LINUX_COMPATIBLE
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "unistd.h"
#endif

#if (defined(__linux) || PIKA_LINUX_COMPATIBLE)
#include <dirent.h>
#endif

void pikaFree(void* mem, uint32_t size);
void* pikaMalloc(uint32_t size);
int pika_pvsprintf(char** buff, const char* fmt, va_list args);
Expand Down Expand Up @@ -279,15 +287,8 @@ PIKA_WEAK long pika_platform_ftell(FILE* stream) {
}

PIKA_WEAK char* pika_platform_getcwd(char* buf, size_t size) {
#if defined(__linux)
#if defined(__linux) || defined(_WIN32)
return getcwd(buf, size);
#elif defined(_WIN32)
if (!GetCurrentDirectoryA(size, buf)) {
return NULL;
}
else {
return buf;
}
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_);
#endif
Expand Down Expand Up @@ -478,7 +479,7 @@ PIKA_WEAK char** pika_platform_listdir(const char* path, int* count) {
struct _finddata_t fb;
intptr_t handle = 0;
char dirpath[256] = {0};
char* currentPath = pika_platform_getcwd(dirpath, sizeof dirpath);
char* currentPath = _getcwd(dirpath, 256);
strcat(dirpath, path);
strcat(dirpath, "\\*");

Expand Down
2 changes: 1 addition & 1 deletion src/PikaVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 13
#define PIKA_VERSION_MICRO 0

#define PIKA_EDIT_TIME "2023/12/03 01:48:58"
#define PIKA_EDIT_TIME "2023/12/06 23:57:32"

0 comments on commit eea7dbd

Please sign in to comment.