index_fst.c 42.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"
17
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
18
#include "index_fst_automation.h"
19 20
#include "tchecksum.h"
#include "tcoding.h"
dengyihao's avatar
dengyihao 已提交
21

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

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

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

49
  taosArrayDestroyEx(nodes->stack, unFinishedNodeDestroyElem);
dengyihao's avatar
dengyihao 已提交
50 51 52
  free(nodes);
}

dengyihao's avatar
dengyihao 已提交
53 54
void fstUnFinishedNodesPushEmpty(FstUnFinishedNodes* nodes, bool isFinal) {
  FstBuilderNode* node = malloc(sizeof(FstBuilderNode));
55
  node->isFinal = isFinal;
dengyihao's avatar
dengyihao 已提交
56
  node->finalOutput = 0;
57
  node->trans = taosArrayInit(16, sizeof(FstTransition));
dengyihao's avatar
dengyihao 已提交
58

59
  FstBuilderNodeUnfinished un = {.node = node, .last = NULL};
dengyihao's avatar
dengyihao 已提交
60 61
  taosArrayPush(nodes->stack, &un);
}
dengyihao's avatar
dengyihao 已提交
62
FstBuilderNode* fstUnFinishedNodesPopRoot(FstUnFinishedNodes* nodes) {
dengyihao's avatar
dengyihao 已提交
63 64
  assert(taosArrayGetSize(nodes->stack) == 1);

dengyihao's avatar
dengyihao 已提交
65
  FstBuilderNodeUnfinished* un = taosArrayPop(nodes->stack);
66 67
  assert(un->last == NULL);
  return un->node;
dengyihao's avatar
dengyihao 已提交
68 69
}

dengyihao's avatar
dengyihao 已提交
70 71
FstBuilderNode* fstUnFinishedNodesPopFreeze(FstUnFinishedNodes* nodes, CompiledAddr addr) {
  FstBuilderNodeUnfinished* un = taosArrayPop(nodes->stack);
dengyihao's avatar
dengyihao 已提交
72
  fstBuilderNodeUnfinishedLastCompiled(un, addr);
73 74 75
  // free(un->last); // TODO add func FstLastTransitionFree()
  // un->last = NULL;
  return un->node;
dengyihao's avatar
dengyihao 已提交
76 77
}

dengyihao's avatar
dengyihao 已提交
78 79
FstBuilderNode* fstUnFinishedNodesPopEmpty(FstUnFinishedNodes* nodes) {
  FstBuilderNodeUnfinished* un = taosArrayPop(nodes->stack);
80 81
  assert(un->last == NULL);
  return un->node;
dengyihao's avatar
dengyihao 已提交
82
}
dengyihao's avatar
dengyihao 已提交
83 84
void fstUnFinishedNodesSetRootOutput(FstUnFinishedNodes* nodes, Output out) {
  FstBuilderNodeUnfinished* un = taosArrayGet(nodes->stack, 0);
85
  un->node->isFinal = true;
dengyihao's avatar
dengyihao 已提交
86
  un->node->finalOutput = out;
87 88
  // un->node->trans       = NULL;
}
dengyihao's avatar
dengyihao 已提交
89 90
void fstUnFinishedNodesTopLastFreeze(FstUnFinishedNodes* nodes, CompiledAddr addr) {
  FstBuilderNodeUnfinished* un = taosArrayGet(nodes->stack, taosArrayGetSize(nodes->stack) - 1);
dengyihao's avatar
dengyihao 已提交
91 92
  fstBuilderNodeUnfinishedLastCompiled(un, addr);
}
dengyihao's avatar
dengyihao 已提交
93 94
void fstUnFinishedNodesAddSuffix(FstUnFinishedNodes* nodes, FstSlice bs, Output out) {
  FstSlice* s = &bs;
dengyihao's avatar
dengyihao 已提交
95
  if (fstSliceIsEmpty(s)) { return; }
96
  size_t                    sz = taosArrayGetSize(nodes->stack) - 1;
dengyihao's avatar
dengyihao 已提交
97
  FstBuilderNodeUnfinished* un = taosArrayGet(nodes->stack, sz);
dengyihao's avatar
dengyihao 已提交
98 99
  assert(un->last == NULL);

100 101 102 103
  // FstLastTransition *trn = malloc(sizeof(FstLastTransition));
  // trn->inp = s->data[s->start];
  // trn->out = out;
  int32_t  len = 0;
dengyihao's avatar
dengyihao 已提交
104
  uint8_t* data = fstSliceData(s, &len);
105
  un->last = fstLastTransitionCreate(data[0], out);
dengyihao's avatar
dengyihao 已提交
106

dengyihao's avatar
dengyihao 已提交
107
  for (uint64_t i = 1; i < len; i++) {
dengyihao's avatar
dengyihao 已提交
108
    FstBuilderNode* n = malloc(sizeof(FstBuilderNode));
109
    n->isFinal = false;
dengyihao's avatar
dengyihao 已提交
110
    n->finalOutput = 0;
111 112 113 114 115
    n->trans = taosArrayInit(16, sizeof(FstTransition));

    // FstLastTransition *trn = malloc(sizeof(FstLastTransition));
    // trn->inp = s->data[i];
    // trn->out = out;
dengyihao's avatar
dengyihao 已提交
116
    FstLastTransition* trn = fstLastTransitionCreate(data[i], 0);
dengyihao's avatar
dengyihao 已提交
117

118 119
    FstBuilderNodeUnfinished un = {.node = n, .last = trn};
    taosArrayPush(nodes->stack, &un);
dengyihao's avatar
dengyihao 已提交
120
  }
121
  fstUnFinishedNodesPushEmpty(nodes, true);
dengyihao's avatar
dengyihao 已提交
122 123
}

dengyihao's avatar
dengyihao 已提交
124 125
uint64_t fstUnFinishedNodesFindCommPrefix(FstUnFinishedNodes* node, FstSlice bs) {
  FstSlice* s = &bs;
dengyihao's avatar
dengyihao 已提交
126

127
  size_t   ssz = taosArrayGetSize(node->stack);  // stack size
dengyihao's avatar
dengyihao 已提交
128
  uint64_t count = 0;
129
  int32_t  lsz;  // data len
dengyihao's avatar
dengyihao 已提交
130
  uint8_t* data = fstSliceData(s, &lsz);
dengyihao's avatar
dengyihao 已提交
131
  for (size_t i = 0; i < ssz && i < lsz; i++) {
dengyihao's avatar
dengyihao 已提交
132
    FstBuilderNodeUnfinished* un = taosArrayGet(node->stack, i);
dengyihao's avatar
dengyihao 已提交
133
    if (un->last->inp == data[i]) {
dengyihao's avatar
dengyihao 已提交
134 135 136
      count++;
    } else {
      break;
137
    }
dengyihao's avatar
dengyihao 已提交
138 139 140
  }
  return count;
}
dengyihao's avatar
dengyihao 已提交
141 142
uint64_t fstUnFinishedNodesFindCommPrefixAndSetOutput(FstUnFinishedNodes* node, FstSlice bs, Output in, Output* out) {
  FstSlice* s = &bs;
dengyihao's avatar
dengyihao 已提交
143

144 145
  size_t lsz = (size_t)(s->end - s->start + 1);  // data len
  size_t ssz = taosArrayGetSize(node->stack);    // stack size
dengyihao's avatar
dengyihao 已提交
146
  *out = in;
dengyihao's avatar
dengyihao 已提交
147 148
  uint64_t i = 0;
  for (i = 0; i < lsz && i < ssz; i++) {
dengyihao's avatar
dengyihao 已提交
149
    FstBuilderNodeUnfinished* un = taosArrayGet(node->stack, i);
dengyihao's avatar
dengyihao 已提交
150

dengyihao's avatar
dengyihao 已提交
151
    FstLastTransition* t = un->last;
152
    uint64_t           addPrefix = 0;
dengyihao's avatar
dengyihao 已提交
153
    uint8_t*           data = fstSliceData(s, NULL);
dengyihao's avatar
dengyihao 已提交
154
    if (t && t->inp == data[i]) {
155 156 157
      uint64_t commPrefix = MIN(t->out, *out);
      uint64_t tAddPrefix = t->out - commPrefix;
      (*out) = (*out) - commPrefix;
dengyihao's avatar
dengyihao 已提交
158
      t->out = commPrefix;
159
      addPrefix = tAddPrefix;
dengyihao's avatar
dengyihao 已提交
160
    } else {
161
      break;
dengyihao's avatar
dengyihao 已提交
162 163
    }
    if (addPrefix != 0) {
dengyihao's avatar
dengyihao 已提交
164
      if (i + 1 < ssz) {
dengyihao's avatar
dengyihao 已提交
165
        FstBuilderNodeUnfinished* unf = taosArrayGet(node->stack, i + 1);
166
        fstBuilderNodeUnfinishedAddOutputPrefix(unf, addPrefix);
dengyihao's avatar
dengyihao 已提交
167
      }
dengyihao's avatar
dengyihao 已提交
168
    }
169
  }
dengyihao's avatar
dengyihao 已提交
170
  return i;
171
}
dengyihao's avatar
dengyihao 已提交
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
  if (addr == EMPTY_ADDRESS) { return fs; }
176

dengyihao's avatar
dengyihao 已提交
177
  uint8_t* data = fstSliceData(slice, NULL);
178 179
  uint8_t  v = data[addr];
  uint8_t  t = (v & 0b11000000) >> 6;
