提交 0566dfbd 编写于 作者: H hzcheng

add more code

上级 7ae3eebe
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#define _TD_DATA_FORMAT_H_ #define _TD_DATA_FORMAT_H_
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include "schema.h" #include "schema.h"
...@@ -39,21 +40,28 @@ typedef void *SDataRow; ...@@ -39,21 +40,28 @@ typedef void *SDataRow;
#define dataRowTuple(r) ((char *)(r) + sizeof(int32_t)) #define dataRowTuple(r) ((char *)(r) + sizeof(int32_t))
#define dataRowSetLen(r, l) (dataRowLen(r) = (l)) #define dataRowSetLen(r, l) (dataRowLen(r) = (l))
#define dataRowIdx(r, i) ((char *)(r) + i) #define dataRowIdx(r, i) ((char *)(r) + i)
#define dataRowCpy(dst, r) memcpy((dst), (r), dataRowLen(r))
SDataRow tdNewDataRow(int32_t bytes); SDataRow tdNewDataRow(int32_t bytes);
SDataRow tdNewDdataFromSchema(SSchema *pSchema); SDataRow tdNewDdataFromSchema(SSchema *pSchema);
void tdFreeDataRow(SDataRow row); void tdFreeDataRow(SDataRow row);
int32_t tdAppendColVal(SDataRow row, void *value, SColumn *pCol, int32_t suffixOffset); int32_t tdAppendColVal(SDataRow row, void *value, SColumn *pCol, int32_t suffixOffset);
void tdDataRowCpy(void *dst, SDataRow row);
void tdDataRowReset(SDataRow row);
/* Data rows definition, the format of it is like below: /* Data rows definition, the format of it is like below:
* +---------+---------+-----------------------+--------+-----------------------+ * +---------+-----------------------+--------+-----------------------+
* | int32_t | int32_t | | | | * | int32_t | | | |
* +---------+---------+-----------------------+--------+-----------------------+ * +---------+-----------------------+--------+-----------------------+
* | len | nrows | SDataRow | .... | SDataRow | * | len | SDataRow | .... | SDataRow |
* +---------+---------+-----------------------+--------+-----------------------+ * +---------+-----------------------+--------+-----------------------+
*/ */
typedef void *SDataRows; typedef void *SDataRows;
#define dataRowsLen(rs) (*(int32_t *)(rs))
#define dataRowsSetLen(rs, l) (dataRowsLen(rs) = (l))
#define dataRowsInit(rs) dataRowsSetLen(rs, sizeof(int32_t))
/* Data column definition /* Data column definition
* +---------+---------+-----------------------+ * +---------+---------+-----------------------+
* | int32_t | int32_t | | * | int32_t | int32_t | |
......
#include <stdlib.h>
#include "dataformat.h" #include "dataformat.h"
/** /**
...@@ -76,6 +74,15 @@ int32_t tdAppendColVal(SDataRow row, void *value, SColumn *pCol, int32_t suffixO ...@@ -76,6 +74,15 @@ int32_t tdAppendColVal(SDataRow row, void *value, SColumn *pCol, int32_t suffixO
return 0; return 0;
} }
/**
* Copy a data row to a destination
* ASSUMPTIONS: dst has enough room for a copy of row
*/
void tdDataRowCpy(void *dst, SDataRow row) { memcpy(dst, row, dataRowLen(row)); }
void tdDataRowReset(SDataRow row) { dataRowSetLen(row, sizeof(int32_t)); }
// ------ Codes below should be refactored
SDataRow tdSDataRowDup(SDataRow rdata) { return NULL; } SDataRow tdSDataRowDup(SDataRow rdata) { return NULL; }
void tdFreeSDataRow(SDataRow rdata) { void tdFreeSDataRow(SDataRow rdata) {
if (rdata == NULL) return; if (rdata == NULL) return;
......
...@@ -46,6 +46,7 @@ typedef struct { ...@@ -46,6 +46,7 @@ typedef struct {
// Submit message for one table // Submit message for one table
typedef struct { typedef struct {
STableId tableId; STableId tableId;
int32_t padding; // TODO just for padding here
int32_t sversion; // data schema version int32_t sversion; // data schema version
int32_t len; // message length int32_t len; // message length
char data[]; char data[];
......
...@@ -184,7 +184,6 @@ static int tsdbFreeTable(STable *pTable) { ...@@ -184,7 +184,6 @@ static int tsdbFreeTable(STable *pTable) {
// TODO: finish this function // TODO: finish this function
if (pTable->type == TSDB_STABLE) { if (pTable->type == TSDB_STABLE) {
tdFreeSDataRow(pTable->pTagVal); tdFreeSDataRow(pTable->pTagVal);
} else { } else {
tdFreeSchema(pTable->pSchema); tdFreeSchema(pTable->pSchema);
} }
......
...@@ -4,35 +4,37 @@ ...@@ -4,35 +4,37 @@
#include "tsdb.h" #include "tsdb.h"
#include "tsdbMeta.h" #include "tsdbMeta.h"
TEST(TsdbTest, createTable) { TEST(TsdbTest, createTable) {
STsdbMeta *pMeta = tsdbCreateMeta(100); STsdbMeta *pMeta = tsdbCreateMeta(100);
ASSERT_NE(pMeta, nullptr); ASSERT_NE(pMeta, nullptr);
STableCfg config;
config.tableId.tid = 0;
config.tableId.uid = 98868728187539L;
config.numOfCols = 5;
config.schema = tdNewSchema(config.numOfCols);
for (int i = 0; i < schemaNCols(config.schema); i++) {
SColumn *pCol = tdNewCol(TD_DATATYPE_BIGINT, i, 0);
tdColCpy(schemaColAt(config.schema, i), pCol);
tdFreeCol(pCol);
}
config.tagValues = nullptr;
tsdbCreateTableImpl(pMeta, &config); STableCfg config;
config.tableId.tid = 0;
config.tableId.uid = 98868728187539L;
config.numOfCols = 5;
config.schema = tdNewSchema(config.numOfCols);
for (int i = 0; i < schemaNCols(config.schema); i++) {
SColumn *pCol = tdNewCol(TD_DATATYPE_BIGINT, i, 0);
tdColCpy(schemaColAt(config.schema, i), pCol);
tdFreeCol(pCol);
}
config.tagValues = nullptr;
STable *pTable = tsdbGetTableByUid(pMeta, config.tableId.uid); tsdbCreateTableImpl(pMeta, &config);
ASSERT_NE(pTable, nullptr);
STable *pTable = tsdbGetTableByUid(pMeta, config.tableId.uid);
ASSERT_NE(pTable, nullptr);
} }
TEST(TsdbTest, createRepo) { TEST(TsdbTest, createRepo) {
STsdbCfg *pCfg = tsdbCreateDefaultCfg(); STsdbCfg *pCfg = tsdbCreateDefaultCfg();
// Create a tsdb repository
tsdb_repo_t *pRepo = tsdbCreateRepo("/root/mnt/test/vnode0", pCfg, NULL); tsdb_repo_t *pRepo = tsdbCreateRepo("/root/mnt/test/vnode0", pCfg, NULL);
ASSERT_NE(pRepo, nullptr); ASSERT_NE(pRepo, nullptr);
tsdbFreeCfg(pCfg); tsdbFreeCfg(pCfg);
// create a normal table in this repository
STableCfg config; STableCfg config;
config.tableId.tid = 0; config.tableId.tid = 0;
config.tableId.uid = 98868728187539L; config.tableId.uid = 98868728187539L;
...@@ -48,7 +50,44 @@ TEST(TsdbTest, createRepo) { ...@@ -48,7 +50,44 @@ TEST(TsdbTest, createRepo) {
} }
tsdbCreateTable(pRepo, &config); tsdbCreateTable(pRepo, &config);
tdFreeSchema(config.schema); // Write some data
int32_t size = sizeof(SSubmitMsg) + sizeof(SSubmitBlock) + tdMaxRowDataBytes(config.schema) * 10 + sizeof(int32_t);
SSubmitMsg *pMsg = (SSubmitMsg *)malloc(size);
pMsg->numOfTables = 1; // TODO: use api
SSubmitBlock *pBlock = (SSubmitBlock *)pMsg->data;
pBlock->tableId = {.uid = 98868728187539L, .tid = 0};
pBlock->sversion = 0;
pBlock->len = sizeof(SSubmitBlock);
SDataRows rows = pBlock->data;
dataRowsInit(rows);
SDataRow row = tdNewDataRow(tdMaxRowDataBytes(config.schema));
int64_t ttime = 1583508800000;
void *pDst = pBlock->data;
for (int i = 0; i < 10; i++) { // loop over rows
ttime += (10000 * i);
tdDataRowReset(row);
for (int j = 0; j < schemaNCols(config.schema); j++) {
if (j == 0) { // set time stamp
tdAppendColVal(row, (void *)(&ttime), schemaColAt(config.schema, j), 24);
} else { // set other fields
int val = 10;
tdAppendColVal(row, (void *)(&val), schemaColAt(config.schema, j), 24);
}
}
dataRowCpy((void *)pDst, row);
pDst += dataRowLen(row);
}
tsdbInsertData(pRepo, pMsg);
tdFreeDataRow(row);
tdFreeSchema(config.schema);
tsdbDropRepo(pRepo); tsdbDropRepo(pRepo);
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册