~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/records.cc

  • Committer: Monty Taylor
  • Date: 2008-10-09 22:38:27 UTC
  • mto: This revision was merged to the branch mainline in revision 497.
  • Revision ID: monty@inaugust.com-20081009223827-bc9gvpiplsmvpwyq
Moved test() to its own file.
Made a new function to possibly replace int10_to_str.

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
 
#include "drizzled/error.h"
24
 
#include "drizzled/table.h"
25
 
#include "drizzled/session.h"
26
 
#include "drizzled/records.h"
27
 
#include "drizzled/optimizer/range.h"
28
 
#include "drizzled/internal/my_sys.h"
29
 
#include "drizzled/internal/iocache.h"
30
 
#include "drizzled/drizzled.h"
31
 
 
32
 
namespace drizzled
33
 
{
34
 
 
35
 
static int rr_sequential(ReadRecord *info);
36
 
static int rr_quick(ReadRecord *info);
37
 
static int rr_from_tempfile(ReadRecord *info);
38
 
static int rr_unpack_from_tempfile(ReadRecord *info);
39
 
static int rr_unpack_from_buffer(ReadRecord *info);
40
 
static int rr_from_pointers(ReadRecord *info);
41
 
static int rr_from_cache(ReadRecord *info);
 
23
#include <drizzled/server_includes.h>
 
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);
42
33
static int rr_cmp(unsigned char *a,unsigned char *b);
43
 
static int rr_index_first(ReadRecord *info);
44
 
static int rr_index(ReadRecord *info);
45
 
 
46
 
void ReadRecord::init_reard_record_sequential()
47
 
{
48
 
  read_record= rr_sequential;
49
 
}
50
 
 
51
 
void ReadRecord::init_read_record_idx(Session *, 
52
 
                                      Table *table_arg,
53
 
                                      bool print_error_arg, 
54
 
                                      uint32_t idx)
55
 
