index.c 16.9 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
void indexInit() {
  // refactor later
dengyihao's avatar
dengyihao 已提交
36 37
  indexQhandle = taosInitScheduler(INDEX_QUEUE_SIZE, INDEX_NUM_OF_THREADS, "index");
}
dengyihao's avatar
dengyihao 已提交
38 39 40 41
void indexCleanUp() {
  // refacto later
  taosCleanUpScheduler(indexQhandle);
}
dengyihao's avatar
dengyihao 已提交
42

dengyihao's avatar
dengyihao 已提交
43
typedef struct SIdxColInfo {
44
  int colId;  // generated by index internal
dengyihao's avatar
dengyihao 已提交
45
  int cVersion;
46
} SIdxColInfo;
dengyihao's avatar
dengyihao 已提交
47

wafwerar's avatar
wafwerar 已提交
48
static TdThreadOnce isInit = PTHREAD_ONCE_INIT;
dengyihao's avatar
dengyihao 已提交
49
// static void           indexInit();
dengyihao's avatar
dengyihao 已提交
50
static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* term, SArray** result);
dengyihao's avatar
dengyihao 已提交
51

dengyihao's avatar
dengyihao 已提交
52 53
static void indexInterResultsDestroy(SArray* results);
static int  indexMergeFinalResults(SArray* interResults, EIndexOperatorType oType, SArray* finalResult);
dengyihao's avatar
dengyihao 已提交
54

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

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

dengyihao's avatar
dengyihao 已提交
60 61
// static int32_t indexSerialTermKey(SIndexTerm* itm, char* buf);
// int32_t        indexSerialKey(ICacheKey* key, char* buf);
dengyihao's avatar
dengyihao 已提交
62

