tsdbSmaTest.cpp 20.2 KB
Newer Older
C
Cary Xu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#include <gtest/gtest.h>
H
Hongze Cheng 已提交
17
#include <vnodeInt.h>
C
Cary Xu 已提交
18

C
Cary Xu 已提交
19 20 21 22
#include <taoserror.h>
#include <tglobal.h>
#include <iostream>

H
Hongze Cheng 已提交
23
#include <vnodeInt.h>
C
Cary Xu 已提交
24 25
#include <tmsg.h>

C
Cary Xu 已提交
26 27 28 29 30 31
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"

C
Cary Xu 已提交
32 33 34 35 36
int main(int argc, char **argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

C
Cary Xu 已提交
37 38 39 40 41 42 43 44 45 46
TEST(testCase, unionEncodeDecodeTest) {
  typedef struct {
    union {
      uint8_t info;
      struct {
        uint8_t rollup : 1;  // 1 means rollup sma
        uint8_t type : 7;
      };
    };
    col_id_t  nBSmaCols;
C
Cary Xu 已提交
47
    col_id_t *pBSmaCols;
C
Cary Xu 已提交
48 49 50 51 52 53 54
  } SUnionTest;

  SUnionTest sut = {0};
  sut.rollup = 1;
  sut.type = 1;

  sut.nBSmaCols = 2;
C
Cary Xu 已提交
55
  sut.pBSmaCols = (col_id_t *)taosMemoryMalloc(sut.nBSmaCols * sizeof(col_id_t));
C
Cary Xu 已提交
56 57 58 59
  for (col_id_t i = 0; i < sut.nBSmaCols; ++i) {
    sut.pBSmaCols[i] = i + 100;
  }

C
Cary Xu 已提交
60 61
  void   *buf = taosMemoryMalloc(1024);
  void   *pBuf = buf;
C
Cary Xu 已提交
62
  void   *qBuf = buf;
C
Cary Xu 已提交
63
  int32_t tlen = 0;
C
Cary Xu 已提交
64 65
  tlen += taosEncodeFixedU8(&pBuf, sut.info);
  tlen += taosEncodeFixedI16(&pBuf, sut.nBSmaCols);
C
Cary Xu 已提交
66
  for (col_id_t i = 0; i < sut.nBSmaCols; ++i) {
C
Cary Xu 已提交
67
    tlen += taosEncodeFixedI16(&pBuf, sut.pBSmaCols[i]);
C
Cary Xu 已提交
68 69 70
  }

  SUnionTest dut = {0};
C
Cary Xu 已提交
71 72
  qBuf = taosDecodeFixedU8(qBuf, &dut.info);
  qBuf = taosDecodeFixedI16(qBuf, &dut.nBSmaCols);
C
Cary Xu 已提交
73 74 75
  if (dut.nBSmaCols > 0) {
    dut.pBSmaCols = (col_id_t *)taosMemoryMalloc(dut.nBSmaCols * sizeof(col_id_t));
    for (col_id_t i = 0; i < dut.nBSmaCols; ++i) {
C
Cary Xu 已提交
76
      qBuf = taosDecodeFixedI16(qBuf, dut.pBSmaCols + i);
C
Cary Xu 已提交
77 78 79 80 81 82 83 84
    }
  } else {
    dut.pBSmaCols = NULL;
  }

  printf("sut.rollup=%" PRIu8 ", type=%" PRIu8 ", info=%" PRIu8 "\n", sut.rollup, sut.type, sut.info);
  printf("dut.rollup=%" PRIu8 ", type=%" PRIu8 ", info=%" PRIu8 "\n", dut.rollup, dut.type, dut.info);

C
Cary Xu 已提交
85 86 87
  EXPECT_EQ(sut.rollup, dut.rollup);
  EXPECT_EQ(sut.type, dut.type);
  EXPECT_EQ(sut.nBSmaCols, dut.nBSmaCols);
C
Cary Xu 已提交
88
  for (col_id_t i = 0; i < sut.nBSmaCols; ++i) {
C
Cary Xu 已提交
89 90
    EXPECT_EQ(*(col_id_t *)(sut.pBSmaCols + i), sut.pBSmaCols[i]);
    EXPECT_EQ(*(col_id_t *)(sut.pBSmaCols + i), dut.pBSmaCols[i]);
C
Cary Xu 已提交
91
  }
C
Cary Xu 已提交
92 93 94 95

  taosMemoryFreeClear(buf);
  taosMemoryFreeClear(dut.pBSmaCols);
  taosMemoryFreeClear(sut.pBSmaCols);
C
Cary Xu 已提交
96
}
97
#if 1
C
Cary Xu 已提交
98
TEST(testCase, tSma_Meta_Encode_Decode_Test) {
C
Cary Xu 已提交
99 100 101
  // encode
  STSma tSma = {0};
  tSma.version = 0;
C
Cary Xu 已提交
102
  tSma.intervalUnit = TIME_UNIT_DAY;
C
Cary Xu 已提交
103
  tSma.interval = 1;
C
Cary Xu 已提交
104
  tSma.slidingUnit = TIME_UNIT_HOUR;
C
Cary Xu 已提交
105 106
  tSma.sliding = 0;
  tstrncpy(tSma.indexName, "sma_index_test", TSDB_INDEX_NAME_LEN);
C
Cary Xu 已提交
107
  tSma.timezoneInt = 8;
C
Cary Xu 已提交
108
  tSma.indexUid = 2345678910;
C
Cary Xu 已提交
109
  tSma.tableUid = 1234567890;
C
Cary Xu 已提交
110

C
Cary Xu 已提交
111 112
  STSmaWrapper tSmaWrapper = {.number = 1, .tSma = &tSma};
  uint32_t     bufLen = tEncodeTSmaWrapper(NULL, &tSmaWrapper);
C
Cary Xu 已提交
113

wafwerar's avatar
wafwerar 已提交
114
  void *buf = taosMemoryCalloc(1, bufLen);
C
Cary Xu 已提交
115
  EXPECT_NE(buf, nullptr);
C
Cary Xu 已提交
116 117 118 119

  STSmaWrapper *pSW = (STSmaWrapper *)buf;
  uint32_t      len = tEncodeTSmaWrapper(&buf, &tSmaWrapper);

C
Cary Xu 已提交
120
  EXPECT_EQ(len, bufLen);
C
Cary Xu 已提交
121 122 123

  // decode
  STSmaWrapper dstTSmaWrapper = {0};
C
Cary Xu 已提交
124
  void        *result = tDecodeTSmaWrapper(pSW, &dstTSmaWrapper);
C
Cary Xu 已提交
125
  EXPECT_NE(result, nullptr);
C
Cary Xu 已提交
126

C
Cary Xu 已提交
127
  EXPECT_EQ(tSmaWrapper.number, dstTSmaWrapper.number);
C
Cary Xu 已提交
128 129 130 131 132

  for (int i = 0; i < tSmaWrapper.number; ++i) {
    STSma *pSma = tSmaWrapper.tSma + i;
    STSma *qSma = dstTSmaWrapper.tSma + i;

C
Cary Xu 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145
    EXPECT_EQ(pSma->version, qSma->version);
    EXPECT_EQ(pSma->intervalUnit, qSma->intervalUnit);
    EXPECT_EQ(pSma->slidingUnit, qSma->slidingUnit);
    EXPECT_STRCASEEQ(pSma->indexName, qSma->indexName);
    EXPECT_EQ(pSma->timezoneInt, qSma->timezoneInt);
    EXPECT_EQ(pSma->indexUid, qSma->indexUid);
    EXPECT_EQ(pSma->tableUid, qSma->tableUid);
    EXPECT_EQ(pSma->interval, qSma->interval);
    EXPECT_EQ(pSma->sliding, qSma->sliding);
    EXPECT_EQ(pSma->exprLen, qSma->exprLen);
    EXPECT_STRCASEEQ(pSma->expr, qSma->expr);
    EXPECT_EQ(pSma->tagsFilterLen, qSma->tagsFilterLen);
    EXPECT_STRCASEEQ(pSma->tagsFilter, qSma->tagsFilter);
C
Cary Xu 已提交
146 147 148
  }

  // resource release
wafwerar's avatar
wafwerar 已提交
149
  taosMemoryFreeClear(pSW);
C
Cary Xu 已提交
150 151
  tdDestroyTSma(&tSma);
  tdDestroyTSmaWrapper(&dstTSmaWrapper);
C
Cary Xu 已提交
152
}
153
#endif
C
Cary Xu 已提交
154

C
Cary Xu 已提交
155
#if 1
C
Cary Xu 已提交
156
TEST(testCase, tSma_metaDB_Put_Get_Del_Test) {
C
Cary Xu 已提交
157 158
  const char    *smaIndexName1 = "sma_index_test_1";
  const char    *smaIndexName2 = "sma_index_test_2";
C
Cary Xu 已提交
159
  int8_t         timezone = 8;
C
Cary Xu 已提交
160 161 162
  const char    *expr = "select count(a,b, top 20), from table interval 1d, sliding 1h;";
  const char    *tagsFilter = "I'm tags filter";
  const char    *smaTestDir = "./smaTest";
C
Cary Xu 已提交
163 164 165
  const tb_uid_t tbUid = 1234567890;
  const int64_t  indexUid1 = 2000000001;
  const int64_t  indexUid2 = 2000000002;
C
Cary Xu 已提交
166
  const uint32_t nCntTSma = 2;
C
Cary Xu 已提交
167 168 169
  // encode
  STSma tSma = {0};
  tSma.version = 0;
C
Cary Xu 已提交
170
  tSma.intervalUnit = TIME_UNIT_DAY;
C
Cary Xu 已提交
171
  tSma.interval = 1;
C
Cary Xu 已提交
172
  tSma.slidingUnit = TIME_UNIT_HOUR;
C
Cary Xu 已提交
173
  tSma.sliding = 0;
C
Cary Xu 已提交
174
  tSma.indexUid = indexUid1;
C
Cary Xu 已提交
175
  tstrncpy(tSma.indexName, smaIndexName1, TSDB_INDEX_NAME_LEN);
C
Cary Xu 已提交
176
  tSma.timezoneInt = 8;
C
Cary Xu 已提交
177
  tSma.tableUid = tbUid;
C
Cary Xu 已提交
178 179

  tSma.exprLen = strlen(expr);
wafwerar's avatar
wafwerar 已提交
180
  tSma.expr = (char *)taosMemoryCalloc(1, tSma.exprLen + 1);
C
Cary Xu 已提交
181
  EXPECT_NE(tSma.expr, nullptr);
C
Cary Xu 已提交
182 183
  tstrncpy(tSma.expr, expr, tSma.exprLen + 1);

C
Cary Xu 已提交
184
  tSma.tagsFilterLen = strlen(tagsFilter);
wafwerar's avatar
wafwerar 已提交
185
  tSma.tagsFilter = (char *)taosMemoryCalloc(tSma.tagsFilterLen + 1, 1);
C
Cary Xu 已提交
186
  EXPECT_NE(tSma.tagsFilter, nullptr);
C
Cary Xu 已提交
187
  tstrncpy(tSma.tagsFilter, tagsFilter, tSma.tagsFilterLen + 1);
C
Cary Xu 已提交
188

C
Cary Xu 已提交
189 190
  SMeta          *pMeta = NULL;
  STSma          *pSmaCfg = &tSma;
C
Cary Xu 已提交
191 192 193 194 195 196 197
  const SMetaCfg *pMetaCfg = &defaultMetaOptions;

  taosRemoveDir(smaTestDir);

  pMeta = metaOpen(smaTestDir, pMetaCfg, NULL);
  assert(pMeta != NULL);
  // save index 1
C
Cary Xu 已提交
198
  EXPECT_EQ(metaSaveSmaToDB(pMeta, pSmaCfg), 0);
C
Cary Xu 已提交
199

C
Cary Xu 已提交
200
  pSmaCfg->indexUid = indexUid2;
C
Cary Xu 已提交
201 202
  tstrncpy(pSmaCfg->indexName, smaIndexName2, TSDB_INDEX_NAME_LEN);
  pSmaCfg->version = 1;
C
Cary Xu 已提交
203
  pSmaCfg->intervalUnit = TIME_UNIT_HOUR;
C
Cary Xu 已提交
204
  pSmaCfg->interval = 1;
C
Cary Xu 已提交
205
  pSmaCfg->slidingUnit = TIME_UNIT_MINUTE;
C
Cary Xu 已提交
206 207 208
  pSmaCfg->sliding = 5;

  // save index 2
C
Cary Xu 已提交
209
  EXPECT_EQ(metaSaveSmaToDB(pMeta, pSmaCfg), 0);
C
Cary Xu 已提交
210 211

  // get value by indexName
C
Cary Xu 已提交
212
  STSma *qSmaCfg = NULL;
C
Cary Xu 已提交
213
  qSmaCfg = metaGetSmaInfoByIndex(pMeta, indexUid1, true);
C
Cary Xu 已提交
214 215
  assert(qSmaCfg != NULL);
  printf("name1 = %s\n", qSmaCfg->indexName);
C
Cary Xu 已提交
216
  printf("timezone1 = %" PRIi8 "\n", qSmaCfg->timezoneInt);
C
Cary Xu 已提交
217
  printf("expr1 = %s\n", qSmaCfg->expr != NULL ? qSmaCfg->expr : "");
C
Cary Xu 已提交
218
  printf("tagsFilter1 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : "");
C
Cary Xu 已提交
219 220
  EXPECT_STRCASEEQ(qSmaCfg->indexName, smaIndexName1);
  EXPECT_EQ(qSmaCfg->tableUid, tSma.tableUid);
C
Cary Xu 已提交
221
  tdDestroyTSma(qSmaCfg);
wafwerar's avatar
wafwerar 已提交
222
  taosMemoryFreeClear(qSmaCfg);
C
Cary Xu 已提交
223

C
Cary Xu 已提交
224
  qSmaCfg = metaGetSmaInfoByIndex(pMeta, indexUid2, true);
C
Cary Xu 已提交
225 226
  assert(qSmaCfg != NULL);
  printf("name2 = %s\n", qSmaCfg->indexName);
C
Cary Xu 已提交
227
  printf("timezone2 = %" PRIi8 "\n", qSmaCfg->timezoneInt);
C
Cary Xu 已提交
228
  printf("expr2 = %s\n", qSmaCfg->expr != NULL ? qSmaCfg->expr : "");
C
Cary Xu 已提交
229
  printf("tagsFilter2 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : "");
C
Cary Xu 已提交
230 231
  EXPECT_STRCASEEQ(qSmaCfg->indexName, smaIndexName2);
  EXPECT_EQ(qSmaCfg->interval, tSma.interval);
C
Cary Xu 已提交
232
  tdDestroyTSma(qSmaCfg);
wafwerar's avatar
wafwerar 已提交
233
  taosMemoryFreeClear(qSmaCfg);
C
Cary Xu 已提交
234

C
Cary Xu 已提交
235
  // get index name by table uid
C
Cary Xu 已提交
236
#if 0
C
Cary Xu 已提交
237 238 239 240
  SMSmaCursor *pSmaCur = metaOpenSmaCursor(pMeta, tbUid);
  assert(pSmaCur != NULL);
  uint32_t indexCnt = 0;
  while (1) {
C
Cary Xu 已提交
241
    const char *indexName = (const char *)metaSmaCursorNext(pSmaCur);
C
Cary Xu 已提交
242 243 244 245 246 247
    if (indexName == NULL) {
      break;
    }
    printf("indexName = %s\n", indexName);
    ++indexCnt;
  }
C
Cary Xu 已提交
248
  EXPECT_EQ(indexCnt, nCntTSma);
C
Cary Xu 已提交
249 250
  metaCloseSmaCursor(pSmaCur);
#endif
C
Cary Xu 已提交
251
  // get wrapper by table uid
C
Cary Xu 已提交
252
  STSmaWrapper *pSW = metaGetSmaInfoByTable(pMeta, tbUid);
C
Cary Xu 已提交
253
  assert(pSW != NULL);
C
Cary Xu 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266
  EXPECT_EQ(pSW->number, nCntTSma);
  EXPECT_STRCASEEQ(pSW->tSma->indexName, smaIndexName1);
  EXPECT_EQ(pSW->tSma->timezoneInt, timezone);
  EXPECT_STRCASEEQ(pSW->tSma->expr, expr);
  EXPECT_STRCASEEQ(pSW->tSma->tagsFilter, tagsFilter);
  EXPECT_EQ(pSW->tSma->indexUid, indexUid1);
  EXPECT_EQ(pSW->tSma->tableUid, tbUid);
  EXPECT_STRCASEEQ((pSW->tSma + 1)->indexName, smaIndexName2);
  EXPECT_EQ((pSW->tSma + 1)->timezoneInt, timezone);
  EXPECT_STRCASEEQ((pSW->tSma + 1)->expr, expr);
  EXPECT_STRCASEEQ((pSW->tSma + 1)->tagsFilter, tagsFilter);
  EXPECT_EQ((pSW->tSma + 1)->indexUid, indexUid2);
  EXPECT_EQ((pSW->tSma + 1)->tableUid, tbUid);
C
Cary Xu 已提交
267

C
Cary Xu 已提交
268
  tdDestroyTSmaWrapper(pSW);
wafwerar's avatar
wafwerar 已提交
269
  taosMemoryFreeClear(pSW);
C
Cary Xu 已提交
270 271 272 273 274 275 276 277

  // get all sma table uids
  SArray *pUids = metaGetSmaTbUids(pMeta, false);
  assert(pUids != NULL);
  for (uint32_t i = 0; i < taosArrayGetSize(pUids); ++i) {
    printf("metaGetSmaTbUids: uid[%" PRIu32 "] = %" PRIi64 "\n", i, *(tb_uid_t *)taosArrayGet(pUids, i));
    // printf("metaGetSmaTbUids: index[%" PRIu32 "] = %s", i, (char *)taosArrayGet(pUids, i));
  }
C
Cary Xu 已提交
278
  EXPECT_EQ(taosArrayGetSize(pUids), 1);
C
Cary Xu 已提交
279
  taosArrayDestroy(pUids);
C
Cary Xu 已提交
280

C
Cary Xu 已提交
281
  // resource release
C
Cary Xu 已提交
282 283
  metaRemoveSmaFromDb(pMeta, indexUid1);
  metaRemoveSmaFromDb(pMeta, indexUid2);
C
Cary Xu 已提交
284

C
Cary Xu 已提交
285
  tdDestroyTSma(&tSma);
C
Cary Xu 已提交
286
  metaClose(pMeta);
C
Cary Xu 已提交
287
}
C
Cary Xu 已提交
288
#endif
C
Cary Xu 已提交
289

C
Cary Xu 已提交
290
#if 1
C
Cary Xu 已提交
291 292
TEST(testCase, tSma_Data_Insert_Query_Test) {
  // step 1: prepare meta
C
Cary Xu 已提交
293
  const char    *smaIndexName1 = "sma_index_test_1";
C
Cary Xu 已提交
294
  const int8_t   timezone = 8;
C
Cary Xu 已提交
295 296 297
  const char    *expr = "select count(a,b, top 20), from table interval 1d, sliding 1h;";
  const char    *tagsFilter = "where tags.location='Beijing' and tags.district='ChaoYang'";
  const char    *smaTestDir = "./smaTest";
C
Cary Xu 已提交
298 299
  const tb_uid_t tbUid = 1234567890;
  const int64_t  indexUid1 = 2000000001;
C
Cary Xu 已提交
300
  const int64_t  interval1 = 1;
C
Cary Xu 已提交
301
  const int8_t   intervalUnit1 = TIME_UNIT_DAY;
C
Cary Xu 已提交
302
  const uint32_t nCntTSma = 2;
C
Cary Xu 已提交
303 304 305
  TSKEY          skey1 = 1646987196;
  const int64_t  testSmaData1 = 100;
  const int64_t  testSmaData2 = 200;
C
Cary Xu 已提交
306 307 308
  // encode
  STSma tSma = {0};
  tSma.version = 0;
C
Cary Xu 已提交
309
  tSma.intervalUnit = TIME_UNIT_MINUTE;
C
Cary Xu 已提交
310
  tSma.interval = 1;
C
Cary Xu 已提交
311
  tSma.slidingUnit = TIME_UNIT_MINUTE;
C
Cary Xu 已提交
312
  tSma.sliding = 1;  // sliding = interval when it's convert window
C
Cary Xu 已提交
313 314
  tSma.indexUid = indexUid1;
  tstrncpy(tSma.indexName, smaIndexName1, TSDB_INDEX_NAME_LEN);
C
Cary Xu 已提交
315
  tSma.timezoneInt = timezone;
C
Cary Xu 已提交
316 317 318
  tSma.tableUid = tbUid;

  tSma.exprLen = strlen(expr);
wafwerar's avatar
wafwerar 已提交
319
  tSma.expr = (char *)taosMemoryCalloc(1, tSma.exprLen + 1);
C
Cary Xu 已提交
320
  EXPECT_NE(tSma.expr, nullptr);
C
Cary Xu 已提交
321 322 323
  tstrncpy(tSma.expr, expr, tSma.exprLen + 1);

  tSma.tagsFilterLen = strlen(tagsFilter);
wafwerar's avatar
wafwerar 已提交
324
  tSma.tagsFilter = (char *)taosMemoryCalloc(1, tSma.tagsFilterLen + 1);
C
Cary Xu 已提交
325
  EXPECT_NE(tSma.tagsFilter, nullptr);
C
Cary Xu 已提交
326 327
  tstrncpy(tSma.tagsFilter, tagsFilter, tSma.tagsFilterLen + 1);

C
Cary Xu 已提交
328 329
  SMeta          *pMeta = NULL;
  STSma          *pSmaCfg = &tSma;
C
Cary Xu 已提交
330 331 332 333 334 335 336
  const SMetaCfg *pMetaCfg = &defaultMetaOptions;

  taosRemoveDir(smaTestDir);

  pMeta = metaOpen(smaTestDir, pMetaCfg, NULL);
  assert(pMeta != NULL);
  // save index 1
C
Cary Xu 已提交
337
  EXPECT_EQ(metaSaveSmaToDB(pMeta, pSmaCfg), 0);
C
Cary Xu 已提交
338

C
Cary Xu 已提交
339
  // step 2: insert data
C
Cary Xu 已提交
340 341
  STsdb    *pTsdb = (STsdb *)taosMemoryCalloc(1, sizeof(STsdb));
  STsdbCfg *pCfg = &pTsdb->config;
342 343 344 345 346 347 348 349 350 351 352 353

  pTsdb->pMeta = pMeta;
  pTsdb->vgId = 2;
  pTsdb->config.daysPerFile = 10;  // default days is 10
  pTsdb->config.keep1 = 30;
  pTsdb->config.keep2 = 90;
  pTsdb->config.keep = 365;
  pTsdb->config.precision = TSDB_TIME_PRECISION_MILLI;
  pTsdb->config.update = TD_ROW_OVERWRITE_UPDATE;
  pTsdb->config.compression = TWO_STAGE_COMP;

  switch (pTsdb->config.precision) {
C
Cary Xu 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367
    case TSDB_TIME_PRECISION_MILLI:
      skey1 *= 1e3;
      break;
    case TSDB_TIME_PRECISION_MICRO:
      skey1 *= 1e6;
      break;
    case TSDB_TIME_PRECISION_NANO:
      skey1 *= 1e9;
      break;
    default:  // ms
      skey1 *= 1e3;
      break;
  }

H
Haojun Liao 已提交
368 369 370
  SDiskCfg pDisks = {0};
  pDisks.level = 0;
  pDisks.primary = 1;
C
Cary Xu 已提交
371 372
  strncpy(pDisks.dir, "/var/lib/taos", TSDB_FILENAME_LEN);
  int32_t numOfDisks = 1;
373
  pTsdb->pTfs = tfsOpen(&pDisks, numOfDisks);
C
Cary Xu 已提交
374
  EXPECT_NE(pTsdb->pTfs, nullptr);
C
Cary Xu 已提交
375

C
Cary Xu 已提交
376 377 378 379 380 381 382
  // generate SSubmitReq msg and update expired window
  int16_t  schemaVer = 0;
  uint32_t mockRowLen = sizeof(STSRow);
  uint32_t mockRowNum = 2;
  uint32_t mockBlkNum = 2;
  uint32_t msgLen = sizeof(SSubmitReq) + mockBlkNum * sizeof(SSubmitBlk) + mockBlkNum * mockRowNum * mockRowLen;

C
Cary Xu 已提交
383
  SSubmitReq *pMsg = (SSubmitReq *)taosMemoryCalloc(1, msgLen);
C
Cary Xu 已提交
384
  EXPECT_NE(pMsg, nullptr);
C
Cary Xu 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  pMsg->version = htobe64(schemaVer);
  pMsg->numOfBlocks = htonl(mockBlkNum);
  pMsg->length = htonl(msgLen);

  SSubmitBlk *pBlk = NULL;
  STSRow     *pRow = NULL;
  TSKEY       now = taosGetTimestamp(pTsdb->config.precision);

  for (uint32_t b = 0; b < mockBlkNum; ++b) {
    pBlk = (SSubmitBlk *)POINTER_SHIFT(pMsg, sizeof(SSubmitReq) + b * (sizeof(SSubmitBlk) + mockRowNum * mockRowLen));
    pBlk->uid = htobe64(tbUid);
    pBlk->suid = htobe64(tbUid);
    pBlk->sversion = htonl(schemaVer);
    pBlk->padding = htonl(0);
    pBlk->schemaLen = htonl(0);
    pBlk->numOfRows = htons(mockRowNum);
    pBlk->dataLen = htonl(mockRowNum * mockRowLen);
    for (uint32_t r = 0; r < mockRowNum; ++r) {
      pRow = (STSRow *)POINTER_SHIFT(pBlk, sizeof(SSubmitBlk) + r * mockRowLen);
      pRow->len = mockRowLen;
      pRow->ts = now + b * 1000 + r * 1000;
      pRow->sver = schemaVer;
    }
  }

C
Cary Xu 已提交
410
  EXPECT_EQ(tdScanAndConvertSubmitMsg(pMsg), TSDB_CODE_SUCCESS);
C
Cary Xu 已提交
411

412
  EXPECT_EQ(tsdbUpdateSmaWindow(pTsdb, pMsg, 0), 0);
C
Cary Xu 已提交
413 414

  // init
C
Cary Xu 已提交
415 416 417 418 419 420 421
  const int32_t  tSmaGroupSize = 4;
  const int32_t  tSmaNumOfTags = 2;
  const int64_t  tSmaGroupId = 12345670;
  const col_id_t tSmaNumOfCols = 9;  // binary/nchar/varbinary/varchar are only used for tags for group by conditions.
  const int32_t  tSmaNumOfRows = 2;

  SArray *pDataBlocks = taosArrayInit(tSmaGroupSize, sizeof(SSDataBlock *));
C
Cary Xu 已提交
422
  EXPECT_NE(pDataBlocks, nullptr);
C
Cary Xu 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435
  int32_t tSmaTypeArray[tSmaNumOfCols] = {TSDB_DATA_TYPE_TIMESTAMP, TSDB_DATA_TYPE_BOOL,     TSDB_DATA_TYPE_INT,
                                          TSDB_DATA_TYPE_UBIGINT,   TSDB_DATA_TYPE_SMALLINT, TSDB_DATA_TYPE_FLOAT,
                                          TSDB_DATA_TYPE_DOUBLE,    TSDB_DATA_TYPE_VARCHAR,  TSDB_DATA_TYPE_NCHAR};
  // last 2 columns for group by tags
  // int32_t tSmaTypeArray[tSmaNumOfCols] = {TSDB_DATA_TYPE_TIMESTAMP, TSDB_DATA_TYPE_BOOL};
  const char *tSmaGroupbyTags[tSmaGroupSize * tSmaNumOfTags] = {"BeiJing",  "HaiDian", "BeiJing",   "ChaoYang",
                                                                "ShangHai", "PuDong",  "ShangHai", "MinHang"};
  TSKEY       tSmaSKeyMs = (int64_t)1648535332 * 1000;
  int64_t     tSmaIntervalMs = tSma.interval * 60 * 1000;
  int64_t     tSmaInitVal = 0;

  for (int32_t g = 0; g < tSmaGroupSize; ++g) {
    SSDataBlock *pDataBlock = (SSDataBlock *)taosMemoryCalloc(1, sizeof(SSDataBlock));
C
Cary Xu 已提交
436
    EXPECT_NE(pDataBlock, nullptr);
C
Cary Xu 已提交
437 438 439 440 441 442
    pDataBlock->pBlockAgg = NULL;
    pDataBlock->info.numOfCols = tSmaNumOfCols;
    pDataBlock->info.rows = tSmaNumOfRows;
    pDataBlock->info.groupId = tSmaGroupId + g;

    pDataBlock->pDataBlock = taosArrayInit(tSmaNumOfCols, sizeof(SColumnInfoData *));
C
Cary Xu 已提交
443
    EXPECT_NE(pDataBlock->pDataBlock, nullptr);
C
Cary Xu 已提交
444 445 446
    for (int32_t c = 0; c < tSmaNumOfCols; ++c) {
      
      SColumnInfoData *pColInfoData = (SColumnInfoData *)taosMemoryCalloc(1, sizeof(SColumnInfoData));
C
Cary Xu 已提交
447
      EXPECT_NE(pColInfoData, nullptr);
C
Cary Xu 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

      pColInfoData->info.type = tSmaTypeArray[c];
      if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
        pColInfoData->info.bytes = 100;  // update accordingly
      } else {
        pColInfoData->info.bytes = TYPE_BYTES[pColInfoData->info.type];
      }
      pColInfoData->pData = (char *)taosMemoryCalloc(1, tSmaNumOfRows * pColInfoData->info.bytes);

      for (int32_t r = 0; r < tSmaNumOfRows; ++r) {
        void *pCellData = pColInfoData->pData + r * pColInfoData->info.bytes;
        switch (pColInfoData->info.type) {
          case TSDB_DATA_TYPE_TIMESTAMP:
            *(TSKEY *)pCellData = tSmaSKeyMs + tSmaIntervalMs * r;
            break;
          case TSDB_DATA_TYPE_BOOL:
            *(bool *)pCellData = (bool)tSmaInitVal++;
            break;
          case TSDB_DATA_TYPE_INT:
            *(int *)pCellData = (int)tSmaInitVal++;
            break;
          case TSDB_DATA_TYPE_UBIGINT:
            *(uint64_t *)pCellData = (uint64_t)tSmaInitVal++;
            break;
          case TSDB_DATA_TYPE_SMALLINT:
            *(int16_t *)pCellData = (int16_t)tSmaInitVal++;
            break;
          case TSDB_DATA_TYPE_FLOAT:
            *(float *)pCellData = (float)tSmaInitVal++;
            break;
          case TSDB_DATA_TYPE_DOUBLE:
            *(double *)pCellData = (double)tSmaInitVal++;
            break;
          case TSDB_DATA_TYPE_VARCHAR:  // city
            varDataSetLen(pCellData, strlen(tSmaGroupbyTags[g * 2]));
            memcpy(varDataVal(pCellData), tSmaGroupbyTags[g * 2], varDataLen(pCellData));
            break;
          case TSDB_DATA_TYPE_NCHAR:  // district
            varDataSetLen(pCellData, strlen(tSmaGroupbyTags[g * 2 + 1]));
            memcpy(varDataVal(pCellData), tSmaGroupbyTags[g * 2 + 1], varDataLen(pCellData));
            break;
          default:
C
Cary Xu 已提交
490
            EXPECT_EQ(0, 1);  // add definition
C
Cary Xu 已提交
491 492 493 494 495 496 497 498 499 500 501
            break;
        }
      }
      // push SColumnInfoData
      taosArrayPush(pDataBlock->pDataBlock, &pColInfoData);
    }
    // push SSDataBlock
    taosArrayPush(pDataBlocks, &pDataBlock);
  }

  // execute
C
Cary Xu 已提交
502
  EXPECT_EQ(tsdbInsertTSmaData(pTsdb, tSma.indexUid, (const char *)pDataBlocks), TSDB_CODE_SUCCESS);
C
Cary Xu 已提交
503

C
Cary Xu 已提交
504 505
#if 0
  STSmaDataWrapper *pSmaData = NULL;
C
Cary Xu 已提交
506 507
  pSmaData = (STSmaDataWrapper *)buf;
  printf(">> allocate [%d] time to %d and addr is %p\n", ++allocCnt, bufSize, pSmaData);
C
Cary Xu 已提交
508 509 510
  pSmaData->skey = skey1;
  pSmaData->interval = interval1;
  pSmaData->intervalUnit = intervalUnit1;
511
  pSmaData->indexUid = indexUid1;
C
Cary Xu 已提交
512 513 514 515

  int32_t len = sizeof(STSmaDataWrapper);
  for (int32_t t = 0; t < numOfTables; ++t) {
    STSmaTbData *pTbData = (STSmaTbData *)POINTER_SHIFT(pSmaData, len);
C
Cary Xu 已提交
516
    pTbData->tableUid = tbUid + t;
C
Cary Xu 已提交
517 518 519 520

    int32_t tableDataLen = sizeof(STSmaTbData);
    for (col_id_t c = 0; c < numOfCols; ++c) {
      if (bufSize - len - tableDataLen < buffer) {
C
Cary Xu 已提交
521
        EXPECT_EQ(tsdbMakeRoom(&buf, bufSize + allocStep), 0);
C
Cary Xu 已提交
522 523 524 525 526 527 528
        pSmaData = (STSmaDataWrapper *)buf;
        pTbData = (STSmaTbData *)POINTER_SHIFT(pSmaData, len);
        bufSize = taosTSizeof(buf);
        printf(">> allocate [%d] time to %d and addr is %p\n", ++allocCnt, bufSize, pSmaData);
      }
      STSmaColData *pColData = (STSmaColData *)POINTER_SHIFT(pSmaData, len + tableDataLen);
      pColData->colId = c + PRIMARYKEY_TIMESTAMP_COL_ID;
C
Cary Xu 已提交
529

C
Cary Xu 已提交
530
      // TODO: fill col data
C
Cary Xu 已提交
531 532 533 534 535 536 537 538 539
      if ((c & 1) == 0) {
        pColData->blockSize = 8;
        memcpy(pColData->data, &testSmaData1, 8);
      } else {
        pColData->blockSize = 16;
        memcpy(pColData->data, &testSmaData1, 8);
        memcpy(POINTER_SHIFT(pColData->data, 8), &testSmaData2, 8);
      }

C
Cary Xu 已提交
540 541 542 543 544
      tableDataLen += (sizeof(STSmaColData) + pColData->blockSize);
    }
    pTbData->dataLen = (tableDataLen - sizeof(STSmaTbData));
    len += tableDataLen;
    // printf("bufSize=%d, len=%d, len of table[%d]=%d\n", bufSize, len, t, tableDataLen);
C
Cary Xu 已提交
545
  }
C
Cary Xu 已提交
546 547
  pSmaData->dataLen = (len - sizeof(STSmaDataWrapper));

C
Cary Xu 已提交
548
  EXPECT_GE(bufSize, pSmaData->dataLen);
C
Cary Xu 已提交
549
  // execute
C
Cary Xu 已提交
550
  EXPECT_EQ(tsdbInsertTSmaData(pTsdb, (char *)pSmaData), TSDB_CODE_SUCCESS);
C
Cary Xu 已提交
551 552
#endif

C
Cary Xu 已提交
553 554
  // step 3: query
  uint32_t checkDataCnt = 0;
C
Cary Xu 已提交
555
  EXPECT_EQ(tsdbGetTSmaData(pTsdb, NULL, indexUid1, skey1, 1), TSDB_CODE_SUCCESS);
C
Cary Xu 已提交
556
  ++checkDataCnt;
C
Cary Xu 已提交
557

C
Cary Xu 已提交
558 559
  printf("%s:%d The sma data check count for insert and query is %" PRIu32 "\n", __FILE__, __LINE__, checkDataCnt);

C
Cary Xu 已提交
560
  // release data
C
Cary Xu 已提交
561
  taosMemoryFreeClear(pMsg);
C
Cary Xu 已提交
562 563

  for (int32_t i = 0; i < taosArrayGetSize(pDataBlocks); ++i) {
C
Cary Xu 已提交
564
    SSDataBlock *pDataBlock = *(SSDataBlock **)taosArrayGet(pDataBlocks, i);
C
Cary Xu 已提交
565 566
    int32_t      numOfOutput = taosArrayGetSize(pDataBlock->pDataBlock);
    for (int32_t j = 0; j < numOfOutput; ++j) {
C
Cary Xu 已提交
567
      SColumnInfoData *pColInfoData = *(SColumnInfoData **)taosArrayGet(pDataBlock->pDataBlock, j);
C
Cary Xu 已提交
568
      colDataDestroy(pColInfoData);
C
Cary Xu 已提交
569
      taosMemoryFreeClear(pColInfoData);
C
Cary Xu 已提交
570 571 572 573 574 575 576 577
    }

    taosArrayDestroy(pDataBlock->pDataBlock);
    taosMemoryFreeClear(pDataBlock->pBlockAgg);
    taosMemoryFreeClear(pDataBlock);
  }
  taosArrayDestroy(pDataBlocks);

C
Cary Xu 已提交
578 579
  // release meta
  tdDestroyTSma(&tSma);
580 581
  tfsClose(pTsdb->pTfs);
  tsdbClose(pTsdb);
C
Cary Xu 已提交
582
  metaClose(pMeta);
C
Cary Xu 已提交
583
}
C
Cary Xu 已提交
584

C
Cary Xu 已提交
585 586 587
#endif

#pragma GCC diagnostic pop