1 /* Copyright (c) 2000, 2013, 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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 /* Dynamic hashing of record with different key-length */
24 
25 #ifndef _hash_h
26 #define _hash_h
27 
28 #include "my_global.h"                          /* uchar */
29 #include "my_sys.h"                             /* DYNAMIC_ARRAY */
30 
31 /*
32   This forward declaration is used from C files where the real
33   definition is included before.  Since C does not allow repeated
34   typedef declarations, even when identical, the definition may not be
35   repeated.
36 */
37 #ifndef CHARSET_INFO_DEFINED
38 #define CHARSET_INFO_DEFINED
39 typedef struct charset_info_st CHARSET_INFO;
40 #endif  /* CHARSET_INFO_DEFINED */
41 
42 #ifdef	__cplusplus
43 extern "C" {
44 #endif
45 
46 /*
47   Overhead to store an element in hash
48   Can be used to approximate memory consumption for a hash
49  */
50 #define HASH_OVERHEAD (sizeof(char*)*2)
51 
52 /* flags for hash_init */
53 #define HASH_UNIQUE     1       /* hash_insert fails on duplicate key */
54 
55 struct st_hash;
56 typedef uint my_hash_value_type;
57 typedef uchar *(*my_hash_get_key)(const uchar *,size_t*,my_bool);
58 typedef void (*my_hash_free_key)(void *);
59 /**
60   Function type representing a hash function to be used with the HASH
61   container.
62   Should accept pointer to HASH, pointer to key buffer and key length
63   as parameters.
64 */
65 typedef my_hash_value_type (*my_hash_function)(const struct st_hash *,
66                                                const uchar *,
67                                                size_t);
68 
69 typedef struct st_hash {
70   size_t key_offset,key_length;		/* Length of key if const length */
71   size_t blength;
72   ulong records;
73   uint flags;
74   DYNAMIC_ARRAY array;				/* Place for hash_keys */
75   my_hash_get_key get_key;
76   void (*free)(void *);
77   CHARSET_INFO *charset;
78   my_hash_function hash_function;
79 } HASH;
80 
81 /* A search iterator state */
82 typedef uint HASH_SEARCH_STATE;
83 
84 #define my_hash_init(A,B,C,D,E,F,G,H) \
85           _my_hash_init(A,0,B,NULL,C,D,E,F,G,H)
86 #define my_hash_init2(A,B,C,D,E,F,G,H,I) \
87           _my_hash_init(A,B,C,NULL,D,E,F,G,H,I)
88 #define my_hash_init3(A,B,C,D,E,F,G,H,I,J) \
89           _my_hash_init(A,B,C,D,E,F,G,H,I,J)
90 my_bool _my_hash_init(HASH *hash, uint growth_size, CHARSET_INFO *charset,
91                       my_hash_function hash_function,
92                       ulong default_array_elements, size_t key_offset,
93                       size_t key_length, my_hash_get_key get_key,
94                       void (*free_element)(void*),
95                       uint flags);
96 void my_hash_free(HASH *tree);
97 void my_hash_reset(HASH *hash);
98 uchar *my_hash_element(HASH *hash, ulong idx);
99 const uchar *my_hash_const_element(const HASH *hash, ulong idx);
100 uchar *my_hash_search(const HASH *info, const uchar *key, size_t length);
101 uchar *my_hash_search_using_hash_value(const HASH *info,
102                                        my_hash_value_type hash_value,
103                                        const uchar *key, size_t length);
104 my_hash_value_type my_calc_hash(const HASH *info,
105                                 const uchar *key, size_t length);
106 uchar *my_hash_first(const HASH *info, const uchar *key, size_t length,
107                      HASH_SEARCH_STATE *state);
108 uchar *my_hash_first_from_hash_value(const HASH *info,
109                                      my_hash_value_type hash_value,
110                                      const uchar *key,
111                                      size_t length,
112                                      HASH_SEARCH_STATE *state);
113 uchar *my_hash_next(const HASH *info, const uchar *key, size_t length,
114                     HASH_SEARCH_STATE *state);
115 my_bool my_hash_insert(HASH *info, const uchar *data);
116 my_bool my_hash_delete(HASH *hash, uchar *record);
117 my_bool my_hash_update(HASH *hash, uchar *record, uchar *old_key,
118                        size_t old_key_length);
119 void my_hash_replace(HASH *hash, HASH_SEARCH_STATE *state, uchar *new_row);
120 my_bool my_hash_check(HASH *hash); /* Only in debug library */
121 
122 #define my_hash_clear(H) memset((H), 0, sizeof(*(H)))
123 #define my_hash_inited(H) ((H)->blength != 0)
124 #define my_hash_init_opt(A,B,C,D,E,F,G,H) \
125           (!my_hash_inited(A) && _my_hash_init(A,0,B,NULL,C,D,E,F,G,H))
126 
127 #ifdef	__cplusplus
128 }
129 #endif
130 #endif
131