提交 bdda9415 编写于 作者: Y Yifan Hao

Hash table cleanup [8/n]

Simplify implementation of taosHashTableResize. Remove unnecessary
while loop.

* Testing
sample mysql run
taos> create database db;
Query OK, 0 of 0 row(s) in database (0.002273s)

taos> use db;
Query OK, 0 of 0 row(s) in database (0.000214s)

taos> create table t (ts timestamp, a int);
Query OK, 0 of 0 row(s) in database (0.003265s)

taos> insert into t values ('2019-07-15 00:00:00', 1);
Query OK, 1 of 1 row(s) in database (0.001309s)

taos> insert into t values ('2019-07-15 01:00:00', 2);
Query OK, 1 of 1 row(s) in database (0.000238s)

taos> select * from t;
           ts            |      a      |
========================================
 2019-07-15 00:00:00.000 |           1 |
 2019-07-15 01:00:00.000 |           2 |
Query OK, 2 row(s) in set (0.000751s)

taos> drop database db;
Query OK, 0 of 0 row(s) in database (0.002543s)
上级 8ed7d45a
......@@ -677,10 +677,6 @@ void taosHashTableResize(SHashObj *pHashObj) {
return;
}
// double the original capacity
SHashNode *pNode = NULL;
SHashNode *pNext = NULL;
int32_t newCapacity = (int32_t)(pHashObj->capacity << 1u);
if (newCapacity > HASH_MAX_CAPACITY) {
uDebug("current capacity:%lu, maximum capacity:%d, no resize applied due to limitation is reached",
......@@ -707,69 +703,41 @@ void taosHashTableResize(SHashObj *pHashObj) {
taosArrayPush(pHashObj->pMemBlock, &p);
pHashObj->capacity = newCapacity;
for (int32_t i = 0; i < pHashObj->capacity; ++i) {
SHashEntry *pe = pHashObj->hashList[i];
for (int32_t idx = 0; idx < pHashObj->capacity; ++idx) {
SHashEntry *pe = pHashObj->hashList[idx];
SHashNode *pNode;
SHashNode *pNext;
SHashNode *pPrev = NULL;
if (pe->num == 0) {
assert(pe->next == NULL);
continue;
}
assert(pe->next != NULL);
pNode = pe->next;
while ((pNode = pe->next) != NULL) {
int32_t j = HASH_INDEX(pNode->hashVal, pHashObj->capacity);
if (j != i) {
pe->num -= 1;
pe->next = pNode->next;
assert(pNode != NULL);
if (pe->num == 0) {
assert(pe->next == NULL);
while (pNode != NULL) {
int32_t newIdx = HASH_INDEX(pNode->hashVal, pHashObj->capacity);
pNext = pNode->next;
if (newIdx != idx) {
pe->num -= 1;
if (pPrev == NULL) {
pe->next = pNext;
} else {
assert(pe->next != NULL);
pPrev->next = pNext;
}
SHashEntry *pNewEntry = pHashObj->hashList[j];
SHashEntry *pNewEntry = pHashObj->hashList[newIdx];
pushfrontNodeInEntryList(pNewEntry, pNode);
} else {
break;
}
}
if (pNode != NULL) {
while ((pNext = pNode->next) != NULL) {
int32_t j = HASH_INDEX(pNext->hashVal, pHashObj->capacity);
if (j != i) {
pe->num -= 1;
pNode->next = pNext->next;
pNext->next = NULL;
// added into new slot
SHashEntry *pNewEntry = pHashObj->hashList[j];
if (pNewEntry->num == 0) {
assert(pNewEntry->next == NULL);
} else {
assert(pNewEntry->next != NULL);
pPrev = pNode;
}
pushfrontNodeInEntryList(pNewEntry, pNext);
} else {
pNode = pNext;
}
}
if (pe->num == 0) {
assert(pe->next == NULL);
} else {
assert(pe->next != NULL);
}
}
}
int64_t et = taosGetTimestampUs();
uDebug("hash table resize completed, new capacity:%d, load factor:%f, elapsed time:%fms", (int32_t)pHashObj->capacity,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册