index_fst.c 29.9 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "index_fst.h"
dengyihao's avatar
dengyihao 已提交
17
#include "tcoding.h"
dengyihao's avatar
dengyihao 已提交
18

dengyihao's avatar
dengyihao 已提交
19 20 21 22 23

static void fstPackDeltaIn(FstCountingWriter *wrt, CompiledAddr nodeAddr, CompiledAddr transAddr, uint8_t nBytes) {
  CompiledAddr deltaAddr = (transAddr == EMPTY_ADDRESS) ? EMPTY_ADDRESS : nodeAddr - transAddr;
  fstCountingWriterPackUintIn(wrt, deltaAddr, nBytes); 
}
dengyihao's avatar
dengyihao 已提交
24
static uint8_t fstPackDetla(FstCountingWriter *wrt, CompiledAddr nodeAddr, CompiledAddr transAddr) {
dengyihao's avatar
dengyihao 已提交
25 26
  uint8_t nBytes = packDeltaSize(nodeAddr, transAddr);
  fstPackDeltaIn(wrt, nodeAddr, transAddr, nBytes); 
dengyihao's avatar
dengyihao 已提交
27
  return nBytes;
dengyihao's avatar
dengyihao 已提交
28
}
dengyihao's avatar
dengyihao 已提交
29

dengyihao's avatar
dengyihao 已提交
30 31 32 33 34 35 36 37
FstUnFinishedNodes *fstUnFinishedNodesCreate() {
  FstUnFinishedNodes *nodes = malloc(sizeof(FstUnFinishedNodes));
  if (nodes == NULL) { return NULL; }

  nodes->stack = (SArray *)taosArrayInit(64, sizeof(FstBuilderNodeUnfinished));
  fstUnFinishedNodesPushEmpty(nodes, false);
  return nodes;
}
dengyihao's avatar
dengyihao 已提交
38 39 40 41 42
void unFinishedNodeDestroyElem(void* elem) {
  FstBuilderNodeUnfinished *b = (FstBuilderNodeUnfinished*)elem;
  fstBuilderNodeDestroy(b->node); 
  free(b->last); 
}  
dengyihao's avatar
dengyihao 已提交
43
void fstUnFinishedNodesDestroy(FstUnFinishedNodes *nodes) {
dengyihao's avatar
dengyihao 已提交
44 45 46 47 48 49
  if (nodes == NULL) { return; } 

  taosArrayDestroyEx(nodes->stack, unFinishedNodeDestroyElem); 
  free(nodes);
}

dengyihao's avatar
dengyihao 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
void fstUnFinishedNodesPushEmpty(FstUnFinishedNodes *nodes, bool isFinal) {
  FstBuilderNode *node = malloc(sizeof(FstBuilderNode));
  node->isFinal     = isFinal;
  node->finalOutput = 0;
  node->trans       = NULL;

  FstBuilderNodeUnfinished un = {.node = node, .last = NULL}; 
  taosArrayPush(nodes->stack, &un);
  
}
FstBuilderNode *fstUnFinishedNodesPopRoot(FstUnFinishedNodes *nodes) {
  assert(taosArrayGetSize(nodes->stack) == 1);

  FstBuilderNodeUnfinished *un = taosArrayPop(nodes->stack);
  assert(un->last == NULL); 
  return un->node;  
}

FstBuilderNode *fstUnFinishedNodesPopFreeze(FstUnFinishedNodes *nodes, CompiledAddr addr) {
  FstBuilderNodeUnfinished *un = taosArrayPop(nodes->stack);
  fstBuilderNodeUnfinishedLastCompiled(un, addr);
  free(un->last); // TODO add func FstLastTransitionFree()
  return un->node; 
}

FstBuilderNode *fstUnFinishedNodesPopEmpty(FstUnFinishedNodes *nodes) {
  FstBuilderNodeUnfinished *un = taosArrayPop(nodes->stack);
  assert(un->last == NULL); 
  return un->node;  
  
}
void fstUnFinishedNodesSetRootOutput(FstUnFinishedNodes *nodes, Output out) {
  FstBuilderNodeUnfinished *un = taosArrayGet(nodes->stack, 0);
  un->node->isFinal     = true;
  un->node->finalOutput = out;
  //un->node->trans       = NULL;  
} 
void fstUnFinishedNodesTopLastFreeze(FstUnFinishedNodes *nodes, CompiledAddr addr) {
  size_t sz = taosArrayGetSize(nodes->stack) - 1; 
  FstBuilderNodeUnfinished *un = taosArrayGet(nodes->stack, sz);
  fstBuilderNodeUnfinishedLastCompiled(un, addr);
}
void fstUnFinishedNodesAddSuffix(FstUnFinishedNodes *nodes, FstSlice bs, Output out) {
  FstSlice *s = &bs;
  if (s->data == NULL || s->dLen == 0 || s->start > s->end) {
    return;
  }
  size_t sz = taosArrayGetSize(nodes->stack) - 1; 
  FstBuilderNodeUnfinished *un = taosArrayGet(nodes->stack, sz);
  assert(un->last == NULL);

dengyihao's avatar
dengyihao 已提交
101
     
dengyihao's avatar
dengyihao 已提交
102
  
dengyihao's avatar
dengyihao 已提交
103 104 105 106
  //FstLastTransition *trn = malloc(sizeof(FstLastTransition)); 
  //trn->inp = s->data[s->start]; 
  //trn->out = out;
  un->last = fstLastTransitionCreate(s->data[s->start], out); 
dengyihao's avatar
dengyihao 已提交
107 108 109 110 111 112 113

  for (uint64_t i = s->start; i <= s->end; i++) {
    FstBuilderNode *n = malloc(sizeof(FstBuilderNode));
    n->isFinal     = false;
    n->finalOutput = 0;
    n->trans       = NULL;
    
dengyihao's avatar
dengyihao 已提交
114 115 116 117
    //FstLastTransition *trn = malloc(sizeof(FstLastTransition)); 
    //trn->inp = s->data[i];
    //trn->out = out; 
    FstLastTransition *trn = fstLastTransitionCreate(s->data[i], out);
dengyihao's avatar
dengyihao 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

    FstBuilderNodeUnfinished un = {.node = n, .last = trn}; 
    taosArrayPush(nodes->stack, &un); 
  }
  fstUnFinishedNodesPushEmpty(nodes, true);  
}


