index_fst_automation.c 5.6 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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 已提交
15

dengyihao's avatar
dengyihao 已提交
16 17
#include "index_fst_automation.h"

dengyihao's avatar
dengyihao 已提交
18
StartWithStateValue* startWithStateValueCreate(StartWithStateKind kind, ValueType ty, void* val) {
wafwerar's avatar
wafwerar 已提交
19
  StartWithStateValue* sv = taosMemoryCalloc(1, sizeof(StartWithStateValue));
dengyihao's avatar
dengyihao 已提交
20
  if (sv == NULL) {
dengyihao's avatar
dengyihao 已提交
21 22
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
23

dengyihao's avatar
dengyihao 已提交
24 25
  sv->kind = kind;
  sv->type = ty;
dengyihao's avatar
dengyihao 已提交
26
  if (ty == FST_INT) {
dengyihao's avatar
dengyihao 已提交
27
    sv->val = *(int*)val;
dengyihao's avatar
dengyihao 已提交
28
  } else if (ty == FST_CHAR) {
dengyihao's avatar
dengyihao 已提交
29
    size_t len = strlen((char*)val);
wafwerar's avatar
wafwerar 已提交
30
    sv->ptr = (char*)taosMemoryCalloc(1, len + 1);
dengyihao's avatar
dengyihao 已提交
31
    memcpy(sv->ptr, val, len);
dengyihao's avatar
dengyihao 已提交
32
  } else if (ty == FST_ARRAY) {
33 34
    // TODO,
    // nsv->arr = taosArrayFromList()
dengyihao's avatar
dengyihao 已提交
35
  }
dengyihao's avatar
dengyihao 已提交
36
  return sv;
dengyihao's avatar
dengyihao 已提交
37
}
dengyihao's avatar
dengyihao 已提交
38 39
void startWithStateValueDestroy(void* val) {
  StartWithStateValue* sv = (StartWithStateValue*)val;
dengyihao's avatar
dengyihao 已提交
40 41 42
  if (sv == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
43 44

  if (sv->type == FST_INT) {
45
    //
dengyihao's avatar
dengyihao 已提交
46
  } else if (sv->type == FST_CHAR) {
wafwerar's avatar
wafwerar 已提交
47
    taosMemoryFree(sv->ptr);
dengyihao's avatar
dengyihao 已提交
48 49 50
  } else if (sv->type == FST_ARRAY) {
    taosArrayDestroy(sv->arr);
  }
wafwerar's avatar
wafwerar 已提交
51
  taosMemoryFree(sv);
dengyihao's avatar
dengyihao 已提交
52
}
dengyihao's avatar
dengyihao 已提交
53
StartWithStateValue* startWithStateValueDump(StartWithStateValue* sv) {
wafwerar's avatar
wafwerar 已提交
54
  StartWithStateValue* nsv = taosMemoryCalloc(1, sizeof(StartWithStateValue));
dengyihao's avatar
dengyihao 已提交
55 56 57
  if (nsv == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
58 59

  nsv->kind = sv->kind;
60
  nsv->type = sv->type;
dengyihao's avatar
dengyihao 已提交
61 62 63 64
  if (nsv->type == FST_INT) {
    nsv->val = sv->val;
  } else if (nsv->type == FST_CHAR) {
    size_t len = strlen(sv->ptr);
wafwerar's avatar
wafwerar 已提交
65
    nsv->ptr = (char*)taosMemoryCalloc(1, len + 1);
dengyihao's avatar
dengyihao 已提交
66 67
    memcpy(nsv->ptr, sv->ptr, len);
  } else if (nsv->type == FST_ARRAY) {
dengyihao's avatar
dengyihao 已提交
68
    //
dengyihao's avatar
dengyihao 已提交
69 70 71 72
  }
  return nsv;
}

dengyihao's avatar
dengyihao 已提交
73
// iterate fst
dengyihao's avatar
dengyihao 已提交
74 75 76 77 78 79
static void* alwaysMatchStart(AutomationCtx* ctx) { return NULL; }
static bool  alwaysMatchIsMatch(AutomationCtx* ctx, void* state) { return true; }
static bool  alwaysMatchCanMatch(AutomationCtx* ctx, void* state) { return true; }
static bool  alwaysMatchWillAlwaysMatch(AutomationCtx* ctx, void* state) { return true; }
static void* alwaysMatchAccpet(AutomationCtx* ctx, void* state, uint8_t byte) { return NULL; }
static void* alwaysMatchAccpetEof(AutomationCtx* ctx, void* state) { return NULL; }
dengyihao's avatar
dengyihao 已提交
80
// prefix query, impl later
dengyihao's avatar
dengyihao 已提交
81

dengyihao's avatar
dengyihao 已提交
82 83
static void* prefixStart(AutomationCtx* ctx) {
  StartWithStateValue* data = (StartWithStateValue*)(ctx->stdata);
84
  return startWithStateValueDump(data);
dengyihao's avatar
dengyihao 已提交
85
};
dengyihao's avatar
dengyihao 已提交
86 87
static bool prefixIsMatch(AutomationCtx* ctx, void* sv) {
  StartWithStateValue* ssv = (StartWithStateValue*)sv;
dengyihao's avatar
dengyihao 已提交
88 89 90 91 92 93 94 95
  if (ssv == NULL) {
    return false;
  }
  if (ssv->type == FST_INT) {
    return ssv->val == strlen(ctx->data);
  } else {
    return false;
  }
dengyihao's avatar
dengyihao 已提交
96
}
dengyihao's avatar
dengyihao 已提交
97 98
static bool prefixCanMatch(AutomationCtx* ctx, void* sv) {
  StartWithStateValue* ssv = (StartWithStateValue*)sv;
dengyihao's avatar
dengyihao 已提交
99 100 101
  if (ssv == NULL) {
    return false;
  }
102
  return ssv->val >= 0;
dengyihao's avatar
dengyihao 已提交
103
}
dengyihao's avatar
dengyihao 已提交
104
static bool  prefixWillAlwaysMatch(AutomationCtx* ctx, void* state) { return true; }
dengyihao's avatar
dengyihao 已提交
105 106
static void* prefixAccept(AutomationCtx* ctx, void* state, uint8_t byte) {
  StartWithStateValue* ssv = (StartWithStateValue*)state;
dengyihao's avatar
dengyihao 已提交
107 108 109
  if (ssv == NULL || ctx == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
110

dengyihao's avatar
dengyihao 已提交
111
  char* data = ctx->data;
dengyihao's avatar
dengyihao 已提交
112 113 114
  if (ssv->kind == Done) {
    return startWithStateValueCreate(Done, FST_INT, &ssv->val);
  }
dengyihao's avatar
dengyihao 已提交
115
  if ((strlen(data) > ssv->val) && data[ssv->val] == byte) {
dengyihao's avatar
dengyihao 已提交
116 117
    int val = ssv->val + 1;

dengyihao's avatar
dengyihao 已提交
118
    StartWithStateValue* nsv = startWithStateValueCreate(Running, FST_INT, &val);
dengyihao's avatar
dengyihao 已提交
119 120 121 122
    if (prefixIsMatch(ctx, nsv)) {
      nsv->kind = Done;
    } else {
      nsv->kind = Running;
123
    }
dengyihao's avatar
dengyihao 已提交
124
    return nsv;
125
  }
dengyihao's avatar
dengyihao 已提交
126 127
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
128
static void* prefixAcceptEof(AutomationCtx* ctx, void* state) { return NULL; }
dengyihao's avatar
dengyihao 已提交
129 130 131

// pattern query, impl later

dengyihao's avatar
dengyihao 已提交
132 133 134 135
static void* patternStart(AutomationCtx* ctx) { return NULL; }
static bool  patternIsMatch(AutomationCtx* ctx, void* data) { return true; }
static bool  patternCanMatch(AutomationCtx* ctx, void* data) { return true; }
static bool  patternWillAlwaysMatch(AutomationCtx* ctx, void* state) { return true; }
dengyihao's avatar
dengyihao 已提交
136

dengyihao's avatar
dengyihao 已提交
137
static void* patternAccept(AutomationCtx* ctx, void* state, uint8_t byte) { return NULL; }
dengyihao's avatar
dengyihao 已提交
138

dengyihao's avatar
dengyihao 已提交
139
static void* patternAcceptEof(AutomationCtx* ctx, void* state) { return NULL; }
dengyihao's avatar
dengyihao 已提交
140

141
AutomationFunc automFuncs[] = {
dengyihao's avatar
dengyihao 已提交
142 143
    {alwaysMatchStart, alwaysMatchIsMatch, alwaysMatchCanMatch, alwaysMatchWillAlwaysMatch, alwaysMatchAccpet,
     alwaysMatchAccpetEof},
144 145 146
    {prefixStart, prefixIsMatch, prefixCanMatch, prefixWillAlwaysMatch, prefixAccept, prefixAcceptEof},
    {patternStart, patternIsMatch, patternCanMatch, patternWillAlwaysMatch, patternAccept, patternAcceptEof}
    // add more search type
dengyihao's avatar
dengyihao 已提交
147 148
};

dengyihao's avatar
dengyihao 已提交
149
AutomationCtx* automCtxCreate(void* data, AutomationType atype) {
wafwerar's avatar
wafwerar 已提交
150
  AutomationCtx* ctx = taosMemoryCalloc(1, sizeof(AutomationCtx));
dengyihao's avatar
dengyihao 已提交
151 152 153
  if (ctx == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
154

dengyihao's avatar
dengyihao 已提交
155
  StartWithStateValue* sv = NULL;
dengyihao's avatar
dengyihao 已提交
156 157 158 159
  if (atype == AUTOMATION_ALWAYS) {
    int val = 0;
    sv = startWithStateValueCreate(Running, FST_INT, &val);
  } else if (atype == AUTOMATION_PREFIX) {
dengyihao's avatar
dengyihao 已提交
160 161
    int val = 0;
    sv = startWithStateValueCreate(Running, FST_INT, &val);
dengyihao's avatar
dengyihao 已提交
162
  } else if (atype == AUTMMATION_MATCH) {
dengyihao's avatar
dengyihao 已提交
163 164
  } else {
    // add more search type
dengyihao's avatar
dengyihao 已提交
165
  }
dengyihao's avatar
dengyihao 已提交
166

dengyihao's avatar
dengyihao 已提交
167
  ctx->data = strdup((char*)data);
168
  ctx->type = atype;
dengyihao's avatar
dengyihao 已提交
169
  ctx->stdata = (void*)sv;
170 171
  return ctx;
}
dengyihao's avatar
dengyihao 已提交
172
void automCtxDestroy(AutomationCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
173
  startWithStateValueDestroy(ctx->stdata);
wafwerar's avatar
wafwerar 已提交
174 175
  taosMemoryFree(ctx->data);
  taosMemoryFree(ctx);
dengyihao's avatar
dengyihao 已提交
176
}