From db2bf567c2596ccae81d5d7b3ccfc4a29f3d1cb4 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 6 Dec 2021 18:43:48 +0800 Subject: [PATCH] refactor builder struct --- source/libs/index/inc/indexInt.h | 11 +++++++++++ source/libs/index/inc/index_fst_counting_writer.h | 2 +- source/libs/index/src/index_fst.c | 15 +++++++++++++-- source/libs/index/src/index_fst_counting_writer.c | 7 +++++-- source/libs/index/test/indexTests.cpp | 8 ++++---- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/source/libs/index/inc/indexInt.h b/source/libs/index/inc/indexInt.h index 742427bf94..a6862c05c8 100644 --- a/source/libs/index/inc/indexInt.h +++ b/source/libs/index/inc/indexInt.h @@ -17,6 +17,7 @@ #define _TD_INDEX_INT_H_ #include "index.h" +#include "tlog.h" #ifdef USE_LUCENE #include @@ -60,6 +61,16 @@ typedef struct SIndexTermQuery { SIndexTerm *indexTermCreate(const char *key, int32_t nKey, const char *val, int32_t nVal); void indexTermDestroy(SIndexTerm *p); + + +#define indexFatal(...) do { if (sDebugFlag & DEBUG_FATAL) { taosPrintLog("index FATAL ", 255, __VA_ARGS__); }} while(0) +#define indexError(...) do { if (sDebugFlag & DEBUG_ERROR) { taosPrintLog("index ERROR ", 255, __VA_ARGS__); }} while(0) +#define indexWarn(...) do { if (sDebugFlag & DEBUG_WARN) { taosPrintLog("index WARN ", 255, __VA_ARGS__); }} while(0) +#define indexInfo(...) do { if (sDebugFlag & DEBUG_INFO) { taosPrintLog("index ", 255, __VA_ARGS__); }} while(0) +#define indexDebug(...) do { if (sDebugFlag & DEBUG_DEBUG) { taosPrintLog("index ", sDebugFlag, __VA_ARGS__); }} while(0) +#define indexTrace(...) do { if (sDebugFlag & DEBUG_TRACE) { taosPrintLog("index ", sDebugFlag, __VA_ARGS__); }} while(0) + + #ifdef __cplusplus } #endif diff --git a/source/libs/index/inc/index_fst_counting_writer.h b/source/libs/index/inc/index_fst_counting_writer.h index 9280461780..86bc0d07d5 100644 --- a/source/libs/index/inc/index_fst_counting_writer.h +++ b/source/libs/index/inc/index_fst_counting_writer.h @@ -25,7 +25,7 @@ extern "C" { #define DefaultMem 1024*1024 -static char tmpFile[] = "/tmp/index"; +static char tmpFile[] = "./index"; typedef enum WriterType {TMemory, TFile} WriterType; typedef struct WriterCtx { diff --git a/source/libs/index/src/index_fst.c b/source/libs/index/src/index_fst.c index 4f6a5a236e..88fc0bd322 100644 --- a/source/libs/index/src/index_fst.c +++ b/source/libs/index/src/index_fst.c @@ -148,7 +148,7 @@ uint64_t fstUnFinishedNodesFindCommPrefixAndSetOutput(FstUnFinishedNodes *node, size_t lsz = (size_t)(s->end - s->start + 1); // data len size_t ssz = taosArrayGetSize(node->stack); // stack size - + *out = in; uint64_t i = 0; for (i = 0; i < lsz && i < ssz; i++) { FstBuilderNodeUnfinished *un = taosArrayGet(node->stack, i); @@ -776,6 +776,17 @@ FstBuilder *fstBuilderCreate(void *w, FstType ty) { b->last = fstSliceCreate(NULL, 0); b->lastAddr = NONE_ADDRESS; b->len = 0; + + char buf64[8] = {0}; + void *pBuf64 = buf64; + taosEncodeFixedU64(&pBuf64, VERSION); + fstCountingWriterWrite(b->wrt, buf64, sizeof(buf64)); + + memset(buf64, 0, sizeof(buf64)); + pBuf64 = buf64; + taosEncodeFixedU64(&pBuf64, ty); + fstCountingWriterWrite(b->wrt, buf64, sizeof(buf64)); + return b; } void fstBuilderDestroy(FstBuilder *b) { @@ -811,7 +822,7 @@ void fstBuilderInsertOutput(FstBuilder *b, FstSlice bs, Output in) { // prefixLen = fstUnFinishedNodesFindCommPrefix(b->unfinished, bs); // out = 0; //} - Output out; + Output out; uint64_t prefixLen = fstUnFinishedNodesFindCommPrefixAndSetOutput(b->unfinished, bs, in, &out); if (prefixLen == FST_SLICE_LEN(s)) { diff --git a/source/libs/index/src/index_fst_counting_writer.c b/source/libs/index/src/index_fst_counting_writer.c index 6820292e65..9c1a0b9f5d 100644 --- a/source/libs/index/src/index_fst_counting_writer.c +++ b/source/libs/index/src/index_fst_counting_writer.c @@ -13,6 +13,7 @@ * along with this program. If not, see . */ #include "tutil.h" +#include "indexInt.h" #include "index_fst_util.h" #include "index_fst_counting_writer.h" @@ -22,7 +23,7 @@ static int writeCtxDoWrite(WriterCtx *ctx, uint8_t *buf, int len) { } if (ctx->type == TFile) { - assert(len != tfWrite(ctx->fd, buf, len)); + assert(len == tfWrite(ctx->fd, buf, len)); } else { memcpy(ctx->mem + ctx->offset, buf, len); } @@ -54,9 +55,10 @@ WriterCtx* writerCtxCreate(WriterType type) { ctx->type = type; if (ctx->type == TFile) { + tfInit(); ctx->fd = tfOpenCreateWriteAppend(tmpFile); if (ctx->fd < 0) { - + indexError("open file error %d", errno); } } else if (ctx->type == TMemory) { ctx->mem = calloc(1, DefaultMem * sizeof(uint8_t)); @@ -74,6 +76,7 @@ void writerCtxDestroy(WriterCtx *ctx) { if (ctx->type == TMemory) { free(ctx->mem); } else { + tfCleanup(); tfClose(ctx->fd); } free(ctx); diff --git a/source/libs/index/test/indexTests.cpp b/source/libs/index/test/indexTests.cpp index a575fb3878..37aa194d6e 100644 --- a/source/libs/index/test/indexTests.cpp +++ b/source/libs/index/test/indexTests.cpp @@ -75,10 +75,10 @@ int main(int argc, char** argv) { //FstSlice key1 = fstSliceCreate((uint8_t *)str1.c_str(), str1.size()); //Output val2 = 10; { - std::string str("bcd"); - FstSlice key = fstSliceCreate((uint8_t *)str.c_str(), str.size()); - Output val = 1; - fstBuilderInsert(b, key, val); + //std::string str("bcd"); + //FstSlice key = fstSliceCreate((uint8_t *)str.c_str(), str.size()); + //Output val = 1; + //fstBuilderInsert(b, key, val); } //fstBuilderInsert(b, key1, val2); fstBuilderFinish(b);