dengyihao's avatar
dengyihao 已提交
180 181 182
  if (t == 0b11) {
    fs.state = OneTransNext;
  } else if (t == 0b10) {
183
    fs.state = OneTrans;
dengyihao's avatar
dengyihao 已提交
184
  } else {
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 192 193 194
static FstState fstStateDict[] = {{.state = OneTransNext, .val = 0b11000000},
                                  {.state = OneTrans, .val = 0b10000000},
                                  {.state = AnyTrans, .val = 0b00000000},
                                  {.state = EmptyFinal, .val = 0b00000000}};
195
// debug
dengyihao's avatar
dengyihao 已提交
196
static const char* fstStateStr[] = {"ONE_TRANS_NEXT", "ONE_TRANS", "ANY_TRANS", "EMPTY_FINAL"};
dengyihao's avatar
dengyihao 已提交
197

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

207
  bool    null = false;
dengyihao's avatar
dengyihao 已提交
208 209 210
  uint8_t v = fstStateCommInput(&s, &null);
  if (null) {
    // w->write_all(&[inp])
dengyihao's avatar
dengyihao 已提交
211
    fstCountingWriterWrite(w, &inp, 1);
212
  }
dengyihao's avatar
dengyihao 已提交
213
  fstCountingWriterWrite(w, &(s.val), 1);
dengyihao's avatar
dengyihao 已提交
214
  // w->write_all(&[s.val])
dengyihao's avatar
dengyihao 已提交
215
  return;
dengyihao's avatar
dengyihao 已提交
216
}
dengyihao's avatar
dengyihao 已提交
217
void fstStateCompileForOneTrans(FstCountingWriter* w, CompiledAddr addr, FstTransition* trn) {
218 219 220
  Output    out = trn->out;
  uint8_t   outPackSize = (out == 0 ? 0 : fstCountingWriterPackUint(w, out));
  uint8_t   transPackSize = fstPackDetla(w, addr, trn->addr);
dengyihao's avatar
dengyihao 已提交
221 222 223 224
  PackSizes packSizes = 0;

  FST_SET_OUTPUT_PACK_SIZE(packSizes, outPackSize);
  FST_SET_TRANSITION_PACK_SIZE(packSizes, transPackSize);
dengyihao's avatar
dengyihao 已提交
225
  fstCountingWriterWrite(w, (char*)&packSizes, sizeof(packSizes));
226 227

  FstState st = fstStateCreate(OneTrans);
dengyihao's avatar
dengyihao 已提交
228 229

  fstStateSetCommInput(&st, trn->inp);
230 231
  bool    null = false;
  uint8_t inp = fstStateCommInput(&st, &null);
dengyihao's avatar
dengyihao 已提交
232 233
  if (null == true) { fstCountingWriterWrite(w, (char*)&trn->inp, sizeof(trn->inp)); }
  fstCountingWriterWrite(w, (char*)(&(st.val)), sizeof(st.val));
234
  return;
dengyihao's avatar
dengyihao 已提交
235
}
dengyihao's avatar
dengyihao 已提交
236
void fstStateCompileForAnyTrans(FstCountingWriter* w, CompiledAddr addr, FstBuilderNode* node) {
237
  size_t sz = taosArrayGetSize(node->trans);
dengyihao's avatar
dengyihao 已提交
238 239 240
  assert(sz <= 256);

  uint8_t tSize = 0;
241 242
  uint8_t oSize = packSize(node->finalOutput);

dengyihao's avatar
dengyihao 已提交
243
  // finalOutput.is_zero()
244
  bool anyOuts = (node->finalOutput != 0);
dengyihao's avatar
dengyihao 已提交
245
  for (size_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
246
    FstTransition* t = taosArrayGet(node->trans, i);
247
    tSize = MAX(tSize, packDeltaSize(addr, t->addr));
dengyihao's avatar
dengyihao 已提交
248
    oSize = MAX(oSize, packSize(t->out));
249
    anyOuts = anyOuts || (t->out != 0);
dengyihao's avatar
dengyihao 已提交
250 251
  }

252 253 254 255 256 257
  PackSizes packSizes = 0;
  if (anyOuts) {
    FST_SET_OUTPUT_PACK_SIZE(packSizes, oSize);
  } else {
    FST_SET_OUTPUT_PACK_SIZE(packSizes, 0);
  }
dengyihao's avatar
dengyihao 已提交
258 259

  FST_SET_TRANSITION_PACK_SIZE(packSizes, tSize);
260

dengyihao's avatar
dengyihao 已提交
261
  FstState st = fstStateCreate(AnyTrans);
262
  fstStateSetFinalState(&st, node->isFinal);
dengyihao's avatar
dengyihao 已提交
263
  fstStateSetStateNtrans(&st, (uint8_t)sz);
264

dengyihao's avatar
dengyihao 已提交
265
  if (anyOuts) {
dengyihao's avatar
dengyihao 已提交
266
    if (FST_BUILDER_NODE_IS_FINAL(node)) { fstCountingWriterPackUintIn(w, node->finalOutput, oSize); }
dengyihao's avatar
dengyihao 已提交
267
    for (int32_t i = sz - 1; i >= 0; i--) {
dengyihao's avatar
dengyihao 已提交
268
      FstTransition* t = taosArrayGet(node->trans, i);
dengyihao's avatar
dengyihao 已提交
269 270
      fstCountingWriterPackUintIn(w, t->out, oSize);
    }
271
  }
dengyihao's avatar
dengyihao 已提交
272
  for (int32_t i = sz - 1; i >= 0; i--) {
dengyihao's avatar
dengyihao 已提交
273
    FstTransition* t = taosArrayGet(node->trans, i);
274
    fstPackDeltaIn(w, addr, t->addr, tSize);
dengyihao's avatar
dengyihao 已提交
275
  }
dengyihao's avatar
dengyihao 已提交
276
  for (int32_t i = sz - 1; i >= 0; i--) {
dengyihao's avatar
dengyihao 已提交
277 278
    FstTransition* t = taosArrayGet(node->trans, i);
    fstCountingWriterWrite(w, (char*)&t->inp, 1);
279
    // fstPackDeltaIn(w, addr, t->addr, tSize);
dengyihao's avatar
dengyihao 已提交
280 281 282 283 284 285
  }
  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 已提交
286
    uint8_t* index = (uint8_t*)malloc(sizeof(uint8_t) * 256);
dengyihao's avatar
dengyihao 已提交
287
    memset(index, 255, sizeof(uint8_t) * 256);
288
    /// for (uint8_t i = 0; i < 256; i++) {
dengyihao's avatar
dengyihao 已提交
289 290
    //  index[i] = 255;
    ///}
dengyihao's avatar
dengyihao 已提交
291
    for (size_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
292
      FstTransition* t = taosArrayGet(node->trans, i);
dengyihao's avatar
dengyihao 已提交
293
      index[t->inp] = i;
294
      // fstPackDeltaIn(w, addr, t->addr, tSize);
dengyihao's avatar
dengyihao 已提交
295
    }
dengyihao's avatar
dengyihao 已提交
296
    fstCountingWriterWrite(w, (char*)index, 256);
dengyihao's avatar
dengyihao 已提交
297
    free(index);
dengyihao's avatar
dengyihao 已提交
298
  }
dengyihao's avatar
dengyihao 已提交
299
  fstCountingWriterWrite(w, (char*)&packSizes, 1);
dengyihao's avatar
dengyihao 已提交
300 301 302
  bool null = false;
  fstStateStateNtrans(&st, &null);
  if (null == true) {
303 304 305
    // 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.
dengyihao's avatar
dengyihao 已提交
306
    uint8_t v = 1;
307
    if (sz == 256) {
dengyihao's avatar
dengyihao 已提交
308
      fstCountingWriterWrite(w, (char*)&v, 1);
309
    } else {
dengyihao's avatar
dengyihao 已提交
310
      fstCountingWriterWrite(w, (char*)&sz, 1);
311
    }
dengyihao's avatar
dengyihao 已提交
312
  }
dengyihao's avatar
dengyihao 已提交
313
  fstCountingWriterWrite(w, (char*)(&(st.val)), 1);
dengyihao's avatar
dengyihao 已提交
314 315 316 317
  return;
}

// set_comm_input
dengyihao's avatar
dengyihao 已提交
318
void fstStateSetCommInput(FstState* s, uint8_t inp) {
dengyihao's avatar
dengyihao 已提交
319 320 321
  assert(s->state == OneTransNext || s->state == OneTrans);

  uint8_t val;
322 323
  COMMON_INDEX(inp, 0x111111, val);
  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;
330 331
  if (v == 0) {
    *null = true;
dengyihao's avatar
dengyihao 已提交
332
    return v;
333 334 335
  }
  // v = 0 indicate that common_input is None
  return v == 0 ? 0 : COMMON_INPUT(v);
dengyihao's avatar
dengyihao 已提交
336 337 338 339
}

// input_len

dengyihao's avatar
dengyihao 已提交
340
uint64_t fstStateInputLen(FstState* s) {
dengyihao's avatar
dengyihao 已提交
341
  assert(s->state == OneTransNext || s->state == OneTrans);
dengyihao's avatar
dengyihao 已提交
342
  bool null = false;
343 344 345 346 347
  fstStateCommInput(s, &null);
  return null ? 1 : 0;
}

