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

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

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 36 37 38 39 40 41 42 43 44
int main(int argc, char **argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

TEST(testCase, tSmaEncodeDecodeTest) {
  // encode
  STSma tSma = {0};
  tSma.version = 0;
  tSma.intervalUnit = TD_TIME_UNIT_DAY;
  tSma.interval = 1;
  tSma.slidingUnit = TD_TIME_UNIT_HOUR;
  tSma.sliding = 0;
  tstrncpy(tSma.indexName, "sma_index_test", TSDB_INDEX_NAME_LEN);
C
Cary Xu 已提交
45
  tstrncpy(tSma.timezone, "Asia/Shanghai", TD_TIMEZONE_LEN);
C
Cary Xu 已提交
46
  tSma.tableUid = 1234567890;
C
Cary Xu 已提交
47 48 49 50 51 52 53 54 55 56 57 58
  tSma.nFuncColIds = 5;
  tSma.funcColIds = (SFuncColIds *)calloc(tSma.nFuncColIds, sizeof(SFuncColIds));
  ASSERT(tSma.funcColIds != NULL);
  for (int32_t n = 0; n < tSma.nFuncColIds; ++n) {
    SFuncColIds *funcColIds = tSma.funcColIds + n;
    funcColIds->funcId = n;
    funcColIds->nColIds = 10;
    funcColIds->colIds = (col_id_t *)calloc(funcColIds->nColIds, sizeof(col_id_t));
    ASSERT(funcColIds->colIds != NULL);
    for (int32_t i = 0; i < funcColIds->nColIds; ++i) {
      *(funcColIds->colIds + i) = (i + PRIMARYKEY_TIMESTAMP_COL_ID);
    }
C
Cary Xu 已提交
59
  }
C
Cary Xu 已提交
60

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

C
Cary Xu 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  void *buf = calloc(bufLen, 1);
  assert(buf != NULL);

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

  EXPECT_EQ(len, bufLen);

  // decode
  STSmaWrapper dstTSmaWrapper = {0};
  void *       result = tDecodeTSmaWrapper(pSW, &dstTSmaWrapper);
  assert(result != NULL);

  EXPECT_EQ(tSmaWrapper.number, dstTSmaWrapper.number);

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

    EXPECT_EQ(pSma->version, qSma->version);
    EXPECT_EQ(pSma->intervalUnit, qSma->intervalUnit);
    EXPECT_EQ(pSma->slidingUnit, qSma->slidingUnit);
    EXPECT_STRCASEEQ(pSma->indexName, qSma->indexName);
C
Cary Xu 已提交
87 88
    EXPECT_STRCASEEQ(pSma->timezone, qSma->timezone);
    EXPECT_EQ(pSma->nFuncColIds, qSma->nFuncColIds);
C
Cary Xu 已提交
89 90 91
    EXPECT_EQ(pSma->tableUid, qSma->tableUid);
    EXPECT_EQ(pSma->interval, qSma->interval);
    EXPECT_EQ(pSma->sliding, qSma->sliding);
C
Cary Xu 已提交
92 93 94 95 96 97 98 99 100 101
    EXPECT_EQ(pSma->tagsFilterLen, qSma->tagsFilterLen);
    EXPECT_STRCASEEQ(pSma->tagsFilter, qSma->tagsFilter);
    for (uint32_t j = 0; j < pSma->nFuncColIds; ++j) {
      SFuncColIds *pFuncColIds = pSma->funcColIds + j;
      SFuncColIds *qFuncColIds = qSma->funcColIds + j;
      EXPECT_EQ(pFuncColIds->funcId, qFuncColIds->funcId);
      EXPECT_EQ(pFuncColIds->nColIds, qFuncColIds->nColIds);
      for (uint32_t k = 0; k < pFuncColIds->nColIds; ++k) {
        EXPECT_EQ(*(pFuncColIds->colIds + k), *(qFuncColIds->colIds + k));
      }
C
Cary Xu 已提交
102 103 104 105
    }
  }

  // resource release
C
Cary Xu 已提交
106 107
  tdDestroyTSma(&tSma);
  tdDestroyTSmaWrapper(&dstTSmaWrapper);
C
Cary Xu 已提交
108 109 110
}

