From 4c43901c44f371fa4ce8aea6d3883f83cc27f9d4 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 28 Mar 2022 05:40:54 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbOs.c | 103 ++++++++++++++++++++------------ source/libs/tdb/src/inc/tdbOs.h | 8 +-- 2 files changed, 70 insertions(+), 41 deletions(-) diff --git a/source/libs/tdb/src/db/tdbOs.c b/source/libs/tdb/src/db/tdbOs.c index 210d582b92..e52f788da9 100644 --- a/source/libs/tdb/src/db/tdbOs.c +++ b/source/libs/tdb/src/db/tdbOs.c @@ -15,55 +15,84 @@ #include "tdbInt.h" +#ifndef TDB_FOR_TDENGINE + // tdbOsRead -i64 tdbOsRead(tdb_fd_t fd, void *pBuf, i64 nBytes) { - // TODO - ASSERT(0); - return 0; +i64 tdbOsRead(tdb_fd_t fd, void *pData, i64 nBytes) { + i64 nRead = 0; + i64 iRead = 0; + u8 *pBuf = (u8 *)pData; + + while (nBytes > 0) { + iRead = read(fd, pBuf, nBytes); + if (iRead < 0) { + if (errno == EINTR) { + continue; + } else { + return -1; + } + } else if (iRead == 0) { + break; + } + + nRead += iRead; + pBuf += iRead; + nBytes -= iRead; + } + + return nRead; } // tdbOsPRead -i64 tdbOsPRead(tdb_fd_t fd, void *pBuf, i64 nBytes, i64 offset) { - // TODO - ASSERT(0); - return 0; +i64 tdbOsPRead(tdb_fd_t fd, void *pData, i64 nBytes, i64 offset) { + i64 nRead = 0; + i64 iRead = 0; + i64 iOffset = offset; + u8 *pBuf = (u8 *)pData; + + while (nBytes > 0) { + iRead = pread(fd, pBuf, nBytes, iOffset); + if (iRead < 0) { + if (errno == EINTR) { + continue; + } else { + return -1; + } + } else if (iRead == 0) { + break; + } + + nRead += iRead; + pBuf += iRead; + iOffset += iRead; + nBytes -= iRead; + } + + return nRead; } // tdbOsWrite -i64 taosWriteFile(tdb_fd_t fd, const void *pBuf, i64 nBytes) { - // TODO - ASSERT(0); - return 0; -} +i64 taosWriteFile(tdb_fd_t fd, const void *pData, i64 nBytes) { + i64 nWrite = 0; + i64 iWrite = 0; + u8 *pBuf = (u8 *)pData; + + while (nBytes > 0) { + iWrite = write(fd, pBuf, nBytes); + if (iWrite < 0) { + if (errno == EINTR) { + continue; + } -#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); + return -1; } - nbytes = nbytes - iread; - pBuf = (void *)((u8 *)pBuf + iread); - ioffset += iread; + nWrite += iWrite; + pBuf += iWrite; + nBytes -= iWrite; } - return count; + return nWrite; } -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/inc/tdbOs.h b/source/libs/tdb/src/inc/tdbOs.h index 98a4a190e0..b8ae85ea65 100644 --- a/source/libs/tdb/src/inc/tdbOs.h +++ b/source/libs/tdb/src/inc/tdbOs.h @@ -21,7 +21,7 @@ extern "C" { #endif // TODO: use cmake to control the option -#define TDB_FOR_TDENGINE +// #define TDB_FOR_TDENGINE // For memory ----------------- #ifdef TDB_FOR_TDENGINE @@ -67,9 +67,9 @@ typedef int tdb_fd_t; #define tdbOsOpen open #define tdbOsClose close -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); +i64 tdbOsRead(tdb_fd_t fd, void *pData, i64 nBytes); +i64 tdbOsPRead(tdb_fd_t fd, void *pData, i64 nBytes, i64 offset); +i64 taosWriteFile(tdb_fd_t fd, const void *pData, i64 nBytes); #define tdbOsFSync fsync #define tdbOsLSeek lseek -- GitLab