tsdbSmaTest.cpp 16.0 KB
Newer Older
C
Cary Xu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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>
#include <taoserror.h>
#include <tglobal.h>
C
Cary Xu 已提交
19
#include <tsdbDef.h>
C
Cary Xu 已提交
20 21
#include <iostream>

C
Cary Xu 已提交
22
#include <metaDef.h>
C
Cary Xu 已提交
23 24
#include <tmsg.h>

C
Cary Xu 已提交
25 26 27 28 29 30
#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 已提交
31 32 33 34 35
int main(int argc, char **argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

C
Cary Xu 已提交
36 37 38 39 40 41 42 43 44 45
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 已提交
46
    col_id_t *pBSmaCols;
C
Cary Xu 已提交
47 48 49 50 51 52 53
  } SUnionTest;

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

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

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

  SUnionTest dut = {0};
  pBuf = taosDecodeFixedU8(pBuf, &dut.info);
  pBuf = taosDecodeFixedI16(pBuf, &dut.nBSmaCols);
C
Cary Xu 已提交
71 72 73
  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 已提交
74 75 76 77 78 79 80 81 82 83 84 85
      pBuf = taosDecodeFixedI16(pBuf, dut.pBSmaCols + i);
    }
  } 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);

  ASSERT_EQ(sut.rollup, dut.rollup);
  ASSERT_EQ(sut.type, dut.type);
  ASSERT_EQ(sut.nBSmaCols, dut.nBSmaCols);
C
Cary Xu 已提交
86 87 88
  for (col_id_t i = 0; i < sut.nBSmaCols; ++i) {
    ASSERT_EQ(*(col_id_t *)(sut.pBSmaCols + i), sut.pBSmaCols[i]);
    ASSERT_EQ(*(col_id_t *)(sut.pBSmaCols + i), dut.pBSmaCols[i]);
C
Cary Xu 已提交
89 90
  }
}
91
#if 1
C
Cary Xu 已提交
92
TEST(testCase, tSma_Meta_Encode_Decode_Test) {
C
Cary Xu 已提交
93 94 95
  // encode
  STSma tSma = {0};
  tSma.version = 0;
C
Cary Xu 已提交
96
  tSma.intervalUnit = TIME_UNIT_DAY;
C
Cary Xu 已提交
97
  tSma.interval = 1;
C
Cary Xu 已提交
98
  tSma.slidingUnit = TIME_UNIT_HOUR;
C
Cary Xu 已提交
99 100
  tSma.sliding = 0;
  tstrncpy(tSma.indexName, "sma_index_test", TSDB_INDEX_NAME_LEN);
C
Cary Xu 已提交
101
  tSma.timezoneInt = 8;
C
Cary Xu 已提交
102
  tSma.indexUid = 2345678910;
C
Cary Xu 已提交
103
  tSma.tableUid = 1234567890;
C
Cary Xu 已提交
104

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

wafwerar's avatar
wafwerar 已提交
108
  void *buf = taosMemoryCalloc(1, bufLen);
C
Cary Xu 已提交
109
  ASSERT_NE(buf, nullptr);
C
Cary Xu 已提交
110 111 112 113

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

C
Cary Xu 已提交
114
  ASSERT_EQ(len, bufLen);
C
Cary Xu 已提交
115 116 117

  // decode
  STSmaWrapper dstTSmaWrapper = {0};
C
Cary Xu 已提交
118
  void        *result = tDecodeTSmaWrapper(pSW, &dstTSmaWrapper);
C
Cary Xu 已提交
119
  ASSERT_NE(result, nullptr);
C
Cary Xu 已提交
120

C
Cary Xu 已提交
121
  ASSERT_EQ(tSmaWrapper.number, dstTSmaWrapper.number);
C
Cary Xu 已提交
122 123 124 125 126

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

C
Cary Xu 已提交
127 128 129 130
    ASSERT_EQ(pSma->version, qSma->version);
    ASSERT_EQ(pSma->intervalUnit, qSma->intervalUnit);
    ASSERT_EQ(pSma->slidingUnit, qSma->slidingUnit);
    ASSERT_STRCASEEQ(pSma->indexName, qSma->indexName);
C
Cary Xu 已提交
131
    ASSERT_EQ(pSma->timezoneInt, qSma->timezoneInt);
C
Cary Xu 已提交
132 133 134 135 136 137 138 139
    ASSERT_EQ(pSma->indexUid, qSma->indexUid);
    ASSERT_EQ(pSma->tableUid, qSma->tableUid);
    ASSERT_EQ(pSma->interval, qSma->interval);
    ASSERT_EQ(pSma->sliding, qSma->sliding);
    ASSERT_EQ(pSma->exprLen, qSma->exprLen);
    ASSERT_STRCASEEQ(pSma->expr, qSma->expr);
    ASSERT_EQ(pSma->tagsFilterLen, qSma->tagsFilterLen);
    ASSERT_STRCASEEQ(pSma->tagsFilter, qSma->tagsFilter);
C
Cary Xu 已提交
140 141 142
  }

  // resource release
