indexFst.c 42.2 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

dengyihao's avatar
dengyihao 已提交
16 17
#include "indexFst.h"
#include "indexFstAutomation.h"
18 19 20
#include "indexInt.h"
#include "tchecksum.h"
#include "tcoding.h"
dengyihao's avatar
dengyihao 已提交
21

dengyihao's avatar
dengyihao 已提交
22
static void fstPackDeltaIn(IdxFstFile* wrt, CompiledAddr nodeAddr, CompiledAddr transAddr, uint8_t nBytes) {
dengyihao's avatar
dengyihao 已提交
23
  CompiledAddr deltaAddr = (transAddr == EMPTY_ADDRESS) ? EMPTY_ADDRESS : nodeAddr - transAddr;
dengyihao's avatar
dengyihao 已提交
24
  idxFilePackUintIn(wrt, deltaAddr, nBytes);
dengyihao's avatar
dengyihao 已提交
25
}
dengyihao's avatar
dengyihao 已提交
26
static uint8_t fstPackDetla(IdxFstFile* 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
FstUnFinishedNodes* fstUnFinishedNodesCreate() {
wafwerar's avatar
wafwerar 已提交
33
  FstUnFinishedNodes* nodes = taosMemoryMalloc(sizeof(FstUnFinishedNodes));
dengyihao's avatar
dengyihao 已提交
34 35 36
  if (nodes == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
37

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

53
  taosArrayDestroyEx(nodes->stack, unFinishedNodeDestroyElem);
wafwerar's avatar
wafwerar 已提交
54
  taosMemoryFree(nodes);
dengyihao's avatar
dengyihao 已提交
55 56
}

dengyihao's avatar
dengyihao 已提交
57
void fstUnFinishedNodesPushEmpty(FstUnFinishedNodes* nodes, bool isFinal) {
wafwerar's avatar
wafwerar 已提交
58
  FstBuilderNode* node = taosMemoryMalloc(sizeof(FstBuilderNode));
59
  node->isFinal = isFinal;
dengyihao's avatar
dengyihao 已提交
60
  node->finalOutput = 0;
61
  node->trans = taosArrayInit(16, sizeof(FstTransition));
dengyihao's avatar
dengyihao 已提交
62

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

dengyihao's avatar
dengyihao 已提交
69
  FstBuilderNodeUnfinished* un = taosArrayPop(nodes->stack);
70 71
  assert(un->last == NULL);
  return un->node;
dengyihao's avatar
dengyihao 已提交
72 73
}

dengyihao's avatar
dengyihao 已提交
74 75
FstBuilderNode* fstUnFinishedNodesPopFreeze(FstUnFinishedNodes* nodes, CompiledAddr addr) {
  FstBuilderNodeUnfinished* un = taosArrayPop(nodes->stack);
dengyihao's avatar
dengyihao 已提交
76
  fstBuilderNodeUnfinishedLastCompiled(un, addr);
wafwerar's avatar
wafwerar 已提交
77
  // taosMemoryFree(un->last); // TODO add func FstLastTransitionFree()
78 79
  // un->last = NULL;
  return un->node;
dengyihao's avatar
dengyihao 已提交
80 81
}

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

wafwerar's avatar
wafwerar 已提交
106
  // FstLastTransition *trn = taosMemoryMalloc(sizeof(FstLastTransition));
107 108 109
  // trn->inp = s->data[s->start];
  // trn->out = out;
  int32_t  len = 0;
dengyihao's avatar
dengyihao 已提交
110
  uint8_t* data = fstSliceData(s, &len);
111
  un->last = fstLastTransitionCreate(data[0], out);
dengyihao's avatar
dengyihao 已提交
112

dengyihao's avatar
dengyihao 已提交
113
  for (uint64_t i = 1; i < len; i++) {
wafwerar's avatar
wafwerar 已提交
114
    FstBuilderNode* n = taosMemoryMalloc(sizeof(FstBuilderNode));
115
    n->isFinal = false;
dengyihao's avatar
dengyihao 已提交
116
    n->finalOutput = 0;
117 118
    n->trans = taosArrayInit(16, sizeof(FstTransition));

wafwerar's avatar
wafwerar 已提交
119
    // FstLastTransition *trn = taosMemoryMalloc(sizeof(FstLastTransition));
120 121
    // trn->inp = s->data[i];
    // trn->out = out;
dengyihao's avatar
dengyihao 已提交
122
    FstLastTransition* trn = fstLastTransitionCreate(data[i], 0);
dengyihao's avatar
dengyihao 已提交
123

124 125
    FstBuilderNodeUnfinished un = {.node = n, .last = trn};
    taosArrayPush(nodes->stack, &un);
dengyihao's avatar
dengyihao 已提交
126
  }
127
  fstUnFinishedNodesPushEmpty(nodes, true);
dengyihao's avatar
dengyihao 已提交
128 129
}

dengyihao's avatar
dengyihao 已提交
130 131
uint64_t fstUnFinishedNodesFindCommPrefix(FstUnFinishedNodes* node, FstSlice bs) {
  FstSlice* s = &bs;
dengyihao's avatar
dengyihao 已提交
132

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

dengyihao's avatar
dengyihao 已提交
150 151
  int32_t lsz = (size_t)(s->end - s->start + 1);  // data len
  int32_t ssz = taosArrayGetSize(node->stack);    // stack size
dengyihao's avatar
dengyihao 已提交
152
  *out = in;
dengyihao's avatar
dengyihao 已提交
153 154
  uint64_t i = 0;
  for (i = 0; i < lsz && i < ssz; i++) {
dengyihao's avatar
dengyihao 已提交
155
    FstBuilderNodeUnfinished* un = taosArrayGet(node->stack, i);
dengyihao's avatar
dengyihao 已提交
156

dengyihao's avatar
dengyihao 已提交
157
    FstLastTransition* t = un->last;
158
    uint64_t           addPrefix = 0;
dengyihao's avatar
dengyihao 已提交
159
    uint8_t*           data = fstSliceData(s, NULL);
dengyihao's avatar
dengyihao 已提交
160
    if (t && t->inp == data[i]) {
dengyihao's avatar
dengyihao 已提交
161
      uint64_t commPrefix = TMIN(t->out, *out);
162 163
      uint64_t tAddPrefix = t->out - commPrefix;
      (*out) = (*out) - commPrefix;
dengyihao's avatar
dengyihao 已提交
164
      t->out = commPrefix;
165
      addPrefix = tAddPrefix;
dengyihao's avatar
dengyihao 已提交
166
    } else {
167
      break;
dengyihao's avatar
dengyihao 已提交
168 169
    }
    if (addPrefix != 0) {
dengyihao's avatar
dengyihao 已提交
170
      if (i + 1 < ssz) {
dengyihao's avatar
dengyihao 已提交
171
        FstBuilderNodeUnfinished* unf = taosArrayGet(node->stack, i + 1);
172
        fstBuilderNodeUnfinishedAddOutputPrefix(unf, addPrefix);
dengyihao's avatar
dengyihao 已提交
173
      }
dengyihao's avatar
dengyihao 已提交
174
    }
175
  }
dengyihao's avatar
dengyihao 已提交
176
  return i;
177
}
dengyihao's avatar
dengyihao 已提交
178

dengyihao's avatar
dengyihao 已提交
179
FstState fstStateCreateFrom(FstSlice* slice, CompiledAddr addr) {
dengyihao's avatar
dengyihao 已提交
180
  FstState fs = {.state = EmptyFinal, .val = 0};
dengyihao's avatar
dengyihao 已提交
181 182 183
  if (addr == EMPTY_ADDRESS) {
    return fs;
  }
184

dengyihao's avatar
dengyihao 已提交
185
  uint8_t* data = fstSliceData(slice, NULL);
186 187
  uint8_t  v = data[addr];
  uint8_t  t = (v & 0b11000000) >> 6;
dengyihao's avatar
dengyihao 已提交
188 189 190
  if (t == 0b11) {
    fs.state = OneTransNext;
  } else if (t == 0b10) {
191
    fs.state = OneTrans;
dengyihao's avatar
dengyihao 已提交
192
  } else {
193
    fs.state = AnyTrans;
dengyihao's avatar
dengyihao 已提交
194
  }
dengyihao's avatar
dengyihao 已提交
195
  fs.val = v;
dengyihao's avatar
dengyihao 已提交
196 197
  return fs;
}
dengyihao's avatar
dengyihao 已提交
198

dengyihao's avatar
dengyihao 已提交
199 200 201 202
static FstState fstStateDict[] = {{.state = OneTransNext, .val = 0b11000000},
                                  {.state = OneTrans, .val = 0b10000000},
                                  {.state = AnyTrans, .val = 0b00000000},
                                  {.state = EmptyFinal, .val = 0b00000000}};
203
// debug
dengyihao's avatar
dengyihao 已提交
204
static const char* fstStateStr[] = {"ONE_TRANS_NEXT", "ONE_TRANS", "ANY_TRANS", "EMPTY_FINAL"};
dengyihao's avatar
dengyihao 已提交
205

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

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

  FST_SET_OUTPUT_PACK_SIZE(packSizes, outPackSize);
  FST_SET_TRANSITION_PACK_SIZE(packSizes, transPackSize);
dengyihao's avatar
dengyihao 已提交
233
  idxFileWrite(w, (char*)&packSizes, sizeof(packSizes));
234 235

  FstState st = fstStateCreate(OneTrans);
dengyihao's avatar
dengyihao 已提交
236 237

  fstStateSetCommInput(&st, trn->inp);
dengyihao's avatar
dengyihao 已提交
238

239 240
  bool    null = false;
  uint8_t inp = fstStateCommInput(&st, &null);
dengyihao's avatar
dengyihao 已提交
241
  if (null == true) {
dengyihao's avatar
dengyihao 已提交
242
    idxFileWrite(w, (char*)&trn->inp, sizeof(trn->inp));
dengyihao's avatar
dengyihao 已提交
243
  }
dengyihao's avatar
dengyihao 已提交
244
  idxFileWrite(w, (char*)(&(st.val)), sizeof(st.val));
245
  return;
dengyihao's avatar
dengyihao 已提交
246
}
dengyihao's avatar
dengyihao 已提交
247
void fstStateCompileForAnyTrans(IdxFstFile* w, CompiledAddr addr, FstBuilderNode* node) {
dengyihao's avatar
dengyihao 已提交
248
  int32_t sz = taosArrayGetSize(node->trans);
dengyihao's avatar
dengyihao 已提交
249 250 251
  assert(sz <= 256);

  uint8_t tSize = 0;
252 253
  uint8_t oSize = packSize(node->finalOutput);

dengyihao's avatar
dengyihao 已提交
254
  // finalOutput.is_zero()
255
  bool anyOuts = (node->finalOutput != 0);
dengyihao's avatar
dengyihao 已提交
256
  for (int32_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
257
    FstTransition* t = taosArrayGet(node->trans, i);
dengyihao's avatar
dengyihao 已提交
258 259
    tSize = TMAX(tSize, packDeltaSize(addr, t->addr));
    oSize = TMAX(oSize, packSize(t->out));
260
    anyOuts = anyOuts || (t->out != 0);
dengyihao's avatar
dengyihao 已提交
261 262
  }

263 264 265 266 267 268
  PackSizes packSizes = 0;
  if (anyOuts) {
    FST_SET_OUTPUT_PACK_SIZE(packSizes, oSize);
  } else {
    FST_SET_OUTPUT_PACK_SIZE(packSizes, 0);
  }
dengyihao's avatar
dengyihao 已提交
269 270

  FST_SET_TRANSITION_PACK_SIZE(packSizes, tSize);
271

dengyihao's avatar
dengyihao 已提交
272
  FstState st = fstStateCreate(AnyTrans);
273
  fstStateSetFinalState(&st, node->isFinal);
dengyihao's avatar
dengyihao 已提交
274
  fstStateSetStateNtrans(&st, (uint8_t)sz);
275

dengyihao's avatar
dengyihao 已提交
276
  if (anyOuts) {
dengyihao's avatar
dengyihao 已提交
277
    if (FST_BUILDER_NODE_IS_FINAL(node)) {
dengyihao's avatar
dengyihao 已提交
278
      idxFilePackUintIn(w, node->finalOutput, oSize);
dengyihao's avatar
dengyihao 已提交
279
    }
dengyihao's avatar
dengyihao 已提交
280
    for (int32_t i = sz - 1; i >= 0; i--) {
dengyihao's avatar
dengyihao 已提交
281
      FstTransition* t = taosArrayGet(node->trans, i);
dengyihao's avatar
dengyihao 已提交
282
      idxFilePackUintIn(w, t->out, oSize);
dengyihao's avatar
dengyihao 已提交
283
    }
284
  }
dengyihao's avatar
dengyihao 已提交
285
  for (int32_t i = sz - 1; i >= 0; i--) {
dengyihao's avatar
dengyihao 已提交
286
    FstTransition* t = taosArrayGet(node->trans, i);
287
    fstPackDeltaIn(w, addr, t->addr, tSize);
dengyihao's avatar
dengyihao 已提交
288
  }
dengyihao's avatar
dengyihao 已提交
289
  for (int32_t i = sz - 1; i >= 0; i--) {
dengyihao's avatar
dengyihao 已提交
290
    FstTransition* t = taosArrayGet(node->trans, i);
dengyihao's avatar
dengyihao 已提交
291
    idxFileWrite(w, (char*)&t->inp, 1);
dengyihao's avatar
dengyihao 已提交
292 293
  }
  if (sz > TRANS_INDEX_THRESHOLD) {
dengyihao's avatar
dengyihao 已提交
294
    // A value of 255 indicates that no transition exists for the byte at that idx
wafwerar's avatar
wafwerar 已提交
295
    uint8_t* index = (uint8_t*)taosMemoryMalloc(sizeof(uint8_t) * 256);
dengyihao's avatar
dengyihao 已提交
296
    memset(index, 255, sizeof(uint8_t) * 256);
dengyihao's avatar
dengyihao 已提交
297
    for (int32_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
298
      FstTransition* t = taosArrayGet(node->trans, i);
dengyihao's avatar
dengyihao 已提交
299 300
      index[t->inp] = i;
    }
dengyihao's avatar
dengyihao 已提交
301
    idxFileWrite(w, (char*)index, 256);
wafwerar's avatar
wafwerar 已提交
302
    taosMemoryFree(index);
dengyihao's avatar
dengyihao 已提交
303
  }
dengyihao's avatar
dengyihao 已提交
304
  idxFileWrite(w, (char*)&packSizes, 1);
dengyihao's avatar
dengyihao 已提交
305 306 307
  bool null = false;
  fstStateStateNtrans(&st, &null);
  if (null == true) {
308 309 310
    // 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 已提交
311
    uint8_t v = 1;
312
    if (sz == 256) {
dengyihao's avatar
dengyihao 已提交
313
      idxFileWrite(w, (char*)&v, 1);
314
    } else {
dengyihao's avatar
dengyihao 已提交
315
      idxFileWrite(w, (char*)&sz, 1);
316
    }
dengyihao's avatar
dengyihao 已提交
317
  }
dengyihao's avatar
dengyihao 已提交
318
  idxFileWrite(w, (char*)(&(st.val)), 1);
dengyihao's avatar
dengyihao 已提交
319 320 321 322
  return;
}

