indexTests.cc 35.5 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 24
#include "indexCache.h"
#include "indexFst.h"
#include "indexFstCountingWriter.h"
#include "indexFstUtil.h"
dengyihao's avatar
dengyihao 已提交
25
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
26 27
#include "indexTfile.h"
#include "indexUtil.h"
dengyihao's avatar
dengyihao 已提交
28
#include "tskiplist.h"
dengyihao's avatar
dengyihao 已提交
29
#include "tutil.h"
dengyihao's avatar
dengyihao 已提交
30
using namespace std;
dengyihao's avatar
dengyihao 已提交
31

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

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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 已提交
50

dengyihao's avatar
dengyihao 已提交
51
class FstWriter {
dengyihao's avatar
dengyihao 已提交
52 53
 public:
  FstWriter() {
54
    _wc = writerCtxCreate(TFile, TD_TMP_DIR_PATH "tindex", false, 64 * 1024 * 1024);
dengyihao's avatar
dengyihao 已提交
55 56 57 58 59 60 61 62 63 64 65 66
    _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 已提交
67
    writerCtxDestroy(_wc, false);
dengyihao's avatar
dengyihao 已提交
68 69 70 71 72
  }

 private:
  FstBuilder* _b;
  WriterCtx*  _wc;
dengyihao's avatar
dengyihao 已提交
73 74 75
};

class FstReadMemory {
dengyihao's avatar
dengyihao 已提交
76 77
 public:
  FstReadMemory(size_t size) {
78
    _wc = writerCtxCreate(TFile, TD_TMP_DIR_PATH "tindex", true, 64 * 1024);
dengyihao's avatar
dengyihao 已提交
79 80 81 82 83
    _w = fstCountingWriterCreate(_wc);
    _size = size;
    memset((void*)&_s, 0, sizeof(_s));
  }
  bool init() {
wafwerar's avatar
wafwerar 已提交
84
    char* buf = (char*)taosMemoryCalloc(1, sizeof(char) * _size);
dengyihao's avatar
dengyihao 已提交
85
    int   nRead = fstCountingWriterRead(_w, (uint8_t*)buf, _size);
dengyihao's avatar
dengyihao 已提交
86 87 88
    if (nRead <= 0) {
      return false;
    }
dengyihao's avatar
dengyihao 已提交
89 90 91
    _size = nRead;
    _s = fstSliceCreate((uint8_t*)buf, _size);
    _fst = fstCreate(&_s);
wafwerar's avatar
wafwerar 已提交
92
    taosMemoryFree(buf);
dengyihao's avatar
dengyihao 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    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 已提交
109 110 111 112
  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 已提交
113

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

  ~FstReadMemory() {
dengyihao's avatar
dengyihao 已提交
127
    fstCountingWriterDestroy(_w);
dengyihao's avatar
dengyihao 已提交
128
    fstDestroy(_fst);
dengyihao's avatar
dengyihao 已提交
129
    fstSliceDestroy(&_s);
dengyihao's avatar
dengyihao 已提交
130
    writerCtxDestroy(_wc, true);
dengyihao's avatar
dengyihao 已提交
131 132 133 134 135 136 137 138 139 140
  }

 private:
  FstCountingWriter* _w;
  Fst*               _fst;
  FstSlice           _s;
  WriterCtx*         _wc;
  size_t             _size;
};

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

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

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

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

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

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

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

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

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

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

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

  {
dengyihao's avatar
dengyihao 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    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 已提交
271
    }
dengyihao's avatar
dengyihao 已提交
272
  }
dengyihao's avatar
dengyihao 已提交
273
  delete m;
dengyihao's avatar
dengyihao 已提交
274
}
wafwerar's avatar
wafwerar 已提交
275
static std::string logDir = TD_TMP_DIR_PATH "log";
dengyihao's avatar
dengyihao 已提交
276 277 278 279 280 281

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

  tsAsyncLog = 0;
dengyihao's avatar
dengyihao 已提交
282
  idxDebugFlag = 143;
dengyihao's avatar
dengyihao 已提交
283 284 285 286 287 288 289 290
  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 已提交
291
class IndexEnv : public ::testing::Test {
dengyihao's avatar
dengyihao 已提交
292 293
 protected:
  virtual void SetUp() {
dengyihao's avatar
dengyihao 已提交
294
    initLog();
dengyihao's avatar
dengyihao 已提交
295 296 297 298 299 300 301 302 303 304
    taosRemoveDir(path);
    opts = indexOptsCreate();
    int ret = indexOpen(opts, path, &index);
    assert(ret == 0);
  }
  virtual void TearDown() {
    indexClose(index);
    indexOptsDestroy(opts);
  }

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

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

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

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

dengyihao's avatar
dengyihao 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    fileName_ = path;

    WriterCtx* ctx = writerCtxCreate(TFile, path.c_str(), false, 64 * 1024 * 1024);

    writer_ = tfileWriterCreate(ctx, &header);
    return writer_ != NULL ? true : false;
  }
  bool InitReader() {
    WriterCtx* ctx = writerCtxCreate(TFile, fileName_.c_str(), true, 64 * 1024 * 1024);
    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 已提交
414
    SIdxTRslt* tr = idxTRsltCreate();
dengyihao's avatar
dengyihao 已提交
415 416 417

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

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

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

  TFileWriter* writer_;
  TFileReader* reader_;

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

dengyihao's avatar
dengyihao 已提交
442 443 444 445 446 447
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 已提交
448 449 450 451 452
  }

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

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

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
501 502 503 504 505 506 507 508 509
  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 已提交
510
  SIndexTermQuery query = {term, QUERY_TERM};
dengyihao's avatar
dengyihao 已提交
511 512 513

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

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

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

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

 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 已提交
572 573 574 575 576 577 578 579 580 581 582 583
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);
  }
}
584
#define MAX_TERM_KEY_LEN 128
dengyihao's avatar
dengyihao 已提交
585
TEST_F(IndexCacheEnv, cache_test) {
586 587
  int     version = 0;
  int16_t colId = 0;
dengyihao's avatar
dengyihao 已提交
588
  int16_t othColId = 10;
dengyihao's avatar
dengyihao 已提交
589

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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