index.c 15.8 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
dengyihao's avatar
dengyihao 已提交
5 6
 * it under the terms of the GNU Affero General Public License, version 3 * or later ("AGPL"), as published by the Free
 * Software Foundation.
H
refact  
Hongze Cheng 已提交
7 8 9 10 11 12 13 14 15
 *
 * 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
#include "index.h"
dengyihao's avatar
dengyihao 已提交
17 18
#include "indexCache.h"
#include "indexComm.h"
dengyihao's avatar
dengyihao 已提交
19
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
20 21
#include "indexTfile.h"
#include "indexUtil.h"
dengyihao's avatar
dengyihao 已提交
22
#include "tdef.h"
dengyihao's avatar
dengyihao 已提交
23
#include "tsched.h"
H
refact  
Hongze Cheng 已提交
24

dengyihao's avatar
dengyihao 已提交
25 26
#ifdef USE_LUCENE
#include "lucene++/Lucene_c.h"
H
refact  
Hongze Cheng 已提交
27 28
#endif

dengyihao's avatar
dengyihao 已提交
29
#define INDEX_NUM_OF_THREADS 4
dengyihao's avatar
dengyihao 已提交
30
#define INDEX_QUEUE_SIZE 200
dengyihao's avatar
dengyihao 已提交
31 32 33

void* indexQhandle = NULL;

dengyihao's avatar
dengyihao 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#define INDEX_DATA_BOOL_NULL 0x02
#define INDEX_DATA_TINYINT_NULL 0x80
#define INDEX_DATA_SMALLINT_NULL 0x8000
#define INDEX_DATA_INT_NULL 0x80000000L
#define INDEX_DATA_BIGINT_NULL 0x8000000000000000L
#define INDEX_DATA_TIMESTAMP_NULL TSDB_DATA_BIGINT_NULL

#define INDEX_DATA_FLOAT_NULL 0x7FF00000            // it is an NAN
#define INDEX_DATA_DOUBLE_NULL 0x7FFFFF0000000000L  // an NAN
#define INDEX_DATA_NCHAR_NULL 0xFFFFFFFF
#define INDEX_DATA_BINARY_NULL 0xFF
#define INDEX_DATA_JSON_NULL 0xFFFFFFFF
#define INDEX_DATA_JSON_null 0xFFFFFFFE
#define INDEX_DATA_JSON_NOT_NULL 0x01

#define INDEX_DATA_UTINYINT_NULL 0xFF
#define INDEX_DATA_USMALLINT_NULL 0xFFFF
#define INDEX_DATA_UINT_NULL 0xFFFFFFFF
#define INDEX_DATA_UBIGINT_NULL 0xFFFFFFFFFFFFFFFFL

#define INDEX_DATA_NULL_STR "NULL"
#define INDEX_DATA_NULL_STR_L "null"

dengyihao's avatar
dengyihao 已提交
57 58
void indexInit() {
  // refactor later
dengyihao's avatar
dengyihao 已提交
59 60
  indexQhandle = taosInitScheduler(INDEX_QUEUE_SIZE, INDEX_NUM_OF_THREADS, "index");
}
dengyihao's avatar
dengyihao 已提交
61 62 63 64
void indexCleanUp() {
  // refacto later
  taosCleanUpScheduler(indexQhandle);
}
dengyihao's avatar
dengyihao 已提交
65

dengyihao's avatar
dengyihao 已提交
66
typedef struct SIdxColInfo {
67
  int colId;  // generated by index internal
dengyihao's avatar
dengyihao 已提交
68
  int cVersion;
69
} SIdxColInfo;
dengyihao's avatar
dengyihao 已提交
70

wafwerar's avatar
wafwerar 已提交
71
static TdThreadOnce isInit = PTHREAD_ONCE_INIT;
dengyihao's avatar
dengyihao 已提交
72
// static void           indexInit();
dengyihao's avatar
dengyihao 已提交
73
static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* term, SArray** result);
dengyihao's avatar
dengyihao 已提交
74

dengyihao's avatar
dengyihao 已提交
75 76
static void indexInterResultsDestroy(SArray* results);
static int  indexMergeFinalResults(SArray* interResults, EIndexOperatorType oType, SArray* finalResult);
dengyihao's avatar
dengyihao 已提交
77