dengyihao's avatar
dengyihao 已提交
63
int indexOpen(SIndexOpts* opts, const char* path, SIndex** index) {
wafwerar's avatar
wafwerar 已提交
64
  taosThreadOnce(&isInit, indexInit);
wafwerar's avatar
wafwerar 已提交
65
  SIndex* sIdx = taosMemoryCalloc(1, sizeof(SIndex));
dengyihao's avatar
dengyihao 已提交
66 67 68
  if (sIdx == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
69

70
#ifdef USE_LUCENE
dengyihao's avatar
dengyihao 已提交
71
  index_t* index = index_open(path);
dengyihao's avatar
dengyihao 已提交
72
  sIdx->index = index;
dengyihao's avatar
dengyihao 已提交
73
#endif
dengyihao's avatar
dengyihao 已提交
74

dengyihao's avatar
dengyihao 已提交
75
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
76 77
  // sIdx->cache = (void*)indexCacheCreate(sIdx);
  sIdx->tindex = indexTFileCreate(path);
dengyihao's avatar
dengyihao 已提交
78 79 80
  if (sIdx->tindex == NULL) {
    goto END;
  }
dengyihao's avatar
dengyihao 已提交
81

82 83
  sIdx->colObj = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  sIdx->cVersion = 1;
dengyihao's avatar
dengyihao 已提交
84
  sIdx->path = tstrdup(path);
wafwerar's avatar
wafwerar 已提交
85
  taosThreadMutexInit(&sIdx->mtx, NULL);
86 87
  *index = sIdx;
  return 0;
dengyihao's avatar
dengyihao 已提交
88
#endif
dengyihao's avatar
dengyihao 已提交
89

dengyihao's avatar
dengyihao 已提交
90
END:
dengyihao's avatar
dengyihao 已提交
91 92 93
  if (sIdx != NULL) {
    indexClose(sIdx);
  }
dengyihao's avatar
dengyihao 已提交
94 95 96

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

dengyihao's avatar
dengyihao 已提交
99
void indexClose(SIndex* sIdx) {
100 101
#ifdef USE_LUCENE
  index_close(sIdex->index);
dengyihao's avatar
dengyihao 已提交
102
  sIdx->index = NULL;
H
refact  
Hongze Cheng 已提交
103
#endif
dengyihao's avatar
dengyihao 已提交
104

dengyihao's avatar
dengyihao 已提交
105
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
106 107 108
  void* iter = taosHashIterate(sIdx->colObj, NULL);
  while (iter) {
    IndexCache** pCache = iter;
dengyihao's avatar
dengyihao 已提交
109 110 111
    if (*pCache) {
      indexCacheUnRef(*pCache);
    }
dengyihao's avatar
dengyihao 已提交
112 113
    iter = taosHashIterate(sIdx->colObj, iter);
  }
114
  taosHashCleanup(sIdx->colObj);
wafwerar's avatar
wafwerar 已提交
115
  taosThreadMutexDestroy(&sIdx->mtx);
116
  indexTFileDestroy(sIdx->tindex);
dengyihao's avatar
dengyihao 已提交
117
#endif
wafwerar's avatar
wafwerar 已提交
118 119
  taosMemoryFree(sIdx->path);
  taosMemoryFree(sIdx);
dengyihao's avatar
dengyihao 已提交
120 121
  return;
}
dengyihao's avatar
dengyihao 已提交
122

dengyihao's avatar
dengyihao 已提交
123
int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) {
124
#ifdef USE_LUCENE
dengyihao's avatar
dengyihao 已提交
125
  index_document_t* doc = index_document_create();
126 127 128

  char buf[16] = {0};
  sprintf(buf, "%d", uid);
dengyihao's avatar
dengyihao 已提交
129

130
  for (int i = 0; i < taosArrayGetSize(fVals); i++) {
dengyihao's avatar
dengyihao 已提交
131 132
    SIndexTerm* p = taosArrayGetP(fVals, i);
    index_document_add(doc, (const char*)(p->key), p->nKey, (const char*)(p->val), p->nVal, 1);
133 134 135 136 137
  }
  index_document_add(doc, NULL, 0, buf, strlen(buf), 0);

  index_put(index->index, doc);
  index_document_destroy(doc);
dengyihao's avatar
dengyihao 已提交
138
#endif
dengyihao's avatar
dengyihao 已提交
139

dengyihao's avatar
dengyihao 已提交
140
#ifdef USE_INVERTED_INDEX
141 142

  // TODO(yihao): reduce the lock range
wafwerar's avatar
wafwerar 已提交
143
  taosThreadMutexLock(&index->mtx);
dengyihao's avatar
dengyihao 已提交
144
  for (int i = 0; i < taosArrayGetSize(fVals); i++) {
dengyihao's avatar
dengyihao 已提交
145 146
    SIndexTerm* p = taosArrayGetP(fVals, i);

dengyihao's avatar
dengyihao 已提交
147 148 149
    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 已提交
150 151

    IndexCache** cache = taosHashGet(index->colObj, buf, sz);
dengyihao's avatar
dengyihao 已提交
152
    if (cache == NULL) {
dengyihao's avatar
dengyihao 已提交
153 154
      IndexCache* pCache = indexCacheCreate(index, p->suid, p->colName, p->colType);
      taosHashPut(index->colObj, buf, sz, &pCache, sizeof(void*));
dengyihao's avatar
dengyihao 已提交
155
    }
156
  }
wafwerar's avatar
wafwerar 已提交
157
  taosThreadMutexUnlock(&index->mtx);
dengyihao's avatar
dengyihao 已提交
158 159

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

dengyihao's avatar
dengyihao 已提交
162 163 164
    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 已提交
165 166

    IndexCache** cache = taosHashGet(index->colObj, buf, sz);
dengyihao's avatar
dengyihao 已提交
167 168
    assert(*cache != NULL);
    int ret = indexCachePut(*cache, p, uid);
dengyihao's avatar
dengyihao 已提交
169 170 171
    if (ret != 0) {
      return ret;
    }
dengyihao's avatar
dengyihao 已提交
172
  }
dengyihao's avatar
dengyihao 已提交
173

dengyihao's avatar
dengyihao 已提交
174
#endif
dengyihao's avatar
dengyihao 已提交
175
  return 0;
dengyihao's avatar
dengyihao 已提交
176
}
dengyihao's avatar
dengyihao 已提交
177
int indexSearch(SIndex* index, SIndexMultiTermQuery* multiQuerys, SArray* result) {
178 179
#ifdef USE_LUCENE
  EIndexOperatorType opera = multiQuerys->opera;
dengyihao's avatar
dengyihao 已提交
180

181
  int    nQuery = taosArrayGetSize(multiQuerys->query);
wafwerar's avatar
wafwerar 已提交
182 183 184
  char** fields = taosMemoryMalloc(sizeof(char*) * nQuery);
  char** keys = taosMemoryMalloc(sizeof(char*) * nQuery);
  int*   types = taosMemoryMalloc(sizeof(int) * nQuery);
dengyihao's avatar
dengyihao 已提交
185 186

  for (int i = 0; i < nQuery; i++) {
dengyihao's avatar
dengyihao 已提交
187 188
    SIndexTermQuery* p = taosArrayGet(multiQuerys->query, i);
    SIndexTerm*      term = p->field_value;
189

wafwerar's avatar
wafwerar 已提交
190 191
    fields[i] = taosMemoryCalloc(1, term->nKey + 1);
    keys[i] = taosMemoryCalloc(1, term->nVal + 1);
192 193 194 195 196

    memcpy(fields[i], term->key, term->nKey);
    memcpy(keys[i], term->val, term->nVal);
    types[i] = (int)(p->type);
  }
dengyihao's avatar
dengyihao 已提交
197
  int* tResult = NULL;
198
  int  tsz = 0;
dengyihao's avatar
dengyihao 已提交
199
  index_multi_search(index->index, (const char**)fields, (const char**)keys, types, nQuery, opera, &tResult, &tsz);
200

dengyihao's avatar
dengyihao 已提交
201 202 203
  for (int i = 0; i < tsz; i++) {
    taosArrayPush(result, &tResult[i]);
  }
dengyihao's avatar
dengyihao 已提交
204 205

  for (int i = 0; i < nQuery; i++) {
wafwerar's avatar
wafwerar 已提交
206 207
    taosMemoryFree(fields[i]);
    taosMemoryFree(keys[i]);
dengyihao's avatar
dengyihao 已提交
208
  }
wafwerar's avatar
wafwerar 已提交
209 210 211
  taosMemoryFree(fields);
  taosMemoryFree(keys);
  taosMemoryFree(types);
dengyihao's avatar
dengyihao 已提交
212 213
#endif

dengyihao's avatar
dengyihao 已提交
214
#ifdef USE_INVERTED_INDEX
215 216
  EIndexOperatorType opera = multiQuerys->opera;  // relation of querys

dengyihao's avatar
dengyihao 已提交
217
  SArray* interResults = taosArrayInit(4, POINTER_BYTES);
218
  int     nQuery = taosArrayGetSize(multiQuerys->query);
dengyihao's avatar
dengyihao 已提交
219
  for (size_t i = 0; i < nQuery; i++) {
dengyihao's avatar
dengyihao 已提交
220 221
    SIndexTermQuery* qTerm = taosArrayGet(multiQuerys->query, i);
    SArray*          tResult = NULL;
222
    indexTermSearch(index, qTerm, &tResult);
dengyihao's avatar
dengyihao 已提交
223
    taosArrayPush(interResults, (void*)&tResult);
224
  }
dengyihao's avatar
dengyihao 已提交
225 226
  indexMergeFinalResults(interResults, opera, result);
  indexInterResultsDestroy(interResults);
227

dengyihao's avatar
dengyihao 已提交
228
#endif
dengyihao's avatar
dengyihao 已提交
229
  return 0;
dengyihao's avatar
dengyihao 已提交
230 231
}

