hash.h 4.4 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 27 28 29 30

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

H
hjxilinx 已提交
31 32
typedef void (*_hash_free_fn_t)(void *param);

33 34
typedef struct SHashNode {
  struct SHashNode *next;
35 36 37 38 39
  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
  int8_t            count;       // reference count
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
40
  char              data[];
41 42
} SHashNode;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
43 44 45
#define GET_HASH_NODE_KEY(_n)  ((char*)(_n) + sizeof(SHashNode) + (_n)->dataLen)
#define GET_HASH_NODE_DATA(_n) ((char*)(_n) + sizeof(SHashNode))
#define GET_HASH_PNODE(_n) ((char*)(_n) - sizeof(SHashNode));
H
Haojun Liao 已提交
46

H
Haojun Liao 已提交
47 48
typedef enum SHashLockTypeE {
  HASH_NO_LOCK     = 0,
H
Haojun Liao 已提交
49
  HASH_ENTRY_LOCK  = 1,
H
Haojun Liao 已提交
50
} SHashLockTypeE;
H
hjxilinx 已提交
51

H
Haojun Liao 已提交
52 53 54
typedef struct SHashEntry {
  int32_t    num;      // number of elements in current entry
  SRWLatch   latch;    // entry latch
H
Haojun Liao 已提交
55
  SHashNode *next;
H
Haojun Liao 已提交
56 57 58 59 60 61 62 63
} SHashEntry;

typedef struct SHashObj {
  SHashEntry    **hashList;
  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
64
  _equal_fn_t     equalFp;       // equal function
H
Haojun Liao 已提交
65

H
Haojun Liao 已提交
66 67
  SRWLatch        lock;         // read-write spin lock
  SHashLockTypeE  type;         // lock type
H
Haojun Liao 已提交
68 69
  bool            enableUpdate; // enable update
  SArray         *pMemBlock;    // memory block allocated for SHashEntry
H
hjxilinx 已提交
70
} SHashObj;
71

H
hjxilinx 已提交
72 73 74 75 76 77 78 79
/**
 * 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 已提交
80
SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTypeE type);
81

82 83 84 85 86 87 88 89 90

/**
 * set equal func of the hash table  
 * @param pHashObj    
 * @param equalFp       
 * @return
 */
void taosHashSetEqualFp(SHashObj *pHashObj, _equal_fn_t fp);

H
hjxilinx 已提交
91 92 93 94 95
/**
 * return the size of hash table
 * @param pHashObj
 * @return
 */
S
TD-2367  
Shengliang Guan 已提交
96
int32_t taosHashGetSize(const SHashObj *pHashObj);
H
hjxilinx 已提交
97 98 99 100 101 102 103 104 105 106

/**
 * 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 已提交
107
int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *data, size_t size);
108

H
hjxilinx 已提交
109 110 111 112 113 114 115 116
/**
 * return the payload data with the specified key
 *
 * @param pHashObj
 * @param key
 * @param keyLen
 * @return
 */
H
hjLiao 已提交
117
void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen);
118

H
Haojun Liao 已提交
119 120 121 122 123 124 125 126 127 128
/**
 * apply the udf before return the result
 * @param pHashObj
 * @param key
 * @param keyLen
 * @param fp
 * @param d
 * @param dsize
 * @return
 */
129
void* taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void* d, size_t dsize);
H
Haojun Liao 已提交
130

H
hjxilinx 已提交
131 132 133 134 135 136
/**
 * remove item with the specified key
 * @param pHashObj
 * @param key
 * @param keyLen
 */
H
Haojun Liao 已提交
137
int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen);
138

H
Haojun Liao 已提交
139
int32_t taosHashRemoveWithData(SHashObj *pHashObj, const void *key, size_t keyLen, void* data, size_t dsize);
H
Haojun Liao 已提交
140

H
Haojun Liao 已提交
141
int32_t taosHashCondTraverse(SHashObj *pHashObj, bool (*fp)(void *, void *), void *param);
H
Haojun Liao 已提交
142

143
void taosHashClear(SHashObj *pHashObj);
144

H
hjxilinx 已提交
145 146 147 148 149
/**
 * clean up hash table
 * @param handle
 */
void taosHashCleanup(SHashObj *pHashObj);
150

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

H
Haojun Liao 已提交
158 159
size_t taosHashGetMemSize(const SHashObj *pHashObj);

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

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

H
hjxilinx 已提交
164 165 166
#ifdef __cplusplus
}
#endif
167 168

#endif  // TDENGINE_HASH_H