jsonUT.cc 21.8 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7
#include <gtest/gtest.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include "index.h"
dengyihao's avatar
dengyihao 已提交
8 9 10 11
#include "indexCache.h"
#include "indexFst.h"
#include "indexFstCountingWriter.h"
#include "indexFstUtil.h"
dengyihao's avatar
dengyihao 已提交
12
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
13 14
#include "indexTfile.h"
#include "indexUtil.h"
dengyihao's avatar
dengyihao 已提交
15 16 17 18
#include "tglobal.h"
#include "tskiplist.h"
#include "tutil.h"

19 20
static std::string dir = TD_TMP_DIR_PATH "json";
static std::string logDir = TD_TMP_DIR_PATH "log";
dengyihao's avatar
dengyihao 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

static void initLog() {
  const char*   defaultLogFileNamePrefix = "taoslog";
  const int32_t maxLogFileNum = 10;

  tsAsyncLog = 0;
  sDebugFlag = 143;
  strcpy(tsLogDir, logDir.c_str());
  taosRemoveDir(tsLogDir);
  taosMkDir(tsLogDir);

  if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) {
    printf("failed to open log file in directory:%s\n", tsLogDir);
  }
}
dengyihao's avatar
dengyihao 已提交
36 37 38
class JsonEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
dengyihao's avatar
dengyihao 已提交
39 40
    taosRemoveDir(logDir.c_str());
    taosMkDir(logDir.c_str());
dengyihao's avatar
dengyihao 已提交
41
    taosRemoveDir(dir.c_str());
dengyihao's avatar
add UT  
dengyihao 已提交
42
    taosMkDir(dir.c_str());
dengyihao's avatar
add UT  
dengyihao 已提交
43
    printf("set up\n");
dengyihao's avatar
dengyihao 已提交
44 45

    initLog();
dengyihao's avatar
dengyihao 已提交
46 47 48 49 50 51 52
    opts = indexOptsCreate();
    int ret = tIndexJsonOpen(opts, dir.c_str(), &index);
    assert(ret == 0);
  }
  virtual void TearDown() {
    tIndexJsonClose(index);
    indexOptsDestroy(opts);
dengyihao's avatar
add UT  
dengyihao 已提交
53
    printf("destory\n");
dengyihao's avatar
dengyihao 已提交
54 55 56 57 58
  }
  SIndexJsonOpts* opts;
  SIndexJson*     index;
};

dengyihao's avatar
dengyihao 已提交
59 60 61 62 63 64 65 66 67 68
static void WriteData(SIndexJson* index, const std::string& colName, int8_t dtype, void* data, int dlen, int tableId,
                      int8_t operType = ADD_VALUE) {
  SIndexTerm* term =
      indexTermCreate(1, (SIndexOperOnColumn)operType, dtype, colName.c_str(), colName.size(), (const char*)data, dlen);
  SIndexMultiTerm* terms = indexMultiTermCreate();
  indexMultiTermAdd(terms, term);
  tIndexJsonPut(index, terms, (int64_t)tableId);

  indexMultiTermDestroy(terms);
}
dengyihao's avatar
dengyihao 已提交
69 70 71 72 73 74 75 76 77 78 79

static void delData(SIndexJson* index, const std::string& colName, int8_t dtype, void* data, int dlen, int tableId,
                    int8_t operType = DEL_VALUE) {
  SIndexTerm* term =
      indexTermCreate(1, (SIndexOperOnColumn)operType, dtype, colName.c_str(), colName.size(), (const char*)data, dlen);
  SIndexMultiTerm* terms = indexMultiTermCreate();
  indexMultiTermAdd(terms, term);
  tIndexJsonPut(index, terms, (int64_t)tableId);

  indexMultiTermDestroy(terms);
}
dengyihao's avatar
dengyihao 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92
static void Search(SIndexJson* index, const std::string& colNam, int8_t dtype, void* data, int dlen, int8_t filterType,
                   SArray** result) {
  std::string colName(colNam);

  SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
  SIndexTerm* q = indexTermCreate(1, ADD_VALUE, dtype, colName.c_str(), colName.size(), (const char*)data, dlen);

  SArray* res = taosArrayInit(1, sizeof(uint64_t));
  indexMultiTermQueryAdd(mq, q, (EIndexQueryType)filterType);
  tIndexJsonSearch(index, mq, res);
  indexMultiTermQueryDestroy(mq);
  *result = res;
}
dengyihao's avatar
dengyihao 已提交
93 94
TEST_F(JsonEnv, testWrite) {
  {
dengyihao's avatar
add UT  
dengyihao 已提交
95
    std::string colName("test");
dengyihao's avatar
dengyihao 已提交
96
    std::string colVal("ab");
dengyihao's avatar
dengyihao 已提交
97
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
98 99 100 101 102 103 104 105 106 107 108 109
                                       colVal.c_str(), colVal.size());

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 100; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
    std::string colName("voltage");
    std::string colVal("ab1");
dengyihao's avatar
dengyihao 已提交
110
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
111 112 113 114 115 116 117 118 119 120 121 122
                                       colVal.c_str(), colVal.size());

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 100; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
    std::string colName("voltage");
    std::string colVal("123");
