index.c 15.8 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

void* indexQhandle = NULL;

dengyihao's avatar
dengyihao 已提交
33 34
void indexInit() {
  // refactor later
dengyihao's avatar
dengyihao 已提交
35 36
  indexQhandle = taosInitScheduler(INDEX_QUEUE_SIZE, INDEX_NUM_OF_THREADS, "index");
}
dengyihao's avatar
dengyihao 已提交
37 38 39 40
void indexCleanUp() {
  // refacto later
  taosCleanUpScheduler(indexQhandle);
}
dengyihao's avatar
dengyihao 已提交
41

dengyihao's avatar
dengyihao 已提交
42
static int uidCompare(const void* a, const void* b) {
dengyihao's avatar
dengyihao 已提交
43
  // add more version compare
dengyihao's avatar
dengyihao 已提交
44 45
  uint64_t u1 = *(uint64_t*)a;
  uint64_t u2 = *(uint64_t*)b;
dengyihao's avatar
dengyihao 已提交
46
  return u1 - u2;
dengyihao's avatar
dengyihao 已提交
47
}
dengyihao's avatar
dengyihao 已提交
48
typedef struct SIdxColInfo {
49
  int colId;  // generated by index internal
dengyihao's avatar
dengyihao 已提交
50
  int cVersion;
51
} SIdxColInfo;
dengyihao's avatar
dengyihao 已提交
52 53

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

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

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

dengyihao's avatar
dengyihao 已提交
62 63 64 65
// merge cache and tfile by opera type
static void indexMergeCacheAndTFile(SArray* result, IterateValue* icache, IterateValue* iTfv);
static void indexMergeSameKey(SArray* result, TFileValue* tv);

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

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

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

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

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

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

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

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
143
#ifdef USE_INVERTED_INDEX
144 145 146

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

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

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

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

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

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

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

184
  int    nQuery = taosArrayGetSize(multiQuerys->query);
dengyihao's avatar
dengyihao 已提交
185 186 187
  char** fields = malloc(sizeof(char*) * nQuery);
  char** keys = malloc(sizeof(char*) * nQuery);
  int*   types = malloc(sizeof(int) * nQuery);
dengyihao's avatar
dengyihao 已提交
188 189

  for (int i = 0; i < nQuery; i++) {
dengyihao's avatar
dengyihao 已提交
190 191
    SIndexTermQuery* p = taosArrayGet(multiQuerys->query, i);
    SIndexTerm*      term = p->field_value;
192 193 194 195 196 197 198 199

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

dengyihao's avatar
dengyihao 已提交
204 205 206
  for (int i = 0; i < tsz; i++) {
    taosArrayPush(result, &tResult[i]);
  }
dengyihao's avatar
dengyihao 已提交
207 208 209 210 211 212 213 214

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

dengyihao's avatar
dengyihao 已提交
217
#ifdef USE_INVERTED_INDEX
218 219
  EIndexOperatorType opera = multiQuerys->opera;  // relation of querys

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

dengyihao's avatar
dengyihao 已提交
231
#endif
dengyihao's avatar
dengyihao 已提交
232
  return 0;
dengyihao's avatar
dengyihao 已提交
233 234
}

dengyihao's avatar
dengyihao 已提交
235
int indexDelete(SIndex* index, SIndexMultiTermQuery* query) {
dengyihao's avatar
dengyihao 已提交
236
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
237
#endif
238

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

}
dengyihao's avatar
dengyihao 已提交
246

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

dengyihao's avatar
dengyihao 已提交
284 285
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 已提交
286
  SIndexTerm* t = (SIndexTerm*)calloc(1, (sizeof(SIndexTerm)));
