indexTests.cc 34.9 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 109 110 111 112 113
    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
  bool Search(AutomationCtx* ctx, std::vector<uint64_t>& result) {
    FstStreamBuilder*      sb = fstSearch(_fst, ctx);
    StreamWithState*       st = streamBuilderIntoStream(sb);
    StreamWithStateResult* rt = NULL;

dengyihao's avatar
dengyihao 已提交
114 115 116
    while ((rt = streamWithStateNextWith(st, NULL)) != NULL) {
      result.push_back((uint64_t)(rt->out.out));
    }
dengyihao's avatar
dengyihao 已提交
117 118 119 120 121 122 123 124 125 126
    return true;
  }
  bool SearchWithTimeCostUs(AutomationCtx* ctx, std::vector<uint64_t>& result) {
    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 223 224 225

  AutomationCtx* ctx = automCtxCreate((void*)"ab", AUTOMATION_PREFIX);
  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
}
dengyihao's avatar
dengyihao 已提交
275 276 277 278 279 280 281
static std::string logDir = "/tmp/log";

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
    SIdxTempResult* tr = idxTempResultCreate();
dengyihao's avatar
dengyihao 已提交
415 416 417

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

dengyihao's avatar
dengyihao 已提交
418 419
    idxTempResultMergeTo(tr, result);
    idxTempResultDestroy(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

501 502
  std::string     colName("voltage");
  std::string     colVal("ab");
dengyihao's avatar
dengyihao 已提交
503
  SIndexTerm*     term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
504
                                     colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
505
  SIndexTermQuery query = {term, QUERY_TERM};
dengyihao's avatar
dengyihao 已提交
506 507 508

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

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

    int ret = indexCacheSearch(cache, query, tr, s);
dengyihao's avatar
dengyihao 已提交
537 538
    idxTempResultMergeTo(tr, result);
    idxTempResultDestroy(tr);
dengyihao's avatar
dengyihao 已提交
539

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

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

567
#define MAX_TERM_KEY_LEN 128
dengyihao's avatar
dengyihao 已提交
568
TEST_F(IndexCacheEnv, cache_test) {
569 570
  int     version = 0;
  int16_t colId = 0;
dengyihao's avatar
dengyihao 已提交
571
  int16_t othColId = 10;
dengyihao's avatar
dengyihao 已提交
572

573
  uint64_t    suid = 0;
dengyihao's avatar
dengyihao 已提交
574 575
  std::string colName("voltage");
  {
576
    std::string colVal("v1");
dengyihao's avatar
dengyihao 已提交
577
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
578
                                       colVal.c_str(), colVal.size());
579
    coj->Put(term, colId, version++, suid++);
580
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
581
    // indexTermDestry(term);
dengyihao's avatar
dengyihao 已提交
582 583
  }
  {
584
    std::string colVal("v3");
dengyihao's avatar
dengyihao 已提交
585
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
586
                                       colVal.c_str(), colVal.size());
587
    coj->Put(term, colId, version++, suid++);
588
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
589 590
  }
  {
591
    std::string colVal("v2");
dengyihao's avatar
dengyihao 已提交
592
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
593
                                       colVal.c_str(), colVal.size());
594
    coj->Put(term, colId, version++, suid++);
595
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
596 597
  }
  {
598
    std::string colVal("v3");
dengyihao's avatar
dengyihao 已提交
599
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
600
                                       colVal.c_str(), colVal.size());
601
    coj->Put(term, colId, version++, suid++);
602
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
603 604
  }
  {
605
    std::string colVal("v3");
dengyihao's avatar
dengyihao 已提交
606
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
607
                                       colVal.c_str(), colVal.size());
608
    coj->Put(term, colId, version++, suid++);
609
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
610
  }
dengyihao's avatar
dengyihao 已提交
611 612
  coj->Debug();
  std::cout << "--------first----------" << std::endl;
dengyihao's avatar
dengyihao 已提交
613 614
  {
    std::string colVal("v3");
dengyihao's avatar
dengyihao 已提交
615
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
616
                                       colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
617
    coj->Put(term, othColId, version++, suid++);
618
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
619 620 621
  }
  {
    std::string colVal("v4");
dengyihao's avatar
dengyihao 已提交
622
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
623
                                       colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
624
    coj->Put(term, othColId, version++, suid++);
625
    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
626
  }
dengyihao's avatar
dengyihao 已提交
627 628
  coj->Debug();
  std::cout << "--------second----------" << std::endl;
