indexTests.cc 14.2 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 20
#include "index_fst.h"
#include "index_fst_counting_writer.h"
dengyihao's avatar
dengyihao 已提交
21 22 23
#include "index_fst_util.h"
#include "index_tfile.h"
#include "tutil.h"
dengyihao's avatar
dengyihao 已提交
24
using namespace std;
dengyihao's avatar
dengyihao 已提交
25
class FstWriter {
dengyihao's avatar
dengyihao 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 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 已提交
47 48 49
};

class FstReadMemory {
dengyihao's avatar
dengyihao 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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
 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 已提交
99
    fstCountingWriterDestroy(_w);
dengyihao's avatar
dengyihao 已提交
100
    fstDestroy(_fst);
dengyihao's avatar
dengyihao 已提交
101
    fstSliceDestroy(&_s);
dengyihao's avatar
dengyihao 已提交
102
    writerCtxDestroy(_wc);
dengyihao's avatar
dengyihao 已提交
103 104 105 106 107 108 109 110 111 112 113
  }

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

// TEST(IndexTest, index_create_test) {
dengyihao's avatar
dengyihao 已提交
114 115 116
//  SIndexOpts *opts = indexOptsCreate();
//  SIndex *index = indexOpen(opts, "./test");
//  if (index == NULL) {
dengyihao's avatar
dengyihao 已提交
117
//    std::cout << "index open failed" << std::endl;
dengyihao's avatar
dengyihao 已提交
118 119
//  }
//
dengyihao's avatar
dengyihao 已提交
120 121
//
//  // write
dengyihao's avatar
dengyihao 已提交
122 123
//  for (int i = 0; i < 100000; i++) {
//    SIndexMultiTerm* terms = indexMultiTermCreate();
dengyihao's avatar
dengyihao 已提交
124
//    std::string val = "field";
dengyihao's avatar
dengyihao 已提交
125 126 127
//
//    indexMultiTermAdd(terms, "tag1", strlen("tag1"), val.c_str(), val.size());
//
dengyihao's avatar
dengyihao 已提交
128
//    val.append(std::to_string(i));
dengyihao's avatar
dengyihao 已提交
129 130 131 132 133
//    indexMultiTermAdd(terms, "tag2", strlen("tag2"), val.c_str(), val.size());
//
//    val.insert(0, std::to_string(i));
//    indexMultiTermAdd(terms, "tag3", strlen("tag3"), val.c_str(), val.size());
//
dengyihao's avatar
dengyihao 已提交
134
//    val.append("const");
dengyihao's avatar
dengyihao 已提交
135 136
//    indexMultiTermAdd(terms, "tag4", strlen("tag4"), val.c_str(), val.size());
//
dengyihao's avatar
dengyihao 已提交
137
//
dengyihao's avatar
dengyihao 已提交
138 139
//    indexPut(index, terms, i);
//    indexMultiTermDestroy(terms);
dengyihao's avatar
dengyihao 已提交
140 141
//  }
//
dengyihao's avatar
dengyihao 已提交
142 143
//
//  // query
dengyihao's avatar
dengyihao 已提交
144 145
//  SIndexMultiTermQuery *multiQuery = indexMultiTermQueryCreate(MUST);
//
dengyihao's avatar
dengyihao 已提交
146 147 148
//  indexMultiTermQueryAdd(multiQuery, "tag1", strlen("tag1"), "field", strlen("field"), QUERY_PREFIX);
//  indexMultiTermQueryAdd(multiQuery, "tag3", strlen("tag3"), "0field0", strlen("0field0"), QUERY_TERM);
//
dengyihao's avatar
dengyihao 已提交
149
//  SArray *result = (SArray *)taosArrayInit(10, sizeof(int));
dengyihao's avatar
dengyihao 已提交
150 151 152 153 154 155 156
//  indexSearch(index, multiQuery, result);
//
//  std::cout << "taos'size : " << taosArrayGetSize(result) << std::endl;
//  for (int i = 0;  i < taosArrayGetSize(result); i++) {
//    int *v = (int *)taosArrayGet(result, i);
//    std::cout << "value --->" << *v  << std::endl;
//  }
dengyihao's avatar
dengyihao 已提交
157
//  // add more test case
dengyihao's avatar
dengyihao 已提交
158 159
//  indexMultiTermQueryDestroy(multiQuery);
//
dengyihao's avatar
dengyihao 已提交
160 161
//  indexOptsDestroy(opts);
//  indexClose(index);
dengyihao's avatar
dengyihao 已提交
162 163
//  //
//}
dengyihao's avatar
dengyihao 已提交
164

