thash.c 22.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
hash  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
H
Haojun Liao 已提交
17
#include "thash.h"
H
Haojun Liao 已提交
18 19
#include "taoserror.h"
#include "os.h"
S
hash  
Shengliang Guan 已提交
20
#include "tlog.h"
21

22
// the add ref count operation may trigger the warning if the reference count is greater than the MAX_WARNING_REF_COUNT
H
Haojun Liao 已提交
23 24 25 26 27 28 29 30 31 32 33
#define MAX_WARNING_REF_COUNT    10000
#define HASH_MAX_CAPACITY        (1024 * 1024 * 16)
#define HASH_DEFAULT_LOAD_FACTOR (0.75)
#define HASH_INDEX(v, c)         ((v) & ((c)-1))

#define HASH_NEED_RESIZE(_h) ((_h)->size >= (_h)->capacity * HASH_DEFAULT_LOAD_FACTOR)

#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)     ((SHashNode *)((char*)(_n) - sizeof(SHashNode)))

34 35
#define FREE_HASH_NODE(_fp, _n) \
  do {                          \
36
/*    if (_fp != NULL) {          \
37
      (_fp)(_n);                \
38
    }*/                           \
39
    taosMemoryFreeClear(_n);    \
H
Haojun Liao 已提交
40 41
  } while (0);

H
Haojun Liao 已提交
42 43 44 45 46 47 48 49 50 51
struct SHashNode {
  SHashNode        *next;
  uint32_t          hashVal;  // the hash value of key
  uint32_t          dataLen;  // length of data
  uint32_t          keyLen;   // length of the key
  uint16_t          refCount; // reference count
  int8_t            removed;  // flag to indicate removed
  char              data[];
};

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

H
Haojun Liao 已提交
58 59 60
struct SHashObj {
  SHashEntry **     hashList;
  size_t            capacity;      // number of slots
wafwerar's avatar
wafwerar 已提交
61
  int64_t           size;          // number of elements in hash table
H
Haojun Liao 已提交
62 63 64 65 66 67 68 69 70
  _hash_fn_t        hashFp;        // hash function
  _equal_fn_t       equalFp;       // equal function
  _hash_free_fn_t   freeFp;        // hash node free callback function
  SRWLatch          lock;          // read-write spin lock
  SHashLockTypeE    type;          // lock type
  bool              enableUpdate;  // enable update
  SArray *          pMemBlock;     // memory block allocated for SHashEntry
  _hash_before_fn_t callbackFp;    // function invoked before return the value to caller
};
H
Haojun Liao 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83

/*
 * Function definition
 */
static FORCE_INLINE void taosHashWLock(SHashObj *pHashObj) {
  if (pHashObj->type == HASH_NO_LOCK) {
    return;
  }
  taosWLockLatch(&pHashObj->lock);
}

static FORCE_INLINE void taosHashWUnlock(SHashObj *pHashObj) {
  if (pHashObj->type == HASH_NO_LOCK) {
H
hjxilinx 已提交
84 85
    return;
  }
H
Haojun Liao 已提交
86 87

  taosWUnLockLatch(&pHashObj->lock);
88 89
}

H
Haojun Liao 已提交
90 91
static FORCE_INLINE void taosHashRLock(SHashObj *pHashObj) {
  if (pHashObj->type == HASH_NO_LOCK) {
H
hjxilinx 已提交
92 93
    return;
  }
H
Haojun Liao 已提交
94 95

  taosRLockLatch(&pHashObj->lock);
96 97
}

H
Haojun Liao 已提交
98 99
static FORCE_INLINE void taosHashRUnlock(SHashObj *pHashObj) {
  if (pHashObj->type == HASH_NO_LOCK) {
H
hjxilinx 已提交
100 101
    return;
  }
H
Haojun Liao 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

  taosRUnLockLatch(&pHashObj->lock);
}

static FORCE_INLINE void taosHashEntryWLock(const SHashObj *pHashObj, SHashEntry* pe) {
  if (pHashObj->type == HASH_NO_LOCK) {
    return;
  }
  taosWLockLatch(&pe->latch);
}

static FORCE_INLINE void taosHashEntryWUnlock(const SHashObj *pHashObj, SHashEntry* pe) {
  if (pHashObj->type == HASH_NO_LOCK) {
    return;
  }

  taosWUnLockLatch(&pe->latch);
}

static FORCE_INLINE void taosHashEntryRLock(const SHashObj *pHashObj, SHashEntry* pe) {
  if (pHashObj->type == HASH_NO_LOCK) {
    return;
  }

  taosRLockLatch(&pe->latch);
127 128
}

H
Haojun Liao 已提交
129 130
static FORCE_INLINE void taosHashEntryRUnlock(const SHashObj *pHashObj, SHashEntry* pe) {
  if (pHashObj->type == HASH_NO_LOCK) {
H
hjxilinx 已提交
131 132
    return;
  }
H
Haojun Liao 已提交
133 134

  taosRUnLockLatch(&pe->latch);
135 136 137
}

static FORCE_INLINE int32_t taosHashCapacity(int32_t length) {
wafwerar's avatar
wafwerar 已提交
138
  int32_t len = (length < HASH_MAX_CAPACITY ? length : HASH_MAX_CAPACITY);
139

S
Shengliang Guan 已提交
140
  int32_t i = 4;
141
  while (i < len) i = (i << 1u);
142 143 144
  return i;
}