uint64_t fstUnFinishedNodesFindCommPrefix(FstUnFinishedNodes *node, FstSlice bs) {
  FstSlice *s = &bs;

  size_t lsz = (size_t)(s->end - s->start + 1);          // data len 
  size_t ssz = taosArrayGetSize(node->stack);  // stack size
    
  uint64_t count = 0;
  for (size_t i = 0; i < ssz && i < lsz; i++) {
    FstBuilderNodeUnfinished *un = taosArrayGet(node->stack, i); 
    if (un->last->inp == s->data[s->start + i]) {
      count++;
    } else {
      break;
    } 
  }
  return count;
}
dengyihao's avatar
dengyihao 已提交
143
uint64_t fstUnFinishedNodesFindCommPrefixAndSetOutput(FstUnFinishedNodes *node, FstSlice bs, Output in, Output *out) {
dengyihao's avatar
dengyihao 已提交
144 145 146 147 148
  FstSlice *s = &bs;

  size_t lsz = (size_t)(s->end - s->start + 1);          // data len 
  size_t ssz = taosArrayGetSize(node->stack);  // stack size

dengyihao's avatar
dengyihao 已提交
149 150
  uint64_t i = 0;
  for (i = 0; i < lsz && i < ssz; i++) {
dengyihao's avatar
dengyihao 已提交
151 152
    FstBuilderNodeUnfinished *un = taosArrayGet(node->stack, i);

dengyihao's avatar
dengyihao 已提交
153 154 155 156 157 158 159 160
    FstLastTransition *t = un->last; 
    uint64_t addPrefix = 0;  
    if (t && t->inp == s->data[s->start + i]) {
      uint64_t commPrefix = MIN(t->out, *out);      
      uint64_t tAddPrefix  = t->out - commPrefix; 
      (*out) = (*out) - commPrefix; 
      t->out = commPrefix;
      addPrefix = tAddPrefix; 
dengyihao's avatar
dengyihao 已提交
161
    } else {
dengyihao's avatar
dengyihao 已提交
162 163 164 165 166
       break;
    }
    if (addPrefix != 0) {
      fstBuilderNodeUnfinishedAddOutputPrefix(un, addPrefix);  

dengyihao's avatar
dengyihao 已提交
167 168
    }
  }   
dengyihao's avatar
dengyihao 已提交
169
  return i;
dengyihao's avatar
dengyihao 已提交
170 171 172
} 


dengyihao's avatar
dengyihao 已提交
173
FstState fstStateCreateFrom(FstSlice* slice, CompiledAddr addr) {
dengyihao's avatar
dengyihao 已提交
174
  FstState fs = {.state = EmptyFinal, .val = 0};
dengyihao's avatar
dengyihao 已提交
175 176 177
  if (addr == EMPTY_ADDRESS) {
    return fs; 
  }
dengyihao's avatar
dengyihao 已提交
178
  uint8_t v = slice->data[addr]; 
dengyihao's avatar
dengyihao 已提交
179 180 181 182
  uint8_t t = (v & 0b11000000) >> 6;
  if (t == 0b11) {
    fs.state = OneTransNext;
  } else if (t == 0b10) {
dengyihao's avatar
dengyihao 已提交
183
    fs.state = OneTrans; 
dengyihao's avatar
dengyihao 已提交
184
  } else {
dengyihao's avatar
dengyihao 已提交
185
    fs.state = AnyTrans;  
dengyihao's avatar
dengyihao 已提交
186
  }
dengyihao's avatar
dengyihao 已提交
187
  fs.val = v;
dengyihao's avatar
dengyihao 已提交
188 189
  return fs;
}
dengyihao's avatar
dengyihao 已提交
190

dengyihao's avatar
dengyihao 已提交
191
static FstState fstStateDict[] = {
dengyihao's avatar
dengyihao 已提交
192 193 194 195 196
  {.state = OneTransNext, .val = 0b11000000},
  {.state = OneTrans,     .val = 0b10000000},
  {.state = AnyTrans,     .val = 0b00000000},
  {.state = EmptyFinal,   .val = 0b00000000}
};
dengyihao's avatar
dengyihao 已提交
197
// debug 
dengyihao's avatar
dengyihao 已提交
198
static const char *fstStateStr[] = {"ONE_TRANS_NEXT", "ONE_TRANS", "ANY_TRANS", "EMPTY_FINAL"}; 
dengyihao's avatar
dengyihao 已提交
199 200 201