// end_addr
dengyihao's avatar
dengyihao 已提交
348
uint64_t fstStateEndAddrForOneTransNext(FstState* s, FstSlice* data) {
dengyihao's avatar
dengyihao 已提交
349
  assert(s->state == OneTransNext);
dengyihao's avatar
dengyihao 已提交
350 351
  return FST_SLICE_LEN(data) - 1 - fstStateInputLen(s);
}
dengyihao's avatar
dengyihao 已提交
352
uint64_t fstStateEndAddrForOneTrans(FstState* s, FstSlice* data, PackSizes sizes) {
dengyihao's avatar
dengyihao 已提交
353
  assert(s->state == OneTrans);
354 355 356
  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 已提交
357
uint64_t fstStateEndAddrForAnyTrans(FstState* state, uint64_t version, FstSlice* date, PackSizes sizes, uint64_t nTrans) {
358 359 360 361 362 363 364
  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
}
// input
dengyihao's avatar
dengyihao 已提交
365
uint8_t fstStateInput(FstState* s, FstNode* node) {
dengyihao's avatar
dengyihao 已提交
366
  assert(s->state == OneTransNext || s->state == OneTrans);
dengyihao's avatar
dengyihao 已提交
367
  FstSlice* slice = &node->data;
368 369
  bool      null = false;
  uint8_t   inp = fstStateCommInput(s, &null);
dengyihao's avatar
dengyihao 已提交
370
  uint8_t*  data = fstSliceData(slice, NULL);
dengyihao's avatar
dengyihao 已提交
371
  return null == false ? inp : data[-1];
dengyihao's avatar
dengyihao 已提交
372
}
dengyihao's avatar
dengyihao 已提交
373
uint8_t fstStateInputForAnyTrans(FstState* s, FstNode* node, uint64_t i) {
dengyihao's avatar
dengyihao 已提交
374
  assert(s->state == AnyTrans);
dengyihao's avatar
dengyihao 已提交
375
  FstSlice* slice = &node->data;
dengyihao's avatar
dengyihao 已提交
376

377 378
  uint64_t at = node->start - fstStateNtransLen(s) - 1                             // pack size
                - fstStateTransIndexSize(s, node->version, node->nTrans) - i - 1;  // the output size
dengyihao's avatar
dengyihao 已提交
379

dengyihao's avatar
dengyihao 已提交
380
  uint8_t* data = fstSliceData(slice, NULL);
dengyihao's avatar
dengyihao 已提交
381
  return data[at];
dengyihao's avatar
dengyihao 已提交
382 383 384
}

// trans_addr
dengyihao's avatar
dengyihao 已提交
385
CompiledAddr fstStateTransAddr(FstState* s, FstNode* node) {
dengyihao's avatar
dengyihao 已提交
386
  assert(s->state == OneTransNext || s->state == OneTrans);
dengyihao's avatar
dengyihao 已提交
387
  FstSlice* slice = &node->data;
dengyihao's avatar
dengyihao 已提交
388
  if (s->state == OneTransNext) {
389
    return (CompiledAddr)(node->end) - 1;
dengyihao's avatar
dengyihao 已提交
390
  } else {
391 392 393 394 395 396
    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 已提交
397
    uint8_t* data = fstSliceData(slice, NULL);
398 399
    return unpackDelta(data + i, tSizes, node->end);
  }
dengyihao's avatar
dengyihao 已提交
400
}
dengyihao's avatar
dengyihao 已提交
401
CompiledAddr fstStateTransAddrForAnyTrans(FstState* s, FstNode* node, uint64_t i) {
dengyihao's avatar
dengyihao 已提交
402 403
  assert(s->state == AnyTrans);

dengyihao's avatar
dengyihao 已提交
404
  FstSlice* slice = &node->data;
405
  uint8_t   tSizes = FST_GET_TRANSITION_PACK_SIZE(node->sizes);
dengyihao's avatar
dengyihao 已提交
406 407 408
  uint64_t  at = node->start - fstStateNtransLen(s) - 1 - fstStateTransIndexSize(s, node->version, node->nTrans) - node->nTrans -
                (i * tSizes) - tSizes;
  uint8_t* data = fstSliceData(slice, NULL);
409
  return unpackDelta(data + at, tSizes, node->end);
dengyihao's avatar
dengyihao 已提交
410 411
}

412
// sizes
dengyihao's avatar
dengyihao 已提交
413
PackSizes fstStateSizes(FstState* s, FstSlice* slice) {
414 415
  assert(s->state == OneTrans || s->state == AnyTrans);
  uint64_t i;
dengyihao's avatar
dengyihao 已提交
416
  if (s->state == OneTrans) {
417
    i = FST_SLICE_LEN(slice) - 1 - fstStateInputLen(s) - 1;
dengyihao's avatar
dengyihao 已提交
418 419 420 421
  } else {
    i = FST_SLICE_LEN(slice) - 1 - fstStateNtransLen(s) - 1;
  }

dengyihao's avatar
dengyihao 已提交
422
  uint8_t* data = fstSliceData(slice, NULL);
423
  return (PackSizes)(*(data + i));
dengyihao's avatar
dengyihao 已提交
424
}
425
// Output
dengyihao's avatar
dengyihao 已提交
426
Output fstStateOutput(FstState* s, FstNode* node) {
427 428
  assert(s->state == OneTrans);

dengyihao's avatar
dengyihao 已提交
429
  uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(node->sizes);
dengyihao's avatar
dengyihao 已提交
430
  if (oSizes == 0) { return 0; }
dengyihao's avatar
dengyihao 已提交
431
  FstSlice* slice = &node->data;
432 433 434
  uint8_t   tSizes = FST_GET_TRANSITION_PACK_SIZE(node->sizes);

  uint64_t i = node->start - fstStateInputLen(s) - 1 - tSizes - oSizes;
dengyihao's avatar
dengyihao 已提交
435
  uint8_t* data = fstSliceData(slice, NULL);
dengyihao's avatar
dengyihao 已提交
436
  return unpackUint64(data + i, oSizes);
dengyihao's avatar
dengyihao 已提交
437
}
dengyihao's avatar
dengyihao 已提交
438
Output fstStateOutputForAnyTrans(FstState* s, FstNode* node, uint64_t i) {
dengyihao's avatar
dengyihao 已提交
439 440
  assert(s->state == AnyTrans);

441
  uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(node->sizes);
dengyihao's avatar
dengyihao 已提交
442
  if (oSizes == 0) { return 0; }
dengyihao's avatar
dengyihao 已提交
443 444
  FstSlice* slice = &node->data;
  uint8_t*  data = fstSliceData(slice, NULL);
445 446
  uint64_t  at = node->start - fstStateNtransLen(s) - 1  // pack size
                - fstStateTotalTransSize(s, node->version, node->sizes, node->nTrans) - (i * oSizes) - oSizes;
dengyihao's avatar
dengyihao 已提交
447 448

  return unpackUint64(data + at, oSizes);
dengyihao's avatar
dengyihao 已提交
449 450 451 452
}

// anyTrans specify function

dengyihao's avatar
dengyihao 已提交
453
void fstStateSetFinalState(FstState* s, bool yes) {
454
  assert(s->state == AnyTrans);
dengyihao's avatar
dengyihao 已提交
455
  if (yes) { s->val |= 0b01000000; }
dengyihao's avatar
dengyihao 已提交
456 457
  return;
}
dengyihao's avatar
dengyihao 已提交
458
bool fstStateIsFinalState(FstState* s) {
459 460 461
  assert(s->state == AnyTrans);
  return (s->val & 0b01000000) == 0b01000000;
}
dengyihao's avatar
dengyihao 已提交
462

dengyihao's avatar
dengyihao 已提交
463
void fstStateSetStateNtrans(FstState* s, uint8_t n) {
464
  assert(s->state == AnyTrans);
dengyihao's avatar
dengyihao 已提交
465
  if (n <= 0b00111111) { s->val = (s->val & 0b11000000) | n; }
dengyihao's avatar
dengyihao 已提交
466 467 468
  return;
}
// state_ntrans
dengyihao's avatar
dengyihao 已提交
469
uint8_t fstStateStateNtrans(FstState* s, bool* null) {
470
  assert(s->state == AnyTrans);
dengyihao's avatar
dengyihao 已提交
471
  *null = false;
472
  uint8_t n = s->val & 0b00111111;
dengyihao's avatar
dengyihao 已提交
473 474

  if (n == 0) {
475 476
    *null = true;  // None
  }
dengyihao's avatar
dengyihao 已提交
477
  return n;
dengyihao's avatar
dengyihao 已提交
478
}
dengyihao's avatar
dengyihao 已提交
479
uint64_t fstStateTotalTransSize(FstState* s, uint64_t version, PackSizes sizes, uint64_t nTrans) {
480 481
  assert(s->state == AnyTrans);
  uint64_t idxSize = fstStateTransIndexSize(s, version, nTrans);
dengyihao's avatar
dengyihao 已提交
482
  return nTrans + (nTrans * FST_GET_TRANSITION_PACK_SIZE(sizes)) + idxSize;
dengyihao's avatar
dengyihao 已提交
483
}
dengyihao's avatar
dengyihao 已提交
484
uint64_t fstStateTransIndexSize(FstState* s, uint64_t version, uint64_t nTrans) {
485 486
  assert(s->state == AnyTrans);
  return (version >= 2 && nTrans > TRANS_INDEX_THRESHOLD) ? 256 : 0;
dengyihao's avatar
dengyihao 已提交
487
}
dengyihao's avatar
dengyihao 已提交
488
uint64_t fstStateNtransLen(FstState* s) {
dengyihao's avatar
dengyihao 已提交
489 490 491
  assert(s->state == AnyTrans);
  bool null = false;
  fstStateStateNtrans(s, &null);
492
  return null == true ? 1 : 0;
dengyihao's avatar
dengyihao 已提交
493
}
dengyihao's avatar
dengyihao 已提交
494
uint64_t fstStateNtrans(FstState* s, FstSlice* slice) {
495
  bool    null = false;
dengyihao's avatar
dengyihao 已提交
496
  uint8_t n = fstStateStateNtrans(s, &null);
dengyihao's avatar
dengyihao 已提交
497
  if (null != true) { return n; }
498
  int32_t  len;
dengyihao's avatar
dengyihao 已提交
499
  uint8_t* data = fstSliceData(slice, &len);
dengyihao's avatar
dengyihao 已提交
500
  n = data[len - 2];
501 502 503 504
  // n = 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
}
dengyihao's avatar
dengyihao 已提交
505
Output fstStateFinalOutput(FstState* s, uint64_t version, FstSlice* slice, PackSizes sizes, uint64_t nTrans) {
506
  uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(sizes);
dengyihao's avatar
dengyihao 已提交
507
  if (oSizes == 0 || !fstStateIsFinalState(s)) { return 0; }
dengyihao's avatar
dengyihao 已提交
508

509 510
  uint64_t at = FST_SLICE_LEN(slice) - 1 - fstStateNtransLen(s) - 1  // pack size
                - fstStateTotalTransSize(s, version, sizes, nTrans) - (nTrans * oSizes) - oSizes;
dengyihao's avatar
dengyihao 已提交
511
  uint8_t* data = fstSliceData(slice, NULL);
512
  return unpackUint64(data + at, (uint8_t)oSizes);
dengyihao's avatar
dengyihao 已提交
513
}
dengyihao's avatar
dengyihao 已提交
514
uint64_t fstStateFindInput(FstState* s, FstNode* node, uint8_t b, bool* null) {
dengyihao's avatar
dengyihao 已提交
515
  assert(s->state == AnyTrans);
dengyihao's avatar
dengyihao 已提交
516
  FstSlice* slice = &node->data;
dengyihao's avatar
dengyihao 已提交
517
  if (node->version >= 2 && node->nTrans > TRANS_INDEX_THRESHOLD) {
518
    uint64_t at = node->start - fstStateNtransLen(s) - 1  // pack size
dengyihao's avatar
dengyihao 已提交
519
                  - fstStateTransIndexSize(s, node->version, node->nTrans);
520
    int32_t  dlen = 0;
dengyihao's avatar
dengyihao 已提交
521
    uint8_t* data = fstSliceData(slice, &dlen);
dengyihao's avatar
dengyihao 已提交
522
    uint64_t i = data[at + b];
523
    // uint64_t i = slice->data[slice->start + at + b];
dengyihao's avatar
dengyihao 已提交
524
    if (i >= node->nTrans) { *null = true; }
dengyihao's avatar
dengyihao 已提交
525 526
    return i;
  } else {
527 528 529
    uint64_t start = node->start - fstStateNtransLen(s) - 1  // pack size
                     - node->nTrans;
    uint64_t end = start + node->nTrans;
dengyihao's avatar
dengyihao 已提交
530
    FstSlice t = fstSliceCopy(slice, start, end - 1);
531
    int32_t  len = 0;
dengyihao's avatar
dengyihao 已提交
532
    uint8_t* data = fstSliceData(&t, &len);
533 534 535
    int      i = 0;
    for (; i < len; i++) {
      uint8_t v = data[i];
dengyihao's avatar
dengyihao 已提交
536
      if (v == b) {
dengyihao's avatar
dengyihao 已提交
537
        fstSliceDestroy(&t);
538
        return node->nTrans - i - 1;  // bug
dengyihao's avatar
dengyihao 已提交
539
      }
540
    }
dengyihao's avatar
dengyihao 已提交
541
    if (i == len) { *null = true; }
dengyihao's avatar
dengyihao 已提交
542
    fstSliceDestroy(&t);
543
  }
dengyihao's avatar
dengyihao 已提交
544 545
}

546
// fst node function
dengyihao's avatar
dengyihao 已提交
547

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

552
  FstState st = fstStateCreateFrom(slice, addr);
dengyihao's avatar
dengyihao 已提交
553 554

  if (st.state == EmptyFinal) {
555 556 557 558 559 560 561 562 563
    n->data = fstSliceCreate(NULL, 0);
    n->version = version;
    n->state = st;
    n->start = EMPTY_ADDRESS;
    n->end = EMPTY_ADDRESS;
    n->isFinal = true;
    n->nTrans = 0;
    n->sizes = 0;
    n->finalOutput = 0;
dengyihao's avatar
dengyihao 已提交
564
  } else if (st.state == OneTransNext) {
565 566 567 568 569 570 571 572 573
    n->data = fstSliceCopy(slice, 0, addr);
    n->version = version;
    n->state = st;
    n->start = addr;
    n->end = fstStateEndAddrForOneTransNext(&st, &n->data);  //? s.end_addr(data);
    n->isFinal = false;
    n->sizes = 0;
    n->nTrans = 1;
    n->finalOutput = 0;
dengyihao's avatar
dengyihao 已提交
574
  } else if (st.state == OneTrans) {
575 576 577 578 579 580 581 582 583 584 585
    FstSlice  data = fstSliceCopy(slice, 0, addr);
    PackSizes sz = fstStateSizes(&st, &data);
    n->data = data;
    n->version = version;
    n->state = st;
    n->start = addr;
    n->end = fstStateEndAddrForOneTrans(&st, &data, sz);  // s.end_addr(data, sz);
    n->isFinal = false;
    n->nTrans = 1;
    n->sizes = sz;
    n->finalOutput = 0;
dengyihao's avatar
dengyihao 已提交
586
  } else {
587 588 589 590 591 592 593 594 595 596 597
    FstSlice data = fstSliceCopy(slice, 0, addr);
    uint64_t sz = fstStateSizes(&st, &data);       // s.sizes(data)
    uint32_t nTrans = fstStateNtrans(&st, &data);  // s.ntrans(data)
    n->data = data;
    n->version = version;
    n->state = st;
    n->start = addr;
    n->end = fstStateEndAddrForAnyTrans(&st, version, &data, sz, nTrans);  // s.end_addr(version, data, sz, ntrans);
    n->isFinal = fstStateIsFinalState(&st);                                // s.is_final_state();
    n->nTrans = nTrans;
    n->sizes = sz;
dengyihao's avatar
dengyihao 已提交
598
    n->finalOutput = fstStateFinalOutput(&st, version, &data, sz, nTrans);  // s.final_output(version, data, sz, ntrans);
599 600
  }
  return n;
dengyihao's avatar
dengyihao 已提交
601
}
dengyihao's avatar
dengyihao 已提交
602 603

