jsonUT.cc 20.0 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 19
#include "tglobal.h"
#include "tskiplist.h"
#include "tutil.h"

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

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 69 70 71 72 73 74 75 76 77 78 79 80 81
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);
}
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 已提交
82 83
TEST_F(JsonEnv, testWrite) {
  {
dengyihao's avatar
add UT  
dengyihao 已提交
84
    std::string colName("test");
dengyihao's avatar
dengyihao 已提交
85
    std::string colVal("ab");
dengyihao's avatar
dengyihao 已提交
86
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
87 88 89 90 91 92 93 94 95 96 97 98
                                       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 已提交
99
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
100 101 102 103 104 105 106 107 108 109 110 111
                                       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 已提交
112
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
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);
  }
  {
dengyihao's avatar
add UT  
dengyihao 已提交
123
    std::string colName("test");
dengyihao's avatar
dengyihao 已提交
124 125 126
    std::string colVal("ab");

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
127
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
128 129 130 131 132 133 134 135
                                    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 已提交
136 137
}
TEST_F(JsonEnv, testWriteMillonData) {
dengyihao's avatar
add UT  
dengyihao 已提交
138 139 140
  {
    std::string colName("test");
    std::string colVal("ab");
dengyihao's avatar
dengyihao 已提交
141
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
add UT  
dengyihao 已提交
142 143 144 145 146 147 148 149 150
                                       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 已提交
151 152
  {
    std::string colName("voltagefdadfa");
dengyihao's avatar
dengyihao 已提交
153
    std::string colVal("abxxxxxxxxxxxx");
wafwerar's avatar
wafwerar 已提交
154
    for (int i = 0; i < 1000; i++) {
dengyihao's avatar
dengyihao 已提交
155
      colVal[i % colVal.size()] = '0' + i % 128;
dengyihao's avatar
dengyihao 已提交
156
      SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
157 158 159 160 161 162 163 164 165 166 167 168
                                         colVal.c_str(), colVal.size());

      SIndexMultiTerm* terms = indexMultiTermCreate();
      indexMultiTermAdd(terms, term);
      for (size_t i = 0; i < 1000; i++) {
        tIndexJsonPut(index, terms, i);
      }
      indexMultiTermDestroy(terms);
    }
  }
  {
    std::string colName("voltagefdadfa");
dengyihao's avatar
add UT  
dengyihao 已提交
169
    std::string colVal("abxxxxxxxxxxxx");
dengyihao's avatar
dengyihao 已提交
170
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
add UT  
dengyihao 已提交
171
                                       colVal.c_str(), colVal.size());
dengyihao's avatar
dengyihao 已提交
172

dengyihao's avatar
add UT  
dengyihao 已提交
173 174
    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
175
    for (size_t i = 0; i < 1000; i++) {
dengyihao's avatar
add UT  
dengyihao 已提交
176 177 178 179 180
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
dengyihao's avatar
add UT  
dengyihao 已提交
181
    std::string colName("test");
dengyihao's avatar
add UT  
dengyihao 已提交
182 183 184
    std::string colVal("ab");

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
185
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
add UT  
dengyihao 已提交
186 187 188 189 190 191 192 193
                                    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);
  }
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  {
    {
      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);
        assert(100 == taosArrayGetSize(result));
        indexMultiTermQueryDestroy(mq);
      }
    }
  }
dengyihao's avatar
dengyihao 已提交
226
}
227 228 229
TEST_F(JsonEnv, testWriteJsonNumberData) {
  {
    std::string colName("test");
dengyihao's avatar
dengyihao 已提交
230 231
    // std::string colVal("10");
    int         val = 10;
232
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
233
                                       (const char*)&val, sizeof(val));
234 235 236 237 238 239 240 241 242 243

    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 已提交
244
    int         val = 20;
245
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
246
                                       (const char*)&val, sizeof(val));
247 248 249 250 251 252 253 254 255

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

    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 已提交
270
    const char* val = "test";
