1/* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
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
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include "myrg_def.h"
17
18static int queue_key_cmp(void *keyseg, uchar *a, uchar *b)
19{
20 MYRG_TABLE *ma= (MYRG_TABLE *)a;
21 MYRG_TABLE *mb= (MYRG_TABLE *)b;
22 MI_INFO *aa= ma->table;
23 MI_INFO *bb= mb->table;
24 uint not_used[2];
25 int ret= ha_key_cmp((HA_KEYSEG *)keyseg, aa->lastkey, bb->lastkey,
26 USE_WHOLE_KEY, SEARCH_FIND, not_used);
27 if (ret < 0)
28 return -1;
29 if (ret > 0)
30 return 1;
31
32 /*
33 If index tuples have the same values, let the record with least rowid
34 value be "smaller", so index scans return records ordered by (keytuple,
35 rowid). This is used by index_merge access method, grep for ROR in
36 sql/opt_range.cc for details.
37 */
38 return (ma->file_offset < mb->file_offset)? -1 : (ma->file_offset >
39 mb->file_offset) ? 1 : 0;
40} /* queue_key_cmp */
41
42
43int _myrg_init_queue(MYRG_INFO *info,int inx,enum ha_rkey_function search_flag)
44{
45 int error=0;
46 QUEUE *q= &(info->by_key);
47
48 if (inx < (int) info->keys)
49 {
50 if (!is_queue_inited(q))
51 {
52 if (init_queue(q,info->tables, 0,
53 (myisam_readnext_vec[search_flag] == SEARCH_SMALLER),
54 queue_key_cmp,
55 info->open_tables->table->s->keyinfo[inx].seg, 0, 0))
56 error=my_errno;
57 }
58 else
59 {
60 if (reinit_queue(q,info->tables, 0,
61 (myisam_readnext_vec[search_flag] == SEARCH_SMALLER),
62 queue_key_cmp,
63 info->open_tables->table->s->keyinfo[inx].seg, 0, 0))
64 error=my_errno;
65 }
66 }
67 else
68 {
69 /*
70 inx may be bigger than info->keys if there are no underlying tables
71 defined. In this case we should return empty result. As we check for
72 underlying tables conformance when we open a table, we may not enter
73 this branch with underlying table that has less keys than merge table
74 have.
75 */
76 DBUG_ASSERT(!info->tables);
77 error= my_errno= HA_ERR_END_OF_FILE;
78 }
79 return error;
80}
81
82int _myrg_mi_read_record(MI_INFO *info, uchar *buf)
83{
84 if (!(*info->read_record)(info,info->lastpos,buf))
85 {
86 info->update|= HA_STATE_AKTIV; /* Record is read */
87 return 0;
88 }
89 return my_errno;
90}
91