From c55db1d8f7ed865689fae8eb7f100f972e0ae7b5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 25 Jul 2024 11:45:15 +0800 Subject: [PATCH] enh: refact return code --- source/dnode/vnode/src/meta/metaQuery.c | 28 +-- source/dnode/vnode/src/meta/metaSma.c | 23 +- source/dnode/vnode/src/meta/metaSnapshot.c | 18 +- source/dnode/vnode/src/meta/metaTable.c | 148 ++++++------ source/dnode/vnode/src/vnd/vnodeOpen.c | 10 +- source/dnode/vnode/src/vnd/vnodeSvr.c | 106 +++++---- source/libs/tdb/src/db/tdbBtree.c | 250 +++++++++++---------- source/libs/tdb/src/db/tdbPager.c | 16 +- 8 files changed, 300 insertions(+), 299 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaQuery.c b/source/dnode/vnode/src/meta/metaQuery.c index 062c019d48..e4a04047f1 100644 --- a/source/dnode/vnode/src/meta/metaQuery.c +++ b/source/dnode/vnode/src/meta/metaQuery.c @@ -48,27 +48,23 @@ void metaReaderClear(SMetaReader *pReader) { } int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) { + int32_t code = 0; SMeta *pMeta = pReader->pMeta; STbDbKey tbDbKey = {.version = version, .uid = uid}; // query table.db if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf) < 0) { - terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; - goto _err; + return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; } // decode the entry tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf); - if (metaDecodeEntry(&pReader->coder, &pReader->me) < 0) { - goto _err; - } + code = metaDecodeEntry(&pReader->coder, &pReader->me); + if (code) return code; // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr); return 0; - -_err: - return -1; } bool metaIsTableExist(void *pVnode, tb_uid_t uid) { @@ -90,8 +86,7 @@ int metaReaderGetTableEntryByUid(SMetaReader *pReader, tb_uid_t uid) { // query uid.idx if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) { - terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; } version1 = ((SUidIdxVal *)pReader->pBuf)[0].version; @@ -103,8 +98,7 @@ int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) { SMetaInfo info; if (metaGetInfo(pMeta, uid, &info, pReader) == TSDB_CODE_NOT_FOUND) { - terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; } return metaGetTableEntryByVersion(pReader, info.version, uid); @@ -116,8 +110,7 @@ int metaGetTableEntryByName(SMetaReader *pReader, const char *name) { // query name.idx if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) { - terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; } uid = *(tb_uid_t *)pReader->pBuf; @@ -148,7 +141,7 @@ int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName) { code = metaReaderGetTableEntryByUid(&mr, uid); if (code < 0) { metaReaderClear(&mr); - return -1; + return code; } STR_TO_VARSTR(tbName, mr.me.name); @@ -164,7 +157,7 @@ int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) { code = metaReaderGetTableEntryByUid(&mr, uid); if (code < 0) { metaReaderClear(&mr); - return -1; + return code; } strncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN); metaReaderClear(&mr); @@ -181,9 +174,8 @@ int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid) { // query name.idx if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) { - terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; metaReaderClear(&mr); - return -1; + return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; } *uid = *(tb_uid_t *)pReader->pBuf; diff --git a/source/dnode/vnode/src/meta/metaSma.c b/source/dnode/vnode/src/meta/metaSma.c index c61725e834..371cc6ff21 100644 --- a/source/dnode/vnode/src/meta/metaSma.c +++ b/source/dnode/vnode/src/meta/metaSma.c @@ -13,9 +13,8 @@ * along with this program. If not, see . */ -#include "vnodeInt.h" #include "meta.h" - +#include "vnodeInt.h" static int metaHandleSmaEntry(SMeta *pMeta, const SMetaEntry *pME); static int metaSaveSmaToDB(SMeta *pMeta, const SMetaEntry *pME); @@ -34,15 +33,15 @@ int32_t metaCreateTSma(SMeta *pMeta, int64_t version, SSmaCfg *pCfg) { int32_t szBuf = 0; void *p = NULL; SMetaReader mr = {0}; + int32_t code = 0; // validate req // save smaIndex metaReaderDoInit(&mr, pMeta, META_READER_LOCK); if (metaReaderGetTableEntryByUidCache(&mr, pCfg->indexUid) == 0) { #if 1 - terrno = TSDB_CODE_TSMA_ALREADY_EXIST; metaReaderClear(&mr); - return -1; // don't goto _err; + return terrno = TSDB_CODE_TSMA_ALREADY_EXIST; #else metaReaderClear(&mr); return 0; @@ -57,7 +56,8 @@ int32_t metaCreateTSma(SMeta *pMeta, int64_t version, SSmaCfg *pCfg) { me.name = pCfg->indexName; me.smaEntry.tsma = pCfg; - if (metaHandleSmaEntry(pMeta, &me) < 0) goto _err; + code = metaHandleSmaEntry(pMeta, &me); + if (code) goto _err; metaDebug("vgId:%d, tsma is created, name:%s uid:%" PRId64, TD_VID(pMeta->pVnode), pCfg->indexName, pCfg->indexUid); @@ -66,7 +66,7 @@ int32_t metaCreateTSma(SMeta *pMeta, int64_t version, SSmaCfg *pCfg) { _err: metaError("vgId:%d, failed to create tsma:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pCfg->indexName, pCfg->indexUid, tstrerror(terrno)); - return -1; + return code; } int32_t metaDropTSma(SMeta *pMeta, int64_t indexUid) { @@ -147,24 +147,25 @@ static int metaUpdateSmaIdx(SMeta *pMeta, const SMetaEntry *pME) { } static int metaHandleSmaEntry(SMeta *pMeta, const SMetaEntry *pME) { + int32_t code = 0; metaWLock(pMeta); // save to table.db - if (metaSaveSmaToDB(pMeta, pME) < 0) goto _err; + if ((code = metaSaveSmaToDB(pMeta, pME)) < 0) goto _err; // update uid.idx - if (metaUpdateUidIdx(pMeta, pME) < 0) goto _err; + if ((code = metaUpdateUidIdx(pMeta, pME)) < 0) goto _err; // update name.idx - if (metaUpdateNameIdx(pMeta, pME) < 0) goto _err; + if ((code = metaUpdateNameIdx(pMeta, pME)) < 0) goto _err; // update sma.idx - if (metaUpdateSmaIdx(pMeta, pME) < 0) goto _err; + if ((code = metaUpdateSmaIdx(pMeta, pME)) < 0) goto _err; metaULock(pMeta); return 0; _err: metaULock(pMeta); - return -1; + return code; } diff --git a/source/dnode/vnode/src/meta/metaSnapshot.c b/source/dnode/vnode/src/meta/metaSnapshot.c index 95c65e5e80..87140cfa12 100644 --- a/source/dnode/vnode/src/meta/metaSnapshot.c +++ b/source/dnode/vnode/src/meta/metaSnapshot.c @@ -32,7 +32,7 @@ int32_t metaSnapReaderOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapRe // alloc pReader = (SMetaSnapReader*)taosMemoryCalloc(1, sizeof(*pReader)); if (pReader == NULL) { - TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit); + TSDB_CHECK_CODE(code = terrno, lino, _exit); } pReader->pMeta = pMeta; pReader->sver = sver; @@ -261,7 +261,9 @@ static void saveSuperTableInfoForChildTable(SMetaEntry* me, SHashObj* suidInfo) int32_t buildSnapContext(SVnode* pVnode, int64_t snapVersion, int64_t suid, int8_t subType, int8_t withMeta, SSnapContext** ctxRet) { SSnapContext* ctx = taosMemoryCalloc(1, sizeof(SSnapContext)); - if (ctx == NULL) return -1; + if (ctx == NULL) { + return terrno; + } *ctxRet = ctx; ctx->pMeta = pVnode->pMeta; ctx->snapVersion = snapVersion; @@ -271,12 +273,12 @@ int32_t buildSnapContext(SVnode* pVnode, int64_t snapVersion, int64_t suid, int8 ctx->withMeta = withMeta; ctx->idVersion = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK); if (ctx->idVersion == NULL) { - return -1; + return terrno; } ctx->suidInfo = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK); if (ctx->suidInfo == NULL) { - return -1; + return terrno; } taosHashSetFreeFp(ctx->suidInfo, destroySTableInfoForChildTable); @@ -426,21 +428,21 @@ static int32_t buildSuperTableInfo(SVCreateStbReq* req, void** pBuf, int32_t* co int32_t ret = 0; tEncodeSize(tEncodeSVCreateStbReq, req, *contLen, ret); if (ret < 0) { - return -1; + return ret; } *contLen += sizeof(SMsgHead); *pBuf = taosMemoryMalloc(*contLen); if (NULL == *pBuf) { - return -1; + return terrno; } SEncoder encoder = {0}; tEncoderInit(&encoder, POINTER_SHIFT(*pBuf, sizeof(SMsgHead)), *contLen); - if (tEncodeSVCreateStbReq(&encoder, req) < 0) { + if ((ret = tEncodeSVCreateStbReq(&encoder, req)) < 0) { taosMemoryFreeClear(*pBuf); tEncoderClear(&encoder); - return -1; + return ret; } tEncoderClear(&encoder); return 0; diff --git a/source/dnode/vnode/src/meta/metaTable.c b/source/dnode/vnode/src/meta/metaTable.c index e40478eec2..92e910dba3 100644 --- a/source/dnode/vnode/src/meta/metaTable.c +++ b/source/dnode/vnode/src/meta/metaTable.c @@ -36,11 +36,15 @@ static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME); static int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME); static int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME); -int8_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) { +static int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) { int32_t nCols = pWp->nCols; int32_t ver = pWp->version; if (add) { SColCmpr *p = taosMemoryCalloc(1, sizeof(SColCmpr) * (nCols + 1)); + if (p == NULL) { + return terrno; + } + memcpy(p, pWp->pColCmpr, sizeof(SColCmpr) * nCols); SColCmpr *pCol = p + nCols; @@ -64,7 +68,7 @@ int8_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, ui pWp->nCols = nCols; pWp->version = ver; } - return 1; + return 0; } static void metaGetEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) { pInfo->uid = pEntry->uid; @@ -87,8 +91,7 @@ static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema)); if (NULL == pMetaRsp->pSchemas) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + return terrno = TSDB_CODE_OUT_OF_MEMORY; } pMetaRsp->pSchemaExt = taosMemoryMalloc(pSchema->nCols * sizeof(SSchemaExt)); @@ -105,9 +108,11 @@ static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema } static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) { + int32_t code = 0; + #ifdef USE_INVERTED_INDEX if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) { - return -1; + return TSDB_CODE_INVALID_PARA; } void *data = pCtbEntry->ctbEntry.pTags; const char *tagName = pSchema->name; @@ -118,8 +123,9 @@ static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const int32_t nTagData = 0; SArray *pTagVals = NULL; - if (tTagToValArray((const STag *)data, &pTagVals) != 0) { - return -1; + code = tTagToValArray((const STag *)data, &pTagVals); + if (code) { + return code; } SIndexMultiTerm *terms = indexMultiTermCreate(); @@ -168,7 +174,7 @@ static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) { #ifdef USE_INVERTED_INDEX if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) { - return -1; + return TSDB_CODE_INVALID_PARA; } void *data = pCtbEntry->ctbEntry.pTags; const char *tagName = pSchema->name; @@ -179,8 +185,9 @@ int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSche int32_t nTagData = 0; SArray *pTagVals = NULL; - if (tTagToValArray((const STag *)data, &pTagVals) != 0) { - return -1; + code = tTagToValArray((const STag *)data, &pTagVals); + if (code) { + return code; } SIndexMultiTerm *terms = indexMultiTermCreate(); @@ -247,6 +254,7 @@ int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { void *pBuf = NULL; int32_t szBuf = 0; void *p = NULL; + int32_t code = 0; // validate req void *pData = NULL; @@ -256,14 +264,12 @@ int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { tdbFree(pData); SMetaInfo info; if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) { - terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; } if (info.uid == info.suid) { return 0; } else { - terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST; } } @@ -283,7 +289,8 @@ int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { me.colCmpr = pReq->colCmpr; } - if (metaHandleEntry(pMeta, &me) < 0) goto _err; + code = metaHandleEntry(pMeta, &me); + if (code) goto _err; ++pMeta->pVnode->config.vndStats.numOfSTables; @@ -295,7 +302,7 @@ int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { _err: metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid, tstrerror(terrno)); - return -1; + return code; } int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq, SArray *tbUidList) { @@ -310,8 +317,7 @@ int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq, SArray *tb rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData); if (rc < 0 || *(tb_uid_t *)pData != pReq->suid) { tdbFree(pData); - terrno = TSDB_CODE_TDB_STB_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_STB_NOT_EXIST; } // drop all child tables @@ -424,16 +430,14 @@ int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { if (ret < 0 || c) { tdbTbcClose(pUidIdxc); - terrno = TSDB_CODE_TDB_STB_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_STB_NOT_EXIST; } ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData); if (ret < 0) { tdbTbcClose(pUidIdxc); - terrno = TSDB_CODE_TDB_STB_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_STB_NOT_EXIST; } oversion = ((SUidIdxVal *)pData)[0].version; @@ -444,9 +448,8 @@ int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { tdbTbcClose(pUidIdxc); tdbTbcClose(pTbDbc); - terrno = TSDB_CODE_TDB_STB_NOT_EXIST; metaError("meta/table: invalide ret: %" PRId32 " or c: %" PRId32 "alter stb failed.", ret, c); - return -1; + return terrno = TSDB_CODE_TDB_STB_NOT_EXIST; } ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData); @@ -454,8 +457,7 @@ int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) { tdbTbcClose(pUidIdxc); tdbTbcClose(pTbDbc); - terrno = TSDB_CODE_TDB_STB_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_STB_NOT_EXIST; } oStbEntry.pBuf = taosMemoryMalloc(nData); @@ -870,8 +872,7 @@ int metaCreateTable(SMeta *pMeta, int64_t ver, SVCreateTbReq *pReq, STableMetaRs if (pReq->type == TSDB_CHILD_TABLE) { tb_uid_t suid = metaGetTableEntryUidByName(pMeta, pReq->ctb.stbName); if (suid != pReq->ctb.suid) { - terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST; } } @@ -879,17 +880,15 @@ int metaCreateTable(SMeta *pMeta, int64_t ver, SVCreateTbReq *pReq, STableMetaRs metaReaderDoInit(&mr, pMeta, META_READER_LOCK); if (metaGetTableEntryByName(&mr, pReq->name) == 0) { if (pReq->type == TSDB_CHILD_TABLE && pReq->ctb.suid != mr.me.ctbEntry.suid) { - terrno = TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE; metaReaderClear(&mr); - return -1; + return terrno = TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE; } pReq->uid = mr.me.uid; if (pReq->type == TSDB_CHILD_TABLE) { pReq->ctb.suid = mr.me.ctbEntry.suid; } - terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST; metaReaderClear(&mr); - return -1; + return terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST; } else if (terrno == TSDB_CODE_PAR_TABLE_NOT_EXIST) { terrno = TSDB_CODE_SUCCESS; } @@ -1001,7 +1000,7 @@ int metaCreateTable(SMeta *pMeta, int64_t ver, SVCreateTbReq *pReq, STableMetaRs _err: metaError("vgId:%d, failed to create table:%s type:%s since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->type == TSDB_CHILD_TABLE ? "child table" : "normal table", tstrerror(terrno)); - return -1; + return TSDB_CODE_FAILED; } int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids, tb_uid_t *tbUid) { @@ -1015,8 +1014,7 @@ int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUi rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData); if (rc < 0) { - terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; } uid = *(tb_uid_t *)pData; @@ -1154,7 +1152,7 @@ int32_t metaTrimTables(SMeta *pMeta) { SArray *tbUids = taosArrayInit(8, sizeof(int64_t)); if (tbUids == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } code = metaFilterTableByHash(pMeta, tbUids); @@ -1195,7 +1193,7 @@ static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) { } else if (pME->type == TSDB_NORMAL_TABLE) { btime = pME->ntbEntry.btime; } else { - return -1; + return TSDB_CODE_FAILED; } btimeKey->btime = btime; @@ -1208,7 +1206,7 @@ static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) { ncolKey->ncol = pME->ntbEntry.schemaRow.nCols; ncolKey->uid = pME->uid; } else { - return -1; + return TSDB_CODE_FAILED; } return 0; } @@ -1235,7 +1233,7 @@ static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *p rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData); if (rc < 0) { - return -1; + return rc; } int64_t version = ((SUidIdxVal *)pData)[0].version; @@ -1245,7 +1243,7 @@ static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *p rc = metaDecodeEntry(&dc, &e); if (rc < 0) { tDecoderClear(&dc); - return -1; + return rc; } if (type) *type = e.type; @@ -1408,16 +1406,14 @@ static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAl int c; bool freeColCmpr = false; if (pAlterTbReq->colName == NULL) { - terrno = TSDB_CODE_INVALID_MSG; metaError("meta/table: null pAlterTbReq->colName"); - return -1; + return terrno = TSDB_CODE_INVALID_MSG; } // search name index ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal); if (ret < 0) { - terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; } uid = *(tb_uid_t *)pVal; @@ -1432,7 +1428,7 @@ static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAl if (c != 0) { tdbTbcClose(pUidIdxc); metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c); - return -1; + return TSDB_CODE_FAILED; } tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData); @@ -1447,7 +1443,7 @@ static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAl tdbTbcClose(pUidIdxc); tdbTbcClose(pTbDbc); metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c); - return -1; + return TSDB_CODE_FAILED; } tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData); @@ -1463,7 +1459,7 @@ static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAl tdbTbcClose(pTbDbc); tDecoderClear(&dc); metaError("meta/table: invalide ret: %" PRId32 " alt tb column failed.", ret); - return -1; + return ret; } if (entry.type != TSDB_NORMAL_TABLE) { @@ -1660,7 +1656,7 @@ _err: tdbTbcClose(pUidIdxc); tDecoderClear(&dc); - return -1; + return TSDB_CODE_FAILED; } static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) { @@ -1676,15 +1672,13 @@ static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pA int nData = 0; if (pAlterTbReq->tagName == NULL) { - terrno = TSDB_CODE_INVALID_MSG; - return -1; + return terrno = TSDB_CODE_INVALID_MSG; } // search name index ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal); if (ret < 0) { - terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; } uid = *(tb_uid_t *)pVal; @@ -1698,9 +1692,8 @@ static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pA tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c); if (c != 0) { tdbTbcClose(pUidIdxc); - terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c); - return -1; + return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; } tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData); @@ -1717,9 +1710,8 @@ static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pA if (c != 0) { tdbTbcClose(pUidIdxc); tdbTbcClose(pTbDbc); - terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c); - return -1; + return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; } tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData); @@ -1866,8 +1858,7 @@ static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *p // search name index ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal); if (ret < 0) { - terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; } uid = *(tb_uid_t *)pVal; @@ -1882,7 +1873,7 @@ static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *p if (c != 0) { tdbTbcClose(pUidIdxc); metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c); - return -1; + return TSDB_CODE_FAILED; } tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData); @@ -1897,7 +1888,7 @@ static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *p tdbTbcClose(pUidIdxc); tdbTbcClose(pTbDbc); metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c); - return -1; + return TSDB_CODE_FAILED; } tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData); @@ -1913,7 +1904,7 @@ static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *p tdbTbcClose(pUidIdxc); tdbTbcClose(pTbDbc); metaError("meta/table: invalide ret: %" PRId32 " alt tb options failed.", ret); - return -1; + return TSDB_CODE_FAILED; } entry.version = version; @@ -1968,15 +1959,13 @@ static int metaAddTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTb SDecoder dc = {0}; if (pAlterTbReq->tagName == NULL) { - terrno = TSDB_CODE_INVALID_MSG; - return -1; + return terrno = TSDB_CODE_INVALID_MSG; } // search name index ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal); if (ret < 0) { - terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; } else { uid = *(tb_uid_t *)pVal; tdbFree(pVal); @@ -2085,7 +2074,7 @@ _err: // if (stbEntry.pBuf) tdbFree(stbEntry.pBuf); // tdbTbcClose(pTbDbc); // tdbTbcClose(pUidIdxc); - return -1; + return TSDB_CODE_FAILED; } typedef struct SMetaPair { @@ -2106,15 +2095,13 @@ static int metaDropTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterT SDecoder dc = {0}; if (pAlterTbReq->tagName == NULL) { - terrno = TSDB_CODE_INVALID_MSG; - return -1; + return terrno = TSDB_CODE_INVALID_MSG; } // search name index ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal); if (ret < 0) { - terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; } suid = *(tb_uid_t *)pVal; tdbFree(pVal); @@ -2200,7 +2187,7 @@ static int metaDropTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterT // set pCol->flags; INDEX_ON return 0; _err: - return -1; + return TSDB_CODE_FAILED; } int32_t metaUpdateTableColCompress(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) { // impl later @@ -2216,8 +2203,7 @@ int32_t metaUpdateTableColCompress(SMeta *pMeta, int64_t version, SVAlterTbReq * SDecoder dc = {0}; ret = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &pVal, &nVal); if (ret < 0) { - terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; - return -1; + return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST; } suid = *(tb_uid_t *)pVal; tdbFree(pVal); @@ -2290,7 +2276,7 @@ int32_t metaUpdateTableColCompress(SMeta *pMeta, int64_t version, SVAlterTbReq * return 0; _err: - return -1; + return TSDB_CODE_FAILED; } int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) { @@ -2313,8 +2299,7 @@ int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMeta case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: return metaUpdateTableColCompress(pMeta, version, pReq); default: - terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION; - return -1; + return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION; break; } } @@ -2370,7 +2355,7 @@ _err: pME->uid, tstrerror(terrno)); taosMemoryFree(pVal); - return -1; + return TSDB_CODE_FAILED; } static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) { @@ -2446,8 +2431,7 @@ int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_ *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey); if (*ppTagIdxKey == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + return terrno = TSDB_CODE_OUT_OF_MEMORY; } (*ppTagIdxKey)->suid = suid; @@ -2667,7 +2651,7 @@ _err: metaULock(pMeta); metaError("vgId:%d, failed to handle meta entry since %s at line:%d, ver:%" PRId64 ", uid:%" PRId64 ", name:%s", TD_VID(pMeta->pVnode), terrstr(), line, pME->version, pME->uid, pME->name); - return -1; + return TSDB_CODE_FAILED; } int32_t colCompressDebug(SHashObj *pColCmprObj) { @@ -2703,7 +2687,7 @@ int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) { if (rc < 0) { taosHashClear(pColCmprObj); metaULock(pMeta); - return -1; + return TSDB_CODE_FAILED; } int64_t version = ((SUidIdxVal *)pData)[0].version; rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData); @@ -2721,7 +2705,7 @@ int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) { tdbFree(pData); metaULock(pMeta); taosHashClear(pColCmprObj); - return -1; + return rc; } if (useCompress(e.type)) { SColCmprWrapper *p = &e.colCmpr; diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index ea9209c6b4..5ef3e18058 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -280,12 +280,13 @@ int32_t vnodeRestoreVgroupId(const char *srcPath, const char *dstPath, int32_t s int32_t diskPrimary, STfs *pTfs) { SVnodeInfo info = {0}; char dir[TSDB_FILENAME_LEN] = {0}; + int32_t code = 0; vnodeGetPrimaryDir(dstPath, diskPrimary, pTfs, dir, TSDB_FILENAME_LEN); if (vnodeLoadInfo(dir, &info) == 0) { if (info.config.vgId != dstVgId) { vError("vgId:%d, unexpected vnode config.vgId:%d", dstVgId, info.config.vgId); - return -1; + return TSDB_CODE_FAILED; } return dstVgId; } @@ -301,13 +302,13 @@ int32_t vnodeRestoreVgroupId(const char *srcPath, const char *dstPath, int32_t s return srcVgId; } else if (info.config.vgId != dstVgId) { vError("vgId:%d, unexpected vnode config.vgId:%d", dstVgId, info.config.vgId); - return -1; + return TSDB_CODE_FAILED; } vInfo("vgId:%d, rename %s to %s", dstVgId, srcPath, dstPath); if (vnodeRenameVgroupId(srcPath, dstPath, srcVgId, dstVgId, diskPrimary, pTfs) < 0) { vError("vgId:%d, failed to rename vnode from %s to %s since %s", dstVgId, srcPath, dstPath, tstrerror(terrno)); - return -1; + return TSDB_CODE_FAILED; } return dstVgId; @@ -332,8 +333,7 @@ static int32_t vnodeCheckDisk(int32_t diskPrimary, STfs *pTfs) { } if (diskPrimary < 0 || diskPrimary >= ndisk) { vError("disk:%d is unavailable from the %d disks mounted at level 0", diskPrimary, ndisk); - terrno = TSDB_CODE_FS_INVLD_CFG; - return -1; + return terrno = TSDB_CODE_FS_INVLD_CFG; } return 0; } diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index fb09a46e75..45b2abeb24 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -513,6 +513,7 @@ int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) { } int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) { + int32_t code = 0; void *ptr = NULL; void *pReq; int32_t len; @@ -520,8 +521,7 @@ int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg if (ver <= pVnode->state.applied) { vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver, pVnode->state.applied); - terrno = TSDB_CODE_VND_DUP_REQUEST; - return -1; + return terrno = TSDB_CODE_VND_DUP_REQUEST; } vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64 @@ -693,7 +693,7 @@ int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg break; default: vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType); - return -1; + return TSDB_CODE_INVALID_MSG; } vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code, @@ -701,21 +701,24 @@ int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg walApplyVer(pVnode->pWal, ver); - if (tqPushMsg(pVnode->pTq, pMsg->msgType) < 0) { + code = tqPushMsg(pVnode->pTq, pMsg->msgType); + if (code) { vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno)); - return -1; + return code; } // commit if need if (needCommit) { vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver); - if (vnodeAsyncCommit(pVnode) < 0) { + code = vnodeAsyncCommit(pVnode); + if (code) { vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno)); goto _err; } // start a new one - if (vnodeBegin(pVnode) < 0) { + code = vnodeBegin(pVnode); + if (code) { vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno)); goto _err; } @@ -727,7 +730,7 @@ _exit: _err: vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), tstrerror(terrno), ver); - return -1; + return code; } int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) { @@ -987,11 +990,18 @@ static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void if (terrno < 0) goto _end; strncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN); void *p = taosArrayPush(pNames, buf); + if (p == NULL) { + goto _end; + } + expiredTb.name = p; if (mr.me.type == TSDB_CHILD_TABLE) { expiredTb.suid = mr.me.ctbEntry.suid; } - taosArrayPush(rsp.pExpiredTbs, &expiredTb); + + if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) { + goto _end; + } } int32_t ret = 0; @@ -1017,6 +1027,7 @@ _end: } static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { + int32_t code = 0; SVCreateStbReq req = {0}; SDecoder coder; @@ -1028,18 +1039,20 @@ static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, // decode and process req tDecoderInit(&coder, pReq, len); - if (tDecodeSVCreateStbReq(&coder, &req) < 0) { - pRsp->code = terrno; + code = tDecodeSVCreateStbReq(&coder, &req); + if (code) { + pRsp->code = code; goto _err; } - if (metaCreateSTable(pVnode->pMeta, ver, &req) < 0) { - pRsp->code = terrno; + code = metaCreateSTable(pVnode->pMeta, ver, &req); + if (code) { + pRsp->code = code; goto _err; } - if (tdProcessRSmaCreate(pVnode->pSma, &req) < 0) { - pRsp->code = terrno; + if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) { + pRsp->code = code; goto _err; } @@ -1048,7 +1061,7 @@ static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, _err: tDecoderClear(&coder); - return -1; + return code; } static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, @@ -1100,7 +1113,11 @@ static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, goto _exit; } strcpy(str, pCreateReq->name); - taosArrayPush(tbNames, &str); + if (taosArrayPush(tbNames, &str) == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + rcode = -1; + goto _exit; + } } // validate hash @@ -1185,6 +1202,7 @@ _exit: } static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { + int32_t code = 0; SVCreateStbReq req = {0}; SDecoder dc = {0}; @@ -1196,16 +1214,17 @@ static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, tDecoderInit(&dc, pReq, len); // decode req - if (tDecodeSVCreateStbReq(&dc, &req) < 0) { - terrno = TSDB_CODE_INVALID_MSG; + code = tDecodeSVCreateStbReq(&dc, &req); + if (code) { tDecoderClear(&dc); - return -1; + return code; } - if (metaAlterSTable(pVnode->pMeta, ver, &req) < 0) { - pRsp->code = terrno; + code = metaAlterSTable(pVnode->pMeta, ver, &req); + if (code) { + pRsp->code = code; tDecoderClear(&dc); - return -1; + return code; } tDecoderClear(&dc); @@ -2224,6 +2243,7 @@ _err: static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { SVCreateStbReq req = {0}; SDecoder dc = {0}; + int32_t code = 0; pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP; pRsp->code = TSDB_CODE_SUCCESS; @@ -2233,35 +2253,38 @@ static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pRe tDecoderInit(&dc, pReq, len); // decode req if (tDecodeSVCreateStbReq(&dc, &req) < 0) { - terrno = TSDB_CODE_INVALID_MSG; tDecoderClear(&dc); - return -1; + return terrno = TSDB_CODE_INVALID_MSG; } - if (metaAddIndexToSTable(pVnode->pMeta, ver, &req) < 0) { - pRsp->code = terrno; + + code = metaAddIndexToSTable(pVnode->pMeta, ver, &req); + if (code) { + pRsp->code = code; goto _err; } tDecoderClear(&dc); return 0; + _err: tDecoderClear(&dc); - return -1; + return code; } static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { SDropIndexReq req = {0}; + int32_t code = 0; pRsp->msgType = TDMT_VND_DROP_INDEX_RSP; pRsp->code = TSDB_CODE_SUCCESS; pRsp->pCont = NULL; pRsp->contLen = 0; - if (tDeserializeSDropIdxReq(pReq, len, &req)) { - terrno = TSDB_CODE_INVALID_MSG; - return -1; + if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) { + return code; } - if (metaDropIndexFromSTable(pVnode->pMeta, ver, &req) < 0) { - pRsp->code = terrno; - return -1; + code = metaDropIndexFromSTable(pVnode->pMeta, ver, &req); + if (code) { + pRsp->code = code; + return code; } return TSDB_CODE_SUCCESS; } @@ -2290,20 +2313,17 @@ static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pR static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) { SSyncState syncState = syncGetState(pVnode->sync); if (syncState.state != TAOS_SYNC_STATE_LEADER) { - terrno = TSDB_CODE_SYN_NOT_LEADER; - return -1; + return terrno = TSDB_CODE_SYN_NOT_LEADER; } char token[TSDB_ARB_TOKEN_SIZE] = {0}; if (vnodeGetArbToken(pVnode, token) != 0) { - terrno = TSDB_CODE_NOT_FOUND; - return -1; + return terrno = TSDB_CODE_NOT_FOUND; } if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 && strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) { - terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH; - return -1; + return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH; } terrno = TSDB_CODE_SUCCESS; @@ -2324,9 +2344,9 @@ static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t l SVArbCheckSyncReq syncReq = {0}; - if (tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq) != 0) { - terrno = TSDB_CODE_INVALID_MSG; - return -1; + code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq); + if (code) { + return terrno = code; } pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP; diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 4dd3d3298c..187d65d975 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -114,7 +114,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, char const *tbname, SPg ret = tdbBegin(pEnv, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED); if (ret < 0) { tdbOsFree(pBt); - return -1; + return ret; } SBtreeInitPageArg zArg; @@ -124,7 +124,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, char const *tbname, SPg if (ret < 0) { tdbAbort(pEnv, txn); tdbOsFree(pBt); - return -1; + return ret; } ret = tdbPagerWrite(pPager, pPage); @@ -132,7 +132,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, char const *tbname, SPg tdbError("failed to write page since %s", terrstr()); tdbAbort(pEnv, txn); tdbOsFree(pBt); - return -1; + return ret; } if (strcmp(TDB_MAINDB_NAME, tbname)) { @@ -145,20 +145,23 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, char const *tbname, SPg if (ret < 0) { tdbAbort(pEnv, txn); tdbOsFree(pBt); - return -1; + return ret; } } tdbPCacheRelease(pPager->pCache, pPage, txn); - tdbCommit(pPager->pEnv, txn); - tdbPostCommit(pPager->pEnv, txn); + ret = tdbCommit(pPager->pEnv, txn); + if (ret) return ret; + + ret = tdbPostCommit(pPager->pEnv, txn); + if (ret) return ret; } if (pgno == 0) { tdbError("tdb/btree-open: pgno cannot be zero."); tdbOsFree(pBt); - return -1; + ASSERT(0); } pBt->root = pgno; /* @@ -200,7 +203,7 @@ int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, in if (ret < 0) { tdbBtcClose(&btc); tdbError("tdb/btree-insert: btc move to failed with ret: %d.", ret); - return -1; + return ret; } if (btc.idx == -1) { @@ -212,7 +215,7 @@ int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, in // dup key not allowed with insert tdbBtcClose(&btc); tdbError("tdb/btree-insert: dup key. pKey: %p, kLen: %d, btc: %p, pTxn: %p", pKey, kLen, &btc, pTxn); - return -1; + return TSDB_CODE_DUP_KEY; } } @@ -220,7 +223,7 @@ int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, in if (ret < 0) { tdbBtcClose(&btc); tdbError("tdb/btree-insert: btc upsert failed with ret: %d.", ret); - return -1; + return ret; } tdbBtcClose(&btc); @@ -245,18 +248,19 @@ int tdbBtreeDelete(SBTree *pBt, const void *pKey, int kLen, TXN *pTxn) { if (ret < 0) { tdbBtcClose(&btc); tdbError("tdb/btree-delete: btc move to failed with ret: %d.", ret); - return -1; + return ret; } if (btc.idx < 0 || c != 0) { tdbBtcClose(&btc); - return -1; + return TSDB_CODE_NOT_FOUND; } // delete the key - if (tdbBtcDelete(&btc) < 0) { + ret = tdbBtcDelete(&btc); + if (ret < 0) { tdbBtcClose(&btc); - return -1; + return ret; } /* SArray *ofps = btc.coder.ofps; @@ -341,13 +345,12 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL if (ret < 0) { tdbBtcClose(&btc); tdbError("tdb/btree-pget: btc move to failed with ret: %d.", ret); - return -1; + return ret; } if (btc.idx < 0 || cret) { tdbBtcClose(&btc); - - return -1; + return TSDB_CODE_NOT_FOUND; } pCell = tdbPageGetCell(btc.pPage, btc.idx); @@ -355,7 +358,7 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL if (ret < 0) { tdbBtcClose(&btc); tdbError("tdb/btree-pget: decode cell failed with ret: %d.", ret); - return -1; + return ret; } if (ppKey) { @@ -363,7 +366,7 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL if (pTKey == NULL) { tdbBtcClose(&btc); tdbError("tdb/btree-pget: realloc pTKey failed."); - return -1; + return terrno; } *ppKey = pTKey; *pkLen = cd.kLen; @@ -375,7 +378,7 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL if (pTVal == NULL) { tdbBtcClose(&btc); tdbError("tdb/btree-pget: realloc pTVal failed."); - return -1; + return terrno; } *ppVal = pTVal; *vLen = cd.vLen; @@ -495,7 +498,7 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild, TXN zArg.pBt = pBt; ret = tdbPagerFetchPage(pPager, &pgnoChild, &pChild, tdbBtreeInitPage, &zArg, pTxn); if (ret < 0) { - return -1; + return ret; } if (!leaf) { @@ -505,7 +508,7 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild, TXN ret = tdbPagerWrite(pPager, pChild); if (ret < 0) { tdbError("failed to write page since %s", terrstr()); - return -1; + return ret; } // Copy the root page content to the child page @@ -516,7 +519,7 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild, TXN zArg.pBt = pBt; ret = tdbBtreeInitPage(pRoot, &zArg, 0); if (ret < 0) { - return -1; + return ret; } pIntHdr = (SIntHdr *)(pRoot->pData); @@ -557,13 +560,13 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx } for (int i = 0; i < nOlds; i++) { if (ASSERT(sIdx + i <= nCells)) { - return -1; + return TSDB_CODE_FAILED; } SPgno pgno; if (sIdx + i == nCells) { if (ASSERT(!TDB_BTREE_PAGE_IS_LEAF(pParent))) { - return -1; + return TSDB_CODE_FAILED; } pgno = ((SIntHdr *)(pParent->pData))->pgno; } else { @@ -575,13 +578,13 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx &((SBtreeInitPageArg){.pBt = pBt, .flags = 0}), pTxn); if (ret < 0) { tdbError("tdb/btree-balance: fetch page failed with ret: %d.", ret); - return -1; + return TSDB_CODE_FAILED; } ret = tdbPagerWrite(pBt->pPager, pOlds[i]); if (ret < 0) { tdbError("failed to write page since %s", terrstr()); - return -1; + return TSDB_CODE_FAILED; } } // copy the parent key out if child pages are not leaf page @@ -608,7 +611,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx ret = tdbPagerWrite(pBt->pPager, pParent); if (ret < 0) { tdbError("failed to write page since %s", terrstr()); - return -1; + return ret; } // drop the cells on parent page @@ -718,7 +721,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx } if (ASSERT(infoNews[iNew - 1].cnt > 0)) { - return -1; + return TSDB_CODE_FAILED; } if (infoNews[iNew].size + szRCell >= infoNews[iNew - 1].size - szRCell) { @@ -763,13 +766,13 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx ret = tdbPagerFetchPage(pBt->pPager, &pgno, pNews + iNew, tdbBtreeInitPage, &iarg, pTxn); if (ret < 0) { tdbError("tdb/btree-balance: fetch page failed with ret: %d.", ret); - return -1; + return ret; } ret = tdbPagerWrite(pBt->pPager, pNews[iNew]); if (ret < 0) { tdbError("failed to write page since %s", terrstr()); - return -1; + return ret; } } } @@ -808,10 +811,10 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx szCell = tdbBtreeCellSize(pPage, pCell, 0, NULL, NULL); if (ASSERT(nNewCells <= infoNews[iNew].cnt)) { - return -1; + return TSDB_CODE_FAILED; } if (ASSERT(iNew < nNews)) { - return -1; + return TSDB_CODE_FAILED; } if (nNewCells < infoNews[iNew].cnt) { @@ -852,10 +855,10 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx } } else { if (ASSERT(childNotLeaf)) { - return -1; + return TSDB_CODE_FAILED; } if (ASSERT(iNew < nNews - 1)) { - return -1; + return TSDB_CODE_FAILED; } // set current new page right-most child @@ -863,7 +866,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx // insert to parent as divider cell if (ASSERT(iNew < nNews - 1)) { - return -1; + return TSDB_CODE_FAILED; } ((SPgno *)pCell)[0] = TDB_PAGE_PGNO(pNews[iNew]); tdbPageInsertCell(pParent, sIdx++, pCell, szCell, 0); @@ -880,7 +883,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx if (childNotLeaf) { if (ASSERT(TDB_PAGE_TOTAL_CELLS(pNews[nNews - 1]) == infoNews[nNews - 1].cnt)) { - return -1; + return TSDB_CODE_FAILED; } ((SIntHdr *)(pNews[nNews - 1]->pData))->pgno = rPgno; @@ -961,7 +964,7 @@ static int tdbBtreeBalance(SBTC *pBtc) { ret = tdbBtreeBalanceDeeper(pBtc->pBt, pPage, &(pBtc->pgStack[1]), pBtc->pTxn); if (ret < 0) { - return -1; + return ret; } pBtc->idx = 0; @@ -975,7 +978,7 @@ static int tdbBtreeBalance(SBTC *pBtc) { ret = tdbBtreeBalanceNonRoot(pBtc->pBt, pParent, pBtc->idxStack[pBtc->iPage - 1], pBtc->pTxn); if (ret < 0) { - return -1; + return ret; } tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage, pBtc->pTxn); @@ -998,14 +1001,14 @@ static int tdbFetchOvflPage(SPgno *pPgno, SPage **ppOfp, TXN *pTxn, SBTree *pBt) iArg.flags = TDB_FLAG_ADD(0, TDB_BTREE_OVFL); ret = tdbPagerFetchPage(pBt->pPager, pPgno, ppOfp, tdbBtreeInitPage, &iArg, pTxn); if (ret < 0) { - return -1; + return ret; } // mark dirty ret = tdbPagerWrite(pBt->pPager, *ppOfp); if (ret < 0) { tdbError("failed to write page since %s", terrstr()); - return -1; + return ret; } tdbPCacheRelease(pBt->pPager->pCache, *ppOfp, pTxn); @@ -1021,7 +1024,7 @@ static int tdbLoadOvflPage(SPgno *pPgno, SPage **ppOfp, TXN *pTxn, SBTree *pBt) iArg.flags = TDB_FLAG_ADD(0, TDB_BTREE_OVFL); ret = tdbPagerFetchPage(pBt->pPager, pPgno, ppOfp, tdbBtreeInitPage, &iArg, pTxn); if (ret < 0) { - return -1; + return ret; } return ret; @@ -1058,13 +1061,13 @@ static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const ret = tdbFetchOvflPage(&pgno, &ofp, pTxn, pBt); if (ret < 0) { - return -1; + return ret; } // local buffer for cell SCell *pBuf = tdbRealloc(NULL, pBt->pageSize); if (pBuf == NULL) { - return -1; + return ret; } int nLeft = nPayload; @@ -1078,7 +1081,7 @@ static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const if (nLocal > nHeader + kLen + sizeof(SPgno)) { if (ASSERT(pVal != NULL && vLen != 0)) { tdbFree(pBuf); - return -1; + return TSDB_CODE_FAILED; } memcpy(pCell + nHeader + kLen, pVal, nLocal - nHeader - kLen - sizeof(SPgno)); nLeft -= nLocal - nHeader - kLen - sizeof(SPgno); @@ -1103,7 +1106,7 @@ static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const ret = tdbFetchOvflPage(&pgno, &nextOfp, pTxn, pBt); if (ret < 0) { tdbFree(pBuf); - return -1; + return ret; } } else { pgno = 0; @@ -1115,7 +1118,7 @@ static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const ret = tdbPageInsertCell(ofp, 0, pBuf, bytes + sizeof(pgno), 0); if (ret < 0) { tdbFree(pBuf); - return -1; + return ret; } ofp = nextOfp; @@ -1163,7 +1166,7 @@ static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const ret = tdbFetchOvflPage(&pgno, &nextOfp, pTxn, pBt); if (ret < 0) { tdbFree(pBuf); - return -1; + return ret; } } } else { @@ -1171,7 +1174,7 @@ static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const ret = tdbFetchOvflPage(&pgno, &nextOfp, pTxn, pBt); if (ret < 0) { tdbFree(pBuf); - return -1; + return ret; } } @@ -1179,7 +1182,7 @@ static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const ret = tdbPageInsertCell(ofp, 0, pBuf, bytes + sizeof(pgno), 0); if (ret < 0) { - return -1; + return ret; } ofp = nextOfp; @@ -1203,7 +1206,7 @@ static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const ret = tdbFetchOvflPage(&pgno, &nextOfp, pTxn, pBt); if (ret < 0) { tdbFree(pBuf); - return -1; + return ret; } } else { pgno = 0; @@ -1214,13 +1217,13 @@ static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const if (ofp == NULL) { tdbFree(pBuf); - return -1; + return ret; } ret = tdbPageInsertCell(ofp, 0, pBuf, bytes + sizeof(pgno), 0); if (ret < 0) { tdbFree(pBuf); - return -1; + return ret; } ofp = nextOfp; @@ -1245,13 +1248,13 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo int ret; if (ASSERT(pPage->kLen == TDB_VARIANT_LEN || pPage->kLen == kLen)) { - return -1; + return TSDB_CODE_FAILED; } if (ASSERT(pPage->vLen == TDB_VARIANT_LEN || pPage->vLen == vLen)) { - return -1; + return TSDB_CODE_FAILED; } if (ASSERT(pKey != NULL && kLen > 0)) { - return -1; + return TSDB_CODE_FAILED; } nPayload = 0; @@ -1263,7 +1266,7 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo if (!leaf) { if (pPage->vLen != sizeof(SPgno)) { tdbError("tdb/btree-encode-cell: invalid cell."); - return -1; + return TSDB_CODE_INVALID_PARA; } ((SPgno *)(pCell + nHeader))[0] = ((SPgno *)pVal)[0]; @@ -1290,7 +1293,7 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo if (ret < 0) { // TODO tdbError("tdb/btree-encode-cell: encode payload failed with ret: %d.", ret); - return -1; + return ret; } *szCell = nHeader + nPayload; @@ -1309,7 +1312,7 @@ static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader, if (pDecoder->pVal) { if (TDB_BTREE_PAGE_IS_LEAF(pPage)) { tdbError("tdb/btree-decode-payload: leaf page with non-null pVal."); - return -1; + return TSDB_CODE_INVALID_DATA_FMT; } nPayload = pDecoder->kLen; } else { @@ -1344,7 +1347,7 @@ static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader, // read partial val to local pDecoder->pVal = tdbRealloc(pDecoder->pVal, vLen); if (pDecoder->pVal == NULL) { - return -1; + return terrno; } TDB_CELLDECODER_SET_FREE_VAL(pDecoder); @@ -1361,7 +1364,7 @@ static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader, while (pgno != 0) { ret = tdbLoadOvflPage(&pgno, &ofp, pTxn, pBt); if (ret < 0) { - return -1; + return ret; } /* if (pDecoder->ofps) { @@ -1389,7 +1392,7 @@ static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader, // load partial key and nextPgno pDecoder->pKey = tdbRealloc(pDecoder->pKey, kLen); if (pDecoder->pKey == NULL) { - return -1; + return terrno; } TDB_CELLDECODER_SET_FREE_KEY(pDecoder); @@ -1406,7 +1409,7 @@ static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader, // printf("tdb decode-ofp, pTxn: %p, pgno:%u by cell:%p\n", pTxn, pgno, pCell); ret = tdbLoadOvflPage(&pgno, &ofp, pTxn, pBt); if (ret < 0) { - return -1; + return ret; } /* if (pDecoder->ofps) { @@ -1439,7 +1442,7 @@ static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader, // read partial val to local pDecoder->pVal = tdbRealloc(pDecoder->pVal, vLen); if (pDecoder->pVal == NULL) { - return -1; + return terrno; } TDB_CELLDECODER_SET_FREE_VAL(pDecoder); @@ -1459,7 +1462,7 @@ static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader, while (nLeft > 0) { ret = tdbLoadOvflPage(&pgno, &ofp, pTxn, pBt); if (ret < 0) { - return -1; + return ret; } ofpCell = tdbPageGetCell(ofp, 0); @@ -1480,7 +1483,7 @@ static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader, if (!pDecoder->pVal) { pDecoder->pVal = tdbRealloc(pDecoder->pVal, vLen); if (pDecoder->pVal == NULL) { - return -1; + return terrno; } TDB_CELLDECODER_SET_FREE_VAL(pDecoder); } @@ -1529,7 +1532,7 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD if (!leaf) { if (pPage->vLen != sizeof(SPgno)) { tdbError("tdb/btree-decode-cell: invalid cell."); - return -1; + return TSDB_CODE_INVALID_DATA_FMT; } pDecoder->pgno = ((SPgno *)(pCell + nHeader))[0]; @@ -1546,7 +1549,7 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD if (pPage->vLen == TDB_VARIANT_LEN) { if (!leaf) { tdbError("tdb/btree-decode-cell: not a leaf page."); - return -1; + return TSDB_CODE_INVALID_DATA_FMT; } nHeader += tdbGetVarInt(pCell + nHeader, &(pDecoder->vLen)); } else { @@ -1556,7 +1559,7 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD // 2. Decode payload part ret = tdbBtreeDecodePayload(pPage, pCell, nHeader, pDecoder, pTxn, pBt); if (ret < 0) { - return -1; + return ret; } return 0; @@ -1610,7 +1613,7 @@ static int tdbBtreeCellSize(const SPage *pPage, SCell *pCell, int dropOfp, TXN * while (pgno != 0) { ret = tdbLoadOvflPage(&pgno, &ofp, pTxn, pBt); if (ret < 0) { - return -1; + return ret; } SCell *ofpCell = tdbPageGetCell(ofp, 0); @@ -1627,7 +1630,7 @@ static int tdbBtreeCellSize(const SPage *pPage, SCell *pCell, int dropOfp, TXN * ret = tdbPagerWrite(pBt->pPager, ofp); if (ret < 0) { tdbError("failed to write page since %s", terrstr()); - return -1; + return ret; } /* tdbPageDropCell(ofp, 0, pTxn, pBt); @@ -1664,12 +1667,13 @@ int tdbBtcOpen(SBTC *pBtc, SBTree *pBt, TXN *pTxn) { if (pTxn == NULL) { TXN *pTxn = tdbOsCalloc(1, sizeof(*pTxn)); if (!pTxn) { - return -1; + return terrno; } - if (tdbTxnOpen(pTxn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, 0) < 0) { + int32_t ret = tdbTxnOpen(pTxn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, 0); + if (ret < 0) { tdbOsFree(pTxn); - return -1; + return ret; } pBtc->pTxn = pTxn; @@ -1698,12 +1702,12 @@ int tdbBtcMoveToFirst(SBTC *pBtc) { &((SBtreeInitPageArg){.pBt = pBt, .flags = TDB_BTREE_ROOT | TDB_BTREE_LEAF}), pBtc->pTxn); if (ret < 0) { tdbError("tdb/btc-move-tofirst: fetch page failed with ret: %d.", ret); - return -1; + return ret; } if (!TDB_BTREE_PAGE_IS_ROOT(pBtc->pPage)) { tdbError("tdb/btc-move-tofirst: not a root page"); - return -1; + return ret; } pBtc->iPage = 0; @@ -1713,7 +1717,7 @@ int tdbBtcMoveToFirst(SBTC *pBtc) { // no any data, point to an invalid position if (!TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { tdbError("tdb/btc-move-to-first: not a leaf page."); - return -1; + return TSDB_CODE_FAILED; } pBtc->idx = -1; @@ -1722,7 +1726,7 @@ int tdbBtcMoveToFirst(SBTC *pBtc) { } else { // TODO tdbError("tdb/btc-move-to-first: move from a dirty cursor."); - return -1; + return TSDB_CODE_FAILED; #if 0 // move from a position int iPage = 0; @@ -1755,7 +1759,7 @@ int tdbBtcMoveToFirst(SBTC *pBtc) { ret = tdbBtcMoveDownward(pBtc); if (ret < 0) { tdbError("tdb/btc-move-tofirst: btc move downward failed with ret: %d.", ret); - return -1; + return ret; } pBtc->idx = 0; @@ -1780,7 +1784,7 @@ int tdbBtcMoveToLast(SBTC *pBtc) { &((SBtreeInitPageArg){.pBt = pBt, .flags = TDB_BTREE_ROOT | TDB_BTREE_LEAF}), pBtc->pTxn); if (ret < 0) { tdbError("tdb/btc-move-tolast: fetch page failed with ret: %d.", ret); - return -1; + return ret; } nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); @@ -1791,7 +1795,7 @@ int tdbBtcMoveToLast(SBTC *pBtc) { // no data at all, point to an invalid position if (!TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { tdbError("tdb/btc-move-to-last: not a leaf page."); - return -1; + return TSDB_CODE_FAILED; } pBtc->idx = -1; @@ -1800,7 +1804,7 @@ int tdbBtcMoveToLast(SBTC *pBtc) { } else { // TODO tdbError("tdb/btc-move-to-last: move from a dirty cursor."); - return -1; + return TSDB_CODE_FAILED; #if 0 int iPage = 0; @@ -1838,7 +1842,7 @@ int tdbBtcMoveToLast(SBTC *pBtc) { ret = tdbBtcMoveDownward(pBtc); if (ret < 0) { tdbError("tdb/btc-move-tolast: btc move downward failed with ret: %d.", ret); - return -1; + return ret; } nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage); @@ -1860,7 +1864,7 @@ int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { // current cursor points to an invalid position if (pBtc->idx < 0) { - return -1; + return TSDB_CODE_FAILED; } pCell = tdbPageGetCell(pBtc->pPage, pBtc->idx); @@ -1868,12 +1872,12 @@ int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { ret = tdbBtreeDecodeCell(pBtc->pPage, pCell, &cd, pBtc->pTxn, pBtc->pBt); if (ret < 0) { tdbError("tdb/btree-next: decode cell failed with ret: %d.", ret); - return -1; + return ret; } pKey = tdbRealloc(*ppKey, cd.kLen); if (pKey == NULL) { - return -1; + return terrno; } *ppKey = pKey; @@ -1885,7 +1889,7 @@ int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { pVal = tdbRealloc(*ppVal, cd.vLen); if (pVal == NULL) { tdbFree(pKey); - return -1; + return terrno; } memcpy(pVal, cd.pVal, cd.vLen); @@ -1909,7 +1913,7 @@ int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { ret = tdbBtcMoveToNext(pBtc); if (ret < 0) { tdbError("tdb/btree-next: btc move to next failed with ret: %d.", ret); - return -1; + return ret; } return 0; @@ -1923,7 +1927,7 @@ int tdbBtreePrev(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { // current cursor points to an invalid position if (pBtc->idx < 0) { - return -1; + return TSDB_CODE_FAILED; } pCell = tdbPageGetCell(pBtc->pPage, pBtc->idx); @@ -1931,12 +1935,12 @@ int tdbBtreePrev(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { ret = tdbBtreeDecodeCell(pBtc->pPage, pCell, &cd, pBtc->pTxn, pBtc->pBt); if (ret < 0) { tdbError("tdb/btree-prev: decode cell failed with ret: %d.", ret); - return -1; + return ret; } pKey = tdbRealloc(*ppKey, cd.kLen); if (pKey == NULL) { - return -1; + return terrno; } *ppKey = pKey; @@ -1948,7 +1952,7 @@ int tdbBtreePrev(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { pVal = tdbRealloc(*ppVal, cd.vLen); if (pVal == NULL) { tdbFree(pKey); - return -1; + return terrno; } *ppVal = pVal; @@ -1959,7 +1963,7 @@ int tdbBtreePrev(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) { ret = tdbBtcMoveToPrev(pBtc); if (ret < 0) { tdbError("tdb/btree-prev: btc move to prev failed with ret: %d.", ret); - return -1; + return ret; } return 0; @@ -1972,10 +1976,10 @@ int tdbBtcMoveToNext(SBTC *pBtc) { if (!TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { tdbError("tdb/btc-move-to-next: not a leaf page."); - return -1; + return TSDB_CODE_FAILED; } - if (pBtc->idx < 0) return -1; + if (pBtc->idx < 0) return TSDB_CODE_FAILED; pBtc->idx++; if (pBtc->idx < TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) { @@ -1994,7 +1998,7 @@ int tdbBtcMoveToNext(SBTC *pBtc) { if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { tdbError("tdb/btree-decode-cell: should not be a leaf page here."); - return -1; + return TSDB_CODE_FAILED; } if (pBtc->idx <= TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) { break; @@ -2008,7 +2012,7 @@ int tdbBtcMoveToNext(SBTC *pBtc) { ret = tdbBtcMoveDownward(pBtc); if (ret < 0) { tdbError("tdb/btc-move-tonext: btc move downward failed with ret: %d.", ret); - return -1; + return ret; } pBtc->idx = 0; @@ -2018,7 +2022,7 @@ int tdbBtcMoveToNext(SBTC *pBtc) { } int tdbBtcMoveToPrev(SBTC *pBtc) { - if (pBtc->idx < 0) return -1; + if (pBtc->idx < 0) return TSDB_CODE_FAILED; pBtc->idx--; if (pBtc->idx >= 0) { @@ -2061,17 +2065,17 @@ static int tdbBtcMoveDownward(SBTC *pBtc) { if (pBtc->idx < 0) { tdbError("tdb/btc-move-downward: invalid idx: %d.", pBtc->idx); - return -1; + return TSDB_CODE_FAILED; } if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) { tdbError("tdb/btc-move-downward: should not be a leaf page here."); - return -1; + return TSDB_CODE_FAILED; } if (TDB_BTREE_PAGE_IS_OVFL(pBtc->pPage)) { tdbError("tdb/btc-move-downward: should not be a ovfl page here."); - return -1; + return TSDB_CODE_FAILED; } if (pBtc->idx < TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) { @@ -2083,7 +2087,7 @@ static int tdbBtcMoveDownward(SBTC *pBtc) { if (!pgno) { tdbError("tdb/btc-move-downward: invalid pgno."); - return -1; + return TSDB_CODE_FAILED; } pBtc->pgStack[pBtc->iPage] = pBtc->pPage; @@ -2096,14 +2100,14 @@ static int tdbBtcMoveDownward(SBTC *pBtc) { &((SBtreeInitPageArg){.pBt = pBtc->pBt, .flags = 0}), pBtc->pTxn); if (ret < 0) { tdbError("tdb/btc-move-downward: fetch page failed with ret: %d.", ret); - return -1; + return TSDB_CODE_FAILED; } return 0; } static int tdbBtcMoveUpward(SBTC *pBtc) { - if (pBtc->iPage == 0) return -1; + if (pBtc->iPage == 0) return TSDB_CODE_FAILED; tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage, pBtc->pTxn); @@ -2118,7 +2122,7 @@ int tdbBtcGet(SBTC *pBtc, const void **ppKey, int *kLen, const void **ppVal, int SCell *pCell; if (pBtc->idx < 0 || pBtc->idx >= TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) { - return -1; + return TSDB_CODE_FAILED; } pCell = tdbPageGetCell(pBtc->pPage, pBtc->idx); @@ -2152,14 +2156,14 @@ int tdbBtcDelete(SBTC *pBtc) { if (idx < 0 || idx >= nCells) { tdbError("tdb/btc-delete: idx: %d out of range[%d, %d).", idx, 0, nCells); - return -1; + return TSDB_CODE_FAILED; } // drop the cell on the leaf ret = tdbPagerWrite(pPager, pBtc->pPage); if (ret < 0) { tdbError("failed to write page since %s", terrstr()); - return -1; + return ret; } bool destroyOfps = false; @@ -2200,7 +2204,7 @@ int tdbBtcDelete(SBTC *pBtc) { ret = tdbPagerWrite(pPager, pPage); if (ret < 0) { tdbError("failed to write page since %s", terrstr()); - return -1; + return ret; } // update the cell with new key @@ -2211,7 +2215,7 @@ int tdbBtcDelete(SBTC *pBtc) { if (ret < 0) { tdbOsFree(pCell); tdbError("tdb/btc-delete: page update cell failed with ret: %d.", ret); - return -1; + return ret; } tdbOsFree(pCell); @@ -2229,7 +2233,7 @@ int tdbBtcDelete(SBTC *pBtc) { ret = tdbBtreeBalance(pBtc); if (ret < 0) { tdbError("tdb/btc-delete: btree balance failed with ret: %d.", ret); - return -1; + return ret; } } @@ -2242,7 +2246,7 @@ int tdbBtcDelete(SBTC *pBtc) { // delete the leaf page and do balance if (TDB_PAGE_TOTAL_CELLS(pBtc->pPage) != 0) { tdbError("tdb/btc-delete: page to be deleted should be empty."); - return -1; + return TSDB_CODE_FAILED; } // printf("tdb/btc-delete: btree balance delete pgno: %d.\n", TDB_PAGE_PGNO(pBtc->pPage)); @@ -2250,7 +2254,7 @@ int tdbBtcDelete(SBTC *pBtc) { ret = tdbBtreeBalance(pBtc); if (ret < 0) { tdbError("tdb/btc-delete: btree balance failed with ret: %d.", ret); - return -1; + return ret; } } } @@ -2268,7 +2272,7 @@ int tdbBtcUpsert(SBTC *pBtc, const void *pKey, int kLen, const void *pData, int if (pBtc->idx < 0) { tdbError("tdb/btc-upsert: invalid idx: %d.", pBtc->idx); - return -1; + return TSDB_CODE_FAILED; } // alloc space @@ -2276,7 +2280,7 @@ int tdbBtcUpsert(SBTC *pBtc, const void *pKey, int kLen, const void *pData, int pBuf = tdbRealloc(pBtc->pBt->pBuf, pBtc->pBt->pageSize > szBuf ? szBuf : pBtc->pBt->pageSize); if (pBuf == NULL) { tdbError("tdb/btc-upsert: realloc pBuf failed."); - return -1; + return terrno; } pBtc->pBt->pBuf = pBuf; pCell = (SCell *)pBtc->pBt->pBuf; @@ -2285,35 +2289,35 @@ int tdbBtcUpsert(SBTC *pBtc, const void *pKey, int kLen, const void *pData, int ret = tdbBtreeEncodeCell(pBtc->pPage, pKey, kLen, pData, nData, pCell, &szCell, pBtc->pTxn, pBtc->pBt); if (ret < 0) { tdbError("tdb/btc-upsert: btree encode cell failed with ret: %d.", ret); - return -1; + return ret; } // mark dirty ret = tdbPagerWrite(pBtc->pBt->pPager, pBtc->pPage); if (ret < 0) { tdbError("failed to write page since %s", terrstr()); - return -1; + return ret; } // insert or update if (insert) { if (pBtc->idx > nCells) { tdbError("tdb/btc-upsert: invalid idx: %d, nCells: %d.", pBtc->idx, nCells); - return -1; + return TSDB_CODE_FAILED; } ret = tdbPageInsertCell(pBtc->pPage, pBtc->idx, pCell, szCell, 0); } else { if (pBtc->idx >= nCells) { tdbError("tdb/btc-upsert: invalid idx: %d, nCells: %d.", pBtc->idx, nCells); - return -1; + return TSDB_CODE_FAILED; } ret = tdbPageUpdateCell(pBtc->pPage, pBtc->idx, pCell, szCell, pBtc->pTxn, pBtc->pBt); } if (ret < 0) { tdbError("tdb/btc-upsert: page insert/update cell failed with ret: %d.", ret); - return -1; + return ret; } /* bool destroyOfps = false; @@ -2327,7 +2331,7 @@ int tdbBtcUpsert(SBTC *pBtc, const void *pKey, int kLen, const void *pData, int ret = tdbBtreeBalance(pBtc); if (ret < 0) { tdbError("tdb/btc-upsert: btree balance failed with ret: %d.", ret); - return -1; + return ret; } } /* @@ -2365,7 +2369,7 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { if (ret < 0) { // TODO tdbError("tdb/btc-move-to: fetch page failed with ret: %d.", ret); - return -1; + return ret; } pBtc->iPage = 0; @@ -2375,7 +2379,7 @@ int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { } else { // TODO tdbError("tdb/btc-move-to: move from a dirty cursor."); - return -1; + return TSDB_CODE_FAILED; #if 0 SPage *pPage; int idx; @@ -2499,7 +2503,7 @@ int tdbBtcClose(SBTC *pBtc) { for (;;) { if (NULL == pBtc->pPage) { tdbError("tdb/btc-close: null ptr pPage."); - return -1; + return TSDB_CODE_FAILED; } tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage, pBtc->pTxn); diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 5d8b05ffcf..414ca42a02 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -862,7 +862,7 @@ static int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno, TXN *pTxn) { // Try to allocate from the free list of the pager ret = tdbPagerAllocFreePage(pPager, ppgno, pTxn); if (ret < 0) { - return -1; + return ret; } if (*ppgno != 0) return 0; @@ -875,7 +875,7 @@ static int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno, TXN *pTxn) { if (*ppgno == 0) { tdbError("tdb/pager:%p, alloc new page failed.", pPager); - return -1; + return TSDB_CODE_FAILED; } return 0; } @@ -907,7 +907,7 @@ static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage if (nRead < pPage->pageSize) { tdbError("tdb/pager:%p, pgno:%d, nRead:%" PRId64 "pgSize:%" PRId32, pPager, pgno, nRead, pPage->pageSize); TDB_UNLOCK_PAGE(pPage); - return -1; + return TAOS_SYSTEM_ERROR(errno); } int32_t encryptAlgorithm = pPager->pEnv->encryptAlgorithm; @@ -954,7 +954,7 @@ static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage tdbError("tdb/pager:%p, pgno:%d, nRead:%" PRId64 "pgSize:%" PRId32 " init page failed.", pPager, pgno, nRead, pPage->pageSize); TDB_UNLOCK_PAGE(pPage); - return -1; + return ret; } tmemory_barrier(); @@ -975,7 +975,7 @@ static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage } else { tdbError("tdb/pager:%p, pgno:%d, nRead:%" PRId64 "pgSize:%" PRId32 " lock page failed.", pPager, pgno, nRead, pPage->pageSize); - return -1; + return TSDB_CODE_FAILED; } return 0; @@ -1127,14 +1127,12 @@ static int tdbPagerRestore(SPager *pPager, const char *jFileName) { if (tdbOsClose(jfd) < 0) { tdbError("failed to close jfd due to %s. jFileName:%s", strerror(errno), pPager->jFileName); - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; + return terrno = TAOS_SYSTEM_ERROR(errno); } if (tdbOsRemove(jFileName) < 0 && errno != ENOENT) { tdbError("failed to remove file due to %s. jFileName:%s", strerror(errno), pPager->jFileName); - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; + return terrno = TAOS_SYSTEM_ERROR(errno); } return 0;