~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
17
/**
18
  @file
19
20
  @brief
21
  Functions for easy reading of records, possible through a cache
22
*/
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
23
#include <drizzled/server_includes.h>
1 by brian
clean slate
24
25
static int rr_quick(READ_RECORD *info);
26
int rr_sequential(READ_RECORD *info);
27
static int rr_from_tempfile(READ_RECORD *info);
28
static int rr_unpack_from_tempfile(READ_RECORD *info);
29
static int rr_unpack_from_buffer(READ_RECORD *info);
30
static int rr_from_pointers(READ_RECORD *info);
31
static int rr_from_cache(READ_RECORD *info);
32
static int init_rr_cache(THD *thd, READ_RECORD *info);
33
static int rr_cmp(uchar *a,uchar *b);
34
static int rr_index_first(READ_RECORD *info);
35
static int rr_index(READ_RECORD *info);
36
37
38
/**
39
  Initialize READ_RECORD structure to perform full index scan (in forward
40
  direction) using read_record.read_record() interface.
41
42
    This function has been added at late stage and is used only by
43
    UPDATE/DELETE. Other statements perform index scans using
44
    join_read_first/next functions.
45
46
  @param info         READ_RECORD structure to initialize.
47
  @param thd          Thread handle
48
  @param table        Table to be accessed
49
  @param print_error  If true, call table->file->print_error() if an error
50
                      occurs (except for end-of-records error)
51
  @param idx          index to scan
52
*/
53
77.1.46 by Monty Taylor
Finished the warnings work!
54
void init_read_record_idx(READ_RECORD *info,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
55
                          THD *thd __attribute__((unused)),
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
56
                          Table *table,
1 by brian
clean slate
57
                          bool print_error, uint idx)
58
{
59
  empty_record(table);
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
60
  memset(info, 0, sizeof(*info));
1 by brian
clean slate
61
  info->table= table;
62
  info->file=  table->file;
63
  info->record= table->record[0];
64
  info->print_error= print_error;
65
66
  table->status=0;			/* And it's always found */
67
  if (!table->file->inited)
68
    table->file->ha_index_init(idx, 1);
69
  /* read_record will be changed to rr_index in rr_index_first */
70
  info->read_record= rr_index_first;
71
}
72
73
74
/*
75
  init_read_record is used to scan by using a number of different methods.
76
  Which method to use is set-up in this call so that later calls to
77
  the info->read_record will call the appropriate method using a function
78
  pointer.
79
80
  There are five methods that relate completely to the sort function
81
  filesort. The result of a filesort is retrieved using read_record
82
  calls. The other two methods are used for normal table access.
83
84
  The filesort will produce references to the records sorted, these
85
  references can be stored in memory or in a temporary file.
86
87
  The temporary file is normally used when the references doesn't fit into
88
  a properly sized memory buffer. For most small queries the references
89
  are stored in the memory buffer.
90
91
  The temporary file is also used when performing an update where a key is
92
  modified.
93
94
  Methods used when ref's are in memory (using rr_from_pointers):
95
    rr_unpack_from_buffer:
96
    ----------------------
97
      This method is used when table->sort.addon_field is allocated.
98
      This is allocated for most SELECT queries not involving any BLOB's.
99
      In this case the records are fetched from a memory buffer.
100
    rr_from_pointers:
101
    -----------------
102
      Used when the above is not true, UPDATE, DELETE and so forth and
103
      SELECT's involving BLOB's. It is also used when the addon_field
104
      buffer is not allocated due to that its size was bigger than the
105
      session variable max_length_for_sort_data.
106
      In this case the record data is fetched from the handler using the
107
      saved reference using the rnd_pos handler call.
108
109
  Methods used when ref's are in a temporary file (using rr_from_tempfile)
110
    rr_unpack_from_tempfile:
111
    ------------------------
112
      Same as rr_unpack_from_buffer except that references are fetched from
113
      temporary file. Should obviously not really happen other than in
114
      strange configurations.
115
116
    rr_from_tempfile:
117
    -----------------
118
      Same as rr_from_pointers except that references are fetched from
119
      temporary file instead of from 
120
    rr_from_cache:
121
    --------------
122
      This is a special variant of rr_from_tempfile that can be used for
123
      handlers that is not using the HA_FAST_KEY_READ table flag. Instead
124
      of reading the references one by one from the temporary file it reads
125
      a set of them, sorts them and reads all of them into a buffer which
126
      is then used for a number of subsequent calls to rr_from_cache.
127
      It is only used for SELECT queries and a number of other conditions
128
      on table size.
129
130
  All other accesses use either index access methods (rr_quick) or a full
131
  table scan (rr_sequential).
132
  rr_quick:
133
  ---------
134
    rr_quick uses one of the QUICK_SELECT classes in opt_range.cc to
135
    perform an index scan. There are loads of functionality hidden
136
    in these quick classes. It handles all index scans of various kinds.
137
  rr_sequential:
138
  --------------
139
    This is the most basic access method of a table using rnd_init,
140
    rnd_next and rnd_end. No indexes are used.
141
*/
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
142
void init_read_record(READ_RECORD *info,THD *thd, Table *table,
1 by brian
clean slate
143
		      SQL_SELECT *select,
144
		      int use_record_cache, bool print_error)