H
Haojun Liao 已提交
145 146
static FORCE_INLINE SHashNode *
doSearchInEntryList(SHashObj *pHashObj, SHashEntry *pe, const void *key, size_t keyLen, uint32_t hashVal) {
H
Haojun Liao 已提交
147
  SHashNode *pNode = pe->next;
H
Haojun Liao 已提交
148
  while (pNode) {
H
Haojun Liao 已提交
149 150
    if ((pNode->keyLen == keyLen) &&
        ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) &&
S
hash  
Shengliang Guan 已提交
151
        pNode->removed == 0) {
H
Haojun Liao 已提交
152 153 154 155 156 157 158 159 160 161
      assert(pNode->hashVal == hashVal);
      break;
    }

    pNode = pNode->next;
  }

  return pNode;
}

162
/**
H
Haojun Liao 已提交
163
 * resize the hash list if the threshold is reached
164
 *
H
hjxilinx 已提交
165
 * @param pHashObj
166
 */
H
hjLiao 已提交
167
static void taosHashTableResize(SHashObj *pHashObj);
168

H
hjLiao 已提交
169
/**
H
Haojun Liao 已提交
170 171
 * allocate and initialize a hash node
 *
H
hjLiao 已提交
172 173
 * @param key      key of object for hash, usually a null-terminated string
 * @param keyLen   length of key
H
Haojun Liao 已提交
174
 * @param pData    data to be stored in hash node
H
hjLiao 已提交
175 176 177 178
 * @param dsize    size of data
 * @return         SHashNode
 */
static SHashNode *doCreateHashNode(const void *key, size_t keyLen, const void *pData, size_t dsize, uint32_t hashVal);
179

H
hjLiao 已提交
180
/**
H
Haojun Liao 已提交
181
 * update the hash node
H
hjLiao 已提交
182
 *
H
Haojun Liao 已提交
183 184 185 186 187
 * @param pHashObj   hash table object
 * @param pe         hash table entry to operate on
 * @param prev       previous node
 * @param pNode      the old node with requested key
 * @param pNewNode   the new node with requested key
H
hjLiao 已提交
188
 */
H
Haojun Liao 已提交
189
static FORCE_INLINE void doUpdateHashNode(SHashObj *pHashObj, SHashEntry* pe, SHashNode* prev, SHashNode *pNode, SHashNode *pNewNode) {
H
Haojun Liao 已提交
190
  assert(pNode->keyLen == pNewNode->keyLen);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
191

wafwerar's avatar
wafwerar 已提交
192
  atomic_sub_fetch_16(&pNode->refCount, 1);
H
Haojun Liao 已提交
193 194
  if (prev != NULL) {
    prev->next = pNewNode;
195
    ASSERT(prev->next != prev);
196 197
  } else {
    pe->next = pNewNode;
H
Haojun Liao 已提交
198
  }
H
Haojun Liao 已提交
199

H
Haojun Liao 已提交
200
  if (pNode->refCount <= 0) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
201
    pNewNode->next = pNode->next;
202 203
    ASSERT(pNewNode->next != pNewNode);

204
    FREE_HASH_NODE(pHashObj->freeFp, pNode);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
205
  } else {
S
hash  
Shengliang Guan 已提交
206
    pNewNode->next = pNode;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
207
    pe->num++;
H
Haojun Liao 已提交
208
    atomic_add_fetch_64(&pHashObj->size, 1);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
209
  }
H
Haojun Liao 已提交
210
}
211

H
hjLiao 已提交
212 213 214
/**
 * insert the hash node at the front of the linked list
 *
H
Haojun Liao 已提交
215 216
 * @param pHashObj   hash table object
 * @param pNode      the old node with requested key
H
hjLiao 已提交
217
 */
H
Haojun Liao 已提交
218
static void pushfrontNodeInEntryList(SHashEntry *pEntry, SHashNode *pNode);
219

220 221 222 223 224 225 226 227
/**
 * Check whether the hash table is empty or not.
 *
 * @param pHashObj the hash table object
 * @return if the hash table is empty or not
 */
static FORCE_INLINE bool taosHashTableEmpty(const SHashObj *pHashObj);

228
/**
H
Haojun Liao 已提交
229
 *
230 231
 * @param pHashObj
 * @return
232
 */
233 234 235 236
static FORCE_INLINE bool taosHashTableEmpty(const SHashObj *pHashObj) {
  return taosHashGetSize(pHashObj) == 0;
}

