hash.h 5.8 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
Haojun Liao 已提交
23
#include "tarray.h"
H
hjxilinx 已提交
24
#include "hashfunc.h"
H
Haojun Liao 已提交
25
#include "tlockfree.h"
26

Y
Yifan Hao 已提交
27 28
// TODO: SHashNode is an internal implementation and should not
// be in the public header file.
29 30
typedef struct SHashNode {
  struct SHashNode *next;
31 32 33 34
  uint32_t          hashVal;     // the hash value of key
  uint32_t          dataLen;     // length of data
  uint32_t          keyLen;      // length of the key
  int8_t            removed;     // flag to indicate removed
Y
Yifan Hao 已提交
35
  int32_t           refCount;    // reference count
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
36
  char              data[];
37
} SHashNode;
H
Haojun Liao 已提交
38 39
  
#define GET_HASH_PNODE(_n) ((SHashNode *)((char*)(_n) - sizeof(SHashNode)))
40

H
Haojun Liao 已提交
41 42
typedef enum SHashLockTypeE {
  HASH_NO_LOCK     = 0,
H
Haojun Liao 已提交
43
  HASH_ENTRY_LOCK  = 1,
H
Haojun Liao 已提交
44
} SHashLockTypeE;
H
hjxilinx 已提交
45

Y
Yifan Hao 已提交
46
typedef struct SHashObj SHashObj;
47

H
hjxilinx 已提交
48
/**
Y
Yifan Hao 已提交
49
 * initialize a hash table
H
hjxilinx 已提交
50
 *
Y
Yifan Hao 已提交
51 52 53 54 55
 * @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
H
hjxilinx 已提交
56
 */
H
Haojun Liao 已提交
57
SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTypeE type);
58

59
/**
Y
Yifan Hao 已提交
60
 * set equal func of the hash table
Y
Yifan Hao 已提交
61
 *
Y
Yifan Hao 已提交
62 63
 * @param pHashObj
 * @param equalFp
64 65 66 67
 * @return
 */
void taosHashSetEqualFp(SHashObj *pHashObj, _equal_fn_t fp);

wmmhello's avatar
wmmhello 已提交
68 69
void taosHashSetFreeFp(SHashObj *pHashObj, _hash_free_fn_t fp);

H
hjxilinx 已提交
70 71
/**
 * return the size of hash table
Y
Yifan Hao 已提交
72
 *
H
hjxilinx 已提交
73 74 75
 * @param pHashObj
 * @return
 */
S
TD-2367  
Shengliang Guan 已提交
76
int32_t taosHashGetSize(const SHashObj *pHashObj);
H
hjxilinx 已提交
77 78 79

/**
 * put element into hash table, if the element with the same key exists, update it
Y
Yifan Hao 已提交
80 81 82 83 84 85 86
 *
 * @param pHashObj   hash table object
 * @param key        key
 * @param keyLen     length of key
 * @param data       data
 * @param size       size of data
 * @return           0 if success, -1 otherwise
H
hjxilinx 已提交
87
 */
H
hjLiao 已提交
88
int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *data, size_t size);
89

H
hjxilinx 已提交
90 91 92
/**
 * return the payload data with the specified key
 *
Y
Yifan Hao 已提交
93 94 95 96
 * @param pHashObj   hash table object
 * @param key        key
 * @param keyLen     length of key
 * @return           pointer to data
H
hjxilinx 已提交
97
 */
H
hjLiao 已提交
98
void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen);
99

H
Haojun Liao 已提交
100
/**
Y
Yifan Hao 已提交
101 102 103 104 105 106 107 108 109
 * Get the data associated with "key". Note that caller needs to make sure
 * "d" has enough capacity to accomodate the data.
 *
 * @param pHashObj   hash table object
 * @param key        key
 * @param keyLen     length of key
 * @param fp         function to be called on hash node when the data is found
 * @param d          buffer
 * @return           pointer to data
H
Haojun Liao 已提交
110
 */
