~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
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.
6
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.
11
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 */
15
16
#include "heapdef.h"
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);
21
22
/* Create a heap table */
23
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)
27
{
28
  uint i, j, key_segs, max_length, length;
29
  HP_SHARE *share= 0;
30
  HA_KEYSEG *keyseg;
31
  DBUG_ENTER("heap_create");
32
33
  if (!create_info->internal_table)
34
  {
35
    pthread_mutex_lock(&THR_LOCK_heap);
36
    if ((share= hp_find_named_heap(name)) && share->open_count == 0)
37
    {
38
      hp_free(share);
39
      share= 0;
40
    }
41
  }  
42
43
  if (!share)
44
  {
45
    HP_KEYDEF *keyinfo;
46
    DBUG_PRINT("info",("Initializing new table"));
47
    
48
    /*
49
      We have to store sometimes uchar* del_link in records,
50
      so the record length should be at least sizeof(uchar*)
51
    */
52
    set_if_bigger(reclength, sizeof (uchar*));
53
    
54
    for (i= key_segs= max_length= 0, keyinfo= keydef; i < keys; i++, keyinfo++)
55
    {
56
      bzero((char*) &keyinfo->block,sizeof(keyinfo->block));
57
      bzero((char*) &keyinfo->rb_tree ,sizeof(keyinfo->rb_tree));
58
      for (j= length= 0; j < keyinfo->keysegs; j++)
59
      {
60
	length+= keyinfo->seg[j].length;
61
	if (keyinfo->seg[j].null_bit)
62
	{
63
	  length++;
64
	  if (!(keyinfo->flag & HA_NULL_ARE_EQUAL))
65
	    keyinfo->flag|= HA_NULL_PART_KEY;
66
	  if (keyinfo->algorithm == HA_KEY_ALG_BTREE)
67
	    keyinfo->rb_tree.size_of_element++;
68
	}
69
	switch (keyinfo->seg[j].type) {
70
	case HA_KEYTYPE_SHORT_INT:
71
	case HA_KEYTYPE_LONG_INT:
72
	case HA_KEYTYPE_FLOAT:
73
	case HA_KEYTYPE_DOUBLE:
74
	case HA_KEYTYPE_USHORT_INT:
75
	case HA_KEYTYPE_ULONG_INT:
76
	case HA_KEYTYPE_LONGLONG:
77
	case HA_KEYTYPE_ULONGLONG:
78
	case HA_KEYTYPE_INT24:
79
	case HA_KEYTYPE_UINT24:
80
	case HA_KEYTYPE_INT8:
81
	  keyinfo->seg[j].flag|= HA_SWAP_KEY;
82
          break;
83
        case HA_KEYTYPE_VARBINARY1:
84
          /* Case-insensitiveness is handled in coll->hash_sort */
85
          keyinfo->seg[j].type= HA_KEYTYPE_VARTEXT1;
86
          /* fall_through */
87
        case HA_KEYTYPE_VARTEXT1:
88
          if (!my_binary_compare(keyinfo->seg[j].charset))
89
            keyinfo->flag|= HA_END_SPACE_KEY;
90
          keyinfo->flag|= HA_VAR_LENGTH_KEY;
91
          length+= 2;
92
          /* Save number of bytes used to store length */
93
          keyinfo->seg[j].bit_start= 1;
94
          break;
95
        case HA_KEYTYPE_VARBINARY2:
96
          /* Case-insensitiveness is handled in coll->hash_sort */
97
          /* fall_through */
98
        case HA_KEYTYPE_VARTEXT2:
99
          if (!my_binary_compare(keyinfo->seg[j].charset))
100
            keyinfo->flag|= HA_END_SPACE_KEY;
101
          keyinfo->flag|= HA_VAR_LENGTH_KEY;
102
          length+= 2;
103
          /* Save number of bytes used to store length */
104
          keyinfo->seg[j].bit_start= 2;
105
          /*
106
            Make future comparison simpler by only having to check for
107
            one type
108
          */
109
          keyinfo->seg[j].type= HA_KEYTYPE_VARTEXT1;
110
          break;
111
	default:
112
	  break;
113
	}
114
        if (keyinfo->seg[j].flag & HA_END_SPACE_ARE_EQUAL)
115
          keyinfo->flag|= HA_END_SPACE_KEY;
116
      }
117
      keyinfo->length= length;
118
      length+= keyinfo->rb_tree.size_of_element + 
119
	       ((keyinfo->algorithm == HA_KEY_ALG_BTREE) ? sizeof(uchar*) : 0);
120
      if (length > max_length)
121
	max_length= length;
122
      key_segs+= keyinfo->keysegs;
123
      if (keyinfo->algorithm == HA_KEY_ALG_BTREE)
124
      {
125
        key_segs++; /* additional HA_KEYTYPE_END segment */
126
        if (keyinfo->flag & HA_VAR_LENGTH_KEY)
127
          keyinfo->get_key_length= hp_rb_var_key_length;
128
        else if (keyinfo->flag & HA_NULL_PART_KEY)
129
          keyinfo->get_key_length= hp_rb_null_key_length;
130
        else
131
          keyinfo->get_key_length= hp_rb_key_length;
132
      }
133
    }
134
    if (!(share= (HP_SHARE*) my_malloc((uint) sizeof(HP_SHARE)+
135
				       keys*sizeof(HP_KEYDEF)+
136
				       key_segs*sizeof(HA_KEYSEG),
137
				       MYF(MY_ZEROFILL))))
138
      goto err;
139
    share->keydef= (HP_KEYDEF*) (share + 1);
140
    share->key_stat_version= 1;
141
    keyseg= (HA_KEYSEG*) (share->keydef + keys);
142
    init_block(&share->block, reclength + 1, min_records, max_records);
143
	/* Fix keys */
144
    memcpy(share->keydef, keydef, (size_t) (sizeof(keydef[0]) * keys));
145
    for (i= 0, keyinfo= share->keydef; i < keys; i++, keyinfo++)
146
    {
147
      keyinfo->seg= keyseg;
148
      memcpy(keyseg, keydef[i].seg,
149
	     (size_t) (sizeof(keyseg[0]) * keydef[i].keysegs));
150
      keyseg+= keydef[i].keysegs;
151
152
      if (keydef[i].algorithm == HA_KEY_ALG_BTREE)
153
      {
154
	/* additional HA_KEYTYPE_END keyseg */
155
	keyseg->type=     HA_KEYTYPE_END;
156
	keyseg->length=   sizeof(uchar*);
157
	keyseg->flag=     0;
158
	keyseg->null_bit= 0;
159
	keyseg++;
160
161
	init_tree(&keyinfo->rb_tree, 0, 0, sizeof(uchar*),
162
		  (qsort_cmp2)keys_compare, 1, NULL, NULL);
163
	keyinfo->delete_key= hp_rb_delete_key;
164
	keyinfo->write_key= hp_rb_write_key;
165
      }
166
      else
167
      {
168
	init_block(&keyinfo->block, sizeof(HASH_INFO), min_records,
169
		   max_records);
170
	keyinfo->delete_key= hp_delete_key;
171
	keyinfo->write_key= hp_write_key;
172
        keyinfo->hash_buckets= 0;
173
      }
174
      if ((keyinfo->flag & HA_AUTO_KEY) && create_info->with_auto_increment)
175
        share->auto_key= i + 1;
176
    }
177
    share->min_records= min_records;
178
    share->max_records= max_records;
179
    share->max_table_size= create_info->max_table_size;
180
    share->data_length= share->index_length= 0;
181
    share->reclength= reclength;
182
    share->blength= 1;
183
    share->keys= keys;
184
    share->max_key_length= max_length;
185
    share->changed= 0;
186
    share->auto_key= create_info->auto_key;
187
    share->auto_key_type= create_info->auto_key_type;
188
    share->auto_increment= create_info->auto_increment;
189
    /* Must be allocated separately for rename to work */
190
    if (!(share->name= my_strdup(name,MYF(0))))
191
    {
192
      my_free((uchar*) share,MYF(0));
193
      goto err;
194
    }
195
#ifdef THREAD
196
    thr_lock_init(&share->lock);
197
    VOID(pthread_mutex_init(&share->intern_lock,MY_MUTEX_INIT_FAST));
198
#endif
199
    if (!create_info->internal_table)
200
    {
201
      share->open_list.data= (void*) share;
202
      heap_share_list= list_add(heap_share_list,&share->open_list);
203
    }
204
    else
205
      share->delete_on_close= 1;
206
  }
207
  if (!create_info->internal_table)
208
    pthread_mutex_unlock(&THR_LOCK_heap);
209
210
  *res= share;
211
  DBUG_RETURN(0);
212
213
err:
214
  if (!create_info->internal_table)
215
    pthread_mutex_unlock(&THR_LOCK_heap);
216
  DBUG_RETURN(1);
217
} /* heap_create */
218
219
220
static int keys_compare(heap_rb_param *param, uchar *key1, uchar *key2)
221
{
222
  uint not_used[2];
223
  return ha_key_cmp(param->keyseg, key1, key2, param->key_length, 
224
		    param->search_flag, not_used);
225
}
226
227
static void init_block(HP_BLOCK *block, uint reclength, ulong min_records,
228
		       ulong max_records)
229
{
230
  uint i,recbuffer,records_in_block;
231
232
  max_records= max(min_records,max_records);
233
  if (!max_records)
234
    max_records= 1000;			/* As good as quess as anything */
235
  recbuffer= (uint) (reclength + sizeof(uchar**) - 1) & ~(sizeof(uchar**) - 1);
236
  records_in_block= max_records / 10;
237
  if (records_in_block < 10 && max_records)
238
    records_in_block= 10;
239
  if (!records_in_block || records_in_block*recbuffer >
240
      (my_default_record_cache_size-sizeof(HP_PTRS)*HP_MAX_LEVELS))
241
    records_in_block= (my_default_record_cache_size - sizeof(HP_PTRS) *
242
		      HP_MAX_LEVELS) / recbuffer + 1;
243
  block->records_in_block= records_in_block;
244
  block->recbuffer= recbuffer;
245
  block->last_allocated= 0L;
246
247
  for (i= 0; i <= HP_MAX_LEVELS; i++)
248
    block->level_info[i].records_under_level=
249
      (!i ? 1 : i == 1 ? records_in_block :
250
       HP_PTRS_IN_NOD * block->level_info[i - 1].records_under_level);
251
}
252
253
254
static inline void heap_try_free(HP_SHARE *share)
255
{
256
  if (share->open_count == 0)
257
    hp_free(share);
258
  else
259
    share->delete_on_close= 1;
260
}
261
262
263
int heap_delete_table(const char *name)
264
{
265
  int result;
266
  register HP_SHARE *share;
267
  DBUG_ENTER("heap_delete_table");
268
269
  pthread_mutex_lock(&THR_LOCK_heap);
270
  if ((share= hp_find_named_heap(name)))
271
  {
272
    heap_try_free(share);
273
    result= 0;
274
  }
275
  else
276
  {
277
    result= my_errno=ENOENT;
278
  }
279
  pthread_mutex_unlock(&THR_LOCK_heap);
280
  DBUG_RETURN(result);
281
}
282
283
284
void heap_drop_table(HP_INFO *info)
285
{
286
  DBUG_ENTER("heap_drop_table");
287
  pthread_mutex_lock(&THR_LOCK_heap);
288
  heap_try_free(info->s);
289
  pthread_mutex_unlock(&THR_LOCK_heap);
290
  DBUG_VOID_RETURN;
291
}
292
293
294
void hp_free(HP_SHARE *share)
295
{
296
  if (share->open_list.data)                    /* If not internal table */
297
    heap_share_list= list_delete(heap_share_list, &share->open_list);
298
  hp_clear(share);			/* Remove blocks from memory */
299
#ifdef THREAD
300
  thr_lock_delete(&share->lock);
301
  VOID(pthread_mutex_destroy(&share->intern_lock));
302
#endif
303
  my_free((uchar*) share->name, MYF(0));
304
  my_free((uchar*) share, MYF(0));
305
  return;
306
}