dengyihao's avatar
dengyihao 已提交
629
  {
630
    std::string colVal("v4");
dengyihao's avatar
dengyihao 已提交
631
    for (size_t i = 0; i < 10; i++) {
632
      colVal[colVal.size() - 1] = 'a' + i;
dengyihao's avatar
dengyihao 已提交
633
      SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
634
                                         colVal.c_str(), colVal.size());
635
      coj->Put(term, colId, version++, suid++);
636
      indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
637 638
    }
  }
dengyihao's avatar
dengyihao 已提交
639 640
  coj->Debug();
  // begin query
dengyihao's avatar
dengyihao 已提交
641
  {
642
    std::string     colVal("v3");
dengyihao's avatar
dengyihao 已提交
643
    SIndexTerm*     term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
644
                                       colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
645
    SIndexTermQuery query = {term, QUERY_TERM};
646 647 648 649
    SArray*         ret = (SArray*)taosArrayInit(4, sizeof(suid));
    STermValueType  valType;

    coj->Get(&query, colId, 10000, ret, &valType);
dengyihao's avatar
dengyihao 已提交
650
    std::cout << "size : " << taosArrayGetSize(ret) << std::endl;
dengyihao's avatar
dengyihao 已提交
651
    assert(taosArrayGetSize(ret) == 4);
652 653 654
    taosArrayDestroy(ret);

    indexTermDestroy(term);
dengyihao's avatar
dengyihao 已提交
655
  }
656
  {
657
    std::string     colVal("v2");
dengyihao's avatar
dengyihao 已提交
658
    SIndexTerm*     term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
659
                                       colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
660
    SIndexTermQuery query = {term, QUERY_TERM};
661 662
    SArray*         ret = (SArray*)taosArrayInit(4, sizeof(suid));
    STermValueType  valType;
dengyihao's avatar
dengyihao 已提交
663

664 665
    coj->Get(&query, colId, 10000, ret, &valType);
    assert(taosArrayGetSize(ret) == 1);
666 667 668
    taosArrayDestroy(ret);

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

dengyihao's avatar
dengyihao 已提交
748 749 750 751 752 753 754 755 756 757
  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 已提交
758 759
  int SearchOne(const std::string& colName, const std::string& colVal) {
    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
760
    SIndexTerm*           term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
761 762 763 764
                                       colVal.c_str(), colVal.size());
    indexMultiTermQueryAdd(mq, term, QUERY_TERM);

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

dengyihao's avatar
dengyihao 已提交
766 767 768 769 770 771
    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 已提交
772
      return -1;
dengyihao's avatar
dengyihao 已提交
773 774 775 776 777 778 779 780 781
    }
    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 已提交
782
    SIndexTerm*           term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
783 784 785 786 787
                                       colVal.c_str(), colVal.size());
    indexMultiTermQueryAdd(mq, term, QUERY_TERM);

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

dengyihao's avatar
dengyihao 已提交
788 789 790
    int64_t s = taosGetTimestampUs();
    if (Search(mq, result) == 0) {
      int64_t e = taosGetTimestampUs();
dengyihao's avatar
add UT  
dengyihao 已提交
791
      std::cout << "search one successfully and time cost:" << e - s << "us\tquery col:" << colName
dengyihao's avatar
dengyihao 已提交
792
                << "\t val: " << colVal << "\t size:" << taosArrayGetSize(result) << std::endl;
dengyihao's avatar
dengyihao 已提交
793 794
    } else {
    }
795 796
    int sz = taosArrayGetSize(result);
    indexMultiTermQueryDestroy(mq);
dengyihao's avatar
dengyihao 已提交
797 798 799
    assert(sz == 1);
    uint64_t* ret = (uint64_t*)taosArrayGet(result, 0);
    assert(val = *ret);
dengyihao's avatar
dengyihao 已提交
800
    taosArrayDestroy(result);
dengyihao's avatar
dengyihao 已提交
801

802
    return sz;
dengyihao's avatar
dengyihao 已提交
803
  }
dengyihao's avatar
dengyihao 已提交
804

dengyihao's avatar
dengyihao 已提交
805
  void PutOne(const std::string& colName, const std::string& colVal) {
806
    SIndexMultiTerm* terms = indexMultiTermCreate();
dengyihao's avatar
dengyihao 已提交
807
    SIndexTerm*      term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
808 809 810 811 812
                                       colVal.c_str(), colVal.size());
    indexMultiTermAdd(terms, term);
    Put(terms, 10);
    indexMultiTermDestroy(terms);
  }