TEST(testCase, tSma_DB_Put_Get_Del_Test) {
C
Cary Xu 已提交
111 112 113 114 115
  const char *   smaIndexName1 = "sma_index_test_1";
  const char *   smaIndexName2 = "sma_index_test_2";
  const char *   timeZone = "Asia/Shanghai";
  const char *   tagsFilter = "I'm tags filter";
  const char *   smaTestDir = "./smaTest";
C
Cary Xu 已提交
116
  const uint64_t tbUid = 1234567890;
C
Cary Xu 已提交
117
  const uint32_t nCntTSma = 2;
C
Cary Xu 已提交
118 119 120 121 122 123 124 125
  // encode
  STSma tSma = {0};
  tSma.version = 0;
  tSma.intervalUnit = TD_TIME_UNIT_DAY;
  tSma.interval = 1;
  tSma.slidingUnit = TD_TIME_UNIT_HOUR;
  tSma.sliding = 0;
  tstrncpy(tSma.indexName, smaIndexName1, TSDB_INDEX_NAME_LEN);
C
Cary Xu 已提交
126
  tstrncpy(tSma.timezone, timeZone, TD_TIMEZONE_LEN);
C
Cary Xu 已提交
127
  tSma.tableUid = tbUid;
C
Cary Xu 已提交
128 129 130 131 132 133 134 135 136 137 138 139
  tSma.nFuncColIds = 5;
  tSma.funcColIds = (SFuncColIds *)calloc(tSma.nFuncColIds, sizeof(SFuncColIds));
  ASSERT(tSma.funcColIds != NULL);
  for (int32_t n = 0; n < tSma.nFuncColIds; ++n) {
    SFuncColIds *funcColIds = tSma.funcColIds + n;
    funcColIds->funcId = n;
    funcColIds->nColIds = 10;
    funcColIds->colIds = (col_id_t *)calloc(funcColIds->nColIds, sizeof(col_id_t));
    ASSERT(funcColIds->colIds != NULL);
    for (int32_t i = 0; i < funcColIds->nColIds; ++i) {
      *(funcColIds->colIds + i) = (i + PRIMARYKEY_TIMESTAMP_COL_ID);
    }
C
Cary Xu 已提交
140
  }
C
Cary Xu 已提交
141 142 143
  tSma.tagsFilterLen = strlen(tagsFilter);
  tSma.tagsFilter = (char *)calloc(tSma.tagsFilterLen + 1, 1);
  tstrncpy(tSma.tagsFilter, tagsFilter, tSma.tagsFilterLen + 1);
C
Cary Xu 已提交
144 145

  SMeta *         pMeta = NULL;
C
Cary Xu 已提交
146
  STSma *         pSmaCfg = &tSma;
C
Cary Xu 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  const SMetaCfg *pMetaCfg = &defaultMetaOptions;

  taosRemoveDir(smaTestDir);

  pMeta = metaOpen(smaTestDir, pMetaCfg, NULL);
  assert(pMeta != NULL);
  // save index 1
  metaSaveSmaToDB(pMeta, pSmaCfg);

  tstrncpy(pSmaCfg->indexName, smaIndexName2, TSDB_INDEX_NAME_LEN);
  pSmaCfg->version = 1;
  pSmaCfg->intervalUnit = TD_TIME_UNIT_HOUR;
  pSmaCfg->interval = 1;
  pSmaCfg->slidingUnit = TD_TIME_UNIT_MINUTE;
  pSmaCfg->sliding = 5;

  // save index 2
  metaSaveSmaToDB(pMeta, pSmaCfg);

  // get value by indexName
C
Cary Xu 已提交
167
  STSma *qSmaCfg = NULL;
C
Cary Xu 已提交
168 169 170
  qSmaCfg = metaGetSmaInfoByName(pMeta, smaIndexName1);
  assert(qSmaCfg != NULL);
  printf("name1 = %s\n", qSmaCfg->indexName);
C
Cary Xu 已提交
171 172
  printf("timezone1 = %s\n", qSmaCfg->timezone);
  printf("tagsFilter1 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : "");
C
Cary Xu 已提交
173 174
  EXPECT_STRCASEEQ(qSmaCfg->indexName, smaIndexName1);
  EXPECT_EQ(qSmaCfg->tableUid, tSma.tableUid);
C
Cary Xu 已提交
175
  tdDestroyTSma(qSmaCfg);
C
Cary Xu 已提交
176
  tfree(qSmaCfg);
C
Cary Xu 已提交
177 178 179 180

  qSmaCfg = metaGetSmaInfoByName(pMeta, smaIndexName2);
  assert(qSmaCfg != NULL);
  printf("name2 = %s\n", qSmaCfg->indexName);
C
Cary Xu 已提交
181 182
  printf("timezone2 = %s\n", qSmaCfg->timezone);
  printf("tagsFilter2 = %s\n", qSmaCfg->tagsFilter != NULL ? qSmaCfg->tagsFilter : "");
C
Cary Xu 已提交
183 184
  EXPECT_STRCASEEQ(qSmaCfg->indexName, smaIndexName2);
  EXPECT_EQ(qSmaCfg->interval, tSma.interval);
C
Cary Xu 已提交
185
  tdDestroyTSma(qSmaCfg);
C
Cary Xu 已提交
186
  tfree(qSmaCfg);
C
Cary Xu 已提交
187

C
Cary Xu 已提交
188
  // get index name by table uid
C
Cary Xu 已提交
189 190 191 192
  SMSmaCursor *pSmaCur = metaOpenSmaCursor(pMeta, tbUid);
  assert(pSmaCur != NULL);
  uint32_t indexCnt = 0;
  while (1) {
C
Cary Xu 已提交
193
    const char *indexName = metaSmaCursorNext(pSmaCur);
C
Cary Xu 已提交
194 195 196 197 198 199
    if (indexName == NULL) {
      break;
    }
    printf("indexName = %s\n", indexName);
    ++indexCnt;
  }
C
Cary Xu 已提交
200
  EXPECT_EQ(indexCnt, nCntTSma);
C
Cary Xu 已提交
201 202
  metaCloseSmaCurosr(pSmaCur);

C
Cary Xu 已提交
203 204 205
  // get wrapper by table uid
  STSmaWrapper *pSW = metaGetSmaInfoByUid(pMeta, tbUid);
  assert(pSW != NULL);
C
Cary Xu 已提交
206
  EXPECT_EQ(pSW->number, nCntTSma);
C
Cary Xu 已提交
207
  EXPECT_STRCASEEQ(pSW->tSma->indexName, smaIndexName1);
C
Cary Xu 已提交
208 209
  EXPECT_STRCASEEQ(pSW->tSma->timezone, timeZone);
  EXPECT_STRCASEEQ(pSW->tSma->tagsFilter, tagsFilter);
C
Cary Xu 已提交
210 211
  EXPECT_EQ(pSW->tSma->tableUid, tSma.tableUid);
  EXPECT_STRCASEEQ((pSW->tSma + 1)->indexName, smaIndexName2);
C
Cary Xu 已提交
212 213
  EXPECT_STRCASEEQ((pSW->tSma + 1)->timezone, timeZone);
  EXPECT_STRCASEEQ((pSW->tSma + 1)->tagsFilter, tagsFilter);
C
Cary Xu 已提交
214
  EXPECT_EQ((pSW->tSma + 1)->tableUid, tSma.tableUid);
C
Cary Xu 已提交
215

C
Cary Xu 已提交
216 217 218 219 220 221 222 223 224 225 226 227
  tdDestroyTSmaWrapper(pSW);
  tfree(pSW);

  // 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));
  }
  EXPECT_EQ(taosArrayGetSize(pUids), 1);
  taosArrayDestroy(pUids);
C
Cary Xu 已提交
228

C
Cary Xu 已提交
229 230 231 232
  // resource release
  metaRemoveSmaFromDb(pMeta, smaIndexName1);
  metaRemoveSmaFromDb(pMeta, smaIndexName2);

C
Cary Xu 已提交
233
  tdDestroyTSma(&tSma);
C
Cary Xu 已提交
234
  metaClose(pMeta);
C
Cary Xu 已提交
235 236
}

