indexTests.cc 9.8 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * 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 16 17 18
#include <gtest/gtest.h>
#include <string>
#include <iostream>
#include "index.h"
dengyihao's avatar
dengyihao 已提交
19
#include "tutil.h"
dengyihao's avatar
dengyihao 已提交
20
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
21 22 23
#include "index_fst.h"
#include "index_fst_util.h"
#include "index_fst_counting_writer.h"
dengyihao's avatar
dengyihao 已提交
24

dengyihao's avatar
dengyihao 已提交
25

dengyihao's avatar
dengyihao 已提交
26 27 28
class FstWriter {
  public:
    FstWriter() {
dengyihao's avatar
dengyihao 已提交
29
      _wc = writerCtxCreate(TFile, "/tmp/tindex", false, 0); 
dengyihao's avatar
dengyihao 已提交
30 31 32 33 34 35 36 37 38 39 40
      _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 已提交
41 42

     writerCtxDestroy(_wc);
dengyihao's avatar
dengyihao 已提交
43 44 45
   }
  private:
    FstBuilder *_b; 
dengyihao's avatar
dengyihao 已提交
46
    WriterCtx *_wc;
dengyihao's avatar
dengyihao 已提交
47 48 49 50 51
};

class FstReadMemory {
  public:
   FstReadMemory(size_t size) {
dengyihao's avatar
dengyihao 已提交
52 53
     _wc   = writerCtxCreate(TFile, "/tmp/tindex", true, 0);   
     _w    = fstCountingWriterCreate(_wc); 
dengyihao's avatar
dengyihao 已提交
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
     _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
dengyihao's avatar
dengyihao 已提交
81 82
   bool Search(AutomationCtx *ctx, std::vector<uint64_t> &result) { 
      FstStreamBuilder *sb = fstSearch(_fst, ctx);
dengyihao's avatar
dengyihao 已提交
83
      StreamWithState  *st = streamBuilderIntoStream(sb);  
dengyihao's avatar
dengyihao 已提交
84 85 86 87 88
      StreamWithStateResult *rt = NULL;  
      
      while ((rt = streamWithStateNextWith(st, NULL)) != NULL) {
        result.push_back((uint64_t)(rt->out.out));
      }
dengyihao's avatar
dengyihao 已提交
89 90
      return true;
   }
dengyihao's avatar
dengyihao 已提交
91 92 93 94 95 96
   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;
   }
dengyihao's avatar
dengyihao 已提交
97 98 99
    
   ~FstReadMemory() {
    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
  } 
  
  private:
   FstCountingWriter *_w; 
   Fst *_fst;
   FstSlice _s;  
dengyihao's avatar
dengyihao 已提交
109
   WriterCtx *_wc;
dengyihao's avatar
dengyihao 已提交
110 111 112
   size_t _size;
   
}; 
dengyihao's avatar
dengyihao 已提交
113

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

dengyihao's avatar
dengyihao 已提交
166

dengyihao's avatar
dengyihao 已提交
167 168 169
#define L 100
#define M 100
#define N 100
dengyihao's avatar
dengyihao 已提交
170

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

void Performance_fstReadRecords(FstReadMemory *m) {
dengyihao's avatar
dengyihao 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  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());
        }
      }
    } 
  }
dengyihao's avatar
dengyihao 已提交
208
}
dengyihao's avatar
dengyihao 已提交
209 210 211
void checkFstPerf() {
  FstWriter *fw = new FstWriter;
  int64_t s = taosGetTimestampUs();
dengyihao's avatar
dengyihao 已提交
212

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

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

dengyihao's avatar
dengyihao 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239
void checkFstPrefixSearch() {
  FstWriter *fw = new FstWriter;
  int64_t s = taosGetTimestampUs();
  int count = 2;
  std::string key("ab");
  
  for (int i = 0; i < count; i++) {
    key[1] = key[1] + i;   
    fw->Put(key, i); 
  }
  int64_t e = taosGetTimestampUs();
  
  std::cout << "insert data count :  " << count << "elapas time: " << e - s << std::endl;
  delete fw;
dengyihao's avatar
dengyihao 已提交
240

dengyihao's avatar
dengyihao 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  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;
  AutomationCtx *ctx = automCtxCreate((void *)"ab", AUTOMATION_PREFIX); 
  m->Search(ctx, result);   
  assert(result.size() == count); 
  for (int i = 0; i < result.size(); i++) {
    assert(result[i] == i); // check result
  }

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

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

  {
dengyihao's avatar
dengyihao 已提交
283 284 285 286 287 288 289
   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());
   }
dengyihao's avatar
dengyihao 已提交
290
   for (int i = 0; i < count; i++) {
dengyihao's avatar
dengyihao 已提交
291
     key.push_back('a' + i);
dengyihao's avatar
dengyihao 已提交
292
     if (m->Get(key, &out) ) {
dengyihao's avatar
dengyihao 已提交
293
       assert(val - i ==  out);
dengyihao's avatar
dengyihao 已提交
294 295 296
       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 已提交
297
    }
dengyihao's avatar
dengyihao 已提交
298 299
   }
  } 
dengyihao's avatar
dengyihao 已提交
300 301
  delete m;
} 
dengyihao's avatar
dengyihao 已提交
302

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

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 已提交
362

dengyihao's avatar
dengyihao 已提交
363 364
TEST_F(IndexEnv, testDel) {
     
dengyihao's avatar
dengyihao 已提交
365
}
dengyihao's avatar
dengyihao 已提交
366

dengyihao's avatar
dengyihao 已提交
367 368


dengyihao's avatar
dengyihao 已提交
369 370