index.c 15.5 KB
Newer Older
H
refact  
Hongze Cheng 已提交
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
#include "index.h"
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
18
#include "index_cache.h"
dengyihao's avatar
dengyihao 已提交
19
#include "index_tfile.h"
dengyihao's avatar
dengyihao 已提交
20
#include "index_util.h"
dengyihao's avatar
dengyihao 已提交
21
#include "tdef.h"
dengyihao's avatar
dengyihao 已提交
22
#include "tsched.h"
H
refact  
Hongze Cheng 已提交
23

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

dengyihao's avatar
dengyihao 已提交
28
#define INDEX_NUM_OF_THREADS 4
dengyihao's avatar
dengyihao 已提交
29
#define INDEX_QUEUE_SIZE 200
dengyihao's avatar
dengyihao 已提交
30 31 32 33 34 35 36 37

void* indexQhandle = NULL;

int32_t indexInit() {
  indexQhandle = taosInitScheduler(INDEX_QUEUE_SIZE, INDEX_NUM_OF_THREADS, "index");
  return indexQhandle == NULL ? -1 : 0;
  // do nothing
}
dengyihao's avatar
dengyihao 已提交
38
void indexCleanUp() { taosCleanUpScheduler(indexQhandle); }
dengyihao's avatar
dengyihao 已提交
39

dengyihao's avatar
dengyihao 已提交
40 41 42
static int uidCompare(const void* a, const void* b) {
  uint64_t u1 = *(uint64_t*)a;
  uint64_t u2 = *(uint64_t*)b;
43 44 45 46 47
  if (u1 == u2) {
    return 0;
  } else {
    return u1 < u2 ? -1 : 1;
  }
dengyihao's avatar
dengyihao 已提交
48
}
dengyihao's avatar
dengyihao 已提交
49
typedef struct SIdxColInfo {
50
  int colId;  // generated by index internal
dengyihao's avatar
dengyihao 已提交
51
  int cVersion;
52
} SIdxColInfo;
dengyihao's avatar
dengyihao 已提交
53 54

static pthread_once_t isInit = PTHREAD_ONCE_INIT;
dengyihao's avatar
dengyihao 已提交
55
// static void           indexInit();
dengyihao's avatar
dengyihao 已提交
56
static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* term, SArray** result);
dengyihao's avatar
dengyihao 已提交
57

dengyihao's avatar
dengyihao 已提交
58 59
static void indexInterResultsDestroy(SArray* results);
static int  indexMergeFinalResults(SArray* interResults, EIndexOperatorType oType, SArray* finalResult);
dengyihao's avatar
dengyihao 已提交
60

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

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

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

dengyihao's avatar
dengyihao 已提交
73
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
74 75
  // sIdx->cache = (void*)indexCacheCreate(sIdx);
  sIdx->tindex = indexTFileCreate(path);
76 77
  sIdx->colObj = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  sIdx->cVersion = 1;
dengyihao's avatar
dengyihao 已提交
78 79
  sIdx->path = calloc(1, strlen(path) + 1);
  memcpy(sIdx->path, path, strlen(path));
dengyihao's avatar
dengyihao 已提交
80
  pthread_mutex_init(&sIdx->mtx, NULL);
dengyihao's avatar
dengyihao 已提交
81

82
  *index = sIdx;
dengyihao's avatar
dengyihao 已提交
83

84
  return 0;
dengyihao's avatar
dengyihao 已提交
85 86 87 88
#endif

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

