tsdbTests.cpp 9.0 KB
Newer Older
H
hzcheng 已提交
1 2
#include <gtest/gtest.h>
#include <stdlib.h>
H
TD-34  
hzcheng 已提交
3
#include <sys/time.h>
H
hzcheng 已提交
4

S
slguan 已提交
5
#include "tdataformat.h"
H
TD-34  
hzcheng 已提交
6
#include "tsdbMain.h"
H
TD-100  
hzcheng 已提交
7
#include "tskiplist.h"
H
TD-27  
hzcheng 已提交
8

H
TD-100  
hzcheng 已提交
9
static double getCurTime() {
H
TD-34  
hzcheng 已提交
10 11 12 13 14
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec + tv.tv_usec * 1E-6;
}

H
TD-100  
hzcheng 已提交
15
typedef struct {
H
TD-353  
Hongze Cheng 已提交
16
  TSDB_REPO_T *pRepo;
H
TD-100  
hzcheng 已提交
17 18
  bool       isAscend;
  int        tid;
19
  uint64_t   uid;
H
TD-100  
hzcheng 已提交
20 21 22 23 24 25
  int        sversion;
  TSKEY      startTime;
  TSKEY      interval;
  int        totalRows;
  int        rowsPerSubmit;
  STSchema * pSchema;
H
TD-100  
hzcheng 已提交
26 27 28 29
} SInsertInfo;

static int insertData(SInsertInfo *pInfo) {
  SSubmitMsg *pMsg =
H
TD-166  
hzcheng 已提交
30
      (SSubmitMsg *)malloc(sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + dataRowMaxBytesFromSchema(pInfo->pSchema) * pInfo->rowsPerSubmit);
H
TD-100  
hzcheng 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  if (pMsg == NULL) return -1;
  TSKEY start_time = pInfo->startTime;

  // Loop to write data
  double stime = getCurTime();

  for (int k = 0; k < pInfo->totalRows/pInfo->rowsPerSubmit; k++) {
    memset((void *)pMsg, 0, sizeof(SSubmitMsg));
    SSubmitBlk *pBlock = pMsg->blocks;
    pBlock->uid = pInfo->uid;
    pBlock->tid = pInfo->tid;
    pBlock->sversion = pInfo->sversion;
    pBlock->len = 0;
    for (int i = 0; i < pInfo->rowsPerSubmit; i++) {
      // start_time += 1000;
H
TD-100  
hzcheng 已提交
46 47 48 49 50
      if (pInfo->isAscend) {
        start_time += pInfo->interval;
      } else {
        start_time -= pInfo->interval;
      }
H
TD-100  
hzcheng 已提交
51 52 53 54
      SDataRow row = (SDataRow)(pBlock->data + pBlock->len);
      tdInitDataRow(row, pInfo->pSchema);

      for (int j = 0; j < schemaNCols(pInfo->pSchema); j++) {
H
TD-166  
hzcheng 已提交
55
        STColumn *pTCol = schemaColAt(pInfo->pSchema, j);
H
TD-100  
hzcheng 已提交
56
        if (j == 0) {  // Just for timestamp
H
TD-166  
hzcheng 已提交
57
          tdAppendColVal(row, (void *)(&start_time), pTCol->type, pTCol->bytes, pTCol->offset);
H
TD-100  
hzcheng 已提交
58 59
        } else {  // For int
          int val = 10;
H
TD-166  
hzcheng 已提交
60
          tdAppendColVal(row, (void *)(&val), pTCol->type, pTCol->bytes, pTCol->offset);
H
TD-100  
hzcheng 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        }
      }
      pBlock->len += dataRowLen(row);
    }
    pMsg->length = pMsg->length + sizeof(SSubmitBlk) + pBlock->len;
    pMsg->numOfBlocks = 1;

    pBlock->len = htonl(pBlock->len);
    pBlock->numOfRows = htonl(pBlock->numOfRows);
    pBlock->uid = htobe64(pBlock->uid);
    pBlock->tid = htonl(pBlock->tid);

    pBlock->sversion = htonl(pBlock->sversion);
    pBlock->padding = htonl(pBlock->padding);

    pMsg->length = htonl(pMsg->length);
    pMsg->numOfBlocks = htonl(pMsg->numOfBlocks);
    pMsg->compressed = htonl(pMsg->numOfBlocks);

    if (tsdbInsertData(pInfo->pRepo, pMsg) < 0) {
      tfree(pMsg);
      return -1;
    }
  }

  double etime = getCurTime();

  printf("Spent %f seconds to write %d records\n", etime - stime, pInfo->totalRows);
  tfree(pMsg);
  return 0;
}

