~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/records.cc

  • Committer: Eric Lambert
  • Date: 2009-06-11 21:19:36 UTC
  • mto: (1061.1.1 merge-all)
  • mto: This revision was merged to the branch mainline in revision 1062.
  • Revision ID: eric.d.lambert@gmail.com-20090611211936-g2gij43xakupz1dw
-removed rm_dir_w_symlink method call with rmdir since dir should not be a sym link in the first place.

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
  @file
19
19
  @brief
20
20
  Functions for easy reading of records, possible through a cache
21
21
*/
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);
 
22
#include <drizzled/server_includes.h>
 
23
#include <drizzled/error.h>
 
24
#include <drizzled/table.h>
 
25
#include <drizzled/session.h>
 
26
 
 
27
int rr_sequential(READ_RECORD *info);
 
28
static int rr_quick(READ_RECORD *info);
 
29
static int rr_from_tempfile(READ_RECORD *info);
 
30
static int rr_unpack_from_tempfile(READ_RECORD *info);
 
31
static int rr_unpack_from_buffer(READ_RECORD *info);
 
32
static int rr_from_pointers(READ_RECORD *info);
 
33
static int rr_from_cache(READ_RECORD *info);
 
34
static int init_rr_cache(Session *session, READ_RECORD *info);
42
35
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;
 
36
static int rr_index_first(READ_RECORD *info);
 
37
static int rr_index(READ_RECORD *info);
 
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 session          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
void init_read_record_idx(READ_RECORD *info, 
 
55
                          Session *, 
 
56
                          Table *table,
 
57
                          bool print_error, 
 
58
                          uint32_t idx)
 
59
{
 
60
  table->emptyRecord();
 
61
  memset(info, 0, sizeof(*info));
 
62
  info->table= table;
 
63
  info->file=  table->file;
 
64
  info->record= table->record[0];
 
65
  info->print_error= print_error;
61
66
 
62
67
  table->status=0;                      /* And it's always found */
63
 
  if (not table->cursor->inited)
64
 
    table->cursor->startIndexScan(idx, 1);
 
68
  if (!table->file->inited)
 
69
    table->file->ha_index_init(idx, 1);
65
70
  /* read_record will be changed to rr_index in rr_index_first */
66
 
  read_record= rr_index_first;
 
71
  info->read_record= rr_index_first;
67
72
}
68
73
 
69
 
 
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,
 
143
                      Session *session, 
 
144
                      Table *table,
 
145
                                  SQL_SELECT *select,
 
146
                                  int use_record_cache, 
 
147
                      bool print_error)