dengyihao's avatar
dengyihao 已提交
287 288 289
  if (t == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
290

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

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

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

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

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

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

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

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

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

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
  if (0 == indexCacheSearch(cache, query, *result, &s)) {
dengyihao's avatar
dengyihao 已提交
345
    if (s == kTypeDeletion) {
dengyihao's avatar
dengyihao 已提交
346
      indexInfo("col: %s already drop by", term->colName);
347
      // coloum already drop by other oper, no need to query tindex
dengyihao's avatar
dengyihao 已提交
348 349 350
      return 0;
    } else {
      if (0 != indexTFileSearch(sIdx->tindex, query, *result)) {
351 352 353
        indexError("corrupt at index(TFile) col:%s val: %s", term->colName, term->colVal);
        return -1;
      }
dengyihao's avatar
dengyihao 已提交
354 355 356 357 358 359
    }
  } else {
    indexError("corrupt at index(cache) col:%s val: %s", term->colName, term->colVal);
    return -1;
  }
  return 0;
dengyihao's avatar
dengyihao 已提交
360
}
dengyihao's avatar
dengyihao 已提交
361
static void indexInterResultsDestroy(SArray* results) {
dengyihao's avatar
dengyihao 已提交
362 363 364
  if (results == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
365 366 367

  size_t sz = taosArrayGetSize(results);
  for (size_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
368
    SArray* p = taosArrayGetP(results, i);
369 370
    taosArrayDestroy(p);
  }
dengyihao's avatar
dengyihao 已提交
371 372
  taosArrayDestroy(results);
}
dengyihao's avatar
dengyihao 已提交
373
static int indexMergeFinalResults(SArray* interResults, EIndexOperatorType oType, SArray* fResults) {
374
  // refactor, merge interResults into fResults by oType
dengyihao's avatar
dengyihao 已提交
375
  SArray* first = taosArrayGetP(interResults, 0);
376
  taosArraySort(first, uidCompare);
dengyihao's avatar
dengyihao 已提交
377
  taosArrayRemoveDuplicate(first, uidCompare, NULL);
dengyihao's avatar
dengyihao 已提交
378

dengyihao's avatar
dengyihao 已提交
379
  if (oType == MUST) {
380 381
    // just one column index, enhance later
    taosArrayAddAll(fResults, first);
dengyihao's avatar
dengyihao 已提交
382
  } else if (oType == SHOULD) {
383 384
    // just one column index, enhance later
    taosArrayAddAll(fResults, first);
dengyihao's avatar
dengyihao 已提交
385 386
    // tag1 condistion || tag2 condition
  } else if (oType == NOT) {
387 388 389
    // just one column index, enhance later
    taosArrayAddAll(fResults, first);
    // not use currently
dengyihao's avatar
dengyihao 已提交
390 391 392
  }
  return 0;
}
dengyihao's avatar
dengyihao 已提交
393

dengyihao's avatar
dengyihao 已提交
394 395 396 397 398
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 已提交
399
    // indexError("merge colVal: %s", lv->colVal);
dengyihao's avatar
dengyihao 已提交
400 401 402 403 404 405 406 407
    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 已提交
408
  }
