summaryrefslogtreecommitdiff
path: root/storage/innobase/gis/gis0rtree.cc
blob: f17c532414b5570472d60e8d9e3795cfc927d0ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
/*****************************************************************************

Copyright (c) 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2019, 2022, MariaDB Corporation.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA

*****************************************************************************/

/**************************************************//**
@file gis/gis0rtree.cc
InnoDB R-tree interfaces

Created 2013/03/27 Allen Lai and Jimmy Yang
***********************************************************************/

#include "fsp0fsp.h"
#include "page0page.h"
#include "page0cur.h"
#include "page0zip.h"
#include "gis0rtree.h"
#include "btr0cur.h"
#include "btr0sea.h"
#include "btr0pcur.h"
#include "rem0cmp.h"
#include "lock0lock.h"
#include "ibuf0ibuf.h"
#include "trx0undo.h"
#include "srv0mon.h"
#include "gis0geo.h"
#include <cmath>

/*************************************************************//**
Initial split nodes info for R-tree split.
@return initialized split nodes array */
static
rtr_split_node_t*
rtr_page_split_initialize_nodes(
/*============================*/
	mem_heap_t*	heap,	/*!< in: pointer to memory heap, or NULL */
	btr_cur_t*	cursor,	/*!< in: cursor at which to insert; when the
				function returns, the cursor is positioned
				on the predecessor of the inserted record */
	rec_offs**	offsets,/*!< in: offsets on inserted record */
	const dtuple_t*	tuple,	/*!< in: tuple to insert */
	double**	buf_pos)/*!< in/out: current buffer position */
{
	rtr_split_node_t*	split_node_array;
	double*			buf;
	ulint			n_recs;
	rtr_split_node_t*	task;
	rtr_split_node_t*	stop;
	rtr_split_node_t*	cur;
	rec_t*			rec;
	buf_block_t*		block;
	page_t*			page;
	ulint			n_uniq;
	ulint			len;
	const byte*		source_cur;

	block = btr_cur_get_block(cursor);
	page = buf_block_get_frame(block);
	n_uniq = dict_index_get_n_unique_in_tree(cursor->index);

	n_recs = ulint(page_get_n_recs(page)) + 1;

	/*We reserve 2 MBRs memory space for temp result of split
	algrithm. And plus the new mbr that need to insert, we
	need (n_recs + 3)*MBR size for storing all MBRs.*/
	buf = static_cast<double*>(mem_heap_alloc(
		heap, DATA_MBR_LEN * (n_recs + 3)
		+ sizeof(rtr_split_node_t) * (n_recs + 1)));

	split_node_array = (rtr_split_node_t*)(buf + SPDIMS * 2 * (n_recs + 3));
	task = split_node_array;
	*buf_pos = buf;
	stop = task + n_recs;

	rec = page_rec_get_next(page_get_infimum_rec(page));
	const ulint n_core = page_is_leaf(page)
		? cursor->index->n_core_fields : 0;
	*offsets = rec_get_offsets(rec, cursor->index, *offsets, n_core,
				   n_uniq, &heap);

	source_cur = rec_get_nth_field(rec, *offsets, 0, &len);

	for (cur = task; cur < stop - 1; ++cur) {
		cur->coords = reserve_coords(buf_pos, SPDIMS);
		cur->key = rec;

		memcpy(cur->coords, source_cur, DATA_MBR_LEN);

		rec = page_rec_get_next(rec);
		*offsets = rec_get_offsets(rec, cursor->index, *offsets,
					   n_core, n_uniq, &heap);
		source_cur = rec_get_nth_field(rec, *offsets, 0, &len);
	}

	/* Put the insert key to node list */
	source_cur = static_cast<const byte*>(dfield_get_data(
		dtuple_get_nth_field(tuple, 0)));
	cur->coords = reserve_coords(buf_pos, SPDIMS);
	rec = (byte*) mem_heap_alloc(
		heap, rec_get_converted_size(cursor->index, tuple, 0));

	rec = rec_convert_dtuple_to_rec(rec, cursor->index, tuple, 0);
	cur->key = rec;

	memcpy(cur->coords, source_cur, DATA_MBR_LEN);

	return split_node_array;
}

/**********************************************************************//**
Builds a Rtree node pointer out of a physical record and a page number.
Note: For Rtree, we just keep the mbr and page no field in non-leaf level
page. It's different with Btree, Btree still keeps PK fields so far.
@return	own: node pointer */
dtuple_t*
rtr_index_build_node_ptr(
/*=====================*/
	const dict_index_t*	index,	/*!< in: index */
	const rtr_mbr_t*	mbr,	/*!< in: mbr of lower page */
	const rec_t*		rec,	/*!< in: record for which to build node
					pointer */
	ulint			page_no,/*!< in: page number to put in node
					pointer */
	mem_heap_t*		heap)	/*!< in: memory heap where pointer
					created */
{
	dtuple_t*	tuple;
	dfield_t*	field;
	byte*		buf;
	ulint		n_unique;
	ulint		info_bits;

	ut_ad(dict_index_is_spatial(index));

	n_unique = DICT_INDEX_SPATIAL_NODEPTR_SIZE;

	tuple = dtuple_create(heap, n_unique + 1);

	/* For rtree internal node, we need to compare page number
	fields. */
	dtuple_set_n_fields_cmp(tuple, n_unique + 1);

	dict_index_copy_types(tuple, index, n_unique);

	/* Write page no field */
	buf = static_cast<byte*>(mem_heap_alloc(heap, 4));

	mach_write_to_4(buf, page_no);

	field = dtuple_get_nth_field(tuple, n_unique);
	dfield_set_data(field, buf, 4);

	dtype_set(dfield_get_type(field), DATA_SYS_CHILD, DATA_NOT_NULL, 4);

	/* Set info bits. */
	info_bits = rec_get_info_bits(rec, dict_table_is_comp(index->table));
	dtuple_set_info_bits(tuple, info_bits | REC_STATUS_NODE_PTR);

	/* Set mbr as index entry data */
	field = dtuple_get_nth_field(tuple, 0);

	buf = static_cast<byte*>(mem_heap_alloc(heap, DATA_MBR_LEN));

	rtr_write_mbr(buf, mbr);

	dfield_set_data(field, buf, DATA_MBR_LEN);

	ut_ad(dtuple_check_typed(tuple));

	return(tuple);
}

