~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/filesort.cc

Merge Andrew - fix bug #663765: Some unittests are won't compile in GCC 4.5
Merge Andrew - Add a GLOBAL UPPER bound to cap SORT BUFFER memory use

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include <queue>
30
30
#include <algorithm>
31
31
 
 
32
#include "drizzled/drizzled.h"
32
33
#include "drizzled/sql_sort.h"
33
34
#include "drizzled/error.h"
34
35
#include "drizzled/probes.h"
41
42
#include "drizzled/internal/my_sys.h"
42
43
#include "plugin/myisam/myisam.h"
43
44
#include "drizzled/plugin/transactional_storage_engine.h"
 
45
#include "drizzled/atomics.h"
 
46
#include "drizzled/global_buffer.h"
44
47
 
45
48
using namespace std;
46
49
 
136
139
                 bool sort_positions, ha_rows *examined_rows)
137
140
{
138
141
  int error;
139
 
  uint32_t memavl, min_sort_memory;
 
142
  uint32_t memavl= 0, min_sort_memory;
140
143
  uint32_t maxbuffer;
 
144
  size_t allocated_sort_memory= 0;
141
145
  buffpek *buffpek_inst;
142
146
  ha_rows records= HA_POS_ERROR;
143
147
  unsigned char **sort_keys= 0;
247
251
    uint32_t old_memavl;
248
252
    uint32_t keys= memavl/(param.rec_length+sizeof(char*));
249
253
    param.keys= (uint32_t) min(records+1, (ha_rows)keys);
 
254
 
 
255
    allocated_sort_memory= param.keys * param.rec_length;
 
256
    if (not global_sort_buffer.add(allocated_sort_memory))
 
257
    {
 
258
      my_error(ER_OUT_OF_GLOBAL_SORTMEMORY, MYF(ME_ERROR+ME_WAITTANG));
 
259
      goto err;
 
260
    }
 
261
 
250
262
    if ((table_sort.sort_keys=
251
263
         (unsigned char **) make_char_array((char **) table_sort.sort_keys,
252
264
                                            param.keys, param.rec_length)))
253
265
      break;
 
266
 
 
267
    global_sort_buffer.sub(allocated_sort_memory);
254
268
    old_memavl= memavl;
255
269
    if ((memavl= memavl/4*3) < min_sort_memory && old_memavl > min_sort_memory)
256
270
      memavl= min_sort_memory;
261
275
    my_error(ER_OUT_OF_SORTMEMORY,MYF(ME_ERROR+ME_WAITTANG));
262
276
    goto err;
263
277
  }
 
278
 
264
279
  if (open_cached_file(&buffpek_pointers,drizzle_tmpdir.c_str(),TEMP_PREFIX,
265
280
                       DISK_BUFFER_SIZE, MYF(MY_WME)))
266
281
    goto err;
357
372
    session->status_var.filesort_rows+= (uint32_t) records;
358
373
  }
359
374
  *examined_rows= param.examined_rows;
 
375
  global_sort_buffer.sub(allocated_sort_memory);
360
376
  memcpy(&table->sort, &table_sort, sizeof(filesort_info));
361
377
  DRIZZLE_FILESORT_DONE(error, records);
362
378
  return (error ? HA_POS_ERROR : records);