~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
16
/*
17
  Creates a index for a database by reading keys, sorting them and outputing
18
  them in sorted order through SORT_INFO functions.
19
*/
20
1130.3.28 by Monty Taylor
Moved heapdef.h and myisamdef.h to *_priv.h for easier filtering for include guard check.
21
#include "myisam_priv.h"
1 by brian
clean slate
22
#include <stddef.h>
960.2.7 by Padraig O'Sullivan
Removed all traces of QUEUE from the codebase. We are using
23
#include <queue>
1067.4.8 by Nathan Williams
Converted all usages of cmin/cmax in plugin directory to std::min/max.
24
#include <algorithm>
1 by brian
clean slate
25
26
/* static variables */
27
28
#undef MIN_SORT_MEMORY
29
#undef MYF_RW
30
#undef DISK_BUFFER_SIZE
31
32
#define MERGEBUFF 15
33
#define MERGEBUFF2 31
34
#define MIN_SORT_MEMORY (4096-MALLOC_OVERHEAD)
35
#define MYF_RW  MYF(MY_NABP | MY_WME | MY_WAIT_IF_FULL)
36
#define DISK_BUFFER_SIZE (IO_SIZE*16)
37
960.2.7 by Padraig O'Sullivan
Removed all traces of QUEUE from the codebase. We are using
38
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
39
using namespace drizzled;
960.2.7 by Padraig O'Sullivan
Removed all traces of QUEUE from the codebase. We are using
40
1 by brian
clean slate
41
42
/*
43
 Pointers of functions for store and read keys from temp file
44
*/
45
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
46
extern void print_error(const char *fmt,...);
1 by brian
clean slate
47
48
/* Functions defined in this file */
49
1165.1.64 by Stewart Smith
find_all_keys() static to myisam/sort.cc (luckily the filesort one was static, otherwise....)
50
static ha_rows  find_all_keys(MI_SORT_PARAM *info,uint32_t keys,
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
51
			      unsigned char **sort_keys,
52
			      DYNAMIC_ARRAY *buffpek,
53
			      size_t *maxbuffer,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
54
			      internal::io_cache_st *tempfile,
55
			      internal::io_cache_st *tempfile_for_exceptions);
1165.1.72 by Stewart Smith
make write_keys() static to myisam/sort.cc
56
static int  write_keys(MI_SORT_PARAM *info,unsigned char **sort_keys,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
57
                             uint32_t count, BUFFPEK *buffpek,internal::io_cache_st *tempfile);
1165.1.71 by Stewart Smith
make write_key() static to myisam/sort.cc
58
static int  write_key(MI_SORT_PARAM *info, unsigned char *key,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
59
			    internal::io_cache_st *tempfile);
1165.1.70 by Stewart Smith
make write_index() static to myisam/sort.cc
60
static int  write_index(MI_SORT_PARAM *info,unsigned char * *sort_keys,
482 by Brian Aker
Remove uint.
61
                              uint32_t count);
1165.1.67 by Stewart Smith
make merge_many_buff() static to myisam/sort.cc
62
static int  merge_many_buff(MI_SORT_PARAM *info,uint32_t keys,
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
63
			    unsigned char * *sort_keys,
64
			    BUFFPEK *buffpek,size_t *maxbuffer,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
65
			    internal::io_cache_st *t_file);
66
static uint32_t  read_to_buffer(internal::io_cache_st *fromfile,BUFFPEK *buffpek,
482 by Brian Aker
Remove uint.
67
                                  uint32_t sort_length);
1165.1.65 by Stewart Smith
make merge_buffers() static to myisam/sort.cc
68
static int  merge_buffers(MI_SORT_PARAM *info,uint32_t keys,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
69
                                internal::io_cache_st *from_file, internal::io_cache_st *to_file,
481 by Brian Aker
Remove all of uchar.
70
                                unsigned char * *sort_keys, BUFFPEK *lastbuff,
1 by brian
clean slate
71
                                BUFFPEK *Fb, BUFFPEK *Tb);
1165.1.66 by Stewart Smith
make merge_index() static to myisam/sort.cc
72
static int  merge_index(MI_SORT_PARAM *,uint,unsigned char **,BUFFPEK *, int,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
73
                              internal::io_cache_st *);
1165.1.73 by Stewart Smith
make write_keys_varlen() static to myisam/sort.cc
74
static int  write_keys_varlen(MI_SORT_PARAM *info,unsigned char **sort_keys,
960.3.1 by Monty Taylor
Fixed linkage issues on solaris.
75
                       uint32_t count, BUFFPEK *buffpek,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
76
                       internal::io_cache_st *tempfile);
77
static uint32_t  read_to_buffer_varlen(internal::io_cache_st *fromfile,BUFFPEK *buffpek,
960.3.1 by Monty Taylor
Fixed linkage issues on solaris.
78
                                uint32_t sort_length);
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
79
static int  write_merge_key(MI_SORT_PARAM *info, internal::io_cache_st *to_file,
960.3.1 by Monty Taylor
Fixed linkage issues on solaris.
80
                     unsigned char *key, uint32_t sort_length, uint32_t count);
1165.1.75 by Stewart Smith
make write_merge_key_varlen() static to myisam/sort.cc
81
static int  write_merge_key_varlen(MI_SORT_PARAM *info,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
82
                            internal::io_cache_st *to_file,
960.3.1 by Monty Taylor
Fixed linkage issues on solaris.
83
                            unsigned char* key, uint32_t sort_length,
84
                            uint32_t count);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