dengyihao's avatar
dengyihao 已提交
78 79
static int indexGenTFile(SIndex* index, IndexCache* cache, SArray* batch);

dengyihao's avatar
dengyihao 已提交
80
// merge cache and tfile by opera type
dengyihao's avatar
dengyihao 已提交
81
static void indexMergeCacheAndTFile(SArray* result, IterateValue* icache, IterateValue* iTfv, SIdxTempResult* helper);
dengyihao's avatar
dengyihao 已提交
82

dengyihao's avatar
dengyihao 已提交
83 84
// static int32_t indexSerialTermKey(SIndexTerm* itm, char* buf);
// int32_t        indexSerialKey(ICacheKey* key, char* buf);
dengyihao's avatar
dengyihao 已提交
85

dengyihao's avatar
dengyihao 已提交
86
int indexOpen(SIndexOpts* opts, const char* path, SIndex** index) {
wafwerar's avatar
wafwerar 已提交
87
  taosThreadOnce(&isInit, indexInit);
wafwerar's avatar
wafwerar 已提交
88
  SIndex* sIdx = taosMemoryCalloc(1, sizeof(SIndex));
dengyihao's avatar
dengyihao 已提交
89 90 91
  if (sIdx == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
92

dengyihao's avatar
dengyihao 已提交
93 94
  // sIdx->cache = (void*)indexCacheCreate(sIdx);
  sIdx->tindex = indexTFileCreate(path);
dengyihao's avatar
dengyihao 已提交
95 96 97
  if (sIdx->tindex == NULL) {
    goto END;
  }
dengyihao's avatar
dengyihao 已提交
98

99 100
  sIdx->colObj = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  sIdx->cVersion = 1;
dengyihao's avatar
dengyihao 已提交
101
  sIdx->path = tstrdup(path);
wafwerar's avatar
wafwerar 已提交
102
  taosThreadMutexInit(&sIdx->mtx, NULL);
103 104
  *index = sIdx;
  return 0;
dengyihao's avatar
dengyihao 已提交
105

dengyihao's avatar
dengyihao 已提交
106
END:
dengyihao's avatar
dengyihao 已提交
107 108 109
  if (sIdx != NULL) {
    indexClose(sIdx);
  }
dengyihao's avatar
dengyihao 已提交
110 111 112

  *index = NULL;
  return -1;
H
refact  
Hongze Cheng 已提交
113
}
dengyihao's avatar
dengyihao 已提交
114

dengyihao's avatar
dengyihao 已提交
115
void indexClose(SIndex* sIdx) {
dengyihao's avatar
dengyihao 已提交
116 117 118
  void* iter = taosHashIterate(sIdx->colObj, NULL);
  while (iter) {
    IndexCache** pCache = iter;
dengyihao's avatar
dengyihao 已提交
119 120 121
    if (*pCache) {
      indexCacheUnRef(*pCache);
    }
dengyihao's avatar
dengyihao 已提交
122 123
    iter = taosHashIterate(sIdx->colObj, iter);
  }
124
  taosHashCleanup(sIdx->colObj);
wafwerar's avatar
wafwerar 已提交
125
  taosThreadMutexDestroy(&sIdx->mtx);
126
  indexTFileDestroy(sIdx->tindex);
wafwerar's avatar
wafwerar 已提交
127 128
  taosMemoryFree(sIdx->path);
  taosMemoryFree(sIdx);
dengyihao's avatar
dengyihao 已提交
129 130
  return;
}
dengyihao's avatar
dengyihao 已提交
131

dengyihao's avatar
dengyihao 已提交
132
int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) {
133
  // TODO(yihao): reduce the lock range
wafwerar's avatar
wafwerar 已提交
134
  taosThreadMutexLock(&index->mtx);
dengyihao's avatar
dengyihao 已提交
135
  for (int i = 0; i < taosArrayGetSize(fVals); i++) {
dengyihao's avatar
dengyihao 已提交
136 137
    SIndexTerm* p = taosArrayGetP(fVals, i);

dengyihao's avatar
dengyihao 已提交
138 139 140
    char      buf[128] = {0};
    ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName), .colType = p->colType};
    int32_t   sz = indexSerialCacheKey(&key, buf);
dengyihao's avatar
dengyihao 已提交
141 142

    IndexCache** cache = taosHashGet(index->colObj, buf, sz);