FstState fstStateCreate(State state){
  uint8_t idx = (uint8_t)state;
dengyihao's avatar
dengyihao 已提交
202
  return fstStateDict[idx];
dengyihao's avatar
dengyihao 已提交
203 204
}
//compile
dengyihao's avatar
dengyihao 已提交
205 206 207 208 209 210 211 212
void fstStateCompileForOneTransNext(FstCountingWriter *w, CompiledAddr addr, uint8_t inp) {
  FstState s = fstStateCreate(OneTransNext);  
  fstStateSetCommInput(&s, inp);

  bool null = false;
  uint8_t v = fstStateCommInput(&s, &null);
  if (null) {
    // w->write_all(&[inp])
dengyihao's avatar
dengyihao 已提交
213 214 215
    fstCountingWriterWrite(w, &inp, 1);
  }     
  fstCountingWriterWrite(w, &(s.val), 1);
dengyihao's avatar
dengyihao 已提交
216
  // w->write_all(&[s.val])
dengyihao's avatar
dengyihao 已提交
217
  return;
dengyihao's avatar
dengyihao 已提交
218
}
dengyihao's avatar
dengyihao 已提交
219
void fstStateCompileForOneTrans(FstCountingWriter *w, CompiledAddr addr, FstTransition* trn) {
dengyihao's avatar
dengyihao 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  Output out = trn->out;   
  uint8_t outPackSize = (out == 0 ? 0 : fstCountingWriterPackUint(w, out));        
  uint8_t transPackSize = fstPackDetla(w, addr, trn->addr);      
  PackSizes packSizes = 0;

  FST_SET_OUTPUT_PACK_SIZE(packSizes, outPackSize);
  FST_SET_TRANSITION_PACK_SIZE(packSizes, transPackSize);
  fstCountingWriterWrite(w, (char *)&packSizes, sizeof(packSizes)); 

  FstState st = fstStateCreate(OneTrans); 
  
  fstStateSetCommInput(&st, trn->inp);
  bool null = false;
  uint8_t inp = fstStateCommInput(&st, &null);   
  if (null == true) {
    fstCountingWriterWrite(w, (char *)&trn->inp, sizeof(trn->inp));
  }
  fstCountingWriterWrite(w, (char *)(&(st.val)), sizeof(st.val));
dengyihao's avatar
dengyihao 已提交
238 239 240
  return ;

}
dengyihao's avatar
dengyihao 已提交
241
void fstStateCompileForAnyTrans(FstCountingWriter *w, CompiledAddr addr, FstBuilderNode *node) {
dengyihao's avatar
dengyihao 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  size_t sz = taosArrayGetSize(node->trans);  
  assert(sz <= 256);

  uint8_t tSize = 0;
  uint8_t oSize = packSize(node->finalOutput) ;  
  
  // finalOutput.is_zero()
  bool anyOuts = (node->finalOutput != 0) ;
  for (size_t i = 0; i < sz; i++) {
    FstTransition *t = taosArrayGet(node->trans, i); 
    tSize = MAX(tSize, packDeltaSize(addr, t->addr)); 
    oSize = MAX(oSize, packSize(t->out));
    anyOuts = anyOuts || (t->out != 0); 
  }

  PackSizes packSizes = 0; 
  if (anyOuts) { FST_SET_OUTPUT_PACK_SIZE(packSizes, oSize); }
  else { FST_SET_OUTPUT_PACK_SIZE(packSizes, 0); }

  FST_SET_TRANSITION_PACK_SIZE(packSizes, tSize);
  
  FstState st = fstStateCreate(AnyTrans);
  fstStateSetFinalState(&st, node->isFinal); 
  fstStateSetStateNtrans(&st, (uint8_t)sz);
   
  if (anyOuts) {
    if (FST_BUILDER_NODE_IS_FINAL(node)) {
      fstCountingWriterPackUintIn(w, node->finalOutput, oSize);
    }
    for (size_t i = 0; i < sz; i++) {
      FstTransition *t = taosArrayGet(node->trans, i);
      fstCountingWriterPackUintIn(w, t->out, oSize);
    }
  } 
  for (size_t i = 0; i < sz; i++) {
      FstTransition *t = taosArrayGet(node->trans, i);
      fstPackDeltaIn(w, addr, t->addr, tSize);
  }
  for (size_t i = 0; i < sz; i++) {
     FstTransition *t = taosArrayGet(node->trans, i);
     fstCountingWriterWrite(w, (char *)&t->inp, 1);
      //fstPackDeltaIn(w, addr, t->addr, tSize);
  }
  if (sz > TRANS_INDEX_THRESHOLD) {
    // A value of 255 indicates that no transition exists for the byte
    // at that index. (Except when there are 256 transitions.) Namely,
    // any value greater than or equal to the number of transitions in
    // this node indicates an absent transition.
dengyihao's avatar
dengyihao 已提交
290
    uint8_t *index = (uint8_t *)malloc(sizeof(uint8_t) * 256); 
dengyihao's avatar
dengyihao 已提交
291 292 293 294 295 296 297 298 299
    for (uint8_t i = 0; i < 256; i++) {
      index[i] = 255;
    }
    for (size_t i = 0; i < sz; i++) {
      FstTransition *t = taosArrayGet(node->trans, i);
      index[t->inp] = i;
      fstCountingWriterWrite(w, (char *)index, sizeof(index)); 
      //fstPackDeltaIn(w, addr, t->addr, tSize);
    }
dengyihao's avatar
dengyihao 已提交
300
    free(index);
dengyihao's avatar
dengyihao 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313
  }
  fstCountingWriterWrite(w, (char *)&packSizes, 1);
  bool null = false;
  fstStateStateNtrans(&st, &null);
  if (null == true) {
     // 256 can't be represented in a u8, so we abuse the fact that
     // the # of transitions can never be 1 here, since 1 is always
     // encoded in the state byte.
    uint8_t v = 1;
    if (sz == 256) { fstCountingWriterWrite(w, (char *)&v, 1); }
    else { fstCountingWriterWrite(w, (char *)&sz, 1); }
  }
  fstCountingWriterWrite(w, (char *)(&(st.val)), 1);
dengyihao's avatar
dengyihao 已提交
314 315 316 317 318 319 320 321 322
  return;
}

// set_comm_input
void fstStateSetCommInput(FstState* s, uint8_t inp) {
  assert(s->state == OneTransNext || s->state == OneTrans);

  uint8_t val;
  COMMON_INDEX(inp, 0x111111, val); 
dengyihao's avatar
dengyihao 已提交
323
  s->val = (s->val & fstStateDict[s->state].val) | val;    
dengyihao's avatar
dengyihao 已提交
324 325 326
}

// comm_input
dengyihao's avatar
dengyihao 已提交
327
uint8_t fstStateCommInput(FstState* s, bool *null) {
dengyihao's avatar
dengyihao 已提交
328 329
  assert(s->state == OneTransNext || s->state == OneTrans);
  uint8_t v = s->val & 0b00111111;
dengyihao's avatar
dengyihao 已提交
330 331 332 333
  if (v == 0) { 
    *null = true; 
    return v;
  } 
dengyihao's avatar
dengyihao 已提交
334 335 336 337 338 339 340 341
  //v = 0 indicate that common_input is None
  return v == 0 ? 0 : COMMON_INPUT(v);  
}

// input_len

uint64_t fstStateInputLen(FstState* s) {
  assert(s->state == OneTransNext || s->state == OneTrans);
dengyihao's avatar
dengyihao 已提交
342 343 344
  bool null = false;
  fstStateCommInput(s, &null); 
  return null ? 1 : 0 ;
dengyihao's avatar
dengyihao 已提交
345
} 
dengyihao's avatar
dengyihao 已提交
346
  