85
960.3.1 by Monty Taylor
Fixed linkage issues on solaris.
86
inline int
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
87
my_var_write(MI_SORT_PARAM *info, internal::io_cache_st *to_file, unsigned char *bufs);
1 by brian
clean slate
88
89
/*
90
  Creates a index of sorted keys
91
92
  SYNOPSIS
93
    _create_index_by_sort()
94
    info		Sort parameters
95
    no_messages		Set to 1 if no output
96
    sortbuff_size	Size if sortbuffer to allocate
97
98
  RESULT
99
    0	ok
100
   <> 0 Error
101
*/
102
281 by Brian Aker
Converted myisam away from my_bool
103
int _create_index_by_sort(MI_SORT_PARAM *info,bool no_messages,
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
104
			  size_t sortbuff_size)
1 by brian
clean slate
105
{
2318.6.1 by Olaf van der Spek
Refactor
106
  size_t skr;
107
  uint32_t memavl,keys;
1 by brian
clean slate
108
  DYNAMIC_ARRAY buffpek;
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
109
  internal::io_cache_st tempfile, tempfile_for_exceptions;
1 by brian
clean slate
110
111
  if (info->keyinfo->flag & HA_VAR_LENGTH_KEY)
112
  {
113
    info->write_keys=write_keys_varlen;
114
    info->read_to_buffer=read_to_buffer_varlen;
115
    info->write_key= write_merge_key_varlen;
116
  }
117
  else
118
  {
119
    info->write_keys=write_keys;
120
    info->read_to_buffer=read_to_buffer;
121
    info->write_key=write_merge_key;
122
  }
123
124
  my_b_clear(&tempfile);
125
  my_b_clear(&tempfile_for_exceptions);
212.6.12 by Mats Kindahl
Removing redundant use of casts in MyISAM storage for memcmp(), memcpy(), memset(), and memmove().
126
  memset(&buffpek, 0, sizeof(buffpek));
2318.6.1 by Olaf van der Spek
Refactor
127
  unsigned char** sort_keys= (unsigned char **) NULL; 
128
  int error= 1;
129
  size_t maxbuffer=1;
1 by brian
clean slate
130
1067.4.8 by Nathan Williams
Converted all usages of cmin/cmax in plugin directory to std::min/max.
131
  memavl=max(sortbuff_size,(size_t)MIN_SORT_MEMORY);
2318.6.1 by Olaf van der Spek
Refactor
132
  ha_rows records= info->sort_info->max_records;
133
  uint32_t sort_length= info->key_length;
1 by brian
clean slate
134
135
  while (memavl >= MIN_SORT_MEMORY)
136
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
137
    if ((records < UINT32_MAX) &&
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
138
       ((internal::my_off_t) (records + 1) *
139
        (sort_length + sizeof(char*)) <= (internal::my_off_t) memavl))
1 by brian
clean slate
140
      keys= (uint)records+1;
141
    else
142
      do
143
      {
2318.6.1 by Olaf van der Spek
Refactor
144
        skr=maxbuffer;
145
        if (memavl < sizeof(BUFFPEK)* maxbuffer ||
146
          (keys=(memavl-sizeof(BUFFPEK)* maxbuffer) / (sort_length+sizeof(char*))) <= 1 ||
147
          keys < maxbuffer)
148
        {
149
          mi_check_print_error(info->sort_info->param,
150
            "myisam_sort_buffer_size is too small");
151
          goto err;
152
        }
1 by brian
clean slate
153
      }
988.2.2 by Trond Norbye
size_t and uint64_t is not the same in 32 bit builds. Add explicit casting
154
      while ((maxbuffer= (size_t)(records/(keys-1)+1)) != skr);
1 by brian
clean slate
155
2318.4.8 by Olaf van der Spek
Remove malloc NULL check. Just die.
156
    sort_keys=(unsigned char **)malloc(keys*(sort_length+sizeof(char*)));
157
    my_init_dynamic_array(&buffpek, sizeof(BUFFPEK), maxbuffer, maxbuffer/2);
158
    break;
1 by brian
clean slate
159
  }
160
  if (memavl < MIN_SORT_MEMORY)
161
  {
971.6.11 by Eric Day
Removed purecov messages.
162
    mi_check_print_error(info->sort_info->param,"MyISAM sort buffer too small");
163
    goto err;
1 by brian
clean slate
164
  }
165
  (*info->lock_in_memory)(info->sort_info->param);/* Everything is allocated */
166
167
  if (!no_messages)
168
    printf("  - Searching for keys, allocating buffer for %d keys\n",keys);
169
170
  if ((records=find_all_keys(info,keys,sort_keys,&buffpek,&maxbuffer,
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
171
			     &tempfile,&tempfile_for_exceptions))
1 by brian
clean slate
172
      == HA_POS_ERROR)
971.6.11 by Eric Day
Removed purecov messages.
173
    goto err;
1 by brian
clean slate
174
  if (maxbuffer == 0)
175
  {
176
    if (!no_messages)
305 by Brian Aker
Another pass of ulong removal.
177
      printf("  - Dumping %u keys\n", (uint32_t) records);
1 by brian
clean slate
178
    if (write_index(info,sort_keys, (uint) records))
971.6.11 by Eric Day
Removed purecov messages.
179
      goto err;
1 by brian
clean slate
180
  }
181
  else
