hash.h 2.9 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
hjxilinx 已提交
23
#include "hashfunc.h"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

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

typedef struct SHashNode {
  char *key;  // null-terminated string
  union {
    struct SHashNode * prev;
    struct SHashEntry *prev1;
  };

  struct SHashNode *next;
  uint32_t          hashVal;  // the hash value of key, if hashVal == HASH_VALUE_IN_TRASH, this node is moved to trash
  uint32_t          keyLen;   // length of the key
  char              data[];
} SHashNode;

typedef struct SHashEntry {
  SHashNode *next;
  uint32_t   num;
} SHashEntry;

H
hjxilinx 已提交
48
typedef struct SHashObj {
49
  SHashEntry **hashList;
H
hjxilinx 已提交
50 51
  size_t       capacity;         // number of slots
  size_t       size;             // number of elements in hash table
52
  _hash_fn_t   hashFp;           // hash function
53

H
hjxilinx 已提交
54 55
#if defined (LINUX)
  pthread_rwlock_t* lock;
56
#else
H
hjxilinx 已提交
57
  pthread_mutex_t*  lock;
58
#endif
H
hjxilinx 已提交
59
} SHashObj;
60

H
hjxilinx 已提交
61 62 63 64 65 66 67 68 69
/**
 * 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
 */
SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool threadsafe);
70

H
hjxilinx 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/**
 * return the size of hash table
 * @param pHashObj
 * @return
 */
size_t taosHashGetSize(const SHashObj *pHashObj);

/**
 * 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
 */
int32_t taosHashPut(SHashObj *pHashObj, const char *key, size_t keyLen, void *data, size_t size);
88

H
hjxilinx 已提交
89 90 91 92 93 94 95 96 97
/**
 * return the payload data with the specified key
 *
 * @param pHashObj
 * @param key
 * @param keyLen
 * @return
 */
void *taosHashGet(SHashObj *pHashObj, const char *key, size_t keyLen);
98

H
hjxilinx 已提交
99 100 101 102 103 104 105
/**
 * remove item with the specified key
 * @param pHashObj
 * @param key
 * @param keyLen
 */
void  taosHashRemove(SHashObj *pHashObj, const char *key, size_t keyLen);
106

H
hjxilinx 已提交
107 108 109 110 111
/**
 * clean up hash table
 * @param handle
 */
void taosHashCleanup(SHashObj *pHashObj);
112

H
hjxilinx 已提交
113 114 115 116 117 118
/**
 *
 * @param pHashObj
 * @return
 */
int32_t taosHashGetMaxOverflowLinkLength(const SHashObj *pHashObj);
119

H
hjxilinx 已提交
120 121 122
#ifdef __cplusplus
}
#endif
123 124

#endif  // TDENGINE_HASH_H