// debug state transition
dengyihao's avatar
dengyihao 已提交
604 605
static const char* fstNodeState(FstNode* node) {
  FstState* st = &node->state;
606
  return fstStateStr[st->state];
dengyihao's avatar
dengyihao 已提交
607 608
}

dengyihao's avatar
dengyihao 已提交
609
void fstNodeDestroy(FstNode* node) {
610
  fstSliceDestroy(&node->data);
dengyihao's avatar
dengyihao 已提交
611 612
  free(node);
}
dengyihao's avatar
dengyihao 已提交
613 614
FstTransitions* fstNodeTransitions(FstNode* node) {
  FstTransitions* t = malloc(sizeof(FstTransitions));
dengyihao's avatar
dengyihao 已提交
615
  if (NULL == t) { return NULL; }
dengyihao's avatar
dengyihao 已提交
616 617
  FstRange range = {.start = 0, .end = FST_NODE_LEN(node)};
  t->range = range;
618 619 620
  t->node = node;
  return t;
}
dengyihao's avatar
dengyihao 已提交
621

622
// Returns the transition at index `i`.
dengyihao's avatar
dengyihao 已提交
623
bool fstNodeGetTransitionAt(FstNode* node, uint64_t i, FstTransition* trn) {
624
  bool      s = true;
dengyihao's avatar
dengyihao 已提交
625
  FstState* st = &node->state;
dengyihao's avatar
dengyihao 已提交
626
  if (st->state == OneTransNext) {
627 628 629
    trn->inp = fstStateInput(st, node);
    trn->out = 0;
    trn->addr = fstStateTransAddr(st, node);
dengyihao's avatar
dengyihao 已提交
630
  } else if (st->state == OneTrans) {
631 632 633
    trn->inp = fstStateInput(st, node);
    trn->out = fstStateOutput(st, node);
    trn->addr = fstStateTransAddr(st, node);
dengyihao's avatar
dengyihao 已提交
634
  } else if (st->state == AnyTrans) {
635 636 637
    trn->inp = fstStateInputForAnyTrans(st, node, i);
    trn->out = fstStateOutputForAnyTrans(st, node, i);
    trn->addr = fstStateTransAddrForAnyTrans(st, node, i);
dengyihao's avatar
dengyihao 已提交
638 639 640 641
  } else {
    s = false;
  }
  return s;
642
}
dengyihao's avatar
dengyihao 已提交
643

dengyihao's avatar
dengyihao 已提交
644
// Returns the transition address of the `i`th transition
dengyihao's avatar
dengyihao 已提交
645
bool fstNodeGetTransitionAddrAt(FstNode* node, uint64_t i, CompiledAddr* res) {
646
  bool      s = true;
dengyihao's avatar
dengyihao 已提交
647
  FstState* st = &node->state;
dengyihao's avatar
dengyihao 已提交
648 649 650 651
  if (st->state == OneTransNext) {
    assert(i == 0);
    fstStateTransAddr(st, node);
  } else if (st->state == OneTrans) {
652
    assert(i == 0);
dengyihao's avatar
dengyihao 已提交
653 654 655
    fstStateTransAddr(st, node);
  } else if (st->state == AnyTrans) {
    fstStateTransAddrForAnyTrans(st, node, i);
656
  } else if (FST_STATE_EMPTY_FINAL(node)) {
dengyihao's avatar
dengyihao 已提交
657
    s = false;
dengyihao's avatar
dengyihao 已提交
658 659
  } else {
    assert(0);
dengyihao's avatar
dengyihao 已提交
660 661
  }
  return s;
dengyihao's avatar
dengyihao 已提交
662 663
}

dengyihao's avatar
dengyihao 已提交
664
//  Finds the `i`th transition corresponding to the given input byte.
665
//  If no transition for this byte exists, then `false` is returned.
dengyihao's avatar
dengyihao 已提交
666
bool fstNodeFindInput(FstNode* node, uint8_t b, uint64_t* res) {
667
  bool      s = true;
dengyihao's avatar
dengyihao 已提交
668
  FstState* st = &node->state;
dengyihao's avatar
dengyihao 已提交
669
  if (st->state == OneTransNext) {
670 671 672 673 674 675 676 677 678 679 680
    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;
    }
dengyihao's avatar
dengyihao 已提交
681
  } else if (st->state == AnyTrans) {
682 683 684 685 686 687 688
    bool     null = false;
    uint64_t out = fstStateFindInput(st, node, b, &null);
    if (null == false) {
      *res = out;
    } else {
      s = false;
    }
dengyihao's avatar
dengyihao 已提交
689
  }
dengyihao's avatar
dengyihao 已提交
690
  return s;
691
}
dengyihao's avatar
dengyihao 已提交
692

dengyihao's avatar
dengyihao 已提交
693
bool fstNodeCompile(FstNode* node, void* w, CompiledAddr lastAddr, CompiledAddr addr, FstBuilderNode* builderNode) {
694
  size_t sz = taosArrayGetSize(builderNode->trans);
dengyihao's avatar
dengyihao 已提交
695 696
  assert(sz < 256);
  if (sz == 0 && builderNode->isFinal && builderNode->finalOutput == 0) {
697
    return true;
dengyihao's avatar
dengyihao 已提交
698
  } else if (sz != 1 || builderNode->isFinal) {
699
    fstStateCompileForAnyTrans(w, addr, builderNode);
dengyihao's avatar
dengyihao 已提交
700 701
    // AnyTrans->Compile(w, addr, node);
  } else {
dengyihao's avatar
dengyihao 已提交
702
    FstTransition* tran = taosArrayGet(builderNode->trans, 0);
dengyihao's avatar
dengyihao 已提交
703
    if (tran->addr == lastAddr && tran->out == 0) {
704 705 706
      fstStateCompileForOneTransNext(w, addr, tran->inp);
      // OneTransNext::compile(w, lastAddr, tran->inp);
      return true;
dengyihao's avatar
dengyihao 已提交
707
    } else {
708 709 710 711 712 713 714
      fstStateCompileForOneTrans(w, addr, tran);
      // OneTrans::Compile(w, lastAddr, *tran);
      return true;
    }
  }
  return true;
}
dengyihao's avatar
dengyihao 已提交
715

