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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
23 
24 #include "myisamdef.h"
25 
26 #ifdef HAVE_RTREE_KEYS
27 #include "rt_index.h"
28 #include "rt_key.h"
29 #include "rt_mbr.h"
30 
31 /*
32   Add key to the page
33 
34   RESULT VALUES
35     -1 	Error
36     0 	Not split
37     1	Split
38 */
39 
rtree_add_key(MI_INFO * info,MI_KEYDEF * keyinfo,uchar * key,uint key_length,uchar * page_buf,my_off_t * new_page)40 int rtree_add_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
41 		  uint key_length, uchar *page_buf, my_off_t *new_page)
42 {
43   uint page_size = mi_getint(page_buf);
44   uint nod_flag = mi_test_if_nod(page_buf);
45   DBUG_ENTER("rtree_add_key");
46 
47   if (page_size + key_length + info->s->base.rec_reflength <=
48       keyinfo->block_length)
49   {
50     /* split won't be necessary */
51     if (nod_flag)
52     {
53       /* save key */
54       DBUG_ASSERT(_mi_kpos(nod_flag, key) < info->state->key_file_length);
55       memcpy(rt_PAGE_END(page_buf), key - nod_flag, key_length + nod_flag);
56       page_size += key_length + nod_flag;
57     }
58     else
59     {
60       /* save key */
61       DBUG_ASSERT(_mi_dpos(info, nod_flag, key + key_length +
62                            info->s->base.rec_reflength) <
63                   info->state->data_file_length + info->s->base.pack_reclength);
64       memcpy(rt_PAGE_END(page_buf), key, key_length +
65                                          info->s->base.rec_reflength);
66       page_size += key_length + info->s->base.rec_reflength;
67     }
68     mi_putint(page_buf, page_size, nod_flag);
69     DBUG_RETURN(0);
70   }
71 
72   DBUG_RETURN((rtree_split_page(info, keyinfo, page_buf, key, key_length,
73                                 new_page) ? -1 : 1));
74 }
75 
76 /*
77   Delete key from the page
78 */
rtree_delete_key(MI_INFO * info,uchar * page_buf,uchar * key,uint key_length,uint nod_flag)79 int rtree_delete_key(MI_INFO *info, uchar *page_buf, uchar *key,
80 		     uint key_length, uint nod_flag)
81 {
82   uint16 page_size = mi_getint(page_buf);
83   uchar *key_start;
84 
85   key_start= key - nod_flag;
86   if (!nod_flag)
87     key_length += info->s->base.rec_reflength;
88 
89   memmove(key_start, key + key_length, page_size - key_length -
90 	  (key - page_buf));
91   page_size-= key_length + nod_flag;
92 
93   mi_putint(page_buf, page_size, nod_flag);
94   return 0;
95 }
96 
97 
98 /*
99   Calculate and store key MBR
100 */
101 
rtree_set_key_mbr(MI_INFO * info,MI_KEYDEF * keyinfo,uchar * key,uint key_length,my_off_t child_page)102 int rtree_set_key_mbr(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
103 		      uint key_length, my_off_t child_page)
104 {
105   DBUG_ENTER("rtree_set_key_mbr");
106 
107   if (!_mi_fetch_keypage(info, keyinfo, child_page,
108                          DFLT_INIT_HITS, info->buff, 0))
109     DBUG_RETURN(-1); /* purecov: inspected */
110 
111   DBUG_RETURN(rtree_page_mbr(info, keyinfo->seg, info->buff, key, key_length));
112 }
113 
114 #endif /*HAVE_RTREE_KEYS*/
115