indexTests.cc 16.3 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
dengyihao's avatar
dengyihao 已提交
5
 * 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 已提交
6 7 8 9 10 11 12 13
 *
 * 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 已提交
14 15
#include <gtest/gtest.h>
#include <iostream>
dengyihao's avatar
dengyihao 已提交
16
#include <string>
dengyihao's avatar
dengyihao 已提交
17
#include "index.h"
dengyihao's avatar
dengyihao 已提交
18
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
19
#include "index_cache.h"
dengyihao's avatar
dengyihao 已提交
20 21
#include "index_fst.h"
#include "index_fst_counting_writer.h"
dengyihao's avatar
dengyihao 已提交
22 23
#include "index_fst_util.h"
#include "index_tfile.h"
dengyihao's avatar
dengyihao 已提交
24
#include "tskiplist.h"
dengyihao's avatar
dengyihao 已提交
25
#include "tutil.h"
dengyihao's avatar
dengyihao 已提交
26
using namespace std;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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 已提交
43
class FstWriter {
dengyihao's avatar
dengyihao 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 public:
  FstWriter() {
    _wc = writerCtxCreate(TFile, "/tmp/tindex", false, 64 * 1024 * 1024);
    _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);

    writerCtxDestroy(_wc);
  }

 private:
  FstBuilder* _b;
  WriterCtx*  _wc;
dengyihao's avatar
dengyihao 已提交
65 66 67
};

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

    while ((rt = streamWithStateNextWith(st, NULL)) != NULL) {
      result.push_back((uint64_t)(rt->out.out));
    }
    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 已提交
117
    fstCountingWriterDestroy(_w);
dengyihao's avatar
dengyihao 已提交
118
    fstDestroy(_fst);
dengyihao's avatar
dengyihao 已提交
119
    fstSliceDestroy(&_s);
dengyihao's avatar
dengyihao 已提交
120
    writerCtxDestroy(_wc);
dengyihao's avatar
dengyihao 已提交
121 122 123 124 125 126 127 128 129 130
  }

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

dengyihao's avatar
dengyihao 已提交
131 132 133
#define L 100
#define M 100
#define N 100
dengyihao's avatar
dengyihao 已提交
134

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

dengyihao's avatar
dengyihao 已提交
176
  int     num = Performance_fstWriteRecords(fw);
dengyihao's avatar
dengyihao 已提交
177
  int64_t e = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
178
  printf("write %d record cost %" PRId64 "us\n", num, e - s);
dengyihao's avatar
dengyihao 已提交
179 180
  delete fw;

dengyihao's avatar
dengyihao 已提交
181 182 183
  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init()) { printf("success to init fst read"); }
  Performance_fstReadRecords(m);
dengyihao's avatar
dengyihao 已提交
184
  delete m;
dengyihao's avatar
dengyihao 已提交
185
}
dengyihao's avatar
dengyihao 已提交
186
void checkFstPrefixSearch() {
dengyihao's avatar
dengyihao 已提交
187 188 189
  FstWriter*  fw = new FstWriter;
  int64_t     s = taosGetTimestampUs();
  int         count = 2;
dengyihao's avatar
dengyihao 已提交
190
  std::string key("ab");
dengyihao's avatar
dengyihao 已提交
191

dengyihao's avatar
dengyihao 已提交
192
  for (int i = 0; i < count; i++) {
dengyihao's avatar
dengyihao 已提交
193 194
    key[1] = key[1] + i;
    fw->Put(key, i);
dengyihao's avatar
dengyihao 已提交
195 196
  }
  int64_t e = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
197

dengyihao's avatar
dengyihao 已提交
198 199
  std::cout << "insert data count :  " << count << "elapas time: " << e - s << std::endl;
  delete fw;
dengyihao's avatar
dengyihao 已提交
200

dengyihao's avatar
dengyihao 已提交
201
  FstReadMemory* m = new FstReadMemory(1024 * 64);
dengyihao's avatar
dengyihao 已提交
202
  if (m->init() == false) {
dengyihao's avatar
dengyihao 已提交
203
    std::cout << "init readMemory failed" << std::endl;
dengyihao's avatar
dengyihao 已提交
204 205 206
    delete m;
    return;
  }
dengyihao's avatar
dengyihao 已提交
207 208

  // prefix search
dengyihao's avatar
dengyihao 已提交
209
  std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
210 211 212 213

  AutomationCtx* ctx = automCtxCreate((void*)"ab", AUTOMATION_PREFIX);
  m->Search(ctx, result);
  assert(result.size() == count);
dengyihao's avatar
dengyihao 已提交
214
  for (int i = 0; i < result.size(); i++) {
dengyihao's avatar
dengyihao 已提交
215
    assert(result[i] == i);  // check result
dengyihao's avatar
dengyihao 已提交
216 217 218 219
  }

  free(ctx);
  delete m;
dengyihao's avatar
dengyihao 已提交
220
}
dengyihao's avatar
dengyihao 已提交
221
void validateFst() {
dengyihao's avatar
dengyihao 已提交
222 223 224 225
  int        val = 100;
  int        count = 100;
  FstWriter* fw = new FstWriter;
  // write
dengyihao's avatar
dengyihao 已提交
226
  {
dengyihao's avatar
dengyihao 已提交
227
    std::string key("ab");
dengyihao's avatar
dengyihao 已提交
228
    for (int i = 0; i < count; i++) {
dengyihao's avatar
dengyihao 已提交
229
      key.push_back('a' + i);
dengyihao's avatar
dengyihao 已提交
230
      fw->Put(key, val - i);
dengyihao's avatar
dengyihao 已提交
231
    }
dengyihao's avatar
dengyihao 已提交
232 233
  }
  delete fw;
dengyihao's avatar
dengyihao 已提交
234

dengyihao's avatar
dengyihao 已提交
235
  // read
dengyihao's avatar
dengyihao 已提交
236 237 238
  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init() == false) {
    std::cout << "init readMemory failed" << std::endl;
dengyihao's avatar
dengyihao 已提交
239 240
    delete m;
    return;
dengyihao's avatar
dengyihao 已提交
241
  }
