1/* Copyright (c) 2000, 2010, Oracle and/or its affiliates.
2 Copyright (c) 2011, 2013, Monty Program Ab.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
17/* Dynamic hashing of record with different key-length */
18
19#ifndef _hash_h
20#define _hash_h
21
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#ifdef __cplusplus
31extern "C" {
32#endif
33
34/*
35 Overhead to store an element in hash
36 Can be used to approximate memory consumption for a hash
37 */
38#define HASH_OVERHEAD (sizeof(char*)*2)
39
40/* flags for hash_init */
41#define HASH_UNIQUE 1 /* hash_insert fails on duplicate key */
42#define HASH_THREAD_SPECIFIC 2 /* Mark allocated memory THREAD_SPECIFIC */
43
44typedef uint32 my_hash_value_type;
45typedef uchar *(*my_hash_get_key)(const uchar *,size_t*,my_bool);
46typedef my_hash_value_type (*my_hash_function)(CHARSET_INFO *,
47 const uchar *, size_t);
48typedef void (*my_hash_free_key)(void *);
49typedef my_bool (*my_hash_walk_action)(void *,void *);
50
51typedef struct st_hash {
52 size_t key_offset,key_length; /* Length of key if const length */
53 size_t blength;
54 ulong records;
55 uint flags;
56 DYNAMIC_ARRAY array; /* Place for hash_keys */
57 my_hash_get_key get_key;
58 my_hash_function hash_function;
59 void (*free)(void *);
60 CHARSET_INFO *charset;
61} HASH;
62
63/* A search iterator state */
64typedef uint HASH_SEARCH_STATE;
65
66#define my_hash_init(A,B,C,D,E,F,G,H) my_hash_init2(A,0,B,C,D,E,F,0,G,H)
67my_bool my_hash_init2(HASH *hash, uint growth_size, CHARSET_INFO *charset,
68 ulong default_array_elements, size_t key_offset,
69 size_t key_length, my_hash_get_key get_key,
70 my_hash_function hash_function,
71 void (*free_element)(void*),
72 uint flags);
73void my_hash_free(HASH *tree);
74void my_hash_reset(HASH *hash);
75uchar *my_hash_element(HASH *hash, size_t idx);
76uchar *my_hash_search(const HASH *info, const uchar *key, size_t length);
77uchar *my_hash_search_using_hash_value(const HASH *info,
78 my_hash_value_type hash_value,
79 const uchar *key, size_t length);
80my_hash_value_type my_hash_sort(CHARSET_INFO *cs,
81 const uchar *key, size_t length);
82#define my_calc_hash(A, B, C) my_hash_sort((A)->charset, B, C)
83uchar *my_hash_first(const HASH *info, const uchar *key, size_t length,
84 HASH_SEARCH_STATE *state);
85uchar *my_hash_first_from_hash_value(const HASH *info,
86 my_hash_value_type hash_value,
87 const uchar *key,
88 size_t length,
89 HASH_SEARCH_STATE *state);
90uchar *my_hash_next(const HASH *info, const uchar *key, size_t length,
91 HASH_SEARCH_STATE *state);
92my_bool my_hash_insert(HASH *info, const uchar *data);
93my_bool my_hash_delete(HASH *hash, uchar *record);
94my_bool my_hash_update(HASH *hash, uchar *record, uchar *old_key,
95 size_t old_key_length);
96void my_hash_replace(HASH *hash, HASH_SEARCH_STATE *state, uchar *new_row);
97my_bool my_hash_check(HASH *hash); /* Only in debug library */
98my_bool my_hash_iterate(HASH *hash, my_hash_walk_action action, void *argument);
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,B,C,D,E,F,G,H))
104
105#ifdef __cplusplus
106}
107#endif
108#endif
109