C
Cary Xu 已提交
237
#if 0
C
Cary Xu 已提交
238
TEST(testCase, tSmaInsertTest) {
C
Cary Xu 已提交
239 240 241
  STSma      tSma = {0};
  STSmaData *pSmaData = NULL;
  STsdb      tsdb = {0};
C
Cary Xu 已提交
242 243 244 245 246 247 248 249

  // init
  tSma.intervalUnit = TD_TIME_UNIT_DAY;
  tSma.interval = 1;
  tSma.numOfFuncIds = 5;  // sum/min/max/avg/last

  int32_t blockSize = tSma.numOfFuncIds * sizeof(int64_t);
  int32_t numOfColIds = 3;
C
Cary Xu 已提交
250
  int32_t numOfBlocks = 10;
C
Cary Xu 已提交
251

C
Cary Xu 已提交
252
  int32_t dataLen = numOfColIds * numOfBlocks * blockSize;
C
Cary Xu 已提交
253

C
Cary Xu 已提交
254
  pSmaData = (STSmaData *)malloc(sizeof(STSmaData) + dataLen);
C
Cary Xu 已提交
255 256 257
  ASSERT_EQ(pSmaData != NULL, true);
  pSmaData->tableUid = 3232329230;
  pSmaData->numOfColIds = numOfColIds;
C
Cary Xu 已提交
258
  pSmaData->numOfBlocks = numOfBlocks;
C
Cary Xu 已提交
259 260 261
  pSmaData->dataLen = dataLen;
  pSmaData->tsWindow.skey = 1640000000;
  pSmaData->tsWindow.ekey = 1645788649;
C
Cary Xu 已提交
262
  pSmaData->colIds = (col_id_t *)malloc(sizeof(col_id_t) * numOfColIds);
C
Cary Xu 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  ASSERT_EQ(pSmaData->colIds != NULL, true);

  for (int32_t i = 0; i < numOfColIds; ++i) {
    *(pSmaData->colIds + i) = (i + PRIMARYKEY_TIMESTAMP_COL_ID);
  }

  // execute
  EXPECT_EQ(tsdbInsertTSmaData(&tsdb, &tSma, pSmaData), TSDB_CODE_SUCCESS);

  // release
  tdDestroySmaData(pSmaData);
}
#endif

#pragma GCC diagnostic pop