75
148
{
76
 
  internal::IO_CACHE *tempfile;
77
 
 
78
 
  session= session_arg;
79
 
  table= table_arg;
80
 
  cursor= table->cursor;
81
 
  forms= &table;                /* Only one table */
 
149
  IO_CACHE *tempfile;
 
150
 
 
151
  memset(info, 0, sizeof(*info));
 
152
  info->session=session;
 
153
  info->table=table;
 
154
  info->file= table->file;
 
155
  info->forms= &info->table;            /* Only one table */
 
156
 
 
157
  if (table->s->tmp_table == NON_TRANSACTIONAL_TMP_TABLE &&
 
158
      !table->sort.addon_field)
 
159
    table->file->extra(HA_EXTRA_MMAP);
82
160
 
83
161
  if (table->sort.addon_field)
84
162
  {
85
 
    rec_buf= table->sort.addon_buf;
86
 
    ref_length= table->sort.addon_length;
 
163
    info->rec_buf= table->sort.addon_buf;
 
164
    info->ref_length= table->sort.addon_length;
87
165
  }
88
166
  else
89
167
  {
90
168
    table->emptyRecord();
91
 
    record= table->getInsertRecord();
92
 
    ref_length= table->cursor->ref_length;
 
169
    info->record= table->record[0];
 
170
    info->ref_length= table->file->ref_length;
93
171
  }
94
 
  select= select_arg;
95
 
  print_error= print_error_arg;
96
 
  ignore_not_found_rows= 0;
 
172
  info->select=select;
 
173
  info->print_error=print_error;
 
174
  info->ignore_not_found_rows= 0;
97
175
  table->status=0;                      /* And it's always found */
98
176
 
99
 
  if (select && my_b_inited(select->file))
100
 
  {
101
 
    tempfile= select->file;
102
 
  }
 
177
  if (select && my_b_inited(&select->file))
 
178
    tempfile= &select->file;
103
179
  else
104
 
  {
105
180
    tempfile= table->sort.io_cache;
106
 
  }
107
 
 
108
181
  if (tempfile && my_b_inited(tempfile)) // Test if ref-records was used
109
182
  {
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);
 
183
    info->read_record= (table->sort.addon_field ?
 
184
                        rr_unpack_from_tempfile : rr_from_tempfile);
 
185
    info->io_cache=tempfile;
 
186
    reinit_io_cache(info->io_cache,READ_CACHE,0L,0,0);
 
187
    info->ref_pos=table->file->ref;
 
188
    if (!table->file->inited)
 
189
      table->file->ha_rnd_init(0);
118
190
 
119
191
    /*
120
192
      table->sort.addon_field is checked because if we use addon fields,
123
195
    */
124
196
    if (!table->sort.addon_field &&
125
197
        session->variables.read_rnd_buff_size &&
126
 
        !(table->cursor->getEngine()->check_flag(HTON_BIT_FAST_KEY_READ)) &&
 
198
        !(table->file->ha_table_flags() & HA_FAST_KEY_READ) &&
127
199
        (table->db_stat & HA_READ_ONLY ||
128
200
        table->reginfo.lock_type <= TL_READ_NO_INSERT) &&
129
 
        (uint64_t) table->getShare()->getRecordLength() * (table->cursor->stats.records+
130
 
                                                table->cursor->stats.deleted) >
 
201
        (uint64_t) table->s->reclength* (table->file->stats.records+
 
202
                                                table->file->stats.deleted) >
131
203
        (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)
 
204
        info->io_cache->end_of_file/info->ref_length * table->s->reclength >
 
205
        (my_off_t) MIN_ROWS_TO_USE_TABLE_CACHE &&
 
206
        !table->s->blob_fields &&
 
207
        info->ref_length <= MAX_REFLENGTH)
136
208
    {
137
 
      if (init_rr_cache())
 
209
      if (! init_rr_cache(session, info))
138
210
      {
139
 
        read_record= rr_from_cache;
 
211
        info->read_record=rr_from_cache;
140
212
      }
141
213
    }
142
214
  }
143
215
  else if (select && select->quick)
144
216
  {
145
 
    read_record= rr_quick;
 
217
    info->read_record=rr_quick;
146
218
  }
147
219
  else if (table->sort.record_pointers)
148
220
  {
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);
 
221
    table->file->ha_rnd_init(0);
 
222
    info->cache_pos=table->sort.record_pointers;
 
223
    info->cache_end=info->cache_pos+
 
224
                    table->sort.found_records*info->ref_length;
 
225
    info->read_record= (table->sort.addon_field ?
 
226
                        rr_unpack_from_buffer : rr_from_pointers);
153
227
  }
154
228
  else
155
229
  {
156
 
    read_record= rr_sequential;
157
 
    table->cursor->startTableScan(1);
 
230
    info->read_record=rr_sequential;
 
231
    table->file->ha_rnd_init(1);
158
232
    /* We can use record cache if we don't update dynamic length tables */
159
233
    if (!table->no_cache &&
160
234
        (use_record_cache > 0 ||
161
235
        (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
 
    }
 
236
        !(table->s->db_options_in_use & HA_OPTION_PACK_RECORD) ||
 
237
        (use_record_cache < 0 &&
 
238
          !(table->file->ha_table_flags() & HA_NOT_DELETE_WITH_CACHE))))
 
239
      table->file->extra_opt(HA_EXTRA_CACHE, session->variables.read_buff_size);
166
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 (session->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);
167
251
 
168
252
  return;
169
253
} /* init_read_record */
170
254
 
