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