diff --git a/source/libs/index/inc/index_fst.h b/source/libs/index/inc/index_fst.h index fe669c8567..cd5073187c 100644 --- a/source/libs/index/inc/index_fst.h +++ b/source/libs/index/inc/index_fst.h @@ -268,11 +268,8 @@ FstNode* fstGetNode(Fst *fst, CompiledAddr); FstNode* fstGetRoot(Fst *fst); FstType fstGetType(Fst *fst); CompiledAddr fstGetRootAddr(Fst *fst); - - - -Output fstEmptyFinalOutput(Fst *fst, bool *null); -bool fstVerify(Fst *fst); +Output fstEmptyFinalOutput(Fst *fst, bool *null); +bool fstVerify(Fst *fst); //refactor this function @@ -304,6 +301,7 @@ typedef struct StreamWithStateResult { } StreamWithStateResult; StreamWithStateResult *swsResultCreate(FstSlice *data, FstOutput fOut, void *state); +void swsResultDestroy(StreamWithStateResult *result); typedef void* (*StreamCallback)(void *); StreamWithState *streamWithStateCreate(Fst *fst, Automation *automation, FstBoundWithData *min, FstBoundWithData *max) ; diff --git a/source/libs/index/src/index_fst.c b/source/libs/index/src/index_fst.c index ca1c7910ed..68d2011b8f 100644 --- a/source/libs/index/src/index_fst.c +++ b/source/libs/index/src/index_fst.c @@ -1301,12 +1301,16 @@ StreamWithStateResult *streamWithStateNextWith(StreamWithState *sws, StreamCallb if (fstBoundWithDataExceededBy(sws->endAt, &slice)) { taosArrayDestroyEx(sws->stack, streamStateDestroy); sws->stack = (SArray *)taosArrayInit(256, sizeof(StreamState)); + fstSliceDestroy(&slice); return NULL; } if (FST_NODE_IS_FINAL(nextNode) && isMatch) { FstOutput fOutput = {.null = false, .out = out + FST_NODE_FINAL_OUTPUT(nextNode)}; - return swsResultCreate(&slice, fOutput , tState); + StreamWithStateResult *result = swsResultCreate(&slice, fOutput , tState); + fstSliceDestroy(&slice); + return result; } + fstSliceDestroy(&slice); } return NULL; @@ -1321,8 +1325,14 @@ StreamWithStateResult *swsResultCreate(FstSlice *data, FstOutput fOut, void *sta result->state = state; return result; - } +void swsResultDestroy(StreamWithStateResult *result) { + if (NULL == result) { return; } + + fstSliceDestroy(&result->data); + free(result); +} + void streamStateDestroy(void *s) { if (NULL == s) { return; } StreamState *ss = (StreamState *)s; diff --git a/source/libs/index/src/index_fst_util.c b/source/libs/index/src/index_fst_util.c index 1c7a1a9e9d..532b0b8ac3 100644 --- a/source/libs/index/src/index_fst_util.c +++ b/source/libs/index/src/index_fst_util.c @@ -107,7 +107,6 @@ FstSlice fstSliceCreate(uint8_t *data, uint64_t len) { FstSlice fstSliceCopy(FstSlice *s, int32_t start, int32_t end) { FstString *str = s->str; str->ref++; - int32_t alen; //uint8_t *buf = fstSliceData(s, &alen); //start = buf + start - (buf - s->start); //end = buf + end - (buf - s->start); @@ -117,9 +116,10 @@ FstSlice fstSliceCopy(FstSlice *s, int32_t start, int32_t end) { } FstSlice fstSliceDeepCopy(FstSlice *s, int32_t start, int32_t end) { - int32_t alen, tlen = end - start + 1; - uint8_t *data = fstSliceData(s, &alen); - assert(tlen <= alen); + int32_t tlen = end - start + 1; + int32_t slen; + uint8_t *data = fstSliceData(s, &slen); + assert(tlen <= slen); uint8_t *buf = malloc(sizeof(uint8_t) * tlen); memcpy(buf, data + start, tlen); @@ -136,7 +136,7 @@ FstSlice fstSliceDeepCopy(FstSlice *s, int32_t start, int32_t end) { return ans; } bool fstSliceIsEmpty(FstSlice *s) { - return s->str == NULL || s->start < 0 || s->end < 0; + return s->str == NULL || s->str->len == 0 || s->start < 0 || s->end < 0; } uint8_t *fstSliceData(FstSlice *s, int32_t *size) { @@ -161,7 +161,6 @@ int fstSliceCompare(FstSlice *a, FstSlice *b) { uint8_t *aBuf = fstSliceData(a, &alen); uint8_t *bBuf = fstSliceData(b, &blen); - uint32_t i, j; for (i = 0, j = 0; i < alen && j < blen; i++, j++) { uint8_t x = aBuf[i];