~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/heap/hp_create.c

  • Committer: Brian Aker
  • Date: 2008-07-07 14:25:25 UTC
  • mto: (77.1.25 codestyle)
  • mto: This revision was merged to the branch mainline in revision 82.
  • Revision ID: brian@tangent.org-20080707142525-xzy2nl3ie2ebwfln
LL() cleanup

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
 
#include "libdrizzle/drizzle_com.h"
18
 
#include "drizzled/error.h"
19
 
 
20
 
static int keys_compare(heap_rb_param *param, unsigned char *key1, unsigned char *key2);
21
 
static void init_block(HP_BLOCK *block,uint32_t chunk_length, uint32_t min_records,
22
 
                        uint32_t max_records);
23
 
 
24
 
#define FIXED_REC_OVERHEAD (sizeof(unsigned char))
25
 
#define VARIABLE_REC_OVERHEAD (sizeof(unsigned char**) + sizeof(unsigned char))
26
 
 
27
 
/* Minimum size that a chunk can take, 12 bytes on 32bit, 24 bytes on 64bit */
28
 
#define VARIABLE_MIN_CHUNK_SIZE \
29
 
        ((sizeof(unsigned char**) + VARIABLE_REC_OVERHEAD + sizeof(unsigned char**) - 1) & ~(sizeof(unsigned char**) - 1))
30
 
 
 
17
 
 
18
static int keys_compare(heap_rb_param *param, uchar *key1, uchar *key2);
 
19
static void init_block(HP_BLOCK *block,uint reclength,ulong min_records,
 
20
                       ulong max_records);
31
21
 
32
22
/* Create a heap table */
33
23
 
34
 
int heap_create(const char *name, uint32_t keys, HP_KEYDEF *keydef,
35
 
    uint32_t columns, HP_COLUMNDEF *columndef,
36
 
    uint32_t max_key_fieldnr, uint32_t key_part_size,
37
 
    uint32_t reclength, uint32_t keys_memory_size,
38
 
    uint32_t max_records, uint32_t min_records,
39
 
    HP_CREATE_INFO *create_info, HP_SHARE **res)
 
24
int heap_create(const char *name, uint keys, HP_KEYDEF *keydef,
 
25
                uint reclength, ulong max_records, ulong min_records,
 
26
                HP_CREATE_INFO *create_info, HP_SHARE **res)
