diff --git a/src/tsdb/inc/tsdbMain.h b/src/tsdb/inc/tsdbMain.h index 678e167351b274c536688377c8c27e8f66b01e44..7936ea423f23e627cc86b0390699b6efb1ae5b53 100644 --- a/src/tsdb/inc/tsdbMain.h +++ b/src/tsdb/inc/tsdbMain.h @@ -475,6 +475,7 @@ int tsdbUpdateFileHeader(SFile* pFile); int tsdbEncodeSFileInfo(void** buf, const STsdbFileInfo* pInfo); void* tsdbDecodeSFileInfo(void* buf, STsdbFileInfo* pInfo); void tsdbRemoveFileGroup(STsdbRepo* pRepo, SFileGroup* pFGroup); +void tsdbGetFileInfoImpl(char* fname, uint32_t* magic, int32_t* size); void tsdbGetFidKeyRange(int daysPerFile, int8_t precision, int fileId, TSKEY *minKey, TSKEY *maxKey); // ------------------ tsdbRWHelper.c diff --git a/src/tsdb/src/tsdbFile.c b/src/tsdb/src/tsdbFile.c index 5ba9a68e37f2f18e25f1c73208eec124c22e35bf..8bc4be6d674b8f38287fd4d3a62525b7ae6c1fa4 100644 --- a/src/tsdb/src/tsdbFile.c +++ b/src/tsdb/src/tsdbFile.c @@ -423,6 +423,35 @@ void tsdbRemoveFileGroup(STsdbRepo *pRepo, SFileGroup *pFGroup) { } } +void tsdbGetFileInfoImpl(char *fname, uint32_t *magic, int32_t *size) { + char buf[TSDB_FILE_HEAD_SIZE] = "\0"; + uint32_t version = 0; + STsdbFileInfo info = {0}; + + int fd = open(fname, O_RDONLY); + if (fd < 0) goto _err; + + if (taosTRead(fd, buf, TSDB_FILE_HEAD_SIZE) < TSDB_FILE_HEAD_SIZE) goto _err; + + if (!taosCheckChecksumWhole((uint8_t *)buf, TSDB_FILE_HEAD_SIZE)) goto _err; + + void *pBuf = (void *)buf; + pBuf = taosDecodeFixedU32(pBuf, &version); + pBuf = tsdbDecodeSFileInfo(pBuf, &info); + + off_t offset = lseek(fd, 0, SEEK_END); + if (offset < 0) goto _err; + close(fd); + + *magic = info.magic; + *size = (int32_t)offset; + +_err: + if (fd >= 0) close(fd); + *magic = TSDB_FILE_INIT_MAGIC; + *size = 0; +} + // ---------------- LOCAL FUNCTIONS ---------------- static int tsdbInitFile(SFile *pFile, STsdbRepo *pRepo, int fid, int type) { uint32_t version; diff --git a/src/tsdb/src/tsdbMain.c b/src/tsdb/src/tsdbMain.c index 628a8bac81ccaf9c4c0c4d5741bafd99f9cf3523..dd647ddd9b320c770dd9e869c9fc0b35a43cc439 100644 --- a/src/tsdb/src/tsdbMain.c +++ b/src/tsdb/src/tsdbMain.c @@ -216,9 +216,9 @@ uint32_t tsdbGetFileInfo(TSDB_REPO_T *repo, char *name, uint32_t *index, uint32_ char *sdup = strdup(pRepo->rootDir); char *prefix = dirname(sdup); int prefixLen = (int)strlen(prefix); - taosTFree(sdup); if (name[0] == 0) { // get the file from index or after, but not larger than eindex + taosTFree(sdup); int fid = (*index) / TSDB_FILE_TYPE_MAX; if (pFileH->nFGroups == 0 || fid > pFileH->pFGroup[pFileH->nFGroups - 1].fileId) { @@ -248,18 +248,19 @@ uint32_t tsdbGetFileInfo(TSDB_REPO_T *repo, char *name, uint32_t *index, uint32_ strcpy(name, fname + prefixLen); } else { // get the named file at the specified index. If not there, return 0 if (*index == TSDB_META_FILE_INDEX) { // get meta file - fname = tsdbGetMetaFileName(pRepo->rootDir); - magic = TSDB_META_FILE_MAGIC(pRepo->tsdbMeta); + fname = malloc(prefixLen + strlen(name) + 2); + sprintf(fname, "%s/%s", prefix, name); + tsdbGetStoreInfo(fname, &magic, size); + taosFree(fname); + taosFree(sdup); + return magic; } else { - int fid = (*index) / TSDB_FILE_TYPE_MAX; - SFileGroup *pFGroup = tsdbSearchFGroup(pFileH, fid, TD_EQ); - if (pFGroup == NULL) { // not found - return 0; - } - - SFile *pFile = &pFGroup->files[(*index) % TSDB_FILE_TYPE_MAX]; - fname = strdup(pFile->fname); - magic = pFile->info.magic; + fname = malloc(prefixLen + strlen(name) + 2); + sprintf(fname, "%s/%s", prefix, name); + tsdbGetFileInfoImpl(fname, &magic, size); + taosFree(fname); + taosFree(sdup); + return magic; } } diff --git a/src/util/inc/tkvstore.h b/src/util/inc/tkvstore.h index a6cc82e75195668333346ce30301e3539ab53887..3b4e8e375717f71d7305aba765ed7bc3685a46d1 100644 --- a/src/util/inc/tkvstore.h +++ b/src/util/inc/tkvstore.h @@ -58,6 +58,7 @@ int tdKVStoreStartCommit(SKVStore *pStore); int tdUpdateKVStoreRecord(SKVStore *pStore, uint64_t uid, void *cont, int contLen); int tdDropKVStoreRecord(SKVStore *pStore, uint64_t uid); int tdKVStoreEndCommit(SKVStore *pStore); +void tsdbGetStoreInfo(char *fname, uint32_t *magic, int32_t *size); #ifdef __cplusplus } diff --git a/src/util/src/tkvstore.c b/src/util/src/tkvstore.c index 499edb4c1a00947e0b2a45418be4b2833025e492..dd0600ec3ce22c7e6586653de0416ec821795f7a 100644 --- a/src/util/src/tkvstore.c +++ b/src/util/src/tkvstore.c @@ -330,6 +330,31 @@ int tdKVStoreEndCommit(SKVStore *pStore) { return 0; } +void tsdbGetStoreInfo(char *fname, uint32_t *magic, int32_t *size) { + char buf[TD_KVSTORE_HEADER_SIZE] = "\0"; + SStoreInfo info = {0}; + + int fd = open(fname, O_RDONLY); + if (fd < 0) goto _err; + + if (taosTRead(fd, buf, TD_KVSTORE_HEADER_SIZE) < TD_KVSTORE_HEADER_SIZE) goto _err; + if (!taosCheckChecksumWhole((uint8_t *)buf, TD_KVSTORE_HEADER_SIZE)) goto _err; + + void *pBuf = (void *)buf; + pBuf = tdDecodeStoreInfo(pBuf, &info); + off_t offset = lseek(fd, 0, SEEK_END); + if (offset < 0) goto _err; + close(fd); + + *magic = info.magic; + *size = (int32_t)offset; + +_err: + if (fd >= 0) close(fd); + *magic = TD_KVSTORE_INIT_MAGIC; + *size = 0; +} + static int tdLoadKVStoreHeader(int fd, char *fname, SStoreInfo *pInfo, uint32_t *version) { char buf[TD_KVSTORE_HEADER_SIZE] = "\0";