dengyihao's avatar
dengyihao 已提交
232
int indexDelete(SIndex* index, SIndexMultiTermQuery* query) {
dengyihao's avatar
dengyihao 已提交
233
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
234

dengyihao's avatar
dengyihao 已提交
235
#endif
236

dengyihao's avatar
dengyihao 已提交
237 238
  return 1;
}
dengyihao's avatar
dengyihao 已提交
239
int indexRebuild(SIndex* index, SIndexOpts* opts) {
dengyihao's avatar
dengyihao 已提交
240
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
241 242
#endif

dengyihao's avatar
dengyihao 已提交
243
  return 0;
dengyihao's avatar
dengyihao 已提交
244
}
dengyihao's avatar
dengyihao 已提交
245

dengyihao's avatar
dengyihao 已提交
246
SIndexOpts* indexOptsCreate() {
247
#ifdef USE_LUCENE
dengyihao's avatar
dengyihao 已提交
248
#endif
249
  return NULL;
dengyihao's avatar
dengyihao 已提交
250
}
dengyihao's avatar
dengyihao 已提交
251
void indexOptsDestroy(SIndexOpts* opts) {
252
#ifdef USE_LUCENE
dengyihao's avatar
dengyihao 已提交
253
#endif
dengyihao's avatar
dengyihao 已提交
254 255 256 257 258 259
  return;
}
/*
 * @param: oper
 *
 */
