indexTests.cc 35.4 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
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
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.
dengyihao's avatar
dengyihao 已提交
7 8 9 10 11 12 13 14
 *
 * 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 已提交
15
#include <gtest/gtest.h>
dengyihao's avatar
dengyihao 已提交
16
#include <algorithm>
dengyihao's avatar
dengyihao 已提交
17
#include <iostream>
dengyihao's avatar
dengyihao 已提交
18
#include <string>
dengyihao's avatar
dengyihao 已提交
19
#include <thread>
dengyihao's avatar
dengyihao 已提交
20
#include "index.h"
dengyihao's avatar
dengyihao 已提交
21 22 23
#include "indexCache.h"
#include "indexFst.h"
#include "indexFstUtil.h"
dengyihao's avatar
dengyihao 已提交
24
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
25 26
#include "indexTfile.h"
#include "indexUtil.h"
dengyihao's avatar
dengyihao 已提交
27
#include "tskiplist.h"
dengyihao's avatar
dengyihao 已提交
28
#include "tutil.h"
dengyihao's avatar
dengyihao 已提交
29
using namespace std;
dengyihao's avatar
dengyihao 已提交
30

dengyihao's avatar
dengyihao 已提交
31
#define NUM_OF_THREAD 10
dengyihao's avatar
dengyihao 已提交
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class DebugInfo {
 public:
  DebugInfo(const char* str) : info(str) {
    std::cout << "------------" << info << "\t"
              << "begin"
              << "-------------" << std::endl;
  }
  ~DebugInfo() {
    std::cout << "-----------" << info << "\t"
              << "end"
              << "--------------" << std::endl;
  }

 private:
  std::string info;
};
dengyihao's avatar
dengyihao 已提交
49

dengyihao's avatar
dengyihao 已提交
50
class FstWriter {
dengyihao's avatar
dengyihao 已提交
51 52
 public:
  FstWriter() {
dengyihao's avatar
dengyihao 已提交
53
    _wc = idxFileCtxCreate(TFile, TD_TMP_DIR_PATH "tindex", false, 64 * 1024 * 1024);
dengyihao's avatar
dengyihao 已提交
54 55 56 57 58 59 60 61 62 63 64 65
    _b = fstBuilderCreate(NULL, 0);
  }
  bool Put(const std::string& key, uint64_t val) {
    FstSlice skey = fstSliceCreate((uint8_t*)key.c_str(), key.size());
    bool     ok = fstBuilderInsert(_b, skey, val);
    fstSliceDestroy(&skey);
    return ok;
  }
  ~FstWriter() {
    fstBuilderFinish(_b);
    fstBuilderDestroy(_b);

dengyihao's avatar
dengyihao 已提交
66
    idxFileCtxDestroy(_wc, false);
dengyihao's avatar
dengyihao 已提交
67 68 69 70
  }

 private:
  FstBuilder* _b;
dengyihao's avatar
dengyihao 已提交
71
  IFileCtx*   _wc;
dengyihao's avatar
dengyihao 已提交
72 73 74
};

class FstReadMemory {
dengyihao's avatar
dengyihao 已提交
75 76
 public:
  FstReadMemory(size_t size) {
dengyihao's avatar
dengyihao 已提交
77 78
    _wc = idxFileCtxCreate(TFile, TD_TMP_DIR_PATH "tindex", true, 64 * 1024);
    _w = idxFileCreate(_wc);
dengyihao's avatar
dengyihao 已提交
79 80 81 82
    _size = size;
    memset((void*)&_s, 0, sizeof(_s));
  }
  bool init() {
wafwerar's avatar
wafwerar 已提交
83
    char* buf = (char*)taosMemoryCalloc(1, sizeof(char) * _size);
dengyihao's avatar
dengyihao 已提交
84
    int   nRead = idxFileRead(_w, (uint8_t*)buf, _size);
dengyihao's avatar
dengyihao 已提交
85 86 87
    if (nRead <= 0) {
      return false;
    }
dengyihao's avatar
dengyihao 已提交
88 89 90
    _size = nRead;
    _s = fstSliceCreate((uint8_t*)buf, _size);
    _fst = fstCreate(&_s);
wafwerar's avatar
wafwerar 已提交
91
    taosMemoryFree(buf);
dengyihao's avatar
dengyihao 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    return _fst != NULL;
  }
  bool Get(const std::string& key, uint64_t* val) {
    FstSlice skey = fstSliceCreate((uint8_t*)key.c_str(), key.size());
    bool     ok = fstGet(_fst, &skey, val);
    fstSliceDestroy(&skey);
    return ok;
  }
  bool GetWithTimeCostUs(const std::string& key, uint64_t* val, uint64_t* elapse) {
    int64_t s = taosGetTimestampUs();
    bool    ok = this->Get(key, val);
    int64_t e = taosGetTimestampUs();
    *elapse = e - s;
    return ok;
  }
  // add later
dengyihao's avatar
dengyihao 已提交
108 109 110 111
  bool Search(FAutoCtx* ctx, std::vector<uint64_t>& result) {
    FStmBuilder* sb = fstSearch(_fst, ctx);
    FStmSt*      st = stmBuilderIntoStm(sb);
    FStmStRslt*  rt = NULL;
dengyihao's avatar
dengyihao 已提交
112

dengyihao's avatar
dengyihao 已提交
113
    while ((rt = stmStNextWith(st, NULL)) != NULL) {
dengyihao's avatar
dengyihao 已提交
114 115
      result.push_back((uint64_t)(rt->out.out));
    }
dengyihao's avatar
dengyihao 已提交
116 117
    return true;
  }
dengyihao's avatar
dengyihao 已提交
118
  bool SearchWithTimeCostUs(FAutoCtx* ctx, std::vector<uint64_t>& result) {
dengyihao's avatar
dengyihao 已提交
119 120 121 122 123 124 125
    int64_t s = taosGetTimestampUs();
    bool    ok = this->Search(ctx, result);
    int64_t e = taosGetTimestampUs();
    return ok;
  }

  ~FstReadMemory() {
dengyihao's avatar
dengyihao 已提交
126
    idxFileDestroy(_w);
dengyihao's avatar
dengyihao 已提交
127
    fstDestroy(_fst);
dengyihao's avatar
dengyihao 已提交
128
    fstSliceDestroy(&_s);
dengyihao's avatar
dengyihao 已提交
129
    idxFileCtxDestroy(_wc, true);
dengyihao's avatar
dengyihao 已提交
130 131 132
  }

 private:
dengyihao's avatar
dengyihao 已提交
133 134 135 136 137
  IdxFstFile* _w;
  Fst*        _fst;
  FstSlice    _s;
  IFileCtx*   _wc;
  size_t      _size;
dengyihao's avatar
dengyihao 已提交
138 139
};

dengyihao's avatar
dengyihao 已提交
140 141 142
#define L 100
#define M 100
#define N 100
dengyihao's avatar
dengyihao 已提交
143