// set_comm_input
dengyihao's avatar
dengyihao 已提交
323
void fstStateSetCommInput(FstState* s, uint8_t inp) {
dengyihao's avatar
dengyihao 已提交
324 325 326
  assert(s->state == OneTransNext || s->state == OneTrans);

  uint8_t val;
dengyihao's avatar
dengyihao 已提交
327
  COMMON_INDEX(inp, 0b111111, val);
328
  s->val = (s->val & fstStateDict[s->state].val) | val;
dengyihao's avatar
dengyihao 已提交
329 330 331
}

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

// input_len

dengyihao's avatar
dengyihao 已提交
345
uint64_t fstStateInputLen(FstState* s) {
dengyihao's avatar
dengyihao 已提交
346
  assert(s->state == OneTransNext || s->state == OneTrans);
dengyihao's avatar
dengyihao 已提交
347
  bool null = false;
348 349 350 351 352
  fstStateCommInput(s, &null);
  return null ? 1 : 0;
}

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

383 384
  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 已提交
385

dengyihao's avatar
dengyihao 已提交
386
  uint8_t* data = fstSliceData(slice, NULL);
dengyihao's avatar
dengyihao 已提交
387
  return data[at];
dengyihao's avatar
dengyihao 已提交
388 389 390
}

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

dengyihao's avatar
dengyihao 已提交
410
  FstSlice* slice = &node->data;
411
  uint8_t   tSizes = FST_GET_TRANSITION_PACK_SIZE(node->sizes);
dengyihao's avatar
dengyihao 已提交
412 413
  uint64_t  at = node->start - fstStateNtransLen(s) - 1 - fstStateTransIndexSize(s, node->version, node->nTrans) -
                node->nTrans - (i * tSizes) - tSizes;