171
 
 
172
 
void ReadRecord::end_read_record()
 
255
void end_read_record(READ_RECORD *info)
173
256
{                   /* free cache if used */
174
 
  if (cache)
 
257
  if (info->cache)
175
258
  {
176
 
    global_read_rnd_buffer.sub(session->variables.read_rnd_buff_size);
177
 
    free((char*) cache);
178
 
    cache= NULL;
 
259
    free((char*) info->cache);
 
260
    info->cache=0;
179
261
  }
180
 
  if (table)
 
262
  if (info->table)
181
263
  {
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;
 
264
    filesort_free_buffers(info->table,0);
 
265
    (void) info->file->extra(HA_EXTRA_NO_CACHE);
 
266
    if (info->read_record != rr_quick) // otherwise quick_range does it
 
267
      (void) info->file->ha_index_or_rnd_end();
 
268
    info->table=0;
188
269
  }
189
270
}
190
271
 
191
 
static int rr_handle_error(ReadRecord *info, int error)
 
272
static int rr_handle_error(READ_RECORD *info, int error)
192
273
{
193
274
  if (error == HA_ERR_END_OF_FILE)
194
275
    error= -1;
195
276
  else
196
277
  {
197
278
    if (info->print_error)
198
 
      info->table->print_error(error, MYF(0));
 
279
      info->table->file->print_error(error, MYF(0));
199
280
    if (error < 0)                            // Fix negative BDB errno
200
281
      error= 1;
201
282
  }
203
284
}
204
285
 
205
286
/** Read a record from head-database. */
206
 
static int rr_quick(ReadRecord *info)
 
287
static int rr_quick(READ_RECORD *info)
207
288
{
208
289
  int tmp;
209
290
  while ((tmp= info->select->quick->get_next()))
210
291
  {
211
 
    if (info->session->getKilled())
 
292
    if (info->session->killed)
212
293
    {
213
294
      my_error(ER_SERVER_SHUTDOWN, MYF(0));
214
295
      return 1;
235
316
  @retval
236
317
    1   Error
237
318
*/
238
 
static int rr_index_first(ReadRecord *info)
 
319
static int rr_index_first(READ_RECORD *info)
239
320
{
240
 
  int tmp= info->cursor->index_first(info->record);
 
321
  int tmp= info->file->index_first(info->record);
241
322
  info->read_record= rr_index;
242
323
  if (tmp)
243
324
    tmp= rr_handle_error(info, tmp);
259
340
  @retval
260
341
    1   Error
261
342
*/
262
 
static int rr_index(ReadRecord *info)
 
343
static int rr_index(READ_RECORD *info)
263
344
{
264
 
  int tmp= info->cursor->index_next(info->record);
 
345
  int tmp= info->file->index_next(info->record);
265
346
  if (tmp)
266
347
    tmp= rr_handle_error(info, tmp);
267
348
  return tmp;
268
349
}
269
350
 
270
 
int rr_sequential(ReadRecord *info)
 
351
int rr_sequential(READ_RECORD *info)
271
352
{
272
353
  int tmp;
273
 
  while ((tmp= info->cursor->rnd_next(info->record)))
 
354
  while ((tmp= info->file->rnd_next(info->record)))
274
355
  {
275
 
    if (info->session->getKilled())
 
356
    if (info->session->killed)
276
357
    {
277
358
      info->session->send_kill_message();
278
359
      return 1;
292
373
  return tmp;
293
374
}
294
375
 
295
 
static int rr_from_tempfile(ReadRecord *info)
 
376
static int rr_from_tempfile(READ_RECORD *info)
296
377
{
297
378
  int tmp;
298
379
  for (;;)
299
380
  {
300
381
    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)))
 
382
      return -1;                                        /* End of file */
 
383
    if (!(tmp=info->file->rnd_pos(info->record,info->ref_pos)))
303
384
      break;
304
385
    /* The following is extremely unlikely to happen */
305
386
    if (tmp == HA_ERR_RECORD_DELETED ||
312
393
} /* rr_from_tempfile */
313
394
 
314
395
/**
315
 
  Read a result set record from a temporary cursor after sorting.
 
396
  Read a result set record from a temporary file after sorting.
316
397
 
317
 
  The function first reads the next sorted record from the temporary cursor.
 
398
  The function first reads the next sorted record from the temporary file.
318
399
  into a buffer. If a success it calls a callback function that unpacks
319
400
  the fields values use in the result set from this buffer into their
320
401
  positions in the regular record buffer.
326
407
  @retval
327
408
    -1   There is no record to be read anymore.
328
409
*/
329
 
static int rr_unpack_from_tempfile(ReadRecord *info)
 
410
static int rr_unpack_from_tempfile(READ_RECORD *info)
330
411
{
331
412
  if (my_b_read(info->io_cache, info->rec_buf, info->ref_length))
332
413
    return -1;
336
417
  return 0;
337
418
}
338
419
 
339
 
static int rr_from_pointers(ReadRecord *info)
 
420
static int rr_from_pointers(READ_RECORD *info)
340
421
{
341
422
  int tmp;
342
423
  unsigned char *cache_pos;
343
424
 
344
 
 
345
425
  for (;;)
346
426
  {
347
427
    if (info->cache_pos == info->cache_end)
348
 
      return -1;                                        /* End of cursor */
 
428
      return -1;                                        /* End of file */
349
429
    cache_pos= info->cache_pos;
350
430
    info->cache_pos+= info->ref_length;
351
431
 
352
 
    if (!(tmp=info->cursor->rnd_pos(info->record,cache_pos)))
 
432
    if (!(tmp=info->file->rnd_pos(info->record,cache_pos)))
353
433
      break;
354
434
 
355
435
    /* The following is extremely unlikely to happen */
377
457
  @retval
378
458
    -1   There is no record to be read anymore.
379
459
*/
380
 
static int rr_unpack_from_buffer(ReadRecord *info)
 
460
static int rr_unpack_from_buffer(READ_RECORD *info)
381
461
{
382
462
  if (info->cache_pos == info->cache_end)
383
463
    return -1;                      /* End of buffer */
389
469
}
390
470
 
391
471
/* cacheing of records from a database */
392
 
bool ReadRecord::init_rr_cache()
 
472
static int init_rr_cache(Session *session, READ_RECORD *info)
393
473
{
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
 
  }
 
474
  uint32_t rec_cache_size;
 
475
 
 
476
  info->struct_length= 3+MAX_REFLENGTH;
 
477
  info->reclength= ALIGN_SIZE(info->table->s->reclength+1);
 
478
  if (info->reclength < info->struct_length)
 
479
    info->reclength= ALIGN_SIZE(info->struct_length);
 
480
 
 
481
  info->error_offset= info->table->s->reclength;
 
482
  info->cache_records= (session->variables.read_rnd_buff_size /
 
483
                        (info->reclength+info->struct_length));
 
484
  rec_cache_size= info->cache_records*info->reclength;
 
485
  info->rec_cache_size= info->cache_records*info->ref_length;
412
486
 
413
487
  // 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
 
488
  if (info->cache_records <= 2 ||
 
489
      !(info->cache=(unsigned char*) malloc(rec_cache_size+info->cache_records*
 
490
                                            info->struct_length+1)))
 
491
    return(1);
 
492
#ifdef HAVE_purify
420
493
  // Avoid warnings in qsort
421
 
  memset(cache, 0, local_rec_cache_size + cache_records * struct_length + 1);
 
494
  memset(info->cache, 0,
 
495
         rec_cache_size+info->cache_records* info->struct_length+1);
422
496
#endif
423
 
  read_positions= cache + local_rec_cache_size;
424
 
  cache_pos= cache_end= cache;
425
 
 
426
 
  return true;
 
497
  info->read_positions=info->cache+rec_cache_size;
 
498
  info->cache_pos=info->cache_end=info->cache;
 
499
  return(0);
427
500
} /* init_rr_cache */
428
501
 
429
 
static int rr_from_cache(ReadRecord *info)
 
502
static int rr_from_cache(READ_RECORD *info)
430
503
{
 
504
  register uint32_t i;
431
505
  uint32_t length;
432
 
  internal::my_off_t rest_of_file;
 
506
  my_off_t rest_of_file;
433
507
  int16_t error;
434
508
  unsigned char *position,*ref_position,*record_pos;
435
509
  uint32_t record;
442
516
      {
443
517
        shortget(error,info->cache_pos);
444
518
        if (info->print_error)
445
 
          info->table->print_error(error,MYF(0));
 
519
          info->table->file->print_error(error,MYF(0));
446
520
      }
447
521
      else
448
522
      {
449
523
        error=0;
450
 
        memcpy(info->record,info->cache_pos, (size_t) info->table->getShare()->getRecordLength());
 
524
        memcpy(info->record,info->cache_pos, (size_t) info->table->s->reclength);
451
525
      }
452
 
      info->cache_pos+= info->reclength;
 
526
      info->cache_pos+=info->reclength;
453
527
      return ((int) error);
454
528
    }
455
529
    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
 
    {
 
530
    rest_of_file=info->io_cache->end_of_file - my_b_tell(info->io_cache);
 
531
    if ((my_off_t) length > rest_of_file)
459
532
      length= (uint32_t) rest_of_file;
460
 
    }
461
 
 
462
 
    if (!length || my_b_read(info->io_cache, info->getCache(), length))
 
533
    if (!length || my_b_read(info->io_cache,info->cache,length))
463
534
    {
464
 
      return -1;                        /* End of cursor */
 
535
      return -1;                        /* End of file */
465
536
    }
466
537
 
467
538
    length/=info->ref_length;
468
 
    position=info->getCache();
 
539
    position=info->cache;
469
540
    ref_position=info->read_positions;
470
 
    for (uint32_t i= 0 ; i < length ; i++,position+=info->ref_length)
 
541
    for (i=0 ; i < length ; i++,position+=info->ref_length)
471
542
    {
472
543
      memcpy(ref_position,position,(size_t) info->ref_length);
473
544
      ref_position+=MAX_REFLENGTH;
474
545
      int3store(ref_position,(long) i);
475
546
      ref_position+=3;
476
547
    }
477
 
    internal::my_qsort(info->read_positions, length, info->struct_length,
478
 
                       (qsort_cmp) rr_cmp);
 
548
    my_qsort(info->read_positions, length, info->struct_length,
 
549
             (qsort_cmp) rr_cmp);
479
550
 
480
551
    position=info->read_positions;
481
 
    for (uint32_t i= 0 ; i < length ; i++)
 
552
    for (i=0 ; i < length ; i++)
482
553
    {
483
 
      memcpy(info->ref_pos, position, (size_t)info->ref_length);
 
554
      memcpy(info->ref_pos,position,(size_t) info->ref_length);
484
555
      position+=MAX_REFLENGTH;
485
556
      record=uint3korr(position);
486
557
      position+=3;
487
 
      record_pos= info->getCache() + record * info->reclength;
488
 
      if ((error=(int16_t) info->cursor->rnd_pos(record_pos,info->ref_pos)))
 
558
      record_pos=info->cache+record*info->reclength;
 
559
      if ((error=(int16_t) info->file->rnd_pos(record_pos,info->ref_pos)))
489
560
      {
490
561
        record_pos[info->error_offset]=1;
491
562
        shortstore(record_pos,error);
493
564
      else
494
565
        record_pos[info->error_offset]=0;
495
566
    }
496
 
    info->cache_end= (info->cache_pos= info->getCache())+length*info->reclength;
 
567
    info->cache_end=(info->cache_pos=info->cache)+length*info->reclength;
497
568
  }
498
569
} /* rr_from_cache */
499
570
 
519
590
  return (int) a[7] - (int) b[7];
520
591
#endif
521
592
}
522
 
 
523
 
} /* namespace drizzled */