~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/records.cc

  • Committer: Stewart Smith
  • Date: 2009-10-08 12:39:27 UTC
  • mto: This revision was merged to the branch mainline in revision 1179.
  • Revision ID: stewart@flamingspork.com-20091008123927-qpf9hog04w4xc5aj
make directory_file_name() static to mysys/my_lib.cc

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
31
 
namespace drizzled
32
 
{
 
22
#include <drizzled/server_includes.h>
 
23
#include <drizzled/error.h>
 
24
#include <drizzled/table.h>
 
25
#include <drizzled/session.h>
33
26
 
34
27
int rr_sequential(READ_RECORD *info);
35
28
static int rr_quick(READ_RECORD *info);
43
36
static int rr_index_first(READ_RECORD *info);
44
37
static int rr_index(READ_RECORD *info);
45
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
*/
46
54
void init_read_record_idx(READ_RECORD *info, 
47
55
                          Session *, 
48
56
                          Table *table,
52
60
  table->emptyRecord();
53
61
  memset(info, 0, sizeof(*info));
54
62
  info->table= table;
55
 
  info->cursor=  table->cursor;
 
63
  info->file=  table->file;
56
64
  info->record= table->record[0];
57
65
  info->print_error= print_error;
58
66
 
59
67
  table->status=0;                      /* And it's always found */
60
 
  if (!table->cursor->inited)
61
 
    table->cursor->ha_index_init(idx, 1);
 
68
  if (!table->file->inited)
 
69
    table->file->ha_index_init(idx, 1);
62
70
  /* read_record will be changed to rr_index in rr_index_first */
63
71
  info->read_record= rr_index_first;
64
72
}
65
73
 
66
 
 
 
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
*/
67
142
void init_read_record(READ_RECORD *info,
68
143
                      Session *session, 
69
144
                      Table *table,
70
 
                      optimizer::SqlSelect *select,
71
 
                      int use_record_cache, 
 
145
                                  SQL_SELECT *select,
 
146
                                  int use_record_cache, 
72
147
                      bool print_error)
73
148
{
74
 
  internal::IO_CACHE *tempfile;
 
149
  IO_CACHE *tempfile;
75
150
 
76
151
  memset(info, 0, sizeof(*info));
77
152
  info->session=session;
78
153
  info->table=table;
79
 
  info->cursor= table->cursor;
 
154
  info->file= table->file;
80
155
  info->forms= &info->table;            /* Only one table */
81
156
 
82
157
  if (table->sort.addon_field)
88
163
  {
89
164
    table->emptyRecord();
90
165
    info->record= table->record[0];
91
 
    info->ref_length= table->cursor->ref_length;
 
166
    info->ref_length= table->file->ref_length;
92
167
  }
93
168
  info->select=select;
94
 
  info->print_error= print_error;
 
169
  info->print_error=print_error;
95
170
  info->ignore_not_found_rows= 0;
96
171
  table->status=0;                      /* And it's always found */
97
172
 
98
 
  if (select && my_b_inited(select->file))
99
 
    tempfile= select->file;
 
173
  if (select && my_b_inited(&select->file))
 
174
    tempfile= &select->file;
100
175
  else
101
176
    tempfile= table->sort.io_cache;
102
177
  if (tempfile && my_b_inited(tempfile)) // Test if ref-records was used
104
179
    info->read_record= (table->sort.addon_field ?
105
180
                        rr_unpack_from_tempfile : rr_from_tempfile);
106
181
    info->io_cache=tempfile;
107
 
    reinit_io_cache(info->io_cache,internal::READ_CACHE,0L,0,0);
108
 
    info->ref_pos=table->cursor->ref;
109
 
    if (!table->cursor->inited)
110
 
      table->cursor->ha_rnd_init(0);
 
182
    reinit_io_cache(info->io_cache,READ_CACHE,0L,0,0);
 
183
    info->ref_pos=table->file->ref;
 
184
    if (!table->file->inited)
 
185
      table->file->ha_rnd_init(0);
111
186
 
112
187
    /*
113
188
      table->sort.addon_field is checked because if we use addon fields,
116
191
    */
117
192
    if (!table->sort.addon_field &&
118
193
        session->variables.read_rnd_buff_size &&
119
 
        !(table->cursor->getEngine()->check_flag(HTON_BIT_FAST_KEY_READ)) &&
 
194
        !(table->file->ha_table_flags() & HA_FAST_KEY_READ) &&
120
195
        (table->db_stat & HA_READ_ONLY ||
121
196
        table->reginfo.lock_type <= TL_READ_NO_INSERT) &&
122
 
        (uint64_t) table->s->reclength* (table->cursor->stats.records+
123
 
                                                table->cursor->stats.deleted) >
 
197
        (uint64_t) table->s->reclength* (table->file->stats.records+
 
198
                                                table->file->stats.deleted) >
124
199
        (uint64_t) MIN_FILE_LENGTH_TO_USE_ROW_CACHE &&
125
200
        info->io_cache->end_of_file/info->ref_length * table->s->reclength >
126
 
        (internal::my_off_t) MIN_ROWS_TO_USE_TABLE_CACHE &&
 
201
        (my_off_t) MIN_ROWS_TO_USE_TABLE_CACHE &&
127
202
        !table->s->blob_fields &&
128
203
        info->ref_length <= MAX_REFLENGTH)
129
204
    {
139
214
  }
140
215
  else if (table->sort.record_pointers)
141
216
  {
142
 
    table->cursor->ha_rnd_init(0);
 
217
    table->file->ha_rnd_init(0);
143
218
    info->cache_pos=table->sort.record_pointers;
144
219
    info->cache_end=info->cache_pos+
145
220
                    table->sort.found_records*info->ref_length;
148
223
  }
149
224
  else
150
225
  {
151
 
    info->read_record= rr_sequential;
152
 
    table->cursor->ha_rnd_init(1);
 
226
    info->read_record=rr_sequential;
 
227
    table->file->ha_rnd_init(1);
153
228
    /* We can use record cache if we don't update dynamic length tables */
154
229
    if (!table->no_cache &&
155
230
        (use_record_cache > 0 ||
156
231
        (int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS ||
157
 
        !(table->s->db_options_in_use & HA_OPTION_PACK_RECORD)))
158
 
      table->cursor->extra_opt(HA_EXTRA_CACHE, session->variables.read_buff_size);
 
232
        !(table->s->db_options_in_use & HA_OPTION_PACK_RECORD) ||
 
233
        (use_record_cache < 0 &&
 
234
          !(table->file->ha_table_flags() & HA_NOT_DELETE_WITH_CACHE))))
 
235
      table->file->extra_opt(HA_EXTRA_CACHE, session->variables.read_buff_size);
159
236
  }
160
237
 
161
238
  return;
162
239
} /* init_read_record */
163
240
 