182
  {
183
    keys=(keys*(sort_length+sizeof(char*)))/sort_length;
184
    if (maxbuffer >= MERGEBUFF2)
185
    {
186
      if (!no_messages)
971.6.11 by Eric Day
Removed purecov messages.
187
	printf("  - Merging %u keys\n", (uint32_t) records);
2210.3.5 by Olaf van der Spek
Refactor
188
      if (merge_many_buff(info,keys,sort_keys, (BUFFPEK*)buffpek.buffer, &maxbuffer, &tempfile))
971.6.11 by Eric Day
Removed purecov messages.
189
	goto err;
1 by brian
clean slate
190
    }
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
191
    if (internal::flush_io_cache(&tempfile) ||
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
192
	tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
971.6.11 by Eric Day
Removed purecov messages.
193
      goto err;
1 by brian
clean slate
194
    if (!no_messages)
971.6.11 by Eric Day
Removed purecov messages.
195
      printf("  - Last merge and dumping keys\n");
2210.3.5 by Olaf van der Spek
Refactor
196
    if (merge_index(info,keys,sort_keys, (BUFFPEK*)buffpek.buffer, maxbuffer, &tempfile))
971.6.11 by Eric Day
Removed purecov messages.
197
      goto err;
1 by brian
clean slate
198
  }
199
76.1.1 by Brian Aker
Final removal of fulltext core from myisam.
200
  if (flush_pending_blocks(info))
1 by brian
clean slate
201
    goto err;
202
203
  if (my_b_inited(&tempfile_for_exceptions))
204
  {
205
    MI_INFO *idx=info->sort_info->info;
482 by Brian Aker
Remove uint.
206
    uint32_t     keyno=info->key;
207
    uint32_t     key_length, ref_length=idx->s->rec_reflength;
1 by brian
clean slate
208
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
209
    if (not no_messages)
971.6.11 by Eric Day
Removed purecov messages.
210
      printf("  - Adding exceptions\n");
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
211
212
    if (flush_io_cache(&tempfile_for_exceptions) || tempfile_for_exceptions.reinit_io_cache(internal::READ_CACHE,0L,0,0))
213
    {
1 by brian
clean slate
214
      goto err;
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
215
    }
1 by brian
clean slate
216
481 by Brian Aker
Remove all of uchar.
217
    while (!my_b_read(&tempfile_for_exceptions,(unsigned char*)&key_length,
1 by brian
clean slate
218
		      sizeof(key_length))
481 by Brian Aker
Remove all of uchar.
219
        && !my_b_read(&tempfile_for_exceptions,(unsigned char*)sort_keys,
1 by brian
clean slate
220
		      (uint) key_length))
221
    {
481 by Brian Aker
Remove all of uchar.
222
	if (_mi_ck_write(idx,keyno,(unsigned char*) sort_keys,key_length-ref_length))
1 by brian
clean slate
223
	  goto err;
224
    }
225
  }
226
227
  error =0;
228
229
err:
2353.3.1 by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer
230
  free(sort_keys);
1 by brian
clean slate
231
  delete_dynamic(&buffpek);
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
232
  tempfile.close_cached_file();
233
  tempfile_for_exceptions.close_cached_file();
1 by brian
clean slate
234
51.1.119 by Jay Pipes
DBUG symbol removal
235
  return(error ? -1 : 0);
1 by brian
clean slate
236
} /* _create_index_by_sort */
237
238
239
/* Search after all keys and place them in a temp. file */
240
1165.1.64 by Stewart Smith
find_all_keys() static to myisam/sort.cc (luckily the filesort one was static, otherwise....)
241
static ha_rows  find_all_keys(MI_SORT_PARAM *info, uint32_t keys,
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
242
			      unsigned char **sort_keys,
243
			      DYNAMIC_ARRAY *buffpek,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
244
			      size_t *maxbuffer, internal::io_cache_st *tempfile,
245
			      internal::io_cache_st *tempfile_for_exceptions)