dengyihao's avatar
dengyihao 已提交
123
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
124 125 126 127 128 129 130 131 132 133
                                       colVal.c_str(), colVal.size());

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 100; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
dengyihao's avatar
add UT  
dengyihao 已提交
134
    std::string colName("test");
dengyihao's avatar
dengyihao 已提交
135 136 137
    std::string colVal("ab");

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
138
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
139 140 141 142 143 144 145 146
                                    colVal.c_str(), colVal.size());

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_TERM);
    tIndexJsonSearch(index, mq, result);
    assert(100 == taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
dengyihao's avatar
add UT  
dengyihao 已提交
147 148
}
TEST_F(JsonEnv, testWriteMillonData) {
dengyihao's avatar
add UT  
dengyihao 已提交
149 150 151
  {
    std::string colName("test");
    std::string colVal("ab");
dengyihao's avatar
dengyihao 已提交
152
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
add UT  
dengyihao 已提交
153 154 155 156
                                       colVal.c_str(), colVal.size());

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
dengyihao's avatar
dengyihao 已提交
157
    for (size_t i = 0; i < 10; i++) {
dengyihao's avatar
add UT  
dengyihao 已提交
158 159 160 161
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
dengyihao's avatar
add UT  
dengyihao 已提交
162 163
  {
    std::string colName("voltagefdadfa");
dengyihao's avatar
dengyihao 已提交
164
    std::string colVal("abxxxxxxxxxxxx");
dengyihao's avatar
dengyihao 已提交
165
    for (int i = 0; i < 10; i++) {
dengyihao's avatar
dengyihao 已提交
166
      colVal[i % colVal.size()] = '0' + i % 128;
dengyihao's avatar
dengyihao 已提交
167
      SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
168 169 170 171
                                         colVal.c_str(), colVal.size());

      SIndexMultiTerm* terms = indexMultiTermCreate();
      indexMultiTermAdd(terms, term);
dengyihao's avatar
dengyihao 已提交
172
      for (size_t i = 0; i < 100; i++) {
dengyihao's avatar
dengyihao 已提交
173 174 175 176 177 178 179
        tIndexJsonPut(index, terms, i);
      }
      indexMultiTermDestroy(terms);
    }
  }
  {
    std::string colName("voltagefdadfa");
dengyihao's avatar
add UT  
dengyihao 已提交
180
    std::string colVal("abxxxxxxxxxxxx");
dengyihao's avatar
dengyihao 已提交
181
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
add UT  
dengyihao 已提交
182
                                       colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
183

dengyihao's avatar
add UT  
dengyihao 已提交
184 185
    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
186
    for (size_t i = 0; i < 1000; i++) {
dengyihao's avatar
add UT  
dengyihao 已提交
187 188 189 190 191
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
dengyihao's avatar
add UT  
dengyihao 已提交
192
    std::string colName("test");
dengyihao's avatar
add UT  
dengyihao 已提交
193 194 195
    std::string colVal("ab");

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
196
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
add UT  
dengyihao 已提交
197 198 199 200 201
                                    colVal.c_str(), colVal.size());

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_TERM);
    tIndexJsonSearch(index, mq, result);
dengyihao's avatar
dengyihao 已提交
202
    EXPECT_EQ(10, taosArrayGetSize(result));
dengyihao's avatar
add UT  
dengyihao 已提交
203 204
    indexMultiTermQueryDestroy(mq);
  }
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  {
    {
      std::string colName("test");
      std::string colVal("ab");

      SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
      SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                      colVal.c_str(), colVal.size());

      SArray* result = taosArrayInit(1, sizeof(uint64_t));
      indexMultiTermQueryAdd(mq, q, QUERY_GREATER_THAN);
      tIndexJsonSearch(index, mq, result);
      assert(0 == taosArrayGetSize(result));
      indexMultiTermQueryDestroy(mq);
    }
    {
      {
        std::string colName("test");
        std::string colVal("ab");

        SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
        SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                        colVal.c_str(), colVal.size());

        SArray* result = taosArrayInit(1, sizeof(uint64_t));
        indexMultiTermQueryAdd(mq, q, QUERY_GREATER_EQUAL);
        tIndexJsonSearch(index, mq, result);
dengyihao's avatar
dengyihao 已提交
232
        EXPECT_EQ(10, taosArrayGetSize(result));
233 234 235 236
        indexMultiTermQueryDestroy(mq);
      }
    }
  }
