records.cc 15.3 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
unknown's avatar
unknown committed
7

unknown's avatar
unknown committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
unknown's avatar
unknown committed
12

unknown's avatar
unknown committed
13 14 15 16 17
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


unknown's avatar
unknown committed
18
/* Functions for easy reading of records, possible through a cache */
unknown's avatar
unknown committed
19 20 21 22 23 24

#include "mysql_priv.h"

static int rr_quick(READ_RECORD *info);
static int rr_sequential(READ_RECORD *info);
static int rr_from_tempfile(READ_RECORD *info);
unknown's avatar
unknown committed
25 26
static int rr_unpack_from_tempfile(READ_RECORD *info);
static int rr_unpack_from_buffer(READ_RECORD *info);
unknown's avatar
unknown committed
27 28
static int rr_from_pointers(READ_RECORD *info);
static int rr_from_cache(READ_RECORD *info);
29
static int init_rr_cache(THD *thd, READ_RECORD *info);
unknown's avatar
unknown committed
30 31
static int rr_cmp(uchar *a,uchar *b);

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
static int rr_index(READ_RECORD *info);



/*
  Initialize READ_RECORD structure to perform full index scan

  SYNOPSIS 
    init_read_record_idx()
      info         READ_RECORD structure to initialize.
      thd          Thread handle
      table        Table to be accessed 
      print_error  If true, call table->file->print_error() if an error
                   occurs (except for end-of-records error)
      idx          index to scan
  
  DESCRIPTION
    Initialize READ_RECORD structure to perform full index scan (in forward
    direction) using read_record.read_record() interface.
    
    This function has been added at late stage and is used only by
    UPDATE/DELETE. Other statements perform index scans using
    join_read_first/next functions.
*/

void init_read_record_idx(READ_RECORD *info, THD *thd, TABLE *table,
                          bool print_error, uint idx)
{
  bzero((char*) info,sizeof(*info));
  info->thd=thd;
  info->table=table;
  info->file= table->file;
  info->forms= &info->table;		/* Only one table */
    
  info->record= table->record[0];
  info->ref_length= table->file->ref_length;

  info->select=NULL;
  info->print_error=print_error;
  info->ignore_not_found_rows= 0;
  table->status=0;			/* And it's always found */

  if (!table->file->inited)
  {
    table->file->ha_index_init(idx);
    table->file->extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY);
  }
  info->read_record= rr_index;
  info->first= TRUE;
}


/* init struct for read with info->read_record */
unknown's avatar
unknown committed
85 86 87

void init_read_record(READ_RECORD *info,THD *thd, TABLE *table,
		      SQL_SELECT *select,
88
		      int use_record_cache, bool print_error)
