~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/records.cc

  • Committer: Brian Aker
  • Date: 2008-07-14 16:09:55 UTC
  • Revision ID: brian@tangent.org-20080714160955-v5nzzyjj5hhv7bz6
Removing a few "additional" ways of saying uint64_t

Show diffs side-by-side

added added

removed removed

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