dengyihao's avatar
dengyihao 已提交
716
bool fstBuilderNodeCompileTo(FstBuilderNode* b, FstCountingWriter* wrt, CompiledAddr lastAddr, CompiledAddr startAddr) {
dengyihao's avatar
dengyihao 已提交
717 718 719
  return fstNodeCompile(NULL, wrt, lastAddr, startAddr, b);
}

dengyihao's avatar
dengyihao 已提交
720 721
FstBuilder* fstBuilderCreate(void* w, FstType ty) {
  FstBuilder* b = malloc(sizeof(FstBuilder));
dengyihao's avatar
dengyihao 已提交
722
  if (NULL == b) { return b; }
dengyihao's avatar
dengyihao 已提交
723

724 725 726 727 728 729
  b->wrt = fstCountingWriterCreate(w);
  b->unfinished = fstUnFinishedNodesCreate();
  b->registry = fstRegistryCreate(10000, 2);
  b->last = fstSliceCreate(NULL, 0);
  b->lastAddr = NONE_ADDRESS;
  b->len = 0;
dengyihao's avatar
dengyihao 已提交
730

731
  char  buf64[8] = {0};
dengyihao's avatar
dengyihao 已提交
732
  void* pBuf64 = buf64;
733
  taosEncodeFixedU64(&pBuf64, VERSION);
dengyihao's avatar
dengyihao 已提交
734
  fstCountingWriterWrite(b->wrt, buf64, sizeof(buf64));
735 736

  memset(buf64, 0, sizeof(buf64));
dengyihao's avatar
dengyihao 已提交
737
  pBuf64 = buf64;
738
  taosEncodeFixedU64(&pBuf64, ty);
dengyihao's avatar
dengyihao 已提交
739 740
  fstCountingWriterWrite(b->wrt, buf64, sizeof(buf64));

dengyihao's avatar
dengyihao 已提交
741 742
  return b;
}
dengyihao's avatar
dengyihao 已提交
743
void fstBuilderDestroy(FstBuilder* b) {
dengyihao's avatar
dengyihao 已提交
744
  if (b == NULL) { return; }
dengyihao's avatar
dengyihao 已提交
745

746 747
  fstCountingWriterDestroy(b->wrt);
  fstUnFinishedNodesDestroy(b->unfinished);
dengyihao's avatar
dengyihao 已提交
748
  fstRegistryDestroy(b->registry);
dengyihao's avatar
dengyihao 已提交
749
  fstSliceDestroy(&b->last);
dengyihao's avatar
dengyihao 已提交
750 751
  free(b);
}
dengyihao's avatar
dengyihao 已提交
752

dengyihao's avatar
dengyihao 已提交
753
bool fstBuilderInsert(FstBuilder* b, FstSlice bs, Output in) {
754
  OrderType t = fstBuilderCheckLastKey(b, bs, true);
dengyihao's avatar
dengyihao 已提交
755 756
  if (t == Ordered) {
    // add log info
757 758 759
    fstBuilderInsertOutput(b, bs, in);
    return true;
  }
dengyihao's avatar
dengyihao 已提交
760
  indexInfo("fst write key must be ordered");
dengyihao's avatar
dengyihao 已提交
761 762 763
  return false;
}

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

  if (prefixLen == FST_SLICE_LEN(s)) {
    assert(out == 0);
    return;
  }

  b->len += 1;
  fstBuilderCompileFrom(b, prefixLen);

  FstSlice sub = fstSliceCopy(s, prefixLen, s->end);
  fstUnFinishedNodesAddSuffix(b->unfinished, sub, out);
  fstSliceDestroy(&sub);
  return;
}
dengyihao's avatar
dengyihao 已提交
793

dengyihao's avatar
dengyihao 已提交
794 795
OrderType fstBuilderCheckLastKey(FstBuilder* b, FstSlice bs, bool ckDup) {
  FstSlice* input = &bs;
dengyihao's avatar
dengyihao 已提交
796
  if (fstSliceIsEmpty(&b->last)) {
dengyihao's avatar
dengyihao 已提交
797
    fstSliceDestroy(&b->last);
dengyihao's avatar
dengyihao 已提交
798
    // deep copy or not
dengyihao's avatar
dengyihao 已提交
799
    b->last = fstSliceDeepCopy(&bs, input->start, input->end);
dengyihao's avatar
dengyihao 已提交
800 801 802
  } else {
    int comp = fstSliceCompare(&b->last, &bs);
    if (comp == 0 && ckDup) {
803
      return DuplicateKey;
dengyihao's avatar
dengyihao 已提交
804 805 806 807
    } else if (comp == 1) {
      return OutOfOrdered;
    }
    // deep copy or not
dengyihao's avatar
dengyihao 已提交
808
    fstSliceDestroy(&b->last);
809 810
    b->last = fstSliceDeepCopy(&bs, input->start, input->end);
  }
dengyihao's avatar
dengyihao 已提交
811
  return Ordered;
812
}
dengyihao's avatar
dengyihao 已提交
813
void fstBuilderCompileFrom(FstBuilder* b, uint64_t istate) {
dengyihao's avatar
dengyihao 已提交
814 815
  CompiledAddr addr = NONE_ADDRESS;
  while (istate + 1 < FST_UNFINISHED_NODES_LEN(b->unfinished)) {
dengyihao's avatar
dengyihao 已提交
816
    FstBuilderNode* bn = NULL;
dengyihao's avatar
dengyihao 已提交
817
    if (addr == NONE_ADDRESS) {
dengyihao's avatar
dengyihao 已提交
818
      bn = fstUnFinishedNodesPopEmpty(b->unfinished);
dengyihao's avatar
dengyihao 已提交
819
    } else {
dengyihao's avatar
dengyihao 已提交
820
      bn = fstUnFinishedNodesPopFreeze(b->unfinished, addr);
dengyihao's avatar
dengyihao 已提交
821
    }
dengyihao's avatar
dengyihao 已提交
822 823 824
    addr = fstBuilderCompile(b, bn);

    fstBuilderNodeDestroy(bn);
825 826
    assert(addr != NONE_ADDRESS);
    // fstBuilderNodeDestroy(n);
dengyihao's avatar
dengyihao 已提交
827 828
  }
  fstUnFinishedNodesTopLastFreeze(b->unfinished, addr);
829
  return;
dengyihao's avatar
dengyihao 已提交
830
}
dengyihao's avatar
dengyihao 已提交
831
CompiledAddr fstBuilderCompile(FstBuilder* b, FstBuilderNode* bn) {
832 833
  if (FST_BUILDER_NODE_IS_FINAL(bn) && FST_BUILDER_NODE_TRANS_ISEMPTY(bn) && FST_BUILDER_NODE_FINALOUTPUT_ISZERO(bn)) {
    return EMPTY_ADDRESS;
dengyihao's avatar
dengyihao 已提交
834
  }
dengyihao's avatar
dengyihao 已提交
835
  FstRegistryEntry* entry = fstRegistryGetEntry(b->registry, bn);
836
  if (entry->state == FOUND) {
dengyihao's avatar
dengyihao 已提交
837
    CompiledAddr ret = entry->addr;
dengyihao's avatar
dengyihao 已提交
838
    fstRegistryEntryDestroy(entry);
dengyihao's avatar
dengyihao 已提交
839
    return ret;
840
  }
dengyihao's avatar
dengyihao 已提交
841 842
  CompiledAddr startAddr = (CompiledAddr)(FST_WRITER_COUNT(b->wrt));

843 844
  fstBuilderNodeCompileTo(bn, b->wrt, b->lastAddr, startAddr);
  b->lastAddr = (CompiledAddr)(FST_WRITER_COUNT(b->wrt) - 1);
dengyihao's avatar
dengyihao 已提交
845
  if (entry->state == NOTFOUND) { FST_REGISTRY_CELL_INSERT(entry->cell, b->lastAddr); }
dengyihao's avatar
dengyihao 已提交
846
  fstRegistryEntryDestroy(entry);
847 848

  return b->lastAddr;
dengyihao's avatar
dengyihao 已提交
849 850
}

dengyihao's avatar
dengyihao 已提交
851
void* fstBuilderInsertInner(FstBuilder* b) {
852
  fstBuilderCompileFrom(b, 0);
dengyihao's avatar
dengyihao 已提交
853
  FstBuilderNode* rootNode = fstUnFinishedNodesPopRoot(b->unfinished);
854
  CompiledAddr    rootAddr = fstBuilderCompile(b, rootNode);
dengyihao's avatar
dengyihao 已提交
855
  fstBuilderNodeDestroy(rootNode);
dengyihao's avatar
dengyihao 已提交
856

857 858
  char buf64[8] = {0};

dengyihao's avatar
dengyihao 已提交
859
  void* pBuf64 = buf64;
860 861
  taosEncodeFixedU64(&pBuf64, b->len);
  fstCountingWriterWrite(b->wrt, buf64, sizeof(buf64));
dengyihao's avatar
dengyihao 已提交
862

dengyihao's avatar
dengyihao 已提交
863
  pBuf64 = buf64;
864 865
  taosEncodeFixedU64(&pBuf64, rootAddr);
  fstCountingWriterWrite(b->wrt, buf64, sizeof(buf64));
dengyihao's avatar
dengyihao 已提交
866

867
  char     buf32[4] = {0};
dengyihao's avatar
dengyihao 已提交
868
  void*    pBuf32 = buf32;
dengyihao's avatar
dengyihao 已提交
869
  uint32_t sum = fstCountingWriterMaskedCheckSum(b->wrt);
870 871 872
  taosEncodeFixedU32(&pBuf32, sum);
  fstCountingWriterWrite(b->wrt, buf32, sizeof(buf32));

dengyihao's avatar
dengyihao 已提交
873
  fstCountingWriterFlush(b->wrt);
874 875
  // fstCountingWriterDestroy(b->wrt);
  // b->wrt = NULL;
dengyihao's avatar
dengyihao 已提交
876 877
  return b->wrt;
}
dengyihao's avatar
dengyihao 已提交
878 879 880
void fstBuilderFinish(FstBuilder* b) {
  fstBuilderInsertInner(b);
}
dengyihao's avatar
dengyihao 已提交
881

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

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

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

