提交 5af1acaa 编写于 作者: Y Yifan Hao

Hash table cleanup [1/n]

A few cleanups for the hashtable implementation:
1. Correct function header comments
2. Complete argument checking for a few functions
3. Remove trailing space
上级 f4307cd2
...@@ -155,11 +155,14 @@ static void pushfrontNodeInEntryList(SHashEntry *pEntry, SHashNode *pNode); ...@@ -155,11 +155,14 @@ static void pushfrontNodeInEntryList(SHashEntry *pEntry, SHashNode *pNode);
static FORCE_INLINE bool taosHashTableEmpty(const SHashObj *pHashObj); static FORCE_INLINE bool taosHashTableEmpty(const SHashObj *pHashObj);
/** /**
* Get the next element in hash table for iterator * Initialize a hash table
* @param pIter *
* @return * @param capacity Initial capacity of the hash table
* @param fn Hash function
* @param update Whether the hash table allows in place update
* @param type Whether the hash table has per entry lock
* @return Hash table object
*/ */
SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTypeE type) { SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTypeE type) {
assert(fn != NULL); assert(fn != NULL);
if (capacity == 0) { if (capacity == 0) {
...@@ -206,7 +209,7 @@ void taosHashSetEqualFp(SHashObj *pHashObj, _equal_fn_t fp) { ...@@ -206,7 +209,7 @@ void taosHashSetEqualFp(SHashObj *pHashObj, _equal_fn_t fp) {
} }
int32_t taosHashGetSize(const SHashObj *pHashObj) { int32_t taosHashGetSize(const SHashObj *pHashObj) {
if (!pHashObj) { if (pHashObj == NULL) {
return 0; return 0;
} }
return (int32_t)atomic_load_64(&pHashObj->size); return (int32_t)atomic_load_64(&pHashObj->size);
...@@ -217,6 +220,10 @@ static FORCE_INLINE bool taosHashTableEmpty(const SHashObj *pHashObj) { ...@@ -217,6 +220,10 @@ static FORCE_INLINE bool taosHashTableEmpty(const SHashObj *pHashObj) {
} }
int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *data, size_t size) { int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *data, size_t size) {
if (pHashObj == NULL || key == NULL || keyLen == 0 || data == NULL || size == 0) {
return -1;
}
uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen); uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen);
SHashNode *pNewNode = doCreateHashNode(key, keyLen, data, size, hashVal); SHashNode *pNewNode = doCreateHashNode(key, keyLen, data, size, hashVal);
if (pNewNode == NULL) { if (pNewNode == NULL) {
...@@ -248,7 +255,9 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *da ...@@ -248,7 +255,9 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *da
SHashNode* prev = NULL; SHashNode* prev = NULL;
while (pNode) { while (pNode) {
if ((pNode->keyLen == keyLen) && ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) && pNode->removed == 0) { if ((pNode->keyLen == keyLen) &&
(*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0 &&
pNode->removed == 0) {
assert(pNode->hashVal == hashVal); assert(pNode->hashVal == hashVal);
break; break;
} }
...@@ -261,11 +270,7 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *da ...@@ -261,11 +270,7 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *da
// no data in hash table with the specified key, add it into hash table // no data in hash table with the specified key, add it into hash table
pushfrontNodeInEntryList(pe, pNewNode); pushfrontNodeInEntryList(pe, pNewNode);
if (pe->num == 0) {
assert(pe->next == NULL);
} else {
assert(pe->next != NULL); assert(pe->next != NULL);
}
if (pHashObj->type == HASH_ENTRY_LOCK) { if (pHashObj->type == HASH_ENTRY_LOCK) {
taosWUnLockLatch(&pe->latch); taosWUnLockLatch(&pe->latch);
...@@ -300,7 +305,7 @@ void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen) { ...@@ -300,7 +305,7 @@ void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen) {
} }
//TODO(yihaoDeng), merge with taosHashGetClone //TODO(yihaoDeng), merge with taosHashGetClone
void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void** d, size_t *sz) { void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void** d, size_t *sz) {
if (taosHashTableEmpty(pHashObj) || keyLen == 0 || key == NULL) { if (pHashObj == NULL || taosHashTableEmpty(pHashObj) || keyLen == 0 || key == NULL) {
return NULL; return NULL;
} }
...@@ -362,7 +367,7 @@ void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, vo ...@@ -362,7 +367,7 @@ void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, vo
} }
void* taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void* d) { void* taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void* d) {
if (taosHashTableEmpty(pHashObj) || keyLen == 0 || key == NULL) { if (pHashObj == NULL || taosHashTableEmpty(pHashObj) || keyLen == 0 || key == NULL) {
return NULL; return NULL;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册