dengyihao's avatar
dengyihao 已提交
409
}
dengyihao's avatar
dengyihao 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
static void indexMergeCacheAndTFile(SArray* result, IterateValue* cv, IterateValue* tv) {
  // opt
  char* colVal = (cv != NULL) ? cv->colVal : tv->colVal;
  // design merge-algorithm later, too complicated to handle all kind of situation
  TFileValue* tfv = tfileValueCreate(colVal);
  if (cv != NULL) {
    if (cv->type == ADD_VALUE) {
      taosArrayAddAll(tfv->tableId, cv->val);
    } else if (cv->type == DEL_VALUE) {
    } else if (cv->type == UPDATE_VALUE) {
    } else {
      // do nothing
    }
  }
  if (tv != NULL) {
    // opt later
    taosArrayAddAll(tfv->tableId, tv->val);
  }

  indexMergeSameKey(result, tfv);
}
dengyihao's avatar
dengyihao 已提交
431 432 433 434 435 436 437 438
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);
}
dengyihao's avatar
dengyihao 已提交
439
int indexFlushCacheToTFile(SIndex* sIdx, void* cache) {
dengyihao's avatar
dengyihao 已提交
440 441 442
  if (sIdx == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
443
  indexInfo("suid %" PRIu64 " merge cache into tindex", sIdx->suid);
dengyihao's avatar
dengyihao 已提交
444

dengyihao's avatar
dengyihao 已提交
445 446
  int64_t st = taosGetTimestampUs();

dengyihao's avatar
dengyihao 已提交
447
  IndexCache*  pCache = (IndexCache*)cache;
dengyihao's avatar
dengyihao 已提交
448
  TFileReader* pReader = tfileGetReaderByCol(sIdx->tindex, pCache->suid, pCache->colName);
dengyihao's avatar
dengyihao 已提交
449 450 451
  if (pReader == NULL) {
    indexWarn("empty tfile reader found");
  }
dengyihao's avatar
dengyihao 已提交
452 453 454
  // handle flush
  Iterate* cacheIter = indexCacheIteratorCreate(pCache);
  Iterate* tfileIter = tfileIteratorCreate(pReader);
dengyihao's avatar
dengyihao 已提交
455 456 457
  if (tfileIter == NULL) {
    indexWarn("empty tfile reader iterator");
  }
dengyihao's avatar
dengyihao 已提交
458 459 460

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

dengyihao's avatar
dengyihao 已提交
461 462
  bool cn = cacheIter ? cacheIter->next(cacheIter) : false;
  bool tn = tfileIter ? tfileIter->next(tfileIter) : false;
dengyihao's avatar
dengyihao 已提交
463 464 465 466 467 468 469 470 471 472 473 474
  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 已提交
475
    if (comp == 0) {
dengyihao's avatar
dengyihao 已提交
476
      indexMergeCacheAndTFile(result, cv, tv);
dengyihao's avatar
dengyihao 已提交
477 478 479
      cn = cacheIter->next(cacheIter);
      tn = tfileIter->next(tfileIter);
    } else if (comp < 0) {
dengyihao's avatar
dengyihao 已提交
480
      indexMergeCacheAndTFile(result, cv, NULL);
dengyihao's avatar
dengyihao 已提交
481 482
      cn = cacheIter->next(cacheIter);
    } else {
dengyihao's avatar
dengyihao 已提交
483
      indexMergeCacheAndTFile(result, NULL, tv);
dengyihao's avatar
dengyihao 已提交
484 485 486
      tn = tfileIter->next(tfileIter);
    }
  }
dengyihao's avatar
dengyihao 已提交
487 488
  int ret = indexGenTFile(sIdx, pCache, result);
  indexDestroyTempResult(result);
dengyihao's avatar
dengyihao 已提交
489

dengyihao's avatar
dengyihao 已提交
490
  indexCacheDestroyImm(pCache);
dengyihao's avatar
dengyihao 已提交
491 492 493 494

  indexCacheIteratorDestroy(cacheIter);
  tfileIteratorDestroy(tfileIter);

dengyihao's avatar
dengyihao 已提交
495 496
  tfileReaderUnRef(pReader);
  indexCacheUnRef(pCache);
dengyihao's avatar
dengyihao 已提交
497 498 499 500 501 502 503 504

  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 已提交
505
}
dengyihao's avatar
dengyihao 已提交
506 507 508
void iterateValueDestroy(IterateValue* value, bool destroy) {
  if (destroy) {
    taosArrayDestroy(value->val);
dengyihao's avatar
dengyihao 已提交
509
    value->val = NULL;
dengyihao's avatar
dengyihao 已提交
510
  } else {
dengyihao's avatar
dengyihao 已提交
511 512 513
    if (value->val != NULL) {
      taosArrayClear(value->val);
    }
dengyihao's avatar
dengyihao 已提交
514
  }
515
  free(value->colVal);
dengyihao's avatar
dengyihao 已提交
516 517
  value->colVal = NULL;
}
dengyihao's avatar
dengyihao 已提交
518 519 520 521
static int indexGenTFile(SIndex* sIdx, IndexCache* cache, SArray* batch) {
  int32_t version = CACHE_VERSION(cache);
  uint8_t colType = cache->type;

dengyihao's avatar
dengyihao 已提交
522
  TFileWriter* tw = tfileWriterOpen(sIdx->path, cache->suid, version, cache->colName, colType);
dengyihao's avatar
dengyihao 已提交
523 524 525 526 527 528 529 530 531 532 533 534
  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 已提交
535
  TFileReader* reader = tfileReaderOpen(sIdx->path, cache->suid, version, cache->colName);
dengyihao's avatar
dengyihao 已提交
536 537 538
  if (reader == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
539 540

  TFileHeader* header = &reader->header;
dengyihao's avatar
dengyihao 已提交
541
  ICacheKey    key = {.suid = cache->suid, .colName = header->colName, .nColName = strlen(header->colName)};
dengyihao's avatar
dengyihao 已提交
542 543 544 545 546 547 548

  pthread_mutex_lock(&sIdx->mtx);
  IndexTFile* ifile = (IndexTFile*)sIdx->tindex;
  tfileCachePut(ifile->cache, &key, reader);
  pthread_mutex_unlock(&sIdx->mtx);
  return ret;
END:
dengyihao's avatar
dengyihao 已提交
549 550 551 552
  if (tw != NULL) {
    writerCtxDestroy(tw->ctx, true);
    free(tw);
  }
dengyihao's avatar
dengyihao 已提交
553
  return -1;
dengyihao's avatar
dengyihao 已提交
554
}
dengyihao's avatar
dengyihao 已提交
555 556 557 558 559 560 561 562 563 564

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