dengyihao's avatar
dengyihao 已提交
897 898 899 900 901
void fstLastTransitionDestroy(FstLastTransition* trn) {
  free(trn);
}
void fstBuilderNodeUnfinishedLastCompiled(FstBuilderNodeUnfinished* unNode, CompiledAddr addr) {
  FstLastTransition* trn = unNode->last;
dengyihao's avatar
dengyihao 已提交
902
  if (trn == NULL) { return; }
903 904 905
  FstTransition t = {.inp = trn->inp, .out = trn->out, .addr = addr};
  taosArrayPush(unNode->node->trans, &t);
  fstLastTransitionDestroy(trn);
dengyihao's avatar
dengyihao 已提交
906
  unNode->last = NULL;
dengyihao's avatar
dengyihao 已提交
907 908 909
  return;
}

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

dengyihao's avatar
dengyihao 已提交
921
Fst* fstCreate(FstSlice* slice) {
dengyihao's avatar
dengyihao 已提交
922
  int32_t slen;
dengyihao's avatar
dengyihao 已提交
923
  char*   buf = fstSliceData(slice, &slen);
dengyihao's avatar
dengyihao 已提交
924
  if (slen < 36) { return NULL; }
dengyihao's avatar
dengyihao 已提交
925
  uint64_t len = slen;
926
  uint64_t skip = 0;
dengyihao's avatar
dengyihao 已提交
927

928 929 930
  uint64_t version;
  taosDecodeFixedU64(buf, &version);
  skip += sizeof(version);
dengyihao's avatar
dengyihao 已提交
931
  if (version == 0 || version > VERSION) { return NULL; }
dengyihao's avatar
dengyihao 已提交
932 933 934

  uint64_t type;
  taosDecodeFixedU64(buf + skip, &type);
935
  skip += sizeof(type);
dengyihao's avatar
dengyihao 已提交
936 937 938

  uint32_t checkSum = 0;
  len -= sizeof(checkSum);
939
  taosDecodeFixedU32(buf + len, &checkSum);
dengyihao's avatar
dengyihao 已提交
940 941

  CompiledAddr rootAddr;
942 943
  len -= sizeof(rootAddr);
  taosDecodeFixedU64(buf + len, &rootAddr);
dengyihao's avatar
dengyihao 已提交
944

945 946
  uint64_t fstLen;
  len -= sizeof(fstLen);
dengyihao's avatar
dengyihao 已提交
947
  taosDecodeFixedU64(buf + len, &fstLen);
948
  // TODO(validate root addr)
dengyihao's avatar
dengyihao 已提交
949
  Fst* fst = (Fst*)calloc(1, sizeof(Fst));
dengyihao's avatar
dengyihao 已提交
950
  if (fst == NULL) { return NULL; }
951

dengyihao's avatar
dengyihao 已提交
952
  fst->meta = (FstMeta*)malloc(sizeof(FstMeta));
dengyihao's avatar
dengyihao 已提交
953
  if (NULL == fst->meta) { goto FST_CREAT_FAILED; }
dengyihao's avatar
dengyihao 已提交
954

955 956 957 958
  fst->meta->version = version;
  fst->meta->rootAddr = rootAddr;
  fst->meta->ty = type;
  fst->meta->len = fstLen;
dengyihao's avatar
dengyihao 已提交
959
  fst->meta->checkSum = checkSum;
dengyihao's avatar
dengyihao 已提交
960

dengyihao's avatar
dengyihao 已提交
961
  FstSlice* s = calloc(1, sizeof(FstSlice));
962 963 964
  *s = fstSliceCopy(slice, 0, FST_SLICE_LEN(slice));
  fst->data = s;

dengyihao's avatar
dengyihao 已提交
965 966
  return fst;

967 968
FST_CREAT_FAILED:
  free(fst->meta);
dengyihao's avatar
dengyihao 已提交
969 970
  free(fst);
}
dengyihao's avatar
dengyihao 已提交
971
void fstDestroy(Fst* fst) {
972 973
  if (fst) {
    free(fst->meta);
dengyihao's avatar
dengyihao 已提交
974 975
    fstSliceDestroy(fst->data);
    free(fst->data);
976 977
  }
  free(fst);
dengyihao's avatar
dengyihao 已提交
978 979
}