wafwerar's avatar
wafwerar 已提交
143
  taosMemoryFreeClear(pSW);
C
Cary Xu 已提交
144 145
  tdDestroyTSma(&tSma);
  tdDestroyTSmaWrapper(&dstTSmaWrapper);
C
Cary Xu 已提交
146
}
147
#endif
C
Cary Xu 已提交
148

C
Cary Xu 已提交
149
#if 1
C
Cary Xu 已提交
150
TEST(testCase, tSma_metaDB_Put_Get_Del_Test) {
C
Cary Xu 已提交
151 152
  const char    *smaIndexName1 = "sma_index_test_1";
  const char    *smaIndexName2 = "sma_index_test_2";
C
Cary Xu 已提交
153
  int8_t         timezone = 8;
C
Cary Xu 已提交
154 155 156
  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 已提交
157 158 159
  const tb_uid_t tbUid = 1234567890;
  const int64_t  indexUid1 = 2000000001;
  const int64_t  indexUid2 = 2000000002;
C
Cary Xu 已提交
160
  const uint32_t nCntTSma = 2;
C
Cary Xu 已提交
161 162 163
  // encode
  STSma tSma = {0};
  tSma.version = 0;
C
Cary Xu 已提交
164
  tSma.intervalUnit = TIME_UNIT_DAY;
C
Cary Xu 已提交
165
  tSma.interval = 1;
C
Cary Xu 已提交
166
  tSma.slidingUnit = TIME_UNIT_HOUR;
C
Cary Xu 已提交
167
  tSma.sliding = 0;
C
Cary Xu 已提交
168
  tSma.indexUid = indexUid1;
C
Cary Xu 已提交
169
  tstrncpy(tSma.indexName, smaIndexName1, TSDB_INDEX_NAME_LEN);
C
Cary Xu 已提交
170
  tSma.timezoneInt = 8;
C
Cary Xu 已提交
171
  tSma.tableUid = tbUid;
C
Cary Xu 已提交
172 173

  tSma.exprLen = strlen(expr);
wafwerar's avatar
wafwerar 已提交
174
  tSma.expr = (char *)taosMemoryCalloc(1, tSma.exprLen + 1);
C
Cary Xu 已提交
175
  ASSERT_NE(tSma.expr, nullptr);
C
Cary Xu 已提交
176 177
  tstrncpy(tSma.expr, expr, tSma.exprLen + 1);

C
Cary Xu 已提交
178
  tSma.tagsFilterLen = strlen(tagsFilter);
wafwerar's avatar
wafwerar 已提交
179
  tSma.tagsFilter = (char *)taosMemoryCalloc(tSma.tagsFilterLen + 1, 1);
C
Cary Xu 已提交
180
  ASSERT_NE(tSma.tagsFilter, nullptr);
C
Cary Xu 已提交
181
  tstrncpy(tSma.tagsFilter, tagsFilter, tSma.tagsFilterLen + 1);
C
Cary Xu 已提交
182

C
Cary Xu 已提交
183 184
  SMeta          *pMeta = NULL;
  STSma          *pSmaCfg = &tSma;
C
Cary Xu 已提交
185 186 187 188 189 190 191
  const SMetaCfg *pMetaCfg = &defaultMetaOptions;

  taosRemoveDir(smaTestDir);

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

C
Cary Xu 已提交
194
  pSmaCfg->indexUid = indexUid2;
C
Cary Xu 已提交
195 196
  tstrncpy(pSmaCfg->indexName, smaIndexName2, TSDB_INDEX_NAME_LEN);
  pSmaCfg->version = 1;
C
Cary Xu 已提交
197
  pSmaCfg->intervalUnit = TIME_UNIT_HOUR;
C
Cary Xu 已提交
198
  pSmaCfg->interval = 1;
C
Cary Xu 已提交
199
  pSmaCfg->slidingUnit = TIME_UNIT_MINUTE;
C
Cary Xu 已提交
200 201 202
  pSmaCfg->sliding = 5;

  // save index 2
C
Cary Xu 已提交
203
  ASSERT_EQ(metaSaveSmaToDB(pMeta, pSmaCfg), 0);
C
Cary Xu 已提交
204 205

  // get value by indexName
C
Cary Xu 已提交
206
  STSma *qSmaCfg = NULL;
C
Cary Xu 已提交
207
  qSmaCfg = metaGetSmaInfoByIndex(pMeta, indexUid1);
C
Cary Xu 已提交
208 209
  assert(qSmaCfg != NULL);
  printf("name1 = %s\n", qSmaCfg->indexName);
C
Cary Xu 已提交
210
  printf("timezone1 = %" PRIi8 "\n", qSmaCfg->timezoneInt);
C
Cary Xu 已提交
211
  printf("expr1 = %s\n", qSmaCfg->expr != NULL ? qSmaCfg->expr : "");
C
Cary Xu 已提交
212
  printf("tagsFilter1 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : "");
C
Cary Xu 已提交
213 214
  ASSERT_STRCASEEQ(qSmaCfg->indexName, smaIndexName1);
  ASSERT_EQ(qSmaCfg->tableUid, tSma.tableUid);
C
Cary Xu 已提交
215
  tdDestroyTSma(qSmaCfg);
wafwerar's avatar
wafwerar 已提交
216
  taosMemoryFreeClear(qSmaCfg);
C
Cary Xu 已提交
217

C
Cary Xu 已提交
218
  qSmaCfg = metaGetSmaInfoByIndex(pMeta, indexUid2);
C
Cary Xu 已提交
219 220
  assert(qSmaCfg != NULL);
  printf("name2 = %s\n", qSmaCfg->indexName);
C
Cary Xu 已提交
221
  printf("timezone2 = %" PRIi8 "\n", qSmaCfg->timezoneInt);
C
Cary Xu 已提交
222
  printf("expr2 = %s\n", qSmaCfg->expr != NULL ? qSmaCfg->expr : "");
C
Cary Xu 已提交
223
  printf("tagsFilter2 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : "");
C
Cary Xu 已提交
224 225
  ASSERT_STRCASEEQ(qSmaCfg->indexName, smaIndexName2);
  ASSERT_EQ(qSmaCfg->interval, tSma.interval);
C
Cary Xu 已提交
226
  tdDestroyTSma(qSmaCfg);
wafwerar's avatar
wafwerar 已提交
227
  taosMemoryFreeClear(qSmaCfg);
C
Cary Xu 已提交
228

C
Cary Xu 已提交
229
  // get index name by table uid
C
Cary Xu 已提交
230 231 232 233
  SMSmaCursor *pSmaCur = metaOpenSmaCursor(pMeta, tbUid);
  assert(pSmaCur != NULL);
  uint32_t indexCnt = 0;
  while (1) {
C
Cary Xu 已提交
234
    const char *indexName = metaSmaCursorNext(pSmaCur);
C
Cary Xu 已提交
235 236 237 238 239 240
    if (indexName == NULL) {
      break;
    }
    printf("indexName = %s\n", indexName);
    ++indexCnt;
  }
C
Cary Xu 已提交
241
  ASSERT_EQ(indexCnt, nCntTSma);
C
Cary Xu 已提交
242 243
  metaCloseSmaCurosr(pSmaCur);

C
Cary Xu 已提交
244
  // get wrapper by table uid
C
Cary Xu 已提交
245
  STSmaWrapper *pSW = metaGetSmaInfoByTable(pMeta, tbUid);
C
Cary Xu 已提交
246
  assert(pSW != NULL);
C
Cary Xu 已提交
247 248
  ASSERT_EQ(pSW->number, nCntTSma);
  ASSERT_STRCASEEQ(pSW->tSma->indexName, smaIndexName1);
C
Cary Xu 已提交
249
  ASSERT_EQ(pSW->tSma->timezoneInt, timezone);
C
Cary Xu 已提交
250 251 252 253 254
  ASSERT_STRCASEEQ(pSW->tSma->expr, expr);
  ASSERT_STRCASEEQ(pSW->tSma->tagsFilter, tagsFilter);
  ASSERT_EQ(pSW->tSma->indexUid, indexUid1);
  ASSERT_EQ(pSW->tSma->tableUid, tbUid);
  ASSERT_STRCASEEQ((pSW->tSma + 1)->indexName, smaIndexName2);
C
Cary Xu 已提交
255
  ASSERT_EQ((pSW->tSma + 1)->timezoneInt, timezone);
C
Cary Xu 已提交
256 257 258 259
  ASSERT_STRCASEEQ((pSW->tSma + 1)->expr, expr);
  ASSERT_STRCASEEQ((pSW->tSma + 1)->tagsFilter, tagsFilter);
  ASSERT_EQ((pSW->tSma + 1)->indexUid, indexUid2);
  ASSERT_EQ((pSW->tSma + 1)->tableUid, tbUid);
C
Cary Xu 已提交
260

C
Cary Xu 已提交
261
  tdDestroyTSmaWrapper(pSW);
wafwerar's avatar
wafwerar 已提交
262
  taosMemoryFreeClear(pSW);
C
Cary Xu 已提交
263 264 265 266 267 268 269 270

  // 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 已提交
271
  ASSERT_EQ(taosArrayGetSize(pUids), 1);
C
Cary Xu 已提交
272
  taosArrayDestroy(pUids);
C
Cary Xu 已提交
273

C
Cary Xu 已提交
274
  // resource release
C
Cary Xu 已提交
275 276
  metaRemoveSmaFromDb(pMeta, indexUid1);
  metaRemoveSmaFromDb(pMeta, indexUid2);
C
Cary Xu 已提交
277

C
Cary Xu 已提交
278
  tdDestroyTSma(&tSma);
C
Cary Xu 已提交
279
  metaClose(pMeta);
C
Cary Xu 已提交
280
}
C
Cary Xu 已提交
281
#endif
C
Cary Xu 已提交
282

C
Cary Xu 已提交
283
#if 1
C
Cary Xu 已提交
284 285
TEST(testCase, tSma_Data_Insert_Query_Test) {
  // step 1: prepare meta
C
Cary Xu 已提交
286
  const char    *smaIndexName1 = "sma_index_test_1";
C
Cary Xu 已提交
287
  const int8_t   timezone = 8;
C
Cary Xu 已提交
288 289 290
  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 已提交
291 292
  const tb_uid_t tbUid = 1234567890;
  const int64_t  indexUid1 = 2000000001;
C
Cary Xu 已提交
293
  const int64_t  interval1 = 1;
C
Cary Xu 已提交
294
  const int8_t   intervalUnit1 = TIME_UNIT_DAY;
C
Cary Xu 已提交
295
  const uint32_t nCntTSma = 2;
C
Cary Xu 已提交
296 297 298
  TSKEY          skey1 = 1646987196;
  const int64_t  testSmaData1 = 100;
  const int64_t  testSmaData2 = 200;
C
Cary Xu 已提交
299 300 301
  // encode
  STSma tSma = {0};
  tSma.version = 0;
C
Cary Xu 已提交
302
  tSma.intervalUnit = TIME_UNIT_DAY;
C
Cary Xu 已提交
303
  tSma.interval = 1;
C
Cary Xu 已提交
304
  tSma.slidingUnit = TIME_UNIT_HOUR;
C
Cary Xu 已提交
305
  tSma.sliding = 1;  // sliding = interval when it's convert window
C
Cary Xu 已提交
306 307
  tSma.indexUid = indexUid1;
  tstrncpy(tSma.indexName, smaIndexName1, TSDB_INDEX_NAME_LEN);
C
Cary Xu 已提交
308
  tSma.timezoneInt = timezone;
C
Cary Xu 已提交
309 310 311
  tSma.tableUid = tbUid;

  tSma.exprLen = strlen(expr);
wafwerar's avatar
wafwerar 已提交
312
  tSma.expr = (char *)taosMemoryCalloc(1, tSma.exprLen + 1);
C
Cary Xu 已提交
313
  ASSERT_NE(tSma.expr, nullptr);
C
Cary Xu 已提交
314 315 316
  tstrncpy(tSma.expr, expr, tSma.exprLen + 1);

  tSma.tagsFilterLen = strlen(tagsFilter);
wafwerar's avatar
wafwerar 已提交
317
  tSma.tagsFilter = (char *)taosMemoryCalloc(1, tSma.tagsFilterLen + 1);
C
Cary Xu 已提交
318
  ASSERT_NE(tSma.tagsFilter, nullptr);
C
Cary Xu 已提交
319 320
  tstrncpy(tSma.tagsFilter, tagsFilter, tSma.tagsFilterLen + 1);

C
Cary Xu 已提交
321 322
  SMeta          *pMeta = NULL;
  STSma          *pSmaCfg = &tSma;
C
Cary Xu 已提交
323 324 325 326 327 328 329
  const SMetaCfg *pMetaCfg = &defaultMetaOptions;

  taosRemoveDir(smaTestDir);

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

C
Cary Xu 已提交
332
  // step 2: insert data
C
Cary Xu 已提交
333
  STSmaDataWrapper *pSmaData = NULL;
C
Cary Xu 已提交
334 335
  STsdb            *pTsdb = (STsdb *)taosMemoryCalloc(1, sizeof(STsdb));
  STsdbCfg         *pCfg = &pTsdb->config;
336 337 338 339 340 341 342 343 344 345 346 347

  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 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361
    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 已提交
362 363 364
  SDiskCfg pDisks = {0};
  pDisks.level = 0;
  pDisks.primary = 1;
C
Cary Xu 已提交
365 366
  strncpy(pDisks.dir, "/var/lib/taos", TSDB_FILENAME_LEN);
  int32_t numOfDisks = 1;
367 368
  pTsdb->pTfs = tfsOpen(&pDisks, numOfDisks);
  ASSERT_NE(pTsdb->pTfs, nullptr);
C
Cary Xu 已提交
369

C
Cary Xu 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
  // 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;

  SSubmitReq *pMsg = (SSubmitReq *)calloc(1, msgLen);
  ASSERT_NE(pMsg, nullptr);
  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;
    }
  }

  ASSERT_EQ(tdScanAndConvertSubmitMsg(pMsg), TSDB_CODE_SUCCESS);

  ASSERT_EQ(tsdbUpdateSmaWindow(pTsdb, (const char *)pMsg), 0);
C
Cary Xu 已提交
407 408

  // init
C
Cary Xu 已提交
409
  int32_t allocCnt = 0;
C
Cary Xu 已提交
410 411
  int32_t allocStep = 16384;
  int32_t buffer = 1024;
C
Cary Xu 已提交
412
  void   *buf = NULL;
C
Cary Xu 已提交
413
  ASSERT_EQ(tsdbMakeRoom(&buf, allocStep), 0);
C
Cary Xu 已提交
414
  int32_t  bufSize = taosTSizeof(buf);
C
Cary Xu 已提交
415 416
  int32_t  numOfTables = 10;
  col_id_t numOfCols = 4096;
C
Cary Xu 已提交
417
  ASSERT_GT(numOfCols, 0);
C
Cary Xu 已提交
418 419 420

  pSmaData = (STSmaDataWrapper *)buf;
  printf(">> allocate [%d] time to %d and addr is %p\n", ++allocCnt, bufSize, pSmaData);
C
Cary Xu 已提交
421 422 423
  pSmaData->skey = skey1;
  pSmaData->interval = interval1;
  pSmaData->intervalUnit = intervalUnit1;
424
  pSmaData->indexUid = indexUid1;
C
Cary Xu 已提交
425 426 427 428

  int32_t len = sizeof(STSmaDataWrapper);
  for (int32_t t = 0; t < numOfTables; ++t) {
    STSmaTbData *pTbData = (STSmaTbData *)POINTER_SHIFT(pSmaData, len);
C
Cary Xu 已提交
429
    pTbData->tableUid = tbUid + t;
C
Cary Xu 已提交
430 431 432 433

    int32_t tableDataLen = sizeof(STSmaTbData);
    for (col_id_t c = 0; c < numOfCols; ++c) {
      if (bufSize - len - tableDataLen < buffer) {
C
Cary Xu 已提交
434
        ASSERT_EQ(tsdbMakeRoom(&buf, bufSize + allocStep), 0);
C
Cary Xu 已提交
435 436 437 438 439 440 441
        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 已提交
442

C
Cary Xu 已提交
443
      // TODO: fill col data
C
Cary Xu 已提交
444 445 446 447 448 449 450 451 452
      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 已提交
453 454 455 456 457
      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 已提交
458
  }
C
Cary Xu 已提交
459 460
  pSmaData->dataLen = (len - sizeof(STSmaDataWrapper));

C
Cary Xu 已提交
461
  ASSERT_GE(bufSize, pSmaData->dataLen);
C
Cary Xu 已提交
462 463

  // execute
464
  ASSERT_EQ(tsdbInsertTSmaData(pTsdb, (char *)pSmaData), TSDB_CODE_SUCCESS);
C
Cary Xu 已提交
465

C
Cary Xu 已提交
466 467 468 469
  // step 3: query
  uint32_t checkDataCnt = 0;
  for (int32_t t = 0; t < numOfTables; ++t) {
    for (col_id_t c = 0; c < numOfCols; ++c) {
470
      ASSERT_EQ(tsdbGetTSmaData(pTsdb, NULL, indexUid1, interval1, intervalUnit1, tbUid + t,
C
Cary Xu 已提交
471 472 473 474 475
                                c + PRIMARYKEY_TIMESTAMP_COL_ID, skey1, 1),
                TSDB_CODE_SUCCESS);
      ++checkDataCnt;
    }
  }
C
Cary Xu 已提交
476

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

C
Cary Xu 已提交
479
  // release data
C
Cary Xu 已提交
480
  taosMemoryFreeClear(pMsg);
C
Cary Xu 已提交
481
  taosTZfree(buf);
C
Cary Xu 已提交
482 483
  // release meta
  tdDestroyTSma(&tSma);
484 485
  tfsClose(pTsdb->pTfs);
  tsdbClose(pTsdb);
C
Cary Xu 已提交
486
  metaClose(pMeta);
C
Cary Xu 已提交
487
}
C
Cary Xu 已提交
488

C
Cary Xu 已提交
489 490 491
#endif

#pragma GCC diagnostic pop