H
Haojun Liao 已提交
237
SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTypeE type) {
H
Haojun Liao 已提交
238 239 240 241 242
  if (fn == NULL) {
    assert(0);
    return NULL;
  }

243 244
  if (capacity == 0) {
    capacity = 4;
245 246
  }

wafwerar's avatar
wafwerar 已提交
247
  SHashObj *pHashObj = (SHashObj *)taosMemoryCalloc(1, sizeof(SHashObj));
H
hjxilinx 已提交
248
  if (pHashObj == NULL) {
H
Haojun Liao 已提交
249
    terrno = TSDB_CODE_OUT_OF_MEMORY;
250 251 252 253
    return NULL;
  }

  // the max slots is not defined by user
S
Shengliang Guan 已提交
254
  pHashObj->capacity = taosHashCapacity((int32_t)capacity);
H
Haojun Liao 已提交
255

256
  pHashObj->equalFp = memcmp;
H
Haojun Liao 已提交
257
  pHashObj->hashFp  = fn;
H
Haojun Liao 已提交
258
  pHashObj->type = type;
H
Haojun Liao 已提交
259
  pHashObj->enableUpdate = update;
260

H
Haojun Liao 已提交
261 262
  ASSERT((pHashObj->capacity & (pHashObj->capacity - 1)) == 0);

wafwerar's avatar
wafwerar 已提交
263
  pHashObj->hashList = (SHashEntry **)taosMemoryCalloc(pHashObj->capacity, sizeof(void *));
H
hjxilinx 已提交
264
  if (pHashObj->hashList == NULL) {
wafwerar's avatar
wafwerar 已提交
265
    taosMemoryFree(pHashObj);
H
Haojun Liao 已提交
266
    terrno = TSDB_CODE_OUT_OF_MEMORY;
267
    return NULL;
H
Haojun Liao 已提交
268
  }
H
Haojun Liao 已提交
269

H
Haojun Liao 已提交
270 271
  pHashObj->pMemBlock = taosArrayInit(8, sizeof(void *));
  if (pHashObj->pMemBlock == NULL) {
wafwerar's avatar
wafwerar 已提交
272 273
    taosMemoryFree(pHashObj->hashList);
    taosMemoryFree(pHashObj);
H
Haojun Liao 已提交
274 275 276
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
H
Haojun Liao 已提交
277

wafwerar's avatar
wafwerar 已提交
278
  void *p = taosMemoryCalloc(pHashObj->capacity, sizeof(SHashEntry));
H
Haojun Liao 已提交
279 280
  if (p == NULL) {
    taosArrayDestroy(pHashObj->pMemBlock);
wafwerar's avatar
wafwerar 已提交
281 282
    taosMemoryFree(pHashObj->hashList);
    taosMemoryFree(pHashObj);
H
Haojun Liao 已提交
283 284
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
285
  }
H
hjxilinx 已提交
286

H
Haojun Liao 已提交
287 288 289 290 291
  for (int32_t i = 0; i < pHashObj->capacity; ++i) {
    pHashObj->hashList[i] = (void *)((char *)p + i * sizeof(SHashEntry));
  }

  taosArrayPush(pHashObj->pMemBlock, &p);
H
hjxilinx 已提交
292
  return pHashObj;
293 294
}

295 296 297
void taosHashSetEqualFp(SHashObj *pHashObj, _equal_fn_t fp) {
  if (pHashObj != NULL && fp != NULL) {
    pHashObj->equalFp = fp;
S
hash  
Shengliang Guan 已提交
298 299
  }
}
300

H
Haojun Liao 已提交
301 302 303 304 305 306
void taosHashSetFreeFp(SHashObj *pHashObj, _hash_free_fn_t fp) {
  if (pHashObj != NULL && fp != NULL) {
    pHashObj->freeFp = fp;
  }
}

307
int32_t taosHashGetSize(const SHashObj *pHashObj) {
H
Haojun Liao 已提交
308
  if (pHashObj == NULL) {
309 310
    return 0;
  }
wafwerar's avatar
wafwerar 已提交
311
  return (int32_t)atomic_load_64((int64_t*)&pHashObj->size);
312 313
}

X
Xiaoyu Wang 已提交
314
int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, const void *data, size_t size) {
H
Haojun Liao 已提交
315
  if (pHashObj == NULL || key == NULL || keyLen == 0) {
316
    terrno = TSDB_CODE_INVALID_PTR;
H
Haojun Liao 已提交
317 318
    return -1;
  }
319

320
  uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen);
321

H
Haojun Liao 已提交
322 323
  // need the resize process, write lock applied
  if (HASH_NEED_RESIZE(pHashObj)) {
H
Haojun Liao 已提交
324
    taosHashWLock(pHashObj);
H
Haojun Liao 已提交
325
    taosHashTableResize(pHashObj);
H
Haojun Liao 已提交
326
    taosHashWUnlock(pHashObj);
H
Haojun Liao 已提交
327 328
  }

329
  // disable resize
H
Haojun Liao 已提交
330
  taosHashRLock(pHashObj);
H
Haojun Liao 已提交
331

H
Haojun Liao 已提交
332
  int32_t     slot = HASH_INDEX(hashVal, pHashObj->capacity);
H
Haojun Liao 已提交
333
  SHashEntry *pe = pHashObj->hashList[slot];
334

H
Haojun Liao 已提交
335
  taosHashEntryWLock(pHashObj, pe);
336

H
Haojun Liao 已提交
337
  SHashNode *pNode = pe->next;
338
#if 0
H
Haojun Liao 已提交
339 340 341 342 343
  if (pe->num > 0) {
    assert(pNode != NULL);
  } else {
    assert(pNode == NULL);
  }
344
#endif
H
Haojun Liao 已提交
345

H
Haojun Liao 已提交
346
  SHashNode* prev = NULL;