dengyihao's avatar
dengyihao 已提交
242 243

  {
dengyihao's avatar
dengyihao 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    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 已提交
259
    }
dengyihao's avatar
dengyihao 已提交
260
  }
dengyihao's avatar
dengyihao 已提交
261
  delete m;
dengyihao's avatar
dengyihao 已提交
262
}
dengyihao's avatar
dengyihao 已提交
263
class IndexEnv : public ::testing::Test {
dengyihao's avatar
dengyihao 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
 protected:
  virtual void SetUp() {
    taosRemoveDir(path);
    opts = indexOptsCreate();
    int ret = indexOpen(opts, path, &index);
    assert(ret == 0);
  }
  virtual void TearDown() {
    indexClose(index);
    indexOptsDestroy(opts);
  }

  const char* path = "/tmp/tindex";
  SIndexOpts* opts;
  SIndex*     index;
dengyihao's avatar
dengyihao 已提交
279 280
};

281 282 283 284 285 286 287 288 289 290 291 292 293
/// TEST_F(IndexEnv, testPut) {
//  /  // single index column
//      / {
//    / std::string colName("tag1"), colVal("Hello world");
//    / 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 < 100; i++) {
//      / int tableId = i;
//      / int ret = indexPut(index, terms, tableId);
//      / assert(ret == 0);
//      /
dengyihao's avatar
dengyihao 已提交
294
//    }
295 296
//    / indexMultiTermDestroy(terms);
//    /
dengyihao's avatar
dengyihao 已提交
297
//  }
298 299 300 301 302 303 304 305 306
//  /  // multi index column
//      / {
//    / SIndexMultiTerm* terms = indexMultiTermCreate();
//    / {
//      / std::string colName("tag1"), colVal("Hello world");
//      / SIndexTerm* term =
//          / indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
//      / indexMultiTermAdd(terms, term);
//      /
dengyihao's avatar
dengyihao 已提交
307
//    }
308 309 310 311 312 313
//    / {
//      / std::string colName("tag2"), colVal("Hello world");
//      / SIndexTerm* term =
//          / indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
//      / indexMultiTermAdd(terms, term);
//      /
dengyihao's avatar
dengyihao 已提交
314
//    }
315 316 317 318 319
//    / / for (int i = 0; i < 100; i++) {
//      / int tableId = i;
//      / int ret = indexPut(index, terms, tableId);
//      / assert(ret == 0);
//      /
dengyihao's avatar
dengyihao 已提交
320
//    }
321 322
//    / indexMultiTermDestroy(terms);
//    /
dengyihao's avatar
dengyihao 已提交
323
//  }
324 325
//  /  //
//      /
dengyihao's avatar
dengyihao 已提交
326 327
//}

