indexTests.cc 35.8 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
    _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() {
dengyihao's avatar
dengyihao 已提交
63
    // fstBuilderFinish(_b);
dengyihao's avatar
dengyihao 已提交
64 65
    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
    _wc = idxFileCtxCreate(TFILE, TD_TMP_DIR_PATH "tindex", true, 64 * 1024);
dengyihao's avatar
dengyihao 已提交
78
    _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
}
dengyihao's avatar
dengyihao 已提交
274

dengyihao's avatar
dengyihao 已提交
275 276 277 278
static std::string logDir = TD_TMP_DIR_PATH "log";
static void        initLog() {
         const char*   defaultLogFileNamePrefix = "taoslog";
         const int32_t maxLogFileNum = 10;
dengyihao's avatar
dengyihao 已提交
279

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

dengyihao's avatar
dengyihao 已提交
286 287
         if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) {
           printf("failed to open log file in directory:%s\n", tsLogDir);
dengyihao's avatar
dengyihao 已提交
288 289
  }
}
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
    taosRemoveDir(path);
dengyihao's avatar
dengyihao 已提交
295 296 297
    SIndexOpts opts;
    opts.cacheSize = 1024 * 1024 * 4;
    int ret = indexOpen(&opts, path, &index);
dengyihao's avatar
dengyihao 已提交
298 299
    assert(ret == 0);
  }
dengyihao's avatar
dengyihao 已提交
300
  virtual void TearDown() { indexClose(index); }
dengyihao's avatar
dengyihao 已提交
301

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

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

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

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

dengyihao's avatar
dengyihao 已提交
390 391
    fileName_ = path;

dengyihao's avatar
dengyihao 已提交
392 393
    IFileCtx* ctx = idxFileCtxCreate(TFILE, path.c_str(), false, 64 * 1024 * 1024);
    ctx->lru = taosLRUCacheInit(1024 * 1024 * 4, -1, .5);
dengyihao's avatar
dengyihao 已提交
394 395 396 397 398

    writer_ = tfileWriterCreate(ctx, &header);
    return writer_ != NULL ? true : false;
  }
  bool InitReader() {
dengyihao's avatar
dengyihao 已提交
399 400
    IFileCtx* ctx = idxFileCtxCreate(TFILE, fileName_.c_str(), true, 64 * 1024 * 1024);
    ctx->lru = taosLRUCacheInit(1024 * 1024 * 4, -1, .5);
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
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);
D
Dingle Zhang 已提交
577
  if (colType == TSDB_DATA_TYPE_BINARY || colType == TSDB_DATA_TYPE_GEOMETRY) {
dengyihao's avatar
dengyihao 已提交
578 579 580 581 582
    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
    SIndexTerm*     term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
660
                                            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
    SIndexTerm*     term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
675
                                            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
    taosMkDir(dir.c_str());
dengyihao's avatar
dengyihao 已提交
701 702 703 704
    SIndexOpts opts;
    opts.cacheSize = 1024 * 1024 * 4;

    int ret = indexOpen(&opts, dir.c_str(), &idx);
dengyihao's avatar
dengyihao 已提交
705 706 707 708 709 710
    if (ret != 0) {
      // opt
      std::cout << "failed to open index: %s" << dir << std::endl;
    }
    return ret;
  }
dengyihao's avatar
dengyihao 已提交
711
  void Del(const std::string& colName, const std::string& colVal, uint64_t uid) {
dengyihao's avatar
dengyihao 已提交
712
    SIndexTerm*      term = indexTermCreateT(0, DEL_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
713
                                             colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
714 715 716 717 718
    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    Put(terms, uid);
    indexMultiTermDestroy(terms);
  }
dengyihao's avatar
dengyihao 已提交
719 720
  int WriteMillonData(const std::string& colName, const std::string& colVal = "Hello world",
                      size_t numOfTable = 100 * 10000) {
dengyihao's avatar
dengyihao 已提交
721
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
722
                                             colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
723 724 725 726 727 728 729 730 731
    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 已提交
732 733 734
  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 已提交
735
    size_t      colValSize = tColVal.size();
dengyihao's avatar
dengyihao 已提交
736 737
    int         skip = 100;
    numOfTable /= skip;
dengyihao's avatar
dengyihao 已提交
738
    for (int i = 0; i < numOfTable; i++) {
dengyihao's avatar
dengyihao 已提交
739 740
      for (int k = 0; k < 10 && k < colVal.size(); k++) {
        // opt
wafwerar's avatar
wafwerar 已提交
741
        tColVal[taosRand() % colValSize] = 'a' + k % 26;
dengyihao's avatar
dengyihao 已提交
742
      }
dengyihao's avatar
dengyihao 已提交
743
      SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
744
                                               tColVal.c_str(), tColVal.size());
dengyihao's avatar
dengyihao 已提交
745 746
      SIndexMultiTerm* terms = indexMultiTermCreate();
      indexMultiTermAdd(terms, term);
dengyihao's avatar
dengyihao 已提交
747 748
      for (size_t j = 0; j < skip; j++) {
        int ret = Put(terms, j);
dengyihao's avatar
dengyihao 已提交
749 750 751 752 753 754
        assert(ret == 0);
      }
      indexMultiTermDestroy(terms);
    }
    return numOfTable;
  }
