tsimplehash.c 10.6 KB
Newer Older
H
Haojun Liao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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/>.
 */

#include "tsimplehash.h"
#include "taoserror.h"

#define SHASH_DEFAULT_LOAD_FACTOR 0.75
C
Cary Xu 已提交
20
#define HASH_MAX_CAPACITY         (1024 * 1024 * 16)
H
Haojun Liao 已提交
21 22
#define SHASH_NEED_RESIZE(_h)     ((_h)->size >= (_h)->capacity * SHASH_DEFAULT_LOAD_FACTOR)

C
Cary Xu 已提交
23 24
#define GET_SHASH_NODE_KEY(_n, _dl) ((char *)(_n) + sizeof(SHNode) + (_dl))
#define GET_SHASH_NODE_DATA(_n)     ((char *)(_n) + sizeof(SHNode))
H
Haojun Liao 已提交
25

C
Cary Xu 已提交
26
#define HASH_INDEX(v, c) ((v) & ((c)-1))
H
Haojun Liao 已提交
27

C
Cary Xu 已提交
28 29 30
#define FREE_HASH_NODE(_n)   \
  do {                       \
    taosMemoryFreeClear(_n); \
H
Haojun Liao 已提交
31 32
  } while (0);

C
Cary Xu 已提交
33
#pragma pack(push, 4)
H
Haojun Liao 已提交
34
typedef struct SHNode {
C
Cary Xu 已提交
35
  struct SHNode *next;
C
Cary Xu 已提交
36 37
  uint32_t       keyLen : 20;
  uint32_t       dataLen : 12;
C
Cary Xu 已提交
38
  char           data[];
H
Haojun Liao 已提交
39
} SHNode;
C
Cary Xu 已提交
40
#pragma pack(pop)
H
Haojun Liao 已提交
41

42
struct SSHashObj {
C
Cary Xu 已提交
43 44
  SHNode    **hashList;
  size_t      capacity;  // number of slots
C
Cary Xu 已提交
45 46 47
  int64_t     size;     // number of elements in hash table
  _hash_fn_t  hashFp;   // hash function
  _equal_fn_t equalFp;  // equal function
48
};
H
Haojun Liao 已提交
49 50

static FORCE_INLINE int32_t taosHashCapacity(int32_t length) {
wafwerar's avatar
wafwerar 已提交
51
  int32_t len = (length < HASH_MAX_CAPACITY ? length : HASH_MAX_CAPACITY);
H
Haojun Liao 已提交
52 53 54 55 56 57

  int32_t i = 4;
  while (i < len) i = (i << 1u);
  return i;
}