dengyihao's avatar
dengyihao 已提交
260
SIndexMultiTermQuery* indexMultiTermQueryCreate(EIndexOperatorType opera) {
wafwerar's avatar
wafwerar 已提交
261
  SIndexMultiTermQuery* p = (SIndexMultiTermQuery*)taosMemoryMalloc(sizeof(SIndexMultiTermQuery));
dengyihao's avatar
dengyihao 已提交
262 263 264
  if (p == NULL) {
    return NULL;
  }
265 266
  p->opera = opera;
  p->query = taosArrayInit(4, sizeof(SIndexTermQuery));
dengyihao's avatar
dengyihao 已提交
267 268
  return p;
}
dengyihao's avatar
dengyihao 已提交
269
void indexMultiTermQueryDestroy(SIndexMultiTermQuery* pQuery) {
dengyihao's avatar
dengyihao 已提交
270
  for (int i = 0; i < taosArrayGetSize(pQuery->query); i++) {
dengyihao's avatar
dengyihao 已提交
271
    SIndexTermQuery* p = (SIndexTermQuery*)taosArrayGet(pQuery->query, i);
dengyihao's avatar
dengyihao 已提交
272
    indexTermDestroy(p->term);
dengyihao's avatar
dengyihao 已提交
273
  }
274
  taosArrayDestroy(pQuery->query);
wafwerar's avatar
wafwerar 已提交
275
  taosMemoryFree(pQuery);
dengyihao's avatar
dengyihao 已提交
276
};
dengyihao's avatar
dengyihao 已提交
277
int indexMultiTermQueryAdd(SIndexMultiTermQuery* pQuery, SIndexTerm* term, EIndexQueryType qType) {
278
  SIndexTermQuery q = {.qType = qType, .term = term};
dengyihao's avatar
dengyihao 已提交
279 280
  taosArrayPush(pQuery->query, &q);
  return 0;
dengyihao's avatar
dengyihao 已提交
281 282
}