dengyihao's avatar
dengyihao 已提交
237
}
238 239 240
TEST_F(JsonEnv, testWriteJsonNumberData) {
  {
    std::string colName("test");
dengyihao's avatar
dengyihao 已提交
241 242
    // std::string colVal("10");
    int         val = 10;
243
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
244
                                       (const char*)&val, sizeof(val));
245 246 247 248 249 250 251 252 253 254

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 1000; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
    std::string colName("test2");
dengyihao's avatar
dengyihao 已提交
255
    int         val = 20;
256
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
257
                                       (const char*)&val, sizeof(val));
258 259 260 261 262 263 264 265 266

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 1000; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
dengyihao's avatar
dengyihao 已提交
267 268
    std::string colName("test");
    int         val = 15;
269
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
270
                                       (const char*)&val, sizeof(val));
271 272 273 274 275 276 277 278 279 280

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 1000; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
    std::string colName("test2");
dengyihao's avatar
dengyihao 已提交
281
    const char* val = "test";
282
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
283
                                       (const char*)val, strlen(val));
284 285 286 287 288 289 290 291 292

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 1000; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
dengyihao's avatar
dengyihao 已提交
293 294
    std::string           colName("test");
    int                   val = 15;
295
    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
296 297
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
298 299 300 301 302 303 304 305 306

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_TERM);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(1000, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
  {
    std::string colName("test");
dengyihao's avatar
dengyihao 已提交
307
    int         val = 15;
308 309

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
310 311
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
312 313 314 315 316 317 318 319 320

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_GREATER_THAN);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(0, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
  {
    std::string colName("test");
dengyihao's avatar
dengyihao 已提交
321 322
    int         val = 10;
    ;
323 324

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
325 326
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(int));
327 328 329 330 331 332 333 334 335

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_GREATER_EQUAL);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(1000, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
  {
    std::string colName("test");
dengyihao's avatar
dengyihao 已提交
336 337
    int         val = 10;
    // std::string colVal("10");
338 339

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
340 341
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
342 343 344 345 346 347 348 349 350

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_LESS_THAN);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(0, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
  {
    std::string colName("test");
dengyihao's avatar
dengyihao 已提交
351 352
    int         val = 10;
    // std::string colVal("10");
353 354

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
355 356
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
357 358 359 360 361 362 363 364 365

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_LESS_EQUAL);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(1000, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
}

dengyihao's avatar
dengyihao 已提交
366
TEST_F(JsonEnv, testWriteJsonTfileAndCache_INT) {
367 368
  {
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
369
    int         val = 10;
370
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
371
                                       (const char*)&val, sizeof(val));
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 1000; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
    std::string colName("test");
    std::string colVal("xxxxxxxxxxxxxxxxxxx");
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
                                       colVal.c_str(), colVal.size());

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
dengyihao's avatar
dengyihao 已提交
388
    for (size_t i = 0; i < 1000; i++) {
389 390 391 392 393 394
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
395
    int         val = 10;
396 397

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
398 399
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
400 401 402 403 404 405 406 407 408

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_TERM);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(1000, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
  {
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
409
    int         val = 10;
410 411

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
412 413
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(int));
414 415 416 417 418 419 420 421 422

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_GREATER_THAN);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(0, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
  {
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
423 424
    // std::string colVal("10");
    int val = 10;
425 426

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
427 428
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
429 430 431 432 433 434 435 436 437

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_GREATER_EQUAL);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(1000, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
  {
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
438
    int         val = 10;
439 440

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
441 442
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
443 444 445 446 447 448 449 450 451

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_GREATER_THAN);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(0, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
  {
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
452
    int         val = 10;
453 454

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
455 456
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
457 458 459 460 461 462 463

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_LESS_EQUAL);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(1000, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
dengyihao's avatar
dengyihao 已提交
464 465
  {
    std::string colName("other_column");
dengyihao's avatar
dengyihao 已提交
466
    int         val = 100;
dengyihao's avatar
dengyihao 已提交
467

dengyihao's avatar
dengyihao 已提交
468
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
469
                                       (const char*)&val, sizeof(val));
dengyihao's avatar
dengyihao 已提交
470 471 472 473 474 475 476 477

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 1000; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
478 479
  {
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
480 481
    int         val = 10;
    // std::string colVal("10");
482 483

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
484 485
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
486 487 488 489 490 491 492

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_LESS_THAN);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(0, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
dengyihao's avatar
dengyihao 已提交
493 494 495 496 497 498 499 500 501
  {
    std::string colName("test1");
    int         val = 15;
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                       (const char*)&val, sizeof(val));

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 1000; i++) {
502
      tIndexJsonPut(index, terms, i + 1000);
dengyihao's avatar
dengyihao 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    }
    indexMultiTermDestroy(terms);
  }
  {
    std::string colName("test1");
    int         val = 8;
    // std::string colVal("10");

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));

    SArray* result = taosArrayInit(1, sizeof(uint64_t));
    indexMultiTermQueryAdd(mq, q, QUERY_GREATER_EQUAL);
    tIndexJsonSearch(index, mq, result);
    EXPECT_EQ(2000, taosArrayGetSize(result));
    indexMultiTermQueryDestroy(mq);
  }
521
}
dengyihao's avatar
dengyihao 已提交
522 523 524 525
TEST_F(JsonEnv, testWriteJsonTfileAndCache_INT2) {
  {
    int         val = 10;
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
526
    for (int i = 0; i < 1000; i++) {
dengyihao's avatar
dengyihao 已提交
527 528 529 530 531 532 533 534
      val += 1;
      WriteData(index, colName, TSDB_DATA_TYPE_INT, &val, sizeof(val), i);
    }
  }
  {
    int         val = 10;
    std::string colName("test2xxx");
    std::string colVal("xxxxxxxxxxxxxxx");
dengyihao's avatar
dengyihao 已提交
535
    for (int i = 0; i < 1000; i++) {
dengyihao's avatar
dengyihao 已提交
536 537 538 539 540 541 542 543 544
      val += 1;
      WriteData(index, colName, TSDB_DATA_TYPE_BINARY, (void*)(colVal.c_str()), colVal.size(), i);
    }
  }
  {
    SArray*     res = NULL;
    std::string colName("test1");
    int         val = 9;
    Search(index, colName, TSDB_DATA_TYPE_INT, &val, sizeof(val), QUERY_GREATER_EQUAL, &res);
dengyihao's avatar
dengyihao 已提交
545
    EXPECT_EQ(1000, taosArrayGetSize(res));
dengyihao's avatar
dengyihao 已提交
546 547 548 549 550 551
  }
  {
    SArray*     res = NULL;
    std::string colName("test2xxx");
    std::string colVal("xxxxxxxxxxxxxxx");
    Search(index, colName, TSDB_DATA_TYPE_BINARY, (void*)(colVal.c_str()), colVal.size(), QUERY_TERM, &res);
dengyihao's avatar
dengyihao 已提交
552
    EXPECT_EQ(1000, taosArrayGetSize(res));
dengyihao's avatar
dengyihao 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566
  }
}
TEST_F(JsonEnv, testWriteJsonTfileAndCache_FLOAT) {
  {
    float       val = 10.0;
    std::string colName("test1");
    for (int i = 0; i < 1000; i++) {
      WriteData(index, colName, TSDB_DATA_TYPE_FLOAT, &val, sizeof(val), i);
    }
  }
  {
    float       val = 2.0;
    std::string colName("test1");
    for (int i = 0; i < 1000; i++) {
567
      WriteData(index, colName, TSDB_DATA_TYPE_FLOAT, &val, sizeof(val), i + 1000);
dengyihao's avatar
dengyihao 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    }
  }
  {
    SArray*     res = NULL;
    std::string colName("test1");
    float       val = 1.9;
    Search(index, colName, TSDB_DATA_TYPE_FLOAT, &val, sizeof(val), QUERY_GREATER_EQUAL, &res);
    EXPECT_EQ(2000, taosArrayGetSize(res));
  }
  {
    SArray*     res = NULL;
    std::string colName("test1");
    float       val = 2.1;
    Search(index, colName, TSDB_DATA_TYPE_FLOAT, &val, sizeof(val), QUERY_GREATER_EQUAL, &res);
    EXPECT_EQ(1000, taosArrayGetSize(res));
  }
  {
    std::string colName("test1");
    SArray*     res = NULL;
    float       val = 2.1;
    Search(index, colName, TSDB_DATA_TYPE_FLOAT, &val, sizeof(val), QUERY_GREATER_EQUAL, &res);
    EXPECT_EQ(1000, taosArrayGetSize(res));
  }
}
dengyihao's avatar
dengyihao 已提交
592 593
TEST_F(JsonEnv, testWriteJsonTfileAndCache_DOUBLE) {
  {
dengyihao's avatar
dengyihao 已提交
594
    double val = 10.0;
dengyihao's avatar
dengyihao 已提交
595
    for (int i = 0; i < 1000; i++) {
dengyihao's avatar
dengyihao 已提交
596
      WriteData(index, "test1", TSDB_DATA_TYPE_DOUBLE, &val, sizeof(val), i);
dengyihao's avatar
dengyihao 已提交
597 598 599
    }
  }
  {
dengyihao's avatar
dengyihao 已提交
600
    double val = 2.0;
dengyihao's avatar
dengyihao 已提交
601
    for (int i = 0; i < 1000; i++) {
dengyihao's avatar
dengyihao 已提交
602
      WriteData(index, "test1", TSDB_DATA_TYPE_DOUBLE, &val, sizeof(val), i + 1000);
dengyihao's avatar
dengyihao 已提交
603 604 605 606 607 608
    }
  }
  {
    SArray*     res = NULL;
    std::string colName("test1");
    double      val = 1.9;
dengyihao's avatar
dengyihao 已提交
609
    Search(index, "test1", TSDB_DATA_TYPE_DOUBLE, &val, sizeof(val), QUERY_GREATER_EQUAL, &res);
dengyihao's avatar
dengyihao 已提交
610 611 612
    EXPECT_EQ(2000, taosArrayGetSize(res));
  }
  {
dengyihao's avatar
dengyihao 已提交
613 614 615
    SArray* res = NULL;
    double  val = 2.1;
    Search(index, "test1", TSDB_DATA_TYPE_DOUBLE, &val, sizeof(val), QUERY_GREATER_EQUAL, &res);
dengyihao's avatar
dengyihao 已提交
616 617 618
    EXPECT_EQ(1000, taosArrayGetSize(res));
  }
  {
dengyihao's avatar
dengyihao 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
    SArray* res = NULL;
    double  val = 2.1;
    Search(index, "test1", TSDB_DATA_TYPE_DOUBLE, &val, sizeof(val), QUERY_GREATER_EQUAL, &res);
    EXPECT_EQ(1000, taosArrayGetSize(res));
  }
  {
    SArray* res = NULL;
    double  val = 10.0;
    Search(index, "test1", TSDB_DATA_TYPE_DOUBLE, &val, sizeof(val), QUERY_LESS_EQUAL, &res);
    EXPECT_EQ(2000, taosArrayGetSize(res));
  }
  {
    SArray* res = NULL;
    double  val = 10.0;
    Search(index, "test1", TSDB_DATA_TYPE_DOUBLE, &val, sizeof(val), QUERY_LESS_THAN, &res);
dengyihao's avatar
dengyihao 已提交
634 635 636
    EXPECT_EQ(1000, taosArrayGetSize(res));
  }
}