~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/heap/hp_create.c

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
#include "heapdef.h"
17
 
 
18
 
#include <drizzled/common.h>
19
 
#include <drizzled/error.h>
20
 
 
21
 
#include <string.h>
 
17
#include "libdrizzle/drizzle_com.h"
 
18
#include "drizzled/error.h"
22
19
 
23
20
static int keys_compare(heap_rb_param *param, unsigned char *key1, unsigned char *key2);
24
21
static void init_block(HP_BLOCK *block,uint32_t chunk_length, uint32_t min_records,
25
22
                        uint32_t max_records);
26
23
 
27
24
#define FIXED_REC_OVERHEAD (sizeof(unsigned char))
28
 
#define VARIABLE_REC_OVERHEAD (sizeof(unsigned char**) + ALIGN_SIZE(sizeof(unsigned char)))
29
 
 
 
25
#define VARIABLE_REC_OVERHEAD (sizeof(unsigned char**) + sizeof(unsigned char))
 
26
 
30
27
/* Minimum size that a chunk can take, 12 bytes on 32bit, 24 bytes on 64bit */
31
28
#define VARIABLE_MIN_CHUNK_SIZE \
32
29
        ((sizeof(unsigned char**) + VARIABLE_REC_OVERHEAD + sizeof(unsigned char**) - 1) & ~(sizeof(unsigned char**) - 1))
54
51
      hp_free(share);
55
52
      share= 0;
56
53
    }
57
 
  }
 
54
  }  
58
55
 
59
56
  if (!share)
60
57
  {
61
 
    size_t chunk_dataspace_length;
62
 
    uint32_t chunk_length, is_variable_size;
 
58
    uint32_t chunk_dataspace_length, chunk_length, is_variable_size;
63
59
    uint32_t fixed_data_length, fixed_column_count;
64
60
    HP_KEYDEF *keyinfo;
65
61
 
177
173
    chunk_length= (uint) (chunk_length + sizeof(unsigned char**) - 1) & ~(sizeof(unsigned char**) - 1);
178
174
 
179
175
 
180
 
 
 
176
    
181
177
    for (i= key_segs= max_length= 0, keyinfo= keydef; i < keys; i++, keyinfo++)
182
178
    {
183
179
      memset(&keyinfo->block, 0, sizeof(keyinfo->block));
236
232
        }
237
233
      }
238
234
      keyinfo->length= length;
239
 
      length+= keyinfo->rb_tree.size_of_element +
 
235
      length+= keyinfo->rb_tree.size_of_element + 
240
236
               ((keyinfo->algorithm == HA_KEY_ALG_BTREE) ? sizeof(unsigned char*) : 0);
241
237
      if (length > max_length)
242
238
        max_length= length;
252
248
          keyinfo->get_key_length= hp_rb_key_length;
253
249
      }
254
250
    }
255
 
    share= NULL;
256
 
    if (!(share= (HP_SHARE*) malloc(sizeof(HP_SHARE))))
257
 
      goto err;
258
 
 
259
 
    memset(share, 0, sizeof(HP_SHARE));
260
 
 
261
 
    if (keys && !(share->keydef= (HP_KEYDEF*) malloc(keys*sizeof(HP_KEYDEF))))
262
 
      goto err;
263
 
 
264
 
    memset(share->keydef, 0, keys*sizeof(HP_KEYDEF));
265
 
 
266
 
    if (keys && !(share->keydef->seg= (HA_KEYSEG*) malloc(key_segs*sizeof(HA_KEYSEG))))
267
 
      goto err;
268
 
    if (!(share->column_defs= (HP_COLUMNDEF*)
269
 
          malloc(columns*sizeof(HP_COLUMNDEF))))
270
 
      goto err;
271
 
 
272
 
    memset(share->column_defs, 0, columns*sizeof(HP_COLUMNDEF));
 
251
    if (!(share= (HP_SHARE*) my_malloc((uint) sizeof(HP_SHARE)+
 
252
                                       keys*sizeof(HP_KEYDEF)+
 
253
                                       columns*sizeof(HP_COLUMNDEF)+
 
254
                                       key_segs*sizeof(HA_KEYSEG),
 
255
                                       MYF(MY_ZEROFILL))))
 
256
      goto err;
273
257
 
274
258
    /*
275
259
       Max_records is used for estimating block sizes and for enforcement.
278
262
    */
279
263
    max_rows_for_stated_memory= (ha_rows) (create_info->max_table_size /
280
264
      (keys_memory_size + chunk_length));
281
 
    max_records = ((max_records && max_records < max_rows_for_stated_memory) ?
 
265
    max_records = ((max_records && max_records < max_rows_for_stated_memory) ? 
282
266
                      max_records : max_rows_for_stated_memory);
283
 
 
 
267
 
 
268
    share->column_defs= (HP_COLUMNDEF*) (share + 1);
284
269
    memcpy(share->column_defs, columndef, (size_t) (sizeof(columndef[0]) * columns));
285
270
 
 
271
    share->keydef= (HP_KEYDEF*) (share->column_defs + columns);    
286
272
    share->key_stat_version= 1;
287
 
    keyseg= keys ? share->keydef->seg : NULL;
288
 
 
 
273
    keyseg= (HA_KEYSEG*) (share->keydef + keys);
289
274
    init_block(&share->recordspace.block, chunk_length, min_records, max_records);
290
275
    /* Fix keys */
291
276
    memcpy(share->keydef, keydef, (size_t) (sizeof(keydef[0]) * keys));
351
336
    }
352
337
 
353
338
    /* Must be allocated separately for rename to work */
354
 
    if (!(share->name= strdup(name)))
 
339
    if (!(share->name= my_strdup(name,MYF(0))))
355
340
    {
 
341
      free((unsigned char*) share);
356
342
      goto err;
357
343
    }
358
344
    thr_lock_init(&share->lock);
372
358
  return(0);
373
359
 
374
360
err:
375
 
  if(share && share->keydef && share->keydef->seg)
376
 
    free(share->keydef->seg);
377
 
  if(share && share->keydef)
378
 
    free(share->keydef);
379
 
  if(share && share->column_defs)
380
 
    free(share->column_defs);
381
 
  if(share)
382
 
    free(share);
383
361
  if (!create_info->internal_table)
384
362
    pthread_mutex_unlock(&THR_LOCK_heap);
385
363
  return(1);
389
367
static int keys_compare(heap_rb_param *param, unsigned char *key1, unsigned char *key2)
390
368
{
391
369
  uint32_t not_used[2];
392
 
  return ha_key_cmp(param->keyseg, key1, key2, param->key_length,
 
370
  return ha_key_cmp(param->keyseg, key1, key2, param->key_length, 
393
371
                    param->search_flag, not_used);
394
372
}
395
373
 
401
379
  max_records= cmax(min_records,max_records);
402
380
  if (!max_records)
403
381
    max_records= 1000;                  /* As good as quess as anything */
404
 
 
 
382
  
405
383
  /* we want to start each chunk at 8 bytes boundary, round recbuffer to the next 8 */
406
 
  recbuffer= (uint) (chunk_length + sizeof(unsigned char**) - 1) & ~(sizeof(unsigned char**) - 1);
 
384
  recbuffer= (uint) (chunk_length + sizeof(unsigned char**) - 1) & ~(sizeof(unsigned char**) - 1);  
407
385
  records_in_block= max_records / 10;
408
386
  if (records_in_block < 10 && max_records)
409
387
    records_in_block= 10;
467
445
  hp_clear(share);                      /* Remove blocks from memory */
468
446
  thr_lock_delete(&share->lock);
469
447
  pthread_mutex_destroy(&share->intern_lock);
470
 
  if(share->keys)
471
 
    free(share->keydef->seg);
472
 
  if(share->keys)
473
 
    free(share->keydef);
474
 
  free(share->column_defs);
475
448
  free((unsigned char*) share->name);
476
449
  free((unsigned char*) share);
477
450
  return;