fstTest.cc 17.5 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1

dengyihao's avatar
dengyihao 已提交
2
#include <algorithm>
dengyihao's avatar
dengyihao 已提交
3 4
#include <iostream>
#include <string>
dengyihao's avatar
dengyihao 已提交
5
#include <thread>
dengyihao's avatar
dengyihao 已提交
6 7
#include <vector>
#include "index.h"
dengyihao's avatar
dengyihao 已提交
8 9 10
#include "indexCache.h"
#include "indexFst.h"
#include "indexFstUtil.h"
dengyihao's avatar
dengyihao 已提交
11
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
12
#include "indexTfile.h"
dengyihao's avatar
dengyihao 已提交
13 14
#include "tskiplist.h"
#include "tutil.h"
dengyihao's avatar
dengyihao 已提交
15
void* callback(void* s) { return s; }
dengyihao's avatar
dengyihao 已提交
16

17
static std::string fileName = TD_TMP_DIR_PATH "tindex.tindex";
dengyihao's avatar
dengyihao 已提交
18 19 20
class FstWriter {
 public:
  FstWriter() {
21
    taosRemoveFile(fileName.c_str());
dengyihao's avatar
dengyihao 已提交
22
    _wc = idxFileCtxCreate(TFile, fileName.c_str(), false, 64 * 1024 * 1024);
dengyihao's avatar
dengyihao 已提交
23 24 25
    _b = fstBuilderCreate(_wc, 0);
  }
  bool Put(const std::string& key, uint64_t val) {
dengyihao's avatar
dengyihao 已提交
26 27 28 29
    // char buf[128] = {0};
    // int  len = 0;
    // taosMbsToUcs4(key.c_str(), key.size(), buf, 128, &len);
    // FstSlice skey = fstSliceCreate((uint8_t*)buf, len);
dengyihao's avatar
dengyihao 已提交
30 31
    FstSlice skey = fstSliceCreate((uint8_t*)key.c_str(), key.size());
    bool     ok = fstBuilderInsert(_b, skey, val);
dengyihao's avatar
dengyihao 已提交
32

dengyihao's avatar
dengyihao 已提交
33 34 35 36 37 38 39
    fstSliceDestroy(&skey);
    return ok;
  }
  ~FstWriter() {
    fstBuilderFinish(_b);
    fstBuilderDestroy(_b);

dengyihao's avatar
dengyihao 已提交
40
    idxFileCtxDestroy(_wc, false);
dengyihao's avatar
dengyihao 已提交
41 42 43 44
  }

 private:
  FstBuilder* _b;
dengyihao's avatar
dengyihao 已提交
45
  IFileCtx*   _wc;
dengyihao's avatar
dengyihao 已提交
46 47 48 49
};

