~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/filesort.cc

  • Committer: Brian Aker
  • Date: 2008-10-20 04:28:21 UTC
  • mto: (492.3.21 drizzle-clean-code)
  • mto: This revision was merged to the branch mainline in revision 530.
  • Revision ID: brian@tangent.org-20081020042821-rqqdrccuu8195k3y
Second pass of thd cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
static bool save_index(SORTPARAM *param,unsigned char **sort_keys, uint32_t count, 
46
46
                       filesort_info_st *table_sort);
47
47
static uint32_t suffix_length(uint32_t string_length);
48
 
static uint32_t sortlength(Session *thd, SORT_FIELD *sortorder, uint32_t s_length,
 
48
static uint32_t sortlength(Session *session, SORT_FIELD *sortorder, uint32_t s_length,
49
49
                       bool *multi_byte_charset);
50
 
static SORT_ADDON_FIELD *get_addon_fields(Session *thd, Field **ptabfield,
 
50
static SORT_ADDON_FIELD *get_addon_fields(Session *session, Field **ptabfield,
51
51
                                          uint32_t sortlength, uint32_t *plength);
52
52
static void unpack_addon_fields(struct st_sort_addon_field *addon_field,
53
53
                                unsigned char *buff);
63
63
  The result set is stored in table->io_cache or
64
64
  table->record_pointers.
65
65
 
66
 
  @param thd           Current thread
 
66
  @param session           Current thread
67
67
  @param table          Table to sort
68
68
  @param sortorder      How to sort the table
69
69
  @param s_length       Number of elements in sortorder
87
87
    examined_rows       will be set to number of examined rows
88
88
*/
89
89
 
90
 
ha_rows filesort(Session *thd, Table *table, SORT_FIELD *sortorder, uint32_t s_length,
 
90
ha_rows filesort(Session *session, Table *table, SORT_FIELD *sortorder, uint32_t s_length,
91
91
                 SQL_SELECT *select, ha_rows max_rows,
92
92
                 bool sort_positions, ha_rows *examined_rows)
93
93
{
111
111
   Release InnoDB's adaptive hash index latch (if holding) before
112
112
   running a sort.
113
113
  */
114
 
  ha_release_temporary_latches(thd);
 
114
  ha_release_temporary_latches(session);
115
115
 
116
116
  /* 
117
117
    Don't use table->sort in filesort as it is also used by 
127
127
  buffpek=0;
128
128
  error= 1;
129
129
  memset(&param, 0, sizeof(param));
130
 
  param.sort_length= sortlength(thd, sortorder, s_length, &multi_byte_charset);
 
130
  param.sort_length= sortlength(session, sortorder, s_length, &multi_byte_charset);
131
131
  param.ref_length= table->file->ref_length;
132
132
  param.addon_field= 0;
133
133
  param.addon_length= 0;
137
137
      Get the descriptors of all fields whose values are appended 
138
138
      to sorted fields and get its total length in param.spack_length.
139
139
    */
140
 
    param.addon_field= get_addon_fields(thd, table->field, 
 
140
    param.addon_field= get_addon_fields(session, table->field, 
141
141
                                        param.sort_length,
142
142
                                        &param.addon_length);
143
143
  }
167
167
 
168
168
  if (select && select->quick)
169
169
  {
170
 
    status_var_increment(thd->status_var.filesort_range_count);
 
170
    status_var_increment(session->status_var.filesort_range_count);
171
171
  }
172
172
  else
173
173
  {
174
 
    status_var_increment(thd->status_var.filesort_scan_count);
 
174
    status_var_increment(session->status_var.filesort_scan_count);
175
175
  }
176
176
#ifdef CAN_TRUST_RANGE
177
177
  if (select && select->quick && select->quick->records > 0L)
197
197
      !(param.tmp_buffer= (char*) my_malloc(param.sort_length,MYF(MY_WME))))
198
198
    goto err;
199
199
 
200
 
  memavl= thd->variables.sortbuff_size;
 
200
  memavl= session->variables.sortbuff_size;
201
201
  min_sort_memory= cmax((uint32_t)MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
202
202
  while (memavl >= min_sort_memory)
203
203
  {
312
312
    my_message(ER_FILSORT_ABORT, ER(ER_FILSORT_ABORT),
313
313
               MYF(ME_ERROR+ME_WAITTANG));
314
314
  else
315
 
    statistic_add(thd->status_var.filesort_rows,
 
315
    statistic_add(session->status_var.filesort_rows,
316
316
                  (uint32_t) records, &LOCK_status);
317
317
  *examined_rows= param.examined_rows;
318
318
  memcpy(&table->sort, &table_sort, sizeof(filesort_info_st));
444
444
  unsigned char *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
445
445
  my_off_t record;
446
446
  Table *sort_form;
447
 
  Session *thd= current_thd;
448
 
  volatile Session::killed_state *killed= &thd->killed;
 
447
  Session *session= current_session;
 
448
  volatile Session::killed_state *killed= &session->killed;
449
449
  handler *file;
450
450
  MY_BITMAP *save_read_set, *save_write_set;
451
451
 
467
467
    next_pos=(unsigned char*) 0;                        /* Find records in sequence */
468
468
    file->ha_rnd_init(1);
469
469
    file->extra_opt(HA_EXTRA_CACHE,
470
 
                    current_thd->variables.read_buff_size);
 
470
                    current_session->variables.read_buff_size);
471
471
  }
472
472
 
473
473
  READ_RECORD read_record_info;
475
475
  {
476
476
    if (select->quick->reset())
477
477
      return(HA_POS_ERROR);
478
 
    init_read_record(&read_record_info, current_thd, select->quick->head,
 
478
    init_read_record(&read_record_info, current_session, select->quick->head,
479
479
                     select, 1, 1);
480
480
  }
481
481
 
557
557
    else
558
558
      file->unlock_row();
559
559
    /* It does not make sense to read more keys in case of a fatal error */
560
 
    if (thd->is_error())
 
560
    if (session->is_error())
561
561
      break;
562
562
  }
563
563
  if (quick_select)
575
575
      file->ha_rnd_end();
576
576
  }
577
577
 
578
 
  if (thd->is_error())
 
578
  if (session->is_error())
579
579
    return(HA_POS_ERROR);
580
580
  
581
581
  /* Signal we should use orignal column read and write maps */
1118
1118
  QUEUE queue;
1119
1119
  qsort2_cmp cmp;
1120
1120
  void *first_cmp_arg;
1121
 
  volatile Session::killed_state *killed= &current_thd->killed;
 
1121
  volatile Session::killed_state *killed= &current_session->killed;
1122
1122
  Session::killed_state not_killable;
1123
1123
 
1124
 
  status_var_increment(current_thd->status_var.filesort_merge_passes);
 
1124
  status_var_increment(current_session->status_var.filesort_merge_passes);
1125
1125
  if (param->not_killable)
1126
1126
  {
1127
1127
    killed= &not_killable;
1336
1336
/**
1337
1337
  Calculate length of sort key.
1338
1338
 
1339
 
  @param thd                      Thread handler
 
1339
  @param session                          Thread handler
1340
1340
  @param sortorder                Order of items to sort
1341
1341
  @param s_length                 Number of items to sort
1342
1342
  @param[out] multi_byte_charset Set to 1 if we are using multi-byte charset
1352
1352
*/
1353
1353
 
1354
1354
static uint32_t
1355
 
sortlength(Session *thd, SORT_FIELD *sortorder, uint32_t s_length,
 
1355
sortlength(Session *session, SORT_FIELD *sortorder, uint32_t s_length,
1356
1356
           bool *multi_byte_charset)
1357
1357
{
1358
1358
  register uint32_t length;
1386
1386
      switch (sortorder->result_type) {
1387
1387
      case STRING_RESULT:
1388
1388
        sortorder->length=sortorder->item->max_length;
1389
 
        set_if_smaller(sortorder->length, thd->variables.max_sort_length);
 
1389
        set_if_smaller(sortorder->length, session->variables.max_sort_length);
1390
1390
        if (use_strnxfrm((cs=sortorder->item->collation.collation)))
1391
1391
        { 
1392
1392
          sortorder->length= cs->coll->strnxfrmlen(cs, sortorder->length);
1421
1421
      if (sortorder->item->maybe_null)
1422
1422
        length++;                               // Place for NULL marker
1423
1423
    }
1424
 
    set_if_smaller(sortorder->length, thd->variables.max_sort_length);
 
1424
    set_if_smaller(sortorder->length, session->variables.max_sort_length);
1425
1425
    length+=sortorder->length;
1426
1426
  }
1427
1427
  sortorder->field= (Field*) 0;                 // end marker
1441
1441
  layouts for the values of the non-sorted fields in the buffer and
1442
1442
  fills them.
1443
1443
 
1444
 
  @param thd                 Current thread
 
1444
  @param session                 Current thread
1445
1445
  @param ptabfield           Array of references to the table fields
1446
1446
  @param sortlength          Total length of sorted fields
1447
1447
  @param[out] plength        Total length of appended fields
1457
1457
*/
1458
1458
 
1459
1459
static SORT_ADDON_FIELD *
1460
 
get_addon_fields(Session *thd, Field **ptabfield, uint32_t sortlength, uint32_t *plength)
 
1460
get_addon_fields(Session *session, Field **ptabfield, uint32_t sortlength, uint32_t *plength)
1461
1461
{
1462
1462
  Field **pfield;
1463
1463
  Field *field;
1493
1493
    return 0;
1494
1494
  length+= (null_fields+7)/8;
1495
1495
 
1496
 
  if (length+sortlength > thd->variables.max_length_for_sort_data ||
 
1496
  if (length+sortlength > session->variables.max_length_for_sort_data ||
1497
1497
      !(addonf= (SORT_ADDON_FIELD *) my_malloc(sizeof(SORT_ADDON_FIELD)*
1498
1498
                                               (fields+1), MYF(MY_WME))))
1499
1499
    return 0;