diff --git a/source/libs/index/src/index_fst.c b/source/libs/index/src/index_fst.c index 403b4a9122877d56e112bd84d2e5c00fdd017241..e7e9be5263e5de40a5d5388a12807e2de2654026 100644 --- a/source/libs/index/src/index_fst.c +++ b/source/libs/index/src/index_fst.c @@ -803,6 +803,7 @@ void fstBuilderDestroy(FstBuilder *b) { fstCountingWriterDestroy(b->wrt); fstUnFinishedNodesDestroy(b->unfinished); fstRegistryDestroy(b->registry); + fstSliceDestroy(&b->last); free(b); } @@ -869,13 +870,15 @@ OrderType fstBuilderCheckLastKey(FstBuilder *b, FstSlice bs, bool ckDup) { void fstBuilderCompileFrom(FstBuilder *b, uint64_t istate) { CompiledAddr addr = NONE_ADDRESS; while (istate + 1 < FST_UNFINISHED_NODES_LEN(b->unfinished)) { - FstBuilderNode *n = NULL; + FstBuilderNode *bn = NULL; if (addr == NONE_ADDRESS) { - n = fstUnFinishedNodesPopEmpty(b->unfinished); + bn = fstUnFinishedNodesPopEmpty(b->unfinished); } else { - n = fstUnFinishedNodesPopFreeze(b->unfinished, addr); + bn = fstUnFinishedNodesPopFreeze(b->unfinished, addr); } - addr = fstBuilderCompile(b, n); + addr = fstBuilderCompile(b, bn); + + fstBuilderNodeDestroy(bn); assert(addr != NONE_ADDRESS); //fstBuilderNodeDestroy(n); } @@ -910,6 +913,7 @@ void* fstBuilderInsertInner(FstBuilder *b) { fstBuilderCompileFrom(b, 0); FstBuilderNode *rootNode = fstUnFinishedNodesPopRoot(b->unfinished); CompiledAddr rootAddr = fstBuilderCompile(b, rootNode); + fstBuilderNodeDestroy(rootNode); char buf64[8] = {0}; @@ -1026,7 +1030,10 @@ Fst* fstCreate(FstSlice *slice) { fst->meta->ty = type; fst->meta->len = fstLen; fst->meta->checkSum = checkSum; - fst->data = slice; + + FstSlice *s = calloc(1, sizeof(FstSlice)); + *s = fstSliceCopy(slice, 0, FST_SLICE_LEN(slice)); + fst->data = s; return fst; @@ -1038,7 +1045,8 @@ FST_CREAT_FAILED: void fstDestroy(Fst *fst) { if (fst) { free(fst->meta); - fstNodeDestroy(fst->root); + fstSliceDestroy(fst->data); + free(fst->data); } free(fst); } @@ -1048,6 +1056,9 @@ bool fstGet(Fst *fst, FstSlice *b, Output *out) { Output tOut = 0; int32_t len; uint8_t *data = fstSliceData(b, &len); + + SArray *nodes = (SArray *)taosArrayInit(len, sizeof(FstNode *)); + taosArrayPush(nodes, &root); for (uint32_t i = 0; i < len; i++) { uint8_t inp = data[i]; Output res = 0; @@ -1059,12 +1070,22 @@ bool fstGet(Fst *fst, FstSlice *b, Output *out) { fstNodeGetTransitionAt(root, res, &trn); tOut += trn.out; root = fstGetNode(fst, trn.addr); + taosArrayPush(nodes, &root); + //fstNodeDestroy(root); } if (!FST_NODE_IS_FINAL(root)) { return false; } else { tOut = tOut + FST_NODE_FINAL_OUTPUT(root); } + for (size_t i = 0; i < taosArrayGetSize(nodes); i++) { + FstNode **node = (FstNode **)taosArrayGet(nodes, i); + fstNodeDestroy(*node); + } + taosArrayDestroy(nodes); + fst->root = NULL; + + *out = tOut; return true; @@ -1228,6 +1249,7 @@ bool streamWithStateSeekMin(StreamWithState *sws, FstBoundWithData *min) { taosArrayPush(sws->stack, &s); out += trn.out; node = fstGetNode(sws->fst, trn.addr); + fstNodeDestroy(node); } else { // This is a little tricky. We're in this case if the