145
{
146
  IO_CACHE *tempfile;
147
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
148
  memset(info, 0, sizeof(*info));
1 by brian
clean slate
149
  info->thd=thd;
150
  info->table=table;
151
  info->file= table->file;
152
  info->forms= &info->table;		/* Only one table */
153
  
154
  if (table->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE &&
155
      !table->sort.addon_field)
156
    VOID(table->file->extra(HA_EXTRA_MMAP));
157
  
158
  if (table->sort.addon_field)
159
  {
160
    info->rec_buf= table->sort.addon_buf;
161
    info->ref_length= table->sort.addon_length;
162
  }
163
  else
164
  {
165
    empty_record(table);
166
    info->record= table->record[0];
167
    info->ref_length= table->file->ref_length;
168
  }
169
  info->select=select;
170
  info->print_error=print_error;
171
  info->ignore_not_found_rows= 0;
172
  table->status=0;			/* And it's always found */
173
174
  if (select && my_b_inited(&select->file))
175
    tempfile= &select->file;
176
  else
177
    tempfile= table->sort.io_cache;
178
  if (tempfile && my_b_inited(tempfile)) // Test if ref-records was used
179
  {
180
    info->read_record= (table->sort.addon_field ?
181
                        rr_unpack_from_tempfile : rr_from_tempfile);
182
    info->io_cache=tempfile;
183
    reinit_io_cache(info->io_cache,READ_CACHE,0L,0,0);
184
    info->ref_pos=table->file->ref;
185
    if (!table->file->inited)
186
      table->file->ha_rnd_init(0);
187
188
    /*
189
      table->sort.addon_field is checked because if we use addon fields,
190
      it doesn't make sense to use cache - we don't read from the table
191
      and table->sort.io_cache is read sequentially
192
    */
193
    if (!table->sort.addon_field &&
194
	thd->variables.read_rnd_buff_size &&
195
	!(table->file->ha_table_flags() & HA_FAST_KEY_READ) &&
196
	(table->db_stat & HA_READ_ONLY ||
197
	 table->reginfo.lock_type <= TL_READ_NO_INSERT) &&
151 by Brian Aker
Ulonglong to uint64_t
198
	(uint64_t) table->s->reclength* (table->file->stats.records+
1 by brian
clean slate
199
                                          table->file->stats.deleted) >
151 by Brian Aker
Ulonglong to uint64_t
200
	(uint64_t) MIN_FILE_LENGTH_TO_USE_ROW_CACHE &&
1 by brian
clean slate
201
	info->io_cache->end_of_file/info->ref_length * table->s->reclength >
202
	(my_off_t) MIN_ROWS_TO_USE_TABLE_CACHE &&
203
	!table->s->blob_fields &&
204
        info->ref_length <= MAX_REFLENGTH)
205
    {
206
      if (! init_rr_cache(thd, info))
207
      {
208
	info->read_record=rr_from_cache;
209
      }
210
    }
211
  }
212
  else if (select && select->quick)
213
  {
214
    info->read_record=rr_quick;
215
  }
216
  else if (table->sort.record_pointers)
217
  {
218
    table->file->ha_rnd_init(0);
219
    info->cache_pos=table->sort.record_pointers;
220
    info->cache_end=info->cache_pos+ 
221
                    table->sort.found_records*info->ref_length;
222
    info->read_record= (table->sort.addon_field ?
223
                        rr_unpack_from_buffer : rr_from_pointers);
224
  }
225
  else
226
  {
227
    info->read_record=rr_sequential;
228
    table->file->ha_rnd_init(1);
229
    /* We can use record cache if we don't update dynamic length tables */
230
    if (!table->no_cache &&
231
	(use_record_cache > 0 ||
232
	 (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY ||
233
	 !(table->s->db_options_in_use & HA_OPTION_PACK_RECORD) ||
234
	 (use_record_cache < 0 &&
235
	  !(table->file->ha_table_flags() & HA_NOT_DELETE_WITH_CACHE))))
236
      VOID(table->file->extra_opt(HA_EXTRA_CACHE,
237
				  thd->variables.read_buff_size));
238
  }
239
  /* 
240
    Do condition pushdown for UPDATE/DELETE.
241
    TODO: Remove this from here as it causes two condition pushdown calls 
242
    when we're running a SELECT and the condition cannot be pushed down.
243
  */
244
  if (thd->variables.engine_condition_pushdown && 
245
      select && select->cond && 
246
      (select->cond->used_tables() & table->map) &&
247
      !table->file->pushed_cond)
248
    table->file->cond_push(select->cond);
249
51.1.4 by Jay Pipes
Removed all DBUG_XXX content from records.cc. Builds/tests run fine. On to next file...
250
  return;
1 by brian
clean slate
251
} /* init_read_record */
252
253
254
255
void end_read_record(READ_RECORD *info)
256
{                   /* free cache if used */
257
  if (info->cache)
258
  {
259
    my_free_lock((char*) info->cache,MYF(0));
260
    info->cache=0;
261
  }
262
  if (info->table)
263
  {
264
    filesort_free_buffers(info->table,0);
265
    (void) info->file->extra(HA_EXTRA_NO_CACHE);
266
    if (info->read_record != rr_quick) // otherwise quick_range does it
267
      (void) info->file->ha_index_or_rnd_end();
268
    info->table=0;
269
  }
270
}
271
272
static int rr_handle_error(READ_RECORD *info, int error)
273
{
274
  if (error == HA_ERR_END_OF_FILE)
275
    error= -1;
276
  else
277
  {
278
    if (info->print_error)
279
      info->table->file->print_error(error, MYF(0));
280
    if (error < 0)                            // Fix negative BDB errno
281
      error= 1;
282
  }
283
  return error;
284
}
285
286
287
/** Read a record from head-database. */
288
289
static int rr_quick(READ_RECORD *info)
290
{
291
  int tmp;
292
  while ((tmp= info->select->quick->get_next()))
293
  {
294
    if (info->thd->killed)
295
    {
296
      my_error(ER_SERVER_SHUTDOWN, MYF(0));
297
      return 1;
298
    }
299
    if (tmp != HA_ERR_RECORD_DELETED)
300
    {
301
      tmp= rr_handle_error(info, tmp);
302
      break;
303
    }
304
  }
305
  return tmp;
306
}
307
308
309
/**
310
  Reads first row in an index scan.
311
312
  @param info  	Scan info
313
314
  @retval
315
    0   Ok
316
  @retval
317
    -1   End of records
318
  @retval
319
    1   Error
320
*/
321
322
static int rr_index_first(READ_RECORD *info)
323
{
324
  int tmp= info->file->index_first(info->record);
325
  info->read_record= rr_index;
326
  if (tmp)
327
    tmp= rr_handle_error(info, tmp);
328
  return tmp;
329
}
330
331
332
/**
333
  Reads index sequentially after first row.
334
335
  Read the next index record (in forward direction) and translate return
336
  value.
337
338
  @param info  Scan info
339
340
  @retval
341
    0   Ok
342
  @retval
343
    -1   End of records
344
  @retval
345
    1   Error
346
*/
347
348
static int rr_index(READ_RECORD *info)
349
{
350
  int tmp= info->file->index_next(info->record);
351
  if (tmp)
352
    tmp= rr_handle_error(info, tmp);
353
  return tmp;
354
}
355
356
357
int rr_sequential(READ_RECORD *info)
358
{
359
  int tmp;
360
  while ((tmp=info->file->rnd_next(info->record)))
361
  {
362
    if (info->thd->killed)
363
    {
364
      info->thd->send_kill_message();
365
      return 1;
366
    }
367
    /*
368
      rnd_next can return RECORD_DELETED for MyISAM when one thread is
369
      reading and another deleting without locks.
370
    */
371
    if (tmp != HA_ERR_RECORD_DELETED)
372
    {
373
      tmp= rr_handle_error(info, tmp);
374
      break;
375
    }
376
  }
377
  return tmp;
378
}
379
380
381
static int rr_from_tempfile(READ_RECORD *info)
382
{
383
  int tmp;
384
  for (;;)
385
  {
386
    if (my_b_read(info->io_cache,info->ref_pos,info->ref_length))
387
      return -1;					/* End of file */
388
    if (!(tmp=info->file->rnd_pos(info->record,info->ref_pos)))
389
      break;
390
    /* The following is extremely unlikely to happen */
391
    if (tmp == HA_ERR_RECORD_DELETED ||
392
        (tmp == HA_ERR_KEY_NOT_FOUND && info->ignore_not_found_rows))
393
      continue;
394
    tmp= rr_handle_error(info, tmp);
395
    break;
396
  }
397
  return tmp;
398
} /* rr_from_tempfile */
399
400
401
/**
402
  Read a result set record from a temporary file after sorting.
403
404
  The function first reads the next sorted record from the temporary file.
405
  into a buffer. If a success it calls a callback function that unpacks 
406
  the fields values use in the result set from this buffer into their
407
  positions in the regular record buffer.
408
409
  @param info          Reference to the context including record descriptors
410
411
  @retval
412
    0   Record successfully read.
413
  @retval
414
    -1   There is no record to be read anymore.
415
*/
416
417
static int rr_unpack_from_tempfile(READ_RECORD *info)
418
{
419
  if (my_b_read(info->io_cache, info->rec_buf, info->ref_length))
420
    return -1;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
421
  Table *table= info->table;
1 by brian
clean slate
422
  (*table->sort.unpack)(table->sort.addon_field, info->rec_buf);
423
424
  return 0;
425
}
426
427
static int rr_from_pointers(READ_RECORD *info)
428
{
429
  int tmp;
430
  uchar *cache_pos;
431
432
  for (;;)
433
  {
434
    if (info->cache_pos == info->cache_end)
435
      return -1;					/* End of file */
436
    cache_pos= info->cache_pos;
437
    info->cache_pos+= info->ref_length;
438
439
    if (!(tmp=info->file->rnd_pos(info->record,cache_pos)))
440
      break;
441
442
    /* The following is extremely unlikely to happen */
443
    if (tmp == HA_ERR_RECORD_DELETED ||
444
        (tmp == HA_ERR_KEY_NOT_FOUND && info->ignore_not_found_rows))
445
      continue;
446
    tmp= rr_handle_error(info, tmp);
447
    break;
448
  }
449
  return tmp;
450
}
451
452
/**
453
  Read a result set record from a buffer after sorting.
454
455
  The function first reads the next sorted record from the sort buffer.
456
  If a success it calls a callback function that unpacks 
457
  the fields values use in the result set from this buffer into their
458
  positions in the regular record buffer.
459
460
  @param info          Reference to the context including record descriptors
461
462
  @retval
463
    0   Record successfully read.
464
  @retval
465
    -1   There is no record to be read anymore.
466
*/
467
468
static int rr_unpack_from_buffer(READ_RECORD *info)
469
{
470
  if (info->cache_pos == info->cache_end)
471
    return -1;                      /* End of buffer */
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
472
  Table *table= info->table;
1 by brian
clean slate
473
  (*table->sort.unpack)(table->sort.addon_field, info->cache_pos);
474
  info->cache_pos+= info->ref_length;
475
476
  return 0;
477
}
478
	/* cacheing of records from a database */
479
480
static int init_rr_cache(THD *thd, READ_RECORD *info)
481
{
482
  uint rec_cache_size;
483
484
  info->struct_length= 3+MAX_REFLENGTH;
485
  info->reclength= ALIGN_SIZE(info->table->s->reclength+1);
486
  if (info->reclength < info->struct_length)
487
    info->reclength= ALIGN_SIZE(info->struct_length);
488
489
  info->error_offset= info->table->s->reclength;
490
  info->cache_records= (thd->variables.read_rnd_buff_size /
491
                        (info->reclength+info->struct_length));
492
  rec_cache_size= info->cache_records*info->reclength;
493
  info->rec_cache_size= info->cache_records*info->ref_length;
494
495
  // We have to allocate one more byte to use uint3korr (see comments for it)
496
  if (info->cache_records <= 2 ||
497
      !(info->cache=(uchar*) my_malloc_lock(rec_cache_size+info->cache_records*
498
					   info->struct_length+1,
499
					   MYF(0))))
51.1.4 by Jay Pipes
Removed all DBUG_XXX content from records.cc. Builds/tests run fine. On to next file...
500
    return(1);
1 by brian
clean slate
501
#ifdef HAVE_purify
502
  // Avoid warnings in qsort
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
503
  memset(info->cache, 0,
504
         rec_cache_size+info->cache_records* info->struct_length+1);
1 by brian
clean slate
505
#endif
506
  info->read_positions=info->cache+rec_cache_size;
507
  info->cache_pos=info->cache_end=info->cache;
51.1.4 by Jay Pipes
Removed all DBUG_XXX content from records.cc. Builds/tests run fine. On to next file...
508
  return(0);
1 by brian
clean slate
509
} /* init_rr_cache */
510
511
512
static int rr_from_cache(READ_RECORD *info)
513
{
514
  register uint i;
308 by Brian Aker
ulong conversion
515
  uint32_t length;
1 by brian
clean slate
516
  my_off_t rest_of_file;
206 by Brian Aker
Removed final uint dead types.
517
  int16_t error;
1 by brian
clean slate
518
  uchar *position,*ref_position,*record_pos;
308 by Brian Aker
ulong conversion
519
  uint32_t record;
1 by brian
clean slate
520
521
  for (;;)
522
  {
523
    if (info->cache_pos != info->cache_end)
524
    {
525
      if (info->cache_pos[info->error_offset])
526
      {
527
	shortget(error,info->cache_pos);
528
	if (info->print_error)
529
	  info->table->file->print_error(error,MYF(0));
530
      }
531
      else
532
      {
533
	error=0;
534
	memcpy(info->record,info->cache_pos,
535
               (size_t) info->table->s->reclength);
536
      }
537
      info->cache_pos+=info->reclength;
538
      return ((int) error);
539
    }
540
    length=info->rec_cache_size;
541
    rest_of_file=info->io_cache->end_of_file - my_b_tell(info->io_cache);
542
    if ((my_off_t) length > rest_of_file)
308 by Brian Aker
ulong conversion
543
      length= (uint32_t) rest_of_file;
1 by brian
clean slate
544
    if (!length || my_b_read(info->io_cache,info->cache,length))
545
    {
546
      return -1;			/* End of file */
547
    }
548
549
    length/=info->ref_length;
550
    position=info->cache;
551
    ref_position=info->read_positions;
552
    for (i=0 ; i < length ; i++,position+=info->ref_length)
553
    {
554
      memcpy(ref_position,position,(size_t) info->ref_length);
555
      ref_position+=MAX_REFLENGTH;
556
      int3store(ref_position,(long) i);
557
      ref_position+=3;
558
    }
559
    my_qsort(info->read_positions, length, info->struct_length,
560
             (qsort_cmp) rr_cmp);
561
562
    position=info->read_positions;
563
    for (i=0 ; i < length ; i++)
564
    {
565
      memcpy(info->ref_pos,position,(size_t) info->ref_length);
566
      position+=MAX_REFLENGTH;
567
      record=uint3korr(position);
568
      position+=3;
569
      record_pos=info->cache+record*info->reclength;
206 by Brian Aker
Removed final uint dead types.
570
      if ((error=(int16_t) info->file->rnd_pos(record_pos,info->ref_pos)))
1 by brian
clean slate
571
      {
572
	record_pos[info->error_offset]=1;
573
	shortstore(record_pos,error);
574
      }
575
      else
576
	record_pos[info->error_offset]=0;
577
    }
578
    info->cache_end=(info->cache_pos=info->cache)+length*info->reclength;
579
  }
580
} /* rr_from_cache */
581
582
583
static int rr_cmp(uchar *a,uchar *b)
584
{
585
  if (a[0] != b[0])
586
    return (int) a[0] - (int) b[0];
587
  if (a[1] != b[1])
588
    return (int) a[1] - (int) b[1];
589
  if (a[2] != b[2])
590
    return (int) a[2] - (int) b[2];
591
#if MAX_REFLENGTH == 4
592
  return (int) a[3] - (int) b[3];
593
#else
594
  if (a[3] != b[3])
595
    return (int) a[3] - (int) b[3];
596
  if (a[4] != b[4])
597
    return (int) a[4] - (int) b[4];
598
  if (a[5] != b[5])
599
    return (int) a[1] - (int) b[5];
600
  if (a[6] != b[6])
601
    return (int) a[6] - (int) b[6];
602
  return (int) a[7] - (int) b[7];
603
#endif
604
}