dengyihao's avatar
dengyihao 已提交
347 348
// end_addr 
uint64_t fstStateEndAddrForOneTransNext(FstState* s, FstSlice *data) {
dengyihao's avatar
dengyihao 已提交
349
  assert(s->state == OneTransNext);
dengyihao's avatar
dengyihao 已提交
350 351 352
  return FST_SLICE_LEN(data) - 1 - fstStateInputLen(s);
}
uint64_t fstStateEndAddrForOneTrans(FstState *s, FstSlice *data, PackSizes sizes) {
dengyihao's avatar
dengyihao 已提交
353
  assert(s->state == OneTrans);
dengyihao's avatar
dengyihao 已提交
354 355 356 357 358 359
  return FST_SLICE_LEN(data) 
                - 1 
                - fstStateInputLen(s) 
                - 1 // pack size 
                - FST_GET_TRANSITION_PACK_SIZE(sizes) 
                - FST_GET_OUTPUT_PACK_SIZE(sizes); 
dengyihao's avatar
dengyihao 已提交
360 361
}
uint64_t fstStateEndAddrForAnyTrans(FstState *state, uint64_t version, FstSlice *date, PackSizes sizes, uint64_t nTrans) {
dengyihao's avatar
dengyihao 已提交
362 363 364 365 366 367 368 369 370
  uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(sizes); 
  uint8_t finalOsize = !fstStateIsFinalState(state) ? 0 : oSizes;    
  return FST_SLICE_LEN(date)  
               - 1 
               - fstStateNtransLen(state) 
               - 1 //pack size 
               - fstStateTotalTransSize(state, version, sizes, nTrans) 
               - nTrans * oSizes  // output values
               - finalOsize;     // final output 
dengyihao's avatar
dengyihao 已提交
371 372
}
// input  
dengyihao's avatar
dengyihao 已提交
373 374 375
uint8_t  fstStateInput(FstState *s, FstNode *node) {
  assert(s->state == OneTransNext || s->state == OneTrans);
  FstSlice *slice = &node->data;
dengyihao's avatar
dengyihao 已提交
376 377 378
  bool null = false;
  uint8_t inp = fstStateCommInput(s, &null);
  return null == false ? inp : slice->data[slice->start - 1];
dengyihao's avatar
dengyihao 已提交
379
}
dengyihao's avatar
dengyihao 已提交
380 381 382 383 384 385 386 387 388 389 390
uint8_t  fstStateInputForAnyTrans(FstState *s, FstNode *node, uint64_t i) {
  assert(s->state == AnyTrans);
  FstSlice *slice = &node->data; 

  uint64_t at = node->start 
            - fstStateNtransLen(s) 
            - 1  // pack size
            - fstStateTransIndexSize(s, node->version, node->nTrans) 
            - i 
            - 1; // the output size 
  return slice->data[at];
dengyihao's avatar
dengyihao 已提交
391 392 393
}

// trans_addr
dengyihao's avatar
dengyihao 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407
CompiledAddr fstStateTransAddr(FstState *s, FstNode *node) {
  assert(s->state == OneTransNext || s->state == OneTrans);
  FstSlice *slice = &node->data;
  if (s->state == OneTransNext) {
    return (CompiledAddr)(node->end);      
  } else {
    PackSizes sizes = node->sizes; 
    uint8_t tSizes = FST_GET_TRANSITION_PACK_SIZE(sizes);  
    uint64_t i = node->start 
                - fstStateInputLen(s)
                - 1 // PackSizes
                - tSizes;

    // refactor error logic 
dengyihao's avatar
dengyihao 已提交
408
    return unpackDelta(slice->data + slice->start + i, tSizes, node->end);      
dengyihao's avatar
dengyihao 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422
  }  
}
CompiledAddr fstStateTransAddrForAnyTrans(FstState *s, FstNode *node, uint64_t i) {
  assert(s->state == AnyTrans);

  FstSlice *slice = &node->data;
  uint8_t tSizes = FST_GET_TRANSITION_PACK_SIZE(node->sizes);
  uint64_t at = node->start  
               - fstStateNtransLen(s)
               - 1
               - fstStateTransIndexSize(s, node->version, node->nTrans)
               - node->nTrans
               - (i * tSizes)
               - tSizes;
dengyihao's avatar
dengyihao 已提交
423
  return unpackDelta(slice->data + slice->start + at, tSizes, node->end); 
dengyihao's avatar
dengyihao 已提交
424 425
}

dengyihao's avatar
dengyihao 已提交
426
// sizes 
dengyihao's avatar
dengyihao 已提交
427 428 429 430 431 432 433 434 435 436
PackSizes fstStateSizes(FstState *s, FstSlice *slice) {
  assert(s->state == OneTrans || s->state == AnyTrans) ;
  uint64_t i; 
  if (s->state == OneTrans) {
    i = FST_SLICE_LEN(slice) - 1 - fstStateInputLen(s) - 1;  
  } else {
    i = FST_SLICE_LEN(slice) - 1 - fstStateNtransLen(s) - 1;
  }

  return (PackSizes)(slice->data[slice->start + i]);
dengyihao's avatar
dengyihao 已提交
437 438
}
// Output 
dengyihao's avatar
dengyihao 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
Output fstStateOutput(FstState *s, FstNode *node) {
  assert(s->state == OneTrans);  
  
  uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(node->sizes);
  if (oSizes == 0) {
    return 0;
  }
  FstSlice *slice = &node->data;
  uint8_t tSizes = FST_GET_TRANSITION_PACK_SIZE(node->sizes);

  uint64_t i = node->start 
                - fstStateInputLen(s);
                - 1
                - tSizes 
                - oSizes;
  return unpackUint64(slice->data + slice->start + i, oSizes);
dengyihao's avatar
dengyihao 已提交
455
  
dengyihao's avatar
dengyihao 已提交
456
}
dengyihao's avatar
dengyihao 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
Output fstStateOutputForAnyTrans(FstState *s, FstNode *node, uint64_t i) {
  assert(s->state == AnyTrans);

  uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(node->sizes); 
  if (oSizes == 0) {
    return 0;    
  }  
  FstSlice *slice = &node->data;
  uint64_t at = node->start
                - fstStateNtransLen(s)
                - 1 // pack size
                - fstStateTotalTransSize(s, node->version, node->sizes, node->nTrans)
                - (i * oSizes)
                - oSizes;
  return unpackUint64(slice->data + slice->start + at, oSizes);
dengyihao's avatar
dengyihao 已提交
472 473 474 475
}

// anyTrans specify function

