Skip to content

Commit

Permalink
fix port for flashdb pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
pikasTech committed Dec 6, 2023
1 parent fc63ab8 commit c8b0ffb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#define PIKA_USING_FLASHDB1 1
#if PIKA_USING_FLASHDB1
//#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.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
2 changes: 1 addition & 1 deletion port/linux/package/pikascript/pikascript-lib/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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#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 port/linux/package/pikascript/pikascript-lib/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 port/linux/package/pikascript/pikascript-lib/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

0 comments on commit c8b0ffb

Please sign in to comment.