index_fst_automation.c 4.8 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));
20 21 22
  if (nsv == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
23 24 25 26 27 28

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

  if (sv->type == FST_INT) {
45
    //
dengyihao's avatar
dengyihao 已提交
46 47 48 49 50
  } else if (sv->type == FST_CHAR) {
    free(sv->ptr);
  } else if (sv->type == FST_ARRAY) {
    taosArrayDestroy(sv->arr);
  }
51
  free(sv);
dengyihao's avatar
dengyihao 已提交
52 53 54
}
StartWithStateValue *startWithStateValueDump(StartWithStateValue *sv) {
  StartWithStateValue *nsv = calloc(1, sizeof(StartWithStateValue));
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 65 66 67 68 69 70 71
  if (nsv->type == FST_INT) {
    nsv->val = sv->val;
  } else if (nsv->type == FST_CHAR) {
    size_t len = strlen(sv->ptr);
    nsv->ptr = (char *)calloc(1, len + 1);
    memcpy(nsv->ptr, sv->ptr, len);
  } else if (nsv->type == FST_ARRAY) {
  }
  return nsv;
}

dengyihao's avatar
dengyihao 已提交
72
// prefix query, impl later
dengyihao's avatar
dengyihao 已提交
73

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

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

// pattern query, impl later

113 114 115 116
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 已提交
117

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

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

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

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

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

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

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