diff --git a/src/tsdb/inc/tsdbMain.h b/src/tsdb/inc/tsdbMain.h index 2238aa7fc87231a48af404c998c234336c090e20..0af8f787fa4bdec80a099eed6717194e32e71955 100644 --- a/src/tsdb/inc/tsdbMain.h +++ b/src/tsdb/inc/tsdbMain.h @@ -142,7 +142,7 @@ typedef struct { } STsdbFileInfo; typedef struct { - char* fname; + char fname[TSDB_FILENAME_LEN]; int fd; STsdbFileInfo info; @@ -345,7 +345,6 @@ void tsdbFitRetention(STsdbRepo* pRepo); int tsdbUpdateFileHeader(SFile* pFile, uint32_t version); int tsdbEncodeSFileInfo(void** buf, const STsdbFileInfo* pInfo); void* tsdbDecodeSFileInfo(void* buf, STsdbFileInfo* pInfo); -int tsdbCpySFile(SFile* src, SFile* dst); void tsdbRemoveFileGroup(STsdbRepo* pRepo, SFileGroup* pFGroup); // ------------------ tsdbRWHelper.c @@ -389,7 +388,7 @@ int tsdbLoadBlockData(SRWHelper* pHelper, SCompBlock* pCompBlock, SDataCols* t #define TSDB_SUBMIT_MSG_HEAD_SIZE sizeof(SSubmitMsg) char* tsdbGetMetaFileName(char* rootDir); -char* tsdbGetDataFileName(STsdbRepo* pRepo, int fid, int type); +void tsdbGetDataFileName(STsdbRepo* pRepo, int fid, int type, char* fname); int tsdbLockRepo(STsdbRepo* pRepo); int tsdbUnlockRepo(STsdbRepo* pRepo); char* tsdbGetDataDirName(char* rootDir); diff --git a/src/tsdb/src/tsdbFile.c b/src/tsdb/src/tsdbFile.c index 4c5681de9341dedec9cd04413b6643f6a78cfa8b..63b219ebcb7cc57eb8054ef85f63e147b4969603 100644 --- a/src/tsdb/src/tsdbFile.c +++ b/src/tsdb/src/tsdbFile.c @@ -245,8 +245,7 @@ int tsdbCreateFile(SFile *pFile, STsdbRepo *pRepo, int fid, int type) { memset((void *)pFile, 0, sizeof(SFile)); pFile->fd = -1; - pFile->fname = tsdbGetDataFileName(pRepo, fid, type); - if (pFile->fname == NULL) return -1; + tsdbGetDataFileName(pRepo, fid, type, pFile->fname); if (access(pFile->fname, F_OK) == 0) { tsdbError("vgId:%d file %s already exists", REPO_ID(pRepo), fid); @@ -343,18 +342,6 @@ void *tsdbDecodeSFileInfo(void *buf, STsdbFileInfo *pInfo) { return buf; } -int tsdbCpySFile(SFile *src, SFile *dst) { - *dst = *src; - dst->fname = strdup(dst->fname); - - if (dst->fname == NULL) { - terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; - return -1; - } - - return 0; -} - void tsdbRemoveFileGroup(STsdbRepo *pRepo, SFileGroup *pFGroup) { ASSERT(pFGroup != NULL); STsdbFileH *pFileH = pRepo->tsdbFileH; @@ -380,8 +367,7 @@ static int tsdbInitFile(SFile *pFile, STsdbRepo *pRepo, int fid, int type) { uint32_t version; char buf[512] = "\0"; - pFile->fname = tsdbGetDataFileName(pRepo, fid, type); - if (pFile->fname == NULL) return -1; + tsdbGetDataFileName(pRepo, fid, type, pFile->fname); pFile->fd = -1; if (tsdbOpenFile(pFile, O_RDONLY) < 0) goto _err; @@ -410,10 +396,7 @@ _err: return -1; } -static void tsdbDestroyFile(SFile *pFile) { - tsdbCloseFile(pFile); - tfree(pFile->fname); -} +static void tsdbDestroyFile(SFile *pFile) { tsdbCloseFile(pFile); } static int compFGroup(const void *arg1, const void *arg2) { int val1 = ((SFileGroup *)arg1)->fileId; diff --git a/src/tsdb/src/tsdbMain.c b/src/tsdb/src/tsdbMain.c index 6c94a791fca018862081a2b38074a04a7cef1435..91cb21b93a53859fc11629b1d5c4e5ec73eea0fe 100644 --- a/src/tsdb/src/tsdbMain.c +++ b/src/tsdb/src/tsdbMain.c @@ -337,17 +337,8 @@ char *tsdbGetMetaFileName(char *rootDir) { return fname; } -char *tsdbGetDataFileName(STsdbRepo *pRepo, int fid, int type) { - int tlen = strlen(pRepo->rootDir) + strlen(tsdbFileSuffix[type]) + 24; - - char *fname = malloc(tlen); - if (fname == NULL) { - terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; - return NULL; - } - - sprintf(fname, "%s/%s/v%df%d.%s", pRepo->rootDir, TSDB_DATA_DIR_NAME, REPO_ID(pRepo), fid, tsdbFileSuffix[type]); - return fname; +void tsdbGetDataFileName(STsdbRepo *pRepo, int fid, int type, char *fname) { + snprintf(fname, TSDB_FILENAME_LEN, "%s/%s/v%df%d.%s", pRepo->rootDir, TSDB_DATA_DIR_NAME, REPO_ID(pRepo), fid, tsdbFileSuffix[type]); } int tsdbLockRepo(STsdbRepo *pRepo) { diff --git a/src/tsdb/src/tsdbMemTable.c b/src/tsdb/src/tsdbMemTable.c index ce57111125eacb913e606baa3beae6ac9a679b22..72a1b307de3cd92b4cb007accac08e57dba18630 100644 --- a/src/tsdb/src/tsdbMemTable.c +++ b/src/tsdb/src/tsdbMemTable.c @@ -499,6 +499,7 @@ static int tsdbCommitToFile(STsdbRepo *pRepo, int fid, SCommitIter *iters, SRWHe char * dataDir = NULL; STsdbMeta * pMeta = pRepo->tsdbMeta; STsdbCfg * pCfg = &pRepo->config; + STsdbFileH *pFileH = pRepo->tsdbFileH; SFileGroup *pGroup = NULL; TSKEY minKey = 0, maxKey = 0; @@ -588,10 +589,12 @@ static int tsdbCommitToFile(STsdbRepo *pRepo, int fid, SCommitIter *iters, SRWHe } tsdbCloseHelperFile(pHelper, 0); - // TODO: make it atomic with some methods + + pthread_rwlock_wrlock(&(pFileH->fhlock)); pGroup->files[TSDB_FILE_TYPE_HEAD] = pHelper->files.headF; pGroup->files[TSDB_FILE_TYPE_DATA] = pHelper->files.dataF; pGroup->files[TSDB_FILE_TYPE_LAST] = pHelper->files.lastF; + pthread_rwlock_unlock(&(pFileH->fhlock)); return 0; diff --git a/src/tsdb/src/tsdbRWHelper.c b/src/tsdb/src/tsdbRWHelper.c index 08fe69b0ee57f782ab33d6bee3948963f6fd388d..aee82ddc50acf891dbb9e1f9daa25d37ca8e03e3 100644 --- a/src/tsdb/src/tsdbRWHelper.c +++ b/src/tsdb/src/tsdbRWHelper.c @@ -100,13 +100,12 @@ int tsdbSetAndOpenHelperFile(SRWHelper *pHelper, SFileGroup *pGroup) { // Set the files pHelper->files.fid = pGroup->fileId; - tsdbCpySFile(&pHelper->files.headF, &pGroup->files[TSDB_FILE_TYPE_HEAD]); - tsdbCpySFile(&pHelper->files.dataF, &pGroup->files[TSDB_FILE_TYPE_DATA]); - tsdbCpySFile(&pHelper->files.lastF, &pGroup->files[TSDB_FILE_TYPE_LAST]); + pHelper->files.headF = pGroup->files[TSDB_FILE_TYPE_HEAD]; + pHelper->files.dataF = pGroup->files[TSDB_FILE_TYPE_DATA]; + pHelper->files.lastF = pGroup->files[TSDB_FILE_TYPE_LAST]; if (helperType(pHelper) == TSDB_WRITE_HELPER) { - - pHelper->files.nHeadF.fname = tsdbGetDataFileName(pHelper->pRepo, pGroup->fileId, TSDB_FILE_TYPE_NHEAD); - pHelper->files.nLastF.fname = tsdbGetDataFileName(pHelper->pRepo, pGroup->fileId, TSDB_FILE_TYPE_NLAST); + tsdbGetDataFileName(pHelper->pRepo, pGroup->fileId, TSDB_FILE_TYPE_NHEAD, pHelper->files.nHeadF.fname); + tsdbGetDataFileName(pHelper->pRepo, pGroup->fileId, TSDB_FILE_TYPE_NLAST, pHelper->files.nLastF.fname); } // Open the files @@ -1036,15 +1035,10 @@ static int tsdbGetRowsInRange(SDataCols *pDataCols, TSKEY minKey, TSKEY maxKey) static void tsdbResetHelperFileImpl(SRWHelper *pHelper) { memset((void *)&pHelper->files, 0, sizeof(pHelper->files)); pHelper->files.fid = -1; - tfree(pHelper->files.headF.fname); pHelper->files.headF.fd = -1; - tfree(pHelper->files.dataF.fname); pHelper->files.dataF.fd = -1; - tfree(pHelper->files.lastF.fname); pHelper->files.lastF.fd = -1; - tfree(pHelper->files.nHeadF.fname); pHelper->files.nHeadF.fd = -1; - tfree(pHelper->files.nLastF.fname); pHelper->files.nLastF.fd = -1; }