index_fst.h 11.0 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
#include "tarray.h"
#include "index_fst_util.h"
#include "index_fst_registry.h"
dengyihao's avatar
dengyihao 已提交
26
#include "index_fst_counting_writer.h"
dengyihao's avatar
dengyihao 已提交
27
#include "index_fst_automation.h"
dengyihao's avatar
dengyihao 已提交
28

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

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

dengyihao's avatar
dengyihao 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
typedef enum { Included, Excluded, Unbounded} FstBound; 

typedef struct FstBoundWithData {
  FstSlice data; 
  FstBound type;
} FstBoundWithData;

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

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


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

dengyihao's avatar
dengyihao 已提交
59

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


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

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

#define FST_UNFINISHED_NODES_LEN(nodes) taosArrayGetSize(nodes->stack) 

dengyihao's avatar
dengyihao 已提交
82
FstUnFinishedNodes *fstUnFinishedNodesCreate();  
dengyihao's avatar
dengyihao 已提交
83
void fstUnFinishedNodesDestroy(FstUnFinishedNodes *node);
dengyihao's avatar
dengyihao 已提交
84 85 86 87 88 89 90 91
void fstUnFinishedNodesPushEmpty(FstUnFinishedNodes *nodes, bool isFinal);
FstBuilderNode *fstUnFinishedNodesPopRoot(FstUnFinishedNodes *nodes);
FstBuilderNode *fstUnFinishedNodesPopFreeze(FstUnFinishedNodes *nodes, CompiledAddr addr);
FstBuilderNode *fstUnFinishedNodesPopEmpty(FstUnFinishedNodes *nodes);
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);
dengyihao's avatar
dengyihao 已提交
92
uint64_t fstUnFinishedNodesFindCommPrefixAndSetOutput(FstUnFinishedNodes *node, FstSlice bs, Output in, Output *out);
dengyihao's avatar
dengyihao 已提交
93

dengyihao's avatar
dengyihao 已提交
94 95

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

dengyihao's avatar
dengyihao 已提交
104

dengyihao's avatar
dengyihao 已提交
105
FstBuilder *fstBuilderCreate(void *w, FstType ty);
dengyihao's avatar
dengyihao 已提交
106 107


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

dengyihao's avatar
dengyihao 已提交
117 118


dengyihao's avatar
dengyihao 已提交
119 120 121 122 123 124

typedef struct FstTransitions {
  FstNode    *node; 
  FstRange   range;   
} FstTransitions;

dengyihao's avatar
dengyihao 已提交
125 126 127 128 129 130 131
//FstState and relation function

typedef struct FstState {
  State state;
  uint8_t val;
} FstState;

dengyihao's avatar
dengyihao 已提交
132 133 134 135
FstState fstStateCreateFrom(FstSlice* data, CompiledAddr addr);
FstState fstStateCreate(State state);

//compile
dengyihao's avatar
dengyihao 已提交
136
void fstStateCompileForOneTransNext(FstCountingWriter *w, CompiledAddr addr, uint8_t inp);
dengyihao's avatar
dengyihao 已提交
137
void fstStateCompileForOneTrans(FstCountingWriter *w, CompiledAddr addr, FstTransition *trn);
dengyihao's avatar
dengyihao 已提交
138
void fstStateCompileForAnyTrans(FstCountingWriter *w, CompiledAddr addr, FstBuilderNode *node);
dengyihao's avatar
dengyihao 已提交
139 140

// set_comm_input
dengyihao's avatar
dengyihao 已提交
141
void fstStateSetCommInput(FstState* state, uint8_t inp);
dengyihao's avatar
dengyihao 已提交
142 143

// comm_input
dengyihao's avatar
dengyihao 已提交
144
uint8_t fstStateCommInput(FstState* state, bool *null);
dengyihao's avatar
dengyihao 已提交
145 146 147

// input_len

dengyihao's avatar
dengyihao 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
uint64_t fstStateInputLen(FstState* state);


// end_addr 
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);
// input  
uint8_t  fstStateInput(FstState *state, FstNode *node);
uint8_t  fstStateInputForAnyTrans(FstState *state, FstNode *node, uint64_t i);

// trans_addr
CompiledAddr fstStateTransAddr(FstState *state, FstNode *node);
CompiledAddr fstStateTransAddrForAnyTrans(FstState *state, FstNode *node, uint64_t i);

// sizes 
PackSizes fstStateSizes(FstState *state, FstSlice *data);
// Output 
Output fstStateOutput(FstState *state, FstNode *node);
Output fstStateOutputForAnyTrans(FstState *state, FstNode *node, uint64_t i);

