indexFst.c 42.3 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 23
static FORCE_INLINE void fstPackDeltaIn(IdxFstFile* wrt, CompiledAddr nodeAddr, CompiledAddr transAddr,
                                        uint8_t nBytes) {
dengyihao's avatar
dengyihao 已提交
24
  CompiledAddr deltaAddr = (transAddr == EMPTY_ADDRESS) ? EMPTY_ADDRESS : nodeAddr - transAddr;
dengyihao's avatar
dengyihao 已提交
25
  idxFilePackUintIn(wrt, deltaAddr, nBytes);
dengyihao's avatar
dengyihao 已提交
26
}
dengyihao's avatar
dengyihao 已提交
27
static FORCE_INLINE uint8_t fstPackDelta(IdxFstFile* wrt, CompiledAddr nodeAddr, CompiledAddr transAddr) {
dengyihao's avatar
dengyihao 已提交
28
  uint8_t nBytes = packDeltaSize(nodeAddr, transAddr);
29
  fstPackDeltaIn(wrt, nodeAddr, transAddr, nBytes);
dengyihao's avatar
dengyihao 已提交
30
  return nBytes;
dengyihao's avatar
dengyihao 已提交
31
}
dengyihao's avatar
dengyihao 已提交
32

dengyihao's avatar
dengyihao 已提交
33
FstUnFinishedNodes* fstUnFinishedNodesCreate() {
wafwerar's avatar
wafwerar 已提交
34
  FstUnFinishedNodes* nodes = taosMemoryMalloc(sizeof(FstUnFinishedNodes));
dengyihao's avatar
dengyihao 已提交
35 36 37
  if (nodes == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
38

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  FST_SET_TRANSITION_PACK_SIZE(packSizes, tSize);
272

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

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

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

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

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

// input_len

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

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

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

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

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

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

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

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

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

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

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

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

// anyTrans specify function

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
1081
FstNode* fstGetRoot(Fst* fst) {
dengyihao's avatar
dengyihao 已提交
1082 1083
  CompiledAddr addr = fstGetRootAddr(fst);
  return fstGetNode(fst, addr);
dengyihao's avatar
dengyihao 已提交
1084
}
dengyihao's avatar
dengyihao 已提交
1085

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

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

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

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

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

1130
  return b;
dengyihao's avatar
dengyihao 已提交
1131 1132
}

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
1165
  sws->emptyOutput.null = true;
1166
  sws->emptyOutput.out = 0;
dengyihao's avatar
dengyihao 已提交
1167

dengyihao's avatar
dengyihao 已提交
1168
  sws->stack = (SArray*)taosArrayInit(256, sizeof(StreamState));
1169
  sws->endAt = max;
dengyihao's avatar
dengyihao 已提交
1170
  stmStSeekMin(sws, min);
dengyihao's avatar
dengyihao 已提交
1171 1172 1173

  return sws;
}
dengyihao's avatar
dengyihao 已提交
1174
void stmStDestroy(FStmSt* sws) {
dengyihao's avatar
dengyihao 已提交
1175 1176 1177
  if (sws == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1178 1179

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

wafwerar's avatar
wafwerar 已提交
1182
  taosMemoryFree(sws);
dengyihao's avatar
dengyihao 已提交
1183 1184
}

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

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

dengyihao's avatar
dengyihao 已提交
1210
  FstNode* node = fstGetRoot(sws->fst);
1211
  Output   out = 0;
dengyihao's avatar
dengyihao 已提交
1212
  void*    autState = automFuncs[aut->type].start(aut);
dengyihao's avatar
dengyihao 已提交
1213

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

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

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

      StreamState s = {.node = node, .trans = i, .out = {.null = false, .out = out}, .autState = autState};
      taosArrayPush(sws->stack, &s);
dengyihao's avatar
dengyihao 已提交
1249
      taosMemoryFree(trans);
1250
      return true;
dengyihao's avatar
dengyihao 已提交
1251 1252
    }
  }
dengyihao's avatar
dengyihao 已提交
1253 1254 1255

  fstNodeDestroy(node);

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

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

    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 已提交
1311
    FstNode* nextNode = fstGetNode(sws->fst, trn.addr);
1312 1313
    taosArrayPush(nodes, &nextNode);
    taosArrayPush(sws->inp, &(trn.inp));
dengyihao's avatar
dengyihao 已提交
1314 1315

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

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

dengyihao's avatar
dengyihao 已提交
1357
FStmStRslt* swsResultCreate(FstSlice* data, FstOutput out, void* state) {
dengyihao's avatar
dengyihao 已提交
1358
  FStmStRslt* result = taosMemoryCalloc(1, sizeof(FStmStRslt));
dengyihao's avatar
dengyihao 已提交
1359 1360 1361
  if (result == NULL) {
    return NULL;
  }
1362 1363

  result->data = fstSliceCopy(data, 0, FST_SLICE_LEN(data) - 1);
dengyihao's avatar
dengyihao 已提交
1364
  result->out = out;
1365
  result->state = state;
dengyihao's avatar
dengyihao 已提交
1366 1367
  return result;
}
dengyihao's avatar
dengyihao 已提交
1368
void swsResultDestroy(FStmStRslt* result) {
dengyihao's avatar
dengyihao 已提交
1369 1370 1371
  if (NULL == result) {
    return;
  }
1372

dengyihao's avatar
dengyihao 已提交
1373
  fstSliceDestroy(&result->data);
1374
  startWithStateValueDestroy(result->state);
wafwerar's avatar
wafwerar 已提交
1375
  taosMemoryFree(result);
dengyihao's avatar
dengyihao 已提交
1376 1377
}

dengyihao's avatar
dengyihao 已提交
1378
void streamStateDestroy(void* s) {
dengyihao's avatar
dengyihao 已提交
1379 1380 1381
  if (NULL == s) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
1382
  StreamState* ss = (StreamState*)s;
dengyihao's avatar
dengyihao 已提交
1383 1384 1385
  fstNodeDestroy(ss->node);
}

dengyihao's avatar
dengyihao 已提交
1386 1387
FStmBuilder* stmBuilderCreate(Fst* fst, FAutoCtx* aut) {
  FStmBuilder* b = taosMemoryCalloc(1, sizeof(FStmBuilder));
dengyihao's avatar
dengyihao 已提交
1388 1389 1390
  if (NULL == b) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1391 1392 1393 1394

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