111
void* taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void* d);
H
Haojun Liao 已提交
112

113
/**
Y
Yifan Hao 已提交
114 115 116 117 118 119 120 121 122 123
 * Get the data associated with "key". Note that caller needs to take ownership
 * of the data "d" and make sure it is deallocated.
 *
 * @param pHashObj   hash table object
 * @param key        key
 * @param keyLen     length of key
 * @param fp         function to be called on hash node when the data is found
 * @param d          buffer
 * @param sz         size of the data buffer
 * @return           pointer to data
124 125
 */
void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void** d, size_t *sz);
Y
Yifan Hao 已提交
126

H
hjxilinx 已提交
127 128
/**
 * remove item with the specified key
Y
Yifan Hao 已提交
129 130 131 132 133
 *
 * @param pHashObj   hash table object
 * @param key        key
 * @param keyLen     length of key
 * @return           0 if success, -1 otherwise
H
hjxilinx 已提交
134
 */
H
Haojun Liao 已提交
135
int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen);
136

Y
Yifan Hao 已提交
137 138 139 140 141 142 143 144 145 146
/**
 * remove item with the specified key
 *
 * @param pHashObj   hash table object
 * @param key        key
 * @param keyLen     length of key
 * @param data       buffer for data
 * @param dsize      size of data buffer
 * @return           0 if success, -1 otherwise
 */
H
Haojun Liao 已提交
147
int32_t taosHashRemoveWithData(SHashObj *pHashObj, const void *key, size_t keyLen, void* data, size_t dsize);
H
Haojun Liao 已提交
148

Y
Yifan Hao 已提交
149 150 151 152 153 154 155 156 157 158
/**
 * traverse through all objects in the hash table and apply "fp" on each node.
 * If "fp" returns false when applied on top of a node, the node will also be
 * removed from table.
 *
 * @param pHashObj   hash table object
 * @param fp         function pointer applied on each node
 * @param param      parameter fed into "fp"
 */
void taosHashCondTraverse(SHashObj *pHashObj, bool (*fp)(void *, void *), void *param);
H
Haojun Liao 已提交
159

Y
Yifan Hao 已提交
160 161 162 163 164
/**
 * clear the contents of the hash table
 *
 * @param pHashObj   hash table object
 */
165
void taosHashClear(SHashObj *pHashObj);
166

H
hjxilinx 已提交
167 168
/**
 * clean up hash table
Y
Yifan Hao 已提交
169 170
 *
 * @param pHashObj   hash table object
H
hjxilinx 已提交
171 172
 */
void taosHashCleanup(SHashObj *pHashObj);
173

H
hjxilinx 已提交
174
/**
Y
Yifan Hao 已提交
175
 * return the number of collisions in the hash table
H
hjxilinx 已提交
176
 *
Y
Yifan Hao 已提交
177 178
 * @param pHashObj   hash table object
 * @return           maximum number of collisions
H
hjxilinx 已提交
179
 */
Y
Yifan Hao 已提交
180
int32_t taosHashGetMaxOverflowLinkLength(SHashObj *pHashObj);
181

Y
Yifan Hao 已提交
182 183 184 185 186 187
/**
 * return the consumed memory of the hash table
 *
 * @param pHashObj   hash table object
 * @return           consumed memory of the hash table
 */
H
Haojun Liao 已提交
188 189
size_t taosHashGetMemSize(const SHashObj *pHashObj);

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
190
void *taosHashIterate(SHashObj *pHashObj, void *p);
191

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
192 193
void  taosHashCancelIterate(SHashObj *pHashObj, void *p);

W
wpan 已提交
194 195
void *taosHashGetDataKey(SHashObj *pHashObj, void *data);

W
wpan 已提交
196 197
uint32_t taosHashGetDataKeyLen(SHashObj *pHashObj, void *data);

H
hjxilinx 已提交
198 199 200
#ifdef __cplusplus
}
#endif
201 202

#endif  // TDENGINE_HASH_H