tsdbTests.cpp 5.6 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

H
hzcheng 已提交
5
#include "dataformat.h"
H
TD-34  
hzcheng 已提交
6
#include "tsdbMain.h"
H
TD-27  
hzcheng 已提交
7

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

H
TD-34  
hzcheng 已提交
14
TEST(TsdbTest, DISABLED_tableEncodeDecode) {
H
TD-34  
hzcheng 已提交
15
// TEST(TsdbTest, tableEncodeDecode) {
H
TD-27  
hzcheng 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  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) {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_TIMESTAMP, i, -1);
    } else {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_INT, i, -1);
    }
  }

  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 已提交
50

H
TD-100  
hzcheng 已提交
51 52 53
// TEST(TsdbTest, DISABLED_createRepo) {
TEST(TsdbTest, createRepo) {
  STsdbCfg config;
H
TD-34  
hzcheng 已提交
54

H
TD-100  
hzcheng 已提交
55 56 57 58 59 60 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  // 1. Create a tsdb repository
  tsdbSetDefaultCfg(&config);
  ASSERT_EQ(tsdbCreateRepo("/home/ubuntu/work/ttest/vnode0", &config, NULL), 0);

  tsdb_repo_t *pRepo = tsdbOpenRepo("/home/ubuntu/work/ttest/vnode0", NULL);
  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);

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

  for (int i = 0; i < nCols; i++) {
    if (i == 0) {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_TIMESTAMP, i, -1);
    } else {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_INT, i, -1);
    }
  }

  tsdbTableSetSchema(&tCfg, schema, true);

  tsdbCreateTable(pRepo, &tCfg);

  // // 3. Loop to write some simple data
  int nRows = 10000000;
  int rowsPerSubmit = 10;
  int64_t start_time = 1584081000000;

  SSubmitMsg *pMsg = (SSubmitMsg *)malloc(sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + tdMaxRowBytesFromSchema(schema) * rowsPerSubmit);

  double stime = getCurTime();

  for (int k = 0; k < nRows/rowsPerSubmit; k++) {
    memset((void *)pMsg, 0, sizeof(SSubmitMsg));
    SSubmitBlk *pBlock = pMsg->blocks;
    pBlock->uid = 987607499877672L;
    pBlock->tid = 0;
    pBlock->sversion = 0;
    pBlock->len = 0;
    for (int i = 0; i < rowsPerSubmit; i++) {
      // start_time += 1000;
      start_time += 1000;
      SDataRow row = (SDataRow)(pBlock->data + pBlock->len);
      tdInitDataRow(row, schema);

      for (int j = 0; j < schemaNCols(schema); j++) {
        if (j == 0) {  // Just for timestamp
          tdAppendColVal(row, (void *)(&start_time), schemaColAt(schema, j));
        } else {  // For int
          int val = 10;
          tdAppendColVal(row, (void *)(&val), schemaColAt(schema, j));
        }
      }
      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);

    tsdbInsertData(pRepo, pMsg);
  }

  double etime = getCurTime();

  void *ptr = malloc(150000);
  free(ptr);

  printf("Spent %f seconds to write %d records\n", etime - stime, nRows);

  tsdbCloseRepo(pRepo);
H
hzcheng 已提交
140 141
}

H
TD-100  
hzcheng 已提交
142 143
TEST(TsdbTest, DISABLED_openRepo) {
// TEST(TsdbTest, openRepo) {
H
TD-100  
hzcheng 已提交
144 145
  // tsdb_repo_t *repo = tsdbOpenRepo("/home/ubuntu/work/build/test/data/vnode/vnode2/tsdb", NULL);
  // ASSERT_NE(repo, nullptr);
H
TD-34  
hzcheng 已提交
146

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

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

H
hzcheng 已提交
151 152 153
//   for (int type = TSDB_FILE_TYPE_HEAD; type < TSDB_FILE_TYPE_MAX; type++) {
//     tsdbOpenFile(&pGroup->files[type], O_RDONLY);
//   }
H
TD-34  
hzcheng 已提交
154

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

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

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

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

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

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

H
TD-100  
hzcheng 已提交
169 170 171
  // STable *pTable = tsdbGetTableByUid(pRepo->tsdbMeta, pCompData->uid);
  // SDataCols *pDataCols = tdNewDataCols(tdMaxRowBytesFromSchema(tsdbGetTableSchema(pRepo->tsdbMeta, pTable)), 5, 10);
  // tdInitDataCols(pDataCols, tsdbGetTableSchema(pRepo->tsdbMeta, pTable));
H
TD-34  
hzcheng 已提交
172

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

H
TD-100  
hzcheng 已提交
175
  // tdResetDataCols(pDataCols);
H
hzcheng 已提交
176

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

H
TD-34  
hzcheng 已提交
179

H
hzcheng 已提交
180
//   int k = 0;
H
TD-34  
hzcheng 已提交
181

H
TD-100  
hzcheng 已提交
182
}
H
TD-34  
hzcheng 已提交
183

H
TD-34  
hzcheng 已提交
184
TEST(TsdbTest, DISABLED_createFileGroup) {
H
TD-34  
hzcheng 已提交
185 186
  SFileGroup fGroup;

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

  int k = 0;
H
TD-27  
hzcheng 已提交
190
}