indexTests.cpp 1.6 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4
#include <gtest/gtest.h>
#include <string>
#include <iostream>
#include "index.h"
dengyihao's avatar
dengyihao 已提交
5 6
#include "indexInt.h"

dengyihao's avatar
dengyihao 已提交
7 8 9 10 11



TEST(IndexTest, index_create_test) {
  SIndexOpts *opts = indexOptsCreate();
dengyihao's avatar
dengyihao 已提交
12
  SIndex *index = indexOpen(opts, "./test");
dengyihao's avatar
dengyihao 已提交
13 14 15
  if (index == NULL) {
    std::cout << "index open failed" << std::endl; 
  }
dengyihao's avatar
dengyihao 已提交
16 17

  
dengyihao's avatar
dengyihao 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  // 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());

dengyihao's avatar
dengyihao 已提交
34
     
dengyihao's avatar
dengyihao 已提交
35 36
    indexPut(index, terms, i);
    indexMultiTermDestroy(terms);
dengyihao's avatar
dengyihao 已提交
37 38 39 40 41
  } 
 

  // query
  SIndexMultiTermQuery *multiQuery = indexMultiTermQueryCreate(MUST); 
dengyihao's avatar
dengyihao 已提交
42
  
dengyihao's avatar
dengyihao 已提交
43
  indexMultiTermQueryAdd(multiQuery, "tag1", strlen("tag1"), "field", strlen("field"), QUERY_PREFIX);
dengyihao's avatar
dengyihao 已提交
44
  indexMultiTermQueryAdd(multiQuery, "tag3", strlen("tag3"), "0field0", strlen("0field0"), QUERY_TERM);
dengyihao's avatar
dengyihao 已提交
45 46 47 48 49 50 51 52 53 54 55

  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;
  }
  indexMultiTermQueryDestroy(multiQuery);

dengyihao's avatar
dengyihao 已提交
56
  indexOptsDestroy(opts); 
dengyihao's avatar
dengyihao 已提交
57 58
  indexClose(index); 
  //
dengyihao's avatar
dengyihao 已提交
59
}