H
TD-34  
hzcheng 已提交
93
TEST(TsdbTest, DISABLED_tableEncodeDecode) {
H
TD-34  
hzcheng 已提交
94
// TEST(TsdbTest, tableEncodeDecode) {
H
TD-27  
hzcheng 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108
  STable *pTable = (STable *)malloc(sizeof(STable));

  pTable->type = TSDB_NORMAL_TABLE;
  pTable->tableId.uid = 987607499877672L;
  pTable->tableId.tid = 0;
  pTable->superUid = -1;
  pTable->sversion = 0;
  pTable->tagSchema = NULL;
  pTable->tagVal = NULL;
  int nCols = 5;
  STSchema *schema = tdNewSchema(nCols);

  for (int i = 0; i < nCols; i++) {
    if (i == 0) {
H
TD-166  
hzcheng 已提交
109
      tdSchemaAddCol(schema, TSDB_DATA_TYPE_TIMESTAMP, i, -1);
H
TD-27  
hzcheng 已提交
110
    } else {
H
TD-166  
hzcheng 已提交
111
      tdSchemaAddCol(schema, TSDB_DATA_TYPE_INT, i, -1);
H
TD-27  
hzcheng 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    }
  }

  pTable->schema = schema;

  int bufLen = 0;
  void *buf = tsdbEncodeTable(pTable, &bufLen);

  STable *tTable = tsdbDecodeTable(buf, bufLen);

  ASSERT_EQ(pTable->type, tTable->type);
  ASSERT_EQ(pTable->tableId.uid, tTable->tableId.uid);
  ASSERT_EQ(pTable->tableId.tid, tTable->tableId.tid);
  ASSERT_EQ(pTable->superUid, tTable->superUid);
  ASSERT_EQ(pTable->sversion, tTable->sversion);
  ASSERT_EQ(memcmp(pTable->schema, tTable->schema, sizeof(STSchema) + sizeof(STColumn) * nCols), 0);
}
H
hzcheng 已提交
129