dengyihao's avatar
dengyihao 已提交
165 166 167
#define L 100
#define M 100
#define N 100
dengyihao's avatar
dengyihao 已提交
168

dengyihao's avatar
dengyihao 已提交
169 170
int Performance_fstWriteRecords(FstWriter* b) {
  std::string str("aa");
dengyihao's avatar
dengyihao 已提交
171
  for (int i = 0; i < L; i++) {
dengyihao's avatar
dengyihao 已提交
172
    str[0] = 'a' + i;
dengyihao's avatar
dengyihao 已提交
173 174
    str.resize(2);
    for (int j = 0; j < M; j++) {
dengyihao's avatar
dengyihao 已提交
175 176
      str[1] = 'a' + j;
      str.resize(2);
dengyihao's avatar
dengyihao 已提交
177
      for (int k = 0; k < N; k++) {
dengyihao's avatar
dengyihao 已提交
178 179
        str.push_back('a');
        b->Put(str, k);
dengyihao's avatar
dengyihao 已提交
180
        printf("(%d, %d, %d, %s)\n", i, j, k, str.c_str());
dengyihao's avatar
dengyihao 已提交
181
      }
dengyihao's avatar
dengyihao 已提交
182
    }
dengyihao's avatar
dengyihao 已提交
183
  }
dengyihao's avatar
dengyihao 已提交
184
  return L * M * N;
dengyihao's avatar
dengyihao 已提交
185 186
}

dengyihao's avatar
dengyihao 已提交
187
void Performance_fstReadRecords(FstReadMemory* m) {
dengyihao's avatar
dengyihao 已提交
188 189 190
  std::string str("aa");
  for (int i = 0; i < M; i++) {
    str[0] = 'a' + i;
dengyihao's avatar
dengyihao 已提交
191 192
    str.resize(2);
    for (int j = 0; j < N; j++) {
dengyihao's avatar
dengyihao 已提交
193 194 195 196
      str[1] = 'a' + j;
      str.resize(2);
      for (int k = 0; k < L; k++) {
        str.push_back('a');
dengyihao's avatar
dengyihao 已提交
197
        uint64_t val, cost;
dengyihao's avatar
dengyihao 已提交
198
        if (m->GetWithTimeCostUs(str, &val, &cost)) {
dengyihao's avatar
dengyihao 已提交
199
          printf("succes to get kv(%s, %" PRId64 "), cost: %" PRId64 "\n", str.c_str(), val, cost);
dengyihao's avatar
dengyihao 已提交
200 201 202 203
        } else {
          printf("failed to get key: %s\n", str.c_str());
        }
      }
dengyihao's avatar
dengyihao 已提交
204
    }
dengyihao's avatar
dengyihao 已提交
205
  }
dengyihao's avatar
dengyihao 已提交
206
}
dengyihao's avatar
dengyihao 已提交
207
void checkFstPerf() {
dengyihao's avatar
dengyihao 已提交
208 209
  FstWriter* fw = new FstWriter;
  int64_t    s = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
210

dengyihao's avatar
dengyihao 已提交
211
  int     num = Performance_fstWriteRecords(fw);
dengyihao's avatar
dengyihao 已提交
212
  int64_t e = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
213
  printf("write %d record cost %" PRId64 "us\n", num, e - s);
dengyihao's avatar
dengyihao 已提交
214 215
  delete fw;

dengyihao's avatar
dengyihao 已提交
216 217 218
  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init()) { printf("success to init fst read"); }
  Performance_fstReadRecords(m);
