indexCache.c 22.7 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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 已提交
16 17 18
#include "indexCache.h"
#include "indexComm.h"
#include "indexUtil.h"
19
#include "tcompare.h"
dengyihao's avatar
dengyihao 已提交
20
#include "tsched.h"
dengyihao's avatar
dengyihao 已提交
21

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

dengyihao's avatar
dengyihao 已提交
24
#define MEM_TERM_LIMIT     10 * 10000
dengyihao's avatar
dengyihao 已提交
25
#define MEM_THRESHOLD      64 * 1024
dengyihao's avatar
dengyihao 已提交
26
#define MEM_ESTIMATE_RADIO 1.5
dengyihao's avatar
dengyihao 已提交
27

dengyihao's avatar
dengyihao 已提交
28 29
static void indexMemRef(MemTable* tbl);
static void indexMemUnRef(MemTable* tbl);
30

dengyihao's avatar
dengyihao 已提交
31 32
static void    indexCacheTermDestroy(CacheTerm* ct);
static int32_t indexCacheTermCompare(const void* l, const void* r);
33
static int32_t indexCacheJsonTermCompare(const void* l, const void* r);
dengyihao's avatar
dengyihao 已提交
34
static char*   indexCacheTermGet(const void* pData);
35

36
static MemTable* indexInternalCacheCreate(int8_t type);
37