dengyihao's avatar
dengyihao 已提交
476 477 478
void fstStateSetFinalState(FstState *s, bool yes) {
  assert(s->state == AnyTrans); 
  if (yes) { s->val |= 0b01000000; } 
dengyihao's avatar
dengyihao 已提交
479 480
  return;
}
dengyihao's avatar
dengyihao 已提交
481 482 483
bool fstStateIsFinalState(FstState *s) {
  assert(s->state == AnyTrans); 
  return (s->val & 0b01000000) == 0b01000000; 
dengyihao's avatar
dengyihao 已提交
484
} 
dengyihao's avatar
dengyihao 已提交
485 486 487 488 489 490

void fstStateSetStateNtrans(FstState *s, uint8_t n) {
  assert(s->state == AnyTrans); 
  if (n <= 0b00111111) {
    s->val = (s->val & 0b11000000) | n;  
  } 
dengyihao's avatar
dengyihao 已提交
491 492 493
  return;
}
// state_ntrans
dengyihao's avatar
dengyihao 已提交
494 495 496 497 498 499 500 501 502
uint8_t fstStateStateNtrans(FstState *s, bool *null) {
  assert(s->state == AnyTrans); 
  *null = false;
  uint8_t n = s->val & 0b00111111; 

  if (n == 0) {
    *null = true; // None
  }  
  return n;
dengyihao's avatar
dengyihao 已提交
503
}
dengyihao's avatar
dengyihao 已提交
504 505 506 507
uint64_t fstStateTotalTransSize(FstState *s, uint64_t version, PackSizes sizes, uint64_t nTrans) {
  assert(s->state == AnyTrans); 
  uint64_t idxSize = fstStateTransIndexSize(s, version, nTrans); 
  return nTrans + (nTrans * FST_GET_TRANSITION_PACK_SIZE(sizes)) + idxSize;
dengyihao's avatar
dengyihao 已提交
508
}
dengyihao's avatar
dengyihao 已提交
509 510 511
uint64_t fstStateTransIndexSize(FstState *s, uint64_t version, uint64_t nTrans) {
  assert(s->state == AnyTrans); 
  return (version >= 2 &&nTrans > TRANS_INDEX_THRESHOLD) ?  256 : 0;
dengyihao's avatar
dengyihao 已提交
512
}
dengyihao's avatar
dengyihao 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
uint64_t fstStateNtransLen(FstState *s) {
  assert(s->state == AnyTrans);
  bool null = false;
  fstStateStateNtrans(s, &null);
  return null == true ?  1 : 0; 
}
uint64_t fstStateNtrans(FstState *s, FstSlice *slice) {
  bool null = false; 
  uint8_t n = fstStateStateNtrans(s, &null);
  if (null != true) {
    return n;   
  }  
  n = slice->data[slice->end - 1]; // data[data.len() - 2]
  return n == 1 ? 256: n; // // "1" is never a normal legal value here, because if there, // is only 1 transition, then it is encoded in the state byte  
}
Output  fstStateFinalOutput(FstState *s, uint64_t version, FstSlice *slice, PackSizes sizes, uint64_t nTrans) {
   uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(sizes);
   if (oSizes == 0 || !fstStateIsFinalState(s)) {
      return 0;
   }
   
   uint64_t at = FST_SLICE_LEN(slice) 
                 - 1 
                 - fstStateNtransLen(s)
                 - fstStateTotalTransSize(s, version, sizes, nTrans)
                 - (nTrans * oSizes)
                 - oSizes;
  return unpackUint64(slice->data + slice->start + at, (uint8_t)oSizes);    

}
uint64_t fstStateFindInput(FstState *s, FstNode *node, uint8_t b, bool *null) {
  assert(s->state == AnyTrans);
  FstSlice *slice = &node->data;
  if (node->version >= 2 && node->nTrans > TRANS_INDEX_THRESHOLD) {
    uint64_t at = node->start
                  - fstStateNtransLen(s)
                  - 1 // pack size 
                  - fstStateTransIndexSize(s, node->version, node->nTrans);
    uint64_t i = slice->data[slice->start + at + b];  
    if (i >= node->nTrans) {
      *null = true;
    } 
    return i;
  } else {
    uint64_t start = node->start 
                    - fstStateNtransLen(s)
                    - 1 // pack size
                    - node->nTrans;
    uint64_t end =  start + node->nTrans;
    uint64_t len = end - start; 
    for(int i = 0; i < len; i++) {
      uint8_t v = slice->data[slice->start + i];
      if (v == b) {
        return node->nTrans - i - 1; // bug  
      }
    } 
  } 
dengyihao's avatar
dengyihao 已提交
570 571 572
}


dengyihao's avatar
dengyihao 已提交
573
// fst node function 
dengyihao's avatar
dengyihao 已提交
574 575

