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 19
StartWithStateValue* startWithStateValueCreate(StartWithStateKind kind, ValueType ty, void* val) {
  StartWithStateValue* nsv = calloc(1, sizeof(StartWithStateValue));
dengyihao's avatar
dengyihao 已提交
20
  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
void startWithStateValueDestroy(void* val) {
  StartWithStateValue* sv = (StartWithStateValue*)val;
dengyihao's avatar
dengyihao 已提交
38
  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
StartWithStateValue* startWithStateValueDump(StartWithStateValue* sv) {
  StartWithStateValue* nsv = calloc(1, sizeof(StartWithStateValue));
dengyihao's avatar
dengyihao 已提交
51
  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
// iterate fst
dengyihao's avatar
dengyihao 已提交
68 69 70 71 72 73
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 已提交
74
// prefix query, impl later
dengyihao's avatar
dengyihao 已提交
75

dengyihao's avatar
dengyihao 已提交
76 77
static void* prefixStart(AutomationCtx* ctx) {
  StartWithStateValue* data = (StartWithStateValue*)(ctx->stdata);
78
  return startWithStateValueDump(data);
dengyihao's avatar
dengyihao 已提交
79
};
dengyihao's avatar
dengyihao 已提交
80 81
static bool prefixIsMatch(AutomationCtx* ctx, void* sv) {
  StartWithStateValue* ssv = (StartWithStateValue*)sv;
82
  return ssv->val == strlen(ctx->data);
dengyihao's avatar
dengyihao 已提交
83
}
dengyihao's avatar
dengyihao 已提交
84 85
static bool prefixCanMatch(AutomationCtx* ctx, void* sv) {
  StartWithStateValue* ssv = (StartWithStateValue*)sv;
86
  return ssv->val >= 0;
dengyihao's avatar
dengyihao 已提交
87
}
dengyihao's avatar
dengyihao 已提交
88
static bool  prefixWillAlwaysMatch(AutomationCtx* ctx, void* state) { return true; }
dengyihao's avatar
dengyihao 已提交
89 90
static void* prefixAccept(AutomationCtx* ctx, void* state, uint8_t byte) {
  StartWithStateValue* ssv = (StartWithStateValue*)state;
dengyihao's avatar
dengyihao 已提交
91
  if (ssv == NULL || ctx == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
92

dengyihao's avatar
dengyihao 已提交
93
  char* data = ctx->data;
dengyihao's avatar
dengyihao 已提交
94
  if (ssv->kind == Done) { return startWithStateValueCreate(Done, FST_INT, &ssv->val); }
dengyihao's avatar
dengyihao 已提交
95
  if ((strlen(data) > ssv->val) && data[ssv->val] == byte) {
dengyihao's avatar
dengyihao 已提交
96 97
    int val = ssv->val + 1;

dengyihao's avatar
dengyihao 已提交
98
    StartWithStateValue* nsv = startWithStateValueCreate(Running, FST_INT, &val);
dengyihao's avatar
dengyihao 已提交
99 100 101 102
    if (prefixIsMatch(ctx, nsv)) {
      nsv->kind = Done;
    } else {
      nsv->kind = Running;
103
    }
dengyihao's avatar
dengyihao 已提交
104
    return nsv;
105
  }
dengyihao's avatar
dengyihao 已提交
106 107
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
108
static void* prefixAcceptEof(AutomationCtx* ctx, void* state) { return NULL; }
dengyihao's avatar
dengyihao 已提交
109 110 111

// pattern query, impl later

dengyihao's avatar
dengyihao 已提交
112 113 114 115
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 已提交
116

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

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

121
AutomationFunc automFuncs[] = {
dengyihao's avatar
dengyihao 已提交
122 123
    {alwaysMatchStart, alwaysMatchIsMatch, alwaysMatchCanMatch, alwaysMatchWillAlwaysMatch, alwaysMatchAccpet,
     alwaysMatchAccpetEof},
124 125 126
    {prefixStart, prefixIsMatch, prefixCanMatch, prefixWillAlwaysMatch, prefixAccept, prefixAcceptEof},
    {patternStart, patternIsMatch, patternCanMatch, patternWillAlwaysMatch, patternAccept, patternAcceptEof}
    // add more search type
dengyihao's avatar
dengyihao 已提交
127 128
};

dengyihao's avatar
dengyihao 已提交
129 130
AutomationCtx* automCtxCreate(void* data, AutomationType atype) {
  AutomationCtx* ctx = calloc(1, sizeof(AutomationCtx));
dengyihao's avatar
dengyihao 已提交
131
  if (ctx == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
132

dengyihao's avatar
dengyihao 已提交
133
  StartWithStateValue* sv = NULL;
dengyihao's avatar
dengyihao 已提交
134 135 136 137 138
  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 已提交
139 140
    int val = 0;
    sv = startWithStateValueCreate(Running, FST_INT, &val);
dengyihao's avatar
dengyihao 已提交
141
    ctx->stdata = (void*)sv;
dengyihao's avatar
dengyihao 已提交
142
  } else if (atype == AUTMMATION_MATCH) {
dengyihao's avatar
dengyihao 已提交
143 144
  } else {
    // add more search type
dengyihao's avatar
dengyihao 已提交
145
  }
dengyihao's avatar
dengyihao 已提交
146

dengyihao's avatar
dengyihao 已提交
147 148 149 150 151 152 153 154
  char* dst = NULL;
  if (data != NULL) {
    char*  src = (char*)data;
    size_t len = strlen(src);
    dst = (char*)malloc(len * sizeof(char) + 1);
    memcpy(dst, src, len);
    dst[len] = 0;
  }
155 156 157

  ctx->data = dst;
  ctx->type = atype;
dengyihao's avatar
dengyihao 已提交
158
  ctx->stdata = (void*)sv;
159 160
  return ctx;
}
dengyihao's avatar
dengyihao 已提交
161
void automCtxDestroy(AutomationCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
162 163
  startWithStateValueDestroy(ctx->stdata);
  free(ctx->data);
dengyihao's avatar
dengyihao 已提交
164 165
  free(ctx);
}