dengyihao's avatar
dengyihao 已提交
813 814
  void PutOneTarge(const std::string& colName, const std::string& colVal, uint64_t val) {
    SIndexMultiTerm* terms = indexMultiTermCreate();
dengyihao's avatar
dengyihao 已提交
815
    SIndexTerm*      term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
816 817 818 819 820
                                       colVal.c_str(), colVal.size());
    indexMultiTermAdd(terms, term);
    Put(terms, val);
    indexMultiTermDestroy(terms);
  }
dengyihao's avatar
dengyihao 已提交
821 822 823 824 825 826
  void Debug() {
    std::cout << "numOfWrite:" << numOfWrite << std::endl;
    std::cout << "numOfRead:" << numOfRead << std::endl;
  }

  ~IndexObj() {
dengyihao's avatar
dengyihao 已提交
827
    // indexCleanUp();
dengyihao's avatar
dengyihao 已提交
828
    indexClose(idx);
dengyihao's avatar
dengyihao 已提交
829 830 831 832 833 834 835 836 837 838 839
  }

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

class IndexEnv2 : public ::testing::Test {
 protected:
dengyihao's avatar
dengyihao 已提交
840 841 842 843
  virtual void SetUp() {
    initLog();
    index = new IndexObj();
  }
844 845 846 847 848
  virtual void TearDown() {
    // taosMsleep(500);
    delete index;
  }
  IndexObj* index;
dengyihao's avatar
dengyihao 已提交
849 850
};
TEST_F(IndexEnv2, testIndexOpen) {
851
  std::string path = TD_TMP_DIR_PATH "test";
852 853 854 855 856
  if (index->Init(path) != 0) {
    std::cout << "failed to init index" << std::endl;
    exit(1);
  }

dengyihao's avatar
dengyihao 已提交
857
  int targetSize = 200;
858
  {
dengyihao's avatar
dengyihao 已提交
859
    std::string colName("tag1"), colVal("Hello");
860

dengyihao's avatar
dengyihao 已提交
861
    SIndexTerm*      term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
862 863 864 865 866 867 868 869 870 871 872
                                       colVal.c_str(), colVal.size());
    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 已提交
873 874
    size_t      size = 200;
    std::string colName("tag1"), colVal("hello");
875

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

dengyihao's avatar
dengyihao 已提交
891
    SIndexTerm*      term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904
                                       colVal.c_str(), colVal.size());
    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");
905
    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
906
    SIndexTerm*           term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
907 908 909 910 911
                                       colVal.c_str(), colVal.size());
    indexMultiTermQueryAdd(mq, term, QUERY_TERM);

    SArray* result = (SArray*)taosArrayInit(1, sizeof(uint64_t));
    index->Search(mq, result);
dengyihao's avatar
dengyihao 已提交
912
    std::cout << "target size: " << taosArrayGetSize(result) << std::endl;
dengyihao's avatar
dengyihao 已提交
913
    EXPECT_EQ(400, taosArrayGetSize(result));
914 915
    taosArrayDestroy(result);
    indexMultiTermQueryDestroy(mq);
dengyihao's avatar
dengyihao 已提交
916 917
  }
}
dengyihao's avatar
dengyihao 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
TEST_F(IndexEnv2, testEmptyIndexOpen) {
  std::string path = "/tmp/test";
  if (index->Init(path) != 0) {
    std::cout << "failed to init index" << std::endl;
    exit(1);
  }

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

    SIndexTerm*      term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                       colVal.c_str(), colVal.size());
    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 已提交
941 942

TEST_F(IndexEnv2, testIndex_TrigeFlush) {
943
  std::string path = TD_TMP_DIR_PATH "testxxx";
dengyihao's avatar
dengyihao 已提交
944 945 946 947
  if (index->Init(path) != 0) {
    // r
    std::cout << "failed to init" << std::endl;
  }
dengyihao's avatar
dengyihao 已提交
948
  int numOfTable = 100 * 100;
dengyihao's avatar
dengyihao 已提交
949 950 951
  index->WriteMillonData("tag1", "Hello Wolrd", numOfTable);
  int target = index->SearchOne("tag1", "Hello Wolrd");
  std::cout << "Get Index: " << target << std::endl;
dengyihao's avatar
dengyihao 已提交
952
  assert(numOfTable == target);
dengyihao's avatar
dengyihao 已提交
953
}
dengyihao's avatar
dengyihao 已提交
954

