records.cc 9.65 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 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   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 */


/* Functions to read, write and lock records */

#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);
static int rr_from_pointers(READ_RECORD *info);
static int rr_from_cache(READ_RECORD *info);
static int init_rr_cache(READ_RECORD *info);
static int rr_cmp(uchar *a,uchar *b);

	/* init struct for read with info->read_record */

void init_read_record(READ_RECORD *info,THD *thd, TABLE *table,
		      SQL_SELECT *select,
34
		      int use_record_cache, bool print_error)
unknown's avatar
unknown committed
35 36 37 38 39 40 41 42 43 44 45 46 47
{
  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 */
  info->record=table->record[0];
  info->ref_length=table->file->ref_length;
  info->select=select;
  info->print_error=print_error;
48 49
  info->ignore_not_found_rows= 0;
  table->status=0;			/* And it's always found */
unknown's avatar
unknown committed
50 51 52 53 54

  if (select && my_b_inited(&select->file))
    tempfile= &select->file;
  else
    tempfile= table->io_cache;
unknown's avatar
unknown committed
55
  if (tempfile && my_b_inited(tempfile)) // Test if ref-records was used
unknown's avatar
unknown committed
56 57 58 59 60 61 62 63 64
  {
    DBUG_PRINT("info",("using rr_from_tempfile"));
    info->read_record=rr_from_tempfile;
    info->io_cache=tempfile;
    reinit_io_cache(info->io_cache,READ_CACHE,0L,0,0);
    info->ref_pos=table->file->ref;
    table->file->rnd_init(0);

    if (! (specialflag & SPECIAL_SAFE_MODE) &&
unknown's avatar
unknown committed
65
	thd->variables.read_rnd_buff_size &&
unknown's avatar
unknown committed
66 67
	!table->file->fast_key_read() &&
	(table->db_stat & HA_READ_ONLY ||
68
	 table->reginfo.lock_type <= TL_READ_NO_INSERT) &&
unknown's avatar
unknown committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82
	(ulonglong) table->reclength*(table->file->records+
				      table->file->deleted) >
	(ulonglong) MIN_FILE_LENGTH_TO_USE_ROW_CACHE &&
	info->io_cache->end_of_file/info->ref_length*table->reclength >
	(my_off_t) MIN_ROWS_TO_USE_TABLE_CACHE &&
	!table->blob_fields)
    {
      if (! init_rr_cache(info))
      {
	DBUG_PRINT("info",("using rr_from_cache"));
	info->read_record=rr_from_cache;
      }
    }
  }
unknown's avatar
unknown committed
83 84 85 86 87
  else if (select && select->quick)
  {
    DBUG_PRINT("info",("using rr_quick"));
    info->read_record=rr_quick;
  }
unknown's avatar
unknown committed
88 89
  else if (table->record_pointers)
  {
90
    DBUG_PRINT("info",("using record_pointers"));
unknown's avatar
unknown committed
91 92 93 94 95 96 97 98 99 100 101
    table->file->rnd_init(0);
    info->cache_pos=table->record_pointers;
    info->cache_end=info->cache_pos+ table->found_records*info->ref_length;
    info->read_record= rr_from_pointers;
  }
  else
  {
    DBUG_PRINT("info",("using rr_sequential"));
    info->read_record=rr_sequential;
    table->file->rnd_init();
    /* We can use record cache if we don't update dynamic length tables */
102
    if (use_record_cache > 0 ||
unknown's avatar
unknown committed
103
	(int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY ||
104 105
	!(table->db_options_in_use & HA_OPTION_PACK_RECORD) ||
	(use_record_cache < 0 &&
106
	 !(table->file->table_flags() & HA_NOT_DELETE_WITH_CACHE)))
unknown's avatar
unknown committed
107 108
      VOID(table->file->extra_opt(HA_EXTRA_CACHE,
				  thd->variables.read_buff_size));
unknown's avatar
unknown committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  }
  DBUG_VOID_RETURN;
} /* init_read_record */


void end_read_record(READ_RECORD *info)
{					/* free cache if used */
  if (info->cache)
  {
    my_free_lock((char*) info->cache,MYF(0));
    info->cache=0;
  }
  if (info->table)
  {
    (void) info->file->extra(HA_EXTRA_NO_CACHE);
    (void) info->file->rnd_end();
    info->table=0;
  }
}

	/* Read a record from head-database */

static int rr_quick(READ_RECORD *info)
{
  int tmp=info->select->quick->get_next();
  if (tmp)
  {
    if (tmp == HA_ERR_END_OF_FILE)
      tmp= -1;
138 139 140 141 142 143 144
    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
145 146 147 148 149 150 151 152 153 154 155 156
  }
  return tmp;
}


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
157
      info->thd->send_kill_message();
unknown's avatar
unknown committed
158 159 160 161 162 163
      return 1;
    }
    if (tmp != HA_ERR_RECORD_DELETED)
    {
      if (tmp == HA_ERR_END_OF_FILE)
	tmp= -1;
164 165 166 167 168 169 170
      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
171 172 173 174 175 176 177 178 179
      break;
    }
  }
  return tmp;
}


