refactor builder struct

This commit is contained in:
yihaoDeng 2021-11-22 18:40:44 +08:00
parent 06fe44cabd
commit e769d0a002
4 changed files with 33 additions and 14 deletions

View File

@ -36,6 +36,8 @@ typedef struct FstRange {
typedef enum { OneTransNext, OneTrans, AnyTrans, EmptyFinal} State; typedef enum { OneTransNext, OneTrans, AnyTrans, EmptyFinal} State;
typedef enum { Included, Excluded, Unbounded} FstBound; 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`. FstCountingWriter *wrt; // The FST raw data is written directly to `wtr`.
FstUnFinishedNodes *unfinished; // The stack of unfinished nodes FstUnFinishedNodes *unfinished; // The stack of unfinished nodes
FstRegistry* registry; // A map of finished 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 CompiledAddr lastAddr; // The address of the last compiled node
uint64_t len; // num of keys added uint64_t len; // num of keys added
} FstBuilder; } FstBuilder;
FstBuilder *fstBuilderCreate(void *w, FstType ty);
OrderType fstBuilderCheckLastKey(FstBuilder *b, FstSlice bs, bool ckDup);
CompiledAddr fstBuilderCompile(FstBuilder *b, FstBuilderNode *bn);
typedef struct FstTransitions { typedef struct FstTransitions {
FstNode *node; FstNode *node;

View File

@ -70,11 +70,11 @@ CompiledAddr unpackDelta(char *data, uint64_t len, uint64_t nodeAddr);
typedef struct FstSlice { typedef struct FstSlice {
uint8_t *data; uint8_t *data;
uint64_t dLen; uint64_t dLen;
uint32_t start; int32_t start;
uint32_t end; int32_t end;
} FstSlice; } 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); FstSlice fstSliceCreate(uint8_t *data, uint64_t dLen);
bool fstSliceEmpty(FstSlice *slice); bool fstSliceEmpty(FstSlice *slice);

View File

@ -300,16 +300,30 @@ FstBuilder *fstBuilderCreate(void *w, FstType ty) {
b->wrt = fstCountingWriterCreate(w); b->wrt = fstCountingWriterCreate(w);
b->unfinished = fstUnFinishedNodesCreate(); b->unfinished = fstUnFinishedNodesCreate();
b->registry = fstRegistryCreate(10000, 2) ; b->registry = fstRegistryCreate(10000, 2) ;
b->last = NULL; b->last = fstSliceCreate(NULL, 0);
b->lastAddr = NONE_ADDRESS; b->lastAddr = NONE_ADDRESS;
b->len = 0; b->len = 0;
return b; 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) { CompiledAddr fstBuilderCompile(FstBuilder *b, FstBuilderNode *bn) {
if (FST_BUILDER_NODE_IS_FINAL(bn) if (FST_BUILDER_NODE_IS_FINAL(bn)

View File

@ -94,7 +94,7 @@ FstSlice fstSliceCreate(uint8_t *data, uint64_t dLen) {
FstSlice slice = {.data = data, .dLen = dLen, .start = 0, .end = dLen - 1}; FstSlice slice = {.data = data, .dLen = dLen, .start = 0, .end = dLen - 1};
return slice; return slice;
} }
FstSlice fstSliceCopy(FstSlice *slice, uint32_t start, uint32_t end) { FstSlice fstSliceCopy(FstSlice *slice, int32_t start, int32_t end) {
FstSlice t; FstSlice t;
if (start >= slice->dLen || end >= slice->dLen || start > end) { if (start >= slice->dLen || end >= slice->dLen || start > end) {
t.data = NULL; t.data = NULL;
@ -111,12 +111,10 @@ bool fstSliceEmpty(FstSlice *slice) {
return slice->data == NULL || slice->dLen <= 0; return slice->data == NULL || slice->dLen <= 0;
} }
int fstSliceCompare(FstSlice *a, FstSlice *b) { int fstSliceCompare(FstSlice *a, FstSlice *b) {
uint32_t aLen = (a->end - a->start + 1); int32_t aLen = (a->end - a->start + 1);
uint32_t bLen = (b->end - b->start + 1); int32_t bLen = (b->end - b->start + 1);
uint32_t mLen = (aLen < bLen ? aLen : bLen); int32_t mLen = (aLen < bLen ? aLen : bLen);
for (int i = 0; i < mLen; i++) { for (int i = 0; i < mLen; i++) {
uint8_t x = a->data[i + a->start]; uint8_t x = a->data[i + a->start];
uint8_t y = b->data[i + b->start]; uint8_t y = b->data[i + b->start];