From f0c1950b63e5ce7b817183baa9b9bb8aeaa79ba9 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 7 Sep 2022 19:39:44 +0800 Subject: [PATCH] refactor code --- source/dnode/vnode/src/meta/metaTable.c | 14 ++-- source/libs/index/src/index.c | 14 ++-- source/libs/index/src/indexFst.c | 7 +- source/libs/index/src/indexFstFile.c | 10 +-- source/libs/index/src/indexFstRegister.c | 2 +- source/libs/index/src/indexFstSparse.c | 10 +-- source/libs/index/src/indexTfile.c | 3 +- source/libs/index/src/indexUtil.c | 2 +- source/libs/index/test/indexBench.cc | 85 ++++++++++++++++++++++++ 9 files changed, 118 insertions(+), 29 deletions(-) create mode 100644 source/libs/index/test/indexBench.cc diff --git a/source/dnode/vnode/src/meta/metaTable.c b/source/dnode/vnode/src/meta/metaTable.c index 2c11b9bf0f..92cf90d328 100644 --- a/source/dnode/vnode/src/meta/metaTable.c +++ b/source/dnode/vnode/src/meta/metaTable.c @@ -416,20 +416,22 @@ int metaCreateTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMe me.ctbEntry.pTags = pReq->ctb.pTag; #ifdef TAG_FILTER_DEBUG - SArray* pTagVals = NULL; - int32_t code = tTagToValArray((STag*)pReq->ctb.pTag, &pTagVals); + SArray *pTagVals = NULL; + int32_t code = tTagToValArray((STag *)pReq->ctb.pTag, &pTagVals); for (int i = 0; i < taosArrayGetSize(pTagVals); i++) { - STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, i); + STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i); if (IS_VAR_DATA_TYPE(pTagVal->type)) { - char* buf = taosMemoryCalloc(pTagVal->nData + 1, 1); + char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1); memcpy(buf, pTagVal->pData, pTagVal->nData); - metaDebug("metaTag table:%s varchar index:%d cid:%d type:%d value:%s", pReq->name, i, pTagVal->cid, pTagVal->type, buf); + metaDebug("metaTag table:%s varchar index:%d cid:%d type:%d value:%s", pReq->name, i, pTagVal->cid, + pTagVal->type, buf); taosMemoryFree(buf); } else { double val = 0; GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64); - metaDebug("metaTag table:%s number index:%d cid:%d type:%d value:%f", pReq->name, i, pTagVal->cid, pTagVal->type, val); + metaDebug("metaTag table:%s number index:%d cid:%d type:%d value:%f", pReq->name, i, pTagVal->cid, + pTagVal->type, val); } } #endif diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index d9a6b80f3d..1f6d793417 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -303,7 +303,7 @@ SIndexTerm* indexTermCreate(int64_t suid, SIndexOperOnColumn oper, uint8_t colTy buf = strndup(INDEX_DATA_NULL_STR, (int32_t)strlen(INDEX_DATA_NULL_STR)); len = (int32_t)strlen(INDEX_DATA_NULL_STR); } else { - const char* emptyStr = " "; + static const char* emptyStr = " "; buf = strndup(emptyStr, (int32_t)strlen(emptyStr)); len = (int32_t)strlen(emptyStr); } @@ -585,6 +585,12 @@ int idxFlushCacheToTFile(SIndex* sIdx, void* cache, bool quit) { idxTRsltDestroy(tr); int ret = idxGenTFile(sIdx, pCache, result); + if (ret != 0) { + indexError("failed to merge, time cost: %" PRId64 "ms", cost / 1000); + } else { + int64_t cost = taosGetTimestampUs() - st; + indexInfo("success to merge , time cost: %" PRId64 "ms", cost / 1000); + } idxDestroyFinalRslt(result); idxCacheDestroyImm(pCache); @@ -595,12 +601,6 @@ int idxFlushCacheToTFile(SIndex* sIdx, void* cache, bool quit) { tfileReaderUnRef(pReader); idxCacheUnRef(pCache); - int64_t cost = taosGetTimestampUs() - st; - if (ret != 0) { - indexError("failed to merge, time cost: %" PRId64 "ms", cost / 1000); - } else { - indexInfo("success to merge , time cost: %" PRId64 "ms", cost / 1000); - } atomic_store_32(&pCache->merging, 0); if (quit) { idxPost(sIdx); diff --git a/source/libs/index/src/indexFst.c b/source/libs/index/src/indexFst.c index 15152cef55..2aa8345e03 100644 --- a/source/libs/index/src/indexFst.c +++ b/source/libs/index/src/indexFst.c @@ -19,11 +19,12 @@ #include "tchecksum.h" #include "tcoding.h" -static void fstPackDeltaIn(IdxFstFile* wrt, CompiledAddr nodeAddr, CompiledAddr transAddr, uint8_t nBytes) { +static FORCE_INLINE void fstPackDeltaIn(IdxFstFile* wrt, CompiledAddr nodeAddr, CompiledAddr transAddr, + uint8_t nBytes) { CompiledAddr deltaAddr = (transAddr == EMPTY_ADDRESS) ? EMPTY_ADDRESS : nodeAddr - transAddr; idxFilePackUintIn(wrt, deltaAddr, nBytes); } -static uint8_t fstPackDetla(IdxFstFile* wrt, CompiledAddr nodeAddr, CompiledAddr transAddr) { +static FORCE_INLINE uint8_t fstPackDetla(IdxFstFile* wrt, CompiledAddr nodeAddr, CompiledAddr transAddr) { uint8_t nBytes = packDeltaSize(nodeAddr, transAddr); fstPackDeltaIn(wrt, nodeAddr, transAddr, nBytes); return nBytes; @@ -39,7 +40,7 @@ FstUnFinishedNodes* fstUnFinishedNodesCreate() { fstUnFinishedNodesPushEmpty(nodes, false); return nodes; } -static void unFinishedNodeDestroyElem(void* elem) { +static void FORCE_INLINE unFinishedNodeDestroyElem(void* elem) { FstBuilderNodeUnfinished* b = (FstBuilderNodeUnfinished*)elem; fstBuilderNodeDestroy(b->node); taosMemoryFree(b->last); diff --git a/source/libs/index/src/indexFstFile.c b/source/libs/index/src/indexFstFile.c index e6d1edfeda..2a33ddd477 100644 --- a/source/libs/index/src/indexFstFile.c +++ b/source/libs/index/src/indexFstFile.c @@ -30,14 +30,14 @@ typedef struct { static void deleteDataBlockFromLRU(const void* key, size_t keyLen, void* value) { taosMemoryFree(value); } -static void idxGenLRUKey(char* buf, const char* path, int32_t blockId) { +static FORCE_INLINE void idxGenLRUKey(char* buf, const char* path, int32_t blockId) { char* p = buf; SERIALIZE_STR_VAR_TO_BUF(p, path, strlen(path)); SERIALIZE_VAR_TO_BUF(p, '_', char); idxInt2str(blockId, p, 0); return; } -static int idxFileCtxDoWrite(IFileCtx* ctx, uint8_t* buf, int len) { +static FORCE_INLINE int idxFileCtxDoWrite(IFileCtx* ctx, uint8_t* buf, int len) { if (ctx->type == TFILE) { int nwr = taosWriteFile(ctx->file.pFile, buf, len); assert(nwr == len); @@ -47,7 +47,7 @@ static int idxFileCtxDoWrite(IFileCtx* ctx, uint8_t* buf, int len) { ctx->offset += len; return len; } -static int idxFileCtxDoRead(IFileCtx* ctx, uint8_t* buf, int len) { +static FORCE_INLINE int idxFileCtxDoRead(IFileCtx* ctx, uint8_t* buf, int len) { int nRead = 0; if (ctx->type == TFILE) { #ifdef USE_MMAP @@ -111,7 +111,7 @@ static int idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t of } while (len > 0); return total; } -static int idxFileCtxGetSize(IFileCtx* ctx) { +static FORCE_INLINE int idxFileCtxGetSize(IFileCtx* ctx) { if (ctx->type == TFILE) { int64_t file_size = 0; taosStatFile(ctx->file.buf, &file_size, NULL); @@ -119,7 +119,7 @@ static int idxFileCtxGetSize(IFileCtx* ctx) { } return 0; } -static int idxFileCtxDoFlush(IFileCtx* ctx) { +static FORCE_INLINE int idxFileCtxDoFlush(IFileCtx* ctx) { if (ctx->type == TFILE) { taosFsyncFile(ctx->file.pFile); } else { diff --git a/source/libs/index/src/indexFstRegister.c b/source/libs/index/src/indexFstRegister.c index 34efee0d0d..e0abcadc78 100644 --- a/source/libs/index/src/indexFstRegister.c +++ b/source/libs/index/src/indexFstRegister.c @@ -16,7 +16,7 @@ #include "indexFstRegistry.h" #include "os.h" -uint64_t fstRegistryHash(FstRegistry* registry, FstBuilderNode* bNode) { +static FORCE_INLINE uint64_t fstRegistryHash(FstRegistry* registry, FstBuilderNode* bNode) { // TODO(yihaoDeng): refactor later const uint64_t FNV_PRIME = 1099511628211; uint64_t h = 14695981039346656037u; diff --git a/source/libs/index/src/indexFstSparse.c b/source/libs/index/src/indexFstSparse.c index ebc0cb3637..8746b04eab 100644 --- a/source/libs/index/src/indexFstSparse.c +++ b/source/libs/index/src/indexFstSparse.c @@ -15,7 +15,7 @@ #include "indexFstSparse.h" -static void sparSetUtil(int32_t *buf, int32_t cap) { +static FORCE_INLINE void sparSetInitBuf(int32_t *buf, int32_t cap) { for (int32_t i = 0; i < cap; i++) { buf[i] = -1; } @@ -28,8 +28,8 @@ FstSparseSet *sparSetCreate(int32_t sz) { ss->dense = (int32_t *)taosMemoryMalloc(sz * sizeof(int32_t)); ss->sparse = (int32_t *)taosMemoryMalloc(sz * sizeof(int32_t)); - sparSetUtil(ss->dense, sz); - sparSetUtil(ss->sparse, sz); + sparSetInitBuf(ss->dense, sz); + sparSetInitBuf(ss->sparse, sz); ss->cap = sz; @@ -90,7 +90,7 @@ void sparSetClear(FstSparseSet *ss) { if (ss == NULL) { return; } - sparSetUtil(ss->dense, ss->cap); - sparSetUtil(ss->sparse, ss->cap); + sparSetInitBuf(ss->dense, ss->cap); + sparSetInitBuf(ss->sparse, ss->cap); ss->size = 0; } diff --git a/source/libs/index/src/indexTfile.c b/source/libs/index/src/indexTfile.c index 48a0c631cf..c3be0ea6f5 100644 --- a/source/libs/index/src/indexTfile.c +++ b/source/libs/index/src/indexTfile.c @@ -1034,7 +1034,8 @@ static void tfileGenFileName(char* filename, uint64_t suid, const char* col, int sprintf(filename, "%" PRIu64 "-%s-%" PRId64 ".tindex", suid, col, version); return; } -static void tfileGenFileFullName(char* fullname, const char* path, uint64_t suid, const char* col, int64_t version) { +static void FORCE_INLINE tfileGenFileFullName(char* fullname, const char* path, uint64_t suid, const char* col, + int64_t version) { char filename[128] = {0}; tfileGenFileName(filename, suid, col, version); sprintf(fullname, "%s/%s", path, filename); diff --git a/source/libs/index/src/indexUtil.c b/source/libs/index/src/indexUtil.c index 3d083c1817..cdfb79016f 100644 --- a/source/libs/index/src/indexUtil.c +++ b/source/libs/index/src/indexUtil.c @@ -21,7 +21,7 @@ typedef struct MergeIndex { int len; } MergeIndex; -static int iBinarySearch(SArray *arr, int s, int e, uint64_t k) { +static FORCE_INLINE int iBinarySearch(SArray *arr, int s, int e, uint64_t k) { uint64_t v; int32_t m; while (s <= e) { diff --git a/source/libs/index/test/indexBench.cc b/source/libs/index/test/indexBench.cc new file mode 100644 index 0000000000..cce582e2f0 --- /dev/null +++ b/source/libs/index/test/indexBench.cc @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 * or later ("AGPL"), as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include +#include +#include "index.h" +using namespace std; + +static void initLog() { + const char *defaultLogFileNamePrefix = "taoslog"; + const int32_t maxLogFileNum = 10; + + tsAsyncLog = 0; + idxDebugFlag = 143; + strcpy(tsLogDir, logDir.c_str()); + taosRemoveDir(tsLogDir); + taosMkDir(tsLogDir); + + if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { + printf("failed to open log file in directory:%s\n", tsLogDir); + } +} + +struct WriteBatch { + SIndexMultiTerm *terms; +}; +class Idx { + public: + Idx(int _cacheSize = 1024 * 1024 * 4, const char *_path = "tindex") { + opts.cacheSize = _cacheSize; + path = TD_TMP_DIR_PATH _path; + } + int SetUp(bool remove) { + initLog(); + + if (remove) taosRemoveDir(path); + + int ret = indexJsonOpen(&opts, path, &index); + return ret; + } + int Write(WriteBatch *batch) { + // write batch + return 0; + } + int Read(const char *json, void *key, int64_t *id) { + // read batch + return 0; + } + + void TearDown() { indexJsonClose(index); } + + std::string path; + + SIndexOpts opts; + SIndex *index; +}; + +int BenchWrite(Idx *idx, int nCol, int Limit) { return 0; } + +int BenchRead(Idx *idx) { return 0; } + +int main() { + Idx *idx = new Idx; + if (idx->SetUp(true) != 0) { + std::cout << "failed to setup index" << std::endl; + return 0; + } else { + std::cout << "succ to setup index" << std::endl; + } + BenchWrite(idx, 10000, 100); +} -- GitLab