dengyihao's avatar
dengyihao 已提交
91
void indexClose(SIndex* sIdx) {
92 93
#ifdef USE_LUCENE
  index_close(sIdex->index);
dengyihao's avatar
dengyihao 已提交
94
  sIdx->index = NULL;
H
refact  
Hongze Cheng 已提交
95
#endif
dengyihao's avatar
dengyihao 已提交
96

dengyihao's avatar
dengyihao 已提交
97
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
98 99 100
  void* iter = taosHashIterate(sIdx->colObj, NULL);
  while (iter) {
    IndexCache** pCache = iter;
dengyihao's avatar
dengyihao 已提交
101
    if (*pCache) { indexCacheUnRef(*pCache); }
dengyihao's avatar
dengyihao 已提交
102 103
    iter = taosHashIterate(sIdx->colObj, iter);
  }
104
  taosHashCleanup(sIdx->colObj);
dengyihao's avatar
dengyihao 已提交
105
  pthread_mutex_destroy(&sIdx->mtx);
106
  indexTFileDestroy(sIdx->tindex);
dengyihao's avatar
dengyihao 已提交
107
#endif
dengyihao's avatar
dengyihao 已提交
108
  free(sIdx->path);
109
  free(sIdx);
dengyihao's avatar
dengyihao 已提交
110 111
  return;
}
dengyihao's avatar
dengyihao 已提交
112