H
Haojun Liao 已提交
347
  while (pNode) {
H
Haojun Liao 已提交
348 349
    if ((pNode->keyLen == keyLen) &&
        (*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0 &&
S
hash  
Shengliang Guan 已提交
350
        pNode->removed == 0) {
H
Haojun Liao 已提交
351 352
      assert(pNode->hashVal == hashVal);
      break;
353 354
    }

H
Haojun Liao 已提交
355
    prev = pNode;
H
Haojun Liao 已提交
356 357 358 359 360
    pNode = pNode->next;
  }

  if (pNode == NULL) {
    // no data in hash table with the specified key, add it into hash table
361 362 363 364 365
    SHashNode *pNewNode = doCreateHashNode(key, keyLen, data, size, hashVal);
    if (pNewNode == NULL) {
      return -1;
    }

H
Haojun Liao 已提交
366
    pushfrontNodeInEntryList(pe, pNewNode);
H
Haojun Liao 已提交
367
    assert(pe->next != NULL);
H
Haojun Liao 已提交
368

H
Haojun Liao 已提交
369
    taosHashEntryWUnlock(pHashObj, pe);
H
Haojun Liao 已提交
370 371

    // enable resize
H
Haojun Liao 已提交
372 373
    taosHashRUnlock(pHashObj);
    atomic_add_fetch_64(&pHashObj->size, 1);
S
hash  
Shengliang Guan 已提交
374

H
Haojun Liao 已提交
375
    return 0;
376
  } else {
H
Haojun Liao 已提交
377 378
    // not support the update operation, return error
    if (pHashObj->enableUpdate) {
379 380 381 382 383
      SHashNode *pNewNode = doCreateHashNode(key, keyLen, data, size, hashVal);
      if (pNewNode == NULL) {
        return -1;
      }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
384
      doUpdateHashNode(pHashObj, pe, prev, pNode, pNewNode);
385 386
    } else {
      terrno = TSDB_CODE_DUP_KEY;
H
Haojun Liao 已提交
387 388
    }

H
Haojun Liao 已提交
389
    taosHashEntryWUnlock(pHashObj, pe);
H
Haojun Liao 已提交
390 391

    // enable resize
H
Haojun Liao 已提交
392
    taosHashRUnlock(pHashObj);
H
Haojun Liao 已提交
393
    return pHashObj->enableUpdate ? 0 : -2;
H
Haojun Liao 已提交
394
  }
395 396
}

H
Haojun Liao 已提交
397
static void* taosHashGetImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void** d, int32_t* size, bool addRef);
D
dapan1121 已提交
398

H
hjLiao 已提交
399
void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen) {
H
Haojun Liao 已提交
400 401
  void* p = NULL;
  return taosHashGetImpl(pHashObj, key, keyLen, &p, 0, false);
H
Haojun Liao 已提交
402
}
H
Haojun Liao 已提交
403

H
Haojun Liao 已提交
404 405 406 407 408
int32_t taosHashGetDup(SHashObj *pHashObj, const void *key, size_t keyLen, void *destBuf) {
  terrno = 0;
  /*char* p = */taosHashGetImpl(pHashObj, key, keyLen, &destBuf, 0, false);
  return terrno;
}
409

H
Haojun Liao 已提交
410 411
int32_t taosHashGetDup_m(SHashObj *pHashObj, const void *key, size_t keyLen, void **destBuf, int32_t* size) {
  terrno = 0;
412

H
Haojun Liao 已提交
413 414
  /*char* p = */taosHashGetImpl(pHashObj, key, keyLen, destBuf, size, false);
  return terrno;
415
}
H
Haojun Liao 已提交
416

H
Haojun Liao 已提交
417 418
void* taosHashGetImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void** d, int32_t* size, bool addRef) {
  if (pHashObj == NULL || taosHashTableEmpty(pHashObj) || keyLen == 0 || key == NULL) {
H
Haojun Liao 已提交
419 420 421
    return NULL;
  }

S
Shengliang Guan 已提交
422
  uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen);
423

H
Haojun Liao 已提交
424
  // only add the read lock to disable the resize process
H
Haojun Liao 已提交
425
  taosHashRLock(pHashObj);
H
Haojun Liao 已提交
426

H
Haojun Liao 已提交
427 428
  int32_t     slot = HASH_INDEX(hashVal, pHashObj->capacity);
  SHashEntry *pe = pHashObj->hashList[slot];
429

H
Haojun Liao 已提交
430 431
  // no data, return directly
  if (atomic_load_32(&pe->num) == 0) {
H
Haojun Liao 已提交
432
    taosHashRUnlock(pHashObj);
433 434
    return NULL;
  }
H
Haojun Liao 已提交
435 436

  char *data = NULL;
H
Haojun Liao 已提交
437
  taosHashEntryRLock(pHashObj, pe);
H
Haojun Liao 已提交
438

H
Haojun Liao 已提交
439
#if 0
H
Haojun Liao 已提交
440 441 442 443 444
  if (pe->num > 0) {
    assert(pe->next != NULL);
  } else {
    assert(pe->next == NULL);
  }
H
Haojun Liao 已提交
445
#endif
H
Haojun Liao 已提交
446

447
  SHashNode *pNode = doSearchInEntryList(pHashObj, pe, key, keyLen, hashVal);