dengyihao's avatar
dengyihao 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
class TFileObj {
 public:
  TFileObj(const std::string& path = "/tmp/tindex", const std::string& colName = "voltage") : path_(path), colName_(colName) {
    colId_ = 10;
    // Do Nothing
    //
  }
  int Put(SArray* tv) {
    if (reader_ != NULL) {
      tfileReaderDestroy(reader_);
      reader_ = NULL;
    }
    if (writer_ == NULL) { InitWriter(); }
    return tfileWriterPut(writer_, tv);
  }
  bool InitWriter() {
    TFileHeader header;
dengyihao's avatar
dengyihao 已提交
345 346
    header.suid = 1;
    header.version = 1;
dengyihao's avatar
dengyihao 已提交
347
    memcpy(header.colName, colName_.c_str(), colName_.size());
dengyihao's avatar
dengyihao 已提交
348 349
    header.colType = TSDB_DATA_TYPE_BINARY;

dengyihao's avatar
dengyihao 已提交
350
    std::string path(path_);
dengyihao's avatar
dengyihao 已提交
351 352
    int         colId = 2;
    char        buf[64] = {0};
dengyihao's avatar
dengyihao 已提交
353
    sprintf(buf, "%" PRIu64 "-%d-%d.tindex", header.suid, colId_, header.version);
dengyihao's avatar
dengyihao 已提交
354 355
    path.append("/").append(buf);

dengyihao's avatar
dengyihao 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    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()) {
      //
      //
    }
    return tfileReaderSearch(reader_, query, result);
  }
  ~TFileObj() {
    if (writer_) { tfileWriterDestroy(writer_); }
    if (reader_) { tfileReaderDestroy(reader_); }
  }

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

  TFileWriter* writer_;
  TFileReader* reader_;

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

dengyihao's avatar
dengyihao 已提交
395 396 397 398 399 400 401
class IndexTFileEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
    taosRemoveDir(dir.c_str());
    taosMkDir(dir.c_str());
    tfInit();
    fObj = new TFileObj(dir, colName);
dengyihao's avatar
dengyihao 已提交
402 403 404 405 406
  }

  virtual void TearDown() {
    // indexClose(index);
    // indexeptsDestroy(opts);
dengyihao's avatar
dengyihao 已提交
407
    delete fObj;
dengyihao's avatar
dengyihao 已提交
408
    tfCleanup();
dengyihao's avatar
dengyihao 已提交
409
    // tfileWriterDestroy(twrite);
dengyihao's avatar
dengyihao 已提交
410
  }
dengyihao's avatar
dengyihao 已提交
411 412 413 414 415 416 417
  TFileObj*   fObj;
  std::string dir = "/tmp/tindex";
  std::string colName = "voltage";

  int coldId = 2;
  int version = 1;
  int colType = TSDB_DATA_TYPE_BINARY;
dengyihao's avatar
dengyihao 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
};