dengyihao's avatar
dengyihao 已提交
113
int indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) {
114
#ifdef USE_LUCENE
dengyihao's avatar
dengyihao 已提交
115
  index_document_t* doc = index_document_create();
116 117 118

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

120
  for (int i = 0; i < taosArrayGetSize(fVals); i++) {
dengyihao's avatar
dengyihao 已提交
121 122
    SIndexTerm* p = taosArrayGetP(fVals, i);
    index_document_add(doc, (const char*)(p->key), p->nKey, (const char*)(p->val), p->nVal, 1);
123 124 125 126 127
  }
  index_document_add(doc, NULL, 0, buf, strlen(buf), 0);

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

dengyihao's avatar
dengyihao 已提交
130
#ifdef USE_INVERTED_INDEX
131 132 133

  // TODO(yihao): reduce the lock range
  pthread_mutex_lock(&index->mtx);
dengyihao's avatar
dengyihao 已提交
134
  for (int i = 0; i < taosArrayGetSize(fVals); i++) {
dengyihao's avatar
dengyihao 已提交
135 136 137 138 139 140 141
    SIndexTerm* p = taosArrayGetP(fVals, i);

    char      buf[128] = {0};
    ICacheKey key = {.suid = p->suid, .colName = p->colName};
    int32_t   sz = indexSerialCacheKey(&key, buf);

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

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

    char      buf[128] = {0};
    ICacheKey key = {.suid = p->suid, .colName = p->colName};
    int32_t   sz = indexSerialCacheKey(&key, buf);

    IndexCache** cache = taosHashGet(index->colObj, buf, sz);
dengyihao's avatar
dengyihao 已提交
157 158
    assert(*cache != NULL);
    int ret = indexCachePut(*cache, p, uid);
dengyihao's avatar
dengyihao 已提交
159
    if (ret != 0) { return ret; }
dengyihao's avatar
dengyihao 已提交
160
  }
dengyihao's avatar
dengyihao 已提交
161

dengyihao's avatar
dengyihao 已提交
162
#endif
dengyihao's avatar
dengyihao 已提交
163
  return 0;
dengyihao's avatar
dengyihao 已提交
164
}
dengyihao's avatar
dengyihao 已提交
165
int indexSearch(SIndex* index, SIndexMultiTermQuery* multiQuerys, SArray* result) {
166 167
#ifdef USE_LUCENE
  EIndexOperatorType opera = multiQuerys->opera;
dengyihao's avatar
dengyihao 已提交
168

169
  int    nQuery = taosArrayGetSize(multiQuerys->query);
dengyihao's avatar
dengyihao 已提交
170 171 172
  char** fields = malloc(sizeof(char*) * nQuery);
  char** keys = malloc(sizeof(char*) * nQuery);
  int*   types = malloc(sizeof(int) * nQuery);
dengyihao's avatar
dengyihao 已提交
173 174

  for (int i = 0; i < nQuery; i++) {
dengyihao's avatar
dengyihao 已提交
175 176
    SIndexTermQuery* p = taosArrayGet(multiQuerys->query, i);
    SIndexTerm*      term = p->field_value;
177 178 179 180 181 182 183 184

    fields[i] = calloc(1, term->nKey + 1);
    keys[i] = calloc(1, term->nVal + 1);

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

dengyihao's avatar
dengyihao 已提交
189
  for (int i = 0; i < tsz; i++) { taosArrayPush(result, &tResult[i]); }
dengyihao's avatar
dengyihao 已提交
190 191 192 193 194 195 196 197

  for (int i = 0; i < nQuery; i++) {
    free(fields[i]);
    free(keys[i]);
  }
  free(fields);
  free(keys);
  free(types);
dengyihao's avatar
dengyihao 已提交
198 199
#endif

dengyihao's avatar
dengyihao 已提交
200
#ifdef USE_INVERTED_INDEX
201 202
  EIndexOperatorType opera = multiQuerys->opera;  // relation of querys

dengyihao's avatar
dengyihao 已提交
203
  SArray* interResults = taosArrayInit(4, POINTER_BYTES);
204
  int     nQuery = taosArrayGetSize(multiQuerys->query);
dengyihao's avatar
dengyihao 已提交
205
  for (size_t i = 0; i < nQuery; i++) {
dengyihao's avatar
dengyihao 已提交
206 207
    SIndexTermQuery* qTerm = taosArrayGet(multiQuerys->query, i);
    SArray*          tResult = NULL;
208
    indexTermSearch(index, qTerm, &tResult);
dengyihao's avatar
dengyihao 已提交
209
    taosArrayPush(interResults, (void*)&tResult);
210
  }
dengyihao's avatar
dengyihao 已提交
211 212
  indexMergeFinalResults(interResults, opera, result);
  indexInterResultsDestroy(interResults);
213

dengyihao's avatar
dengyihao 已提交
214
#endif
dengyihao's avatar
dengyihao 已提交
215 216 217
  return 1;
}

dengyihao's avatar
dengyihao 已提交
218
int indexDelete(SIndex* index, SIndexMultiTermQuery* query) {
dengyihao's avatar
dengyihao 已提交
219
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
220
#endif
221

dengyihao's avatar
dengyihao 已提交
222 223
  return 1;
}
dengyihao's avatar
dengyihao 已提交
224
int indexRebuild(SIndex* index, SIndexOpts* opts){
dengyihao's avatar
dengyihao 已提交
225
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
226 227 228
#endif

}
dengyihao's avatar
dengyihao 已提交
229

dengyihao's avatar
dengyihao 已提交
230
SIndexOpts* indexOptsCreate() {
231
#ifdef USE_LUCENE
dengyihao's avatar
dengyihao 已提交
232
#endif
233
  return NULL;
dengyihao's avatar
dengyihao 已提交
234
}
dengyihao's avatar
dengyihao 已提交
235
void indexOptsDestroy(SIndexOpts* opts) {
236
#ifdef USE_LUCENE
dengyihao's avatar
dengyihao 已提交
237
#endif
dengyihao's avatar
dengyihao 已提交
238 239 240 241 242 243
  return;
}
/*
 * @param: oper
 *
 */
dengyihao's avatar
dengyihao 已提交
244 245
SIndexMultiTermQuery* indexMultiTermQueryCreate(EIndexOperatorType opera) {
  SIndexMultiTermQuery* p = (SIndexMultiTermQuery*)malloc(sizeof(SIndexMultiTermQuery));
dengyihao's avatar
dengyihao 已提交
246
  if (p == NULL) { return NULL; }
247 248
  p->opera = opera;
  p->query = taosArrayInit(4, sizeof(SIndexTermQuery));
dengyihao's avatar
dengyihao 已提交
249 250
  return p;
}
dengyihao's avatar
dengyihao 已提交
251
void indexMultiTermQueryDestroy(SIndexMultiTermQuery* pQuery) {
dengyihao's avatar
dengyihao 已提交
252
  for (int i = 0; i < taosArrayGetSize(pQuery->query); i++) {
dengyihao's avatar
dengyihao 已提交
253
    SIndexTermQuery* p = (SIndexTermQuery*)taosArrayGet(pQuery->query, i);
dengyihao's avatar
dengyihao 已提交
254
    indexTermDestroy(p->term);
dengyihao's avatar
dengyihao 已提交
255
  }
256
  taosArrayDestroy(pQuery->query);
dengyihao's avatar
dengyihao 已提交
257 258
  free(pQuery);
};
dengyihao's avatar
dengyihao 已提交
259
int indexMultiTermQueryAdd(SIndexMultiTermQuery* pQuery, SIndexTerm* term, EIndexQueryType qType) {
260
  SIndexTermQuery q = {.qType = qType, .term = term};
dengyihao's avatar
dengyihao 已提交
261 262
  taosArrayPush(pQuery->query, &q);
  return 0;
dengyihao's avatar
dengyihao 已提交
263 264
}

dengyihao's avatar
dengyihao 已提交
265 266
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 已提交
267
  SIndexTerm* t = (SIndexTerm*)calloc(1, (sizeof(SIndexTerm)));
dengyihao's avatar
dengyihao 已提交
268
  if (t == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
269

270 271
  t->suid = suid;
  t->operType = oper;
dengyihao's avatar
dengyihao 已提交
272 273
  t->colType = colType;

dengyihao's avatar
dengyihao 已提交
274
  t->colName = (char*)calloc(1, nColName + 1);
dengyihao's avatar
dengyihao 已提交
275 276
  memcpy(t->colName, colName, nColName);
  t->nColName = nColName;
dengyihao's avatar
dengyihao 已提交
277

dengyihao's avatar
dengyihao 已提交
278
  t->colVal = (char*)calloc(1, nColVal + 1);
dengyihao's avatar
dengyihao 已提交
279 280
  memcpy(t->colVal, colVal, nColVal);
  t->nColVal = nColVal;
dengyihao's avatar
dengyihao 已提交
281
  return t;
dengyihao's avatar
dengyihao 已提交
282
}
dengyihao's avatar
dengyihao 已提交
283
void indexTermDestroy(SIndexTerm* p) {
dengyihao's avatar
dengyihao 已提交
284 285
  free(p->colName);
  free(p->colVal);
dengyihao's avatar
dengyihao 已提交
286
  free(p);
dengyihao's avatar
dengyihao 已提交
287 288
}

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

dengyihao's avatar
dengyihao 已提交
291
int indexMultiTermAdd(SIndexMultiTerm* terms, SIndexTerm* term) {
292 293
  taosArrayPush(terms, &term);
  return 0;
dengyihao's avatar
dengyihao 已提交
294
}
dengyihao's avatar
dengyihao 已提交
295
void indexMultiTermDestroy(SIndexMultiTerm* terms) {
dengyihao's avatar
dengyihao 已提交
296
  for (int32_t i = 0; i < taosArrayGetSize(terms); i++) {
dengyihao's avatar
dengyihao 已提交
297
    SIndexTerm* p = taosArrayGetP(terms, i);
dengyihao's avatar
dengyihao 已提交
298 299
    indexTermDestroy(p);
  }
dengyihao's avatar
dengyihao 已提交
300
  taosArrayDestroy(terms);
dengyihao's avatar
dengyihao 已提交
301
}
dengyihao's avatar
dengyihao 已提交
302

dengyihao's avatar
dengyihao 已提交
303 304 305
static int indexTermSearch(SIndex* sIdx, SIndexTermQuery* query, SArray** result) {
  SIndexTerm* term = query->term;
  const char* colName = term->colName;
306
  int32_t     nColName = term->nColName;
dengyihao's avatar
dengyihao 已提交
307

dengyihao's avatar
dengyihao 已提交
308 309
  // Get col info
  IndexCache* cache = NULL;
310
  pthread_mutex_lock(&sIdx->mtx);
dengyihao's avatar
dengyihao 已提交
311 312 313 314 315 316

  char      buf[128] = {0};
  ICacheKey key = {.suid = term->suid, .colName = term->colName};
  int32_t   sz = indexSerialCacheKey(&key, buf);

  IndexCache** pCache = taosHashGet(sIdx->colObj, buf, sz);
dengyihao's avatar
dengyihao 已提交
317
  if (pCache == NULL) {
318 319
    pthread_mutex_unlock(&sIdx->mtx);
    return -1;
dengyihao's avatar
dengyihao 已提交
320
  }
dengyihao's avatar
dengyihao 已提交
321
  cache = *pCache;
322 323
  pthread_mutex_unlock(&sIdx->mtx);

dengyihao's avatar
dengyihao 已提交
324
  *result = taosArrayInit(4, sizeof(uint64_t));
325
  // TODO: iterator mem and tidex
dengyihao's avatar
dengyihao 已提交
326
  STermValueType s = kTypeValue;
dengyihao's avatar
dengyihao 已提交
327
  if (0 == indexCacheSearch(cache, query, *result, &s)) {
dengyihao's avatar
dengyihao 已提交
328 329
    if (s == kTypeDeletion) {
      indexInfo("col: %s already drop by other opera", term->colName);
330
      // coloum already drop by other oper, no need to query tindex
dengyihao's avatar
dengyihao 已提交
331 332 333
      return 0;
    } else {
      if (0 != indexTFileSearch(sIdx->tindex, query, *result)) {
334 335 336
        indexError("corrupt at index(TFile) col:%s val: %s", term->colName, term->colVal);
        return -1;
      }
dengyihao's avatar
dengyihao 已提交
337 338 339 340 341 342
    }
  } else {
    indexError("corrupt at index(cache) col:%s val: %s", term->colName, term->colVal);
    return -1;
  }
  return 0;
dengyihao's avatar
dengyihao 已提交
343
}
dengyihao's avatar
dengyihao 已提交
344
static void indexInterResultsDestroy(SArray* results) {
dengyihao's avatar
dengyihao 已提交
345
  if (results == NULL) { return; }
dengyihao's avatar
dengyihao 已提交
346 347 348

  size_t sz = taosArrayGetSize(results);
  for (size_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
349
    SArray* p = taosArrayGetP(results, i);
350 351
    taosArrayDestroy(p);
  }
dengyihao's avatar
dengyihao 已提交
352 353
  taosArrayDestroy(results);
}
dengyihao's avatar
dengyihao 已提交
354
static int indexMergeFinalResults(SArray* interResults, EIndexOperatorType oType, SArray* fResults) {
355
  // refactor, merge interResults into fResults by oType
dengyihao's avatar
dengyihao 已提交
356
  SArray* first = taosArrayGetP(interResults, 0);
357
  taosArraySort(first, uidCompare);
dengyihao's avatar
dengyihao 已提交
358
  taosArrayRemoveDuplicate(first, uidCompare, NULL);
dengyihao's avatar
dengyihao 已提交
359

dengyihao's avatar
dengyihao 已提交
360
  if (oType == MUST) {
361 362
    // just one column index, enhance later
    taosArrayAddAll(fResults, first);
dengyihao's avatar
dengyihao 已提交
363
  } else if (oType == SHOULD) {
364 365
    // just one column index, enhance later
    taosArrayAddAll(fResults, first);
dengyihao's avatar
dengyihao 已提交
366 367
    // tag1 condistion || tag2 condition
  } else if (oType == NOT) {
368 369 370
    // just one column index, enhance later
    taosArrayAddAll(fResults, first);
    // not use currently
dengyihao's avatar
dengyihao 已提交
371 372 373
  }
  return 0;
}
dengyihao's avatar
dengyihao 已提交
374

dengyihao's avatar
dengyihao 已提交
375 376 377 378 379
static void indexMergeSameKey(SArray* result, TFileValue* tv) {
  int32_t sz = result ? taosArrayGetSize(result) : 0;
  if (sz > 0) {
    // TODO(yihao): remove duplicate tableid
    TFileValue* lv = taosArrayGetP(result, sz - 1);
dengyihao's avatar
dengyihao 已提交
380
    // indexError("merge colVal: %s", lv->colVal);
dengyihao's avatar
dengyihao 已提交
381 382 383 384 385 386 387 388
    if (strcmp(lv->colVal, tv->colVal) == 0) {
      taosArrayAddAll(lv->tableId, tv->tableId);
      tfileValueDestroy(tv);
    } else {
      taosArrayPush(result, &tv);
    }
  } else {
    taosArrayPush(result, &tv);
dengyihao's avatar
dengyihao 已提交
389
    // indexError("merge colVal: %s", tv->colVal);
dengyihao's avatar
dengyihao 已提交
390
  }
dengyihao's avatar
dengyihao 已提交
391 392 393 394 395 396 397 398 399 400 401
}
static void indexDestroyTempResult(SArray* result) {
  int32_t sz = result ? taosArrayGetSize(result) : 0;
  for (size_t i = 0; i < sz; i++) {
    TFileValue* tv = taosArrayGetP(result, i);
    tfileValueDestroy(tv);
  }
  taosArrayDestroy(result);
}
int indexFlushCacheTFile(SIndex* sIdx, void* cache) {
  if (sIdx == NULL) { return -1; }
402
  indexWarn("suid %" PRIu64 " merge cache into tindex", sIdx->suid);
dengyihao's avatar
dengyihao 已提交
403

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

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

dengyihao's avatar
dengyihao 已提交
414 415
  bool cn = cacheIter ? cacheIter->next(cacheIter) : false;
  bool tn = tfileIter ? tfileIter->next(tfileIter) : false;
dengyihao's avatar
dengyihao 已提交
416 417 418 419 420 421 422 423 424 425
  while (cn == true && tn == true) {
    IterateValue* cv = cacheIter->getValue(cacheIter);
    IterateValue* tv = tfileIter->getValue(tfileIter);

    // dump value
    int comp = strcmp(cv->colVal, tv->colVal);
    if (comp == 0) {
      TFileValue* tfv = tfileValueCreate(cv->colVal);
      taosArrayAddAll(tfv->tableId, cv->val);
      taosArrayAddAll(tfv->tableId, tv->val);
dengyihao's avatar
dengyihao 已提交
426
      indexMergeSameKey(result, tfv);
dengyihao's avatar
dengyihao 已提交
427 428 429 430 431 432 433

      cn = cacheIter->next(cacheIter);
      tn = tfileIter->next(tfileIter);
      continue;
    } else if (comp < 0) {
      TFileValue* tfv = tfileValueCreate(cv->colVal);
      taosArrayAddAll(tfv->tableId, cv->val);
dengyihao's avatar
dengyihao 已提交
434 435

      indexMergeSameKey(result, tfv);
dengyihao's avatar
dengyihao 已提交
436 437 438 439 440
      // copy to final Result;
      cn = cacheIter->next(cacheIter);
    } else {
      TFileValue* tfv = tfileValueCreate(tv->colVal);
      taosArrayAddAll(tfv->tableId, tv->val);
dengyihao's avatar
dengyihao 已提交
441 442

      indexMergeSameKey(result, tfv);
dengyihao's avatar
dengyihao 已提交
443 444 445 446 447 448 449 450
      // copy to final result
      tn = tfileIter->next(tfileIter);
    }
  }
  while (cn == true) {
    IterateValue* cv = cacheIter->getValue(cacheIter);
    TFileValue*   tfv = tfileValueCreate(cv->colVal);
    taosArrayAddAll(tfv->tableId, cv->val);
dengyihao's avatar
dengyihao 已提交
451
    indexMergeSameKey(result, tfv);
dengyihao's avatar
dengyihao 已提交
452 453 454 455 456
    cn = cacheIter->next(cacheIter);
  }
  while (tn == true) {
    IterateValue* tv = tfileIter->getValue(tfileIter);
    TFileValue*   tfv = tfileValueCreate(tv->colVal);
dengyihao's avatar
dengyihao 已提交
457 458 459 460
    if (tv->val == NULL) {
      // HO
      printf("NO....");
    }
dengyihao's avatar
dengyihao 已提交
461
    taosArrayAddAll(tfv->tableId, tv->val);
dengyihao's avatar
dengyihao 已提交
462
    indexMergeSameKey(result, tfv);
dengyihao's avatar
dengyihao 已提交
463 464
    tn = tfileIter->next(tfileIter);
  }
dengyihao's avatar
dengyihao 已提交
465 466
  int ret = indexGenTFile(sIdx, pCache, result);
  indexDestroyTempResult(result);
dengyihao's avatar
dengyihao 已提交
467
  indexCacheDestroyImm(pCache);
dengyihao's avatar
dengyihao 已提交
468 469 470 471

  indexCacheIteratorDestroy(cacheIter);
  tfileIteratorDestroy(tfileIter);

dengyihao's avatar
dengyihao 已提交
472 473
  tfileReaderUnRef(pReader);
  indexCacheUnRef(pCache);
dengyihao's avatar
dengyihao 已提交
474 475
  return 0;
}
dengyihao's avatar
dengyihao 已提交
476 477 478
void iterateValueDestroy(IterateValue* value, bool destroy) {
  if (destroy) {
    taosArrayDestroy(value->val);
dengyihao's avatar
dengyihao 已提交
479
    value->val = NULL;
dengyihao's avatar
dengyihao 已提交
480
  } else {
dengyihao's avatar
dengyihao 已提交
481
    if (value->val != NULL) { taosArrayClear(value->val); }
dengyihao's avatar
dengyihao 已提交
482
  }
483
  free(value->colVal);
dengyihao's avatar
dengyihao 已提交
484 485
  value->colVal = NULL;
}
dengyihao's avatar
dengyihao 已提交
486 487 488 489
static int indexGenTFile(SIndex* sIdx, IndexCache* cache, SArray* batch) {
  int32_t version = CACHE_VERSION(cache);
  uint8_t colType = cache->type;

dengyihao's avatar
dengyihao 已提交
490
  TFileWriter* tw = tfileWriterOpen(sIdx->path, cache->suid, version, cache->colName, colType);
dengyihao's avatar
dengyihao 已提交
491 492 493 494 495 496 497 498 499 500 501 502
  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 已提交
503 504 505 506 507 508
  TFileReader* reader = tfileReaderOpen(sIdx->path, cache->suid, version, cache->colName);

  char         buf[128] = {0};
  TFileHeader* header = &reader->header;
  ICacheKey    key = {
      .suid = cache->suid, .colName = header->colName, .nColName = strlen(header->colName), .colType = header->colType};
dengyihao's avatar
dengyihao 已提交
509 510 511 512 513 514 515 516 517 518 519

  pthread_mutex_lock(&sIdx->mtx);

  IndexTFile* ifile = (IndexTFile*)sIdx->tindex;
  tfileCachePut(ifile->cache, &key, reader);

  pthread_mutex_unlock(&sIdx->mtx);
  return ret;
END:
  tfileWriterClose(tw);
}
dengyihao's avatar
dengyihao 已提交
520 521 522 523 524 525 526 527 528 529

int32_t indexSerialCacheKey(ICacheKey* key, char* buf) {
  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);
  SERIALIZE_STR_MEM_TO_BUF(buf, key, colName, key->nColName);
  return buf - p;
}