90#define HASH_INSERT(TYPE, NAME, TABLE, HASH_VALUE, DATA) \
92 hash_cell_t *cell3333; \
94 const uint64_t hash_value3333 = HASH_VALUE; \
96 hash_assert_can_modify(TABLE, hash_value3333); \
98 (DATA)->NAME = NULL; \
101 hash_get_nth_cell(TABLE, hash_calc_cell_id(hash_value3333, TABLE)); \
103 if (cell3333->node == NULL) { \
104 cell3333->node = DATA; \
106 struct3333 = (TYPE *)cell3333->node; \
108 while (struct3333->NAME != NULL) { \
109 struct3333 = (TYPE *)struct3333->NAME; \
112 struct3333->NAME = DATA; \
116#ifdef UNIV_HASH_DEBUG
117#define HASH_ASSERT_VALID(DATA) ut_a((void *)(DATA) != (void *)-1)
118#define HASH_INVALIDATE(DATA, NAME) *(void **)(&DATA->NAME) = (void *)-1
120#define HASH_ASSERT_VALID(DATA) \
123#define HASH_INVALIDATE(DATA, NAME) \
130#define HASH_DELETE(TYPE, NAME, TABLE, HASH_VALUE, DATA) \
132 hash_cell_t *cell3333; \
134 const uint64_t hash_value3333 = HASH_VALUE; \
136 hash_assert_can_modify(TABLE, hash_value3333); \
139 hash_get_nth_cell(TABLE, hash_calc_cell_id(hash_value3333, TABLE)); \
141 if (cell3333->node == DATA) { \
142 HASH_ASSERT_VALID(DATA->NAME); \
143 cell3333->node = DATA->NAME; \
145 struct3333 = (TYPE *)cell3333->node; \
147 while (struct3333->NAME != DATA) { \
148 struct3333 = (TYPE *)struct3333->NAME; \
152 struct3333->NAME = DATA->NAME; \
154 HASH_INVALIDATE(DATA, NAME); \
165#define HASH_GET_NEXT(NAME, DATA) ((DATA)->NAME)
168#define HASH_SEARCH(NAME, TABLE, HASH_VALUE, TYPE, DATA, ASSERTION, TEST) \
170 const uint64_t hash_value3333 = HASH_VALUE; \
172 hash_assert_can_search(TABLE, hash_value3333); \
175 (TYPE)hash_get_first(TABLE, hash_calc_cell_id(hash_value3333, TABLE)); \
176 HASH_ASSERT_VALID(DATA); \
178 while ((DATA) != NULL) { \
183 HASH_ASSERT_VALID(HASH_GET_NEXT(NAME, DATA)); \
184 (DATA) = (TYPE)HASH_GET_NEXT(NAME, DATA); \
190#define HASH_SEARCH_ALL(NAME, TABLE, TYPE, DATA, ASSERTION, TEST) \
194 for (i3333 = (TABLE)->get_n_cells(); i3333--;) { \
195 (DATA) = (TYPE)hash_get_first(TABLE, i3333); \
197 while ((DATA) != NULL) { \
198 HASH_ASSERT_VALID(DATA); \
205 (DATA) = (TYPE)HASH_GET_NEXT(NAME, DATA); \
208 if ((DATA) != NULL) { \
226#define HASH_DELETE_AND_COMPACT(TYPE, NAME, TABLE, NODE) \
230 hash_cell_t *cell111; \
231 uint64_t hash_value111; \
233 hash_value111 = (NODE)->hash_value; \
235 HASH_DELETE(TYPE, NAME, TABLE, hash_value111, NODE); \
238 (TYPE *)mem_heap_get_top(hash_get_heap(TABLE), sizeof(TYPE)); \
243 if (NODE != top_node111) { \
246 *(NODE) = *top_node111; \
248 cell111 = hash_get_nth_cell( \
249 TABLE, hash_calc_cell_id(top_node111->hash_value, TABLE)); \
253 if (cell111->node == top_node111) { \
256 cell111->node = NODE; \
260 node111 = static_cast<TYPE *>(cell111->node); \
262 while (top_node111 != HASH_GET_NEXT(NAME, node111)) { \
263 node111 = static_cast<TYPE *>(HASH_GET_NEXT(NAME, node111)); \
268 node111->NAME = NODE; \
274 mem_heap_free_top(hash_get_heap(TABLE), sizeof(TYPE)); \
277#ifndef UNIV_HOTBACKUP
280#define HASH_MIGRATE(OLD_TABLE, NEW_TABLE, NODE_TYPE, PTR_NAME, HASH_FUNC) \
283 size_t cell_count2222; \
285 cell_count2222 = hash_get_n_cells(OLD_TABLE); \
287 for (i2222 = 0; i2222 < cell_count2222; i2222++) { \
288 NODE_TYPE *node2222 = \
289 static_cast<NODE_TYPE *>(hash_get_first((OLD_TABLE), i2222)); \
292 NODE_TYPE *next2222 = static_cast<NODE_TYPE *>(node2222->PTR_NAME); \
293 uint64_t hash_value2222 = HASH_FUNC(node2222); \
295 HASH_INSERT(NODE_TYPE, PTR_NAME, (NEW_TABLE), hash_value2222, \
298 node2222 = next2222; \
308 uint64_t hash_value);
326 uint64_t hash_value);
336 uint64_t hash_value);
346 uint64_t hash_value);
370#define hash_get_heap(table) ((table)->heap)
371#define hash_lock_x_all(t) ((void)0)
372#define hash_unlock_x_all(t) ((void)0)
373#define hash_unlock_x_all_but(t, l) ((void)0)
381 cells = ut::make_unique<hash_cell_t[]>(prime);
411#ifndef UNIV_HOTBACKUP
428#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
429#ifndef UNIV_HOTBACKUP
456#ifndef UNIV_HOTBACKUP
Definition: hash0hash.h:375
static constexpr uint32_t HASH_TABLE_MAGIC_N
Definition: hash0hash.h:465
hash_table_t(size_t n)
Definition: hash0hash.h:377
size_t get_n_cells() const
Returns number of cells in cells[] array.
Definition: hash0hash.h:393
size_t n_sync_obj
if rw_locks != nullptr, then it's their number (must be a power of two).
Definition: hash0hash.h:457
uint32_t magic_n
Definition: hash0hash.h:466
void set_n_cells(size_t n)
Sets the number of n_cells, to the provided one.
Definition: hash0hash.h:408
enum hash_table_sync_t type
Either: a) HASH_TABLE_SYNC_NONE in which case n_sync_obj is 0 and rw_locks is nullptr or b) HASH_TABL...
Definition: hash0hash.h:424
rw_lock_t * rw_locks
nullptr, or an array of n_sync_obj rw_locks used to protect segments of the hash table.
Definition: hash0hash.h:460
ut::mt_fast_modulo_t n_cells_fast_modulo
Utility to calculate the modulo n_cells fast.
Definition: hash0hash.h:445
bool adaptive
Definition: hash0hash.h:429
std::atomic< size_t > n_cells
The number of cells in the hash table.
Definition: hash0hash.h:440
const ut::fast_modulo_t get_n_cells_fast_modulo() const
Returns a helper class for calculating fast modulo n_cells.
Definition: hash0hash.h:399
~hash_table_t()
Definition: hash0hash.h:385
ut::unique_ptr< hash_cell_t[]> cells
The pointer to the array of cells.
Definition: hash0hash.h:453
mem_heap_t * heap
Definition: hash0hash.h:463
Allows to execute x % mod for a specified mod in a fast way, without using a slow operation of divisi...
Definition: ut0math.h:165
A class that allows to atomically set new modulo value for fast modulo computations.
Definition: ut0math.h:257
void store(uint64_t new_mod)
Definition: ut0math.h:272
fast_modulo_t load() const
Definition: ut0math.h:265
void * hash_node_t
Definition: hash0hash.h:47
static rw_lock_t * hash_lock_s_confirm(rw_lock_t *hash_lock, hash_table_t *table, uint64_t hash_value)
If not appropriate rw_lock for a hash value in a hash table, relock S-lock the another rw_lock until ...
void hash_unlock_x_all_but(hash_table_t *table, rw_lock_t *keep_lock)
Releases all but passed in lock of a hash table,.
Definition: hash0hash.cc:84
static void *& hash_get_first(hash_table_t *table, size_t cell_id)
Gets the first struct in a hash chain, NULL if none.
Definition: hash0hash.h:159
static rw_lock_t * hash_lock_x_confirm(rw_lock_t *hash_lock, hash_table_t *table, uint64_t hash_value)
If not appropriate rw_lock for a hash value in a hash table, relock X-lock the another rw_lock until ...
static uint64_t hash_get_sync_obj_index(hash_table_t *table, uint64_t hash_value)
Gets the sync object index for a hash value in a hash table.
hash_table_sync_t
Definition: hash0hash.h:53
@ HASH_TABLE_SYNC_RW_LOCK
Use rw_locks to control access to this hash_table.
Definition: hash0hash.h:57
@ HASH_TABLE_SYNC_NONE
Don't use any internal synchronization objects for this hash_table.
Definition: hash0hash.h:54
static hash_cell_t * hash_get_nth_cell(hash_table_t *table, size_t n)
Gets the nth cell in a hash table.
void hash_lock_x_all(hash_table_t *table)
Reserves all the locks of a hash table, in an ascending order.
Definition: hash0hash.cc:55
static rw_lock_t * hash_get_lock(hash_table_t *table, uint64_t hash_value)
Gets the rw_lock for a hash value in a hash table.
static mem_heap_t * hash_get_heap(hash_table_t *table)
Gets the heap for a hash value in a hash table.
static rw_lock_t * hash_get_nth_lock(hash_table_t *table, size_t i)
Gets the nth rw_lock in a hash table.
static uint64_t hash_calc_cell_id(uint64_t hash_value, hash_table_t const *table)
Calculates the cell index from a hashed value for a specified hash table.
bool hash_lock_has_all_x(const hash_table_t *table)
Verifies that the current thread holds X-latch on all shards.
Definition: hash0hash.cc:43
static void hash_table_clear(hash_table_t *table)
Clears a hash table so that all the cells become empty.
void hash_create_sync_obj(hash_table_t *table, latch_id_t id, size_t n_sync_obj)
Creates a sync object array to protect a hash table.
Definition: hash0hash.cc:104
static size_t hash_get_n_cells(hash_table_t const *table)
Returns the number of cells in a hash table.
void hash_unlock_x_all(hash_table_t *table)
Releases all the locks of a hash table, in an ascending order.
Definition: hash0hash.cc:70
The simple hash table utility.
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
uint64_t find_prime(uint64_t n)
Looks for a prime number slightly greater than the given argument.
Definition: ut0math.cc:36
std::conditional_t< !std::is_array< T >::value, std::unique_ptr< T, detail::Deleter< T > >, std::conditional_t< detail::is_unbounded_array_v< T >, std::unique_ptr< T, detail::Array_deleter< std::remove_extent_t< T > > >, void > > unique_ptr
The following is a common type that is returned by all the ut::make_unique (non-aligned) specializati...
Definition: ut0new.h:2440
Definition: hash0hash.h:61
void * node
hash chain node, NULL if none
Definition: hash0hash.h:62
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:302
The structure used in the spin lock implementation of a read-write lock.
Definition: sync0rw.h:363
The read-write lock (for threads, not for database transactions)
latch_id_t
Each latch has an ID.
Definition: sync0types.h:345
Version control for database, common definitions, and include files.
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:105
Random numbers and hashing.
int n
Definition: xcom_base.cc:509