dengyihao's avatar
dengyihao 已提交
144 145
int Performance_fstWriteRecords(FstWriter* b) {
  std::string str("aa");
dengyihao's avatar
dengyihao 已提交
146
  for (int i = 0; i < L; i++) {
dengyihao's avatar
dengyihao 已提交
147
    str[0] = 'a' + i;
dengyihao's avatar
dengyihao 已提交
148 149
    str.resize(2);
    for (int j = 0; j < M; j++) {
dengyihao's avatar
dengyihao 已提交
150 151
      str[1] = 'a' + j;
      str.resize(2);
dengyihao's avatar
dengyihao 已提交
152
      for (int k = 0; k < N; k++) {
dengyihao's avatar
dengyihao 已提交
153 154
        str.push_back('a');
        b->Put(str, k);
dengyihao's avatar
dengyihao 已提交
155
        printf("(%d, %d, %d, %s)\n", i, j, k, str.c_str());
dengyihao's avatar
dengyihao 已提交
156
      }
dengyihao's avatar
dengyihao 已提交
157
    }
dengyihao's avatar
dengyihao 已提交
158
  }
dengyihao's avatar
dengyihao 已提交
159
  return L * M * N;
dengyihao's avatar
dengyihao 已提交
160
}
dengyihao's avatar
dengyihao 已提交
161
void Performance_fstReadRecords(FstReadMemory* m) {
dengyihao's avatar
dengyihao 已提交
162 163 164
  std::string str("aa");
  for (int i = 0; i < M; i++) {
    str[0] = 'a' + i;
dengyihao's avatar
dengyihao 已提交
165 166
    str.resize(2);
    for (int j = 0; j < N; j++) {
dengyihao's avatar
dengyihao 已提交
167 168 169 170
      str[1] = 'a' + j;
      str.resize(2);
      for (int k = 0; k < L; k++) {
        str.push_back('a');
dengyihao's avatar
dengyihao 已提交
171
        uint64_t val, cost;
dengyihao's avatar
dengyihao 已提交
172
        if (m->GetWithTimeCostUs(str, &val, &cost)) {
dengyihao's avatar
dengyihao 已提交
173
          printf("succes to get kv(%s, %" PRId64 "), cost: %" PRId64 "\n", str.c_str(), val, cost);
dengyihao's avatar
dengyihao 已提交
174 175 176 177
        } else {
          printf("failed to get key: %s\n", str.c_str());
        }
      }
dengyihao's avatar
dengyihao 已提交
178
    }
dengyihao's avatar
dengyihao 已提交
179
  }
dengyihao's avatar
dengyihao 已提交
180
}
dengyihao's avatar
dengyihao 已提交
181
void checkFstPerf() {
dengyihao's avatar
dengyihao 已提交
182 183
  FstWriter* fw = new FstWriter;
  int64_t    s = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
184

dengyihao's avatar
dengyihao 已提交
185
  int     num = Performance_fstWriteRecords(fw);
dengyihao's avatar
dengyihao 已提交
186
  int64_t e = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
187
  printf("write %d record cost %" PRId64 "us\n", num, e - s);
dengyihao's avatar
dengyihao 已提交
188 189
  delete fw;

dengyihao's avatar
dengyihao 已提交
190
  FstReadMemory* m = new FstReadMemory(1024 * 64);
dengyihao's avatar
dengyihao 已提交
191 192 193
  if (m->init()) {
    printf("success to init fst read");
  }
dengyihao's avatar
dengyihao 已提交
194
  Performance_fstReadRecords(m);
dengyihao's avatar
dengyihao 已提交
195
  delete m;
dengyihao's avatar
dengyihao 已提交
196
}
dengyihao's avatar
dengyihao 已提交
197
void checkFstPrefixSearch() {
dengyihao's avatar
dengyihao 已提交
198 199 200
  FstWriter*  fw = new FstWriter;
  int64_t     s = taosGetTimestampUs();
  int         count = 2;
dengyihao's avatar
dengyihao 已提交
201
  std::string key("ab");
dengyihao's avatar
dengyihao 已提交
202

dengyihao's avatar
dengyihao 已提交
203
  for (int i = 0; i < count; i++) {
dengyihao's avatar
dengyihao 已提交
204 205
    key[1] = key[1] + i;
    fw->Put(key, i);
dengyihao's avatar
dengyihao 已提交
206 207
  }
  int64_t e = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
208

dengyihao's avatar
dengyihao 已提交
209 210
  std::cout << "insert data count :  " << count << "elapas time: " << e - s << std::endl;
  delete fw;
dengyihao's avatar
dengyihao 已提交
211

dengyihao's avatar
dengyihao 已提交
212
  FstReadMemory* m = new FstReadMemory(1024 * 64);
dengyihao's avatar
dengyihao 已提交
213
  if (m->init() == false) {
dengyihao's avatar
dengyihao 已提交
214
    std::cout << "init readMemory failed" << std::endl;
dengyihao's avatar
dengyihao 已提交
215 216 217
    delete m;
    return;
  }
dengyihao's avatar
dengyihao 已提交
218 219

  // prefix search
dengyihao's avatar
dengyihao 已提交
220
  std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
221

dengyihao's avatar
dengyihao 已提交
222
  FAutoCtx* ctx = automCtxCreate((void*)"ab", AUTOMATION_PREFIX);
dengyihao's avatar
dengyihao 已提交
223 224
  m->Search(ctx, result);
  assert(result.size() == count);
dengyihao's avatar
dengyihao 已提交
225
  for (int i = 0; i < result.size(); i++) {
dengyihao's avatar
dengyihao 已提交
226
    assert(result[i] == i);  // check result
dengyihao's avatar
dengyihao 已提交
227 228
  }

wafwerar's avatar
wafwerar 已提交
229
  taosMemoryFree(ctx);
dengyihao's avatar
dengyihao 已提交
230
  delete m;
dengyihao's avatar
dengyihao 已提交
231
}
dengyihao's avatar
dengyihao 已提交
232
void validateFst() {
dengyihao's avatar
dengyihao 已提交
233 234 235 236
  int        val = 100;
  int        count = 100;
  FstWriter* fw = new FstWriter;
  // write
dengyihao's avatar
dengyihao 已提交
237
  {
dengyihao's avatar
dengyihao 已提交
238
    std::string key("ab");
dengyihao's avatar
dengyihao 已提交
239
    for (int i = 0; i < count; i++) {
dengyihao's avatar
dengyihao 已提交
240
      key.push_back('a' + i);
dengyihao's avatar
dengyihao 已提交
241
      fw->Put(key, val - i);
dengyihao's avatar
dengyihao 已提交
242
    }
dengyihao's avatar
dengyihao 已提交
243 244
  }
  delete fw;
dengyihao's avatar
dengyihao 已提交
245

dengyihao's avatar
dengyihao 已提交
246
  // read
dengyihao's avatar
dengyihao 已提交
247 248 249
  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init() == false) {
    std::cout << "init readMemory failed" << std::endl;
dengyihao's avatar
dengyihao 已提交
250 251
    delete m;
    return;
dengyihao's avatar
dengyihao 已提交
252
  }
dengyihao's avatar
dengyihao 已提交
253 254

  {
dengyihao's avatar
dengyihao 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    std::string key("ab");
    uint64_t    out;
    if (m->Get(key, &out)) {
      printf("success to get (%s, %" PRId64 ")\n", key.c_str(), out);
    } else {
      printf("failed to get(%s)\n", key.c_str());
    }
    for (int i = 0; i < count; i++) {
      key.push_back('a' + i);
      if (m->Get(key, &out)) {
        assert(val - i == out);
        printf("success to get (%s, %" PRId64 ")\n", key.c_str(), out);
      } else {
        printf("failed to get(%s)\n", key.c_str());
      }
dengyihao's avatar
dengyihao 已提交
270
    }
dengyihao's avatar
dengyihao 已提交
271
  }
dengyihao's avatar
dengyihao 已提交
272
  delete m;
dengyihao's avatar
dengyihao 已提交
273
}
wafwerar's avatar
wafwerar 已提交
274
static std::string logDir = TD_TMP_DIR_PATH "log";
dengyihao's avatar
dengyihao 已提交
275 276 277 278 279 280