class FstReadMemory {
 public:
50
  FstReadMemory(int32_t size, const std::string& fileName = TD_TMP_DIR_PATH "tindex.tindex") {
dengyihao's avatar
dengyihao 已提交
51 52
    _wc = idxFileCtxCreate(TFile, fileName.c_str(), true, 64 * 1024);
    _w = idxFileCreate(_wc);
dengyihao's avatar
dengyihao 已提交
53 54 55 56
    _size = size;
    memset((void*)&_s, 0, sizeof(_s));
  }
  bool init() {
wafwerar's avatar
wafwerar 已提交
57
    char* buf = (char*)taosMemoryCalloc(1, sizeof(char) * _size);
dengyihao's avatar
dengyihao 已提交
58
    int   nRead = idxFileRead(_w, (uint8_t*)buf, _size);
dengyihao's avatar
dengyihao 已提交
59 60 61
    if (nRead <= 0) {
      return false;
    }
dengyihao's avatar
dengyihao 已提交
62 63 64
    _size = nRead;
    _s = fstSliceCreate((uint8_t*)buf, _size);
    _fst = fstCreate(&_s);
wafwerar's avatar
wafwerar 已提交
65
    taosMemoryFree(buf);
dengyihao's avatar
dengyihao 已提交
66 67 68
    return _fst != NULL;
  }
  bool Get(const std::string& key, uint64_t* val) {
dengyihao's avatar
dengyihao 已提交
69 70 71 72 73
    // char buf[128] = {0};
    // int  len = 0;
    // taosMbsToUcs4(key.c_str(), key.size(), buf, 128, &len);
    // FstSlice skey = fstSliceCreate((uint8_t*)buf, len);

dengyihao's avatar
dengyihao 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
    FstSlice skey = fstSliceCreate((uint8_t*)key.c_str(), key.size());
    bool     ok = fstGet(_fst, &skey, val);
    fstSliceDestroy(&skey);
    return ok;
  }
  bool GetWithTimeCostUs(const std::string& key, uint64_t* val, uint64_t* elapse) {
    int64_t s = taosGetTimestampUs();
    bool    ok = this->Get(key, val);
    int64_t e = taosGetTimestampUs();
    *elapse = e - s;
    return ok;
  }
  // add later
dengyihao's avatar
dengyihao 已提交
87 88 89 90 91
  bool Search(FAutoCtx* ctx, std::vector<uint64_t>& result) {
    FStmBuilder* sb = fstSearch(_fst, ctx);
    FStmSt*      st = stmBuilderIntoStm(sb);
    FStmStRslt*  rt = NULL;
    while ((rt = stmStNextWith(st, NULL)) != NULL) {
dengyihao's avatar
dengyihao 已提交
92 93 94 95 96 97
      // result.push_back((uint64_t)(rt->out.out));
      FstSlice*   s = &rt->data;
      int32_t     sz = 0;
      char*       ch = (char*)fstSliceData(s, &sz);
      std::string key(ch, sz);
      printf("key: %s, val: %" PRIu64 "\n", key.c_str(), (uint64_t)(rt->out.out));
dengyihao's avatar
dengyihao 已提交
98
      result.push_back(rt->out.out);
dengyihao's avatar
dengyihao 已提交
99
      swsResultDestroy(rt);
dengyihao's avatar
dengyihao 已提交
100
    }
dengyihao's avatar
dengyihao 已提交
101 102
    stmStDestroy(st);
    stmBuilderDestroy(sb);
dengyihao's avatar
dengyihao 已提交
103 104
    return true;
  }
dengyihao's avatar
dengyihao 已提交
105
  bool SearchRange(FAutoCtx* ctx, const std::string& low, RangeType lowType, const std::string& high,
dengyihao's avatar
dengyihao 已提交
106
                   RangeType highType, std::vector<uint64_t>& result) {
dengyihao's avatar
dengyihao 已提交
107
    FStmBuilder* sb = fstSearch(_fst, ctx);
dengyihao's avatar
dengyihao 已提交
108 109 110 111 112

    FstSlice l = fstSliceCreate((uint8_t*)low.c_str(), low.size());
    FstSlice h = fstSliceCreate((uint8_t*)high.c_str(), high.size());

    // range [low, high);
dengyihao's avatar
dengyihao 已提交
113 114
    stmBuilderSetRange(sb, &l, lowType);
    stmBuilderSetRange(sb, &h, highType);
dengyihao's avatar
dengyihao 已提交
115 116 117 118

    fstSliceDestroy(&l);
    fstSliceDestroy(&h);

dengyihao's avatar
dengyihao 已提交
119 120 121
    FStmSt*     st = stmBuilderIntoStm(sb);
    FStmStRslt* rt = NULL;
    while ((rt = stmStNextWith(st, NULL)) != NULL) {
dengyihao's avatar
dengyihao 已提交
122 123 124 125 126 127 128 129
      // result.push_back((uint64_t)(rt->out.out));
      FstSlice*   s = &rt->data;
      int32_t     sz = 0;
      char*       ch = (char*)fstSliceData(s, &sz);
      std::string key(ch, sz);
      printf("key: %s, val: %" PRIu64 "\n", key.c_str(), (uint64_t)(rt->out.out));
      result.push_back(rt->out.out);
      swsResultDestroy(rt);
dengyihao's avatar
dengyihao 已提交
130
    }
dengyihao's avatar
dengyihao 已提交
131 132
    stmStDestroy(st);
    stmBuilderDestroy(sb);
dengyihao's avatar
dengyihao 已提交
133 134
    return true;
  }
dengyihao's avatar
dengyihao 已提交
135
  bool SearchWithTimeCostUs(FAutoCtx* ctx, std::vector<uint64_t>& result) {
dengyihao's avatar
dengyihao 已提交
136 137 138 139 140 141 142
    int64_t s = taosGetTimestampUs();
    bool    ok = this->Search(ctx, result);
    int64_t e = taosGetTimestampUs();
    return ok;
  }

  ~FstReadMemory() {
dengyihao's avatar
dengyihao 已提交
143
    idxFileDestroy(_w);
dengyihao's avatar
dengyihao 已提交
144 145
    fstDestroy(_fst);
    fstSliceDestroy(&_s);
dengyihao's avatar
dengyihao 已提交
146
    idxFileCtxDestroy(_wc, false);
dengyihao's avatar
dengyihao 已提交
147 148 149
  }

 private:
dengyihao's avatar
dengyihao 已提交
150 151 152 153 154
  IdxFstFile* _w;
  Fst*        _fst;
  FstSlice    _s;
  IFileCtx*   _wc;
  int32_t     _size;
dengyihao's avatar
dengyihao 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
};

#define L 100
#define M 100
#define N 100

int Performance_fstWriteRecords(FstWriter* b) {
  std::string str("aa");
  for (int i = 0; i < L; i++) {
    str[0] = 'a' + i;
    str.resize(2);
    for (int j = 0; j < M; j++) {
      str[1] = 'a' + j;
      str.resize(2);
      for (int k = 0; k < N; k++) {
        str.push_back('a');
        b->Put(str, k);
        printf("(%d, %d, %d, %s)\n", i, j, k, str.c_str());
      }
    }
  }
  return L * M * N;
}
dengyihao's avatar
dengyihao 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
void Performance_fstReadRecords(FstReadMemory* m) {
  std::string str("aa");
  for (int i = 0; i < M; i++) {
    str[0] = 'a' + i;
    str.resize(2);
    for (int j = 0; j < N; j++) {
      str[1] = 'a' + j;
      str.resize(2);
      for (int k = 0; k < L; k++) {
        str.push_back('a');
        uint64_t val, cost;
        if (m->GetWithTimeCostUs(str, &val, &cost)) {
          printf("succes to get kv(%s, %" PRId64 "), cost: %" PRId64 "\n", str.c_str(), val, cost);
        } else {
          printf("failed to get key: %s\n", str.c_str());
        }
      }
    }
  }
}

void checkMillonWriteAndReadOfFst() {
  FstWriter* fw = new FstWriter;
  Performance_fstWriteRecords(fw);
  delete fw;
  FstReadMemory* fr = new FstReadMemory(1024 * 64 * 1024);

dengyihao's avatar
dengyihao 已提交
205 206 207
  if (fr->init()) {
    printf("success to init fst read");
  }
dengyihao's avatar
dengyihao 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

  Performance_fstReadRecords(fr);
  delete fr;
}
void checkFstLongTerm() {
  FstWriter* fw = new FstWriter;
  // Performance_fstWriteRecords(fw);

  fw->Put("A B", 1);
  fw->Put("C", 2);
  fw->Put("a", 3);
  delete fw;

  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init() == false) {
    std::cout << "init readMemory failed" << std::endl;
    delete m;
    return;
  }
  {
    uint64_t val = 0;
    if (m->Get("A B", &val)) {
      std::cout << "success to Get: " << val << std::endl;
    } else {
      std::cout << "failed to Get:" << val << std::endl;
    }
  }
  {
    uint64_t val = 0;
    if (m->Get("C", &val)) {
      std::cout << "success to Get: " << val << std::endl;
    } else {
      std::cout << "failed to Get:" << val << std::endl;
    }
  }
  {
    uint64_t val = 0;
    if (m->Get("a", &val)) {
      std::cout << "success to Get: " << val << std::endl;
    } else {
      std::cout << "failed to Get:" << val << std::endl;
    }
  }

  // prefix search
  // std::vector<uint64_t> result;

dengyihao's avatar
dengyihao 已提交
255
  // FAutoCtx* ctx = automCtxCreate((void*)"ab", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
256 257 258 259 260 261
  // m->Search(ctx, result);
  // std::cout << "size: " << result.size() << std::endl;
  // assert(result.size() == count);
  // for (int i = 0; i < result.size(); i++) {
  // assert(result[i] == i);  // check result
  //}
wafwerar's avatar
wafwerar 已提交
262
  // taosMemoryFree(ctx);
dengyihao's avatar
dengyihao 已提交
263 264
  // delete m;
}
dengyihao's avatar
dengyihao 已提交
265
void checkFstCheckIterator1() {
dengyihao's avatar
dengyihao 已提交
266 267 268
  FstWriter* fw = new FstWriter;
  int64_t    s = taosGetTimestampUs();
  int        count = 2;
dengyihao's avatar
dengyihao 已提交
269
  // Performance_fstWriteRecords(fw);
dengyihao's avatar
dengyihao 已提交
270 271 272
  int64_t e = taosGetTimestampUs();

  std::cout << "insert data count :  " << count << "elapas time: " << e - s << std::endl;
dengyihao's avatar
dengyihao 已提交
273

274 275
  fw->Put("test1&^D&10", 1);
  fw->Put("test2&^D&10", 2);
dengyihao's avatar
dengyihao 已提交
276 277 278 279 280 281 282 283 284 285 286 287
  delete fw;

  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init() == false) {
    std::cout << "init readMemory failed" << std::endl;
    delete m;
    return;
  }

  // prefix search
  std::vector<uint64_t> result;

dengyihao's avatar
dengyihao 已提交
288
  FAutoCtx* ctx = automCtxCreate((void*)"He", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
289 290 291 292 293 294
  m->Search(ctx, result);
  std::cout << "size: " << result.size() << std::endl;
  // assert(result.size() == count);
  for (int i = 0; i < result.size(); i++) {
    // assert(result[i] == i);  // check result
  }
dengyihao's avatar
dengyihao 已提交
295
  automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
296 297 298

  delete m;
}
dengyihao's avatar
dengyihao 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
void checkFstCheckIterator2() {
  FstWriter* fw = new FstWriter;
  int64_t    s = taosGetTimestampUs();
  int        count = 2;
  // Performance_fstWriteRecords(fw);
  int64_t e = taosGetTimestampUs();

  std::cout << "insert data count :  " << count << "elapas time: " << e - s << std::endl;

  fw->Put("a", 1);
  fw->Put("b", 2);
  fw->Put("c", 4);
  delete fw;

  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init() == false) {
    std::cout << "init readMemory failed" << std::endl;
    delete m;
    return;
  }

  // prefix search
  std::vector<uint64_t> result;

dengyihao's avatar
dengyihao 已提交
323
  FAutoCtx* ctx = automCtxCreate((void*)"He", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
324 325 326 327 328 329
  m->Search(ctx, result);
  std::cout << "size: " << result.size() << std::endl;
  // assert(result.size() == count);
  for (int i = 0; i < result.size(); i++) {
    // assert(result[i] == i);  // check result
  }
dengyihao's avatar
dengyihao 已提交
330
  automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
331 332 333

  delete m;
}
dengyihao's avatar
dengyihao 已提交
334 335 336 337 338 339 340 341 342 343 344 345
void checkFstCheckIteratorPrefix() {
  FstWriter* fw = new FstWriter;
  int64_t    s = taosGetTimestampUs();
  int        count = 2;
  // Performance_fstWriteRecords(fw);
  int64_t e = taosGetTimestampUs();

  std::cout << "insert data count :  " << count << "elapas time: " << e - s << std::endl;

  fw->Put("Hello world", 1);
  fw->Put("Hello worle", 2);
  fw->Put("hello worlf", 4);
dengyihao's avatar
dengyihao 已提交
346 347 348 349 350
  fw->Put("ja", 4);
  fw->Put("jb", 4);
  fw->Put("jc", 4);
  fw->Put("jddddddddd", 4);
  fw->Put("jefffffff", 4);
dengyihao's avatar
dengyihao 已提交
351 352 353 354 355 356 357 358
  delete fw;

  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init() == false) {
    std::cout << "init readMemory failed" << std::endl;
    delete m;
    return;
  }
dengyihao's avatar
dengyihao 已提交
359 360 361
  {
    // prefix search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
362

dengyihao's avatar
dengyihao 已提交
363
    FAutoCtx* ctx = automCtxCreate((void*)"he", AUTOMATION_PREFIX);
dengyihao's avatar
dengyihao 已提交
364 365
    m->Search(ctx, result);
    assert(result.size() == 1);
dengyihao's avatar
dengyihao 已提交
366
    automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
367 368 369 370
  }
  {
    // prefix search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
371

dengyihao's avatar
dengyihao 已提交
372
    FAutoCtx* ctx = automCtxCreate((void*)"Hello", AUTOMATION_PREFIX);
dengyihao's avatar
dengyihao 已提交
373 374
    m->Search(ctx, result);
    assert(result.size() == 2);
dengyihao's avatar
dengyihao 已提交
375
    automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
376
  }
dengyihao's avatar
dengyihao 已提交
377 378
  {
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
379

dengyihao's avatar
dengyihao 已提交
380
    FAutoCtx* ctx = automCtxCreate((void*)"jddd", AUTOMATION_PREFIX);
dengyihao's avatar
dengyihao 已提交
381 382
    m->Search(ctx, result);
    assert(result.size() == 1);
dengyihao's avatar
dengyihao 已提交
383
    automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
384
  }
dengyihao's avatar
dengyihao 已提交
385 386
  delete m;
}
dengyihao's avatar
dengyihao 已提交
387
void checkFstCheckIteratorRange1() {
dengyihao's avatar
dengyihao 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400
  FstWriter* fw = new FstWriter;
  int64_t    s = taosGetTimestampUs();
  int        count = 2;
  // Performance_fstWriteRecords(fw);
  int64_t e = taosGetTimestampUs();

  std::cout << "insert data count :  " << count << "elapas time: " << e - s << std::endl;

  fw->Put("a", 1);
  fw->Put("b", 2);
  fw->Put("c", 3);
  fw->Put("d", 4);
  fw->Put("e", 5);
dengyihao's avatar
dengyihao 已提交
401 402
  fw->Put("f", 5);
  fw->Put("G", 5);
dengyihao's avatar
dengyihao 已提交
403 404 405 406 407 408 409 410 411 412 413
  delete fw;

  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init() == false) {
    std::cout << "init readMemory failed" << std::endl;
    delete m;
    return;
  }
  {
    // prefix search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
414
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
415
    // [b, e)
dengyihao's avatar
dengyihao 已提交
416
    m->SearchRange(ctx, "b", GE, "e", LT, result);
dengyihao's avatar
dengyihao 已提交
417
    assert(result.size() == 3);
dengyihao's avatar
dengyihao 已提交
418
    automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
419
  }
dengyihao's avatar
dengyihao 已提交
420 421 422
  {
    // prefix search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
423
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
424 425 426
    // [b, e)
    m->SearchRange(ctx, "b", GT, "e", LT, result);
    assert(result.size() == 2);
dengyihao's avatar
dengyihao 已提交
427
    automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
428 429 430 431
  }
  {
    // prefix search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
432
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
433 434 435
    // [b, e)
    m->SearchRange(ctx, "b", GT, "e", LE, result);
    assert(result.size() == 3);
dengyihao's avatar
dengyihao 已提交
436
    automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
437 438 439 440
  }
  {
    // prefix search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
441
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
442 443 444
    // [b, e)
    m->SearchRange(ctx, "b", GE, "e", LE, result);
    assert(result.size() == 4);
dengyihao's avatar
dengyihao 已提交
445
    automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
446
  }
dengyihao's avatar
dengyihao 已提交
447
  delete m;
dengyihao's avatar
dengyihao 已提交
448 449 450 451 452 453 454 455 456 457 458
}
void checkFstCheckIteratorRange2() {
  FstWriter* fw = new FstWriter;
  int64_t    s = taosGetTimestampUs();
  int        count = 2;
  // Performance_fstWriteRecords(fw);
  int64_t e = taosGetTimestampUs();

  std::cout << "insert data count :  " << count << "elapas time: " << e - s << std::endl;

  fw->Put("ab", 1);
dengyihao's avatar
dengyihao 已提交
459
  fw->Put("b", 2);
dengyihao's avatar
dengyihao 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472
  fw->Put("cdd", 3);
  fw->Put("cde", 3);
  fw->Put("ddd", 4);
  fw->Put("ed", 5);
  delete fw;

  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init() == false) {
    std::cout << "init readMemory failed" << std::endl;
    delete m;
    return;
  }
  {
dengyihao's avatar
dengyihao 已提交
473
    // range  search
dengyihao's avatar
dengyihao 已提交
474
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
475
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
476
    // [b, e)
dengyihao's avatar
dengyihao 已提交
477
    m->SearchRange(ctx, "b", GE, "ed", LT, result);
dengyihao's avatar
dengyihao 已提交
478
    assert(result.size() == 4);
dengyihao's avatar
dengyihao 已提交
479
    automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
480
  }
dengyihao's avatar
dengyihao 已提交
481 482 483
  {
    // range  search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
484
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
485 486 487 488 489
    // [b, e)
    m->SearchRange(ctx, "bb", GE, "ed", LT, result);
    assert(result.size() == 3);
    automCtxDestroy(ctx);
  }
dengyihao's avatar
dengyihao 已提交
490 491 492
  {
    // range  search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
493
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
494 495 496
    // [b, e)
    m->SearchRange(ctx, "b", GE, "ed", LE, result);
    assert(result.size() == 5);
dengyihao's avatar
dengyihao 已提交
497 498
    automCtxDestroy(ctx);
    // taosMemoryFree(ctx);
dengyihao's avatar
dengyihao 已提交
499 500 501 502
  }
  {
    // range  search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
503
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
504 505 506
    // [b, e)
    m->SearchRange(ctx, "b", GT, "ed", LE, result);
    assert(result.size() == 4);
dengyihao's avatar
dengyihao 已提交
507
    automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
508 509 510 511
  }
  {
    // range  search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
512
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
513 514 515
    // [b, e)
    m->SearchRange(ctx, "b", GT, "ed", LT, result);
    assert(result.size() == 3);
dengyihao's avatar
dengyihao 已提交
516
    automCtxDestroy(ctx);
dengyihao's avatar
dengyihao 已提交
517
  }
dengyihao's avatar
dengyihao 已提交
518
  delete m;
dengyihao's avatar
dengyihao 已提交
519
}
dengyihao's avatar
dengyihao 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
void checkFstCheckIteratorRange3() {
  FstWriter* fw = new FstWriter;
  int64_t    s = taosGetTimestampUs();
  int        count = 2;
  // Performance_fstWriteRecords(fw);
  int64_t e = taosGetTimestampUs();

  std::cout << "insert data count :  " << count << "elapas time: " << e - s << std::endl;

  fw->Put("ab", 1);
  fw->Put("b", 2);
  fw->Put("cdd", 3);
  fw->Put("cde", 3);
  fw->Put("ddd", 4);
  fw->Put("ed", 5);
  delete fw;

  FstReadMemory* m = new FstReadMemory(1024 * 64);
  if (m->init() == false) {
    std::cout << "init readMemory failed" << std::endl;
    delete m;
    return;
  }
  {
    // range  search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
546
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
547 548 549 550 551 552 553 554
    // [b, e)
    m->SearchRange(ctx, "b", GE, "", (RangeType)10, result);
    assert(result.size() == 5);
    automCtxDestroy(ctx);
  }
  {
    // range  search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
555
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
556 557 558 559 560 561 562 563 564
    // [b, e)
    m->SearchRange(ctx, "", (RangeType)20, "ab", LE, result);
    assert(result.size() == 1);
    automCtxDestroy(ctx);
    // taosMemoryFree(ctx);
  }
  {
    // range  search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
565
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
566 567 568 569 570 571 572 573
    // [b, e)
    m->SearchRange(ctx, "", (RangeType)30, "ab", LT, result);
    assert(result.size() == 0);
    automCtxDestroy(ctx);
  }
  {
    // range  search
    std::vector<uint64_t> result;
dengyihao's avatar
dengyihao 已提交
574
    FAutoCtx*             ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
575 576 577 578 579 580 581
    // [b, e)
    m->SearchRange(ctx, "ed", GT, "ed", (RangeType)40, result);
    assert(result.size() == 0);
    automCtxDestroy(ctx);
  }
  delete m;
}
dengyihao's avatar
dengyihao 已提交
582 583 584

void fst_get(Fst* fst) {
  for (int i = 0; i < 10000; i++) {
dengyihao's avatar
dengyihao 已提交
585
    std::string term = "Hello World";
dengyihao's avatar
dengyihao 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    FstSlice    key = fstSliceCreate((uint8_t*)term.c_str(), term.size());
    uint64_t    offset = 0;
    bool        ret = fstGet(fst, &key, &offset);
    if (ret == false) {
      std::cout << "not found" << std::endl;
    } else {
      std::cout << "found value:" << offset << std::endl;
    }
  }
}

#define NUM_OF_THREAD 10
void validateTFile(char* arg) {
  std::thread threads[NUM_OF_THREAD];
  // std::vector<std::thread> threads;
dengyihao's avatar
dengyihao 已提交
601
  TFileReader* reader = tfileReaderOpen(arg, 0, 20000000, "tag1");
dengyihao's avatar
dengyihao 已提交
602 603 604 605 606 607 608 609 610 611 612

  for (int i = 0; i < NUM_OF_THREAD; i++) {
    threads[i] = std::thread(fst_get, reader->fst);
    // threads.push_back(fst_get, reader->fst);
    // std::thread t(fst_get, reader->fst);
  }
  for (int i = 0; i < NUM_OF_THREAD; i++) {
    // wait join
    threads[i].join();
  }
}
dengyihao's avatar
dengyihao 已提交
613

dengyihao's avatar
add UT  
dengyihao 已提交
614
void iterTFileReader(char* path, char* uid, char* colName, char* ver) {
dengyihao's avatar
dengyihao 已提交
615
  // tfInit();
dengyihao's avatar
dengyihao 已提交
616

dengyihao's avatar
add UT  
dengyihao 已提交
617 618 619 620 621 622 623 624 625
  uint64_t suid = atoi(uid);
  int      version = atoi(ver);

  TFileReader* reader = tfileReaderOpen(path, suid, version, colName);

  Iterate* iter = tfileIteratorCreate(reader);
  bool     tn = iter ? iter->next(iter) : false;
  int      count = 0;
  int      termCount = 0;
dengyihao's avatar
dengyihao 已提交
626 627 628 629 630 631 632 633 634 635 636 637
  while (tn == true) {
    count++;
    IterateValue* cv = iter->getValue(iter);
    termCount += (int)taosArrayGetSize(cv->val);
    printf("col val: %s, size: %d\n", cv->colVal, (int)taosArrayGetSize(cv->val));
    tn = iter->next(iter);
  }
  printf("total size: %d\n term count: %d\n", count, termCount);

  tfileIteratorDestroy(iter);
}

dengyihao's avatar
dengyihao 已提交
638
int main(int argc, char* argv[]) {
dengyihao's avatar
dengyihao 已提交
639
  // tool to check all kind of fst test
dengyihao's avatar
dengyihao 已提交
640
  // if (argc > 1) { validateTFile(argv[1]); }
dengyihao's avatar
dengyihao 已提交
641 642 643 644
  // if (argc > 4) {
  // path suid colName ver
  // iterTFileReader(argv[1], argv[2], argv[3], argv[4]);
  //}
dengyihao's avatar
dengyihao 已提交
645
  checkFstCheckIterator1();
646 647 648 649 650
  // checkFstCheckIterator2();
  // checkFstCheckIteratorPrefix();
  // checkFstCheckIteratorRange1();
  // checkFstCheckIteratorRange2();
  // checkFstCheckIteratorRange3();
dengyihao's avatar
dengyihao 已提交
651
  // checkFstLongTerm();
dengyihao's avatar
dengyihao 已提交
652
  // checkFstPrefixSearch();
dengyihao's avatar
dengyihao 已提交
653

dengyihao's avatar
dengyihao 已提交
654 655
  // checkMillonWriteAndReadOfFst();

dengyihao's avatar
dengyihao 已提交
656 657
  return 1;
}