C
Cary Xu 已提交
58
SSHashObj *tSimpleHashInit(size_t capacity, _hash_fn_t fn) {
H
Haojun Liao 已提交
59 60 61 62 63 64
  ASSERT(fn != NULL);

  if (capacity == 0) {
    capacity = 4;
  }

C
Cary Xu 已提交
65
  SSHashObj *pHashObj = (SSHashObj *)taosMemoryCalloc(1, sizeof(SSHashObj));
C
Cary Xu 已提交
66
  if (!pHashObj) {
H
Haojun Liao 已提交
67 68 69 70 71 72 73 74
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  // the max slots is not defined by user
  pHashObj->capacity = taosHashCapacity((int32_t)capacity);

  pHashObj->equalFp = memcmp;
C
Cary Xu 已提交
75
  pHashObj->hashFp = fn;
H
Haojun Liao 已提交
76 77 78
  ASSERT((pHashObj->capacity & (pHashObj->capacity - 1)) == 0);


wafwerar's avatar
wafwerar 已提交
79
  pHashObj->hashList = (SHNode **)taosMemoryCalloc(pHashObj->capacity, sizeof(void *));
C
Cary Xu 已提交
80
  if (!pHashObj->hashList) {
wafwerar's avatar
wafwerar 已提交
81
    taosMemoryFree(pHashObj);
H
Haojun Liao 已提交
82 83 84 85 86 87 88
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
  return pHashObj;
}

int32_t tSimpleHashGetSize(const SSHashObj *pHashObj) {
C
Cary Xu 已提交
89
  if (!pHashObj) {
H
Haojun Liao 已提交
90 91
    return 0;
  }
C
Cary Xu 已提交
92
  return (int32_t)atomic_load_64((int64_t *)&pHashObj->size);
H
Haojun Liao 已提交
93 94
}

C
Cary Xu 已提交
95 96
static SHNode *doCreateHashNode(const void *key, size_t keyLen, const void *data, size_t dataLen, uint32_t hashVal) {
  SHNode *pNewNode = taosMemoryMalloc(sizeof(SHNode) + keyLen + dataLen);
C
Cary Xu 已提交
97
  if (!pNewNode) {
H
Haojun Liao 已提交
98 99 100
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
C
Cary Xu 已提交
101 102
  pNewNode->keyLen = keyLen;
  pNewNode->dataLen = dataLen;
H
Haojun Liao 已提交
103
  pNewNode->next = NULL;
C
Cary Xu 已提交
104 105
  memcpy(GET_SHASH_NODE_DATA(pNewNode), data, dataLen);
  memcpy(GET_SHASH_NODE_KEY(pNewNode, dataLen), key, keyLen);
H
Haojun Liao 已提交
106 107 108
  return pNewNode;
}

109
static void taosHashTableResize(SSHashObj *pHashObj) {
C
Cary Xu 已提交
110
  if (!SHASH_NEED_RESIZE(pHashObj)) {
H
Haojun Liao 已提交
111 112 113 114 115
    return;
  }

  int32_t newCapacity = (int32_t)(pHashObj->capacity << 1u);
  if (newCapacity > HASH_MAX_CAPACITY) {
C
Cary Xu 已提交
116 117
    //    uDebug("current capacity:%zu, maximum capacity:%d, no resize applied due to limitation is reached",
    //           pHashObj->capacity, HASH_MAX_CAPACITY);
H
Haojun Liao 已提交
118 119 120 121
    return;
  }

  int64_t st = taosGetTimestampUs();
C
Cary Xu 已提交
122
  void   *pNewEntryList = taosMemoryRealloc(pHashObj->hashList, sizeof(void *) * newCapacity);
C
Cary Xu 已提交
123
  if (!pNewEntryList) {
C
Cary Xu 已提交
124
    //    qWarn("hash resize failed due to out of memory, capacity remain:%zu", pHashObj->capacity);
H
Haojun Liao 已提交
125 126 127 128
    return;
  }

  size_t inc = newCapacity - pHashObj->capacity;
C
Cary Xu 已提交
129
  memset((char *)pNewEntryList + pHashObj->capacity * sizeof(void *), 0, inc);
H
Haojun Liao 已提交
130 131 132 133 134

  pHashObj->hashList = pNewEntryList;
  pHashObj->capacity = newCapacity;

  for (int32_t idx = 0; idx < pHashObj->capacity; ++idx) {
C
Cary Xu 已提交
135
    SHNode *pNode = pHashObj->hashList[idx];
C
Cary Xu 已提交
136
    if (!pNode) {
H
Haojun Liao 已提交
137 138 139
      continue;
    }

C
Cary Xu 已提交
140
    SHNode *pNext = NULL;
C
Cary Xu 已提交
141 142
    SHNode *pPrev = NULL;

H
Haojun Liao 已提交
143
    while (pNode != NULL) {
C
Cary Xu 已提交
144 145
      void    *key = GET_SHASH_NODE_KEY(pNode, pNode->dataLen);
      uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)pNode->keyLen);
H
Haojun Liao 已提交
146 147 148 149

      int32_t newIdx = HASH_INDEX(hashVal, pHashObj->capacity);
      pNext = pNode->next;
      if (newIdx != idx) {
C
Cary Xu 已提交
150
        if (!pPrev) {
H
Haojun Liao 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
          pHashObj->hashList[idx] = pNext;
        } else {
          pPrev->next = pNext;
        }

        pNode->next = pHashObj->hashList[newIdx];
        pHashObj->hashList[newIdx] = pNode;
      } else {
        pPrev = pNode;
      }

      pNode = pNext;
    }
  }

  int64_t et = taosGetTimestampUs();

C
Cary Xu 已提交
168 169 170
  //  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
Haojun Liao 已提交
171 172
}

C
Cary Xu 已提交
173
int32_t tSimpleHashPut(SSHashObj *pHashObj, const void *key, size_t keyLen, const void *data, size_t dataLen) {
C
Cary Xu 已提交
174
  if (!pHashObj || !key) {
H
Haojun Liao 已提交
175 176 177
    return -1;
  }

C
Cary Xu 已提交
178
  uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen);
H
Haojun Liao 已提交
179 180 181 182 183 184 185 186 187

  // need the resize process, write lock applied
  if (SHASH_NEED_RESIZE(pHashObj)) {
    taosHashTableResize(pHashObj);
  }

  int32_t slot = HASH_INDEX(hashVal, pHashObj->capacity);

  SHNode *pNode = pHashObj->hashList[slot];
C
Cary Xu 已提交
188
  if (!pNode) {
C
Cary Xu 已提交
189
    SHNode *pNewNode = doCreateHashNode(key, keyLen, data, dataLen, hashVal);
C
Cary Xu 已提交
190
    if (!pNewNode) {
H
Haojun Liao 已提交
191 192 193 194
      return -1;
    }

    pHashObj->hashList[slot] = pNewNode;
C
Cary Xu 已提交
195
    atomic_add_fetch_64(&pHashObj->size, 1);
H
Haojun Liao 已提交
196 197 198 199
    return 0;
  }

  while (pNode) {
C
Cary Xu 已提交
200
    if ((*(pHashObj->equalFp))(GET_SHASH_NODE_KEY(pNode, pNode->dataLen), key, keyLen) == 0) {
H
Haojun Liao 已提交
201 202 203 204 205
      break;
    }
    pNode = pNode->next;
  }

C
Cary Xu 已提交
206
  if (!pNode) {
C
Cary Xu 已提交
207
    SHNode *pNewNode = doCreateHashNode(key, keyLen, data, dataLen, hashVal);
C
Cary Xu 已提交
208
    if (!pNewNode) {
H
Haojun Liao 已提交
209 210 211 212 213
      return -1;
    }
    pNewNode->next = pHashObj->hashList[slot];
    pHashObj->hashList[slot] = pNewNode;
    atomic_add_fetch_64(&pHashObj->size, 1);
C
Cary Xu 已提交
214
  } else {  // update data
C
Cary Xu 已提交
215
    memcpy(GET_SHASH_NODE_DATA(pNode), data, dataLen);
H
Haojun Liao 已提交
216 217 218 219 220
  }

  return 0;
}

C
Cary Xu 已提交
221
static FORCE_INLINE SHNode *doSearchInEntryList(SSHashObj *pHashObj, const void *key, size_t keyLen, int32_t index) {
H
Haojun Liao 已提交
222 223
  SHNode *pNode = pHashObj->hashList[index];
  while (pNode) {
C
Cary Xu 已提交
224
    if ((*(pHashObj->equalFp))(GET_SHASH_NODE_KEY(pNode, pNode->dataLen), key, keyLen) == 0) {
H
Haojun Liao 已提交
225 226 227 228 229 230 231 232 233
      break;
    }

    pNode = pNode->next;
  }

  return pNode;
}

C
Cary Xu 已提交
234
static FORCE_INLINE bool taosHashTableEmpty(const SSHashObj *pHashObj) { return tSimpleHashGetSize(pHashObj) == 0; }
H
Haojun Liao 已提交
235

C
Cary Xu 已提交
236
void *tSimpleHashGet(SSHashObj *pHashObj, const void *key, size_t keyLen) {
C
Cary Xu 已提交
237
  if (!pHashObj || taosHashTableEmpty(pHashObj) || !key) {
H
Haojun Liao 已提交
238 239 240
    return NULL;
  }

C
Cary Xu 已提交
241
  uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen);
H
Haojun Liao 已提交
242 243 244

  int32_t slot = HASH_INDEX(hashVal, pHashObj->capacity);
  SHNode *pNode = pHashObj->hashList[slot];
C
Cary Xu 已提交
245
  if (!pNode) {
H
Haojun Liao 已提交
246 247 248 249
    return NULL;
  }

  char *data = NULL;
C
Cary Xu 已提交
250
  pNode = doSearchInEntryList(pHashObj, key, keyLen, slot);
H
Haojun Liao 已提交
251 252 253 254 255 256 257
  if (pNode != NULL) {
    data = GET_SHASH_NODE_DATA(pNode);
  }

  return data;
}

C
Cary Xu 已提交
258
int32_t tSimpleHashRemove(SSHashObj *pHashObj, const void *key, size_t keyLen) {
C
Cary Xu 已提交
259 260 261 262
  if (!pHashObj || !key) {
    return TSDB_CODE_FAILED;
  }

C
Cary Xu 已提交
263
  uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen);
C
Cary Xu 已提交
264 265 266 267 268 269

  int32_t slot = HASH_INDEX(hashVal, pHashObj->capacity);

  SHNode *pNode = pHashObj->hashList[slot];
  SHNode *pPrev = NULL;
  while (pNode) {
C
Cary Xu 已提交
270
    if ((*(pHashObj->equalFp))(GET_SHASH_NODE_KEY(pNode, pNode->dataLen), key, keyLen) == 0) {
C
Cary Xu 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283
      if (!pPrev) {
        pHashObj->hashList[slot] = pNode->next;
      } else {
        pPrev->next = pNode->next;
      }
      FREE_HASH_NODE(pNode);
      atomic_sub_fetch_64(&pHashObj->size, 1);
      break;
    }
    pPrev = pNode;
    pNode = pNode->next;
  }

284
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
285 286 287
}

void tSimpleHashClear(SSHashObj *pHashObj) {
C
Cary Xu 已提交
288
  if (!pHashObj || taosHashTableEmpty(pHashObj)) {
H
Haojun Liao 已提交
289 290 291
    return;
  }

C
Cary Xu 已提交
292
  SHNode *pNode = NULL, *pNext = NULL;
H
Haojun Liao 已提交
293 294
  for (int32_t i = 0; i < pHashObj->capacity; ++i) {
    pNode = pHashObj->hashList[i];
C
Cary Xu 已提交
295
    if (!pNode) {
H
Haojun Liao 已提交
296 297 298 299 300 301 302 303 304
      continue;
    }

    while (pNode) {
      pNext = pNode->next;
      FREE_HASH_NODE(pNode);
      pNode = pNext;
    }
  }
C
Cary Xu 已提交
305
  atomic_store_64(&pHashObj->size, 0);
H
Haojun Liao 已提交
306 307 308
}

