Merge pull request #9082 from taosdata/feature/fst
update search framework
This commit is contained in:
commit
2aaa43a457
|
@ -20,6 +20,8 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "index_fst_util.h"
|
#include "index_fst_util.h"
|
||||||
|
|
||||||
|
|
||||||
typedef struct AutomationCtx AutomationCtx;
|
typedef struct AutomationCtx AutomationCtx;
|
||||||
|
|
||||||
typedef enum AutomationType {
|
typedef enum AutomationType {
|
||||||
|
@ -42,14 +44,23 @@ typedef struct AutomationCtx {
|
||||||
} AutomationCtx;
|
} AutomationCtx;
|
||||||
|
|
||||||
|
|
||||||
|
typedef enum ValueType { FST_INT, FST_CHAR, FST_ARRAY} ValueType;
|
||||||
typedef enum StartWithStateKind { Done, Running } StartWithStateKind;
|
typedef enum StartWithStateKind { Done, Running } StartWithStateKind;
|
||||||
|
|
||||||
typedef struct StartWithStateValue {
|
typedef struct StartWithStateValue {
|
||||||
StartWithStateKind kind;
|
StartWithStateKind kind;
|
||||||
void *value;
|
ValueType type;
|
||||||
|
union {
|
||||||
|
int val;
|
||||||
|
char *ptr;
|
||||||
|
SArray *arr;
|
||||||
|
// add more type
|
||||||
|
} ;
|
||||||
} StartWithStateValue;
|
} StartWithStateValue;
|
||||||
|
|
||||||
|
StartWithStateValue *startWithStateValueDump(StartWithStateValue *sv);
|
||||||
|
|
||||||
|
|
||||||
typedef struct AutomationFunc {
|
typedef struct AutomationFunc {
|
||||||
void* (*start)(AutomationCtx *ctx) ;
|
void* (*start)(AutomationCtx *ctx) ;
|
||||||
bool (*isMatch)(AutomationCtx *ctx, void *);
|
bool (*isMatch)(AutomationCtx *ctx, void *);
|
||||||
|
|
|
@ -1322,6 +1322,7 @@ StreamWithStateResult *streamWithStateNextWith(StreamWithState *sws, StreamCallb
|
||||||
return swsResultCreate(&s, output, callback(start));
|
return swsResultCreate(&s, output, callback(start));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
SArray *nodes = taosArrayInit(8, sizeof(FstNode *));
|
||||||
while (taosArrayGetSize(sws->stack) > 0) {
|
while (taosArrayGetSize(sws->stack) > 0) {
|
||||||
StreamState *p = (StreamState *)taosArrayPop(sws->stack);
|
StreamState *p = (StreamState *)taosArrayPop(sws->stack);
|
||||||
if (p->trans >= FST_NODE_LEN(p->node) || automFuncs[aut->type].canMatch(aut, p->autState)) {
|
if (p->trans >= FST_NODE_LEN(p->node) || automFuncs[aut->type].canMatch(aut, p->autState)) {
|
||||||
|
@ -1337,8 +1338,8 @@ StreamWithStateResult *streamWithStateNextWith(StreamWithState *sws, StreamCallb
|
||||||
void* nextState = automFuncs[aut->type].accept(aut, p->autState, trn.inp);
|
void* nextState = automFuncs[aut->type].accept(aut, p->autState, trn.inp);
|
||||||
void* tState = callback(nextState);
|
void* tState = callback(nextState);
|
||||||
bool isMatch = automFuncs[aut->type].isMatch(aut, nextState);
|
bool isMatch = automFuncs[aut->type].isMatch(aut, nextState);
|
||||||
//bool isMatch = sws->aut->isMatch(nextState);
|
|
||||||
FstNode *nextNode = fstGetNode(sws->fst, trn.addr);
|
FstNode *nextNode = fstGetNode(sws->fst, trn.addr);
|
||||||
|
taosArrayPush(nodes, &nextNode);
|
||||||
taosArrayPush(sws->inp, &(trn.inp));
|
taosArrayPush(sws->inp, &(trn.inp));
|
||||||
|
|
||||||
if (FST_NODE_IS_FINAL(nextNode)) {
|
if (FST_NODE_IS_FINAL(nextNode)) {
|
||||||
|
@ -1354,26 +1355,35 @@ StreamWithStateResult *streamWithStateNextWith(StreamWithState *sws, StreamCallb
|
||||||
StreamState s2 = {.node = nextNode, .trans = 0, .out = {.null = false, .out = out}, .autState = nextState};
|
StreamState s2 = {.node = nextNode, .trans = 0, .out = {.null = false, .out = out}, .autState = nextState};
|
||||||
taosArrayPush(sws->stack, &s2);
|
taosArrayPush(sws->stack, &s2);
|
||||||
|
|
||||||
uint8_t *buf = (uint8_t *)malloc(taosArrayGetSize(sws->inp) * sizeof(uint8_t));
|
|
||||||
for (uint32_t i = 0; i < taosArrayGetSize(sws->inp); i++) {
|
size_t isz = taosArrayGetSize(sws->inp);
|
||||||
uint8_t *t = (uint8_t *)taosArrayGet(sws->inp, i);
|
uint8_t *buf = (uint8_t *)malloc(isz * sizeof(uint8_t));
|
||||||
buf[i] = *t;
|
for (uint32_t i = 0; i < isz; i++) {
|
||||||
|
buf[i] = *(uint8_t *)taosArrayGet(sws->inp, i);
|
||||||
}
|
}
|
||||||
FstSlice slice = fstSliceCreate(buf, taosArrayGetSize(sws->inp));
|
FstSlice slice = fstSliceCreate(buf, taosArrayGetSize(sws->inp));
|
||||||
if (fstBoundWithDataExceededBy(sws->endAt, &slice)) {
|
if (fstBoundWithDataExceededBy(sws->endAt, &slice)) {
|
||||||
taosArrayDestroyEx(sws->stack, streamStateDestroy);
|
taosArrayDestroyEx(sws->stack, streamStateDestroy);
|
||||||
sws->stack = (SArray *)taosArrayInit(256, sizeof(StreamState));
|
sws->stack = (SArray *)taosArrayInit(256, sizeof(StreamState));
|
||||||
|
free(buf);
|
||||||
fstSliceDestroy(&slice);
|
fstSliceDestroy(&slice);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (FST_NODE_IS_FINAL(nextNode) && isMatch) {
|
if (FST_NODE_IS_FINAL(nextNode) && isMatch) {
|
||||||
FstOutput fOutput = {.null = false, .out = out + FST_NODE_FINAL_OUTPUT(nextNode)};
|
FstOutput fOutput = {.null = false, .out = out + FST_NODE_FINAL_OUTPUT(nextNode)};
|
||||||
StreamWithStateResult *result = swsResultCreate(&slice, fOutput , tState);
|
StreamWithStateResult *result = swsResultCreate(&slice, fOutput, tState);
|
||||||
|
free(buf);
|
||||||
fstSliceDestroy(&slice);
|
fstSliceDestroy(&slice);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
free(buf);
|
||||||
fstSliceDestroy(&slice);
|
fstSliceDestroy(&slice);
|
||||||
}
|
}
|
||||||
|
for (size_t i = 0; i < taosArrayGetSize(nodes); i++) {
|
||||||
|
FstNode** node = (FstNode **)taosArrayGet(nodes, i);
|
||||||
|
fstNodeDestroy(*node);
|
||||||
|
}
|
||||||
|
taosArrayDestroy(nodes);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,9 +16,59 @@
|
||||||
#include "index_fst_automation.h"
|
#include "index_fst_automation.h"
|
||||||
|
|
||||||
|
|
||||||
|
StartWithStateValue *startWithStateValueCreate(StartWithStateKind kind, ValueType ty, void *val) {
|
||||||
|
StartWithStateValue *nsv = calloc(1, sizeof(StartWithStateValue));
|
||||||
|
if (nsv == NULL) { return NULL; }
|
||||||
|
|
||||||
|
nsv->kind = kind;
|
||||||
|
nsv->type = ty;
|
||||||
|
if (ty == FST_INT) {
|
||||||
|
nsv->val = *(int *)val;
|
||||||
|
} else if (ty == FST_CHAR) {
|
||||||
|
size_t len = strlen((char *)val);
|
||||||
|
nsv->ptr = (char *)calloc(1, len + 1);
|
||||||
|
memcpy(nsv->ptr, val, len);
|
||||||
|
} else if (ty == FST_ARRAY) {
|
||||||
|
//TODO,
|
||||||
|
//nsv->arr = taosArrayFromList()
|
||||||
|
}
|
||||||
|
return nsv;
|
||||||
|
}
|
||||||
|
void startWithStateValueDestroy(StartWithStateValue *sv) {
|
||||||
|
if (sv == NULL) { return; }
|
||||||
|
|
||||||
|
if (sv->type == FST_INT) {
|
||||||
|
//
|
||||||
|
} else if (sv->type == FST_CHAR) {
|
||||||
|
free(sv->ptr);
|
||||||
|
} else if (sv->type == FST_ARRAY) {
|
||||||
|
taosArrayDestroy(sv->arr);
|
||||||
|
}
|
||||||
|
free(sv);
|
||||||
|
}
|
||||||
|
StartWithStateValue *startWithStateValueDump(StartWithStateValue *sv) {
|
||||||
|
StartWithStateValue *nsv = calloc(1, sizeof(StartWithStateValue));
|
||||||
|
if (nsv == NULL) { return NULL; }
|
||||||
|
|
||||||
|
nsv->kind = sv->kind;
|
||||||
|
nsv->type= sv->type;
|
||||||
|
if (nsv->type == FST_INT) {
|
||||||
|
nsv->val = sv->val;
|
||||||
|
} else if (nsv->type == FST_CHAR) {
|
||||||
|
size_t len = strlen(sv->ptr);
|
||||||
|
nsv->ptr = (char *)calloc(1, len + 1);
|
||||||
|
memcpy(nsv->ptr, sv->ptr, len);
|
||||||
|
} else if (nsv->type == FST_ARRAY) {
|
||||||
|
}
|
||||||
|
return nsv;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// prefix query, impl later
|
// prefix query, impl later
|
||||||
|
|
||||||
static void* prefixStart(AutomationCtx *ctx) {
|
static void* prefixStart(AutomationCtx *ctx) {
|
||||||
StartWithStateValue *data = (StartWithStateValue *)(ctx->data);
|
StartWithStateValue *data = (StartWithStateValue *)(ctx->data);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
static bool prefixIsMatch(AutomationCtx *ctx, void *data) {
|
static bool prefixIsMatch(AutomationCtx *ctx, void *data) {
|
||||||
|
@ -86,7 +136,7 @@ AutomationCtx* automCtxCreate(void *data, AutomationType type) {
|
||||||
if (type == AUTOMATION_PREFIX) {
|
if (type == AUTOMATION_PREFIX) {
|
||||||
StartWithStateValue *swsv = (StartWithStateValue *)calloc(1, sizeof(StartWithStateValue));
|
StartWithStateValue *swsv = (StartWithStateValue *)calloc(1, sizeof(StartWithStateValue));
|
||||||
swsv->kind = Done;
|
swsv->kind = Done;
|
||||||
swsv->value = NULL;
|
//swsv->value = NULL;
|
||||||
ctx->data = (void *)swsv;
|
ctx->data = (void *)swsv;
|
||||||
} else if (type == AUTMMATION_MATCH) {
|
} else if (type == AUTMMATION_MATCH) {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue