fstUT.cc 6.1 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8

#include <gtest/gtest.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include "index.h"
dengyihao's avatar
dengyihao 已提交
9 10 11
#include "indexCache.h"
#include "indexFst.h"
#include "indexFstUtil.h"
dengyihao's avatar
dengyihao 已提交
12
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
13
#include "indexTfile.h"
dengyihao's avatar
dengyihao 已提交
14
#include "tglobal.h"
dengyihao's avatar
dengyihao 已提交
15
#include "tlog.h"
dengyihao's avatar
dengyihao 已提交
16 17 18
#include "tskiplist.h"
#include "tutil.h"

19
static std::string dir = TD_TMP_DIR_PATH "index";
dengyihao's avatar
dengyihao 已提交
20 21 22 23 24 25 26 27 28 29

static char indexlog[PATH_MAX] = {0};
static char tindex[PATH_MAX] = {0};
static char tindexDir[PATH_MAX] = {0};

static void EnvInit() {
  std::string path = dir;
  taosRemoveDir(path.c_str());
  taosMkDir(path.c_str());
  // init log file
S
os env  
Shengliang Guan 已提交
30
  tstrncpy(tsLogDir, path.c_str(), PATH_MAX);
S
Shengliang Guan 已提交
31
  if (taosInitLog("tindex.idx", 1) != 0) {
dengyihao's avatar
dengyihao 已提交
32 33 34 35 36 37 38 39 40 41
    printf("failed to init log");
  }
  // init index file
  memset(tindex, 0, sizeof(tindex));
  snprintf(tindex, PATH_MAX, "%s/tindex.idx", path.c_str());
}
static void EnvCleanup() {}
class FstWriter {
 public:
  FstWriter() {
dengyihao's avatar
dengyihao 已提交
42
    _wc = idxFileCtxCreate(TFILE, tindex, false, 64 * 1024 * 1024);
dengyihao's avatar
dengyihao 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    _b = fstBuilderCreate(_wc, 0);
  }
  bool Put(const std::string& key, uint64_t val) {
    // char buf[128] = {0};
    // int  len = 0;
    // taosMbsToUcs4(key.c_str(), key.size(), buf, 128, &len);
    // FstSlice skey = fstSliceCreate((uint8_t*)buf, len);
    FstSlice skey = fstSliceCreate((uint8_t*)key.c_str(), key.size());
    bool     ok = fstBuilderInsert(_b, skey, val);

    fstSliceDestroy(&skey);
    return ok;
  }
  ~FstWriter() {
    fstBuilderDestroy(_b);

dengyihao's avatar
dengyihao 已提交
59
    idxFileCtxDestroy(_wc, false);
dengyihao's avatar
dengyihao 已提交
60 61 62 63
  }

 private:
  FstBuilder* _b;
dengyihao's avatar
dengyihao 已提交
64
  IFileCtx*   _wc;
dengyihao's avatar
dengyihao 已提交
65 66 67 68 69
};

class FstReadMemory {
 public:
  FstReadMemory(size_t size) {
dengyihao's avatar
dengyihao 已提交
70
    _wc = idxFileCtxCreate(TFILE, tindex, true, 64 * 1024);
dengyihao's avatar
dengyihao 已提交
71
    _w = idxFileCreate(_wc);
dengyihao's avatar
dengyihao 已提交
72 73 74 75
    _size = size;
    memset((void*)&_s, 0, sizeof(_s));
  }
  bool init() {
wafwerar's avatar
wafwerar 已提交
76
    char* buf = (char*)taosMemoryCalloc(1, sizeof(char) * _size);
dengyihao's avatar
dengyihao 已提交
77
    int   nRead = idxFileRead(_w, (uint8_t*)buf, _size);
dengyihao's avatar
dengyihao 已提交
78 79 80 81 82 83
    if (nRead <= 0) {
      return false;
    }
    _size = nRead;
    _s = fstSliceCreate((uint8_t*)buf, _size);
    _fst = fstCreate(&_s);
wafwerar's avatar
wafwerar 已提交
84
    taosMemoryFreeClear(buf);
dengyihao's avatar
dengyihao 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97
    return _fst != NULL;
  }
  bool Get(const std::string& key, uint64_t* val) {
    // char buf[128] = {0};
    // int  len = 0;
    // taosMbsToUcs4(key.c_str(), key.size(), buf, 128, &len);
    // FstSlice skey = fstSliceCreate((uint8_t*)buf, len);

    FstSlice skey = fstSliceCreate((uint8_t*)key.c_str(), key.size());
    bool     ok = fstGet(_fst, &skey, val);
    fstSliceDestroy(&skey);
    return ok;
  }
dengyihao's avatar
dengyihao 已提交
98

dengyihao's avatar
dengyihao 已提交
99 100 101 102 103 104 105 106
  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 已提交
107 108 109 110 111
  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 已提交
112 113 114 115 116 117 118 119 120 121 122
      // 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));
      swsResultDestroy(rt);
    }
    std::cout << std::endl;
    return true;
  }