void tSimpleHashCleanup(SSHashObj *pHashObj) {
C
Cary Xu 已提交
309
  if (!pHashObj) {
H
Haojun Liao 已提交
310 311 312 313
    return;
  }

  tSimpleHashClear(pHashObj);
wafwerar's avatar
wafwerar 已提交
314
  taosMemoryFreeClear(pHashObj->hashList);
C
Cary Xu 已提交
315
  taosMemoryFree(pHashObj);
H
Haojun Liao 已提交
316 317 318
}

size_t tSimpleHashGetMemSize(const SSHashObj *pHashObj) {
C
Cary Xu 已提交
319
  if (!pHashObj) {
H
Haojun Liao 已提交
320 321 322 323 324 325
    return 0;
  }

  return (pHashObj->capacity * sizeof(void *)) + sizeof(SHNode) * tSimpleHashGetSize(pHashObj) + sizeof(SSHashObj);
}

C
Cary Xu 已提交
326 327
void *tSimpleHashGetKey(void *data, size_t *keyLen) {
  SHNode *node = (SHNode *)((char *)data - offsetof(SHNode, data));
C
Cary Xu 已提交
328
  if (keyLen) {
C
Cary Xu 已提交
329
    *keyLen = node->keyLen;
H
Haojun Liao 已提交
330 331
  }

C
Cary Xu 已提交
332 333
  return POINTER_SHIFT(data, node->dataLen);
}
C
Cary Xu 已提交
334

C
Cary Xu 已提交
335 336 337
void *tSimpleHashIterate(const SSHashObj *pHashObj, void *data, int32_t *iter) {
  if (!pHashObj) {
    return NULL;
C
Cary Xu 已提交
338 339
  }

C
Cary Xu 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  SHNode *pNode = NULL;

  if (!data) {
    for (int32_t i = 0; i < pHashObj->capacity; ++i) {
      pNode = pHashObj->hashList[i];
      if (!pNode) {
        continue;
      }
      *iter = i;
      return GET_SHASH_NODE_DATA(pNode);
    }
    return NULL;
  }

  pNode = (SHNode *)((char *)data - offsetof(SHNode, data));

  if (pNode->next) {
    return GET_SHASH_NODE_DATA(pNode->next);
  }

  ++(*iter);
  for (int32_t i = *iter; i < pHashObj->capacity; ++i) {
    pNode = pHashObj->hashList[i];
    if (!pNode) {
      continue;
    }
    *iter = i;
    return GET_SHASH_NODE_DATA(pNode);
  }

  return NULL;
C
Cary Xu 已提交
371 372
}

C
Cary Xu 已提交
373
void *tSimpleHashIterateKV(const SSHashObj *pHashObj, void *data, void **key, int32_t *iter) {
C
Cary Xu 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386
  if (!pHashObj) {
    return NULL;
  }

  SHNode *pNode = NULL;

  if (!data) {
    for (int32_t i = 0; i < pHashObj->capacity; ++i) {
      pNode = pHashObj->hashList[i];
      if (!pNode) {
        continue;
      }
      *iter = i;
C
Cary Xu 已提交
387 388 389
      if (key) {
        *key = GET_SHASH_NODE_KEY(pNode, pNode->dataLen);
      }
C
Cary Xu 已提交
390 391 392 393 394 395 396 397
      return GET_SHASH_NODE_DATA(pNode);
    }
    return NULL;
  }

  pNode = (SHNode *)((char *)data - offsetof(SHNode, data));

  if (pNode->next) {
C
Cary Xu 已提交
398 399 400
    if (key) {
      *key = GET_SHASH_NODE_KEY(pNode->next, pNode->next->dataLen);
    }
C
Cary Xu 已提交
401 402 403 404 405 406 407 408 409 410
    return GET_SHASH_NODE_DATA(pNode->next);
  }

  ++(*iter);
  for (int32_t i = *iter; i < pHashObj->capacity; ++i) {
    pNode = pHashObj->hashList[i];
    if (!pNode) {
      continue;
    }
    *iter = i;
C
Cary Xu 已提交
411 412 413
    if (key) {
      *key = GET_SHASH_NODE_KEY(pNode, pNode->dataLen);
    }
C
Cary Xu 已提交
414 415 416 417
    return GET_SHASH_NODE_DATA(pNode);
  }

  return NULL;
H
Haojun Liao 已提交
418
}