dengyihao's avatar
dengyihao 已提交
38 39 40 41 42 43 44 45 46
static int32_t cacheSearchTerm(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchPrefix(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchSuffix(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchRegex(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchLessThan(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchLessEqual(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchGreaterThan(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchGreaterEqual(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchRange(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
dengyihao's avatar
dengyihao 已提交
47
/*comm func of compare, used in (LE/LT/GE/GT compare)*/
dengyihao's avatar
dengyihao 已提交
48
static int32_t cacheSearchCompareFunc(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s,
dengyihao's avatar
dengyihao 已提交
49
                                      RangeType type);
dengyihao's avatar
dengyihao 已提交
50 51 52 53 54 55 56 57 58 59 60 61
static int32_t cacheSearchTerm_JSON(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchPrefix_JSON(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchSuffix_JSON(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchRegex_JSON(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchLessThan_JSON(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchLessEqual_JSON(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchGreaterThan_JSON(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchGreaterEqual_JSON(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);
static int32_t cacheSearchRange_JSON(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s);

static int32_t cacheSearchCompareFunc_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s,
                                           RangeType type);
dengyihao's avatar
dengyihao 已提交
62

dengyihao's avatar
dengyihao 已提交
63 64 65 66 67 68
static int32_t (*cacheSearch[][QUERY_MAX])(void* cache, SIndexTerm* ct, SIdxTempResult* tr, STermValueType* s) = {
    {cacheSearchTerm, cacheSearchPrefix, cacheSearchSuffix, cacheSearchRegex, cacheSearchLessThan, cacheSearchLessEqual,
     cacheSearchGreaterThan, cacheSearchGreaterEqual, cacheSearchRange},
    {cacheSearchTerm_JSON, cacheSearchPrefix_JSON, cacheSearchSuffix_JSON, cacheSearchRegex_JSON,
     cacheSearchLessThan_JSON, cacheSearchLessEqual_JSON, cacheSearchGreaterThan_JSON, cacheSearchGreaterEqual_JSON,
     cacheSearchRange_JSON}};
dengyihao's avatar
dengyihao 已提交
69

dengyihao's avatar
dengyihao 已提交
70 71 72
static void doMergeWork(SSchedMsg* msg);
static bool indexCacheIteratorNext(Iterate* itera);

dengyihao's avatar
dengyihao 已提交
73
static int32_t cacheSearchTerm(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
dengyihao's avatar
dengyihao 已提交
74 75 76
  if (cache == NULL) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
77 78
  MemTable*   mem = cache;
  IndexCache* pCache = mem->pCache;
dengyihao's avatar
dengyihao 已提交
79

dengyihao's avatar
dengyihao 已提交
80 81 82 83 84
  CacheTerm* pCt = taosMemoryCalloc(1, sizeof(CacheTerm));
  pCt->colVal = term->colVal;
  pCt->version = atomic_load_32(&pCache->version);

  char* key = indexCacheTermGet(pCt);
dengyihao's avatar
dengyihao 已提交
85 86 87 88 89 90 91 92

  SSkipListIterator* iter = tSkipListCreateIterFromVal(mem->mem, key, TSDB_DATA_TYPE_BINARY, TSDB_ORDER_ASC);
  while (tSkipListIterNext(iter)) {
    SSkipListNode* node = tSkipListIterGet(iter);
    if (node == NULL) {
      break;
    }
    CacheTerm* c = (CacheTerm*)SL_GET_NODE_DATA(node);
dengyihao's avatar
dengyihao 已提交
93
    if (0 == strcmp(c->colVal, pCt->colVal)) {
dengyihao's avatar
dengyihao 已提交
94 95 96 97 98 99 100 101 102 103 104
      if (c->operaType == ADD_VALUE) {
        INDEX_MERGE_ADD_DEL(tr->deled, tr->added, c->uid)
        // taosArrayPush(result, &c->uid);
        *s = kTypeValue;
      } else if (c->operaType == DEL_VALUE) {
        INDEX_MERGE_ADD_DEL(tr->added, tr->deled, c->uid)
      }
    } else {
      break;
    }
  }
dengyihao's avatar
dengyihao 已提交
105 106

  taosMemoryFree(pCt);
dengyihao's avatar
dengyihao 已提交
107
  tSkipListDestroyIter(iter);
dengyihao's avatar
dengyihao 已提交
108 109
  return 0;
}
dengyihao's avatar
dengyihao 已提交
110
static int32_t cacheSearchPrefix(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
dengyihao's avatar
dengyihao 已提交
111 112 113
  // impl later
  return 0;
}
dengyihao's avatar
dengyihao 已提交
114
static int32_t cacheSearchSuffix(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
dengyihao's avatar
dengyihao 已提交
115 116 117
  // impl later
  return 0;
}
dengyihao's avatar
dengyihao 已提交
118
static int32_t cacheSearchRegex(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
dengyihao's avatar
dengyihao 已提交
119 120 121
  // impl later
  return 0;
}
dengyihao's avatar
dengyihao 已提交
122
static int32_t cacheSearchCompareFunc(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s,
dengyihao's avatar
dengyihao 已提交
123 124 125 126
                                      RangeType type) {
  if (cache == NULL) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
127

128
  _cache_range_compare cmpFn = indexGetCompare(type);
dengyihao's avatar
dengyihao 已提交
129

dengyihao's avatar
dengyihao 已提交
130 131 132 133 134 135 136 137
  MemTable*   mem = cache;
  IndexCache* pCache = mem->pCache;

  CacheTerm* pCt = taosMemoryCalloc(1, sizeof(CacheTerm));
  pCt->colVal = term->colVal;
  pCt->version = atomic_load_32(&pCache->version);

  char* key = indexCacheTermGet(pCt);
dengyihao's avatar
dengyihao 已提交
138 139 140 141 142 143 144 145

  SSkipListIterator* iter = tSkipListCreateIter(mem->mem);
  while (tSkipListIterNext(iter)) {
    SSkipListNode* node = tSkipListIterGet(iter);
    if (node == NULL) {
      break;
    }
    CacheTerm* c = (CacheTerm*)SL_GET_NODE_DATA(node);
dengyihao's avatar
dengyihao 已提交
146
    TExeCond   cond = cmpFn(c->colVal, pCt->colVal, pCt->colType);
dengyihao's avatar
dengyihao 已提交
147 148 149 150 151 152 153 154 155
    if (cond == MATCH) {
      if (c->operaType == ADD_VALUE) {
        INDEX_MERGE_ADD_DEL(tr->deled, tr->added, c->uid)
        // taosArrayPush(result, &c->uid);
        *s = kTypeValue;
      } else if (c->operaType == DEL_VALUE) {
        INDEX_MERGE_ADD_DEL(tr->added, tr->deled, c->uid)
      }
    } else if (cond == CONTINUE) {
dengyihao's avatar
dengyihao 已提交
156
      continue;
dengyihao's avatar
dengyihao 已提交
157 158 159 160
    } else if (cond == BREAK) {
      break;
    }
  }
dengyihao's avatar
dengyihao 已提交
161
  taosMemoryFree(pCt);
dengyihao's avatar
dengyihao 已提交
162 163 164
  tSkipListDestroyIter(iter);
  return TSDB_CODE_SUCCESS;
}
dengyihao's avatar
dengyihao 已提交
165 166 167 168 169
static int32_t cacheSearchLessThan(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return cacheSearchCompareFunc(cache, term, tr, s, LT);
}
static int32_t cacheSearchLessEqual(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return cacheSearchCompareFunc(cache, term, tr, s, LE);
dengyihao's avatar
dengyihao 已提交
170
}
dengyihao's avatar
dengyihao 已提交
171 172 173 174 175 176 177 178
static int32_t cacheSearchGreaterThan(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return cacheSearchCompareFunc(cache, term, tr, s, GT);
}
static int32_t cacheSearchGreaterEqual(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return cacheSearchCompareFunc(cache, term, tr, s, GE);
}

static int32_t cacheSearchTerm_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
dengyihao's avatar
dengyihao 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  if (cache == NULL) {
    return 0;
  }
  MemTable*   mem = cache;
  IndexCache* pCache = mem->pCache;

  CacheTerm* pCt = taosMemoryCalloc(1, sizeof(CacheTerm));
  pCt->colVal = term->colVal;
  pCt->version = atomic_load_32(&pCache->version);

  char* exBuf = NULL;
  if (INDEX_TYPE_CONTAIN_EXTERN_TYPE(term->colType, TSDB_DATA_TYPE_JSON)) {
    exBuf = indexPackJsonData(term);
    pCt->colVal = exBuf;
  }
  char* key = indexCacheTermGet(pCt);

  SSkipListIterator* iter = tSkipListCreateIterFromVal(mem->mem, key, TSDB_DATA_TYPE_BINARY, TSDB_ORDER_ASC);
  while (tSkipListIterNext(iter)) {
    SSkipListNode* node = tSkipListIterGet(iter);
    if (node == NULL) {
      break;
    }
    CacheTerm* c = (CacheTerm*)SL_GET_NODE_DATA(node);
203

dengyihao's avatar
dengyihao 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    if (0 == strcmp(c->colVal, pCt->colVal)) {
      if (c->operaType == ADD_VALUE) {
        INDEX_MERGE_ADD_DEL(tr->deled, tr->added, c->uid)
        // taosArrayPush(result, &c->uid);
        *s = kTypeValue;
      } else if (c->operaType == DEL_VALUE) {
        INDEX_MERGE_ADD_DEL(tr->added, tr->deled, c->uid)
      }
    } else {
      break;
    }
  }

  taosMemoryFree(pCt);
  taosMemoryFree(exBuf);
  tSkipListDestroyIter(iter);
  return 0;

dengyihao's avatar
dengyihao 已提交
222 223 224 225 226 227 228 229 230 231
  return TSDB_CODE_SUCCESS;
}
static int32_t cacheSearchPrefix_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return TSDB_CODE_SUCCESS;
}
static int32_t cacheSearchSuffix_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return TSDB_CODE_SUCCESS;
}
static int32_t cacheSearchRegex_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
232
}
dengyihao's avatar
dengyihao 已提交
233 234
static int32_t cacheSearchLessThan_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return cacheSearchCompareFunc_JSON(cache, term, tr, s, LT);
dengyihao's avatar
dengyihao 已提交
235
}
dengyihao's avatar
dengyihao 已提交
236 237
static int32_t cacheSearchLessEqual_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return cacheSearchCompareFunc_JSON(cache, term, tr, s, LE);
dengyihao's avatar
dengyihao 已提交
238
}
dengyihao's avatar
dengyihao 已提交
239 240 241 242 243 244 245 246 247 248 249 250
static int32_t cacheSearchGreaterThan_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return cacheSearchCompareFunc_JSON(cache, term, tr, s, GT);
}
static int32_t cacheSearchGreaterEqual_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return cacheSearchCompareFunc_JSON(cache, term, tr, s, GE);
}
static int32_t cacheSearchRange_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
  return TSDB_CODE_SUCCESS;
}

static int32_t cacheSearchCompareFunc_JSON(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s,
                                           RangeType type) {
dengyihao's avatar
dengyihao 已提交
251 252 253
  if (cache == NULL) {
    return 0;
  }
254
  _cache_range_compare cmpFn = indexGetCompare(type);
dengyihao's avatar
dengyihao 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

  MemTable*   mem = cache;
  IndexCache* pCache = mem->pCache;

  CacheTerm* pCt = taosMemoryCalloc(1, sizeof(CacheTerm));
  pCt->colVal = term->colVal;
  pCt->version = atomic_load_32(&pCache->version);

  int8_t dType = INDEX_TYPE_GET_TYPE(term->colType);
  int    skip = 0;
  char*  exBuf = NULL;

  if (INDEX_TYPE_CONTAIN_EXTERN_TYPE(term->colType, TSDB_DATA_TYPE_JSON)) {
    exBuf = indexPackJsonDataPrefix(term, &skip);
    pCt->colVal = exBuf;
  }
  char* key = indexCacheTermGet(pCt);

273
  // SSkipListIterator* iter = tSkipListCreateIter(mem->mem);
dengyihao's avatar
dengyihao 已提交
274 275 276 277 278 279 280
  SSkipListIterator* iter = tSkipListCreateIterFromVal(mem->mem, key, TSDB_DATA_TYPE_BINARY, TSDB_ORDER_ASC);
  while (tSkipListIterNext(iter)) {
    SSkipListNode* node = tSkipListIterGet(iter);
    if (node == NULL) {
      break;
    }
    CacheTerm* c = (CacheTerm*)SL_GET_NODE_DATA(node);
281 282 283
    // printf("json val: %s\n", c->colVal);
    if (0 != strncmp(c->colVal, pCt->colVal, skip)) {
      break;
284
    }
dengyihao's avatar
dengyihao 已提交
285 286
    char* p = taosMemoryCalloc(1, strlen(c->colVal) + 1);
    memcpy(p, c->colVal, strlen(c->colVal));
dengyihao's avatar
dengyihao 已提交
287

dengyihao's avatar
dengyihao 已提交
288
    TExeCond cond = cmpFn(p + skip, term->colVal, dType);
dengyihao's avatar
dengyihao 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301
    if (cond == MATCH) {
      if (c->operaType == ADD_VALUE) {
        INDEX_MERGE_ADD_DEL(tr->deled, tr->added, c->uid)
        // taosArrayPush(result, &c->uid);
        *s = kTypeValue;
      } else if (c->operaType == DEL_VALUE) {
        INDEX_MERGE_ADD_DEL(tr->added, tr->deled, c->uid)
      }
    } else if (cond == CONTINUE) {
      continue;
    } else if (cond == BREAK) {
      break;
    }
dengyihao's avatar
dengyihao 已提交
302
    taosMemoryFree(p);
dengyihao's avatar
dengyihao 已提交
303 304 305 306 307 308
  }

  taosMemoryFree(pCt);
  taosMemoryFree(exBuf);
  tSkipListDestroyIter(iter);

dengyihao's avatar
dengyihao 已提交
309 310 311
  return TSDB_CODE_SUCCESS;
}
static int32_t cacheSearchRange(void* cache, SIndexTerm* term, SIdxTempResult* tr, STermValueType* s) {
dengyihao's avatar
dengyihao 已提交
312 313 314
  // impl later
  return 0;
}
315
static IterateValue* indexCacheIteratorGetValue(Iterate* iter);
dengyihao's avatar
dengyihao 已提交
316

dengyihao's avatar
dengyihao 已提交
317
IndexCache* indexCacheCreate(SIndex* idx, uint64_t suid, const char* colName, int8_t type) {
wafwerar's avatar
wafwerar 已提交
318
  IndexCache* cache = taosMemoryCalloc(1, sizeof(IndexCache));
dengyihao's avatar
dengyihao 已提交
319 320 321
  if (cache == NULL) {
    indexError("failed to create index cache");
    return NULL;
dengyihao's avatar
dengyihao 已提交
322
  };
dengyihao's avatar
dengyihao 已提交
323

dengyihao's avatar
dengyihao 已提交
324
  cache->mem = indexInternalCacheCreate(type);
dengyihao's avatar
dengyihao 已提交
325
  cache->mem->pCache = cache;
dengyihao's avatar
dengyihao 已提交
326
  cache->colName = INDEX_TYPE_CONTAIN_EXTERN_TYPE(type, TSDB_DATA_TYPE_JSON) ? tstrdup(JSON_COLUMN) : tstrdup(colName);
dengyihao's avatar
dengyihao 已提交
327 328 329
  cache->type = type;
  cache->index = idx;
  cache->version = 0;
dengyihao's avatar
dengyihao 已提交
330
  cache->suid = suid;
dengyihao's avatar
dengyihao 已提交
331
  cache->occupiedMem = 0;
dengyihao's avatar
dengyihao 已提交
332

wafwerar's avatar
wafwerar 已提交
333 334
  taosThreadMutexInit(&cache->mtx, NULL);
  taosThreadCondInit(&cache->finished, NULL);
dengyihao's avatar
dengyihao 已提交
335

dengyihao's avatar
dengyihao 已提交
336
  indexCacheRef(cache);
dengyihao's avatar
dengyihao 已提交
337 338
  return cache;
}
339
void indexCacheDebug(IndexCache* cache) {
340 341
  MemTable* tbl = NULL;

wafwerar's avatar
wafwerar 已提交
342
  taosThreadMutexLock(&cache->mtx);
343 344
  tbl = cache->mem;
  indexMemRef(tbl);
wafwerar's avatar
wafwerar 已提交
345
  taosThreadMutexUnlock(&cache->mtx);
346

dengyihao's avatar
dengyihao 已提交
347 348 349 350 351 352 353 354 355 356
  {
    SSkipList*         slt = tbl->mem;
    SSkipListIterator* iter = tSkipListCreateIter(slt);
    while (tSkipListIterNext(iter)) {
      SSkipListNode* node = tSkipListIterGet(iter);
      CacheTerm*     ct = (CacheTerm*)SL_GET_NODE_DATA(node);
      if (ct != NULL) {
        // TODO, add more debug info
        indexInfo("{colVal: %s, version: %d} \t", ct->colVal, ct->version);
      }
357
    }
dengyihao's avatar
dengyihao 已提交
358 359 360
    tSkipListDestroyIter(iter);

    indexMemUnRef(tbl);
361
  }
362

dengyihao's avatar
dengyihao 已提交
363
  {
wafwerar's avatar
wafwerar 已提交
364
    taosThreadMutexLock(&cache->mtx);
dengyihao's avatar
dengyihao 已提交
365 366
    tbl = cache->imm;
    indexMemRef(tbl);
wafwerar's avatar
wafwerar 已提交
367
    taosThreadMutexUnlock(&cache->mtx);
dengyihao's avatar
dengyihao 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    if (tbl != NULL) {
      SSkipList*         slt = tbl->mem;
      SSkipListIterator* iter = tSkipListCreateIter(slt);
      while (tSkipListIterNext(iter)) {
        SSkipListNode* node = tSkipListIterGet(iter);
        CacheTerm*     ct = (CacheTerm*)SL_GET_NODE_DATA(node);
        if (ct != NULL) {
          // TODO, add more debug info
          indexInfo("{colVal: %s, version: %d} \t", ct->colVal, ct->version);
        }
      }
      tSkipListDestroyIter(iter);
    }

    indexMemUnRef(tbl);
  }
384
}
dengyihao's avatar
dengyihao 已提交
385

dengyihao's avatar
dengyihao 已提交
386 387 388 389 390
void indexCacheDestroySkiplist(SSkipList* slt) {
  SSkipListIterator* iter = tSkipListCreateIter(slt);
  while (tSkipListIterNext(iter)) {
    SSkipListNode* node = tSkipListIterGet(iter);
    CacheTerm*     ct = (CacheTerm*)SL_GET_NODE_DATA(node);
dengyihao's avatar
dengyihao 已提交
391
    if (ct != NULL) {
wafwerar's avatar
wafwerar 已提交
392 393
      taosMemoryFree(ct->colVal);
      taosMemoryFree(ct);
dengyihao's avatar
dengyihao 已提交
394
    }
dengyihao's avatar
dengyihao 已提交
395 396
  }
  tSkipListDestroyIter(iter);
397
  tSkipListDestroy(slt);
dengyihao's avatar
dengyihao 已提交
398
}
dengyihao's avatar
dengyihao 已提交
399
void indexCacheDestroyImm(IndexCache* cache) {
dengyihao's avatar
dengyihao 已提交
400 401 402
  if (cache == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
403

404
  MemTable* tbl = NULL;
wafwerar's avatar
wafwerar 已提交
405
  taosThreadMutexLock(&cache->mtx);
dengyihao's avatar
dengyihao 已提交
406

407
  tbl = cache->imm;
dengyihao's avatar
dengyihao 已提交
408
  cache->imm = NULL;  // or throw int bg thread
wafwerar's avatar
wafwerar 已提交
409
  taosThreadCondBroadcast(&cache->finished);
dengyihao's avatar
dengyihao 已提交
410

wafwerar's avatar
wafwerar 已提交
411
  taosThreadMutexUnlock(&cache->mtx);
dengyihao's avatar
dengyihao 已提交
412 413

  indexMemUnRef(tbl);
414
  indexMemUnRef(tbl);
dengyihao's avatar
dengyihao 已提交
415
}
dengyihao's avatar
dengyihao 已提交
416 417
void indexCacheDestroy(void* cache) {
  IndexCache* pCache = cache;
dengyihao's avatar
dengyihao 已提交
418 419 420
  if (pCache == NULL) {
    return;
  }
421 422
  indexMemUnRef(pCache->mem);
  indexMemUnRef(pCache->imm);
wafwerar's avatar
wafwerar 已提交
423
  taosMemoryFree(pCache->colName);
dengyihao's avatar
dengyihao 已提交
424

wafwerar's avatar
wafwerar 已提交
425 426
  taosThreadMutexDestroy(&pCache->mtx);
  taosThreadCondDestroy(&pCache->finished);
dengyihao's avatar
dengyihao 已提交
427

wafwerar's avatar
wafwerar 已提交
428
  taosMemoryFree(pCache);
dengyihao's avatar
dengyihao 已提交
429 430
}

dengyihao's avatar
dengyihao 已提交
431
Iterate* indexCacheIteratorCreate(IndexCache* cache) {
wafwerar's avatar
wafwerar 已提交
432
  Iterate* iiter = taosMemoryCalloc(1, sizeof(Iterate));
dengyihao's avatar
dengyihao 已提交
433 434 435
  if (iiter == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
436

wafwerar's avatar
wafwerar 已提交
437
  taosThreadMutexLock(&cache->mtx);
dengyihao's avatar
dengyihao 已提交
438 439

  indexMemRef(cache->imm);
dengyihao's avatar
dengyihao 已提交
440

441
  MemTable* tbl = cache->imm;
dengyihao's avatar
dengyihao 已提交
442
  iiter->val.val = taosArrayInit(1, sizeof(uint64_t));
443
  iiter->val.colVal = NULL;
444
  iiter->iter = tbl != NULL ? tSkipListCreateIter(tbl->mem) : NULL;
dengyihao's avatar
dengyihao 已提交
445 446 447
  iiter->next = indexCacheIteratorNext;
  iiter->getValue = indexCacheIteratorGetValue;

wafwerar's avatar
wafwerar 已提交
448
  taosThreadMutexUnlock(&cache->mtx);
dengyihao's avatar
dengyihao 已提交
449

dengyihao's avatar
dengyihao 已提交
450 451 452
  return iiter;
}
void indexCacheIteratorDestroy(Iterate* iter) {
dengyihao's avatar
dengyihao 已提交
453 454 455
  if (iter == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
456 457
  tSkipListDestroyIter(iter->iter);
  iterateValueDestroy(&iter->val, true);
wafwerar's avatar
wafwerar 已提交
458
  taosMemoryFree(iter);
dengyihao's avatar
dengyihao 已提交
459
}
dengyihao's avatar
dengyihao 已提交
460 461 462 463 464 465

int indexCacheSchedToMerge(IndexCache* pCache) {
  SSchedMsg schedMsg = {0};
  schedMsg.fp = doMergeWork;
  schedMsg.ahandle = pCache;
  schedMsg.thandle = NULL;
dengyihao's avatar
dengyihao 已提交
466 467
  // schedMsg.thandle = taosMemoryCalloc(1, sizeof(int64_t));
  // memcpy((char*)(schedMsg.thandle), (char*)&(pCache->index->refId), sizeof(int64_t));
dengyihao's avatar
dengyihao 已提交
468
  schedMsg.msg = NULL;
dengyihao's avatar
dengyihao 已提交
469
  indexAcquireRef(pCache->index->refId);
dengyihao's avatar
dengyihao 已提交
470
  taosScheduleTask(indexQhandle, &schedMsg);
471 472

  return 0;
dengyihao's avatar
dengyihao 已提交
473
}
474

dengyihao's avatar
dengyihao 已提交
475 476
static void indexCacheMakeRoomForWrite(IndexCache* cache) {
  while (true) {
dengyihao's avatar
dengyihao 已提交
477
    if (cache->occupiedMem * MEM_ESTIMATE_RADIO < MEM_THRESHOLD) {
dengyihao's avatar
dengyihao 已提交
478 479 480
      break;
    } else if (cache->imm != NULL) {
      // TODO: wake up by condition variable
wafwerar's avatar
wafwerar 已提交
481
      taosThreadCondWait(&cache->finished, &cache->mtx);
dengyihao's avatar
dengyihao 已提交
482
    } else {
dengyihao's avatar
dengyihao 已提交
483
      indexCacheRef(cache);
dengyihao's avatar
dengyihao 已提交
484 485
      cache->imm = cache->mem;
      cache->mem = indexInternalCacheCreate(cache->type);
dengyihao's avatar
dengyihao 已提交
486
      cache->mem->pCache = cache;
dengyihao's avatar
dengyihao 已提交
487
      cache->occupiedMem = 0;
dengyihao's avatar
dengyihao 已提交
488 489 490 491 492 493
      // sched to merge
      // unref cache in bgwork
      indexCacheSchedToMerge(cache);
    }
  }
}
dengyihao's avatar
dengyihao 已提交
494
int indexCachePut(void* cache, SIndexTerm* term, uint64_t uid) {
dengyihao's avatar
dengyihao 已提交
495 496 497
  if (cache == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
498
  bool hasJson = INDEX_TYPE_CONTAIN_EXTERN_TYPE(term->colType, TSDB_DATA_TYPE_JSON);
dengyihao's avatar
dengyihao 已提交
499

dengyihao's avatar
dengyihao 已提交
500
  IndexCache* pCache = cache;
dengyihao's avatar
dengyihao 已提交
501
  indexCacheRef(pCache);
dengyihao's avatar
dengyihao 已提交
502
  // encode data
wafwerar's avatar
wafwerar 已提交
503
  CacheTerm* ct = taosMemoryCalloc(1, sizeof(CacheTerm));
dengyihao's avatar
dengyihao 已提交
504 505 506
  if (cache == NULL) {
    return -1;
  }
507 508
  // set up key
  ct->colType = term->colType;
dengyihao's avatar
dengyihao 已提交
509
  if (hasJson) {
dengyihao's avatar
add UT  
dengyihao 已提交
510
    ct->colVal = indexPackJsonData(term);
dengyihao's avatar
dengyihao 已提交
511
  } else {
wafwerar's avatar
wafwerar 已提交
512
    ct->colVal = (char*)taosMemoryCalloc(1, sizeof(char) * (term->nColVal + 1));
dengyihao's avatar
dengyihao 已提交
513 514
    memcpy(ct->colVal, term->colVal, term->nColVal);
  }
dengyihao's avatar
dengyihao 已提交
515 516
  ct->version = atomic_add_fetch_32(&pCache->version, 1);
  // set value
517 518
  ct->uid = uid;
  ct->operaType = term->operType;
dengyihao's avatar
dengyihao 已提交
519
  // ugly code, refactor later
dengyihao's avatar
dengyihao 已提交
520
  int64_t estimate = sizeof(ct) + strlen(ct->colVal);
dengyihao's avatar
dengyihao 已提交
521

wafwerar's avatar
wafwerar 已提交
522
  taosThreadMutexLock(&pCache->mtx);
dengyihao's avatar
dengyihao 已提交
523
  pCache->occupiedMem += estimate;
dengyihao's avatar
dengyihao 已提交
524
  indexCacheMakeRoomForWrite(pCache);
525 526 527 528 529
  MemTable* tbl = pCache->mem;
  indexMemRef(tbl);
  tSkipListPut(tbl->mem, (char*)ct);
  indexMemUnRef(tbl);

wafwerar's avatar
wafwerar 已提交
530
  taosThreadMutexUnlock(&pCache->mtx);
dengyihao's avatar
dengyihao 已提交
531 532

  indexCacheUnRef(pCache);
dengyihao's avatar
dengyihao 已提交
533
  return 0;
dengyihao's avatar
dengyihao 已提交
534
  // encode end
dengyihao's avatar
dengyihao 已提交
535
}
dengyihao's avatar
dengyihao 已提交
536
int indexCacheDel(void* cache, const char* fieldValue, int32_t fvlen, uint64_t uid, int8_t operType) {
dengyihao's avatar
dengyihao 已提交
537
  IndexCache* pCache = cache;
dengyihao's avatar
dengyihao 已提交
538
  return 0;
dengyihao's avatar
dengyihao 已提交
539
}
540

dengyihao's avatar
dengyihao 已提交
541
static int32_t indexQueryMem(MemTable* mem, SIndexTermQuery* query, SIdxTempResult* tr, STermValueType* s) {
dengyihao's avatar
dengyihao 已提交
542 543 544
  if (mem == NULL) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
545 546 547 548 549 550 551 552 553

  SIndexTerm*     term = query->term;
  EIndexQueryType qtype = query->qType;

  if (INDEX_TYPE_CONTAIN_EXTERN_TYPE(term->colType, TSDB_DATA_TYPE_JSON)) {
    return cacheSearch[1][qtype](mem, term, tr, s);
  } else {
    return cacheSearch[0][qtype](mem, term, tr, s);
  }
dengyihao's avatar
dengyihao 已提交
554
}
dengyihao's avatar
dengyihao 已提交
555
int indexCacheSearch(void* cache, SIndexTermQuery* query, SIdxTempResult* result, STermValueType* s) {
dengyihao's avatar
add UT  
dengyihao 已提交
556
  int64_t st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
557 558 559
  if (cache == NULL) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
560 561 562
  IndexCache* pCache = cache;

  MemTable *mem = NULL, *imm = NULL;
wafwerar's avatar
wafwerar 已提交
563
  taosThreadMutexLock(&pCache->mtx);
dengyihao's avatar
dengyihao 已提交
564 565 566 567
  mem = pCache->mem;
  imm = pCache->imm;
  indexMemRef(mem);
  indexMemRef(imm);
wafwerar's avatar
wafwerar 已提交
568
  taosThreadMutexUnlock(&pCache->mtx);
dengyihao's avatar
dengyihao 已提交
569

dengyihao's avatar
dengyihao 已提交
570
  int ret = indexQueryMem(mem, query, result, s);
dengyihao's avatar
dengyihao 已提交
571 572
  if (ret == 0 && *s != kTypeDeletion) {
    // continue search in imm
dengyihao's avatar
dengyihao 已提交
573
    ret = indexQueryMem(imm, query, result, s);
dengyihao's avatar
dengyihao 已提交
574
  }
dengyihao's avatar
dengyihao 已提交
575

576 577
  indexMemUnRef(mem);
  indexMemUnRef(imm);
dengyihao's avatar
add UT  
dengyihao 已提交
578
  indexInfo("cache search, time cost %" PRIu64 "us", taosGetTimestampUs() - st);
dengyihao's avatar
dengyihao 已提交
579 580

  return ret;
dengyihao's avatar
dengyihao 已提交
581
}
dengyihao's avatar
dengyihao 已提交
582 583

void indexCacheRef(IndexCache* cache) {
dengyihao's avatar
dengyihao 已提交
584 585 586
  if (cache == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
587 588 589 590
  int ref = T_REF_INC(cache);
  UNUSED(ref);
}
void indexCacheUnRef(IndexCache* cache) {
dengyihao's avatar
dengyihao 已提交
591 592 593
  if (cache == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
594
  int ref = T_REF_DEC(cache);
dengyihao's avatar
dengyihao 已提交
595 596 597
  if (ref == 0) {
    indexCacheDestroy(cache);
  }
dengyihao's avatar
dengyihao 已提交
598
}
599 600

void indexMemRef(MemTable* tbl) {
dengyihao's avatar
dengyihao 已提交
601 602 603
  if (tbl == NULL) {
    return;
  }
604 605 606 607
  int ref = T_REF_INC(tbl);
  UNUSED(ref);
}
void indexMemUnRef(MemTable* tbl) {
dengyihao's avatar
dengyihao 已提交
608 609 610
  if (tbl == NULL) {
    return;
  }
611 612 613 614
  int ref = T_REF_DEC(tbl);
  if (ref == 0) {
    SSkipList* slt = tbl->mem;
    indexCacheDestroySkiplist(slt);
wafwerar's avatar
wafwerar 已提交
615
    taosMemoryFree(tbl);
616 617 618
  }
}

dengyihao's avatar
dengyihao 已提交
619
static void indexCacheTermDestroy(CacheTerm* ct) {
dengyihao's avatar
dengyihao 已提交
620 621 622
  if (ct == NULL) {
    return;
  }
wafwerar's avatar
wafwerar 已提交
623 624
  taosMemoryFree(ct->colVal);
  taosMemoryFree(ct);
625
}
dengyihao's avatar
dengyihao 已提交
626
static char* indexCacheTermGet(const void* pData) {
627 628 629
  CacheTerm* p = (CacheTerm*)pData;
  return (char*)p;
}
dengyihao's avatar
dengyihao 已提交
630
static int32_t indexCacheTermCompare(const void* l, const void* r) {
631 632 633
  CacheTerm* lt = (CacheTerm*)l;
  CacheTerm* rt = (CacheTerm*)r;
  // compare colVal
dengyihao's avatar
dengyihao 已提交
634
  int32_t cmp = strcmp(lt->colVal, rt->colVal);
dengyihao's avatar
dengyihao 已提交
635 636 637
  if (cmp == 0) {
    return rt->version - lt->version;
  }
dengyihao's avatar
dengyihao 已提交
638
  return cmp;
639 640
}

641 642 643 644 645 646 647
static int indexFindCh(char* a, char c) {
  char* p = a;
  while (*p != 0 && *p++ != c) {
  }
  return p - a;
}
static int indexCacheJsonTermCompareImpl(char* a, char* b) {
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
  // int alen = indexFindCh(a, '&');
  // int blen = indexFindCh(b, '&');

  // int cmp = strncmp(a, b, MIN(alen, blen));
  // if (cmp == 0) {
  //  cmp = alen - blen;
  //  if (cmp != 0) {
  //    return cmp;
  //  }
  //  cmp = *(a + alen) - *(b + blen);
  //  if (cmp != 0) {
  //    return cmp;
  //  }
  //  alen += 2;
  //  blen += 2;
  //  cmp = strcmp(a + alen, b + blen);
  //}
  return 0;
666 667 668 669 670
}
static int32_t indexCacheJsonTermCompare(const void* l, const void* r) {
  CacheTerm* lt = (CacheTerm*)l;
  CacheTerm* rt = (CacheTerm*)r;
  // compare colVal
671
  int32_t cmp = strcmp(lt->colVal, rt->colVal);
672 673 674 675 676
  if (cmp == 0) {
    return rt->version - lt->version;
  }
  return cmp;
}
677
static MemTable* indexInternalCacheCreate(int8_t type) {
678 679 680
  int ttype = INDEX_TYPE_CONTAIN_EXTERN_TYPE(type, TSDB_DATA_TYPE_JSON) ? TSDB_DATA_TYPE_BINARY : type;
  int32_t (*cmpFn)(const void* l, const void* r) =
      INDEX_TYPE_CONTAIN_EXTERN_TYPE(type, TSDB_DATA_TYPE_JSON) ? indexCacheJsonTermCompare : indexCacheTermCompare;
dengyihao's avatar
dengyihao 已提交
681

wafwerar's avatar
wafwerar 已提交
682
  MemTable* tbl = taosMemoryCalloc(1, sizeof(MemTable));
683
  indexMemRef(tbl);
684 685 686
  if (ttype == TSDB_DATA_TYPE_BINARY || ttype == TSDB_DATA_TYPE_NCHAR) {
    tbl->mem =
        tSkipListCreate(MAX_SKIP_LIST_LEVEL, ttype, MAX_INDEX_KEY_LEN, cmpFn, SL_ALLOW_DUP_KEY, indexCacheTermGet);
687 688 689 690 691 692 693
  }
  return tbl;
}

static void doMergeWork(SSchedMsg* msg) {
  IndexCache* pCache = msg->ahandle;
  SIndex*     sidx = (SIndex*)pCache->index;
dengyihao's avatar
dengyihao 已提交
694
  indexFlushCacheToTFile(sidx, pCache);
695 696 697
}
static bool indexCacheIteratorNext(Iterate* itera) {
  SSkipListIterator* iter = itera->iter;
dengyihao's avatar
dengyihao 已提交
698 699 700
  if (iter == NULL) {
    return false;
  }
dengyihao's avatar
dengyihao 已提交
701
  IterateValue* iv = &itera->val;
702 703 704 705 706 707 708 709
  iterateValueDestroy(iv, false);

  bool next = tSkipListIterNext(iter);
  if (next) {
    SSkipListNode* node = tSkipListIterGet(iter);
    CacheTerm*     ct = (CacheTerm*)SL_GET_NODE_DATA(node);

    iv->type = ct->operaType;
dengyihao's avatar
dengyihao 已提交
710
    iv->ver = ct->version;
dengyihao's avatar
dengyihao 已提交
711
    iv->colVal = tstrdup(ct->colVal);
712 713
    // printf("col Val: %s\n", iv->colVal);
    // iv->colType = cv->colType;
714 715 716 717 718 719

    taosArrayPush(iv->val, &ct->uid);
  }
  return next;
}

dengyihao's avatar
dengyihao 已提交
720 721 722 723
static IterateValue* indexCacheIteratorGetValue(Iterate* iter) {
  // opt later
  return &iter->val;
}