H
nothing  
hzcheng 已提交
130 131
// TEST(TsdbTest, DISABLED_createRepo) {
TEST(TsdbTest, createRepo) {
H
TD-100  
hzcheng 已提交
132
  STsdbCfg config;
H
TD-100  
hzcheng 已提交
133
  STsdbRepo *repo;
H
TD-34  
hzcheng 已提交
134

H
TD-100  
hzcheng 已提交
135 136 137 138
  // 1. Create a tsdb repository
  tsdbSetDefaultCfg(&config);
  ASSERT_EQ(tsdbCreateRepo("/home/ubuntu/work/ttest/vnode0", &config, NULL), 0);

H
TD-353  
Hongze Cheng 已提交
139
  TSDB_REPO_T *pRepo = tsdbOpenRepo("/home/ubuntu/work/ttest/vnode0", NULL);
H
TD-100  
hzcheng 已提交
140 141 142 143 144 145
  ASSERT_NE(pRepo, nullptr);

  // 2. Create a normal table
  STableCfg tCfg;
  ASSERT_EQ(tsdbInitTableCfg(&tCfg, TSDB_SUPER_TABLE, 987607499877672L, 0), -1);
  ASSERT_EQ(tsdbInitTableCfg(&tCfg, TSDB_NORMAL_TABLE, 987607499877672L, 0), 0);
H
nothing  
hzcheng 已提交
146
  tsdbTableSetName(&tCfg, "test", false);
H
TD-100  
hzcheng 已提交
147 148 149 150 151 152

  int       nCols = 5;
  STSchema *schema = tdNewSchema(nCols);

  for (int i = 0; i < nCols; i++) {
    if (i == 0) {
H
TD-166  
hzcheng 已提交
153
      tdSchemaAddCol(schema, TSDB_DATA_TYPE_TIMESTAMP, i, -1);
H
TD-100  
hzcheng 已提交
154
    } else {
H
TD-166  
hzcheng 已提交
155
      tdSchemaAddCol(schema, TSDB_DATA_TYPE_INT, i, -1);
H
TD-100  
hzcheng 已提交
156 157 158 159 160 161 162
    }
  }

  tsdbTableSetSchema(&tCfg, schema, true);

  tsdbCreateTable(pRepo, &tCfg);

H
TD-100  
hzcheng 已提交
163 164 165
  // Insert Some Data
  SInsertInfo iInfo = {
    .pRepo = pRepo,
H
TD-100  
hzcheng 已提交
166 167
    // .isAscend = true,
    .isAscend = false,
H
TD-100  
hzcheng 已提交
168 169 170 171 172
    .tid = tCfg.tableId.tid,
    .uid = tCfg.tableId.uid,
    .sversion = tCfg.sversion,
    .startTime = 1584081000000,
    .interval = 1000,
H
TD-100  
hzcheng 已提交
173
    .totalRows = 10000000,
H
TD-100  
hzcheng 已提交
174 175 176 177 178 179 180 181
    .rowsPerSubmit = 1,
    .pSchema = schema
  };

  ASSERT_EQ(insertData(&iInfo), 0);

  // Close the repository
  tsdbCloseRepo(pRepo);
H
TD-100  
hzcheng 已提交
182

H
TD-100  
hzcheng 已提交
183 184 185 186
  // Open the repository again
  pRepo = tsdbOpenRepo("/home/ubuntu/work/ttest/vnode0", NULL);
  repo = (STsdbRepo *)pRepo;
  ASSERT_NE(pRepo, nullptr);
H
TD-100  
hzcheng 已提交
187

H
TD-100  
hzcheng 已提交
188 189 190 191 192
  // // Insert more data
  // iInfo.startTime = iInfo.startTime + iInfo.interval * iInfo.totalRows;
  // iInfo.totalRows = 10;
  // iInfo.pRepo = pRepo;
  // ASSERT_EQ(insertData(&iInfo), 0);
H
TD-100  
hzcheng 已提交
193

H
TD-100  
hzcheng 已提交
194 195
  // // Close the repository
  // tsdbCloseRepo(pRepo);
H
TD-100  
hzcheng 已提交
196

H
TD-100  
hzcheng 已提交
197 198 199 200
  // // Open the repository again
  // pRepo = tsdbOpenRepo("/home/ubuntu/work/ttest/vnode0", NULL);
  // repo = (STsdbRepo *)pRepo;
  // ASSERT_NE(pRepo, nullptr);
H
TD-100  
hzcheng 已提交
201

H
TD-100  
hzcheng 已提交
202 203 204
  // // Read from file
  // SRWHelper rhelper;
  // tsdbInitReadHelper(&rhelper, repo);
H
TD-100  
hzcheng 已提交
205

H
TD-100  
hzcheng 已提交
206 207 208
  // SFileGroup *pFGroup = tsdbSearchFGroup(repo->tsdbFileH, 1833);
  // ASSERT_NE(pFGroup, nullptr);
  // ASSERT_GE(tsdbSetAndOpenHelperFile(&rhelper, pFGroup), 0);
H
TD-100  
hzcheng 已提交
209

H
TD-100  
hzcheng 已提交
210 211 212
  // STable *pTable = tsdbGetTableByUid(repo->tsdbMeta, tCfg.tableId.uid);
  // ASSERT_NE(pTable, nullptr);
  // tsdbSetHelperTable(&rhelper, pTable, repo);
H
TD-100  
hzcheng 已提交
213

H
TD-100  
hzcheng 已提交
214 215
  // ASSERT_EQ(tsdbLoadCompInfo(&rhelper, NULL), 0);
  // ASSERT_EQ(tsdbLoadBlockData(&rhelper, blockAtIdx(&rhelper, 0), NULL), 0);
H
TD-100  
hzcheng 已提交
216 217

  int k = 0;
H
hzcheng 已提交
218 219
}

