index_cache.c 4.2 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 "tcompare.h"
dengyihao's avatar
dengyihao 已提交
18
#include "index_util.h"
dengyihao's avatar
dengyihao 已提交
19

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

dengyihao's avatar
dengyihao 已提交
22 23 24
// ref index_cache.h:22 
#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 26 27
static char*  getIndexKey(const void *pData) {
  return NULL;  
} 
dengyihao's avatar
dengyihao 已提交
28 29 30 31
static int32_t compareKey(const void *l, const void *r) {
  char *lp = (char *)l;
  char *rp = (char *)r;

dengyihao's avatar
dengyihao 已提交
32
  // skip total len, not compare
dengyihao's avatar
dengyihao 已提交
33 34 35 36 37 38 39
  int32_t ll, rl;  // len    
  memcpy(&ll, lp, sizeof(int32_t));
  memcpy(&rl, rp, sizeof(int32_t));
  lp += sizeof(int32_t); 
  rp += sizeof(int32_t);
  
  // compare field id
dengyihao's avatar
dengyihao 已提交
40
  int16_t lf, rf; // field id
dengyihao's avatar
dengyihao 已提交
41 42 43 44 45 46 47 48
  memcpy(&lf, lp, sizeof(lf));
  memcpy(&rf, rp, sizeof(rf));
  if (lf != rf) {
    return lf < rf ? -1: 1;
  }
  lp += sizeof(lf);
  rp += sizeof(rf);

dengyihao's avatar
dengyihao 已提交
49
  // compare field type
dengyihao's avatar
dengyihao 已提交
50
  int8_t lft, rft; 
dengyihao's avatar
dengyihao 已提交
51 52 53 54 55 56 57
  memcpy(&lft, lp, sizeof(lft));
  memcpy(&rft, rp, sizeof(rft));
  lp += sizeof(lft);
  rp += sizeof(rft);
  assert(rft == rft);
  
  // skip value len  
dengyihao's avatar
dengyihao 已提交
58 59 60 61 62 63
  int32_t lfl, rfl;
  memcpy(&lfl, lp, sizeof(lfl)); 
  memcpy(&rfl, rp, sizeof(rfl)); 
  lp += sizeof(lfl);
  rp += sizeof(rfl);
  
dengyihao's avatar
dengyihao 已提交
64
  // compare value 
dengyihao's avatar
dengyihao 已提交
65 66 67 68 69 70 71 72 73 74
  int32_t i, j;
  for (i = 0, j = 0; i < lfl && j < rfl; i++, j++) {
    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; }
  lp += lfl;
  rp += rfl; 

dengyihao's avatar
dengyihao 已提交
75 76 77 78 79 80 81 82
  // skip uid 
  uint64_t lu, ru;
  memcpy(&lu, lp, sizeof(lu)); 
  memcpy(&ru, rp, sizeof(ru));
  lp += sizeof(lu);
  rp += sizeof(ru);
  
  // compare version, desc order
dengyihao's avatar
dengyihao 已提交
83 84 85 86 87
  int32_t lv, rv;
  memcpy(&lv, lp, sizeof(lv));
  memcpy(&rv, rp, sizeof(rv));
  if (lv != rv) {
    return lv > rv ? -1 : 1; 
dengyihao's avatar
dengyihao 已提交
88
  }   
dengyihao's avatar
dengyihao 已提交
89 90
  lp += sizeof(lv);
  rp += sizeof(rv);
dengyihao's avatar
dengyihao 已提交
91
  // not care item type
dengyihao's avatar
dengyihao 已提交
92 93 94 95 96 97

  return 0;  
  
} 
IndexCache *indexCacheCreate() {
  IndexCache *cache = calloc(1, sizeof(IndexCache));
dengyihao's avatar
dengyihao 已提交
98
  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 已提交
99
  return cache;
dengyihao's avatar
dengyihao 已提交
100
  
dengyihao's avatar
dengyihao 已提交
101 102
}

dengyihao's avatar
dengyihao 已提交
103 104 105 106 107
void indexCacheDestroy(void *cache) {
  IndexCache *pCache = cache; 
  if (pCache == NULL) { return; } 
  tSkipListDestroy(pCache->skiplist);
  free(pCache);
dengyihao's avatar
dengyihao 已提交
108 109
}

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

dengyihao's avatar
dengyihao 已提交
113
  IndexCache *pCache = cache;
dengyihao's avatar
dengyihao 已提交
114
  // encode data
dengyihao's avatar
dengyihao 已提交
115
  int32_t total = CACHE_KEY_LEN(term); 
dengyihao's avatar
dengyihao 已提交
116 117 118
  char *buf = calloc(1, total); 
  char *p   = buf;

dengyihao's avatar
dengyihao 已提交
119 120
  SERIALIZE_VAR_TO_BUF(p, total,int32_t);
  SERIALIZE_VAR_TO_BUF(p, colId, int16_t);
dengyihao's avatar
dengyihao 已提交
121

dengyihao's avatar
dengyihao 已提交
122 123 124
  SERIALIZE_MEM_TO_BUF(p, term, colType);
  SERIALIZE_MEM_TO_BUF(p, term, nColVal); 
  SERIALIZE_STR_MEM_TO_BUF(p, term, colVal, term->nColVal); 
dengyihao's avatar
dengyihao 已提交
125
  
dengyihao's avatar
dengyihao 已提交
126 127
  SERIALIZE_VAR_TO_BUF(p, version, int32_t);
  SERIALIZE_VAR_TO_BUF(p, uid, uint64_t); 
dengyihao's avatar
dengyihao 已提交
128

dengyihao's avatar
dengyihao 已提交
129
  SERIALIZE_MEM_TO_BUF(p, term, operType);
dengyihao's avatar
dengyihao 已提交
130

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

  char *buf = calloc(1, keyLen);
  if (qtype == QUERY_TERM) {
dengyihao's avatar
dengyihao 已提交
150
           
dengyihao's avatar
dengyihao 已提交
151 152 153 154 155 156 157 158
  } else if (qtype == QUERY_PREFIX) {

  } else if (qtype == QUERY_SUFFIX) {

  } else if (qtype == QUERY_REGEX) {

  }
  
dengyihao's avatar
dengyihao 已提交
159
  return 0;
dengyihao's avatar
dengyihao 已提交
160
}
dengyihao's avatar
dengyihao 已提交
161