From a04f78c7602393d1d7b20c294a5d4375582f177a Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 31 Dec 2021 09:41:04 +0000 Subject: [PATCH 01/30] mroe --- source/util/src/encode.c | 132 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 source/util/src/encode.c diff --git a/source/util/src/encode.c b/source/util/src/encode.c new file mode 100644 index 0000000000..9e648bb281 --- /dev/null +++ b/source/util/src/encode.c @@ -0,0 +1,132 @@ +/* + * 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 "encode.h" +#include "freelist.h" + +#define CODER_NODE_FIELDS \ + uint8_t* data; \ + int32_t size; \ + int32_t pos; + +struct SCoderNode { + TD_SLIST_NODE(SCoderNode); + CODER_NODE_FIELDS +}; + +typedef struct { + td_endian_t endian; + SFreeList fl; + CODER_NODE_FIELDS + TD_SLIST(SCoderNode) stack; +} SCoder; + +bool tDecodeIsEnd(SCoder* pCoder) { return (pCoder->size == pCoder->pos); } + +void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size) { + pCoder->endian = endian; + pCoder->data = data; + pCoder->size = size; + pCoder->pos = 0; + tFreeListInit(&(pCoder->fl)); + TD_SLIST_INIT(&(pCoder->stack)); +} + +void tCoderClear(SCoder* pCoder) { + tFreeListClear(&(pCoder->fl)); + struct SCoderNode* pNode; + for (;;) { + pNode = TD_SLIST_HEAD(&(pCoder->stack)); + if (pNode == NULL) break; + TD_SLIST_POP(&(pCoder->stack)); + free(pNode); + } +} + +int tStartEncode(SCoder* pCoder) { + if (pCoder->data) { + struct SCoderNode* pNode = malloc(sizeof(*pNode)); + if (pNode == NULL) return -1; + + pNode->data = pCoder->data; + pNode->pos = pCoder->pos; + pNode->size = pCoder->size; + + pCoder->data = pNode->data + sizeof(int32_t); + pCoder->pos = 0; + pCoder->size = pNode->size - pNode->pos - sizeof(int32_t); + + TD_SLIST_PUSH(&(pCoder->stack), pNode); + } else { + pCoder->pos += sizeof(int32_t); + } + return 0; +} + +void tEndEncode(SCoder* pCoder) { + struct SCoderNode* pNode; + + if (pCoder->data) { + pNode = TD_SLIST_HEAD(&(pCoder->stack)); + ASSERT(pNode); + TD_SLIST_POP(&(pCoder->stack)); + + // TODO: tEncodeI32(pNode, pCoder->pos); + + pCoder->data = pNode->data; + pCoder->size = pNode->size; + pCoder->pos = pNode->pos + pCoder->pos; + + free(pNode); + } +} + +int tStartDecode(SCoder* pCoder) { + int32_t size; + struct SCoderNode* pNode; + + pNode = malloc(sizeof(*pNode)); + if (pNode == NULL) return -1; + + // TODO: tDecodeI32(pCoder, &size); + + pNode->data = pCoder->data; + pNode->pos = pCoder->pos; + pNode->size = pCoder->size; + + pCoder->data = pCoder->data; + pCoder->size = size; + pCoder->pos = 0; + + TD_SLIST_PUSH(&(pCoder->stack), pNode); + + return 0; +} + +void tEndDecode(SCoder* pCoder) { + ASSERT(tDecodeIsEnd(pCoder)); + + struct SCoderNode* pNode; + + pNode = TD_SLIST_HEAD(&(pCoder->stack)); + ASSERT(pNode); + TD_SLIST_POP(&(pCoder->stack)); + + pCoder->data = pNode->data; + pCoder->size = pNode->size; + pCoder->pos = pCoder->pos + pNode->pos; + + free(pNode); +} From 9920b43be5997caadbd15c9745da7b265a5b59ed Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 31 Dec 2021 18:06:13 +0800 Subject: [PATCH 02/30] fix fst bug --- source/libs/index/src/index.c | 2 + source/libs/index/src/index_cache.c | 5 +- source/libs/index/src/index_fst.c | 4 +- source/libs/index/src/index_tfile.c | 6 +- source/libs/index/test/fstTest.cc | 115 ++++++++++++++++++++++++++- source/libs/index/test/indexTests.cc | 15 +--- 6 files changed, 127 insertions(+), 20 deletions(-) diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 5167196031..6398259a96 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -360,6 +360,7 @@ static void indexMergeSameKey(SArray* result, TFileValue* tv) { if (sz > 0) { // TODO(yihao): remove duplicate tableid TFileValue* lv = taosArrayGetP(result, sz - 1); + // indexError("merge colVal: %s", lv->colVal); if (strcmp(lv->colVal, tv->colVal) == 0) { taosArrayAddAll(lv->tableId, tv->tableId); tfileValueDestroy(tv); @@ -368,6 +369,7 @@ static void indexMergeSameKey(SArray* result, TFileValue* tv) { } } else { taosArrayPush(result, &tv); + // indexError("merge colVal: %s", tv->colVal); } } static void indexDestroyTempResult(SArray* result) { diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index 0e46445a00..503d7cd928 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -20,7 +20,7 @@ #define MAX_INDEX_KEY_LEN 256 // test only, change later -#define MEM_TERM_LIMIT 10000 * 10 +#define MEM_TERM_LIMIT 10 * 10000 // ref index_cache.h:22 //#define CACHE_KEY_LEN(p) \ // (sizeof(int32_t) + sizeof(uint16_t) + sizeof(p->colType) + sizeof(p->nColVal) + p->nColVal + sizeof(uint64_t) + @@ -353,6 +353,9 @@ static bool indexCacheIteratorNext(Iterate* itera) { SSkipListIterator* iter = itera->iter; if (iter == NULL) { return false; } IterateValue* iv = &itera->val; + if (iv->colVal != NULL && iv->val != NULL) { + // indexError("value in cache: colVal: %s, size: %d", iv->colVal, (int)taosArrayGetSize(iv->val)); + } iterateValueDestroy(iv, false); bool next = tSkipListIterNext(iter); diff --git a/source/libs/index/src/index_fst.c b/source/libs/index/src/index_fst.c index 04a08dafd2..088c7369d5 100644 --- a/source/libs/index/src/index_fst.c +++ b/source/libs/index/src/index_fst.c @@ -319,7 +319,7 @@ void fstStateSetCommInput(FstState* s, uint8_t inp) { assert(s->state == OneTransNext || s->state == OneTrans); uint8_t val; - COMMON_INDEX(inp, 0x111111, val); + COMMON_INDEX(inp, 0b111111, val); s->val = (s->val & fstStateDict[s->state].val) | val; } @@ -369,7 +369,7 @@ uint8_t fstStateInput(FstState* s, FstNode* node) { bool null = false; uint8_t inp = fstStateCommInput(s, &null); uint8_t* data = fstSliceData(slice, NULL); - return null == false ? inp : data[-1]; + return null == false ? inp : data[node->start - 1]; } uint8_t fstStateInputForAnyTrans(FstState* s, FstNode* node, uint64_t i) { assert(s->state == AnyTrans); diff --git a/source/libs/index/src/index_tfile.c b/source/libs/index/src/index_tfile.c index fc4f8593a1..6669198861 100644 --- a/source/libs/index/src/index_tfile.c +++ b/source/libs/index/src/index_tfile.c @@ -385,8 +385,10 @@ int indexTFilePut(void* tfile, SIndexTerm* term, uint64_t uid) { } static bool tfileIteratorNext(Iterate* iiter) { IterateValue* iv = &iiter->val; + if (iv->colVal != NULL && iv->val != NULL) { + // indexError("value in fst: colVal: %s, size: %d", iv->colVal, (int)taosArrayGetSize(iv->val)); + } iterateValueDestroy(iv, false); - // SArray* tblIds = iv->val; char* colVal = NULL; uint64_t offset = 0; @@ -406,7 +408,7 @@ static bool tfileIteratorNext(Iterate* iiter) { if (tfileReaderLoadTableIds(tIter->rdr, offset, iv->val) != 0) { return false; } iv->colVal = colVal; - + return true; // std::string key(ch, sz); } diff --git a/source/libs/index/test/fstTest.cc b/source/libs/index/test/fstTest.cc index da974ce6c4..3d978c05a5 100644 --- a/source/libs/index/test/fstTest.cc +++ b/source/libs/index/test/fstTest.cc @@ -24,8 +24,13 @@ class FstWriter { _b = fstBuilderCreate(_wc, 0); } bool Put(const std::string& key, uint64_t val) { + // char buf[128] = {0}; + // int len = 0; + // taosMbsToUcs4(key.c_str(), key.size(), buf, 128, &len); + // FstSlice skey = fstSliceCreate((uint8_t*)buf, len); FstSlice skey = fstSliceCreate((uint8_t*)key.c_str(), key.size()); bool ok = fstBuilderInsert(_b, skey, val); + fstSliceDestroy(&skey); return ok; } @@ -61,6 +66,11 @@ class FstReadMemory { return _fst != NULL; } bool Get(const std::string& key, uint64_t* val) { + // char buf[128] = {0}; + // int len = 0; + // taosMbsToUcs4(key.c_str(), key.size(), buf, 128, &len); + // FstSlice skey = fstSliceCreate((uint8_t*)buf, len); + FstSlice skey = fstSliceCreate((uint8_t*)key.c_str(), key.size()); bool ok = fstGet(_fst, &skey, val); fstSliceDestroy(&skey); @@ -135,15 +145,109 @@ int Performance_fstWriteRecords(FstWriter* b) { } return L * M * N; } +void Performance_fstReadRecords(FstReadMemory* m) { + std::string str("aa"); + for (int i = 0; i < M; i++) { + str[0] = 'a' + i; + str.resize(2); + for (int j = 0; j < N; j++) { + str[1] = 'a' + j; + str.resize(2); + for (int k = 0; k < L; k++) { + str.push_back('a'); + uint64_t val, cost; + if (m->GetWithTimeCostUs(str, &val, &cost)) { + printf("succes to get kv(%s, %" PRId64 "), cost: %" PRId64 "\n", str.c_str(), val, cost); + } else { + printf("failed to get key: %s\n", str.c_str()); + } + } + } + } +} + +void checkMillonWriteAndReadOfFst() { + tfInit(); + FstWriter* fw = new FstWriter; + Performance_fstWriteRecords(fw); + delete fw; + FstReadMemory* fr = new FstReadMemory(1024 * 64 * 1024); + + if (fr->init()) { printf("success to init fst read"); } + + Performance_fstReadRecords(fr); + tfCleanup(); + delete fr; +} +void checkFstLongTerm() { + tfInit(); + FstWriter* fw = new FstWriter; + // Performance_fstWriteRecords(fw); + + fw->Put("A B", 1); + fw->Put("C", 2); + fw->Put("a", 3); + delete fw; + + FstReadMemory* m = new FstReadMemory(1024 * 64); + if (m->init() == false) { + std::cout << "init readMemory failed" << std::endl; + delete m; + return; + } + { + uint64_t val = 0; + if (m->Get("A B", &val)) { + std::cout << "success to Get: " << val << std::endl; + } else { + std::cout << "failed to Get:" << val << std::endl; + } + } + { + uint64_t val = 0; + if (m->Get("C", &val)) { + std::cout << "success to Get: " << val << std::endl; + } else { + std::cout << "failed to Get:" << val << std::endl; + } + } + { + uint64_t val = 0; + if (m->Get("a", &val)) { + std::cout << "success to Get: " << val << std::endl; + } else { + std::cout << "failed to Get:" << val << std::endl; + } + } + + // prefix search + // std::vector result; + + // AutomationCtx* ctx = automCtxCreate((void*)"ab", AUTOMATION_ALWAYS); + // m->Search(ctx, result); + // std::cout << "size: " << result.size() << std::endl; + // assert(result.size() == count); + // for (int i = 0; i < result.size(); i++) { + // assert(result[i] == i); // check result + //} + tfCleanup(); + // free(ctx); + // delete m; +} void checkFstCheckIterator() { tfInit(); FstWriter* fw = new FstWriter; int64_t s = taosGetTimestampUs(); int count = 2; - Performance_fstWriteRecords(fw); + // Performance_fstWriteRecords(fw); int64_t e = taosGetTimestampUs(); std::cout << "insert data count : " << count << "elapas time: " << e - s << std::endl; + + fw->Put("Hello world", 1); + fw->Put("hello world", 2); + fw->Put("hello worle", 3); + fw->Put("hello worlf", 4); delete fw; FstReadMemory* m = new FstReadMemory(1024 * 64); @@ -171,7 +275,7 @@ void checkFstCheckIterator() { void fst_get(Fst* fst) { for (int i = 0; i < 10000; i++) { - std::string term = "Hello"; + std::string term = "Hello World"; FstSlice key = fstSliceCreate((uint8_t*)term.c_str(), term.size()); uint64_t offset = 0; bool ret = fstGet(fst, &key, &offset); @@ -189,7 +293,7 @@ void validateTFile(char* arg) { std::thread threads[NUM_OF_THREAD]; // std::vector threads; - TFileReader* reader = tfileReaderOpen(arg, 0, 295868, "tag1"); + TFileReader* reader = tfileReaderOpen(arg, 0, 999992, "tag1"); for (int i = 0; i < NUM_OF_THREAD; i++) { threads[i] = std::thread(fst_get, reader->fst); @@ -203,9 +307,12 @@ void validateTFile(char* arg) { tfCleanup(); } int main(int argc, char* argv[]) { - if (argc > 1) { validateTFile(argv[1]); } + // tool to check all kind of fst test + // if (argc > 1) { validateTFile(argv[1]); } // checkFstCheckIterator(); + // checkFstLongTerm(); // checkFstPrefixSearch(); + checkMillonWriteAndReadOfFst(); return 1; } diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index 080becccf1..3ad64cd03e 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -787,14 +787,15 @@ TEST_F(IndexEnv2, testIndexOpen) { } TEST_F(IndexEnv2, testIndex_TrigeFlush) { - std::string path = "/tmp/test"; + std::string path = "/tmp/test1"; if (index->Init(path) != 0) { // r std::cout << "failed to init" << std::endl; } int numOfTable = 100 * 10000; - index->WriteMillonData("tag1", "Hello", numOfTable); - int target = index->SearchOne("tag1", "Hello"); + index->WriteMillonData("tag1", "Hello Wolrd", numOfTable); + int target = index->SearchOne("tag1", "Hello Wolrd"); + std::cout << "Get Index: " << target << std::endl; assert(numOfTable == target); } @@ -821,14 +822,6 @@ TEST_F(IndexEnv2, testIndex_serarch_cache_and_tfile) { threads[i].join(); } } -TEST_F(IndexEnv2, testIndex_multi_thread_write) { - std::string path = "/tmp"; - if (index->Init(path) != 0) {} -} -TEST_F(IndexEnv2, testIndex_multi_thread_read) { - std::string path = "/tmp"; - if (index->Init(path) != 0) {} -} TEST_F(IndexEnv2, testIndex_restart) { std::string path = "/tmp"; From db474b20789740c325b4094f6af96a583672338e Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sun, 2 Jan 2022 14:28:38 +0800 Subject: [PATCH 03/30] refactor code --- source/libs/index/inc/indexInt.h | 9 ++++ source/libs/index/inc/index_cache.h | 3 +- source/libs/index/inc/index_tfile.h | 13 ++--- source/libs/index/inc/index_util.h | 2 +- source/libs/index/src/index.c | 59 ++++++++++++++++------ source/libs/index/src/index_cache.c | 4 +- source/libs/index/src/index_tfile.c | 73 +++++++++++++--------------- source/libs/index/test/indexTests.cc | 2 +- 8 files changed, 94 insertions(+), 71 deletions(-) diff --git a/source/libs/index/inc/indexInt.h b/source/libs/index/inc/indexInt.h index 048c9e804e..6e1256d857 100644 --- a/source/libs/index/inc/indexInt.h +++ b/source/libs/index/inc/indexInt.h @@ -108,8 +108,17 @@ void iterateValueDestroy(IterateValue* iv, bool destroy); extern void* indexQhandle; +typedef struct TFileCacheKey { + uint64_t suid; + uint8_t colType; + char* colName; + int32_t nColName; +} ICacheKey; + int indexFlushCacheTFile(SIndex* sIdx, void*); +int32_t indexSerialCacheKey(ICacheKey* key, char* buf); + #define indexFatal(...) \ do { \ if (sDebugFlag & DEBUG_FATAL) { taosPrintLog("index FATAL ", 255, __VA_ARGS__); } \ diff --git a/source/libs/index/inc/index_cache.h b/source/libs/index/inc/index_cache.h index 12b66bca2c..805137ccaf 100644 --- a/source/libs/index/inc/index_cache.h +++ b/source/libs/index/inc/index_cache.h @@ -42,6 +42,7 @@ typedef struct IndexCache { int32_t version; int32_t nTerm; int8_t type; + uint64_t suid; pthread_mutex_t mtx; } IndexCache; @@ -58,7 +59,7 @@ typedef struct CacheTerm { } CacheTerm; // -IndexCache* indexCacheCreate(SIndex* idx, const char* colName, int8_t type); +IndexCache* indexCacheCreate(SIndex* idx, uint64_t suid, const char* colName, int8_t type); void indexCacheDestroy(void* cache); diff --git a/source/libs/index/inc/index_tfile.h b/source/libs/index/inc/index_tfile.h index 4928e01a63..675203fa1a 100644 --- a/source/libs/index/inc/index_tfile.h +++ b/source/libs/index/inc/index_tfile.h @@ -49,13 +49,6 @@ typedef struct TFileValue { int32_t offset; } TFileValue; -typedef struct TFileCacheKey { - uint64_t suid; - uint8_t colType; - char* colName; - int32_t nColName; -} TFileCacheKey; - // table cache // refactor to LRU cache later typedef struct TFileCache { @@ -103,10 +96,10 @@ typedef struct TFileReaderOpt { // tfile cache, manage tindex reader TFileCache* tfileCacheCreate(const char* path); void tfileCacheDestroy(TFileCache* tcache); -TFileReader* tfileCacheGet(TFileCache* tcache, TFileCacheKey* key); -void tfileCachePut(TFileCache* tcache, TFileCacheKey* key, TFileReader* reader); +TFileReader* tfileCacheGet(TFileCache* tcache, ICacheKey* key); +void tfileCachePut(TFileCache* tcache, ICacheKey* key, TFileReader* reader); -TFileReader* tfileGetReaderByCol(IndexTFile* tf, char* colName); +TFileReader* tfileGetReaderByCol(IndexTFile* tf, uint64_t suid, char* colName); TFileReader* tfileReaderOpen(char* path, uint64_t suid, int32_t version, const char* colName); TFileReader* tfileReaderCreate(WriterCtx* ctx); diff --git a/source/libs/index/inc/index_util.h b/source/libs/index/inc/index_util.h index 21c5ca155b..adeb52bb8c 100644 --- a/source/libs/index/inc/index_util.h +++ b/source/libs/index/inc/index_util.h @@ -34,7 +34,7 @@ extern "C" { #define SERIALIZE_VAR_TO_BUF(buf, var, type) \ do { \ type c = var; \ - assert(sizeof(var) == sizeof(type)); \ + assert(sizeof(type) == sizeof(c)); \ memcpy((void*)buf, (void*)&c, sizeof(c)); \ buf += sizeof(c); \ } while (0) diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 6398259a96..11a2aa5e5f 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -17,6 +17,7 @@ #include "indexInt.h" #include "index_cache.h" #include "index_tfile.h" +#include "index_util.h" #include "tdef.h" #include "tsched.h" @@ -130,18 +131,28 @@ int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { // TODO(yihao): reduce the lock range pthread_mutex_lock(&index->mtx); for (int i = 0; i < taosArrayGetSize(fVals); i++) { - SIndexTerm* p = taosArrayGetP(fVals, i); - IndexCache** cache = taosHashGet(index->colObj, p->colName, p->nColName); + SIndexTerm* p = taosArrayGetP(fVals, i); + + char buf[128] = {0}; + ICacheKey key = {.suid = p->suid, .colName = p->colName}; + int32_t sz = indexSerialCacheKey(&key, buf); + + IndexCache** cache = taosHashGet(index->colObj, buf, sz); if (cache == NULL) { - IndexCache* pCache = indexCacheCreate(index, p->colName, p->colType); - taosHashPut(index->colObj, p->colName, p->nColName, &pCache, sizeof(void*)); + IndexCache* pCache = indexCacheCreate(index, p->suid, p->colName, p->colType); + taosHashPut(index->colObj, buf, sz, &pCache, sizeof(void*)); } } pthread_mutex_unlock(&index->mtx); for (int i = 0; i < taosArrayGetSize(fVals); i++) { - SIndexTerm* p = taosArrayGetP(fVals, i); - IndexCache** cache = taosHashGet(index->colObj, p->colName, p->nColName); + SIndexTerm* p = taosArrayGetP(fVals, i); + + char buf[128] = {0}; + ICacheKey key = {.suid = p->suid, .colName = p->colName}; + int32_t sz = indexSerialCacheKey(&key, buf); + + IndexCache** cache = taosHashGet(index->colObj, buf, sz); assert(*cache != NULL); int ret = indexCachePut(*cache, p, uid); if (ret != 0) { return ret; } @@ -296,7 +307,12 @@ static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* query, SArray** result // Get col info IndexCache* cache = NULL; pthread_mutex_lock(&sIdx->mtx); - IndexCache** pCache = taosHashGet(sIdx->colObj, colName, nColName); + + char buf[128] = {0}; + ICacheKey key = {.suid = term->suid, .colName = term->colName}; + int32_t sz = indexSerialCacheKey(&key, buf); + + IndexCache** pCache = taosHashGet(sIdx->colObj, buf, sz); if (pCache == NULL) { pthread_mutex_unlock(&sIdx->mtx); return -1; @@ -385,10 +401,12 @@ int indexFlushCacheTFile(SIndex* sIdx, void* cache) { indexWarn("suid %" PRIu64 " merge cache into tindex", sIdx->suid); IndexCache* pCache = (IndexCache*)cache; - TFileReader* pReader = tfileGetReaderByCol(sIdx->tindex, pCache->colName); + TFileReader* pReader = tfileGetReaderByCol(sIdx->tindex, pCache->suid, pCache->colName); + if (pReader == NULL) { indexWarn("empty pReader found"); } // handle flush Iterate* cacheIter = indexCacheIteratorCreate(pCache); Iterate* tfileIter = tfileIteratorCreate(pReader); + if (tfileIter == NULL) { indexWarn("empty tfile reader iterator"); } SArray* result = taosArrayInit(1024, sizeof(void*)); @@ -468,7 +486,7 @@ static int indexGenTFile(SIndex* sIdx, IndexCache* cache, SArray* batch) { int32_t version = CACHE_VERSION(cache); uint8_t colType = cache->type; - TFileWriter* tw = tfileWriterOpen(sIdx->path, sIdx->suid, version, cache->colName, colType); + TFileWriter* tw = tfileWriterOpen(sIdx->path, cache->suid, version, cache->colName, colType); if (tw == NULL) { indexError("failed to open file to write"); return -1; @@ -481,14 +499,13 @@ static int indexGenTFile(SIndex* sIdx, IndexCache* cache, SArray* batch) { } tfileWriterClose(tw); - TFileReader* reader = tfileReaderOpen(sIdx->path, sIdx->suid, version, cache->colName); + TFileReader* reader = tfileReaderOpen(sIdx->path, cache->suid, version, cache->colName); + + char buf[128] = {0}; + TFileHeader* header = &reader->header; + ICacheKey key = { + .suid = cache->suid, .colName = header->colName, .nColName = strlen(header->colName), .colType = header->colType}; - char buf[128] = {0}; - TFileHeader* header = &reader->header; - TFileCacheKey key = {.suid = header->suid, - .colName = header->colName, - .nColName = strlen(header->colName), - .colType = header->colType}; pthread_mutex_lock(&sIdx->mtx); IndexTFile* ifile = (IndexTFile*)sIdx->tindex; @@ -499,3 +516,13 @@ static int indexGenTFile(SIndex* sIdx, IndexCache* cache, SArray* batch) { END: tfileWriterClose(tw); } + +int32_t indexSerialCacheKey(ICacheKey* key, char* buf) { + char* p = buf; + SERIALIZE_MEM_TO_BUF(buf, key, suid); + SERIALIZE_VAR_TO_BUF(buf, '_', char); + // SERIALIZE_MEM_TO_BUF(buf, key, colType); + // SERIALIZE_VAR_TO_BUF(buf, '_', char); + SERIALIZE_STR_MEM_TO_BUF(buf, key, colName, key->nColName); + return buf - p; +} diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index 503d7cd928..0f00d9d4af 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -40,7 +40,7 @@ static bool indexCacheIteratorNext(Iterate* itera); static IterateValue* indexCacheIteratorGetValue(Iterate* iter); -IndexCache* indexCacheCreate(SIndex* idx, const char* colName, int8_t type) { +IndexCache* indexCacheCreate(SIndex* idx, uint64_t suid, const char* colName, int8_t type) { IndexCache* cache = calloc(1, sizeof(IndexCache)); if (cache == NULL) { indexError("failed to create index cache"); @@ -53,7 +53,7 @@ IndexCache* indexCacheCreate(SIndex* idx, const char* colName, int8_t type) { cache->type = type; cache->index = idx; cache->version = 0; - + cache->suid = suid; pthread_mutex_init(&cache->mtx, NULL); indexCacheRef(cache); return cache; diff --git a/source/libs/index/src/index_tfile.c b/source/libs/index/src/index_tfile.c index 6669198861..fe54b5ebb3 100644 --- a/source/libs/index/src/index_tfile.c +++ b/source/libs/index/src/index_tfile.c @@ -51,7 +51,6 @@ static void tfileDestroyFileName(void* elem); static int tfileCompare(const void* a, const void* b); static int tfileParseFileName(const char* filename, uint64_t* suid, int* colId, int* version); static void tfileGenFileName(char* filename, uint64_t suid, int colId, int version); -static void tfileSerialCacheKey(TFileCacheKey* key, char* buf); TFileCache* tfileCacheCreate(const char* path) { TFileCache* tcache = calloc(1, sizeof(TFileCache)); @@ -80,18 +79,18 @@ TFileCache* tfileCacheCreate(const char* path) { goto End; } - char buf[128] = {0}; - TFileReader* reader = tfileReaderCreate(wc); - TFileHeader* header = &reader->header; - TFileCacheKey key = {.suid = header->suid, - .colName = header->colName, - .nColName = strlen(header->colName), - .colType = header->colType}; - tfileSerialCacheKey(&key, buf); + char buf[128] = {0}; + TFileReader* reader = tfileReaderCreate(wc); + TFileHeader* header = &reader->header; + ICacheKey key = {.suid = header->suid, + .colName = header->colName, + .nColName = strlen(header->colName), + .colType = header->colType}; + int32_t sz = indexSerialCacheKey(&key, buf); + assert(sz < sizeof(buf)); + taosHashPut(tcache->tableCache, buf, sz, &reader, sizeof(void*)); tfileReaderRef(reader); - // indexTable - taosHashPut(tcache->tableCache, buf, strlen(buf), &reader, sizeof(void*)); } taosArrayDestroyEx(files, tfileDestroyFileName); return tcache; @@ -117,30 +116,30 @@ void tfileCacheDestroy(TFileCache* tcache) { free(tcache); } -TFileReader* tfileCacheGet(TFileCache* tcache, TFileCacheKey* key) { - char buf[128] = {0}; - tfileSerialCacheKey(key, buf); - - TFileReader** reader = taosHashGet(tcache->tableCache, buf, strlen(buf)); +TFileReader* tfileCacheGet(TFileCache* tcache, ICacheKey* key) { + char buf[128] = {0}; + int32_t sz = indexSerialCacheKey(key, buf); + assert(sz < sizeof(buf)); + TFileReader** reader = taosHashGet(tcache->tableCache, buf, sz); if (reader == NULL) { return NULL; } tfileReaderRef(*reader); return *reader; } -void tfileCachePut(TFileCache* tcache, TFileCacheKey* key, TFileReader* reader) { - char buf[128] = {0}; - tfileSerialCacheKey(key, buf); +void tfileCachePut(TFileCache* tcache, ICacheKey* key, TFileReader* reader) { + char buf[128] = {0}; + int32_t sz = indexSerialCacheKey(key, buf); // remove last version index reader - TFileReader** p = taosHashGet(tcache->tableCache, buf, strlen(buf)); + TFileReader** p = taosHashGet(tcache->tableCache, buf, sz); if (p != NULL) { TFileReader* oldReader = *p; - taosHashRemove(tcache->tableCache, buf, strlen(buf)); + taosHashRemove(tcache->tableCache, buf, sz); oldReader->remove = true; tfileReaderUnRef(oldReader); } + taosHashPut(tcache->tableCache, buf, sz, &reader, sizeof(void*)); tfileReaderRef(reader); - taosHashPut(tcache->tableCache, buf, strlen(buf), &reader, sizeof(void*)); return; } TFileReader* tfileReaderCreate(WriterCtx* ctx) { @@ -230,8 +229,6 @@ TFileReader* tfileReaderOpen(char* path, uint64_t suid, int32_t version, const c TFileReader* reader = tfileReaderCreate(wc); return reader; - - // tfileSerialCacheKey(&key, buf); } TFileWriter* tfileWriterCreate(WriterCtx* ctx, TFileHeader* header) { // char pathBuf[128] = {0}; @@ -325,15 +322,19 @@ int tfileWriterPut(TFileWriter* tw, void* data, bool order) { tfileWriterClose(tw); return -1; } - // write fst + + // write data indexError("--------Begin----------------"); for (size_t i = 0; i < sz; i++) { // TODO, fst batch write later TFileValue* v = taosArrayGetP((SArray*)data, i); - if (tfileWriteData(tw, v) == 0) { - // + if (tfileWriteData(tw, v) != 0) { + indexError("failed to write data: %s, offset: %d len: %d", v->colVal, v->offset, + (int)taosArrayGetSize(v->tableId)); + } else { + indexInfo("success to write data: %s, offset: %d len: %d", v->colVal, v->offset, + (int)taosArrayGetSize(v->tableId)); } - indexError("data: %s, offset: %d len: %d", v->colVal, v->offset, (int)taosArrayGetSize(v->tableId)); } indexError("--------End----------------"); fstBuilderFinish(tw->fb); @@ -369,9 +370,8 @@ int indexTFileSearch(void* tfile, SIndexTermQuery* query, SArray* result) { if (tfile == NULL) { return ret; } IndexTFile* pTfile = (IndexTFile*)tfile; - SIndexTerm* term = query->term; - TFileCacheKey key = { - .suid = term->suid, .colType = term->colType, .colName = term->colName, .nColName = term->nColName}; + SIndexTerm* term = query->term; + ICacheKey key = {.suid = term->suid, .colType = term->colType, .colName = term->colName, .nColName = term->nColName}; TFileReader* reader = tfileCacheGet(pTfile->cache, &key); if (reader == NULL) { return 0; } @@ -453,9 +453,9 @@ void tfileIteratorDestroy(Iterate* iter) { free(iter); } -TFileReader* tfileGetReaderByCol(IndexTFile* tf, char* colName) { +TFileReader* tfileGetReaderByCol(IndexTFile* tf, uint64_t suid, char* colName) { if (tf == NULL) { return NULL; } - TFileCacheKey key = {.suid = 0, .colType = TSDB_DATA_TYPE_BINARY, .colName = colName, .nColName = strlen(colName)}; + ICacheKey key = {.suid = suid, .colType = TSDB_DATA_TYPE_BINARY, .colName = colName, .nColName = strlen(colName)}; return tfileCacheGet(tf->cache, &key); } @@ -650,10 +650,3 @@ static int tfileParseFileName(const char* filename, uint64_t* suid, int* colId, } return -1; } -static void tfileSerialCacheKey(TFileCacheKey* key, char* buf) { - // SERIALIZE_MEM_TO_BUF(buf, key, suid); - // SERIALIZE_VAR_TO_BUF(buf, '_', char); - // SERIALIZE_MEM_TO_BUF(buf, key, colType); - // SERIALIZE_VAR_TO_BUF(buf, '_', char); - SERIALIZE_STR_MEM_TO_BUF(buf, key, colName, key->nColName); -} diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index 3ad64cd03e..7bf40bc7b3 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -477,7 +477,7 @@ class CacheObj { public: CacheObj() { // TODO - cache = indexCacheCreate(NULL, "voltage", TSDB_DATA_TYPE_BINARY); + cache = indexCacheCreate(NULL, 0, "voltage", TSDB_DATA_TYPE_BINARY); } int Put(SIndexTerm* term, int16_t colId, int32_t version, uint64_t uid) { int ret = indexCachePut(cache, term, uid); From 734ff3f83938a2adb69e34c78aac6b61e080e949 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 06:48:48 +0000 Subject: [PATCH 04/30] refact --- include/util/encode.h | 14 +++++++------- source/util/src/encode.c | 10 +++++++--- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/util/encode.h b/include/util/encode.h index ee35791012..a3cbc9a42c 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -23,13 +23,6 @@ extern "C" { #endif -typedef struct { - td_endian_t endian; - uint8_t* data; - int32_t size; - int32_t pos; -} SEncoder, SDecoder; - #define tPut(TYPE, BUF, VAL) ((TYPE*)(BUF))[0] = (VAL) #define tGet(TYPE, BUF, VAL) (VAL) = ((TYPE*)(BUF))[0] @@ -57,6 +50,13 @@ typedef struct { #define tRGet32 tRPut32 #define tRGet64 tRPut64 +typedef struct { + td_endian_t endian; + uint8_t* data; + int32_t size; + int32_t pos; +} SEncoder, SDecoder; + #define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos) #define TD_CODER_MOVE_POS(CODER, MOVE) ((CODER)->pos += (MOVE)) #define TD_CHECK_CODER_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE)) diff --git a/source/util/src/encode.c b/source/util/src/encode.c index 9e648bb281..dc1b926db0 100644 --- a/source/util/src/encode.c +++ b/source/util/src/encode.c @@ -56,8 +56,12 @@ void tCoderClear(SCoder* pCoder) { } int tStartEncode(SCoder* pCoder) { + struct SCoderNode* pNode; + if (pCoder->data) { - struct SCoderNode* pNode = malloc(sizeof(*pNode)); + if (pCoder->size - pCoder->pos < sizeof(int32_t)) return -1; + + pNode = malloc(sizeof(*pNode)); if (pNode == NULL) return -1; pNode->data = pCoder->data; @@ -97,11 +101,11 @@ int tStartDecode(SCoder* pCoder) { int32_t size; struct SCoderNode* pNode; + // TODO: if (tDecodeI32(pCoder, &size) < 0) return -1; + pNode = malloc(sizeof(*pNode)); if (pNode == NULL) return -1; - // TODO: tDecodeI32(pCoder, &size); - pNode->data = pCoder->data; pNode->pos = pCoder->pos; pNode->size = pCoder->size; From 6420811c8a2f4ff7e483de96758efb82d917e297 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 08:47:28 +0000 Subject: [PATCH 05/30] mor --- include/util/encode.h | 479 ++++++++++++++------------------------- source/util/src/encode.c | 29 +-- 2 files changed, 176 insertions(+), 332 deletions(-) diff --git a/include/util/encode.h b/include/util/encode.h index a3cbc9a42c..6aa132060b 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -16,6 +16,7 @@ #ifndef _TD_UTIL_ENCODE_H_ #define _TD_UTIL_ENCODE_H_ +#include "freelist.h" #include "tcoding.h" #include "tmacro.h" @@ -50,38 +51,150 @@ extern "C" { #define tRGet32 tRPut32 #define tRGet64 tRPut64 -typedef struct { - td_endian_t endian; - uint8_t* data; - int32_t size; - int32_t pos; -} SEncoder, SDecoder; +typedef enum { TD_ENCODER, TD_DECODER } td_coder_t; +#define CODER_NODE_FIELDS \ + uint8_t* data; \ + int32_t size; \ + int32_t pos; + +struct SCoderNode { + TD_SLIST_NODE(SCoderNode); + CODER_NODE_FIELDS +}; + +typedef struct { + td_coder_t type; + td_endian_t endian; + SFreeList fl; + CODER_NODE_FIELDS + TD_SLIST(SCoderNode) stack; +} SCoder; + +#define TD_CODER_POS(CODER) ((CODER)->pos) #define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos) #define TD_CODER_MOVE_POS(CODER, MOVE) ((CODER)->pos += (MOVE)) #define TD_CHECK_CODER_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE)) -/* ------------------------ FOR ENCODER ------------------------ */ -static FORCE_INLINE void tInitEncoder(SEncoder* pEncoder, td_endian_t endian, uint8_t* data, int32_t size) { - pEncoder->endian = endian; - pEncoder->data = data; - pEncoder->size = (data) ? size : 0; - pEncoder->pos = 0; -} +void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size, td_coder_t type); +void tCoderClear(SCoder* pCoder); + +/* ------------------------ ENCODE ------------------------ */ +int tStartEncode(SCoder* pEncoder); +void tEndEncode(SCoder* pEncoder); +int tEncodeU8(SCoder* pEncoder, uint8_t val); +int tEncodeI8(SCoder* pEncoder, int8_t val); +int tEncodeU16(SCoder* pEncoder, uint16_t val); +int tEncodeI16(SCoder* pEncoder, int16_t val); +int tEncodeU32(SCoder* pEncoder, uint32_t val); +int tEncodeI32(SCoder* pEncoder, int32_t val); +int tEncodeU64(SCoder* pEncoder, uint64_t val); +int tEncodeI64(SCoder* pEncoder, int64_t val); +int tEncodeU16v(SCoder* pEncoder, uint16_t val); +int tEncodeI16v(SCoder* pEncoder, int16_t val); +int tEncodeU32v(SCoder* pEncoder, uint32_t val); +int tEncodeI32v(SCoder* pEncoder, int32_t val); +int tEncodeU64v(SCoder* pEncoder, uint64_t val); +int tEncodeI64v(SCoder* pEncoder, int64_t val); +int tEncodeFloat(SCoder* pEncoder, float val); +int tEncodeDouble(SCoder* pEncoder, double val); +int tEncodeCStr(SCoder* pEncoder, const char* val); + +/* ------------------------ DECODE ------------------------ */ +int tStartDecode(SCoder* pDecoder); +void tEndDecode(SCoder* pDecoder); +int tDecodeU8(SCoder* pDecoder, uint8_t* val); +int tDecodeI8(SCoder* pDecoder, int8_t* val); +int tDecodeU16(SCoder* pDecoder, uint16_t* val); +int tDecodeI16(SCoder* pDecoder, int16_t* val); +int tDecodeU32(SCoder* pDecoder, uint32_t* val); +int tDecodeI32(SCoder* pDecoder, int32_t* val); +int tDecodeU64(SCoder* pDecoder, uint64_t* val); +int tDecodeI64(SCoder* pDecoder, int64_t* val); +int tDecodeU16v(SCoder* pDecoder, uint16_t* val); +int tDecodeI16v(SCoder* pDecoder, int16_t* val); +int tDecodeU32v(SCoder* pDecoder, uint32_t* val); +int tDecodeI32v(SCoder* pDecoder, int32_t* val); +int tDecodeU64v(SCoder* pDecoder, uint64_t* val); +int tDecodeI64v(SCoder* pDecoder, int64_t* val); +int tDecodeFloat(SCoder* pDecoder, float* val); +int tDecodeDouble(SCoder* pDecoder, double* val); +int tDecodeCStr(SCoder* pEncoder, const char** val); + +/* ------------------------ IMPL ------------------------ */ +#define TD_ENCODE_MACRO(CODER, VAL, TYPE, BITS) \ + if ((CODER)->data) { \ + if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(VAL))) return -1; \ + if (TD_RT_ENDIAN() == (CODER)->endian) { \ + tPut(TYPE, TD_CODER_CURRENT(CODER), (VAL)); \ + } else { \ + tRPut##BITS(TD_CODER_CURRENT(CODER), &(VAL)); \ + } \ + } \ + TD_CODER_MOVE_POS(CODER, sizeof(VAL)); \ + return 0; + +#define TD_ENCODE_VARIANT_MACRO(CODER, VAL) \ + while ((VAL) > ENCODE_LIMIT) { \ + if ((CODER)->data) { \ + if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) return -1; \ + TD_CODER_CURRENT(CODER)[0] = ((VAL) | ENCODE_LIMIT) & 0xff; \ + } \ + \ + (VAL) >>= 7; \ + TD_CODER_MOVE_POS(CODER, 1); \ + } \ + \ + if ((CODER)->data) { \ + if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) return -1; \ + TD_CODER_CURRENT(CODER)[0] = (uint8_t)(VAL); \ + } \ + TD_CODER_MOVE_POS(CODER, 1); \ + return 0; + +#define TD_DECODE_MACRO(CODER, PVAL, TYPE, BITS) \ + if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(*(PVAL)))) return -1; \ + if (TD_RT_ENDIAN() == (CODER)->endian) { \ + tGet(TYPE, TD_CODER_CURRENT(CODER), *(PVAL)); \ + } else { \ + tRGet##BITS(PVAL, TD_CODER_CURRENT(CODER)); \ + } \ + \ + TD_CODER_MOVE_POS(CODER, sizeof(*(PVAL))); \ + return 0; + +#define TD_DECODE_VARIANT_MACRO(CODER, PVAL, TYPE) \ + int32_t i = 0; \ + *(PVAL) = 0; \ + for (;;) { \ + if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) return -1; \ + TYPE tval = TD_CODER_CURRENT(CODER)[0]; \ + if (tval < ENCODE_LIMIT) { \ + *(PVAL) |= (tval << (7 * i)); \ + TD_CODER_MOVE_POS(pDecoder, 1); \ + break; \ + } else { \ + *(PVAL) |= (((tval) & (ENCODE_LIMIT - 1)) << (7 * i)); \ + i++; \ + TD_CODER_MOVE_POS(pDecoder, 1); \ + } \ + } \ + \ + return 0; // 8 -static FORCE_INLINE int tEncodeU8(SEncoder* pEncoder, uint8_t val) { +static FORCE_INLINE int tEncodeU8(SCoder* pEncoder, uint8_t val) { if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; + if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; tPut(uint8_t, TD_CODER_CURRENT(pEncoder), val); } TD_CODER_MOVE_POS(pEncoder, sizeof(val)); return 0; } -static FORCE_INLINE int tEncodeI8(SEncoder* pEncoder, int8_t val) { +static FORCE_INLINE int tEncodeI8(SCoder* pEncoder, int8_t val) { if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; + if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; tPut(int8_t, TD_CODER_CURRENT(pEncoder), val); } TD_CODER_MOVE_POS(pEncoder, sizeof(val)); @@ -89,303 +202,77 @@ static FORCE_INLINE int tEncodeI8(SEncoder* pEncoder, int8_t val) { } // 16 -static FORCE_INLINE int tEncodeU16(SEncoder* pEncoder, uint16_t val) { - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; - if (TD_RT_ENDIAN() == pEncoder->endian) { - tPut(uint16_t, TD_CODER_CURRENT(pEncoder), val); - } else { - tRPut16(TD_CODER_CURRENT(pEncoder), &val); - } - } - TD_CODER_MOVE_POS(pEncoder, sizeof(val)); - return 0; -} - -static FORCE_INLINE int tEncodeI16(SEncoder* pEncoder, int16_t val) { - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; - if (TD_RT_ENDIAN() == pEncoder->endian) { - tPut(int16_t, TD_CODER_CURRENT(pEncoder), val); - } else { - tRPut16(TD_CODER_CURRENT(pEncoder), &val); - } - } - TD_CODER_MOVE_POS(pEncoder, sizeof(val)); - return 0; -} - +static FORCE_INLINE int tEncodeU16(SCoder* pEncoder, uint16_t val) { TD_ENCODE_MACRO(pEncoder, val, uint16_t, 16); } +static FORCE_INLINE int tEncodeI16(SCoder* pEncoder, int16_t val) { TD_ENCODE_MACRO(pEncoder, val, int16_t, 16); } // 32 -static FORCE_INLINE int tEncodeU32(SEncoder* pEncoder, uint32_t val) { - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; - if (TD_RT_ENDIAN() == pEncoder->endian) { - tPut(uint32_t, TD_CODER_CURRENT(pEncoder), val); - } else { - tRPut32(TD_CODER_CURRENT(pEncoder), &val); - } - } - TD_CODER_MOVE_POS(pEncoder, sizeof(val)); - return 0; -} - -static FORCE_INLINE int tEncodeI32(SEncoder* pEncoder, int32_t val) { - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; - if (TD_RT_ENDIAN() == pEncoder->endian) { - tPut(int32_t, TD_CODER_CURRENT(pEncoder), val); - } else { - tRPut32(TD_CODER_CURRENT(pEncoder), &val); - } - } - TD_CODER_MOVE_POS(pEncoder, sizeof(val)); - return 0; -} - +static FORCE_INLINE int tEncodeU32(SCoder* pEncoder, uint32_t val) { TD_ENCODE_MACRO(pEncoder, val, uint32_t, 32); } +static FORCE_INLINE int tEncodeI32(SCoder* pEncoder, int32_t val) { TD_ENCODE_MACRO(pEncoder, val, int32_t, 32); } // 64 -static FORCE_INLINE int tEncodeU64(SEncoder* pEncoder, uint64_t val) { - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; - if (TD_RT_ENDIAN() == pEncoder->endian) { - tPut(uint64_t, TD_CODER_CURRENT(pEncoder), val); - } else { - tRPut64(TD_CODER_CURRENT(pEncoder), &val); - } - } - TD_CODER_MOVE_POS(pEncoder, sizeof(val)); - return 0; -} - -static FORCE_INLINE int tEncodeI64(SEncoder* pEncoder, int64_t val) { - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; - if (TD_RT_ENDIAN() == pEncoder->endian) { - tPut(int64_t, TD_CODER_CURRENT(pEncoder), val); - } else { - tRPut64(TD_CODER_CURRENT(pEncoder), &val); - } - } - TD_CODER_MOVE_POS(pEncoder, sizeof(val)); - return 0; -} - +static FORCE_INLINE int tEncodeU64(SCoder* pEncoder, uint64_t val) { TD_ENCODE_MACRO(pEncoder, val, uint64_t, 64); } +static FORCE_INLINE int tEncodeI64(SCoder* pEncoder, int64_t val) { TD_ENCODE_MACRO(pEncoder, val, int64_t, 64); } // 16v -static FORCE_INLINE int tEncodeU16v(SEncoder* pEncoder, uint16_t val) { - int64_t i = 0; - while (val >= ENCODE_LIMIT) { - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, 1)) return -1; - TD_CODER_CURRENT(pEncoder)[i] = (val | ENCODE_LIMIT) & 0xff; - } - - val >>= 7; - i++; - } - - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, 1)) return -1; - TD_CODER_CURRENT(pEncoder)[i] = (uint8_t)val; - } - - TD_CODER_MOVE_POS(pEncoder, i + 1); - - return 0; -} - -static FORCE_INLINE int tEncodeI16v(SEncoder* pEncoder, int16_t val) { +static FORCE_INLINE int tEncodeU16v(SCoder* pEncoder, uint16_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); } +static FORCE_INLINE int tEncodeI16v(SCoder* pEncoder, int16_t val) { return tEncodeU16v(pEncoder, ZIGZAGE(int16_t, val)); } - // 32v -static FORCE_INLINE int tEncodeU32v(SEncoder* pEncoder, uint32_t val) { - int64_t i = 0; - while (val >= ENCODE_LIMIT) { - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, 1)) return -1; - TD_CODER_CURRENT(pEncoder)[i] = (val | ENCODE_LIMIT) & 0xff; - } - - val >>= 7; - i++; - } - - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, 1)) return -1; - TD_CODER_CURRENT(pEncoder)[i] = (uint8_t)val; - } - - TD_CODER_MOVE_POS(pEncoder, i + 1); - - return 0; -} - -static FORCE_INLINE int tEncodeI32v(SEncoder* pEncoder, int32_t val) { +static FORCE_INLINE int tEncodeU32v(SCoder* pEncoder, uint32_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); } +static FORCE_INLINE int tEncodeI32v(SCoder* pEncoder, int32_t val) { return tEncodeU32v(pEncoder, ZIGZAGE(int32_t, val)); } - // 64v -static FORCE_INLINE int tEncodeU64v(SEncoder* pEncoder, uint64_t val) { - int64_t i = 0; - while (val >= ENCODE_LIMIT) { - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, 1)) return -1; - TD_CODER_CURRENT(pEncoder)[i] = (val | ENCODE_LIMIT) & 0xff; - } - - val >>= 7; - i++; - } - - if (pEncoder->data) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pEncoder, 1)) return -1; - TD_CODER_CURRENT(pEncoder)[i] = (uint8_t)val; - } - - TD_CODER_MOVE_POS(pEncoder, i + 1); - - return 0; -} - -static FORCE_INLINE int tEncodeI64v(SEncoder* pEncoder, int64_t val) { +static FORCE_INLINE int tEncodeU64v(SCoder* pEncoder, uint64_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); } +static FORCE_INLINE int tEncodeI64v(SCoder* pEncoder, int64_t val) { return tEncodeU64v(pEncoder, ZIGZAGE(int64_t, val)); } -static FORCE_INLINE int tEncodeFloat(SEncoder* pEncoder, float val) { +static FORCE_INLINE int tEncodeFloat(SCoder* pEncoder, float val) { // TODO return 0; } -static FORCE_INLINE int tEncodeDouble(SEncoder* pEncoder, double val) { +static FORCE_INLINE int tEncodeDouble(SCoder* pEncoder, double val) { // TODO return 0; } -static FORCE_INLINE int tEncodeCStr(SEncoder* pEncoder, const char* val) { +static FORCE_INLINE int tEncodeCStr(SCoder* pEncoder, const char* val) { // TODO return 0; } /* ------------------------ FOR DECODER ------------------------ */ -static FORCE_INLINE void tInitDecoder(SDecoder* pDecoder, td_endian_t endian, uint8_t* data, int32_t size) { - ASSERT(!TD_IS_NULL(data)); - pDecoder->endian = endian; - pDecoder->data = data; - pDecoder->size = size; - pDecoder->pos = 0; -} - // 8 -static FORCE_INLINE int tDecodeU8(SDecoder* pDecoder, uint8_t* val) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; +static FORCE_INLINE int tDecodeU8(SCoder* pDecoder, uint8_t* val) { + if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; tGet(uint8_t, TD_CODER_CURRENT(pDecoder), *val); TD_CODER_MOVE_POS(pDecoder, sizeof(*val)); return 0; } -static FORCE_INLINE int tDecodeI8(SDecoder* pDecoder, int8_t* val) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; +static FORCE_INLINE int tDecodeI8(SCoder* pDecoder, int8_t* val) { + if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; tGet(int8_t, TD_CODER_CURRENT(pDecoder), *val); TD_CODER_MOVE_POS(pDecoder, sizeof(*val)); return 0; } // 16 -static FORCE_INLINE int tDecodeU16(SDecoder* pDecoder, uint16_t* val) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; - if (TD_RT_ENDIAN() == pDecoder->endian) { - tGet(uint16_t, TD_CODER_CURRENT(pDecoder), *val); - } else { - tRGet16(val, TD_CODER_CURRENT(pDecoder)); - } - - TD_CODER_MOVE_POS(pDecoder, sizeof(*val)); - return 0; -} - -static FORCE_INLINE int tDecodeI16(SDecoder* pDecoder, int16_t* val) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; - if (TD_RT_ENDIAN() == pDecoder->endian) { - tGet(int16_t, TD_CODER_CURRENT(pDecoder), *val); - } else { - tRGet16(val, TD_CODER_CURRENT(pDecoder)); - } - - TD_CODER_MOVE_POS(pDecoder, sizeof(*val)); - return 0; -} - +static FORCE_INLINE int tDecodeU16(SCoder* pDecoder, uint16_t* val) { TD_DECODE_MACRO(pDecoder, val, uint16_t, 16); } +static FORCE_INLINE int tDecodeI16(SCoder* pDecoder, int16_t* val) { TD_DECODE_MACRO(pDecoder, val, int16_t, 16); } // 32 -static FORCE_INLINE int tDecodeU32(SDecoder* pDecoder, uint32_t* val) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; - if (TD_RT_ENDIAN() == pDecoder->endian) { - tGet(uint32_t, TD_CODER_CURRENT(pDecoder), *val); - } else { - tRGet32(val, TD_CODER_CURRENT(pDecoder)); - } - - TD_CODER_MOVE_POS(pDecoder, sizeof(*val)); - return 0; -} - -static FORCE_INLINE int tDecodeI32(SDecoder* pDecoder, int32_t* val) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; - if (TD_RT_ENDIAN() == pDecoder->endian) { - tGet(int32_t, TD_CODER_CURRENT(pDecoder), *val); - } else { - tRGet32(val, TD_CODER_CURRENT(pDecoder)); - } - - TD_CODER_MOVE_POS(pDecoder, sizeof(*val)); - return 0; -} - +static FORCE_INLINE int tDecodeU32(SCoder* pDecoder, uint32_t* val) { TD_DECODE_MACRO(pDecoder, val, uint32_t, 32); } +static FORCE_INLINE int tDecodeI32(SCoder* pDecoder, int32_t* val) { TD_DECODE_MACRO(pDecoder, val, int32_t, 32); } // 64 -static FORCE_INLINE int tDecodeU64(SDecoder* pDecoder, uint64_t* val) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; - if (TD_RT_ENDIAN() == pDecoder->endian) { - tGet(uint64_t, TD_CODER_CURRENT(pDecoder), *val); - } else { - tRGet64(val, TD_CODER_CURRENT(pDecoder)); - } - - TD_CODER_MOVE_POS(pDecoder, sizeof(*val)); - return 0; -} - -static FORCE_INLINE int tDecodeI64(SDecoder* pDecoder, int64_t* val) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; - if (TD_RT_ENDIAN() == pDecoder->endian) { - tGet(int64_t, TD_CODER_CURRENT(pDecoder), *val); - } else { - tRGet64(val, TD_CODER_CURRENT(pDecoder)); - } - - TD_CODER_MOVE_POS(pDecoder, sizeof(*val)); - return 0; -} +static FORCE_INLINE int tDecodeU64(SCoder* pDecoder, uint64_t* val) { TD_DECODE_MACRO(pDecoder, val, uint64_t, 64); } +static FORCE_INLINE int tDecodeI64(SCoder* pDecoder, int64_t* val) { TD_DECODE_MACRO(pDecoder, val, int64_t, 64); } // 16v -static FORCE_INLINE int tDecodeU16v(SDecoder* pDecoder, uint16_t* val) { - int64_t i = 0; - *val = 0; - for (;;) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, 1)) return -1; - uint16_t tval = TD_CODER_CURRENT(pDecoder)[i]; - if (tval < ENCODE_LIMIT) { - (*val) |= (tval << (7 * i)); - break; - } else { - (*val) |= (((tval) & (ENCODE_LIMIT - 1)) << (7 * i)); - i++; - } - } - - TD_CODER_MOVE_POS(pDecoder, i); - - return 0; +static FORCE_INLINE int tDecodeU16v(SCoder* pDecoder, uint16_t* val) { + TD_DECODE_VARIANT_MACRO(pDecoder, val, uint16_t); } -static FORCE_INLINE int tDecodeI16v(SDecoder* pDecoder, int16_t* val) { +static FORCE_INLINE int tDecodeI16v(SCoder* pDecoder, int16_t* val) { uint16_t tval; if (tDecodeU16v(pDecoder, &tval) < 0) { return -1; @@ -395,27 +282,11 @@ static FORCE_INLINE int tDecodeI16v(SDecoder* pDecoder, int16_t* val) { } // 32v -static FORCE_INLINE int tDecodeU32v(SDecoder* pDecoder, uint32_t* val) { - int64_t i = 0; - *val = 0; - for (;;) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, 1)) return -1; - uint32_t tval = TD_CODER_CURRENT(pDecoder)[i]; - if (tval < ENCODE_LIMIT) { - (*val) |= (tval << (7 * i)); - break; - } else { - (*val) |= (((tval) & (ENCODE_LIMIT - 1)) << (7 * i)); - i++; - } - } - - TD_CODER_MOVE_POS(pDecoder, i); - - return 0; +static FORCE_INLINE int tDecodeU32v(SCoder* pDecoder, uint32_t* val) { + TD_DECODE_VARIANT_MACRO(pDecoder, val, uint32_t); } -static FORCE_INLINE int tDecodeI32v(SDecoder* pDecoder, int32_t* val) { +static FORCE_INLINE int tDecodeI32v(SCoder* pDecoder, int32_t* val) { uint32_t tval; if (tDecodeU32v(pDecoder, &tval) < 0) { return -1; @@ -425,27 +296,11 @@ static FORCE_INLINE int tDecodeI32v(SDecoder* pDecoder, int32_t* val) { } // 64v -static FORCE_INLINE int tDecodeU64v(SDecoder* pDecoder, uint64_t* val) { - int64_t i = 0; - *val = 0; - for (;;) { - if (TD_CHECK_CODER_CAPACITY_FAILED(pDecoder, 1)) return -1; - uint64_t tval = TD_CODER_CURRENT(pDecoder)[i]; - if (tval < ENCODE_LIMIT) { - (*val) |= (tval << (7 * i)); - break; - } else { - (*val) |= (((tval) & (ENCODE_LIMIT - 1)) << (7 * i)); - i++; - } - } - - TD_CODER_MOVE_POS(pDecoder, i); - - return 0; +static FORCE_INLINE int tDecodeU64v(SCoder* pDecoder, uint64_t* val) { + TD_DECODE_VARIANT_MACRO(pDecoder, val, uint64_t); } -static FORCE_INLINE int tDecodeI64v(SDecoder* pDecoder, int64_t* val) { +static FORCE_INLINE int tDecodeI64v(SCoder* pDecoder, int64_t* val) { uint64_t tval; if (tDecodeU64v(pDecoder, &tval) < 0) { return -1; @@ -454,17 +309,17 @@ static FORCE_INLINE int tDecodeI64v(SDecoder* pDecoder, int64_t* val) { return 0; } -static FORCE_INLINE int tDecodeFloat(SDecoder* pDecoder, float* val) { +static FORCE_INLINE int tDecodeFloat(SCoder* pDecoder, float* val) { // TODO return 0; } -static FORCE_INLINE int tDecodeDouble(SDecoder* pDecoder, double* val) { +static FORCE_INLINE int tDecodeDouble(SCoder* pDecoder, double* val) { // TODO return 0; } -static FORCE_INLINE int tDecodeCStr(SDecoder* pEncoder, const char** val) { +static FORCE_INLINE int tDecodeCStr(SCoder* pEncoder, const char** val) { // TODO return 0; } diff --git a/source/util/src/encode.c b/source/util/src/encode.c index dc1b926db0..192ecefe43 100644 --- a/source/util/src/encode.c +++ b/source/util/src/encode.c @@ -14,28 +14,15 @@ */ #include "encode.h" -#include "freelist.h" -#define CODER_NODE_FIELDS \ - uint8_t* data; \ - int32_t size; \ - int32_t pos; +void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size, td_coder_t type) { + if (type == TD_ENCODER) { + if (data == NULL) size = 0; + } else { + ASSERT(data && size > 0); + } -struct SCoderNode { - TD_SLIST_NODE(SCoderNode); - CODER_NODE_FIELDS -}; - -typedef struct { - td_endian_t endian; - SFreeList fl; - CODER_NODE_FIELDS - TD_SLIST(SCoderNode) stack; -} SCoder; - -bool tDecodeIsEnd(SCoder* pCoder) { return (pCoder->size == pCoder->pos); } - -void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size) { + pCoder->type = type; pCoder->endian = endian; pCoder->data = data; pCoder->size = size; @@ -55,6 +42,8 @@ void tCoderClear(SCoder* pCoder) { } } +bool tDecodeIsEnd(SCoder* pCoder) { return (pCoder->size == pCoder->pos); } + int tStartEncode(SCoder* pCoder) { struct SCoderNode* pNode; From 6b6f3f75af8240c8120ca0b902b87952091ae2ba Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 09:27:33 +0000 Subject: [PATCH 06/30] refact --- include/common/tmsg.h | 6 ++- include/util/encode.h | 78 ++++++++++++++-------------- source/client/CMakeLists.txt | 4 +- source/common/CMakeLists.txt | 4 +- source/common/src/tmsg.c | 10 +++- source/libs/catalog/CMakeLists.txt | 4 +- source/libs/parser/CMakeLists.txt | 4 +- source/libs/planner/CMakeLists.txt | 4 +- source/libs/qcom/CMakeLists.txt | 8 +-- source/libs/qworker/CMakeLists.txt | 4 +- source/libs/scheduler/CMakeLists.txt | 4 +- source/util/CMakeLists.txt | 5 +- 12 files changed, 82 insertions(+), 53 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index af250bff03..5365a0ee36 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -58,6 +58,7 @@ extern int tMsgDict[]; typedef uint16_t tmsg_t; /* ------------------------ ENCODE/DECODE FUNCTIONS AND MACROS ------------------------ */ +#if 0 struct SMEListNode { TD_SLIST_NODE(SMEListNode); SEncoder coder; @@ -96,6 +97,7 @@ typedef struct SMsgDecoder { void tmsgInitMsgDecoder(SMsgDecoder* pMD, td_endian_t endian, uint8_t* data, int64_t size); void tmsgClearMsgDecoder(SMsgDecoder* pMD); +#endif /* ------------------------ OTHER DEFINITIONS ------------------------ */ // IE type @@ -1283,8 +1285,8 @@ typedef struct { SArray* pArray; } SVCreateTbBatchReq; -int tmsgSVCreateTbReqEncode(SMsgEncoder* pCoder, SVCreateTbReq* pReq); -int tmsgSVCreateTbReqDecode(SMsgDecoder* pCoder, SVCreateTbReq* pReq); +// int tmsgSVCreateTbReqEncode(SMsgEncoder* pCoder, SVCreateTbReq* pReq); +// int tmsgSVCreateTbReqDecode(SMsgDecoder* pCoder, SVCreateTbReq* pReq); int tSerializeSVCreateTbReq(void** buf, SVCreateTbReq* pReq); void* tDeserializeSVCreateTbReq(void* buf, SVCreateTbReq* pReq); int tSVCreateTbBatchReqSerialize(void** buf, SVCreateTbBatchReq* pReq); diff --git a/include/util/encode.h b/include/util/encode.h index 6aa132060b..5c7d3134ae 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -74,52 +74,52 @@ typedef struct { #define TD_CODER_POS(CODER) ((CODER)->pos) #define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos) #define TD_CODER_MOVE_POS(CODER, MOVE) ((CODER)->pos += (MOVE)) -#define TD_CHECK_CODER_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE)) +#define TD_CODER_CHECK_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE)) void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size, td_coder_t type); void tCoderClear(SCoder* pCoder); /* ------------------------ ENCODE ------------------------ */ -int tStartEncode(SCoder* pEncoder); -void tEndEncode(SCoder* pEncoder); -int tEncodeU8(SCoder* pEncoder, uint8_t val); -int tEncodeI8(SCoder* pEncoder, int8_t val); -int tEncodeU16(SCoder* pEncoder, uint16_t val); -int tEncodeI16(SCoder* pEncoder, int16_t val); -int tEncodeU32(SCoder* pEncoder, uint32_t val); -int tEncodeI32(SCoder* pEncoder, int32_t val); -int tEncodeU64(SCoder* pEncoder, uint64_t val); -int tEncodeI64(SCoder* pEncoder, int64_t val); -int tEncodeU16v(SCoder* pEncoder, uint16_t val); -int tEncodeI16v(SCoder* pEncoder, int16_t val); -int tEncodeU32v(SCoder* pEncoder, uint32_t val); -int tEncodeI32v(SCoder* pEncoder, int32_t val); -int tEncodeU64v(SCoder* pEncoder, uint64_t val); -int tEncodeI64v(SCoder* pEncoder, int64_t val); -int tEncodeFloat(SCoder* pEncoder, float val); -int tEncodeDouble(SCoder* pEncoder, double val); -int tEncodeCStr(SCoder* pEncoder, const char* val); +static int tStartEncode(SCoder* pEncoder); +static void tEndEncode(SCoder* pEncoder); +static int tEncodeU8(SCoder* pEncoder, uint8_t val); +static int tEncodeI8(SCoder* pEncoder, int8_t val); +static int tEncodeU16(SCoder* pEncoder, uint16_t val); +static int tEncodeI16(SCoder* pEncoder, int16_t val); +static int tEncodeU32(SCoder* pEncoder, uint32_t val); +static int tEncodeI32(SCoder* pEncoder, int32_t val); +static int tEncodeU64(SCoder* pEncoder, uint64_t val); +static int tEncodeI64(SCoder* pEncoder, int64_t val); +static int tEncodeU16v(SCoder* pEncoder, uint16_t val); +static int tEncodeI16v(SCoder* pEncoder, int16_t val); +static int tEncodeU32v(SCoder* pEncoder, uint32_t val); +static int tEncodeI32v(SCoder* pEncoder, int32_t val); +static int tEncodeU64v(SCoder* pEncoder, uint64_t val); +static int tEncodeI64v(SCoder* pEncoder, int64_t val); +static int tEncodeFloat(SCoder* pEncoder, float val); +static int tEncodeDouble(SCoder* pEncoder, double val); +static int tEncodeCStr(SCoder* pEncoder, const char* val); /* ------------------------ DECODE ------------------------ */ -int tStartDecode(SCoder* pDecoder); -void tEndDecode(SCoder* pDecoder); -int tDecodeU8(SCoder* pDecoder, uint8_t* val); -int tDecodeI8(SCoder* pDecoder, int8_t* val); -int tDecodeU16(SCoder* pDecoder, uint16_t* val); -int tDecodeI16(SCoder* pDecoder, int16_t* val); -int tDecodeU32(SCoder* pDecoder, uint32_t* val); -int tDecodeI32(SCoder* pDecoder, int32_t* val); -int tDecodeU64(SCoder* pDecoder, uint64_t* val); -int tDecodeI64(SCoder* pDecoder, int64_t* val); -int tDecodeU16v(SCoder* pDecoder, uint16_t* val); -int tDecodeI16v(SCoder* pDecoder, int16_t* val); -int tDecodeU32v(SCoder* pDecoder, uint32_t* val); -int tDecodeI32v(SCoder* pDecoder, int32_t* val); -int tDecodeU64v(SCoder* pDecoder, uint64_t* val); -int tDecodeI64v(SCoder* pDecoder, int64_t* val); -int tDecodeFloat(SCoder* pDecoder, float* val); -int tDecodeDouble(SCoder* pDecoder, double* val); -int tDecodeCStr(SCoder* pEncoder, const char** val); +static int tStartDecode(SCoder* pDecoder); +static void tEndDecode(SCoder* pDecoder); +static int tDecodeU8(SCoder* pDecoder, uint8_t* val); +static int tDecodeI8(SCoder* pDecoder, int8_t* val); +static int tDecodeU16(SCoder* pDecoder, uint16_t* val); +static int tDecodeI16(SCoder* pDecoder, int16_t* val); +static int tDecodeU32(SCoder* pDecoder, uint32_t* val); +static int tDecodeI32(SCoder* pDecoder, int32_t* val); +static int tDecodeU64(SCoder* pDecoder, uint64_t* val); +static int tDecodeI64(SCoder* pDecoder, int64_t* val); +static int tDecodeU16v(SCoder* pDecoder, uint16_t* val); +static int tDecodeI16v(SCoder* pDecoder, int16_t* val); +static int tDecodeU32v(SCoder* pDecoder, uint32_t* val); +static int tDecodeI32v(SCoder* pDecoder, int32_t* val); +static int tDecodeU64v(SCoder* pDecoder, uint64_t* val); +static int tDecodeI64v(SCoder* pDecoder, int64_t* val); +static int tDecodeFloat(SCoder* pDecoder, float* val); +static int tDecodeDouble(SCoder* pDecoder, double* val); +static int tDecodeCStr(SCoder* pEncoder, const char** val); /* ------------------------ IMPL ------------------------ */ #define TD_ENCODE_MACRO(CODER, VAL, TYPE, BITS) \ diff --git a/source/client/CMakeLists.txt b/source/client/CMakeLists.txt index ad6a93415c..3210c0c6de 100644 --- a/source/client/CMakeLists.txt +++ b/source/client/CMakeLists.txt @@ -11,4 +11,6 @@ target_link_libraries( PRIVATE os util common transport parser planner catalog scheduler function qcom ) -ADD_SUBDIRECTORY(test) \ No newline at end of file +if(${BUILD_TEST}) + ADD_SUBDIRECTORY(test) +endif(${BUILD_TEST}) \ No newline at end of file diff --git a/source/common/CMakeLists.txt b/source/common/CMakeLists.txt index 1ff83b091d..04e5a36e86 100644 --- a/source/common/CMakeLists.txt +++ b/source/common/CMakeLists.txt @@ -12,4 +12,6 @@ target_link_libraries( INTERFACE api ) -ADD_SUBDIRECTORY(test) +if(${BUILD_TEST}) + ADD_SUBDIRECTORY(test) +endif(${BUILD_TEST}) diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index a18a472dba..c0baa1e411 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -27,12 +27,15 @@ #undef TD_MSG_SEG_CODE_ #include "tmsgdef.h" +#if 0 static int tmsgStartEncode(SMsgEncoder *pME); static void tmsgEndEncode(SMsgEncoder *pME); static int tmsgStartDecode(SMsgDecoder *pMD); static void tmsgEndDecode(SMsgDecoder *pMD); +#endif /* ------------------------ ENCODE/DECODE FUNCTIONS ------------------------ */ +#if 0 void tmsgInitMsgEncoder(SMsgEncoder *pME, td_endian_t endian, uint8_t *data, int64_t size) { tInitEncoder(&(pME->coder), endian, data, size); TD_SLIST_INIT(&(pME->eStack)); @@ -74,8 +77,10 @@ void tmsgClearMsgDecoder(SMsgDecoder *pMD) { } } } +#endif /* ------------------------ MESSAGE ENCODE/DECODE ------------------------ */ +#if 0 int tmsgSVCreateTbReqEncode(SMsgEncoder *pCoder, SVCreateTbReq *pReq) { tmsgStartEncode(pCoder); // TODO @@ -97,6 +102,7 @@ int tmsgSVCreateTbReqDecode(SMsgDecoder *pCoder, SVCreateTbReq *pReq) { tmsgEndDecode(pCoder); return 0; } +#endif int tSerializeSVCreateTbReq(void **buf, SVCreateTbReq *pReq) { int tlen = 0; @@ -221,6 +227,7 @@ void *tSVCreateTbBatchReqDeserialize(void *buf, SVCreateTbBatchReq *pReq) { } /* ------------------------ STATIC METHODS ------------------------ */ +#if 0 static int tmsgStartEncode(SMsgEncoder *pME) { struct SMEListNode *pNode = (struct SMEListNode *)malloc(sizeof(*pNode)); if (TD_IS_NULL(pNode)) return -1; @@ -278,4 +285,5 @@ static void tmsgEndDecode(SMsgDecoder *pMD) { pMD->coder = pNode->coder; free(pNode); -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/source/libs/catalog/CMakeLists.txt b/source/libs/catalog/CMakeLists.txt index f47e105b8a..bb9ed686a7 100644 --- a/source/libs/catalog/CMakeLists.txt +++ b/source/libs/catalog/CMakeLists.txt @@ -11,4 +11,6 @@ target_link_libraries( PRIVATE os util transport qcom ) -ADD_SUBDIRECTORY(test) \ No newline at end of file +if(${BUILD_TEST}) + ADD_SUBDIRECTORY(test) +endif(${BUILD_TEST}) \ No newline at end of file diff --git a/source/libs/parser/CMakeLists.txt b/source/libs/parser/CMakeLists.txt index 6ab3020935..7f77876c4c 100644 --- a/source/libs/parser/CMakeLists.txt +++ b/source/libs/parser/CMakeLists.txt @@ -11,4 +11,6 @@ target_link_libraries( PRIVATE os util catalog function transport qcom ) -ADD_SUBDIRECTORY(test) +if(${BUILD_TEST}) + ADD_SUBDIRECTORY(test) +endif(${BUILD_TEST}) diff --git a/source/libs/planner/CMakeLists.txt b/source/libs/planner/CMakeLists.txt index 7f8c118663..6234dbe0ac 100644 --- a/source/libs/planner/CMakeLists.txt +++ b/source/libs/planner/CMakeLists.txt @@ -11,4 +11,6 @@ target_link_libraries( PRIVATE os util catalog cjson parser transport function qcom ) -ADD_SUBDIRECTORY(test) +if(${BUILD_TEST}) + ADD_SUBDIRECTORY(test) +endif(${BUILD_TEST}) diff --git a/source/libs/qcom/CMakeLists.txt b/source/libs/qcom/CMakeLists.txt index 8e09f3d97a..c63e54fb9b 100644 --- a/source/libs/qcom/CMakeLists.txt +++ b/source/libs/qcom/CMakeLists.txt @@ -7,8 +7,10 @@ target_include_directories( ) target_link_libraries( - qcom - PRIVATE os util transport + qcom + PRIVATE os util transport ) -ADD_SUBDIRECTORY(test) +if(${BUILD_TEST}) + ADD_SUBDIRECTORY(test) +endif(${BUILD_TEST}) diff --git a/source/libs/qworker/CMakeLists.txt b/source/libs/qworker/CMakeLists.txt index 001756d7c3..ea3e97057b 100644 --- a/source/libs/qworker/CMakeLists.txt +++ b/source/libs/qworker/CMakeLists.txt @@ -11,4 +11,6 @@ target_link_libraries( PRIVATE os util transport planner qcom ) -ADD_SUBDIRECTORY(test) \ No newline at end of file +if(${BUILD_TEST}) + ADD_SUBDIRECTORY(test) +endif(${BUILD_TEST}) \ No newline at end of file diff --git a/source/libs/scheduler/CMakeLists.txt b/source/libs/scheduler/CMakeLists.txt index 6baaab1ef4..4e297f4e17 100644 --- a/source/libs/scheduler/CMakeLists.txt +++ b/source/libs/scheduler/CMakeLists.txt @@ -12,4 +12,6 @@ target_link_libraries( PRIVATE os util planner qcom common catalog transport ) -ADD_SUBDIRECTORY(test) \ No newline at end of file +if(${BUILD_TEST}) + ADD_SUBDIRECTORY(test) +endif(${BUILD_TEST}) \ No newline at end of file diff --git a/source/util/CMakeLists.txt b/source/util/CMakeLists.txt index bf1774b45b..ed936e90f6 100644 --- a/source/util/CMakeLists.txt +++ b/source/util/CMakeLists.txt @@ -14,4 +14,7 @@ target_link_libraries( PUBLIC api ) -ADD_SUBDIRECTORY(test) +if(${BUILD_TEST}) + ADD_SUBDIRECTORY(test) +endif(${BUILD_TEST}) + From 6d3032d538c82715784eb0c1dd80dc96faa242f6 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 09:40:11 +0000 Subject: [PATCH 07/30] more --- include/util/encode.h | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/include/util/encode.h b/include/util/encode.h index 5c7d3134ae..4164a7660f 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -227,13 +227,21 @@ static FORCE_INLINE int tEncodeI64v(SCoder* pEncoder, int64_t val) { } static FORCE_INLINE int tEncodeFloat(SCoder* pEncoder, float val) { - // TODO - return 0; + union { + uint32_t ui; + float f; + } v = {.f = val}; + + return tEncodeU32(pEncoder, v.ui); } static FORCE_INLINE int tEncodeDouble(SCoder* pEncoder, double val) { - // TODO - return 0; + union { + uint64_t ui; + double d; + } v = {.d = val}; + + return tEncodeU64(pEncoder, v.ui); } static FORCE_INLINE int tEncodeCStr(SCoder* pEncoder, const char* val) { @@ -310,12 +318,30 @@ static FORCE_INLINE int tDecodeI64v(SCoder* pDecoder, int64_t* val) { } static FORCE_INLINE int tDecodeFloat(SCoder* pDecoder, float* val) { - // TODO + union { + uint32_t ui; + float f; + } v; + + if (tDecodeU32(pDecoder, &(v.ui)) < 0) { + return -1; + } + + *val = v.f; return 0; } static FORCE_INLINE int tDecodeDouble(SCoder* pDecoder, double* val) { - // TODO + union { + uint64_t ui; + double d; + } v; + + if (tDecodeU64(pEncoder, &(v.ui)) < 0) { + return -1; + } + + *val = v.d; return 0; } From e4252065d1a4d113ae66baace85765433e2085dd Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 09:41:00 +0000 Subject: [PATCH 08/30] refact --- include/util/encode.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/util/encode.h b/include/util/encode.h index 4164a7660f..3c98e8108f 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -236,7 +236,7 @@ static FORCE_INLINE int tEncodeFloat(SCoder* pEncoder, float val) { } static FORCE_INLINE int tEncodeDouble(SCoder* pEncoder, double val) { - union { + union { uint64_t ui; double d; } v = {.d = val}; From ec03a0eb8fdb7059fad42bac3ee991ec88d36e44 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 09:42:57 +0000 Subject: [PATCH 09/30] more --- source/util/src/encode.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/source/util/src/encode.c b/source/util/src/encode.c index 192ecefe43..a3946daf5c 100644 --- a/source/util/src/encode.c +++ b/source/util/src/encode.c @@ -15,6 +15,11 @@ #include "encode.h" +#if __STDC_VERSION__ >= 201112L +static_assert(sizeof(float) == sizeof(uint32_t), "sizeof(float) must equal to sizeof(uint32_t)"); +static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) must equal to sizeof(uint64_t)"); +#endif + void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size, td_coder_t type) { if (type == TD_ENCODER) { if (data == NULL) size = 0; From 40fc4b6cf38c0373de43be0f68638fd858c1a562 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sun, 2 Jan 2022 17:50:40 +0800 Subject: [PATCH 10/30] fix mem leak and invalid read/write found by valgrind --- source/libs/index/inc/index_tfile.h | 1 + source/libs/index/src/index.c | 3 ++- source/libs/index/src/index_cache.c | 1 + source/libs/index/src/index_fst.c | 2 ++ source/libs/index/src/index_tfile.c | 9 ++++++--- source/libs/index/test/indexTests.cc | 29 +++++++++++++++++++++++++--- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/source/libs/index/inc/index_tfile.h b/source/libs/index/inc/index_tfile.h index 675203fa1a..4618a39197 100644 --- a/source/libs/index/inc/index_tfile.h +++ b/source/libs/index/inc/index_tfile.h @@ -117,6 +117,7 @@ int tfileWriterFinish(TFileWriter* tw); // IndexTFile* indexTFileCreate(const char* path); +void indexTFileDestroy(IndexTFile* tfile); int indexTFilePut(void* tfile, SIndexTerm* term, uint64_t uid); int indexTFileSearch(void* tfile, SIndexTermQuery* query, SArray* result); diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 11a2aa5e5f..9c7320b301 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -103,6 +103,7 @@ void indexClose(SIndex* sIdx) { } taosHashCleanup(sIdx->colObj); pthread_mutex_destroy(&sIdx->mtx); + indexTFileDestroy(sIdx->tindex); #endif free(sIdx->path); free(sIdx); @@ -479,7 +480,7 @@ void iterateValueDestroy(IterateValue* value, bool destroy) { } else { if (value->val != NULL) { taosArrayClear(value->val); } } - // free(value->colVal); + free(value->colVal); value->colVal = NULL; } static int indexGenTFile(SIndex* sIdx, IndexCache* cache, SArray* batch) { diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index 0f00d9d4af..b4c533e998 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -150,6 +150,7 @@ Iterate* indexCacheIteratorCreate(IndexCache* cache) { MemTable* tbl = cache->imm; iiter->val.val = taosArrayInit(1, sizeof(uint64_t)); + iiter->val.colVal = NULL; iiter->iter = tbl != NULL ? tSkipListCreateIter(tbl->mem) : NULL; iiter->next = indexCacheIteratorNext; iiter->getValue = indexCacheIteratorGetValue; diff --git a/source/libs/index/src/index_fst.c b/source/libs/index/src/index_fst.c index 088c7369d5..bfaeeaaa33 100644 --- a/source/libs/index/src/index_fst.c +++ b/source/libs/index/src/index_fst.c @@ -1062,6 +1062,7 @@ Output fstEmptyFinalOutput(Fst* fst, bool* null) { } else { *null = true; } + fstNodeDestroy(node); return res; } @@ -1286,6 +1287,7 @@ StreamWithStateResult* streamWithStateNextWith(StreamWithState* sws, StreamCallb StreamWithStateResult* result = swsResultCreate(&slice, fOutput, tState); free(buf); fstSliceDestroy(&slice); + taosArrayDestroy(nodes); return result; } free(buf); diff --git a/source/libs/index/src/index_tfile.c b/source/libs/index/src/index_tfile.c index fe54b5ebb3..95c713fb0a 100644 --- a/source/libs/index/src/index_tfile.c +++ b/source/libs/index/src/index_tfile.c @@ -360,7 +360,7 @@ IndexTFile* indexTFileCreate(const char* path) { tfile->cache = tfileCacheCreate(path); return tfile; } -void IndexTFileDestroy(IndexTFile* tfile) { +void indexTFileDestroy(IndexTFile* tfile) { tfileCacheDestroy(tfile->cache); free(tfile); } @@ -415,7 +415,7 @@ static bool tfileIteratorNext(Iterate* iiter) { static IterateValue* tifileIterateGetValue(Iterate* iter) { return &iter->val; } static TFileFstIter* tfileFstIteratorCreate(TFileReader* reader) { - TFileFstIter* tIter = calloc(1, sizeof(Iterate)); + TFileFstIter* tIter = calloc(1, sizeof(TFileFstIter)); if (tIter == NULL) { return NULL; } tIter->ctx = automCtxCreate(NULL, AUTOMATION_ALWAYS); @@ -437,6 +437,7 @@ Iterate* tfileIteratorCreate(TFileReader* reader) { iter->next = tfileIteratorNext; iter->getValue = tifileIterateGetValue; iter->val.val = taosArrayInit(1, sizeof(uint64_t)); + iter->val.colVal = NULL; return iter; } void tfileIteratorDestroy(Iterate* iter) { @@ -449,6 +450,7 @@ void tfileIteratorDestroy(Iterate* iter) { streamWithStateDestroy(tIter->st); fstStreamBuilderDestroy(tIter->fb); automCtxDestroy(tIter->ctx); + free(tIter); free(iter); } @@ -482,7 +484,7 @@ static int tfileValueCompare(const void* a, const void* b, const void* param) { TFileValue* tfileValueCreate(char* val) { TFileValue* tf = calloc(1, sizeof(TFileValue)); if (tf == NULL) { return NULL; } - tf->colVal = val; + tf->colVal = tstrdup(val); tf->tableId = taosArrayInit(32, sizeof(uint64_t)); return tf; } @@ -493,6 +495,7 @@ int tfileValuePush(TFileValue* tf, uint64_t val) { } void tfileValueDestroy(TFileValue* tf) { taosArrayDestroy(tf->tableId); + free(tf->colVal); free(tf); } static void tfileSerialTableIdsToBuf(char* buf, SArray* ids) { diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index 7bf40bc7b3..bdfb86ce17 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -457,7 +457,10 @@ TEST_F(IndexTFileEnv, test_tfile_write) { // taosArrayPush(data, &v4); fObj->Put(data); - for (size_t i = 0; i < taosArrayGetSize(data); i++) { destroyTFileValue(taosArrayGetP(data, i)); } + for (size_t i = 0; i < taosArrayGetSize(data); i++) { + // data + destroyTFileValue(taosArrayGetP(data, i)); + } taosArrayDestroy(data); std::string colName("voltage"); @@ -470,6 +473,7 @@ TEST_F(IndexTFileEnv, test_tfile_write) { fObj->Get(&query, result); assert(taosArrayGetSize(result) == 200); indexTermDestroy(term); + taosArrayDestroy(result); // tfileWriterDestroy(twrite); } @@ -534,6 +538,7 @@ TEST_F(IndexCacheEnv, cache_test) { SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size()); coj->Put(term, colId, version++, suid++); + indexTermDestroy(term); // indexTermDestry(term); } { @@ -541,24 +546,28 @@ TEST_F(IndexCacheEnv, cache_test) { SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size()); coj->Put(term, colId, version++, suid++); + indexTermDestroy(term); } { std::string colVal("v2"); SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size()); coj->Put(term, colId, version++, suid++); + indexTermDestroy(term); } { std::string colVal("v3"); SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size()); coj->Put(term, colId, version++, suid++); + indexTermDestroy(term); } { std::string colVal("v3"); SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size()); coj->Put(term, colId, version++, suid++); + indexTermDestroy(term); } coj->Debug(); std::cout << "--------first----------" << std::endl; @@ -567,12 +576,14 @@ TEST_F(IndexCacheEnv, cache_test) { SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size()); coj->Put(term, othColId, version++, suid++); + indexTermDestroy(term); } { std::string colVal("v4"); SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size()); coj->Put(term, othColId, version++, suid++); + indexTermDestroy(term); } coj->Debug(); std::cout << "--------second----------" << std::endl; @@ -583,6 +594,7 @@ TEST_F(IndexCacheEnv, cache_test) { SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size()); coj->Put(term, colId, version++, suid++); + indexTermDestroy(term); } } coj->Debug(); @@ -598,6 +610,9 @@ TEST_F(IndexCacheEnv, cache_test) { coj->Get(&query, colId, 10000, ret, &valType); std::cout << "size : " << taosArrayGetSize(ret) << std::endl; assert(taosArrayGetSize(ret) == 4); + taosArrayDestroy(ret); + + indexTermDestroy(term); } { std::string colVal("v2"); @@ -609,6 +624,9 @@ TEST_F(IndexCacheEnv, cache_test) { coj->Get(&query, colId, 10000, ret, &valType); assert(taosArrayGetSize(ret) == 1); + taosArrayDestroy(ret); + + indexTermDestroy(term); } } class IndexObj { @@ -678,13 +696,16 @@ class IndexObj { SArray* result = (SArray*)taosArrayInit(1, sizeof(uint64_t)); if (Search(mq, result) == 0) { std::cout << "search one successfully" << std::endl; } - return taosArrayGetSize(result); + int sz = taosArrayGetSize(result); + indexMultiTermQueryDestroy(mq); + taosArrayDestroy(result); + return sz; // assert(taosArrayGetSize(result) == targetSize); } void PutOne(const std::string& colName, const std::string& colVal) { + SIndexMultiTerm* terms = indexMultiTermCreate(); SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size()); - SIndexMultiTerm* terms = indexMultiTermCreate(); indexMultiTermAdd(terms, term); Put(terms, 10); indexMultiTermDestroy(terms); @@ -783,6 +804,8 @@ TEST_F(IndexEnv2, testIndexOpen) { index->Search(mq, result); std::cout << "target size: " << taosArrayGetSize(result) << std::endl; assert(taosArrayGetSize(result) == 400); + taosArrayDestroy(result); + indexMultiTermQueryDestroy(mq); } } From 82be83a22616c8868231ca02eb8489d4bd3f9acf Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 09:51:47 +0000 Subject: [PATCH 11/30] more --- include/util/encode.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/include/util/encode.h b/include/util/encode.h index 3c98e8108f..7dc8d07f0a 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -244,11 +244,20 @@ static FORCE_INLINE int tEncodeDouble(SCoder* pEncoder, double val) { return tEncodeU64(pEncoder, v.ui); } -static FORCE_INLINE int tEncodeCStr(SCoder* pEncoder, const char* val) { - // TODO +static FORCE_INLINE int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, size_t len) { + if (tEncodeI32v(pEncoder, len) < 0) return -1; + if (pEncoder->data) { + if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, len + 1)) return -1; + memcpy(TD_CODER_CURRENT(pEncoder), val, len + 1); + } + TD_CODER_MOVE_POS(pEncoder, len + 1); return 0; } +static FORCE_INLINE int tEncodeCStr(SCoder* pEncoder, const char* val) { + return tEncodeCstrWithLen(pEncoder, val, strlen(val)); +} + /* ------------------------ FOR DECODER ------------------------ */ // 8 static FORCE_INLINE int tDecodeU8(SCoder* pDecoder, uint8_t* val) { From 6835e4956be283f2bb1184772a96e284f919cac7 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 10:03:21 +0000 Subject: [PATCH 12/30] more --- include/util/encode.h | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/include/util/encode.h b/include/util/encode.h index 7dc8d07f0a..0b90d66f5a 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -98,6 +98,7 @@ static int tEncodeU64v(SCoder* pEncoder, uint64_t val); static int tEncodeI64v(SCoder* pEncoder, int64_t val); static int tEncodeFloat(SCoder* pEncoder, float val); static int tEncodeDouble(SCoder* pEncoder, double val); +static int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, uint64_t len); static int tEncodeCStr(SCoder* pEncoder, const char* val); /* ------------------------ DECODE ------------------------ */ @@ -119,6 +120,7 @@ static int tDecodeU64v(SCoder* pDecoder, uint64_t* val); static int tDecodeI64v(SCoder* pDecoder, int64_t* val); static int tDecodeFloat(SCoder* pDecoder, float* val); static int tDecodeDouble(SCoder* pDecoder, double* val); +static int tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len); static int tDecodeCStr(SCoder* pEncoder, const char** val); /* ------------------------ IMPL ------------------------ */ @@ -244,8 +246,8 @@ static FORCE_INLINE int tEncodeDouble(SCoder* pEncoder, double val) { return tEncodeU64(pEncoder, v.ui); } -static FORCE_INLINE int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, size_t len) { - if (tEncodeI32v(pEncoder, len) < 0) return -1; +static FORCE_INLINE int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, uint64_t len) { + if (tEncodeU64v(pEncoder, len) < 0) return -1; if (pEncoder->data) { if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, len + 1)) return -1; memcpy(TD_CODER_CURRENT(pEncoder), val, len + 1); @@ -255,7 +257,7 @@ static FORCE_INLINE int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, si } static FORCE_INLINE int tEncodeCStr(SCoder* pEncoder, const char* val) { - return tEncodeCstrWithLen(pEncoder, val, strlen(val)); + return tEncodeCstrWithLen(pEncoder, val, (uint64_t)strlen(val)); } /* ------------------------ FOR DECODER ------------------------ */ @@ -346,7 +348,7 @@ static FORCE_INLINE int tDecodeDouble(SCoder* pDecoder, double* val) { double d; } v; - if (tDecodeU64(pEncoder, &(v.ui)) < 0) { + if (tDecodeU64(pDecoder, &(v.ui)) < 0) { return -1; } @@ -354,11 +356,21 @@ static FORCE_INLINE int tDecodeDouble(SCoder* pDecoder, double* val) { return 0; } -static FORCE_INLINE int tDecodeCStr(SCoder* pEncoder, const char** val) { - // TODO +static FORCE_INLINE int tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len) { + if (tDecodeU64v(pDecoder, len) < 0) return -1; + + if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, *len + 1)) return -1; + *val = (char *)TD_CODER_CURRENT(pDecoder); + + TD_CODER_MOVE_POS(pDecoder, *len + 1); return 0; } +static FORCE_INLINE int tDecodeCStr(SCoder* pDecoder, const char** val) { + uint64_t len; + return tDecodeCStrAndLen(pDecoder, val, &len); +} + #ifdef __cplusplus } #endif From 17852c2c431f558cae31182f9b5af5555661013f Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 10:05:05 +0000 Subject: [PATCH 13/30] more --- include/util/encode.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/util/encode.h b/include/util/encode.h index 0b90d66f5a..b3ebda4ed7 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -75,6 +75,7 @@ typedef struct { #define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos) #define TD_CODER_MOVE_POS(CODER, MOVE) ((CODER)->pos += (MOVE)) #define TD_CODER_CHECK_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE)) +#define TCODER_MALLOC(SIZE, CODER) TFL_MALLOC(SIZE, &((CODER)->fl)) void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size, td_coder_t type); void tCoderClear(SCoder* pCoder); From bac6f05b96a9998b8e5ef7d8b4228f7b56c4105f Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 10:06:00 +0000 Subject: [PATCH 14/30] refact --- include/util/encode.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/util/encode.h b/include/util/encode.h index b3ebda4ed7..7585566dc9 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -361,7 +361,7 @@ static FORCE_INLINE int tDecodeCStrAndLen(SCoder* pDecoder, const char** val, ui if (tDecodeU64v(pDecoder, len) < 0) return -1; if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, *len + 1)) return -1; - *val = (char *)TD_CODER_CURRENT(pDecoder); + *val = (char*)TD_CODER_CURRENT(pDecoder); TD_CODER_MOVE_POS(pDecoder, *len + 1); return 0; From ecaf4268987976a679ac4ba9ac1e7d60c168f2a3 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 10:15:52 +0000 Subject: [PATCH 15/30] more --- include/util/encode.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/include/util/encode.h b/include/util/encode.h index 7585566dc9..c916d8fc6a 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -99,6 +99,7 @@ static int tEncodeU64v(SCoder* pEncoder, uint64_t val); static int tEncodeI64v(SCoder* pEncoder, int64_t val); static int tEncodeFloat(SCoder* pEncoder, float val); static int tEncodeDouble(SCoder* pEncoder, double val); +static int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len); static int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, uint64_t len); static int tEncodeCStr(SCoder* pEncoder, const char* val); @@ -121,8 +122,9 @@ static int tDecodeU64v(SCoder* pDecoder, uint64_t* val); static int tDecodeI64v(SCoder* pDecoder, int64_t* val); static int tDecodeFloat(SCoder* pDecoder, float* val); static int tDecodeDouble(SCoder* pDecoder, double* val); +static int tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len); static int tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len); -static int tDecodeCStr(SCoder* pEncoder, const char** val); +static int tDecodeCStr(SCoder* pDecoder, const char** val); /* ------------------------ IMPL ------------------------ */ #define TD_ENCODE_MACRO(CODER, VAL, TYPE, BITS) \ @@ -247,6 +249,17 @@ static FORCE_INLINE int tEncodeDouble(SCoder* pEncoder, double val) { return tEncodeU64(pEncoder, v.ui); } +static FORCE_INLINE int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len) { + if (tEncodeU64v(pEncoder, len) < 0) return -1; + if (pEncoder->data) { + if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, len)) return -1; + memcpy(TD_CODER_CURRENT(pEncoder), len); + } + + TD_CODER_MOVE_POS(pEncoder, len); + return 0; +} + static FORCE_INLINE int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, uint64_t len) { if (tEncodeU64v(pEncoder, len) < 0) return -1; if (pEncoder->data) { @@ -357,6 +370,16 @@ static FORCE_INLINE int tDecodeDouble(SCoder* pDecoder, double* val) { return 0; } +static FORCE_INLINE int tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len) { + if (tDecodeU64v(pDecoder, &len) < 0) return -1; + + if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, *len)) return -1; + *val = (void*)TD_CODER_CURRENT(pDecoder); + + TD_CODER_MOVE_POS(pDecoder, *len); + return 0; +} + static FORCE_INLINE int tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len) { if (tDecodeU64v(pDecoder, len) < 0) return -1; From da8e603c49f9091780e0aa5fda324ebbf535b64b Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 10:21:37 +0000 Subject: [PATCH 16/30] more --- include/util/encode.h | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/include/util/encode.h b/include/util/encode.h index c916d8fc6a..f5c8db0b5a 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -253,7 +253,7 @@ static FORCE_INLINE int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_ if (tEncodeU64v(pEncoder, len) < 0) return -1; if (pEncoder->data) { if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, len)) return -1; - memcpy(TD_CODER_CURRENT(pEncoder), len); + memcpy(TD_CODER_CURRENT(pEncoder), val, len); } TD_CODER_MOVE_POS(pEncoder, len); @@ -261,13 +261,7 @@ static FORCE_INLINE int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_ } static FORCE_INLINE int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, uint64_t len) { - if (tEncodeU64v(pEncoder, len) < 0) return -1; - if (pEncoder->data) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, len + 1)) return -1; - memcpy(TD_CODER_CURRENT(pEncoder), val, len + 1); - } - TD_CODER_MOVE_POS(pEncoder, len + 1); - return 0; + return tEncodeBinary(pEncoder, (void*)val, len + 1); } static FORCE_INLINE int tEncodeCStr(SCoder* pEncoder, const char* val) { @@ -371,7 +365,7 @@ static FORCE_INLINE int tDecodeDouble(SCoder* pDecoder, double* val) { } static FORCE_INLINE int tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len) { - if (tDecodeU64v(pDecoder, &len) < 0) return -1; + if (tDecodeU64v(pDecoder, len) < 0) return -1; if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, *len)) return -1; *val = (void*)TD_CODER_CURRENT(pDecoder); @@ -381,12 +375,8 @@ static FORCE_INLINE int tDecodeBinary(SCoder* pDecoder, const void** val, uint64 } static FORCE_INLINE int tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len) { - if (tDecodeU64v(pDecoder, len) < 0) return -1; - - if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, *len + 1)) return -1; - *val = (char*)TD_CODER_CURRENT(pDecoder); - - TD_CODER_MOVE_POS(pDecoder, *len + 1); + if (tDecodeBinary(pDecoder, (const void**)val, len) < 0) return -1; + (*len) -= 1; return 0; } From 8f1a1b7bbc5b5d15ea49d9e8e5944c518e71984c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 10:26:32 +0000 Subject: [PATCH 17/30] refact --- include/common/tmsg.h | 42 ------------ source/common/src/tmsg.c | 141 +-------------------------------------- 2 files changed, 1 insertion(+), 182 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 5365a0ee36..6c2b4a1acc 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -57,48 +57,6 @@ extern int tMsgDict[]; typedef uint16_t tmsg_t; -/* ------------------------ ENCODE/DECODE FUNCTIONS AND MACROS ------------------------ */ -#if 0 -struct SMEListNode { - TD_SLIST_NODE(SMEListNode); - SEncoder coder; -}; - -typedef struct SMsgEncoder { - SEncoder coder; - TD_SLIST(SMEListNode) eStack; // encode stack -} SMsgEncoder; - -struct SMDFreeListNode { - TD_SLIST_NODE(SMDFreeListNode); - char payload[]; -}; - -struct SMDListNode { - TD_SLIST_NODE(SMDListNode); - SDecoder coder; -}; - -typedef struct SMsgDecoder { - SDecoder coder; - TD_SLIST(SMDListNode) dStack; - TD_SLIST(SMDFreeListNode) freeList; -} SMsgDecoder; - -#define TMSG_MALLOC(SIZE, DECODER) \ - ({ \ - void* ptr = malloc((SIZE) + sizeof(struct SMDFreeListNode)); \ - if (ptr) { \ - TD_SLIST_PUSH(&((DECODER)->freeList), (struct SMDFreeListNode*)ptr); \ - ptr = POINTER_SHIFT(ptr, sizeof(struct SMDFreeListNode*)); \ - } \ - ptr; \ - }) - -void tmsgInitMsgDecoder(SMsgDecoder* pMD, td_endian_t endian, uint8_t* data, int64_t size); -void tmsgClearMsgDecoder(SMsgDecoder* pMD); -#endif - /* ------------------------ OTHER DEFINITIONS ------------------------ */ // IE type #define TSDB_IE_TYPE_SEC 1 diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index c0baa1e411..0874c471aa 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -27,83 +27,6 @@ #undef TD_MSG_SEG_CODE_ #include "tmsgdef.h" -#if 0 -static int tmsgStartEncode(SMsgEncoder *pME); -static void tmsgEndEncode(SMsgEncoder *pME); -static int tmsgStartDecode(SMsgDecoder *pMD); -static void tmsgEndDecode(SMsgDecoder *pMD); -#endif - -/* ------------------------ ENCODE/DECODE FUNCTIONS ------------------------ */ -#if 0 -void tmsgInitMsgEncoder(SMsgEncoder *pME, td_endian_t endian, uint8_t *data, int64_t size) { - tInitEncoder(&(pME->coder), endian, data, size); - TD_SLIST_INIT(&(pME->eStack)); -} - -void tmsgClearMsgEncoder(SMsgEncoder *pME) { - struct SMEListNode *pNode; - for (;;) { - pNode = TD_SLIST_HEAD(&(pME->eStack)); - if (TD_IS_NULL(pNode)) break; - TD_SLIST_POP(&(pME->eStack)); - free(pNode); - } -} - -void tmsgInitMsgDecoder(SMsgDecoder *pMD, td_endian_t endian, uint8_t *data, int64_t size) { - tInitDecoder(&pMD->coder, endian, data, size); - TD_SLIST_INIT(&(pMD->dStack)); - TD_SLIST_INIT(&(pMD->freeList)); -} - -void tmsgClearMsgDecoder(SMsgDecoder *pMD) { - { - struct SMDFreeListNode *pNode; - for (;;) { - pNode = TD_SLIST_HEAD(&(pMD->freeList)); - if (TD_IS_NULL(pNode)) break; - TD_SLIST_POP(&(pMD->freeList)); - free(pNode); - } - } - { - struct SMDListNode *pNode; - for (;;) { - pNode = TD_SLIST_HEAD(&(pMD->dStack)); - if (TD_IS_NULL(pNode)) break; - TD_SLIST_POP(&(pMD->dStack)); - free(pNode); - } - } -} -#endif - -/* ------------------------ MESSAGE ENCODE/DECODE ------------------------ */ -#if 0 -int tmsgSVCreateTbReqEncode(SMsgEncoder *pCoder, SVCreateTbReq *pReq) { - tmsgStartEncode(pCoder); - // TODO - - tmsgEndEncode(pCoder); - return 0; -} - -int tmsgSVCreateTbReqDecode(SMsgDecoder *pCoder, SVCreateTbReq *pReq) { - tmsgStartDecode(pCoder); - - // TODO: decode - - // Decode is not end - if (pCoder->coder.pos != pCoder->coder.size) { - // Continue decode - } - - tmsgEndDecode(pCoder); - return 0; -} -#endif - int tSerializeSVCreateTbReq(void **buf, SVCreateTbReq *pReq) { int tlen = 0; @@ -224,66 +147,4 @@ void *tSVCreateTbBatchReqDeserialize(void *buf, SVCreateTbBatchReq *pReq) { } return buf; -} - -/* ------------------------ STATIC METHODS ------------------------ */ -#if 0 -static int tmsgStartEncode(SMsgEncoder *pME) { - struct SMEListNode *pNode = (struct SMEListNode *)malloc(sizeof(*pNode)); - if (TD_IS_NULL(pNode)) return -1; - - pNode->coder = pME->coder; - TD_SLIST_PUSH(&(pME->eStack), pNode); - TD_CODER_MOVE_POS(&(pME->coder), sizeof(int32_t)); - - return 0; -} - -static void tmsgEndEncode(SMsgEncoder *pME) { - int32_t size; - struct SMEListNode *pNode; - - pNode = TD_SLIST_HEAD(&(pME->eStack)); - ASSERT(pNode); - TD_SLIST_POP(&(pME->eStack)); - - size = pME->coder.pos - pNode->coder.pos; - tEncodeI32(&(pNode->coder), size); - - free(pNode); -} - -static int tmsgStartDecode(SMsgDecoder *pMD) { - struct SMDListNode *pNode; - int32_t size; - - pNode = (struct SMDListNode *)malloc(sizeof(*pNode)); - if (pNode == NULL) return -1; - - tDecodeI32(&(pMD->coder), &size); - - pNode->coder = pMD->coder; - TD_SLIST_PUSH(&(pMD->dStack), pNode); - - pMD->coder.pos = 0; - pMD->coder.size = size - sizeof(int32_t); - pMD->coder.data = TD_CODER_CURRENT(&(pNode->coder)); - - return 0; -} - -static void tmsgEndDecode(SMsgDecoder *pMD) { - ASSERT(pMD->coder.pos == pMD->coder.size); - struct SMDListNode *pNode; - - pNode = TD_SLIST_HEAD(&(pMD->dStack)); - ASSERT(pNode); - TD_SLIST_POP(&(pMD->dStack)); - - pNode->coder.pos += pMD->coder.size; - - pMD->coder = pNode->coder; - - free(pNode); -} -#endif \ No newline at end of file +} \ No newline at end of file From 729aabca1591540002121139c3d002276f077ef4 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 10:29:47 +0000 Subject: [PATCH 18/30] refact --- include/common/tmsg.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 6c2b4a1acc..85e75139ce 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1243,8 +1243,6 @@ typedef struct { SArray* pArray; } SVCreateTbBatchReq; -// int tmsgSVCreateTbReqEncode(SMsgEncoder* pCoder, SVCreateTbReq* pReq); -// int tmsgSVCreateTbReqDecode(SMsgDecoder* pCoder, SVCreateTbReq* pReq); int tSerializeSVCreateTbReq(void** buf, SVCreateTbReq* pReq); void* tDeserializeSVCreateTbReq(void* buf, SVCreateTbReq* pReq); int tSVCreateTbBatchReqSerialize(void** buf, SVCreateTbBatchReq* pReq); From eaca069a45b8b9ff4b98245f91a58edfeca5d16c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 2 Jan 2022 10:34:31 +0000 Subject: [PATCH 19/30] more --- include/util/encode.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/util/encode.h b/include/util/encode.h index f5c8db0b5a..02edbe888e 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -125,6 +125,7 @@ static int tDecodeDouble(SCoder* pDecoder, double* val); static int tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len); static int tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len); static int tDecodeCStr(SCoder* pDecoder, const char** val); +static int tDecodeCStrTo(SCoder* pDecoder, char* val); /* ------------------------ IMPL ------------------------ */ #define TD_ENCODE_MACRO(CODER, VAL, TYPE, BITS) \ @@ -385,6 +386,15 @@ static FORCE_INLINE int tDecodeCStr(SCoder* pDecoder, const char** val) { return tDecodeCStrAndLen(pDecoder, val, &len); } +static int tDecodeCStrTo(SCoder* pDecoder, char* val) { + const char* pStr; + uint64_t len; + if (tDecodeCStrAndLen(pDecoder, &pStr, &len) < 0) return -1; + + memcpy(val, pStr, len+1); + return 0; +} + #ifdef __cplusplus } #endif From 62b60991f39b92b6b86f64ca90839b6819d818b2 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 3 Jan 2022 06:04:15 +0000 Subject: [PATCH 20/30] add encode test --- source/util/test/CMakeLists.txt | 4 + source/util/test/encodeTest.cpp | 166 ++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 source/util/test/encodeTest.cpp diff --git a/source/util/test/CMakeLists.txt b/source/util/test/CMakeLists.txt index bfc3906f79..4b6311022d 100644 --- a/source/util/test/CMakeLists.txt +++ b/source/util/test/CMakeLists.txt @@ -41,4 +41,8 @@ target_sources(freelistTest ) target_link_libraries(freelistTest os util gtest gtest_main) +# encodeTest +add_executable(encodeTest "encodeTest.cpp") +target_link_libraries(encodeTest os util gtest gtest_main) + diff --git a/source/util/test/encodeTest.cpp b/source/util/test/encodeTest.cpp new file mode 100644 index 0000000000..e89c84d8d2 --- /dev/null +++ b/source/util/test/encodeTest.cpp @@ -0,0 +1,166 @@ +#include + +#include "gtest/gtest.h" + +#include "encode.h" + +#define BUF_SIZE 64 +td_endian_t endian_arr[2] = {TD_LITTLE_ENDIAN, TD_BIG_ENDIAN}; + +static int encode(SCoder *pCoder, int8_t val) { return tEncodeI8(pCoder, val); } +static int encode(SCoder *pCoder, uint8_t val) { return tEncodeU8(pCoder, val); } +static int encode(SCoder *pCoder, int16_t val) { return tEncodeI16(pCoder, val); } +static int encode(SCoder *pCoder, uint16_t val) { return tEncodeU16(pCoder, val); } +static int encode(SCoder *pCoder, int32_t val) { return tEncodeI32(pCoder, val); } +static int encode(SCoder *pCoder, uint32_t val) { return tEncodeU32(pCoder, val); } +static int encode(SCoder *pCoder, int64_t val) { return tEncodeI64(pCoder, val); } +static int encode(SCoder *pCoder, uint64_t val) { return tEncodeU64(pCoder, val); } + +static int decode(SCoder *pCoder, int8_t *val) { return tDecodeI8(pCoder, val); } +static int decode(SCoder *pCoder, uint8_t *val) { return tDecodeU8(pCoder, val); } +static int decode(SCoder *pCoder, int16_t *val) { return tDecodeI16(pCoder, val); } +static int decode(SCoder *pCoder, uint16_t *val) { return tDecodeU16(pCoder, val); } +static int decode(SCoder *pCoder, int32_t *val) { return tDecodeI32(pCoder, val); } +static int decode(SCoder *pCoder, uint32_t *val) { return tDecodeU32(pCoder, val); } +static int decode(SCoder *pCoder, int64_t *val) { return tDecodeI64(pCoder, val); } +static int decode(SCoder *pCoder, uint64_t *val) { return tDecodeU64(pCoder, val); } + +static int encodev(SCoder *pCoder, int8_t val) { return tEncodeI8(pCoder, val); } +static int encodev(SCoder *pCoder, uint8_t val) { return tEncodeU8(pCoder, val); } +static int encodev(SCoder *pCoder, int16_t val) { return tEncodeI16v(pCoder, val); } +static int encodev(SCoder *pCoder, uint16_t val) { return tEncodeU16v(pCoder, val); } +static int encodev(SCoder *pCoder, int32_t val) { return tEncodeI32v(pCoder, val); } +static int encodev(SCoder *pCoder, uint32_t val) { return tEncodeU32v(pCoder, val); } +static int encodev(SCoder *pCoder, int64_t val) { return tEncodeI64v(pCoder, val); } +static int encodev(SCoder *pCoder, uint64_t val) { return tEncodeU64v(pCoder, val); } + +static int decodev(SCoder *pCoder, int8_t *val) { return tDecodeI8(pCoder, val); } +static int decodev(SCoder *pCoder, uint8_t *val) { return tDecodeU8(pCoder, val); } +static int decodev(SCoder *pCoder, int16_t *val) { return tDecodeI16v(pCoder, val); } +static int decodev(SCoder *pCoder, uint16_t *val) { return tDecodeU16v(pCoder, val); } +static int decodev(SCoder *pCoder, int32_t *val) { return tDecodeI32v(pCoder, val); } +static int decodev(SCoder *pCoder, uint32_t *val) { return tDecodeU32v(pCoder, val); } +static int decodev(SCoder *pCoder, int64_t *val) { return tDecodeI64v(pCoder, val); } +static int decodev(SCoder *pCoder, uint64_t *val) { return tDecodeU64v(pCoder, val); } + +template +static void simple_encode_decode_func(bool var_len) { + uint8_t buf[BUF_SIZE]; + SCoder coder; + T min_val, max_val; + T step = 1; + + if (typeid(T) == typeid(int8_t)) { + min_val = INT8_MIN; + max_val = INT8_MAX; + step = 1; + } else if (typeid(T) == typeid(uint8_t)) { + min_val = 0; + max_val = UINT8_MAX; + step = 1; + } else if (typeid(T) == typeid(int16_t)) { + min_val = INT16_MIN; + max_val = INT16_MAX; + step = 1; + } else if (typeid(T) == typeid(uint16_t)) { + min_val = 0; + max_val = UINT16_MAX; + step = 1; + } else if (typeid(T) == typeid(int32_t)) { + min_val = INT32_MIN; + max_val = INT32_MAX; + step = ((T)1) << 16; + } else if (typeid(T) == typeid(uint32_t)) { + min_val = 0; + max_val = UINT32_MAX; + step = ((T)1) << 16; + } else if (typeid(T) == typeid(int64_t)) { + min_val = INT64_MIN; + max_val = INT64_MAX; + step = ((T)1) << 48; + } else if (typeid(T) == typeid(uint64_t)) { + min_val = 0; + max_val = UINT64_MAX; + step = ((T)1) << 48; + } + + T i = min_val; + for (;; /*T i = min_val; i <= max_val; i += step*/) { + T dval; + + // Encode NULL + for (td_endian_t endian : endian_arr) { + tCoderInit(&coder, endian, NULL, 0, TD_ENCODER); + + if (var_len) { + GTEST_ASSERT_EQ(encodev(&coder, i), 0); + } else { + GTEST_ASSERT_EQ(encode(&coder, i), 0); + GTEST_ASSERT_EQ(coder.pos, sizeof(T)); + } + + tCoderClear(&coder); + } + + // Encode and decode + for (td_endian_t e_endian : endian_arr) { + for (td_endian_t d_endian : endian_arr) { + // Encode + tCoderInit(&coder, e_endian, buf, BUF_SIZE, TD_ENCODER); + + if (var_len) { + GTEST_ASSERT_EQ(encodev(&coder, i), 0); + } else { + GTEST_ASSERT_EQ(encode(&coder, i), 0); + GTEST_ASSERT_EQ(coder.pos, sizeof(T)); + } + + tCoderClear(&coder); + // Decode + tCoderInit(&coder, d_endian, buf, BUF_SIZE, TD_DECODER); + + if (var_len) { + GTEST_ASSERT_EQ(decodev(&coder, &dval), 0); + } else { + GTEST_ASSERT_EQ(decode(&coder, &dval), 0); + GTEST_ASSERT_EQ(coder.pos, sizeof(T)); + } + + if (typeid(T) == typeid(int8_t) || typeid(T) == typeid(uint8_t) || e_endian == d_endian) { + GTEST_ASSERT_EQ(i, dval); + } + + tCoderClear(&coder); + } + } + + if (i == max_val) break; + + if (max_val - i < step) { + i = max_val; + } else { + i = i + step; + } + } +} + +TEST(td_encode_test, simple_encode_and_decode_test) { + uint8_t buf[10]; + SCoder encoder, decoder; + + simple_encode_decode_func(false); + simple_encode_decode_func(false); + simple_encode_decode_func(false); + simple_encode_decode_func(false); + simple_encode_decode_func(false); + simple_encode_decode_func(false); + simple_encode_decode_func(false); + simple_encode_decode_func(false); + + simple_encode_decode_func(true); + simple_encode_decode_func(true); + simple_encode_decode_func(true); + simple_encode_decode_func(true); + simple_encode_decode_func(true); + simple_encode_decode_func(true); +} \ No newline at end of file From ad3374477197c399db8d12df658a4d0417862c76 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 3 Jan 2022 06:15:52 +0000 Subject: [PATCH 21/30] Fix variant encoding bug --- include/util/encode.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/util/encode.h b/include/util/encode.h index 02edbe888e..42548c03e3 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -141,7 +141,7 @@ static int tDecodeCStrTo(SCoder* pDecoder, char* val); return 0; #define TD_ENCODE_VARIANT_MACRO(CODER, VAL) \ - while ((VAL) > ENCODE_LIMIT) { \ + while ((VAL) >= ENCODE_LIMIT) { \ if ((CODER)->data) { \ if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) return -1; \ TD_CODER_CURRENT(CODER)[0] = ((VAL) | ENCODE_LIMIT) & 0xff; \ @@ -391,7 +391,7 @@ static int tDecodeCStrTo(SCoder* pDecoder, char* val) { uint64_t len; if (tDecodeCStrAndLen(pDecoder, &pStr, &len) < 0) return -1; - memcpy(val, pStr, len+1); + memcpy(val, pStr, len + 1); return 0; } From 99510b689c3e7f1c8e7bda3bd8d75ab745aeabf9 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 3 Jan 2022 06:18:07 +0000 Subject: [PATCH 22/30] add more check --- source/util/test/encodeTest.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/util/test/encodeTest.cpp b/source/util/test/encodeTest.cpp index e89c84d8d2..0e276e4338 100644 --- a/source/util/test/encodeTest.cpp +++ b/source/util/test/encodeTest.cpp @@ -115,6 +115,8 @@ static void simple_encode_decode_func(bool var_len) { GTEST_ASSERT_EQ(coder.pos, sizeof(T)); } + int32_t epos = coder.pos; + tCoderClear(&coder); // Decode tCoderInit(&coder, d_endian, buf, BUF_SIZE, TD_DECODER); @@ -126,6 +128,8 @@ static void simple_encode_decode_func(bool var_len) { GTEST_ASSERT_EQ(coder.pos, sizeof(T)); } + GTEST_ASSERT_EQ(coder.pos, epos); + if (typeid(T) == typeid(int8_t) || typeid(T) == typeid(uint8_t) || e_endian == d_endian) { GTEST_ASSERT_EQ(i, dval); } From 8aaf22ae7549b1f306d6fb53f63f877174896c42 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 3 Jan 2022 06:21:33 +0000 Subject: [PATCH 23/30] more --- source/util/test/encodeTest.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/source/util/test/encodeTest.cpp b/source/util/test/encodeTest.cpp index 0e276e4338..59a37386d3 100644 --- a/source/util/test/encodeTest.cpp +++ b/source/util/test/encodeTest.cpp @@ -148,10 +148,7 @@ static void simple_encode_decode_func(bool var_len) { } } -TEST(td_encode_test, simple_encode_and_decode_test) { - uint8_t buf[10]; - SCoder encoder, decoder; - +TEST(td_encode_test, encode_decode_fixed_len_integer) { simple_encode_decode_func(false); simple_encode_decode_func(false); simple_encode_decode_func(false); @@ -160,7 +157,9 @@ TEST(td_encode_test, simple_encode_and_decode_test) { simple_encode_decode_func(false); simple_encode_decode_func(false); simple_encode_decode_func(false); +} +TEST(td_encode_test, encode_decode_variant_len_integer) { simple_encode_decode_func(true); simple_encode_decode_func(true); simple_encode_decode_func(true); From 7b3a04c4dd196e896ab66ccbf70a72e736e87e98 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 3 Jan 2022 06:34:54 +0000 Subject: [PATCH 24/30] more --- include/util/encode.h | 8 ++++---- source/util/test/encodeTest.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/include/util/encode.h b/include/util/encode.h index 42548c03e3..31d386c450 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -100,7 +100,7 @@ static int tEncodeI64v(SCoder* pEncoder, int64_t val); static int tEncodeFloat(SCoder* pEncoder, float val); static int tEncodeDouble(SCoder* pEncoder, double val); static int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len); -static int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, uint64_t len); +static int tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len); static int tEncodeCStr(SCoder* pEncoder, const char* val); /* ------------------------ DECODE ------------------------ */ @@ -141,7 +141,7 @@ static int tDecodeCStrTo(SCoder* pDecoder, char* val); return 0; #define TD_ENCODE_VARIANT_MACRO(CODER, VAL) \ - while ((VAL) >= ENCODE_LIMIT) { \ + while ((VAL) >= ENCODE_LIMIT) { \ if ((CODER)->data) { \ if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) return -1; \ TD_CODER_CURRENT(CODER)[0] = ((VAL) | ENCODE_LIMIT) & 0xff; \ @@ -261,12 +261,12 @@ static FORCE_INLINE int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_ return 0; } -static FORCE_INLINE int tEncodeCstrWithLen(SCoder* pEncoder, const char* val, uint64_t len) { +static FORCE_INLINE int tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len) { return tEncodeBinary(pEncoder, (void*)val, len + 1); } static FORCE_INLINE int tEncodeCStr(SCoder* pEncoder, const char* val) { - return tEncodeCstrWithLen(pEncoder, val, (uint64_t)strlen(val)); + return tEncodeCStrWithLen(pEncoder, val, (uint64_t)strlen(val)); } /* ------------------------ FOR DECODER ------------------------ */ diff --git a/source/util/test/encodeTest.cpp b/source/util/test/encodeTest.cpp index 59a37386d3..dc10cec443 100644 --- a/source/util/test/encodeTest.cpp +++ b/source/util/test/encodeTest.cpp @@ -166,4 +166,36 @@ TEST(td_encode_test, encode_decode_variant_len_integer) { simple_encode_decode_func(true); simple_encode_decode_func(true); simple_encode_decode_func(true); +} + +TEST(td_encode_test, encode_decode_cstr) { + uint8_t * buf = new uint8_t[1024 * 1024]; + char * cstr = new char[1024 * 1024]; + const char *dcstr; + SCoder encoder; + SCoder decoder; + + for (size_t i = 0; i < 1024 * 2 - 1; i++) { + memset(cstr, 'a', i); + cstr[i] = '\0'; + for (td_endian_t endian : endian_arr) { + // Encode + tCoderInit(&encoder, endian, buf, 1024 * 1024, TD_ENCODER); + + GTEST_ASSERT_EQ(tEncodeCStr(&encoder, cstr), 0); + + tCoderClear(&encoder); + + // Decode + tCoderInit(&decoder, endian, buf, 1024 * 1024, TD_DECODER); + + GTEST_ASSERT_EQ(tDecodeCStr(&decoder, &dcstr), 0); + GTEST_ASSERT_EQ(memcmp(dcstr, cstr, i + 1), 0); + + tCoderClear(&decoder); + } + } + + delete buf; + delete cstr; } \ No newline at end of file From 086a2202ce9c807f776bf4cc8e2f9385760ceba2 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 3 Jan 2022 08:45:31 +0000 Subject: [PATCH 25/30] more --- include/util/encode.h | 49 ++++---- source/util/src/encode.c | 21 ++-- source/util/test/encodeTest.cpp | 199 ++++++++++++++++++++++++++++++++ 3 files changed, 237 insertions(+), 32 deletions(-) diff --git a/include/util/encode.h b/include/util/encode.h index 31d386c450..ba63759737 100644 --- a/include/util/encode.h +++ b/include/util/encode.h @@ -81,31 +81,32 @@ void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size, void tCoderClear(SCoder* pCoder); /* ------------------------ ENCODE ------------------------ */ -static int tStartEncode(SCoder* pEncoder); -static void tEndEncode(SCoder* pEncoder); -static int tEncodeU8(SCoder* pEncoder, uint8_t val); -static int tEncodeI8(SCoder* pEncoder, int8_t val); -static int tEncodeU16(SCoder* pEncoder, uint16_t val); -static int tEncodeI16(SCoder* pEncoder, int16_t val); -static int tEncodeU32(SCoder* pEncoder, uint32_t val); -static int tEncodeI32(SCoder* pEncoder, int32_t val); -static int tEncodeU64(SCoder* pEncoder, uint64_t val); -static int tEncodeI64(SCoder* pEncoder, int64_t val); -static int tEncodeU16v(SCoder* pEncoder, uint16_t val); -static int tEncodeI16v(SCoder* pEncoder, int16_t val); -static int tEncodeU32v(SCoder* pEncoder, uint32_t val); -static int tEncodeI32v(SCoder* pEncoder, int32_t val); -static int tEncodeU64v(SCoder* pEncoder, uint64_t val); -static int tEncodeI64v(SCoder* pEncoder, int64_t val); -static int tEncodeFloat(SCoder* pEncoder, float val); -static int tEncodeDouble(SCoder* pEncoder, double val); -static int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len); -static int tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len); -static int tEncodeCStr(SCoder* pEncoder, const char* val); +int tStartEncode(SCoder* pEncoder); +void tEndEncode(SCoder* pEncoder); +static int tEncodeU8(SCoder* pEncoder, uint8_t val); +static int tEncodeI8(SCoder* pEncoder, int8_t val); +static int tEncodeU16(SCoder* pEncoder, uint16_t val); +static int tEncodeI16(SCoder* pEncoder, int16_t val); +static int tEncodeU32(SCoder* pEncoder, uint32_t val); +static int tEncodeI32(SCoder* pEncoder, int32_t val); +static int tEncodeU64(SCoder* pEncoder, uint64_t val); +static int tEncodeI64(SCoder* pEncoder, int64_t val); +static int tEncodeU16v(SCoder* pEncoder, uint16_t val); +static int tEncodeI16v(SCoder* pEncoder, int16_t val); +static int tEncodeU32v(SCoder* pEncoder, uint32_t val); +static int tEncodeI32v(SCoder* pEncoder, int32_t val); +static int tEncodeU64v(SCoder* pEncoder, uint64_t val); +static int tEncodeI64v(SCoder* pEncoder, int64_t val); +static int tEncodeFloat(SCoder* pEncoder, float val); +static int tEncodeDouble(SCoder* pEncoder, double val); +static int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len); +static int tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len); +static int tEncodeCStr(SCoder* pEncoder, const char* val); /* ------------------------ DECODE ------------------------ */ -static int tStartDecode(SCoder* pDecoder); -static void tEndDecode(SCoder* pDecoder); +int tStartDecode(SCoder* pDecoder); +void tEndDecode(SCoder* pDecoder); +static bool tDecodeIsEnd(SCoder* pCoder); static int tDecodeU8(SCoder* pDecoder, uint8_t* val); static int tDecodeI8(SCoder* pDecoder, int8_t* val); static int tDecodeU16(SCoder* pDecoder, uint16_t* val); @@ -395,6 +396,8 @@ static int tDecodeCStrTo(SCoder* pDecoder, char* val) { return 0; } +static FORCE_INLINE bool tDecodeIsEnd(SCoder* pCoder) { return (pCoder->size == pCoder->pos); } + #ifdef __cplusplus } #endif diff --git a/source/util/src/encode.c b/source/util/src/encode.c index a3946daf5c..24d033f697 100644 --- a/source/util/src/encode.c +++ b/source/util/src/encode.c @@ -47,8 +47,6 @@ void tCoderClear(SCoder* pCoder) { } } -bool tDecodeIsEnd(SCoder* pCoder) { return (pCoder->size == pCoder->pos); } - int tStartEncode(SCoder* pCoder) { struct SCoderNode* pNode; @@ -62,7 +60,7 @@ int tStartEncode(SCoder* pCoder) { pNode->pos = pCoder->pos; pNode->size = pCoder->size; - pCoder->data = pNode->data + sizeof(int32_t); + pCoder->data = pNode->data + pNode->pos + sizeof(int32_t); pCoder->pos = 0; pCoder->size = pNode->size - pNode->pos - sizeof(int32_t); @@ -75,27 +73,32 @@ int tStartEncode(SCoder* pCoder) { void tEndEncode(SCoder* pCoder) { struct SCoderNode* pNode; + int32_t len; if (pCoder->data) { pNode = TD_SLIST_HEAD(&(pCoder->stack)); ASSERT(pNode); TD_SLIST_POP(&(pCoder->stack)); - // TODO: tEncodeI32(pNode, pCoder->pos); + len = pCoder->pos; pCoder->data = pNode->data; pCoder->size = pNode->size; - pCoder->pos = pNode->pos + pCoder->pos; + pCoder->pos = pNode->pos; + + tEncodeI32(pCoder, len); + + TD_CODER_MOVE_POS(pCoder, len); free(pNode); } } int tStartDecode(SCoder* pCoder) { - int32_t size; + int32_t len; struct SCoderNode* pNode; - // TODO: if (tDecodeI32(pCoder, &size) < 0) return -1; + if (tDecodeI32(pCoder, &len) < 0) return -1; pNode = malloc(sizeof(*pNode)); if (pNode == NULL) return -1; @@ -104,8 +107,8 @@ int tStartDecode(SCoder* pCoder) { pNode->pos = pCoder->pos; pNode->size = pCoder->size; - pCoder->data = pCoder->data; - pCoder->size = size; + pCoder->data = pNode->data + pNode->pos; + pCoder->size = len; pCoder->pos = 0; TD_SLIST_PUSH(&(pCoder->stack), pNode); diff --git a/source/util/test/encodeTest.cpp b/source/util/test/encodeTest.cpp index dc10cec443..b0e970eb07 100644 --- a/source/util/test/encodeTest.cpp +++ b/source/util/test/encodeTest.cpp @@ -198,4 +198,203 @@ TEST(td_encode_test, encode_decode_cstr) { delete buf; delete cstr; +} + +typedef struct { + int32_t A_a; + int64_t A_b; + char * A_c; +} SStructA_v1; + +static int tSStructA_v1_encode(SCoder *pCoder, const SStructA_v1 *pSAV1) { + if (tStartEncode(pCoder) < 0) return -1; + + if (tEncodeI32(pCoder, pSAV1->A_a) < 0) return -1; + if (tEncodeI64(pCoder, pSAV1->A_b) < 0) return -1; + if (tEncodeCStr(pCoder, pSAV1->A_c) < 0) return -1; + + tEndEncode(pCoder); + return 0; +} + +static int tSStructA_v1_decode(SCoder *pCoder, SStructA_v1 *pSAV1) { + if (tStartDecode(pCoder) < 0) return -1; + + if (tDecodeI32(pCoder, &pSAV1->A_a) < 0) return -1; + if (tDecodeI64(pCoder, &pSAV1->A_b) < 0) return -1; + const char *tstr; + uint64_t len; + if (tDecodeCStrAndLen(pCoder, &tstr, &len) < 0) return -1; + pSAV1->A_c = (char *)TCODER_MALLOC(len + 1, pCoder); + memcpy(pSAV1->A_c, tstr, len + 1); + + tEndDecode(pCoder); + return 0; +} + +typedef struct { + int32_t A_a; + int64_t A_b; + char * A_c; + // -------------------BELOW FEILDS ARE ADDED IN A NEW VERSION-------------- + int16_t A_d; + int16_t A_e; +} SStructA_v2; + +static int tSStructA_v2_encode(SCoder *pCoder, const SStructA_v2 *pSAV2) { + if (tStartEncode(pCoder) < 0) return -1; + + if (tEncodeI32(pCoder, pSAV2->A_a) < 0) return -1; + if (tEncodeI64(pCoder, pSAV2->A_b) < 0) return -1; + if (tEncodeCStr(pCoder, pSAV2->A_c) < 0) return -1; + + // ------------------------NEW FIELDS ENCODE------------------------------- + if (tEncodeI16(pCoder, pSAV2->A_d) < 0) return -1; + if (tEncodeI16(pCoder, pSAV2->A_e) < 0) return -1; + + tEndEncode(pCoder); + return 0; +} + +static int tSStructA_v2_decode(SCoder *pCoder, SStructA_v2 *pSAV2) { + if (tStartDecode(pCoder) < 0) return -1; + + if (tDecodeI32(pCoder, &pSAV2->A_a) < 0) return -1; + if (tDecodeI64(pCoder, &pSAV2->A_b) < 0) return -1; + const char *tstr; + uint64_t len; + if (tDecodeCStrAndLen(pCoder, &tstr, &len) < 0) return -1; + pSAV2->A_c = (char *)TCODER_MALLOC(len + 1, pCoder); + memcpy(pSAV2->A_c, tstr, len + 1); + + // ------------------------NEW FIELDS DECODE------------------------------- + if (!tDecodeIsEnd(pCoder)) { + if (tDecodeI16(pCoder, &pSAV2->A_d) < 0) return -1; + if (tDecodeI16(pCoder, &pSAV2->A_e) < 0) return -1; + } else { + pSAV2->A_d = 0; + pSAV2->A_e = 0; + } + + tEndDecode(pCoder); + return 0; +} + +typedef struct { + SStructA_v1 *pA; + int32_t v_a; + int8_t v_b; +} SFinalReq_v1; + +static int tSFinalReq_v1_encode(SCoder *pCoder, const SFinalReq_v1 *ps1) { + if (tStartEncode(pCoder) < 0) return -1; + + if (tSStructA_v1_encode(pCoder, ps1->pA) < 0) return -1; + if (tEncodeI32(pCoder, ps1->v_a) < 0) return -1; + if (tEncodeI8(pCoder, ps1->v_b) < 0) return -1; + + tEndEncode(pCoder); + return 0; +} + +static int tSFinalReq_v1_decode(SCoder *pCoder, SFinalReq_v1 *ps1) { + if (tStartDecode(pCoder) < 0) return -1; + + ps1->pA = (SStructA_v1 *)TCODER_MALLOC(sizeof(*(ps1->pA)), pCoder); + if (tSStructA_v1_decode(pCoder, ps1->pA) < 0) return -1; + if (tDecodeI32(pCoder, &ps1->v_a) < 0) return -1; + if (tDecodeI8(pCoder, &ps1->v_b) < 0) return -1; + + tEndEncode(pCoder); + return 0; +} + +typedef struct { + SStructA_v2 *pA; + int32_t v_a; + int8_t v_b; + // ----------------------- Feilds added ----------------------- + int16_t v_c; +} SFinalReq_v2; + +static int tSFinalReq_v2_encode(SCoder *pCoder, const SFinalReq_v2 *ps2) { + if (tStartEncode(pCoder) < 0) return -1; + + if (tSStructA_v2_encode(pCoder, ps2->pA) < 0) return -1; + if (tEncodeI32(pCoder, ps2->v_a) < 0) return -1; + if (tEncodeI8(pCoder, ps2->v_b) < 0) return -1; + + // ----------------------- Feilds added encode ----------------------- + if (tEncodeI16(pCoder, ps2->v_c) < 0) return -1; + + tEndEncode(pCoder); + return 0; +} + +static int tSFinalReq_v2_decode(SCoder *pCoder, SFinalReq_v2 *ps2) { + if (tStartDecode(pCoder) < 0) return -1; + + ps2->pA = (SStructA_v2 *)TCODER_MALLOC(sizeof(*(ps2->pA)), pCoder); + if (tSStructA_v2_decode(pCoder, ps2->pA) < 0) return -1; + if (tDecodeI32(pCoder, &ps2->v_a) < 0) return -1; + if (tDecodeI8(pCoder, &ps2->v_b) < 0) return -1; + + // ----------------------- Feilds added decode ----------------------- + if (tDecodeIsEnd(pCoder)) { + ps2->v_c = 0; + } else { + if (tDecodeI16(pCoder, &ps2->v_c) < 0) return -1; + } + + tEndEncode(pCoder); + return 0; +} + +TEST(td_encode_test, compound_struct_encode_test) { + SCoder encoder, decoder; + uint8_t * buf1; + int32_t buf1size; + uint8_t * buf2; + int32_t buf2size; + SStructA_v1 sa1 = {.A_a = 10, .A_b = 65478, .A_c = "Hello"}; + SStructA_v2 sa2 = {.A_a = 10, .A_b = 65478, .A_c = "Hello", .A_d = 67, .A_e = 13}; + SFinalReq_v1 req1 = {.pA = &sa1, .v_a = 15, .v_b = 35}; + SFinalReq_v2 req2 = {.pA = &sa2, .v_a = 15, .v_b = 32, .v_c = 37}; + SFinalReq_v1 dreq1; + SFinalReq_v2 dreq21, dreq22; + + // Get size + tCoderInit(&encoder, TD_LITTLE_ENDIAN, nullptr, 0, TD_ENCODER); + GTEST_ASSERT_EQ(tSFinalReq_v1_encode(&encoder, &req1), 0); + buf1size = encoder.pos; + buf1 = new uint8_t[encoder.pos]; + tCoderClear(&encoder); + + tCoderInit(&encoder, TD_LITTLE_ENDIAN, nullptr, 0, TD_ENCODER); + GTEST_ASSERT_EQ(tSFinalReq_v2_encode(&encoder, &req2), 0); + buf2size = encoder.pos; + buf2 = new uint8_t[encoder.pos]; + tCoderClear(&encoder); + + // Encode + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf1, buf1size, TD_ENCODER); + GTEST_ASSERT_EQ(tSFinalReq_v1_encode(&encoder, &req1), 0); + tCoderClear(&encoder); + + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf2, buf2size, TD_ENCODER); + GTEST_ASSERT_EQ(tSFinalReq_v2_encode(&encoder, &req2), 0); + tCoderClear(&encoder); + + // Decode + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf1, buf1size, TD_DECODER); + GTEST_ASSERT_EQ(tSFinalReq_v1_decode(&decoder, &dreq1), 0); + tCoderClear(&decoder); + + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf1, buf1size, TD_DECODER); + GTEST_ASSERT_EQ(tSFinalReq_v2_decode(&decoder, &dreq21), 0); + tCoderClear(&decoder); + + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf2, buf2size, TD_DECODER); + GTEST_ASSERT_EQ(tSFinalReq_v2_decode(&decoder, &dreq22), 0); + tCoderClear(&decoder); } \ No newline at end of file From 97d26ef10d28cc41620e0819d2902dc0d484a9f4 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 3 Jan 2022 09:34:20 +0000 Subject: [PATCH 26/30] fix bug --- source/util/src/encode.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/util/src/encode.c b/source/util/src/encode.c index 24d033f697..71985845af 100644 --- a/source/util/src/encode.c +++ b/source/util/src/encode.c @@ -86,7 +86,11 @@ void tEndEncode(SCoder* pCoder) { pCoder->size = pNode->size; pCoder->pos = pNode->pos; - tEncodeI32(pCoder, len); + if (TD_RT_ENDIAN() == pCoder->endian) { + tPut(int32_t, pCoder->data + pCoder->pos, len); + } else { + tRPut32(pCoder->data + pCoder->pos, len); + } TD_CODER_MOVE_POS(pCoder, len); From 1fde98815b40f4819ae89f2a3a8d98763e840a3e Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 3 Jan 2022 10:02:21 +0000 Subject: [PATCH 27/30] refact and fix some bugs --- source/util/src/encode.c | 10 +++++++--- source/util/test/encodeTest.cpp | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/source/util/src/encode.c b/source/util/src/encode.c index 71985845af..40c03ea051 100644 --- a/source/util/src/encode.c +++ b/source/util/src/encode.c @@ -50,6 +50,7 @@ void tCoderClear(SCoder* pCoder) { int tStartEncode(SCoder* pCoder) { struct SCoderNode* pNode; + ASSERT(pCoder->type == TD_ENCODER); if (pCoder->data) { if (pCoder->size - pCoder->pos < sizeof(int32_t)) return -1; @@ -75,6 +76,7 @@ void tEndEncode(SCoder* pCoder) { struct SCoderNode* pNode; int32_t len; + ASSERT(pCoder->type == TD_ENCODER); if (pCoder->data) { pNode = TD_SLIST_HEAD(&(pCoder->stack)); ASSERT(pNode); @@ -92,7 +94,7 @@ void tEndEncode(SCoder* pCoder) { tRPut32(pCoder->data + pCoder->pos, len); } - TD_CODER_MOVE_POS(pCoder, len); + TD_CODER_MOVE_POS(pCoder, len + sizeof(int32_t)); free(pNode); } @@ -102,6 +104,7 @@ int tStartDecode(SCoder* pCoder) { int32_t len; struct SCoderNode* pNode; + ASSERT(pCoder->type == TD_DECODER); if (tDecodeI32(pCoder, &len) < 0) return -1; pNode = malloc(sizeof(*pNode)); @@ -121,10 +124,11 @@ int tStartDecode(SCoder* pCoder) { } void tEndDecode(SCoder* pCoder) { - ASSERT(tDecodeIsEnd(pCoder)); - struct SCoderNode* pNode; + ASSERT(pCoder->type == TD_DECODER); + ASSERT(tDecodeIsEnd(pCoder)); + pNode = TD_SLIST_HEAD(&(pCoder->stack)); ASSERT(pNode); TD_SLIST_POP(&(pCoder->stack)); diff --git a/source/util/test/encodeTest.cpp b/source/util/test/encodeTest.cpp index b0e970eb07..ec4d9cd182 100644 --- a/source/util/test/encodeTest.cpp +++ b/source/util/test/encodeTest.cpp @@ -305,7 +305,7 @@ static int tSFinalReq_v1_decode(SCoder *pCoder, SFinalReq_v1 *ps1) { if (tDecodeI32(pCoder, &ps1->v_a) < 0) return -1; if (tDecodeI8(pCoder, &ps1->v_b) < 0) return -1; - tEndEncode(pCoder); + tEndDecode(pCoder); return 0; } @@ -346,7 +346,7 @@ static int tSFinalReq_v2_decode(SCoder *pCoder, SFinalReq_v2 *ps2) { if (tDecodeI16(pCoder, &ps2->v_c) < 0) return -1; } - tEndEncode(pCoder); + tEndDecode(pCoder); return 0; } From 395b2bbeab93586d1616778544e507cfa3810b61 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 3 Jan 2022 10:07:42 +0000 Subject: [PATCH 28/30] add more test --- source/util/test/encodeTest.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/source/util/test/encodeTest.cpp b/source/util/test/encodeTest.cpp index ec4d9cd182..1a861701d7 100644 --- a/source/util/test/encodeTest.cpp +++ b/source/util/test/encodeTest.cpp @@ -388,13 +388,34 @@ TEST(td_encode_test, compound_struct_encode_test) { // Decode tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf1, buf1size, TD_DECODER); GTEST_ASSERT_EQ(tSFinalReq_v1_decode(&decoder, &dreq1), 0); + GTEST_ASSERT_EQ(dreq1.pA->A_a, req1.pA->A_a); + GTEST_ASSERT_EQ(dreq1.pA->A_b, req1.pA->A_b); + GTEST_ASSERT_EQ(strcmp(dreq1.pA->A_c, req1.pA->A_c), 0); + GTEST_ASSERT_EQ(dreq1.v_a, req1.v_a); + GTEST_ASSERT_EQ(dreq1.v_b, req1.v_b); tCoderClear(&decoder); tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf1, buf1size, TD_DECODER); GTEST_ASSERT_EQ(tSFinalReq_v2_decode(&decoder, &dreq21), 0); + GTEST_ASSERT_EQ(dreq21.pA->A_a, req1.pA->A_a); + GTEST_ASSERT_EQ(dreq21.pA->A_b, req1.pA->A_b); + GTEST_ASSERT_EQ(strcmp(dreq21.pA->A_c, req1.pA->A_c), 0); + GTEST_ASSERT_EQ(dreq21.pA->A_d, 0); + GTEST_ASSERT_EQ(dreq21.pA->A_e, 0); + GTEST_ASSERT_EQ(dreq21.v_a, req1.v_a); + GTEST_ASSERT_EQ(dreq21.v_b, req1.v_b); + GTEST_ASSERT_EQ(dreq21.v_c, 0); tCoderClear(&decoder); tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf2, buf2size, TD_DECODER); GTEST_ASSERT_EQ(tSFinalReq_v2_decode(&decoder, &dreq22), 0); + GTEST_ASSERT_EQ(dreq22.pA->A_a, req2.pA->A_a); + GTEST_ASSERT_EQ(dreq22.pA->A_b, req2.pA->A_b); + GTEST_ASSERT_EQ(strcmp(dreq22.pA->A_c, req2.pA->A_c), 0); + GTEST_ASSERT_EQ(dreq22.pA->A_d, req2.pA->A_d); + GTEST_ASSERT_EQ(dreq22.pA->A_e, req2.pA->A_e); + GTEST_ASSERT_EQ(dreq22.v_a, req2.v_a); + GTEST_ASSERT_EQ(dreq22.v_b, req2.v_b); + GTEST_ASSERT_EQ(dreq22.v_c, req2.v_c); tCoderClear(&decoder); } \ No newline at end of file From 88ba5faec9ff06f6f492cec61b6301e164ce9dd2 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 3 Jan 2022 22:12:28 +0800 Subject: [PATCH 29/30] refactor code --- .../index/inc/index_fst_counting_writer.h | 1 + source/libs/index/src/index.c | 8 +- source/libs/index/src/index_cache.c | 2 +- .../index/src/index_fst_counting_writer.c | 7 ++ source/libs/index/src/index_tfile.c | 90 +++++++++++-------- source/libs/index/test/indexTests.cc | 35 +++++--- 6 files changed, 89 insertions(+), 54 deletions(-) diff --git a/source/libs/index/inc/index_fst_counting_writer.h b/source/libs/index/inc/index_fst_counting_writer.h index 1504a69a08..fcc0d5a0b3 100644 --- a/source/libs/index/inc/index_fst_counting_writer.h +++ b/source/libs/index/inc/index_fst_counting_writer.h @@ -38,6 +38,7 @@ typedef struct WriterCtx { int fd; bool readOnly; char buf[256]; + int size; } file; struct { int32_t capa; diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 9c7320b301..ff973dd7e3 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -135,7 +135,7 @@ int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { SIndexTerm* p = taosArrayGetP(fVals, i); char buf[128] = {0}; - ICacheKey key = {.suid = p->suid, .colName = p->colName}; + ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName)}; int32_t sz = indexSerialCacheKey(&key, buf); IndexCache** cache = taosHashGet(index->colObj, buf, sz); @@ -150,7 +150,7 @@ int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { SIndexTerm* p = taosArrayGetP(fVals, i); char buf[128] = {0}; - ICacheKey key = {.suid = p->suid, .colName = p->colName}; + ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName)}; int32_t sz = indexSerialCacheKey(&key, buf); IndexCache** cache = taosHashGet(index->colObj, buf, sz); @@ -212,7 +212,7 @@ int indexSearch(SIndex* index, SIndexMultiTermQuery* multiQuerys, SArray* result indexInterResultsDestroy(interResults); #endif - return 1; + return 0; } int indexDelete(SIndex* index, SIndexMultiTermQuery* query) { @@ -310,7 +310,7 @@ static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* query, SArray** result pthread_mutex_lock(&sIdx->mtx); char buf[128] = {0}; - ICacheKey key = {.suid = term->suid, .colName = term->colName}; + ICacheKey key = {.suid = term->suid, .colName = term->colName, .nColName = strlen(term->colName)}; int32_t sz = indexSerialCacheKey(&key, buf); IndexCache** pCache = taosHashGet(sIdx->colObj, buf, sz); diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index b4c533e998..e95de9286e 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -20,7 +20,7 @@ #define MAX_INDEX_KEY_LEN 256 // test only, change later -#define MEM_TERM_LIMIT 10 * 10000 +#define MEM_TERM_LIMIT 5 * 10000 // ref index_cache.h:22 //#define CACHE_KEY_LEN(p) \ // (sizeof(int32_t) + sizeof(uint16_t) + sizeof(p->colType) + sizeof(p->nColVal) + p->nColVal + sizeof(uint64_t) + diff --git a/source/libs/index/src/index_fst_counting_writer.c b/source/libs/index/src/index_fst_counting_writer.c index 7906dfea11..fb730d5f70 100644 --- a/source/libs/index/src/index_fst_counting_writer.c +++ b/source/libs/index/src/index_fst_counting_writer.c @@ -72,9 +72,15 @@ WriterCtx* writerCtxCreate(WriterType type, const char* path, bool readOnly, int if (readOnly == false) { // ctx->file.fd = open(path, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO); ctx->file.fd = tfOpenCreateWriteAppend(path); + struct stat fstat; + stat(path, &fstat); + ctx->file.size = fstat.st_size; } else { // ctx->file.fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO); ctx->file.fd = tfOpenRead(path); + struct stat fstat; + stat(path, &fstat); + ctx->file.size = fstat.st_size; } memcpy(ctx->file.buf, path, strlen(path)); if (ctx->file.fd < 0) { @@ -104,6 +110,7 @@ void writerCtxDestroy(WriterCtx* ctx, bool remove) { free(ctx->mem.buf); } else { tfClose(ctx->file.fd); + ctx->flush(ctx); if (remove) { unlink(ctx->file.buf); } } free(ctx); diff --git a/source/libs/index/src/index_tfile.c b/source/libs/index/src/index_tfile.c index 95c713fb0a..9c4d1d1139 100644 --- a/source/libs/index/src/index_tfile.c +++ b/source/libs/index/src/index_tfile.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2019 TAOS Data, Inc. - * +p * * 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. @@ -45,12 +45,13 @@ static int tfileReaderLoadHeader(TFileReader* reader); static int tfileReaderLoadFst(TFileReader* reader); static int tfileReaderLoadTableIds(TFileReader* reader, int32_t offset, SArray* result); -static int tfileGetFileList(const char* path, SArray* result); -static int tfileRmExpireFile(SArray* result); -static void tfileDestroyFileName(void* elem); -static int tfileCompare(const void* a, const void* b); -static int tfileParseFileName(const char* filename, uint64_t* suid, int* colId, int* version); -static void tfileGenFileName(char* filename, uint64_t suid, int colId, int version); +static SArray* tfileGetFileList(const char* path); +static int tfileRmExpireFile(SArray* result); +static void tfileDestroyFileName(void* elem); +static int tfileCompare(const void* a, const void* b); +static int tfileParseFileName(const char* filename, uint64_t* suid, char* col, int* version); +static void tfileGenFileName(char* filename, uint64_t suid, const char* col, int version); +static void tfileGenFileFullName(char* fullname, const char* path, uint64_t suid, const char* col, int32_t version); TFileCache* tfileCacheCreate(const char* path) { TFileCache* tcache = calloc(1, sizeof(TFileCache)); @@ -59,21 +60,22 @@ TFileCache* tfileCacheCreate(const char* path) { tcache->tableCache = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); tcache->capacity = 64; - SArray* files = taosArrayInit(4, sizeof(void*)); - tfileGetFileList(path, files); - taosArraySort(files, tfileCompare); - tfileRmExpireFile(files); + SArray* files = tfileGetFileList(path); uint64_t suid; int32_t colId, version; for (size_t i = 0; i < taosArrayGetSize(files); i++) { char* file = taosArrayGetP(files, i); - if (0 != tfileParseFileName(file, &suid, (int*)&colId, (int*)&version)) { + char colName[256] = {0}; + if (0 != tfileParseFileName(file, &suid, colName, (int*)&version)) { indexInfo("try parse invalid file: %s, skip it", file); continue; } + // use version info later + char fullName[256] = {0}; + sprintf(fullName, "%s/%s", path, file); - WriterCtx* wc = writerCtxCreate(TFile, file, true, 1024 * 1024 * 64); + WriterCtx* wc = writerCtxCreate(TFile, fullName, true, 1024 * 1024 * 64); if (wc == NULL) { indexError("failed to open index:%s", file); goto End; @@ -200,12 +202,9 @@ int tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SArray* resul } TFileWriter* tfileWriterOpen(char* path, uint64_t suid, int32_t version, const char* colName, uint8_t colType) { - char filename[128] = {0}; - int32_t coldId = 1; - tfileGenFileName(filename, suid, coldId, version); - char fullname[256] = {0}; - snprintf(fullname, sizeof(fullname), "%s/%s", path, filename); + tfileGenFileFullName(fullname, path, suid, colName, version); + indexInfo("open write file name %s", fullname); WriterCtx* wcx = writerCtxCreate(TFile, fullname, false, 1024 * 1024 * 64); if (wcx == NULL) { return NULL; } @@ -218,13 +217,11 @@ TFileWriter* tfileWriterOpen(char* path, uint64_t suid, int32_t version, const c return tfileWriterCreate(wcx, &tfh); } TFileReader* tfileReaderOpen(char* path, uint64_t suid, int32_t version, const char* colName) { - char filename[128] = {0}; - int32_t coldId = 1; - tfileGenFileName(filename, suid, coldId, version); - char fullname[256] = {0}; - snprintf(fullname, sizeof(fullname), "%s/%s", path, filename); + tfileGenFileFullName(fullname, path, suid, colName, version); + WriterCtx* wc = writerCtxCreate(TFile, fullname, true, 1024 * 1024 * 1024); + indexInfo("open read file name:%s, size: %d", wc->file.buf, wc->file.size); if (wc == NULL) { return NULL; } TFileReader* reader = tfileReaderCreate(wc); @@ -324,7 +321,6 @@ int tfileWriterPut(TFileWriter* tw, void* data, bool order) { } // write data - indexError("--------Begin----------------"); for (size_t i = 0; i < sz; i++) { // TODO, fst batch write later TFileValue* v = taosArrayGetP((SArray*)data, i); @@ -332,11 +328,10 @@ int tfileWriterPut(TFileWriter* tw, void* data, bool order) { indexError("failed to write data: %s, offset: %d len: %d", v->colVal, v->offset, (int)taosArrayGetSize(v->tableId)); } else { - indexInfo("success to write data: %s, offset: %d len: %d", v->colVal, v->offset, - (int)taosArrayGetSize(v->tableId)); + // indexInfo("success to write data: %s, offset: %d len: %d", v->colVal, v->offset, + // (int)taosArrayGetSize(v->tableId)); } } - indexError("--------End----------------"); fstBuilderFinish(tw->fb); fstBuilderDestroy(tw->fb); tw->fb = NULL; @@ -550,6 +545,9 @@ static int tfileReaderLoadHeader(TFileReader* reader) { // indexError("actual Read: %d, to read: %d, errno: %d, filefd: %d, filename: %s", (int)(nread), (int)sizeof(buf), errno, reader->ctx->file.fd, reader->ctx->file.buf); + } else { + indexError("actual Read: %d, to read: %d, errno: %d, filefd: %d, filename: %s", (int)(nread), (int)sizeof(buf), + errno, reader->ctx->file.fd, reader->ctx->file.buf); } // assert(nread == sizeof(buf)); memcpy(&reader->header, buf, sizeof(buf)); @@ -558,13 +556,14 @@ static int tfileReaderLoadHeader(TFileReader* reader) { } static int tfileReaderLoadFst(TFileReader* reader) { // current load fst into memory, refactor it later - static int FST_MAX_SIZE = 64 * 1024; + static int FST_MAX_SIZE = 64 * 1024 * 1024; char* buf = calloc(1, sizeof(char) * FST_MAX_SIZE); if (buf == NULL) { return -1; } WriterCtx* ctx = reader->ctx; int32_t nread = ctx->readFrom(ctx, buf, FST_MAX_SIZE, reader->header.fstOffset); + indexError("nread = %d, and fst offset=%d, filename: %s ", nread, reader->header.fstOffset, ctx->file.buf); // we assuse fst size less than FST_MAX_SIZE assert(nread > 0 && nread < FST_MAX_SIZE); @@ -608,19 +607,26 @@ void tfileReaderUnRef(TFileReader* reader) { } } -static int tfileGetFileList(const char* path, SArray* result) { +static SArray* tfileGetFileList(const char* path) { + SArray* files = taosArrayInit(4, sizeof(void*)); + DIR* dir = opendir(path); - if (NULL == dir) { return -1; } + if (NULL == dir) { return NULL; } struct dirent* entry; while ((entry = readdir(dir)) != NULL) { + if (entry->d_type && DT_DIR) { continue; } size_t len = strlen(entry->d_name); char* buf = calloc(1, len + 1); memcpy(buf, entry->d_name, len); - taosArrayPush(result, &buf); + taosArrayPush(files, &buf); } closedir(dir); - return 0; + + taosArraySort(files, tfileCompare); + tfileRmExpireFile(files); + + return files; } static int tfileRmExpireFile(SArray* result) { // TODO(yihao): remove expire tindex after restart @@ -641,15 +647,21 @@ static int tfileCompare(const void* a, const void* b) { if (ret == 0) { return ret; } return ret < 0 ? -1 : 1; } -// tfile name suid-colId-version.tindex -static void tfileGenFileName(char* filename, uint64_t suid, int colId, int version) { - sprintf(filename, "%" PRIu64 "-%d-%d.tindex", suid, colId, version); - return; -} -static int tfileParseFileName(const char* filename, uint64_t* suid, int* colId, int* version) { - if (3 == sscanf(filename, "%" PRIu64 "-%d-%d.tindex", suid, colId, version)) { + +static int tfileParseFileName(const char* filename, uint64_t* suid, char* col, int* version) { + if (3 == sscanf(filename, "%" PRIu64 "-%[^-]-%d.tindex", suid, col, version)) { // read suid & colid & version success return 0; } return -1; } +// tfile name suid-colId-version.tindex +static void tfileGenFileName(char* filename, uint64_t suid, const char* col, int version) { + sprintf(filename, "%" PRIu64 "-%s-%d.tindex", suid, col, version); + return; +} +static void tfileGenFileFullName(char* fullname, const char* path, uint64_t suid, const char* col, int32_t version) { + char filename[128] = {0}; + tfileGenFileName(filename, suid, col, version); + sprintf(fullname, "%s/%s", path, filename); +} diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index bdfb86ce17..3a8a880b3b 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -13,6 +13,7 @@ * along with this program. If not, see . */ #include +#include #include #include #include @@ -638,7 +639,7 @@ class IndexObj { indexInit(); } int Init(const std::string& dir) { - taosRemoveDir(dir.c_str()); + // taosRemoveDir(dir.c_str()); taosMkDir(dir.c_str()); int ret = indexOpen(&opts, dir.c_str(), &idx); if (ret != 0) { @@ -663,10 +664,11 @@ class IndexObj { int WriteMultiMillonData(const std::string& colName, const std::string& colVal = "Hello world", size_t numOfTable = 100 * 10000) { std::string tColVal = colVal; + size_t colValSize = tColVal.size(); for (int i = 0; i < numOfTable; i++) { - tColVal[tColVal.size() - 1] = 'a' + i % 26; + tColVal[i % colValSize] = 'a' + i % 26; SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), - colVal.c_str(), colVal.size()); + tColVal.c_str(), tColVal.size()); SIndexMultiTerm* terms = indexMultiTermCreate(); indexMultiTermAdd(terms, term); for (size_t i = 0; i < 10; i++) { @@ -695,7 +697,13 @@ class IndexObj { indexMultiTermQueryAdd(mq, term, QUERY_TERM); SArray* result = (SArray*)taosArrayInit(1, sizeof(uint64_t)); - if (Search(mq, result) == 0) { std::cout << "search one successfully" << std::endl; } + + int64_t s = taosGetTimestampUs(); + if (Search(mq, result) == 0) { + int64_t e = taosGetTimestampUs(); + std::cout << "search one successfully and time cost:" << e - s << std::endl; + } else { + } int sz = taosArrayGetSize(result); indexMultiTermQueryDestroy(mq); taosArrayDestroy(result); @@ -810,12 +818,12 @@ TEST_F(IndexEnv2, testIndexOpen) { } TEST_F(IndexEnv2, testIndex_TrigeFlush) { - std::string path = "/tmp/test1"; + std::string path = "/tmp/testxxx"; if (index->Init(path) != 0) { // r std::cout << "failed to init" << std::endl; } - int numOfTable = 100 * 10000; + int numOfTable = 2 * 10000; index->WriteMillonData("tag1", "Hello Wolrd", numOfTable); int target = index->SearchOne("tag1", "Hello Wolrd"); std::cout << "Get Index: " << target << std::endl; @@ -826,6 +834,10 @@ static void write_and_search(IndexObj* idx) { std::string colName("tag1"), colVal("Hello"); int target = idx->SearchOne("tag1", "Hello"); + std::cout << "search: " << target << std::endl; + target = idx->SearchOne("tag2", "Test"); + std::cout << "search: " << target << std::endl; + idx->PutOne(colName, colVal); } TEST_F(IndexEnv2, testIndex_serarch_cache_and_tfile) { @@ -833,7 +845,10 @@ TEST_F(IndexEnv2, testIndex_serarch_cache_and_tfile) { if (index->Init(path) != 0) { // opt } - index->WriteMultiMillonData("tag1", "Hello", 200000); + index->PutOne("tag1", "Hello"); + index->PutOne("tag2", "Test"); + index->WriteMultiMillonData("tag1", "Hello", 5 * 10000); + index->WriteMultiMillonData("tag2", "Test", 5 * 10000); std::thread threads[NUM_OF_THREAD]; for (int i = 0; i < NUM_OF_THREAD; i++) { @@ -847,15 +862,15 @@ TEST_F(IndexEnv2, testIndex_serarch_cache_and_tfile) { } TEST_F(IndexEnv2, testIndex_restart) { - std::string path = "/tmp"; + std::string path = "/tmp/test1"; if (index->Init(path) != 0) {} } TEST_F(IndexEnv2, testIndex_performance) { - std::string path = "/tmp"; + std::string path = "/tmp/test2"; if (index->Init(path) != 0) {} } TEST_F(IndexEnv2, testIndexMultiTag) { - std::string path = "/tmp"; + std::string path = "/tmp/test3"; if (index->Init(path) != 0) {} } From c1a2366d673702e5939604b40e56df159f850efd Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 3 Jan 2022 22:28:01 +0800 Subject: [PATCH 30/30] refactor code --- source/libs/index/src/index.c | 3 +++ source/libs/index/src/index_fst_counting_writer.c | 2 ++ source/libs/index/src/index_tfile.c | 11 +++++++---- source/libs/index/test/indexTests.cc | 6 +++--- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index ff973dd7e3..0657c68458 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -73,6 +73,7 @@ int indexOpen(SIndexOpts* opts, const char* path, SIndex** index) { #ifdef USE_INVERTED_INDEX // sIdx->cache = (void*)indexCacheCreate(sIdx); sIdx->tindex = indexTFileCreate(path); + if (sIdx->tindex == NULL) { goto END; } sIdx->colObj = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); sIdx->cVersion = 1; sIdx->path = calloc(1, strlen(path) + 1); @@ -83,6 +84,8 @@ int indexOpen(SIndexOpts* opts, const char* path, SIndex** index) { return 0; #endif +END: + if (sIdx != NULL) { indexClose(sIdx); } *index = NULL; return -1; diff --git a/source/libs/index/src/index_fst_counting_writer.c b/source/libs/index/src/index_fst_counting_writer.c index fb730d5f70..0f29da1c27 100644 --- a/source/libs/index/src/index_fst_counting_writer.c +++ b/source/libs/index/src/index_fst_counting_writer.c @@ -72,12 +72,14 @@ WriterCtx* writerCtxCreate(WriterType type, const char* path, bool readOnly, int if (readOnly == false) { // ctx->file.fd = open(path, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO); ctx->file.fd = tfOpenCreateWriteAppend(path); + struct stat fstat; stat(path, &fstat); ctx->file.size = fstat.st_size; } else { // ctx->file.fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO); ctx->file.fd = tfOpenRead(path); + struct stat fstat; stat(path, &fstat); ctx->file.size = fstat.st_size; diff --git a/source/libs/index/src/index_tfile.c b/source/libs/index/src/index_tfile.c index 9c4d1d1139..d4d13ddf19 100644 --- a/source/libs/index/src/index_tfile.c +++ b/source/libs/index/src/index_tfile.c @@ -66,12 +66,14 @@ TFileCache* tfileCacheCreate(const char* path) { int32_t colId, version; for (size_t i = 0; i < taosArrayGetSize(files); i++) { char* file = taosArrayGetP(files, i); - char colName[256] = {0}; + + // refactor later, use colname and version info + char colName[256] = {0}; if (0 != tfileParseFileName(file, &suid, colName, (int*)&version)) { indexInfo("try parse invalid file: %s, skip it", file); continue; } - // use version info later + char fullName[256] = {0}; sprintf(fullName, "%s/%s", path, file); @@ -204,7 +206,7 @@ int tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SArray* resul TFileWriter* tfileWriterOpen(char* path, uint64_t suid, int32_t version, const char* colName, uint8_t colType) { char fullname[256] = {0}; tfileGenFileFullName(fullname, path, suid, colName, version); - indexInfo("open write file name %s", fullname); + // indexInfo("open write file name %s", fullname); WriterCtx* wcx = writerCtxCreate(TFile, fullname, false, 1024 * 1024 * 64); if (wcx == NULL) { return NULL; } @@ -221,7 +223,7 @@ TFileReader* tfileReaderOpen(char* path, uint64_t suid, int32_t version, const c tfileGenFileFullName(fullname, path, suid, colName, version); WriterCtx* wc = writerCtxCreate(TFile, fullname, true, 1024 * 1024 * 1024); - indexInfo("open read file name:%s, size: %d", wc->file.buf, wc->file.size); + // indexInfo("open read file name:%s, size: %d", wc->file.buf, wc->file.size); if (wc == NULL) { return NULL; } TFileReader* reader = tfileReaderCreate(wc); @@ -356,6 +358,7 @@ IndexTFile* indexTFileCreate(const char* path) { return tfile; } void indexTFileDestroy(IndexTFile* tfile) { + if (tfile == NULL) { return; } tfileCacheDestroy(tfile->cache); free(tfile); } diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index 3a8a880b3b..77b6e02f18 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -823,7 +823,7 @@ TEST_F(IndexEnv2, testIndex_TrigeFlush) { // r std::cout << "failed to init" << std::endl; } - int numOfTable = 2 * 10000; + int numOfTable = 100 * 10000; index->WriteMillonData("tag1", "Hello Wolrd", numOfTable); int target = index->SearchOne("tag1", "Hello Wolrd"); std::cout << "Get Index: " << target << std::endl; @@ -847,8 +847,8 @@ TEST_F(IndexEnv2, testIndex_serarch_cache_and_tfile) { } index->PutOne("tag1", "Hello"); index->PutOne("tag2", "Test"); - index->WriteMultiMillonData("tag1", "Hello", 5 * 10000); - index->WriteMultiMillonData("tag2", "Test", 5 * 10000); + index->WriteMultiMillonData("tag1", "Hello", 50 * 10000); + index->WriteMultiMillonData("tag2", "Test", 50 * 10000); std::thread threads[NUM_OF_THREAD]; for (int i = 0; i < NUM_OF_THREAD; i++) {