FstNode *fstNodeCreate(int64_t version, CompiledAddr addr, FstSlice *slice) {
dengyihao's avatar
dengyihao 已提交
576 577 578
  FstNode *n = (FstNode *)malloc(sizeof(FstNode)); 
  if (n == NULL) { return NULL; }

dengyihao's avatar
dengyihao 已提交
579
  FstState st = fstStateCreateFrom(slice, addr);  
dengyihao's avatar
dengyihao 已提交
580 581

  if (st.state == EmptyFinal) {
dengyihao's avatar
dengyihao 已提交
582
     n->data    = fstSliceCreate(NULL, 0);   
dengyihao's avatar
dengyihao 已提交
583
     n->version = version;
dengyihao's avatar
dengyihao 已提交
584
     n->state   = st;  
dengyihao's avatar
dengyihao 已提交
585 586 587 588 589
     n->start   = EMPTY_ADDRESS;
     n->end     = EMPTY_ADDRESS;  
     n->isFinal = true; 
     n->nTrans  = 0;
     n->sizes   = 0;  
dengyihao's avatar
dengyihao 已提交
590
     n->finalOutput = 0;  
dengyihao's avatar
dengyihao 已提交
591
  } else if (st.state == OneTransNext) {
dengyihao's avatar
dengyihao 已提交
592 593
     n->data    = fstSliceCopy(slice, 0, addr);     
     n->version = version;
dengyihao's avatar
dengyihao 已提交
594
     n->state   = st;  
dengyihao's avatar
dengyihao 已提交
595
     n->start   = addr;
dengyihao's avatar
dengyihao 已提交
596
     n->end     = fstStateEndAddrForOneTransNext(&st, slice); //? s.end_addr(data); 
dengyihao's avatar
dengyihao 已提交
597 598
     n->isFinal = false;
     n->sizes   = 0;
dengyihao's avatar
dengyihao 已提交
599
     n->nTrans  = 1;
dengyihao's avatar
dengyihao 已提交
600
     n->finalOutput = 0;
dengyihao's avatar
dengyihao 已提交
601
  } else if (st.state == OneTrans) {
dengyihao's avatar
dengyihao 已提交
602 603 604
     FstSlice data = fstSliceCopy(slice, 0, addr); 
     PackSizes sz = fstStateSizes(&st, &data);
     n->data    =   fstSliceCopy(slice, 0, addr); 
dengyihao's avatar
dengyihao 已提交
605
     n->version = version; 
dengyihao's avatar
dengyihao 已提交
606
     n->state   = st; 
dengyihao's avatar
dengyihao 已提交
607
     n->start   = addr;
dengyihao's avatar
dengyihao 已提交
608
     n->end     = fstStateEndAddrForOneTrans(&st, slice, sz); // s.end_addr(data, sz);
dengyihao's avatar
dengyihao 已提交
609 610 611 612
     n->isFinal = false; 
     n->nTrans  = 1; 
     n->sizes   = sz;   
     n->finalOutput = 0; 
dengyihao's avatar
dengyihao 已提交
613
  } else {
dengyihao's avatar
dengyihao 已提交
614 615
     uint64_t sz = fstStateSizes(&st, slice);    // s.sizes(data)
     uint32_t nTrans = fstStateNtrans(&st, slice); // s.ntrans(data)  
dengyihao's avatar
dengyihao 已提交
616 617
     n->data    = *slice; 
     n->version = version;
dengyihao's avatar
dengyihao 已提交
618
     n->state   = st;
dengyihao's avatar
dengyihao 已提交
619
     n->start   = addr;
dengyihao's avatar
dengyihao 已提交
620 621
     n->end     = fstStateEndAddrForAnyTrans(&st, version, slice, sz, nTrans); // s.end_addr(version, data, sz, ntrans);
     n->isFinal = fstStateIsFinalState(&st); // s.is_final_state();
dengyihao's avatar
dengyihao 已提交
622 623
     n->nTrans  = nTrans;
     n->sizes   = sz;
dengyihao's avatar
dengyihao 已提交
624
     n->finalOutput = fstStateFinalOutput(&st, version, slice, sz, nTrans); // s.final_output(version, data, sz, ntrans);
dengyihao's avatar
dengyihao 已提交
625
  }
dengyihao's avatar
dengyihao 已提交
626 627
   return n; 
}
dengyihao's avatar
dengyihao 已提交
628 629 630 631 632 633 634 635

// debug state transition
static const char *fstNodeState(FstNode *node) {
  FstState *st = &node->state; 
  return fstStateStr[st->state]; 
}


dengyihao's avatar
dengyihao 已提交
636 637 638
void fstNodeDestroy(FstNode *node) {
  free(node);
}
dengyihao's avatar
dengyihao 已提交
639 640 641 642 643 644 645
FstTransitions* fstNodeTransitions(FstNode *node) {
  FstTransitions *t = malloc(sizeof(FstTransitions));
  if (NULL == t) {
    return NULL; 
  }
  FstRange range = {.start = 0, .end = FST_NODE_LEN(node)};
  t->range = range;
dengyihao's avatar
dengyihao 已提交
646
  t->node  = node;
dengyihao's avatar
dengyihao 已提交
647 648
  return t; 
} 
dengyihao's avatar
dengyihao 已提交
649

dengyihao's avatar
dengyihao 已提交
650 651 652
// Returns the transition at index `i`. 
bool fstNodeGetTransitionAt(FstNode *node, uint64_t i, FstTransition *trn) {
  bool s = true;
dengyihao's avatar
dengyihao 已提交
653 654 655
  FstState *st = &node->state;
  if (st->state == OneTransNext) {
    trn->inp  = fstStateInput(st, node); 
dengyihao's avatar
dengyihao 已提交
656
    trn->out  = 0;
dengyihao's avatar
dengyihao 已提交
657 658 659 660 661 662 663 664 665
    trn->addr = fstStateTransAddr(st, node); 
  } else if (st->state == OneTrans) {
    trn->inp  = fstStateInput(st, node);    
    trn->out  = fstStateOutput(st, node);
    trn->addr = fstStateTransAddr(st, node); 
  } else if (st->state == AnyTrans) {
    trn->inp  = fstStateInputForAnyTrans(st, node, i);
    trn->out  = fstStateOutputForAnyTrans(st, node, i);
    trn->addr = fstStateTransAddrForAnyTrans(st, node, i); 
dengyihao's avatar
dengyihao 已提交
666 667 668 669 670 671
  } else {
    s = false;
  }
  return s;
} 

dengyihao's avatar
dengyihao 已提交
672
// Returns the transition address of the `i`th transition
dengyihao's avatar
dengyihao 已提交
673 674
bool fstNodeGetTransitionAddrAt(FstNode *node, uint64_t i, CompiledAddr *res) {
  bool s = true;
dengyihao's avatar
dengyihao 已提交
675 676 677 678 679 680 681 682 683
  FstState *st = &node->state;
  if (st->state == OneTransNext) {
    assert(i == 0);
    fstStateTransAddr(st, node);
  } else if (st->state == OneTrans) {
    assert(i == 0); 
    fstStateTransAddr(st, node);
  } else if (st->state == AnyTrans) {
    fstStateTransAddrForAnyTrans(st, node, i);
dengyihao's avatar
dengyihao 已提交
684
  } else if (FST_STATE_EMPTY_FINAL(node)){
dengyihao's avatar
dengyihao 已提交
685
    s = false;
dengyihao's avatar
dengyihao 已提交
686 687
  } else {
    assert(0);
dengyihao's avatar
dengyihao 已提交
688 689
  }
  return s;
dengyihao's avatar
dengyihao 已提交
690 691
}

dengyihao's avatar
dengyihao 已提交
692 693
//  Finds the `i`th transition corresponding to the given input byte.
//  If no transition for this byte exists, then `false` is returned. 
dengyihao's avatar
dengyihao 已提交
694 695
bool fstNodeFindInput(FstNode *node, uint8_t b, uint64_t *res) {
  bool s = true;
dengyihao's avatar
dengyihao 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708
  FstState *st = &node->state;
  if (st->state == OneTransNext) {
    if (fstStateInput(st,node) == b) { *res = 0; } 
    else { s = false; } } 
  else if (st->state == OneTrans) {
    if (fstStateInput(st, node) == b) { *res = 0 ;}
    else {  s = false; }
  } else if (st->state == AnyTrans) {
    bool null = false;
    uint64_t out = fstStateFindInput(st, node, b, &null); 
    if (null == false) { *res = out; } 
    else { s = false;}
  }
dengyihao's avatar
dengyihao 已提交
709 710 711 712 713 714 715 716 717
  return s;
} 