dengyihao's avatar
dengyihao 已提交
955
static void single_write_and_search(IndexObj* idx) {
dengyihao's avatar
dengyihao 已提交
956 957
  // int target = idx->SearchOne("tag1", "Hello");
  // target = idx->SearchOne("tag2", "Test");
dengyihao's avatar
dengyihao 已提交
958 959
}
static void multi_write_and_search(IndexObj* idx) {
960 961
  idx->PutOne("tag1", "Hello");
  idx->PutOne("tag2", "Test");
dengyihao's avatar
dengyihao 已提交
962 963
  int target = idx->SearchOne("tag1", "Hello");
  target = idx->SearchOne("tag2", "Test");
dengyihao's avatar
dengyihao 已提交
964 965
  idx->WriteMultiMillonData("tag1", "hello world test", 100 * 100);
  idx->WriteMultiMillonData("tag2", "world test nothing", 100 * 10);
dengyihao's avatar
dengyihao 已提交
966
}
dengyihao's avatar
dengyihao 已提交
967
TEST_F(IndexEnv2, testIndex_serarch_cache_and_tfile) {
968
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
969 970 971
  if (index->Init(path) != 0) {
    // opt
  }
dengyihao's avatar
dengyihao 已提交
972 973
  index->PutOne("tag1", "Hello");
  index->PutOne("tag2", "Test");
dengyihao's avatar
dengyihao 已提交
974 975
  index->WriteMultiMillonData("tag1", "Hello", 100 * 100);
  index->WriteMultiMillonData("tag2", "Test", 100 * 100);
dengyihao's avatar
dengyihao 已提交
976 977 978 979
  std::thread threads[NUM_OF_THREAD];

  for (int i = 0; i < NUM_OF_THREAD; i++) {
    //
dengyihao's avatar
dengyihao 已提交
980 981 982 983 984 985 986 987
    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) {
988
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
989 990
  if (index->Init(path) != 0) {
  }
dengyihao's avatar
dengyihao 已提交
991 992 993 994 995

  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 已提交
996 997 998 999 1000
  }
  for (int i = 0; i < NUM_OF_THREAD; i++) {
    // TOD
    threads[i].join();
  }
dengyihao's avatar
dengyihao 已提交
1001 1002
}

1003 1004 1005 1006 1007 1008 1009
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 已提交
1010
// TEST_F(IndexEnv2, testIndex_restart1) {
1011
//  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
1012
//  if (index->Init(path, false) != 0) {
dengyihao's avatar
dengyihao 已提交
1013 1014 1015 1016 1017
//  }
//  index->ReadMultiMillonData("tag1", "coding");
//  index->SearchOneTarget("tag1", "Hello", 10);
//  index->SearchOneTarget("tag2", "Test", 10);
//}
dengyihao's avatar
dengyihao 已提交
1018

dengyihao's avatar
dengyihao 已提交
1019
// TEST_F(IndexEnv2, testIndex_read_performance) {
1020
//  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
dengyihao 已提交
1021 1022 1023 1024 1025 1026 1027 1028
//  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"));
//}
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
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 已提交
1039
TEST_F(IndexEnv2, testLongComVal1) {
1040
  std::string path = TD_TMP_DIR_PATH "long_colVal";
dengyihao's avatar
dengyihao 已提交
1041 1042
  if (index->Init(path) != 0) {
  }
dengyihao's avatar
dengyihao 已提交
1043 1044
  // gen colVal by randstr
  std::string randstr = "xxxxxxxxxxxxxxxxx";
dengyihao's avatar
dengyihao 已提交
1045
  index->WriteMultiMillonData("tag1", randstr, 100 * 1000);
dengyihao's avatar
dengyihao 已提交
1046
}
dengyihao's avatar
dengyihao 已提交
1047 1048

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

dengyihao's avatar
test  
dengyihao 已提交
1126
  index->PutOneTarge("tag10", "Hello", 17);  // add again
dengyihao's avatar
dengyihao 已提交
1127
  EXPECT_EQ(98, index->SearchOne("tag10", "Hello"));
dengyihao's avatar
test  
dengyihao 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151

  // 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) {
1152
  std::string path = TD_TMP_DIR_PATH "cache_and_tfile";
dengyihao's avatar
test  
dengyihao 已提交
1153 1154 1155 1156 1157 1158 1159
  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 已提交
1160
  EXPECT_EQ(98, index->SearchOne("tag10", "Hello"));
dengyihao's avatar
test  
dengyihao 已提交
1161

dengyihao's avatar
dengyihao 已提交
1162
  index->WriteMultiMillonData("tag10", "xxxxxxxxxxxxxx", 100 * 100);
dengyihao's avatar
test  
dengyihao 已提交
1163 1164
  index->Del("tag10", "Hello", 17);
  EXPECT_EQ(97, index->SearchOne("tag10", "Hello"));
dengyihao's avatar
dengyihao 已提交
1165
}