{
56
 
  table_arg->emptyRecord();
57
 
  table= table_arg;
58
 
  cursor=  table->cursor;
59
 
  record= table->getInsertRecord();
60
 
  print_error= print_error_arg;
 
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
 
 
54
void init_read_record_idx(READ_RECORD *info,
 
55
                          THD *thd __attribute__((unused)),
 
56
                          Table *table,
 
57
                          bool print_error, uint32_t idx)
 
58
{
 
59
  empty_record(table);
 
60
  memset(info, 0, sizeof(*info));
 
61
  info->table= table;
 
62
  info->file=  table->file;
 
63
  info->record= table->record[0];
 
64
  info->print_error= print_error;
61
65
 
62
66
  table->status=0;                      /* And it's always found */
63
 
  if (not table->cursor->inited)
64
 
    table->cursor->startIndexScan(idx, 1);
 
67
  if (!table->file->inited)
 
68
    table->file->ha_index_init(idx, 1);
65
69
  /* read_record will be changed to rr_index in rr_index_first */
66
 
  read_record= rr_index_first;
 
70
  info->read_record= rr_index_first;
67
71
}
68
72
 
69
73
 
70
 
void ReadRecord::init_read_record(Session *session_arg, 
71
 
                                  Table *table_arg,
72
 
                                  optimizer::SqlSelect *select_arg,
73
 
                                  int use_record_cache, 
74
 
                                  bool print_error_arg)
 
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
*/
 
142
void init_read_record(READ_RECORD *info,THD *thd, Table *table,
 
143
                      SQL_SELECT *select,
 
144
                      int use_record_cache, bool print_error)
75
145
{
76
 
  internal::IO_CACHE *tempfile;
77
 
 
78
 
  session= session_arg;
79
 
  table= table_arg;
80
 
  cursor= table->cursor;
81
 
  forms= &table;                /* Only one table */
82
 
 
 
146
  IO_CACHE *tempfile;
 
147
 
 
148
  memset(info, 0, sizeof(*info));
 
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
    table->file->extra(HA_EXTRA_MMAP);
 
157
  
83
158
  if (table->sort.addon_field)
84
159
  {
85
 
    rec_buf= table->sort.addon_buf;
86
 
    ref_length= table->sort.addon_length;
 
160
    info->rec_buf= table->sort.addon_buf;
 
161
    info->ref_length= table->sort.addon_length;
87
162
  }
88
163
  else
89
164
  {
90
 
    table->emptyRecord();
91
 
    record= table->getInsertRecord();
92
 
    ref_length= table->cursor->ref_length;
 
165
    empty_record(table);
 
166
    info->record= table->record[0];
 
167
    info->ref_length= table->file->ref_length;
93
168
  }
94
 
  select= select_arg;
95
 
  print_error= print_error_arg;
96
 
  ignore_not_found_rows= 0;
 
169
  info->select=select;
 
170
  info->print_error=print_error;
 
171
  info->ignore_not_found_rows= 0;
97
172
  table->status=0;                      /* And it's always found */
98
173
 
99
 
  if (select && my_b_inited(select->file))
100
 
  {
101
 
    tempfile= select->file;
102
 
  }
 
174
  if (select && my_b_inited(&select->file))
 
175
    tempfile= &select->file;
103
176
  else
104
 
  {
105
177
    tempfile= table->sort.io_cache;
106
 
  }
107
 
 
108
178
  if (tempfile && my_b_inited(tempfile)) // Test if ref-records was used
109
179
  {
110
 
    read_record= (table->sort.addon_field ?
111
 
                  rr_unpack_from_tempfile : rr_from_tempfile);
112
 
 
113
 
    io_cache=tempfile;
114
 
    io_cache->reinit_io_cache(internal::READ_CACHE,0L,0,0);
115
 
    ref_pos=table->cursor->ref;
116
 
    if (!table->cursor->inited)
117
 
      table->cursor->startTableScan(0);
 
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);
118
187
 
119
188
    /*
120
189
      table->sort.addon_field is checked because if we use addon fields,
122
191
      and table->sort.io_cache is read sequentially
123
192
    */
124
193
    if (!table->sort.addon_field &&
125
 
        session->variables.read_rnd_buff_size &&
126
 
        !(table->cursor->getEngine()->check_flag(HTON_BIT_FAST_KEY_READ)) &&
127
 
        (table->db_stat & HA_READ_ONLY ||
128
 
        table->reginfo.lock_type <= TL_READ_NO_INSERT) &&
129
 
        (uint64_t) table->getShare()->getRecordLength() * (table->cursor->stats.records+
130
 
                                                table->cursor->stats.deleted) >
131
 
        (uint64_t) MIN_FILE_LENGTH_TO_USE_ROW_CACHE &&
132
 
        io_cache->end_of_file/ref_length * table->getShare()->getRecordLength() >
133
 
        (internal::my_off_t) MIN_ROWS_TO_USE_TABLE_CACHE &&
134
 
        !table->getShare()->blob_fields &&
135
 
        ref_length <= MAX_REFLENGTH)
 
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) &&
 
198
        (uint64_t) table->s->reclength* (table->file->stats.records+
 
199
                                          table->file->stats.deleted) >
 
200
        (uint64_t) MIN_FILE_LENGTH_TO_USE_ROW_CACHE &&
 
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)
136
205
    {
137
 
      if (init_rr_cache())
 
206
      if (! init_rr_cache(thd, info))
138
207
      {
139
 
        read_record= rr_from_cache;
 
208
        info->read_record=rr_from_cache;
140
209
      }
141
210
    }
142
211
  }
143
212
  else if (select && select->quick)
144
213
  {
145
 
    read_record= rr_quick;
 
214
    info->read_record=rr_quick;
146
215
  }
147
216
  else if (table->sort.record_pointers)
148
217
  {
149
 
    table->cursor->startTableScan(0);
150
 
    cache_pos=table->sort.record_pointers;
151
 
    cache_end= cache_pos+ table->sort.found_records * ref_length;
152
 
    read_record= (table->sort.addon_field ?  rr_unpack_from_buffer : rr_from_pointers);
 
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);
153
224
  }
154
225
  else
