| 1 | /* Copyright (c) 2000, 2002, 2005-2007 MySQL AB |
| 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 Street, Fifth Floor, Boston, MA 02110-1301, USA */ |
| 16 | |
| 17 | #include "heapdef.h" |
| 18 | |
| 19 | /* Read next record with the same key */ |
| 20 | |
| 21 | int heap_rnext(HP_INFO *info, uchar *record) |
| 22 | { |
| 23 | uchar *pos; |
| 24 | HP_SHARE *share=info->s; |
| 25 | HP_KEYDEF *keyinfo; |
| 26 | DBUG_ENTER("heap_rnext" ); |
| 27 | |
| 28 | if (info->lastinx < 0) |
| 29 | DBUG_RETURN(my_errno=HA_ERR_WRONG_INDEX); |
| 30 | |
| 31 | keyinfo = share->keydef + info->lastinx; |
| 32 | if (keyinfo->algorithm == HA_KEY_ALG_BTREE) |
| 33 | { |
| 34 | heap_rb_param custom_arg; |
| 35 | |
| 36 | /* If no active record and last was not deleted */ |
| 37 | if (!(info->update & (HA_STATE_AKTIV | HA_STATE_NO_KEY | |
| 38 | HA_STATE_DELETED))) |
| 39 | { |
| 40 | if (info->update & HA_STATE_NEXT_FOUND) |
| 41 | pos= 0; /* Can't search after last row */ |
| 42 | else |
| 43 | { |
| 44 | /* Last was 'prev' before first record; search after first record */ |
| 45 | pos= tree_search_edge(&keyinfo->rb_tree, info->parents, |
| 46 | &info->last_pos, offsetof(TREE_ELEMENT, left)); |
| 47 | } |
| 48 | } |
| 49 | else if (info->last_pos) |
| 50 | { |
| 51 | /* |
| 52 | We enter this branch for non-DELETE queries after heap_rkey() |
| 53 | or heap_rfirst(). As last key position (info->last_pos) is available, |
| 54 | we only need to climb the tree using tree_search_next(). |
| 55 | */ |
| 56 | pos = tree_search_next(&keyinfo->rb_tree, &info->last_pos, |
| 57 | offsetof(TREE_ELEMENT, left), |
| 58 | offsetof(TREE_ELEMENT, right)); |
| 59 | } |
| 60 | else if (!info->lastkey_len) |
| 61 | { |
| 62 | /* |
| 63 | We enter this branch only for DELETE queries after heap_rfirst(). E.g. |
| 64 | DELETE FROM t1 WHERE a<10. As last key position is not available |
| 65 | (last key is removed by heap_delete()), we must restart search as it |
| 66 | is done in heap_rfirst(). |
| 67 | |
| 68 | It should be safe to handle this situation without this branch. That is |
| 69 | branch below should find smallest element in a tree as lastkey_len is |
| 70 | zero. tree_search_edge() is a kind of optimisation here as it should be |
| 71 | faster than tree_search_key(). |
| 72 | */ |
| 73 | pos= tree_search_edge(&keyinfo->rb_tree, info->parents, |
| 74 | &info->last_pos, offsetof(TREE_ELEMENT, left)); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | /* |
| 79 | We enter this branch only for DELETE queries after heap_rkey(). E.g. |
| 80 | DELETE FROM t1 WHERE a=10. As last key position is not available |
| 81 | (last key is removed by heap_delete()), we must restart search as it |
| 82 | is done in heap_rkey(). |
| 83 | */ |
| 84 | custom_arg.keyseg = keyinfo->seg; |
| 85 | custom_arg.key_length = info->lastkey_len; |
| 86 | custom_arg.search_flag = SEARCH_SAME | SEARCH_FIND; |
| 87 | info->last_find_flag= HA_READ_KEY_OR_NEXT; |
| 88 | pos = tree_search_key(&keyinfo->rb_tree, info->lastkey, info->parents, |
| 89 | &info->last_pos, info->last_find_flag, &custom_arg); |
| 90 | } |
| 91 | if (pos) |
| 92 | { |
| 93 | memcpy(&pos, pos + (*keyinfo->get_key_length)(keyinfo, pos), |
| 94 | sizeof(uchar*)); |
| 95 | info->current_ptr = pos; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | my_errno = HA_ERR_KEY_NOT_FOUND; |
| 100 | } |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | if (info->current_hash_ptr) |
| 105 | pos= hp_search_next(info, keyinfo, info->lastkey, |
| 106 | info->current_hash_ptr); |
| 107 | else |
| 108 | { |
| 109 | if (!info->current_ptr && (info->update & HA_STATE_NEXT_FOUND)) |
| 110 | { |
| 111 | pos=0; /* Read next after last */ |
| 112 | my_errno=HA_ERR_KEY_NOT_FOUND; |
| 113 | } |
| 114 | else if (!info->current_ptr) /* Deleted or first call */ |
| 115 | pos= hp_search(info, keyinfo, info->lastkey, 0); |
| 116 | else |
| 117 | pos= hp_search(info, keyinfo, info->lastkey, 1); |
| 118 | } |
| 119 | } |
| 120 | if (!pos) |
| 121 | { |
| 122 | info->update=HA_STATE_NEXT_FOUND; /* For heap_rprev */ |
| 123 | if (my_errno == HA_ERR_KEY_NOT_FOUND) |
| 124 | my_errno=HA_ERR_END_OF_FILE; |
| 125 | DBUG_RETURN(my_errno); |
| 126 | } |
| 127 | memcpy(record,pos,(size_t) share->reclength); |
| 128 | info->update=HA_STATE_AKTIV | HA_STATE_NEXT_FOUND; |
| 129 | DBUG_RETURN(0); |
| 130 | } |
| 131 | |