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
  fstBuilderFinish(b);
dengyihao's avatar
dengyihao 已提交
776

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  fstNodeDestroy(node);

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

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

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

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

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

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

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

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

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

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

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