You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
5.5 KiB

17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
17 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1994, 2011, Oracle and/or its affiliates. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  12. *****************************************************************************/
  13. /********************************************************************//**
  14. @file include/ha0ha.ic
  15. The hash table with external chains
  16. Created 8/18/1994 Heikki Tuuri
  17. *************************************************************************/
  18. #include "ut0rnd.h"
  19. #include "mem0mem.h"
  20. #include "btr0types.h"
  21. /***********************************************************//**
  22. Deletes a hash node. */
  23. UNIV_INTERN
  24. void
  25. ha_delete_hash_node(
  26. /*================*/
  27. hash_table_t* table, /*!< in: hash table */
  28. ha_node_t* del_node); /*!< in: node to be deleted */
  29. /******************************************************************//**
  30. Gets a hash node data.
  31. @return pointer to the data */
  32. UNIV_INLINE
  33. const rec_t*
  34. ha_node_get_data(
  35. /*=============*/
  36. const ha_node_t* node) /*!< in: hash chain node */
  37. {
  38. return(node->data);
  39. }
  40. /******************************************************************//**
  41. Sets hash node data. */
  42. UNIV_INLINE
  43. void
  44. ha_node_set_data_func(
  45. /*==================*/
  46. ha_node_t* node, /*!< in: hash chain node */
  47. #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
  48. buf_block_t* block, /*!< in: buffer block containing the data */
  49. #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
  50. const rec_t* data) /*!< in: pointer to the data */
  51. {
  52. #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
  53. node->block = block;
  54. #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
  55. node->data = data;
  56. }
  57. #if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG
  58. /** Sets hash node data.
  59. @param n in: hash chain node
  60. @param b in: buffer block containing the data
  61. @param d in: pointer to the data */
  62. # define ha_node_set_data(n,b,d) ha_node_set_data_func(n,b,d)
  63. #else /* UNIV_AHI_DEBUG || UNIV_DEBUG */
  64. /** Sets hash node data.
  65. @param n in: hash chain node
  66. @param b in: buffer block containing the data
  67. @param d in: pointer to the data */
  68. # define ha_node_set_data(n,b,d) ha_node_set_data_func(n,d)
  69. #endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */
  70. /******************************************************************//**
  71. Gets the next node in a hash chain.
  72. @return next node, NULL if none */
  73. UNIV_INLINE
  74. ha_node_t*
  75. ha_chain_get_next(
  76. /*==============*/
  77. ha_node_t* node) /*!< in: hash chain node */
  78. {
  79. return(node->next);
  80. }
  81. /******************************************************************//**
  82. Gets the first node in a hash chain.
  83. @return first node, NULL if none */
  84. UNIV_INLINE
  85. ha_node_t*
  86. ha_chain_get_first(
  87. /*===============*/
  88. hash_table_t* table, /*!< in: hash table */
  89. ulint fold) /*!< in: fold value determining the chain */
  90. {
  91. return((ha_node_t*)
  92. hash_get_nth_cell(table, hash_calc_hash(fold, table))->node);
  93. }
  94. /*************************************************************//**
  95. Looks for an element in a hash table.
  96. @return pointer to the data of the first hash table node in chain
  97. having the fold number, NULL if not found */
  98. UNIV_INLINE
  99. const rec_t*
  100. ha_search_and_get_data(
  101. /*===================*/
  102. hash_table_t* table, /*!< in: hash table */
  103. ulint fold) /*!< in: folded value of the searched data */
  104. {
  105. ha_node_t* node;
  106. ASSERT_HASH_MUTEX_OWN(table, fold);
  107. #ifdef UNIV_SYNC_DEBUG
  108. // ut_ad(rw_lock_own(&btr_search_latch, RW_LOCK_SHARED));
  109. #endif /* UNIV_SYNC_DEBUG */
  110. ut_ad(btr_search_enabled);
  111. node = ha_chain_get_first(table, fold);
  112. while (node) {
  113. if (node->fold == fold) {
  114. return(node->data);
  115. }
  116. node = ha_chain_get_next(node);
  117. }
  118. return(NULL);
  119. }
  120. /*********************************************************//**
  121. Looks for an element when we know the pointer to the data.
  122. @return pointer to the hash table node, NULL if not found in the table */
  123. UNIV_INLINE
  124. ha_node_t*
  125. ha_search_with_data(
  126. /*================*/
  127. hash_table_t* table, /*!< in: hash table */
  128. ulint fold, /*!< in: folded value of the searched data */
  129. const rec_t* data) /*!< in: pointer to the data */
  130. {
  131. ha_node_t* node;
  132. ASSERT_HASH_MUTEX_OWN(table, fold);
  133. ut_ad(btr_search_enabled);
  134. node = ha_chain_get_first(table, fold);
  135. while (node) {
  136. if (node->data == data) {
  137. return(node);
  138. }
  139. node = ha_chain_get_next(node);
  140. }
  141. return(NULL);
  142. }
  143. /*********************************************************//**
  144. Looks for an element when we know the pointer to the data, and deletes
  145. it from the hash table, if found.
  146. @return TRUE if found */
  147. UNIV_INLINE
  148. ibool
  149. ha_search_and_delete_if_found(
  150. /*==========================*/
  151. hash_table_t* table, /*!< in: hash table */
  152. ulint fold, /*!< in: folded value of the searched data */
  153. const rec_t* data) /*!< in: pointer to the data */
  154. {
  155. ha_node_t* node;
  156. ASSERT_HASH_MUTEX_OWN(table, fold);
  157. #ifdef UNIV_SYNC_DEBUG
  158. // ut_ad(rw_lock_own(&btr_search_latch, RW_LOCK_EX));
  159. #endif /* UNIV_SYNC_DEBUG */
  160. ut_ad(btr_search_enabled);
  161. node = ha_search_with_data(table, fold, data);
  162. if (node) {
  163. ha_delete_hash_node(table, node);
  164. return(TRUE);
  165. }
  166. return(FALSE);
  167. }