H
Haojun Liao 已提交
448
  if (pNode != NULL) {
H
Haojun Liao 已提交
449 450
    if (pHashObj->callbackFp != NULL) {
      pHashObj->callbackFp(GET_HASH_NODE_DATA(pNode));
H
Haojun Liao 已提交
451
    }
H
Haojun Liao 已提交
452

H
Haojun Liao 已提交
453 454 455
    if (size != NULL) {
      if (*d == NULL) {
        *size =  pNode->dataLen;
wafwerar's avatar
wafwerar 已提交
456
        *d = taosMemoryCalloc(1, *size);
H
Haojun Liao 已提交
457 458 459 460 461 462
        if (*d == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          return NULL;
        }
      } else if (*size < pNode->dataLen) {
        *size =  pNode->dataLen;
wafwerar's avatar
wafwerar 已提交
463
        char* tmp = taosMemoryRealloc(*d, *size);
H
Haojun Liao 已提交
464 465 466 467 468 469 470 471 472 473 474
        if (tmp == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          return NULL;
        }

        *d = tmp;
      }
    }

    if (addRef) {
      atomic_add_fetch_16(&pNode->refCount, 1);
H
Haojun Liao 已提交
475
    }
476

H
Haojun Liao 已提交
477 478
    if (*d != NULL) {
      memcpy(*d, GET_HASH_NODE_DATA(pNode), pNode->dataLen);
D
dapan1121 已提交
479 480
    }

481
    data = GET_HASH_NODE_DATA(pNode);
H
Haojun Liao 已提交
482 483
  }

H
Haojun Liao 已提交
484 485
  taosHashEntryRUnlock(pHashObj, pe);
  taosHashRUnlock(pHashObj);
H
Haojun Liao 已提交
486 487

  return data;
488 489
}

H
Haojun Liao 已提交
490
int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen) {
H
Haojun Liao 已提交
491
  if (pHashObj == NULL || taosHashTableEmpty(pHashObj) || key == NULL || keyLen == 0) {
H
Haojun Liao 已提交
492 493 494
    return -1;
  }

S
Shengliang Guan 已提交
495
  uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen);
H
Haojun Liao 已提交
496

H
Haojun Liao 已提交
497
  // disable the resize process
H
Haojun Liao 已提交
498
  taosHashRLock(pHashObj);
H
Haojun Liao 已提交
499

H
Haojun Liao 已提交
500
  int32_t     slot = HASH_INDEX(hashVal, pHashObj->capacity);
H
Haojun Liao 已提交
501 502
  SHashEntry *pe = pHashObj->hashList[slot];

H
Haojun Liao 已提交
503
  taosHashEntryWLock(pHashObj, pe);
504 505

  // double check after locked
H
Haojun Liao 已提交
506
  if (pe->num == 0) {
H
Haojun Liao 已提交
507 508
    assert(pe->next == NULL);

H
Haojun Liao 已提交
509 510
    taosHashEntryWUnlock(pHashObj, pe);
    taosHashRUnlock(pHashObj);
H
Haojun Liao 已提交
511
    return -1;
H
Haojun Liao 已提交
512 513
  }

H
Haojun Liao 已提交
514
  int code = -1;
H
Haojun Liao 已提交
515
  SHashNode *pNode = pe->next;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
516
  SHashNode *prevNode = NULL;
H
Haojun Liao 已提交
517

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
518
  while (pNode) {
H
Haojun Liao 已提交
519 520 521 522
    if ((pNode->keyLen == keyLen) &&
        ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) &&
        pNode->removed == 0) {
      code = 0;  // it is found
H
Haojun Liao 已提交
523

wafwerar's avatar
wafwerar 已提交
524
      atomic_sub_fetch_16(&pNode->refCount, 1);
H
Haojun Liao 已提交
525 526 527 528 529 530
      pNode->removed = 1;
      if (pNode->refCount <= 0) {
        if (prevNode == NULL) {
          pe->next = pNode->next;
        } else {
          prevNode->next = pNode->next;
531
          ASSERT(prevNode->next != prevNode);
H
Haojun Liao 已提交
532
        }
H
Haojun Liao 已提交
533

H
Haojun Liao 已提交
534 535
        pe->num--;
        atomic_sub_fetch_64(&pHashObj->size, 1);
536
        FREE_HASH_NODE(pHashObj->freeFp, pNode);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
537
      }
H
Haojun Liao 已提交
538 539 540
    } else {
      prevNode = pNode;
      pNode = pNode->next;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
541
    }
H
Haojun Liao 已提交
542
  }
543

H
Haojun Liao 已提交
544 545
  taosHashEntryWUnlock(pHashObj, pe);
  taosHashRUnlock(pHashObj);
H
Haojun Liao 已提交
546

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
547
  return code;
H
Haojun Liao 已提交
548 549
}

550
void taosHashClear(SHashObj *pHashObj) {
H
Haojun Liao 已提交
551 552 553
  if (pHashObj == NULL) {
    return;
  }
554 555 556

  SHashNode *pNode, *pNext;

H
Haojun Liao 已提交
557
  taosHashWLock(pHashObj);
558

559 560 561
  for (int32_t i = 0; i < pHashObj->capacity; ++i) {
    SHashEntry *pEntry = pHashObj->hashList[i];
    if (pEntry->num == 0) {
H
Haojun Liao 已提交
562
      assert(pEntry->next == NULL);
563 564
      continue;
    }
565

566 567
    pNode = pEntry->next;
    assert(pNode != NULL);
H
Haojun Liao 已提交
568

569 570
    while (pNode) {
      pNext = pNode->next;
571
      FREE_HASH_NODE(pHashObj->freeFp, pNode);
H
hjxilinx 已提交
572

573
      pNode = pNext;
574 575
    }

576 577
    pEntry->num = 0;
    pEntry->next = NULL;
578 579
  }

H
Haojun Liao 已提交
580 581
  pHashObj->size = 0;
  taosHashWUnlock(pHashObj);
582 583
}

