index_cache.c 4.4 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

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

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

dengyihao's avatar
dengyihao 已提交
31
  // skip total len, not compare
dengyihao's avatar
dengyihao 已提交
32 33 34 35 36 37 38
  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 已提交
39
  int16_t lf, rf; // field id
dengyihao's avatar
dengyihao 已提交
40 41 42 43 44 45 46 47
  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 已提交
48 49 50 51 52 53 54 55 56
  // compare field type
  int16_t lft, rft; 
  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 已提交
57 58 59 60 61 62
  int32_t lfl, rfl;
  memcpy(&lfl, lp, sizeof(lfl)); 
  memcpy(&rfl, rp, sizeof(rfl)); 
  lp += sizeof(lfl);
  rp += sizeof(rfl);
  
dengyihao's avatar
dengyihao 已提交
63
  // compare value 
dengyihao's avatar
dengyihao 已提交
64 65 66 67 68 69 70 71 72 73
  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 已提交
74 75 76 77 78 79 80 81
  // 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 已提交
82 83 84 85 86
  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 已提交
87
  }   
dengyihao's avatar
dengyihao 已提交
88 89
  lp += sizeof(lv);
  rp += sizeof(rv);
dengyihao's avatar
dengyihao 已提交
90
  // not care item type
dengyihao's avatar
dengyihao 已提交
91 92 93 94 95 96

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

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

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

dengyihao's avatar
dengyihao 已提交
112 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 119

  char *buf = calloc(1, total); 
  char *p   = buf;

dengyihao's avatar
dengyihao 已提交
120 121
  memcpy(p, &total, sizeof(total)); 
  p += sizeof(total);
dengyihao's avatar
dengyihao 已提交
122

dengyihao's avatar
dengyihao 已提交
123 124
  memcpy(p, &colId, sizeof(colId));   
  p += sizeof(colId);
dengyihao's avatar
dengyihao 已提交
125

dengyihao's avatar
dengyihao 已提交
126 127
  memcpy(p, &term->colType, sizeof(term->colType));
  p += sizeof(term->colType);
dengyihao's avatar
dengyihao 已提交
128
  
dengyihao's avatar
dengyihao 已提交
129 130 131 132
  memcpy(p, &term->nColVal, sizeof(term->nColVal));
  p += sizeof(term->nColVal);
  memcpy(p, term->colVal, term->nColVal); 
  p += term->nColVal;
dengyihao's avatar
dengyihao 已提交
133

dengyihao's avatar
dengyihao 已提交
134 135
  memcpy(p, &version, sizeof(version));
  p += sizeof(version);
dengyihao's avatar
dengyihao 已提交
136

dengyihao's avatar
dengyihao 已提交
137 138
  memcpy(p, &uid, sizeof(uid));  
  p += sizeof(uid);
dengyihao's avatar
dengyihao 已提交
139

dengyihao's avatar
dengyihao 已提交
140 141
  memcpy(p, &term->operType, sizeof(term->operType));
  p += sizeof(term->operType); 
dengyihao's avatar
dengyihao 已提交
142

dengyihao's avatar
dengyihao 已提交
143
  tSkipListPut(pCache->skiplist, (void *)buf);  
dengyihao's avatar
dengyihao 已提交
144
  return 0;
dengyihao's avatar
dengyihao 已提交
145
  // encode end
dengyihao's avatar
dengyihao 已提交
146 147
    
}
dengyihao's avatar
dengyihao 已提交
148 149 150
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 已提交
151
}
dengyihao's avatar
dengyihao 已提交
152
int indexCacheSearch(void *cache, SIndexTermQuery *query, int16_t colId, int32_t version, SArray *result) {
dengyihao's avatar
dengyihao 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  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) {
       
  } else if (qtype == QUERY_PREFIX) {

  } else if (qtype == QUERY_SUFFIX) {

  } else if (qtype == QUERY_REGEX) {

  }
  
dengyihao's avatar
dengyihao 已提交
171
  return 0;
dengyihao's avatar
dengyihao 已提交
172
}
dengyihao's avatar
dengyihao 已提交
173