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

#include "index_cache.h"
dengyihao's avatar
dengyihao 已提交
17
#include "index_util.h"
18
#include "tcompare.h"
dengyihao's avatar
dengyihao 已提交
19

20
#define MAX_INDEX_KEY_LEN 256  // test only, change later
dengyihao's avatar
dengyihao 已提交
21

22
// ref index_cache.h:22
dengyihao's avatar
dengyihao 已提交
23 24
#define CACHE_KEY_LEN(p) \
  (sizeof(int32_t) + sizeof(uint16_t) + sizeof(p->colType) + sizeof(p->nColVal) + p->nColVal + sizeof(uint64_t) + sizeof(p->operType))
dengyihao's avatar
dengyihao 已提交
25

dengyihao's avatar
dengyihao 已提交
26 27 28 29 30 31
static char* getIndexKey(const void* pData) {
  return NULL;
}
static int32_t compareKey(const void* l, const void* r) {
  char* lp = (char*)l;
  char* rp = (char*)r;
dengyihao's avatar
dengyihao 已提交
32

dengyihao's avatar
dengyihao 已提交
33
  // skip total len, not compare
34
  int32_t ll, rl;  // len
dengyihao's avatar
dengyihao 已提交
35 36
  memcpy(&ll, lp, sizeof(int32_t));
  memcpy(&rl, rp, sizeof(int32_t));
37
  lp += sizeof(int32_t);
dengyihao's avatar
dengyihao 已提交
38
  rp += sizeof(int32_t);
39

dengyihao's avatar
dengyihao 已提交
40
  // compare field id
41
  int16_t lf, rf;  // field id
dengyihao's avatar
dengyihao 已提交
42 43
  memcpy(&lf, lp, sizeof(lf));
  memcpy(&rf, rp, sizeof(rf));
dengyihao's avatar
dengyihao 已提交
44
  if (lf != rf) { return lf < rf ? -1 : 1; }
dengyihao's avatar
dengyihao 已提交
45 46 47
  lp += sizeof(lf);
  rp += sizeof(rf);

dengyihao's avatar
dengyihao 已提交
48
  // compare field type
49
  int8_t lft, rft;
dengyihao's avatar
dengyihao 已提交
50 51 52 53 54
  memcpy(&lft, lp, sizeof(lft));
  memcpy(&rft, rp, sizeof(rft));
  lp += sizeof(lft);
  rp += sizeof(rft);
  assert(rft == rft);
55 56

  // skip value len
dengyihao's avatar
dengyihao 已提交
57
  int32_t lfl, rfl;
58 59
  memcpy(&lfl, lp, sizeof(lfl));
  memcpy(&rfl, rp, sizeof(rfl));
dengyihao's avatar
dengyihao 已提交
60 61
  lp += sizeof(lfl);
  rp += sizeof(rfl);
62 63

  // compare value
dengyihao's avatar
dengyihao 已提交
64 65
  int32_t i, j;
  for (i = 0, j = 0; i < lfl && j < rfl; i++, j++) {
66 67 68 69 70 71 72 73 74 75
    if (lp[i] == rp[j]) {
      continue;
    } else {
      return lp[i] < rp[j] ? -1 : 1;
    }
  }
  if (i < lfl) {
    return 1;
  } else if (j < rfl) {
    return -1;
dengyihao's avatar
dengyihao 已提交
76 77
  }
  lp += lfl;
78
  rp += rfl;
dengyihao's avatar
dengyihao 已提交
79

80
  // skip uid
dengyihao's avatar
dengyihao 已提交
81
  uint64_t lu, ru;
82
  memcpy(&lu, lp, sizeof(lu));
dengyihao's avatar
dengyihao 已提交
83 84 85
  memcpy(&ru, rp, sizeof(ru));
  lp += sizeof(lu);
  rp += sizeof(ru);
86

dengyihao's avatar
dengyihao 已提交
87
  // compare version, desc order
dengyihao's avatar
dengyihao 已提交
88 89 90
  int32_t lv, rv;
  memcpy(&lv, lp, sizeof(lv));
  memcpy(&rv, rp, sizeof(rv));
dengyihao's avatar
dengyihao 已提交
91 92
  if (lv != rv) { return lv > rv ? -1 : 1; }

dengyihao's avatar
dengyihao 已提交
93 94
  lp += sizeof(lv);
  rp += sizeof(rv);
dengyihao's avatar
dengyihao 已提交
95
  // not care item type
dengyihao's avatar
dengyihao 已提交
96

97 98
  return 0;
}
dengyihao's avatar
dengyihao 已提交
99 100
IndexCache* indexCacheCreate() {
  IndexCache* cache = calloc(1, sizeof(IndexCache));
dengyihao's avatar
dengyihao 已提交
101 102 103 104
  if (cache == NULL) {
    indexError("failed to create index cache");
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
105 106
  cache->skiplist =
      tSkipListCreate(MAX_SKIP_LIST_LEVEL, TSDB_DATA_TYPE_BINARY, MAX_INDEX_KEY_LEN, compareKey, SL_ALLOW_DUP_KEY, getIndexKey);
dengyihao's avatar
dengyihao 已提交
107 108 109
  return cache;
}

dengyihao's avatar
dengyihao 已提交
110 111
void indexCacheDestroy(void* cache) {
  IndexCache* pCache = cache;
dengyihao's avatar
dengyihao 已提交
112
  if (pCache == NULL) { return; }
dengyihao's avatar
dengyihao 已提交
113 114
  tSkipListDestroy(pCache->skiplist);
  free(pCache);
dengyihao's avatar
dengyihao 已提交
115 116
}

dengyihao's avatar
dengyihao 已提交
117
int indexCachePut(void* cache, SIndexTerm* term, int16_t colId, int32_t version, uint64_t uid) {
dengyihao's avatar
dengyihao 已提交
118
  if (cache == NULL) { return -1; }
dengyihao's avatar
dengyihao 已提交
119

dengyihao's avatar
dengyihao 已提交
120
  IndexCache* pCache = cache;
dengyihao's avatar
dengyihao 已提交
121
  // encode data
122
  int32_t total = CACHE_KEY_LEN(term);
dengyihao's avatar
dengyihao 已提交
123

dengyihao's avatar
dengyihao 已提交
124 125
  char* buf = calloc(1, total);
  char* p = buf;
dengyihao's avatar
dengyihao 已提交
126

127
  SERIALIZE_VAR_TO_BUF(p, total, int32_t);
dengyihao's avatar
dengyihao 已提交
128
  SERIALIZE_VAR_TO_BUF(p, colId, int16_t);
dengyihao's avatar
dengyihao 已提交
129

dengyihao's avatar
dengyihao 已提交
130
  SERIALIZE_MEM_TO_BUF(p, term, colType);
131 132 133
  SERIALIZE_MEM_TO_BUF(p, term, nColVal);
  SERIALIZE_STR_MEM_TO_BUF(p, term, colVal, term->nColVal);

dengyihao's avatar
dengyihao 已提交
134
  SERIALIZE_VAR_TO_BUF(p, version, int32_t);
135
  SERIALIZE_VAR_TO_BUF(p, uid, uint64_t);
dengyihao's avatar
dengyihao 已提交
136

dengyihao's avatar
dengyihao 已提交
137
  SERIALIZE_MEM_TO_BUF(p, term, operType);
dengyihao's avatar
dengyihao 已提交
138

dengyihao's avatar
dengyihao 已提交
139
  tSkipListPut(pCache->skiplist, (void*)buf);
dengyihao's avatar
dengyihao 已提交
140
  return 0;
dengyihao's avatar
dengyihao 已提交
141
  // encode end
dengyihao's avatar
dengyihao 已提交
142
}
dengyihao's avatar
dengyihao 已提交
143 144
int indexCacheDel(void* cache, int32_t fieldId, const char* fieldValue, int32_t fvlen, uint64_t uid, int8_t operType) {
  IndexCache* pCache = cache;
dengyihao's avatar
dengyihao 已提交
145
  return 0;
dengyihao's avatar
dengyihao 已提交
146
}
dengyihao's avatar
dengyihao 已提交
147
int indexCacheSearch(void* cache, SIndexTermQuery* query, int16_t colId, int32_t version, SArray* result, STermValueType* s) {
dengyihao's avatar
dengyihao 已提交
148
  if (cache == NULL) { return -1; }
dengyihao's avatar
dengyihao 已提交
149 150
  IndexCache*     pCache = cache;
  SIndexTerm*     term = query->term;
151 152 153
  EIndexQueryType qtype = query->qType;

  int32_t keyLen = CACHE_KEY_LEN(term);
dengyihao's avatar
dengyihao 已提交
154

dengyihao's avatar
dengyihao 已提交
155
  char* buf = calloc(1, keyLen);
dengyihao's avatar
dengyihao 已提交
156
  if (qtype == QUERY_TERM) {
dengyihao's avatar
dengyihao 已提交
157
    //
dengyihao's avatar
dengyihao 已提交
158
  } else if (qtype == QUERY_PREFIX) {
dengyihao's avatar
dengyihao 已提交
159
    //
dengyihao's avatar
dengyihao 已提交
160
  } else if (qtype == QUERY_SUFFIX) {
dengyihao's avatar
dengyihao 已提交
161
    //
dengyihao's avatar
dengyihao 已提交
162
  } else if (qtype == QUERY_REGEX) {
dengyihao's avatar
dengyihao 已提交
163
    //
dengyihao's avatar
dengyihao 已提交
164
  }
165

dengyihao's avatar
dengyihao 已提交
166
  return 0;
dengyihao's avatar
dengyihao 已提交
167
}