hash.h 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * 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/>.
 */

#ifndef TDENGINE_HASH_H
#define TDENGINE_HASH_H

H
hjxilinx 已提交
19 20 21 22
#ifdef __cplusplus
extern "C" {
#endif

H
hjxilinx 已提交
23
#include "hashfunc.h"
24 25 26 27 28

#define HASH_MAX_CAPACITY (1024 * 1024 * 16)
#define HASH_DEFAULT_LOAD_FACTOR (0.75)
#define HASH_INDEX(v, c) ((v) & ((c)-1))

H
hjxilinx 已提交
29 30
typedef void (*_hash_free_fn_t)(void *param);

31
typedef struct SHashNode {
H
Haojun Liao 已提交
32 33
  char             *key;
  struct SHashNode *prev;
34 35 36
  struct SHashNode *next;
  uint32_t          hashVal;  // the hash value of key, if hashVal == HASH_VALUE_IN_TRASH, this node is moved to trash
  uint32_t          keyLen;   // length of the key
H
Haojun Liao 已提交
37
  char             *data;
38 39
} SHashNode;

H
hjxilinx 已提交
40
typedef struct SHashObj {
41
  SHashNode     **hashList;
H
hjxilinx 已提交
42 43 44 45 46
  size_t          capacity;  // number of slots
  size_t          size;      // number of elements in hash table
  _hash_fn_t      hashFp;    // hash function
  _hash_free_fn_t freeFp;    // hash node free callback function

H
Haojun Liao 已提交
47
  bool            enableUpdate; // enable update
H
hjxilinx 已提交
48 49
#if defined(LINUX)
  pthread_rwlock_t *lock;
50
#else
H
hjxilinx 已提交
51
  pthread_mutex_t *lock;
52
#endif
H
hjxilinx 已提交
53
} SHashObj;
54

H
hjxilinx 已提交
55 56 57 58 59 60 61 62
typedef struct SHashMutableIterator {
  SHashObj * pHashObj;
  int32_t    entryIndex;
  SHashNode *pCur;
  SHashNode *pNext;  // current node can be deleted for mutable iterator, so keep the next one before return current
  int32_t    num;    // already check number of elements in hash table
} SHashMutableIterator;

H
hjxilinx 已提交
63 64 65 66 67 68 69 70
/**
 * init the hash table
 *
 * @param capacity    initial capacity of the hash table
 * @param fn          hash function to generate the hash value
 * @param threadsafe  thread safe or not
 * @return
 */
H
Haojun Liao 已提交
71
SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, bool threadsafe);
72

H
hjxilinx 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/**
 * return the size of hash table
 * @param pHashObj
 * @return
 */
size_t taosHashGetSize(const SHashObj *pHashObj);

/**
 * put element into hash table, if the element with the same key exists, update it
 * @param pHashObj
 * @param key
 * @param keyLen
 * @param data
 * @param size
 * @return
 */
H
hjLiao 已提交
89
int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *data, size_t size);
90

H
hjxilinx 已提交
91 92 93 94 95 96 97 98
/**
 * return the payload data with the specified key
 *
 * @param pHashObj
 * @param key
 * @param keyLen
 * @return
 */
H
hjLiao 已提交
99
void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen);
100

H
hjxilinx 已提交
101 102 103 104 105 106
/**
 * remove item with the specified key
 * @param pHashObj
 * @param key
 * @param keyLen
 */
H
Haojun Liao 已提交
107
int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen);
108

H
Haojun Liao 已提交
109 110

void* taosHashRemoveNode(SHashObj *pHashObj, const void *key, size_t keyLen);
H
Haojun Liao 已提交
111

H
hjxilinx 已提交
112 113 114 115 116
/**
 * clean up hash table
 * @param handle
 */
void taosHashCleanup(SHashObj *pHashObj);
117

H
hjxilinx 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/**
 * Set the free callback function
 * This function if set will be invoked right before freeing each hash node
 * @param pHashObj
 */
void taosHashSetFreecb(SHashObj *pHashObj, _hash_free_fn_t freeFp);

/**
 *
 * @param pHashObj
 * @return
 */
SHashMutableIterator* taosHashCreateIter(SHashObj *pHashObj);

/**
 *
 * @param iter
 * @return
 */
bool taosHashIterNext(SHashMutableIterator *iter);

/**
 *
 * @param iter
 * @return
 */
void *taosHashIterGet(SHashMutableIterator *iter);

/**
 *
 * @param iter
 * @return
 */
void* taosHashDestroyIter(SHashMutableIterator* iter);

H
hjxilinx 已提交
153 154 155 156 157 158
/**
 *
 * @param pHashObj
 * @return
 */
int32_t taosHashGetMaxOverflowLinkLength(const SHashObj *pHashObj);
159

H
hjxilinx 已提交
160 161 162
#ifdef __cplusplus
}
#endif
163 164

#endif  // TDENGINE_HASH_H