indexFst.h 11.4 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 24 25 26 27
#include "indexFstAutomation.h"
#include "indexFstCountingWriter.h"
#include "indexFstNode.h"
#include "indexFstRegistry.h"
#include "indexFstUtil.h"
S
Shengliang Guan 已提交
28
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
29

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

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

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

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

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

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

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
80 81 82 83 84 85 86 87 88 89
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);
90

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

typedef struct FstBuilder {
dengyihao's avatar
dengyihao 已提交
94 95 96
  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.
97 98 99
  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 已提交
100
} FstBuilder;
dengyihao's avatar
dengyihao 已提交
101

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

dengyihao's avatar
dengyihao 已提交
104 105 106 107 108 109 110 111
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 已提交
112 113

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

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

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

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

128
// compile
dengyihao's avatar
dengyihao 已提交
129 130 131
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 已提交
132 133

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

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

// input_len

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

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

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

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

// anyTrans specify function

dengyihao's avatar
dengyihao 已提交
164 165 166
void fstStateSetFinalState(FstState* state, bool yes);
bool fstStateIsFinalState(FstState* state);
void fstStateSetStateNtrans(FstState* state, uint8_t n);
dengyihao's avatar
dengyihao 已提交
167
// state_ntrans
dengyihao's avatar
dengyihao 已提交
168 169 170 171 172 173 174
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 已提交
175

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

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

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

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

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

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

214 215
// 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 已提交
216
#define FST_NODE_FINAL_OUTPUT(node) node->finalOutput
217 218
// Returns true if and only if this node corresponds to a final or "match",
// state in the finite state transducer.
dengyihao's avatar
dengyihao 已提交
219
#define FST_NODE_IS_FINAL(node) node->isFinal
220 221
// Returns the number of transitions in this node, The maximum number of
// transitions is 256.
dengyihao's avatar
dengyihao 已提交
222 223 224 225
#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.
226
#define FST_NODE_ADDR(node) node->start
dengyihao's avatar
dengyihao 已提交
227

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

dengyihao's avatar
dengyihao 已提交
231 232 233 234 235
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 已提交
236

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

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

// ops
dengyihao's avatar
dengyihao 已提交
242 243 244 245 246 247

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

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

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

typedef struct Fst {
dengyihao's avatar
dengyihao 已提交
260 261 262
  FstMeta*      meta;
  FstSlice*     data;  //
  FstNode*      root;  //
wafwerar's avatar
wafwerar 已提交
263
  TdThreadMutex mtx;
dengyihao's avatar
dengyihao 已提交
264
} Fst;
dengyihao's avatar
dengyihao 已提交
265

266
// refactor simple function
dengyihao's avatar
dengyihao 已提交
267

dengyihao's avatar
dengyihao 已提交
268 269
Fst* fstCreate(FstSlice* data);
void fstDestroy(Fst* fst);
dengyihao's avatar
dengyihao 已提交
270

dengyihao's avatar
dengyihao 已提交
271 272 273 274 275 276 277
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 已提交
278

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

dengyihao's avatar
dengyihao 已提交
283
bool fstVerify(Fst* fst);
dengyihao's avatar
dengyihao 已提交
284

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

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

dengyihao's avatar
dengyihao 已提交
295
void streamStateDestroy(void* s);
dengyihao's avatar
dengyihao 已提交
296

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

dengyihao's avatar
dengyihao 已提交
306
typedef struct StreamWithStateResult {
307
  FstSlice  data;
dengyihao's avatar
dengyihao 已提交
308
  FstOutput out;
dengyihao's avatar
dengyihao 已提交
309
  void*     state;
dengyihao's avatar
dengyihao 已提交
310 311
} StreamWithStateResult;

dengyihao's avatar
dengyihao 已提交
312 313
StreamWithStateResult* swsResultCreate(FstSlice* data, FstOutput fOut, void* state);
void                   swsResultDestroy(StreamWithStateResult* result);
314

dengyihao's avatar
dengyihao 已提交
315
typedef void* (*StreamCallback)(void*);
dengyihao's avatar
dengyihao 已提交
316 317
StreamWithState* streamWithStateCreate(Fst* fst, AutomationCtx* automation, FstBoundWithData* min,
                                       FstBoundWithData* max);
dengyihao's avatar
dengyihao 已提交
318

dengyihao's avatar
dengyihao 已提交
319
void streamWithStateDestroy(StreamWithState* sws);
dengyihao's avatar
dengyihao 已提交
320

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

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

dengyihao's avatar
dengyihao 已提交
325
FstStreamBuilder* fstStreamBuilderCreate(Fst* fst, AutomationCtx* aut);
dengyihao's avatar
dengyihao 已提交
326 327

void fstStreamBuilderDestroy(FstStreamBuilder* b);
dengyihao's avatar
dengyihao 已提交
328

dengyihao's avatar
dengyihao 已提交
329 330 331
// set up bound range
// refator later:  to simple code by marco
void fstStreamBuilderSetRange(FstStreamBuilder* b, FstSlice* val, RangeType type);
dengyihao's avatar
dengyihao 已提交
332

dengyihao's avatar
dengyihao 已提交
333 334 335 336
#ifdef __cplusplus
}
#endif

dengyihao's avatar
dengyihao 已提交
337
#endif