| 1 | /* Copyright (c) 2000-2003, 2005-2008 MySQL AB, 2009 Sun Microsystems, Inc. |
| 2 | Use is subject to license terms. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; version 2 of the License. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 16 | |
| 17 | /* Read record based on a key */ |
| 18 | |
| 19 | /* |
| 20 | * HA_READ_KEY_EXACT => SEARCH_BIGGER |
| 21 | * HA_READ_KEY_OR_NEXT => SEARCH_BIGGER |
| 22 | * HA_READ_AFTER_KEY => SEARCH_BIGGER |
| 23 | * HA_READ_PREFIX => SEARCH_BIGGER |
| 24 | * HA_READ_KEY_OR_PREV => SEARCH_SMALLER |
| 25 | * HA_READ_BEFORE_KEY => SEARCH_SMALLER |
| 26 | * HA_READ_PREFIX_LAST => SEARCH_SMALLER |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | #include "myrg_def.h" |
| 31 | |
| 32 | /* todo: we could store some additional info to speedup lookups: |
| 33 | column (key, keyseg) can be constant per table |
| 34 | it can also be increasing (table1.val > table2.val > ...), |
| 35 | or decreasing, <=, >=, etc. |
| 36 | SerG |
| 37 | */ |
| 38 | |
| 39 | int myrg_rkey(MYRG_INFO *info,uchar *buf,int inx, const uchar *key, |
| 40 | key_part_map keypart_map, enum ha_rkey_function search_flag) |
| 41 | { |
| 42 | uchar *UNINIT_VAR(key_buff); |
| 43 | uint UNINIT_VAR(pack_key_length); |
| 44 | uint16 UNINIT_VAR(last_used_keyseg); |
| 45 | MYRG_TABLE *table; |
| 46 | MI_INFO *mi; |
| 47 | int err; |
| 48 | DBUG_ENTER("myrg_rkey" ); |
| 49 | |
| 50 | if (_myrg_init_queue(info,inx,search_flag)) |
| 51 | DBUG_RETURN(my_errno); |
| 52 | |
| 53 | for (table=info->open_tables ; table != info->end_table ; table++) |
| 54 | { |
| 55 | mi=table->table; |
| 56 | |
| 57 | if (table == info->open_tables) |
| 58 | { |
| 59 | err=mi_rkey(mi, 0, inx, key, keypart_map, search_flag); |
| 60 | /* Get the saved packed key and packed key length. */ |
| 61 | key_buff=(uchar*) mi->lastkey+mi->s->base.max_key_length; |
| 62 | pack_key_length=mi->pack_key_length; |
| 63 | last_used_keyseg= mi->last_used_keyseg; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | mi->once_flags|= USE_PACKED_KEYS; |
| 68 | mi->last_used_keyseg= last_used_keyseg; |
| 69 | err=mi_rkey(mi, 0, inx, key_buff, pack_key_length, search_flag); |
| 70 | } |
| 71 | info->last_used_table=table+1; |
| 72 | |
| 73 | if (err) |
| 74 | { |
| 75 | if (err == HA_ERR_KEY_NOT_FOUND) |
| 76 | continue; |
| 77 | DBUG_PRINT("exit" , ("err: %d" , err)); |
| 78 | DBUG_RETURN(err); |
| 79 | } |
| 80 | /* adding to queue */ |
| 81 | queue_insert(&(info->by_key),(uchar *)table); |
| 82 | |
| 83 | } |
| 84 | |
| 85 | DBUG_PRINT("info" , ("tables with matches: %u" , info->by_key.elements)); |
| 86 | if (!info->by_key.elements) |
| 87 | DBUG_RETURN(HA_ERR_KEY_NOT_FOUND); |
| 88 | |
| 89 | mi=(info->current_table=(MYRG_TABLE *)queue_top(&(info->by_key)))->table; |
| 90 | mi->once_flags|= RRND_PRESERVE_LASTINX; |
| 91 | DBUG_PRINT("info" , ("using table no: %d" , |
| 92 | (int) (info->current_table - info->open_tables + 1))); |
| 93 | DBUG_DUMP("result key" , (uchar*) mi->lastkey, mi->lastkey_length); |
| 94 | DBUG_RETURN(_myrg_mi_read_record(mi,buf)); |
| 95 | } |
| 96 | |