static int rr_from_tempfile(READ_RECORD *info)
{
180 181
  int tmp;
tryNext:
unknown's avatar
unknown committed
182 183
  if (my_b_read(info->io_cache,info->ref_pos,info->ref_length))
    return -1;					/* End of file */
184
  if ((tmp=info->file->rnd_pos(info->record,info->ref_pos)))
unknown's avatar
unknown committed
185 186 187
  {
    if (tmp == HA_ERR_END_OF_FILE)
      tmp= -1;
188 189
    else if (tmp == HA_ERR_RECORD_DELETED ||
	     (tmp == HA_ERR_KEY_NOT_FOUND && info->ignore_not_found_rows))
190
      goto tryNext;
191 192 193 194 195 196 197
    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
198 199 200 201
  }
  return tmp;
} /* rr_from_tempfile */

202

unknown's avatar
unknown committed
203 204
static int rr_from_pointers(READ_RECORD *info)
{
205
  int tmp;
206 207
  byte *cache_pos;
tryNext:
unknown's avatar
unknown committed
208 209
  if (info->cache_pos == info->cache_end)
    return -1;					/* End of file */
210
  cache_pos=info->cache_pos;
unknown's avatar
unknown committed
211 212
  info->cache_pos+=info->ref_length;

213
  if ((tmp=info->file->rnd_pos(info->record,cache_pos)))
unknown's avatar
unknown committed
214 215 216
  {
    if (tmp == HA_ERR_END_OF_FILE)
      tmp= -1;
217 218
    else if (tmp == HA_ERR_RECORD_DELETED ||
	     (tmp == HA_ERR_KEY_NOT_FOUND && info->ignore_not_found_rows))
219
      goto tryNext;
220 221 222 223 224 225 226
    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
227 228 229 230 231 232 233 234 235
  }
  return tmp;
}

	/* cacheing of records from a database */

static int init_rr_cache(READ_RECORD *info)
{
  uint rec_cache_size;
236 237
  THD *thd= current_thd;

unknown's avatar
unknown committed
238 239 240 241 242 243 244 245
  DBUG_ENTER("init_rr_cache");

  info->struct_length=3+MAX_REFLENGTH;
  info->reclength=ALIGN_SIZE(info->table->reclength+1);
  if (info->reclength < info->struct_length)
    info->reclength=ALIGN_SIZE(info->struct_length);

  info->error_offset=info->table->reclength;
unknown's avatar
unknown committed
246
  info->cache_records= thd->variables.read_rnd_buff_size /
unknown's avatar
unknown committed
247 248 249 250 251 252 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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    (info->reclength+info->struct_length);
  rec_cache_size=info->cache_records*info->reclength;
  info->rec_cache_size=info->cache_records*info->ref_length;

  if (info->cache_records <= 2 ||
      !(info->cache=(byte*) my_malloc_lock(rec_cache_size+info->cache_records*
					   info->struct_length,
					   MYF(0))))
    DBUG_RETURN(1);
#ifdef HAVE_purify
  bzero(info->cache,rec_cache_size);		// Avoid warnings in qsort
#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;
	memcpy(info->record,info->cache_pos,(size_t) info->table->reclength);
      }
      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
}