1 by brian
clean slate
246
{
247
  int error;
482 by Brian Aker
Remove uint.
248
  uint32_t idx;
1 by brian
clean slate
249
250
  idx=error=0;
481 by Brian Aker
Remove all of uchar.
251
  sort_keys[0]=(unsigned char*) (sort_keys+keys);
1 by brian
clean slate
252
253
  while (!(error=(*info->key_read)(info,sort_keys[idx])))
254
  {
255
    if (info->real_key_length > info->key_length)
256
    {
257
      if (write_key(info,sort_keys[idx],tempfile_for_exceptions))
971.6.11 by Eric Day
Removed purecov messages.
258
        return(HA_POS_ERROR);
1 by brian
clean slate
259
      continue;
260
    }
261
262
    if (++idx == keys)
263
    {
264
      if (info->write_keys(info,sort_keys,idx-1,(BUFFPEK *)alloc_dynamic(buffpek),
265
		     tempfile))
971.6.11 by Eric Day
Removed purecov messages.
266
      return(HA_POS_ERROR);
1 by brian
clean slate
267
481 by Brian Aker
Remove all of uchar.
268
      sort_keys[0]=(unsigned char*) (sort_keys+keys);
1 by brian
clean slate
269
      memcpy(sort_keys[0],sort_keys[idx-1],(size_t) info->key_length);
270
      idx=1;
271
    }
272
    sort_keys[idx]=sort_keys[idx-1]+info->key_length;
273
  }
274
  if (error > 0)
971.6.11 by Eric Day
Removed purecov messages.
275
    return(HA_POS_ERROR);
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
276
  if (buffpek->size())
1 by brian
clean slate
277
  {
278
    if (info->write_keys(info,sort_keys,idx,(BUFFPEK *)alloc_dynamic(buffpek),
279
		   tempfile))
971.6.11 by Eric Day
Removed purecov messages.
280
      return(HA_POS_ERROR);
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
281
    *maxbuffer=buffpek->size() - 1;
1 by brian
clean slate
282
  }
283
  else
284
    *maxbuffer=0;
285
51.1.119 by Jay Pipes
DBUG symbol removal
286
  return((*maxbuffer)*(keys-1)+idx);
1 by brian
clean slate
287
} /* find_all_keys */
288
289
290
int thr_write_keys(MI_SORT_PARAM *sort_param)
291
{
292
  SORT_INFO *sort_info= sort_param->sort_info;
293
  MI_CHECK *param= sort_info->param;
305 by Brian Aker
Another pass of ulong removal.
294
  uint32_t length= 0, keys;
295
  ulong *rec_per_key_part= param->rec_per_key_part;
1 by brian
clean slate
296
  int got_error=sort_info->got_error;
482 by Brian Aker
Remove uint.
297
  uint32_t i;
1 by brian
clean slate
298
  MI_INFO *info=sort_info->info;
299
  MYISAM_SHARE *share=info->s;
300
  MI_SORT_PARAM *sinfo;
481 by Brian Aker
Remove all of uchar.
301
  unsigned char *mergebuf=0;
1 by brian
clean slate
302
303
  for (i= 0, sinfo= sort_param ;
304
       i < sort_info->total_keys ;
305
       i++, rec_per_key_part+=sinfo->keyinfo->keysegs, sinfo++)
306
  {
307
    if (!sinfo->sort_keys)
308
    {
309
      got_error=1;
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
310
      void * rec_buff_ptr= mi_get_rec_buff_ptr(info, sinfo->rec_buff);
2353.3.1 by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer
311
      free(rec_buff_ptr);
1 by brian
clean slate
312
      continue;
313
    }
314
    if (!got_error)
315
    {
316
      mi_set_key_active(share->state.key_map, sinfo->key);
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
317
      if (!sinfo->buffpek.size())
1 by brian
clean slate
318
      {
319
        if (param->testflag & T_VERBOSE)
320
        {
321
          printf("Key %d  - Dumping %u keys\n",sinfo->key+1, sinfo->keys);
322
          fflush(stdout);
323
        }
76.1.1 by Brian Aker
Final removal of fulltext core from myisam.
324
        if (write_index(sinfo, sinfo->sort_keys, sinfo->keys) || flush_pending_blocks(sinfo))
1 by brian
clean slate
325
          got_error=1;
326
      }
327
      if (!got_error && param->testflag & T_STATISTICS)
328
        update_key_parts(sinfo->keyinfo, rec_per_key_part, sinfo->unique,
329
                         param->stats_method == MI_STATS_METHOD_IGNORE_NULLS?
330
                         sinfo->notnull: NULL,
151 by Brian Aker
Ulonglong to uint64_t
331
                         (uint64_t) info->state->records);
1 by brian
clean slate
332
    }
481 by Brian Aker
Remove all of uchar.
333
    free((unsigned char*) sinfo->sort_keys);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
334
    void * rec_buff_ptr= mi_get_rec_buff_ptr(info, sinfo->rec_buff);
2353.3.1 by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer
335
    free(rec_buff_ptr);
1 by brian
clean slate
336
    sinfo->sort_keys=0;
337
  }
338
339
  for (i= 0, sinfo= sort_param ;
340
       i < sort_info->total_keys ;
341
       i++,
342
	 delete_dynamic(&sinfo->buffpek),
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
343
	 sinfo->tempfile.close_cached_file(),
344
	 sinfo->tempfile_for_exceptions.close_cached_file(),
1 by brian
clean slate
345
	 sinfo++)
346
  {
347
    if (got_error)
348
      continue;
349
    if (sinfo->keyinfo->flag & HA_VAR_LENGTH_KEY)
350
    {
351
      sinfo->write_keys=write_keys_varlen;
352
      sinfo->read_to_buffer=read_to_buffer_varlen;
353
      sinfo->write_key=write_merge_key_varlen;
354
    }
355
    else
356
    {
357
      sinfo->write_keys=write_keys;
358
      sinfo->read_to_buffer=read_to_buffer;
359
      sinfo->write_key=write_merge_key;
360
    }
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
361
    if (sinfo->buffpek.size())
1 by brian
clean slate
362
    {
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
363
      size_t maxbuffer=sinfo->buffpek.size() - 1;
1 by brian
clean slate
364
      if (!mergebuf)
365
      {
366
        length=param->sort_buffer_length;
367
        while (length >= MIN_SORT_MEMORY)
368
        {
960.2.2 by Monty Taylor
Moved MyISAM files to C++ so we can continue to consolidate code.
369
          if ((mergebuf= (unsigned char *)malloc(length)))
1 by brian
clean slate
370
              break;
371
          length=length*3/4;
372
        }
373
        if (!mergebuf)
374
        {
375
          got_error=1;
376
          continue;
377
        }
378
      }
379
      keys=length/sinfo->key_length;
380
      if (maxbuffer >= MERGEBUFF2)
381
      {
382
        if (param->testflag & T_VERBOSE)
383
          printf("Key %d  - Merging %u keys\n",sinfo->key+1, sinfo->keys);
2210.3.5 by Olaf van der Spek
Refactor
384
        if (merge_many_buff(sinfo, keys, (unsigned char **)mergebuf, (BUFFPEK*)sinfo->buffpek.buffer,
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
385
			    &maxbuffer, &sinfo->tempfile))
1 by brian
clean slate
386
        {
387
          got_error=1;
388
          continue;
389
        }
390
      }
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
391
      if (flush_io_cache(&sinfo->tempfile) || sinfo->tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
1 by brian
clean slate
392
      {
393
        got_error=1;
394
        continue;
395
      }
396
      if (param->testflag & T_VERBOSE)
397
        printf("Key %d  - Last merge and dumping keys\n", sinfo->key+1);
2210.3.5 by Olaf van der Spek
Refactor
398
      if (merge_index(sinfo, keys, (unsigned char **)mergebuf, (BUFFPEK*)sinfo->buffpek.buffer,
1 by brian
clean slate
399
                      maxbuffer,&sinfo->tempfile) ||
400
	  flush_pending_blocks(sinfo))
401
      {
402
        got_error=1;
403
        continue;
404
      }
405
    }
406
    if (my_b_inited(&sinfo->tempfile_for_exceptions))
407
    {
482 by Brian Aker
Remove uint.
408
      uint32_t key_length;
1 by brian
clean slate
409
410
      if (param->testflag & T_VERBOSE)
411
        printf("Key %d  - Dumping 'long' keys\n", sinfo->key+1);
412
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
413
      if (flush_io_cache(&sinfo->tempfile_for_exceptions) || sinfo->tempfile_for_exceptions.reinit_io_cache(internal::READ_CACHE,0L,0,0))
1 by brian
clean slate
414
      {
415
        got_error=1;
416
        continue;
417
      }
418
419
      while (!got_error &&
481 by Brian Aker
Remove all of uchar.
420
	     !my_b_read(&sinfo->tempfile_for_exceptions,(unsigned char*)&key_length,
1 by brian
clean slate
421
			sizeof(key_length)))
422
      {
481 by Brian Aker
Remove all of uchar.
423
        unsigned char ft_buf[10];
1 by brian
clean slate
424
        if (key_length > sizeof(ft_buf) ||
481 by Brian Aker
Remove all of uchar.
425
            my_b_read(&sinfo->tempfile_for_exceptions, (unsigned char*)ft_buf,
1 by brian
clean slate
426
                      (uint)key_length) ||
481 by Brian Aker
Remove all of uchar.
427
            _mi_ck_write(info, sinfo->key, (unsigned char*)ft_buf,
1 by brian
clean slate
428
                         key_length - info->s->rec_reflength))
429
          got_error=1;
430
      }
431
    }
432
  }
481 by Brian Aker
Remove all of uchar.
433
  free((unsigned char*) mergebuf);
51.1.119 by Jay Pipes
DBUG symbol removal
434
  return(got_error);
1 by brian
clean slate
435
}
436
437
        /* Write all keys in memory to file for later merge */
438
1165.1.72 by Stewart Smith
make write_keys() static to myisam/sort.cc
439
static int  write_keys(MI_SORT_PARAM *info, register unsigned char **sort_keys,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
440
                             uint32_t count, BUFFPEK *buffpek, internal::io_cache_st *tempfile)
1 by brian
clean slate
441
{
481 by Brian Aker
Remove all of uchar.
442
  unsigned char **end;
482 by Brian Aker
Remove uint.
443
  uint32_t sort_length=info->key_length;
1 by brian
clean slate
444
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
445
  internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
1 by brian
clean slate
446
            info);
1909.1.2 by Brian Aker
Additional io_cache encapsulation.
447
  if (!my_b_inited(tempfile) && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
971.6.11 by Eric Day
Removed purecov messages.
448
    return(1);
1 by brian
clean slate
449
450
  buffpek->file_pos=my_b_tell(tempfile);
451
  buffpek->count=count;
452
453
  for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
454
  {
481 by Brian Aker
Remove all of uchar.
455
    if (my_b_write(tempfile,(unsigned char*) *sort_keys,(uint) sort_length))
971.6.11 by Eric Day
Removed purecov messages.
456
      return(1);
1 by brian
clean slate
457
  }
51.1.119 by Jay Pipes
DBUG symbol removal
458
  return(0);
1 by brian
clean slate
459
} /* write_keys */
460
461
960.3.1 by Monty Taylor
Fixed linkage issues on solaris.
462
inline int
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
463
my_var_write(MI_SORT_PARAM *info, internal::io_cache_st *to_file, unsigned char *bufs)
1 by brian
clean slate
464
{
465
  int err;
481 by Brian Aker
Remove all of uchar.
466
  uint16_t len = _mi_keylength(info->keyinfo, (unsigned char*) bufs);
1 by brian
clean slate
467
468
  /* The following is safe as this is a local file */
481 by Brian Aker
Remove all of uchar.
469
  if ((err= my_b_write(to_file, (unsigned char*)&len, sizeof(len))))
1 by brian
clean slate
470
    return (err);
471
  if ((err= my_b_write(to_file,bufs, (uint) len)))
472
    return (err);
473
  return (0);
474
}
475
476
1165.1.73 by Stewart Smith
make write_keys_varlen() static to myisam/sort.cc
477
static int  write_keys_varlen(MI_SORT_PARAM *info,
481 by Brian Aker
Remove all of uchar.
478
				    register unsigned char **sort_keys,
482 by Brian Aker
Remove uint.
479
                                    uint32_t count, BUFFPEK *buffpek,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
480
				    internal::io_cache_st *tempfile)
1 by brian
clean slate
481
{
481 by Brian Aker
Remove all of uchar.
482
  unsigned char **end;
1 by brian
clean slate
483
  int err;
484
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
485
  internal::my_qsort2((unsigned char*) sort_keys,count,sizeof(unsigned char*),(qsort2_cmp) info->key_cmp,
1 by brian
clean slate
486
            info);
1909.1.2 by Brian Aker
Additional io_cache encapsulation.
487
  if (!my_b_inited(tempfile) && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
971.6.11 by Eric Day
Removed purecov messages.
488
    return(1);
1 by brian
clean slate
489
490
  buffpek->file_pos=my_b_tell(tempfile);
491
  buffpek->count=count;
492
  for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
493
  {
481 by Brian Aker
Remove all of uchar.
494
    if ((err= my_var_write(info,tempfile, (unsigned char*) *sort_keys)))
51.1.119 by Jay Pipes
DBUG symbol removal
495
      return(err);
1 by brian
clean slate
496
  }
51.1.119 by Jay Pipes
DBUG symbol removal
497
  return(0);
1 by brian
clean slate
498
} /* write_keys_varlen */
499
500
1165.1.71 by Stewart Smith
make write_key() static to myisam/sort.cc
501
static int  write_key(MI_SORT_PARAM *info, unsigned char *key,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
502
			    internal::io_cache_st *tempfile)
1 by brian
clean slate
503
{
482 by Brian Aker
Remove uint.
504
  uint32_t key_length=info->real_key_length;
1 by brian
clean slate
505
1909.1.2 by Brian Aker
Additional io_cache encapsulation.
506
  if (!my_b_inited(tempfile) && tempfile->open_cached_file(P_tmpdir, "ST", DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
51.1.119 by Jay Pipes
DBUG symbol removal
507
    return(1);
1 by brian
clean slate
508
481 by Brian Aker
Remove all of uchar.
509
  if (my_b_write(tempfile,(unsigned char*)&key_length,sizeof(key_length)) ||
510
      my_b_write(tempfile,(unsigned char*)key,(uint) key_length))
51.1.119 by Jay Pipes
DBUG symbol removal
511
    return(1);
512
  return(0);
1 by brian
clean slate
513
} /* write_key */
514
515
516
/* Write index */
517
1165.1.70 by Stewart Smith
make write_index() static to myisam/sort.cc
518
static int  write_index(MI_SORT_PARAM *info, register unsigned char **sort_keys,
482 by Brian Aker
Remove uint.
519
                              register uint32_t count)
1 by brian
clean slate
520
{
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
521
  internal::my_qsort2((unsigned char*) sort_keys,(size_t) count,sizeof(unsigned char*),
1 by brian
clean slate
522
           (qsort2_cmp) info->key_cmp,info);
523
  while (count--)
524
  {
525
    if ((*info->key_write)(info,*sort_keys++))
971.6.11 by Eric Day
Removed purecov messages.
526
      return(-1);
1 by brian
clean slate
527
  }
51.1.119 by Jay Pipes
DBUG symbol removal
528
  return(0);
1 by brian
clean slate
529
} /* write_index */
530
531
532
        /* Merge buffers to make < MERGEBUFF2 buffers */
533
1165.1.67 by Stewart Smith
make merge_many_buff() static to myisam/sort.cc
534
static int  merge_many_buff(MI_SORT_PARAM *info, uint32_t keys,
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
535
			    unsigned char **sort_keys, BUFFPEK *buffpek,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
536
			    size_t *maxbuffer, internal::io_cache_st *t_file)
1 by brian
clean slate
537
{
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
538
  uint32_t i;
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
539
  internal::io_cache_st t_file2, *from_file, *to_file, *temp;
1 by brian
clean slate
540
  BUFFPEK *lastbuff;
541
542
  if (*maxbuffer < MERGEBUFF2)
971.6.11 by Eric Day
Removed purecov messages.
543
    return(0);
1909.1.2 by Brian Aker
Additional io_cache encapsulation.
544
  if (flush_io_cache(t_file) || t_file2.open_cached_file(P_tmpdir, "ST",
1 by brian
clean slate
545
                       DISK_BUFFER_SIZE, info->sort_info->param->myf_rw))
971.6.11 by Eric Day
Removed purecov messages.
546
    return(1);
1 by brian
clean slate
547
548
  from_file= t_file ; to_file= &t_file2;
549
  while (*maxbuffer >= MERGEBUFF2)
550
  {
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
551
    from_file->reinit_io_cache(internal::READ_CACHE,0L,0,0);
552
    to_file->reinit_io_cache(internal::WRITE_CACHE,0L,0,0);
1 by brian
clean slate
553
    lastbuff=buffpek;
554
    for (i=0 ; i <= *maxbuffer-MERGEBUFF*3/2 ; i+=MERGEBUFF)
555
    {
556
      if (merge_buffers(info,keys,from_file,to_file,sort_keys,lastbuff++,
557
                        buffpek+i,buffpek+i+MERGEBUFF-1))
558
        goto cleanup;
559
    }
560
    if (merge_buffers(info,keys,from_file,to_file,sort_keys,lastbuff++,
561
                      buffpek+i,buffpek+ *maxbuffer))
971.6.11 by Eric Day
Removed purecov messages.
562
      break;
1 by brian
clean slate
563
    if (flush_io_cache(to_file))
971.6.11 by Eric Day
Removed purecov messages.
564
      break;
1 by brian
clean slate
565
    temp=from_file; from_file=to_file; to_file=temp;
566
    *maxbuffer= (int) (lastbuff-buffpek)-1;
567
  }
568
cleanup:
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
569
  to_file->close_cached_file();                   /* This holds old result */
1 by brian
clean slate
570
  if (to_file == t_file)
571
    *t_file=t_file2;                            /* Copy result file */
572
51.1.119 by Jay Pipes
DBUG symbol removal
573
  return(*maxbuffer >= MERGEBUFF2);        /* Return 1 if interrupted */
1 by brian
clean slate
574
} /* merge_many_buff */
575
576
577
/*
578
   Read data to buffer
579
580
  SYNOPSIS
581
    read_to_buffer()
582
    fromfile		File to read from
583
    buffpek		Where to read from
584
    sort_length		max length to read
585
  RESULT
586
    > 0	Ammount of bytes read
587
    -1	Error
588
*/
589
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
590
static uint32_t  read_to_buffer(internal::io_cache_st *fromfile, BUFFPEK *buffpek,
482 by Brian Aker
Remove uint.
591
                                  uint32_t sort_length)
1 by brian
clean slate
592
{
482 by Brian Aker
Remove uint.
593
  register uint32_t count;
594
  uint32_t length;
1 by brian
clean slate
595
1067.4.8 by Nathan Williams
Converted all usages of cmin/cmax in plugin directory to std::min/max.
596
  if ((count=(uint) min((ha_rows) buffpek->max_keys,buffpek->count)))
1 by brian
clean slate
597
  {
481 by Brian Aker
Remove all of uchar.
598
    if (my_pread(fromfile->file,(unsigned char*) buffpek->base,
1 by brian
clean slate
599
                 (length= sort_length*count),buffpek->file_pos,MYF_RW))
971.6.11 by Eric Day
Removed purecov messages.
600
      return((uint) -1);
1 by brian
clean slate
601
    buffpek->key=buffpek->base;
602
    buffpek->file_pos+= length;                 /* New filepos */
603
    buffpek->count-=    count;
604
    buffpek->mem_count= count;
605
  }
606
  return (count*sort_length);
607
} /* read_to_buffer */
608
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
609
static uint32_t  read_to_buffer_varlen(internal::io_cache_st *fromfile, BUFFPEK *buffpek,
482 by Brian Aker
Remove uint.
610
                                         uint32_t sort_length)
1 by brian
clean slate
611
{
482 by Brian Aker
Remove uint.
612
  register uint32_t count;
206 by Brian Aker
Removed final uint dead types.
613
  uint16_t length_of_key = 0;
482 by Brian Aker
Remove uint.
614
  uint32_t idx;
481 by Brian Aker
Remove all of uchar.
615
  unsigned char *buffp;
1 by brian
clean slate
616
1067.4.8 by Nathan Williams
Converted all usages of cmin/cmax in plugin directory to std::min/max.
617
  if ((count=(uint) min((ha_rows) buffpek->max_keys,buffpek->count)))
1 by brian
clean slate
618
  {
619
    buffp = buffpek->base;
620
621
    for (idx=1;idx<=count;idx++)
622
    {
481 by Brian Aker
Remove all of uchar.
623
      if (my_pread(fromfile->file,(unsigned char*)&length_of_key,sizeof(length_of_key),
1 by brian
clean slate
624
                   buffpek->file_pos,MYF_RW))
625
        return((uint) -1);
626
      buffpek->file_pos+=sizeof(length_of_key);
481 by Brian Aker
Remove all of uchar.
627
      if (my_pread(fromfile->file,(unsigned char*) buffp,length_of_key,
1 by brian
clean slate
628
                   buffpek->file_pos,MYF_RW))
629
        return((uint) -1);
630
      buffpek->file_pos+=length_of_key;
631
      buffp = buffp + sort_length;
632
    }
633
    buffpek->key=buffpek->base;
634
    buffpek->count-=    count;
635
    buffpek->mem_count= count;
636
  }
637
  return (count*sort_length);
638
} /* read_to_buffer_varlen */
639
640
1165.1.75 by Stewart Smith
make write_merge_key_varlen() static to myisam/sort.cc
641
static int  write_merge_key_varlen(MI_SORT_PARAM *info,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
642
					 internal::io_cache_st *to_file, unsigned char* key,
482 by Brian Aker
Remove uint.
643
                                         uint32_t sort_length, uint32_t count)
1 by brian
clean slate
644
{
482 by Brian Aker
Remove uint.
645
  uint32_t idx;
481 by Brian Aker
Remove all of uchar.
646
  unsigned char *bufs = key;
1 by brian
clean slate
647
648
  for (idx=1;idx<=count;idx++)
649
  {
650
    int err;
651
    if ((err= my_var_write(info, to_file, bufs)))
652
      return (err);
653
    bufs=bufs+sort_length;
654
  }
655
  return(0);
656
}
657
658
1165.1.74 by Stewart Smith
make write_merge_key() static to myisam/sort.cc
659
static int  write_merge_key(MI_SORT_PARAM *info,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
660
				  internal::io_cache_st *to_file, unsigned char *key,
482 by Brian Aker
Remove uint.
661
				  uint32_t sort_length, uint32_t count)
1 by brian
clean slate
662
{
779.3.1 by Monty Taylor
More cleanup.
663
  (void)info;
1 by brian
clean slate
664
  return my_b_write(to_file, key, (size_t) sort_length*count);
665
}
666
960.2.16 by Padraig O'Sullivan
Adding comments to the function object that is used as the comparison
667
/*
668
 * Function object to be used as the comparison function
669
 * for the priority queue in the merge_buffers method.
670
 */
960.2.8 by Padraig O'Sullivan
Adding a function object to be used as the comparison parameter for the
671
class compare_functor
672
{
960.2.12 by Padraig O'Sullivan
Making the class members of my function object have the correct type.
673
  qsort2_cmp key_compare;
960.2.8 by Padraig O'Sullivan
Adding a function object to be used as the comparison parameter for the
674
  void *key_compare_arg;
675
  public:
960.2.12 by Padraig O'Sullivan
Making the class members of my function object have the correct type.
676
  compare_functor(qsort2_cmp in_key_compare, void *in_compare_arg)
960.2.8 by Padraig O'Sullivan
Adding a function object to be used as the comparison parameter for the
677
    : key_compare(in_key_compare), key_compare_arg(in_compare_arg) { }
678
  inline bool operator()(const BUFFPEK *i, const BUFFPEK *j) const
679
  {
680
    int val= key_compare(key_compare_arg,
681
                      &i->key, &j->key);
682
    return (val >= 0);
683
  }
684
};
685
1 by brian
clean slate
686
/*
687
  Merge buffers to one buffer
688
  If to_file == 0 then use info->key_write
689
*/
690
1165.1.65 by Stewart Smith
make merge_buffers() static to myisam/sort.cc
691
static int
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
692
merge_buffers(MI_SORT_PARAM *info, uint32_t keys, internal::io_cache_st *from_file,
693
              internal::io_cache_st *to_file, unsigned char **sort_keys, BUFFPEK *lastbuff,
1 by brian
clean slate
694
              BUFFPEK *Fb, BUFFPEK *Tb)
695
{
696
  int error;
482 by Brian Aker
Remove uint.
697
  uint32_t sort_length,maxcount;
1 by brian
clean slate
698
  ha_rows count;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
699
  internal::my_off_t to_start_filepos= 0;
481 by Brian Aker
Remove all of uchar.
700
  unsigned char *strpos;
960.2.11 by Padraig O'Sullivan
Removing unused variables.
701
  BUFFPEK *buffpek;
960.2.8 by Padraig O'Sullivan
Adding a function object to be used as the comparison parameter for the
702
  priority_queue<BUFFPEK *, vector<BUFFPEK *>, compare_functor > 
960.2.16 by Padraig O'Sullivan
Adding comments to the function object that is used as the comparison
703
    queue(compare_functor((qsort2_cmp) info->key_cmp, static_cast<void *>(info)));
1 by brian
clean slate
704
  volatile int *killed= killed_ptr(info->sort_info->param);
705
706
  count=error=0;
707
  maxcount=keys/((uint) (Tb-Fb) +1);
51.1.119 by Jay Pipes
DBUG symbol removal
708
  assert(maxcount > 0);
1 by brian
clean slate
709
  if (to_file)
710
    to_start_filepos=my_b_tell(to_file);
481 by Brian Aker
Remove all of uchar.
711
  strpos=(unsigned char*) sort_keys;
1 by brian
clean slate
712
  sort_length=info->key_length;
713
714
  for (buffpek= Fb ; buffpek <= Tb ; buffpek++)
715
  {
716
    count+= buffpek->count;
717
    buffpek->base= strpos;
718
    buffpek->max_keys=maxcount;
719
    strpos+= (uint) (error=(int) info->read_to_buffer(from_file,buffpek,
720
                                                      sort_length));
721
    if (error == -1)
971.6.11 by Eric Day
Removed purecov messages.
722
      goto err;
960.2.7 by Padraig O'Sullivan
Removed all traces of QUEUE from the codebase. We are using
723
    queue.push(buffpek);
1 by brian
clean slate
724
  }
725
960.2.7 by Padraig O'Sullivan
Removed all traces of QUEUE from the codebase. We are using
726
  while (queue.size() > 1)
1 by brian
clean slate
727
  {
728
    for (;;)
729
    {
730
      if (*killed)
731
      {
732
        error=1; goto err;
733
      }
960.2.7 by Padraig O'Sullivan
Removed all traces of QUEUE from the codebase. We are using
734
      buffpek= queue.top();
1 by brian
clean slate
735
      if (to_file)
736
      {
481 by Brian Aker
Remove all of uchar.
737
        if (info->write_key(info,to_file,(unsigned char*) buffpek->key,
1 by brian
clean slate
738
                            (uint) sort_length,1))
739
        {
971.6.11 by Eric Day
Removed purecov messages.
740
          error=1; goto err;
1 by brian
clean slate
741
        }
742
      }
743
      else
744
      {
745
        if ((*info->key_write)(info,(void*) buffpek->key))
746
        {
971.6.11 by Eric Day
Removed purecov messages.
747
          error=1; goto err;
1 by brian
clean slate
748
        }
749
      }
750
      buffpek->key+=sort_length;
751
      if (! --buffpek->mem_count)
752
      {
753
        if (!(error=(int) info->read_to_buffer(from_file,buffpek,sort_length)))
754
        {
960.2.7 by Padraig O'Sullivan
Removed all traces of QUEUE from the codebase. We are using
755
          queue.pop();
1 by brian
clean slate
756
          break;                /* One buffer have been removed */
757
        }
758
      }
759
      else if (error == -1)
971.6.11 by Eric Day
Removed purecov messages.
760
        goto err;
960.2.7 by Padraig O'Sullivan
Removed all traces of QUEUE from the codebase. We are using
761
      /* Top element has been replaced */
762
      queue.pop();
763
      queue.push(buffpek);
1 by brian
clean slate
764
    }
765
  }
960.2.7 by Padraig O'Sullivan
Removed all traces of QUEUE from the codebase. We are using
766
  buffpek= queue.top();
481 by Brian Aker
Remove all of uchar.
767
  buffpek->base=(unsigned char *) sort_keys;
1 by brian
clean slate
768
  buffpek->max_keys=keys;
769
  do
770
  {
771
    if (to_file)
772
    {
481 by Brian Aker
Remove all of uchar.
773
      if (info->write_key(info,to_file,(unsigned char*) buffpek->key,
1 by brian
clean slate
774
                         sort_length,buffpek->mem_count))
775
      {
971.6.11 by Eric Day
Removed purecov messages.
776
        error=1; goto err;
1 by brian
clean slate
777
      }
778
    }
779
    else
780
    {
481 by Brian Aker
Remove all of uchar.
781
      register unsigned char *end;
1 by brian
clean slate
782
      strpos= buffpek->key;
783
      for (end=strpos+buffpek->mem_count*sort_length;
784
           strpos != end ;
785
           strpos+=sort_length)
786
      {
787
        if ((*info->key_write)(info,(void*) strpos))
788
        {
971.6.11 by Eric Day
Removed purecov messages.
789
          error=1; goto err;
1 by brian
clean slate
790
        }
791
      }
792
    }
793
  }
794
  while ((error=(int) info->read_to_buffer(from_file,buffpek,sort_length)) != -1 &&
795
         error != 0);
796
797
  lastbuff->count=count;
798
  if (to_file)
799
    lastbuff->file_pos=to_start_filepos;
800
err:
51.1.119 by Jay Pipes
DBUG symbol removal
801
  return(error);
1 by brian
clean slate
802
} /* merge_buffers */
803
804
805
        /* Do a merge to output-file (save only positions) */
806
1165.1.66 by Stewart Smith
make merge_index() static to myisam/sort.cc
807
static int
482 by Brian Aker
Remove uint.
808
merge_index(MI_SORT_PARAM *info, uint32_t keys, unsigned char **sort_keys,
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
809
            BUFFPEK *buffpek, int maxbuffer, internal::io_cache_st *tempfile)
1 by brian
clean slate
810
{
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
811
  if (merge_buffers(info,keys,tempfile,(internal::io_cache_st*) 0,sort_keys,buffpek,buffpek,
1 by brian
clean slate
812
                    buffpek+maxbuffer))
971.6.11 by Eric Day
Removed purecov messages.
813
    return(1);
51.1.119 by Jay Pipes
DBUG symbol removal
814
  return(0);
1 by brian
clean slate
815
} /* merge_index */
816