TDengine/source/dnode/vnode/src/tsdb/tsdbWrite.c

105 lines
3.7 KiB
C
Raw Normal View History

2021-12-15 06:48:47 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2023-11-02 12:13:29 +00:00
#include "cos.h"
2022-04-26 11:04:26 +00:00
#include "tsdb.h"
2021-12-15 06:48:47 +00:00
/**
* @brief max key by precision
* approximately calculation:
* ms: 3600*1000*8765*1000 // 1970 + 1000 years
* us: 3600*1000000*8765*1000 // 1970 + 1000 years
* ns: 3600*1000000000*8765*292 // 1970 + 292 years
*/
2023-02-15 09:59:54 +00:00
int64_t tsMaxKeyByPrecision[] = {31556995200000L, 31556995200000000L, 9214646400000000000L};
2022-05-18 08:30:50 +00:00
// static int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitReq *pMsg);
2022-04-25 11:38:05 +00:00
int tsdbInsertData(STsdb *pTsdb, int64_t version, SSubmitReq2 *pMsg, SSubmitRsp2 *pRsp) {
2024-07-17 14:09:09 +00:00
int32_t code;
int32_t arrSize = 0;
int32_t affectedrows = 0;
int32_t numOfRows = 0;
2022-04-25 11:38:05 +00:00
2023-02-22 06:29:14 +00:00
if (ASSERTS(pTsdb->mem != NULL, "vgId:%d, mem is NULL", TD_VID(pTsdb->pVnode))) {
2024-07-24 02:09:24 +00:00
TAOS_RETURN(TSDB_CODE_INVALID_PTR);
2023-02-22 06:29:14 +00:00
}
2022-04-25 11:38:05 +00:00
2023-02-27 10:09:51 +00:00
arrSize = taosArrayGetSize(pMsg->aSubmitTbData);
2022-04-25 11:38:05 +00:00
// scan and convert
2024-07-17 14:09:09 +00:00
if ((code = tsdbScanAndConvertSubmitMsg(pTsdb, pMsg)) < 0) {
if (code != TSDB_CODE_TDB_TABLE_RECONFIGURE) {
2024-07-24 02:09:24 +00:00
tsdbError("vgId:%d, failed to insert data since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-04-25 11:38:05 +00:00
}
2024-07-17 14:09:09 +00:00
return code;
2022-04-25 11:38:05 +00:00
}
// loop to insert
for (int32_t i = 0; i < arrSize; ++i) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_RETURN(tsdbInsertTableData(pTsdb, version, taosArrayGet(pMsg->aSubmitTbData, i), &affectedrows));
2022-04-25 11:38:05 +00:00
}
if (pRsp != NULL) {
2022-05-10 05:57:31 +00:00
// pRsp->affectedRows = affectedrows;
// pRsp->numOfRows = numOfRows;
2022-04-25 11:38:05 +00:00
}
return 0;
}
static FORCE_INLINE int tsdbCheckRowRange(STsdb *pTsdb, tb_uid_t uid, TSKEY rowKey, TSKEY minKey, TSKEY maxKey,
2022-05-18 08:30:50 +00:00
TSKEY now) {
if (rowKey < minKey || rowKey > maxKey) {
2022-06-02 05:57:39 +00:00
tsdbError("vgId:%d, table uid %" PRIu64 " timestamp is out of range! now %" PRId64 " minKey %" PRId64
2022-05-18 08:30:50 +00:00
" maxKey %" PRId64 " row key %" PRId64,
2022-06-10 06:48:21 +00:00
TD_VID(pTsdb->pVnode), uid, now, minKey, maxKey, rowKey);
return TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
2022-05-18 08:30:50 +00:00
}
return 0;
}
int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitReq2 *pMsg) {
STsdbKeepCfg *pCfg = &pTsdb->keepCfg;
TSKEY now = taosGetTimestamp(pCfg->precision);
TSKEY minKey = now - tsTickPerMin[pCfg->precision] * pCfg->keep2;
TSKEY maxKey = tsMaxKeyByPrecision[pCfg->precision];
int32_t size = taosArrayGetSize(pMsg->aSubmitTbData);
2024-07-17 14:09:09 +00:00
for (int32_t i = 0; i < size; ++i) {
SSubmitTbData *pData = TARRAY_GET_ELEM(pMsg->aSubmitTbData, i);
if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
uint64_t nColData = TARRAY_SIZE(pData->aCol);
SColData *aColData = (SColData *)TARRAY_DATA(pData->aCol);
if (nColData > 0) {
int32_t nRows = aColData[0].nVal;
TSKEY *aKey = (TSKEY *)aColData[0].pData;
for (int32_t r = 0; r < nRows; ++r) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_RETURN(tsdbCheckRowRange(pTsdb, pData->uid, aKey[r], minKey, maxKey, now));
}
}
} else {
int32_t nRows = taosArrayGetSize(pData->aRowP);
for (int32_t r = 0; r < nRows; ++r) {
SRow *pRow = (SRow *)taosArrayGetP(pData->aRowP, r);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_RETURN(tsdbCheckRowRange(pTsdb, pData->uid, pRow->ts, minKey, maxKey, now));
}
}
}
2024-07-17 14:09:09 +00:00
return 0;
}