static void initLog() {
  const char*   defaultLogFileNamePrefix = "taoslog";
  const int32_t maxLogFileNum = 10;

  tsAsyncLog = 0;
dengyihao's avatar
dengyihao 已提交
281
  idxDebugFlag = 143;
dengyihao's avatar
dengyihao 已提交
282 283 284 285 286 287 288 289
  strcpy(tsLogDir, logDir.c_str());
  taosRemoveDir(tsLogDir);
  taosMkDir(tsLogDir);

  if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) {
    printf("failed to open log file in directory:%s\n", tsLogDir);
  }
}
dengyihao's avatar
dengyihao 已提交
290
class IndexEnv : public ::testing::Test {
dengyihao's avatar
dengyihao 已提交
291 292
 protected:
  virtual void SetUp() {
dengyihao's avatar
dengyihao 已提交
293
    initLog();
dengyihao's avatar
dengyihao 已提交
294 295 296 297 298 299 300 301 302 303
    taosRemoveDir(path);
    opts = indexOptsCreate();
    int ret = indexOpen(opts, path, &index);
    assert(ret == 0);
  }
  virtual void TearDown() {
    indexClose(index);
    indexOptsDestroy(opts);
  }

304
  const char* path = TD_TMP_DIR_PATH "tindex";
dengyihao's avatar
dengyihao 已提交
305 306
  SIndexOpts* opts;
  SIndex*     index;
dengyihao's avatar
dengyihao 已提交
307 308
};

309 310 311 312 313
/// TEST_F(IndexEnv, testPut) {
//  /  // single index column
//      / {
//    / std::string colName("tag1"), colVal("Hello world");
//    / SIndexTerm* term =
314 315
//        indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), /
//        colVal.size());
316 317 318 319 320 321 322
//    SIndexMultiTerm* terms = indexMultiTermCreate();
//    indexMultiTermAdd(terms, term);
//    / / for (size_t i = 0; i < 100; i++) {
//      / int tableId = i;
//      / int ret = indexPut(index, terms, tableId);
//      / assert(ret == 0);
//      /
dengyihao's avatar
dengyihao 已提交
323
//    }
324 325
//    / indexMultiTermDestroy(terms);
//    /
dengyihao's avatar
dengyihao 已提交
326
//  }
327 328 329 330 331 332
//  /  // multi index column
//      / {
//    / SIndexMultiTerm* terms = indexMultiTermCreate();
//    / {
//      / std::string colName("tag1"), colVal("Hello world");
//      / SIndexTerm* term =
333 334
//          / indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(),
//          colVal.size());
335 336
//      / indexMultiTermAdd(terms, term);
//      /
dengyihao's avatar
dengyihao 已提交
337
//    }
338 339 340
//    / {
//      / std::string colName("tag2"), colVal("Hello world");
//      / SIndexTerm* term =
341 342
//          / indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(),
//          colVal.size());
343 344
//      / indexMultiTermAdd(terms, term);
//      /
dengyihao's avatar
dengyihao 已提交
345
//    }
346 347 348 349 350
//    / / for (int i = 0; i < 100; i++) {
//      / int tableId = i;
//      / int ret = indexPut(index, terms, tableId);
//      / assert(ret == 0);
//      /
dengyihao's avatar
dengyihao 已提交
351
//    }
352 353
//    / indexMultiTermDestroy(terms);
//    /
dengyihao's avatar
dengyihao 已提交
354
//  }
355 356
//  /  //
//      /
dengyihao's avatar
dengyihao 已提交
357 358
//}

dengyihao's avatar
dengyihao 已提交
359 360
class TFileObj {
 public:
361
  TFileObj(const std::string& path = TD_TMP_DIR_PATH "tindex", const std::string& colName = "voltage")
362
      : path_(path), colName_(colName) {
dengyihao's avatar
dengyihao 已提交
363
    colId_ = 10;
dengyihao's avatar
dengyihao 已提交
364 365
    reader_ = NULL;
    writer_ = NULL;
dengyihao's avatar
dengyihao 已提交
366 367 368 369 370 371 372 373
    // Do Nothing
    //
  }
  int Put(SArray* tv) {
    if (reader_ != NULL) {
      tfileReaderDestroy(reader_);
      reader_ = NULL;
    }
dengyihao's avatar
dengyihao 已提交
374 375 376
    if (writer_ == NULL) {
      InitWriter();
    }
dengyihao's avatar
dengyihao 已提交
377
    return tfileWriterPut(writer_, tv, false);
dengyihao's avatar
dengyihao 已提交
378 379 380
  }
  bool InitWriter() {
    TFileHeader header;
dengyihao's avatar
dengyihao 已提交
381 382
    header.suid = 1;
    header.version = 1;
dengyihao's avatar
dengyihao 已提交
383
    memcpy(header.colName, colName_.c_str(), colName_.size());
dengyihao's avatar
dengyihao 已提交
384 385
    header.colType = TSDB_DATA_TYPE_BINARY;

dengyihao's avatar
dengyihao 已提交
386
    std::string path(path_);
dengyihao's avatar
dengyihao 已提交
387 388
    int         colId = 2;
    char        buf[64] = {0};
dengyihao's avatar
dengyihao 已提交
389
    sprintf(buf, "%" PRIu64 "-%d-%" PRId64 ".tindex", header.suid, colId_, header.version);
dengyihao's avatar
dengyihao 已提交
390 391
    path.append("/").append(buf);

dengyihao's avatar
dengyihao 已提交
392 393
    fileName_ = path;

dengyihao's avatar
dengyihao 已提交
394
    IFileCtx* ctx = idxFileCtxCreate(TFile, path.c_str(), false, 64 * 1024 * 1024);
dengyihao's avatar
dengyihao 已提交
395 396 397 398 399

    writer_ = tfileWriterCreate(ctx, &header);
    return writer_ != NULL ? true : false;
  }
  bool InitReader() {
dengyihao's avatar
dengyihao 已提交
400
    IFileCtx* ctx = idxFileCtxCreate(TFile, fileName_.c_str(), true, 64 * 1024 * 1024);
dengyihao's avatar
dengyihao 已提交
401 402 403 404 405 406 407 408 409 410 411 412
    reader_ = tfileReaderCreate(ctx);
    return reader_ != NULL ? true : false;
  }
  int Get(SIndexTermQuery* query, SArray* result) {
    if (writer_ != NULL) {
      tfileWriterDestroy(writer_);
      writer_ = NULL;
    }
    if (reader_ == NULL && InitReader()) {
      //
      //
    }
dengyihao's avatar
dengyihao 已提交
413
    SIdxTRslt* tr = idxTRsltCreate();
dengyihao's avatar
dengyihao 已提交
414 415 416

    int ret = tfileReaderSearch(reader_, query, tr);

dengyihao's avatar
dengyihao 已提交
417 418
    idxTRsltMergeTo(tr, result);
    idxTRsltDestroy(tr);
dengyihao's avatar
dengyihao 已提交
419
    return ret;
dengyihao's avatar
dengyihao 已提交
420 421
  }
  ~TFileObj() {
dengyihao's avatar
dengyihao 已提交
422 423 424 425 426 427
    if (writer_) {
      tfileWriterDestroy(writer_);
    }
    if (reader_) {
      tfileReaderDestroy(reader_);
    }
dengyihao's avatar
dengyihao 已提交
428 429 430 431 432 433 434 435 436 437 438 439
  }

 private:
  std::string path_;
  std::string colName_;
  std::string fileName_;

  TFileWriter* writer_;
  TFileReader* reader_;

  int colId_;
};
dengyihao's avatar
dengyihao 已提交
440

dengyihao's avatar
dengyihao 已提交
441 442 443 444 445 446
class IndexTFileEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
    taosRemoveDir(dir.c_str());
    taosMkDir(dir.c_str());
    fObj = new TFileObj(dir, colName);