dengyihao's avatar
dengyihao 已提交
414
  uint8_t* data = fstSliceData(slice, NULL);
415
  return unpackDelta(data + at, tSizes, node->end);
dengyihao's avatar
dengyihao 已提交
416 417
}

418
// sizes
dengyihao's avatar
dengyihao 已提交
419
PackSizes fstStateSizes(FstState* s, FstSlice* slice) {
420 421
  assert(s->state == OneTrans || s->state == AnyTrans);
  uint64_t i;
dengyihao's avatar
dengyihao 已提交
422
  if (s->state == OneTrans) {
423
    i = FST_SLICE_LEN(slice) - 1 - fstStateInputLen(s) - 1;
dengyihao's avatar
dengyihao 已提交
424 425 426 427
  } else {
    i = FST_SLICE_LEN(slice) - 1 - fstStateNtransLen(s) - 1;
  }

dengyihao's avatar
dengyihao 已提交
428
  uint8_t* data = fstSliceData(slice, NULL);
429
  return (PackSizes)(*(data + i));
dengyihao's avatar
dengyihao 已提交
430
}
431
// Output
dengyihao's avatar
dengyihao 已提交
432
Output fstStateOutput(FstState* s, FstNode* node) {
433 434
  assert(s->state == OneTrans);

dengyihao's avatar
dengyihao 已提交
435
  uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(node->sizes);
dengyihao's avatar
dengyihao 已提交
436 437 438
  if (oSizes == 0) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
439
  FstSlice* slice = &node->data;
440 441 442
  uint8_t   tSizes = FST_GET_TRANSITION_PACK_SIZE(node->sizes);

  uint64_t i = node->start - fstStateInputLen(s) - 1 - tSizes - oSizes;
dengyihao's avatar
dengyihao 已提交
443
  uint8_t* data = fstSliceData(slice, NULL);
dengyihao's avatar
dengyihao 已提交
444
  return unpackUint64(data + i, oSizes);
dengyihao's avatar
dengyihao 已提交
445
}
dengyihao's avatar
dengyihao 已提交
446
Output fstStateOutputForAnyTrans(FstState* s, FstNode* node, uint64_t i) {
dengyihao's avatar
dengyihao 已提交
447 448
  assert(s->state == AnyTrans);

449
  uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(node->sizes);
dengyihao's avatar
dengyihao 已提交
450 451 452
  if (oSizes == 0) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
453 454
  FstSlice* slice = &node->data;
  uint8_t*  data = fstSliceData(slice, NULL);
455 456
  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 已提交
457 458

  return unpackUint64(data + at, oSizes);
dengyihao's avatar
dengyihao 已提交
459 460 461 462
}

// anyTrans specify function

dengyihao's avatar
dengyihao 已提交
463
void fstStateSetFinalState(FstState* s, bool yes) {
464
  assert(s->state == AnyTrans);
dengyihao's avatar
dengyihao 已提交
465 466 467
  if (yes) {
    s->val |= 0b01000000;
  }
dengyihao's avatar
dengyihao 已提交
468 469
  return;
}
dengyihao's avatar
dengyihao 已提交
470
bool fstStateIsFinalState(FstState* s) {
471 472 473
  assert(s->state == AnyTrans);
  return (s->val & 0b01000000) == 0b01000000;
}
dengyihao's avatar
dengyihao 已提交
474