164
 
 
165
241
void end_read_record(READ_RECORD *info)
166
242
{                   /* free cache if used */
167
243
  if (info->cache)
172
248
  if (info->table)
173
249
  {
174
250
    info->table->filesort_free_buffers();
175
 
    (void) info->cursor->extra(HA_EXTRA_NO_CACHE);
 
251
    (void) info->file->extra(HA_EXTRA_NO_CACHE);
176
252
    if (info->read_record != rr_quick) // otherwise quick_range does it
177
 
      (void) info->cursor->ha_index_or_rnd_end();
 
253
      (void) info->file->ha_index_or_rnd_end();
178
254
    info->table=0;
179
255
  }
180
256
}
186
262
  else
187
263
  {
188
264
    if (info->print_error)
189
 
      info->table->print_error(error, MYF(0));
 
265
      info->table->file->print_error(error, MYF(0));
190
266
    if (error < 0)                            // Fix negative BDB errno
191
267
      error= 1;
192
268
  }
228
304
*/
229
305
static int rr_index_first(READ_RECORD *info)
230
306
{
231
 
  int tmp= info->cursor->index_first(info->record);
 
307
  int tmp= info->file->index_first(info->record);
232
308
  info->read_record= rr_index;
233
309
  if (tmp)
234
310
    tmp= rr_handle_error(info, tmp);
252
328
*/
253
329
static int rr_index(READ_RECORD *info)
254
330
{
255
 
  int tmp= info->cursor->index_next(info->record);
 
331
  int tmp= info->file->index_next(info->record);
256
332
  if (tmp)
257
333
    tmp= rr_handle_error(info, tmp);
258
334
  return tmp;
261
337
int rr_sequential(READ_RECORD *info)
262
338
{
263
339
  int tmp;
264
 
  while ((tmp= info->cursor->rnd_next(info->record)))
 
340
  while ((tmp= info->file->rnd_next(info->record)))
265
341
  {
266
342
    if (info->session->killed)
267
343
    {
289
365
  for (;;)
290
366
  {
291
367
    if (my_b_read(info->io_cache,info->ref_pos,info->ref_length))
292
 
      return -1;                                        /* End of cursor */
293
 
    if (!(tmp=info->cursor->rnd_pos(info->record,info->ref_pos)))
 
368
      return -1;                                        /* End of file */
 
369
    if (!(tmp=info->file->rnd_pos(info->record,info->ref_pos)))
294
370
      break;
295
371
    /* The following is extremely unlikely to happen */
296
372
    if (tmp == HA_ERR_RECORD_DELETED ||
303
379
} /* rr_from_tempfile */
304
380
 
305
381
/**
306
 
  Read a result set record from a temporary cursor after sorting.
 
382
  Read a result set record from a temporary file after sorting.
307
383
 
308
 
  The function first reads the next sorted record from the temporary cursor.
 
384
  The function first reads the next sorted record from the temporary file.
309
385
  into a buffer. If a success it calls a callback function that unpacks
310
386
  the fields values use in the result set from this buffer into their
311
387
  positions in the regular record buffer.
335
411
  for (;;)
336
412
  {
337
413
    if (info->cache_pos == info->cache_end)
338
 
      return -1;                                        /* End of cursor */
 
414
      return -1;                                        /* End of file */
339
415
    cache_pos= info->cache_pos;
340
416
    info->cache_pos+= info->ref_length;
341
417
 
342
 
    if (!(tmp=info->cursor->rnd_pos(info->record,cache_pos)))
 
418
    if (!(tmp=info->file->rnd_pos(info->record,cache_pos)))
343
419
      break;
344
420
 
345
421
    /* The following is extremely unlikely to happen */
413
489
{
414
490
  register uint32_t i;
415
491
  uint32_t length;
416
 
  internal::my_off_t rest_of_file;
 
492
  my_off_t rest_of_file;
417
493
  int16_t error;
418
494
  unsigned char *position,*ref_position,*record_pos;
419
495
  uint32_t record;
426
502
      {
427
503
        shortget(error,info->cache_pos);
428
504
        if (info->print_error)
429
 
          info->table->print_error(error,MYF(0));
 
505
          info->table->file->print_error(error,MYF(0));
430
506
      }
431
507
      else
432
508
      {
438
514
    }
439
515
    length=info->rec_cache_size;
440
516
    rest_of_file=info->io_cache->end_of_file - my_b_tell(info->io_cache);
441
 
    if ((internal::my_off_t) length > rest_of_file)
 
517
    if ((my_off_t) length > rest_of_file)
442
518
      length= (uint32_t) rest_of_file;
443
519
    if (!length || my_b_read(info->io_cache,info->cache,length))
444
520
    {
445
 
      return -1;                        /* End of cursor */
 
521
      return -1;                        /* End of file */
446
522
    }
447
523
 
448
524
    length/=info->ref_length;
455
531
      int3store(ref_position,(long) i);
456
532
      ref_position+=3;
457
533
    }
458
 
    internal::my_qsort(info->read_positions, length, info->struct_length,
459
 
                       (qsort_cmp) rr_cmp);
 
534
    my_qsort(info->read_positions, length, info->struct_length,
 
535
             (qsort_cmp) rr_cmp);
460
536
 
461
537
    position=info->read_positions;
462
538
    for (i=0 ; i < length ; i++)
466
542
      record=uint3korr(position);
467
543
      position+=3;
468
544
      record_pos=info->cache+record*info->reclength;
469
 
      if ((error=(int16_t) info->cursor->rnd_pos(record_pos,info->ref_pos)))
 
545
      if ((error=(int16_t) info->file->rnd_pos(record_pos,info->ref_pos)))
470
546
      {
471
547
        record_pos[info->error_offset]=1;
472
548
        shortstore(record_pos,error);
500
576
  return (int) a[7] - (int) b[7];
501
577
#endif
502
578
}
503
 
 
504
 
} /* namespace drizzled */