dengyihao's avatar
dengyihao 已提交
219
  delete m;
dengyihao's avatar
dengyihao 已提交
220
}
dengyihao's avatar
dengyihao 已提交
221

dengyihao's avatar
dengyihao 已提交
222
void checkFstPrefixSearch() {
dengyihao's avatar
dengyihao 已提交
223 224 225
  FstWriter*  fw = new FstWriter;
  int64_t     s = taosGetTimestampUs();
  int         count = 2;
dengyihao's avatar
dengyihao 已提交
226
  std::string key("ab");
dengyihao's avatar
dengyihao 已提交
227

dengyihao's avatar
dengyihao 已提交
228
  for (int i = 0; i < count; i++) {
dengyihao's avatar
dengyihao 已提交
229 230
    key[1] = key[1] + i;
    fw->Put(key, i);
dengyihao's avatar
dengyihao 已提交
231 232
  }
  int64_t e = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
233

dengyihao's avatar
dengyihao 已提交
234 235
  std::cout << "insert data count :  " << count << "elapas time: " << e - s << std::endl;
  delete fw;
dengyihao's avatar
dengyihao 已提交
236

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

  // prefix search
dengyihao's avatar
dengyihao 已提交
245
  std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
246 247 248 249

  AutomationCtx* ctx = automCtxCreate((void*)"ab", AUTOMATION_PREFIX);
  m->Search(ctx, result);
  assert(result.size() == count);
dengyihao's avatar
dengyihao 已提交
250
  for (int i = 0; i < result.size(); i++) {
dengyihao's avatar
dengyihao 已提交
251
    assert(result[i] == i);  // check result
dengyihao's avatar
dengyihao 已提交
252 253 254 255
  }

  free(ctx);
  delete m;
dengyihao's avatar
dengyihao 已提交
256
}
dengyihao's avatar
dengyihao 已提交
257
void validateFst() {
dengyihao's avatar
dengyihao 已提交
258 259 260 261
  int        val = 100;
  int        count = 100;
  FstWriter* fw = new FstWriter;
  // write
dengyihao's avatar
dengyihao 已提交
262
  {
dengyihao's avatar
dengyihao 已提交
263
    std::string key("ab");
dengyihao's avatar
dengyihao 已提交
264
    for (int i = 0; i < count; i++) {
dengyihao's avatar
dengyihao 已提交
265
      key.push_back('a' + i);
dengyihao's avatar
dengyihao 已提交
266
      fw->Put(key, val - i);
dengyihao's avatar
dengyihao 已提交
267
    }
dengyihao's avatar
dengyihao 已提交
268 269
  }
  delete fw;
dengyihao's avatar
dengyihao 已提交
270

dengyihao's avatar
dengyihao 已提交
271
  // read
dengyihao's avatar
dengyihao 已提交
272 273 274
  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init() == false) {
    std::cout << "init readMemory failed" << std::endl;
dengyihao's avatar
dengyihao 已提交
275 276
    delete m;
    return;
dengyihao's avatar
dengyihao 已提交
277
  }
dengyihao's avatar
dengyihao 已提交
278 279

  {
dengyihao's avatar
dengyihao 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    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 已提交
295
    }
dengyihao's avatar
dengyihao 已提交
296
  }
dengyihao's avatar
dengyihao 已提交
297
  delete m;
dengyihao's avatar
dengyihao 已提交
298
}
dengyihao's avatar
dengyihao 已提交
299

dengyihao's avatar
dengyihao 已提交
300
class IndexEnv : public ::testing::Test {
dengyihao's avatar
dengyihao 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
 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 已提交
316 317
};