bool fstNodeCompile(FstNode *node, void *w, CompiledAddr lastAddr, CompiledAddr addr, FstBuilderNode *builderNode) {
  size_t sz = taosArrayGetSize(builderNode->trans);  
  assert(sz < 256);
  if (sz == 0 && builderNode->isFinal && builderNode->finalOutput == 0) {
    return true; 
  } else if (sz != 1 || builderNode->isFinal) {
dengyihao's avatar
dengyihao 已提交
718
     fstStateCompileForAnyTrans(w, addr, builderNode); 
dengyihao's avatar
dengyihao 已提交
719 720 721 722
    // AnyTrans->Compile(w, addr, node);
  } else {
    FstTransition *tran = taosArrayGet(builderNode->trans, 0);   
    if (tran->addr == lastAddr && tran->out == 0) {
dengyihao's avatar
dengyihao 已提交
723
       fstStateCompileForOneTransNext(w, addr, tran->inp);
dengyihao's avatar
dengyihao 已提交
724 725 726
       //OneTransNext::compile(w, lastAddr, tran->inp);
       return true;
    } else {
dengyihao's avatar
dengyihao 已提交
727
       fstStateCompileForOneTrans(w, addr, tran);
dengyihao's avatar
dengyihao 已提交
728 729 730 731 732 733 734 735 736 737 738 739
      //OneTrans::Compile(w, lastAddr, *tran);
       return true;
    } 
  } 
  return true; 
} 


FstBuilder *fstBuilderCreate(void *w, FstType ty) {
  FstBuilder *b = malloc(sizeof(FstBuilder));  
  if (NULL == b) { return b; }

dengyihao's avatar
dengyihao 已提交
740 741 742 743
   
  b->wrt = fstCountingWriterCreate(w);
  b->unfinished = fstUnFinishedNodesCreate();   
  b->registry   = fstRegistryCreate(10000, 2) ;
dengyihao's avatar
dengyihao 已提交
744
  b->last       = fstSliceCreate(NULL, 0);
dengyihao's avatar
dengyihao 已提交
745 746
  b->lastAddr   = NONE_ADDRESS; 
  b->len        = 0;
dengyihao's avatar
dengyihao 已提交
747 748
  return b;
}
dengyihao's avatar
dengyihao 已提交
749 750
void fstBuilderDestroy(FstBuilder *b) {
  if (b == NULL) { return; }
dengyihao's avatar
dengyihao 已提交
751

dengyihao's avatar
dengyihao 已提交
752 753 754 755 756
  fstCountingWriterDestroy(b->wrt); 
  fstUnFinishedNodesDestroy(b->unfinished); 
  fstRegistryDestroy(b->registry);
  free(b);
}
dengyihao's avatar
dengyihao 已提交
757 758 759 760 761 762 763 764 765 766 767 768


bool fstBuilderInsert(FstBuilder *b, FstSlice bs, Output in) {
  OrderType t = fstBuilderCheckLastKey(b, bs, true);  
  if (t == Ordered) {
    // add log info
    fstBuilderInsertOutput(b, bs, in); 
    return true; 
  } 
  return false;
}

dengyihao's avatar
dengyihao 已提交
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
void fstBuilderInsertOutput(FstBuilder *b, FstSlice bs, Output in) {
   FstSlice *s = &bs;
   if (fstSliceEmpty(s)) {
     b->len = 1; 
     fstUnFinishedNodesSetRootOutput(b->unfinished, in);
     return;
   }
   Output out; 
   uint64_t prefixLen;
   if (in != 0) { //if let Some(in) = in 
      prefixLen = fstUnFinishedNodesFindCommPrefixAndSetOutput(b->unfinished, bs, in, &out);  
   } else {
      prefixLen = fstUnFinishedNodesFindCommPrefix(b->unfinished, bs);
      out = 0;
   }

   if (prefixLen == FST_SLICE_LEN(s)) {
dengyihao's avatar
dengyihao 已提交
786
      assert(out == 0);
dengyihao's avatar
dengyihao 已提交
787 788 789 790 791 792 793 794 795 796
      return;
   }

   b->len += 1;
   fstBuilderCompileFrom(b, prefixLen); 
   
   FstSlice sub = fstSliceCopy(s, prefixLen, s->end);
   fstUnFinishedNodesAddSuffix(b->unfinished, sub, out);
   return;
 }
dengyihao's avatar
dengyihao 已提交
797

dengyihao's avatar
dengyihao 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
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;
dengyihao's avatar
dengyihao 已提交
814
} 
dengyihao's avatar
dengyihao 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
void fstBuilderCompileFrom(FstBuilder *b, uint64_t istate) {
  CompiledAddr addr = NONE_ADDRESS;
  while (istate + 1 < FST_UNFINISHED_NODES_LEN(b->unfinished)) {
    FstBuilderNode *n = NULL;
    if (addr == NONE_ADDRESS) {
      n = fstUnFinishedNodesPopEmpty(b->unfinished);
    } else {
      n = fstUnFinishedNodesPopFreeze(b->unfinished, addr);
    }
    addr =  fstBuilderCompile(b, n);
    assert(addr != NONE_ADDRESS);      
    fstBuilderNodeDestroy(n);
  }
  fstUnFinishedNodesTopLastFreeze(b->unfinished, addr);
  return; 
}
dengyihao's avatar
dengyihao 已提交
831 832 833 834 835 836 837 838 839
CompiledAddr fstBuilderCompile(FstBuilder *b, FstBuilderNode *bn) {
  if (FST_BUILDER_NODE_IS_FINAL(bn) 
      && FST_BUILDER_NODE_TRANS_ISEMPTY(bn) 
      && FST_BUILDER_NODE_FINALOUTPUT_ISZERO(bn)) {
    return EMPTY_ADDRESS; 
  }
  FstRegistryEntry *entry = fstRegistryGetEntry(b->registry, bn); 
  if (entry->state == FOUND) { 
    CompiledAddr ret = entry->addr;
dengyihao's avatar
dengyihao 已提交
840
    fstRegistryEntryDestroy(entry);
dengyihao's avatar
dengyihao 已提交
841 842 843 844 845
    return ret;
  } 
  CompiledAddr startAddr = (CompiledAddr)(FST_WRITER_COUNT(b->wrt));

  fstBuilderNodeCompileTo(bn, b->wrt, b->lastAddr, startAddr);  
dengyihao's avatar
dengyihao 已提交
846
  b->lastAddr =  (CompiledAddr)(FST_WRITER_COUNT(b->wrt) - 1);  
dengyihao's avatar
dengyihao 已提交
847 848 849
  if (entry->state == NOTFOUND) {
    FST_REGISTRY_CELL_INSERT(entry->cell, b->lastAddr);    
  }
dengyihao's avatar
dengyihao 已提交
850 851
  fstRegistryEntryDestroy(entry);
  
dengyihao's avatar
dengyihao 已提交
852 853 854
  return b->lastAddr;  
}

