tsdbTests.cpp 3.2 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
TD-34  
hzcheng 已提交
6
#include "tsdbFile.h"
H
TD-27  
hzcheng 已提交
7 8 9 10 11 12 13 14 15 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
#include "tsdbMeta.h"

TEST(TsdbTest, tableEncodeDecode) {
  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);
  ASSERT_EQ(tTable->content.pData, nullptr);
}
H
hzcheng 已提交
45

H
hzcheng 已提交
46
TEST(TsdbTest, createRepo) {
H
hzcheng 已提交
47
  STsdbCfg config;
H
hzcheng 已提交
48

H
hzcheng 已提交
49
  // 1. Create a tsdb repository
H
hzcheng 已提交
50 51
  tsdbSetDefaultCfg(&config);
  tsdb_repo_t *pRepo = tsdbCreateRepo("/home/ubuntu/work/ttest/vnode0", &config, NULL);
H
hzcheng 已提交
52 53
  ASSERT_NE(pRepo, nullptr);

H
hzcheng 已提交
54 55 56
  // 2. Create a normal table
  STableCfg tCfg;
  ASSERT_EQ(tsdbInitTableCfg(&tCfg, TSDB_SUPER_TABLE, 987607499877672L, 0), -1);
S
[TD-10]  
slguan 已提交
57
  ASSERT_EQ(tsdbInitTableCfg(&tCfg, TSDB_NORMAL_TABLE, 987607499877672L, 0), 0);
H
hzcheng 已提交
58

H
hzcheng 已提交
59
  int       nCols = 5;
H
hzcheng 已提交
60 61
  STSchema *schema = tdNewSchema(nCols);

H
hzcheng 已提交
62
  for (int i = 0; i < nCols; i++) {
H
hzcheng 已提交
63 64 65 66 67 68
    if (i == 0) {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_TIMESTAMP, i, -1);
    } else {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_INT, i, -1);
    }
  }
H
hzcheng 已提交
69

H
hzcheng 已提交
70
  tsdbTableSetSchema(&tCfg, schema, true);
H
hzcheng 已提交
71

H
hzcheng 已提交
72
  tsdbCreateTable(pRepo, &tCfg);
H
hzcheng 已提交
73

H
hzcheng 已提交
74
  // // 3. Loop to write some simple data
H
more  
hzcheng 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87
  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 已提交
88
    for (int j = 0; j < schemaNCols(schema); j++) {
H
more  
hzcheng 已提交
89
      if (j == 0) { // Just for timestamp
H
hzcheng 已提交
90
        tdAppendColVal(row, (void *)(&ttime), schemaColAt(schema, j));
H
more  
hzcheng 已提交
91 92
      } else { // For int
        int val = 10;
H
fix bug  
hzcheng 已提交
93
        tdAppendColVal(row, (void *)(&val), schemaColAt(schema, j));
H
more  
hzcheng 已提交
94 95 96
      }

    }
H
hzcheng 已提交
97
    pBlock->len += dataRowLen(row);
H
more  
hzcheng 已提交
98 99

  }
H
hzcheng 已提交
100
  pMsg->length = pMsg->length + sizeof(SSubmitBlk) + pBlock->len;
H
more  
hzcheng 已提交
101 102

  tsdbInsertData(pRepo, pMsg);
H
hzcheng 已提交
103

H
TD-34  
hzcheng 已提交
104 105
  tsdbTriggerCommit(pRepo);

H
hzcheng 已提交
106 107
}

H
TD-27  
hzcheng 已提交
108 109 110
TEST(TsdbTest, openRepo) {
  tsdb_repo_t *pRepo = tsdbOpenRepo("/home/ubuntu/work/ttest/vnode0");
  ASSERT_NE(pRepo, nullptr);
H
TD-34  
hzcheng 已提交
111 112 113 114 115 116 117 118
}

TEST(TsdbTest, createFileGroup) {
  SFileGroup fGroup;

  ASSERT_EQ(tsdbCreateFileGroup("/home/ubuntu/work/ttest/vnode0/data", 1820, &fGroup, 1000), 0);

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