index.c 15.6 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);
dengyihao's avatar
dengyihao 已提交
76
  if (sIdx->tindex == NULL) { goto END; }
77 78
  sIdx->colObj = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  sIdx->cVersion = 1;
dengyihao's avatar
dengyihao 已提交
79 80
  sIdx->path = calloc(1, strlen(path) + 1);
  memcpy(sIdx->path, path, strlen(path));
dengyihao's avatar
dengyihao 已提交
81
  pthread_mutex_init(&sIdx->mtx, NULL);
dengyihao's avatar
dengyihao 已提交
82

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

85
  return 0;
dengyihao's avatar
dengyihao 已提交
86
#endif
dengyihao's avatar
dengyihao 已提交
87 88
END:
  if (sIdx != NULL) { indexClose(sIdx); }
dengyihao's avatar
dengyihao 已提交
89 90 91

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

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

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
133
#ifdef USE_INVERTED_INDEX
134 135 136

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

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

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

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

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

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

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

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

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

    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 已提交
188
  int* tResult = NULL;
189
  int  tsz = 0;
dengyihao's avatar
dengyihao 已提交
190
  index_multi_search(index->index, (const char**)fields, (const char**)keys, types, nQuery, opera, &tResult, &tsz);
191

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

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

dengyihao's avatar
dengyihao 已提交
203
#ifdef USE_INVERTED_INDEX
204 205
  EIndexOperatorType opera = multiQuerys->opera;  // relation of querys

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

dengyihao's avatar
dengyihao 已提交
217
#endif
dengyihao's avatar
dengyihao 已提交
218
  return 0;
dengyihao's avatar
dengyihao 已提交
219 220
}

dengyihao's avatar
dengyihao 已提交
221
int indexDelete(SIndex* index, SIndexMultiTermQuery* query) {
dengyihao's avatar
dengyihao 已提交
222
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
223
#endif
224

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

}
dengyihao's avatar
dengyihao 已提交
232

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

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

273 274
  t->suid = suid;
  t->operType = oper;
dengyihao's avatar
dengyihao 已提交
275 276
  t->colType = colType;

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

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

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

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

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

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

  char      buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
316
  ICacheKey key = {.suid = term->suid, .colName = term->colName, .nColName = strlen(term->colName)};
dengyihao's avatar
dengyihao 已提交
317 318 319
  int32_t   sz = indexSerialCacheKey(&key, buf);

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

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

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

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

dengyihao's avatar
dengyihao 已提交
378 379 380 381 382
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 已提交
383
    // indexError("merge colVal: %s", lv->colVal);
dengyihao's avatar
dengyihao 已提交
384 385 386 387 388 389 390 391
    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 已提交
392
    // indexError("merge colVal: %s", tv->colVal);
dengyihao's avatar
dengyihao 已提交
393
  }
dengyihao's avatar
dengyihao 已提交
394 395 396 397 398 399 400 401 402 403 404
}
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; }
405
  indexWarn("suid %" PRIu64 " merge cache into tindex", sIdx->suid);
dengyihao's avatar
dengyihao 已提交
406

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

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

dengyihao's avatar
dengyihao 已提交
417 418
  bool cn = cacheIter ? cacheIter->next(cacheIter) : false;
  bool tn = tfileIter ? tfileIter->next(tfileIter) : false;
dengyihao's avatar
dengyihao 已提交
419 420 421 422 423 424 425 426 427 428
  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 已提交
429
      indexMergeSameKey(result, tfv);
dengyihao's avatar
dengyihao 已提交
430 431 432 433 434 435 436

      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 已提交
437 438

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

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

  indexCacheIteratorDestroy(cacheIter);
  tfileIteratorDestroy(tfileIter);

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

dengyihao's avatar
dengyihao 已提交
493
  TFileWriter* tw = tfileWriterOpen(sIdx->path, cache->suid, version, cache->colName, colType);
dengyihao's avatar
dengyihao 已提交
494 495 496 497 498 499 500 501 502 503 504 505
  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 已提交
506 507 508 509 510 511
  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 已提交
512 513 514 515 516 517 518 519 520 521 522

  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 已提交
523 524 525 526 527 528 529 530 531 532

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;
}