wafwerar's avatar
wafwerar 已提交
584
// the input paras should be SHashObj **, so the origin input will be set by taosMemoryFreeClear(*pHashObj)
585 586 587 588 589
void taosHashCleanup(SHashObj *pHashObj) {
  if (pHashObj == NULL) {
    return;
  }

590
  taosHashClear(pHashObj);
wafwerar's avatar
wafwerar 已提交
591
  taosMemoryFreeClear(pHashObj->hashList);
H
Haojun Liao 已提交
592 593 594

  // destroy mem block
  size_t memBlock = taosArrayGetSize(pHashObj->pMemBlock);
H
Haojun Liao 已提交
595 596
  for (int32_t i = 0; i < memBlock; ++i) {
    void *p = taosArrayGetP(pHashObj->pMemBlock, i);
wafwerar's avatar
wafwerar 已提交
597
    taosMemoryFreeClear(p);
H
Haojun Liao 已提交
598 599 600
  }

  taosArrayDestroy(pHashObj->pMemBlock);
wafwerar's avatar
wafwerar 已提交
601
  taosMemoryFree(pHashObj);
602 603 604
}

// for profile only
H
Haojun Liao 已提交
605
int32_t taosHashGetMaxOverflowLinkLength(const SHashObj *pHashObj){
606
  if (pHashObj == NULL || taosHashTableEmpty(pHashObj)) {
607 608
    return 0;
  }
H
hjxilinx 已提交
609

610
  int32_t num = 0;
H
hjxilinx 已提交
611

H
Haojun Liao 已提交
612
  taosHashRLock((SHashObj*) pHashObj);
H
hjxilinx 已提交
613
  for (int32_t i = 0; i < pHashObj->size; ++i) {
H
Haojun Liao 已提交
614
    SHashEntry *pEntry = pHashObj->hashList[i];
H
Haojun Liao 已提交
615 616 617

    // fine grain per entry lock is not held since this is used
    // for profiling only and doesn't need an accurate count.
H
Haojun Liao 已提交
618 619
    if (num < pEntry->num) {
      num = pEntry->num;
620 621
    }
  }
H
hjxilinx 已提交
622

H
Haojun Liao 已提交
623
  taosHashRUnlock((SHashObj*) pHashObj);
624 625
  return num;
}
H
hjLiao 已提交
626 627

void taosHashTableResize(SHashObj *pHashObj) {
H
Haojun Liao 已提交
628
  if (!HASH_NEED_RESIZE(pHashObj)) {
H
hjLiao 已提交
629 630
    return;
  }
H
Haojun Liao 已提交
631

H
Haojun Liao 已提交
632 633 634 635
  int32_t newCapacity = (int32_t)(pHashObj->capacity << 1u);
  if (newCapacity > HASH_MAX_CAPACITY) {
//    uDebug("current capacity:%zu, maximum capacity:%d, no resize applied due to limitation is reached",
//           pHashObj->capacity, HASH_MAX_CAPACITY);
H
hjLiao 已提交
636 637 638
    return;
  }

H
Haojun Liao 已提交
639
  int64_t st = taosGetTimestampUs();
wafwerar's avatar
wafwerar 已提交
640
  void *pNewEntryList = taosMemoryRealloc(pHashObj->hashList, sizeof(void *) * newCapacity);
H
Haojun Liao 已提交
641 642
  if (pNewEntryList == NULL) {
//    uDebug("cache resize failed due to out of memory, capacity remain:%zu", pHashObj->capacity);
H
hjLiao 已提交
643 644
    return;
  }
H
Haojun Liao 已提交
645

H
Haojun Liao 已提交
646 647
  pHashObj->hashList = pNewEntryList;

H
Haojun Liao 已提交
648
  size_t inc = newCapacity - pHashObj->capacity;
wafwerar's avatar
wafwerar 已提交
649
  void * p = taosMemoryCalloc(inc, sizeof(SHashEntry));
H
Haojun Liao 已提交
650

H
Haojun Liao 已提交
651
  for (int32_t i = 0; i < inc; ++i) {
S
Shengliang Guan 已提交
652
    pHashObj->hashList[i + pHashObj->capacity] = (void *)((char *)p + i * sizeof(SHashEntry));
H
Haojun Liao 已提交
653 654 655 656
  }

  taosArrayPush(pHashObj->pMemBlock, &p);

H
Haojun Liao 已提交
657 658 659 660 661 662
  pHashObj->capacity = newCapacity;
  for (int32_t idx = 0; idx < pHashObj->capacity; ++idx) {
    SHashEntry *pe = pHashObj->hashList[idx];
    SHashNode *pNode;
    SHashNode *pNext;
    SHashNode *pPrev = NULL;
H
Haojun Liao 已提交
663

H
Haojun Liao 已提交
664
    if (pe->num == 0) {
H
Haojun Liao 已提交
665
      assert(pe->next == NULL);
H
Haojun Liao 已提交
666
      continue;
H
hjLiao 已提交
667
    }
H
Haojun Liao 已提交
668

H
Haojun Liao 已提交
669
    pNode = pe->next;
H
Haojun Liao 已提交
670

H
Haojun Liao 已提交
671
    assert(pNode != NULL);
H
Haojun Liao 已提交
672

H
Haojun Liao 已提交
673 674 675 676 677 678 679
    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;
H
Haojun Liao 已提交
680
        } else {
H
Haojun Liao 已提交
681
          pPrev->next = pNext;
H
Haojun Liao 已提交
682
        }
H
Haojun Liao 已提交
683

H
Haojun Liao 已提交
684 685
        SHashEntry *pNewEntry = pHashObj->hashList[newIdx];
        pushfrontNodeInEntryList(pNewEntry, pNode);
H
Haojun Liao 已提交
686
      } else {
H
Haojun Liao 已提交
687
        pPrev = pNode;
H
Haojun Liao 已提交
688
      }