dengyihao's avatar
dengyihao 已提交
447 448 449 450 451
  }

  virtual void TearDown() {
    // indexClose(index);
    // indexeptsDestroy(opts);
dengyihao's avatar
dengyihao 已提交
452 453
    delete fObj;
    // tfileWriterDestroy(twrite);
dengyihao's avatar
dengyihao 已提交
454
  }
dengyihao's avatar
dengyihao 已提交
455
  TFileObj*   fObj;
456
  std::string dir = TD_TMP_DIR_PATH "tindex";
dengyihao's avatar
dengyihao 已提交
457 458 459 460 461
  std::string colName = "voltage";

  int coldId = 2;
  int version = 1;
  int colType = TSDB_DATA_TYPE_BINARY;
dengyihao's avatar
dengyihao 已提交
462 463 464
};

static TFileValue* genTFileValue(const char* val) {
wafwerar's avatar
wafwerar 已提交
465
  TFileValue* tv = (TFileValue*)taosMemoryCalloc(1, sizeof(TFileValue));
dengyihao's avatar
dengyihao 已提交
466
  int32_t     vlen = strlen(val) + 1;
wafwerar's avatar
wafwerar 已提交
467
  tv->colVal = (char*)taosMemoryCalloc(1, vlen);
dengyihao's avatar
dengyihao 已提交
468 469 470
  memcpy(tv->colVal, val, vlen);

  tv->tableId = (SArray*)taosArrayInit(1, sizeof(uint64_t));
dengyihao's avatar
dengyihao 已提交
471
  for (size_t i = 0; i < 200; i++) {
dengyihao's avatar
dengyihao 已提交
472 473 474 475 476 477 478
    uint64_t v = i;
    taosArrayPush(tv->tableId, &v);
  }
  return tv;
}
static void destroyTFileValue(void* val) {
  TFileValue* tv = (TFileValue*)val;
wafwerar's avatar
wafwerar 已提交
479
  taosMemoryFree(tv->colVal);
dengyihao's avatar
dengyihao 已提交
480
  taosArrayDestroy(tv->tableId);
wafwerar's avatar
wafwerar 已提交
481
  taosMemoryFree(tv);
dengyihao's avatar
dengyihao 已提交
482
}
dengyihao's avatar
dengyihao 已提交
483
TEST_F(IndexTFileEnv, test_tfile_write) {
dengyihao's avatar
dengyihao 已提交
484
  TFileValue* v1 = genTFileValue("ab");
dengyihao's avatar
dengyihao 已提交
485

dengyihao's avatar
dengyihao 已提交
486
  SArray* data = (SArray*)taosArrayInit(4, sizeof(void*));
dengyihao's avatar
dengyihao 已提交
487

dengyihao's avatar
dengyihao 已提交
488
  taosArrayPush(data, &v1);
dengyihao's avatar
dengyihao 已提交
489 490 491
  // taosArrayPush(data, &v2);
  // taosArrayPush(data, &v3);
  // taosArrayPush(data, &v4);
dengyihao's avatar
dengyihao 已提交
492

dengyihao's avatar
dengyihao 已提交
493
  fObj->Put(data);
494 495 496 497
  for (size_t i = 0; i < taosArrayGetSize(data); i++) {
    // data
    destroyTFileValue(taosArrayGetP(data, i));
  }
dengyihao's avatar
dengyihao 已提交
498
  taosArrayDestroy(data);
dengyihao's avatar
dengyihao 已提交
499

dengyihao's avatar
dengyihao 已提交
500 501 502 503 504 505 506 507 508
  std::string colName("voltage");
  std::string colVal("ab");

  char    buf[256] = {0};
  int16_t sz = colVal.size();
  memcpy(buf, (uint16_t*)&sz, 2);
  memcpy(buf + 2, colVal.c_str(), colVal.size());
  SIndexTerm* term =
      indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), buf, sizeof(buf));
dengyihao's avatar
dengyihao 已提交
509
  SIndexTermQuery query = {term, QUERY_TERM};
dengyihao's avatar
dengyihao 已提交
510 511 512

  SArray* result = (SArray*)taosArrayInit(1, sizeof(uint64_t));
  fObj->Get(&query, result);
dengyihao's avatar
dengyihao 已提交
513
  assert(taosArrayGetSize(result) == 200);
dengyihao's avatar
dengyihao 已提交
514
  indexTermDestroy(term);
515
  taosArrayDestroy(result);
dengyihao's avatar
dengyihao 已提交
516 517

  // tfileWriterDestroy(twrite);
dengyihao's avatar
dengyihao 已提交
518
}
dengyihao's avatar
dengyihao 已提交
519 520 521 522
class CacheObj {
 public:
  CacheObj() {
    // TODO
dengyihao's avatar
dengyihao 已提交
523
    cache = idxCacheCreate(NULL, 0, "voltage", TSDB_DATA_TYPE_BINARY);
dengyihao's avatar
dengyihao 已提交
524 525
  }
  int Put(SIndexTerm* term, int16_t colId, int32_t version, uint64_t uid) {
dengyihao's avatar
dengyihao 已提交
526
    int ret = idxCachePut(cache, term, uid);
dengyihao's avatar
dengyihao 已提交
527 528 529 530 531 532
    if (ret != 0) {
      //
      std::cout << "failed to put into cache: " << ret << std::endl;
    }
    return ret;
  }
dengyihao's avatar
dengyihao 已提交
533 534
  void Debug() {
    //
dengyihao's avatar
dengyihao 已提交
535
    idxCacheDebug(cache);
dengyihao's avatar
dengyihao 已提交
536
  }
dengyihao's avatar
dengyihao 已提交
537
  int Get(SIndexTermQuery* query, int16_t colId, int32_t version, SArray* result, STermValueType* s) {
dengyihao's avatar
dengyihao 已提交
538
    SIdxTRslt* tr = idxTRsltCreate();
dengyihao's avatar
dengyihao 已提交
539

dengyihao's avatar
dengyihao 已提交
540
    int ret = idxCacheSearch(cache, query, tr, s);
dengyihao's avatar
dengyihao 已提交
541 542
    idxTRsltMergeTo(tr, result);
    idxTRsltDestroy(tr);
dengyihao's avatar
dengyihao 已提交
543

dengyihao's avatar
dengyihao 已提交
544 545 546 547 548 549 550
    if (ret != 0) {
      std::cout << "failed to get from cache:" << ret << std::endl;
    }
    return ret;
  }
  ~CacheObj() {
    // TODO
dengyihao's avatar
dengyihao 已提交
551
    idxCacheDestroy(cache);
dengyihao's avatar
dengyihao 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  }

 private:
  IndexCache* cache = NULL;
};

class IndexCacheEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
    // TODO
    coj = new CacheObj();
  }
  virtual void TearDown() {
    delete coj;
    // formate
  }
  CacheObj* coj;
};