dengyihao's avatar
dengyihao 已提交
143
    if (cache == NULL) {
dengyihao's avatar
dengyihao 已提交
144 145
      IndexCache* pCache = indexCacheCreate(index, p->suid, p->colName, p->colType);
      taosHashPut(index->colObj, buf, sz, &pCache, sizeof(void*));
dengyihao's avatar
dengyihao 已提交
146
    }
147
  }
wafwerar's avatar
wafwerar 已提交
148
  taosThreadMutexUnlock(&index->mtx);
dengyihao's avatar
dengyihao 已提交
149 150

  for (int i = 0; i < taosArrayGetSize(fVals); i++) {
dengyihao's avatar
dengyihao 已提交
151 152
    SIndexTerm* p = taosArrayGetP(fVals, i);

dengyihao's avatar
dengyihao 已提交
153 154 155
    char      buf[128] = {0};
    ICacheKey key = {.suid = p->suid, .colName = p->colName, .nColName = strlen(p->colName), .colType = p->colType};
    int32_t   sz = indexSerialCacheKey(&key, buf);
dengyihao's avatar
dengyihao 已提交
156 157

    IndexCache** cache = taosHashGet(index->colObj, buf, sz);
dengyihao's avatar
dengyihao 已提交
158 159
    assert(*cache != NULL);
    int ret = indexCachePut(*cache, p, uid);
dengyihao's avatar
dengyihao 已提交
160 161 162
    if (ret != 0) {
      return ret;
    }
dengyihao's avatar
dengyihao 已提交
163
  }
dengyihao's avatar
dengyihao 已提交
164
  return 0;
dengyihao's avatar
dengyihao 已提交
165
}
dengyihao's avatar
dengyihao 已提交
166
int indexSearch(SIndex* index, SIndexMultiTermQuery* multiQuerys, SArray* result) {
167 168
  EIndexOperatorType opera = multiQuerys->opera;  // relation of querys

dengyihao's avatar
dengyihao 已提交
169
  SArray* iRslts = taosArrayInit(4, POINTER_BYTES);
170
  int     nQuery = taosArrayGetSize(multiQuerys->query);
dengyihao's avatar
dengyihao 已提交
171
  for (size_t i = 0; i < nQuery; i++) {
dengyihao's avatar
dengyihao 已提交
172 173 174 175
    SIndexTermQuery* qterm = taosArrayGet(multiQuerys->query, i);
    SArray*          trslt = NULL;
    indexTermSearch(index, qterm, &trslt);
    taosArrayPush(iRslts, (void*)&trslt);
176
  }
dengyihao's avatar
dengyihao 已提交
177 178
  indexMergeFinalResults(iRslts, opera, result);
  indexInterResultsDestroy(iRslts);
dengyihao's avatar
dengyihao 已提交
179
  return 0;
dengyihao's avatar
dengyihao 已提交
180 181
}

182 183
int indexDelete(SIndex* index, SIndexMultiTermQuery* query) { return 1; }
int indexRebuild(SIndex* index, SIndexOpts* opts) { return 0; }
dengyihao's avatar
dengyihao 已提交
184

185 186
SIndexOpts* indexOptsCreate() { return NULL; }
void        indexOptsDestroy(SIndexOpts* opts) { return; }
dengyihao's avatar
dengyihao 已提交
187 188 189 190
/*
 * @param: oper
 *
 */
