1/* Copyright (c) 2000, 2011, Oracle and/or its affiliates.
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
17/* This file should be included when using heap_database_functions */
18/* Author: Michael Widenius */
19
20#ifndef _heap_h
21#define _heap_h
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#ifndef _my_base_h
27#include <my_base.h>
28#endif
29
30#include <my_pthread.h>
31#include <thr_lock.h>
32
33#include "my_compare.h"
34#include "my_tree.h"
35
36 /* defines used by heap-funktions */
37
38#define HP_MAX_LEVELS 4 /* 128^5 records is enough */
39#define HP_PTRS_IN_NOD 128
40
41 /* struct used with heap_funktions */
42
43typedef struct st_heapinfo /* Struct from heap_info */
44{
45 ulong records; /* Records in database */
46 ulong deleted; /* Deleted records in database */
47 ulong max_records;
48 ulonglong data_length;
49 ulonglong index_length;
50 uint reclength; /* Length of one record */
51 int errkey;
52 ulonglong auto_increment;
53 time_t create_time;
54} HEAPINFO;
55
56
57 /* Structs used by heap-database-handler */
58
59typedef struct st_heap_ptrs
60{
61 uchar *blocks[HP_PTRS_IN_NOD]; /* pointers to HP_PTRS or records */
62} HP_PTRS;
63
64struct st_level_info
65{
66 /* Number of unused slots in *last_blocks HP_PTRS block (0 for 0th level) */
67 uint free_ptrs_in_block;
68
69 /*
70 Maximum number of records that can be 'contained' inside of each element
71 of last_blocks array. For level 0 - 1, for level 1 - HP_PTRS_IN_NOD, for
72 level 2 - HP_PTRS_IN_NOD^2 and so forth.
73 */
74 ulong records_under_level;
75
76 /*
77 Ptr to last allocated HP_PTRS (or records buffer for level 0) on this
78 level.
79 */
80 HP_PTRS *last_blocks;
81};
82
83
84/*
85 Heap table records and hash index entries are stored in HP_BLOCKs.
86 HP_BLOCK is used as a 'growable array' of fixed-size records. Size of record
87 is recbuffer bytes.
88 The internal representation is as follows:
89 HP_BLOCK is a hierarchical structure of 'blocks'.
90 A block at level 0 is an array records_in_block records.
91 A block at higher level is an HP_PTRS structure with pointers to blocks at
92 lower levels.
93 At the highest level there is one top block. It is stored in HP_BLOCK::root.
94
95 See hp_find_block for a description of how record pointer is obtained from
96 its index.
97 See hp_get_new_block
98*/
99
100typedef struct st_heap_block
101{
102 HP_PTRS *root; /* Top-level block */
103 struct st_level_info level_info[HP_MAX_LEVELS+1];
104 uint levels; /* number of used levels */
105 uint recbuffer; /* Length of one saved record */
106 ulong records_in_block; /* Records in one heap-block */
107 ulong last_allocated; /* number of records there is allocated space for */
108} HP_BLOCK;
109
110struct st_heap_info; /* For referense */
111
112typedef struct st_hp_keydef /* Key definition with open */
113{
114 uint flag; /* HA_NOSAME | HA_NULL_PART_KEY */
115 uint keysegs; /* Number of key-segment */
116 uint length; /* Length of key (automatic) */
117 uint8 algorithm; /* HASH / BTREE */
118 HA_KEYSEG *seg;
119 HP_BLOCK block; /* Where keys are saved */
120 /*
121 Number of buckets used in hash table. Used only to provide
122 #records estimates for heap key scans.
123 */
124 ha_rows hash_buckets;
125 TREE rb_tree;
126 int (*write_key)(struct st_heap_info *info, struct st_hp_keydef *keyinfo,
127 const uchar *record, uchar *recpos);
128 int (*delete_key)(struct st_heap_info *info, struct st_hp_keydef *keyinfo,
129 const uchar *record, uchar *recpos, int flag);
130 uint (*get_key_length)(struct st_hp_keydef *keydef, const uchar *key);
131} HP_KEYDEF;
132
133typedef struct st_heap_share
134{
135 HP_BLOCK block;
136 HP_KEYDEF *keydef;
137 ulonglong data_length,index_length,max_table_size;
138 ulonglong auto_increment;
139 ulong min_records,max_records; /* Params to open */
140 ulong records; /* records */
141 ulong blength; /* records rounded up to 2^n */
142 ulong deleted; /* Deleted records in database */
143 uint key_stat_version; /* version to indicate insert/delete */
144 uint key_version; /* Updated on key change */
145 uint file_version; /* Update on clear */
146 uint reclength; /* Length of one record */
147 uint visible; /* Offset to the visible/deleted mark */
148 uint changed;
149 uint keys,max_key_length;
150 uint currently_disabled_keys; /* saved value from "keys" when disabled */
151 uint open_count;
152 uchar *del_link; /* Link to next block with del. rec */
153 char * name; /* Name of "memory-file" */
154 time_t create_time;
155 THR_LOCK lock;
156 mysql_mutex_t intern_lock; /* Locking for use with _locking */
157 my_bool delete_on_close;
158 my_bool internal; /* Internal temporary table */
159 LIST open_list;
160 uint auto_key;
161 uint auto_key_type; /* real type of the auto key segment */
162} HP_SHARE;
163
164struct st_hp_hash_info;
165
166typedef struct st_heap_info
167{
168 HP_SHARE *s;
169 uchar *current_ptr;
170 struct st_hp_hash_info *current_hash_ptr;
171 ulong current_record,next_block;
172 int lastinx,errkey;
173 int mode; /* Mode of file (READONLY..) */
174 uint opt_flag,update;
175 uchar *lastkey; /* Last used key with rkey */
176 uchar *recbuf; /* Record buffer for rb-tree keys */
177 enum ha_rkey_function last_find_flag;
178 TREE_ELEMENT *parents[MAX_TREE_HEIGHT+1];
179 TREE_ELEMENT **last_pos;
180 uint key_version; /* Version at last read */
181 uint file_version; /* Version at scan */
182 uint lastkey_len;
183 my_bool implicit_emptied;
184 THR_LOCK_DATA lock;
185 LIST open_list;
186} HP_INFO;
187
188
189typedef struct st_heap_create_info
190{
191 HP_KEYDEF *keydef;
192 uint auto_key; /* keynr [1 - maxkey] for auto key */
193 uint auto_key_type;
194 uint keys;
195 uint reclength;
196 ulong max_records;
197 ulong min_records;
198 ulonglong max_table_size;
199 ulonglong auto_increment;
200 my_bool with_auto_increment;
201 my_bool internal_table;
202 /*
203 TRUE if heap_create should 'pin' the created share by setting
204 open_count to 1. Is only looked at if not internal_table.
205 */
206 my_bool pin_share;
207} HP_CREATE_INFO;
208
209 /* Prototypes for heap-functions */
210
211extern HP_INFO *heap_open(const char *name, int mode);
212extern HP_INFO *heap_open_from_share(HP_SHARE *share, int mode);
213extern HP_INFO *heap_open_from_share_and_register(HP_SHARE *share, int mode);
214extern void heap_release_share(HP_SHARE *share, my_bool internal_table);
215extern int heap_close(HP_INFO *info);
216extern int heap_write(HP_INFO *info,const uchar *buff);
217extern int heap_update(HP_INFO *info,const uchar *old,const uchar *newdata);
218extern int heap_rrnd(HP_INFO *info,uchar *buf,uchar *pos);
219extern int heap_scan_init(HP_INFO *info);
220extern int heap_scan(HP_INFO *info, uchar *record);
221extern int heap_delete(HP_INFO *info,const uchar *buff);
222extern int heap_info(HP_INFO *info,HEAPINFO *x,int flag);
223extern int heap_create(const char *name,
224 HP_CREATE_INFO *create_info, HP_SHARE **share,
225 my_bool *created_new_share);
226extern int heap_delete_table(const char *name);
227extern void heap_drop_table(HP_INFO *info);
228extern int heap_extra(HP_INFO *info,enum ha_extra_function function);
229extern int heap_reset(HP_INFO *info);
230extern int heap_rename(const char *old_name,const char *new_name);
231extern int heap_panic(enum ha_panic_function flag);
232extern int heap_rsame(HP_INFO *info,uchar *record,int inx);
233extern int heap_rnext(HP_INFO *info,uchar *record);
234extern int heap_rprev(HP_INFO *info,uchar *record);
235extern int heap_rfirst(HP_INFO *info,uchar *record,int inx);
236extern int heap_rlast(HP_INFO *info,uchar *record,int inx);
237extern void heap_clear(HP_INFO *info);
238extern void heap_clear_keys(HP_INFO *info);
239extern int heap_disable_indexes(HP_INFO *info);
240extern int heap_enable_indexes(HP_INFO *info);
241extern int heap_indexes_are_disabled(HP_INFO *info);
242extern void heap_update_auto_increment(HP_INFO *info, const uchar *record);
243ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, key_range *min_key,
244 key_range *max_key);
245int hp_panic(enum ha_panic_function flag);
246int heap_rkey(HP_INFO *info, uchar *record, int inx, const uchar *key,
247 key_part_map keypart_map, enum ha_rkey_function find_flag);
248extern uchar * heap_find(HP_INFO *info,int inx,const uchar *key);
249extern int heap_check_heap(HP_INFO *info, my_bool print_status);
250extern uchar *heap_position(HP_INFO *info);
251
252/* The following is for programs that uses the old HEAP interface where
253 pointer to rows where a long instead of a (uchar*).
254*/
255
256#if defined(WANT_OLD_HEAP_VERSION) || defined(OLD_HEAP_VERSION)
257extern int heap_rrnd_old(HP_INFO *info,uchar *buf,ulong pos);
258extern ulong heap_position_old(HP_INFO *info);
259#endif
260#ifdef OLD_HEAP_VERSION
261typedef ulong HEAP_PTR;
262#define heap_position(A) heap_position_old(A)
263#define heap_rrnd(A,B,C) heap_rrnd_old(A,B,C)
264#else
265typedef uchar *HEAP_PTR;
266#endif
267
268#ifdef __cplusplus
269}
270#endif
271#endif
272