From 8d5d0fd4efac0b759324c86379863ad016fd8c32 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 1 Jun 2020 03:55:36 +0000 Subject: [PATCH] refactor --- src/common/inc/tdataformat.h | 6 +++--- src/common/src/tdataformat.c | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/common/inc/tdataformat.h b/src/common/inc/tdataformat.h index 47a1d642e1..19cbbea0fb 100644 --- a/src/common/inc/tdataformat.h +++ b/src/common/inc/tdataformat.h @@ -241,12 +241,12 @@ typedef struct { #define kvRowLen(r) (*(int16_t *)(r)) #define kvRowNCols(r) (*(int16_t *)POINTER_SHIFT(r, sizeof(int16_t))) +#define kvRowSetLen(r, len) kvRowLen(r) = (len) +#define kvRowSetNCols(r, n) kvRowNCols(r) = (n) #define kvRowColIdx(r) (SColIdx *)POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE) #define kvRowValues(r) POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE + sizeof(SColIdx) * kvRowNCols(r)) #define kvRowCpy(dst, r) memcpy((dst), (r), kvRowLen(r)) #define kvRowColVal(r, colIdx) POINTER_SHIFT(kvRowValues(r), (colIdx)->offset) -#define kvRowSetLen(r, len) kvRowLen(r) = (len) -#define kvRowSetNCols(r, n) kvRowNCols(r) = (n) #define kvRowColIdxAt(r, i) (kvRowColIdx(r) + (i)) SKVRow tdKVRowDup(SKVRow row); @@ -264,7 +264,7 @@ static FORCE_INLINE int comparTagId(const void *key1, const void *key2) { } } -static FORCE_INLINE void *tdGetKVRowDataOfCol(SKVRow row, int16_t colId) { +static FORCE_INLINE void *tdGetKVRowValOfCol(SKVRow row, int16_t colId) { void *ret = taosbsearch(&colId, kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), comparTagId, TD_EQ); if (ret == NULL) return NULL; return kvRowColVal(row, (SColIdx *)ret); diff --git a/src/common/src/tdataformat.c b/src/common/src/tdataformat.c index 333e15058d..536135010a 100644 --- a/src/common/src/tdataformat.c +++ b/src/common/src/tdataformat.c @@ -709,11 +709,13 @@ SKVRow tdGetKVRowFromBuilder(SKVRowBuilder *pBuilder) { int tlen = sizeof(SColIdx) * pBuilder->nCols + pBuilder->size; if (tlen == 0) return NULL; - SKVRow row = malloc(TD_KV_ROW_HEAD_SIZE + tlen); + tlen += TD_KV_ROW_HEAD_SIZE; + + SKVRow row = malloc(tlen); if (row == NULL) return NULL; kvRowSetNCols(row, pBuilder->nCols); - kvRowSetLen(row, TD_KV_ROW_HEAD_SIZE + tlen); + kvRowSetLen(row, tlen); memcpy(kvRowColIdx(row), pBuilder->pColIdx, sizeof(SColIdx) * pBuilder->nCols); memcpy(kvRowValues(row), pBuilder->buf, pBuilder->size); @@ -737,7 +739,9 @@ int tdAddColToKVRow(SKVRowBuilder *pBuilder, int16_t colId, int8_t type, void *v int tlen = IS_VAR_DATA_TYPE(type) ? varDataTLen(value) : TYPE_BYTES[type]; if (tlen > pBuilder->alloc - pBuilder->size) { - pBuilder->alloc *= 2; + while (tlen > pBuilder->alloc - pBuilder->size) { + pBuilder->alloc *= 2; + } pBuilder->buf = realloc(pBuilder->buf, pBuilder->alloc); if (pBuilder->buf == NULL) return -1; } -- GitLab