Debian Codegraph @

storage/innobase/gis/gis0geo.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2013, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file gis/gis0geo.cc
29 InnoDB R-tree related functions.
30
31 Created 2013/03/27 Allen Lai and Jimmy Yang
32 *******************************************************/
33
34 #include <cmath>
35 #include "page0cur.h"
36
37 namespace dd {
38 class Spatial_reference_system;
39 }
40
41 /** Copy mbr of dimension n_dim from src to dst. */
42 inline static void copy_coords(double *dst, /*!< in/out: destination. */
43 const double *src) /*!< in: source. */
44 {
45 memcpy(dst, src, DATA_MBR_LEN);
46 }
47
48 /** Select two nodes to collect group upon */
49 static void pick_seeds(
50 rtr_split_node_t *node, /*!< in: split nodes. */
51 int n_entries, /*!< in: entries number. */
52 rtr_split_node_t **seed_a, /*!< out: seed 1. */
53 rtr_split_node_t **seed_b, /*!< out: seed 2. */
54 int n_dim, /*!< in: dimensions. */
55 const dd::Spatial_reference_system *srs) /*!< in: SRS of R-tree */
56 {
57 rtr_split_node_t *cur1;
58 rtr_split_node_t *lim1 = node + (n_entries - 1);
59 rtr_split_node_t *cur2;
60 rtr_split_node_t *lim2 = node + n_entries;
61
62 double max_d = -DBL_MAX;
63 double d;
64
65 *seed_a = node;
66 *seed_b = node + 1;
67
68 for (cur1 = node; cur1 < lim1; ++cur1) {
69 for (cur2 = cur1 + 1; cur2 < lim2; ++cur2) {
70 d = mbr_join_area(srs, cur1->coords, cur2->coords, n_dim) - cur1->square -
71 cur2->square;
72 if (d > max_d) {
73 max_d = d;
74 *seed_a = cur1;
75 *seed_b = cur2;
76 }
77 }
78 }
79 }
80
81 /** Generates a random boolean value.
82 @return the random value */
83 static bool ut_rnd_gen_bool(void) { return ut::random_64() & 1; }
84
85 /** Select next node and group where to add. */
86 static void pick_next(
87 rtr_split_node_t *node, /*!< in: split nodes. */
88 int n_entries, /*!< in: entries number. */
89 double *g1, /*!< in: mbr of group 1. */
90 double *g2, /*!< in: mbr of group 2. */
91 rtr_split_node_t **choice, /*!< out: the next node.*/
92 int *n_group, /*!< out: group number.*/
93 int n_dim, /*!< in: dimensions. */
94 const dd::Spatial_reference_system *srs) /*!< in: SRS of R-tree */
95 {
96 rtr_split_node_t *cur = node;
97 rtr_split_node_t *end = node + n_entries;
98 double max_diff = -DBL_MAX;
99
100 for (; cur < end; ++cur) {
101 double diff;
102 double abs_diff;
103
104 if (cur->n_node != 0) {
105 continue;
106 }
107
108 diff = mbr_join_area(srs, g1, cur->coords, n_dim) -
109 mbr_join_area(srs, g2, cur->coords, n_dim);
110
111 abs_diff = fabs(diff);
112 if (abs_diff > max_diff) {
113 max_diff = abs_diff;
114
115 /* Introduce some randomness if the record
116 is identical */
117 if (diff == 0) {
118 diff = static_cast<double>(ut_rnd_gen_bool());
119 }
120
121 *n_group = 1 + (diff > 0);
122 *choice = cur;
123 }
124 }
125 }
126
127 /** Mark not-in-group entries as n_group. */
128 static void mark_all_entries(
129 rtr_split_node_t *node, /*!< in/out: split nodes. */
130 int n_entries, /*!< in: entries number. */
131 int n_group) /*!< in: group number. */
132 {
133 rtr_split_node_t *cur = node;
134 rtr_split_node_t *end = node + n_entries;
135 for (; cur < end; ++cur) {
136 if (cur->n_node != 0) {
137 continue;
138 }
139 cur->n_node = n_group;
140 }
141 }
142
143 /** Split rtree node.
144 Return which group the first rec is in. */
145 int split_rtree_node(
146 rtr_split_node_t *node, /*!< in: split nodes. */
147 int n_entries, /*!< in: entries number. */
148 int all_size, /*!< in: total key's size. */
149 int key_size, /*!< in: key's size. */
150 int min_size, /*!< in: minimal group size. */
151 int size1, /*!< in: size of group. */
152 int size2, /*!< in: initial group sizes */
153 double **d_buffer, /*!< in/out: buffer. */
154 int n_dim, /*!< in: dimensions. */
155 uchar *first_rec, /*!< in: the first rec. */
156 const dd::Spatial_reference_system *srs) /*!< in: SRS of R-tree */
157 {
158 rtr_split_node_t *cur;
159 rtr_split_node_t *a = nullptr;
160 rtr_split_node_t *b = nullptr;
161 double *g1 = reserve_coords(d_buffer, n_dim);
162 double *g2 = reserve_coords(d_buffer, n_dim);
163 rtr_split_node_t *next = nullptr;
164 int next_node = 0;
165 int i;
166 int first_rec_group = 1;
167 rtr_split_node_t *end = node + n_entries;
168
169 if (all_size < min_size * 2) {
170 return 1;
171 }
172
173 cur = node;
174 for (; cur < end; ++cur) {
175 cur->square = compute_area(srs, cur->coords, n_dim);
176 cur->n_node = 0;
177 }
178
179 pick_seeds(node, n_entries, &a, &b, n_dim, srs);
180 a->n_node = 1;
181 b->n_node = 2;
182
183 copy_coords(g1, a->coords);
184 size1 += key_size;
185 copy_coords(g2, b->coords);
186 size2 += key_size;
187
188 for (i = n_entries - 2; i > 0; --i) {
189 /* Can't write into group 2 */
190 if (all_size - (size2 + key_size) < min_size) {
191 mark_all_entries(node, n_entries, 1);
192 break;
193 }
194
195 /* Can't write into group 1 */
196 if (all_size - (size1 + key_size) < min_size) {
197 mark_all_entries(node, n_entries, 2);
198 break;
199 }
200
201 pick_next(node, n_entries, g1, g2, &next, &next_node, n_dim, srs);
202 if (next_node == 1) {
203 size1 += key_size;
204 mbr_join(srs, g1, next->coords, n_dim);
205 } else {
206 size2 += key_size;
207 mbr_join(srs, g2, next->coords, n_dim);
208 }
209
210 next->n_node = next_node;
211
212 /* Find out where the first rec (of the page) will be at,
213 and inform the caller */
214 if (first_rec && first_rec == next->key) {
215 first_rec_group = next_node;
216 }
217 }
218
219 return (first_rec_group);
220 }
221
222 bool rtree_key_cmp(page_cur_mode_t mode, const uchar *a, int a_len,
223 const uchar *b, int b_len,
224 const dd::Spatial_reference_system *srs) {
225 rtr_mbr_t x, y;
226
227 /* Dimension length. */
228 uint dim_len = SPDIMS * sizeof(double);
229 assert(static_cast<unsigned int>(b_len) == 2 * dim_len);
230 assert(static_cast<unsigned int>(a_len) == 2 + 2 * dim_len ||
231 static_cast<unsigned int>(a_len) == 2 * dim_len);
232
233 x.xmin = mach_double_read(a);
234 y.xmin = mach_double_read(b);
235 x.xmax = mach_double_read(a + sizeof(double));
236 y.xmax = mach_double_read(b + sizeof(double));
237
238 x.ymin = mach_double_read(a + dim_len);
239 y.ymin = mach_double_read(b + dim_len);
240 x.ymax = mach_double_read(a + dim_len + sizeof(double));
241 y.ymax = mach_double_read(b + dim_len + sizeof(double));
242
243 switch (mode) {
244 case PAGE_CUR_INTERSECT:
245 return (mbr_intersect_cmp(srs, &x, &y));
246 case PAGE_CUR_CONTAIN:
247 return (mbr_contain_cmp(srs, &x, &y));
248 case PAGE_CUR_WITHIN:
249 return (mbr_within_cmp(srs, &x, &y));
250 case PAGE_CUR_MBR_EQUAL:
251 /* TBD: Why is it important to use mbr_equal_physically()
252 vs mbr_equal_logically() here? */
253 return (mbr_equal_logically(srs, &x, &y));
254 case PAGE_CUR_DISJOINT:
255 return (mbr_disjoint_cmp(srs, &x, &y));
256 default:
257 /* if unknown comparison operator */
258 ut_d(ut_error);
259 }
260
261 ut_o(return (false));
262 }