diff --git a/include/util/thash.h b/include/util/thash.h index c6275d276c..475d03f533 100644 --- a/include/util/thash.h +++ b/include/util/thash.h @@ -28,8 +28,7 @@ typedef int32_t (*_equal_fn_t)(const void *, const void *, size_t len); typedef void (*_hash_before_fn_t)(void *); typedef void (*_hash_free_fn_t)(void *); -#define HASH_KEY_ALREADY_EXISTS (-2) -#define HASH_NODE_EXIST(code) (code == HASH_KEY_ALREADY_EXISTS) +#define HASH_NODE_EXIST(code) (code == TSDB_CODE_DUP_KEY) /** * murmur hash algorithm diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index a7d459ff31..e45ca2b872 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -744,7 +744,7 @@ static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClient req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); } - if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) < 0) { + if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) != 0) { taosMemoryFree(user); code = terrno ? terrno : TSDB_CODE_APP_ERROR; goto _return; diff --git a/source/dnode/vnode/src/sma/smaRollup.c b/source/dnode/vnode/src/sma/smaRollup.c index 214d0300e1..f68d83350b 100644 --- a/source/dnode/vnode/src/sma/smaRollup.c +++ b/source/dnode/vnode/src/sma/smaRollup.c @@ -414,7 +414,7 @@ int32_t tdRSmaProcessCreateImpl(SSma *pSma, SRSmaParam *param, int64_t suid, con goto _err; } - if (taosHashPut(RSMA_INFO_HASH(pStat), &suid, sizeof(tb_uid_t), &pRSmaInfo, sizeof(pRSmaInfo)) < 0) { + if (taosHashPut(RSMA_INFO_HASH(pStat), &suid, sizeof(tb_uid_t), &pRSmaInfo, sizeof(pRSmaInfo)) != 0) { goto _err; } @@ -540,12 +540,12 @@ static int32_t tdUidStorePut(STbUidStore *pStore, tb_uid_t suid, tb_uid_t *uid) taosArrayDestroy(pUidArray); return TSDB_CODE_FAILED; } - if (taosHashPut(pStore->uidHash, &suid, sizeof(suid), &pUidArray, sizeof(pUidArray)) < 0) { + if (taosHashPut(pStore->uidHash, &suid, sizeof(suid), &pUidArray, sizeof(pUidArray)) != 0) { return TSDB_CODE_FAILED; } } } else { - if (taosHashPut(pStore->uidHash, &suid, sizeof(suid), NULL, 0) < 0) { + if (taosHashPut(pStore->uidHash, &suid, sizeof(suid), NULL, 0) != 0) { return TSDB_CODE_FAILED; } } diff --git a/source/dnode/vnode/src/tq/tq.c b/source/dnode/vnode/src/tq/tq.c index ac57a003c5..7daf807c27 100644 --- a/source/dnode/vnode/src/tq/tq.c +++ b/source/dnode/vnode/src/tq/tq.c @@ -612,7 +612,7 @@ int32_t tqProcessAddCheckInfoReq(STQ* pTq, int64_t sversion, char* msg, int32_t return -1; } tDecoderClear(&decoder); - if (taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)) < 0) { + if (taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)) != 0) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } diff --git a/source/dnode/vnode/src/tq/tqOffset.c b/source/dnode/vnode/src/tq/tqOffset.c index 8b0e039ad5..4456359717 100644 --- a/source/dnode/vnode/src/tq/tqOffset.c +++ b/source/dnode/vnode/src/tq/tqOffset.c @@ -74,7 +74,7 @@ int32_t tqOffsetRestoreFromFile(STqOffsetStore* pStore, const char* fname) { } tDecoderClear(&decoder); - if (taosHashPut(pStore->pHash, offset.subKey, strlen(offset.subKey), &offset, sizeof(STqOffset)) < 0) { + if (taosHashPut(pStore->pHash, offset.subKey, strlen(offset.subKey), &offset, sizeof(STqOffset)) != 0) { return -1; } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 97a6239a6f..c2278649e1 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -13179,7 +13179,7 @@ static int32_t buildTagIndexForBindTags(SMsgBuf* pMsgBuf, SCreateSubTableFromFil if (code) break; - if (taosHashPut(pIdxHash, &idx, sizeof(idx), NULL, 0) < 0) { + if (taosHashPut(pIdxHash, &idx, sizeof(idx), NULL, 0) != 0) { code = terrno; goto _OUT; } diff --git a/source/libs/stream/src/streamMeta.c b/source/libs/stream/src/streamMeta.c index d0b1f6ca93..3ef5a4ac10 100644 --- a/source/libs/stream/src/streamMeta.c +++ b/source/libs/stream/src/streamMeta.c @@ -922,7 +922,7 @@ void streamMetaLoadAllTasks(SStreamMeta* pMeta) { continue; } - if (taosHashPut(pMeta->pTasksMap, &id, sizeof(id), &pTask, POINTER_BYTES) < 0) { + if (taosHashPut(pMeta->pTasksMap, &id, sizeof(id), &pTask, POINTER_BYTES) != 0) { stError("s-task:0x%x failed to put into hashTable, code:%s, continue", pTask->id.taskId, tstrerror(terrno)); taosArrayPop(pMeta->pTaskList); tFreeStreamTask(pTask); diff --git a/source/util/src/thash.c b/source/util/src/thash.c index 3a53145fc4..b509aaebc3 100644 --- a/source/util/src/thash.c +++ b/source/util/src/thash.c @@ -316,7 +316,7 @@ int32_t taosHashGetSize(const SHashObj *pHashObj) { int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, const void *data, size_t size) { if (pHashObj == NULL || key == NULL || keyLen == 0) { terrno = TSDB_CODE_INVALID_PTR; - return -1; + return TSDB_CODE_INVALID_PTR; } uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen); @@ -331,6 +331,7 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, const vo // disable resize taosHashRLock(pHashObj); + int32_t code = TSDB_CODE_SUCCESS; uint32_t slot = HASH_INDEX(hashVal, pHashObj->capacity); SHashEntry *pe = pHashObj->hashList[slot]; @@ -352,36 +353,34 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, const vo // no data in hash table with the specified key, add it into hash table SHashNode *pNewNode = doCreateHashNode(key, keyLen, data, size, hashVal); if (pNewNode == NULL) { - return -1; + terrno = TSDB_CODE_OUT_OF_MEMORY; + code = terrno; + goto _exit; } pushfrontNodeInEntryList(pe, pNewNode); - taosHashEntryWUnlock(pHashObj, pe); - - // enable resize - taosHashRUnlock(pHashObj); atomic_add_fetch_64(&pHashObj->size, 1); - - return 0; } else { // not support the update operation, return error if (pHashObj->enableUpdate) { SHashNode *pNewNode = doCreateHashNode(key, keyLen, data, size, hashVal); if (pNewNode == NULL) { - return -1; + terrno = TSDB_CODE_OUT_OF_MEMORY; + code = terrno; + goto _exit; } doUpdateHashNode(pHashObj, pe, prev, pNode, pNewNode); } else { terrno = TSDB_CODE_DUP_KEY; + code = terrno; + goto _exit; } - - taosHashEntryWUnlock(pHashObj, pe); - - // enable resize - taosHashRUnlock(pHashObj); - return pHashObj->enableUpdate ? 0 : -2; } +_exit: + taosHashEntryWUnlock(pHashObj, pe); + taosHashRUnlock(pHashObj); + return code; } static void *taosHashGetImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void **d, int32_t *size, bool addRef);