/**************************************************************//**
Update the mbr field of a spatial index row. */
void
rtr_update_mbr_field(
/*=================*/
	btr_cur_t*	cursor,		/*!< in/out: cursor pointed to rec.*/
	rec_offs*	offsets,	/*!< in/out: offsets on rec. */
	btr_cur_t*	cursor2,	/*!< in/out: cursor pointed to rec
					that should be deleted.
					this cursor is for btr_compress to
					delete the merged page's father rec.*/
	page_t*		child_page,	/*!< in: child page. */
	rtr_mbr_t*	mbr,		/*!< in: the new mbr. */
	rec_t*		new_rec,	/*!< in: rec to use */
	mtr_t*		mtr)		/*!< in: mtr */
{
	dict_index_t*	index = cursor->index;
	mem_heap_t*	heap;
	page_t*		page;
	rec_t*		rec;
	constexpr ulint flags = BTR_NO_UNDO_LOG_FLAG
			| BTR_NO_LOCKING_FLAG
			| BTR_KEEP_SYS_FLAG;
	dberr_t		err;
	big_rec_t*	dummy_big_rec;
	buf_block_t*	block;
	rec_t*		child_rec;
	ulint		up_match = 0;
	ulint		low_match = 0;
	ulint		child;
	ulint		rec_info;
	bool		ins_suc = true;
	ulint		cur2_pos = 0;
	ulint		del_page_no = 0;
	rec_offs*	offsets2;

	rec = btr_cur_get_rec(cursor);
	page = page_align(rec);

	rec_info = rec_get_info_bits(rec, rec_offs_comp(offsets));

	heap = mem_heap_create(100);
	block = btr_cur_get_block(cursor);
	ut_ad(page == buf_block_get_frame(block));

	child = btr_node_ptr_get_child_page_no(rec, offsets);
	const ulint n_core = page_is_leaf(block->page.frame)
		? index->n_core_fields : 0;

	if (new_rec) {
		child_rec = new_rec;
	} else {
		child_rec = page_rec_get_next(page_get_infimum_rec(child_page));
	}

	dtuple_t* node_ptr = rtr_index_build_node_ptr(
		index, mbr, child_rec, child, heap);

	/* We need to remember the child page no of cursor2, since page could be
	reorganized or insert a new rec before it. */
	if (cursor2) {
		rec_t*	del_rec = btr_cur_get_rec(cursor2);
		offsets2 = rec_get_offsets(btr_cur_get_rec(cursor2),
					   index, NULL, 0,
					   ULINT_UNDEFINED, &heap);
		del_page_no = btr_node_ptr_get_child_page_no(del_rec, offsets2);
		cur2_pos = page_rec_get_n_recs_before(btr_cur_get_rec(cursor2));
	}

	ut_ad(rec_offs_validate(rec, index, offsets));
	ut_ad(rec_offs_base(offsets)[0 + 1] == DATA_MBR_LEN);
	ut_ad(node_ptr->fields[0].len == DATA_MBR_LEN);

	if (rec_info & REC_INFO_MIN_REC_FLAG) {
		/* When the rec is minimal rec in this level, we do
		in-place update for avoiding it move to other place. */
		page_zip_des_t* page_zip = buf_block_get_page_zip(block);

		if (UNIV_LIKELY_NULL(page_zip)) {
			/* Check if there's enough space for in-place
			update the zip page. */
			if (!btr_cur_update_alloc_zip(
					page_zip,
					btr_cur_get_page_cur(cursor),
					index, offsets,
					rec_offs_size(offsets),
					false, mtr)) {

				/* If there's not enought space for
				inplace update zip page, we do delete
				insert. */
				ins_suc = false;

				/* Since btr_cur_update_alloc_zip could
				reorganize the page, we need to repositon
				cursor2. */
				if (cursor2) {
					cursor2->page_cur.rec =
						page_rec_get_nth(page,
								 cur2_pos);
				}

				goto update_mbr;
			}

			/* Record could be repositioned */
			rec = btr_cur_get_rec(cursor);

#ifdef UNIV_DEBUG
			/* Make sure it is still the first record */
			rec_info = rec_get_info_bits(
					rec, rec_offs_comp(offsets));
			ut_ad(rec_info & REC_INFO_MIN_REC_FLAG);
#endif /* UNIV_DEBUG */
			memcpy(rec, node_ptr->fields[0].data, DATA_MBR_LEN);
			page_zip_write_rec(block, rec, index, offsets, 0, mtr);
		} else {
			mtr->memcpy<mtr_t::MAYBE_NOP>(*block, rec,
						      node_ptr->fields[0].data,
						      DATA_MBR_LEN);
		}

		if (cursor2) {
			rec_offs* offsets2;

			if (UNIV_LIKELY_NULL(page_zip)) {
				cursor2->page_cur.rec
					= page_rec_get_nth(page, cur2_pos);
			}
			offsets2 = rec_get_offsets(btr_cur_get_rec(cursor2),
						   index, NULL, 0,
						   ULINT_UNDEFINED, &heap);
			ut_ad(del_page_no == btr_node_ptr_get_child_page_no(
							cursor2->page_cur.rec,
							offsets2));

			page_cur_delete_rec(btr_cur_get_page_cur(cursor2),
					    index, offsets2, mtr);
		}
	} else if (page_get_n_recs(page) == 1) {
		/* When there's only one rec in the page, we do insert/delete to
		avoid page merge. */

		page_cur_t		page_cur;
		rec_t*			insert_rec;
		rec_offs*		insert_offsets = NULL;
		ulint			old_pos;
		rec_t*			old_rec;

		ut_ad(cursor2 == NULL);

		/* Insert the new mbr rec. */
		old_pos = page_rec_get_n_recs_before(rec);

		err = btr_cur_optimistic_insert(
			flags,
			cursor, &insert_offsets, &heap,
			node_ptr, &insert_rec, &dummy_big_rec, 0, NULL, mtr);

		ut_ad(err == DB_SUCCESS);

		btr_cur_position(index, insert_rec, block, cursor);

		/* Delete the old mbr rec. */
		old_rec = page_rec_get_nth(page, old_pos);
		ut_ad(old_rec != insert_rec);

		page_cur_position(old_rec, block, &page_cur);
		offsets2 = rec_get_offsets(old_rec, index, NULL, n_core,
					   ULINT_UNDEFINED, &heap);
		page_cur_delete_rec(&page_cur, index, offsets2, mtr);

	} else {
update_mbr:
		/* When there're not only 1 rec in the page, we do delete/insert
		to avoid page split. */
		rec_t*			insert_rec;
		rec_offs*		insert_offsets = NULL;
		rec_t*			next_rec;

		/* Delete the rec which cursor point to. */
		next_rec = page_rec_get_next(rec);
		page_cur_delete_rec(btr_cur_get_page_cur(cursor),
				    index, offsets, mtr);
		if (!ins_suc) {
			ut_ad(rec_info & REC_INFO_MIN_REC_FLAG);

			btr_set_min_rec_mark(next_rec, *block, mtr);
		}

		/* If there's more than 1 rec left in the page, delete
		the rec which cursor2 point to. Otherwise, delete it later.*/
		if (cursor2 && page_get_n_recs(page) > 1) {
			ulint		cur2_rec_info;
			rec_t*		cur2_rec;

			cur2_rec = cursor2->page_cur.rec;
			offsets2 = rec_get_offsets(cur2_rec, index, NULL,
						   n_core,
						   ULINT_UNDEFINED, &heap);

			cur2_rec_info = rec_get_info_bits(cur2_rec,
						rec_offs_comp(offsets2));
			if (cur2_rec_info & REC_INFO_MIN_REC_FLAG) {
				/* If we delete the leftmost node
				pointer on a non-leaf level, we must
				mark the new leftmost node pointer as
				the predefined minimum record */
				rec_t*	next_rec = page_rec_get_next(cur2_rec);
				btr_set_min_rec_mark(next_rec, *block, mtr);
			}

			ut_ad(del_page_no
			      == btr_node_ptr_get_child_page_no(cur2_rec,
								offsets2));
			page_cur_delete_rec(btr_cur_get_page_cur(cursor2),
					    index, offsets2, mtr);
			cursor2 = NULL;
		}

		/* Insert the new rec. */
		page_cur_search_with_match(block, index, node_ptr,
					   PAGE_CUR_LE , &up_match, &low_match,
					   btr_cur_get_page_cur(cursor), NULL);

		err = btr_cur_optimistic_insert(flags, cursor, &insert_offsets,
						&heap, node_ptr, &insert_rec,
						&dummy_big_rec, 0, NULL, mtr);

		if (!ins_suc && err == DB_SUCCESS) {
			ins_suc = true;
		}

		/* If optimistic insert fail, try reorganize the page
		and insert again. */
		if (err != DB_SUCCESS && ins_suc) {
			btr_page_reorganize(btr_cur_get_page_cur(cursor),
					    index, mtr);

			err = btr_cur_optimistic_insert(flags,
							cursor,
							&insert_offsets,
							&heap,
							node_ptr,
							&insert_rec,
							&dummy_big_rec,
							0, NULL, mtr);

			/* Will do pessimistic insert */
			if (err != DB_SUCCESS) {
				ins_suc = false;
			}
		}

		/* Insert succeed, position cursor the inserted rec.*/
		if (ins_suc) {
			btr_cur_position(index, insert_rec, block, cursor);
			offsets = rec_get_offsets(insert_rec,
						  index, offsets, n_core,
						  ULINT_UNDEFINED, &heap);
		}

		/* Delete the rec which cursor2 point to. */
		if (cursor2) {
			ulint		cur2_pno;
			rec_t*		cur2_rec;

			cursor2->page_cur.rec = page_rec_get_nth(page,
								 cur2_pos);

			cur2_rec = btr_cur_get_rec(cursor2);

			offsets2 = rec_get_offsets(cur2_rec, index, NULL,
						   n_core,
						   ULINT_UNDEFINED, &heap);

			/* If the cursor2 position is on a wrong rec, we
			need to reposition it. */
			cur2_pno = btr_node_ptr_get_child_page_no(cur2_rec, offsets2);
			if ((del_page_no != cur2_pno)
			    || (cur2_rec == insert_rec)) {
				cur2_rec = page_rec_get_next(
					page_get_infimum_rec(page));

				while (!page_rec_is_supremum(cur2_rec)) {
					offsets2 = rec_get_offsets(cur2_rec, index,
								   NULL,
								   n_core,
								   ULINT_UNDEFINED,
								   &heap);
					cur2_pno = btr_node_ptr_get_child_page_no(
							cur2_rec, offsets2);
					if (cur2_pno == del_page_no) {
						if (insert_rec != cur2_rec) {
							cursor2->page_cur.rec =
								cur2_rec;
							break;
						}
					}
					cur2_rec = page_rec_get_next(cur2_rec);
				}

				ut_ad(!page_rec_is_supremum(cur2_rec));
			}

			rec_info = rec_get_info_bits(cur2_rec,
						     rec_offs_comp(offsets2));
			if (rec_info & REC_INFO_MIN_REC_FLAG) {
				/* If we delete the leftmost node
				pointer on a non-leaf level, we must
				mark the new leftmost node pointer as
				the predefined minimum record */
				rec_t*	next_rec = page_rec_get_next(cur2_rec);
				btr_set_min_rec_mark(next_rec, *block, mtr);
			}

			ut_ad(cur2_pno == del_page_no && cur2_rec != insert_rec);

			page_cur_delete_rec(btr_cur_get_page_cur(cursor2),
					    index, offsets2, mtr);
		}

		if (!ins_suc) {
			mem_heap_t*	new_heap = NULL;

			err = btr_cur_pessimistic_insert(
				flags,
				cursor, &insert_offsets, &new_heap,
				node_ptr, &insert_rec, &dummy_big_rec,
				0, NULL, mtr);

			ut_ad(err == DB_SUCCESS);

			if (new_heap) {
				mem_heap_free(new_heap);
			}

		}

		if (cursor2) {
			btr_cur_compress_if_useful(cursor, FALSE, mtr);
		}
	}

	ut_ad(page_has_prev(page)
	      || (REC_INFO_MIN_REC_FLAG & rec_get_info_bits(
			  page_rec_get_next(page_get_infimum_rec(page)),
			  page_is_comp(page))));

	mem_heap_free(heap);
}

