~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to sql/ha_heap.cc

Merge with 4.0.3
Some simple optimzations, more comments and indentation changes.
Add ` around database in 'use database' in binary log.
Moved max_error_count and max_warning_count to variables struct.
Removed SHOW_WARNS_COUNT and SHOW_ERRORS_COUNT calls.
Changed string functions to use character set of first string argument as default return characterset
(Each string function can change the above assumption if needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
int ha_heap::open(const char *name, int mode, uint test_if_locked)
35
35
{
36
 
  uint key,parts,mem_per_row=0;
37
 
  ulong max_rows;
38
 
  HP_KEYDEF *keydef;
39
 
  HP_KEYSEG *seg;
40
 
  THD *thd= current_thd;
41
 
 
42
 
  for (key=parts=0 ; key < table->keys ; key++)
43
 
    parts+=table->key_info[key].key_parts;
44
 
 
45
 
  if (!(keydef=(HP_KEYDEF*) my_malloc(table->keys*sizeof(HP_KEYDEF)+
46
 
                                      parts*sizeof(HP_KEYSEG),MYF(MY_WME))))
47
 
    return my_errno;
48
 
  seg=my_reinterpret_cast(HP_KEYSEG*) (keydef+table->keys);
49
 
  for (key=0 ; key < table->keys ; key++)
 
36
  if (!(file= heap_open(name, mode)) && my_errno == ENOENT)
50
37
  {
51
 
    KEY *pos=table->key_info+key;
52
 
    KEY_PART_INFO *key_part=     pos->key_part;
53
 
    KEY_PART_INFO *key_part_end= key_part+pos->key_parts;
54
 
 
55
 
    mem_per_row += (pos->key_length + (sizeof(char*) * 2));
56
 
 
57
 
    keydef[key].keysegs=(uint) pos->key_parts;
58
 
    keydef[key].flag = (pos->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL));
59
 
    keydef[key].seg=seg;
60
 
 
61
 
    for (; key_part != key_part_end ; key_part++, seg++)
62
 
    {
63
 
      uint flag=key_part->key_type;
64
 
      Field *field=key_part->field;
65
 
      if (!f_is_packed(flag) &&
66
 
          f_packtype(flag) == (int) FIELD_TYPE_DECIMAL &&
67
 
          !(flag & FIELDFLAG_BINARY))
68
 
        seg->type= (int) HA_KEYTYPE_TEXT;
69
 
      else
70
 
        seg->type= (int) HA_KEYTYPE_BINARY;
71
 
      seg->start=(uint) key_part->offset;
72
 
      seg->length=(uint) key_part->length;
73
 
      if (field->null_ptr)
74
 
      {
75
 
        seg->null_bit=field->null_bit;
76
 
        seg->null_pos= (uint) (field->null_ptr-
77
 
                               (uchar*) table->record[0]);
78
 
      }
79
 
      else
80
 
      {
81
 
        seg->null_bit=0;
82
 
        seg->null_pos=0;
83
 
      }
84
 
    }
 
38
    if (!create(name, table, NULL))
 
39
      file= heap_open(name, mode);
85
40
  }
86
 
  mem_per_row += MY_ALIGN(table->reclength+1, sizeof(char*));
87
 
  max_rows = (ulong) (thd->variables.max_heap_table_size / mem_per_row);
88
 
  file=heap_open(name,mode,
89
 
                 table->keys,keydef,
90
 
                 table->reclength,
91
 
                 ((table->max_rows < max_rows && table->max_rows) ? 
92
 
                  table->max_rows : max_rows),
93
 
                 table->min_rows);
94
 
  my_free((gptr) keydef,MYF(0));
95
 
  if (file)
96
 
    info(HA_STATUS_NO_LOCK | HA_STATUS_CONST | HA_STATUS_VARIABLE);
97
 
  ref_length=sizeof(HEAP_PTR);
98
 
  return (!file ? errno : 0);
 
41
  return (file ? 0 : 1);
99
42
}
100
43
 
101
44
int ha_heap::close(void)
125
68
  return heap_delete(file,buf);
126
69
}
127
70
 
128
 
int ha_heap::index_read(byte * buf, const byte * key,
129
 
                        uint key_len __attribute__((unused)),
130
 
                        enum ha_rkey_function find_flag
131
 
                        __attribute__((unused)))
 
71
int ha_heap::index_read(byte * buf, const byte * key, uint key_len,
 
72
                        enum ha_rkey_function find_flag)
132
73
{
133
 
  statistic_increment(ha_read_key_count,&LOCK_status);
134
 
  int error=heap_rkey(file,buf,active_index, key);
135
 
  table->status=error ? STATUS_NOT_FOUND: 0;
 
74
  statistic_increment(ha_read_key_count, &LOCK_status);
 
75
  int error = heap_rkey(file,buf,active_index, key, key_len, find_flag);
 
76
  table->status = error ? STATUS_NOT_FOUND : 0;
136
77
  return error;
137
78
}
138
79
 
139
80
int ha_heap::index_read_idx(byte * buf, uint index, const byte * key,
140
 
                            uint key_len __attribute__((unused)),
141
 
                            enum ha_rkey_function find_flag
142
 
                            __attribute__((unused)))
 
81
                            uint key_len, enum ha_rkey_function find_flag)
143
82
{
144
 
  statistic_increment(ha_read_key_count,&LOCK_status);
145
 
  int error=heap_rkey(file, buf, index, key);
146
 
  table->status=error ? STATUS_NOT_FOUND: 0;
 
83
  statistic_increment(ha_read_key_count, &LOCK_status);
 
84
  int error = heap_rkey(file, buf, index, key, key_len, find_flag);
 
85
  table->status = error ? STATUS_NOT_FOUND : 0;
147
86
  return error;
148
87
}
149
88
 
167
106
int ha_heap::index_first(byte * buf)
168
107
{
169
108
  statistic_increment(ha_read_first_count,&LOCK_status);
170
 
  int error=heap_rfirst(file, buf);
 
109
  int error=heap_rfirst(file, buf, active_index);
171
110
  table->status=error ? STATUS_NOT_FOUND: 0;
172
111
  return error;
173
112
}
175
114
int ha_heap::index_last(byte * buf)
176
115
{
177
116
  statistic_increment(ha_read_last_count,&LOCK_status);
178
 
  int error=heap_rlast(file, buf);
 
117
  int error=heap_rlast(file, buf, active_index);
179
118
  table->status=error ? STATUS_NOT_FOUND: 0;
180
119
  return error;
181
120
}
255
194
  return to;
256
195
}
257
196
 
258
 
 
259
197
/*
260
198
  We have to ignore ENOENT entries as the HEAP table is created on open and
261
199
  not when doing a CREATE on the table.
272
210
  return heap_rename(from,to);
273
211
}
274
212
 
275
 
 
276
213
ha_rows ha_heap::records_in_range(int inx,
277
214
                                  const byte *start_key,uint start_key_len,
278
215
                                  enum ha_rkey_function start_search_flag,
280
217
                                  enum ha_rkey_function end_search_flag)
281
218
{
282
219
  KEY *pos=table->key_info+inx;
283
 
  if (start_key_len != end_key_len ||
284
 
      start_key_len != pos->key_length ||
285
 
      start_search_flag != HA_READ_KEY_EXACT ||
286
 
      end_search_flag != HA_READ_AFTER_KEY)
287
 
    return HA_POS_ERROR;                        // Can't only use exact keys
288
 
  return 10;                                    // Good guess
 
220
  if (pos->algorithm == HA_KEY_ALG_BTREE)
 
221
  {
 
222
    return hp_rb_records_in_range(file, inx, start_key, start_key_len,
 
223
                                  start_search_flag, end_key, end_key_len,
 
224
                                  end_search_flag);
 
225
  }
 
226
  else
 
227
  {
 
228
    if (start_key_len != end_key_len ||
 
229
        start_key_len != pos->key_length ||
 
230
        start_search_flag != HA_READ_KEY_EXACT ||
 
231
        end_search_flag != HA_READ_AFTER_KEY)
 
232
      return HA_POS_ERROR;                      // Can't only use exact keys
 
233
    return 10;                                  // Good guess
 
234
  }
289
235
}
290
236
 
291
237
 
292
 
int ha_heap::create(const char *name, TABLE *form, HA_CREATE_INFO *create_info)
293
 
 
 
238
int ha_heap::create(const char *name, TABLE *table,
 
239
                    HA_CREATE_INFO *create_info)
294
240
{
 
241
  uint key, parts, mem_per_row= 0;
 
242
  ulong max_rows;
 
243
  HP_KEYDEF *keydef;
 
244
  HA_KEYSEG *seg;
295
245
  char buff[FN_REFLEN];
296
 
  return heap_create(fn_format(buff,name,"","",4+2));
 
246
  int error;
 
247
 
 
248
  for (key= parts= 0; key < table->keys; key++)
 
249
  {
 
250
    parts+= table->key_info[key].key_parts;
 
251
    if (table->key_info[key].algorithm == HA_KEY_ALG_BTREE)
 
252
      parts++; /* additional HA_KEYTYPE_END keyseg */
 
253
  }
 
254
 
 
255
  if (!(keydef= (HP_KEYDEF*) my_malloc(table->keys * sizeof(HP_KEYDEF) +
 
256
                                       parts * sizeof(HA_KEYSEG),
 
257
                                       MYF(MY_WME))))
 
258
    return my_errno;
 
259
  seg= my_reinterpret_cast(HA_KEYSEG*) (keydef + table->keys);
 
260
  for (key= 0; key < table->keys; key++)
 
261
  {
 
262
    KEY *pos= table->key_info+key;
 
263
    KEY_PART_INFO *key_part=     pos->key_part;
 
264
    KEY_PART_INFO *key_part_end= key_part + pos->key_parts;
 
265
 
 
266
    mem_per_row+= (pos->key_length + (sizeof(char*) * 2));
 
267
 
 
268
    keydef[key].keysegs=   (uint) pos->key_parts;
 
269
    keydef[key].flag=      (pos->flags & (HA_NOSAME | HA_NULL_ARE_EQUAL));
 
270
    keydef[key].seg=       seg;
 
271
    keydef[key].algorithm= ((pos->algorithm == HA_KEY_ALG_UNDEF) ? 
 
272
                            HA_KEY_ALG_HASH : pos->algorithm);
 
273
 
 
274
    for (; key_part != key_part_end; key_part++, seg++)
 
275
    {
 
276
      uint flag=    key_part->key_type;
 
277
      Field *field= key_part->field;
 
278
      if (pos->algorithm == HA_KEY_ALG_BTREE)
 
279
        seg->type= field->key_type();
 
280
      else
 
281
      {
 
282
        if (!f_is_packed(flag) &&
 
283
            f_packtype(flag) == (int) FIELD_TYPE_DECIMAL &&
 
284
            !(flag & FIELDFLAG_BINARY))
 
285
          seg->type= (int) HA_KEYTYPE_TEXT;
 
286
        else
 
287
          seg->type= (int) HA_KEYTYPE_BINARY;
 
288
      }
 
289
      seg->start=   (uint) key_part->offset;
 
290
      seg->length=  (uint) key_part->length;
 
291
      seg->flag =   0;
 
292
      seg->charset= field->binary() ? NULL : ((Field_str*)field)->charset();
 
293
      if (field->null_ptr)
 
294
      {
 
295
        seg->null_bit= field->null_bit;
 
296
        seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
 
297
      }
 
298
      else
 
299
      {
 
300
        seg->null_bit= 0;
 
301
        seg->null_pos= 0;
 
302
      }
 
303
    }
 
304
    if (pos->algorithm == HA_KEY_ALG_BTREE)
 
305
    {
 
306
      /* additional HA_KEYTYPE_END keyseg */
 
307
      seg->type=     HA_KEYTYPE_END;
 
308
      seg->length=   sizeof(byte*);
 
309
      seg->flag=     0;
 
310
      seg->null_bit= 0;
 
311
      seg++;
 
312
    }
 
313
  }
 
314
  mem_per_row+= MY_ALIGN(table->reclength + 1, sizeof(char*));
 
315
  max_rows = (ulong) (current_thd->variables.max_heap_table_size /
 
316
                      mem_per_row);
 
317
  error= heap_create(fn_format(buff,name,"","",4+2),
 
318
                     table->keys,keydef, table->reclength,
 
319
                     ((table->max_rows < max_rows && table->max_rows) ? 
 
320
                     table->max_rows : max_rows),
 
321
                     table->min_rows);
 
322
  my_free((gptr) keydef, MYF(0));
 
323
  if (file)
 
324
    info(HA_STATUS_NO_LOCK | HA_STATUS_CONST | HA_STATUS_VARIABLE);
 
325
  ref_length= sizeof(HEAP_PTR);
 
326
  return (error);
297
327
}