271
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_BINARY, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
272
                                       (const char*)val, strlen(val));
273 274 275 276 277 278 279 280 281

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 1000; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
dengyihao's avatar
dengyihao 已提交
282 283
    std::string           colName("test");
    int                   val = 15;
284
    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
285 286
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
287 288 289 290 291 292 293 294 295

    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 已提交
296
    int         val = 15;
297 298

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
299 300
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
301 302 303 304 305 306 307 308 309

    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 已提交
310 311
    int         val = 10;
    ;
312 313

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

    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 已提交
325 326
    int         val = 10;
    // std::string colVal("10");
327 328

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

    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 已提交
340 341
    int         val = 10;
    // std::string colVal("10");
342 343

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

    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 已提交
355
TEST_F(JsonEnv, testWriteJsonTfileAndCache_INT) {
356 357
  {
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
358
    int         val = 10;
359
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
360
                                       (const char*)&val, sizeof(val));
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

    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);
    for (size_t i = 0; i < 100000; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
  {
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
384
    int         val = 10;
385 386

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
387 388
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
389 390 391 392 393 394 395 396 397

    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 已提交
398
    int         val = 10;
399 400

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

    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 已提交
412 413
    // std::string colVal("10");
    int val = 10;
414 415

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

    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 已提交
427
    int         val = 10;
428 429

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

    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 已提交
441
    int         val = 10;
442 443

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

    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 已提交
453 454
  {
    std::string colName("other_column");
dengyihao's avatar
dengyihao 已提交
455
    int         val = 100;
dengyihao's avatar
dengyihao 已提交
456

dengyihao's avatar
dengyihao 已提交
457
    SIndexTerm* term = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
dengyihao's avatar
dengyihao 已提交
458
                                       (const char*)&val, sizeof(val));
dengyihao's avatar
dengyihao 已提交
459 460 461 462 463 464 465 466

    SIndexMultiTerm* terms = indexMultiTermCreate();
    indexMultiTermAdd(terms, term);
    for (size_t i = 0; i < 1000; i++) {
      tIndexJsonPut(index, terms, i);
    }
    indexMultiTermDestroy(terms);
  }
467 468
  {
    std::string colName("test1");
dengyihao's avatar
dengyihao 已提交
469 470
    int         val = 10;
    // std::string colVal("10");
471 472

    SIndexMultiTermQuery* mq = indexMultiTermQueryCreate(MUST);
dengyihao's avatar
dengyihao 已提交
473 474
    SIndexTerm*           q = indexTermCreate(1, ADD_VALUE, TSDB_DATA_TYPE_INT, colName.c_str(), colName.size(),
                                    (const char*)&val, sizeof(val));
475 476 477 478 479 480 481

    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 已提交
482 483 484 485 486 487 488 489 490
  {
    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++) {
491
      tIndexJsonPut(index, terms, i + 1000);
dengyihao's avatar
dengyihao 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    }
    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);
  }
510
}
dengyihao's avatar
dengyihao 已提交
511 512 513 514 515 516 517 518 519 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 546 547 548 549 550 551 552 553 554 555
TEST_F(JsonEnv, testWriteJsonTfileAndCache_INT2) {
  {
    int         val = 10;
    std::string colName("test1");
    for (int i = 0; i < 10000; i++) {
      val += 1;
      WriteData(index, colName, TSDB_DATA_TYPE_INT, &val, sizeof(val), i);
    }
  }
  {
    int         val = 10;
    std::string colName("test2xxx");
    std::string colVal("xxxxxxxxxxxxxxx");
    for (int i = 0; i < 100000; i++) {
      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);
    EXPECT_EQ(10000, taosArrayGetSize(res));
  }
  {
    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);
    EXPECT_EQ(100000, taosArrayGetSize(res));
  }
}
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++) {
556
      WriteData(index, colName, TSDB_DATA_TYPE_FLOAT, &val, sizeof(val), i + 1000);
dengyihao's avatar
dengyihao 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    }
  }
  {
    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));
  }
}