/**************************************************************//**
Update parent page's MBR and Predicate lock information during a split */
static MY_ATTRIBUTE((nonnull))
void
rtr_adjust_upper_level(
/*===================*/
	btr_cur_t*	sea_cur,	/*!< in: search cursor */
	ulint		flags,		/*!< in: undo logging and
					locking flags */
	buf_block_t*	block,		/*!< in/out: page to be split */
	buf_block_t*	new_block,	/*!< in/out: the new half page */
	rtr_mbr_t*	mbr,		/*!< in: MBR on the old page */
	rtr_mbr_t*	new_mbr,	/*!< in: MBR on the new page */
	mtr_t*		mtr)		/*!< in: mtr */
{
	ulint		page_no;
	ulint		new_page_no;
	dict_index_t*	index = sea_cur->index;
	btr_cur_t	cursor;
	rec_offs*	offsets;
	mem_heap_t*	heap;
	ulint		level;
	dtuple_t*	node_ptr_upper;
	page_cur_t*	page_cursor;
	lock_prdt_t	prdt;
	lock_prdt_t	new_prdt;
	dberr_t		err;
	big_rec_t*	dummy_big_rec;
	rec_t*		rec;

	/* Create a memory heap where the data tuple is stored */
	heap = mem_heap_create(1024);
	cursor.init();

	cursor.thr = sea_cur->thr;

	/* Get the level of the split pages */
	level = btr_page_get_level(buf_block_get_frame(block));
	ut_ad(level == btr_page_get_level(buf_block_get_frame(new_block)));

	page_no = block->page.id().page_no();

	new_page_no = new_block->page.id().page_no();

	/* Set new mbr for the old page on the upper level. */
	/* Look up the index for the node pointer to page */
	offsets = rtr_page_get_father_block(
		NULL, heap, index, block, mtr, sea_cur, &cursor);

	page_cursor = btr_cur_get_page_cur(&cursor);

	rtr_update_mbr_field(&cursor, offsets, nullptr, block->page.frame, mbr,
			     nullptr, mtr);

	/* Already updated parent MBR, reset in our path */
	if (sea_cur->rtr_info) {
		node_visit_t*	node_visit = rtr_get_parent_node(
						sea_cur, level + 1, true);
		if (node_visit) {
			node_visit->mbr_inc = 0;
		}
	}

	/* Insert the node for the new page. */
	node_ptr_upper = rtr_index_build_node_ptr(
		index, new_mbr,
		page_rec_get_next(page_get_infimum_rec(new_block->page.frame)),
		new_page_no, heap);

	ulint	up_match = 0;
	ulint	low_match = 0;

	buf_block_t*	father_block = btr_cur_get_block(&cursor);

	page_cur_search_with_match(
		father_block, index, node_ptr_upper,
		PAGE_CUR_LE , &up_match, &low_match,
		btr_cur_get_page_cur(&cursor), NULL);

	err = btr_cur_optimistic_insert(
		flags
		| BTR_NO_LOCKING_FLAG
		| BTR_KEEP_SYS_FLAG
		| BTR_NO_UNDO_LOG_FLAG,
		&cursor, &offsets, &heap,
		node_ptr_upper, &rec, &dummy_big_rec, 0, NULL, mtr);

	if (err == DB_FAIL) {
		cursor.rtr_info = sea_cur->rtr_info;
		cursor.tree_height = sea_cur->tree_height;

		/* Recreate a memory heap as input parameter for
		btr_cur_pessimistic_insert(), because the heap may be
		emptied in btr_cur_pessimistic_insert(). */
		mem_heap_t* new_heap = mem_heap_create(1024);

		err = btr_cur_pessimistic_insert(flags
						 | BTR_NO_LOCKING_FLAG
						 | BTR_KEEP_SYS_FLAG
						 | BTR_NO_UNDO_LOG_FLAG,
						 &cursor, &offsets, &new_heap,
						 node_ptr_upper, &rec,
						 &dummy_big_rec, 0, NULL, mtr);
		cursor.rtr_info = NULL;
		ut_a(err == DB_SUCCESS);

		mem_heap_free(new_heap);
	}

	prdt.data = static_cast<void*>(mbr);
	prdt.op = 0;
	new_prdt.data = static_cast<void*>(new_mbr);
	new_prdt.op = 0;

	lock_prdt_update_parent(block, new_block, &prdt, &new_prdt,
				page_cursor->block->page.id());

	mem_heap_free(heap);

	ut_ad(block->zip_size() == index->table->space->zip_size());

	const uint32_t next_page_no = btr_page_get_next(block->page.frame);

	if (next_page_no != FIL_NULL) {
		buf_block_t*	next_block = btr_block_get(
			*index, next_page_no, RW_X_LATCH, false, mtr);
#ifdef UNIV_BTR_DEBUG
		ut_a(page_is_comp(next_block->page.frame)
		     == page_is_comp(block->page.frame));
		ut_a(btr_page_get_prev(next_block->page.frame)
		     == block->page.id().page_no());
#endif /* UNIV_BTR_DEBUG */

		btr_page_set_prev(next_block, new_page_no, mtr);
	}

	btr_page_set_next(block, new_page_no, mtr);

	btr_page_set_prev(new_block, page_no, mtr);
	btr_page_set_next(new_block, next_page_no, mtr);
}

/*************************************************************//**
Moves record list to another page for rtree splitting.

IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
if new_block is a compressed leaf page in a secondary index.
This has to be done either within the same mini-transaction,
or by invoking ibuf_reset_free_bits() before mtr_commit().

@return TRUE on success; FALSE on compression failure */
static
ibool
rtr_split_page_move_rec_list(
/*=========================*/
	rtr_split_node_t*	node_array,	/*!< in: split node array. */
	int			first_rec_group,/*!< in: group number of the
						first rec. */
	buf_block_t*		new_block,	/*!< in/out: index page
						where to move */
	buf_block_t*		block,		/*!< in/out: page containing
						split_rec */
	rec_t*			first_rec,	/*!< in: first record not to
						move */
	dict_index_t*		index,		/*!< in: record descriptor */
	mem_heap_t*		heap,		/*!< in: pointer to memory
						heap, or NULL */
	mtr_t*			mtr)		/*!< in: mtr */
{
	rtr_split_node_t*	cur_split_node;
	rtr_split_node_t*	end_split_node;
	page_cur_t		page_cursor;
	page_cur_t		new_page_cursor;
	page_t*			page;
	page_t*			new_page;
	rec_offs		offsets_[REC_OFFS_NORMAL_SIZE];
	rec_offs*		offsets		= offsets_;
	page_zip_des_t*		new_page_zip
		= buf_block_get_page_zip(new_block);
	rec_t*			rec;
	rec_t*			ret;
	ulint			moved		= 0;
	ulint			max_to_move	= 0;
	rtr_rec_move_t*		rec_move	= NULL;

	ut_ad(!dict_index_is_ibuf(index));
	ut_ad(dict_index_is_spatial(index));

	rec_offs_init(offsets_);

	page_cur_set_before_first(block, &page_cursor);
	page_cur_set_before_first(new_block, &new_page_cursor);

	page = buf_block_get_frame(block);
	new_page = buf_block_get_frame(new_block);
	ret = page_rec_get_prev(page_get_supremum_rec(new_page));

	end_split_node = node_array + page_get_n_recs(page);

	mtr_log_t	log_mode = MTR_LOG_NONE;

	if (new_page_zip) {
		log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
	}

	max_to_move = page_get_n_recs(buf_block_get_frame(block));
	rec_move = static_cast<rtr_rec_move_t*>(mem_heap_alloc(
			heap,
			sizeof (*rec_move) * max_to_move));
	const ulint n_core = page_is_leaf(page)
		? index->n_core_fields : 0;

	/* Insert the recs in group 2 to new page.  */
	for (cur_split_node = node_array;
	     cur_split_node < end_split_node; ++cur_split_node) {
		if (cur_split_node->n_node != first_rec_group) {
			lock_rec_store_on_page_infimum(
				block, cur_split_node->key);

			offsets = rec_get_offsets(cur_split_node->key,
						  index, offsets, n_core,
						  ULINT_UNDEFINED, &heap);

			ut_ad(!n_core || cur_split_node->key != first_rec);

			rec = page_cur_insert_rec_low(
				&new_page_cursor,
				index, cur_split_node->key, offsets, mtr);

			ut_a(rec);

			lock_rec_restore_from_page_infimum(
				*new_block, rec, block->page.id());

			page_cur_move_to_next(&new_page_cursor);

			rec_move[moved].new_rec = rec;
			rec_move[moved].old_rec = cur_split_node->key;
			rec_move[moved].moved = false;
			moved++;

			if (moved > max_to_move) {
				ut_ad(0);
				break;
			}
		}
	}

	/* Update PAGE_MAX_TRX_ID on the uncompressed page.
	Modifications will be redo logged and copied to the compressed
	page in page_zip_compress() or page_zip_reorganize() below.
	Multiple transactions cannot simultaneously operate on the
	same temp-table in parallel.
	max_trx_id is ignored for temp tables because it not required
	for MVCC. */
	if (n_core && !index->table->is_temporary()) {
		page_update_max_trx_id(new_block, NULL,
				       page_get_max_trx_id(page),
				       mtr);
	}

	if (new_page_zip) {
		mtr_set_log_mode(mtr, log_mode);

		if (!page_zip_compress(new_block, index,
				       page_zip_level, mtr)) {
			ulint	ret_pos;

			/* Before trying to reorganize the page,
			store the number of preceding records on the page. */
			ret_pos = page_rec_get_n_recs_before(ret);
			/* Before copying, "ret" was the predecessor
			of the predefined supremum record.  If it was
			the predefined infimum record, then it would
			still be the infimum, and we would have
			ret_pos == 0. */

			if (UNIV_UNLIKELY
			    (!page_zip_reorganize(new_block, index,
						  page_zip_level, mtr))) {

				if (UNIV_UNLIKELY
				    (!page_zip_decompress(new_page_zip,
							  new_page, FALSE))) {
					ut_error;
				}
#ifdef UNIV_GIS_DEBUG
				ut_ad(page_validate(new_page, index));
#endif

				return(false);
			}

			/* The page was reorganized: Seek to ret_pos. */
			ret = page_rec_get_nth(new_page, ret_pos);
		}
	}

	/* Update the lock table */
	lock_rtr_move_rec_list(new_block, block, rec_move, moved);

	/* Delete recs in second group from the old page. */
	for (cur_split_node = node_array;
	     cur_split_node < end_split_node; ++cur_split_node) {
		if (cur_split_node->n_node != first_rec_group) {
			page_cur_position(cur_split_node->key,
					  block, &page_cursor);
			offsets = rec_get_offsets(
				page_cur_get_rec(&page_cursor), index,
				offsets, n_core, ULINT_UNDEFINED,
				&heap);
			page_cur_delete_rec(&page_cursor,
				index, offsets, mtr);
		}
	}

	return(true);
}

