hash
This commit is contained in:
parent
e7852552a1
commit
cb71affec5
|
@ -12,19 +12,20 @@
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
* 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/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#ifndef TDENGINE_HEAP_H
|
|
||||||
#define TDENGINE_HEAP_H
|
#ifndef _TD_UTIL_HEAP_H_
|
||||||
|
#define _TD_UTIL_HEAP_H_
|
||||||
|
|
||||||
|
#include "os.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "os.h"
|
|
||||||
|
|
||||||
struct HeapNode;
|
struct HeapNode;
|
||||||
|
|
||||||
/* Return non-zero if a < b. */
|
/* Return non-zero if a < b. */
|
||||||
typedef int (*HeapCompareFn)(const struct HeapNode* a, const struct HeapNode* b);
|
typedef int32_t (*HeapCompareFn)(const struct HeapNode* a, const struct HeapNode* b);
|
||||||
|
|
||||||
typedef struct HeapNode {
|
typedef struct HeapNode {
|
||||||
struct HeapNode* left;
|
struct HeapNode* left;
|
||||||
|
@ -43,10 +44,9 @@ typedef struct {
|
||||||
HeapCompareFn compFn;
|
HeapCompareFn compFn;
|
||||||
} Heap;
|
} Heap;
|
||||||
|
|
||||||
|
|
||||||
Heap* heapCreate(HeapCompareFn fn);
|
Heap* heapCreate(HeapCompareFn fn);
|
||||||
|
|
||||||
void heapDestroy(Heap *heap);
|
void heapDestroy(Heap* heap);
|
||||||
|
|
||||||
HeapNode* heapMin(const Heap* heap);
|
HeapNode* heapMin(const Heap* heap);
|
||||||
|
|
||||||
|
@ -56,10 +56,10 @@ void heapRemove(Heap* heap, struct HeapNode* node);
|
||||||
|
|
||||||
void heapDequeue(Heap* heap);
|
void heapDequeue(Heap* heap);
|
||||||
|
|
||||||
size_t heapSize(Heap *heap);
|
size_t heapSize(Heap* heap);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // TDENGINE_HASH_H
|
#endif /*_TD_UTIL_HEAP_H_*/
|
||||||
|
|
|
@ -13,31 +13,24 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _TD_UTIL_IDPOOL_H
|
#ifndef _TD_UTIL_IDPOOL_H_
|
||||||
#define _TD_UTIL_IDPOOL_H
|
#define _TD_UTIL_IDPOOL_H_
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void *taosInitIdPool(int maxId);
|
void *taosInitIdPool(int32_t maxId);
|
||||||
|
int32_t taosUpdateIdPool(void *handle, int32_t maxId);
|
||||||
int taosUpdateIdPool(void *handle, int maxId);
|
int32_t taosIdPoolMaxSize(void *handle);
|
||||||
|
int32_t taosAllocateId(void *handle);
|
||||||
int taosIdPoolMaxSize(void *handle);
|
void taosFreeId(void *handle, int32_t id);
|
||||||
|
|
||||||
int taosAllocateId(void *handle);
|
|
||||||
|
|
||||||
void taosFreeId(void *handle, int id);
|
|
||||||
|
|
||||||
void taosIdPoolCleanUp(void *handle);
|
void taosIdPoolCleanUp(void *handle);
|
||||||
|
int32_t taosIdPoolNumOfUsed(void *handle);
|
||||||
int taosIdPoolNumOfUsed(void *handle);
|
bool taosIdPoolMarkStatus(void *handle, int32_t id);
|
||||||
|
|
||||||
bool taosIdPoolMarkStatus(void *handle, int id);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /*_TD_UTIL_IDPOOL_H*/
|
#endif /*_TD_UTIL_IDPOOL_H_*/
|
||||||
|
|
|
@ -13,10 +13,9 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "os.h"
|
#define _DEFAULT_SOURCE
|
||||||
#include "thash.h"
|
#include "thash.h"
|
||||||
#include "tcompare.h"
|
#include "tcompare.h"
|
||||||
#include "taos.h"
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
#define ROTL32(x, r) ((x) << (r) | (x) >> (32u - (r)))
|
#define ROTL32(x, r) ((x) << (r) | (x) >> (32u - (r)))
|
||||||
|
@ -34,7 +33,7 @@
|
||||||
|
|
||||||
uint32_t MurmurHash3_32(const char *key, uint32_t len) {
|
uint32_t MurmurHash3_32(const char *key, uint32_t len) {
|
||||||
const uint8_t *data = (const uint8_t *)key;
|
const uint8_t *data = (const uint8_t *)key;
|
||||||
const int nblocks = len >> 2u;
|
const int32_t nblocks = len >> 2u;
|
||||||
|
|
||||||
uint32_t h1 = 0x12345678;
|
uint32_t h1 = 0x12345678;
|
||||||
|
|
||||||
|
@ -43,7 +42,7 @@ uint32_t MurmurHash3_32(const char *key, uint32_t len) {
|
||||||
|
|
||||||
const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
|
const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
|
||||||
|
|
||||||
for (int i = -nblocks; i; i++) {
|
for (int32_t i = -nblocks; i; i++) {
|
||||||
uint32_t k1 = blocks[i];
|
uint32_t k1 = blocks[i];
|
||||||
|
|
||||||
k1 *= c1;
|
k1 *= c1;
|
||||||
|
@ -92,7 +91,7 @@ uint32_t taosFloatHash(const char *key, uint32_t UNUSED_PARAM(len)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (fabs(f) < FLT_MAX/BASE - DLT) {
|
if (fabs(f) < FLT_MAX/BASE - DLT) {
|
||||||
int t = (int)(round(BASE * (f + DLT)));
|
int32_t t = (int32_t)(round(BASE * (f + DLT)));
|
||||||
return (uint32_t)t;
|
return (uint32_t)t;
|
||||||
} else {
|
} else {
|
||||||
return 0x7fc00000;
|
return 0x7fc00000;
|
||||||
|
@ -108,7 +107,7 @@ uint32_t taosDoubleHash(const char *key, uint32_t UNUSED_PARAM(len)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (fabs(f) < DBL_MAX/BASE - DLT) {
|
if (fabs(f) < DBL_MAX/BASE - DLT) {
|
||||||
int t = (int)(round(BASE * (f + DLT)));
|
int32_t t = (int32_t)(round(BASE * (f + DLT)));
|
||||||
return (uint32_t)t;
|
return (uint32_t)t;
|
||||||
} else {
|
} else {
|
||||||
return 0x7fc00000;
|
return 0x7fc00000;
|
||||||
|
|
|
@ -13,15 +13,16 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define _DEFAULT_SOURCE
|
||||||
#include "theap.h"
|
#include "theap.h"
|
||||||
|
|
||||||
size_t heapSize(Heap* heap) {
|
size_t heapSize(Heap* heap) { return heap->nelts; }
|
||||||
return heap->nelts;
|
|
||||||
}
|
|
||||||
|
|
||||||
Heap* heapCreate(HeapCompareFn fn) {
|
Heap* heapCreate(HeapCompareFn fn) {
|
||||||
Heap *heap = calloc(1, sizeof(Heap));
|
Heap* heap = calloc(1, sizeof(Heap));
|
||||||
if (heap == NULL) { return NULL; }
|
if (heap == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
heap->min = NULL;
|
heap->min = NULL;
|
||||||
heap->nelts = 0;
|
heap->nelts = 0;
|
||||||
|
@ -29,13 +30,9 @@ Heap* heapCreate(HeapCompareFn fn) {
|
||||||
return heap;
|
return heap;
|
||||||
}
|
}
|
||||||
|
|
||||||
void heapDestroy(Heap *heap) {
|
void heapDestroy(Heap* heap) { free(heap); }
|
||||||
free(heap);
|
|
||||||
}
|
|
||||||
|
|
||||||
HeapNode* heapMin(const Heap* heap) {
|
HeapNode* heapMin(const Heap* heap) { return heap->min; }
|
||||||
return heap->min;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Swap parent with child. Child moves closer to the root, parent moves away. */
|
/* Swap parent with child. Child moves closer to the root, parent moves away. */
|
||||||
static void heapNodeSwap(Heap* heap, HeapNode* parent, HeapNode* child) {
|
static void heapNodeSwap(Heap* heap, HeapNode* parent, HeapNode* child) {
|
||||||
|
@ -54,13 +51,10 @@ static void heapNodeSwap(Heap* heap, HeapNode* parent, HeapNode* child) {
|
||||||
child->right = parent;
|
child->right = parent;
|
||||||
sibling = child->left;
|
sibling = child->left;
|
||||||
}
|
}
|
||||||
if (sibling != NULL)
|
if (sibling != NULL) sibling->parent = child;
|
||||||
sibling->parent = child;
|
|
||||||
|
|
||||||
if (parent->left != NULL)
|
if (parent->left != NULL) parent->left->parent = parent;
|
||||||
parent->left->parent = parent;
|
if (parent->right != NULL) parent->right->parent = parent;
|
||||||
if (parent->right != NULL)
|
|
||||||
parent->right->parent = parent;
|
|
||||||
|
|
||||||
if (child->parent == NULL)
|
if (child->parent == NULL)
|
||||||
heap->min = child;
|
heap->min = child;
|
||||||
|
@ -73,9 +67,9 @@ static void heapNodeSwap(Heap* heap, HeapNode* parent, HeapNode* child) {
|
||||||
void heapInsert(Heap* heap, HeapNode* newnode) {
|
void heapInsert(Heap* heap, HeapNode* newnode) {
|
||||||
HeapNode** parent;
|
HeapNode** parent;
|
||||||
HeapNode** child;
|
HeapNode** child;
|
||||||
unsigned int path;
|
uint32_t path;
|
||||||
unsigned int n;
|
uint32_t n;
|
||||||
unsigned int k;
|
uint32_t k;
|
||||||
|
|
||||||
newnode->left = NULL;
|
newnode->left = NULL;
|
||||||
newnode->right = NULL;
|
newnode->right = NULL;
|
||||||
|
@ -85,8 +79,7 @@ void heapInsert(Heap* heap, HeapNode* newnode) {
|
||||||
* heap so we always insert at the left-most free node of the bottom row.
|
* heap so we always insert at the left-most free node of the bottom row.
|
||||||
*/
|
*/
|
||||||
path = 0;
|
path = 0;
|
||||||
for (k = 0, n = 1 + heap->nelts; n >= 2; k += 1, n /= 2)
|
for (k = 0, n = 1 + heap->nelts; n >= 2; k += 1, n /= 2) path = (path << 1) | (n & 1);
|
||||||
path = (path << 1) | (n & 1);
|
|
||||||
|
|
||||||
/* Now traverse the heap using the path we calculated in the previous step. */
|
/* Now traverse the heap using the path we calculated in the previous step. */
|
||||||
parent = child = &heap->min;
|
parent = child = &heap->min;
|
||||||
|
@ -116,19 +109,17 @@ void heapRemove(Heap* heap, HeapNode* node) {
|
||||||
HeapNode* smallest;
|
HeapNode* smallest;
|
||||||
HeapNode** max;
|
HeapNode** max;
|
||||||
HeapNode* child;
|
HeapNode* child;
|
||||||
unsigned int path;
|
uint32_t path;
|
||||||
unsigned int k;
|
uint32_t k;
|
||||||
unsigned int n;
|
uint32_t n;
|
||||||
|
|
||||||
if (heap->nelts == 0)
|
if (heap->nelts == 0) return;
|
||||||
return;
|
|
||||||
|
|
||||||
/* Calculate the path from the min (the root) to the max, the left-most node
|
/* Calculate the path from the min (the root) to the max, the left-most node
|
||||||
* of the bottom row.
|
* of the bottom row.
|
||||||
*/
|
*/
|
||||||
path = 0;
|
path = 0;
|
||||||
for (k = 0, n = heap->nelts; n >= 2; k += 1, n /= 2)
|
for (k = 0, n = heap->nelts; n >= 2; k += 1, n /= 2) path = (path << 1) | (n & 1);
|
||||||
path = (path << 1) | (n & 1);
|
|
||||||
|
|
||||||
/* Now traverse the heap using the path we calculated in the previous step. */
|
/* Now traverse the heap using the path we calculated in the previous step. */
|
||||||
max = &heap->min;
|
max = &heap->min;
|
||||||
|
@ -182,12 +173,9 @@ void heapRemove(Heap* heap, HeapNode* node) {
|
||||||
*/
|
*/
|
||||||
for (;;) {
|
for (;;) {
|
||||||
smallest = child;
|
smallest = child;
|
||||||
if (child->left != NULL && (heap->compFn)(child->left, smallest))
|
if (child->left != NULL && (heap->compFn)(child->left, smallest)) smallest = child->left;
|
||||||
smallest = child->left;
|
if (child->right != NULL && (heap->compFn)(child->right, smallest)) smallest = child->right;
|
||||||
if (child->right != NULL && (heap->compFn)(child->right, smallest))
|
if (smallest == child) break;
|
||||||
smallest = child->right;
|
|
||||||
if (smallest == child)
|
|
||||||
break;
|
|
||||||
heapNodeSwap(heap, child, smallest);
|
heapNodeSwap(heap, child, smallest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,12 +183,7 @@ void heapRemove(Heap* heap, HeapNode* node) {
|
||||||
* this is required, because `max` node is not guaranteed to be the
|
* this is required, because `max` node is not guaranteed to be the
|
||||||
* actual maximum in tree
|
* actual maximum in tree
|
||||||
*/
|
*/
|
||||||
while (child->parent != NULL && (heap->compFn)(child, child->parent))
|
while (child->parent != NULL && (heap->compFn)(child, child->parent)) heapNodeSwap(heap, child->parent, child);
|
||||||
heapNodeSwap(heap, child->parent, child);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void heapDequeue(Heap* heap) {
|
void heapDequeue(Heap* heap) { heapRemove(heap, heap->min); }
|
||||||
heapRemove(heap, heap->min);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,17 +13,18 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define _DEFAULT_SOURCE
|
||||||
#include "tlog.h"
|
#include "tlog.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int maxId;
|
int32_t maxId;
|
||||||
int numOfFree;
|
int32_t numOfFree;
|
||||||
int freeSlot;
|
int32_t freeSlot;
|
||||||
bool * freeList;
|
bool *freeList;
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
} id_pool_t;
|
} id_pool_t;
|
||||||
|
|
||||||
void *taosInitIdPool(int maxId) {
|
void *taosInitIdPool(int32_t maxId) {
|
||||||
id_pool_t *pIdPool = calloc(1, sizeof(id_pool_t));
|
id_pool_t *pIdPool = calloc(1, sizeof(id_pool_t));
|
||||||
if (pIdPool == NULL) return NULL;
|
if (pIdPool == NULL) return NULL;
|
||||||
|
|
||||||
|
@ -44,17 +45,17 @@ void *taosInitIdPool(int maxId) {
|
||||||
return pIdPool;
|
return pIdPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
int taosAllocateId(void *handle) {
|
int32_t taosAllocateId(void *handle) {
|
||||||
id_pool_t *pIdPool = handle;
|
id_pool_t *pIdPool = handle;
|
||||||
if (handle == NULL) {
|
if (handle == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int slot = -1;
|
int32_t slot = -1;
|
||||||
pthread_mutex_lock(&pIdPool->mutex);
|
pthread_mutex_lock(&pIdPool->mutex);
|
||||||
|
|
||||||
if (pIdPool->numOfFree > 0) {
|
if (pIdPool->numOfFree > 0) {
|
||||||
for (int i = 0; i < pIdPool->maxId; ++i) {
|
for (int32_t i = 0; i < pIdPool->maxId; ++i) {
|
||||||
slot = (i + pIdPool->freeSlot) % pIdPool->maxId;
|
slot = (i + pIdPool->freeSlot) % pIdPool->maxId;
|
||||||
if (!pIdPool->freeList[slot]) {
|
if (!pIdPool->freeList[slot]) {
|
||||||
pIdPool->freeList[slot] = true;
|
pIdPool->freeList[slot] = true;
|
||||||
|
@ -69,13 +70,13 @@ int taosAllocateId(void *handle) {
|
||||||
return slot + 1;
|
return slot + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void taosFreeId(void *handle, int id) {
|
void taosFreeId(void *handle, int32_t id) {
|
||||||
id_pool_t *pIdPool = handle;
|
id_pool_t *pIdPool = handle;
|
||||||
if (handle == NULL) return;
|
if (handle == NULL) return;
|
||||||
|
|
||||||
pthread_mutex_lock(&pIdPool->mutex);
|
pthread_mutex_lock(&pIdPool->mutex);
|
||||||
|
|
||||||
int slot = (id - 1) % pIdPool->maxId;
|
int32_t slot = (id - 1) % pIdPool->maxId;
|
||||||
if (pIdPool->freeList[slot]) {
|
if (pIdPool->freeList[slot]) {
|
||||||
pIdPool->freeList[slot] = false;
|
pIdPool->freeList[slot] = false;
|
||||||
pIdPool->numOfFree++;
|
pIdPool->numOfFree++;
|
||||||
|
@ -100,22 +101,22 @@ void taosIdPoolCleanUp(void *handle) {
|
||||||
free(pIdPool);
|
free(pIdPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
int taosIdPoolNumOfUsed(void *handle) {
|
int32_t taosIdPoolNumOfUsed(void *handle) {
|
||||||
id_pool_t *pIdPool = handle;
|
id_pool_t *pIdPool = handle;
|
||||||
|
|
||||||
pthread_mutex_lock(&pIdPool->mutex);
|
pthread_mutex_lock(&pIdPool->mutex);
|
||||||
int ret = pIdPool->maxId - pIdPool->numOfFree;
|
int32_t ret = pIdPool->maxId - pIdPool->numOfFree;
|
||||||
pthread_mutex_unlock(&pIdPool->mutex);
|
pthread_mutex_unlock(&pIdPool->mutex);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool taosIdPoolMarkStatus(void *handle, int id) {
|
bool taosIdPoolMarkStatus(void *handle, int32_t id) {
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
id_pool_t *pIdPool = handle;
|
id_pool_t *pIdPool = handle;
|
||||||
pthread_mutex_lock(&pIdPool->mutex);
|
pthread_mutex_lock(&pIdPool->mutex);
|
||||||
|
|
||||||
int slot = (id - 1) % pIdPool->maxId;
|
int32_t slot = (id - 1) % pIdPool->maxId;
|
||||||
if (!pIdPool->freeList[slot]) {
|
if (!pIdPool->freeList[slot]) {
|
||||||
pIdPool->freeList[slot] = true;
|
pIdPool->freeList[slot] = true;
|
||||||
pIdPool->numOfFree--;
|
pIdPool->numOfFree--;
|
||||||
|
@ -128,8 +129,8 @@ bool taosIdPoolMarkStatus(void *handle, int id) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int taosUpdateIdPool(id_pool_t *handle, int maxId) {
|
int32_t taosUpdateIdPool(id_pool_t *handle, int32_t maxId) {
|
||||||
id_pool_t *pIdPool = (id_pool_t*)handle;
|
id_pool_t *pIdPool = (id_pool_t *)handle;
|
||||||
if (maxId <= pIdPool->maxId) {
|
if (maxId <= pIdPool->maxId) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -154,11 +155,11 @@ int taosUpdateIdPool(id_pool_t *handle, int maxId) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int taosIdPoolMaxSize(void *handle) {
|
int32_t taosIdPoolMaxSize(void *handle) {
|
||||||
id_pool_t *pIdPool = (id_pool_t *)handle;
|
id_pool_t *pIdPool = (id_pool_t *)handle;
|
||||||
|
|
||||||
pthread_mutex_lock(&pIdPool->mutex);
|
pthread_mutex_lock(&pIdPool->mutex);
|
||||||
int ret = pIdPool->maxId;
|
int32_t ret = pIdPool->maxId;
|
||||||
pthread_mutex_unlock(&pIdPool->mutex);
|
pthread_mutex_unlock(&pIdPool->mutex);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
Loading…
Reference in New Issue