Debian Codegraph @

storage/innobase/include/gis0rtree.h

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.h
29 R-tree header file
30
31 Created 2013/03/27 Jimmy Yang and Allen Lai
32 ***********************************************************************/
33
34 #ifndef gis0rtree_h
35 #define gis0rtree_h
36
37 #include "univ.i"
38
39 #include "btr0cur.h"
40 #include "btr0types.h"
41 #include "data0type.h"
42 #include "data0types.h"
43 #include "dict0types.h"
44 #include "gis0geo.h"
45 #include "gis0type.h"
46 #include "hash0hash.h"
47 #include "mem0mem.h"
48 #include "page0page.h"
49 #include "que0types.h"
50 #include "rem0types.h"
51 #include "row0types.h"
52 #include "trx0types.h"
53 #include "ut0vec.h"
54 #include "ut0wqueue.h"
55
56 /* Define it for rtree search mode checking. */
57 static inline bool RTREE_SEARCH_MODE(page_cur_mode_t mode) {
58 return mode >= PAGE_CUR_CONTAIN && mode <= PAGE_CUR_RTREE_GET_FATHER;
59 }
60
61 /* Geometry data header */
62 constexpr uint32_t GEO_DATA_HEADER_SIZE = 4;
63 /** Builds a Rtree node pointer out of a physical record and a page number.
64 @return own: node pointer
65 @param[in] index index
66 @param[in] mbr mbr of lower page
67 @param[in] rec record for which to build node pointer
68 @param[in] page_no page number to put in node pointer
69 @param[in] heap memory heap where pointer created */
70 dtuple_t *rtr_index_build_node_ptr(const dict_index_t *index,
71 const rtr_mbr_t *mbr, const rec_t *rec,
72 page_no_t page_no, mem_heap_t *heap);
73
74 /** Splits an R-tree index page to halves and inserts the tuple. It is assumed
75 that mtr holds an x-latch to the index tree. NOTE: the tree x-latch is
76 released within this function! NOTE that the operation of this
77 function must always succeed, we cannot reverse it: therefore enough
78 free disk space (2 pages) must be guaranteed to be available before
79 this function is called.
80 @return inserted record */
81 rec_t *rtr_page_split_and_insert(
82 uint32_t flags, /*!< in: undo logging and locking flags */
83 btr_cur_t *cursor, /*!< in/out: cursor at which to insert; when the
84 function returns, the cursor is positioned
85 on the predecessor of the inserted record */
86 ulint **offsets, /*!< out: offsets on inserted record */
87 mem_heap_t **heap, /*!< in/out: pointer to memory heap, or NULL */
88 const dtuple_t *tuple, /*!< in: tuple to insert */
89 mtr_t *mtr); /*!< in: mtr */
90
91 /** Sets the child node mbr in a node pointer.
92 @param[in] index index
93 @param[in] block buffer block
94 @param[out] mbr MBR encapsulates the page
95 @param[in] heap heap for the memory allocation */
96 static inline void rtr_page_cal_mbr(const dict_index_t *index,
97 const buf_block_t *block, rtr_mbr_t *mbr,
98 mem_heap_t *heap);
99
100 /** Find the next matching record. This function will first exhaust
101 the copied record listed in the rtr_info->matches vector before
102 moving to next page
103 @param[in] tuple Data tuple; NOTE: n_fields_cmp in tuple
104 must be set so that it cannot get compared
105 to the node ptr page number field!
106 @param[in] mode Cursor search mode
107 @param[in] sel_mode Select mode: SELECT_ORDINARY,
108 SELECT_SKIP_LOKCED, or SELECT_NO_WAIT
109 @param[in] cursor Persistent cursor; NOTE that the function
110 may release the page latch
111 @param[in] cur_level Current level
112 @param[in] mtr Mini-transaction
113 @return true if there is next qualified record found, otherwise(if
114 exhausted) false */
115 bool rtr_pcur_move_to_next(const dtuple_t *tuple, page_cur_mode_t mode,
116 select_mode sel_mode, btr_pcur_t *cursor,
117 ulint cur_level, mtr_t *mtr);
118
119 /** Searches the right position in rtree for a page cursor.
120 @param[in] block Buffer block
121 @param[in] index Index descriptor
122 @param[in] tuple Data tuple
123 @param[in] mode Page_cur_l, page_cur_le, page_cur_g, or page_cur_ge
124 @param[in,out] cursor Page cursor
125 @param[in,out] rtr_info Search stack */
126 bool rtr_cur_search_with_match(const buf_block_t *block, dict_index_t *index,
127 const dtuple_t *tuple, page_cur_mode_t mode,
128 page_cur_t *cursor, rtr_info_t *rtr_info);
129
130 /** Calculate the area increased for a new record
131 @return area increased */
132 double rtr_rec_cal_increase(
133 const dtuple_t *dtuple, /*!< in: data tuple to insert, which
134 cause area increase */
135 const rec_t *rec, /*!< in: physical record which differs from
136 dtuple in some of the common fields, or which
137 has an equal number or more fields than
138 dtuple */
139 const ulint *offsets, /*!< in: array returned by rec_get_offsets() */
140 double *area, /*!< out: increased area */
141 const dd::Spatial_reference_system *srs); /*!< in: SRS of R-tree */
142
143 /** Following the right link to find the proper block for insert.
144 @return the proper block.
145 @param[in] btr_cur btr cursor
146 @param[in] mtr mtr */
147 dberr_t rtr_ins_enlarge_mbr(btr_cur_t *btr_cur, mtr_t *mtr);
148
149 /** */
150 void rtr_get_father_node(
151 dict_index_t *index, /*!< in: index */
152 ulint level, /*!< in: the tree level of search */
153 const dtuple_t *tuple, /*!< in: data tuple; NOTE: n_fields_cmp in
154 tuple must be set so that it cannot get
155 compared to the node ptr page number field! */
156 btr_cur_t *sea_cur, /*!< in: search cursor */
157 btr_cur_t *cursor, /*!< in/out: tree cursor; the cursor page is
158 s- or x-latched */
159 page_no_t page_no, /*!< in: current page no */
160 mtr_t *mtr); /*!< in: mtr */
161
162 /** Push a nonleaf index node to the search path
163 @param[in,out] path search path
164 @param[in] pageno pageno to insert
165 @param[in] seq_no Node sequence num
166 @param[in] level index level
167 @param[in] child_no child page no
168 @param[in] cursor position cursor
169 @param[in] mbr_inc MBR needs to be enlarged */
170 static inline void rtr_non_leaf_stack_push(rtr_node_path_t *path,
171 page_no_t pageno, node_seq_t seq_no,
172 ulint level, page_no_t child_no,
173 btr_pcur_t *cursor, double mbr_inc);
174
175 /** Push a nonleaf index node to the search path for insertion
176 @param[in] index index descriptor
177 @param[in,out] path search path
178 @param[in] level index level
179 @param[in] block block of the page
180 @param[in] rec positioned record
181 @param[in] mbr_inc MBR needs to be enlarged */
182 void rtr_non_leaf_insert_stack_push(dict_index_t *index, rtr_node_path_t *path,
183 ulint level, const buf_block_t *block,
184 const rec_t *rec, double mbr_inc);
185
186 /** Allocates a new Split Sequence Number.
187 @return new SSN id */
188 static inline node_seq_t rtr_get_new_ssn_id(
189 dict_index_t *index); /*!< in: the index struct */
190
191 /** Get the current Split Sequence Number.
192 @return current SSN id */
193 static inline node_seq_t rtr_get_current_ssn_id(
194 dict_index_t *index); /*!< in/out: the index struct */
195
196 /** Create a RTree search info structure
197 @param[in] need_prdt Whether predicate lock is needed
198 @param[in] init_matches Whether to initiate the "matches" structure for
199 collecting matched leaf records
200 @param[in] cursor Tree search cursor
201 @param[in] index Index struct */
202 rtr_info_t *rtr_create_rtr_info(bool need_prdt, bool init_matches,
203 btr_cur_t *cursor, dict_index_t *index);
204
205 /** Update a btr_cur_t with rtr_info
206 @param[in,out] cursor Tree cursor
207 @param[in] rtr_info Rtr_info to set to the cursor */
208 void rtr_info_update_btr(btr_cur_t *cursor, rtr_info_t *rtr_info);
209
210 /** Update a btr_cur_t with rtr_info */
211 void rtr_init_rtr_info(
212 /****************/
213 rtr_info_t *rtr_info, /*!< in: rtr_info to set to the
214 cursor */
215 bool need_prdt, /*!< in: Whether predicate lock is
216 needed */
217 btr_cur_t *cursor, /*!< in: tree search cursor */
218 dict_index_t *index, /*!< in: index structure */
219 bool reinit); /*!< in: Whether this is a reinit */
220
221 /** Clean up Rtree cursor */
222 void rtr_clean_rtr_info(rtr_info_t *rtr_info, /*!< in: RTree search info */
223 bool free_all); /*!< in: need to free rtr_info itself */
224
225 /** Get the bounding box content from an index record
226 @param[in] rec Data tuple
227 @param[in] offsets Offsets array
228 @param[out] mbr Mbr */
229 void rtr_get_mbr_from_rec(const rec_t *rec, const ulint *offsets,
230 rtr_mbr_t *mbr);
231
232 /** Get the bounding box content from a MBR data record
233 @param[in] dtuple Data tuple
234 @param[out] mbr Mbr to fill */
235 void rtr_get_mbr_from_tuple(const dtuple_t *dtuple, rtr_mbr *mbr);
236
237 /* Get the rtree page father.
238 @param[in] offsets work area for the return value
239 @param[in] index rtree index
240 @param[in] block child page in the index
241 @param[in] mtr mtr
242 @param[in] sea_cur search cursor, contains information
243 about parent nodes in search
244 @param[in] cursor cursor on node pointer record,
245 its page x-latched */
246 void rtr_page_get_father(dict_index_t *index, buf_block_t *block, mtr_t *mtr,
247 btr_cur_t *sea_cur, btr_cur_t *cursor);
248
249 /** Returns the father block to a page. It is assumed that mtr holds
250 an X or SX latch on the tree.
251 @return rec_get_offsets() of the node pointer record */
252 ulint *rtr_page_get_father_block(
253 ulint *offsets, /*!< in: work area for the return value */
254 mem_heap_t *heap, /*!< in: memory heap to use */
255 dict_index_t *index, /*!< in: b-tree index */
256 buf_block_t *block, /*!< in: child page in the index */
257 mtr_t *mtr, /*!< in: mtr */
258 btr_cur_t *sea_cur, /*!< in: search cursor, contains information
259 about parent nodes in search */
260 btr_cur_t *cursor); /*!< out: cursor on node pointer record,
261 its page x-latched */
262 /** Store the parent path cursor
263 @return number of cursor stored */
264 ulint rtr_store_parent_path(
265 const buf_block_t *block, /*!< in: block of the page */
266 btr_cur_t *btr_cur, /*!< in/out: persistent cursor */
267 ulint latch_mode,
268 /*!< in: latch_mode */
269 ulint level, /*!< in: index level */
270 mtr_t *mtr); /*!< in: mtr */
271
272 /** Initializes and opens a persistent cursor to an index tree. It should be
273 closed with btr_pcur::close. Mainly called by row_search_index_entry()
274 @param[in] index index
275 @param[in] level level in the btree
276 @param[in] tuple tuple on which search done
277 @param[in] mode PAGE_CUR_L, ...; NOTE that if the search is made using a unique
278 prefix of a record, mode should be PAGE_CUR_LE, not PAGE_CUR_GE, as the latter
279 may end up on the previous page from the record!
280 @param[in] latch_mode BTR_SEARCH_LEAF, ...
281 @param[in] cursor memore buffer for persistent cursor
282 @param[in] location location where called
283 @param[in] mtr mtr */
284 void rtr_pcur_open_low(dict_index_t *index, ulint level, const dtuple_t *tuple,
285 page_cur_mode_t mode, ulint latch_mode,
286 btr_pcur_t *cursor, ut::Location location, mtr_t *mtr);
287
288 static inline void rtr_pcur_open(dict_index_t *i, const dtuple_t *t,
289 page_cur_mode_t md, ulint l, btr_pcur_t *c,
290 ut::Location loc, mtr_t *m) {
291 rtr_pcur_open_low(i, 0, t, md, l, c, loc, m);
292 }
293
294 struct btr_cur_t;
295
296 /** Returns the R-Tree node stored in the parent search path
297 @param[in] btr_cur persistent cursor
298 @param[in] level index level of buffer page
299 @param[in] is_insert whether insert operation
300 @return pointer to R-Tree cursor component */
301 static inline node_visit_t *rtr_get_parent_node(btr_cur_t *btr_cur, ulint level,
302 ulint is_insert);
303
304 /** Returns the R-Tree cursor stored in the parent search path
305 @param[in] btr_cur persistent cursor
306 @param[in] level index level of buffer page
307 @param[in] is_insert whether insert operation
308 @return pointer to R-Tree cursor component */
309 static inline btr_pcur_t *rtr_get_parent_cursor(btr_cur_t *btr_cur, ulint level,
310 ulint is_insert);
311
312 /** Copy recs from a page to new_block of rtree.
313 Differs from page_copy_rec_list_end, because this function does not
314 touch the lock table and max trx id on page or compress the page.
315
316 IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
317 if new_block is a compressed leaf page in a secondary index.
318 This has to be done either within the same mini-transaction,
319 or by invoking ibuf_reset_free_bits() before mtr_commit().
320
321 @param[in] new_block Index page to copy to
322 @param[in] block Index page of rec
323 @param[in] rec Record on page
324 @param[in] index Record descriptor
325 @param[in,out] heap Heap memory
326 @param[in] rec_move Recording records moved
327 @param[in] max_move Num of rec to move
328 @param[out] num_moved Num of rec to move
329 @param[in] mtr Mini-transaction */
330 void rtr_page_copy_rec_list_end_no_locks(buf_block_t *new_block,
331 buf_block_t *block, rec_t *rec,
332 dict_index_t *index, mem_heap_t *heap,
333 rtr_rec_move_t *rec_move,
334 ulint max_move, ulint *num_moved,
335 mtr_t *mtr);
336
337 /** Copy recs till a specified rec from a page to new_block of rtree.
338 @param[in] new_block Index page to copy to
339 @param[in] block Index page of rec
340 @param[in] rec Record on page
341 @param[in] index Record descriptor
342 @param[in,out] heap Heap memory
343 @param[in] rec_move Recording records moved
344 @param[in] max_move Num of rec to move
345 @param[out] num_moved Num of rec to move
346 @param[in] mtr Mini-transaction */
347 void rtr_page_copy_rec_list_start_no_locks(
348 buf_block_t *new_block, buf_block_t *block, rec_t *rec, dict_index_t *index,
349 mem_heap_t *heap, rtr_rec_move_t *rec_move, ulint max_move,
350 ulint *num_moved, mtr_t *mtr);
351
352 /** Merge 2 mbrs and update the the mbr that cursor is on.
353 @param[in,out] cursor Cursor
354 @param[in] cursor2 The other cursor
355 @param[in] offsets Rec offsets
356 @param[in] offsets2 Rec offsets
357 @param[in] child_page The child page.
358 @param[in] mtr Mini-transaction */
359 dberr_t rtr_merge_and_update_mbr(btr_cur_t *cursor, btr_cur_t *cursor2,
360 ulint *offsets, ulint *offsets2,
361 page_t *child_page, mtr_t *mtr);
362
363 /** Deletes on the upper level the node pointer to a page.
364 @param[in] sea_cur Search cursor, contains information about parent nodes in
365 search
366 @param[in] mtr Mini-transaction
367 */
368 void rtr_node_ptr_delete(btr_cur_t *sea_cur, mtr_t *mtr);
369
370 /** Check two MBRs are identical or need to be merged
371 @param[in] cursor Cursor
372 @param[in] cursor2 The other cursor
373 @param[in] offsets Rec offsets
374 @param[in] offsets2 Rec offsets
375 @param[out] new_mbr Mbr to update */
376 bool rtr_merge_mbr_changed(btr_cur_t *cursor, btr_cur_t *cursor2,
377 ulint *offsets, ulint *offsets2, rtr_mbr_t *new_mbr);
378
379 /** Update the mbr field of a spatial index row.
380 @return true if successful */
381 bool rtr_update_mbr_field(
382 btr_cur_t *cursor, /*!< in: cursor pointed to rec.*/
383 ulint *offsets, /*!< in: offsets on rec. */
384 btr_cur_t *cursor2, /*!< in/out: cursor pointed to rec
385 that should be deleted.
386 this cursor is for btr_compress to
387 delete the merged page's father rec.*/
388 page_t *child_page, /*!< in: child page. */
389 rtr_mbr_t *new_mbr, /*!< in: the new mbr. */
390 rec_t *new_rec, /*!< in: rec to use */
391 mtr_t *mtr); /*!< in: mtr */
392
393 /** Check whether a Rtree page is child of a parent page
394 @return true if there is child/parent relationship */
395 bool rtr_check_same_block(
396 dict_index_t *index, /*!< in: index tree */
397 btr_cur_t *cur, /*!< in/out: position at the parent entry
398 pointing to the child if successful */
399 buf_block_t *parentb, /*!< in: parent page to check */
400 buf_block_t *childb, /*!< in: child Page */
401 mem_heap_t *heap); /*!< in: memory heap */
402
403 /** Sets pointer to the data and length in a field.
404 @param[out] data data
405 @param[in] mbr data */
406 static inline void rtr_write_mbr(byte *data, const rtr_mbr_t *mbr);
407
408 /** Sets pointer to the data and length in a field.
409 @param[in] data data
410 @param[out] mbr data */
411 static inline void rtr_read_mbr(const byte *data, rtr_mbr_t *mbr);
412
413 /** Check whether a discarding page is in anyone's search path
414 @param[in] index Index
415 @param[in,out] cursor Cursor on the page to discard: not on the root page
416 @param[in] block Block of page to be discarded */
417 void rtr_check_discard_page(dict_index_t *index, btr_cur_t *cursor,
418 buf_block_t *block);
419
420 /** Reinitialize a RTree search info
421 @param[in,out] cursor tree cursor
422 @param[in] index index struct
423 @param[in] need_prdt Whether predicate lock is needed */
424 static inline void rtr_info_reinit_in_cursor(btr_cur_t *cursor,
425 dict_index_t *index,
426 bool need_prdt);
427
428 /** Estimates the number of rows in a given area.
429 @param[in] index index
430 @param[in] tuple range tuple containing mbr, may also be empty tuple
431 @param[in] mode search mode
432 @return estimated number of rows */
433 int64_t rtr_estimate_n_rows_in_range(dict_index_t *index, const dtuple_t *tuple,
434 page_cur_mode_t mode);
435
436 #include "gis0rtree.ic"
437 #endif