dengyihao's avatar
dengyihao 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
void* fstBuilderInsertInner(FstBuilder *b) {
  fstBuilderCompileFrom(b, 0);  
  FstBuilderNode *rootNode = fstUnFinishedNodesPopRoot(b->unfinished); 
  CompiledAddr  rootAddr = fstBuilderCompile(b, rootNode);

  uint8_t buf64[8] = {0}; 

  taosEncodeFixedU64((void **)&buf64, b->len); 
  fstCountingWriterWrite(b->wrt, buf64, sizeof(buf64));  
    
  taosEncodeFixedU64((void **)&buf64, rootAddr);  
  fstCountingWriterWrite(b->wrt, buf64, sizeof(buf64));  

  uint8_t buf32[4] = {0};
  uint32_t sum = fstCountingWriterMaskedCheckSum(b->wrt);
  taosEncodeFixedU32((void **)&buf32, sum); 
  fstCountingWriterWrite(b->wrt, buf32, sizeof(buf32));  
  
  fstCountingWriterFlush(b->wrt);
  return b->wrt;
  
}
void fstBuilderFinish(FstBuilder *b) {
  fstBuilderInsertInner(b);
}
dengyihao's avatar
dengyihao 已提交
880

dengyihao's avatar
dengyihao 已提交
881 882


dengyihao's avatar
dengyihao 已提交
883 884 885 886 887
FstSlice fstNodeAsSlice(FstNode *node) {
  FstSlice *slice = &node->data; 
  FstSlice s = fstSliceCopy(slice, slice->end, slice->dLen - 1);   
  return s; 
}
dengyihao's avatar
dengyihao 已提交
888

dengyihao's avatar
dengyihao 已提交
889 890 891 892 893 894 895 896 897 898 899 900
FstLastTransition *fstLastTransitionCreate(uint8_t inp, Output out) {
  FstLastTransition *trn = malloc(sizeof(FstLastTransition));
  if (trn == NULL) { return NULL; }

  trn->inp = inp;
  trn->out = out;
  return trn;
}

void fstLastTransitionDestroy(FstLastTransition *trn) {
  free(trn);
}
dengyihao's avatar
dengyihao 已提交
901 902
void fstBuilderNodeUnfinishedLastCompiled(FstBuilderNodeUnfinished *unNode, CompiledAddr addr) {
  FstLastTransition *trn = unNode->last;       
dengyihao's avatar
dengyihao 已提交
903 904
  if (trn == NULL) { return; }  

dengyihao's avatar
dengyihao 已提交
905 906
  FstTransition t = {.inp = trn->inp, .out = trn->out, .addr = addr};      
  taosArrayPush(unNode->node->trans, &t); 
dengyihao's avatar
dengyihao 已提交
907 908 909
  return;
}

dengyihao's avatar
dengyihao 已提交
910 911 912 913 914 915 916 917 918 919 920 921
void fstBuilderNodeUnfinishedAddOutputPrefix(FstBuilderNodeUnfinished *unNode, Output out) {
  if (FST_BUILDER_NODE_IS_FINAL(unNode->node)) {
    unNode->node->finalOutput += out;  
  }
  size_t sz = taosArrayGetSize(unNode->node->trans);
  for (size_t i = 0; i < sz; i++) {
    FstTransition *trn = taosArrayGet(unNode->node->trans, i); 
    trn->out += out;
  }
  if (unNode->last) {
    unNode->last->out += out;  
  }
dengyihao's avatar
dengyihao 已提交
922 923 924
  return;
}

dengyihao's avatar
dengyihao 已提交
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
Fst* fstCreate(FstSlice *slice) {
  
  char *buf = slice->data;
  uint64_t skip = 0;  
  uint64_t len = slice->dLen;
  if (len < 36) { 
    return NULL; 
  }

  uint64_t version; 
  taosDecodeFixedU64(buf, &version); 
  skip += sizeof(version); 
  if (version == 0 || version > VERSION) {
    return NULL; 
  }  

  uint64_t type;
  taosDecodeFixedU64(buf + skip, &type);
  skip += sizeof(type); 

  uint32_t checkSum = 0;
  len -= sizeof(checkSum);
  taosDecodeFixedU32(buf + len, &checkSum); 

  CompiledAddr rootAddr;
  len -= sizeof(rootAddr); 
  taosDecodeFixedU64(buf + len, &rootAddr); 

  uint64_t fstLen; 
  len -= sizeof(fstLen); 
  taosDecodeFixedU64(buf + len, &fstLen);
  //TODO(validat root addr)
  // 
  Fst *fst= (Fst *)calloc(1, sizeof(Fst)); 
  if (fst == NULL) { return NULL; }  
  
  fst->meta = (FstMeta *)malloc(sizeof(FstMeta));
  if (NULL == fst->meta) { 
    goto FST_CREAT_FAILED; 
  }

  fst->meta->version   = version;  
  fst->meta->rootAddr = rootAddr; 
  fst->meta->ty       = type;
  fst->meta->len      = fstLen;
  fst->meta->checkSum = checkSum;
  return fst;

FST_CREAT_FAILED: 
  free(fst->meta); 
  free(fst);

}
void fstDestroy(Fst *fst) {

}

dengyihao's avatar
dengyihao 已提交
982