dengyihao's avatar
dengyihao 已提交
980 981
bool fstGet(Fst* fst, FstSlice* b, Output* out) {
  FstNode* root = fstGetRoot(fst);
982 983
  Output   tOut = 0;
  int32_t  len;
dengyihao's avatar
dengyihao 已提交
984
  uint8_t* data = fstSliceData(b, &len);
dengyihao's avatar
dengyihao 已提交
985

dengyihao's avatar
dengyihao 已提交
986
  SArray* nodes = (SArray*)taosArrayInit(len, sizeof(FstNode*));
dengyihao's avatar
dengyihao 已提交
987
  taosArrayPush(nodes, &root);
dengyihao's avatar
dengyihao 已提交
988 989
  for (uint32_t i = 0; i < len; i++) {
    uint8_t inp = data[i];
dengyihao's avatar
dengyihao 已提交
990
    Output  res = 0;
dengyihao's avatar
dengyihao 已提交
991
    if (false == fstNodeFindInput(root, inp, &res)) { return false; }
dengyihao's avatar
dengyihao 已提交
992

993
    FstTransition trn;
dengyihao's avatar
dengyihao 已提交
994
    fstNodeGetTransitionAt(root, res, &trn);
995
    tOut += trn.out;
dengyihao's avatar
dengyihao 已提交
996
    root = fstGetNode(fst, trn.addr);
dengyihao's avatar
dengyihao 已提交
997
    taosArrayPush(nodes, &root);
dengyihao's avatar
dengyihao 已提交
998 999 1000 1001
  }
  if (!FST_NODE_IS_FINAL(root)) {
    return false;
  } else {
1002
    tOut = tOut + FST_NODE_FINAL_OUTPUT(root);
dengyihao's avatar
dengyihao 已提交
1003
  }
dengyihao's avatar
dengyihao 已提交
1004

dengyihao's avatar
dengyihao 已提交
1005
  for (size_t i = 0; i < taosArrayGetSize(nodes); i++) {
dengyihao's avatar
dengyihao 已提交
1006
    FstNode** node = (FstNode**)taosArrayGet(nodes, i);
1007
    fstNodeDestroy(*node);
dengyihao's avatar
dengyihao 已提交
1008 1009
  }
  taosArrayDestroy(nodes);
dengyihao's avatar
dengyihao 已提交
1010

dengyihao's avatar
dengyihao 已提交
1011
  fst->root = NULL;
dengyihao's avatar
dengyihao 已提交
1012
  *out = tOut;
1013 1014

  return true;
dengyihao's avatar
dengyihao 已提交
1015
}
dengyihao's avatar
dengyihao 已提交
1016 1017 1018 1019
FstStreamBuilder* fstSearch(Fst* fst, AutomationCtx* ctx) {
  return fstStreamBuilderCreate(fst, ctx);
}
StreamWithState* streamBuilderIntoStream(FstStreamBuilder* sb) {
dengyihao's avatar
dengyihao 已提交
1020
  if (sb == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
1021 1022
  return streamWithStateCreate(sb->fst, sb->aut, sb->min, sb->max);
}
dengyihao's avatar
dengyihao 已提交
1023 1024 1025
FstStreamWithStateBuilder* fstSearchWithState(Fst* fst, AutomationCtx* ctx) {
  return fstStreamBuilderCreate(fst, ctx);
}
dengyihao's avatar
dengyihao 已提交
1026

dengyihao's avatar
dengyihao 已提交
1027
FstNode* fstGetRoot(Fst* fst) {
dengyihao's avatar
dengyihao 已提交
1028
  if (fst->root != NULL) { return fst->root; }
1029 1030
  CompiledAddr rAddr = fstGetRootAddr(fst);
  fst->root = fstGetNode(fst, rAddr);
dengyihao's avatar
dengyihao 已提交
1031
  return fst->root;
dengyihao's avatar
dengyihao 已提交
1032
}
dengyihao's avatar
dengyihao 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041
FstNode* fstGetNode(Fst* fst, CompiledAddr addr) {
  return fstNodeCreate(fst->meta->version, addr, fst->data);
}
FstType fstGetType(Fst* fst) {
  return fst->meta->ty;
}
CompiledAddr fstGetRootAddr(Fst* fst) {
  return fst->meta->rootAddr;
}
dengyihao's avatar
dengyihao 已提交
1042

dengyihao's avatar
dengyihao 已提交
1043
Output fstEmptyFinalOutput(Fst* fst, bool* null) {
1044
  Output   res = 0;
dengyihao's avatar
dengyihao 已提交
1045
  FstNode* node = fstGetRoot(fst);
dengyihao's avatar
dengyihao 已提交
1046 1047
  if (FST_NODE_IS_FINAL(node)) {
    *null = false;
1048
    res = FST_NODE_FINAL_OUTPUT(node);
dengyihao's avatar
dengyihao 已提交
1049 1050 1051 1052 1053 1054
  } else {
    *null = true;
  }
  return res;
}

dengyihao's avatar
dengyihao 已提交
1055
bool fstVerify(Fst* fst) {
dengyihao's avatar
dengyihao 已提交
1056
  uint32_t checkSum = fst->meta->checkSum;
1057
  int32_t  len;
dengyihao's avatar
dengyihao 已提交
1058
  uint8_t* data = fstSliceData(fst->data, &len);
1059
  TSCKSUM  initSum = 0;
dengyihao's avatar
dengyihao 已提交
1060
  if (!taosCheckChecksumWhole(data, len)) { return false; }
dengyihao's avatar
dengyihao 已提交
1061
  return true;
dengyihao's avatar
dengyihao 已提交
1062 1063
}

dengyihao's avatar
dengyihao 已提交
1064
// data bound function
dengyihao's avatar
dengyihao 已提交
1065 1066
FstBoundWithData* fstBoundStateCreate(FstBound type, FstSlice* data) {
  FstBoundWithData* b = calloc(1, sizeof(FstBoundWithData));
dengyihao's avatar
dengyihao 已提交
1067
  if (b == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
1068 1069 1070 1071 1072 1073

  if (data != NULL) {
    b->data = fstSliceCopy(data, data->start, data->end);
  } else {
    b->data = fstSliceCreate(NULL, 0);
  }
dengyihao's avatar
dengyihao 已提交
1074
  b->type = type;
dengyihao's avatar
dengyihao 已提交
1075

1076
  return b;
dengyihao's avatar
dengyihao 已提交
1077 1078
}

dengyihao's avatar
dengyihao 已提交
1079
bool fstBoundWithDataExceededBy(FstBoundWithData* bound, FstSlice* slice) {
dengyihao's avatar
dengyihao 已提交
1080 1081 1082 1083 1084 1085
  int comp = fstSliceCompare(slice, &bound->data);
  if (bound->type == Included) {
    return comp > 0 ? true : false;
  } else if (bound->type == Excluded) {
    return comp >= 0 ? true : false;
  } else {
dengyihao's avatar
dengyihao 已提交
1086
    return false;
dengyihao's avatar
dengyihao 已提交
1087 1088
  }
}
dengyihao's avatar
dengyihao 已提交
1089
bool fstBoundWithDataIsEmpty(FstBoundWithData* bound) {
dengyihao's avatar
dengyihao 已提交
1090 1091
  if (bound->type == Unbounded) {
    return true;
1092 1093 1094
  } else {
    return fstSliceIsEmpty(&bound->data);
  }
dengyihao's avatar
dengyihao 已提交
1095 1096
}

dengyihao's avatar
dengyihao 已提交
1097 1098 1099
bool fstBoundWithDataIsIncluded(FstBoundWithData* bound) {
  return bound->type == Excluded ? false : true;
}
dengyihao's avatar
dengyihao 已提交
1100

dengyihao's avatar
dengyihao 已提交
1101 1102 1103
void fstBoundDestroy(FstBoundWithData* bound) {
  free(bound);
}
dengyihao's avatar
dengyihao 已提交
1104

dengyihao's avatar
dengyihao 已提交
1105 1106
StreamWithState* streamWithStateCreate(Fst* fst, AutomationCtx* automation, FstBoundWithData* min, FstBoundWithData* max) {
  StreamWithState* sws = calloc(1, sizeof(StreamWithState));
dengyihao's avatar
dengyihao 已提交
1107
  if (sws == NULL) { return NULL; }
1108 1109 1110

  sws->fst = fst;
  sws->aut = automation;
dengyihao's avatar
dengyihao 已提交
1111
  sws->inp = (SArray*)taosArrayInit(256, sizeof(uint8_t));
dengyihao's avatar
dengyihao 已提交
1112 1113

  sws->emptyOutput.null = false;
1114
  sws->emptyOutput.out = 0;
dengyihao's avatar
dengyihao 已提交
1115

dengyihao's avatar
dengyihao 已提交
1116
  sws->stack = (SArray*)taosArrayInit(256, sizeof(StreamState));
1117
  sws->endAt = max;
dengyihao's avatar
dengyihao 已提交
1118 1119 1120 1121
  streamWithStateSeekMin(sws, min);

  return sws;
}
dengyihao's avatar
dengyihao 已提交
1122
void streamWithStateDestroy(StreamWithState* sws) {
dengyihao's avatar
dengyihao 已提交
1123
  if (sws == NULL) { return; }
dengyihao's avatar
dengyihao 已提交
1124 1125

  taosArrayDestroy(sws->inp);
dengyihao's avatar
dengyihao 已提交
1126
  taosArrayDestroyEx(sws->stack, streamStateDestroy);
dengyihao's avatar
dengyihao 已提交
1127

1128
  free(sws);
dengyihao's avatar
dengyihao 已提交
1129 1130
}

dengyihao's avatar
dengyihao 已提交
1131 1132
bool streamWithStateSeekMin(StreamWithState* sws, FstBoundWithData* min) {
  AutomationCtx* aut = sws->aut;
dengyihao's avatar
dengyihao 已提交
1133
  if (fstBoundWithDataIsEmpty(min)) {
dengyihao's avatar
dengyihao 已提交
1134
    if (fstBoundWithDataIsIncluded(min)) { sws->emptyOutput.out = fstEmptyFinalOutput(sws->fst, &(sws->emptyOutput.null)); }
1135
    StreamState s = {.node = fstGetRoot(sws->fst),
dengyihao's avatar
dengyihao 已提交
1136 1137 1138
                     .trans = 0,
                     .out = {.null = false, .out = 0},
                     .autState = automFuncs[aut->type].start(aut)};  // auto.start callback
dengyihao's avatar
dengyihao 已提交
1139 1140
    taosArrayPush(sws->stack, &s);
    return true;
1141
  }
dengyihao's avatar
dengyihao 已提交
1142
  FstSlice* key = NULL;
1143
  bool      inclusize = false;
dengyihao's avatar
dengyihao 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

  if (min->type == Included) {
    key = &min->data;
    inclusize = true;
  } else if (min->type == Excluded) {
    key = &min->data;
  } else {
    return false;
  }

dengyihao's avatar
dengyihao 已提交
1154
  FstNode* node = fstGetRoot(sws->fst);
1155 1156
  Output   out = 0;
  // void*   autState = sws->aut->start();
dengyihao's avatar
dengyihao 已提交
1157
  void* autState = automFuncs[aut->type].start(aut);
dengyihao's avatar
dengyihao 已提交
1158

1159
  int32_t  len;
dengyihao's avatar
dengyihao 已提交
1160
  uint8_t* data = fstSliceData(key, &len);
dengyihao's avatar
dengyihao 已提交
1161
  for (uint32_t i = 0; i < len; i++) {
1162
    uint8_t  b = data[i];
dengyihao's avatar
dengyihao 已提交
1163
    uint64_t res = 0;
1164
    bool     null = fstNodeFindInput(node, b, &res);
dengyihao's avatar
dengyihao 已提交
1165 1166
    if (null == false) {
      FstTransition trn;
1167
      fstNodeGetTransitionAt(node, res, &trn);
dengyihao's avatar
dengyihao 已提交
1168
      void* preState = autState;
dengyihao's avatar
dengyihao 已提交
1169 1170
      // autState = sws->aut->accept(preState, b);
      autState = automFuncs[aut->type].accept(aut, preState, b);
dengyihao's avatar
dengyihao 已提交
1171
      taosArrayPush(sws->inp, &b);
1172
      StreamState s = {.node = node, .trans = res + 1, .out = {.null = false, .out = out}, .autState = preState};
dengyihao's avatar
dengyihao 已提交
1173 1174
      taosArrayPush(sws->stack, &s);
      out += trn.out;
1175
      node = fstGetNode(sws->fst, trn.addr);
dengyihao's avatar
dengyihao 已提交
1176
      fstNodeDestroy(node);
dengyihao's avatar
dengyihao 已提交
1177 1178 1179 1180 1181
    } else {
      // This is a little tricky. We're in this case if the
      // given bound is not a prefix of any key in the FST.
      // Since this is a minimum bound, we need to find the
      // first transition in this node that proceeds the current
1182
      // input byte.
dengyihao's avatar
dengyihao 已提交
1183
      FstTransitions* trans = fstNodeTransitions(node);
1184
      uint64_t        i = 0;
dengyihao's avatar
dengyihao 已提交
1185 1186
      for (i = trans->range.start; i < trans->range.end; i++) {
        FstTransition trn;
dengyihao's avatar
dengyihao 已提交
1187
        if (fstNodeGetTransitionAt(node, i, &trn) && trn.inp > b) { break; }
dengyihao's avatar
dengyihao 已提交
1188
      }
1189 1190 1191 1192

      StreamState s = {.node = node, .trans = i, .out = {.null = false, .out = out}, .autState = autState};
      taosArrayPush(sws->stack, &s);
      return true;
dengyihao's avatar
dengyihao 已提交
1193 1194
    }
  }
1195
  uint32_t sz = taosArrayGetSize(sws->stack);
dengyihao's avatar
dengyihao 已提交
1196
  if (sz != 0) {
dengyihao's avatar
dengyihao 已提交
1197
    StreamState* s = taosArrayGet(sws->stack, sz - 1);
dengyihao's avatar
dengyihao 已提交
1198 1199 1200 1201
    if (inclusize) {
      s->trans -= 1;
      taosArrayPop(sws->inp);
    } else {
dengyihao's avatar
dengyihao 已提交
1202
      FstNode*      n = s->node;
1203 1204
      uint64_t      trans = s->trans;
      FstTransition trn;
dengyihao's avatar
dengyihao 已提交
1205
      fstNodeGetTransitionAt(n, trans - 1, &trn);
dengyihao's avatar
dengyihao 已提交
1206
      StreamState s = {.node = fstGetNode(sws->fst, trn.addr), .trans = 0, .out = {.null = false, .out = out}, .autState = autState};
dengyihao's avatar
dengyihao 已提交
1207
      taosArrayPush(sws->stack, &s);
1208 1209 1210 1211 1212
      return true;
    }
    return false;
  }
}
dengyihao's avatar
dengyihao 已提交
1213

dengyihao's avatar
dengyihao 已提交
1214 1215
StreamWithStateResult* streamWithStateNextWith(StreamWithState* sws, StreamCallback callback) {
  AutomationCtx* aut = sws->aut;
1216
  FstOutput      output = sws->emptyOutput;
dengyihao's avatar
dengyihao 已提交
1217
  if (output.null == false) {
1218
    FstSlice emptySlice = fstSliceCreate(NULL, 0);
dengyihao's avatar
dengyihao 已提交
1219 1220
    if (fstBoundWithDataExceededBy(sws->endAt, &emptySlice)) {
      taosArrayDestroyEx(sws->stack, streamStateDestroy);
dengyihao's avatar
dengyihao 已提交
1221
      sws->stack = (SArray*)taosArrayInit(256, sizeof(StreamState));
dengyihao's avatar
dengyihao 已提交
1222 1223
      return NULL;
    }
dengyihao's avatar
dengyihao 已提交
1224
    void* start = automFuncs[aut->type].start(aut);
dengyihao's avatar
dengyihao 已提交
1225
    if (automFuncs[aut->type].isMatch(aut, start)) {
dengyihao's avatar
dengyihao 已提交
1226
      FstSlice s = fstSliceCreate(NULL, 0);
dengyihao's avatar
dengyihao 已提交
1227
      return swsResultCreate(&s, output, callback == NULL ? NULL : callback(start));
dengyihao's avatar
dengyihao 已提交
1228 1229
    }
  }
dengyihao's avatar
dengyihao 已提交
1230
  SArray* nodes = taosArrayInit(8, sizeof(FstNode*));
dengyihao's avatar
dengyihao 已提交
1231
  while (taosArrayGetSize(sws->stack) > 0) {
dengyihao's avatar
dengyihao 已提交
1232
    StreamState* p = (StreamState*)taosArrayPop(sws->stack);
dengyihao's avatar
dengyihao 已提交
1233
    if (p->trans >= FST_NODE_LEN(p->node) || !automFuncs[aut->type].canMatch(aut, p->autState)) {
dengyihao's avatar
dengyihao 已提交
1234
      if (FST_NODE_ADDR(p->node) != fstGetRootAddr(sws->fst)) { taosArrayPop(sws->inp); }
1235
      streamStateDestroy(p);
dengyihao's avatar
dengyihao 已提交
1236 1237
      continue;
    }
1238
    FstTransition trn;
dengyihao's avatar
dengyihao 已提交
1239
    fstNodeGetTransitionAt(p->node, p->trans, &trn);
dengyihao's avatar
dengyihao 已提交
1240 1241 1242 1243 1244 1245

    Output out = p->out.out + trn.out;
    void*  nextState = automFuncs[aut->type].accept(aut, p->autState, trn.inp);
    void*  tState = (callback == NULL) ? NULL : callback(nextState);
    bool   isMatch = automFuncs[aut->type].isMatch(aut, nextState);

dengyihao's avatar
dengyihao 已提交
1246
    FstNode* nextNode = fstGetNode(sws->fst, trn.addr);
1247 1248
    taosArrayPush(nodes, &nextNode);
    taosArrayPush(sws->inp, &(trn.inp));
dengyihao's avatar
dengyihao 已提交
1249 1250

    if (FST_NODE_IS_FINAL(nextNode)) {
1251
      // void *eofState = sws->aut->acceptEof(nextState);
dengyihao's avatar
dengyihao 已提交
1252
      void* eofState = automFuncs[aut->type].acceptEof(aut, nextState);
dengyihao's avatar
dengyihao 已提交
1253
      if (eofState != NULL) { isMatch = automFuncs[aut->type].isMatch(aut, eofState); }
1254 1255
    }
    StreamState s1 = {.node = p->node, .trans = p->trans + 1, .out = p->out, .autState = p->autState};
dengyihao's avatar
dengyihao 已提交
1256 1257 1258 1259
    taosArrayPush(sws->stack, &s1);

    StreamState s2 = {.node = nextNode, .trans = 0, .out = {.null = false, .out = out}, .autState = nextState};
    taosArrayPush(sws->stack, &s2);
dengyihao's avatar
dengyihao 已提交
1260

1261
    size_t   isz = taosArrayGetSize(sws->inp);
dengyihao's avatar
dengyihao 已提交
1262 1263 1264 1265
    uint8_t* buf = (uint8_t*)malloc(isz * sizeof(uint8_t));
    for (uint32_t i = 0; i < isz; i++) {
      buf[i] = *(uint8_t*)taosArrayGet(sws->inp, i);
    }
dengyihao's avatar
dengyihao 已提交
1266 1267 1268
    FstSlice slice = fstSliceCreate(buf, taosArrayGetSize(sws->inp));
    if (fstBoundWithDataExceededBy(sws->endAt, &slice)) {
      taosArrayDestroyEx(sws->stack, streamStateDestroy);
dengyihao's avatar
dengyihao 已提交
1269
      sws->stack = (SArray*)taosArrayInit(256, sizeof(StreamState));
dengyihao's avatar
dengyihao 已提交
1270
      free(buf);
dengyihao's avatar
dengyihao 已提交
1271
      fstSliceDestroy(&slice);
dengyihao's avatar
dengyihao 已提交
1272 1273 1274
      return NULL;
    }
    if (FST_NODE_IS_FINAL(nextNode) && isMatch) {
1275
      FstOutput              fOutput = {.null = false, .out = out + FST_NODE_FINAL_OUTPUT(nextNode)};
dengyihao's avatar
dengyihao 已提交
1276
      StreamWithStateResult* result = swsResultCreate(&slice, fOutput, tState);
dengyihao's avatar
dengyihao 已提交
1277
      free(buf);
dengyihao's avatar
dengyihao 已提交
1278
      fstSliceDestroy(&slice);
1279
      return result;
dengyihao's avatar
dengyihao 已提交
1280
    }
dengyihao's avatar
dengyihao 已提交
1281
    free(buf);
dengyihao's avatar
dengyihao 已提交
1282
    fstSliceDestroy(&slice);
dengyihao's avatar
dengyihao 已提交
1283
  }
dengyihao's avatar
dengyihao 已提交
1284
  for (size_t i = 0; i < taosArrayGetSize(nodes); i++) {
dengyihao's avatar
dengyihao 已提交
1285
    FstNode** node = (FstNode**)taosArrayGet(nodes, i);
dengyihao's avatar
dengyihao 已提交
1286 1287 1288
    fstNodeDestroy(*node);
  }
  taosArrayDestroy(nodes);
1289
  return NULL;
dengyihao's avatar
dengyihao 已提交
1290 1291
}

dengyihao's avatar
dengyihao 已提交
1292 1293
StreamWithStateResult* swsResultCreate(FstSlice* data, FstOutput fOut, void* state) {
  StreamWithStateResult* result = calloc(1, sizeof(StreamWithStateResult));
dengyihao's avatar
dengyihao 已提交
1294
  if (result == NULL) { return NULL; }
1295 1296 1297 1298

  result->data = fstSliceCopy(data, 0, FST_SLICE_LEN(data) - 1);
  result->out = fOut;
  result->state = state;
dengyihao's avatar
dengyihao 已提交
1299 1300 1301

  return result;
}
dengyihao's avatar
dengyihao 已提交
1302
void swsResultDestroy(StreamWithStateResult* result) {
dengyihao's avatar
dengyihao 已提交
1303
  if (NULL == result) { return; }
1304

dengyihao's avatar
dengyihao 已提交
1305
  fstSliceDestroy(&result->data);
1306
  startWithStateValueDestroy(result->state);
dengyihao's avatar
dengyihao 已提交
1307 1308 1309
  free(result);
}

dengyihao's avatar
dengyihao 已提交
1310
void streamStateDestroy(void* s) {
dengyihao's avatar
dengyihao 已提交
1311
  if (NULL == s) { return; }
dengyihao's avatar
dengyihao 已提交
1312
  StreamState* ss = (StreamState*)s;
dengyihao's avatar
dengyihao 已提交
1313 1314

  fstNodeDestroy(ss->node);
1315
  // free(s->autoState);
dengyihao's avatar
dengyihao 已提交
1316 1317
}

dengyihao's avatar
dengyihao 已提交
1318 1319
FstStreamBuilder* fstStreamBuilderCreate(Fst* fst, AutomationCtx* aut) {
  FstStreamBuilder* b = calloc(1, sizeof(FstStreamBuilder));
dengyihao's avatar
dengyihao 已提交
1320
  if (NULL == b) { return NULL; }
dengyihao's avatar
dengyihao 已提交
1321 1322 1323 1324

  b->fst = fst;
  b->aut = aut;
  b->min = fstBoundStateCreate(Unbounded, NULL);
1325
  b->max = fstBoundStateCreate(Unbounded, NULL);
dengyihao's avatar
dengyihao 已提交
1326 1327
  return b;
}
dengyihao's avatar
dengyihao 已提交
1328
void fstStreamBuilderDestroy(FstStreamBuilder* b) {
dengyihao's avatar
dengyihao 已提交
1329
  fstSliceDestroy(&b->min->data);
1330
  tfree(b->min);
dengyihao's avatar
dengyihao 已提交
1331
  fstSliceDestroy(&b->max->data);
dengyihao's avatar
dengyihao 已提交
1332
  tfree(b->max);
dengyihao's avatar
dengyihao 已提交
1333 1334
  free(b);
}
dengyihao's avatar
dengyihao 已提交
1335
FstStreamBuilder* fstStreamBuilderRange(FstStreamBuilder* b, FstSlice* val, RangeType type) {
dengyihao's avatar
dengyihao 已提交
1336
  if (b == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

  if (type == GE) {
    b->min->type = Included;
    fstSliceDestroy(&(b->min->data));
    b->min->data = fstSliceDeepCopy(val, 0, FST_SLICE_LEN(val) - 1);
  } else if (type == GT) {
    b->min->type = Excluded;
    fstSliceDestroy(&(b->min->data));
    b->min->data = fstSliceDeepCopy(val, 0, FST_SLICE_LEN(val) - 1);
  } else if (type == LE) {
    b->max->type = Included;
    fstSliceDestroy(&(b->max->data));
    b->max->data = fstSliceDeepCopy(val, 0, FST_SLICE_LEN(val) - 1);
  } else if (type == LT) {
    b->max->type = Excluded;
    fstSliceDestroy(&(b->max->data));
    b->max->data = fstSliceDeepCopy(val, 0, FST_SLICE_LEN(val) - 1);
  }
  return b;
}