dengyihao's avatar
dengyihao 已提交
571 572 573 574 575 576 577 578 579 580 581 582
SIndexTerm* indexTermCreateT(int64_t suid, SIndexOperOnColumn oper, uint8_t colType, const char* colName,
                             int32_t nColName, const char* colVal, int32_t nColVal) {
  char    buf[256] = {0};
  int16_t sz = nColVal;
  memcpy(buf, (uint16_t*)&sz, 2);
  memcpy(buf + 2, colVal, nColVal);
  if (colType == TSDB_DATA_TYPE_BINARY) {
    return indexTermCreate(suid, oper, colType, colName, nColName, buf, sizeof(buf));
  } else {
    return indexTermCreate(suid, oper, colType, colName, nColName, colVal, nColVal);
  }
}
583
#define MAX_TERM_KEY_LEN 128
dengyihao's avatar
dengyihao 已提交
584
TEST_F(IndexCacheEnv, cache_test) {
585 586
  int     version = 0;
  int16_t colId = 0;
dengyihao's avatar
dengyihao 已提交
587
  int16_t othColId = 10;
dengyihao's avatar
dengyihao 已提交
588

589
  uint64_t    suid = 0;
dengyihao's avatar
dengyihao 已提交
590 591
  std::string colName("voltage");
  {
592
    std::string colVal("v1");
dengyihao's avatar
dengyihao 已提交
593 594
    SIndexTerm* term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
595
    coj->Put(term, colId, version++, suid++);
596
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
597
    // indexTermDestry(term);
dengyihao's avatar
dengyihao 已提交
598 599
  }
  {
600
    std::string colVal("v3");
dengyihao's avatar
dengyihao 已提交
601 602
    SIndexTerm* term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
603
    coj->Put(term, colId, version++, suid++);
604
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
605 606
  }
  {
607
    std::string colVal("v2");
dengyihao's avatar
dengyihao 已提交
608 609
    SIndexTerm* term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
610
    coj->Put(term, colId, version++, suid++);
611
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
612 613
  }
  {
614
    std::string colVal("v3");
dengyihao's avatar
dengyihao 已提交
615 616
    SIndexTerm* term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
617
    coj->Put(term, colId, version++, suid++);
618
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
619 620
  }
  {
621
    std::string colVal("v3");
dengyihao's avatar
dengyihao 已提交
622 623
    SIndexTerm* term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
624
    coj->Put(term, colId, version++, suid++);
625
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
626
  }
dengyihao's avatar
dengyihao 已提交
627 628
  coj->Debug();
  std::cout << "--------first----------" << std::endl;
dengyihao's avatar
dengyihao 已提交
629 630
  {
    std::string colVal("v3");
dengyihao's avatar
dengyihao 已提交
631 632
    SIndexTerm* term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
633
    coj->Put(term, othColId, version++, suid++);
634
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
635 636 637
  }
  {
    std::string colVal("v4");
dengyihao's avatar
dengyihao 已提交
638 639
    SIndexTerm* term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
640
    coj->Put(term, othColId, version++, suid++);
641
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
642
  }
dengyihao's avatar
dengyihao 已提交
643 644
  coj->Debug();
  std::cout << "--------second----------" << std::endl;
dengyihao's avatar
dengyihao 已提交
645
  {
646
    std::string colVal("v4");
dengyihao's avatar
dengyihao 已提交
647
    for (size_t i = 0; i < 10; i++) {
648
      colVal[colVal.size() - 1] = 'a' + i;
dengyihao's avatar
dengyihao 已提交
649 650
      SIndexTerm* term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                          colVal.c_str(), colVal.size());
651
      coj->Put(term, colId, version++, suid++);
652
      indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
653 654
    }
  }
dengyihao's avatar
dengyihao 已提交
655 656
  coj->Debug();
  // begin query
dengyihao's avatar
dengyihao 已提交
657
  {
658
    std::string     colVal("v3");
dengyihao's avatar
dengyihao 已提交
659 660
    SIndexTerm*     term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
661
    SIndexTermQuery query = {term, QUERY_TERM};
662 663 664 665
    SArray*         ret = (SArray*)taosArrayInit(4, sizeof(suid));
    STermValueType  valType;

    coj->Get(&query, colId, 10000, ret, &valType);
dengyihao's avatar
dengyihao 已提交
666
    std::cout << "size : " << taosArrayGetSize(ret) << std::endl;
dengyihao's avatar
dengyihao 已提交
667
    assert(taosArrayGetSize(ret) == 4);
668 669 670
    taosArrayDestroy(ret);

    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
671
  }
672
  {
673
    std::string     colVal("v2");
dengyihao's avatar
dengyihao 已提交
674 675
    SIndexTerm*     term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
676
    SIndexTermQuery query = {term, QUERY_TERM};
677 678
    SArray*         ret = (SArray*)taosArrayInit(4, sizeof(suid));
    STermValueType  valType;
dengyihao's avatar
dengyihao 已提交
679

680 681
    coj->Get(&query, colId, 10000, ret, &valType);
    assert(taosArrayGetSize(ret) == 1);
682 683 684
    taosArrayDestroy(ret);

    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
685 686
  }
}
dengyihao's avatar
dengyihao 已提交
687 688 689 690 691 692
class IndexObj {
 public:
  IndexObj() {
    // opt
    numOfWrite = 0;
    numOfRead = 0;
693
    // indexInit();
dengyihao's avatar
dengyihao 已提交
694
  }
695 696 697 698 699
  int Init(const std::string& dir, bool remove = true) {
    if (remove) {
      taosRemoveDir(dir.c_str());
      taosMkDir(dir.c_str());
    }
dengyihao's avatar
dengyihao 已提交
700 701 702 703 704 705 706 707
    taosMkDir(dir.c_str());
    int ret = indexOpen(&opts, dir.c_str(), &idx);
    if (ret != 0) {
      // opt
      std::cout << "failed to open index: %s" << dir << std::endl;
    }
    return ret;
  }
dengyihao's avatar
dengyihao 已提交
708
  void Del(const std::string& colName, const std::string& colVal, uint64_t uid) {
dengyihao's avatar
dengyihao 已提交
709 710
    SIndexTerm*      term = indexTermCreateT(0, DEL_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
711 712 713 714 715
    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    Put(terms, uid);
    indexMultiTermDestroy(terms);
  }
dengyihao's avatar
dengyihao 已提交
716 717
  int WriteMillonData(const std::string& colName, const std::string& colVal = "Hello world",
                      size_t numOfTable = 100 * 10000) {
dengyihao's avatar
dengyihao 已提交
718 719
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
720 721 722 723 724 725 726 727 728
    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < numOfTable; i++) {
      int ret = Put(terms, i);
      assert(ret == 0);
    }
    indexMultiTermDestroy(terms);
    return numOfTable;
  }
dengyihao's avatar
dengyihao 已提交
729 730 731
  int WriteMultiMillonData(const std::string& colName, const std::string& colVal = "Hello world",
                           size_t numOfTable = 100 * 10000) {
    std::string tColVal = colVal;
dengyihao's avatar
dengyihao 已提交
732
    size_t      colValSize = tColVal.size();
dengyihao's avatar
dengyihao 已提交
733 734
    int         skip = 100;
    numOfTable /= skip;
dengyihao's avatar
dengyihao 已提交
735
    for (int i = 0; i < numOfTable; i++) {
dengyihao's avatar
dengyihao 已提交
736 737
      for (int k = 0; k < 10 && k < colVal.size(); k++) {
        // opt
wafwerar's avatar
wafwerar 已提交
738
        tColVal[taosRand() % colValSize] = 'a' + k % 26;
dengyihao's avatar
dengyihao 已提交
739
      }
dengyihao's avatar
dengyihao 已提交
740 741
      SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                          tColVal.c_str(), tColVal.size());
dengyihao's avatar
dengyihao 已提交
742 743
      SIndexMultiTerm* terms = indexMultiTermCreate();
      indexMultiTermAdd(terms, term);
dengyihao's avatar
dengyihao 已提交
744 745
      for (size_t j = 0; j < skip; j++) {
        int ret = Put(terms, j);
dengyihao's avatar
dengyihao 已提交
746 747 748 749 750 751
        assert(ret == 0);
      }
      indexMultiTermDestroy(terms);
    }
    return numOfTable;
  }
