index_cache.c 3.8 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 20 21 22 23
#define MAX_INDEX_KEY_LEN 128 // test only, change later

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

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

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

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

dengyihao's avatar
dengyihao 已提交
105 106
int indexCachePut(IndexCache *cache, int16_t fieldId, int16_t fieldType, const char *fieldValue,  int32_t fvLen, 
              uint32_t version, uint64_t uid, int8_t operType) {
dengyihao's avatar
dengyihao 已提交
107 108
  if (cache == NULL) { return -1;} 

dengyihao's avatar
dengyihao 已提交
109
  // encode data
dengyihao's avatar
dengyihao 已提交
110
  int32_t total = sizeof(int32_t) + sizeof(fieldId) + sizeof(fieldType) + sizeof(fvLen) + fvLen  + sizeof(version) + sizeof(uid) + sizeof(operType); 
dengyihao's avatar
dengyihao 已提交
111 112 113 114

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

dengyihao's avatar
dengyihao 已提交
115 116
  memcpy(p, &total, sizeof(total)); 
  p += sizeof(total);
dengyihao's avatar
dengyihao 已提交
117

dengyihao's avatar
dengyihao 已提交
118 119
  memcpy(p, &fieldId, sizeof(fieldId));   
  p += sizeof(fieldId);
dengyihao's avatar
dengyihao 已提交
120

dengyihao's avatar
dengyihao 已提交
121 122 123 124 125 126 127
  memcpy(p, &fieldType, sizeof(fieldType));
  p += sizeof(fieldType);
  
  memcpy(p, &fvLen, sizeof(fvLen));
  p += sizeof(fvLen);
  memcpy(p, fieldValue, fvLen); 
  p += fvLen;
dengyihao's avatar
dengyihao 已提交
128

dengyihao's avatar
dengyihao 已提交
129 130
  memcpy(p, &version, sizeof(version));
  p += sizeof(version);
dengyihao's avatar
dengyihao 已提交
131

dengyihao's avatar
dengyihao 已提交
132 133
  memcpy(p, &uid, sizeof(uid));  
  p += sizeof(uid);
dengyihao's avatar
dengyihao 已提交
134

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

  tSkipListPut(cache->skiplist, (void *)buf);  
  // encode end
 
dengyihao's avatar
dengyihao 已提交
141 142 143
    
}
int indexCacheDel(IndexCache *cache, int32_t fieldId, const char *fieldValue, int32_t fvlen, uint64_t uid, int8_t operType) {
dengyihao's avatar
dengyihao 已提交
144 145 146 147 148 149 150 151 152 153 154

}
int indexCacheSearch(IndexCache *cache, SIndexMultiTermQuery *query, SArray *result) {
  
  return 0;  
}