dengyihao's avatar
dengyihao 已提交
191
SIndexMultiTermQuery* indexMultiTermQueryCreate(EIndexOperatorType opera) {
dengyihao's avatar
dengyihao 已提交
192 193
  SIndexMultiTermQuery* mtq = (SIndexMultiTermQuery*)taosMemoryMalloc(sizeof(SIndexMultiTermQuery));
  if (mtq == NULL) {
dengyihao's avatar
dengyihao 已提交
194 195
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
196 197 198
  mtq->opera = opera;
  mtq->query = taosArrayInit(4, sizeof(SIndexTermQuery));
  return mtq;
dengyihao's avatar
dengyihao 已提交
199
}
dengyihao's avatar
dengyihao 已提交
200
void indexMultiTermQueryDestroy(SIndexMultiTermQuery* pQuery) {
dengyihao's avatar
dengyihao 已提交
201
  for (int i = 0; i < taosArrayGetSize(pQuery->query); i++) {
dengyihao's avatar
dengyihao 已提交
202
    SIndexTermQuery* p = (SIndexTermQuery*)taosArrayGet(pQuery->query, i);
dengyihao's avatar
dengyihao 已提交
203
    indexTermDestroy(p->term);
dengyihao's avatar
dengyihao 已提交
204
  }
205
  taosArrayDestroy(pQuery->query);
wafwerar's avatar
wafwerar 已提交
206
  taosMemoryFree(pQuery);
dengyihao's avatar
dengyihao 已提交
207
};
dengyihao's avatar
dengyihao 已提交
208
int indexMultiTermQueryAdd(SIndexMultiTermQuery* pQuery, SIndexTerm* term, EIndexQueryType qType) {
209
  SIndexTermQuery q = {.qType = qType, .term = term};
dengyihao's avatar
dengyihao 已提交
210 211
  taosArrayPush(pQuery->query, &q);
  return 0;
dengyihao's avatar
dengyihao 已提交
212 213
}

dengyihao's avatar
dengyihao 已提交
214 215
SIndexTerm* indexTermCreate(int64_t suid, SIndexOperOnColumn oper, uint8_t colType, const char* colName,
                            int32_t nColName, const char* colVal, int32_t nColVal) {
dengyihao's avatar
dengyihao 已提交
216 217
  SIndexTerm* tm = (SIndexTerm*)taosMemoryCalloc(1, (sizeof(SIndexTerm)));
  if (tm == NULL) {
dengyihao's avatar
dengyihao 已提交
218 219
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
220

dengyihao's avatar
dengyihao 已提交
221 222 223
  tm->suid = suid;
  tm->operType = oper;
  tm->colType = colType;
dengyihao's avatar
dengyihao 已提交
224

dengyihao's avatar
dengyihao 已提交
225 226 227
  tm->colName = (char*)taosMemoryCalloc(1, nColName + 1);
  memcpy(tm->colName, colName, nColName);
  tm->nColName = nColName;
dengyihao's avatar
dengyihao 已提交
228

dengyihao's avatar
dengyihao 已提交
229 230 231 232 233
  tm->colVal = (char*)taosMemoryCalloc(1, nColVal + 1);
  memcpy(tm->colVal, colVal, nColVal);
  tm->nColVal = nColVal;

  return tm;
dengyihao's avatar
dengyihao 已提交
234
}
dengyihao's avatar
dengyihao 已提交
235
void indexTermDestroy(SIndexTerm* p) {
wafwerar's avatar
wafwerar 已提交
236 237 238
  taosMemoryFree(p->colName);
  taosMemoryFree(p->colVal);
  taosMemoryFree(p);
dengyihao's avatar
dengyihao 已提交
239 240
}

dengyihao's avatar
dengyihao 已提交
241
SIndexMultiTerm* indexMultiTermCreate() { return taosArrayInit(4, sizeof(SIndexTerm*)); }
242

dengyihao's avatar
dengyihao 已提交
243
int indexMultiTermAdd(SIndexMultiTerm* terms, SIndexTerm* term) {
244 245
  taosArrayPush(terms, &term);
  return 0;
dengyihao's avatar
dengyihao 已提交
246
}
dengyihao's avatar
dengyihao 已提交
247
void indexMultiTermDestroy(SIndexMultiTerm* terms) {
dengyihao's avatar
dengyihao 已提交
248
  for (int32_t i = 0; i < taosArrayGetSize(terms); i++) {
dengyihao's avatar
dengyihao 已提交
249
    SIndexTerm* p = taosArrayGetP(terms, i);
dengyihao's avatar
dengyihao 已提交
250 251
    indexTermDestroy(p);
  }
dengyihao's avatar
dengyihao 已提交
252
  taosArrayDestroy(terms);
dengyihao's avatar
dengyihao 已提交
253
}
dengyihao's avatar
dengyihao 已提交
254

dengyihao's avatar
dengyihao 已提交
255 256 257
static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* query, SArray** result) {
  SIndexTerm* term = query->term;
  const char* colName = term->colName;
258
  int32_t     nColName = term->nColName;
dengyihao's avatar
dengyihao 已提交
259

dengyihao's avatar
dengyihao 已提交
260 261
  // Get col info
  IndexCache* cache = NULL;
dengyihao's avatar
dengyihao 已提交
262 263

  char      buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
264 265 266
  ICacheKey key = {
      .suid = term->suid, .colName = term->colName, .nColName = strlen(term->colName), .colType = term->colType};
  int32_t sz = indexSerialCacheKey(&key, buf);
dengyihao's avatar
dengyihao 已提交
267

wafwerar's avatar
wafwerar 已提交
268
  taosThreadMutexLock(&sIdx->mtx);
dengyihao's avatar
dengyihao 已提交
269
  IndexCache** pCache = taosHashGet(sIdx->colObj, buf, sz);
dengyihao's avatar
dengyihao 已提交
270
  cache = (pCache == NULL) ? NULL : *pCache;
wafwerar's avatar
wafwerar 已提交
271
  taosThreadMutexUnlock(&sIdx->mtx);
272

dengyihao's avatar
dengyihao 已提交
273
  *result = taosArrayInit(4, sizeof(uint64_t));
274
  // TODO: iterator mem and tidex
dengyihao's avatar
dengyihao 已提交
275
  STermValueType s = kTypeValue;
dengyihao's avatar
dengyihao 已提交
276

dengyihao's avatar
add UT  
dengyihao 已提交
277 278
  int64_t st = taosGetTimestampUs();

dengyihao's avatar
dengyihao 已提交
279 280
  SIdxTempResult* tr = sIdxTempResultCreate();
  if (0 == indexCacheSearch(cache, query, tr, &s)) {
dengyihao's avatar
dengyihao 已提交
281
    if (s == kTypeDeletion) {
dengyihao's avatar
dengyihao 已提交
282
      indexInfo("col: %s already drop by", term->colName);
283
      // coloum already drop by other oper, no need to query tindex
dengyihao's avatar
dengyihao 已提交
284 285
      return 0;
    } else {
dengyihao's avatar
add UT  
dengyihao 已提交
286
      st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
287
      if (0 != indexTFileSearch(sIdx->tindex, query, tr)) {
288
        indexError("corrupt at index(TFile) col:%s val: %s", term->colName, term->colVal);
dengyihao's avatar
dengyihao 已提交
289
        goto END;
290
      }
dengyihao's avatar
add UT  
dengyihao 已提交
291 292
      int64_t tfCost = taosGetTimestampUs() - st;
      indexInfo("tfile search cost: %" PRIu64 "us", tfCost);
dengyihao's avatar
dengyihao 已提交
293 294 295
    }
  } else {
    indexError("corrupt at index(cache) col:%s val: %s", term->colName, term->colVal);
dengyihao's avatar
dengyihao 已提交
296
    goto END;
dengyihao's avatar
dengyihao 已提交
297
  }
dengyihao's avatar
add UT  
dengyihao 已提交
298 299
  int64_t cost = taosGetTimestampUs() - st;
  indexInfo("search cost: %" PRIu64 "us", cost);
dengyihao's avatar
dengyihao 已提交
300 301

  sIdxTempResultMergeTo(*result, tr);
dengyihao's avatar
add UT  
dengyihao 已提交
302

dengyihao's avatar
dengyihao 已提交
303
  sIdxTempResultDestroy(tr);
dengyihao's avatar
dengyihao 已提交
304
  return 0;
dengyihao's avatar
dengyihao 已提交
305 306 307
END:
  sIdxTempResultDestroy(tr);
  return -1;
dengyihao's avatar
dengyihao 已提交
308
}
dengyihao's avatar
dengyihao 已提交
309
static void indexInterResultsDestroy(SArray* results) {
dengyihao's avatar
dengyihao 已提交
310 311 312
  if (results == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
313 314 315

  size_t sz = taosArrayGetSize(results);
  for (size_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
316
    SArray* p = taosArrayGetP(results, i);
317 318
    taosArrayDestroy(p);
  }
dengyihao's avatar
dengyihao 已提交
319 320
  taosArrayDestroy(results);
}
dengyihao's avatar
dengyihao 已提交
321

dengyihao's avatar
dengyihao 已提交
322
static int indexMergeFinalResults(SArray* interResults, EIndexOperatorType oType, SArray* fResults) {
323
  // refactor, merge interResults into fResults by oType
dengyihao's avatar
dengyihao 已提交
324 325 326 327 328
  for (int i = 0; i < taosArrayGetSize(interResults); i--) {
    SArray* t = taosArrayGetP(interResults, i);
    taosArraySort(t, uidCompare);
    taosArrayRemoveDuplicate(t, uidCompare, NULL);
  }
dengyihao's avatar
dengyihao 已提交
329

dengyihao's avatar
dengyihao 已提交
330
  if (oType == MUST) {
dengyihao's avatar
dengyihao 已提交
331
    iIntersection(interResults, fResults);
dengyihao's avatar
dengyihao 已提交
332
  } else if (oType == SHOULD) {
dengyihao's avatar
dengyihao 已提交
333
    iUnion(interResults, fResults);
dengyihao's avatar
dengyihao 已提交
334
  } else if (oType == NOT) {
335
    // just one column index, enhance later
dengyihao's avatar
dengyihao 已提交
336
    taosArrayAddAll(fResults, interResults);
337
    // not use currently
dengyihao's avatar
dengyihao 已提交
338 339 340
  }
  return 0;
}
dengyihao's avatar
dengyihao 已提交
341

dengyihao's avatar
dengyihao 已提交
342
static void indexMayMergeTempToFinalResult(SArray* result, TFileValue* tfv, SIdxTempResult* tr) {
343 344 345 346
  int32_t sz = taosArrayGetSize(result);
  if (sz > 0) {
    TFileValue* lv = taosArrayGetP(result, sz - 1);
    if (tfv != NULL && strcmp(lv->colVal, tfv->colVal) != 0) {
dengyihao's avatar
dengyihao 已提交
347 348
      sIdxTempResultMergeTo(lv->tableId, tr);
      sIdxTempResultClear(tr);
349 350 351

      taosArrayPush(result, &tfv);
    } else if (tfv == NULL) {
dengyihao's avatar
dengyihao 已提交
352 353
      // handle last iterator
      sIdxTempResultMergeTo(lv->tableId, tr);
354
    } else {
dengyihao's avatar
dengyihao 已提交
355
      // temp result saved in help
356 357 358 359 360 361
      tfileValueDestroy(tfv);
    }
  } else {
    taosArrayPush(result, &tfv);
  }
}
dengyihao's avatar
dengyihao 已提交
362
static void indexMergeCacheAndTFile(SArray* result, IterateValue* cv, IterateValue* tv, SIdxTempResult* tr) {
363
  char*       colVal = (cv != NULL) ? cv->colVal : tv->colVal;
dengyihao's avatar
dengyihao 已提交
364
  TFileValue* tfv = tfileValueCreate(colVal);
365

dengyihao's avatar
dengyihao 已提交
366
  indexMayMergeTempToFinalResult(result, tfv, tr);
367

dengyihao's avatar
dengyihao 已提交
368
  if (cv != NULL) {
369
    uint64_t id = *(uint64_t*)taosArrayGet(cv->val, 0);
dengyihao's avatar
dengyihao 已提交
370
    uint32_t ver = cv->ver;
dengyihao's avatar
dengyihao 已提交
371
    if (cv->type == ADD_VALUE) {
dengyihao's avatar
dengyihao 已提交
372
      INDEX_MERGE_ADD_DEL(tr->deled, tr->added, id)
dengyihao's avatar
dengyihao 已提交
373
    } else if (cv->type == DEL_VALUE) {
dengyihao's avatar
dengyihao 已提交
374
      INDEX_MERGE_ADD_DEL(tr->added, tr->deled, id)
dengyihao's avatar
dengyihao 已提交
375 376 377
    }
  }
  if (tv != NULL) {
dengyihao's avatar
dengyihao 已提交
378
    taosArrayAddAll(tr->total, tv->val);
dengyihao's avatar
dengyihao 已提交
379 380
  }
}
dengyihao's avatar
dengyihao 已提交
381
static void indexDestroyFinalResult(SArray* result) {
dengyihao's avatar
dengyihao 已提交
382 383 384 385 386 387 388
  int32_t sz = result ? taosArrayGetSize(result) : 0;
  for (size_t i = 0; i < sz; i++) {
    TFileValue* tv = taosArrayGetP(result, i);
    tfileValueDestroy(tv);
  }
  taosArrayDestroy(result);
}
389

dengyihao's avatar
dengyihao 已提交
390
int indexFlushCacheToTFile(SIndex* sIdx, void* cache) {
dengyihao's avatar
dengyihao 已提交
391 392 393
  if (sIdx == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
394
  indexInfo("suid %" PRIu64 " merge cache into tindex", sIdx->suid);
dengyihao's avatar
dengyihao 已提交
395

dengyihao's avatar
dengyihao 已提交
396 397
  int64_t st = taosGetTimestampUs();

dengyihao's avatar
dengyihao 已提交
398
  IndexCache*  pCache = (IndexCache*)cache;
dengyihao's avatar
dengyihao 已提交
399
  TFileReader* pReader = tfileGetReaderByCol(sIdx->tindex, pCache->suid, pCache->colName);
dengyihao's avatar
dengyihao 已提交
400 401 402
  if (pReader == NULL) {
    indexWarn("empty tfile reader found");
  }
dengyihao's avatar
dengyihao 已提交
403 404 405
  // handle flush
  Iterate* cacheIter = indexCacheIteratorCreate(pCache);
  Iterate* tfileIter = tfileIteratorCreate(pReader);
dengyihao's avatar
dengyihao 已提交
406 407 408
  if (tfileIter == NULL) {
    indexWarn("empty tfile reader iterator");
  }
dengyihao's avatar
dengyihao 已提交
409 410 411

  SArray* result = taosArrayInit(1024, sizeof(void*));

dengyihao's avatar
dengyihao 已提交
412 413
  bool cn = cacheIter ? cacheIter->next(cacheIter) : false;
  bool tn = tfileIter ? tfileIter->next(tfileIter) : false;
414

dengyihao's avatar
dengyihao 已提交
415
  SIdxTempResult* tr = sIdxTempResultCreate();
dengyihao's avatar
dengyihao 已提交
416 417 418 419 420 421 422 423 424 425 426 427
  while (cn == true || tn == true) {
    IterateValue* cv = (cn == true) ? cacheIter->getValue(cacheIter) : NULL;
    IterateValue* tv = (tn == true) ? tfileIter->getValue(tfileIter) : NULL;

    int comp = 0;
    if (cn == true && tn == true) {
      comp = strcmp(cv->colVal, tv->colVal);
    } else if (cn == true) {
      comp = -1;
    } else {
      comp = 1;
    }
dengyihao's avatar
dengyihao 已提交
428
    if (comp == 0) {
dengyihao's avatar
dengyihao 已提交
429
      indexMergeCacheAndTFile(result, cv, tv, tr);
dengyihao's avatar
dengyihao 已提交
430 431 432
      cn = cacheIter->next(cacheIter);
      tn = tfileIter->next(tfileIter);
    } else if (comp < 0) {
dengyihao's avatar
dengyihao 已提交
433
      indexMergeCacheAndTFile(result, cv, NULL, tr);
dengyihao's avatar
dengyihao 已提交
434 435
      cn = cacheIter->next(cacheIter);
    } else {
dengyihao's avatar
dengyihao 已提交
436
      indexMergeCacheAndTFile(result, NULL, tv, tr);
dengyihao's avatar
dengyihao 已提交
437 438 439
      tn = tfileIter->next(tfileIter);
    }
  }
dengyihao's avatar
dengyihao 已提交
440 441
  indexMayMergeTempToFinalResult(result, NULL, tr);
  sIdxTempResultDestroy(tr);
442

dengyihao's avatar
dengyihao 已提交
443
  int ret = indexGenTFile(sIdx, pCache, result);
dengyihao's avatar
dengyihao 已提交
444
  indexDestroyFinalResult(result);
dengyihao's avatar
dengyihao 已提交
445

dengyihao's avatar
dengyihao 已提交
446
  indexCacheDestroyImm(pCache);
dengyihao's avatar
dengyihao 已提交
447 448 449 450

  indexCacheIteratorDestroy(cacheIter);
  tfileIteratorDestroy(tfileIter);

dengyihao's avatar
dengyihao 已提交
451 452
  tfileReaderUnRef(pReader);
  indexCacheUnRef(pCache);
dengyihao's avatar
dengyihao 已提交
453 454 455 456 457 458 459 460

  int64_t cost = taosGetTimestampUs() - st;
  if (ret != 0) {
    indexError("failed to merge, time cost: %" PRId64 "ms", cost / 1000);
  } else {
    indexInfo("success to merge , time cost: %" PRId64 "ms", cost / 1000);
  }
  return ret;
dengyihao's avatar
dengyihao 已提交
461
}
dengyihao's avatar
dengyihao 已提交
462 463 464
void iterateValueDestroy(IterateValue* value, bool destroy) {
  if (destroy) {
    taosArrayDestroy(value->val);
dengyihao's avatar
dengyihao 已提交
465
    value->val = NULL;
dengyihao's avatar
dengyihao 已提交
466
  } else {
dengyihao's avatar
dengyihao 已提交
467 468 469
    if (value->val != NULL) {
      taosArrayClear(value->val);
    }
dengyihao's avatar
dengyihao 已提交
470
  }
wafwerar's avatar
wafwerar 已提交
471
  taosMemoryFree(value->colVal);
dengyihao's avatar
dengyihao 已提交
472 473
  value->colVal = NULL;
}
dengyihao's avatar
dengyihao 已提交
474 475 476 477
static int indexGenTFile(SIndex* sIdx, IndexCache* cache, SArray* batch) {
  int32_t version = CACHE_VERSION(cache);
  uint8_t colType = cache->type;

dengyihao's avatar
dengyihao 已提交
478
  TFileWriter* tw = tfileWriterOpen(sIdx->path, cache->suid, version, cache->colName, colType);
dengyihao's avatar
dengyihao 已提交
479 480 481 482 483 484 485 486 487 488 489 490
  if (tw == NULL) {
    indexError("failed to open file to write");
    return -1;
  }

  int ret = tfileWriterPut(tw, batch, true);
  if (ret != 0) {
    indexError("failed to write into tindex ");
    goto END;
  }
  tfileWriterClose(tw);

dengyihao's avatar
dengyihao 已提交
491
  TFileReader* reader = tfileReaderOpen(sIdx->path, cache->suid, version, cache->colName);
dengyihao's avatar
dengyihao 已提交
492 493 494
  if (reader == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
495 496

  TFileHeader* header = &reader->header;
dengyihao's avatar
dengyihao 已提交
497
  ICacheKey    key = {.suid = cache->suid, .colName = header->colName, .nColName = strlen(header->colName)};
dengyihao's avatar
dengyihao 已提交
498

wafwerar's avatar
wafwerar 已提交
499
  taosThreadMutexLock(&sIdx->mtx);
dengyihao's avatar
dengyihao 已提交
500 501
  IndexTFile* ifile = (IndexTFile*)sIdx->tindex;
  tfileCachePut(ifile->cache, &key, reader);
wafwerar's avatar
wafwerar 已提交
502
  taosThreadMutexUnlock(&sIdx->mtx);
dengyihao's avatar
dengyihao 已提交
503 504
  return ret;
END:
dengyihao's avatar
dengyihao 已提交
505 506
  if (tw != NULL) {
    writerCtxDestroy(tw->ctx, true);
wafwerar's avatar
wafwerar 已提交
507
    taosMemoryFree(tw);
dengyihao's avatar
dengyihao 已提交
508
  }
dengyihao's avatar
dengyihao 已提交
509
  return -1;
dengyihao's avatar
dengyihao 已提交
510
}
dengyihao's avatar
dengyihao 已提交
511

dengyihao's avatar
dengyihao 已提交
512 513
int32_t indexSerialCacheKey(ICacheKey* key, char* buf) {
  bool hasJson = INDEX_TYPE_CONTAIN_EXTERN_TYPE(key->colType, TSDB_DATA_TYPE_JSON);
dengyihao's avatar
dengyihao 已提交
514

dengyihao's avatar
dengyihao 已提交
515 516 517 518 519
  char* p = buf;
  SERIALIZE_MEM_TO_BUF(buf, key, suid);
  SERIALIZE_VAR_TO_BUF(buf, '_', char);
  // SERIALIZE_MEM_TO_BUF(buf, key, colType);
  // SERIALIZE_VAR_TO_BUF(buf, '_', char);
dengyihao's avatar
dengyihao 已提交
520 521 522 523 524
  if (hasJson) {
    SERIALIZE_STR_VAR_TO_BUF(buf, JSON_COLUMN, strlen(JSON_COLUMN));
  } else {
    SERIALIZE_STR_MEM_TO_BUF(buf, key, colName, key->nColName);
  }
dengyihao's avatar
dengyihao 已提交
525 526
  return buf - p;
}