155
226
  {
156
 
    read_record= rr_sequential;
157
 
    table->cursor->startTableScan(1);
 
227
    info->read_record=rr_sequential;
 
228
    table->file->ha_rnd_init(1);
158
229
    /* We can use record cache if we don't update dynamic length tables */
159
230
    if (!table->no_cache &&
160
 
        (use_record_cache > 0 ||
161
 
        (int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS ||
162
 
        !(table->getShare()->db_options_in_use & HA_OPTION_PACK_RECORD)))
163
 
    {
164
 
      table->cursor->extra_opt(HA_EXTRA_CACHE, session->variables.read_buff_size);
165
 
    }
 
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
      table->file->extra_opt(HA_EXTRA_CACHE, thd->variables.read_buff_size);
166
237
  }
 
238
  /* 
 
239
    Do condition pushdown for UPDATE/DELETE.
 
240
    TODO: Remove this from here as it causes two condition pushdown calls 
 
241
    when we're running a SELECT and the condition cannot be pushed down.
 
242
  */
 
243
  if (thd->variables.engine_condition_pushdown && 
 
244
      select && select->cond && 
 
245
      (select->cond->used_tables() & table->map) &&
 
246
      !table->file->pushed_cond)
 
247
    table->file->cond_push(select->cond);
167
248
 
168
249
  return;
169
250
} /* init_read_record */
170
251
 
171
252
 
172
 
void ReadRecord::end_read_record()
 
253
 
 
254
void end_read_record(READ_RECORD *info)
173
255
{                   /* free cache if used */
174
 
  if (cache)
 
256
  if (info->cache)
175
257
  {
176
 
    global_read_rnd_buffer.sub(session->variables.read_rnd_buff_size);
177
 
    free((char*) cache);
178
 
    cache= NULL;
 
258
    free((char*) info->cache);
 
259
    info->cache=0;
179
260
  }
180
 
  if (table)
 
261
  if (info->table)
181
262
  {
182
 
    table->filesort_free_buffers();
183
 
    (void) cursor->extra(HA_EXTRA_NO_CACHE);
184
 
    if (read_record != rr_quick) // otherwise quick_range does it
185
 
      (void) cursor->ha_index_or_rnd_end();
186
 
 
187
 
    table= NULL;
 
263
    filesort_free_buffers(info->table,0);
 
264
    (void) info->file->extra(HA_EXTRA_NO_CACHE);
 
265
    if (info->read_record != rr_quick) // otherwise quick_range does it
 
266
      (void) info->file->ha_index_or_rnd_end();
 
267
    info->table=0;
188
268
  }
189
269
}
190
270
 
191
 
static int rr_handle_error(ReadRecord *info, int error)
 
271
static int rr_handle_error(READ_RECORD *info, int error)
192
272
{
193
273
  if (error == HA_ERR_END_OF_FILE)
194
274
    error= -1;
195
275
  else
196
276
  {
197
277
    if (info->print_error)
198
 
      info->table->print_error(error, MYF(0));
 
278
      info->table->file->print_error(error, MYF(0));
199
279
    if (error < 0)                            // Fix negative BDB errno
200
280
      error= 1;
201
281
  }
202
282
  return error;
203
283
}
204
284
 
 
285
 
205
286
/** Read a record from head-database. */
206
 
static int rr_quick(ReadRecord *info)
 
287
 
 
288
static int rr_quick(READ_RECORD *info)
207
289
{
208
290
  int tmp;
209
291
  while ((tmp= info->select->quick->get_next()))
210
292
  {
211
 
    if (info->session->getKilled())
 
293
    if (info->thd->killed)
212
294
    {
213
295
      my_error(ER_SERVER_SHUTDOWN, MYF(0));
214
296
      return 1;
219
301
      break;
220
302
    }
221
303
  }
222
 
 
223
304
  return tmp;
224
305
}
225
306
 
 
307
 
226
308
/**
227
309
  Reads first row in an index scan.
228
310
 
235
317
  @retval
236
318
    1   Error
237
319
*/
238
 
static int rr_index_first(ReadRecord *info)
 
320
 
 
321
static int rr_index_first(READ_RECORD *info)
239
322
{
240
 
  int tmp= info->cursor->index_first(info->record);
 
323
  int tmp= info->file->index_first(info->record);
241
324
  info->read_record= rr_index;
242
325
  if (tmp)
243
326
    tmp= rr_handle_error(info, tmp);
244
327
  return tmp;
245
328
}
246
329
 
 
330
 
247
331
/**
248
332
  Reads index sequentially after first row.
249
333
 
259
343
  @retval
260
344
    1   Error
261
345
*/
262
 
static int rr_index(ReadRecord *info)
 
