From 416e6354e9bd610fd8af57eb1e23773bc96871ed Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 22 Nov 2021 13:45:41 +0800 Subject: [PATCH] more --- include/common/trow.h | 59 +++++++++++++++++------ include/common/tschema.h | 48 ++++++++++++++++--- source/common/src/trow.c | 76 ++++-------------------------- source/common/test/trowTest.cpp | 21 +++++++++ source/common/test/tschemaTest.cpp | 6 +++ 5 files changed, 121 insertions(+), 89 deletions(-) create mode 100644 source/common/test/trowTest.cpp create mode 100644 source/common/test/tschemaTest.cpp diff --git a/include/common/trow.h b/include/common/trow.h index 8c6d9846fa..7186505e03 100644 --- a/include/common/trow.h +++ b/include/common/trow.h @@ -19,18 +19,18 @@ #include "os.h" #include "tbuffer.h" #include "tdef.h" +#include "tschema.h" #ifdef __cplusplus extern "C" { #endif -#define TD_OR_ROW 0 -#define TD_KV_ROW 1 - -typedef uint16_t col_id_t; +#define TD_UNDECIDED_ROW 0 +#define TD_OR_ROW 1 +#define TD_KV_ROW 2 typedef struct { - TSKEY ts; + // TODO } SOrRow; typedef struct { @@ -48,21 +48,30 @@ typedef struct { /// union field for encode and decode uint64_t info; struct { + /// is deleted row + uint64_t del : 1; /// row type - uint64_t type : 2; + uint64_t type : 3; /// row schema version uint64_t sver : 16; /// row total length - uint64_t len : 46; + uint64_t len : 32; + /// reserved for back compatibility + uint64_t reserve : 12; }; }; /// row version uint64_t ver; - /// timestamp of the row - TSKEY ts; - char content[]; + /// timestamp + TSKEY ts; + char content[]; } SRow; +typedef struct { + uint32_t nRows; + char rows[]; +} SRowBatch; + typedef enum { /// ordinary row builder TD_OR_ROW_BUILDER = 0, @@ -82,12 +91,32 @@ typedef struct { } SRowBuilder; typedef struct { - /* TODO */ -} SRowBatchBuilder; + SSchema *pSchema; + SRow * pRow; +} SRowReader; + +typedef struct { + uint32_t it; + SRowBatch *pRowBatch; +} SRowBatchIter; + +// SRowBuilder +#define trbInit(rt, allocator, endian, target, size) \ + { .type = (rt), .bw = tbufInitWriter(allocator, endian), .pRow = (target) } +void trbSetRowInfo(SRowBuilder *pRB, bool del, uint16_t sver); +void trbSetRowVersion(SRowBuilder *pRB, uint64_t ver); +void trbSetRowTS(SRowBuilder *pRB, TSKEY ts); +int trbWriteCol(SRowBuilder *pRB, void *pData, col_id_t cid); + +// SRowReader +#define tRowReaderInit(schema, row) \ + { .schema = (schema), .row = (row) } +int tRowReaderRead(SRowReader *pRowReader, col_id_t cid, void *target, uint64_t size); -#define tRBInit(type, allocator, endian) \ - { .type = (type), tbufInitWriter(allocator, endian), NULL } -void tRBClear(SRowBuilder *pRB); +// SRowBatchIter +#define tRowBatchIterInit(pRB) \ + { .it = 0, .pRowBatch = (pRB) } +const SRow *tRowBatchIterNext(SRowBatchIter *pRowBatchIter); #ifdef __cplusplus } diff --git a/include/common/tschema.h b/include/common/tschema.h index acc6a4ad7a..59a858bbb7 100644 --- a/include/common/tschema.h +++ b/include/common/tschema.h @@ -17,27 +17,61 @@ #define _TD_COMMON_SCHEMA_H_ #include "os.h" +#include "tarray.h" #ifdef __cplusplus extern "C" { #endif -typedef struct SColAttr { - /* data */ -} SColAttr; +typedef uint16_t col_id_t; typedef struct SColumn { - uint8_t type; - uint16_t cid; - uint16_t bytes; + /// column name + char *cname; + union { + /// for encode purpose + uint64_t info; + struct { + uint64_t sma : 1; + /// column data type + uint64_t type : 7; + /// column id + uint64_t cid : 16; + /// max bytes of the column + uint64_t bytes : 32; + /// reserved + uint64_t reserve : 8; + }; + }; + /// comment about the column + char *comment; } SColumn; typedef struct SSchema { /// schema version uint16_t sver; - /* data */ + /// number of columns + uint16_t ncols; + /// sma attributes + struct { + bool sma; + SArray *smaArray; + }; + /// column info + SColumn cols[]; } SSchema; +typedef struct { + uint64_t size; + SSchema *pSchema; +} SShemaBuilder; + +#define tSchemaBuilderInit(target, capacity) \ + { .size = (capacity), .pSchema = (target) } +void tSchemaBuilderSetSver(SShemaBuilder *pSchemaBuilder, uint16_t sver); +void tSchemaBuilderSetSMA(bool sma, SArray *smaArray); +int tSchemaBuilderPutColumn(char *cname, bool sma, uint8_t type, col_id_t cid, uint32_t bytes, char *comment); + #ifdef __cplusplus } #endif diff --git a/source/common/src/trow.c b/source/common/src/trow.c index e443f92b25..2e36e6a757 100644 --- a/source/common/src/trow.c +++ b/source/common/src/trow.c @@ -15,77 +15,19 @@ #include "trow.h" -#if 0 -/* ------------ Structures ---------- */ -struct SRowBatch { - int32_t compress : 1; // if batch row is compressed - int32_t nrows : 31; // number of rows - int32_t tlen; // total length (including `nrows` and `tlen`) - char rows[]; -}; - -struct SRowBuilder { +void trbSetRowInfo(SRowBuilder *pRB, bool del, uint16_t sver) { // TODO -}; - -struct SRowBatchIter { - int32_t counter; // row counter - SRowBatch *rb; // row batch to iter - SRow nrow; // next row -}; - -struct SRowBatchBuilder { - // TODO -}; - -/* ------------ Methods ---------- */ - -// SRowBuilder -SRowBuilder *rowBuilderCreate() { - SRowBuilder *pRowBuilder = NULL; - // TODO - - return pRowBuilder; } -void rowBuilderDestroy(SRowBuilder *pRowBuilder) { - if (pRowBuilder) { - free(pRowBuilder); - } -} - -// SRowBatchIter -SRowBatchIter *rowBatchIterCreate(SRowBatch *pRowBatch) { - SRowBatchIter *pRowBatchIter = (SRowBatchIter *)malloc(sizeof(*pRowBatchIter)); - if (pRowBatchIter == NULL) { - return NULL; - } - - pRowBatchIter->counter = 0; - pRowBatchIter->rb = pRowBatch; - pRowBatchIter->nrow = pRowBatch->rows; - - return pRowBatchIter; -}; - -void rowBatchIterDestroy(SRowBatchIter *pRowBatchIter) { - if (pRowBatchIter) { - free(pRowBatchIter); - } +void trbSetRowVersion(SRowBuilder *pRB, uint64_t ver) { + // TODO } -const SRow rowBatchIterNext(SRowBatchIter *pRowBatchIter) { - SRow r = NULL; - if (pRowBatchIter->counter < pRowBatchIter->rb->nrows) { - r = pRowBatchIter->nrow; - pRowBatchIter->counter += 1; - pRowBatchIter->nrow = (SRow)POINTER_SHIFT(r, rowLen(r)); - } - - return r; +void trbSetRowTS(SRowBuilder *pRB, TSKEY ts) { + // TODO } -// SRowBatchBuilder -SRowBatchBuilder *rowBatchBuilderCreate(); -void rowBatchBuilderDestroy(SRowBatchBuilder *); -#endif \ No newline at end of file +int trbWriteCol(SRowBuilder *pRB, void *pData, col_id_t cid) { + // TODO + return 0; +} \ No newline at end of file diff --git a/source/common/test/trowTest.cpp b/source/common/test/trowTest.cpp new file mode 100644 index 0000000000..8c628464e3 --- /dev/null +++ b/source/common/test/trowTest.cpp @@ -0,0 +1,21 @@ +#include + +#include "trow.h" + +TEST(td_row_test, build_row_to_target) { + char dst[1024]; + SRow* pRow = (SRow*)dst; + int ncols = 10; + col_id_t cid; + void* pData; + SRowBuilder rb = trbInit(TD_OR_ROW_BUILDER, NULL, 0, pRow, 1024); + + trbSetRowInfo(&rb, false, 0); + trbSetRowTS(&rb, 1637550210000); + for (int c = 0; c < ncols; c++) { + cid = c; + if (trbWriteCol(&rb, pData, cid) < 0) { + // TODO + } + } +} \ No newline at end of file diff --git a/source/common/test/tschemaTest.cpp b/source/common/test/tschemaTest.cpp new file mode 100644 index 0000000000..acced6e09e --- /dev/null +++ b/source/common/test/tschemaTest.cpp @@ -0,0 +1,6 @@ +#include +#include "tschema.h" + +TEST(td_schema_test, build_schema_test) { + +} \ No newline at end of file -- GitLab