40
27
{
41
 
  uint32_t i, j, key_segs, max_length, length;
42
 
  uint32_t max_rows_for_stated_memory;
 
28
  uint i, j, key_segs, max_length, length;
43
29
  HP_SHARE *share= 0;
44
30
  HA_KEYSEG *keyseg;
 
31
  DBUG_ENTER("heap_create");
45
32
 
46
33
  if (!create_info->internal_table)
47
34
  {
55
42
 
56
43
  if (!share)
57
44
  {
58
 
    uint32_t chunk_dataspace_length, chunk_length, is_variable_size;
59
 
    uint32_t fixed_data_length, fixed_column_count;
60
45
    HP_KEYDEF *keyinfo;
61
 
 
62
 
    if (create_info->max_chunk_size)
63
 
    {
64
 
      uint32_t configured_chunk_size= create_info->max_chunk_size;
65
 
 
66
 
      /* User requested variable-size records, let's see if they're possible */
67
 
 
68
 
      if (configured_chunk_size < key_part_size)
69
 
      {
70
 
        /* Eventual chunk_size cannot be smaller than key data,
71
 
           which allows all keys to fit into the first chunk */
72
 
        my_error(ER_CANT_USE_OPTION_HERE, MYF(0), "block_size");
73
 
        pthread_mutex_unlock(&THR_LOCK_heap);
74
 
        return(ER_CANT_USE_OPTION_HERE);
75
 
      }
76
 
 
77
 
      if ((reclength - configured_chunk_size) >= VARIABLE_MIN_CHUNK_SIZE<<1)
78
 
      {
79
 
        /* Allow variable size only if we're saving at least two smallest chunks */
80
 
        /* There has to be at least one field after indexed fields */
81
 
        /* Note that NULL bits are already included in key_part_size */
82
 
        is_variable_size= 1;
83
 
        chunk_dataspace_length= configured_chunk_size;
84
 
      }
85
 
      else
86
 
      {
87
 
        /* max_chunk_size is near the full reclength, let's use fixed size */
88
 
        is_variable_size= 0;
89
 
        chunk_dataspace_length= reclength;
90
 
      }
91
 
    }
92
 
    else if (create_info->is_dynamic)
93
 
    {
94
 
      /* User asked for dynamic records - use 256 as the chunk size */
95
 
      if ((key_part_size + VARIABLE_REC_OVERHEAD) > 256)
96
 
        chunk_dataspace_length= key_part_size;
97
 
      else
98
 
        chunk_dataspace_length= 256 - VARIABLE_REC_OVERHEAD;
99
 
 
100
 
      is_variable_size= 1;
101
 
    }
102
 
    else
103
 
    {
104
 
      /* if max_chunk_size is not specified, put the whole record in one chunk */
105
 
      is_variable_size= 0;
106
 
      chunk_dataspace_length= reclength;
107
 
    }
108
 
 
109
 
    if (is_variable_size)
110
 
    {
111
 
      /* Check whether we have any variable size records past key data */
112
 
      uint32_t has_variable_fields= 0;
113
 
 
114
 
      fixed_data_length= key_part_size;
115
 
      fixed_column_count= max_key_fieldnr;
116
 
 
117
 
      for (i= max_key_fieldnr; i < columns; i++)
118
 
      {
119
 
        HP_COLUMNDEF* column= columndef + i;
120
 
        if (column->type == DRIZZLE_TYPE_VARCHAR && column->length >= 32)
121
 
        {
122
 
            /* The field has to be >= 5.0.3 true VARCHAR and have substantial length */
123
 
            /* TODO: do we want to calculate minimum length? */
124
 
            has_variable_fields= 1;
125
 
            break;
126
 
        }
127
 
 
128
 
        if (has_variable_fields)
129
 
        {
130
 
          break;
131
 
        }
132
 
 
133
 
        if ((column->offset + column->length) <= chunk_dataspace_length)
134
 
        {
135
 
          /* Still no variable-size columns, add one fixed-length */
136
 
          fixed_column_count= i + 1;
137
 
          fixed_data_length= column->offset + column->length;
138
 
        }
139
 
      }
140
 
 
141
 
      if (!has_variable_fields)
142
 
      {
143
 
        /* There is no need to use variable-size records without variable-size columns */
144
 
        /* Reset sizes if it's not variable size anymore */
145
 
        is_variable_size= 0;
146
 
        chunk_dataspace_length= reclength;
147
 
        fixed_data_length= reclength;
148
 
        fixed_column_count= columns;
149
 
      }
150
 
    }
151
 
    else
152
 
    {
153
 
      fixed_data_length= reclength;
154
 
      fixed_column_count= columns;
155
 
    }
156
 
 
 
46
    DBUG_PRINT("info",("Initializing new table"));
 
47
    
157
48
    /*
158
 
      We store unsigned char* del_link inside the data area of deleted records,
159
 
      so the data length should be at least sizeof(unsigned char*)
 
49
      We have to store sometimes uchar* del_link in records,
 
50
      so the record length should be at least sizeof(uchar*)
160
51
    */
161
 
    set_if_bigger(chunk_dataspace_length, sizeof (unsigned char**));
162
 
 
163
 
    if (is_variable_size)
164
 
    {
165
 
      chunk_length= chunk_dataspace_length + VARIABLE_REC_OVERHEAD;
166
 
    }
167
 
    else
168
 
    {
169
 
      chunk_length= chunk_dataspace_length + FIXED_REC_OVERHEAD;
170
 
    }
171
 
 
172
 
    /* Align chunk length to the next pointer */
173
 
    chunk_length= (uint) (chunk_length + sizeof(unsigned char**) - 1) & ~(sizeof(unsigned char**) - 1);
174
 
 
175
 
 
 
52
    set_if_bigger(reclength, sizeof (uchar*));
176
53
    
177
54
    for (i= key_segs= max_length= 0, keyinfo= keydef; i < keys; i++, keyinfo++)
178
55
    {
179
 
      memset(&keyinfo->block, 0, sizeof(keyinfo->block));
180
 
      memset(&keyinfo->rb_tree , 0, sizeof(keyinfo->rb_tree));
 
56
      bzero((char*) &keyinfo->block,sizeof(keyinfo->block));
 
57
      bzero((char*) &keyinfo->rb_tree ,sizeof(keyinfo->rb_tree));
181
58
      for (j= length= 0; j < keyinfo->keysegs; j++)
182
59
      {
183
60
        length+= keyinfo->seg[j].length;
233
110
      }
234
111
      keyinfo->length= length;
235
112
      length+= keyinfo->rb_tree.size_of_element + 
236
 
               ((keyinfo->algorithm == HA_KEY_ALG_BTREE) ? sizeof(unsigned char*) : 0);
 
113
               ((keyinfo->algorithm == HA_KEY_ALG_BTREE) ? sizeof(uchar*) : 0);
237
114
      if (length > max_length)
238
115
        max_length= length;
239
116
      key_segs+= keyinfo->keysegs;
250
127
    }
251
128
    if (!(share= (HP_SHARE*) my_malloc((uint) sizeof(HP_SHARE)+
252
129
                                       keys*sizeof(HP_KEYDEF)+
253
 
                                       columns*sizeof(HP_COLUMNDEF)+
254
130
                                       key_segs*sizeof(HA_KEYSEG),
255
131
                                       MYF(MY_ZEROFILL))))
256
132
      goto err;
257
 
 
258
 
    /*
259
 
       Max_records is used for estimating block sizes and for enforcement.
260
 
       Calculate the very maximum number of rows (if everything was one chunk) and
261
 
       then take either that value or configured max_records (pick smallest one)
262
 
    */
263
 
    max_rows_for_stated_memory= (ha_rows) (create_info->max_table_size /
264
 
      (keys_memory_size + chunk_length));
265
 
    max_records = ((max_records && max_records < max_rows_for_stated_memory) ? 
266
 
                      max_records : max_rows_for_stated_memory);
267
 
 
268
 
    share->column_defs= (HP_COLUMNDEF*) (share + 1);
269
 
    memcpy(share->column_defs, columndef, (size_t) (sizeof(columndef[0]) * columns));
270
 
 
271
 
    share->keydef= (HP_KEYDEF*) (share->column_defs + columns);    
 
133
    share->keydef= (HP_KEYDEF*) (share + 1);
272
134
    share->key_stat_version= 1;
273
135
    keyseg= (HA_KEYSEG*) (share->keydef + keys);
274
 
    init_block(&share->recordspace.block, chunk_length, min_records, max_records);
275
 
    /* Fix keys */
 
136
    init_block(&share->block, reclength + 1, min_records, max_records);
 
137
        /* Fix keys */
276
138
    memcpy(share->keydef, keydef, (size_t) (sizeof(keydef[0]) * keys));
277
139
    for (i= 0, keyinfo= share->keydef; i < keys; i++, keyinfo++)
278
140
    {
285
147
      {
286
148
        /* additional HA_KEYTYPE_END keyseg */
287
149
        keyseg->type=     HA_KEYTYPE_END;
288
 
        keyseg->length=   sizeof(unsigned char*);
 
150
        keyseg->length=   sizeof(uchar*);
289
151
        keyseg->flag=     0;
290
152
        keyseg->null_bit= 0;
291
153
        keyseg++;
292
154
 
293
 
        init_tree(&keyinfo->rb_tree, 0, 0, sizeof(unsigned char*),
 
155
        init_tree(&keyinfo->rb_tree, 0, 0, sizeof(uchar*),
294
156
                  (qsort_cmp2)keys_compare, 1, NULL, NULL);
295
157
        keyinfo->delete_key= hp_rb_delete_key;
296
158
        keyinfo->write_key= hp_rb_write_key;
309
171
    share->min_records= min_records;
310
172
    share->max_records= max_records;
311
173
    share->max_table_size= create_info->max_table_size;
312
 
    share->index_length= 0;
 
174
    share->data_length= share->index_length= 0;
 
175
    share->reclength= reclength;
313
176
    share->blength= 1;
314
177
    share->keys= keys;
315
178
    share->max_key_length= max_length;
316
 
    share->column_count= columns;
317
179
    share->changed= 0;
318
180
    share->auto_key= create_info->auto_key;
319
181
    share->auto_key_type= create_info->auto_key_type;
320
182
    share->auto_increment= create_info->auto_increment;
321
 
 
322
 
    share->fixed_data_length= fixed_data_length;
323
 
    share->fixed_column_count= fixed_column_count;
324
 
 
325
 
    share->recordspace.chunk_length= chunk_length;
326
 
    share->recordspace.chunk_dataspace_length= chunk_dataspace_length;
327
 
    share->recordspace.is_variable_size= is_variable_size;
328
 
    share->recordspace.total_data_length= 0;
329
 
 
330
 
    if (is_variable_size) {
331
 
      share->recordspace.offset_link= chunk_dataspace_length;
332
 
      share->recordspace.offset_status= share->recordspace.offset_link + sizeof(unsigned char**);
333
 
    } else {
334
 
      share->recordspace.offset_link= 1<<22; /* Make it likely to fail if anyone uses this offset */
335
 
      share->recordspace.offset_status= chunk_dataspace_length;
336
 
    }
337
 
 
338
183
    /* Must be allocated separately for rename to work */
339
184
    if (!(share->name= my_strdup(name,MYF(0))))
340
185
    {
341
 
      free((unsigned char*) share);
 
186
      my_free((uchar*) share,MYF(0));
342
187
      goto err;
343
188
    }
344
189
    thr_lock_init(&share->lock);
345
 
    pthread_mutex_init(&share->intern_lock,MY_MUTEX_INIT_FAST);
 
190
    VOID(pthread_mutex_init(&share->intern_lock,MY_MUTEX_INIT_FAST));
346
191
    if (!create_info->internal_table)
347
192
    {
348
193
      share->open_list.data= (void*) share;
355
200
    pthread_mutex_unlock(&THR_LOCK_heap);
356
201
 
357
202
  *res= share;
358
 
  return(0);
 
203
  DBUG_RETURN(0);
359
204
 
360
205
err:
361
206
  if (!create_info->internal_table)
362
207
    pthread_mutex_unlock(&THR_LOCK_heap);
363
 
  return(1);
 
208
  DBUG_RETURN(1);
364
209
} /* heap_create */
365
210
 
366
211
 
367
 
static int keys_compare(heap_rb_param *param, unsigned char *key1, unsigned char *key2)
 
212
static int keys_compare(heap_rb_param *param, uchar *key1, uchar *key2)
368
213
{
369
 
  uint32_t not_used[2];
 
214
  uint not_used[2];
370
215
  return ha_key_cmp(param->keyseg, key1, key2, param->key_length, 
371
216
                    param->search_flag, not_used);
372
217
}
373
218
 
374
 
static void init_block(HP_BLOCK *block, uint32_t chunk_length, uint32_t min_records,
375
 
                       uint32_t max_records)
 
219
static void init_block(HP_BLOCK *block, uint reclength, ulong min_records,
 
220
                       ulong max_records)
376
221
{
377
 
  uint32_t i,recbuffer,records_in_block;
 
222
  uint i,recbuffer,records_in_block;
378
223
 
379
 
  max_records= cmax(min_records,max_records);
 
224
  max_records= max(min_records,max_records);
380
225
  if (!max_records)
381
226
    max_records= 1000;                  /* As good as quess as anything */
382
 
  
383
 
  /* we want to start each chunk at 8 bytes boundary, round recbuffer to the next 8 */
384
 
  recbuffer= (uint) (chunk_length + sizeof(unsigned char**) - 1) & ~(sizeof(unsigned char**) - 1);  
 
227
  recbuffer= (uint) (reclength + sizeof(uchar**) - 1) & ~(sizeof(uchar**) - 1);
385
228
  records_in_block= max_records / 10;
386
229
  if (records_in_block < 10 && max_records)
387
230
    records_in_block= 10;
413
256
{
414
257
  int result;
415
258
  register HP_SHARE *share;
 
259
  DBUG_ENTER("heap_delete_table");
416
260
 
417
261
  pthread_mutex_lock(&THR_LOCK_heap);
418
262
  if ((share= hp_find_named_heap(name)))
425
269
    result= my_errno=ENOENT;
426
270
  }
427
271
  pthread_mutex_unlock(&THR_LOCK_heap);
428
 
  return(result);
 
272
  DBUG_RETURN(result);
429
273
}
430
274
 
431
275
 
432
276
void heap_drop_table(HP_INFO *info)
433
277
{
 
278
  DBUG_ENTER("heap_drop_table");
434
279
  pthread_mutex_lock(&THR_LOCK_heap);
435
280
  heap_try_free(info->s);
436
281
  pthread_mutex_unlock(&THR_LOCK_heap);
437
 
  return;
 
282
  DBUG_VOID_RETURN;
438
283
}
439
284
 
440
285
 
444
289
    heap_share_list= list_delete(heap_share_list, &share->open_list);
445
290
  hp_clear(share);                      /* Remove blocks from memory */
446
291
  thr_lock_delete(&share->lock);
447
 
  pthread_mutex_destroy(&share->intern_lock);
448
 
  free((unsigned char*) share->name);
449
 
  free((unsigned char*) share);
 
292
  VOID(pthread_mutex_destroy(&share->intern_lock));
 
293
  my_free((uchar*) share->name, MYF(0));
 
294
  my_free((uchar*) share, MYF(0));
450
295
  return;
451
296
}