static TFileValue* genTFileValue(const char* val) {
  TFileValue* tv = (TFileValue*)calloc(1, sizeof(TFileValue));
  int32_t     vlen = strlen(val) + 1;
  tv->colVal = (char*)calloc(1, vlen);
  memcpy(tv->colVal, val, vlen);

  tv->tableId = (SArray*)taosArrayInit(1, sizeof(uint64_t));
  for (size_t i = 0; i < 10; i++) {
    uint64_t v = i;
    taosArrayPush(tv->tableId, &v);
  }
  return tv;
}
static void destroyTFileValue(void* val) {
  TFileValue* tv = (TFileValue*)val;
  free(tv->colVal);
  taosArrayDestroy(tv->tableId);
  free(tv);
dengyihao's avatar
dengyihao 已提交
438
}
dengyihao's avatar
dengyihao 已提交
439 440
TEST_F(IndexTFileEnv, test_tfile_write) {
  TFileValue* v1 = genTFileValue("c");
dengyihao's avatar
dengyihao 已提交
441
  TFileValue* v2 = genTFileValue("ab");
dengyihao's avatar
dengyihao 已提交
442 443
  TFileValue* v3 = genTFileValue("b");
  TFileValue* v4 = genTFileValue("d");
dengyihao's avatar
dengyihao 已提交
444

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

dengyihao's avatar
dengyihao 已提交
447 448
  taosArrayPush(data, &v1);
  taosArrayPush(data, &v2);
dengyihao's avatar
dengyihao 已提交
449 450
  taosArrayPush(data, &v3);
  taosArrayPush(data, &v4);
dengyihao's avatar
dengyihao 已提交
451

dengyihao's avatar
dengyihao 已提交
452
  fObj->Put(data);
dengyihao's avatar
dengyihao 已提交
453 454 455 456
  for (size_t i = 0; i < taosArrayGetSize(data); i++) {
    destroyTFileValue(taosArrayGetP(data, i));
  }
  taosArrayDestroy(data);
dengyihao's avatar
dengyihao 已提交
457 458

  std::string colName("voltage");
dengyihao's avatar
dengyihao 已提交
459
  std::string colVal("ab");
dengyihao's avatar
dengyihao 已提交
460 461 462 463 464 465 466 467 468
  SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
  SIndexTermQuery query = {.term = term, .qType = QUERY_TERM};

  SArray* result = (SArray*)taosArrayInit(1, sizeof(uint64_t));
  fObj->Get(&query, result);
  assert(taosArrayGetSize(result) == 10);
  indexTermDestroy(term);

  // tfileWriterDestroy(twrite);
dengyihao's avatar
dengyihao 已提交
469
}
dengyihao's avatar
dengyihao 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483
class CacheObj {
 public:
  CacheObj() {
    // TODO
    cache = indexCacheCreate();
  }
  int Put(SIndexTerm* term, int16_t colId, int32_t version, uint64_t uid) {
    int ret = indexCachePut(cache, term, colId, version, uid);
    if (ret != 0) {
      //
      std::cout << "failed to put into cache: " << ret << std::endl;
    }
    return ret;
  }
dengyihao's avatar
dengyihao 已提交
484 485 486 487
  void Debug() {
    //
    indexCacheDebug(cache);
  }
dengyihao's avatar
dengyihao 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
  int Get(SIndexTermQuery* query, int16_t colId, int32_t version, SArray* result, STermValueType* s) {
    int ret = indexCacheSearch(cache, query, colId, version, result, s);
    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;
};

518
#define MAX_TERM_KEY_LEN 128
dengyihao's avatar
dengyihao 已提交
519
TEST_F(IndexCacheEnv, cache_test) {
520 521
  int     version = 0;
  int16_t colId = 0;
dengyihao's avatar
dengyihao 已提交
522
  int16_t othColId = 10;
dengyihao's avatar
dengyihao 已提交
523

524
  uint64_t    suid = 0;
dengyihao's avatar
dengyihao 已提交
525 526
  std::string colName("voltage");
  {
527 528 529
    std::string colVal("v1");
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
    coj->Put(term, colId, version++, suid++);
dengyihao's avatar
dengyihao 已提交
530 531
  }
  {
532 533 534
    std::string colVal("v3");
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
    coj->Put(term, colId, version++, suid++);
dengyihao's avatar
dengyihao 已提交
535 536
  }
  {
537 538 539
    std::string colVal("v2");
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
    coj->Put(term, colId, version++, suid++);
dengyihao's avatar
dengyihao 已提交
540 541
  }
  {
542 543 544
    std::string colVal("v3");
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
    coj->Put(term, colId, version++, suid++);
dengyihao's avatar
dengyihao 已提交
545 546
  }
  {
547 548 549
    std::string colVal("v3");
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
    coj->Put(term, colId, version++, suid++);
dengyihao's avatar
dengyihao 已提交
550
  }
551

dengyihao's avatar
dengyihao 已提交
552 553 554 555 556 557 558 559 560 561
  {
    std::string colVal("v3");
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
    coj->Put(term, othColId, version++, suid++);
  }
  {
    std::string colVal("v4");
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
    coj->Put(term, othColId, version++, suid++);
  }
dengyihao's avatar
dengyihao 已提交
562
  {
563 564 565 566 567 568
    std::string colVal("v4");
    for (size_t i = 0; i < 100; i++) {
      colVal[colVal.size() - 1] = 'a' + i;
      SIndexTerm* term =
          indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
      coj->Put(term, colId, version++, suid++);
dengyihao's avatar
dengyihao 已提交
569 570
    }
  }
dengyihao's avatar
dengyihao 已提交
571 572
  coj->Debug();
  // begin query
dengyihao's avatar
dengyihao 已提交
573
  {
574 575 576 577 578 579 580 581
    std::string colVal("v3");
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
    SIndexTermQuery query = {.term = term, .qType = QUERY_TERM};
    SArray*         ret = (SArray*)taosArrayInit(4, sizeof(suid));
    STermValueType  valType;

    coj->Get(&query, colId, 10000, ret, &valType);
    assert(taosArrayGetSize(ret) == 3);
dengyihao's avatar
dengyihao 已提交
582
  }
583 584 585 586 587 588
  {
    std::string colVal("v2");
    SIndexTerm* term = indexTermCreate(0, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(), colVal.c_str(), colVal.size());
    SIndexTermQuery query = {.term = term, .qType = QUERY_TERM};
    SArray*         ret = (SArray*)taosArrayInit(4, sizeof(suid));
    STermValueType  valType;
dengyihao's avatar
dengyihao 已提交
589

590 591
    coj->Get(&query, colId, 10000, ret, &valType);
    assert(taosArrayGetSize(ret) == 1);
dengyihao's avatar
dengyihao 已提交
592 593
  }
}