13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "heap_priv.h"
18
18
#include <drizzled/common.h>
19
19
#include <drizzled/error.h>
21
21
#include <string.h>
25
using namespace drizzled;
27
23
static int keys_compare(heap_rb_param *param, unsigned char *key1, unsigned char *key2);
28
24
static void init_block(HP_BLOCK *block,uint32_t chunk_length, uint32_t min_records,
29
25
uint32_t max_records);
31
27
#define FIXED_REC_OVERHEAD (sizeof(unsigned char))
32
#define VARIABLE_REC_OVERHEAD (sizeof(unsigned char**) + ALIGN_SIZE(sizeof(unsigned char)))
28
#define VARIABLE_REC_OVERHEAD (sizeof(unsigned char**) + sizeof(unsigned char))
34
30
/* Minimum size that a chunk can take, 12 bytes on 32bit, 24 bytes on 64bit */
35
31
#define VARIABLE_MIN_CHUNK_SIZE \
65
size_t chunk_dataspace_length;
66
uint32_t chunk_length, is_variable_size;
61
uint32_t chunk_dataspace_length, chunk_length, is_variable_size;
67
62
uint32_t fixed_data_length, fixed_column_count;
68
63
HP_KEYDEF *keyinfo;
198
193
keyinfo->rb_tree.size_of_element++;
200
195
switch (keyinfo->seg[j].type) {
196
case HA_KEYTYPE_SHORT_INT:
201
197
case HA_KEYTYPE_LONG_INT:
198
case HA_KEYTYPE_FLOAT:
202
199
case HA_KEYTYPE_DOUBLE:
200
case HA_KEYTYPE_USHORT_INT:
203
201
case HA_KEYTYPE_ULONG_INT:
204
202
case HA_KEYTYPE_LONGLONG:
205
203
case HA_KEYTYPE_ULONGLONG:
204
case HA_KEYTYPE_INT24:
206
205
case HA_KEYTYPE_UINT24:
206
case HA_KEYTYPE_INT8:
207
207
keyinfo->seg[j].flag|= HA_SWAP_KEY;
209
209
case HA_KEYTYPE_VARBINARY1:
251
251
keyinfo->get_key_length= hp_rb_key_length;
255
if (!(share= (HP_SHARE*) malloc(sizeof(HP_SHARE))))
258
memset(share, 0, sizeof(HP_SHARE));
260
if (keys && !(share->keydef= (HP_KEYDEF*) malloc(keys*sizeof(HP_KEYDEF))))
263
memset(share->keydef, 0, keys*sizeof(HP_KEYDEF));
265
if (keys && !(share->keydef->seg= (HA_KEYSEG*) malloc(key_segs*sizeof(HA_KEYSEG))))
267
if (!(share->column_defs= (HP_COLUMNDEF*)
268
malloc(columns*sizeof(HP_COLUMNDEF))))
271
memset(share->column_defs, 0, columns*sizeof(HP_COLUMNDEF));
254
if (!(share= (HP_SHARE*) malloc(sizeof(HP_SHARE)+
255
keys*sizeof(HP_KEYDEF)+
256
columns*sizeof(HP_COLUMNDEF)+
257
key_segs*sizeof(HA_KEYSEG))))
259
memset(share, 0, sizeof(HP_SHARE)+
260
keys*sizeof(HP_KEYDEF)+
261
columns*sizeof(HP_COLUMNDEF)+
262
key_segs*sizeof(HA_KEYSEG));
274
265
Max_records is used for estimating block sizes and for enforcement.
275
266
Calculate the very maximum number of rows (if everything was one chunk) and
276
267
then take either that value or configured max_records (pick smallest one)
278
max_rows_for_stated_memory= (uint32_t)(create_info->max_table_size /
269
max_rows_for_stated_memory= (ha_rows) (create_info->max_table_size /
279
270
(keys_memory_size + chunk_length));
280
271
max_records = ((max_records && max_records < max_rows_for_stated_memory) ?
281
272
max_records : max_rows_for_stated_memory);
274
share->column_defs= (HP_COLUMNDEF*) (share + 1);
283
275
memcpy(share->column_defs, columndef, (size_t) (sizeof(columndef[0]) * columns));
277
share->keydef= (HP_KEYDEF*) (share->column_defs + columns);
285
278
share->key_stat_version= 1;
286
keyseg= keys ? share->keydef->seg : NULL;
279
keyseg= (HA_KEYSEG*) (share->keydef + keys);
288
280
init_block(&share->recordspace.block, chunk_length, min_records, max_records);
290
282
memcpy(share->keydef, keydef, (size_t) (sizeof(keydef[0]) * keys));
307
299
init_tree(&keyinfo->rb_tree, 0, 0, sizeof(unsigned char*),
308
(qsort_cmp2)keys_compare, true, NULL, NULL);
300
(qsort_cmp2)keys_compare, 1, NULL, NULL);
309
301
keyinfo->delete_key= hp_rb_delete_key;
310
302
keyinfo->write_key= hp_rb_write_key;
352
344
/* Must be allocated separately for rename to work */
353
345
if (!(share->name= strdup(name)))
347
free((unsigned char*) share);
357
350
thr_lock_init(&share->lock);
358
351
pthread_mutex_init(&share->intern_lock,MY_MUTEX_INIT_FAST);
359
352
if (!create_info->internal_table)
361
heap_share_list.push_front(share);
354
share->open_list.data= (void*) share;
355
heap_share_list= list_add(heap_share_list,&share->open_list);
364
358
share->delete_on_close= 1;
373
if(share && share->keydef && share->keydef->seg)
374
free(share->keydef->seg);
375
if(share && share->keydef)
377
if(share && share->column_defs)
378
free(share->column_defs);
381
367
if (!create_info->internal_table)
382
368
pthread_mutex_unlock(&THR_LOCK_heap);
397
383
uint32_t i,recbuffer,records_in_block;
399
max_records= max(min_records,max_records);
385
max_records= cmax(min_records,max_records);
400
386
if (!max_records)
401
387
max_records= 1000; /* As good as quess as anything */
406
392
if (records_in_block < 10 && max_records)
407
393
records_in_block= 10;
408
394
if (!records_in_block || records_in_block*recbuffer >
409
(internal::my_default_record_cache_size-sizeof(HP_PTRS)*HP_MAX_LEVELS))
410
records_in_block= (internal::my_default_record_cache_size - sizeof(HP_PTRS) *
395
(my_default_record_cache_size-sizeof(HP_PTRS)*HP_MAX_LEVELS))
396
records_in_block= (my_default_record_cache_size - sizeof(HP_PTRS) *
411
397
HP_MAX_LEVELS) / recbuffer + 1;
412
398
block->records_in_block= records_in_block;
413
399
block->recbuffer= recbuffer;
461
447
void hp_free(HP_SHARE *share)
463
heap_share_list.remove(share); /* If not internal table */
449
if (share->open_list.data) /* If not internal table */
450
heap_share_list= list_delete(heap_share_list, &share->open_list);
464
451
hp_clear(share); /* Remove blocks from memory */
465
452
thr_lock_delete(&share->lock);
466
453
pthread_mutex_destroy(&share->intern_lock);
467
if (share->keydef && share->keydef->seg)
468
free(share->keydef->seg);
471
free(share->column_defs);
472
454
free((unsigned char*) share->name);
473
455
free((unsigned char*) share);