H
Haojun Liao 已提交
689
      pNode = pNext;
H
hjLiao 已提交
690 691 692
    }
  }

H
Haojun Liao 已提交
693 694
  int64_t et = taosGetTimestampUs();

H
Haojun Liao 已提交
695 696
//  uDebug("hash table resize completed, new capacity:%d, load factor:%f, elapsed time:%fms", (int32_t)pHashObj->capacity,
//         ((double)pHashObj->size) / pHashObj->capacity, (et - st) / 1000.0);
H
hjLiao 已提交
697 698 699
}

SHashNode *doCreateHashNode(const void *key, size_t keyLen, const void *pData, size_t dsize, uint32_t hashVal) {
wafwerar's avatar
wafwerar 已提交
700
  SHashNode *pNewNode = taosMemoryMalloc(sizeof(SHashNode) + keyLen + dsize);
H
Haojun Liao 已提交
701

H
hjLiao 已提交
702
  if (pNewNode == NULL) {
H
Haojun Liao 已提交
703
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
hjLiao 已提交
704 705
    return NULL;
  }
H
Haojun Liao 已提交
706

H
Haojun Liao 已提交
707
  pNewNode->keyLen  = (uint32_t)keyLen;
H
hjLiao 已提交
708
  pNewNode->hashVal = hashVal;
S
hash  
Shengliang Guan 已提交
709
  pNewNode->dataLen = (uint32_t)dsize;
H
Haojun Liao 已提交
710
  pNewNode->refCount= 1;
711
  pNewNode->removed = 0;
H
Haojun Liao 已提交
712
  pNewNode->next    = NULL;
H
Haojun Liao 已提交
713 714 715 716

  memcpy(GET_HASH_NODE_DATA(pNewNode), pData, dsize);
  memcpy(GET_HASH_NODE_KEY(pNewNode), key, keyLen);

H
hjLiao 已提交
717 718 719
  return pNewNode;
}

H
Haojun Liao 已提交
720
void pushfrontNodeInEntryList(SHashEntry *pEntry, SHashNode *pNode) {
H
Haojun Liao 已提交
721
  assert(pNode != NULL && pEntry != NULL);
H
Haojun Liao 已提交
722

H
Haojun Liao 已提交
723 724
  pNode->next = pEntry->next;
  pEntry->next = pNode;
H
Haojun Liao 已提交
725

726
  ASSERT(pNode->next != pNode);
H
Haojun Liao 已提交
727
  pEntry->num += 1;
H
hjLiao 已提交
728 729
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
730 731 732 733
size_t taosHashGetMemSize(const SHashObj *pHashObj) {
  if (pHashObj == NULL) {
    return 0;
  }
H
Haojun Liao 已提交
734

H
Haojun Liao 已提交
735
  return (pHashObj->capacity * (sizeof(SHashEntry) + sizeof(void*))) + sizeof(SHashNode) * taosHashGetSize(pHashObj) + sizeof(SHashObj);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
736
}
H
Haojun Liao 已提交
737

H
Haojun Liao 已提交
738 739 740
void *taosHashGetKey(void *data, size_t* keyLen) {
  SHashNode * node = GET_HASH_PNODE(data);
  if (keyLen != NULL) {
L
Liu Jicong 已提交
741 742
    *keyLen = node->keyLen;
  }
D
dapan1121 已提交
743

H
Haojun Liao 已提交
744
  return GET_HASH_NODE_KEY(node);
W
wpan 已提交
745 746
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
747
// release the pNode, return next pNode, and lock the current entry
H
Haojun Liao 已提交
748
static void *taosHashReleaseNode(SHashObj *pHashObj, void *p, int *slot) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
749 750 751 752 753 754
  SHashNode *pOld = (SHashNode *)GET_HASH_PNODE(p);
  SHashNode *prevNode = NULL;

  *slot = HASH_INDEX(pOld->hashVal, pHashObj->capacity);
  SHashEntry *pe = pHashObj->hashList[*slot];

H
Haojun Liao 已提交
755
  taosHashEntryWLock(pHashObj, pe);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
756 757 758

  SHashNode *pNode = pe->next;
  while (pNode) {
H
Haojun Liao 已提交
759 760
    if (pNode == pOld)
      break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
761 762 763 764 765

    prevNode = pNode;
    pNode = pNode->next;
  }

S
hash  
Shengliang Guan 已提交
766
  if (pNode) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
767
    pNode = pNode->next;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
768 769 770 771 772
    while (pNode) {
      if (pNode->removed == 0) break;
      pNode = pNode->next;
    }

wafwerar's avatar
wafwerar 已提交
773
    atomic_sub_fetch_16(&pOld->refCount, 1);
H
Haojun Liao 已提交
774
    if (pOld->refCount <=0) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
775 776
      if (prevNode) {
        prevNode->next = pOld->next;
H
Haojun Liao 已提交
777
        ASSERT(prevNode->next != prevNode);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
778 779
      } else {
        pe->next = pOld->next;
H
Haojun Liao 已提交
780 781
        SHashNode* x = pe->next;
        ASSERT(x->next != x);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
782
      }
S
hash  
Shengliang Guan 已提交
783

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
784
      pe->num--;
H
Haojun Liao 已提交
785
      atomic_sub_fetch_64(&pHashObj->size, 1);
786
      FREE_HASH_NODE(pHashObj->freeFp, pOld);
S
hash  
Shengliang Guan 已提交
787
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
788
  } else {
H
Haojun Liao 已提交
789
//    uError("pNode:%p data:%p is not there!!!", pNode, p);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
790 791 792 793 794 795
  }

  return pNode;
}