dengyihao's avatar
dengyihao 已提交
475
void fstStateSetStateNtrans(FstState* s, uint8_t n) {
476
  assert(s->state == AnyTrans);
dengyihao's avatar
dengyihao 已提交
477 478 479
  if (n <= 0b00111111) {
    s->val = (s->val & 0b11000000) | n;
  }
dengyihao's avatar
dengyihao 已提交
480 481 482
  return;
}
// state_ntrans
dengyihao's avatar
dengyihao 已提交
483
uint8_t fstStateStateNtrans(FstState* s, bool* null) {
484
  assert(s->state == AnyTrans);
dengyihao's avatar
dengyihao 已提交
485
  *null = false;
486
  uint8_t n = s->val & 0b00111111;
dengyihao's avatar
dengyihao 已提交
487 488

  if (n == 0) {
489 490
    *null = true;  // None
  }
dengyihao's avatar
dengyihao 已提交
491
  return n;
dengyihao's avatar
dengyihao 已提交
492
}
dengyihao's avatar
dengyihao 已提交
493
uint64_t fstStateTotalTransSize(FstState* s, uint64_t version, PackSizes sizes, uint64_t nTrans) {
494 495
  assert(s->state == AnyTrans);
  uint64_t idxSize = fstStateTransIndexSize(s, version, nTrans);
dengyihao's avatar
dengyihao 已提交
496
  return nTrans + (nTrans * FST_GET_TRANSITION_PACK_SIZE(sizes)) + idxSize;
dengyihao's avatar
dengyihao 已提交
497
}
dengyihao's avatar
dengyihao 已提交
498
uint64_t fstStateTransIndexSize(FstState* s, uint64_t version, uint64_t nTrans) {
499 500
  assert(s->state == AnyTrans);
  return (version >= 2 && nTrans > TRANS_INDEX_THRESHOLD) ? 256 : 0;
dengyihao's avatar
dengyihao 已提交
501
}
dengyihao's avatar
dengyihao 已提交
502
uint64_t fstStateNtransLen(FstState* s) {
dengyihao's avatar
dengyihao 已提交
503 504 505
  assert(s->state == AnyTrans);
  bool null = false;
  fstStateStateNtrans(s, &null);
506
  return null == true ? 1 : 0;
dengyihao's avatar
dengyihao 已提交
507
}
dengyihao's avatar
dengyihao 已提交
508
uint64_t fstStateNtrans(FstState* s, FstSlice* slice) {
509
  bool    null = false;
dengyihao's avatar
dengyihao 已提交
510
  uint8_t n = fstStateStateNtrans(s, &null);
dengyihao's avatar
dengyihao 已提交
511 512 513
  if (null != true) {
    return n;
  }
514
  int32_t  len;
dengyihao's avatar
dengyihao 已提交
515
  uint8_t* data = fstSliceData(slice, &len);
dengyihao's avatar
dengyihao 已提交
516
  n = data[len - 2];
517 518 519
  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 已提交
520
Output fstStateFinalOutput(FstState* s, uint64_t version, FstSlice* slice, PackSizes sizes, uint64_t nTrans) {
521
  uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(sizes);
dengyihao's avatar
dengyihao 已提交
522 523 524
  if (oSizes == 0 || !fstStateIsFinalState(s)) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
525

526 527
  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 已提交
528
  uint8_t* data = fstSliceData(slice, NULL);
529
  return unpackUint64(data + at, (uint8_t)oSizes);
dengyihao's avatar
dengyihao 已提交
530
}
dengyihao's avatar
dengyihao 已提交
531
uint64_t fstStateFindInput(FstState* s, FstNode* node, uint8_t b, bool* null) {
dengyihao's avatar
dengyihao 已提交
532
  assert(s->state == AnyTrans);
dengyihao's avatar
dengyihao 已提交
533
  FstSlice* slice = &node->data;
dengyihao's avatar
dengyihao 已提交
534
  if (node->version >= 2 && node->nTrans > TRANS_INDEX_THRESHOLD) {
535
    uint64_t at = node->start - fstStateNtransLen(s) - 1  // pack size
dengyihao's avatar
dengyihao 已提交
536
                  - fstStateTransIndexSize(s, node->version, node->nTrans);
537
    int32_t  dlen = 0;
dengyihao's avatar
dengyihao 已提交
538
    uint8_t* data = fstSliceData(slice, &dlen);
dengyihao's avatar
dengyihao 已提交
539
    uint64_t i = data[at + b];
dengyihao's avatar
dengyihao 已提交
540 541 542
    if (i >= node->nTrans) {
      *null = true;
    }
dengyihao's avatar
dengyihao 已提交
543 544
    return i;
  } else {
545 546 547
    uint64_t start = node->start - fstStateNtransLen(s) - 1  // pack size
                     - node->nTrans;
    uint64_t end = start + node->nTrans;
dengyihao's avatar
dengyihao 已提交
548
    FstSlice t = fstSliceCopy(slice, start, end - 1);
549
    int32_t  len = 0;
dengyihao's avatar
dengyihao 已提交
550
    uint8_t* data = fstSliceData(&t, &len);
dengyihao's avatar
dengyihao 已提交
551
    for (int i = 0; i < len; i++) {
552
      uint8_t v = data[i];
dengyihao's avatar
dengyihao 已提交
553
      if (v == b) {
dengyihao's avatar
dengyihao 已提交
554
        fstSliceDestroy(&t);
555
        return node->nTrans - i - 1;  // bug
dengyihao's avatar
dengyihao 已提交
556
      }
dengyihao's avatar
dengyihao 已提交
557 558 559
      if (i + 1 == len) {
        *null = true;
      }
dengyihao's avatar
dengyihao 已提交
560
    }
dengyihao's avatar
dengyihao 已提交
561
    fstSliceDestroy(&t);
562
  }
563 564

  return 0;
dengyihao's avatar
dengyihao 已提交
565 566
}

567
// fst node function
dengyihao's avatar
dengyihao 已提交
568

dengyihao's avatar
dengyihao 已提交
569
FstNode* fstNodeCreate(int64_t version, CompiledAddr addr, FstSlice* slice) {
wafwerar's avatar
wafwerar 已提交
570
  FstNode* n = (FstNode*)taosMemoryMalloc(sizeof(FstNode));
dengyihao's avatar
dengyihao 已提交
571 572 573
  if (n == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
574

575
  FstState st = fstStateCreateFrom(slice, addr);
dengyihao's avatar
dengyihao 已提交
576 577

  if (st.state == EmptyFinal) {
578 579 580 581 582 583 584 585 586
    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 已提交
587
  } else if (st.state == OneTransNext) {
588 589 590 591 592 593 594 595 596
    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 已提交
597
  } else if (st.state == OneTrans) {
598 599 600 601 602 603 604 605 606 607 608
    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 已提交
609
  } else {
610 611 612 613 614 615 616 617 618 619 620
    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 已提交
621 622
    n->finalOutput =
        fstStateFinalOutput(&st, version, &data, sz, nTrans);  // s.final_output(version, data, sz, ntrans);
623 624
  }
  return n;
dengyihao's avatar
dengyihao 已提交
625
}
dengyihao's avatar
dengyihao 已提交
626 627

// debug state transition
dengyihao's avatar
dengyihao 已提交
628 629
static const char* fstNodeState(FstNode* node) {
  FstState* st = &node->state;
630
  return fstStateStr[st->state];
dengyihao's avatar
dengyihao 已提交
631 632
}

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
742
bool fstBuilderNodeCompileTo(FstBuilderNode* b, IdxFstFile* wrt, CompiledAddr lastAddr, CompiledAddr startAddr) {
dengyihao's avatar
dengyihao 已提交
743 744 745
  return fstNodeCompile(NULL, wrt, lastAddr, startAddr, b);
}

dengyihao's avatar
dengyihao 已提交
746
FstBuilder* fstBuilderCreate(void* w, FstType ty) {
wafwerar's avatar
wafwerar 已提交
747
  FstBuilder* b = taosMemoryMalloc(sizeof(FstBuilder));
dengyihao's avatar
dengyihao 已提交
748 749 750
  if (NULL == b) {
    return b;
  }
dengyihao's avatar
dengyihao 已提交
751

dengyihao's avatar
dengyihao 已提交
752
  b->wrt = idxFileCreate(w);
753 754 755 756 757
  b->unfinished = fstUnFinishedNodesCreate();
  b->registry = fstRegistryCreate(10000, 2);
  b->last = fstSliceCreate(NULL, 0);
  b->lastAddr = NONE_ADDRESS;
  b->len = 0;
dengyihao's avatar
dengyihao 已提交
758

759
  char  buf64[8] = {0};
dengyihao's avatar
dengyihao 已提交
760
  void* pBuf64 = buf64;
761
  taosEncodeFixedU64(&pBuf64, VERSION);
dengyihao's avatar
dengyihao 已提交
762
  idxFileWrite(b->wrt, buf64, sizeof(buf64));
763

dengyihao's avatar
dengyihao 已提交
764
  pBuf64 = buf64;
dengyihao's avatar
dengyihao 已提交
765
  memset(buf64, 0, sizeof(buf64));
766
  taosEncodeFixedU64(&pBuf64, ty);
dengyihao's avatar
dengyihao 已提交
767
  idxFileWrite(b->wrt, buf64, sizeof(buf64));
dengyihao's avatar
dengyihao 已提交
768

dengyihao's avatar
dengyihao 已提交
769 770
  return b;
}
dengyihao's avatar
dengyihao 已提交
771
void fstBuilderDestroy(FstBuilder* b) {
dengyihao's avatar
dengyihao 已提交
772 773 774
  if (b == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
775

dengyihao's avatar
dengyihao 已提交
776
  idxFileDestroy(b->wrt);
777
  fstUnFinishedNodesDestroy(b->unfinished);
dengyihao's avatar
dengyihao 已提交
778
  fstRegistryDestroy(b->registry);
dengyihao's avatar
dengyihao 已提交
779
  fstSliceDestroy(&b->last);
wafwerar's avatar
wafwerar 已提交
780
  taosMemoryFree(b);
dengyihao's avatar
dengyihao 已提交
781
}
dengyihao's avatar
dengyihao 已提交
782

dengyihao's avatar
dengyihao 已提交
783
bool fstBuilderInsert(FstBuilder* b, FstSlice bs, Output in) {
dengyihao's avatar
dengyihao 已提交
784
  FstOrderType t = fstBuilderCheckLastKey(b, bs, true);
dengyihao's avatar
dengyihao 已提交
785 786
  if (t == Ordered) {
    // add log info
787 788 789
    fstBuilderInsertOutput(b, bs, in);
    return true;
  }
dengyihao's avatar
dengyihao 已提交
790
  indexInfo("fst write key must be ordered");
dengyihao's avatar
dengyihao 已提交
791 792 793
  return false;
}

dengyihao's avatar
dengyihao 已提交
794 795
void fstBuilderInsertOutput(FstBuilder* b, FstSlice bs, Output in) {
  FstSlice* s = &bs;
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
  if (fstSliceIsEmpty(s)) {
    b->len = 1;
    fstUnFinishedNodesSetRootOutput(b->unfinished, in);
    return;
  }
  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 已提交
817

dengyihao's avatar
dengyihao 已提交
818
FstOrderType fstBuilderCheckLastKey(FstBuilder* b, FstSlice bs, bool ckDup) {
dengyihao's avatar
dengyihao 已提交
819
  FstSlice* input = &bs;
dengyihao's avatar
dengyihao 已提交
820
  if (fstSliceIsEmpty(&b->last)) {
dengyihao's avatar
dengyihao 已提交
821
    fstSliceDestroy(&b->last);
dengyihao's avatar
dengyihao 已提交
822
    // deep copy or not
dengyihao's avatar
dengyihao 已提交
823
    b->last = fstSliceDeepCopy(&bs, input->start, input->end);
dengyihao's avatar
dengyihao 已提交
824 825 826
  } else {
    int comp = fstSliceCompare(&b->last, &bs);
    if (comp == 0 && ckDup) {
827
      return DuplicateKey;
dengyihao's avatar
dengyihao 已提交
828 829 830 831
    } else if (comp == 1) {
      return OutOfOrdered;
    }
    // deep copy or not
dengyihao's avatar
dengyihao 已提交
832
    fstSliceDestroy(&b->last);
833 834
    b->last = fstSliceDeepCopy(&bs, input->start, input->end);
  }
dengyihao's avatar
dengyihao 已提交
835
  return Ordered;
836
}
dengyihao's avatar
dengyihao 已提交
837
void fstBuilderCompileFrom(FstBuilder* b, uint64_t istate) {
dengyihao's avatar
dengyihao 已提交
838 839
  CompiledAddr addr = NONE_ADDRESS;
  while (istate + 1 < FST_UNFINISHED_NODES_LEN(b->unfinished)) {
dengyihao's avatar
dengyihao 已提交
840
    FstBuilderNode* bn = NULL;
dengyihao's avatar
dengyihao 已提交
841
    if (addr == NONE_ADDRESS) {
dengyihao's avatar
dengyihao 已提交
842
      bn = fstUnFinishedNodesPopEmpty(b->unfinished);
dengyihao's avatar
dengyihao 已提交
843
    } else {
dengyihao's avatar
dengyihao 已提交
844
      bn = fstUnFinishedNodesPopFreeze(b->unfinished, addr);
dengyihao's avatar
dengyihao 已提交
845
    }
dengyihao's avatar
dengyihao 已提交
846 847 848
    addr = fstBuilderCompile(b, bn);

    fstBuilderNodeDestroy(bn);
849
    assert(addr != NONE_ADDRESS);
dengyihao's avatar
dengyihao 已提交
850 851
  }
  fstUnFinishedNodesTopLastFreeze(b->unfinished, addr);
852
  return;
dengyihao's avatar
dengyihao 已提交
853
}
dengyihao's avatar
dengyihao 已提交
854

dengyihao's avatar
dengyihao 已提交
855
CompiledAddr fstBuilderCompile(FstBuilder* b, FstBuilderNode* bn) {
856 857
  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 已提交
858
  }
dengyihao's avatar
dengyihao 已提交
859
  FstRegistryEntry* entry = fstRegistryGetEntry(b->registry, bn);
860
  if (entry->state == FOUND) {
dengyihao's avatar
dengyihao 已提交
861
    CompiledAddr ret = entry->addr;
dengyihao's avatar
dengyihao 已提交
862
    fstRegistryEntryDestroy(entry);
dengyihao's avatar
dengyihao 已提交
863
    return ret;
864
  }
dengyihao's avatar
dengyihao 已提交
865 866
  CompiledAddr startAddr = (CompiledAddr)(FST_WRITER_COUNT(b->wrt));

867 868
  fstBuilderNodeCompileTo(bn, b->wrt, b->lastAddr, startAddr);
  b->lastAddr = (CompiledAddr)(FST_WRITER_COUNT(b->wrt) - 1);
dengyihao's avatar
dengyihao 已提交
869 870 871
  if (entry->state == NOTFOUND) {
    FST_REGISTRY_CELL_INSERT(entry->cell, b->lastAddr);
  }
dengyihao's avatar
dengyihao 已提交
872
  fstRegistryEntryDestroy(entry);
873 874

  return b->lastAddr;
dengyihao's avatar
dengyihao 已提交
875 876
}

dengyihao's avatar
dengyihao 已提交
877
void* fstBuilderInsertInner(FstBuilder* b) {
878
  fstBuilderCompileFrom(b, 0);
dengyihao's avatar
dengyihao 已提交
879
  FstBuilderNode* rootNode = fstUnFinishedNodesPopRoot(b->unfinished);
880
  CompiledAddr    rootAddr = fstBuilderCompile(b, rootNode);
dengyihao's avatar
dengyihao 已提交
881
  fstBuilderNodeDestroy(rootNode);
dengyihao's avatar
dengyihao 已提交
882

883 884
  char buf64[8] = {0};

dengyihao's avatar
dengyihao 已提交
885
  void* pBuf64 = buf64;
886
  taosEncodeFixedU64(&pBuf64, b->len);
dengyihao's avatar
dengyihao 已提交
887
  idxFileWrite(b->wrt, buf64, sizeof(buf64));
dengyihao's avatar
dengyihao 已提交
888

dengyihao's avatar
dengyihao 已提交
889
  pBuf64 = buf64;
890
  taosEncodeFixedU64(&pBuf64, rootAddr);
dengyihao's avatar
dengyihao 已提交
891
  idxFileWrite(b->wrt, buf64, sizeof(buf64));
dengyihao's avatar
dengyihao 已提交
892

893
  char     buf32[4] = {0};
dengyihao's avatar
dengyihao 已提交
894
  void*    pBuf32 = buf32;
dengyihao's avatar
dengyihao 已提交
895
  uint32_t sum = idxFileMaskedCheckSum(b->wrt);
896
  taosEncodeFixedU32(&pBuf32, sum);
dengyihao's avatar
dengyihao 已提交
897
  idxFileWrite(b->wrt, buf32, sizeof(buf32));
898

dengyihao's avatar
dengyihao 已提交
899
  idxFileFlush(b->wrt);
dengyihao's avatar
dengyihao 已提交
900 901
  return b->wrt;
}
dengyihao's avatar
dengyihao 已提交
902
void fstBuilderFinish(FstBuilder* b) { fstBuilderInsertInner(b); }
dengyihao's avatar
dengyihao 已提交
903

dengyihao's avatar
dengyihao 已提交
904 905
FstSlice fstNodeAsSlice(FstNode* node) {
  FstSlice* slice = &node->data;
906 907
  FstSlice  s = fstSliceCopy(slice, slice->end, FST_SLICE_LEN(slice) - 1);
  return s;
dengyihao's avatar
dengyihao 已提交
908
}
dengyihao's avatar
dengyihao 已提交
909

dengyihao's avatar
dengyihao 已提交
910
FstLastTransition* fstLastTransitionCreate(uint8_t inp, Output out) {
wafwerar's avatar
wafwerar 已提交
911
  FstLastTransition* trn = taosMemoryMalloc(sizeof(FstLastTransition));
dengyihao's avatar
dengyihao 已提交
912 913 914
  if (trn == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
915 916 917 918 919 920

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

wafwerar's avatar
wafwerar 已提交
921
void fstLastTransitionDestroy(FstLastTransition* trn) { taosMemoryFree(trn); }
dengyihao's avatar
dengyihao 已提交
922

dengyihao's avatar
dengyihao 已提交
923 924
void fstBuilderNodeUnfinishedLastCompiled(FstBuilderNodeUnfinished* unNode, CompiledAddr addr) {
  FstLastTransition* trn = unNode->last;
dengyihao's avatar
dengyihao 已提交
925 926 927
  if (trn == NULL) {
    return;
  }
928 929 930
  FstTransition t = {.inp = trn->inp, .out = trn->out, .addr = addr};
  taosArrayPush(unNode->node->trans, &t);
  fstLastTransitionDestroy(trn);
dengyihao's avatar
dengyihao 已提交
931
  unNode->last = NULL;
dengyihao's avatar
dengyihao 已提交
932 933 934
  return;
}

dengyihao's avatar
dengyihao 已提交
935
void fstBuilderNodeUnfinishedAddOutputPrefix(FstBuilderNodeUnfinished* unNode, Output out) {
dengyihao's avatar
dengyihao 已提交
936 937 938
  if (FST_BUILDER_NODE_IS_FINAL(unNode->node)) {
    unNode->node->finalOutput += out;
  }
dengyihao's avatar
dengyihao 已提交
939 940
  int32_t sz = taosArrayGetSize(unNode->node->trans);
  for (int32_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
941
    FstTransition* trn = taosArrayGet(unNode->node->trans, i);
dengyihao's avatar
dengyihao 已提交
942 943
    trn->out += out;
  }
dengyihao's avatar
dengyihao 已提交
944 945 946
  if (unNode->last) {
    unNode->last->out += out;
  }
dengyihao's avatar
dengyihao 已提交
947 948 949
  return;
}

dengyihao's avatar
dengyihao 已提交
950
Fst* fstCreate(FstSlice* slice) {
dengyihao's avatar
dengyihao 已提交
951
  int32_t slen;
dengyihao's avatar
dengyihao 已提交
952
  char*   buf = fstSliceData(slice, &slen);
dengyihao's avatar
dengyihao 已提交
953 954 955
  if (slen < 36) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
956
  uint64_t len = slen;
957
  uint64_t skip = 0;
dengyihao's avatar
dengyihao 已提交
958

959 960 961
  uint64_t version;
  taosDecodeFixedU64(buf, &version);
  skip += sizeof(version);
dengyihao's avatar
dengyihao 已提交
962 963 964
  if (version == 0 || version > VERSION) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
965 966 967

  uint64_t type;
  taosDecodeFixedU64(buf + skip, &type);
968
  skip += sizeof(type);
dengyihao's avatar
dengyihao 已提交
969 970 971

  uint32_t checkSum = 0;
  len -= sizeof(checkSum);
972
  taosDecodeFixedU32(buf + len, &checkSum);
dengyihao's avatar
dengyihao 已提交
973
  if (taosCheckChecksum(buf, len, checkSum)) {
dengyihao's avatar
dengyihao 已提交
974
    indexError("index file is corrupted");
dengyihao's avatar
dengyihao 已提交
975 976 977
    // verify fst
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
978
  CompiledAddr rootAddr;
979 980
  len -= sizeof(rootAddr);
  taosDecodeFixedU64(buf + len, &rootAddr);
dengyihao's avatar
dengyihao 已提交
981

982 983
  uint64_t fstLen;
  len -= sizeof(fstLen);
dengyihao's avatar
dengyihao 已提交
984
  taosDecodeFixedU64(buf + len, &fstLen);
985
  // TODO(validate root addr)
wafwerar's avatar
wafwerar 已提交
986
  Fst* fst = (Fst*)taosMemoryCalloc(1, sizeof(Fst));
dengyihao's avatar
dengyihao 已提交
987 988 989
  if (fst == NULL) {
    return NULL;
  }
990

wafwerar's avatar
wafwerar 已提交
991
  fst->meta = (FstMeta*)taosMemoryMalloc(sizeof(FstMeta));
dengyihao's avatar
dengyihao 已提交
992 993 994
  if (NULL == fst->meta) {
    goto FST_CREAT_FAILED;
  }
dengyihao's avatar
dengyihao 已提交
995

996 997 998 999
  fst->meta->version = version;
  fst->meta->rootAddr = rootAddr;
  fst->meta->ty = type;
  fst->meta->len = fstLen;
dengyihao's avatar
dengyihao 已提交
1000
  fst->meta->checkSum = checkSum;
dengyihao's avatar
dengyihao 已提交
1001

wafwerar's avatar
wafwerar 已提交
1002
  FstSlice* s = taosMemoryCalloc(1, sizeof(FstSlice));
dengyihao's avatar
dengyihao 已提交
1003
  *s = fstSliceCopy(slice, 0, FST_SLICE_LEN(slice) - 1);
1004 1005
  fst->data = s;

wafwerar's avatar
wafwerar 已提交
1006
  taosThreadMutexInit(&fst->mtx, NULL);
dengyihao's avatar
dengyihao 已提交
1007 1008
  return fst;

1009
FST_CREAT_FAILED:
wafwerar's avatar
wafwerar 已提交
1010 1011
  taosMemoryFree(fst->meta);
  taosMemoryFree(fst);
1012 1013

  return NULL;
dengyihao's avatar
dengyihao 已提交
1014
}
dengyihao's avatar
dengyihao 已提交
1015
void fstDestroy(Fst* fst) {
1016
  if (fst) {
wafwerar's avatar
wafwerar 已提交
1017
    taosMemoryFree(fst->meta);
dengyihao's avatar
dengyihao 已提交
1018
    fstSliceDestroy(fst->data);
wafwerar's avatar
wafwerar 已提交
1019
    taosMemoryFree(fst->data);
wafwerar's avatar
wafwerar 已提交
1020
    taosThreadMutexDestroy(&fst->mtx);
1021
  }
wafwerar's avatar
wafwerar 已提交
1022
  taosMemoryFree(fst);
dengyihao's avatar
dengyihao 已提交
1023 1024
}

dengyihao's avatar
dengyihao 已提交
1025 1026
bool fstGet(Fst* fst, FstSlice* b, Output* out) {
  FstNode* root = fstGetRoot(fst);
1027 1028
  Output   tOut = 0;
  int32_t  len;
dengyihao's avatar
dengyihao 已提交
1029

dengyihao's avatar
dengyihao 已提交
1030
  uint8_t* data = fstSliceData(b, &len);
dengyihao's avatar
dengyihao 已提交
1031

dengyihao's avatar
dengyihao 已提交
1032
  SArray* nodes = (SArray*)taosArrayInit(len, sizeof(FstNode*));
dengyihao's avatar
dengyihao 已提交
1033
  taosArrayPush(nodes, &root);
dengyihao's avatar
dengyihao 已提交
1034 1035
  for (uint32_t i = 0; i < len; i++) {
    uint8_t inp = data[i];
dengyihao's avatar
dengyihao 已提交
1036
    Output  res = 0;
dengyihao's avatar
dengyihao 已提交
1037 1038 1039
    if (false == fstNodeFindInput(root, inp, &res)) {
      return false;
    }
dengyihao's avatar
dengyihao 已提交
1040

1041
    FstTransition trn;
dengyihao's avatar
dengyihao 已提交
1042
    fstNodeGetTransitionAt(root, res, &trn);
1043
    tOut += trn.out;
dengyihao's avatar
dengyihao 已提交
1044
    root = fstGetNode(fst, trn.addr);
dengyihao's avatar
dengyihao 已提交
1045
    taosArrayPush(nodes, &root);
dengyihao's avatar
dengyihao 已提交
1046 1047 1048 1049
  }
  if (!FST_NODE_IS_FINAL(root)) {
    return false;
  } else {
1050
    tOut = tOut + FST_NODE_FINAL_OUTPUT(root);
dengyihao's avatar
dengyihao 已提交
1051
  }
dengyihao's avatar
dengyihao 已提交
1052

dengyihao's avatar
dengyihao 已提交
1053
  for (int32_t i = 0; i < taosArrayGetSize(nodes); i++) {
dengyihao's avatar
dengyihao 已提交
1054
    FstNode** node = (FstNode**)taosArrayGet(nodes, i);
1055
    fstNodeDestroy(*node);
dengyihao's avatar
dengyihao 已提交
1056 1057
  }
  taosArrayDestroy(nodes);
dengyihao's avatar
dengyihao 已提交
1058
  *out = tOut;
1059
  return true;
dengyihao's avatar
dengyihao 已提交
1060
}
dengyihao's avatar
dengyihao 已提交
1061
FStmBuilder* fstSearch(Fst* fst, FAutoCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
1062
  // refactor later
dengyihao's avatar
dengyihao 已提交
1063
  return stmBuilderCreate(fst, ctx);
dengyihao's avatar
dengyihao 已提交
1064
}
dengyihao's avatar
dengyihao 已提交
1065
FStmSt* stmBuilderIntoStm(FStmBuilder* sb) {
dengyihao's avatar
dengyihao 已提交
1066 1067 1068
  if (sb == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1069
  return stmStCreate(sb->fst, sb->aut, sb->min, sb->max);
dengyihao's avatar
dengyihao 已提交
1070
}
dengyihao's avatar
dengyihao 已提交
1071
FStmStBuilder* fstSearchWithState(Fst* fst, FAutoCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
1072
  // refactor later
dengyihao's avatar
dengyihao 已提交
1073
  return stmBuilderCreate(fst, ctx);
dengyihao's avatar
dengyihao 已提交
1074
}
dengyihao's avatar
dengyihao 已提交
1075

dengyihao's avatar
dengyihao 已提交
1076
FstNode* fstGetRoot(Fst* fst) {
1077
  CompiledAddr rAddr = fstGetRootAddr(fst);
dengyihao's avatar
dengyihao 已提交
1078
  return fstGetNode(fst, rAddr);
dengyihao's avatar
dengyihao 已提交
1079
}
dengyihao's avatar
dengyihao 已提交
1080

dengyihao's avatar
dengyihao 已提交
1081
FstNode* fstGetNode(Fst* fst, CompiledAddr addr) {
dengyihao's avatar
dengyihao 已提交
1082
  // refactor later
dengyihao's avatar
dengyihao 已提交
1083 1084
  return fstNodeCreate(fst->meta->version, addr, fst->data);
}
dengyihao's avatar
dengyihao 已提交
1085 1086
FstType      fstGetType(Fst* fst) { return fst->meta->ty; }
CompiledAddr fstGetRootAddr(Fst* fst) { return fst->meta->rootAddr; }
dengyihao's avatar
dengyihao 已提交
1087

dengyihao's avatar
dengyihao 已提交
1088
Output fstEmptyFinalOutput(Fst* fst, bool* null) {
1089
  Output   res = 0;
dengyihao's avatar
dengyihao 已提交
1090
  FstNode* node = fstGetRoot(fst);
dengyihao's avatar
dengyihao 已提交
1091 1092
  if (FST_NODE_IS_FINAL(node)) {
    *null = false;
1093
    res = FST_NODE_FINAL_OUTPUT(node);
dengyihao's avatar
dengyihao 已提交
1094 1095 1096
  } else {
    *null = true;
  }
1097
  fstNodeDestroy(node);
dengyihao's avatar
dengyihao 已提交
1098 1099 1100
  return res;
}

dengyihao's avatar
dengyihao 已提交
1101
bool fstVerify(Fst* fst) {
dengyihao's avatar
dengyihao 已提交
1102
  uint32_t len, checkSum = fst->meta->checkSum;
dengyihao's avatar
dengyihao 已提交
1103
  uint8_t* data = fstSliceData(fst->data, &len);
1104
  TSCKSUM  initSum = 0;
dengyihao's avatar
dengyihao 已提交
1105 1106 1107
  if (!taosCheckChecksumWhole(data, len)) {
    return false;
  }
dengyihao's avatar
dengyihao 已提交
1108
  return true;
dengyihao's avatar
dengyihao 已提交
1109 1110
}

dengyihao's avatar
dengyihao 已提交
1111
// data bound function
dengyihao's avatar
dengyihao 已提交
1112
FstBoundWithData* fstBoundStateCreate(FstBound type, FstSlice* data) {
wafwerar's avatar
wafwerar 已提交
1113
  FstBoundWithData* b = taosMemoryCalloc(1, sizeof(FstBoundWithData));
dengyihao's avatar
dengyihao 已提交
1114 1115 1116
  if (b == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1117 1118 1119 1120 1121 1122

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

1125
  return b;
dengyihao's avatar
dengyihao 已提交
1126 1127
}

dengyihao's avatar
dengyihao 已提交
1128
bool fstBoundWithDataExceededBy(FstBoundWithData* bound, FstSlice* slice) {
dengyihao's avatar
dengyihao 已提交
1129 1130 1131 1132 1133 1134
  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 已提交
1135
    return false;
dengyihao's avatar
dengyihao 已提交
1136 1137
  }
}
dengyihao's avatar
dengyihao 已提交
1138
bool fstBoundWithDataIsEmpty(FstBoundWithData* bound) {
dengyihao's avatar
dengyihao 已提交
1139 1140
  if (bound->type == Unbounded) {
    return true;
1141 1142 1143
  } else {
    return fstSliceIsEmpty(&bound->data);
  }
dengyihao's avatar
dengyihao 已提交
1144 1145
}

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

wafwerar's avatar
wafwerar 已提交
1148
void fstBoundDestroy(FstBoundWithData* bound) { taosMemoryFree(bound); }
dengyihao's avatar
dengyihao 已提交
1149

dengyihao's avatar
dengyihao 已提交
1150 1151
FStmSt* stmStCreate(Fst* fst, FAutoCtx* automation, FstBoundWithData* min, FstBoundWithData* max) {
  FStmSt* sws = taosMemoryCalloc(1, sizeof(FStmSt));
dengyihao's avatar
dengyihao 已提交
1152 1153 1154
  if (sws == NULL) {
    return NULL;
  }
1155 1156 1157

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

dengyihao's avatar
dengyihao 已提交
1160
  sws->emptyOutput.null = true;
1161
  sws->emptyOutput.out = 0;
dengyihao's avatar
dengyihao 已提交
1162

dengyihao's avatar
dengyihao 已提交
1163
  sws->stack = (SArray*)taosArrayInit(256, sizeof(StreamState));
1164
  sws->endAt = max;
dengyihao's avatar
dengyihao 已提交
1165
  stmStSeekMin(sws, min);
dengyihao's avatar
dengyihao 已提交
1166 1167 1168

  return sws;
}
dengyihao's avatar
dengyihao 已提交
1169
void stmStDestroy(FStmSt* sws) {
dengyihao's avatar
dengyihao 已提交
1170 1171 1172
  if (sws == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1173 1174

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

wafwerar's avatar
wafwerar 已提交
1177
  taosMemoryFree(sws);
dengyihao's avatar
dengyihao 已提交
1178 1179
}

dengyihao's avatar
dengyihao 已提交
1180 1181
bool stmStSeekMin(FStmSt* sws, FstBoundWithData* min) {
  FAutoCtx* aut = sws->aut;
dengyihao's avatar
dengyihao 已提交
1182
  if (fstBoundWithDataIsEmpty(min)) {
dengyihao's avatar
dengyihao 已提交
1183 1184 1185
    if (fstBoundWithDataIsIncluded(min)) {
      sws->emptyOutput.out = fstEmptyFinalOutput(sws->fst, &(sws->emptyOutput.null));
    }
1186
    StreamState s = {.node = fstGetRoot(sws->fst),
dengyihao's avatar
dengyihao 已提交
1187 1188 1189
                     .trans = 0,
                     .out = {.null = false, .out = 0},
                     .autState = automFuncs[aut->type].start(aut)};  // auto.start callback
dengyihao's avatar
dengyihao 已提交
1190 1191
    taosArrayPush(sws->stack, &s);
    return true;
1192
  }
dengyihao's avatar
dengyihao 已提交
1193
  FstSlice* key = NULL;
1194
  bool      inclusize = false;
dengyihao's avatar
dengyihao 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

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

dengyihao's avatar
dengyihao 已提交
1205
  FstNode* node = fstGetRoot(sws->fst);
1206
  Output   out = 0;
dengyihao's avatar
dengyihao 已提交
1207
  void*    autState = automFuncs[aut->type].start(aut);
dengyihao's avatar
dengyihao 已提交
1208

1209
  int32_t  len;
dengyihao's avatar
dengyihao 已提交
1210
  uint8_t* data = fstSliceData(key, &len);
dengyihao's avatar
dengyihao 已提交
1211
  for (uint32_t i = 0; i < len; i++) {
1212
    uint8_t  b = data[i];
dengyihao's avatar
dengyihao 已提交
1213
    uint64_t res = 0;
dengyihao's avatar
dengyihao 已提交
1214
    if (fstNodeFindInput(node, b, &res)) {
dengyihao's avatar
dengyihao 已提交
1215
      FstTransition trn;
1216
      fstNodeGetTransitionAt(node, res, &trn);
dengyihao's avatar
dengyihao 已提交
1217
      void* preState = autState;
dengyihao's avatar
dengyihao 已提交
1218
      autState = automFuncs[aut->type].accept(aut, preState, b);
dengyihao's avatar
dengyihao 已提交
1219
      taosArrayPush(sws->inp, &b);
dengyihao's avatar
dengyihao 已提交
1220

1221
      StreamState s = {.node = node, .trans = res + 1, .out = {.null = false, .out = out}, .autState = preState};
dengyihao's avatar
dengyihao 已提交
1222 1223
      node = NULL;

dengyihao's avatar
dengyihao 已提交
1224 1225
      taosArrayPush(sws->stack, &s);
      out += trn.out;
1226
      node = fstGetNode(sws->fst, trn.addr);
dengyihao's avatar
dengyihao 已提交
1227 1228 1229 1230 1231
    } 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
1232
      // input byte.
dengyihao's avatar
dengyihao 已提交
1233
      FstTransitions* trans = fstNodeTransitions(node);
1234
      uint64_t        i = 0;
dengyihao's avatar
dengyihao 已提交
1235 1236
      for (i = trans->range.start; i < trans->range.end; i++) {
        FstTransition trn;
dengyihao's avatar
dengyihao 已提交
1237 1238 1239
        if (fstNodeGetTransitionAt(node, i, &trn) && trn.inp > b) {
          break;
        }
dengyihao's avatar
dengyihao 已提交
1240
      }
1241 1242 1243 1244

      StreamState s = {.node = node, .trans = i, .out = {.null = false, .out = out}, .autState = autState};
      taosArrayPush(sws->stack, &s);
      return true;
dengyihao's avatar
dengyihao 已提交
1245 1246
    }
  }
dengyihao's avatar
dengyihao 已提交
1247 1248 1249

  fstNodeDestroy(node);

1250
  uint32_t sz = taosArrayGetSize(sws->stack);
dengyihao's avatar
dengyihao 已提交
1251
  if (sz != 0) {
dengyihao's avatar
dengyihao 已提交
1252
    StreamState* s = taosArrayGet(sws->stack, sz - 1);
dengyihao's avatar
dengyihao 已提交
1253 1254 1255 1256
    if (inclusize) {
      s->trans -= 1;
      taosArrayPop(sws->inp);
    } else {
dengyihao's avatar
dengyihao 已提交
1257
      FstNode*      n = s->node;
1258 1259
      uint64_t      trans = s->trans;
      FstTransition trn;
dengyihao's avatar
dengyihao 已提交
1260
      fstNodeGetTransitionAt(n, trans - 1, &trn);
dengyihao's avatar
dengyihao 已提交
1261 1262
      StreamState s = {
          .node = fstGetNode(sws->fst, trn.addr), .trans = 0, .out = {.null = false, .out = out}, .autState = autState};
dengyihao's avatar
dengyihao 已提交
1263
      taosArrayPush(sws->stack, &s);
1264 1265 1266 1267
      return true;
    }
    return false;
  }
1268 1269

  return false;
1270
}
dengyihao's avatar
dengyihao 已提交
1271 1272 1273
FStmStRslt* stmStNextWith(FStmSt* sws, StreamCallback callback) {
  FAutoCtx* aut = sws->aut;
  FstOutput output = sws->emptyOutput;
dengyihao's avatar
dengyihao 已提交
1274
  if (output.null == false) {
1275
    FstSlice emptySlice = fstSliceCreate(NULL, 0);
dengyihao's avatar
dengyihao 已提交
1276 1277
    if (fstBoundWithDataExceededBy(sws->endAt, &emptySlice)) {
      taosArrayDestroyEx(sws->stack, streamStateDestroy);
dengyihao's avatar
dengyihao 已提交
1278
      sws->stack = (SArray*)taosArrayInit(256, sizeof(StreamState));
dengyihao's avatar
dengyihao 已提交
1279 1280
      return NULL;
    }
dengyihao's avatar
dengyihao 已提交
1281
    void* start = automFuncs[aut->type].start(aut);
dengyihao's avatar
dengyihao 已提交
1282
    if (automFuncs[aut->type].isMatch(aut, start)) {
dengyihao's avatar
dengyihao 已提交
1283
      FstSlice s = fstSliceCreate(NULL, 0);
dengyihao's avatar
dengyihao 已提交
1284
      return swsResultCreate(&s, output, callback == NULL ? NULL : callback(start));
dengyihao's avatar
dengyihao 已提交
1285 1286
    }
  }
dengyihao's avatar
dengyihao 已提交
1287
  SArray* nodes = taosArrayInit(8, sizeof(FstNode*));
dengyihao's avatar
dengyihao 已提交
1288
  while (taosArrayGetSize(sws->stack) > 0) {
dengyihao's avatar
dengyihao 已提交
1289
    StreamState* p = (StreamState*)taosArrayPop(sws->stack);
dengyihao's avatar
dengyihao 已提交
1290
    if (p->trans >= FST_NODE_LEN(p->node) || !automFuncs[aut->type].canMatch(aut, p->autState)) {
dengyihao's avatar
dengyihao 已提交
1291 1292 1293
      if (FST_NODE_ADDR(p->node) != fstGetRootAddr(sws->fst)) {
        taosArrayPop(sws->inp);
      }
dengyihao's avatar
dengyihao 已提交
1294
      streamStateDestroy(p);
dengyihao's avatar
dengyihao 已提交
1295 1296
      continue;
    }
1297
    FstTransition trn;
dengyihao's avatar
dengyihao 已提交
1298
    fstNodeGetTransitionAt(p->node, p->trans, &trn);
dengyihao's avatar
dengyihao 已提交
1299 1300 1301 1302 1303 1304

    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 已提交
1305
    FstNode* nextNode = fstGetNode(sws->fst, trn.addr);
1306 1307
    taosArrayPush(nodes, &nextNode);
    taosArrayPush(sws->inp, &(trn.inp));
dengyihao's avatar
dengyihao 已提交
1308 1309

    if (FST_NODE_IS_FINAL(nextNode)) {
dengyihao's avatar
dengyihao 已提交
1310
      void* eofState = automFuncs[aut->type].acceptEof(aut, nextState);
dengyihao's avatar
dengyihao 已提交
1311 1312 1313
      if (eofState != NULL) {
        isMatch = automFuncs[aut->type].isMatch(aut, eofState);
      }
1314 1315
    }
    StreamState s1 = {.node = p->node, .trans = p->trans + 1, .out = p->out, .autState = p->autState};
dengyihao's avatar
dengyihao 已提交
1316 1317 1318 1319
    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 已提交
1320

dengyihao's avatar
dengyihao 已提交
1321
    int32_t  isz = taosArrayGetSize(sws->inp);
wafwerar's avatar
wafwerar 已提交
1322
    uint8_t* buf = (uint8_t*)taosMemoryMalloc(isz * sizeof(uint8_t));
dengyihao's avatar
dengyihao 已提交
1323 1324 1325
    for (uint32_t i = 0; i < isz; i++) {
      buf[i] = *(uint8_t*)taosArrayGet(sws->inp, i);
    }
dengyihao's avatar
dengyihao 已提交
1326
    FstSlice slice = fstSliceCreate(buf, isz);
dengyihao's avatar
dengyihao 已提交
1327 1328
    if (fstBoundWithDataExceededBy(sws->endAt, &slice)) {
      taosArrayDestroyEx(sws->stack, streamStateDestroy);
dengyihao's avatar
dengyihao 已提交
1329
      sws->stack = (SArray*)taosArrayInit(256, sizeof(StreamState));
wafwerar's avatar
wafwerar 已提交
1330
      taosMemoryFreeClear(buf);
dengyihao's avatar
dengyihao 已提交
1331
      fstSliceDestroy(&slice);
dengyihao's avatar
dengyihao 已提交
1332
      taosArrayDestroy(nodes);
dengyihao's avatar
dengyihao 已提交
1333 1334 1335
      return NULL;
    }
    if (FST_NODE_IS_FINAL(nextNode) && isMatch) {
dengyihao's avatar
dengyihao 已提交
1336 1337
      FstOutput   fOutput = {.null = false, .out = out + FST_NODE_FINAL_OUTPUT(nextNode)};
      FStmStRslt* result = swsResultCreate(&slice, fOutput, tState);
wafwerar's avatar
wafwerar 已提交
1338
      taosMemoryFreeClear(buf);
dengyihao's avatar
dengyihao 已提交
1339
      fstSliceDestroy(&slice);
dengyihao's avatar
dengyihao 已提交
1340
      taosArrayDestroy(nodes);
dengyihao's avatar
dengyihao 已提交
1341
      nodes = NULL;
1342
      return result;
dengyihao's avatar
dengyihao 已提交
1343
    }
wafwerar's avatar
wafwerar 已提交
1344
    taosMemoryFreeClear(buf);
dengyihao's avatar
dengyihao 已提交
1345
    fstSliceDestroy(&slice);
dengyihao's avatar
dengyihao 已提交
1346
  };
dengyihao's avatar
dengyihao 已提交
1347
  taosArrayDestroy(nodes);
1348
  return NULL;
dengyihao's avatar
dengyihao 已提交
1349 1350
}

dengyihao's avatar
dengyihao 已提交
1351
FStmStRslt* swsResultCreate(FstSlice* data, FstOutput out, void* state) {
dengyihao's avatar
dengyihao 已提交
1352
  FStmStRslt* result = taosMemoryCalloc(1, sizeof(FStmStRslt));
dengyihao's avatar
dengyihao 已提交
1353 1354 1355
  if (result == NULL) {
    return NULL;
  }
1356 1357

  result->data = fstSliceCopy(data, 0, FST_SLICE_LEN(data) - 1);
dengyihao's avatar
dengyihao 已提交
1358
  result->out = out;
1359
  result->state = state;
dengyihao's avatar
dengyihao 已提交
1360 1361
  return result;
}
dengyihao's avatar
dengyihao 已提交
1362
void swsResultDestroy(FStmStRslt* result) {
dengyihao's avatar
dengyihao 已提交
1363 1364 1365
  if (NULL == result) {
    return;
  }
1366

dengyihao's avatar
dengyihao 已提交
1367
  fstSliceDestroy(&result->data);
1368
  startWithStateValueDestroy(result->state);
wafwerar's avatar
wafwerar 已提交
1369
  taosMemoryFree(result);
dengyihao's avatar
dengyihao 已提交
1370 1371
}

dengyihao's avatar
dengyihao 已提交
1372
void streamStateDestroy(void* s) {
dengyihao's avatar
dengyihao 已提交
1373 1374 1375
  if (NULL == s) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1376
  StreamState* ss = (StreamState*)s;
dengyihao's avatar
dengyihao 已提交
1377 1378 1379
  fstNodeDestroy(ss->node);
}

dengyihao's avatar
dengyihao 已提交
1380 1381
FStmBuilder* stmBuilderCreate(Fst* fst, FAutoCtx* aut) {
  FStmBuilder* b = taosMemoryCalloc(1, sizeof(FStmBuilder));
dengyihao's avatar
dengyihao 已提交
1382 1383 1384
  if (NULL == b) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1385 1386 1387 1388

  b->fst = fst;
  b->aut = aut;
  b->min = fstBoundStateCreate(Unbounded, NULL);
1389
  b->max = fstBoundStateCreate(Unbounded, NULL);
dengyihao's avatar
dengyihao 已提交
1390 1391
  return b;
}
dengyihao's avatar
dengyihao 已提交
1392
void stmBuilderDestroy(FStmBuilder* b) {
dengyihao's avatar
dengyihao 已提交
1393 1394
  fstSliceDestroy(&b->min->data);
  fstSliceDestroy(&b->max->data);
wafwerar's avatar
wafwerar 已提交
1395 1396 1397
  taosMemoryFreeClear(b->min);
  taosMemoryFreeClear(b->max);
  taosMemoryFree(b);
dengyihao's avatar
dengyihao 已提交
1398
}
dengyihao's avatar
dengyihao 已提交
1399
void stmBuilderSetRange(FStmBuilder* b, FstSlice* val, RangeType type) {
dengyihao's avatar
dengyihao 已提交
1400
  if (b == NULL) {
dengyihao's avatar
dengyihao 已提交
1401
    return;
dengyihao's avatar
dengyihao 已提交
1402
  }
dengyihao's avatar
dengyihao 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
  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);
  }
}