dengyihao's avatar
dengyihao 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
// 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);
//    }
//    indexMultiTermDestroy(terms);
//  }
//  // 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);
//    }
//    {
//      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);
//    }
//
//    for (int i = 0; i < 100; i++) {
//      int tableId = i;
//      int ret = indexPut(index, terms, tableId);
//      assert(ret == 0);
//    }
//    indexMultiTermDestroy(terms);
//  }
//  //
//}

dengyihao's avatar
dengyihao 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
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 已提交
375 376
    header.suid = 1;
    header.version = 1;
dengyihao's avatar
dengyihao 已提交
377
    memcpy(header.colName, colName_.c_str(), colName_.size());
dengyihao's avatar
dengyihao 已提交
378 379
    header.colType = TSDB_DATA_TYPE_BINARY;

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

dengyihao's avatar
dengyihao 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    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_;
};
class IndexTFileEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
    taosRemoveDir(dir.c_str());
    taosMkDir(dir.c_str());
    tfInit();
    fObj = new TFileObj(dir, colName);

    // std::string colName("voltage");
    // header.suid = 1;
    // header.version = 1;
    // memcpy(header.colName, colName.c_str(), colName.size());
    // header.colType = TSDB_DATA_TYPE_BINARY;

    // std::string path(dir);
    // int         colId = 2;
    // char        buf[64] = {0};
    // sprintf(buf, "%" PRIu64 "-%d-%d.tindex", header.suid, colId, header.version);
    // path.append("/").append(buf);

    // ctx = writerCtxCreate(TFile, path.c_str(), false, 64 * 1024 * 1024);
dengyihao's avatar
dengyihao 已提交
445

dengyihao's avatar
dengyihao 已提交
446
    // twrite = tfileWriterCreate(ctx, &header);
dengyihao's avatar
dengyihao 已提交
447 448 449 450 451
  }

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

  int coldId = 2;
  int version = 1;
  int colType = TSDB_DATA_TYPE_BINARY;

  // WriterCtx*   ctx = NULL;
  // TFileHeader  header;
  // TFileWriter* twrite = NULL;
dengyihao's avatar
dengyihao 已提交
467 468
};

dengyihao's avatar
dengyihao 已提交
469 470 471 472
// static TFileWriter* genTFileWriter(const char* path, TFileHeader* header) {
//  char       buf[128] = {0};
//  WriterCtx* ctx = writerCtxCreate(TFile, path, false, )
//}
dengyihao's avatar
dengyihao 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
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 已提交
491
}
dengyihao's avatar
dengyihao 已提交
492

dengyihao's avatar
dengyihao 已提交
493 494 495
TEST_F(IndexTFileEnv, test_tfile_write) {
  TFileValue* v1 = genTFileValue("c");
  TFileValue* v2 = genTFileValue("a");
dengyihao's avatar
dengyihao 已提交
496 497
  TFileValue* v3 = genTFileValue("b");
  TFileValue* v4 = genTFileValue("d");
dengyihao's avatar
dengyihao 已提交
498

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

dengyihao's avatar
dengyihao 已提交
501 502
  taosArrayPush(data, &v1);
  taosArrayPush(data, &v2);
dengyihao's avatar
dengyihao 已提交
503 504
  taosArrayPush(data, &v3);
  taosArrayPush(data, &v4);
dengyihao's avatar
dengyihao 已提交
505

dengyihao's avatar
dengyihao 已提交
506
  fObj->Put(data);
dengyihao's avatar
dengyihao 已提交
507 508 509 510
  for (size_t i = 0; i < taosArrayGetSize(data); i++) {
    destroyTFileValue(taosArrayGetP(data, i));
  }
  taosArrayDestroy(data);
dengyihao's avatar
dengyihao 已提交
511 512 513 514 515 516 517 518 519 520 521 522

  std::string colName("voltage");
  std::string colVal("b");
  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 已提交
523
}