| 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 "myisamdef.h" |
| 18 | |
| 19 | #ifdef HAVE_RTREE_KEYS |
| 20 | #include "rt_index.h" |
| 21 | #include "rt_key.h" |
| 22 | #include "rt_mbr.h" |
| 23 | |
| 24 | /* |
| 25 | Add key to the page |
| 26 | |
| 27 | RESULT VALUES |
| 28 | -1 Error |
| 29 | 0 Not split |
| 30 | 1 Split |
| 31 | */ |
| 32 | |
| 33 | int rtree_add_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key, |
| 34 | uint key_length, uchar *page_buf, my_off_t *new_page) |
| 35 | { |
| 36 | uint page_size = mi_getint(page_buf); |
| 37 | uint nod_flag = mi_test_if_nod(page_buf); |
| 38 | DBUG_ENTER("rtree_add_key" ); |
| 39 | |
| 40 | if (page_size + key_length + info->s->base.rec_reflength <= |
| 41 | keyinfo->block_length) |
| 42 | { |
| 43 | /* split won't be necessary */ |
| 44 | if (nod_flag) |
| 45 | { |
| 46 | /* save key */ |
| 47 | DBUG_ASSERT(_mi_kpos(nod_flag, key) < info->state->key_file_length); |
| 48 | memcpy(rt_PAGE_END(page_buf), key - nod_flag, key_length + nod_flag); |
| 49 | page_size += key_length + nod_flag; |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | /* save key */ |
| 54 | DBUG_ASSERT(_mi_dpos(info, nod_flag, key + key_length + |
| 55 | info->s->base.rec_reflength) < |
| 56 | info->state->data_file_length + info->s->base.pack_reclength); |
| 57 | memcpy(rt_PAGE_END(page_buf), key, key_length + |
| 58 | info->s->base.rec_reflength); |
| 59 | page_size += key_length + info->s->base.rec_reflength; |
| 60 | } |
| 61 | mi_putint(page_buf, page_size, nod_flag); |
| 62 | DBUG_RETURN(0); |
| 63 | } |
| 64 | |
| 65 | DBUG_RETURN((rtree_split_page(info, keyinfo, page_buf, key, key_length, |
| 66 | new_page) ? -1 : 1)); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | Delete key from the page |
| 71 | */ |
| 72 | int rtree_delete_key(MI_INFO *info, uchar *page_buf, uchar *key, |
| 73 | uint key_length, uint nod_flag) |
| 74 | { |
| 75 | uint16 page_size = mi_getint(page_buf); |
| 76 | uchar *key_start; |
| 77 | |
| 78 | key_start= key - nod_flag; |
| 79 | if (!nod_flag) |
| 80 | key_length += info->s->base.rec_reflength; |
| 81 | |
| 82 | memmove(key_start, key + key_length, page_size - key_length - |
| 83 | (key - page_buf)); |
| 84 | page_size-= key_length + nod_flag; |
| 85 | |
| 86 | mi_putint(page_buf, page_size, nod_flag); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | Calculate and store key MBR |
| 93 | */ |
| 94 | |
| 95 | int rtree_set_key_mbr(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key, |
| 96 | uint key_length, my_off_t child_page) |
| 97 | { |
| 98 | DBUG_ENTER("rtree_set_key_mbr" ); |
| 99 | |
| 100 | if (!_mi_fetch_keypage(info, keyinfo, child_page, |
| 101 | DFLT_INIT_HITS, info->buff, 0)) |
| 102 | DBUG_RETURN(-1); /* purecov: inspected */ |
| 103 | |
| 104 | DBUG_RETURN(rtree_page_mbr(info, keyinfo->seg, info->buff, key, key_length)); |
| 105 | } |
| 106 | |
| 107 | #endif /*HAVE_RTREE_KEYS*/ |
| 108 | |