void *taosHashIterate(SHashObj *pHashObj, void *p) {
S
hash  
Shengliang Guan 已提交
796
  if (pHashObj == NULL) return NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
797

H
Haojun Liao 已提交
798
  int  slot = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
799 800 801
  char *data = NULL;

  // only add the read lock to disable the resize process
H
Haojun Liao 已提交
802
  taosHashRLock(pHashObj);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
803 804 805 806 807 808

  SHashNode *pNode = NULL;
  if (p) {
    pNode = taosHashReleaseNode(pHashObj, p, &slot);
    if (pNode == NULL) {
      SHashEntry *pe = pHashObj->hashList[slot];
H
Haojun Liao 已提交
809
      taosHashEntryWUnlock(pHashObj, pe);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
810 811

      slot = slot + 1;
H
Haojun Liao 已提交
812
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
813
  }
H
Haojun Liao 已提交
814

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
815 816 817
  if (pNode == NULL) {
    for (; slot < pHashObj->capacity; ++slot) {
      SHashEntry *pe = pHashObj->hashList[slot];
H
Haojun Liao 已提交
818

H
Haojun Liao 已提交
819
      taosHashEntryWLock(pHashObj, pe);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
820 821

      pNode = pe->next;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
822 823 824 825 826
      while (pNode) {
        if (pNode->removed == 0) break;
        pNode = pNode->next;
      }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
827 828
      if (pNode) break;

H
Haojun Liao 已提交
829
      taosHashEntryWUnlock(pHashObj, pe);
H
Haojun Liao 已提交
830
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
831
  }
H
Haojun Liao 已提交
832

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
833 834
  if (pNode) {
    SHashEntry *pe = pHashObj->hashList[slot];
835

H
Haojun Liao 已提交
836 837 838
    uint16_t prevRef = atomic_load_16(&pNode->refCount);
    uint16_t afterRef = atomic_add_fetch_16(&pNode->refCount, 1);
    ASSERT(prevRef < afterRef);
839 840 841 842 843

    // the reference count value is overflow, which will cause the delete node operation immediately.
    if (prevRef > afterRef) {
      uError("hash entry ref count overflow, prev ref:%d, current ref:%d", prevRef, afterRef);
      // restore the value
H
Haojun Liao 已提交
844
      atomic_sub_fetch_16(&pNode->refCount, 1);
845 846 847 848 849 850 851 852 853
      data = NULL;
    } else {
      data = GET_HASH_NODE_DATA(pNode);
    }

    if (afterRef >= MAX_WARNING_REF_COUNT) {
      uWarn("hash entry ref count is abnormally high: %d", afterRef);
    }

H
Haojun Liao 已提交
854
    taosHashEntryWUnlock(pHashObj, pe);
H
hjLiao 已提交
855
  }
H
Haojun Liao 已提交
856

H
Haojun Liao 已提交
857
  taosHashRUnlock(pHashObj);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
858
  return data;
H
hjLiao 已提交
859
}
H
Haojun Liao 已提交
860

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
861 862
void taosHashCancelIterate(SHashObj *pHashObj, void *p) {
  if (pHashObj == NULL || p == NULL) return;
H
Haojun Liao 已提交
863

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
864
  // only add the read lock to disable the resize process
H
Haojun Liao 已提交
865
  taosHashRLock(pHashObj);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
866

H
Haojun Liao 已提交
867
  int slot;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
868 869 870
  taosHashReleaseNode(pHashObj, p, &slot);

  SHashEntry *pe = pHashObj->hashList[slot];
D
dapan1121 已提交
871

H
Haojun Liao 已提交
872 873 874 875
  taosHashEntryWUnlock(pHashObj, pe);
  taosHashRUnlock(pHashObj);
}

876
//TODO remove it
H
Haojun Liao 已提交
877 878 879
void *taosHashAcquire(SHashObj *pHashObj, const void *key, size_t keyLen) {
  void* p = NULL;
  return taosHashGetImpl(pHashObj, key, keyLen, &p, 0, true);
D
dapan1121 已提交
880 881
}

S
hash  
Shengliang Guan 已提交
882
void taosHashRelease(SHashObj *pHashObj, void *p) { taosHashCancelIterate(pHashObj, p); }