tsdbTests.cpp 1.8 KB
Newer Older
H
hzcheng 已提交
1 2 3
#include <gtest/gtest.h>
#include <stdlib.h>

H
hzcheng 已提交
4
#include "tsdb.h"
H
hzcheng 已提交
5
#include "dataformat.h"
H
hzcheng 已提交
6

H
hzcheng 已提交
7
TEST(TsdbTest, createRepo) {
H
hzcheng 已提交
8
  STsdbCfg config;
H
hzcheng 已提交
9

H
hzcheng 已提交
10
  // 1. Create a tsdb repository
H
hzcheng 已提交
11 12
  tsdbSetDefaultCfg(&config);
  tsdb_repo_t *pRepo = tsdbCreateRepo("/home/ubuntu/work/ttest/vnode0", &config, NULL);
H
hzcheng 已提交
13 14
  ASSERT_NE(pRepo, nullptr);

H
hzcheng 已提交
15 16 17 18 19
  // 2. Create a normal table
  STableCfg tCfg;
  ASSERT_EQ(tsdbInitTableCfg(&tCfg, TSDB_SUPER_TABLE, 987607499877672L, 0), -1);
  ASSERT_EQ(tsdbInitTableCfg(&tCfg, TSDB_NTABLE, 987607499877672L, 0), 0);

H
hzcheng 已提交
20
  int       nCols = 5;
H
hzcheng 已提交
21 22
  STSchema *schema = tdNewSchema(nCols);

H
hzcheng 已提交
23
  for (int i = 0; i < nCols; i++) {
H
hzcheng 已提交
24 25 26 27 28 29
    if (i == 0) {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_TIMESTAMP, i, -1);
    } else {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_INT, i, -1);
    }
  }
H
hzcheng 已提交
30

H
hzcheng 已提交
31
  tsdbTableSetSchema(&tCfg, schema, true);
H
hzcheng 已提交
32

H
hzcheng 已提交
33
  tsdbCreateTable(pRepo, &tCfg);
H
hzcheng 已提交
34

H
hzcheng 已提交
35
  // // 3. Loop to write some simple data
H
more  
hzcheng 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
  int nRows = 10;
  SSubmitMsg *pMsg = (SSubmitMsg *)malloc(sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + tdMaxRowBytesFromSchema(schema) * nRows);

  SSubmitBlk *pBlock = pMsg->blocks;
  pBlock->tableId = {.uid = 987607499877672L, .tid = 0};
  pBlock->sversion = 0;
  pBlock->len = 0;
  int64_t start_time = 1584081000000;
  for (int i = 0; i < nRows; i++) {
    int64_t ttime = start_time + 1000 * i;
    SDataRow row = (SDataRow)(pBlock->data + pBlock->len);
    tdInitDataRow(row, schema);

H
fix bug  
hzcheng 已提交
49
    for (int j = 0; j < schemaNCols(schema); j++) {
H
more  
hzcheng 已提交
50
      if (j == 0) { // Just for timestamp
H
fix bug  
hzcheng 已提交
51
        tdAppendColVal(row, (void *)(&time), schemaColAt(schema, j));
H
more  
hzcheng 已提交
52 53
      } else { // For int
        int val = 10;
H
fix bug  
hzcheng 已提交
54
        tdAppendColVal(row, (void *)(&val), schemaColAt(schema, j));
H
more  
hzcheng 已提交
55 56 57
      }

    }
H
hzcheng 已提交
58
    pBlock->len += dataRowLen(row);
H
more  
hzcheng 已提交
59 60

  }
H
hzcheng 已提交
61
  pMsg->length = pMsg->length + sizeof(SSubmitBlk) + pBlock->len;
H
more  
hzcheng 已提交
62 63

  tsdbInsertData(pRepo, pMsg);
H
hzcheng 已提交
64 65
}