dengyihao's avatar
dengyihao 已提交
283 284
SIndexTerm* indexTermCreate(int64_t suid, SIndexOperOnColumn oper, uint8_t colType, const char* colName,
                            int32_t nColName, const char* colVal, int32_t nColVal) {
wafwerar's avatar
wafwerar 已提交
285
  SIndexTerm* t = (SIndexTerm*)taosMemoryCalloc(1, (sizeof(SIndexTerm)));
dengyihao's avatar
dengyihao 已提交
286 287 288
  if (t == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
289

290 291
  t->suid = suid;
  t->operType = oper;
dengyihao's avatar
dengyihao 已提交
292 293
  t->colType = colType;

wafwerar's avatar
wafwerar 已提交
294
  t->colName = (char*)taosMemoryCalloc(1, nColName + 1);
dengyihao's avatar
dengyihao 已提交
295 296
  memcpy(t->colName, colName, nColName);
  t->nColName = nColName;
dengyihao's avatar
dengyihao 已提交
297

wafwerar's avatar
wafwerar 已提交
298
  t->colVal = (char*)taosMemoryCalloc(1, nColVal + 1);
dengyihao's avatar
dengyihao 已提交
299 300
  memcpy(t->colVal, colVal, nColVal);
  t->nColVal = nColVal;
dengyihao's avatar
dengyihao 已提交
301
  return t;
dengyihao's avatar
dengyihao 已提交
302
}
dengyihao's avatar
dengyihao 已提交
303
void indexTermDestroy(SIndexTerm* p) {
wafwerar's avatar
wafwerar 已提交
304 305 306
  taosMemoryFree(p->colName);
  taosMemoryFree(p->colVal);
  taosMemoryFree(p);
dengyihao's avatar
dengyihao 已提交
307 308
}

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

dengyihao's avatar
dengyihao 已提交
311
int indexMultiTermAdd(SIndexMultiTerm* terms, SIndexTerm* term) {
312 313
  taosArrayPush(terms, &term);
  return 0;
dengyihao's avatar
dengyihao 已提交
314
}
dengyihao's avatar
dengyihao 已提交
315
void indexMultiTermDestroy(SIndexMultiTerm* terms) {
dengyihao's avatar
dengyihao 已提交
316
  for (int32_t i = 0; i < taosArrayGetSize(terms); i++) {
dengyihao's avatar
dengyihao 已提交
317
    SIndexTerm* p = taosArrayGetP(terms, i);
dengyihao's avatar
dengyihao 已提交
318 319
    indexTermDestroy(p);
  }
dengyihao's avatar
dengyihao 已提交
320
  taosArrayDestroy(terms);
dengyihao's avatar
dengyihao 已提交
321
}
dengyihao's avatar
dengyihao 已提交
322

dengyihao's avatar
dengyihao 已提交
323 324 325
static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* query, SArray** result) {
  SIndexTerm* term = query->term;
  const char* colName = term->colName;
326
  int32_t     nColName = term->nColName;
dengyihao's avatar
dengyihao 已提交
327

dengyihao's avatar
dengyihao 已提交
328 329
  // Get col info
  IndexCache* cache = NULL;
dengyihao's avatar
dengyihao 已提交
330 331

  char      buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
332 333 334
  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 已提交
335

wafwerar's avatar
wafwerar 已提交
336
  taosThreadMutexLock(&sIdx->mtx);
dengyihao's avatar
dengyihao 已提交
337
  IndexCache** pCache = taosHashGet(sIdx->colObj, buf, sz);
dengyihao's avatar
dengyihao 已提交
338
  cache = (pCache == NULL) ? NULL : *pCache;
wafwerar's avatar
wafwerar 已提交
339
  taosThreadMutexUnlock(&sIdx->mtx);
340

dengyihao's avatar
dengyihao 已提交
341
  *result = taosArrayInit(4, sizeof(uint64_t));
342
  // TODO: iterator mem and tidex
dengyihao's avatar
dengyihao 已提交
343
  STermValueType s = kTypeValue;
dengyihao's avatar
dengyihao 已提交
344

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

dengyihao's avatar
dengyihao 已提交
347 348
  SIdxTempResult* tr = sIdxTempResultCreate();
  if (0 == indexCacheSearch(cache, query, tr, &s)) {
dengyihao's avatar
dengyihao 已提交
349
    if (s == kTypeDeletion) {
dengyihao's avatar
dengyihao 已提交
350
      indexInfo("col: %s already drop by", term->colName);
351
      // coloum already drop by other oper, no need to query tindex
dengyihao's avatar
dengyihao 已提交
352 353
      return 0;
    } else {
dengyihao's avatar
add UT  
dengyihao 已提交
354
      st = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
355
      if (0 != indexTFileSearch(sIdx->tindex, query, tr)) {
356
        indexError("corrupt at index(TFile) col:%s val: %s", term->colName, term->colVal);
dengyihao's avatar
dengyihao 已提交
357
        goto END;
358
      }
dengyihao's avatar
add UT  
dengyihao 已提交
359 360
      int64_t tfCost = taosGetTimestampUs() - st;
      indexInfo("tfile search cost: %" PRIu64 "us", tfCost);
dengyihao's avatar
dengyihao 已提交
361 362 363
    }
  } else {
    indexError("corrupt at index(cache) col:%s val: %s", term->colName, term->colVal);
dengyihao's avatar
dengyihao 已提交
364
    goto END;
dengyihao's avatar
dengyihao 已提交
365
  }
dengyihao's avatar
add UT  
dengyihao 已提交
366 367
  int64_t cost = taosGetTimestampUs() - st;
  indexInfo("search cost: %" PRIu64 "us", cost);
dengyihao's avatar
dengyihao 已提交
368 369

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

dengyihao's avatar
dengyihao 已提交
371
  sIdxTempResultDestroy(tr);
dengyihao's avatar
dengyihao 已提交
372
  return 0;
dengyihao's avatar
dengyihao 已提交
373 374 375
END:
  sIdxTempResultDestroy(tr);
  return -1;
dengyihao's avatar
dengyihao 已提交
376
}
dengyihao's avatar
dengyihao 已提交
377
static void indexInterResultsDestroy(SArray* results) {
dengyihao's avatar
dengyihao 已提交
378 379 380
  if (results == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
381 382 383

  size_t sz = taosArrayGetSize(results);
  for (size_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
384
    SArray* p = taosArrayGetP(results, i);
385 386
    taosArrayDestroy(p);
  }
dengyihao's avatar
dengyihao 已提交
387 388
  taosArrayDestroy(results);
}
dengyihao's avatar
dengyihao 已提交
389

dengyihao's avatar
dengyihao 已提交
390
static int indexMergeFinalResults(SArray* interResults, EIndexOperatorType oType, SArray* fResults) {
391
  // refactor, merge interResults into fResults by oType
dengyihao's avatar
dengyihao 已提交
392 393 394 395 396
  for (int i = 0; i < taosArrayGetSize(interResults); i--) {
    SArray* t = taosArrayGetP(interResults, i);
    taosArraySort(t, uidCompare);
    taosArrayRemoveDuplicate(t, uidCompare, NULL);
  }
dengyihao's avatar
dengyihao 已提交
397

dengyihao's avatar
dengyihao 已提交
398
  if (oType == MUST) {
dengyihao's avatar
dengyihao 已提交
399
    iIntersection(interResults, fResults);
dengyihao's avatar
dengyihao 已提交
400
  } else if (oType == SHOULD) {
dengyihao's avatar
dengyihao 已提交
401
    iUnion(interResults, fResults);
dengyihao's avatar
dengyihao 已提交
402
  } else if (oType == NOT) {
403
    // just one column index, enhance later
dengyihao's avatar
dengyihao 已提交
404
    taosArrayAddAll(fResults, interResults);
405
    // not use currently
dengyihao's avatar
dengyihao 已提交
406 407 408
  }
  return 0;
}
dengyihao's avatar
dengyihao 已提交
409

dengyihao's avatar
dengyihao 已提交
410
static void indexMayMergeTempToFinalResult(SArray* result, TFileValue* tfv, SIdxTempResult* tr) {
411 412 413 414
  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 已提交
415 416
      sIdxTempResultMergeTo(lv->tableId, tr);
      sIdxTempResultClear(tr);
417 418 419

      taosArrayPush(result, &tfv);
    } else if (tfv == NULL) {
dengyihao's avatar
dengyihao 已提交
420 421
      // handle last iterator
      sIdxTempResultMergeTo(lv->tableId, tr);
422
    } else {
dengyihao's avatar
dengyihao 已提交
423
      // temp result saved in help
424 425 426 427 428 429
      tfileValueDestroy(tfv);
    }
  } else {
    taosArrayPush(result, &tfv);
  }
}
dengyihao's avatar
dengyihao 已提交
430
static void indexMergeCacheAndTFile(SArray* result, IterateValue* cv, IterateValue* tv, SIdxTempResult* tr) {
431
  char*       colVal = (cv != NULL) ? cv->colVal : tv->colVal;
dengyihao's avatar
dengyihao 已提交
432
  TFileValue* tfv = tfileValueCreate(colVal);
433

dengyihao's avatar
dengyihao 已提交
434
  indexMayMergeTempToFinalResult(result, tfv, tr);
435

dengyihao's avatar
dengyihao 已提交
436
  if (cv != NULL) {
437
    uint64_t id = *(uint64_t*)taosArrayGet(cv->val, 0);
dengyihao's avatar
dengyihao 已提交
438
    uint32_t ver = cv->ver;
dengyihao's avatar
dengyihao 已提交
439
    if (cv->type == ADD_VALUE) {
dengyihao's avatar
dengyihao 已提交
440
      INDEX_MERGE_ADD_DEL(tr->deled, tr->added, id)
dengyihao's avatar
dengyihao 已提交
441
    } else if (cv->type == DEL_VALUE) {
dengyihao's avatar
dengyihao 已提交
442
      INDEX_MERGE_ADD_DEL(tr->added, tr->deled, id)
dengyihao's avatar
dengyihao 已提交
443 444 445
    }
  }
  if (tv != NULL) {
dengyihao's avatar
dengyihao 已提交
446
    taosArrayAddAll(tr->total, tv->val);
dengyihao's avatar
dengyihao 已提交
447 448
  }
}
dengyihao's avatar
dengyihao 已提交
449
static void indexDestroyFinalResult(SArray* result) {
dengyihao's avatar
dengyihao 已提交
450 451 452 453 454 455 456
  int32_t sz = result ? taosArrayGetSize(result) : 0;
  for (size_t i = 0; i < sz; i++) {
    TFileValue* tv = taosArrayGetP(result, i);
    tfileValueDestroy(tv);
  }
  taosArrayDestroy(result);
}
457

