diff --git a/source/libs/tdb/src/db/tdbOs.c b/source/libs/tdb/src/db/tdbOs.c index 210d582b92754fdf78364ec7caea9d87916db1ce..e52f788da92176697745d73201796668f82799ed 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 98a4a190e0917e8bc04e6b366158abb4bb9447b5..b8ae85ea6584f7fdb89c19f210d39418644e4768 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