From 5f90bae8bbce6eaef907a084d8e60fce979fdd47 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 28 Mar 2022 03:51:02 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbOs.c | 40 ++++++++++++++++++++++--- source/libs/tdb/src/db/tdbPager.c | 2 +- source/libs/tdb/src/db/tdbUtil.c | 50 +------------------------------ source/libs/tdb/src/inc/tdbOs.h | 10 ++++--- source/libs/tdb/src/inc/tdbUtil.h | 16 +++------- 5 files changed, 48 insertions(+), 70 deletions(-) diff --git a/source/libs/tdb/src/db/tdbOs.c b/source/libs/tdb/src/db/tdbOs.c index d8df761069..210d582b92 100644 --- a/source/libs/tdb/src/db/tdbOs.c +++ b/source/libs/tdb/src/db/tdbOs.c @@ -31,7 +31,39 @@ i64 tdbOsPRead(tdb_fd_t fd, void *pBuf, i64 nBytes, i64 offset) { // tdbOsWrite i64 taosWriteFile(tdb_fd_t fd, const void *pBuf, i64 nBytes) { - // TODO - ASSERT(0); - return 0; -} \ No newline at end of file + // TODO + ASSERT(0); + return 0; +} + +#if 0 +int tdbPRead(int fd, void *pData, int count, i64 offset) { + void *pBuf; + int nbytes; + i64 ioffset; + int iread; + + pBuf = pData; + nbytes = count; + ioffset = offset; + while (nbytes > 0) { + iread = pread(fd, pBuf, nbytes, ioffset); + if (iread < 0) { + /* TODO */ + } else if (iread == 0) { + return (count - iread); + } + + nbytes = nbytes - iread; + pBuf = (void *)((u8 *)pBuf + iread); + ioffset += iread; + } + + return count; +} + +int tdbWrite(int fd, void *pData, int count) { + // TODO + return write(fd, pData, count); +} +#endif \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 0abc64d0b0..4fac00d5ad 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -209,7 +209,7 @@ int tdbPagerCommit(SPager *pPager) { tdbOsFSync(pPager->fd); tdbOsClose(pPager->jfd); - remove(pPager->jFileName); + tdbOsRemove(pPager->jFileName); // pPager->jfd = -1; return 0; diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index e7de0a859a..4abc890f94 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -33,28 +33,10 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique) { return 0; } -// int tdbCheckFileAccess(const char *pathname, int mode) { -// int flags = 0; - -// if (mode & TDB_F_OK) { -// flags |= F_OK; -// } - -// if (mode & TDB_R_OK) { -// flags |= R_OK; -// } - -// if (mode & TDB_W_OK) { -// flags |= W_OK; -// } - -// return access(pathname, flags); -// } - int tdbGetFileSize(const char *fname, int pgSize, SPgno *pSize) { struct stat st; int ret; - int64_t file_size = 0; + int64_t file_size = 0; ret = taosStatFile(fname, &file_size, NULL); if (ret != 0) { return -1; @@ -64,34 +46,4 @@ int tdbGetFileSize(const char *fname, int pgSize, SPgno *pSize) { *pSize = file_size / pgSize; return 0; -} - -int tdbPRead(int fd, void *pData, int count, i64 offset) { - void *pBuf; - int nbytes; - i64 ioffset; - int iread; - - pBuf = pData; - nbytes = count; - ioffset = offset; - while (nbytes > 0) { - iread = pread(fd, pBuf, nbytes, ioffset); - if (iread < 0) { - /* TODO */ - } else if (iread == 0) { - return (count - iread); - } - - nbytes = nbytes - iread; - pBuf = (void *)((u8 *)pBuf + iread); - ioffset += iread; - } - - return count; -} - -int tdbWrite(int fd, void *pData, int count) { - // TODO - return write(fd, pData, count); } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbOs.h b/source/libs/tdb/src/inc/tdbOs.h index 851dd69c29..98a4a190e0 100644 --- a/source/libs/tdb/src/inc/tdbOs.h +++ b/source/libs/tdb/src/inc/tdbOs.h @@ -53,6 +53,7 @@ typedef TdFilePtr tdb_fd_t; #define tdbOsWrite taosWriteFile #define tdbOsFSync taosFsyncFile #define tdbOsLSeek taosLSeekFile +#define tdbOsRemove remove /* directory */ #define tdbOsMkdir taosMkDir @@ -70,12 +71,13 @@ i64 tdbOsRead(tdb_fd_t fd, void *pBuf, i64 nBytes); i64 tdbOsPRead(tdb_fd_t fd, void *pBuf, i64 nBytes, i64 offset); i64 taosWriteFile(tdb_fd_t fd, const void *pBuf, i64 nBytes); -#define tdbOsFSync fsync -#define tdbOsLSeek lseek +#define tdbOsFSync fsync +#define tdbOsLSeek lseek +#define tdbOsRemove remove /* directory */ -#define tdbOsMkdir mkdir -#define tdbOsRmdir rmdir +#define tdbOsMkdir mkdir +#define tdbOsRmdir rmdir #endif diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 0633d4e48b..6e6faf9b74 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -30,16 +30,8 @@ extern "C" { int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); -// #define TDB_F_OK 0x1 -// #define TDB_R_OK 0x2 -// #define TDB_W_OK 0x4 -// int tdbCheckFileAccess(const char *pathname, int mode); - int tdbGetFileSize(const char *fname, int pgSize, SPgno *pSize); -int tdbPRead(int fd, void *pData, int count, i64 offset); -int tdbWrite(int fd, void *pData, int count); - #define TDB_REALLOC(PTR, SIZE) \ ({ \ void *nPtr; \ @@ -55,11 +47,11 @@ int tdbWrite(int fd, void *pData, int count); nPtr; \ }) -#define TDB_FREE(PTR) \ - do { \ - if (PTR) { \ +#define TDB_FREE(PTR) \ + do { \ + if (PTR) { \ tdbOsFree((char *)(PTR) - sizeof(int)); \ - } \ + } \ } while (0) static inline void *tdbDefaultMalloc(void *arg, size_t size) { -- GitLab