dengyihao's avatar
dengyihao 已提交
752
  int ReadMultiMillonData(const std::string& colName, const std::string& colVal = "Hello world",
dengyihao's avatar
dengyihao 已提交
753
                          size_t numOfTable = 100) {
dengyihao's avatar
dengyihao 已提交
754 755 756 757 758 759 760 761 762
    std::string tColVal = colVal;

    int colValSize = tColVal.size();
    for (int i = 0; i < numOfTable; i++) {
      tColVal[i % colValSize] = 'a' + i % 26;
      SearchOne(colName, tColVal);
    }
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
763

dengyihao's avatar
dengyihao 已提交
764 765 766 767 768 769 770 771 772 773
  int Put(SIndexMultiTerm* fvs, uint64_t uid) {
    numOfWrite += taosArrayGetSize(fvs);
    return indexPut(idx, fvs, uid);
  }
  int Search(SIndexMultiTermQuery* multiQ, SArray* result) {
    SArray* query = multiQ->query;
    numOfRead = taosArrayGetSize(query);
    return indexSearch(idx, multiQ, result);
  }

dengyihao's avatar
dengyihao 已提交
774 775
  int SearchOne(const std::string& colName, const std::string& colVal) {
    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
776 777
    SIndexTerm*           term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
778 779 780
    indexMultiTermQueryAdd(mq, term, QUERY_TERM);

    SArray* result = (SArray*)taosArrayInit(1, sizeof(uint64_t));
dengyihao's avatar
dengyihao 已提交
781

dengyihao's avatar
dengyihao 已提交
782 783 784 785 786 787
    int64_t s = taosGetTimestampUs();
    if (Search(mq, result) == 0) {
      int64_t e = taosGetTimestampUs();
      std::cout << "search and time cost:" << e - s << "\tquery col:" << colName << "\t val: " << colVal
                << "\t size:" << taosArrayGetSize(result) << std::endl;
    } else {
dengyihao's avatar
dengyihao 已提交
788
      return -1;
dengyihao's avatar
dengyihao 已提交
789 790 791 792 793 794 795 796 797
    }
    int sz = taosArrayGetSize(result);
    indexMultiTermQueryDestroy(mq);
    taosArrayDestroy(result);
    return sz;
    // assert(taosArrayGetSize(result) == targetSize);
  }
  int SearchOneTarget(const std::string& colName, const std::string& colVal, uint64_t val) {
    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
798 799
    SIndexTerm*           term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
800 801 802 803
    indexMultiTermQueryAdd(mq, term, QUERY_TERM);

    SArray* result = (SArray*)taosArrayInit(1, sizeof(uint64_t));

dengyihao's avatar
dengyihao 已提交
804 805 806
    int64_t s = taosGetTimestampUs();
    if (Search(mq, result) == 0) {
      int64_t e = taosGetTimestampUs();
dengyihao's avatar
add UT  
dengyihao 已提交
807
      std::cout << "search one successfully and time cost:" << e - s << "us\tquery col:" << colName
dengyihao's avatar
dengyihao 已提交
808
                << "\t val: " << colVal << "\t size:" << taosArrayGetSize(result) << std::endl;
dengyihao's avatar
dengyihao 已提交
809 810
    } else {
    }
811 812
    int sz = taosArrayGetSize(result);
    indexMultiTermQueryDestroy(mq);
dengyihao's avatar
dengyihao 已提交
813 814 815
    assert(sz == 1);
    uint64_t* ret = (uint64_t*)taosArrayGet(result, 0);
    assert(val = *ret);
dengyihao's avatar
dengyihao 已提交
816
    taosArrayDestroy(result);
dengyihao's avatar
dengyihao 已提交
817

818
    return sz;
dengyihao's avatar
dengyihao 已提交
819
  }
dengyihao's avatar
dengyihao 已提交
820

dengyihao's avatar
dengyihao 已提交
821
  void PutOne(const std::string& colName, const std::string& colVal) {
822
    SIndexMultiTerm* terms = indexMultiTermCreate();
dengyihao's avatar
dengyihao 已提交
823 824
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
825 826 827 828
    indexMultiTermAdd(terms, term);
    Put(terms, 10);
    indexMultiTermDestroy(terms);
  }
dengyihao's avatar
dengyihao 已提交
829 830
  void PutOneTarge(const std::string& colName, const std::string& colVal, uint64_t val) {
    SIndexMultiTerm* terms = indexMultiTermCreate();
dengyihao's avatar
dengyihao 已提交
831 832
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
833 834 835 836
    indexMultiTermAdd(terms, term);
    Put(terms, val);
    indexMultiTermDestroy(terms);
  }
dengyihao's avatar
dengyihao 已提交
837 838 839 840 841 842
  void Debug() {
    std::cout << "numOfWrite:" << numOfWrite << std::endl;
    std::cout << "numOfRead:" << numOfRead << std::endl;
  }

  ~IndexObj() {
dengyihao's avatar
dengyihao 已提交
843
    // indexCleanUp();
dengyihao's avatar
dengyihao 已提交
844
    indexClose(idx);
dengyihao's avatar
dengyihao 已提交
845 846 847 848 849 850 851 852 853 854 855
  }

 private:
  SIndexOpts opts;
  SIndex*    idx;
  int        numOfWrite;
  int        numOfRead;
};

class IndexEnv2 : public ::testing::Test {
 protected:
dengyihao's avatar
dengyihao 已提交
856 857 858 859
  virtual void SetUp() {
    initLog();
    index = new IndexObj();
  }
860 861 862 863 864
  virtual void TearDown() {
    // taosMsleep(500);
    delete index;
  }
  IndexObj* index;
dengyihao's avatar
dengyihao 已提交
865 866
};
TEST_F(IndexEnv2, testIndexOpen) {
867
  std::string path = TD_TMP_DIR_PATH "test";
868 869 870 871 872
  if (index->Init(path) != 0) {
    std::cout << "failed to init index" << std::endl;
    exit(1);
  }

dengyihao's avatar
dengyihao 已提交
873
  int targetSize = 200;
874
  {
dengyihao's avatar
dengyihao 已提交
875
    std::string colName("tag1"), colVal("Hello");
876

dengyihao's avatar
dengyihao 已提交
877 878
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
879 880 881 882 883 884 885 886 887 888
    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < targetSize; i++) {
      int tableId = i;
      int ret = index->Put(terms, tableId);
      assert(ret == 0);
    }
    indexMultiTermDestroy(terms);
  }
  {
dengyihao's avatar
dengyihao 已提交
889 890
    size_t      size = 200;
    std::string colName("tag1"), colVal("hello");
891

dengyihao's avatar
dengyihao 已提交
892 893
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
894 895 896 897 898 899 900 901 902 903
    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < size; i++) {
      int tableId = i;
      int ret = index->Put(terms, tableId);
      assert(ret == 0);
    }
    indexMultiTermDestroy(terms);
  }
  {
dengyihao's avatar
dengyihao 已提交
904
    size_t      size = 200;
dengyihao's avatar
dengyihao 已提交
905
    std::string colName("tag1"), colVal("Hello");
906

dengyihao's avatar
dengyihao 已提交
907 908
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
909 910 911 912 913 914 915 916 917 918 919 920
    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = size * 3; i < size * 4; i++) {
      int tableId = i;
      int ret = index->Put(terms, tableId);
      assert(ret == 0);
    }
    indexMultiTermDestroy(terms);
  }

  {
    std::string           colName("tag1"), colVal("Hello");
921
    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
922 923
    SIndexTerm*           term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
924 925 926 927
    indexMultiTermQueryAdd(mq, term, QUERY_TERM);

    SArray* result = (SArray*)taosArrayInit(1, sizeof(uint64_t));
    index->Search(mq, result);
dengyihao's avatar
dengyihao 已提交
928
    std::cout << "target size: " << taosArrayGetSize(result) << std::endl;
dengyihao's avatar
dengyihao 已提交
929
    EXPECT_EQ(400, taosArrayGetSize(result));
930 931
    taosArrayDestroy(result);
    indexMultiTermQueryDestroy(mq);
dengyihao's avatar
dengyihao 已提交
932 933
  }
}
dengyihao's avatar
dengyihao 已提交
934
TEST_F(IndexEnv2, testEmptyIndexOpen) {
wafwerar's avatar
wafwerar 已提交
935
  std::string path = TD_TMP_DIR_PATH "test";
dengyihao's avatar
dengyihao 已提交
936 937 938 939 940 941 942 943 944
  if (index->Init(path) != 0) {
    std::cout << "failed to init index" << std::endl;
    exit(1);
  }

  int targetSize = 1;
  {
    std::string colName("tag1"), colVal("Hello");

dengyihao's avatar
dengyihao 已提交
945 946
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
947 948 949 950 951 952 953 954 955 956
    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < targetSize; i++) {
      int tableId = i;
      int ret = index->Put(terms, tableId);
      assert(ret == 0);
    }
    indexMultiTermDestroy(terms);
  }
}
dengyihao's avatar
dengyihao 已提交
957 958