// anyTrans specify function

void fstStateSetFinalState(FstState *state, bool yes);
bool fstStateIsFinalState(FstState *state); 
void fstStateSetStateNtrans(FstState *state, uint8_t n);
// state_ntrans
dengyihao's avatar
dengyihao 已提交
175
uint8_t fstStateStateNtrans(FstState *state, bool *null);
dengyihao's avatar
dengyihao 已提交
176 177 178
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);
dengyihao's avatar
dengyihao 已提交
179
uint64_t fstStateNtrans(FstState *state, FstSlice *slice);
dengyihao's avatar
dengyihao 已提交
180
Output   fstStateFinalOutput(FstState *state, uint64_t version, FstSlice *date, PackSizes sizes, uint64_t nTrans);
dengyihao's avatar
dengyihao 已提交
181
uint64_t fstStateFindInput(FstState *state, FstNode *node, uint8_t b, bool *null);
dengyihao's avatar
dengyihao 已提交
182 183


dengyihao's avatar
dengyihao 已提交
184

dengyihao's avatar
dengyihao 已提交
185 186 187 188
#define FST_STATE_ONE_TRNAS_NEXT(node) (node->state.state == OneTransNext) 
#define FST_STATE_ONE_TRNAS(node) (node->state.state == OneTrans)
#define FST_STATE_ANY_TRANS(node) (node->state.state == AnyTrans)
#define FST_STATE_EMPTY_FINAL(node) (node->state.state == EmptyFinal) 
dengyihao's avatar
dengyihao 已提交
189 190 191 192 193 194 195


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

dengyihao's avatar
dengyihao 已提交
196 197 198 199
/* 
 * FstBuilderNodeUnfinished and helper function
 * TODO: simple function name 
 */
dengyihao's avatar
dengyihao 已提交
200
typedef struct FstBuilderNodeUnfinished {
dengyihao's avatar
dengyihao 已提交
201 202
  FstBuilderNode *node; 
  FstLastTransition* last; 
dengyihao's avatar
dengyihao 已提交
203 204
} FstBuilderNodeUnfinished;

dengyihao's avatar
dengyihao 已提交
205 206


dengyihao's avatar
dengyihao 已提交
207
void fstBuilderNodeUnfinishedLastCompiled(FstBuilderNodeUnfinished *node, CompiledAddr addr);
dengyihao's avatar
dengyihao 已提交
208
void fstBuilderNodeUnfinishedAddOutputPrefix(FstBuilderNodeUnfinished *node, Output out);
dengyihao's avatar
dengyihao 已提交
209 210 211 212

/*
 * FstNode and helper function  
 */
dengyihao's avatar
dengyihao 已提交
213
typedef struct FstNode {
dengyihao's avatar
dengyihao 已提交
214
  FstSlice     data;
dengyihao's avatar
dengyihao 已提交
215
  uint64_t     version; 
dengyihao's avatar
dengyihao 已提交
216
  FstState     state;
dengyihao's avatar
dengyihao 已提交
217 218 219 220 221 222 223 224
  CompiledAddr start; 
  CompiledAddr end;  
  bool         isFinal;
  uint64_t     nTrans;
  PackSizes    sizes;
  Output      finalOutput;
} FstNode;

dengyihao's avatar
dengyihao 已提交
225 226 227 228 229 230 231 232 233 234 235
// If this node is final and has a terminal output value, then it is,  returned. Otherwise, a zero output is returned 
#define FST_NODE_FINAL_OUTPUT(node) node->finalOutput
// Returns true if and only if this node corresponds to a final or "match", state in the finite state transducer.
#define FST_NODE_IS_FINAL(node) node->isFinal
// Returns the number of transitions in this node, The maximum number of transitions is 256.
#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.
#define FST_NODE_ADDR(node) node->start 

dengyihao's avatar
dengyihao 已提交
236

dengyihao's avatar
dengyihao 已提交
237
FstNode *fstNodeCreate(int64_t version, CompiledAddr addr, FstSlice *data);
dengyihao's avatar
dengyihao 已提交
238 239
void fstNodeDestroy(FstNode *fstNode);

dengyihao's avatar
dengyihao 已提交
240 241 242 243 244 245 246 247
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);
bool fstNodeCompile(FstNode *node, void *w, CompiledAddr lastAddr, CompiledAddr addr, FstBuilderNode *builderNode); 
FstSlice  fstNodeAsSlice(FstNode *node); 

dengyihao's avatar
dengyihao 已提交
248 249 250 251 252 253 254 255 256
// ops  

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

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