H
TD-100  
hzcheng 已提交
220 221
TEST(TsdbTest, DISABLED_openRepo) {
// TEST(TsdbTest, openRepo) {
H
TD-100  
hzcheng 已提交
222 223
  // tsdb_repo_t *repo = tsdbOpenRepo("/home/ubuntu/work/build/test/data/vnode/vnode2/tsdb", NULL);
  // ASSERT_NE(repo, nullptr);
H
TD-34  
hzcheng 已提交
224

H
TD-100  
hzcheng 已提交
225
  // STsdbRepo *pRepo = (STsdbRepo *)repo;
H
TD-34  
hzcheng 已提交
226

H
TD-100  
hzcheng 已提交
227
  // SFileGroup *pGroup = tsdbSearchFGroup(pRepo->tsdbFileH, 1655);
H
TD-34  
hzcheng 已提交
228

H
hzcheng 已提交
229 230 231
//   for (int type = TSDB_FILE_TYPE_HEAD; type < TSDB_FILE_TYPE_MAX; type++) {
//     tsdbOpenFile(&pGroup->files[type], O_RDONLY);
//   }
H
TD-34  
hzcheng 已提交
232

H
hzcheng 已提交
233 234
//   SCompIdx *pIdx = (SCompIdx *)calloc(pRepo->config.maxTables, sizeof(SCompIdx));
//   tsdbLoadCompIdx(pGroup, (void *)pIdx, pRepo->config.maxTables);
H
TD-34  
hzcheng 已提交
235

H
hzcheng 已提交
236
//   SCompInfo *pCompInfo = (SCompInfo *)malloc(sizeof(SCompInfo) + pIdx[1].len);
H
TD-34  
hzcheng 已提交
237

H
TD-100  
hzcheng 已提交
238
  // tsdbLoadCompBlocks(pGroup, &pIdx[1], (void *)pCompInfo);
H
TD-34  
hzcheng 已提交
239

H
hzcheng 已提交
240 241
//   int blockIdx = 0;
//   SCompBlock *pBlock = &(pCompInfo->blocks[blockIdx]);
H
TD-34  
hzcheng 已提交
242

H
hzcheng 已提交
243
//   SCompData *pCompData = (SCompData *)malloc(sizeof(SCompData) + sizeof(SCompCol) * pBlock->numOfCols);
H
TD-34  
hzcheng 已提交
244

H
hzcheng 已提交
245
//   tsdbLoadCompCols(&pGroup->files[TSDB_FILE_TYPE_DATA], pBlock, (void *)pCompData);
H
TD-34  
hzcheng 已提交
246

H
TD-100  
hzcheng 已提交
247
  // STable *pTable = tsdbGetTableByUid(pRepo->tsdbMeta, pCompData->uid);
H
TD-166  
hzcheng 已提交
248
  // SDataCols *pDataCols = tdNewDataCols(tdMaxRowBytesFromSchema(tsdbGetTableSchema(pRepo->tsdbMeta, pTable)), 5);
H
TD-100  
hzcheng 已提交
249
  // tdInitDataCols(pDataCols, tsdbGetTableSchema(pRepo->tsdbMeta, pTable));
H
TD-34  
hzcheng 已提交
250

H
hzcheng 已提交
251
//   tsdbLoadDataBlock(&pGroup->files[TSDB_FILE_TYPE_DATA], pBlock, 1, pDataCols, pCompData);
H
TD-34  
hzcheng 已提交
252

H
TD-100  
hzcheng 已提交
253
  // tdResetDataCols(pDataCols);
H
hzcheng 已提交
254

H
TD-100  
hzcheng 已提交
255
  // tsdbLoadDataBlock(&pGroup->files[TSDB_FILE_TYPE_DATA], pBlock + 1, 1, pDataCols, pCompData);
H
hzcheng 已提交
256

H
TD-34  
hzcheng 已提交
257

H
hzcheng 已提交
258
//   int k = 0;
H
TD-34  
hzcheng 已提交
259

H
TD-100  
hzcheng 已提交
260
}
H
TD-34  
hzcheng 已提交
261

H
TD-34  
hzcheng 已提交
262
TEST(TsdbTest, DISABLED_createFileGroup) {
H
TD-34  
hzcheng 已提交
263 264
  SFileGroup fGroup;

H
TD-34  
hzcheng 已提交
265
  // ASSERT_EQ(tsdbCreateFileGroup("/home/ubuntu/work/ttest/vnode0/data", 1820, &fGroup, 1000), 0);
H
TD-34  
hzcheng 已提交
266 267

  int k = 0;
H
TD-100  
hzcheng 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

static char *getTKey(const void *data) {
  return (char *)data;
}

static void insertSkipList(bool isAscend) {
  TSKEY start_time = 1587393453000;
  TSKEY interval = 1000;

  SSkipList *pList = tSkipListCreate(5, TSDB_DATA_TYPE_TIMESTAMP, sizeof(TSKEY), 0, 0, 1, getTKey);
  ASSERT_NE(pList, nullptr);

  for (size_t i = 0; i < 20000000; i++)
  {
    TSKEY time = isAscend ? (start_time + i * interval) : (start_time - i * interval);
    int32_t level = 0;
    int32_t headSize = 0;

    tSkipListNewNodeInfo(pList, &level, &headSize);
    SSkipListNode *pNode = (SSkipListNode *)malloc(headSize + sizeof(TSKEY));
    ASSERT_NE(pNode, nullptr);
    pNode->level = level;
    *(TSKEY *)((char *)pNode + headSize) = time;
    tSkipListPut(pList, pNode);
  }

  tSkipListDestroy(pList);
}

H
nothing  
hzcheng 已提交
298 299
TEST(TsdbTest, DISABLED_testSkipList) {
// TEST(TsdbTest, testSkipList) {
H
TD-100  
hzcheng 已提交
300 301 302 303 304 305 306 307 308 309 310
  double stime = getCurTime();
  insertSkipList(true);
  double etime = getCurTime();

  printf("Time used to insert 100000000 records takes %f seconds\n", etime-stime);

  stime = getCurTime();
  insertSkipList(false);
  etime = getCurTime();

  printf("Time used to insert 100000000 records takes %f seconds\n", etime-stime);
H
TD-27  
hzcheng 已提交
311
}