From e769d0a00230266eb9f140128840aae911dee1cc Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 22 Nov 2021 18:40:44 +0800 Subject: [PATCH] refactor builder struct --- source/libs/index/inc/index_fst.h | 9 ++++++++- source/libs/index/inc/index_fst_util.h | 6 +++--- source/libs/index/src/index_fst.c | 22 ++++++++++++++++++---- source/libs/index/src/index_fst_util.c | 10 ++++------ 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/source/libs/index/inc/index_fst.h b/source/libs/index/inc/index_fst.h index cb60ef7cb8..86346f64ff 100644 --- a/source/libs/index/inc/index_fst.h +++ b/source/libs/index/inc/index_fst.h @@ -36,6 +36,8 @@ typedef struct FstRange { typedef enum { OneTransNext, OneTrans, AnyTrans, EmptyFinal} State; typedef enum { Included, Excluded, Unbounded} FstBound; +typedef enum {Ordered, OutOfOrdered, DuplicateKey} OrderType; + /* @@ -66,11 +68,16 @@ typedef struct FstBuilder { FstCountingWriter *wrt; // The FST raw data is written directly to `wtr`. FstUnFinishedNodes *unfinished; // The stack of unfinished nodes FstRegistry* registry; // A map of finished nodes. - SArray* last; // The last word added + FstSlice last; // The last word added CompiledAddr lastAddr; // The address of the last compiled node uint64_t len; // num of keys added } FstBuilder; +FstBuilder *fstBuilderCreate(void *w, FstType ty); +OrderType fstBuilderCheckLastKey(FstBuilder *b, FstSlice bs, bool ckDup); +CompiledAddr fstBuilderCompile(FstBuilder *b, FstBuilderNode *bn); + + typedef struct FstTransitions { FstNode *node; diff --git a/source/libs/index/inc/index_fst_util.h b/source/libs/index/inc/index_fst_util.h index 6410df6d61..36639af6f3 100644 --- a/source/libs/index/inc/index_fst_util.h +++ b/source/libs/index/inc/index_fst_util.h @@ -70,11 +70,11 @@ CompiledAddr unpackDelta(char *data, uint64_t len, uint64_t nodeAddr); typedef struct FstSlice { uint8_t *data; uint64_t dLen; - uint32_t start; - uint32_t end; + int32_t start; + int32_t end; } FstSlice; -FstSlice fstSliceCopy(FstSlice *slice, uint32_t start, uint32_t end); +FstSlice fstSliceCopy(FstSlice *slice, int32_t start, int32_t end); FstSlice fstSliceCreate(uint8_t *data, uint64_t dLen); bool fstSliceEmpty(FstSlice *slice); diff --git a/source/libs/index/src/index_fst.c b/source/libs/index/src/index_fst.c index 22cfe76de4..6206180668 100644 --- a/source/libs/index/src/index_fst.c +++ b/source/libs/index/src/index_fst.c @@ -300,16 +300,30 @@ FstBuilder *fstBuilderCreate(void *w, FstType ty) { b->wrt = fstCountingWriterCreate(w); b->unfinished = fstUnFinishedNodesCreate(); b->registry = fstRegistryCreate(10000, 2) ; - b->last = NULL; + b->last = fstSliceCreate(NULL, 0); b->lastAddr = NONE_ADDRESS; b->len = 0; return b; } -bool fstBuilderCheckLastKey(FstBuilder *b, FstSlice bs, bool ckDupe) { - - return true; + +OrderType fstBuilderCheckLastKey(FstBuilder *b, FstSlice bs, bool ckDup) { + FstSlice *input = &bs; + if (fstSliceEmpty(&b->last)) { + // deep copy or not + b->last = fstSliceCopy(&bs, input->start, input->end); + } else { + int comp = fstSliceCompare(&b->last, &bs); + if (comp == 0 && ckDup) { + return DuplicateKey; + } else if (comp == 1) { + return OutOfOrdered; + } + // deep copy or not + b->last = fstSliceCopy(&bs, input->start, input->end); + } + return Ordered; } CompiledAddr fstBuilderCompile(FstBuilder *b, FstBuilderNode *bn) { if (FST_BUILDER_NODE_IS_FINAL(bn) diff --git a/source/libs/index/src/index_fst_util.c b/source/libs/index/src/index_fst_util.c index 23e7056c64..8e0a104b5f 100644 --- a/source/libs/index/src/index_fst_util.c +++ b/source/libs/index/src/index_fst_util.c @@ -94,7 +94,7 @@ FstSlice fstSliceCreate(uint8_t *data, uint64_t dLen) { FstSlice slice = {.data = data, .dLen = dLen, .start = 0, .end = dLen - 1}; return slice; } -FstSlice fstSliceCopy(FstSlice *slice, uint32_t start, uint32_t end) { +FstSlice fstSliceCopy(FstSlice *slice, int32_t start, int32_t end) { FstSlice t; if (start >= slice->dLen || end >= slice->dLen || start > end) { t.data = NULL; @@ -111,12 +111,10 @@ bool fstSliceEmpty(FstSlice *slice) { return slice->data == NULL || slice->dLen <= 0; } - - int fstSliceCompare(FstSlice *a, FstSlice *b) { - uint32_t aLen = (a->end - a->start + 1); - uint32_t bLen = (b->end - b->start + 1); - uint32_t mLen = (aLen < bLen ? aLen : bLen); + int32_t aLen = (a->end - a->start + 1); + int32_t bLen = (b->end - b->start + 1); + int32_t mLen = (aLen < bLen ? aLen : bLen); for (int i = 0; i < mLen; i++) { uint8_t x = a->data[i + a->start]; uint8_t y = b->data[i + b->start];