dengyihao's avatar
dengyihao 已提交
259 260 261 262 263 264 265 266 267
typedef struct FstMeta {
  uint64_t     version;
  CompiledAddr rootAddr;  
  FstType      ty;
  uint64_t     len;
  uint32_t     checkSum;
} FstMeta;

typedef struct Fst {
dengyihao's avatar
dengyihao 已提交
268 269 270
  FstMeta  *meta; 
  FstSlice *data;  //  
  FstNode  *root;  // 
dengyihao's avatar
dengyihao 已提交
271
} Fst;
dengyihao's avatar
dengyihao 已提交
272

dengyihao's avatar
dengyihao 已提交
273
// refactor simple function 
dengyihao's avatar
dengyihao 已提交
274

dengyihao's avatar
dengyihao 已提交
275 276
Fst*         fstCreate(FstSlice *data);
void         fstDestroy(Fst *fst);
dengyihao's avatar
dengyihao 已提交
277

dengyihao's avatar
dengyihao 已提交
278 279
bool         fstGet(Fst *fst, FstSlice *b, Output *out);
FstNode*     fstGetNode(Fst *fst, CompiledAddr); 
dengyihao's avatar
dengyihao 已提交
280
FstNode*     fstGetRoot(Fst *fst);
dengyihao's avatar
dengyihao 已提交
281 282
FstType      fstGetType(Fst *fst); 
CompiledAddr fstGetRootAddr(Fst *fst);
dengyihao's avatar
dengyihao 已提交
283
Output       fstEmptyFinalOutput(Fst *fst, bool *null);
dengyihao's avatar
dengyihao 已提交
284 285
FstStreamBuilder *fstSearch(Fst *fst, AutomationCtx *ctx);
FstStreamWithStateBuilder *fstSearchWithState(Fst *fst, AutomationCtx *ctx);
dengyihao's avatar
dengyihao 已提交
286

dengyihao's avatar
dengyihao 已提交
287 288 289
// into stream to expand later 
StreamWithState* streamBuilderIntoStream(FstStreamBuilder *sb);

dengyihao's avatar
dengyihao 已提交
290
bool         fstVerify(Fst *fst);
dengyihao's avatar
dengyihao 已提交
291 292 293 294


//refactor this function 
bool fstBuilderNodeCompileTo(FstBuilderNode *b, FstCountingWriter *wrt, CompiledAddr lastAddr, CompiledAddr startAddr); 
dengyihao's avatar
dengyihao 已提交
295 296 297 298 299 300 301 302

typedef struct StreamState {
  FstNode   *node; 
  uint64_t  trans;
  FstOutput out; 
  void     *autState; 
} StreamState; 

dengyihao's avatar
dengyihao 已提交
303 304
void streamStateDestroy(void *s);

dengyihao's avatar
dengyihao 已提交
305
typedef struct StreamWithState {
dengyihao's avatar
dengyihao 已提交
306 307 308 309 310
  Fst           *fst;
  AutomationCtx *aut; 
  SArray        *inp;
  FstOutput     emptyOutput;
  SArray        *stack; // <StreamState>
dengyihao's avatar
dengyihao 已提交
311
  FstBoundWithData *endAt;
dengyihao's avatar
dengyihao 已提交
312
} StreamWithState;
dengyihao's avatar
dengyihao 已提交
313

dengyihao's avatar
dengyihao 已提交
314 315 316 317 318 319 320
typedef struct StreamWithStateResult {
  FstSlice data;  
  FstOutput out;
  void     *state; 
} StreamWithStateResult;

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

dengyihao's avatar
dengyihao 已提交
323
typedef void* (*StreamCallback)(void *);
dengyihao's avatar
dengyihao 已提交
324
StreamWithState *streamWithStateCreate(Fst *fst, AutomationCtx *automation, FstBoundWithData *min, FstBoundWithData *max) ;
dengyihao's avatar
dengyihao 已提交
325 326
void streamWithStateDestroy(StreamWithState *sws);
bool streamWithStateSeekMin(StreamWithState *sws, FstBoundWithData *min);           
dengyihao's avatar
dengyihao 已提交
327

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

dengyihao's avatar
dengyihao 已提交
330
FstStreamBuilder *fstStreamBuilderCreate(Fst *fst, AutomationCtx *aut); 
dengyihao's avatar
dengyihao 已提交
331 332 333 334 335
// set up bound range
// refator, simple code by marco 

FstStreamBuilder *fstStreamBuilderRange(FstStreamBuilder *b, FstSlice *val, RangeType type);

dengyihao's avatar
dengyihao 已提交
336 337 338 339 340

#ifdef __cplusplus
}
#endif

dengyihao's avatar
dengyihao 已提交
341
#endif