TEST_F(IndexEnv2, testIndex_TrigeFlush) {
959
  std::string path = TD_TMP_DIR_PATH "testxxx";
dengyihao's avatar
dengyihao 已提交
960 961 962 963
  if (index->Init(path) != 0) {
    // r
    std::cout << "failed to init" << std::endl;
  }
dengyihao's avatar
dengyihao 已提交
964
  int numOfTable = 100 * 100;
dengyihao's avatar
dengyihao 已提交
965 966 967
  index->WriteMillonData("tag1", "Hello Wolrd", numOfTable);
  int target = index->SearchOne("tag1", "Hello Wolrd");
  std::cout << "Get Index: " << target << std::endl;
dengyihao's avatar
dengyihao 已提交
968
  assert(numOfTable == target);
dengyihao's avatar
dengyihao 已提交
969
}
dengyihao's avatar
dengyihao 已提交
970

dengyihao's avatar
dengyihao 已提交
971
static void single_write_and_search(IndexObj* idx) {
dengyihao's avatar
dengyihao 已提交
972 973
  // int target = idx->SearchOne("tag1", "Hello");
  // target = idx->SearchOne("tag2", "Test");
dengyihao's avatar
dengyihao 已提交
974 975
}
static void multi_write_and_search(IndexObj* idx) {
976 977
  idx->PutOne("tag1", "Hello");
  idx->PutOne("tag2", "Test");
dengyihao's avatar
dengyihao 已提交
978 979
  int target = idx->SearchOne("tag1", "Hello");
  target = idx->SearchOne("tag2", "Test");
dengyihao's avatar
dengyihao 已提交
980 981
  idx->WriteMultiMillonData("tag1", "hello world test", 100 * 100);
  idx->WriteMultiMillonData("tag2", "world test nothing", 100 * 10);
dengyihao's avatar
dengyihao 已提交
982
}
dengyihao's avatar
dengyihao 已提交
983
TEST_F(IndexEnv2, testIndex_serarch_cache_and_tfile) {
984
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
985 986 987
  if (index->Init(path) != 0) {
    // opt
  }
dengyihao's avatar
dengyihao 已提交
988 989
  index->PutOne("tag1", "Hello");
  index->PutOne("tag2", "Test");
dengyihao's avatar
dengyihao 已提交
990 991
  index->WriteMultiMillonData("tag1", "Hello", 100 * 100);
  index->WriteMultiMillonData("tag2", "Test", 100 * 100);
dengyihao's avatar
dengyihao 已提交
992 993 994 995
  std::thread threads[NUM_OF_THREAD];

  for (int i = 0; i < NUM_OF_THREAD; i++) {
    //
dengyihao's avatar
dengyihao 已提交
996 997 998 999 1000 1001 1002 1003
    threads[i] = std::thread(single_write_and_search, index);
  }
  for (int i = 0; i < NUM_OF_THREAD; i++) {
    // TOD
    threads[i].join();
  }
}
TEST_F(IndexEnv2, testIndex_MultiWrite_and_MultiRead) {
1004
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
1005 1006
  if (index->Init(path) != 0) {
  }
dengyihao's avatar
dengyihao 已提交
1007 1008 1009 1010 1011

  std::thread threads[NUM_OF_THREAD];
  for (int i = 0; i < NUM_OF_THREAD; i++) {
    //
    threads[i] = std::thread(multi_write_and_search, index);
dengyihao's avatar
dengyihao 已提交
1012 1013 1014 1015 1016
  }
  for (int i = 0; i < NUM_OF_THREAD; i++) {
    // TOD
    threads[i].join();
  }
dengyihao's avatar
dengyihao 已提交
1017 1018
}

1019 1020 1021 1022 1023 1024 1025
TEST_F(IndexEnv2, testIndex_restart) {
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
  if (index->Init(path, false) != 0) {
  }
  index->SearchOneTarget("tag1", "Hello", 10);
  index->SearchOneTarget("tag2", "Test", 10);
}
dengyihao's avatar
dengyihao 已提交
1026
// TEST_F(IndexEnv2, testIndex_restart1) {
1027
//  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
1028
//  if (index->Init(path, false) != 0) {
dengyihao's avatar
dengyihao 已提交
1029 1030 1031 1032 1033
//  }
//  index->ReadMultiMillonData("tag1", "coding");
//  index->SearchOneTarget("tag1", "Hello", 10);
//  index->SearchOneTarget("tag2", "Test", 10);
//}
dengyihao's avatar
dengyihao 已提交
1034

dengyihao's avatar
dengyihao 已提交
1035
// TEST_F(IndexEnv2, testIndex_read_performance) {
1036
//  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
1037 1038 1039 1040 1041 1042 1043 1044
//  if (index->Init(path) != 0) {
//  }
//  index->PutOneTarge("tag1", "Hello", 12);
//  index->PutOneTarge("tag1", "Hello", 15);
//  index->ReadMultiMillonData("tag1", "Hello");
//  std::cout << "reader sz: " << index->SearchOne("tag1", "Hello") << std::endl;
//  assert(3 == index->SearchOne("tag1", "Hello"));
//}
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
TEST_F(IndexEnv2, testIndexMultiTag) {
  std::string path = TD_TMP_DIR_PATH "multi_tag";
  if (index->Init(path) != 0) {
  }
  int64_t st = taosGetTimestampUs();
  int32_t num = 100 * 100;
  index->WriteMultiMillonData("tag1", "xxxxxxxxxxxxxxx", num);
  std::cout << "numOfRow: " << num << "\ttime cost:" << taosGetTimestampUs() - st << std::endl;
  // index->WriteMultiMillonData("tag2", "xxxxxxxxxxxxxxxxxxxxxxxxx", 100 * 10000);
}
dengyihao's avatar
dengyihao 已提交
1055
TEST_F(IndexEnv2, testLongComVal1) {
1056
  std::string path = TD_TMP_DIR_PATH "long_colVal";
dengyihao's avatar
dengyihao 已提交
1057 1058
  if (index->Init(path) != 0) {
  }
dengyihao's avatar
dengyihao 已提交
1059 1060
  // gen colVal by randstr
  std::string randstr = "xxxxxxxxxxxxxxxxx";
dengyihao's avatar
dengyihao 已提交
1061
  index->WriteMultiMillonData("tag1", randstr, 100 * 1000);
dengyihao's avatar
dengyihao 已提交
1062
}
dengyihao's avatar
dengyihao 已提交
1063 1064