dengyihao's avatar
dengyihao 已提交
123
  bool SearchWithTimeCostUs(FAutoCtx* ctx, std::vector<uint64_t>& result) {
dengyihao's avatar
dengyihao 已提交
124 125 126 127 128 129 130
    int64_t s = taosGetTimestampUs();
    bool    ok = this->Search(ctx, result);
    int64_t e = taosGetTimestampUs();
    return ok;
  }

  ~FstReadMemory() {
dengyihao's avatar
dengyihao 已提交
131
    idxFileDestroy(_w);
dengyihao's avatar
dengyihao 已提交
132 133
    fstDestroy(_fst);
    fstSliceDestroy(&_s);
dengyihao's avatar
dengyihao 已提交
134
    idxFileCtxDestroy(_wc, false);
dengyihao's avatar
dengyihao 已提交
135 136 137
  }

 private:
dengyihao's avatar
dengyihao 已提交
138 139 140 141 142
  IdxFstFile* _w;
  Fst*        _fst;
  FstSlice    _s;
  IFileCtx*   _wc;
  size_t      _size;
dengyihao's avatar
dengyihao 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
};

class FstWriterEnv : public ::testing::Test {
 protected:
  virtual void SetUp() { fw = new FstWriter(); }
  virtual void TearDown() { delete fw; }
  FstWriter*   fw = NULL;
};

class FstReadEnv : public ::testing::Test {
 protected:
  virtual void   SetUp() { fr = new FstReadMemory(1024); }
  virtual void   TearDown() { delete fr; }
  FstReadMemory* fr = NULL;
};

class TFst {
 public:
  void CreateWriter() { fw = new FstWriter; }
  void ReCreateWriter() {
    if (fw != NULL) delete fw;
    fw = new FstWriter;
  }
  void DestroyWriter() {
    if (fw != NULL) delete fw;
  }
  void CreateReader() {
    fr = new FstReadMemory(1024);
    fr->init();
  }
  void ReCreateReader() {
    if (fr != NULL) delete fr;
    fr = new FstReadMemory(1024);
  }
  void DestroyReader() {
    delete fr;
    fr = NULL;
  }
  bool Put(const std::string& k, uint64_t v) {
    if (fw == NULL) {
      return false;
    }
    return fw->Put(k, v);
  }
  bool Get(const std::string& k, uint64_t* v) {
    if (fr == NULL) {
      return false;
    }
    return fr->Get(k, v);
  }
dengyihao's avatar
dengyihao 已提交
193
  bool Search(FAutoCtx* ctx, std::vector<uint64_t>& result) {
dengyihao's avatar
dengyihao 已提交
194 195 196
    // add more
    return fr->Search(ctx, result);
  }
dengyihao's avatar
dengyihao 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

 private:
  FstWriter*     fw;
  FstReadMemory* fr;
};
class FstEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
    EnvInit();
    fst = new TFst;
  }
  virtual void TearDown() { delete fst; }
  TFst*        fst;
};

TEST_F(FstEnv, writeNormal) {
  fst->CreateWriter();
dengyihao's avatar
dengyihao 已提交
214
  std::string str("11");
dengyihao's avatar
dengyihao 已提交
215
  for (int i = 0; i < 10; i++) {
dengyihao's avatar
dengyihao 已提交
216
    str[0] = '1' + i;
dengyihao's avatar
dengyihao 已提交
217 218 219 220
    str.resize(2);
    assert(fst->Put(str, i) == true);
  }
  // order failed
dengyihao's avatar
dengyihao 已提交
221
  assert(fst->Put("11", 1) == false);
dengyihao's avatar
dengyihao 已提交
222 223 224 225 226

  fst->DestroyWriter();

  fst->CreateReader();
  uint64_t val;
dengyihao's avatar
dengyihao 已提交
227 228
  assert(fst->Get("1", &val) == false);
  assert(fst->Get("11", &val) == true);
dengyihao's avatar
dengyihao 已提交
229
  assert(val == 0);
dengyihao's avatar
dengyihao 已提交
230 231

  std::vector<uint64_t> rlt;
dengyihao's avatar
dengyihao 已提交
232
  FAutoCtx*             ctx = automCtxCreate((void*)"ab", AUTOMATION_ALWAYS);
dengyihao's avatar
dengyihao 已提交
233
  assert(fst->Search(ctx, rlt) == true);
dengyihao's avatar
dengyihao 已提交
234
}
dengyihao's avatar
dengyihao 已提交
235
TEST_F(FstEnv, WriteMillonrRecord) {}
dengyihao's avatar
add UT  
dengyihao 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
TEST_F(FstEnv, writeAbNormal) {
  fst->CreateWriter();
  std::string str1("voltage&\b&ab");
  std::string str2("voltbge&\b&ab");

  fst->Put(str1, 1);
  fst->Put(str2, 2);

  fst->DestroyWriter();

  fst->CreateReader();
  uint64_t val;
  assert(fst->Get("1", &val) == false);
  assert(fst->Get("voltage&\b&ab", &val) == true);
  assert(val == 1);
}