index_fst.h 11.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
#ifndef __INDEX_FST_H__
#define __INDEX_FST_H__
dengyihao's avatar
dengyihao 已提交
18

dengyihao's avatar
dengyihao 已提交
19 20 21
#ifdef __cplusplus
extern "C" {
#endif
dengyihao's avatar
dengyihao 已提交
22

dengyihao's avatar
dengyihao 已提交
23
#include "index_fst_automation.h"
24 25 26 27
#include "index_fst_counting_writer.h"
#include "index_fst_registry.h"
#include "index_fst_util.h"
#include "tarray.h"
dengyihao's avatar
dengyihao 已提交
28

29
#define OUTPUT_PREFIX(a, b) ((a) > (b) ? (b) : (a)
dengyihao's avatar
dengyihao 已提交
30

31 32
typedef struct Fst             Fst;
typedef struct FstNode         FstNode;
dengyihao's avatar
dengyihao 已提交
33
typedef struct StreamWithState StreamWithState;
dengyihao's avatar
dengyihao 已提交
34

35
typedef enum { Included, Excluded, Unbounded } FstBound;
dengyihao's avatar
dengyihao 已提交
36 37

typedef struct FstBoundWithData {
38
  FstSlice data;
dengyihao's avatar
dengyihao 已提交
39 40 41 42
  FstBound type;
} FstBoundWithData;

typedef struct FstStreamBuilder {
dengyihao's avatar
dengyihao 已提交
43 44 45 46
  Fst*              fst;
  AutomationCtx*    aut;
  FstBoundWithData* min;
  FstBoundWithData* max;
dengyihao's avatar
dengyihao 已提交
47
} FstStreamBuilder, FstStreamWithStateBuilder;
dengyihao's avatar
dengyihao 已提交
48 49 50 51 52 53

typedef struct FstRange {
  uint64_t start;
  uint64_t end;
} FstRange;

54 55 56
typedef enum { GE, GT, LE, LT } RangeType;
typedef enum { OneTransNext, OneTrans, AnyTrans, EmptyFinal } State;
typedef enum { Ordered, OutOfOrdered, DuplicateKey } OrderType;
dengyihao's avatar
dengyihao 已提交
57

dengyihao's avatar
dengyihao 已提交
58 59 60 61
FstBoundWithData* fstBoundStateCreate(FstBound type, FstSlice* data);
bool              fstBoundWithDataExceededBy(FstBoundWithData* bound, FstSlice* slice);
bool              fstBoundWithDataIsEmpty(FstBoundWithData* bound);
bool              fstBoundWithDataIsIncluded(FstBoundWithData* bound);
dengyihao's avatar
dengyihao 已提交
62 63 64 65 66 67

typedef struct FstOutput {
  bool   null;
  Output out;
} FstOutput;

dengyihao's avatar
dengyihao 已提交
68
/*
69
 *
dengyihao's avatar
dengyihao 已提交
70
 * UnFinished node and helper function
71
 * TODO: simple function name
dengyihao's avatar
dengyihao 已提交
72 73
 */
typedef struct FstUnFinishedNodes {
dengyihao's avatar
dengyihao 已提交
74
  SArray* stack;  // <FstBuilderNodeUnfinished> } FstUnFinishedNodes;
dengyihao's avatar
dengyihao 已提交
75 76
} FstUnFinishedNodes;

77
#define FST_UNFINISHED_NODES_LEN(nodes) taosArrayGetSize(nodes->stack)
dengyihao's avatar
dengyihao 已提交
78

dengyihao's avatar
dengyihao 已提交
79 80 81 82 83 84 85 86 87 88
FstUnFinishedNodes* fstUnFinishedNodesCreate();
void                fstUnFinishedNodesDestroy(FstUnFinishedNodes* node);
void                fstUnFinishedNodesPushEmpty(FstUnFinishedNodes* nodes, bool isFinal);
void                fstUnFinishedNodesSetRootOutput(FstUnFinishedNodes* node, Output out);
void                fstUnFinishedNodesTopLastFreeze(FstUnFinishedNodes* node, CompiledAddr addr);
void                fstUnFinishedNodesAddSuffix(FstUnFinishedNodes* node, FstSlice bs, Output out);
uint64_t            fstUnFinishedNodesFindCommPrefix(FstUnFinishedNodes* node, FstSlice bs);
FstBuilderNode*     fstUnFinishedNodesPopRoot(FstUnFinishedNodes* nodes);
FstBuilderNode*     fstUnFinishedNodesPopFreeze(FstUnFinishedNodes* nodes, CompiledAddr addr);
FstBuilderNode*     fstUnFinishedNodesPopEmpty(FstUnFinishedNodes* nodes);
89

dengyihao's avatar
dengyihao 已提交
90
uint64_t fstUnFinishedNodesFindCommPrefixAndSetOutput(FstUnFinishedNodes* node, FstSlice bs, Output in, Output* out);
dengyihao's avatar
dengyihao 已提交
91 92

typedef struct FstBuilder {
dengyihao's avatar
dengyihao 已提交
93 94 95
  FstCountingWriter*  wrt;         // The FST raw data is written directly to `wtr`.
  FstUnFinishedNodes* unfinished;  // The stack of unfinished nodes
  FstRegistry*        registry;    // A map of finished nodes.
96 97 98
  FstSlice            last;        // The last word added
  CompiledAddr        lastAddr;    // The address of the last compiled node
  uint64_t            len;         // num of keys added
dengyihao's avatar
dengyihao 已提交
99
} FstBuilder;
dengyihao's avatar
dengyihao 已提交
100

dengyihao's avatar
dengyihao 已提交
101
FstBuilder* fstBuilderCreate(void* w, FstType ty);
dengyihao's avatar
dengyihao 已提交
102

dengyihao's avatar
dengyihao 已提交
103 104 105 106 107 108 109 110
void         fstBuilderDestroy(FstBuilder* b);
void         fstBuilderInsertOutput(FstBuilder* b, FstSlice bs, Output in);
bool         fstBuilderInsert(FstBuilder* b, FstSlice bs, Output in);
void         fstBuilderCompileFrom(FstBuilder* b, uint64_t istate);
void*        fstBuilerIntoInner(FstBuilder* b);
void         fstBuilderFinish(FstBuilder* b);
OrderType    fstBuilderCheckLastKey(FstBuilder* b, FstSlice bs, bool ckDup);
CompiledAddr fstBuilderCompile(FstBuilder* b, FstBuilderNode* bn);
dengyihao's avatar
dengyihao 已提交
111 112

typedef struct FstTransitions {
dengyihao's avatar
dengyihao 已提交
113
  FstNode* node;
114
  FstRange range;
dengyihao's avatar
dengyihao 已提交
115 116
} FstTransitions;

117
// FstState and relation function
dengyihao's avatar
dengyihao 已提交
118 119

typedef struct FstState {
120
  State   state;
dengyihao's avatar
dengyihao 已提交
121 122 123
  uint8_t val;
} FstState;

dengyihao's avatar
dengyihao 已提交
124
FstState fstStateCreateFrom(FstSlice* data, CompiledAddr addr);
dengyihao's avatar
dengyihao 已提交
125 126
FstState fstStateCreate(State state);

127
// compile
dengyihao's avatar
dengyihao 已提交
128 129 130
void fstStateCompileForOneTransNext(FstCountingWriter* w, CompiledAddr addr, uint8_t inp);
void fstStateCompileForOneTrans(FstCountingWriter* w, CompiledAddr addr, FstTransition* trn);
void fstStateCompileForAnyTrans(FstCountingWriter* w, CompiledAddr addr, FstBuilderNode* node);
dengyihao's avatar
dengyihao 已提交
131 132

// set_comm_input
dengyihao's avatar
dengyihao 已提交
133
void fstStateSetCommInput(FstState* state, uint8_t inp);
dengyihao's avatar
dengyihao 已提交
134 135

// comm_input
dengyihao's avatar
dengyihao 已提交
136
uint8_t fstStateCommInput(FstState* state, bool* null);
dengyihao's avatar
dengyihao 已提交
137 138 139

// input_len

dengyihao's avatar
dengyihao 已提交
140
uint64_t fstStateInputLen(FstState* state);
dengyihao's avatar
dengyihao 已提交
141

142
// end_addr
dengyihao's avatar
dengyihao 已提交
143 144 145
uint64_t fstStateEndAddrForOneTransNext(FstState* state, FstSlice* data);
uint64_t fstStateEndAddrForOneTrans(FstState* state, FstSlice* data, PackSizes sizes);
uint64_t fstStateEndAddrForAnyTrans(FstState* state, uint64_t version, FstSlice* date, PackSizes sizes, uint64_t nTrans);
146
// input
dengyihao's avatar
dengyihao 已提交
147 148
uint8_t fstStateInput(FstState* state, FstNode* node);
uint8_t fstStateInputForAnyTrans(FstState* state, FstNode* node, uint64_t i);
dengyihao's avatar
dengyihao 已提交
149 150

// trans_addr
dengyihao's avatar
dengyihao 已提交
151 152
CompiledAddr fstStateTransAddr(FstState* state, FstNode* node);
CompiledAddr fstStateTransAddrForAnyTrans(FstState* state, FstNode* node, uint64_t i);
dengyihao's avatar
dengyihao 已提交
153

154
// sizes
dengyihao's avatar
dengyihao 已提交
155
PackSizes fstStateSizes(FstState* state, FstSlice* data);
156
// Output
dengyihao's avatar
dengyihao 已提交
157 158
Output fstStateOutput(FstState* state, FstNode* node);
Output fstStateOutputForAnyTrans(FstState* state, FstNode* node, uint64_t i);
dengyihao's avatar
dengyihao 已提交
159 160 161

// anyTrans specify function

dengyihao's avatar
dengyihao 已提交
162 163 164
void fstStateSetFinalState(FstState* state, bool yes);
bool fstStateIsFinalState(FstState* state);
void fstStateSetStateNtrans(FstState* state, uint8_t n);
dengyihao's avatar
dengyihao 已提交
165
// state_ntrans
dengyihao's avatar
dengyihao 已提交
166 167 168 169 170 171 172
uint8_t  fstStateStateNtrans(FstState* state, bool* null);
uint64_t fstStateTotalTransSize(FstState* state, uint64_t version, PackSizes size, uint64_t nTrans);
uint64_t fstStateTransIndexSize(FstState* state, uint64_t version, uint64_t nTrans);
uint64_t fstStateNtransLen(FstState* state);
uint64_t fstStateNtrans(FstState* state, FstSlice* slice);
Output   fstStateFinalOutput(FstState* state, uint64_t version, FstSlice* date, PackSizes sizes, uint64_t nTrans);
uint64_t fstStateFindInput(FstState* state, FstNode* node, uint8_t b, bool* null);
dengyihao's avatar
dengyihao 已提交
173

174
#define FST_STATE_ONE_TRNAS_NEXT(node) (node->state.state == OneTransNext)
dengyihao's avatar
dengyihao 已提交
175 176
#define FST_STATE_ONE_TRNAS(node) (node->state.state == OneTrans)
#define FST_STATE_ANY_TRANS(node) (node->state.state == AnyTrans)
177
#define FST_STATE_EMPTY_FINAL(node) (node->state.state == EmptyFinal)
dengyihao's avatar
dengyihao 已提交
178 179 180 181 182 183

typedef struct FstLastTransition {
  uint8_t inp;
  Output  out;
} FstLastTransition;

184
/*
dengyihao's avatar
dengyihao 已提交
185
 * FstBuilderNodeUnfinished and helper function
186
 * TODO: simple function name
dengyihao's avatar
dengyihao 已提交
187
 */
dengyihao's avatar
dengyihao 已提交
188
typedef struct FstBuilderNodeUnfinished {
dengyihao's avatar
dengyihao 已提交
189 190
  FstBuilderNode*    node;
  FstLastTransition* last;
dengyihao's avatar
dengyihao 已提交
191 192
} FstBuilderNodeUnfinished;

dengyihao's avatar
dengyihao 已提交
193
void fstBuilderNodeUnfinishedLastCompiled(FstBuilderNodeUnfinished* node, CompiledAddr addr);
194

dengyihao's avatar
dengyihao 已提交
195
void fstBuilderNodeUnfinishedAddOutputPrefix(FstBuilderNodeUnfinished* node, Output out);
dengyihao's avatar
dengyihao 已提交
196 197

/*
198
 * FstNode and helper function
dengyihao's avatar
dengyihao 已提交
199
 */
dengyihao's avatar
dengyihao 已提交
200
typedef struct FstNode {
dengyihao's avatar
dengyihao 已提交
201
  FstSlice     data;
202
  uint64_t     version;
dengyihao's avatar
dengyihao 已提交
203
  FstState     state;
204 205
  CompiledAddr start;
  CompiledAddr end;
dengyihao's avatar
dengyihao 已提交
206 207 208
  bool         isFinal;
  uint64_t     nTrans;
  PackSizes    sizes;
209
  Output       finalOutput;
dengyihao's avatar
dengyihao 已提交
210 211
} FstNode;

212 213
// If this node is final and has a terminal output value, then it is,  returned.
// Otherwise, a zero output is returned
dengyihao's avatar
dengyihao 已提交
214
#define FST_NODE_FINAL_OUTPUT(node) node->finalOutput
215 216
// Returns true if and only if this node corresponds to a final or "match",
// state in the finite state transducer.
dengyihao's avatar
dengyihao 已提交
217
#define FST_NODE_IS_FINAL(node) node->isFinal
218 219
// Returns the number of transitions in this node, The maximum number of
// transitions is 256.
dengyihao's avatar
dengyihao 已提交
220 221 222 223
#define FST_NODE_LEN(node) node->nTrans
// Returns true if and only if this node has zero transitions.
#define FST_NODE_IS_EMPTYE(node) (node->nTrans == 0)
// Return the address of this node.
224
#define FST_NODE_ADDR(node) node->start
dengyihao's avatar
dengyihao 已提交
225

dengyihao's avatar
dengyihao 已提交
226 227
FstNode* fstNodeCreate(int64_t version, CompiledAddr addr, FstSlice* data);
void     fstNodeDestroy(FstNode* fstNode);
dengyihao's avatar
dengyihao 已提交
228

dengyihao's avatar
dengyihao 已提交
229 230 231 232 233
FstTransitions  fstNodeTransitionIter(FstNode* node);
FstTransitions* fstNodeTransitions(FstNode* node);
bool            fstNodeGetTransitionAt(FstNode* node, uint64_t i, FstTransition* res);
bool            fstNodeGetTransitionAddrAt(FstNode* node, uint64_t i, CompiledAddr* res);
bool            fstNodeFindInput(FstNode* node, uint8_t b, uint64_t* res);
dengyihao's avatar
dengyihao 已提交
234

dengyihao's avatar
dengyihao 已提交
235
bool fstNodeCompile(FstNode* node, void* w, CompiledAddr lastAddr, CompiledAddr addr, FstBuilderNode* builderNode);
236

dengyihao's avatar
dengyihao 已提交
237
FstSlice fstNodeAsSlice(FstNode* node);
238 239

// ops
dengyihao's avatar
dengyihao 已提交
240 241 242 243 244 245

typedef struct FstIndexedValue {
  uint64_t index;
  uint64_t value;
} FstIndexedValue;

dengyihao's avatar
dengyihao 已提交
246 247
FstLastTransition* fstLastTransitionCreate(uint8_t inp, Output out);
void               fstLastTransitionDestroy(FstLastTransition* trn);
dengyihao's avatar
dengyihao 已提交
248

dengyihao's avatar
dengyihao 已提交
249 250
typedef struct FstMeta {
  uint64_t     version;
251
  CompiledAddr rootAddr;
dengyihao's avatar
dengyihao 已提交
252 253 254 255 256 257
  FstType      ty;
  uint64_t     len;
  uint32_t     checkSum;
} FstMeta;

typedef struct Fst {
dengyihao's avatar
dengyihao 已提交
258 259 260
  FstMeta*  meta;
  FstSlice* data;  //
  FstNode*  root;  //
dengyihao's avatar
dengyihao 已提交
261
} Fst;
dengyihao's avatar
dengyihao 已提交
262

263
// refactor simple function
dengyihao's avatar
dengyihao 已提交
264

dengyihao's avatar
dengyihao 已提交
265 266
Fst* fstCreate(FstSlice* data);
void fstDestroy(Fst* fst);
dengyihao's avatar
dengyihao 已提交
267

dengyihao's avatar
dengyihao 已提交
268 269 270 271 272 273 274
bool              fstGet(Fst* fst, FstSlice* b, Output* out);
FstNode*          fstGetNode(Fst* fst, CompiledAddr);
FstNode*          fstGetRoot(Fst* fst);
FstType           fstGetType(Fst* fst);
CompiledAddr      fstGetRootAddr(Fst* fst);
Output            fstEmptyFinalOutput(Fst* fst, bool* null);
FstStreamBuilder* fstSearch(Fst* fst, AutomationCtx* ctx);
dengyihao's avatar
dengyihao 已提交
275

dengyihao's avatar
dengyihao 已提交
276
FstStreamWithStateBuilder* fstSearchWithState(Fst* fst, AutomationCtx* ctx);
277
// into stream to expand later
dengyihao's avatar
dengyihao 已提交
278
StreamWithState* streamBuilderIntoStream(FstStreamBuilder* sb);
dengyihao's avatar
dengyihao 已提交
279

dengyihao's avatar
dengyihao 已提交
280
bool fstVerify(Fst* fst);
dengyihao's avatar
dengyihao 已提交
281

282
// refactor this function
dengyihao's avatar
dengyihao 已提交
283
bool fstBuilderNodeCompileTo(FstBuilderNode* b, FstCountingWriter* wrt, CompiledAddr lastAddr, CompiledAddr startAddr);
dengyihao's avatar
dengyihao 已提交
284 285

typedef struct StreamState {
dengyihao's avatar
dengyihao 已提交
286
  FstNode*  node;
dengyihao's avatar
dengyihao 已提交
287
  uint64_t  trans;
288
  FstOutput out;
dengyihao's avatar
dengyihao 已提交
289
  void*     autState;
290
} StreamState;
dengyihao's avatar
dengyihao 已提交
291

dengyihao's avatar
dengyihao 已提交
292
void streamStateDestroy(void* s);
dengyihao's avatar
dengyihao 已提交
293

dengyihao's avatar
dengyihao 已提交
294
typedef struct StreamWithState {
dengyihao's avatar
dengyihao 已提交
295 296 297
  Fst*              fst;
  AutomationCtx*    aut;
  SArray*           inp;
298
  FstOutput         emptyOutput;
dengyihao's avatar
dengyihao 已提交
299 300
  SArray*           stack;  // <StreamState>
  FstBoundWithData* endAt;
dengyihao's avatar
dengyihao 已提交
301
} StreamWithState;
dengyihao's avatar
dengyihao 已提交
302

dengyihao's avatar
dengyihao 已提交
303
typedef struct StreamWithStateResult {
304
  FstSlice  data;
dengyihao's avatar
dengyihao 已提交
305
  FstOutput out;
dengyihao's avatar
dengyihao 已提交
306
  void*     state;
dengyihao's avatar
dengyihao 已提交
307 308
} StreamWithStateResult;

dengyihao's avatar
dengyihao 已提交
309 310
StreamWithStateResult* swsResultCreate(FstSlice* data, FstOutput fOut, void* state);
void                   swsResultDestroy(StreamWithStateResult* result);
311

dengyihao's avatar
dengyihao 已提交
312 313
typedef void* (*StreamCallback)(void*);
StreamWithState* streamWithStateCreate(Fst* fst, AutomationCtx* automation, FstBoundWithData* min, FstBoundWithData* max);
dengyihao's avatar
dengyihao 已提交
314

dengyihao's avatar
dengyihao 已提交
315
void streamWithStateDestroy(StreamWithState* sws);
dengyihao's avatar
dengyihao 已提交
316

dengyihao's avatar
dengyihao 已提交
317
bool streamWithStateSeekMin(StreamWithState* sws, FstBoundWithData* min);
dengyihao's avatar
dengyihao 已提交
318

dengyihao's avatar
dengyihao 已提交
319
StreamWithStateResult* streamWithStateNextWith(StreamWithState* sws, StreamCallback callback);
320

dengyihao's avatar
dengyihao 已提交
321
FstStreamBuilder* fstStreamBuilderCreate(Fst* fst, AutomationCtx* aut);
dengyihao's avatar
dengyihao 已提交
322
// set up bound range
323
// refator, simple code by marco
dengyihao's avatar
dengyihao 已提交
324

dengyihao's avatar
dengyihao 已提交
325
FstStreamBuilder* fstStreamBuilderRange(FstStreamBuilder* b, FstSlice* val, RangeType type);
dengyihao's avatar
dengyihao 已提交
326

dengyihao's avatar
dengyihao 已提交
327 328 329 330
#ifdef __cplusplus
}
#endif

dengyihao's avatar
dengyihao 已提交
331
#endif