346
 
 
347
static int rr_index(READ_RECORD *info)
263
348
{
264
 
  int tmp= info->cursor->index_next(info->record);
 
349
  int tmp= info->file->index_next(info->record);
265
350
  if (tmp)
266
351
    tmp= rr_handle_error(info, tmp);
267
352
  return tmp;
268
353
}
269
354
 
270
 
int rr_sequential(ReadRecord *info)
 
355
 
 
356
int rr_sequential(READ_RECORD *info)
271
357
{
272
358
  int tmp;
273
 
  while ((tmp= info->cursor->rnd_next(info->record)))
 
359
  while ((tmp=info->file->rnd_next(info->record)))
274
360
  {
275
 
    if (info->session->getKilled())
 
361
    if (info->thd->killed)
276
362
    {
277
 
      info->session->send_kill_message();
 
363
      info->thd->send_kill_message();
278
364
      return 1;
279
365
    }
280
366
    /*
281
 
      TODO> Fix this so that engine knows how to behave on its own.
282
367
      rnd_next can return RECORD_DELETED for MyISAM when one thread is
283
368
      reading and another deleting without locks.
284
369
    */
288
373
      break;
289
374
    }
290
375
  }
291
 
 
292
376
  return tmp;
293
377
}
294
378
 
295
 
static int rr_from_tempfile(ReadRecord *info)
 
