1/* Copyright (C) 2006 MySQL AB & Ramil Kalimullin
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 Street, Fifth Floor, Boston, MA 02111-1301 USA */
15
16#include "maria_def.h"
17#include "trnman.h"
18#include "ma_key_recover.h"
19
20#ifdef HAVE_RTREE_KEYS
21#include "ma_rt_index.h"
22#include "ma_rt_key.h"
23#include "ma_rt_mbr.h"
24
25/*
26 Add key to the page
27
28 RESULT VALUES
29 -1 Error
30 0 Not split
31 1 Split
32*/
33
34int maria_rtree_add_key(const MARIA_KEY *key, MARIA_PAGE *page,
35 my_off_t *new_page)
36{
37 MARIA_HA *info= page->info;
38 MARIA_SHARE *share= info->s;
39 uint page_size= page->size;
40 uint nod_flag= page->node;
41 uchar *key_pos= rt_PAGE_END(page);
42 uint tot_key_length= key->data_length + key->ref_length + nod_flag;
43 DBUG_ENTER("maria_rtree_add_key");
44
45 if (page_size + tot_key_length <=
46 (uint)(key->keyinfo->block_length - KEYPAGE_CHECKSUM_SIZE))
47 {
48 /* split won't be necessary */
49 if (nod_flag)
50 {
51 DBUG_ASSERT(_ma_kpos(nod_flag, key->data) <
52 info->state->key_file_length);
53 /* We don't store reference to row on nod pages for rtree index */
54 tot_key_length-= key->ref_length;
55 }
56 /* save key */
57 memcpy(key_pos, key->data - nod_flag, tot_key_length);
58 page->size+= tot_key_length;
59 page_store_size(share, page);
60 if (share->now_transactional &&
61 _ma_log_add(page, (uint)(key_pos - page->buff),
62 key_pos, tot_key_length, tot_key_length, 0,
63 KEY_OP_DEBUG_LOG_ADD_1))
64 DBUG_RETURN(-1);
65 DBUG_RETURN(0);
66 }
67 DBUG_RETURN(maria_rtree_split_page(key, page, new_page) ? -1 : 1);
68}
69
70
71/*
72 Delete key from the page
73
74 Notes
75 key_length is only the data part of the key
76*/
77
78int maria_rtree_delete_key(MARIA_PAGE *page, uchar *key, uint key_length)
79{
80 MARIA_HA *info= page->info;
81 MARIA_SHARE *share= info->s;
82 uint key_length_with_nod_flag;
83 uchar *key_start;
84
85 key_start= key - page->node;
86 if (!page->node)
87 key_length+= share->base.rec_reflength;
88
89 memmove(key_start, key + key_length, page->size - key_length -
90 (key - page->buff));
91 key_length_with_nod_flag= key_length + page->node;
92 page->size-= key_length_with_nod_flag;
93 page_store_size(share, page);
94 if (share->now_transactional &&
95 _ma_log_delete(page, key_start, 0, key_length_with_nod_flag,
96 0, KEY_OP_DEBUG_LOG_DEL_CHANGE_RT))
97 return -1;
98 return 0;
99}
100
101
102/*
103 Calculate and store key MBR into *key.
104*/
105
106int maria_rtree_set_key_mbr(MARIA_HA *info, MARIA_KEY *key,
107 my_off_t child_page)
108{
109 MARIA_PAGE page;
110 DBUG_ENTER("maria_rtree_set_key_mbr");
111 if (_ma_fetch_keypage(&page, info, key->keyinfo, child_page,
112 PAGECACHE_LOCK_LEFT_UNLOCKED,
113 DFLT_INIT_HITS, info->buff, 0))
114 DBUG_RETURN(-1);
115
116 DBUG_RETURN(maria_rtree_page_mbr(key->keyinfo->seg,
117 &page, key->data, key->data_length));
118}
119
120#endif /*HAVE_RTREE_KEYS*/
121