index_fst_automation.c 5.5 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 19 20
StartWithStateValue* startWithStateValueCreate(StartWithStateKind kind, ValueType ty, void* val) {
  StartWithStateValue* nsv = calloc(1, sizeof(StartWithStateValue));
  if (nsv == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
21 22 23 24

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

  if (sv->type == FST_INT) {
41
    //
dengyihao's avatar
dengyihao 已提交
42 43 44 45 46
  } else if (sv->type == FST_CHAR) {
    free(sv->ptr);
  } else if (sv->type == FST_ARRAY) {
    taosArrayDestroy(sv->arr);
  }
47
  free(sv);
dengyihao's avatar
dengyihao 已提交
48
}
dengyihao's avatar
dengyihao 已提交
49 50 51
StartWithStateValue* startWithStateValueDump(StartWithStateValue* sv) {
  StartWithStateValue* nsv = calloc(1, sizeof(StartWithStateValue));
  if (nsv == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
52 53

  nsv->kind = sv->kind;
54
  nsv->type = sv->type;
dengyihao's avatar
dengyihao 已提交
55 56 57 58
  if (nsv->type == FST_INT) {
    nsv->val = sv->val;
  } else if (nsv->type == FST_CHAR) {
    size_t len = strlen(sv->ptr);
dengyihao's avatar
dengyihao 已提交
59
    nsv->ptr = (char*)calloc(1, len + 1);
dengyihao's avatar
dengyihao 已提交
60 61
    memcpy(nsv->ptr, sv->ptr, len);
  } else if (nsv->type == FST_ARRAY) {
dengyihao's avatar
dengyihao 已提交
62
    //
dengyihao's avatar
dengyihao 已提交
63 64 65 66
  }
  return nsv;
}

dengyihao's avatar
dengyihao 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
// iterate fst
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 已提交
86
// prefix query, impl later
dengyihao's avatar
dengyihao 已提交
87

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

dengyihao's avatar
dengyihao 已提交
107 108
  char* data = ctx->data;
  if (ssv->kind == Done) { return startWithStateValueCreate(Done, FST_INT, &ssv->val); }
dengyihao's avatar
dengyihao 已提交
109
  if ((strlen(data) > ssv->val) && data[ssv->val] == byte) {
110
    int                  val = ssv->val + 1;
dengyihao's avatar
dengyihao 已提交
111
    StartWithStateValue* nsv = startWithStateValueCreate(Running, FST_INT, &val);
dengyihao's avatar
dengyihao 已提交
112 113 114 115
    if (prefixIsMatch(ctx, nsv)) {
      nsv->kind = Done;
    } else {
      nsv->kind = Running;
116
    }
dengyihao's avatar
dengyihao 已提交
117
    return nsv;
118
  }
dengyihao's avatar
dengyihao 已提交
119 120
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
121 122 123
static void* prefixAcceptEof(AutomationCtx* ctx, void* state) {
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
124 125 126

// pattern query, impl later

dengyihao's avatar
dengyihao 已提交
127 128 129 130 131 132 133 134 135 136 137 138
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 已提交
139

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

dengyihao's avatar
dengyihao 已提交
144 145 146
static void* patternAcceptEof(AutomationCtx* ctx, void* state) {
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
147

148
AutomationFunc automFuncs[] = {
dengyihao's avatar
dengyihao 已提交
149
    {alwaysMatchStart, alwaysMatchIsMatch, alwaysMatchCanMatch, alwaysMatchWillAlwaysMatch, alwaysMatchAccpet, alwaysMatchAccpetEof},
150 151 152
    {prefixStart, prefixIsMatch, prefixCanMatch, prefixWillAlwaysMatch, prefixAccept, prefixAcceptEof},
    {patternStart, patternIsMatch, patternCanMatch, patternWillAlwaysMatch, patternAccept, patternAcceptEof}
    // add more search type
dengyihao's avatar
dengyihao 已提交
153 154
};

dengyihao's avatar
dengyihao 已提交
155 156 157
AutomationCtx* automCtxCreate(void* data, AutomationType atype) {
  AutomationCtx* ctx = calloc(1, sizeof(AutomationCtx));
  if (ctx == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
158

dengyihao's avatar
dengyihao 已提交
159
  StartWithStateValue* sv = NULL;
dengyihao's avatar
dengyihao 已提交
160 161 162 163 164
  if (atype == AUTOMATION_ALWAYS) {
    int val = 0;
    sv = startWithStateValueCreate(Running, FST_INT, &val);
    ctx->stdata = (void*)sv;
  } else if (atype == AUTOMATION_PREFIX) {
dengyihao's avatar
dengyihao 已提交
165 166
    int val = 0;
    sv = startWithStateValueCreate(Running, FST_INT, &val);
dengyihao's avatar
dengyihao 已提交
167
    ctx->stdata = (void*)sv;
dengyihao's avatar
dengyihao 已提交
168
  } else if (atype == AUTMMATION_MATCH) {
dengyihao's avatar
dengyihao 已提交
169 170
  } else {
    // add more search type
dengyihao's avatar
dengyihao 已提交
171
  }
dengyihao's avatar
dengyihao 已提交
172

dengyihao's avatar
dengyihao 已提交
173
  char*  src = (char*)data;
dengyihao's avatar
dengyihao 已提交
174
  size_t len = strlen(src);
dengyihao's avatar
dengyihao 已提交
175
  char*  dst = (char*)malloc(len * sizeof(char) + 1);
dengyihao's avatar
dengyihao 已提交
176 177
  memcpy(dst, src, len);
  dst[len] = 0;
178 179 180

  ctx->data = dst;
  ctx->type = atype;
dengyihao's avatar
dengyihao 已提交
181
  ctx->stdata = (void*)sv;
182 183
  return ctx;
}
dengyihao's avatar
dengyihao 已提交
184
void automCtxDestroy(AutomationCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
185 186
  startWithStateValueDestroy(ctx->stdata);
  free(ctx->data);
dengyihao's avatar
dengyihao 已提交
187 188
  free(ctx);
}