refactor builder struct
This commit is contained in:
parent
0f682ca20a
commit
d0844e5dda
|
@ -62,7 +62,7 @@ FstBuilderNode *fstUnFinishedNodesPopRoot(FstUnFinishedNodes *nodes) {
|
|||
assert(taosArrayGetSize(nodes->stack) == 1);
|
||||
|
||||
FstBuilderNodeUnfinished *un = taosArrayPop(nodes->stack);
|
||||
assert(un->last == NULL);
|
||||
//assert(un->last == NULL);
|
||||
return un->node;
|
||||
}
|
||||
|
||||
|
@ -70,6 +70,7 @@ FstBuilderNode *fstUnFinishedNodesPopFreeze(FstUnFinishedNodes *nodes, CompiledA
|
|||
FstBuilderNodeUnfinished *un = taosArrayPop(nodes->stack);
|
||||
fstBuilderNodeUnfinishedLastCompiled(un, addr);
|
||||
free(un->last); // TODO add func FstLastTransitionFree()
|
||||
un->last = NULL;
|
||||
return un->node;
|
||||
}
|
||||
|
||||
|
@ -99,8 +100,6 @@ void fstUnFinishedNodesAddSuffix(FstUnFinishedNodes *nodes, FstSlice bs, Output
|
|||
FstBuilderNodeUnfinished *un = taosArrayGet(nodes->stack, sz);
|
||||
assert(un->last == NULL);
|
||||
|
||||
|
||||
|
||||
//FstLastTransition *trn = malloc(sizeof(FstLastTransition));
|
||||
//trn->inp = s->data[s->start];
|
||||
//trn->out = out;
|
||||
|
@ -108,7 +107,7 @@ void fstUnFinishedNodesAddSuffix(FstUnFinishedNodes *nodes, FstSlice bs, Output
|
|||
uint8_t *data = fstSliceData(s, &len);
|
||||
un->last = fstLastTransitionCreate(data[0], out);
|
||||
|
||||
for (uint64_t i = 0; i < len; i++) {
|
||||
for (uint64_t i = 1; i < len; i++) {
|
||||
FstBuilderNode *n = malloc(sizeof(FstBuilderNode));
|
||||
n->isFinal = false;
|
||||
n->finalOutput = 0;
|
||||
|
@ -117,7 +116,7 @@ void fstUnFinishedNodesAddSuffix(FstUnFinishedNodes *nodes, FstSlice bs, Output
|
|||
//FstLastTransition *trn = malloc(sizeof(FstLastTransition));
|
||||
//trn->inp = s->data[i];
|
||||
//trn->out = out;
|
||||
FstLastTransition *trn = fstLastTransitionCreate(data[i], out);
|
||||
FstLastTransition *trn = fstLastTransitionCreate(data[i], 0);
|
||||
|
||||
FstBuilderNodeUnfinished un = {.node = n, .last = trn};
|
||||
taosArrayPush(nodes->stack, &un);
|
||||
|
@ -890,22 +889,26 @@ void* fstBuilderInsertInner(FstBuilder *b) {
|
|||
FstBuilderNode *rootNode = fstUnFinishedNodesPopRoot(b->unfinished);
|
||||
CompiledAddr rootAddr = fstBuilderCompile(b, rootNode);
|
||||
|
||||
uint8_t buf64[8] = {0};
|
||||
char buf64[8] = {0};
|
||||
|
||||
taosEncodeFixedU64((void **)&buf64, b->len);
|
||||
void *pBuf64 = buf64;
|
||||
taosEncodeFixedU64(&pBuf64, b->len);
|
||||
fstCountingWriterWrite(b->wrt, buf64, sizeof(buf64));
|
||||
|
||||
taosEncodeFixedU64((void **)&buf64, rootAddr);
|
||||
pBuf64 = buf64;
|
||||
taosEncodeFixedU64(&pBuf64, rootAddr);
|
||||
fstCountingWriterWrite(b->wrt, buf64, sizeof(buf64));
|
||||
|
||||
uint8_t buf32[4] = {0};
|
||||
char buf32[4] = {0};
|
||||
void *pBuf32 = buf32;
|
||||
uint32_t sum = fstCountingWriterMaskedCheckSum(b->wrt);
|
||||
taosEncodeFixedU32((void **)&buf32, sum);
|
||||
taosEncodeFixedU32(&pBuf32, sum);
|
||||
fstCountingWriterWrite(b->wrt, buf32, sizeof(buf32));
|
||||
|
||||
fstCountingWriterFlush(b->wrt);
|
||||
//fstCountingWriterDestroy(b->wrt);
|
||||
//b->wrt = NULL;
|
||||
return b->wrt;
|
||||
|
||||
}
|
||||
void fstBuilderFinish(FstBuilder *b) {
|
||||
fstBuilderInsertInner(b);
|
||||
|
|
|
@ -52,9 +52,12 @@ WriterCtx* writerCtxCreate(WriterType type) {
|
|||
WriterCtx *ctx = calloc(1, sizeof(WriterCtx));
|
||||
if (ctx == NULL) { return NULL; }
|
||||
|
||||
ctx->type == type;
|
||||
ctx->type = type;
|
||||
if (ctx->type == TFile) {
|
||||
ctx->fd = tfOpenCreateWriteAppend(tmpFile);
|
||||
if (ctx->fd < 0) {
|
||||
|
||||
}
|
||||
} else if (ctx->type == TMemory) {
|
||||
ctx->mem = calloc(1, DefaultMem * sizeof(uint8_t));
|
||||
}
|
||||
|
|
|
@ -30,20 +30,26 @@ void fstBuilderNodeDestroy(FstBuilderNode *node) {
|
|||
|
||||
bool fstBuilderNodeEqual(FstBuilderNode *n1, FstBuilderNode *n2) {
|
||||
if (n1 == n2) { return true; }
|
||||
|
||||
if (n1->isFinal != n2->isFinal ||
|
||||
n1->finalOutput != n2->finalOutput ||
|
||||
taosArrayGetSize(n1->trans) != taosArrayGetSize(n2->trans)) {
|
||||
if (n1 == NULL || n2 == NULL ) {
|
||||
return false;
|
||||
}
|
||||
size_t sz = taosArrayGetSize(n1->trans);
|
||||
for (size_t i = 0; i < sz; i++) {
|
||||
|
||||
if (n1->isFinal != n2->isFinal || n1->finalOutput != n2->finalOutput) {
|
||||
return false;
|
||||
}
|
||||
size_t s1 = n1->trans? taosArrayGetSize(n1->trans): 0;
|
||||
size_t s2 = n2->trans? taosArrayGetSize(n2->trans): 0;
|
||||
if (s1 != s2) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < s1; i++) {
|
||||
FstTransition *t1 = taosArrayGet(n1->trans, i);
|
||||
FstTransition *t2 = taosArrayGet(n2->trans, i);
|
||||
if (t1->inp != t2->inp || t1->out != t2->out || t1->addr != t2->addr) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
FstBuilderNode *fstBuilderNodeClone(FstBuilderNode *src) {
|
||||
|
|
|
@ -65,14 +65,14 @@
|
|||
int main(int argc, char** argv) {
|
||||
std::string str("abc");
|
||||
FstSlice key = fstSliceCreate((uint8_t *)str.c_str(), str.size());
|
||||
Output val = 10;
|
||||
Output val = 1;
|
||||
|
||||
std::string str1("bcd");
|
||||
FstSlice key1 = fstSliceCreate((uint8_t *)str1.c_str(), str1.size());
|
||||
Output val2 = 10;
|
||||
//std::string str1("bcd");
|
||||
//FstSlice key1 = fstSliceCreate((uint8_t *)str1.c_str(), str1.size());
|
||||
//Output val2 = 10;
|
||||
FstBuilder *b = fstBuilderCreate(NULL, 1);
|
||||
fstBuilderInsert(b, key, val);
|
||||
fstBuilderInsert(b, key1, val2);
|
||||
//fstBuilderInsert(b, key1, val2);
|
||||
fstBuilderFinish(b);
|
||||
fstBuilderDestroy(b);
|
||||
fstSliceDestroy(&key);
|
||||
|
|
Loading…
Reference in New Issue