Debian Codegraph @

storage/innobase/gis/gis0rtree.cc

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 gis/gis0rtree.cc
29 InnoDB R-tree interfaces
30
31 Created 2013/03/27 Allen Lai and Jimmy Yang
32 ***********************************************************************/
33
34 #include <sys/types.h>
35 #include <cmath>
36
37 #include "fsp0fsp.h"
38 #include "gis0rtree.h"
39
40 #include "my_dbug.h"
41
42 #include "page0cur.h"
43 #include "page0page.h"
44 #include "page0zip.h"
45
46 #include "btr0cur.h"
47 #include "btr0pcur.h"
48 #include "btr0sea.h"
49 #include "gis0geo.h"
50 #include "ibuf0ibuf.h"
51 #include "lock0lock.h"
52 #include "rem0cmp.h"
53 #include "srv0mon.h"
54 #include "trx0trx.h"
55
56 /** Initial split nodes info for R-tree split.
57 @return initialized split nodes array */
58 static rtr_split_node_t *rtr_page_split_initialize_nodes(
59 mem_heap_t *heap, /*!< in: pointer to memory heap, or NULL */
60 btr_cur_t *cursor, /*!< in: cursor at which to insert; when the
61 function returns, the cursor is positioned
62 on the predecessor of the inserted record */
63 ulint **offsets, /*!< in: offsets on inserted record */
64 const dtuple_t *tuple, /*!< in: tuple to insert */
65 double **buf_pos) /*!< in/out: current buffer position */
66 {
67 rtr_split_node_t *split_node_array;
68 double *buf;
69 ulint n_recs;
70 rtr_split_node_t *task;
71 rtr_split_node_t *stop;
72 rtr_split_node_t *cur;
73 rec_t *rec;
74 buf_block_t *block;
75 page_t *page;
76 ulint n_uniq;
77 ulint len;
78 const byte *source_cur;
79
80 block = btr_cur_get_block(cursor);
81 page = buf_block_get_frame(block);
82 n_uniq = dict_index_get_n_unique_in_tree(cursor->index);
83
84 n_recs = page_get_n_recs(page) + 1;
85
86 /*We reserve 2 MBRs memory space for temp result of split
87 algrithm. And plus the new mbr that need to insert, we
88 need (n_recs + 3)*MBR size for storing all MBRs.*/
89 buf = static_cast<double *>(mem_heap_alloc(
90 heap,
91 DATA_MBR_LEN * (n_recs + 3) + sizeof(rtr_split_node_t) * (n_recs + 1)));
92
93 split_node_array = (rtr_split_node_t *)(buf + SPDIMS * 2 * (n_recs + 3));
94 task = split_node_array;
95 *buf_pos = buf;
96 stop = task + n_recs;
97
98 rec = page_rec_get_next(page_get_infimum_rec(page));
99 *offsets = rec_get_offsets(rec, cursor->index, *offsets, n_uniq,
100 UT_LOCATION_HERE, &heap);
101
102 source_cur = rec_get_nth_field(cursor->index, rec, *offsets, 0, &len);
103
104 for (cur = task; cur < stop - 1; ++cur) {
105 cur->coords = reserve_coords(buf_pos, SPDIMS);
106 cur->key = rec;
107
108 memcpy(cur->coords, source_cur, DATA_MBR_LEN);
109
110 rec = page_rec_get_next(rec);
111 *offsets = rec_get_offsets(rec, cursor->index, *offsets, n_uniq,
112 UT_LOCATION_HERE, &heap);
113 source_cur = rec_get_nth_field(cursor->index, rec, *offsets, 0, &len);
114 }
115
116 /* Put the insert key to node list */
117 source_cur =
118 static_cast<byte *>(dfield_get_data(dtuple_get_nth_field(tuple, 0)));
119 cur->coords = reserve_coords(buf_pos, SPDIMS);
120 rec = (byte *)mem_heap_alloc(heap,
121 rec_get_converted_size(cursor->index, tuple));
122
123 rec = rec_convert_dtuple_to_rec(rec, cursor->index, tuple);
124 cur->key = rec;
125
126 memcpy(cur->coords, source_cur, DATA_MBR_LEN);
127
128 return split_node_array;
129 }
130
131 dtuple_t *rtr_index_build_node_ptr(const dict_index_t *index,
132 const rtr_mbr_t *mbr, const rec_t *rec,
133 page_no_t page_no, mem_heap_t *heap) {
134 dtuple_t *tuple;
135 dfield_t *field;
136 byte *buf;
137 ulint n_unique;
138 ulint info_bits;
139
140 ut_ad(dict_index_is_spatial(index));
141
142 n_unique = DICT_INDEX_SPATIAL_NODEPTR_SIZE;
143
144 tuple = dtuple_create(heap, n_unique + 1);
145
146 /* For rtree internal node, we need to compare page number
147 fields. */
148 dtuple_set_n_fields_cmp(tuple, n_unique + 1);
149
150 dict_index_copy_types(tuple, index, n_unique);
151
152 /* Write page no field */
153 buf = static_cast<byte *>(mem_heap_alloc(heap, 4));
154
155 mach_write_to_4(buf, page_no);
156
157 field = dtuple_get_nth_field(tuple, n_unique);
158 dfield_set_data(field, buf, 4);
159
160 dtype_set(dfield_get_type(field), DATA_SYS_CHILD, DATA_NOT_NULL, 4);
161
162 /* Set info bits. */
163 info_bits = rec_get_info_bits(rec, dict_table_is_comp(index->table));
164 dtuple_set_info_bits(tuple, info_bits | REC_STATUS_NODE_PTR);
165
166 /* Set mbr as index entry data */
167 field = dtuple_get_nth_field(tuple, 0);
168
169 buf = static_cast<byte *>(mem_heap_alloc(heap, DATA_MBR_LEN));
170
171 rtr_write_mbr(buf, mbr);
172
173 dfield_set_data(field, buf, DATA_MBR_LEN);
174
175 ut_ad(dtuple_check_typed(tuple));
176
177 return (tuple);
178 }
179
180 /** In-place update the mbr field of a spatial index row. */
181 static void rtr_update_mbr_field_in_place(
182 dict_index_t *index, /*!< in: spatial index. */
183 rec_t *rec, /*!< in/out: rec to be modified.*/
184 ulint *offsets, /*!< in/out: offsets on rec. */
185 rtr_mbr_t *mbr, /*!< in: the new mbr. */
186 mtr_t *mtr) /*!< in: mtr */
187 {
188 void *new_mbr_ptr;
189 double new_mbr[SPDIMS * 2];
190 byte *log_ptr = nullptr;
191 ulint len = DATA_MBR_LEN;
192 ulint flags = BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG | BTR_KEEP_SYS_FLAG;
193 ulint rec_info;
194
195 rtr_write_mbr(reinterpret_cast<byte *>(&new_mbr), mbr);
196 new_mbr_ptr = static_cast<void *>(new_mbr);
197 /* Otherwise, set the mbr to the new_mbr. */
198 rec_set_nth_field(index, rec, offsets, 0, new_mbr_ptr, len);
199
200 rec_info = rec_get_info_bits(rec, rec_offs_comp(offsets));
201
202 /* Write redo log. */
203 /* For now, we use LOG_REC_UPDATE_IN_PLACE to log this enlarge.
204 In the future, we may need to add a new log type for this. */
205 const bool opened = mlog_open_and_write_index(
206 mtr, rec, index, MLOG_REC_UPDATE_IN_PLACE,
207 1 + DATA_ROLL_PTR_LEN + 14 + 2 + MLOG_BUF_MARGIN, log_ptr);
208
209 if (!opened) {
210 /* Logging in mtr is switched off during
211 crash recovery */
212 return;
213 }
214
215 /* Flags */
216 mach_write_to_1(log_ptr, flags);
217 log_ptr++;
218 /* TRX_ID Position */
219 log_ptr += mach_write_compressed(log_ptr, 0);
220 /* ROLL_PTR */
221 trx_write_roll_ptr(log_ptr, 0);
222 log_ptr += DATA_ROLL_PTR_LEN;
223 /* TRX_ID */
224 log_ptr += mach_u64_write_compressed(log_ptr, 0);
225
226 /* Offset */
227 mach_write_to_2(log_ptr, page_offset(rec));
228 log_ptr += 2;
229 /* Info bits */
230 mach_write_to_1(log_ptr, rec_info);
231 log_ptr++;
232 /* N fields */
233 log_ptr += mach_write_compressed(log_ptr, 1);
234 /* Field no, len */
235 log_ptr += mach_write_compressed(log_ptr, 0);
236 log_ptr += mach_write_compressed(log_ptr, len);
237 /* Data */
238 memcpy(log_ptr, new_mbr_ptr, len);
239 log_ptr += len;
240
241 mlog_close(mtr, log_ptr);
242
243 return;
244 }
245
246 /** Update the mbr field of a spatial index row.
247 @return true if update is successful */
248 bool rtr_update_mbr_field(
249 btr_cur_t *cursor, /*!< in/out: cursor pointed to rec.*/
250 ulint *offsets, /*!< in/out: offsets on rec. */
251 btr_cur_t *cursor2, /*!< in/out: cursor pointed to rec
252 that should be deleted.
253 this cursor is for btr_compress to
254 delete the merged page's father rec.*/
255 page_t *child_page, /*!< in: child page. */
256 rtr_mbr_t *mbr, /*!< in: the new mbr. */
257 rec_t *new_rec, /*!< in: rec to use */
258 mtr_t *mtr) /*!< in: mtr */
259 {
260 dict_index_t *index = cursor->index;
261 mem_heap_t *heap;
262 page_t *page;
263 rec_t *rec;
264 uint32_t flags =
265 BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG | BTR_KEEP_SYS_FLAG;
266 dberr_t err;
267 big_rec_t *dummy_big_rec;
268 buf_block_t *block;
269 rec_t *child_rec;
270 ulint up_match = 0;
271 ulint low_match = 0;
272 page_no_t child;
273 ulint rec_info;
274 page_zip_des_t *page_zip;
275 bool ins_suc = true;
276 ulint cur2_pos = 0;
277 page_no_t del_page_no = 0;
278 ulint *offsets2;
279
280 rec = btr_cur_get_rec(cursor);
281 page = page_align(rec);
282
283 rec_info = rec_get_info_bits(rec, rec_offs_comp(offsets));
284
285 heap = mem_heap_create(100, UT_LOCATION_HERE);
286 block = btr_cur_get_block(cursor);
287 ut_ad(page == buf_block_get_frame(block));
288 page_zip = buf_block_get_page_zip(block);
289
290 child = btr_node_ptr_get_child_page_no(rec, offsets);
291
292 if (new_rec) {
293 child_rec = new_rec;
294 } else {
295 child_rec = page_rec_get_next(page_get_infimum_rec(child_page));
296 }
297
298 dtuple_t *node_ptr =
299 rtr_index_build_node_ptr(index, mbr, child_rec, child, heap);
300
301 /* We need to remember the child page no of cursor2, since page could be
302 reorganized or insert a new rec before it. */
303 if (cursor2) {
304 rec_t *del_rec = btr_cur_get_rec(cursor2);
305 offsets2 = rec_get_offsets(btr_cur_get_rec(cursor2), index, nullptr,
306 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
307 del_page_no = btr_node_ptr_get_child_page_no(del_rec, offsets2);
308 cur2_pos = page_rec_get_n_recs_before(btr_cur_get_rec(cursor2));
309 }
310
311 if (rec_info & REC_INFO_MIN_REC_FLAG) {
312 /* When the rec is minimal rec in this level, we do
313 in-place update for avoiding it move to other place. */
314
315 if (page_zip) {
316 /* Check if there's enough space for in-place
317 update the zip page. */
318 if (!btr_cur_update_alloc_zip(page_zip, btr_cur_get_page_cur(cursor),
319 index, offsets, rec_offs_size(offsets),
320 false, mtr)) {
321 /* If there's not enough space for
322 inplace update zip page, we do delete
323 insert. */
324 ins_suc = false;
325
326 /* Since btr_cur_update_alloc_zip could
327 reorganize the page, we need to reposition
328 cursor2. */
329 if (cursor2) {
330 cursor2->page_cur.rec = page_rec_get_nth(page, cur2_pos);
331 }
332
333 goto update_mbr;
334 }
335
336 /* Record could be repositioned */
337 rec = btr_cur_get_rec(cursor);
338
339 #ifdef UNIV_DEBUG
340 /* Make sure it is still the first record */
341 rec_info = rec_get_info_bits(rec, rec_offs_comp(offsets));
342 ut_ad(rec_info & REC_INFO_MIN_REC_FLAG);
343 #endif /* UNIV_DEBUG */
344 }
345
346 rtr_update_mbr_field_in_place(index, rec, offsets, mbr, mtr);
347
348 if (page_zip) {
349 page_zip_write_rec(page_zip, rec, index, offsets, 0);
350 }
351
352 if (cursor2) {
353 ulint *offsets2;
354
355 if (page_zip) {
356 cursor2->page_cur.rec = page_rec_get_nth(page, cur2_pos);
357 }
358 offsets2 = rec_get_offsets(btr_cur_get_rec(cursor2), index, nullptr,
359 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
360 ut_ad(del_page_no ==
361 btr_node_ptr_get_child_page_no(cursor2->page_cur.rec, offsets2));
362
363 page_cur_delete_rec(btr_cur_get_page_cur(cursor2), index, offsets2, mtr);
364 }
365 } else if (page_get_n_recs(page) == 1) {
366 /* When there's only one rec in the page, we do insert/delete to
367 avoid page merge. */
368
369 page_cur_t page_cur;
370 rec_t *insert_rec;
371 ulint *insert_offsets = nullptr;
372 ulint old_pos;
373 rec_t *old_rec;
374
375 ut_ad(cursor2 == nullptr);
376
377 /* Insert the new mbr rec. */
378 old_pos = page_rec_get_n_recs_before(rec);
379
380 err = btr_cur_optimistic_insert(flags, cursor, &insert_offsets, &heap,
381 node_ptr, &insert_rec, &dummy_big_rec,
382 nullptr, mtr);
383
384 ut_ad(err == DB_SUCCESS);
385
386 btr_cur_position(index, insert_rec, block, cursor);
387
388 /* Delete the old mbr rec. */
389 old_rec = page_rec_get_nth(page, old_pos);
390 ut_ad(old_rec != insert_rec);
391
392 page_cur_position(old_rec, block, &page_cur);
393 offsets2 = rec_get_offsets(old_rec, index, nullptr, ULINT_UNDEFINED,
394 UT_LOCATION_HERE, &heap);
395 page_cur_delete_rec(&page_cur, index, offsets2, mtr);
396
397 } else {
398 update_mbr:
399 /* When there're not only 1 rec in the page, we do delete/insert
400 to avoid page split. */
401 rec_t *insert_rec;
402 ulint *insert_offsets = nullptr;
403 rec_t *next_rec;
404
405 /* Delete the rec which cursor point to. */
406 next_rec = page_rec_get_next(rec);
407 page_cur_delete_rec(btr_cur_get_page_cur(cursor), index, offsets, mtr);
408 if (!ins_suc) {
409 ut_ad(rec_info & REC_INFO_MIN_REC_FLAG);
410
411 btr_set_min_rec_mark(next_rec, mtr);
412 }
413
414 /* If there's more than 1 rec left in the page, delete
415 the rec which cursor2 point to. Otherwise, delete it later.*/
416 if (cursor2 && page_get_n_recs(page) > 1) {
417 ulint cur2_rec_info;
418 rec_t *cur2_rec;
419
420 cur2_rec = cursor2->page_cur.rec;
421 offsets2 = rec_get_offsets(cur2_rec, index, nullptr, ULINT_UNDEFINED,
422 UT_LOCATION_HERE, &heap);
423
424 cur2_rec_info = rec_get_info_bits(cur2_rec, rec_offs_comp(offsets2));
425 if (cur2_rec_info & REC_INFO_MIN_REC_FLAG) {
426 /* If we delete the leftmost node
427 pointer on a non-leaf level, we must
428 mark the new leftmost node pointer as
429 the predefined minimum record */
430 rec_t *next_rec = page_rec_get_next(cur2_rec);
431 btr_set_min_rec_mark(next_rec, mtr);
432 }
433
434 ut_ad(del_page_no == btr_node_ptr_get_child_page_no(cur2_rec, offsets2));
435 page_cur_delete_rec(btr_cur_get_page_cur(cursor2), index, offsets2, mtr);
436 cursor2 = nullptr;
437 }
438
439 /* Insert the new rec. */
440 page_cur_search_with_match(block, index, node_ptr, PAGE_CUR_LE, &up_match,
441 &low_match, btr_cur_get_page_cur(cursor),
442 nullptr);
443
444 err = btr_cur_optimistic_insert(flags, cursor, &insert_offsets, &heap,
445 node_ptr, &insert_rec, &dummy_big_rec,
446 nullptr, mtr);
447
448 if (!ins_suc && err == DB_SUCCESS) {
449 ins_suc = true;
450 }
451
452 /* If optimistic insert fail, try reorganize the page
453 and insert again. */
454 if (err != DB_SUCCESS && ins_suc) {
455 btr_page_reorganize(btr_cur_get_page_cur(cursor), index, mtr);
456
457 err = btr_cur_optimistic_insert(flags, cursor, &insert_offsets, &heap,
458 node_ptr, &insert_rec, &dummy_big_rec,
459 nullptr, mtr);
460
461 /* Will do pessimistic insert */
462 if (err != DB_SUCCESS) {
463 ins_suc = false;
464 }
465 }
466
467 /* Insert succeed, position cursor the inserted rec.*/
468 if (ins_suc) {
469 btr_cur_position(index, insert_rec, block, cursor);
470 offsets = rec_get_offsets(insert_rec, index, offsets, ULINT_UNDEFINED,
471 UT_LOCATION_HERE, &heap);
472 }
473
474 /* Delete the rec which cursor2 point to. */
475 if (cursor2) {
476 ulint cur2_pno;
477 rec_t *cur2_rec;
478
479 cursor2->page_cur.rec = page_rec_get_nth(page, cur2_pos);
480
481 cur2_rec = btr_cur_get_rec(cursor2);
482
483 offsets2 = rec_get_offsets(cur2_rec, index, nullptr, ULINT_UNDEFINED,
484 UT_LOCATION_HERE, &heap);
485
486 /* If the cursor2 position is on a wrong rec, we
487 need to reposition it. */
488 cur2_pno = btr_node_ptr_get_child_page_no(cur2_rec, offsets2);
489 if ((del_page_no != cur2_pno) || (cur2_rec == insert_rec)) {
490 cur2_rec = page_rec_get_next(page_get_infimum_rec(page));
491
492 while (!page_rec_is_supremum(cur2_rec)) {
493 offsets2 = rec_get_offsets(cur2_rec, index, nullptr, ULINT_UNDEFINED,
494 UT_LOCATION_HERE, &heap);
495 cur2_pno = btr_node_ptr_get_child_page_no(cur2_rec, offsets2);
496 if (cur2_pno == del_page_no) {
497 if (insert_rec != cur2_rec) {
498 cursor2->page_cur.rec = cur2_rec;
499 break;
500 }
501 }
502 cur2_rec = page_rec_get_next(cur2_rec);
503 }
504
505 ut_ad(!page_rec_is_supremum(cur2_rec));
506 }
507
508 rec_info = rec_get_info_bits(cur2_rec, rec_offs_comp(offsets2));
509 if (rec_info & REC_INFO_MIN_REC_FLAG) {
510 /* If we delete the leftmost node
511 pointer on a non-leaf level, we must
512 mark the new leftmost node pointer as
513 the predefined minimum record */
514 rec_t *next_rec = page_rec_get_next(cur2_rec);
515 btr_set_min_rec_mark(next_rec, mtr);
516 }
517
518 ut_ad(cur2_pno == del_page_no && cur2_rec != insert_rec);
519
520 page_cur_delete_rec(btr_cur_get_page_cur(cursor2), index, offsets2, mtr);
521 }
522
523 if (!ins_suc) {
524 mem_heap_t *new_heap = nullptr;
525
526 err = btr_cur_pessimistic_insert(flags, cursor, &insert_offsets,
527 &new_heap, node_ptr, &insert_rec,
528 &dummy_big_rec, nullptr, mtr);
529
530 ut_ad(err == DB_SUCCESS);
531
532 if (new_heap) {
533 mem_heap_free(new_heap);
534 }
535 }
536
537 if (cursor2) {
538 btr_cur_compress_if_useful(cursor, false, mtr);
539 }
540 }
541
542 #ifdef UNIV_DEBUG
543 page_no_t left_page_no = btr_page_get_prev(page, mtr);
544
545 if (left_page_no == FIL_NULL) {
546 ut_a(REC_INFO_MIN_REC_FLAG &
547 rec_get_info_bits(page_rec_get_next(page_get_infimum_rec(page)),
548 page_is_comp(page)));
549 }
550 #endif /* UNIV_DEBUG */
551
552 mem_heap_free(heap);
553
554 return (true);
555 }
556
557 /** Update parent page's MBR and Predicate lock information during a split */
558 static void rtr_adjust_upper_level(
559 btr_cur_t *sea_cur, /*!< in: search cursor */
560 uint32_t flags, /*!< in: undo logging and
561 locking flags */
562 buf_block_t *block, /*!< in/out: page to be split */
563 buf_block_t *new_block, /*!< in/out: the new half page */
564 rtr_mbr_t *mbr, /*!< in: MBR on the old page */
565 rtr_mbr_t *new_mbr, /*!< in: MBR on the new page */
566 mtr_t *mtr) /*!< in: mtr */
567 {
568 page_t *page;
569 page_t *new_page;
570 page_no_t page_no;
571 page_no_t new_page_no;
572 page_zip_des_t *page_zip;
573 page_zip_des_t *new_page_zip;
574 dict_index_t *index = sea_cur->index;
575 btr_cur_t cursor;
576 ulint *offsets;
577 mem_heap_t *heap;
578 ulint level;
579 dtuple_t *node_ptr_upper;
580 page_no_t prev_page_no;
581 page_no_t next_page_no;
582 space_id_t space;
583 page_cur_t *page_cursor;
584 rtr_mbr_t parent_mbr;
585 lock_prdt_t prdt;
586 lock_prdt_t new_prdt;
587 dberr_t err;
588 big_rec_t *dummy_big_rec;
589 rec_t *rec;
590
591 /* Create a memory heap where the data tuple is stored */
592 heap = mem_heap_create(1024, UT_LOCATION_HERE);
593
594 cursor.thr = sea_cur->thr;
595
596 /* Get the level of the split pages */
597 level = btr_page_get_level(buf_block_get_frame(block));
598 ut_ad(level == btr_page_get_level(buf_block_get_frame(new_block)));
599
600 page = buf_block_get_frame(block);
601 page_no = block->page.id.page_no();
602 page_zip = buf_block_get_page_zip(block);
603
604 new_page = buf_block_get_frame(new_block);
605 new_page_no = new_block->page.id.page_no();
606 new_page_zip = buf_block_get_page_zip(new_block);
607
608 /* Set new mbr for the old page on the upper level. */
609 /* Look up the index for the node pointer to page */
610 offsets = rtr_page_get_father_block(nullptr, heap, index, block, mtr, sea_cur,
611 &cursor);
612
613 page_cursor = btr_cur_get_page_cur(&cursor);
614
615 rtr_get_mbr_from_rec(page_cursor->rec, offsets, &parent_mbr);
616
617 rtr_update_mbr_field(&cursor, offsets, nullptr, page, mbr, nullptr, mtr);
618
619 /* Already updated parent MBR, reset in our path */
620 if (sea_cur->rtr_info) {
621 node_visit_t *node_visit = rtr_get_parent_node(sea_cur, level + 1, true);
622 if (node_visit) {
623 node_visit->mbr_inc = 0;
624 }
625 }
626
627 /* Insert the node for the new page. */
628 node_ptr_upper = rtr_index_build_node_ptr(
629 index, new_mbr, page_rec_get_next(page_get_infimum_rec(new_page)),
630 new_page_no, heap);
631
632 ulint up_match = 0;
633 ulint low_match = 0;
634
635 buf_block_t *father_block = btr_cur_get_block(&cursor);
636
637 page_cur_search_with_match(father_block, index, node_ptr_upper, PAGE_CUR_LE,
638 &up_match, &low_match,
639 btr_cur_get_page_cur(&cursor), nullptr);
640
641 err = btr_cur_optimistic_insert(
642 flags | BTR_NO_LOCKING_FLAG | BTR_KEEP_SYS_FLAG | BTR_NO_UNDO_LOG_FLAG,
643 &cursor, &offsets, &heap, node_ptr_upper, &rec, &dummy_big_rec, nullptr,
644 mtr);
645
646 if (err == DB_FAIL) {
647 cursor.rtr_info = sea_cur->rtr_info;
648 cursor.tree_height = sea_cur->tree_height;
649
650 mem_heap_t *new_heap = nullptr;
651
652 DBUG_EXECUTE_IF("rtr_page_need_first_split",
653 { DBUG_SET("+d,rtr_page_need_second_split"); });
654
655 err = btr_cur_pessimistic_insert(
656 flags | BTR_NO_LOCKING_FLAG | BTR_KEEP_SYS_FLAG | BTR_NO_UNDO_LOG_FLAG,
657 &cursor, &offsets, &new_heap, node_ptr_upper, &rec, &dummy_big_rec,
658 nullptr, mtr);
659
660 DBUG_EXECUTE_IF("rtr_page_need_first_split",
661 { DBUG_SET("-d,rtr_page_need_second_split"); });
662
663 cursor.rtr_info = nullptr;
664 ut_a(err == DB_SUCCESS);
665
666 if (new_heap) {
667 mem_heap_free(new_heap);
668 }
669 }
670
671 prdt.data = static_cast<void *>(mbr);
672 prdt.op = 0;
673 new_prdt.data = static_cast<void *>(new_mbr);
674 new_prdt.op = 0;
675
676 ut_ad(dict_index_get_space(index) == page_cursor->block->page.id.space());
677 lock_prdt_update_parent(block, new_block, &prdt, &new_prdt,
678 page_cursor->block->page.id);
679
680 mem_heap_free(heap);
681
682 /* Get the previous and next pages of page */
683 prev_page_no = btr_page_get_prev(page, mtr);
684 next_page_no = btr_page_get_next(page, mtr);
685 space = block->page.id.space();
686 const page_size_t &page_size = dict_table_page_size(index->table);
687
688 /* Update page links of the level */
689 if (prev_page_no != FIL_NULL) {
690 page_id_t prev_page_id(space, prev_page_no);
691
692 buf_block_t *prev_block = btr_block_get(prev_page_id, page_size, RW_X_LATCH,
693 UT_LOCATION_HERE, index, mtr);
694 #ifdef UNIV_BTR_DEBUG
695 ut_a(page_is_comp(prev_block->frame) == page_is_comp(page));
696 ut_a(btr_page_get_next(prev_block->frame, mtr) == block->page.id.page_no());
697 #endif /* UNIV_BTR_DEBUG */
698
699 btr_page_set_next(buf_block_get_frame(prev_block),
700 buf_block_get_page_zip(prev_block), page_no, mtr);
701 }
702
703 if (next_page_no != FIL_NULL) {
704 page_id_t next_page_id(space, next_page_no);
705
706 buf_block_t *next_block = btr_block_get(next_page_id, page_size, RW_X_LATCH,
707 UT_LOCATION_HERE, index, mtr);
708 #ifdef UNIV_BTR_DEBUG
709 ut_a(page_is_comp(next_block->frame) == page_is_comp(page));
710 ut_a(btr_page_get_prev(next_block->frame, mtr) == page_get_page_no(page));
711 #endif /* UNIV_BTR_DEBUG */
712
713 btr_page_set_prev(buf_block_get_frame(next_block),
714 buf_block_get_page_zip(next_block), new_page_no, mtr);
715 }
716
717 btr_page_set_prev(page, page_zip, prev_page_no, mtr);
718 btr_page_set_next(page, page_zip, new_page_no, mtr);
719
720 btr_page_set_prev(new_page, new_page_zip, page_no, mtr);
721 btr_page_set_next(new_page, new_page_zip, next_page_no, mtr);
722 }
723
724 /** Moves record list to another page for rtree splitting.
725
726 IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
727 if new_block is a compressed leaf page in a secondary index.
728 This has to be done either within the same mini-transaction,
729 or by invoking ibuf_reset_free_bits() before mtr_commit().
730
731 @return true on success; false on compression failure */
732 static bool rtr_split_page_move_rec_list(
733 rtr_split_node_t *node_array, /*!< in: split node array. */
734 int first_rec_group, /*!< in: group number of the
735 first rec. */
736 buf_block_t *new_block, /*!< in/out: index page
737 where to move */
738 buf_block_t *block, /*!< in/out: page containing
739 split_rec */
740 rec_t *first_rec, /*!< in: first record not to
741 move */
742 dict_index_t *index, /*!< in: record descriptor */
743 mem_heap_t *heap, /*!< in: pointer to memory
744 heap, or NULL */
745 mtr_t *mtr) /*!< in: mtr */
746 {
747 rtr_split_node_t *cur_split_node;
748 rtr_split_node_t *end_split_node;
749 page_cur_t page_cursor;
750 page_cur_t new_page_cursor;
751 page_t *page;
752 page_t *new_page;
753 ulint offsets_[REC_OFFS_NORMAL_SIZE];
754 ulint *offsets = offsets_;
755 page_zip_des_t *new_page_zip = buf_block_get_page_zip(new_block);
756 rec_t *rec;
757 rec_t *ret;
758 ulint moved = 0;
759 ulint max_to_move = 0;
760 rtr_rec_move_t *rec_move = nullptr;
761
762 rec_offs_init(offsets_);
763
764 page_cur_set_before_first(block, &page_cursor);
765 page_cur_set_before_first(new_block, &new_page_cursor);
766
767 page = buf_block_get_frame(block);
768 new_page = buf_block_get_frame(new_block);
769 ret = page_rec_get_prev(page_get_supremum_rec(new_page));
770
771 end_split_node = node_array + page_get_n_recs(page);
772
773 mtr_log_t log_mode = MTR_LOG_NONE;
774
775 if (new_page_zip) {
776 log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
777 }
778
779 max_to_move = page_get_n_recs(buf_block_get_frame(block));
780 rec_move = static_cast<rtr_rec_move_t *>(
781 mem_heap_alloc(heap, sizeof(*rec_move) * max_to_move));
782
783 /* Insert the recs in group 2 to new page. */
784 for (cur_split_node = node_array; cur_split_node < end_split_node;
785 ++cur_split_node) {
786 if (cur_split_node->n_node != first_rec_group) {
787 lock_rec_store_on_page_infimum(block, cur_split_node->key);
788
789 offsets = rec_get_offsets(cur_split_node->key, index, offsets,
790 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
791
792 ut_ad(cur_split_node->key != first_rec || !page_is_leaf(page));
793
794 rec = page_cur_insert_rec_low(page_cur_get_rec(&new_page_cursor), index,
795 cur_split_node->key, offsets, mtr);
796
797 ut_a(rec);
798
799 lock_rec_restore_from_page_infimum(new_block, rec, block);
800
801 page_cur_move_to_next(&new_page_cursor);
802
803 rec_move[moved].new_rec = rec;
804 rec_move[moved].old_rec = cur_split_node->key;
805 rec_move[moved].moved = false;
806 moved++;
807
808 if (moved > max_to_move) {
809 ut_d(ut_error);
810 ut_o(break);
811 }
812 }
813 }
814
815 /* Update PAGE_MAX_TRX_ID on the uncompressed page.
816 Modifications will be redo logged and copied to the compressed
817 page in page_zip_compress() or page_zip_reorganize() below.
818 Multiple transactions cannot simultaneously operate on the
819 same temp-table in parallel.
820 max_trx_id is ignored for temp tables because it not required
821 for MVCC. */
822 if (dict_index_is_sec_or_ibuf(index) && page_is_leaf(page) &&
823 !index->table->is_temporary()) {
824 page_update_max_trx_id(new_block, nullptr, page_get_max_trx_id(page), mtr);
825 }
826
827 if (new_page_zip) {
828 mtr_set_log_mode(mtr, log_mode);
829
830 if (!page_zip_compress(new_page_zip, new_page, index, page_zip_level,
831 mtr)) {
832 ulint ret_pos;
833
834 /* Before trying to reorganize the page,
835 store the number of preceding records on the page. */
836 ret_pos = page_rec_get_n_recs_before(ret);
837 /* Before copying, "ret" was the predecessor
838 of the predefined supremum record. If it was
839 the predefined infimum record, then it would
840 still be the infimum, and we would have
841 ret_pos == 0. */
842
843 if (UNIV_UNLIKELY(!page_zip_reorganize(new_block, index, mtr))) {
844 if (UNIV_UNLIKELY(
845 !page_zip_decompress(new_page_zip, new_page, false))) {
846 ut_error;
847 }
848 #ifdef UNIV_GIS_DEBUG
849 ut_ad(page_validate(new_page, index));
850 #endif
851
852 return (false);
853 }
854
855 /* The page was reorganized: Seek to ret_pos. */
856 ret = page_rec_get_nth(new_page, ret_pos);
857 }
858 }
859
860 /* Update the lock table */
861 lock_rtr_move_rec_list(new_block, block, rec_move, moved);
862
863 /* Delete recs in second group from the old page. */
864 for (cur_split_node = node_array; cur_split_node < end_split_node;
865 ++cur_split_node) {
866 if (cur_split_node->n_node != first_rec_group) {
867 page_cur_position(cur_split_node->key, block, &page_cursor);
868 offsets = rec_get_offsets(page_cur_get_rec(&page_cursor), index, offsets,
869 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
870 page_cur_delete_rec(&page_cursor, index, offsets, mtr);
871 }
872 }
873
874 return (true);
875 }
876
877 /** Splits an R-tree index page to halves and inserts the tuple. It is assumed
878 that mtr holds an x-latch to the index tree. NOTE: the tree x-latch is
879 released within this function! NOTE that the operation of this
880 function must always succeed, we cannot reverse it: therefore enough
881 free disk space (2 pages) must be guaranteed to be available before
882 this function is called.
883 @return inserted record */
884 rec_t *rtr_page_split_and_insert(
885 uint32_t flags, /*!< in: undo logging and locking flags */
886 btr_cur_t *cursor, /*!< in/out: cursor at which to insert; when the
887 function returns, the cursor is positioned
888 on the predecessor of the inserted record */
889 ulint **offsets, /*!< out: offsets on inserted record */
890 mem_heap_t **heap, /*!< in/out: pointer to memory heap, or NULL */
891 const dtuple_t *tuple, /*!< in: tuple to insert */
892 mtr_t *mtr) /*!< in: mtr */
893 {
894 buf_block_t *block;
895 page_t *page;
896 page_t *new_page;
897 page_no_t page_no;
898 byte direction;
899 page_no_t hint_page_no;
900 buf_block_t *new_block;
901 page_zip_des_t *page_zip;
902 page_zip_des_t *new_page_zip;
903 buf_block_t *insert_block;
904 page_cur_t *page_cursor;
905 rec_t *rec = nullptr;
906 ulint n_recs;
907 ulint total_data;
908 ulint insert_size;
909 rtr_split_node_t *rtr_split_node_array;
910 rtr_split_node_t *cur_split_node;
911 rtr_split_node_t *end_split_node;
912 double *buf_pos;
913 ulint page_level;
914 node_seq_t current_ssn;
915 node_seq_t next_ssn;
916 buf_block_t *root_block;
917 rtr_mbr_t mbr;
918 rtr_mbr_t new_mbr;
919 lock_prdt_t prdt;
920 lock_prdt_t new_prdt;
921 rec_t *first_rec = nullptr;
922 int first_rec_group = 1;
923 ulint n_iterations [[maybe_unused]] = 0;
924
925 if (!*heap) {
926 *heap = mem_heap_create(1024, UT_LOCATION_HERE);
927 }
928
929 func_start:
930 ut_ad(tuple->m_heap != *heap);
931 mem_heap_empty(*heap);
932 *offsets = nullptr;
933
934 ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(cursor->index),
935 MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK));
936 ut_ad(!dict_index_is_online_ddl(cursor->index) || (flags & BTR_CREATE_FLAG) ||
937 cursor->index->is_clustered());
938 ut_ad(rw_lock_own_flagged(dict_index_get_lock(cursor->index),
939 RW_LOCK_FLAG_X | RW_LOCK_FLAG_SX));
940
941 block = btr_cur_get_block(cursor);
942 page = buf_block_get_frame(block);
943 page_zip = buf_block_get_page_zip(block);
944 page_level = btr_page_get_level(page);
945 current_ssn = page_get_ssn_id(page);
946
947 ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
948 ut_ad(page_get_n_recs(page) >= 1);
949
950 page_no = block->page.id.page_no();
951
952 if (btr_page_get_prev(page, mtr) == FIL_NULL && !page_is_leaf(page)) {
953 first_rec =
954 page_rec_get_next(page_get_infimum_rec(buf_block_get_frame(block)));
955 }
956
957 /* Initial split nodes array. */
958 rtr_split_node_array =
959 rtr_page_split_initialize_nodes(*heap, cursor, offsets, tuple, &buf_pos);
960
961 /* Divide all mbrs to two groups. */
962 n_recs = page_get_n_recs(page) + 1;
963
964 end_split_node = rtr_split_node_array + n_recs;
965
966 #ifdef UNIV_GIS_DEBUG
967 fprintf(stderr, "Before split a page:\n");
968 for (cur_split_node = rtr_split_node_array; cur_split_node < end_split_node;
969 ++cur_split_node) {
970 for (int i = 0; i < SPDIMS * 2; i++) {
971 fprintf(stderr, "%.2lf ", *(cur_split_node->coords + i));
972 }
973 fprintf(stderr, "\n");
974 }
975 #endif
976
977 insert_size = rec_get_converted_size(cursor->index, tuple);
978 total_data = page_get_data_size(page) + insert_size;
979 first_rec_group = split_rtree_node(
980 rtr_split_node_array, static_cast<int>(n_recs),
981 static_cast<int>(total_data), static_cast<int>(insert_size), 0, 2, 2,
982 &buf_pos, SPDIMS, static_cast<uchar *>(first_rec),
983 cursor->index->rtr_srs.get());
984
985 /* Allocate a new page to the index */
986 direction = FSP_UP;
987 hint_page_no = page_no + 1;
988 new_block = btr_page_alloc(cursor->index, hint_page_no, direction, page_level,
989 mtr, mtr);
990 new_page_zip = buf_block_get_page_zip(new_block);
991 btr_page_create(new_block, new_page_zip, cursor->index, page_level, mtr);
992
993 new_page = buf_block_get_frame(new_block);
994 ut_ad(page_get_ssn_id(new_page) == 0);
995
996 /* Set new ssn to the new page and page. */
997 page_set_ssn_id(new_block, new_page_zip, current_ssn, mtr);
998 next_ssn = rtr_get_new_ssn_id(cursor->index);
999
1000 page_set_ssn_id(block, page_zip, next_ssn, mtr);
1001
1002 /* Keep recs in first group to the old page, move recs in second
1003 groups to the new page. */
1004 if (false
1005 #ifdef UNIV_ZIP_COPY
1006 || page_zip
1007 #endif
1008 || !rtr_split_page_move_rec_list(rtr_split_node_array, first_rec_group,
1009 new_block, block, first_rec,
1010 cursor->index, *heap, mtr)) {
1011 ulint n = 0;
1012 rec_t *rec;
1013 ulint moved = 0;
1014 ulint max_to_move = 0;
1015 rtr_rec_move_t *rec_move = nullptr;
1016 ulint pos;
1017
1018 /* For some reason, compressing new_page failed,
1019 even though it should contain fewer records than
1020 the original page. Copy the page byte for byte
1021 and then delete the records from both pages
1022 as appropriate. Deleting will always succeed. */
1023 ut_a(new_page_zip);
1024
1025 page_zip_copy_recs(new_page_zip, new_page, page_zip, page, cursor->index,
1026 mtr);
1027
1028 page_cursor = btr_cur_get_page_cur(cursor);
1029
1030 /* Move locks on recs. */
1031 max_to_move = page_get_n_recs(page);
1032 rec_move = static_cast<rtr_rec_move_t *>(
1033 mem_heap_alloc(*heap, sizeof(*rec_move) * max_to_move));
1034
1035 /* Init the rec_move array for moving lock on recs. */
1036 for (cur_split_node = rtr_split_node_array;
1037 cur_split_node < end_split_node - 1; ++cur_split_node) {
1038 if (cur_split_node->n_node != first_rec_group) {
1039 pos = page_rec_get_n_recs_before(cur_split_node->key);
1040 rec = page_rec_get_nth(new_page, pos);
1041 ut_a(rec);
1042
1043 rec_move[moved].new_rec = rec;
1044 rec_move[moved].old_rec = cur_split_node->key;
1045 rec_move[moved].moved = false;
1046 moved++;
1047
1048 if (moved > max_to_move) {
1049 ut_d(ut_error);
1050 ut_o(break);
1051 }
1052 }
1053 }
1054
1055 /* Update the lock table */
1056 lock_rtr_move_rec_list(new_block, block, rec_move, moved);
1057
1058 /* Delete recs in first group from the new page. */
1059 for (cur_split_node = rtr_split_node_array;
1060 cur_split_node < end_split_node - 1; ++cur_split_node) {
1061 if (cur_split_node->n_node == first_rec_group) {
1062 ulint pos;
1063
1064 pos = page_rec_get_n_recs_before(cur_split_node->key);
1065 ut_a(pos > 0);
1066 rec_t *new_rec = page_rec_get_nth(new_page, pos - n);
1067
1068 ut_a(new_rec && page_rec_is_user_rec(new_rec));
1069 page_cur_position(new_rec, new_block, page_cursor);
1070
1071 *offsets =
1072 rec_get_offsets(page_cur_get_rec(page_cursor), cursor->index,
1073 *offsets, ULINT_UNDEFINED, UT_LOCATION_HERE, heap);
1074
1075 page_cur_delete_rec(page_cursor, cursor->index, *offsets, mtr);
1076 n++;
1077 }
1078 }
1079
1080 /* Delete recs in second group from the old page. */
1081 for (cur_split_node = rtr_split_node_array;
1082 cur_split_node < end_split_node - 1; ++cur_split_node) {
1083 if (cur_split_node->n_node != first_rec_group) {
1084 page_cur_position(cur_split_node->key, block, page_cursor);
1085 *offsets =
1086 rec_get_offsets(page_cur_get_rec(page_cursor), cursor->index,
1087 *offsets, ULINT_UNDEFINED, UT_LOCATION_HERE, heap);
1088 page_cur_delete_rec(page_cursor, cursor->index, *offsets, mtr);
1089 }
1090 }
1091
1092 #ifdef UNIV_GIS_DEBUG
1093 ut_ad(page_validate(new_page, cursor->index));
1094 ut_ad(page_validate(page, cursor->index));
1095 #endif
1096 }
1097
1098 /* Insert the new rec to the proper page. */
1099 cur_split_node = end_split_node - 1;
1100 if (cur_split_node->n_node != first_rec_group) {
1101 insert_block = new_block;
1102 } else {
1103 insert_block = block;
1104 }
1105
1106 /* Reposition the cursor for insert and try insertion */
1107 page_cursor = btr_cur_get_page_cur(cursor);
1108
1109 page_cur_search(insert_block, cursor->index, tuple, PAGE_CUR_LE, page_cursor);
1110
1111 /* It's possible that the new record is too big to be inserted into
1112 the page, and it'll need the second round split in this case.
1113 We test this scenario here*/
1114 DBUG_EXECUTE_IF(
1115 "rtr_page_need_second_split", if (n_iterations == 0) {
1116 rec = nullptr;
1117 goto after_insert;
1118 });
1119
1120 rec = page_cur_tuple_insert(page_cursor, tuple, cursor->index, offsets, heap,
1121 mtr);
1122
1123 /* If insert did not fit, try page reorganization.
1124 For compressed pages, page_cur_tuple_insert() will have
1125 attempted this already. */
1126 if (rec == nullptr) {
1127 if (!page_cur_get_page_zip(page_cursor) &&
1128 btr_page_reorganize(page_cursor, cursor->index, mtr)) {
1129 rec = page_cur_tuple_insert(page_cursor, tuple, cursor->index, offsets,
1130 heap, mtr);
1131 }
1132 /* If insert fail, we will try to split the insert_block
1133 again. */
1134 }
1135
1136 #ifdef UNIV_DEBUG
1137 after_insert:
1138 #endif
1139 /* Calculate the mbr on the upper half-page, and the mbr on
1140 original page. */
1141 rtr_page_cal_mbr(cursor->index, block, &mbr, *heap);
1142 rtr_page_cal_mbr(cursor->index, new_block, &new_mbr, *heap);
1143 prdt.data = &mbr;
1144 new_prdt.data = &new_mbr;
1145
1146 ut_ad(dict_index_get_space(cursor->index) == block->page.id.space());
1147 ut_ad(page_no == block->page.id.page_no());
1148 /* Check any predicate locks need to be moved/copied to the
1149 new page */
1150 lock_prdt_update_split(block, new_block, &prdt, &new_prdt);
1151
1152 /* Adjust the upper level. */
1153 rtr_adjust_upper_level(cursor, flags, block, new_block, &mbr, &new_mbr, mtr);
1154
1155 /* Save the new ssn to the root page, since we need to reinit
1156 the first ssn value from it after restart server. */
1157
1158 root_block = btr_root_block_get(cursor->index, RW_SX_LATCH, mtr);
1159
1160 page_zip = buf_block_get_page_zip(root_block);
1161 page_set_ssn_id(root_block, page_zip, next_ssn, mtr);
1162
1163 /* Insert fit on the page: update the free bits (for non-temporary
1164 tablespaces) for the left and right pages in the same mtr */
1165 if (page_is_leaf(page) && !cursor->index->table->is_temporary()) {
1166 ibuf_update_free_bits_for_two_pages_low(block, new_block, mtr);
1167 }
1168
1169 /* If the new res insert fail, we need to do another split
1170 again. */
1171 if (!rec) {
1172 /* We play safe and reset the free bits for new_page */
1173 if (!cursor->index->is_clustered() &&
1174 !cursor->index->table->is_temporary()) {
1175 ibuf_reset_free_bits(new_block);
1176 ibuf_reset_free_bits(block);
1177 }
1178
1179 if (cursor->m_own_rtr_info) {
1180 /* We need to clean the parent path here and search father node later,
1181 otherwise, it's possible that find a wrong parent. */
1182 rtr_clean_rtr_info(cursor->rtr_info, true);
1183 }
1184
1185 cursor->rtr_info = nullptr;
1186 n_iterations++;
1187
1188 rec_t *i_rec =
1189 page_rec_get_next(page_get_infimum_rec(buf_block_get_frame(block)));
1190 btr_cur_position(cursor->index, i_rec, block, cursor);
1191
1192 goto func_start;
1193 }
1194
1195 #ifdef UNIV_GIS_DEBUG
1196 ut_ad(page_validate(buf_block_get_frame(block), cursor->index));
1197 ut_ad(page_validate(buf_block_get_frame(new_block), cursor->index));
1198
1199 ut_ad(!rec || rec_offs_validate(rec, cursor->index, *offsets));
1200 #endif
1201 MONITOR_INC(MONITOR_INDEX_SPLIT);
1202
1203 return (rec);
1204 }
1205
1206 dberr_t rtr_ins_enlarge_mbr(btr_cur_t *btr_cur, mtr_t *mtr) {
1207 dberr_t err = DB_SUCCESS;
1208 rtr_mbr_t new_mbr;
1209 buf_block_t *block;
1210 mem_heap_t *heap;
1211 dict_index_t *index = btr_cur->index;
1212 page_cur_t *page_cursor;
1213 ulint *offsets;
1214 node_visit_t *node_visit;
1215 page_t *page;
1216
1217 ut_ad(dict_index_is_spatial(index));
1218
1219 /* If no rtr_info or rtree is one level tree, return. */
1220 if (!btr_cur->rtr_info || btr_cur->tree_height == 1) {
1221 return (err);
1222 }
1223
1224 /* Check path info is not empty. */
1225 ut_ad(!btr_cur->rtr_info->parent_path->empty());
1226
1227 /* Create a memory heap. */
1228 heap = mem_heap_create(1024, UT_LOCATION_HERE);
1229
1230 /* Leaf level page is stored in cursor */
1231 page_cursor = btr_cur_get_page_cur(btr_cur);
1232 block = page_cur_get_block(page_cursor);
1233
1234 for (ulint i = 1; i < btr_cur->tree_height; i++) {
1235 node_visit = rtr_get_parent_node(btr_cur, i, true);
1236 ut_ad(node_visit != nullptr);
1237
1238 /* If there's no mbr enlarge, return.*/
1239 if (node_visit->mbr_inc == 0) {
1240 block = node_visit->cursor->get_block();
1241 continue;
1242 }
1243
1244 /* Calculate the mbr of the child page. */
1245 rtr_page_cal_mbr(index, block, &new_mbr, heap);
1246
1247 /* Get father block. */
1248 btr_cur_t cursor;
1249 offsets = rtr_page_get_father_block(nullptr, heap, index, block, mtr,
1250 btr_cur, &cursor);
1251
1252 page = buf_block_get_frame(block);
1253
1254 /* Update the mbr field of the rec. */
1255 if (!rtr_update_mbr_field(&cursor, offsets, nullptr, page, &new_mbr,
1256 nullptr, mtr)) {
1257 err = DB_ERROR;
1258 break;
1259 }
1260
1261 page_cursor = btr_cur_get_page_cur(&cursor);
1262 block = page_cur_get_block(page_cursor);
1263 }
1264
1265 mem_heap_free(heap);
1266
1267 return (err);
1268 }
1269
1270 /** Copy recs from a page to new_block of rtree.
1271 Differs from page_copy_rec_list_end, because this function does not
1272 touch the lock table and max trx id on page or compress the page.
1273
1274 IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
1275 if new_block is a compressed leaf page in a secondary index.
1276 This has to be done either within the same mini-transaction,
1277 or by invoking ibuf_reset_free_bits() before mtr_commit().
1278
1279 @param[in] new_block Index page to copy to
1280 @param[in] block Index page of rec
1281 @param[in] rec Record on page
1282 @param[in] index Record descriptor
1283 @param[in,out] heap Heap memory
1284 @param[in] rec_move Recording records moved
1285 @param[in] max_move Num of rec to move
1286 @param[out] num_moved Num of rec to move
1287 @param[in] mtr Mini-transaction */
1288 void rtr_page_copy_rec_list_end_no_locks(buf_block_t *new_block,
1289 buf_block_t *block, rec_t *rec,
1290 dict_index_t *index, mem_heap_t *heap,
1291 rtr_rec_move_t *rec_move,
1292 ulint max_move, ulint *num_moved,
1293 mtr_t *mtr) {
1294 page_t *new_page = buf_block_get_frame(new_block);
1295 page_cur_t page_cur;
1296 page_cur_t cur1;
1297 rec_t *cur_rec;
1298 ulint offsets_1[REC_OFFS_NORMAL_SIZE];
1299 ulint *offsets1 = offsets_1;
1300 ulint offsets_2[REC_OFFS_NORMAL_SIZE];
1301 ulint *offsets2 = offsets_2;
1302 ulint moved = 0;
1303 bool is_leaf = page_is_leaf(new_page);
1304
1305 rec_offs_init(offsets_1);
1306 rec_offs_init(offsets_2);
1307
1308 page_cur_position(rec, block, &cur1);
1309
1310 if (page_cur_is_before_first(&cur1)) {
1311 page_cur_move_to_next(&cur1);
1312 }
1313
1314 btr_assert_not_corrupted(new_block, index);
1315 ut_a(page_is_comp(new_page) == page_rec_is_comp(rec));
1316 ut_a(mach_read_from_2(new_page + UNIV_PAGE_SIZE - 10) ==
1317 (page_is_comp(new_page) ? PAGE_NEW_INFIMUM : PAGE_OLD_INFIMUM));
1318
1319 cur_rec =
1320 page_rec_get_next(page_get_infimum_rec(buf_block_get_frame(new_block)));
1321 page_cur_position(cur_rec, new_block, &page_cur);
1322
1323 /* Copy records from the original page to the new page */
1324 while (!page_cur_is_after_last(&cur1)) {
1325 rec_t *cur1_rec = page_cur_get_rec(&cur1);
1326 rec_t *ins_rec;
1327
1328 if (page_rec_is_infimum(cur_rec)) {
1329 cur_rec = page_rec_get_next(cur_rec);
1330 }
1331
1332 offsets1 = rec_get_offsets(cur1_rec, index, offsets1, ULINT_UNDEFINED,
1333 UT_LOCATION_HERE, &heap);
1334 while (!page_rec_is_supremum(cur_rec)) {
1335 ulint cur_matched_fields = 0;
1336 int cmp;
1337
1338 offsets2 = rec_get_offsets(cur_rec, index, offsets2, ULINT_UNDEFINED,
1339 UT_LOCATION_HERE, &heap);
1340 cmp = cmp_rec_rec_with_match(cur1_rec, cur_rec, offsets1, offsets2, index,
1341 page_is_spatial_non_leaf(cur1_rec, index),
1342 false, &cur_matched_fields);
1343 if (cmp < 0) {
1344 page_cur_move_to_prev(&page_cur);
1345 break;
1346 } else if (cmp > 0) {
1347 /* Skip small recs. */
1348 page_cur_move_to_next(&page_cur);
1349 cur_rec = page_cur_get_rec(&page_cur);
1350 } else if (is_leaf) {
1351 if (rec_get_deleted_flag(cur1_rec, dict_table_is_comp(index->table))) {
1352 goto next;
1353 } else {
1354 /* We have two identical leaf records,
1355 skip copying the undeleted one, and
1356 unmark deleted on the current page */
1357 btr_rec_set_deleted_flag(cur_rec, nullptr, false);
1358 goto next;
1359 }
1360 }
1361 }
1362
1363 /* If position is on suprenum rec, need to move to
1364 previous rec. */
1365 if (page_rec_is_supremum(cur_rec)) {
1366 page_cur_move_to_prev(&page_cur);
1367 }
1368
1369 cur_rec = page_cur_get_rec(&page_cur);
1370
1371 offsets1 = rec_get_offsets(cur1_rec, index, offsets1, ULINT_UNDEFINED,
1372 UT_LOCATION_HERE, &heap);
1373
1374 ins_rec = page_cur_insert_rec_low(cur_rec, index, cur1_rec, offsets1, mtr);
1375 if (UNIV_UNLIKELY(!ins_rec)) {
1376 fprintf(stderr, "page number %lu and %lu\n",
1377 ulong{new_block->page.id.page_no()},
1378 ulong{block->page.id.page_no()});
1379
1380 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_520)
1381 << "rec offset " << page_offset(rec) << ", cur1 offset "
1382 << page_offset(page_cur_get_rec(&cur1)) << ", cur_rec offset "
1383 << page_offset(cur_rec);
1384 }
1385
1386 rec_move[moved].new_rec = ins_rec;
1387 rec_move[moved].old_rec = cur1_rec;
1388 rec_move[moved].moved = false;
1389 moved++;
1390 next:
1391 if (moved > max_move) {
1392 ut_d(ut_error);
1393 ut_o(break);
1394 }
1395
1396 page_cur_move_to_next(&cur1);
1397 }
1398
1399 *num_moved = moved;
1400 }
1401
1402 /** Copy recs till a specified rec from a page to new_block of rtree.
1403 @param[in] new_block Index page to copy to
1404 @param[in] block Index page of rec
1405 @param[in] rec Record on page
1406 @param[in] index Record descriptor
1407 @param[in,out] heap Heap memory
1408 @param[in] rec_move Recording records moved
1409 @param[in] max_move Num of rec to move
1410 @param[out] num_moved Num of rec to move
1411 @param[in] mtr Mini-transaction */
1412 void rtr_page_copy_rec_list_start_no_locks(
1413 buf_block_t *new_block, buf_block_t *block, rec_t *rec, dict_index_t *index,
1414 mem_heap_t *heap, rtr_rec_move_t *rec_move, ulint max_move,
1415 ulint *num_moved, mtr_t *mtr) {
1416 page_cur_t cur1;
1417 rec_t *cur_rec;
1418 ulint offsets_1[REC_OFFS_NORMAL_SIZE];
1419 ulint *offsets1 = offsets_1;
1420 ulint offsets_2[REC_OFFS_NORMAL_SIZE];
1421 ulint *offsets2 = offsets_2;
1422 page_cur_t page_cur;
1423 ulint moved = 0;
1424 bool is_leaf = page_is_leaf(buf_block_get_frame(block));
1425
1426 rec_offs_init(offsets_1);
1427 rec_offs_init(offsets_2);
1428
1429 page_cur_set_before_first(block, &cur1);
1430 page_cur_move_to_next(&cur1);
1431
1432 cur_rec =
1433 page_rec_get_next(page_get_infimum_rec(buf_block_get_frame(new_block)));
1434 page_cur_position(cur_rec, new_block, &page_cur);
1435
1436 while (page_cur_get_rec(&cur1) != rec) {
1437 rec_t *cur1_rec = page_cur_get_rec(&cur1);
1438 rec_t *ins_rec;
1439
1440 if (page_rec_is_infimum(cur_rec)) {
1441 cur_rec = page_rec_get_next(cur_rec);
1442 }
1443
1444 offsets1 = rec_get_offsets(cur1_rec, index, offsets1, ULINT_UNDEFINED,
1445 UT_LOCATION_HERE, &heap);
1446
1447 while (!page_rec_is_supremum(cur_rec)) {
1448 ulint cur_matched_fields = 0;
1449 int cmp;
1450
1451 offsets2 = rec_get_offsets(cur_rec, index, offsets2, ULINT_UNDEFINED,
1452 UT_LOCATION_HERE, &heap);
1453 cmp = cmp_rec_rec_with_match(cur1_rec, cur_rec, offsets1, offsets2, index,
1454 page_is_spatial_non_leaf(cur1_rec, index),
1455 false, &cur_matched_fields);
1456 if (cmp < 0) {
1457 page_cur_move_to_prev(&page_cur);
1458 cur_rec = page_cur_get_rec(&page_cur);
1459 break;
1460 } else if (cmp > 0) {
1461 /* Skip small recs. */
1462 page_cur_move_to_next(&page_cur);
1463 cur_rec = page_cur_get_rec(&page_cur);
1464 } else if (is_leaf) {
1465 if (rec_get_deleted_flag(cur1_rec, dict_table_is_comp(index->table))) {
1466 goto next;
1467 } else {
1468 /* We have two identical leaf records,
1469 skip copying the undeleted one, and
1470 unmark deleted on the current page */
1471 btr_rec_set_deleted_flag(cur_rec, nullptr, false);
1472 goto next;
1473 }
1474 }
1475 }
1476
1477 /* If position is on suprenum rec, need to move to
1478 previous rec. */
1479 if (page_rec_is_supremum(cur_rec)) {
1480 page_cur_move_to_prev(&page_cur);
1481 }
1482
1483 cur_rec = page_cur_get_rec(&page_cur);
1484
1485 offsets1 = rec_get_offsets(cur1_rec, index, offsets1, ULINT_UNDEFINED,
1486 UT_LOCATION_HERE, &heap);
1487
1488 ins_rec = page_cur_insert_rec_low(cur_rec, index, cur1_rec, offsets1, mtr);
1489 if (UNIV_UNLIKELY(!ins_rec)) {
1490 fprintf(stderr, "page number %ld and %ld\n",
1491 (long)new_block->page.id.page_no(),
1492 (long)block->page.id.page_no());
1493
1494 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_521)
1495 << "rec offset " << page_offset(rec) << ", cur1 offset "
1496 << page_offset(page_cur_get_rec(&cur1)) << ", cur_rec offset "
1497 << page_offset(cur_rec);
1498 }
1499
1500 rec_move[moved].new_rec = ins_rec;
1501 rec_move[moved].old_rec = cur1_rec;
1502 rec_move[moved].moved = false;
1503 moved++;
1504 next:
1505 if (moved > max_move) {
1506 ut_d(ut_error);
1507 ut_o(break);
1508 }
1509
1510 page_cur_move_to_next(&cur1);
1511 }
1512
1513 *num_moved = moved;
1514 }
1515
1516 bool rtr_merge_mbr_changed(btr_cur_t *cursor, btr_cur_t *cursor2,
1517 ulint *offsets, ulint *offsets2,
1518 rtr_mbr_t *new_mbr) {
1519 double *mbr;
1520 double mbr1[SPDIMS * 2];
1521 double mbr2[SPDIMS * 2];
1522 rec_t *rec;
1523 ulint len;
1524 bool changed = false;
1525
1526 ut_ad(dict_index_is_spatial(cursor->index));
1527
1528 rec = btr_cur_get_rec(cursor);
1529
1530 rtr_read_mbr(rec_get_nth_field(cursor->index, rec, offsets, 0, &len),
1531 reinterpret_cast<rtr_mbr_t *>(mbr1));
1532
1533 rec = btr_cur_get_rec(cursor2);
1534
1535 rtr_read_mbr(rec_get_nth_field(cursor->index, rec, offsets2, 0, &len),
1536 reinterpret_cast<rtr_mbr_t *>(mbr2));
1537
1538 mbr = reinterpret_cast<double *>(new_mbr);
1539
1540 for (uint i = 0; i < SPDIMS * 2; i += 2) {
1541 changed = (changed || mbr1[i] != mbr2[i]);
1542 *mbr = mbr1[i] < mbr2[i] ? mbr1[i] : mbr2[i];
1543 mbr++;
1544 changed = (changed || mbr1[i + 1] != mbr2[i + 1]);
1545 *mbr = mbr1[i + 1] > mbr2[i + 1] ? mbr1[i + 1] : mbr2[i + 1];
1546 mbr++;
1547 }
1548
1549 return (changed);
1550 }
1551
1552 dberr_t rtr_merge_and_update_mbr(btr_cur_t *cursor, btr_cur_t *cursor2,
1553 ulint *offsets, ulint *offsets2,
1554 page_t *child_page, mtr_t *mtr) {
1555 dberr_t err = DB_SUCCESS;
1556 rtr_mbr_t new_mbr;
1557 bool changed = false;
1558
1559 ut_ad(dict_index_is_spatial(cursor->index));
1560
1561 changed = rtr_merge_mbr_changed(cursor, cursor2, offsets, offsets2, &new_mbr);
1562
1563 /* Update the mbr field of the rec. And will delete the record
1564 pointed by cursor2 */
1565 if (changed) {
1566 if (!rtr_update_mbr_field(cursor, offsets, cursor2, child_page, &new_mbr,
1567 nullptr, mtr)) {
1568 err = DB_ERROR;
1569 }
1570 } else {
1571 rtr_node_ptr_delete(cursor2, mtr);
1572 }
1573
1574 return (err);
1575 }
1576
1577 void rtr_node_ptr_delete(btr_cur_t *sea_cur, mtr_t *mtr) {
1578 dberr_t err;
1579
1580 auto compressed =
1581 btr_cur_pessimistic_delete(&err, true, sea_cur, BTR_CREATE_FLAG, false, 0,
1582 0, 0, mtr, nullptr, nullptr);
1583 ut_a(err == DB_SUCCESS);
1584
1585 if (!compressed) {
1586 btr_cur_compress_if_useful(sea_cur, false, mtr);
1587 }
1588 }
1589
1590 /** Check whether a Rtree page is child of a parent page
1591 @return true if there is child/parent relationship */
1592 bool rtr_check_same_block(
1593 dict_index_t *index, /*!< in: index tree */
1594 btr_cur_t *cursor, /*!< in/out: position at the parent entry
1595 pointing to the child if successful */
1596 buf_block_t *parentb, /*!< in: parent page to check */
1597 buf_block_t *childb, /*!< in: child Page */
1598 mem_heap_t *heap) /*!< in: memory heap */
1599
1600 {
1601 page_no_t page_no = childb->page.id.page_no();
1602 ulint *offsets;
1603 rec_t *rec =
1604 page_rec_get_next(page_get_infimum_rec(buf_block_get_frame(parentb)));
1605
1606 while (!page_rec_is_supremum(rec)) {
1607 offsets = rec_get_offsets(rec, index, nullptr, ULINT_UNDEFINED,
1608 UT_LOCATION_HERE, &heap);
1609
1610 if (btr_node_ptr_get_child_page_no(rec, offsets) == page_no) {
1611 btr_cur_position(index, rec, parentb, cursor);
1612 return (true);
1613 }
1614
1615 rec = page_rec_get_next(rec);
1616 }
1617
1618 return (false);
1619 }
1620
1621 /** Calculate the area increased for a new record
1622 @return area increased */
1623 double rtr_rec_cal_increase(
1624 const dtuple_t *dtuple, /*!< in: data tuple to insert, which
1625 cause area increase */
1626 const rec_t *rec, /*!< in: physical record which differs from
1627 dtuple in some of the common fields, or which
1628 has an equal number or more fields than
1629 dtuple */
1630 const ulint *offsets, /*!< in: array returned by rec_get_offsets() */
1631 double *area, /*!< out: increased area */
1632 const dd::Spatial_reference_system *srs) /*!< in: SRS of R-tree */
1633 {
1634 const dfield_t *dtuple_field;
1635 ulint dtuple_f_len;
1636 ulint rec_f_len;
1637 const byte *rec_b_ptr;
1638 double ret = 0;
1639
1640 ut_ad(!page_rec_is_supremum(rec));
1641 ut_ad(!page_rec_is_infimum(rec));
1642
1643 dtuple_field = dtuple_get_nth_field(dtuple, 0);
1644 dtuple_f_len = dfield_get_len(dtuple_field);
1645
1646 rec_b_ptr = rec_get_nth_field(nullptr, rec, offsets, 0, &rec_f_len);
1647 ret = rtree_area_increase(
1648 srs, rec_b_ptr, static_cast<const byte *>(dfield_get_data(dtuple_field)),
1649 static_cast<int>(dtuple_f_len), area);
1650
1651 return (ret);
1652 }
1653
1654 /** Estimates the number of rows in a given area.
1655 @param[in] index index
1656 @param[in] tuple range tuple containing mbr, may also be empty tuple
1657 @param[in] mode search mode
1658 @return estimated number of rows */
1659 int64_t rtr_estimate_n_rows_in_range(dict_index_t *index, const dtuple_t *tuple,
1660 page_cur_mode_t mode) {
1661 /* Check tuple & mode */
1662 if (tuple->n_fields == 0) {
1663 return (HA_POS_ERROR);
1664 }
1665
1666 switch (mode) {
1667 case PAGE_CUR_DISJOINT:
1668 case PAGE_CUR_CONTAIN:
1669 case PAGE_CUR_INTERSECT:
1670 case PAGE_CUR_WITHIN:
1671 case PAGE_CUR_MBR_EQUAL:
1672 break;
1673 default:
1674 return (HA_POS_ERROR);
1675 }
1676
1677 DBUG_EXECUTE_IF("rtr_pcur_move_to_next_return", return (2););
1678
1679 /* Read mbr from tuple. */
1680 const dfield_t *dtuple_field;
1681 ulint dtuple_f_len [[maybe_unused]];
1682 rtr_mbr_t range_mbr;
1683 double range_area;
1684 byte *range_mbr_ptr;
1685
1686 dtuple_field = dtuple_get_nth_field(tuple, 0);
1687 dtuple_f_len = dfield_get_len(dtuple_field);
1688 range_mbr_ptr = reinterpret_cast<byte *>(dfield_get_data(dtuple_field));
1689
1690 ut_ad(dtuple_f_len >= DATA_MBR_LEN);
1691 rtr_read_mbr(range_mbr_ptr, &range_mbr);
1692
1693 double range_mbr_coords[4]{range_mbr.xmin, range_mbr.xmax, range_mbr.ymin,
1694 range_mbr.ymax};
1695 range_area = compute_area(index->rtr_srs.get(), range_mbr_coords, SPDIMS);
1696
1697 /* Get index root page. */
1698 page_size_t page_size(dict_table_page_size(index->table));
1699 page_id_t page_id(dict_index_get_space(index), dict_index_get_page(index));
1700 mtr_t mtr;
1701 buf_block_t *block;
1702 page_t *page;
1703 ulint n_recs;
1704
1705 mtr_start(&mtr);
1706
1707 mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
1708
1709 block = btr_block_get(page_id, page_size, RW_S_LATCH, UT_LOCATION_HERE, index,
1710 &mtr);
1711 page = buf_block_get_frame(block);
1712 n_recs = page_header_get_field(page, PAGE_N_RECS);
1713
1714 if (n_recs == 0) {
1715 mtr_commit(&mtr);
1716 return (HA_POS_ERROR);
1717 }
1718
1719 rec_t *rec;
1720 const byte *field;
1721 ulint len;
1722 ulint *offsets = nullptr;
1723 mem_heap_t *heap;
1724
1725 heap = mem_heap_create(512, UT_LOCATION_HERE);
1726 rec = page_rec_get_next(page_get_infimum_rec(page));
1727 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
1728 UT_LOCATION_HERE, &heap);
1729
1730 /* Scan records in root page and calculate area. */
1731 double area = 0;
1732 while (!page_rec_is_supremum(rec)) {
1733 rtr_mbr_t mbr;
1734 double rec_area;
1735
1736 field = rec_get_nth_field(index, rec, offsets, 0, &len);
1737 ut_ad(len == DATA_MBR_LEN);
1738
1739 rtr_read_mbr(field, &mbr);
1740
1741 double mbr_coords[4]{mbr.xmin, mbr.xmax, mbr.ymin, mbr.ymax};
1742 rec_area = compute_area(index->rtr_srs.get(), mbr_coords, SPDIMS);
1743
1744 if (rec_area == 0) {
1745 switch (mode) {
1746 case PAGE_CUR_CONTAIN:
1747 case PAGE_CUR_INTERSECT:
1748 area += 1;
1749 break;
1750
1751 case PAGE_CUR_DISJOINT:
1752 break;
1753
1754 case PAGE_CUR_WITHIN:
1755 case PAGE_CUR_MBR_EQUAL:
1756 if (rtree_key_cmp(PAGE_CUR_WITHIN, range_mbr_ptr, DATA_MBR_LEN, field,
1757 DATA_MBR_LEN, index->rtr_srs.get())) {
1758 area += 1;
1759 }
1760
1761 break;
1762
1763 default:
1764 ut_error;
1765 }
1766 } else {
1767 switch (mode) {
1768 case PAGE_CUR_CONTAIN:
1769 case PAGE_CUR_INTERSECT:
1770 area += rtree_area_overlapping(index->rtr_srs.get(), range_mbr_ptr,
1771 field, DATA_MBR_LEN) /
1772 rec_area;
1773 break;
1774
1775 case PAGE_CUR_DISJOINT:
1776 area += 1;
1777 area -= rtree_area_overlapping(index->rtr_srs.get(), range_mbr_ptr,
1778 field, DATA_MBR_LEN) /
1779 rec_area;
1780 break;
1781
1782 case PAGE_CUR_WITHIN:
1783 case PAGE_CUR_MBR_EQUAL:
1784 if (rtree_key_cmp(PAGE_CUR_WITHIN, range_mbr_ptr, DATA_MBR_LEN, field,
1785 DATA_MBR_LEN, index->rtr_srs.get())) {
1786 area += range_area / rec_area;
1787 }
1788
1789 break;
1790 default:
1791 ut_error;
1792 }
1793 }
1794
1795 rec = page_rec_get_next(rec);
1796 }
1797
1798 mtr_commit(&mtr);
1799 mem_heap_free(heap);
1800
1801 if (!std::isfinite(area)) {
1802 return (HA_POS_ERROR);
1803 }
1804
1805 return (static_cast<int64_t>(dict_table_get_n_rows(index->table) * area /
1806 n_recs));
1807 }