thash.c 23.2 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
#include "os.h"
L
Liu Jicong 已提交
19
#include "taoserror.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
#define MAX_WARNING_REF_COUNT    10000
D
dapan1121 已提交
24
#define HASH_MAX_CAPACITY        (1024 * 1024 * 1024)
H
Haojun Liao 已提交
25 26 27 28 29
#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)

L
Liu Jicong 已提交
30 31 32
#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)))
H
Haojun Liao 已提交
33

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

H
Haojun Liao 已提交
42
struct SHashNode {
L
Liu Jicong 已提交
43 44 45 46 47 48 49
  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 已提交
50 51
};

H
Haojun Liao 已提交
52
typedef struct SHashEntry {
L
Liu Jicong 已提交
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
struct SHashObj {
X
Xiaoyu Wang 已提交
59
  SHashEntry      **hashList;
H
Haojun Liao 已提交
60
  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
  _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
X
Xiaoyu Wang 已提交
68
  SArray           *pMemBlock;     // memory block allocated for SHashEntry
H
Haojun Liao 已提交
69
  _hash_before_fn_t callbackFp;    // function invoked before return the value to caller
H
Haojun Liao 已提交
70
//  int64_t           compTimes;
H
Haojun Liao 已提交
71
};
H
Haojun Liao 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84

/*
 * 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 已提交
85 86
    return;
  }
H
Haojun Liao 已提交
87 88

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

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

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

H
Haojun Liao 已提交
99 100
static FORCE_INLINE void taosHashRUnlock(SHashObj *pHashObj) {
  if (pHashObj->type == HASH_NO_LOCK) {
H
hjxilinx 已提交
101 102
    return;
  }
H
Haojun Liao 已提交
103 104 105 106

  taosRUnLockLatch(&pHashObj->lock);
}

L
Liu Jicong 已提交
107
static FORCE_INLINE void taosHashEntryWLock(const SHashObj *pHashObj, SHashEntry *pe) {
H
Haojun Liao 已提交
108 109 110 111 112 113
  if (pHashObj->type == HASH_NO_LOCK) {
    return;
  }
  taosWLockLatch(&pe->latch);
}

L
Liu Jicong 已提交
114
static FORCE_INLINE void taosHashEntryWUnlock(const SHashObj *pHashObj, SHashEntry *pe) {
H
Haojun Liao 已提交
115 116 117 118 119 120 121
  if (pHashObj->type == HASH_NO_LOCK) {
    return;
  }

  taosWUnLockLatch(&pe->latch);
}

L
Liu Jicong 已提交
122
static FORCE_INLINE void taosHashEntryRLock(const SHashObj *pHashObj, SHashEntry *pe) {
H
Haojun Liao 已提交
123 124 125 126 127
  if (pHashObj->type == HASH_NO_LOCK) {
    return;
  }

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

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

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

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

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

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

    pNode = pNode->next;
  }

  return pNode;
}

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

H
hjLiao 已提交
170
/**
H
Haojun Liao 已提交
171 172
 * allocate and initialize a hash node
 *
H
hjLiao 已提交
173 174
 * @param key      key of object for hash, usually a null-terminated string
 * @param keyLen   length of key
H
Haojun Liao 已提交
175
 * @param pData    data to be stored in hash node
H
hjLiao 已提交
176 177 178 179
 * @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);
180

H
hjLiao 已提交
181
/**
H
Haojun Liao 已提交
182
 * update the hash node
H
hjLiao 已提交
183
 *
H
Haojun Liao 已提交
184 185 186 187 188
 * @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 已提交
189
 */
L
Liu Jicong 已提交
190 191
static FORCE_INLINE void doUpdateHashNode(SHashObj *pHashObj, SHashEntry *pe, SHashNode *prev, SHashNode *pNode,
                                          SHashNode *pNewNode) {
H
Haojun Liao 已提交
192
  assert(pNode->keyLen == pNewNode->keyLen);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
193

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

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

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

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

222 223 224 225 226 227 228 229
/**
 * 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);

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

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);
255
  pHashObj->size = 0;
H
Haojun Liao 已提交
256

257
  pHashObj->equalFp = memcmp;
L
Liu Jicong 已提交
258
  pHashObj->hashFp = fn;
H
Haojun Liao 已提交
259
  pHashObj->type = type;
260
  pHashObj->lock = 0;
H
Haojun Liao 已提交
261
  pHashObj->enableUpdate = update;
262 263
  pHashObj->freeFp = NULL;
  pHashObj->callbackFp = NULL;
264

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

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

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

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

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

  taosArrayPush(pHashObj->pMemBlock, &p);
H
hjxilinx 已提交
296
  return pHashObj;
297 298
}

299 300 301
void taosHashSetEqualFp(SHashObj *pHashObj, _equal_fn_t fp) {
  if (pHashObj != NULL && fp != NULL) {
    pHashObj->equalFp = fp;
S
hash  
Shengliang Guan 已提交
302 303
  }
}
304

H
Haojun Liao 已提交
305 306 307 308 309 310
void taosHashSetFreeFp(SHashObj *pHashObj, _hash_free_fn_t fp) {
  if (pHashObj != NULL && fp != NULL) {
    pHashObj->freeFp = fp;
  }
}

311
int32_t taosHashGetSize(const SHashObj *pHashObj) {
H
Haojun Liao 已提交
312
  if (pHashObj == NULL) {
313 314
    return 0;
  }
L
Liu Jicong 已提交
315
  return (int32_t)atomic_load_64((int64_t *)&pHashObj->size);
316 317
}

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

324
  uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen);
325

H
Haojun Liao 已提交
326 327
  // need the resize process, write lock applied
  if (HASH_NEED_RESIZE(pHashObj)) {
H
Haojun Liao 已提交
328
    taosHashWLock(pHashObj);
H
Haojun Liao 已提交
329
    taosHashTableResize(pHashObj);
H
Haojun Liao 已提交
330
    taosHashWUnlock(pHashObj);
H
Haojun Liao 已提交
331 332
  }

333
  // disable resize
H
Haojun Liao 已提交
334
  taosHashRLock(pHashObj);
H
Haojun Liao 已提交
335

H
Hongze Cheng 已提交
336
  uint32_t    slot = HASH_INDEX(hashVal, pHashObj->capacity);
H
Haojun Liao 已提交
337
  SHashEntry *pe = pHashObj->hashList[slot];
338

H
Haojun Liao 已提交
339
  taosHashEntryWLock(pHashObj, pe);
340

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

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

H
Haojun Liao 已提交
358
    prev = pNode;
H
Haojun Liao 已提交
359 360 361 362 363
    pNode = pNode->next;
  }

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

H
Haojun Liao 已提交
369
    pushfrontNodeInEntryList(pe, pNewNode);
H
Haojun Liao 已提交
370
    assert(pe->next != NULL);
H
Haojun Liao 已提交
371

H
Haojun Liao 已提交
372
    taosHashEntryWUnlock(pHashObj, pe);
H
Haojun Liao 已提交
373 374

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

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

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

H
Haojun Liao 已提交
392
    taosHashEntryWUnlock(pHashObj, pe);
H
Haojun Liao 已提交
393 394

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

L
Liu Jicong 已提交
400
static void *taosHashGetImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void **d, int32_t *size, bool addRef);
D
dapan1121 已提交
401

H
hjLiao 已提交
402
void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen) {
L
Liu Jicong 已提交
403
  void *p = NULL;
H
Haojun Liao 已提交
404
  return taosHashGetImpl(pHashObj, key, keyLen, &p, 0, false);
H
Haojun Liao 已提交
405
}
H
Haojun Liao 已提交
406

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

L
Liu Jicong 已提交
413
int32_t taosHashGetDup_m(SHashObj *pHashObj, const void *key, size_t keyLen, void **destBuf, int32_t *size) {
H
Haojun Liao 已提交
414
  terrno = 0;
415

L
Liu Jicong 已提交
416
  /*char* p = */ taosHashGetImpl(pHashObj, key, keyLen, destBuf, size, false);
H
Haojun Liao 已提交
417
  return terrno;
418
}
H
Haojun Liao 已提交
419

L
Liu Jicong 已提交
420
void *taosHashGetImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void **d, int32_t *size, bool addRef) {
H
Haojun Liao 已提交
421
  if (pHashObj == NULL || taosHashTableEmpty(pHashObj) || keyLen == 0 || key == NULL) {
H
Haojun Liao 已提交
422 423 424
    return NULL;
  }

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

H
Haojun Liao 已提交
427
  // only add the read lock to disable the resize process
H
Haojun Liao 已提交
428
  taosHashRLock(pHashObj);
H
Haojun Liao 已提交
429

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

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

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

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

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

H
Haojun Liao 已提交
456 457
    if (size != NULL) {
      if (*d == NULL) {
L
Liu Jicong 已提交
458
        *size = pNode->dataLen;
wafwerar's avatar
wafwerar 已提交
459
        *d = taosMemoryCalloc(1, *size);
H
Haojun Liao 已提交
460 461 462 463 464
        if (*d == NULL) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          return NULL;
        }
      } else if (*size < pNode->dataLen) {
L
Liu Jicong 已提交
465 466
        *size = pNode->dataLen;
        char *tmp = taosMemoryRealloc(*d, *size);
H
Haojun Liao 已提交
467 468 469 470 471 472 473 474 475 476 477
        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 已提交
478
    }
479

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

484
    data = GET_HASH_NODE_DATA(pNode);
H
Haojun Liao 已提交
485 486
  }

H
Haojun Liao 已提交
487 488
  taosHashEntryRUnlock(pHashObj, pe);
  taosHashRUnlock(pHashObj);
H
Haojun Liao 已提交
489 490

  return data;
491 492
}

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

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

H
Haojun Liao 已提交
500
  // disable the resize process
H
Haojun Liao 已提交
501
  taosHashRLock(pHashObj);
H
Haojun Liao 已提交
502

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

H
Haojun Liao 已提交
506
  taosHashEntryWLock(pHashObj, pe);
507 508

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

H
Haojun Liao 已提交
512 513
    taosHashEntryWUnlock(pHashObj, pe);
    taosHashRUnlock(pHashObj);
H
Haojun Liao 已提交
514
    return -1;
H
Haojun Liao 已提交
515 516
  }

L
Liu Jicong 已提交
517
  int        code = -1;
H
Haojun Liao 已提交
518
  SHashNode *pNode = pe->next;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
519
  SHashNode *prevNode = NULL;
H
Haojun Liao 已提交
520

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

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

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

H
Haojun Liao 已提交
546 547
  taosHashEntryWUnlock(pHashObj, pe);
  taosHashRUnlock(pHashObj);
H
Haojun Liao 已提交
548

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

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

  SHashNode *pNode, *pNext;

H
Haojun Liao 已提交
559
  taosHashWLock(pHashObj);
560

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

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

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

575
      pNode = pNext;
576 577
    }

578 579
    pEntry->num = 0;
    pEntry->next = NULL;
580 581
  }

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

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

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

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

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

// for profile only
L
Liu Jicong 已提交
607
int32_t taosHashGetMaxOverflowLinkLength(const SHashObj *pHashObj) {
608
  if (pHashObj == NULL || taosHashTableEmpty(pHashObj)) {
609 610
    return 0;
  }
H
hjxilinx 已提交
611

612
  int32_t num = 0;
H
hjxilinx 已提交
613

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

    // 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 已提交
620 621
    if (num < pEntry->num) {
      num = pEntry->num;
622 623
    }
  }
H
hjxilinx 已提交
624

L
Liu Jicong 已提交
625
  taosHashRUnlock((SHashObj *)pHashObj);
626 627
  return num;
}
H
hjLiao 已提交
628 629

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

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

H
Haojun Liao 已提交
641
  int64_t st = taosGetTimestampUs();
642
  SHashEntry **pNewEntryList = taosMemoryRealloc(pHashObj->hashList, sizeof(SHashEntry *) * newCapacity);
H
Haojun Liao 已提交
643
  if (pNewEntryList == NULL) {
L
Liu Jicong 已提交
644
    //    uDebug("cache resize failed due to out of memory, capacity remain:%zu", pHashObj->capacity);
H
hjLiao 已提交
645 646
    return;
  }
H
Haojun Liao 已提交
647

H
Haojun Liao 已提交
648 649
  pHashObj->hashList = pNewEntryList;

H
Haojun Liao 已提交
650
  size_t inc = newCapacity - pHashObj->capacity;
X
Xiaoyu Wang 已提交
651
  void  *p = taosMemoryCalloc(inc, sizeof(SHashEntry));
H
Haojun Liao 已提交
652

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

  taosArrayPush(pHashObj->pMemBlock, &p);

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

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

H
Haojun Liao 已提交
671
    pNode = pe->next;
H
Haojun Liao 已提交
672

H
Haojun Liao 已提交
673
    assert(pNode != NULL);
H
Haojun Liao 已提交
674

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

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

H
Haojun Liao 已提交
695 696
  int64_t et = taosGetTimestampUs();

L
Liu Jicong 已提交
697 698 699
  //  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 已提交
700 701 702
}

SHashNode *doCreateHashNode(const void *key, size_t keyLen, const void *pData, size_t dsize, uint32_t hashVal) {
L
Liu Jicong 已提交
703
  SHashNode *pNewNode = taosMemoryMalloc(sizeof(SHashNode) + keyLen + dsize + 1);
H
Haojun Liao 已提交
704

H
hjLiao 已提交
705
  if (pNewNode == NULL) {
H
Haojun Liao 已提交
706
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
hjLiao 已提交
707 708
    return NULL;
  }
H
Haojun Liao 已提交
709

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

L
Liu Jicong 已提交
717
  if (pData) memcpy(GET_HASH_NODE_DATA(pNewNode), pData, dsize);
H
Haojun Liao 已提交
718 719
  memcpy(GET_HASH_NODE_KEY(pNewNode), key, keyLen);

H
hjLiao 已提交
720 721 722
  return pNewNode;
}

H
Haojun Liao 已提交
723
void pushfrontNodeInEntryList(SHashEntry *pEntry, SHashNode *pNode) {
H
Haojun Liao 已提交
724
  assert(pNode != NULL && pEntry != NULL);
H
Haojun Liao 已提交
725

H
Haojun Liao 已提交
726 727
  pNode->next = pEntry->next;
  pEntry->next = pNode;
H
Haojun Liao 已提交
728

729
  ASSERT(pNode->next != pNode);
H
Haojun Liao 已提交
730
  pEntry->num += 1;
H
hjLiao 已提交
731 732
}

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

L
Liu Jicong 已提交
738 739
  return (pHashObj->capacity * (sizeof(SHashEntry) + sizeof(void *))) + sizeof(SHashNode) * taosHashGetSize(pHashObj) +
         sizeof(SHashObj);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
740
}
H
Haojun Liao 已提交
741

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

H
Haojun Liao 已提交
748
  return GET_HASH_NODE_KEY(node);
W
wpan 已提交
749 750
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
751
// release the pNode, return next pNode, and lock the current entry
H
Haojun Liao 已提交
752
static void *taosHashReleaseNode(SHashObj *pHashObj, void *p, int *slot) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
753 754 755 756 757 758
  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 已提交
759
  taosHashEntryWLock(pHashObj, pe);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
760 761 762

  SHashNode *pNode = pe->next;
  while (pNode) {
L
Liu Jicong 已提交
763
    if (pNode == pOld) break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
764 765 766 767 768

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

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

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

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

  return pNode;
}

void *taosHashIterate(SHashObj *pHashObj, void *p) {
D
dapan1121 已提交
801
  if (pHashObj == NULL || pHashObj->size == 0) return NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
802

L
Liu Jicong 已提交
803
  int   slot = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
804 805 806
  char *data = NULL;

  // only add the read lock to disable the resize process
H
Haojun Liao 已提交
807
  taosHashRLock(pHashObj);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
808 809 810 811 812 813

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

      slot = slot + 1;
H
Haojun Liao 已提交
817
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
818
  }
H
Haojun Liao 已提交
819

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

H
Haojun Liao 已提交
824
      taosHashEntryWLock(pHashObj, pe);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
825 826

      pNode = pe->next;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
827 828 829 830 831
      while (pNode) {
        if (pNode->removed == 0) break;
        pNode = pNode->next;
      }

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

H
Haojun Liao 已提交
834
      taosHashEntryWUnlock(pHashObj, pe);
H
Haojun Liao 已提交
835
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
836
  }
H
Haojun Liao 已提交
837

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

841
    /*uint16_t prevRef = atomic_load_16(&pNode->refCount);*/
H
Haojun Liao 已提交
842
    uint16_t afterRef = atomic_add_fetch_16(&pNode->refCount, 1);
843
#if 0
H
Haojun Liao 已提交
844
    ASSERT(prevRef < afterRef);
845 846 847 848 849

    // 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 已提交
850
      atomic_sub_fetch_16(&pNode->refCount, 1);
851 852 853 854
      data = NULL;
    } else {
      data = GET_HASH_NODE_DATA(pNode);
    }
855 856
#endif
    data = GET_HASH_NODE_DATA(pNode);
857 858 859 860 861

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

H
Haojun Liao 已提交
862
    taosHashEntryWUnlock(pHashObj, pe);
H
hjLiao 已提交
863
  }
H
Haojun Liao 已提交
864

H
Haojun Liao 已提交
865
  taosHashRUnlock(pHashObj);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
866
  return data;
H
hjLiao 已提交
867
}
H
Haojun Liao 已提交
868

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

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

H
Haojun Liao 已提交
875
  int slot;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
876 877 878
  taosHashReleaseNode(pHashObj, p, &slot);

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

H
Haojun Liao 已提交
880 881 882 883
  taosHashEntryWUnlock(pHashObj, pe);
  taosHashRUnlock(pHashObj);
}

L
Liu Jicong 已提交
884
// TODO remove it
H
Haojun Liao 已提交
885
void *taosHashAcquire(SHashObj *pHashObj, const void *key, size_t keyLen) {
L
Liu Jicong 已提交
886
  void *p = NULL;
H
Haojun Liao 已提交
887
  return taosHashGetImpl(pHashObj, key, keyLen, &p, 0, true);
D
dapan1121 已提交
888 889
}

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

H
Haojun Liao 已提交
892
//int64_t taosHashGetCompTimes(SHashObj *pHashObj) { return atomic_load_64(&pHashObj->compTimes); }