dengyihao's avatar
dengyihao 已提交
458
int indexFlushCacheToTFile(SIndex* sIdx, void* cache) {
dengyihao's avatar
dengyihao 已提交
459 460 461
  if (sIdx == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
462
  indexInfo("suid %" PRIu64 " merge cache into tindex", sIdx->suid);
dengyihao's avatar
dengyihao 已提交
463

dengyihao's avatar
dengyihao 已提交
464 465
  int64_t st = taosGetTimestampUs();

dengyihao's avatar
dengyihao 已提交
466
  IndexCache*  pCache = (IndexCache*)cache;
dengyihao's avatar
dengyihao 已提交
467
  TFileReader* pReader = tfileGetReaderByCol(sIdx->tindex, pCache->suid, pCache->colName);
dengyihao's avatar
dengyihao 已提交
468 469 470
  if (pReader == NULL) {
    indexWarn("empty tfile reader found");
  }
dengyihao's avatar
dengyihao 已提交
471 472 473
  // handle flush
  Iterate* cacheIter = indexCacheIteratorCreate(pCache);
  Iterate* tfileIter = tfileIteratorCreate(pReader);
dengyihao's avatar
dengyihao 已提交
474 475 476
  if (tfileIter == NULL) {
    indexWarn("empty tfile reader iterator");
  }
dengyihao's avatar
dengyihao 已提交
477 478 479

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

dengyihao's avatar
dengyihao 已提交
480 481
  bool cn = cacheIter ? cacheIter->next(cacheIter) : false;
  bool tn = tfileIter ? tfileIter->next(tfileIter) : false;
482

dengyihao's avatar
dengyihao 已提交
483
  SIdxTempResult* tr = sIdxTempResultCreate();
dengyihao's avatar
dengyihao 已提交
484 485 486 487 488 489 490 491 492 493 494 495
  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 已提交
496
    if (comp == 0) {
dengyihao's avatar
dengyihao 已提交
497
      indexMergeCacheAndTFile(result, cv, tv, tr);
dengyihao's avatar
dengyihao 已提交
498 499 500
      cn = cacheIter->next(cacheIter);
      tn = tfileIter->next(tfileIter);
    } else if (comp < 0) {
dengyihao's avatar
dengyihao 已提交
501
      indexMergeCacheAndTFile(result, cv, NULL, tr);
dengyihao's avatar
dengyihao 已提交
502 503
      cn = cacheIter->next(cacheIter);
    } else {
dengyihao's avatar
dengyihao 已提交
504
      indexMergeCacheAndTFile(result, NULL, tv, tr);
dengyihao's avatar
dengyihao 已提交
505 506 507
      tn = tfileIter->next(tfileIter);
    }
  }
dengyihao's avatar
dengyihao 已提交
508 509
  indexMayMergeTempToFinalResult(result, NULL, tr);
  sIdxTempResultDestroy(tr);
510

dengyihao's avatar
dengyihao 已提交
511
  int ret = indexGenTFile(sIdx, pCache, result);
dengyihao's avatar
dengyihao 已提交
512
  indexDestroyFinalResult(result);
dengyihao's avatar
dengyihao 已提交
513

dengyihao's avatar
dengyihao 已提交
514
  indexCacheDestroyImm(pCache);
dengyihao's avatar
dengyihao 已提交
515 516 517 518

  indexCacheIteratorDestroy(cacheIter);
  tfileIteratorDestroy(tfileIter);

dengyihao's avatar
dengyihao 已提交
519 520
  tfileReaderUnRef(pReader);
  indexCacheUnRef(pCache);
dengyihao's avatar
dengyihao 已提交
521 522 523 524 525 526 527 528

  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 已提交
529
}
dengyihao's avatar
dengyihao 已提交
530 531 532
void iterateValueDestroy(IterateValue* value, bool destroy) {
  if (destroy) {
    taosArrayDestroy(value->val);
dengyihao's avatar
dengyihao 已提交
533
    value->val = NULL;
dengyihao's avatar
dengyihao 已提交
534
  } else {
dengyihao's avatar
dengyihao 已提交
535 536 537
    if (value->val != NULL) {
      taosArrayClear(value->val);
    }
dengyihao's avatar
dengyihao 已提交
538
  }
wafwerar's avatar
wafwerar 已提交
539
  taosMemoryFree(value->colVal);
dengyihao's avatar
dengyihao 已提交
540 541
  value->colVal = NULL;
}
dengyihao's avatar
dengyihao 已提交
542 543 544 545
static int indexGenTFile(SIndex* sIdx, IndexCache* cache, SArray* batch) {
  int32_t version = CACHE_VERSION(cache);
  uint8_t colType = cache->type;

dengyihao's avatar
dengyihao 已提交
546
  TFileWriter* tw = tfileWriterOpen(sIdx->path, cache->suid, version, cache->colName, colType);
dengyihao's avatar
dengyihao 已提交
547 548 549 550 551 552 553 554 555 556 557 558
  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 已提交
559
  TFileReader* reader = tfileReaderOpen(sIdx->path, cache->suid, version, cache->colName);
dengyihao's avatar
dengyihao 已提交
560 561 562
  if (reader == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
563 564

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

wafwerar's avatar
wafwerar 已提交
567
  taosThreadMutexLock(&sIdx->mtx);
dengyihao's avatar
dengyihao 已提交
568 569
  IndexTFile* ifile = (IndexTFile*)sIdx->tindex;
  tfileCachePut(ifile->cache, &key, reader);
wafwerar's avatar
wafwerar 已提交
570
  taosThreadMutexUnlock(&sIdx->mtx);
dengyihao's avatar
dengyihao 已提交
571 572
  return ret;
END:
dengyihao's avatar
dengyihao 已提交
573 574
  if (tw != NULL) {
    writerCtxDestroy(tw->ctx, true);
wafwerar's avatar
wafwerar 已提交
575
    taosMemoryFree(tw);
dengyihao's avatar
dengyihao 已提交
576
  }
dengyihao's avatar
dengyihao 已提交
577
  return -1;
dengyihao's avatar
dengyihao 已提交
578
}
dengyihao's avatar
dengyihao 已提交
579

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

dengyihao's avatar
dengyihao 已提交
583 584 585 586 587
  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 已提交
588 589 590 591 592
  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 已提交
593 594
  return buf - p;
}