dengyihao's avatar
dengyihao 已提交
755
  int ReadMultiMillonData(const std::string& colName, const std::string& colVal = "Hello world",
dengyihao's avatar
dengyihao 已提交
756
                          size_t numOfTable = 100) {
dengyihao's avatar
dengyihao 已提交
757 758 759 760 761 762 763 764 765
    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 已提交
766

dengyihao's avatar
dengyihao 已提交
767 768 769 770 771 772 773 774 775 776
  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 已提交
777 778
  int SearchOne(const std::string& colName, const std::string& colVal) {
    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
779
    SIndexTerm*           term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
780
                                                  colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
781 782 783
    indexMultiTermQueryAdd(mq, term, QUERY_TERM);

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

dengyihao's avatar
dengyihao 已提交
785 786 787 788 789 790
    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 已提交
791
      return -1;
dengyihao's avatar
dengyihao 已提交
792 793 794 795 796 797 798 799 800
    }
    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 已提交
801
    SIndexTerm*           term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
802
                                                  colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
803 804 805 806
    indexMultiTermQueryAdd(mq, term, QUERY_TERM);

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

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

821
    return sz;
dengyihao's avatar
dengyihao 已提交
822
  }
dengyihao's avatar
dengyihao 已提交
823

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

  ~IndexObj() {
dengyihao's avatar
dengyihao 已提交
846
    // indexCleanUp();
dengyihao's avatar
dengyihao 已提交
847
    indexClose(idx);
dengyihao's avatar
dengyihao 已提交
848 849 850
  }

 private:
dengyihao's avatar
dengyihao 已提交
851 852 853 854
  SIndexOpts* opts;
  SIndex*     idx;
  int         numOfWrite;
  int         numOfRead;
dengyihao's avatar
dengyihao 已提交
855 856 857 858
};

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

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

dengyihao's avatar
dengyihao 已提交
880
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
881
                                             colVal.c_str(), colVal.size());
882 883 884 885 886 887 888 889 890 891
    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 已提交
892 893
    size_t      size = 200;
    std::string colName("tag1"), colVal("hello");
894

dengyihao's avatar
dengyihao 已提交
895
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
896
                                             colVal.c_str(), colVal.size());
897 898 899 900 901 902 903 904 905 906
    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 已提交
907
    size_t      size = 200;
dengyihao's avatar
dengyihao 已提交
908
    std::string colName("tag1"), colVal("Hello");
909

dengyihao's avatar
dengyihao 已提交
910
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
911
                                             colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
912 913 914 915 916 917 918 919 920 921 922 923
    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");
924
    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
925
    SIndexTerm*           term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
926
                                                  colVal.c_str(), colVal.size());
927 928 929 930
    indexMultiTermQueryAdd(mq, term, QUERY_TERM);

    SArray* result = (SArray*)taosArrayInit(1, sizeof(uint64_t));
    index->Search(mq, result);
dengyihao's avatar
dengyihao 已提交
931
    std::cout << "target size: " << taosArrayGetSize(result) << std::endl;
dengyihao's avatar
dengyihao 已提交
932
    EXPECT_EQ(400, taosArrayGetSize(result));
933 934
    taosArrayDestroy(result);
    indexMultiTermQueryDestroy(mq);
dengyihao's avatar
dengyihao 已提交
935 936
  }
}
dengyihao's avatar
dengyihao 已提交
937
TEST_F(IndexEnv2, testEmptyIndexOpen) {
wafwerar's avatar
wafwerar 已提交
938
  std::string path = TD_TMP_DIR_PATH "test";
dengyihao's avatar
dengyihao 已提交
939 940 941 942 943 944 945 946 947
  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 已提交
948
    SIndexTerm*      term = indexTermCreateT(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
949
                                             colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
950 951 952 953 954 955 956 957 958 959
    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 已提交
960 961

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

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

  for (int i = 0; i < NUM_OF_THREAD; i++) {
    //
dengyihao's avatar
dengyihao 已提交
999 1000 1001 1002 1003 1004 1005 1006
    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) {
1007
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
1008 1009
  if (index->Init(path) != 0) {
  }
dengyihao's avatar
dengyihao 已提交
1010 1011 1012 1013 1014

  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 已提交
1015 1016 1017 1018 1019
  }
  for (int i = 0; i < NUM_OF_THREAD; i++) {
    // TOD
    threads[i].join();
  }
dengyihao's avatar
dengyihao 已提交
1020 1021
}

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

dengyihao's avatar
dengyihao 已提交
1038
// TEST_F(IndexEnv2, testIndex_read_performance) {
1039
//  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
1040 1041 1042 1043 1044 1045 1046 1047
//  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"));
//}
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
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 已提交
1058
TEST_F(IndexEnv2, testLongComVal1) {
1059
  std::string path = TD_TMP_DIR_PATH "long_colVal";
dengyihao's avatar
dengyihao 已提交
1060 1061
  if (index->Init(path) != 0) {
  }
dengyihao's avatar
dengyihao 已提交
1062 1063
  // gen colVal by randstr
  std::string randstr = "xxxxxxxxxxxxxxxxx";
dengyihao's avatar
dengyihao 已提交
1064
  index->WriteMultiMillonData("tag1", randstr, 100 * 1000);
dengyihao's avatar
dengyihao 已提交
1065
}
dengyihao's avatar
dengyihao 已提交
1066 1067

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

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

  // 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) {
1171
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
test  
dengyihao 已提交
1172 1173 1174 1175 1176 1177 1178
  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 已提交
1179
  EXPECT_EQ(98, index->SearchOne("tag10", "Hello"));
dengyihao's avatar
test  
dengyihao 已提交
1180

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