379
 
 
380
static int rr_from_tempfile(READ_RECORD *info)
296
381
{
297
382
  int tmp;
298
383
  for (;;)
299
384
  {
300
385
    if (my_b_read(info->io_cache,info->ref_pos,info->ref_length))
301
 
      return -1;                                        /* End of cursor */
302
 
    if (!(tmp=info->cursor->rnd_pos(info->record,info->ref_pos)))
 
386
      return -1;                                        /* End of file */
 
387
    if (!(tmp=info->file->rnd_pos(info->record,info->ref_pos)))
303
388
      break;
304
389
    /* The following is extremely unlikely to happen */
305
390
    if (tmp == HA_ERR_RECORD_DELETED ||
311
396
  return tmp;
312
397
} /* rr_from_tempfile */
313
398
 
 
399
 
314
400
/**
315
 
  Read a result set record from a temporary cursor after sorting.
 
401
  Read a result set record from a temporary file after sorting.
316
402
 
317
 
  The function first reads the next sorted record from the temporary cursor.
318
 
  into a buffer. If a success it calls a callback function that unpacks
 
403
  The function first reads the next sorted record from the temporary file.
 
404
  into a buffer. If a success it calls a callback function that unpacks 
319
405
  the fields values use in the result set from this buffer into their
320
406
  positions in the regular record buffer.
321
407
 
326
412
  @retval
327
413
    -1   There is no record to be read anymore.
328
414
*/
329
 
static int rr_unpack_from_tempfile(ReadRecord *info)
 
415
 
 
416
static int rr_unpack_from_tempfile(READ_RECORD *info)
330
417
{
331
418
  if (my_b_read(info->io_cache, info->rec_buf, info->ref_length))
332
419
    return -1;
336
423
  return 0;
337
424
}
338
425
 
339
 
static int rr_from_pointers(ReadRecord *info)
 
426
static int rr_from_pointers(READ_RECORD *info)
340
427
{
341
428
  int tmp;
342
429
  unsigned char *cache_pos;
343
430
 
344
 
 
345
431
  for (;;)
346
432
  {
347
433
    if (info->cache_pos == info->cache_end)
348
 
      return -1;                                        /* End of cursor */
 
434
      return -1;                                        /* End of file */
349
435
    cache_pos= info->cache_pos;
350
436
    info->cache_pos+= info->ref_length;
351
437
 
352
 
    if (!(tmp=info->cursor->rnd_pos(info->record,cache_pos)))
 
438
    if (!(tmp=info->file->rnd_pos(info->record,cache_pos)))
353
439
      break;
354
440
 
355
441
    /* The following is extremely unlikely to happen */
366
452
  Read a result set record from a buffer after sorting.
367
453
 
368
454
  The function first reads the next sorted record from the sort buffer.
369
 
  If a success it calls a callback function that unpacks
 
455
  If a success it calls a callback function that unpacks 
370
456
  the fields values use in the result set from this buffer into their
371
457
  positions in the regular record buffer.
372
458
 
377
463
  @retval
378
464
    -1   There is no record to be read anymore.
379
465
*/
380
 
static int rr_unpack_from_buffer(ReadRecord *info)
 
466
 
 
467
static int rr_unpack_from_buffer(READ_RECORD *info)
381
468
{
382
469
  if (info->cache_pos == info->cache_end)
383
470
    return -1;                      /* End of buffer */
387
474
 
388
475
  return 0;
389
476
}
 
477
        /* cacheing of records from a database */
390
478
 
391
 
/* cacheing of records from a database */
392
 
bool ReadRecord::init_rr_cache()
 
479
static int init_rr_cache(THD *thd, READ_RECORD *info)
393
480
{
394
 
  uint32_t local_rec_cache_size;
395
 
 
396
 
  struct_length= 3 + MAX_REFLENGTH;
397
 
  reclength= ALIGN_SIZE(table->getShare()->getRecordLength() + 1);
398
 
  if (reclength < struct_length)
399
 
    reclength= ALIGN_SIZE(struct_length);
400
 
 
401
 
  error_offset= table->getShare()->getRecordLength();
402
 
  cache_records= (session->variables.read_rnd_buff_size /
403
 
                        (reclength + struct_length));
404
 
  local_rec_cache_size= cache_records * reclength;
405
 
  rec_cache_size= cache_records * ref_length;
406
 
 
407
 
  if (not global_read_rnd_buffer.add(session->variables.read_rnd_buff_size))
408
 
  {
409
 
    my_error(ER_OUT_OF_GLOBAL_READRNDMEMORY, MYF(ME_ERROR+ME_WAITTANG));
410
 
    return false;
411
 
  }
 
481
  uint32_t rec_cache_size;
 
482
 
 
483
  info->struct_length= 3+MAX_REFLENGTH;
 
484
  info->reclength= ALIGN_SIZE(info->table->s->reclength+1);
 
485
  if (info->reclength < info->struct_length)
 
486
    info->reclength= ALIGN_SIZE(info->struct_length);
 
487
 
 
488
  info->error_offset= info->table->s->reclength;
 
489
  info->cache_records= (thd->variables.read_rnd_buff_size /
 
490
                        (info->reclength+info->struct_length));
 
491
  rec_cache_size= info->cache_records*info->reclength;
 
492
  info->rec_cache_size= info->cache_records*info->ref_length;
412
493
 
413
494
  // We have to allocate one more byte to use uint3korr (see comments for it)
414
 
  if (cache_records <= 2 ||
415
 
      !(cache=(unsigned char*) malloc(local_rec_cache_size + cache_records * struct_length + 1)))
416
 
  {
417
 
    return false;
418
 
  }
419
 
#ifdef HAVE_VALGRIND
 
495
  if (info->cache_records <= 2 ||
 
496
      !(info->cache=(unsigned char*) my_malloc_lock(rec_cache_size+info->cache_records*
 
497
                                           info->struct_length+1,
 
498
                                           MYF(0))))
 
499
    return(1);
 
500
#ifdef HAVE_purify
420
501
  // Avoid warnings in qsort
421
 
  memset(cache, 0, local_rec_cache_size + cache_records * struct_length + 1);
 
502
  memset(info->cache, 0,
 
503
         rec_cache_size+info->cache_records* info->struct_length+1);
422
504
#endif
423
 
  read_positions= cache + local_rec_cache_size;
424
 
  cache_pos= cache_end= cache;
425
 
 
426
 
  return true;
 
505
  info->read_positions=info->cache+rec_cache_size;
 
506
  info->cache_pos=info->cache_end=info->cache;
 
507
  return(0);
427
508
} /* init_rr_cache */
428
509
 
429
 
static int rr_from_cache(ReadRecord *info)
 
510
 
 
511
static int rr_from_cache(READ_RECORD *info)
430
512
{
 
513
  register uint32_t i;
431
514
  uint32_t length;
432
 
  internal::my_off_t rest_of_file;
 
515
  my_off_t rest_of_file;
433
516
  int16_t error;
434
517
  unsigned char *position,*ref_position,*record_pos;
435
518
  uint32_t record;
440
523
    {
441
524
      if (info->cache_pos[info->error_offset])
442
525
      {
443
 
        shortget(error,info->cache_pos);
444
 
        if (info->print_error)
445
 
          info->table->print_error(error,MYF(0));
 
526
        shortget(error,info->cache_pos);
 
527
        if (info->print_error)
 
528
          info->table->file->print_error(error,MYF(0));
446
529
      }
447
530
      else
448
531
      {
449
 
        error=0;
450
 
        memcpy(info->record,info->cache_pos, (size_t) info->table->getShare()->getRecordLength());
 
532
        error=0;
 
533
        memcpy(info->record,info->cache_pos,
 
534
               (size_t) info->table->s->reclength);
451
535
      }
452
 
      info->cache_pos+= info->reclength;
 
536
      info->cache_pos+=info->reclength;
453
537
      return ((int) error);
454
538
    }
455
539
    length=info->rec_cache_size;
456
 
    rest_of_file= info->io_cache->end_of_file - my_b_tell(info->io_cache);
457
 
    if ((internal::my_off_t) length > rest_of_file)
458
 
    {
 
540
    rest_of_file=info->io_cache->end_of_file - my_b_tell(info->io_cache);
 
541
    if ((my_off_t) length > rest_of_file)
459
542
      length= (uint32_t) rest_of_file;
460
 
    }
461
 
 
462
 
    if (!length || my_b_read(info->io_cache, info->getCache(), length))
 
543
    if (!length || my_b_read(info->io_cache,info->cache,length))
463
544
    {
464
 
      return -1;                        /* End of cursor */
 
545
      return -1;                        /* End of file */
465
546
    }
466
547
 
467
548
    length/=info->ref_length;
468
 
    position=info->getCache();
 
549
    position=info->cache;
469
550
    ref_position=info->read_positions;
470
 
    for (uint32_t i= 0 ; i < length ; i++,position+=info->ref_length)
 
551
    for (i=0 ; i < length ; i++,position+=info->ref_length)
471
552
    {
472
553
      memcpy(ref_position,position,(size_t) info->ref_length);
473
554
      ref_position+=MAX_REFLENGTH;
474
555
      int3store(ref_position,(long) i);
475
556
      ref_position+=3;
476
557
    }
477
 
    internal::my_qsort(info->read_positions, length, info->struct_length,
478
 
                       (qsort_cmp) rr_cmp);
 
558
    my_qsort(info->read_positions, length, info->struct_length,
 
559
             (qsort_cmp) rr_cmp);
479
560
 
480
561
    position=info->read_positions;
481
 
    for (uint32_t i= 0 ; i < length ; i++)
 
562
    for (i=0 ; i < length ; i++)
482
563
    {
483
 
      memcpy(info->ref_pos, position, (size_t)info->ref_length);
 
564
      memcpy(info->ref_pos,position,(size_t) info->ref_length);
484
565
      position+=MAX_REFLENGTH;
485
566
      record=uint3korr(position);
486
567
      position+=3;
487
 
      record_pos= info->getCache() + record * info->reclength;
488
 
      if ((error=(int16_t) info->cursor->rnd_pos(record_pos,info->ref_pos)))
 
568
      record_pos=info->cache+record*info->reclength;
 
569
      if ((error=(int16_t) info->file->rnd_pos(record_pos,info->ref_pos)))
489
570
      {
490
 
        record_pos[info->error_offset]=1;
491
 
        shortstore(record_pos,error);
 
571
        record_pos[info->error_offset]=1;
 
572
        shortstore(record_pos,error);
492
573
      }
493
574
      else
494
 
        record_pos[info->error_offset]=0;
 
575
        record_pos[info->error_offset]=0;
495
576
    }
496
 
    info->cache_end= (info->cache_pos= info->getCache())+length*info->reclength;
 
577
    info->cache_end=(info->cache_pos=info->cache)+length*info->reclength;
497
578
  }
498
579
} /* rr_from_cache */
499
580
 
 
581
 
500
582
static int rr_cmp(unsigned char *a,unsigned char *b)
501
583
{
502
584
  if (a[0] != b[0])
519
601
  return (int) a[7] - (int) b[7];
520
602
#endif
521
603
}
522
 
 
523
 
} /* namespace drizzled */