index_fst_automation.c 4.7 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
// prefix query, impl later
dengyihao's avatar
dengyihao 已提交
68

dengyihao's avatar
dengyihao 已提交
69 70
static void* prefixStart(AutomationCtx* ctx) {
  StartWithStateValue* data = (StartWithStateValue*)(ctx->stdata);
71
  return startWithStateValueDump(data);
dengyihao's avatar
dengyihao 已提交
72
};
dengyihao's avatar
dengyihao 已提交
73 74
static bool prefixIsMatch(AutomationCtx* ctx, void* sv) {
  StartWithStateValue* ssv = (StartWithStateValue*)sv;
75
  return ssv->val == strlen(ctx->data);
dengyihao's avatar
dengyihao 已提交
76
}
dengyihao's avatar
dengyihao 已提交
77 78
static bool prefixCanMatch(AutomationCtx* ctx, void* sv) {
  StartWithStateValue* ssv = (StartWithStateValue*)sv;
79
  return ssv->val >= 0;
dengyihao's avatar
dengyihao 已提交
80
}
dengyihao's avatar
dengyihao 已提交
81 82 83 84 85 86
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 已提交
87

dengyihao's avatar
dengyihao 已提交
88 89
  char* data = ctx->data;
  if (ssv->kind == Done) { return startWithStateValueCreate(Done, FST_INT, &ssv->val); }
dengyihao's avatar
dengyihao 已提交
90
  if ((strlen(data) > ssv->val) && data[ssv->val] == byte) {
91
    int                  val = ssv->val + 1;
dengyihao's avatar
dengyihao 已提交
92
    StartWithStateValue* nsv = startWithStateValueCreate(Running, FST_INT, &val);
dengyihao's avatar
dengyihao 已提交
93 94 95 96
    if (prefixIsMatch(ctx, nsv)) {
      nsv->kind = Done;
    } else {
      nsv->kind = Running;
97
    }
dengyihao's avatar
dengyihao 已提交
98
    return nsv;
99
  }
dengyihao's avatar
dengyihao 已提交
100 101
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
102 103 104
static void* prefixAcceptEof(AutomationCtx* ctx, void* state) {
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
105 106 107

// pattern query, impl later

dengyihao's avatar
dengyihao 已提交
108 109 110 111 112 113 114 115 116 117 118 119
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 已提交
120

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

dengyihao's avatar
dengyihao 已提交
125 126 127
static void* patternAcceptEof(AutomationCtx* ctx, void* state) {
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
128

129 130 131 132
AutomationFunc automFuncs[] = {
    {prefixStart, prefixIsMatch, prefixCanMatch, prefixWillAlwaysMatch, prefixAccept, prefixAcceptEof},
    {patternStart, patternIsMatch, patternCanMatch, patternWillAlwaysMatch, patternAccept, patternAcceptEof}
    // add more search type
dengyihao's avatar
dengyihao 已提交
133 134
};

dengyihao's avatar
dengyihao 已提交
135 136 137
AutomationCtx* automCtxCreate(void* data, AutomationType atype) {
  AutomationCtx* ctx = calloc(1, sizeof(AutomationCtx));
  if (ctx == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
138

dengyihao's avatar
dengyihao 已提交
139
  StartWithStateValue* sv = NULL;
dengyihao's avatar
dengyihao 已提交
140
  if (atype == AUTOMATION_PREFIX) {
dengyihao's avatar
dengyihao 已提交
141 142
    int val = 0;
    sv = startWithStateValueCreate(Running, FST_INT, &val);
dengyihao's avatar
dengyihao 已提交
143
    ctx->stdata = (void*)sv;
dengyihao's avatar
dengyihao 已提交
144
  } else if (atype == AUTMMATION_MATCH) {
dengyihao's avatar
dengyihao 已提交
145 146
  } else {
    // add more search type
dengyihao's avatar
dengyihao 已提交
147
  }
dengyihao's avatar
dengyihao 已提交
148

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

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