TEST_F(IndexEnv2, testLongComVal2) {
1065
  std::string path = TD_TMP_DIR_PATH "long_colVal";
dengyihao's avatar
dengyihao 已提交
1066 1067 1068 1069
  if (index->Init(path) != 0) {
  }
  // gen colVal by randstr
  std::string randstr = "abcccc fdadfafdafda";
dengyihao's avatar
dengyihao 已提交
1070
  index->WriteMultiMillonData("tag1", randstr, 100 * 1000);
dengyihao's avatar
dengyihao 已提交
1071 1072
}
TEST_F(IndexEnv2, testLongComVal3) {
1073
  std::string path = TD_TMP_DIR_PATH "long_colVal";
dengyihao's avatar
dengyihao 已提交
1074 1075 1076 1077
  if (index->Init(path) != 0) {
  }
  // gen colVal by randstr
  std::string randstr = "Yes, coding and coding and coding";
dengyihao's avatar
dengyihao 已提交
1078
  index->WriteMultiMillonData("tag1", randstr, 100 * 1000);
dengyihao's avatar
dengyihao 已提交
1079 1080
}
TEST_F(IndexEnv2, testLongComVal4) {
1081
  std::string path = TD_TMP_DIR_PATH "long_colVal";
dengyihao's avatar
dengyihao 已提交
1082 1083 1084 1085
  if (index->Init(path) != 0) {
  }
  // gen colVal by randstr
  std::string randstr = "111111 bac fdadfa";
dengyihao's avatar
dengyihao 已提交
1086
  index->WriteMultiMillonData("tag1", randstr, 100 * 100);
dengyihao's avatar
dengyihao 已提交
1087 1088
}
TEST_F(IndexEnv2, testIndex_read_performance1) {
1089
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
1090 1091 1092 1093 1094 1095
  if (index->Init(path) != 0) {
  }
  index->PutOneTarge("tag1", "Hello", 12);
  index->PutOneTarge("tag1", "Hello", 15);
  index->ReadMultiMillonData("tag1", "Hello", 1000);
  std::cout << "reader sz: " << index->SearchOne("tag1", "Hello") << std::endl;
dengyihao's avatar
dengyihao 已提交
1096
  EXPECT_EQ(2, index->SearchOne("tag1", "Hello"));
dengyihao's avatar
dengyihao 已提交
1097 1098
}
TEST_F(IndexEnv2, testIndex_read_performance2) {
1099
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
1100 1101 1102 1103
  if (index->Init(path) != 0) {
  }
  index->PutOneTarge("tag1", "Hello", 12);
  index->PutOneTarge("tag1", "Hello", 15);
dengyihao's avatar
dengyihao 已提交
1104
  index->ReadMultiMillonData("tag1", "Hello", 1000);
dengyihao's avatar
dengyihao 已提交
1105
  std::cout << "reader sz: " << index->SearchOne("tag1", "Hello") << std::endl;
dengyihao's avatar
dengyihao 已提交
1106
  EXPECT_EQ(2, index->SearchOne("tag1", "Hello"));
dengyihao's avatar
dengyihao 已提交
1107 1108
}
TEST_F(IndexEnv2, testIndex_read_performance3) {
1109
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
1110 1111 1112 1113
  if (index->Init(path) != 0) {
  }
  index->PutOneTarge("tag1", "Hello", 12);
  index->PutOneTarge("tag1", "Hello", 15);
dengyihao's avatar
dengyihao 已提交
1114
  index->ReadMultiMillonData("tag1", "Hello", 1000);
dengyihao's avatar
dengyihao 已提交
1115
  std::cout << "reader sz: " << index->SearchOne("tag1", "Hello") << std::endl;
dengyihao's avatar
dengyihao 已提交
1116
  EXPECT_EQ(2, index->SearchOne("tag1", "Hello"));
dengyihao's avatar
dengyihao 已提交
1117 1118
}
TEST_F(IndexEnv2, testIndex_read_performance4) {
1119
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
1120 1121 1122 1123
  if (index->Init(path) != 0) {
  }
  index->PutOneTarge("tag10", "Hello", 12);
  index->PutOneTarge("tag12", "Hello", 15);
dengyihao's avatar
dengyihao 已提交
1124
  index->ReadMultiMillonData("tag10", "Hello", 1000);
dengyihao's avatar
dengyihao 已提交
1125
  std::cout << "reader sz: " << index->SearchOne("tag1", "Hello") << std::endl;
dengyihao's avatar
dengyihao 已提交
1126
  EXPECT_EQ(1, index->SearchOne("tag10", "Hello"));
dengyihao's avatar
dengyihao 已提交
1127
}
dengyihao's avatar
test  
dengyihao 已提交
1128
TEST_F(IndexEnv2, testIndex_cache_del) {
1129
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
1130 1131 1132 1133 1134 1135 1136 1137
  if (index->Init(path) != 0) {
  }
  for (int i = 0; i < 100; i++) {
    index->PutOneTarge("tag10", "Hello", i);
  }
  index->Del("tag10", "Hello", 12);
  index->Del("tag10", "Hello", 11);

dengyihao's avatar
test  
dengyihao 已提交
1138 1139 1140
  // index->WriteMultiMillonData("tag10", "xxxxxxxxxxxxxx", 100 * 10000);
  index->Del("tag10", "Hello", 17);
  EXPECT_EQ(97, index->SearchOne("tag10", "Hello"));
dengyihao's avatar
dengyihao 已提交
1141

dengyihao's avatar
test  
dengyihao 已提交
1142
  index->PutOneTarge("tag10", "Hello", 17);  // add again
dengyihao's avatar
dengyihao 已提交
1143
  EXPECT_EQ(98, index->SearchOne("tag10", "Hello"));
dengyihao's avatar
test  
dengyihao 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

  // del all
  for (int i = 0; i < 200; i++) {
    index->Del("tag10", "Hello", i);
  }
  EXPECT_EQ(0, index->SearchOne("tag10", "Hello"));

  // add other item
  for (int i = 0; i < 2000; i++) {
    index->PutOneTarge("tag10", "World", i);
  }

  for (int i = 0; i < 2000; i++) {
    index->PutOneTarge("tag10", "Hello", i);
  }
  EXPECT_EQ(2000, index->SearchOne("tag10", "Hello"));

  for (int i = 0; i < 2000; i++) {
    index->Del("tag10", "Hello", i);
  }
  EXPECT_EQ(0, index->SearchOne("tag10", "Hello"));
}

TEST_F(IndexEnv2, testIndex_del) {
1168
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
test  
dengyihao 已提交
1169 1170 1171 1172 1173 1174 1175
  if (index->Init(path) != 0) {
  }
  for (int i = 0; i < 100; i++) {
    index->PutOneTarge("tag10", "Hello", i);
  }
  index->Del("tag10", "Hello", 12);
  index->Del("tag10", "Hello", 11);
dengyihao's avatar
add UT  
dengyihao 已提交
1176
  EXPECT_EQ(98, index->SearchOne("tag10", "Hello"));
dengyihao's avatar
test  
dengyihao 已提交
1177

dengyihao's avatar
dengyihao 已提交
1178
  index->WriteMultiMillonData("tag10", "xxxxxxxxxxxxxx", 100 * 100);
dengyihao's avatar
test  
dengyihao 已提交
1179 1180
  index->Del("tag10", "Hello", 17);
  EXPECT_EQ(97, index->SearchOne("tag10", "Hello"));
dengyihao's avatar
dengyihao 已提交
1181
}