1 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. 2 3 This program is free software; you can redistribute it and/or modify 4 it under the terms of the GNU General Public License as published by 5 the Free Software Foundation; version 2 of the License. 6 7 This program is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 GNU General Public License for more details. 11 12 You should have received a copy of the GNU General Public License 13 along with this program; if not, write to the Free Software 14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 15 16 /* Dynamic hashing of record with different key-length */ 17 18 #ifndef _hash_h 19 #define _hash_h 20 21 #include "my_global.h" /* uchar */ 22 #include "my_sys.h" /* DYNAMIC_ARRAY */ 23 24 /* 25 This forward declaration is used from C files where the real 26 definition is included before. Since C does not allow repeated 27 typedef declarations, even when identical, the definition may not be 28 repeated. 29 */ 30 #ifndef CHARSET_INFO_DEFINED 31 #define CHARSET_INFO_DEFINED 32 typedef struct charset_info_st CHARSET_INFO; 33 #endif /* CHARSET_INFO_DEFINED */ 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 /* 40 Overhead to store an element in hash 41 Can be used to approximate memory consumption for a hash 42 */ 43 #define HASH_OVERHEAD (sizeof(char*)*2) 44 45 /* flags for hash_init */ 46 #define HASH_UNIQUE 1 /* hash_insert fails on duplicate key */ 47 48 typedef uint my_hash_value_type; 49 typedef uchar *(*my_hash_get_key)(const uchar *,size_t*,my_bool); 50 typedef void (*my_hash_free_key)(void *); 51 52 typedef struct st_hash { 53 size_t key_offset,key_length; /* Length of key if const length */ 54 size_t blength; 55 ulong records; 56 uint flags; 57 DYNAMIC_ARRAY array; /* Place for hash_keys */ 58 my_hash_get_key get_key; 59 void (*free)(void *); 60 CHARSET_INFO *charset; 61 } HASH; 62 63 /* A search iterator state */ 64 typedef uint HASH_SEARCH_STATE; 65 66 #define my_hash_init(A,B,C,D,E,F,G,H) \ 67 _my_hash_init(A,0,B,C,D,E,F,G,H) 68 #define my_hash_init2(A,B,C,D,E,F,G,H,I) \ 69 _my_hash_init(A,B,C,D,E,F,G,H,I) 70 my_bool _my_hash_init(HASH *hash, uint growth_size, CHARSET_INFO *charset, 71 ulong default_array_elements, size_t key_offset, 72 size_t key_length, my_hash_get_key get_key, 73 void (*free_element)(void*), 74 uint flags); 75 void my_hash_free(HASH *tree); 76 void my_hash_reset(HASH *hash); 77 uchar *my_hash_element(HASH *hash, ulong idx); 78 uchar *my_hash_search(const HASH *info, const uchar *key, size_t length); 79 uchar *my_hash_search_using_hash_value(const HASH *info, 80 my_hash_value_type hash_value, 81 const uchar *key, size_t length); 82 my_hash_value_type my_calc_hash(const HASH *info, 83 const uchar *key, size_t length); 84 uchar *my_hash_first(const HASH *info, const uchar *key, size_t length, 85 HASH_SEARCH_STATE *state); 86 uchar *my_hash_first_from_hash_value(const HASH *info, 87 my_hash_value_type hash_value, 88 const uchar *key, 89 size_t length, 90 HASH_SEARCH_STATE *state); 91 uchar *my_hash_next(const HASH *info, const uchar *key, size_t length, 92 HASH_SEARCH_STATE *state); 93 my_bool my_hash_insert(HASH *info, const uchar *data); 94 my_bool my_hash_delete(HASH *hash, uchar *record); 95 my_bool my_hash_update(HASH *hash, uchar *record, uchar *old_key, 96 size_t old_key_length); 97 void my_hash_replace(HASH *hash, HASH_SEARCH_STATE *state, uchar *new_row); 98 my_bool my_hash_check(HASH *hash); /* Only in debug library */ 99 100 #define my_hash_clear(H) bzero((char*) (H), sizeof(*(H))) 101 #define my_hash_inited(H) ((H)->blength != 0) 102 #define my_hash_init_opt(A,B,C,D,E,F,G,H) \ 103 (!my_hash_inited(A) && _my_hash_init(A,0,B,C,D,E,F,G,H)) 104 105 #ifdef __cplusplus 106 } 107 #endif 108 #endif 109