1
/* Copyright (C) 2000,2004 MySQL AB
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* This file should be included when using heap_database_functions */
17
/* Author: Michael Widenius */
29
#include <my_pthread.h>
33
#include "my_handler.h"
36
/* defines used by heap-funktions */
38
#define HP_MAX_LEVELS 4 /* 128^5 records is enough */
39
#define HP_PTRS_IN_NOD 128
41
/* struct used with heap_funktions */
43
typedef struct st_heapinfo /* Struct from heap_info */
45
ulong records; /* Records in database */
46
ulong deleted; /* Deleted records in database */
48
ulonglong data_length;
49
ulonglong index_length;
50
uint reclength; /* Length of one record */
52
ulonglong auto_increment;
56
/* Structs used by heap-database-handler */
58
typedef struct st_heap_ptrs
60
uchar *blocks[HP_PTRS_IN_NOD]; /* pointers to HP_PTRS or records */
65
/* Number of unused slots in *last_blocks HP_PTRS block (0 for 0th level) */
66
uint free_ptrs_in_block;
69
Maximum number of records that can be 'contained' inside of each element
70
of last_blocks array. For level 0 - 1, for level 1 - HP_PTRS_IN_NOD, for
71
level 2 - HP_PTRS_IN_NOD^2 and so forth.
73
ulong records_under_level;
76
Ptr to last allocated HP_PTRS (or records buffer for level 0) on this
84
Heap table records and hash index entries are stored in HP_BLOCKs.
85
HP_BLOCK is used as a 'growable array' of fixed-size records. Size of record
87
The internal representation is as follows:
88
HP_BLOCK is a hierarchical structure of 'blocks'.
89
A block at level 0 is an array records_in_block records.
90
A block at higher level is an HP_PTRS structure with pointers to blocks at
92
At the highest level there is one top block. It is stored in HP_BLOCK::root.
94
See hp_find_block for a description of how record pointer is obtained from
99
typedef struct st_heap_block
101
HP_PTRS *root; /* Top-level block */
102
struct st_level_info level_info[HP_MAX_LEVELS+1];
103
uint levels; /* number of used levels */
104
uint records_in_block; /* Records in one heap-block */
105
uint recbuffer; /* Length of one saved record */
106
ulong last_allocated; /* number of records there is allocated space for */
109
struct st_heap_info; /* For referense */
111
typedef struct st_hp_keydef /* Key definition with open */
113
uint flag; /* HA_NOSAME | HA_NULL_PART_KEY */
114
uint keysegs; /* Number of key-segment */
115
uint length; /* Length of key (automatic) */
116
uint8 algorithm; /* HASH / BTREE */
118
HP_BLOCK block; /* Where keys are saved */
120
Number of buckets used in hash table. Used only to provide
121
#records estimates for heap key scans.
123
ha_rows hash_buckets;
125
int (*write_key)(struct st_heap_info *info, struct st_hp_keydef *keyinfo,
126
const uchar *record, uchar *recpos);
127
int (*delete_key)(struct st_heap_info *info, struct st_hp_keydef *keyinfo,
128
const uchar *record, uchar *recpos, int flag);
129
uint (*get_key_length)(struct st_hp_keydef *keydef, const uchar *key);
132
typedef struct st_heap_share
136
ulong min_records,max_records; /* Params to open */
137
ulonglong data_length,index_length,max_table_size;
138
uint key_stat_version; /* version to indicate insert/delete */
139
uint records; /* records */
140
uint blength; /* records rounded up to 2^n */
141
uint deleted; /* Deleted records in database */
142
uint reclength; /* Length of one record */
144
uint keys,max_key_length;
145
uint currently_disabled_keys; /* saved value from "keys" when disabled */
147
uchar *del_link; /* Link to next block with del. rec */
148
char * name; /* Name of "memory-file" */
151
pthread_mutex_t intern_lock; /* Locking for use with _locking */
153
my_bool delete_on_close;
156
uint auto_key_type; /* real type of the auto key segment */
157
ulonglong auto_increment;
160
struct st_hp_hash_info;
162
typedef struct st_heap_info
166
struct st_hp_hash_info *current_hash_ptr;
167
ulong current_record,next_block;
169
int mode; /* Mode of file (READONLY..) */
170
uint opt_flag,update;
171
uchar *lastkey; /* Last used key with rkey */
172
uchar *recbuf; /* Record buffer for rb-tree keys */
173
enum ha_rkey_function last_find_flag;
174
TREE_ELEMENT *parents[MAX_TREE_HEIGHT+1];
175
TREE_ELEMENT **last_pos;
177
my_bool implicit_emptied;
185
typedef struct st_heap_create_info
187
uint auto_key; /* keynr [1 - maxkey] for auto key */
189
ulonglong max_table_size;
190
ulonglong auto_increment;
191
my_bool with_auto_increment;
192
my_bool internal_table;
195
/* Prototypes for heap-functions */
197
extern HP_INFO *heap_open(const char *name, int mode);
198
extern HP_INFO *heap_open_from_share(HP_SHARE *share, int mode);
199
extern HP_INFO *heap_open_from_share_and_register(HP_SHARE *share, int mode);
200
extern int heap_close(HP_INFO *info);
201
extern int heap_write(HP_INFO *info,const uchar *buff);
202
extern int heap_update(HP_INFO *info,const uchar *old,const uchar *newdata);
203
extern int heap_rrnd(HP_INFO *info,uchar *buf,uchar *pos);
204
extern int heap_scan_init(HP_INFO *info);
205
extern int heap_scan(register HP_INFO *info, uchar *record);
206
extern int heap_delete(HP_INFO *info,const uchar *buff);
207
extern int heap_info(HP_INFO *info,HEAPINFO *x,int flag);
208
extern int heap_create(const char *name, uint keys, HP_KEYDEF *keydef,
209
uint reclength, ulong max_records, ulong min_records,
210
HP_CREATE_INFO *create_info, HP_SHARE **share);
211
extern int heap_delete_table(const char *name);
212
extern void heap_drop_table(HP_INFO *info);
213
extern int heap_extra(HP_INFO *info,enum ha_extra_function function);
214
extern int heap_reset(HP_INFO *info);
215
extern int heap_rename(const char *old_name,const char *new_name);
216
extern int heap_panic(enum ha_panic_function flag);
217
extern int heap_rsame(HP_INFO *info,uchar *record,int inx);
218
extern int heap_rnext(HP_INFO *info,uchar *record);
219
extern int heap_rprev(HP_INFO *info,uchar *record);
220
extern int heap_rfirst(HP_INFO *info,uchar *record,int inx);
221
extern int heap_rlast(HP_INFO *info,uchar *record,int inx);
222
extern void heap_clear(HP_INFO *info);
223
extern void heap_clear_keys(HP_INFO *info);
224
extern int heap_disable_indexes(HP_INFO *info);
225
extern int heap_enable_indexes(HP_INFO *info);
226
extern int heap_indexes_are_disabled(HP_INFO *info);
227
extern void heap_update_auto_increment(HP_INFO *info, const uchar *record);
228
ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, key_range *min_key,
230
int hp_panic(enum ha_panic_function flag);
231
int heap_rkey(HP_INFO *info, uchar *record, int inx, const uchar *key,
232
key_part_map keypart_map, enum ha_rkey_function find_flag);
233
extern uchar * heap_find(HP_INFO *info,int inx,const uchar *key);
234
extern int heap_check_heap(HP_INFO *info, my_bool print_status);
235
extern uchar *heap_position(HP_INFO *info);
237
/* The following is for programs that uses the old HEAP interface where
238
pointer to rows where a long instead of a (uchar*).
241
#if defined(WANT_OLD_HEAP_VERSION) || defined(OLD_HEAP_VERSION)
242
extern int heap_rrnd_old(HP_INFO *info,uchar *buf,ulong pos);
243
extern ulong heap_position_old(HP_INFO *info);
245
#ifdef OLD_HEAP_VERSION
246
typedef ulong HEAP_PTR;
247
#define heap_position(A) heap_position_old(A)
248
#define heap_rrnd(A,B,C) heap_rrnd_old(A,B,C)
250
typedef uchar *HEAP_PTR;