Debian Codegraph @

storage/innobase/include/gis0rtree.ic

raw
1 /*****************************************************************************
2
3 Copyright (c) 2014, 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 include/gis0rtree.ic
29 R-tree Inline code
30
31 Created 2013/03/27 Jimmy Yang and Allen Lai
32 ***********************************************************************/
33
34 /** Sets the child node mbr in a node pointer. */
35 static inline void rtr_page_cal_mbr(
36 const dict_index_t *index, /*!< in: index */
37 const buf_block_t *block, /*!< in: buffer block */
38 rtr_mbr_t *rtr_mbr, /*!< out: MBR encapsulates the page */
39 mem_heap_t *heap) /*!< in: heap for the memory
40 allocation */
41 {
42 page_t *page;
43 rec_t *rec;
44 const byte *field;
45 ulint len;
46 ulint *offsets = nullptr;
47 double bmin, bmax;
48 double *amin;
49 double *amax;
50 ulint inc = 0;
51 double *mbr;
52
53 rtr_mbr->xmin = DBL_MAX;
54 rtr_mbr->ymin = DBL_MAX;
55 rtr_mbr->xmax = -DBL_MAX;
56 rtr_mbr->ymax = -DBL_MAX;
57
58 mbr = reinterpret_cast<double *>(rtr_mbr);
59
60 page = buf_block_get_frame(block);
61
62 rec = page_rec_get_next(page_get_infimum_rec(page));
63 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
64 UT_LOCATION_HERE, &heap);
65
66 do {
67 /* The mbr address is in the first field. */
68 field = rec_get_nth_field(index, rec, offsets, 0, &len);
69
70 ut_ad(len == DATA_MBR_LEN);
71 inc = 0;
72 for (uint i = 0; i < SPDIMS; i++) {
73 bmin = mach_double_read(field + inc);
74 bmax = mach_double_read(field + inc + sizeof(double));
75
76 amin = mbr + i * SPDIMS;
77 amax = mbr + i * SPDIMS + 1;
78
79 if (*amin > bmin) *amin = bmin;
80 if (*amax < bmax) *amax = bmax;
81
82 inc += 2 * sizeof(double);
83 }
84
85 rec = page_rec_get_next(rec);
86
87 if (rec == nullptr) {
88 break;
89 }
90 } while (!page_rec_is_supremum(rec));
91 }
92
93 /** push a nonleaf index node to the search path */
94 static inline void rtr_non_leaf_stack_push(
95 rtr_node_path_t *path, /*!< in/out: search path */
96 page_no_t pageno, /*!< in: pageno to insert */
97 node_seq_t seq_no, /*!< in: Node sequence num */
98 ulint level, /*!< in: index page level */
99 page_no_t child_no, /*!< in: child page no */
100 btr_pcur_t *cursor, /*!< in: position cursor */
101 double mbr_inc) /*!< in: MBR needs to be
102 enlarged */
103 {
104 node_visit_t insert_val;
105
106 insert_val.page_no = pageno;
107 insert_val.seq_no = seq_no;
108 insert_val.level = level;
109 insert_val.child_no = child_no;
110 insert_val.cursor = cursor;
111 insert_val.mbr_inc = mbr_inc;
112
113 path->push_back(insert_val);
114
115 #ifdef RTR_SEARCH_DIAGNOSTIC
116 fprintf(stderr,
117 "INNODB_RTR: Push page %d, level %d, seq %d"
118 " to search stack \n",
119 static_cast<int>(pageno), static_cast<int>(level),
120 static_cast<int>(seq_no));
121 #endif /* RTR_SEARCH_DIAGNOSTIC */
122 }
123
124 /** Allocates a new Split Sequence Number.
125 @return new SSN id */
126 static inline node_seq_t rtr_get_new_ssn_id(
127 dict_index_t *index) /*!< in/out: the index struct */
128 {
129 node_seq_t ssn;
130
131 mutex_enter(&(index->rtr_ssn.mutex));
132 ssn = ++index->rtr_ssn.seq_no;
133 mutex_exit(&(index->rtr_ssn.mutex));
134
135 return (ssn);
136 }
137 /** Get the current Split Sequence Number.
138 @return current SSN id */
139 static inline node_seq_t rtr_get_current_ssn_id(
140 dict_index_t *index) /*!< in: index struct */
141 {
142 node_seq_t ssn;
143
144 mutex_enter(&(index->rtr_ssn.mutex));
145 ssn = index->rtr_ssn.seq_no;
146 mutex_exit(&(index->rtr_ssn.mutex));
147
148 return (ssn);
149 }
150
151 /** Sets pointer to the data and length in a field. */
152 static inline void rtr_write_mbr(byte *data, /*!< out: data */
153 const rtr_mbr_t *mbr) /*!< in: data */
154 {
155 const double *my_mbr = reinterpret_cast<const double *>(mbr);
156
157 for (uint i = 0; i < SPDIMS * 2; i++) {
158 mach_double_write(data + i * sizeof(double), my_mbr[i]);
159 }
160 }
161
162 /** Sets pointer to the data and length in a field. */
163 static inline void rtr_read_mbr(const byte *data, /*!< in: data */
164 rtr_mbr_t *mbr) /*!< out: MBR */
165 {
166 for (uint i = 0; i < SPDIMS * 2; i++) {
167 (reinterpret_cast<double *>(mbr))[i] =
168 mach_double_read(data + i * sizeof(double));
169 }
170
171 ut_ad(mbr->xmin == DBL_MAX || mbr->xmax == -DBL_MAX ||
172 mbr->xmin <= mbr->xmax);
173 ut_ad(mbr->ymin == DBL_MAX || mbr->ymax == -DBL_MAX ||
174 mbr->ymin <= mbr->ymax);
175 }
176
177 /** Returns the R-Tree node stored in the parent search path
178 @return pointer to R-Tree cursor component in the parent path,
179 NULL if parent path is empty or index is larger than num of items contained */
180 static inline node_visit_t *rtr_get_parent_node(
181 btr_cur_t *btr_cur, /*!< in: persistent cursor */
182 ulint level, /*!< in: index level of buffer page */
183 ulint is_insert) /*!< in: whether it is insert */
184 {
185 ulint num;
186 ulint tree_height = btr_cur->tree_height;
187 node_visit_t *found_node = nullptr;
188
189 if (level >= tree_height) {
190 return (nullptr);
191 }
192
193 mutex_enter(&btr_cur->rtr_info->rtr_path_mutex);
194
195 num = btr_cur->rtr_info->parent_path->size();
196
197 if (!num) {
198 mutex_exit(&btr_cur->rtr_info->rtr_path_mutex);
199 return (nullptr);
200 }
201
202 if (is_insert) {
203 ulint idx = tree_height - level - 1;
204 ut_ad(idx < num);
205
206 found_node = &(*btr_cur->rtr_info->parent_path)[idx];
207 } else {
208 node_visit_t *node;
209
210 while (num > 0) {
211 node = &(*btr_cur->rtr_info->parent_path)[num - 1];
212
213 if (node->level == level) {
214 found_node = node;
215 break;
216 }
217 num--;
218 }
219 }
220
221 mutex_exit(&btr_cur->rtr_info->rtr_path_mutex);
222
223 return (found_node);
224 }
225
226 /** Returns the R-Tree cursor stored in the parent search path
227 @return pointer to R-Tree cursor component */
228 static inline btr_pcur_t *rtr_get_parent_cursor(
229 btr_cur_t *btr_cur, /*!< in: persistent cursor */
230 ulint level, /*!< in: index level of buffer page */
231 ulint is_insert) /*!< in: whether insert operation */
232 {
233 node_visit_t *found_node = rtr_get_parent_node(btr_cur, level, is_insert);
234
235 return ((found_node) ? found_node->cursor : nullptr);
236 }
237
238 /** Reinitialize a R-Tree search info in btr_cur_t */
239 static inline void rtr_info_reinit_in_cursor(
240 /************************/
241 btr_cur_t *cursor, /*!< in/out: tree cursor */
242 dict_index_t *index, /*!< in: index struct */
243 bool need_prdt) /*!< in: Whether predicate lock is
244 needed */
245 {
246 rtr_clean_rtr_info(cursor->rtr_info, false);
247 rtr_init_rtr_info(cursor->rtr_info, need_prdt, cursor, index, true);
248 }