unknown's avatar
unknown committed
89 90 91 92 93 94 95 96 97
{
  IO_CACHE *tempfile;
  DBUG_ENTER("init_read_record");

  bzero((char*) info,sizeof(*info));
  info->thd=thd;
  info->table=table;
  info->file= table->file;
  info->forms= &info->table;		/* Only one table */
unknown's avatar
unknown committed
98 99 100 101 102 103 104 105 106 107
  if (table->sort.addon_field)
  {
    info->rec_buf= table->sort.addon_buf;
    info->ref_length= table->sort.addon_length;
  }
  else
  {
    info->record= table->record[0];
    info->ref_length= table->file->ref_length;
  }
unknown's avatar
unknown committed
108 109
  info->select=select;
  info->print_error=print_error;
110 111
  info->ignore_not_found_rows= 0;
  table->status=0;			/* And it's always found */
unknown's avatar
unknown committed
112 113 114 115

  if (select && my_b_inited(&select->file))
    tempfile= &select->file;
  else
unknown's avatar
unknown committed
116
    tempfile= table->sort.io_cache;
unknown's avatar
unknown committed
117
  if (tempfile && my_b_inited(tempfile)) // Test if ref-records was used
unknown's avatar
unknown committed
118 119
  {
    DBUG_PRINT("info",("using rr_from_tempfile"));
unknown's avatar
unknown committed
120 121
    info->read_record= (table->sort.addon_field ?
                        rr_unpack_from_tempfile : rr_from_tempfile);
unknown's avatar
unknown committed
122 123 124
    info->io_cache=tempfile;
    reinit_io_cache(info->io_cache,READ_CACHE,0L,0,0);
    info->ref_pos=table->file->ref;
unknown's avatar
unknown committed
125 126
    if (!table->file->inited)
      table->file->ha_rnd_init(0);
unknown's avatar
unknown committed
127

unknown's avatar
unknown committed
128 129 130 131 132 133 134
    /*
      table->sort.addon_field is checked because if we use addon fields,
      it doesn't make sense to use cache - we don't read from the table
      and table->sort.io_cache is read sequentially
    */
    if (!table->sort.addon_field &&
        ! (specialflag & SPECIAL_SAFE_MODE) &&
unknown's avatar
unknown committed
135
	thd->variables.read_rnd_buff_size &&
136
	!(table->file->table_flags() & HA_FAST_KEY_READ) &&
unknown's avatar
unknown committed
137
	(table->db_stat & HA_READ_ONLY ||
138
	 table->reginfo.lock_type <= TL_READ_NO_INSERT) &&
139 140
	(ulonglong) table->s->reclength* (table->file->records+
                                          table->file->deleted) >
unknown's avatar
unknown committed
141
	(ulonglong) MIN_FILE_LENGTH_TO_USE_ROW_CACHE &&
142
	info->io_cache->end_of_file/info->ref_length * table->s->reclength >
unknown's avatar
unknown committed
143
	(my_off_t) MIN_ROWS_TO_USE_TABLE_CACHE &&
144 145
	!table->s->blob_fields &&
        info->ref_length <= MAX_REFLENGTH)
unknown's avatar
unknown committed
146
    {
147
      if (! init_rr_cache(thd, info))
unknown's avatar
unknown committed
148 149 150 151 152 153
      {
	DBUG_PRINT("info",("using rr_from_cache"));
	info->read_record=rr_from_cache;
      }
    }
  }
unknown's avatar
unknown committed
154 155 156 157 158
  else if (select && select->quick)
  {
    DBUG_PRINT("info",("using rr_quick"));
    info->read_record=rr_quick;
  }
unknown's avatar
unknown committed
159
  else if (table->sort.record_pointers)
unknown's avatar
unknown committed
160
  {
161
    DBUG_PRINT("info",("using record_pointers"));
unknown's avatar
unknown committed
162
    table->file->ha_rnd_init(0);
unknown's avatar
unknown committed
163 164 165 166 167
    info->cache_pos=table->sort.record_pointers;
    info->cache_end=info->cache_pos+ 
                    table->sort.found_records*info->ref_length;
    info->read_record= (table->sort.addon_field ?
                        rr_unpack_from_buffer : rr_from_pointers);
unknown's avatar
unknown committed
168 169 170 171 172
  }
  else
  {
    DBUG_PRINT("info",("using rr_sequential"));
    info->read_record=rr_sequential;
173
    table->file->ha_rnd_init(1);
unknown's avatar
unknown committed
174
    /* We can use record cache if we don't update dynamic length tables */
175 176 177
    if (!table->no_cache &&
	(use_record_cache > 0 ||
	 (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY ||
178
	 !(table->s->db_options_in_use & HA_OPTION_PACK_RECORD) ||
179 180
	 (use_record_cache < 0 &&
	  !(table->file->table_flags() & HA_NOT_DELETE_WITH_CACHE))))
unknown's avatar
unknown committed
181 182
      VOID(table->file->extra_opt(HA_EXTRA_CACHE,
				  thd->variables.read_buff_size));
unknown's avatar
unknown committed
183
  }
184 185 186
  /* Condition pushdown to storage engine */
  if (thd->variables.engine_condition_pushdown && 
      select && select->cond && 
unknown's avatar
unknown committed
187
      (select->cond->used_tables() & table->map) &&
188
      !table->file->pushed_cond)
189 190
    table->file->cond_push(select->cond);

unknown's avatar
unknown committed
191 192 193 194 195
  DBUG_VOID_RETURN;
} /* init_read_record */


void end_read_record(READ_RECORD *info)
unknown's avatar
unknown committed
196
{                   /* free cache if used */
unknown's avatar
unknown committed
197 198 199 200 201 202 203
  if (info->cache)
  {
    my_free_lock((char*) info->cache,MYF(0));
    info->cache=0;
  }
  if (info->table)
  {
unknown's avatar
unknown committed
204
    filesort_free_buffers(info->table);
unknown's avatar
unknown committed
205
    (void) info->file->extra(HA_EXTRA_NO_CACHE);
unknown's avatar
unknown committed
206 207
    if (info->read_record != rr_quick) // otherwise quick_range does it
      (void) info->file->ha_index_or_rnd_end();
unknown's avatar
unknown committed
208 209 210 211 212 213 214 215
    info->table=0;
  }
}

	/* Read a record from head-database */

static int rr_quick(READ_RECORD *info)
{
unknown's avatar
unknown committed
216 217
  int tmp;
  while ((tmp= info->select->quick->get_next()))
unknown's avatar
unknown committed
218
  {
unknown's avatar
unknown committed
219
    if (info->thd->killed)
220
    {
unknown's avatar
unknown committed
221 222 223 224
      my_error(ER_SERVER_SHUTDOWN, MYF(0));
      return 1;
    }
    if (tmp != HA_ERR_RECORD_DELETED)
225
    {
unknown's avatar
unknown committed
226 227 228 229 230 231 232 233 234 235
      if (tmp == HA_ERR_END_OF_FILE)
        tmp= -1;
      else
      {
        if (info->print_error)
          info->file->print_error(tmp,MYF(0));
        if (tmp < 0)				// Fix negative BDB errno
          tmp=1;
      }
      break;
236
    }
unknown's avatar
unknown committed
237 238 239 240 241
  }
  return tmp;
}


242
/*
unknown's avatar
unknown committed
243
  A READ_RECORD::read_record implementation that reads index sequentially
244 245 246 247

  SYNOPSIS
    rr_index()
      info  Scan info
unknown's avatar
unknown committed
248 249 250 251 252
  
  DESCRIPTION
    Read the next index record (in forward direction) and translate return
    value.
    
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  RETURN
    0   Ok
    -1  End of records 
    1   Error   
*/

static int rr_index(READ_RECORD *info)
{
  int tmp;
  while (1)
  {
    if (info->first)
    {
      info->first= FALSE;
      tmp= info->file->index_first(info->record);
    }
    else
      tmp= info->file->index_next(info->record);
    
    if (!tmp)
      break;
    if (info->thd->killed)
    {
      my_error(ER_SERVER_SHUTDOWN,MYF(0));
      return 1;
    }
    if (tmp != HA_ERR_RECORD_DELETED)
    {
      if (tmp == HA_ERR_END_OF_FILE)
unknown's avatar
unknown committed
282
        tmp= -1;
283 284
      else
      {
unknown's avatar
unknown committed
285 286 287 288
        if (info->print_error)
          info->table->file->print_error(tmp,MYF(0));
        if (tmp < 0)                            // Fix negative BDB errno
          tmp=1;
289 290 291 292 293 294 295
      }
      break;
    }
  }
  return tmp;
}

unknown's avatar
unknown committed
296 297 298 299 300 301 302
static int rr_sequential(READ_RECORD *info)
{
  int tmp;
  while ((tmp=info->file->rnd_next(info->record)))
  {
    if (info->thd->killed)
    {
unknown's avatar
SCRUM  
unknown committed
303
      info->thd->send_kill_message();
unknown's avatar
unknown committed
304 305 306 307 308 309
      return 1;
    }
    if (tmp != HA_ERR_RECORD_DELETED)
    {
      if (tmp == HA_ERR_END_OF_FILE)
	tmp= -1;
310 311 312 313 314 315 316
      else
      {
	if (info->print_error)
	  info->table->file->print_error(tmp,MYF(0));
	if (tmp < 0)				// Fix negative BDB errno
	  tmp=1;
      }
unknown's avatar
unknown committed
317 318 319 320 321 322 323 324 325
      break;
    }
  }
  return tmp;
}


static int rr_from_tempfile(READ_RECORD *info)
{
326 327
  int tmp;
tryNext:
unknown's avatar
unknown committed
328 329
  if (my_b_read(info->io_cache,info->ref_pos,info->ref_length))
    return -1;					/* End of file */
330
  if ((tmp=info->file->rnd_pos(info->record,info->ref_pos)))
unknown's avatar
unknown committed
331 332 333
  {
    if (tmp == HA_ERR_END_OF_FILE)
      tmp= -1;
334 335
    else if (tmp == HA_ERR_RECORD_DELETED ||
	     (tmp == HA_ERR_KEY_NOT_FOUND && info->ignore_not_found_rows))
336
      goto tryNext;
337 338 339 340 341 342 343
    else
    {
      if (info->print_error)
	info->file->print_error(tmp,MYF(0));
      if (tmp < 0)				// Fix negative BDB errno
	tmp=1;
    }
unknown's avatar
unknown committed
344 345 346 347
  }
  return tmp;
} /* rr_from_tempfile */

348

unknown's avatar
unknown committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
/*
  Read a result set record from a temporary file after sorting

  SYNOPSIS
    rr_unpack_from_tempfile()
    info                Reference to the context including record descriptors

  DESCRIPTION
    The function first reads the next sorted record from the temporary file.
    into a buffer. If a success it calls a callback function that unpacks 
    the fields values use in the result set from this buffer into their
    positions in the regular record buffer.

  RETURN
     0 - Record successfully read.
    -1 - There is no record to be read anymore. 
*/

static int rr_unpack_from_tempfile(READ_RECORD *info)
{
  if (my_b_read(info->io_cache, info->rec_buf, info->ref_length))
    return -1;
  TABLE *table= info->table;
  (*table->sort.unpack)(table->sort.addon_field, info->rec_buf);

  return 0;
}

unknown's avatar
unknown committed
377 378
static int rr_from_pointers(READ_RECORD *info)
{
379
  int tmp;
380 381
  byte *cache_pos;
tryNext:
unknown's avatar
unknown committed
382 383
  if (info->cache_pos == info->cache_end)
    return -1;					/* End of file */
384
  cache_pos=info->cache_pos;
unknown's avatar
unknown committed
385 386
  info->cache_pos+=info->ref_length;

387
  if ((tmp=info->file->rnd_pos(info->record,cache_pos)))
unknown's avatar
unknown committed
388 389 390
  {
    if (tmp == HA_ERR_END_OF_FILE)
      tmp= -1;
391 392
    else if (tmp == HA_ERR_RECORD_DELETED ||
	     (tmp == HA_ERR_KEY_NOT_FOUND && info->ignore_not_found_rows))
393
      goto tryNext;
394 395 396 397 398 399 400
    else
    {
      if (info->print_error)
	info->file->print_error(tmp,MYF(0));
      if (tmp < 0)				// Fix negative BDB errno
	tmp=1;
    }
unknown's avatar
unknown committed
401 402 403 404
  }
  return tmp;
}

unknown's avatar
unknown committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
/*
  Read a result set record from a buffer after sorting

  SYNOPSIS
    rr_unpack_from_buffer()
    info                   Reference to the context including record descriptors

  DESCRIPTION
    The function first reads the next sorted record from the sort buffer.
    If a success it calls a callback function that unpacks 
    the fields values use in the result set from this buffer into their
    positions in the regular record buffer.

  RETURN
     0 - Record successfully read.
    -1 - There is no record to be read anymore. 
*/

static int rr_unpack_from_buffer(READ_RECORD *info)
{
  if (info->cache_pos == info->cache_end)
    return -1;                      /* End of buffer */
  TABLE *table= info->table;
  (*table->sort.unpack)(table->sort.addon_field, info->cache_pos);
  info->cache_pos+= info->ref_length;

  return 0;
}
unknown's avatar
unknown committed
433 434
	/* cacheing of records from a database */

435
static int init_rr_cache(THD *thd, READ_RECORD *info)
unknown's avatar
unknown committed
436 437 438 439
{
  uint rec_cache_size;
  DBUG_ENTER("init_rr_cache");

440 441
  info->struct_length= 3+MAX_REFLENGTH;
  info->reclength= ALIGN_SIZE(info->table->s->reclength+1);
unknown's avatar
unknown committed
442
  if (info->reclength < info->struct_length)
443
    info->reclength= ALIGN_SIZE(info->struct_length);
unknown's avatar
unknown committed
444

445 446 447 448 449
  info->error_offset= info->table->s->reclength;
  info->cache_records= (thd->variables.read_rnd_buff_size /
                        (info->reclength+info->struct_length));
  rec_cache_size= info->cache_records*info->reclength;
  info->rec_cache_size= info->cache_records*info->ref_length;
unknown's avatar
unknown committed
450

451
  // We have to allocate one more byte to use uint3korr (see comments for it)
unknown's avatar
unknown committed
452 453
  if (info->cache_records <= 2 ||
      !(info->cache=(byte*) my_malloc_lock(rec_cache_size+info->cache_records*
454
					   info->struct_length+1,
unknown's avatar
unknown committed
455 456 457
					   MYF(0))))
    DBUG_RETURN(1);
#ifdef HAVE_purify
458 459
  // Avoid warnings in qsort
  bzero(info->cache,rec_cache_size+info->cache_records* info->struct_length+1);
unknown's avatar
unknown committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
#endif
  DBUG_PRINT("info",("Allocated buffert for %d records",info->cache_records));
  info->read_positions=info->cache+rec_cache_size;
  info->cache_pos=info->cache_end=info->cache;
  DBUG_RETURN(0);
} /* init_rr_cache */


static int rr_from_cache(READ_RECORD *info)
{
  reg1 uint i;
  ulong length;
  my_off_t rest_of_file;
  int16 error;
  byte *position,*ref_position,*record_pos;
  ulong record;

  for (;;)
  {
    if (info->cache_pos != info->cache_end)
    {
      if (info->cache_pos[info->error_offset])
      {
	shortget(error,info->cache_pos);
	if (info->print_error)
	  info->table->file->print_error(error,MYF(0));
      }
      else
      {
	error=0;
490 491
	memcpy(info->record,info->cache_pos,
               (size_t) info->table->s->reclength);
unknown's avatar
unknown committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
      }
      info->cache_pos+=info->reclength;
      return ((int) error);
    }
    length=info->rec_cache_size;
    rest_of_file=info->io_cache->end_of_file - my_b_tell(info->io_cache);
    if ((my_off_t) length > rest_of_file)
      length= (ulong) rest_of_file;
    if (!length || my_b_read(info->io_cache,info->cache,length))
    {
      DBUG_PRINT("info",("Found end of file"));
      return -1;			/* End of file */
    }

    length/=info->ref_length;
    position=info->cache;
    ref_position=info->read_positions;
    for (i=0 ; i < length ; i++,position+=info->ref_length)
    {
      memcpy(ref_position,position,(size_s) info->ref_length);
      ref_position+=MAX_REFLENGTH;
      int3store(ref_position,(long) i);
      ref_position+=3;
    }
    qsort(info->read_positions,length,info->struct_length,(qsort_cmp) rr_cmp);

    position=info->read_positions;
    for (i=0 ; i < length ; i++)
    {
      memcpy(info->ref_pos,position,(size_s) info->ref_length);
      position+=MAX_REFLENGTH;
      record=uint3korr(position);
      position+=3;
      record_pos=info->cache+record*info->reclength;
      if ((error=(int16) info->file->rnd_pos(record_pos,info->ref_pos)))
      {
	record_pos[info->error_offset]=1;
	shortstore(record_pos,error);
	DBUG_PRINT("error",("Got error: %d:%d when reading row",
			    my_errno, error));
      }
      else
	record_pos[info->error_offset]=0;
    }
    info->cache_end=(info->cache_pos=info->cache)+length*info->reclength;
  }
} /* rr_from_cache */


static int rr_cmp(uchar *a,uchar *b)
{
  if (a[0] != b[0])
    return (int) a[0] - (int) b[0];
  if (a[1] != b[1])
    return (int) a[1] - (int) b[1];
  if (a[2] != b[2])
    return (int) a[2] - (int) b[2];
#if MAX_REFLENGTH == 4
  return (int) a[3] - (int) b[3];
#else
  if (a[3] != b[3])
    return (int) a[3] - (int) b[3];
  if (a[4] != b[4])
    return (int) a[4] - (int) b[4];
  if (a[5] != b[5])
    return (int) a[1] - (int) b[5];
  if (a[6] != b[6])
    return (int) a[6] - (int) b[6];
  return (int) a[7] - (int) b[7];
#endif
}