/*************************************************************//**
Splits an R-tree index page to halves and inserts the tuple. It is assumed
that mtr holds an x-latch to the index tree. NOTE: the tree x-latch is
released within this function! NOTE that the operation of this
function must always succeed, we cannot reverse it: therefore enough
free disk space (2 pages) must be guaranteed to be available before
this function is called.
@return inserted record */
rec_t*
rtr_page_split_and_insert(
/*======================*/
	ulint		flags,	/*!< in: undo logging and locking flags */
	btr_cur_t*	cursor,	/*!< in/out: cursor at which to insert; when the
				function returns, the cursor is positioned
				on the predecessor of the inserted record */
	rec_offs**	offsets,/*!< out: offsets on inserted record */
	mem_heap_t**	heap,	/*!< in/out: pointer to memory heap, or NULL */
	const dtuple_t*	tuple,	/*!< in: tuple to insert */
	ulint		n_ext,	/*!< in: number of externally stored columns */
	mtr_t*		mtr)	/*!< in: mtr */
{
	buf_block_t*		block;
	page_t*			page;
	page_t*			new_page;
	buf_block_t*		new_block;
	page_zip_des_t*		page_zip;
	page_zip_des_t*		new_page_zip;
	buf_block_t*		insert_block;
	page_cur_t*		page_cursor;
	rec_t*			rec = 0;
	ulint			n_recs;
	ulint			total_data;
	ulint			insert_size;
	rtr_split_node_t*	rtr_split_node_array;
	rtr_split_node_t*	cur_split_node;
	rtr_split_node_t*	end_split_node;
	double*			buf_pos;
	node_seq_t		current_ssn;
	node_seq_t		next_ssn;
	buf_block_t*		root_block;
	rtr_mbr_t		mbr;
	rtr_mbr_t		new_mbr;
	lock_prdt_t		prdt;
	lock_prdt_t		new_prdt;
	rec_t*			first_rec = NULL;
	int			first_rec_group = 1;
	ulint			n_iterations = 0;

	if (!*heap) {
		*heap = mem_heap_create(1024);
	}

func_start:
	mem_heap_empty(*heap);
	*offsets = NULL;

	ut_ad(mtr->memo_contains_flagged(&cursor->index->lock, MTR_MEMO_X_LOCK
					 | MTR_MEMO_SX_LOCK));
	ut_ad(!dict_index_is_online_ddl(cursor->index)
	      || (flags & BTR_CREATE_FLAG)
	      || dict_index_is_clust(cursor->index));
	ut_ad(cursor->index->lock.have_u_or_x());

	block = btr_cur_get_block(cursor);
	page = buf_block_get_frame(block);
	page_zip = buf_block_get_page_zip(block);
	current_ssn = page_get_ssn_id(page);

	ut_ad(mtr->memo_contains_flagged(block, MTR_MEMO_PAGE_X_FIX));
	ut_ad(page_get_n_recs(page) >= 1);

	const page_id_t page_id(block->page.id());

	if (!page_has_prev(page) && !page_is_leaf(page)) {
		first_rec = page_rec_get_next(
			page_get_infimum_rec(buf_block_get_frame(block)));
	}

	/* Initial split nodes array. */
	rtr_split_node_array = rtr_page_split_initialize_nodes(
		*heap, cursor, offsets, tuple, &buf_pos);

	/* Divide all mbrs to two groups. */
	n_recs = ulint(page_get_n_recs(page)) + 1;

	end_split_node = rtr_split_node_array + n_recs;

#ifdef UNIV_GIS_DEBUG
	fprintf(stderr, "Before split a page:\n");
	for (cur_split_node = rtr_split_node_array;
		cur_split_node < end_split_node; ++cur_split_node) {
		for (int i = 0; i < SPDIMS * 2; i++) {
			fprintf(stderr, "%.2lf ",
			        *(cur_split_node->coords + i));
		}
		fprintf(stderr, "\n");
	}
#endif

	insert_size = rec_get_converted_size(cursor->index, tuple, n_ext);
	total_data = page_get_data_size(page) + insert_size;
	first_rec_group = split_rtree_node(rtr_split_node_array,
					   static_cast<int>(n_recs),
					   static_cast<int>(total_data),
					   static_cast<int>(insert_size),
					   0, 2, 2, &buf_pos, SPDIMS,
					   static_cast<uchar*>(first_rec));

	/* Allocate a new page to the index */
	const uint16_t page_level = btr_page_get_level(page);
	new_block = btr_page_alloc(cursor->index, page_id.page_no() + 1,
				   FSP_UP, page_level, mtr, mtr);
	if (!new_block) {
		return NULL;
	}

	new_page_zip = buf_block_get_page_zip(new_block);
	if (page_level && UNIV_LIKELY_NULL(new_page_zip)) {
		/* ROW_FORMAT=COMPRESSED non-leaf pages are not expected
		to contain FIL_NULL in FIL_PAGE_PREV at this stage. */
		memset_aligned<4>(new_block->page.frame + FIL_PAGE_PREV, 0, 4);
	}
	btr_page_create(new_block, new_page_zip, cursor->index,
			page_level, mtr);

	new_page = buf_block_get_frame(new_block);
	ut_ad(page_get_ssn_id(new_page) == 0);

	/* Set new ssn to the new page and page. */
	page_set_ssn_id(new_block, new_page_zip, current_ssn, mtr);
	next_ssn = rtr_get_new_ssn_id(cursor->index);

	page_set_ssn_id(block, page_zip, next_ssn, mtr);

	/* Keep recs in first group to the old page, move recs in second
	groups to the new page. */
	if (0
#ifdef UNIV_ZIP_COPY
	    || page_zip
#endif
	    || !rtr_split_page_move_rec_list(rtr_split_node_array,
					     first_rec_group,
					     new_block, block, first_rec,
					     cursor->index, *heap, mtr)) {
		ulint			n		= 0;
		rec_t*			rec;
		ulint			moved		= 0;
		ulint			max_to_move	= 0;
		rtr_rec_move_t*		rec_move	= NULL;
		ulint			pos;

		/* For some reason, compressing new_page failed,
		even though it should contain fewer records than
		the original page.  Copy the page byte for byte
		and then delete the records from both pages
		as appropriate.  Deleting will always succeed. */
		ut_a(new_page_zip);

		page_zip_copy_recs(new_block,
				   page_zip, page, cursor->index, mtr);

		page_cursor = btr_cur_get_page_cur(cursor);

		/* Move locks on recs. */
		max_to_move = page_get_n_recs(page);
		rec_move = static_cast<rtr_rec_move_t*>(mem_heap_alloc(
				*heap,
				sizeof (*rec_move) * max_to_move));

		/* Init the rec_move array for moving lock on recs.  */
		for (cur_split_node = rtr_split_node_array;
		     cur_split_node < end_split_node - 1; ++cur_split_node) {
			if (cur_split_node->n_node != first_rec_group) {
				pos = page_rec_get_n_recs_before(
					cur_split_node->key);
				rec = page_rec_get_nth(new_page, pos);
				ut_a(rec);

				rec_move[moved].new_rec = rec;
				rec_move[moved].old_rec = cur_split_node->key;
				rec_move[moved].moved = false;
				moved++;

				if (moved > max_to_move) {
					ut_ad(0);
					break;
				}
			}
		}

		/* Update the lock table */
		lock_rtr_move_rec_list(new_block, block, rec_move, moved);

		const ulint n_core = page_level
			? 0 : cursor->index->n_core_fields;

		/* Delete recs in first group from the new page. */
		for (cur_split_node = rtr_split_node_array;
		     cur_split_node < end_split_node - 1; ++cur_split_node) {
			if (cur_split_node->n_node == first_rec_group) {
				ulint	pos;

				pos = page_rec_get_n_recs_before(
						cur_split_node->key);
				ut_a(pos > 0);
				rec_t* new_rec = page_rec_get_nth(new_page,
								  pos - n);

				ut_a(new_rec && page_rec_is_user_rec(new_rec));
				page_cur_position(new_rec, new_block,
						  page_cursor);

				*offsets = rec_get_offsets(
					page_cur_get_rec(page_cursor),
					cursor->index, *offsets, n_core,
					ULINT_UNDEFINED, heap);

				page_cur_delete_rec(page_cursor,
					cursor->index, *offsets, mtr);
				n++;
			}
		}

		/* Delete recs in second group from the old page. */
		for (cur_split_node = rtr_split_node_array;
		     cur_split_node < end_split_node - 1; ++cur_split_node) {
			if (cur_split_node->n_node != first_rec_group) {
				page_cur_position(cur_split_node->key,
						  block, page_cursor);
				*offsets = rec_get_offsets(
					page_cur_get_rec(page_cursor),
					cursor->index, *offsets, n_core,
					ULINT_UNDEFINED, heap);
				page_cur_delete_rec(page_cursor,
					cursor->index, *offsets, mtr);
			}
		}

#ifdef UNIV_GIS_DEBUG
		ut_ad(page_validate(new_page, cursor->index));
		ut_ad(page_validate(page, cursor->index));
#endif
	}

	/* Insert the new rec to the proper page. */
	cur_split_node = end_split_node - 1;
	if (cur_split_node->n_node != first_rec_group) {
		insert_block = new_block;
	} else {
		insert_block = block;
	}

	/* Reposition the cursor for insert and try insertion */
	page_cursor = btr_cur_get_page_cur(cursor);

	page_cur_search(insert_block, cursor->index, tuple,
			PAGE_CUR_LE, page_cursor);

	/* It's possible that the new record is too big to be inserted into
	the page, and it'll need the second round split in this case.
	We test this scenario here*/
	DBUG_EXECUTE_IF("rtr_page_need_second_split",
			if (n_iterations == 0) {
				rec = NULL;
				goto after_insert; }
	);

	rec = page_cur_tuple_insert(page_cursor, tuple, cursor->index,
				    offsets, heap, n_ext, mtr);

	/* If insert did not fit, try page reorganization.
	For compressed pages, page_cur_tuple_insert() will have
	attempted this already. */
	if (rec == NULL) {
		if (!is_page_cur_get_page_zip(page_cursor)
		    && btr_page_reorganize(page_cursor, cursor->index, mtr)) {
			rec = page_cur_tuple_insert(page_cursor, tuple,
						    cursor->index, offsets,
						    heap, n_ext, mtr);

		}
		/* If insert fail, we will try to split the insert_block
		again. */
	}

#ifdef UNIV_DEBUG
after_insert:
#endif
	/* Calculate the mbr on the upper half-page, and the mbr on
	original page. */
	rtr_page_cal_mbr(cursor->index, block, &mbr, *heap);
	rtr_page_cal_mbr(cursor->index, new_block, &new_mbr, *heap);
	prdt.data = &mbr;
	new_prdt.data = &new_mbr;

	/* Check any predicate locks need to be moved/copied to the
	new page */
	lock_prdt_update_split(new_block, &prdt, &new_prdt, page_id);

	/* Adjust the upper level. */
	rtr_adjust_upper_level(cursor, flags, block, new_block,
			       &mbr, &new_mbr, mtr);

	/* Save the new ssn to the root page, since we need to reinit
	the first ssn value from it after restart server. */

	root_block = btr_root_block_get(cursor->index, RW_SX_LATCH, mtr);

	page_zip = buf_block_get_page_zip(root_block);
	page_set_ssn_id(root_block, page_zip, next_ssn, mtr);

	/* If the new res insert fail, we need to do another split
	 again. */
	if (!rec) {
		/* We play safe and reset the free bits for new_page */
		if (!dict_index_is_clust(cursor->index)
		    && !cursor->index->table->is_temporary()) {
			ibuf_reset_free_bits(new_block);
			ibuf_reset_free_bits(block);
		}

		/* We need to clean the parent path here and search father
		node later, otherwise, it's possible that find a wrong
		parent. */
		rtr_clean_rtr_info(cursor->rtr_info, true);
		cursor->rtr_info = NULL;
		n_iterations++;

		rec_t* i_rec = page_rec_get_next(page_get_infimum_rec(
			buf_block_get_frame(block)));
		btr_cur_position(cursor->index, i_rec, block, cursor);

		goto func_start;
	}

#ifdef UNIV_GIS_DEBUG
	ut_ad(page_validate(buf_block_get_frame(block), cursor->index));
	ut_ad(page_validate(buf_block_get_frame(new_block), cursor->index));

	ut_ad(!rec || rec_offs_validate(rec, cursor->index, *offsets));
#endif
	MONITOR_INC(MONITOR_INDEX_SPLIT);

	return(rec);
}

/****************************************************************//**
Following the right link to find the proper block for insert.
@return the proper block.*/
dberr_t
rtr_ins_enlarge_mbr(
/*================*/
	btr_cur_t*		btr_cur,	/*!< in: btr cursor */
	mtr_t*			mtr)		/*!< in: mtr */
{
	dberr_t			err = DB_SUCCESS;
	rtr_mbr_t		new_mbr;
	buf_block_t*		block;
	mem_heap_t*		heap;
	dict_index_t*		index = btr_cur->index;
	page_cur_t*		page_cursor;
	rec_offs*		offsets;
	node_visit_t*		node_visit;
	btr_cur_t		cursor;
	page_t*			page;

	ut_ad(dict_index_is_spatial(index));

	/* If no rtr_info or rtree is one level tree, return. */
	if (!btr_cur->rtr_info || btr_cur->tree_height == 1) {
		return(err);
	}

	/* Check path info is not empty. */
	ut_ad(!btr_cur->rtr_info->parent_path->empty());

	/* Create a memory heap. */
	heap = mem_heap_create(1024);

	/* Leaf level page is stored in cursor */
	page_cursor = btr_cur_get_page_cur(btr_cur);
	block = page_cur_get_block(page_cursor);

	for (ulint i = 1; i < btr_cur->tree_height; i++) {
		node_visit = rtr_get_parent_node(btr_cur, i, true);
		ut_ad(node_visit != NULL);

		/* If there's no mbr enlarge, return.*/
		if (node_visit->mbr_inc == 0) {
			block = btr_pcur_get_block(node_visit->cursor);
			continue;
		}

		/* Calculate the mbr of the child page. */
		rtr_page_cal_mbr(index, block, &new_mbr, heap);

		/* Get father block. */
		cursor.init();
		offsets = rtr_page_get_father_block(
			NULL, heap, index, block, mtr, btr_cur, &cursor);

		page = buf_block_get_frame(block);

		/* Update the mbr field of the rec. */
		rtr_update_mbr_field(&cursor, offsets, NULL, page,
				     &new_mbr, NULL, mtr);
		page_cursor = btr_cur_get_page_cur(&cursor);
		block = page_cur_get_block(page_cursor);
	}

	mem_heap_free(heap);

	return(err);
}

/*************************************************************//**
Copy recs from a page to new_block of rtree.
Differs from page_copy_rec_list_end, because this function does not
touch the lock table and max trx id on page or compress the page.

IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
if new_block is a compressed leaf page in a secondary index.
This has to be done either within the same mini-transaction,
or by invoking ibuf_reset_free_bits() before mtr_commit(). */
void
rtr_page_copy_rec_list_end_no_locks(
/*================================*/
	buf_block_t*	new_block,	/*!< in: index page to copy to */
	buf_block_t*	block,		/*!< in: index page of rec */
	rec_t*		rec,		/*!< in: record on page */
	dict_index_t*	index,		/*!< in: record descriptor */
	mem_heap_t*	heap,		/*!< in/out: heap memory */
	rtr_rec_move_t*	rec_move,	/*!< in: recording records moved */
	ulint		max_move,	/*!< in: num of rec to move */
	ulint*		num_moved,	/*!< out: num of rec to move */
	mtr_t*		mtr)		/*!< in: mtr */
{
	page_t*		new_page	= buf_block_get_frame(new_block);
	page_cur_t	page_cur;
	page_cur_t	cur1;
	rec_t*		cur_rec;
	rec_offs	offsets_1[REC_OFFS_NORMAL_SIZE];
	rec_offs*	offsets1 = offsets_1;
	rec_offs	offsets_2[REC_OFFS_NORMAL_SIZE];
	rec_offs*	offsets2 = offsets_2;
	ulint		moved = 0;
	const ulint	n_core = page_is_leaf(new_page)
		? index->n_core_fields : 0;

	rec_offs_init(offsets_1);
	rec_offs_init(offsets_2);

	page_cur_position(rec, block, &cur1);

	if (page_cur_is_before_first(&cur1)) {
		page_cur_move_to_next(&cur1);
	}

	btr_assert_not_corrupted(new_block, index);
	ut_a(page_is_comp(new_page) == page_rec_is_comp(rec));
	ut_a(mach_read_from_2(new_page + srv_page_size - 10) == (ulint)
	     (page_is_comp(new_page) ? PAGE_NEW_INFIMUM : PAGE_OLD_INFIMUM));

	cur_rec = page_rec_get_next(
		page_get_infimum_rec(buf_block_get_frame(new_block)));
	page_cur_position(cur_rec, new_block, &page_cur);

	/* Copy records from the original page to the new page */
	while (!page_cur_is_after_last(&cur1)) {
		rec_t*	cur1_rec = page_cur_get_rec(&cur1);
		rec_t*	ins_rec;

		if (page_rec_is_infimum(cur_rec)) {
			cur_rec = page_rec_get_next(cur_rec);
		}

		offsets1 = rec_get_offsets(cur1_rec, index, offsets1, n_core,
					   ULINT_UNDEFINED, &heap);
		while (!page_rec_is_supremum(cur_rec)) {
			ulint		cur_matched_fields = 0;
			int		cmp;

			offsets2 = rec_get_offsets(cur_rec, index, offsets2,
						   n_core,
						   ULINT_UNDEFINED, &heap);
			cmp = cmp_rec_rec(cur1_rec, cur_rec,
					  offsets1, offsets2, index, false,
					  &cur_matched_fields);
			if (cmp < 0) {
				page_cur_move_to_prev(&page_cur);
				break;
			} else if (cmp > 0) {
				/* Skip small recs. */
				page_cur_move_to_next(&page_cur);
				cur_rec = page_cur_get_rec(&page_cur);
			} else if (n_core) {
				if (rec_get_deleted_flag(cur1_rec,
					dict_table_is_comp(index->table))) {
					goto next;
				} else {
					/* We have two identical leaf records,
					skip copying the undeleted one, and
					unmark deleted on the current page */
					btr_rec_set_deleted<false>(
						new_block, cur_rec, mtr);
					goto next;
				}
			}
		}

		/* If position is on suprenum rec, need to move to
		previous rec. */
		if (page_rec_is_supremum(cur_rec)) {
			page_cur_move_to_prev(&page_cur);
		}

		cur_rec = page_cur_get_rec(&page_cur);

		offsets1 = rec_get_offsets(cur1_rec, index, offsets1, n_core,
					   ULINT_UNDEFINED, &heap);

		ins_rec = page_cur_insert_rec_low(&page_cur, index,
						  cur1_rec, offsets1, mtr);
		if (UNIV_UNLIKELY(!ins_rec)) {
			fprintf(stderr, "page number %u and %u\n",
				new_block->page.id().page_no(),
				block->page.id().page_no());

			ib::fatal() << "rec offset " << page_offset(rec)
				<< ", cur1 offset "
				<<  page_offset(page_cur_get_rec(&cur1))
				<< ", cur_rec offset "
				<< page_offset(cur_rec);
		}

		rec_move[moved].new_rec = ins_rec;
		rec_move[moved].old_rec = cur1_rec;
		rec_move[moved].moved = false;
		moved++;
next:
		if (moved > max_move) {
			ut_ad(0);
			break;
		}

		page_cur_move_to_next(&cur1);
	}

	*num_moved = moved;
}

/*************************************************************//**
Copy recs till a specified rec from a page to new_block of rtree. */
void
rtr_page_copy_rec_list_start_no_locks(
/*==================================*/
	buf_block_t*	new_block,	/*!< in: index page to copy to */
	buf_block_t*	block,		/*!< in: index page of rec */
	rec_t*		rec,		/*!< in: record on page */
	dict_index_t*	index,		/*!< in: record descriptor */
	mem_heap_t*	heap,		/*!< in/out: heap memory */
	rtr_rec_move_t*	rec_move,	/*!< in: recording records moved */
	ulint		max_move,	/*!< in: num of rec to move */
	ulint*		num_moved,	/*!< out: num of rec to move */
	mtr_t*		mtr)		/*!< in: mtr */
{
	page_cur_t	cur1;
	rec_t*		cur_rec;
	rec_offs	offsets_1[REC_OFFS_NORMAL_SIZE];
	rec_offs*	offsets1 = offsets_1;
	rec_offs	offsets_2[REC_OFFS_NORMAL_SIZE];
	rec_offs*	offsets2 = offsets_2;
	page_cur_t	page_cur;
	ulint		moved = 0;
	const ulint	n_core = page_is_leaf(buf_block_get_frame(block))
		? index->n_core_fields : 0;

	rec_offs_init(offsets_1);
	rec_offs_init(offsets_2);

	page_cur_set_before_first(block, &cur1);
	page_cur_move_to_next(&cur1);

	cur_rec = page_rec_get_next(
		page_get_infimum_rec(buf_block_get_frame(new_block)));
	page_cur_position(cur_rec, new_block, &page_cur);

	while (page_cur_get_rec(&cur1) != rec) {
		rec_t*	cur1_rec = page_cur_get_rec(&cur1);
		rec_t*	ins_rec;

		if (page_rec_is_infimum(cur_rec)) {
			cur_rec = page_rec_get_next(cur_rec);
		}

		offsets1 = rec_get_offsets(cur1_rec, index, offsets1, n_core,
					   ULINT_UNDEFINED, &heap);

		while (!page_rec_is_supremum(cur_rec)) {
			ulint		cur_matched_fields = 0;

			offsets2 = rec_get_offsets(cur_rec, index, offsets2,
						   n_core,
						   ULINT_UNDEFINED, &heap);
			int cmp = cmp_rec_rec(cur1_rec, cur_rec,
					      offsets1, offsets2, index, false,
					      &cur_matched_fields);
			if (cmp < 0) {
				page_cur_move_to_prev(&page_cur);
				cur_rec = page_cur_get_rec(&page_cur);
				break;
			} else if (cmp > 0) {
				/* Skip small recs. */
				page_cur_move_to_next(&page_cur);
				cur_rec = page_cur_get_rec(&page_cur);
			} else if (n_core) {
				if (rec_get_deleted_flag(
					cur1_rec,
					dict_table_is_comp(index->table))) {
					goto next;
				} else {
					/* We have two identical leaf records,
					skip copying the undeleted one, and
					unmark deleted on the current page */
					btr_rec_set_deleted<false>(
						new_block, cur_rec, mtr);
					goto next;
				}
			}
		}

		/* If position is on suprenum rec, need to move to
		previous rec. */
		if (page_rec_is_supremum(cur_rec)) {
			page_cur_move_to_prev(&page_cur);
		}

		cur_rec = page_cur_get_rec(&page_cur);

		offsets1 = rec_get_offsets(cur1_rec, index, offsets1, n_core,
					   ULINT_UNDEFINED, &heap);

		ins_rec = page_cur_insert_rec_low(&page_cur, index,
						  cur1_rec, offsets1, mtr);
		if (UNIV_UNLIKELY(!ins_rec)) {
			ib::fatal() << new_block->page.id()
				<< "rec offset " << page_offset(rec)
				<< ", cur1 offset "
				<<  page_offset(page_cur_get_rec(&cur1))
				<< ", cur_rec offset "
				<< page_offset(cur_rec);
		}

		rec_move[moved].new_rec = ins_rec;
		rec_move[moved].old_rec = cur1_rec;
		rec_move[moved].moved = false;
		moved++;
next:
		if (moved > max_move) {
			ut_ad(0);
			break;
		}

		page_cur_move_to_next(&cur1);
	}

	*num_moved = moved;
}

/****************************************************************//**
Check two MBRs are identical or need to be merged */
bool
rtr_merge_mbr_changed(
/*==================*/
	btr_cur_t*		cursor,		/*!< in/out: cursor */
	btr_cur_t*		cursor2,	/*!< in: the other cursor */
	rec_offs*		offsets,	/*!< in: rec offsets */
	rec_offs*		offsets2,	/*!< in: rec offsets */
	rtr_mbr_t*		new_mbr)	/*!< out: MBR to update */
{
	double*		mbr;
	double		mbr1[SPDIMS * 2];
	double		mbr2[SPDIMS * 2];
	rec_t*		rec;
	ulint		len;
	bool		changed = false;

	ut_ad(dict_index_is_spatial(cursor->index));

	rec = btr_cur_get_rec(cursor);

	rtr_read_mbr(rec_get_nth_field(rec, offsets, 0, &len),
		     reinterpret_cast<rtr_mbr_t*>(mbr1));

	rec = btr_cur_get_rec(cursor2);

	rtr_read_mbr(rec_get_nth_field(rec, offsets2, 0, &len),
		     reinterpret_cast<rtr_mbr_t*>(mbr2));

	mbr = reinterpret_cast<double*>(new_mbr);

	for (int i = 0; i < SPDIMS * 2; i += 2) {
		changed = (changed || mbr1[i] != mbr2[i]);
		*mbr = mbr1[i] < mbr2[i] ? mbr1[i] : mbr2[i];
		mbr++;
		changed = (changed || mbr1[i + 1] != mbr2 [i + 1]);
		*mbr = mbr1[i + 1] > mbr2[i + 1] ? mbr1[i + 1] : mbr2[i + 1];
		mbr++;
	}

	return(changed);
}

/****************************************************************//**
Merge 2 mbrs and update the the mbr that cursor is on. */
void
rtr_merge_and_update_mbr(
/*=====================*/
	btr_cur_t*		cursor,		/*!< in/out: cursor */
	btr_cur_t*		cursor2,	/*!< in: the other cursor */
	rec_offs*		offsets,	/*!< in: rec offsets */
	rec_offs*		offsets2,	/*!< in: rec offsets */
	page_t*			child_page,	/*!< in: the page. */
	mtr_t*			mtr)		/*!< in: mtr */
{
	rtr_mbr_t		new_mbr;

	if (rtr_merge_mbr_changed(cursor, cursor2, offsets, offsets2,
                                  &new_mbr)) {
		rtr_update_mbr_field(cursor, offsets, cursor2, child_page,
				     &new_mbr, NULL, mtr);
	} else {
		rtr_node_ptr_delete(cursor2, mtr);
	}
}

/*************************************************************//**
Deletes on the upper level the node pointer to a page. */
void
rtr_node_ptr_delete(
/*================*/
	btr_cur_t*	cursor, /*!< in: search cursor, contains information
				about parent nodes in search */
	mtr_t*		mtr)	/*!< in: mtr */
{
	ibool		compressed;
	dberr_t		err;

	compressed = btr_cur_pessimistic_delete(&err, TRUE, cursor,
						BTR_CREATE_FLAG, false, mtr);
	ut_a(err == DB_SUCCESS);

	if (!compressed) {
		btr_cur_compress_if_useful(cursor, FALSE, mtr);
	}
}

/**************************************************************//**
Check whether a Rtree page is child of a parent page
@return true if there is child/parent relationship */
bool
rtr_check_same_block(
/*================*/
	dict_index_t*	index,	/*!< in: index tree */
	btr_cur_t*	cursor,	/*!< in/out: position at the parent entry
				pointing to the child if successful */
	buf_block_t*	parentb,/*!< in: parent page to check */
	buf_block_t*	childb,	/*!< in: child Page */
	mem_heap_t*	heap)	/*!< in: memory heap */

{
	ulint		page_no = childb->page.id().page_no();
	rec_offs*	offsets;
	rec_t*		rec = page_rec_get_next(page_get_infimum_rec(
				buf_block_get_frame(parentb)));

	while (!page_rec_is_supremum(rec)) {
		offsets = rec_get_offsets(
			rec, index, NULL, 0, ULINT_UNDEFINED, &heap);

		if (btr_node_ptr_get_child_page_no(rec, offsets) == page_no) {
			btr_cur_position(index, rec, parentb, cursor);
			return(true);
		}

		rec = page_rec_get_next(rec);
	}

	return(false);
}

/*************************************************************//**
Calculates MBR_AREA(a+b) - MBR_AREA(a)
Note: when 'a' and 'b' objects are far from each other,
the area increase can be really big, so this function
can return 'inf' as a result.
Return the area increaed. */
static double
rtree_area_increase(
	const uchar*	a,		/*!< in: original mbr. */
	const uchar*	b,		/*!< in: new mbr. */
	double*		ab_area)	/*!< out: increased area. */
{
	double		a_area = 1.0;
	double		loc_ab_area = 1.0;
	double		amin, amax, bmin, bmax;
	double		data_round = 1.0;

	static_assert(DATA_MBR_LEN == SPDIMS * 2 * sizeof(double),
		      "compatibility");

	for (auto i = SPDIMS; i--; ) {
		double	area;

		amin = mach_double_read(a);
		bmin = mach_double_read(b);
		amax = mach_double_read(a + sizeof(double));
		bmax = mach_double_read(b + sizeof(double));

		a += 2 * sizeof(double);
		b += 2 * sizeof(double);

		area = amax - amin;
		if (area == 0) {
			a_area *= LINE_MBR_WEIGHTS;
		} else {
			a_area *= area;
		}

		area = (double)std::max(amax, bmax) -
		       (double)std::min(amin, bmin);
		if (area == 0) {
			loc_ab_area *= LINE_MBR_WEIGHTS;
		} else {
			loc_ab_area *= area;
		}

		/* Value of amax or bmin can be so large that small difference
		are ignored. For example: 3.2884281489988079e+284 - 100 =
		3.2884281489988079e+284. This results some area difference
		are not detected */
		if (loc_ab_area == a_area) {
			if (bmin < amin || bmax > amax) {
				data_round *= ((double)std::max(amax, bmax)
					       - amax
					       + (amin - (double)std::min(
								amin, bmin)));
			} else {
				data_round *= area;
			}
		}
	}

	*ab_area = loc_ab_area;

	if (loc_ab_area == a_area && data_round != 1.0) {
		return(data_round);
	}

	return(loc_ab_area - a_area);
}

/** Calculates overlapping area
@param[in]	a	mbr a
@param[in]	b	mbr b
@return overlapping area */
static double rtree_area_overlapping(const byte *a, const byte *b)
{
	double	area = 1.0;
	double	amin;
	double	amax;
	double	bmin;
	double	bmax;

	static_assert(DATA_MBR_LEN == SPDIMS * 2 * sizeof(double),
		      "compatibility");

	for (auto i = SPDIMS; i--; ) {
		amin = mach_double_read(a);
		bmin = mach_double_read(b);
		amax = mach_double_read(a + sizeof(double));
		bmax = mach_double_read(b + sizeof(double));
		a += 2 * sizeof(double);
		b += 2 * sizeof(double);

		amin = std::max(amin, bmin);
		amax = std::min(amax, bmax);

		if (amin > amax) {
			return(0);
		} else {
			area *= (amax - amin);
		}
	}

	return(area);
}

/****************************************************************//**
Calculate the area increased for a new record
@return area increased */
double
rtr_rec_cal_increase(
/*=================*/
	const dtuple_t*	dtuple,	/*!< in: data tuple to insert, which
				cause area increase */
	const rec_t*	rec,	/*!< in: physical record which differs from
				dtuple in some of the common fields, or which
				has an equal number or more fields than
				dtuple */
	double*		area)	/*!< out: increased area */
{
	const dfield_t*	dtuple_field;

	ut_ad(!page_rec_is_supremum(rec));
	ut_ad(!page_rec_is_infimum(rec));

	dtuple_field = dtuple_get_nth_field(dtuple, 0);
	ut_ad(dfield_get_len(dtuple_field) == DATA_MBR_LEN);

	return rtree_area_increase(rec,
				   static_cast<const byte*>(
					   dfield_get_data(dtuple_field)),
				   area);
}

/** Estimates the number of rows in a given area.
@param[in]	index	index
@param[in]	tuple	range tuple containing mbr, may also be empty tuple
@param[in]	mode	search mode
@return estimated number of rows */
ha_rows
rtr_estimate_n_rows_in_range(
	dict_index_t*	index,
	const dtuple_t*	tuple,
	page_cur_mode_t	mode)
{
	ut_ad(dict_index_is_spatial(index));

	/* Check tuple & mode */
	if (tuple->n_fields == 0) {
		return(HA_POS_ERROR);
	}

	switch (mode) {
	case PAGE_CUR_DISJOINT:
	case PAGE_CUR_CONTAIN:
	case PAGE_CUR_INTERSECT:
	case PAGE_CUR_WITHIN:
	case PAGE_CUR_MBR_EQUAL:
		break;
	default:
		return(HA_POS_ERROR);
	}

	DBUG_EXECUTE_IF("rtr_pcur_move_to_next_return",
		return(2);
	);

	/* Read mbr from tuple. */
	rtr_mbr_t	range_mbr;
	double		range_area;

	const dfield_t* dtuple_field = dtuple_get_nth_field(tuple, 0);
	ut_ad(dfield_get_len(dtuple_field) >= DATA_MBR_LEN);
	const byte* range_mbr_ptr = reinterpret_cast<const byte*>(
		dfield_get_data(dtuple_field));

	rtr_read_mbr(range_mbr_ptr, &range_mbr);
	range_area = (range_mbr.xmax - range_mbr.xmin)
		 * (range_mbr.ymax - range_mbr.ymin);

	/* Get index root page. */
	mtr_t		mtr;

	mtr.start();
	index->set_modified(mtr);
	mtr_s_lock_index(index, &mtr);

	buf_block_t* block = btr_root_block_get(index, RW_S_LATCH, &mtr);
	if (!block) {
err_exit:
		mtr.commit();
		return HA_POS_ERROR;
	}
	const page_t* page = buf_block_get_frame(block);
	const unsigned n_recs = page_header_get_field(page, PAGE_N_RECS);

	if (n_recs == 0) {
		goto err_exit;
	}

	/* Scan records in root page and calculate area. */
	double	area = 0;
	for (const rec_t* rec = page_rec_get_next(
		     page_get_infimum_rec(block->page.frame));
	     !page_rec_is_supremum(rec);
	     rec = page_rec_get_next_const(rec)) {
		rtr_mbr_t	mbr;
		double		rec_area;

		rtr_read_mbr(rec, &mbr);

		rec_area = (mbr.xmax - mbr.xmin) * (mbr.ymax - mbr.ymin);

		if (rec_area == 0) {
			switch (mode) {
			case PAGE_CUR_CONTAIN:
			case PAGE_CUR_INTERSECT:
				area += 1;
				break;

			case PAGE_CUR_DISJOINT:
				break;

			case PAGE_CUR_WITHIN:
			case PAGE_CUR_MBR_EQUAL:
				if (!rtree_key_cmp(
					    PAGE_CUR_WITHIN, range_mbr_ptr,
					    rec)) {
					area += 1;
				}

				break;

			default:
				ut_error;
			}
		} else {
			switch (mode) {
			case PAGE_CUR_CONTAIN:
			case PAGE_CUR_INTERSECT:
				area += rtree_area_overlapping(
					range_mbr_ptr, rec)
					/ rec_area;
				break;

			case PAGE_CUR_DISJOINT:
				area += 1;
				area -= rtree_area_overlapping(
					range_mbr_ptr, rec)
					/ rec_area;
				break;

			case PAGE_CUR_WITHIN:
			case PAGE_CUR_MBR_EQUAL:
				if (!rtree_key_cmp(
					    PAGE_CUR_WITHIN, range_mbr_ptr,
					    rec)) {
					area += range_area / rec_area;
				}

				break;
			default:
				ut_error;
			}
		}
	}

	mtr.commit();

	if (!std::isfinite(area)) {
		return(HA_POS_ERROR);
	}

	area /= n_recs;
	return ha_rows(static_cast<double>(dict_table_get_n_rows(index->table))
		       * area);
}