Commit a0bc3b7e authored by Michael Widenius's avatar Michael Widenius Committed by Monty

Change read_to_buffer to use ulong instead of uint

This is mostly to document that read_to_buffer can read more than 65K.
Also changed merge_buffers to return bool instead of int
parent 062a3176
...@@ -1508,21 +1508,21 @@ int merge_many_buff(Sort_param *param, uchar *sort_buffer, ...@@ -1508,21 +1508,21 @@ int merge_many_buff(Sort_param *param, uchar *sort_buffer,
Read data to buffer. Read data to buffer.
@retval Number of bytes read @retval Number of bytes read
(uint)-1 if something goes wrong (ulong)-1 if something goes wrong
*/ */
uint read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek, ulong read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
uint rec_length) uint rec_length)
{ {
uint count; register ulong count;
uint length= 0; ulong length= 0;
if ((count=(uint) MY_MIN((ha_rows) buffpek->max_keys,buffpek->count))) if ((count= (ulong) MY_MIN((ha_rows) buffpek->max_keys,buffpek->count)))
{ {
length= rec_length*count; length= rec_length*count;
if (unlikely(my_b_pread(fromfile, (uchar*) buffpek->base, length, if (unlikely(my_b_pread(fromfile, (uchar*) buffpek->base, length,
buffpek->file_pos))) buffpek->file_pos)))
return ((uint) -1); return ((ulong) -1);
buffpek->key=buffpek->base; buffpek->key=buffpek->base;
buffpek->file_pos+= length; /* New filepos */ buffpek->file_pos+= length; /* New filepos */
buffpek->count-= count; buffpek->count-= count;
...@@ -1582,18 +1582,18 @@ void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint key_length) ...@@ -1582,18 +1582,18 @@ void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint key_length)
@retval @retval
0 OK 0 OK
@retval @retval
other error 1 ERROR
*/ */
int merge_buffers(Sort_param *param, IO_CACHE *from_file, bool merge_buffers(Sort_param *param, IO_CACHE *from_file,
IO_CACHE *to_file, uchar *sort_buffer, IO_CACHE *to_file, uchar *sort_buffer,
BUFFPEK *lastbuff, BUFFPEK *Fb, BUFFPEK *Tb, BUFFPEK *lastbuff, BUFFPEK *Fb, BUFFPEK *Tb,
int flag) int flag)
{ {
int error; bool error= 0;
uint rec_length,res_length,offset; uint rec_length,res_length,offset;
size_t sort_length; size_t sort_length;
ulong maxcount; ulong maxcount, bytes_read;
ha_rows max_rows,org_max_rows; ha_rows max_rows,org_max_rows;
my_off_t to_start_filepos; my_off_t to_start_filepos;
uchar *strpos; uchar *strpos;
...@@ -1611,7 +1611,6 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, ...@@ -1611,7 +1611,6 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file,
thd->inc_status_sort_merge_passes(); thd->inc_status_sort_merge_passes();
thd->query_plan_fsort_passes++; thd->query_plan_fsort_passes++;
error=0;
rec_length= param->rec_length; rec_length= param->rec_length;
res_length= param->res_length; res_length= param->res_length;
sort_length= param->sort_length; sort_length= param->sort_length;
...@@ -1639,18 +1638,18 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, ...@@ -1639,18 +1638,18 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file,
cmp= get_ptr_compare(sort_length); cmp= get_ptr_compare(sort_length);
first_cmp_arg= (void*) &sort_length; first_cmp_arg= (void*) &sort_length;
} }
if (init_queue(&queue, (uint) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0, if (unlikely(init_queue(&queue, (uint) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
(queue_compare) cmp, first_cmp_arg, 0, 0)) (queue_compare) cmp, first_cmp_arg, 0, 0)))
DBUG_RETURN(1); /* purecov: inspected */ DBUG_RETURN(1); /* purecov: inspected */
for (buffpek= Fb ; buffpek <= Tb ; buffpek++) for (buffpek= Fb ; buffpek <= Tb ; buffpek++)
{ {
buffpek->base= strpos; buffpek->base= strpos;
buffpek->max_keys= maxcount; buffpek->max_keys= maxcount;
strpos+= bytes_read= read_to_buffer(from_file, buffpek, rec_length);
(uint) (error= (int) read_to_buffer(from_file, buffpek, rec_length)); if (unlikely(bytes_read == (ulong) -1))
if (unlikely(error == -1))
goto err; /* purecov: inspected */ goto err; /* purecov: inspected */
strpos+= bytes_read;
buffpek->max_keys= buffpek->mem_count; // If less data in buffers than expected buffpek->max_keys= buffpek->mem_count; // If less data in buffers than expected
queue_insert(&queue, (uchar*) buffpek); queue_insert(&queue, (uchar*) buffpek);
} }
...@@ -1670,13 +1669,13 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, ...@@ -1670,13 +1669,13 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file,
buffpek->key+= rec_length; buffpek->key+= rec_length;
if (! --buffpek->mem_count) if (! --buffpek->mem_count)
{ {
if (unlikely(!(error= (int) read_to_buffer(from_file, buffpek, if (unlikely(!(bytes_read= read_to_buffer(from_file, buffpek,
rec_length)))) rec_length))))
{ {
(void) queue_remove_top(&queue); (void) queue_remove_top(&queue);
reuse_freed_buff(&queue, buffpek, rec_length); reuse_freed_buff(&queue, buffpek, rec_length);
} }
else if (unlikely(error == -1)) else if (unlikely(bytes_read == (ulong) -1))
goto err; /* purecov: inspected */ goto err; /* purecov: inspected */
} }
queue_replace_top(&queue); // Top element has been used queue_replace_top(&queue); // Top element has been used
...@@ -1687,9 +1686,8 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, ...@@ -1687,9 +1686,8 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file,
while (queue.elements > 1) while (queue.elements > 1)
{ {
if (killable && unlikely(thd->check_killed())) if (killable && unlikely(thd->check_killed()))
{ goto err; /* purecov: inspected */
error= 1; goto err; /* purecov: inspected */
}
for (;;) for (;;)
{ {
buffpek= (BUFFPEK*) queue_top(&queue); buffpek= (BUFFPEK*) queue_top(&queue);
...@@ -1726,9 +1724,7 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, ...@@ -1726,9 +1724,7 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file,
if (!check_dupl_count || dupl_count >= min_dupl_count) if (!check_dupl_count || dupl_count >= min_dupl_count)
{ {
if (my_b_write(to_file, src+wr_offset, wr_len)) if (my_b_write(to_file, src+wr_offset, wr_len))
{ goto err; /* purecov: inspected */
error=1; goto err; /* purecov: inspected */
}
} }
if (cmp) if (cmp)
{ {
...@@ -1739,7 +1735,7 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, ...@@ -1739,7 +1735,7 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file,
} }
if (!--max_rows) if (!--max_rows)
{ {
error= 0; /* purecov: inspected */ /* Nothing more to do */
goto end; /* purecov: inspected */ goto end; /* purecov: inspected */
} }
...@@ -1747,14 +1743,14 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, ...@@ -1747,14 +1743,14 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file,
buffpek->key+= rec_length; buffpek->key+= rec_length;
if (! --buffpek->mem_count) if (! --buffpek->mem_count)
{ {
if (unlikely(!(error= (int) read_to_buffer(from_file, buffpek, if (unlikely(!(bytes_read= read_to_buffer(from_file, buffpek,
rec_length)))) rec_length))))
{ {
(void) queue_remove_top(&queue); (void) queue_remove_top(&queue);
reuse_freed_buff(&queue, buffpek, rec_length); reuse_freed_buff(&queue, buffpek, rec_length);
break; /* One buffer have been removed */ break; /* One buffer have been removed */
} }
else if (error == -1) else if (unlikely(bytes_read == (ulong) -1))
goto err; /* purecov: inspected */ goto err; /* purecov: inspected */
} }
queue_replace_top(&queue); /* Top element has been replaced */ queue_replace_top(&queue); /* Top element has been replaced */
...@@ -1790,14 +1786,9 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, ...@@ -1790,14 +1786,9 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file,
{ {
src= unique_buff; src= unique_buff;
if (my_b_write(to_file, src+wr_offset, wr_len)) if (my_b_write(to_file, src+wr_offset, wr_len))
{ goto err; /* purecov: inspected */
error=1; goto err; /* purecov: inspected */
}
if (!--max_rows) if (!--max_rows)
{
error= 0;
goto end; goto end;
}
} }
} }
...@@ -1813,9 +1804,7 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, ...@@ -1813,9 +1804,7 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file,
{ {
if (my_b_write(to_file, (uchar*) buffpek->key, if (my_b_write(to_file, (uchar*) buffpek->key,
(size_t)(rec_length*buffpek->mem_count))) (size_t)(rec_length*buffpek->mem_count)))
{ goto err; /* purecov: inspected */
error= 1; goto err; /* purecov: inspected */
}
} }
else else
{ {
...@@ -1832,21 +1821,26 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file, ...@@ -1832,21 +1821,26 @@ int merge_buffers(Sort_param *param, IO_CACHE *from_file,
continue; continue;
} }
if (my_b_write(to_file, src, wr_len)) if (my_b_write(to_file, src, wr_len))
{ goto err;
error=1; goto err;
}
} }
} }
} }
while (likely((error=(int) read_to_buffer(from_file, buffpek, rec_length)) while (likely(!(error=
!= -1 && error != 0)); (bytes_read= read_to_buffer(from_file, buffpek,
rec_length)) == (ulong) -1)) &&
bytes_read != 0);
end: end:
lastbuff->count= MY_MIN(org_max_rows-max_rows, param->max_rows); lastbuff->count= MY_MIN(org_max_rows-max_rows, param->max_rows);
lastbuff->file_pos= to_start_filepos; lastbuff->file_pos= to_start_filepos;
err: cleanup:
delete_queue(&queue); delete_queue(&queue);
DBUG_RETURN(error); DBUG_RETURN(error);
err:
error= 1;
goto cleanup;
} /* merge_buffers */ } /* merge_buffers */
......
...@@ -100,12 +100,12 @@ class Sort_param { ...@@ -100,12 +100,12 @@ class Sort_param {
int merge_many_buff(Sort_param *param, uchar *sort_buffer, int merge_many_buff(Sort_param *param, uchar *sort_buffer,
BUFFPEK *buffpek, BUFFPEK *buffpek,
uint *maxbuffer, IO_CACHE *t_file); uint *maxbuffer, IO_CACHE *t_file);
uint read_to_buffer(IO_CACHE *fromfile,BUFFPEK *buffpek, ulong read_to_buffer(IO_CACHE *fromfile,BUFFPEK *buffpek,
uint sort_length); uint sort_length);
int merge_buffers(Sort_param *param,IO_CACHE *from_file, bool merge_buffers(Sort_param *param,IO_CACHE *from_file,
IO_CACHE *to_file, uchar *sort_buffer, IO_CACHE *to_file, uchar *sort_buffer,
BUFFPEK *lastbuff,BUFFPEK *Fb, BUFFPEK *lastbuff,BUFFPEK *Fb,
BUFFPEK *Tb,int flag); BUFFPEK *Tb,int flag);
int merge_index(Sort_param *param, uchar *sort_buffer, int merge_index(Sort_param *param, uchar *sort_buffer,
BUFFPEK *buffpek, uint maxbuffer, BUFFPEK *buffpek, uint maxbuffer,
IO_CACHE *tempfile, IO_CACHE *outfile); IO_CACHE *tempfile, IO_CACHE *outfile);
......
...@@ -509,7 +509,7 @@ static bool merge_walk(uchar *merge_buffer, size_t merge_buffer_size, ...@@ -509,7 +509,7 @@ static bool merge_walk(uchar *merge_buffer, size_t merge_buffer_size,
key_length); key_length);
/* if piece_size is aligned reuse_freed_buffer will always hit */ /* if piece_size is aligned reuse_freed_buffer will always hit */
uint piece_size= max_key_count_per_piece * key_length; uint piece_size= max_key_count_per_piece * key_length;
uint bytes_read; /* to hold return value of read_to_buffer */ ulong bytes_read; /* to hold return value of read_to_buffer */
BUFFPEK *top; BUFFPEK *top;
int res= 1; int res= 1;
uint cnt_ofs= key_length - (with_counters ? sizeof(element_count) : 0); uint cnt_ofs= key_length - (with_counters ? sizeof(element_count) : 0);
...@@ -525,7 +525,7 @@ static bool merge_walk(uchar *merge_buffer, size_t merge_buffer_size, ...@@ -525,7 +525,7 @@ static bool merge_walk(uchar *merge_buffer, size_t merge_buffer_size,
top->base= merge_buffer + (top - begin) * piece_size; top->base= merge_buffer + (top - begin) * piece_size;
top->max_keys= max_key_count_per_piece; top->max_keys= max_key_count_per_piece;
bytes_read= read_to_buffer(file, top, key_length); bytes_read= read_to_buffer(file, top, key_length);
if (unlikely(bytes_read == (uint) (-1))) if (unlikely(bytes_read == (ulong) -1))
goto end; goto end;
DBUG_ASSERT(bytes_read); DBUG_ASSERT(bytes_read);
queue_insert(&queue, (uchar *) top); queue_insert(&queue, (uchar *) top);
...@@ -554,9 +554,9 @@ static bool merge_walk(uchar *merge_buffer, size_t merge_buffer_size, ...@@ -554,9 +554,9 @@ static bool merge_walk(uchar *merge_buffer, size_t merge_buffer_size,
memcpy(save_key_buff, old_key, key_length); memcpy(save_key_buff, old_key, key_length);
old_key= save_key_buff; old_key= save_key_buff;
bytes_read= read_to_buffer(file, top, key_length); bytes_read= read_to_buffer(file, top, key_length);
if (unlikely(bytes_read == (uint) (-1))) if (unlikely(bytes_read == (ulong) -1))
goto end; goto end;
else if (bytes_read > 0) /* top->key, top->mem_count are reset */ else if (bytes_read) /* top->key, top->mem_count are reset */
queue_replace_top(&queue); /* in read_to_buffer */ queue_replace_top(&queue); /* in read_to_buffer */
else else
{ {
...@@ -602,7 +602,7 @@ static bool merge_walk(uchar *merge_buffer, size_t merge_buffer_size, ...@@ -602,7 +602,7 @@ static bool merge_walk(uchar *merge_buffer, size_t merge_buffer_size,
} }
while (--top->mem_count); while (--top->mem_count);
bytes_read= read_to_buffer(file, top, key_length); bytes_read= read_to_buffer(file, top, key_length);
if (unlikely(bytes_read == (uint) (-1))) if (unlikely(bytes_read == (ulong) -1))
goto end; goto end;
} }
while (bytes_read); while (bytes_read);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment