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
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
|
/*****************************************************************************
Copyright (c) 1994, 2019, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2008, Google Inc.
Copyright (c) 2012, Facebook Inc.
Copyright (c) 2015, 2022, MariaDB Corporation.
Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.
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 btr/btr0cur.cc
The index tree cursor
All changes that row operations make to a B-tree or the records
there must go through this module! Undo log records are written here
of every modify or insert of a clustered index record.
NOTE!!!
To make sure we do not run out of disk space during a pessimistic
insert or update, we have to reserve 2 x the height of the index tree
many pages in the tablespace before we start the operation, because
if leaf splitting has been started, it is difficult to undo, except
by crashing the database and doing a roll-forward.
Created 10/16/1994 Heikki Tuuri
*******************************************************/
#include "btr0cur.h"
#include "row0upd.h"
#include "mtr0log.h"
#include "page0page.h"
#include "page0zip.h"
#include "rem0rec.h"
#include "rem0cmp.h"
#include "buf0lru.h"
#include "btr0btr.h"
#include "btr0sea.h"
#include "row0log.h"
#include "row0purge.h"
#include "row0upd.h"
#include "trx0rec.h"
#include "trx0roll.h"
#include "que0que.h"
#include "row0row.h"
#include "srv0srv.h"
#include "ibuf0ibuf.h"
#include "lock0lock.h"
#include "zlib.h"
#include "srv0start.h"
#include "mysql_com.h"
#include "dict0stats.h"
#include "row0ins.h"
#ifdef WITH_WSREP
#include "mysql/service_wsrep.h"
#endif /* WITH_WSREP */
#include "log.h"
/** Buffered B-tree operation types, introduced as part of delete buffering. */
enum btr_op_t {
BTR_NO_OP = 0, /*!< Not buffered */
BTR_INSERT_OP, /*!< Insert, do not ignore UNIQUE */
BTR_INSERT_IGNORE_UNIQUE_OP, /*!< Insert, ignoring UNIQUE */
BTR_DELETE_OP, /*!< Purge a delete-marked record */
BTR_DELMARK_OP /*!< Mark a record for deletion */
};
/** Modification types for the B-tree operation.
Note that the order must be DELETE, BOTH, INSERT !!
*/
enum btr_intention_t {
BTR_INTENTION_DELETE,
BTR_INTENTION_BOTH,
BTR_INTENTION_INSERT
};
/** For the index->lock scalability improvement, only possibility of clear
performance regression observed was caused by grown huge history list length.
That is because the exclusive use of index->lock also worked as reserving
free blocks and read IO bandwidth with priority. To avoid huge glowing history
list as same level with previous implementation, prioritizes pessimistic tree
operations by purge as the previous, when it seems to be growing huge.
Experimentally, the history list length starts to affect to performance
throughput clearly from about 100000. */
#define BTR_CUR_FINE_HISTORY_LENGTH 100000
#ifdef BTR_CUR_HASH_ADAPT
/** Number of searches down the B-tree in btr_cur_search_to_nth_level(). */
ib_counter_t<ulint, ib_counter_element_t> btr_cur_n_non_sea;
/** Old value of btr_cur_n_non_sea. Copied by
srv_refresh_innodb_monitor_stats(). Referenced by
srv_printf_innodb_monitor(). */
ulint btr_cur_n_non_sea_old;
/** Number of successful adaptive hash index lookups in
btr_cur_search_to_nth_level(). */
ib_counter_t<ulint, ib_counter_element_t> btr_cur_n_sea;
/** Old value of btr_cur_n_sea. Copied by
srv_refresh_innodb_monitor_stats(). Referenced by
srv_printf_innodb_monitor(). */
ulint btr_cur_n_sea_old;
#endif /* BTR_CUR_HASH_ADAPT */
#ifdef UNIV_DEBUG
/* Flag to limit optimistic insert records */
uint btr_cur_limit_optimistic_insert_debug;
#endif /* UNIV_DEBUG */
/** In the optimistic insert, if the insert does not fit, but this much space
can be released by page reorganize, then it is reorganized */
#define BTR_CUR_PAGE_REORGANIZE_LIMIT (srv_page_size / 32)
/** The structure of a BLOB part header */
/* @{ */
/*--------------------------------------*/
#define BTR_BLOB_HDR_PART_LEN 0 /*!< BLOB part len on this
page */
#define BTR_BLOB_HDR_NEXT_PAGE_NO 4 /*!< next BLOB part page no,
FIL_NULL if none */
/*--------------------------------------*/
#define BTR_BLOB_HDR_SIZE 8 /*!< Size of a BLOB
part header, in bytes */
/* @} */
/*******************************************************************//**
Marks all extern fields in a record as owned by the record. This function
should be called if the delete mark of a record is removed: a not delete
marked record always owns all its extern fields. */
static
void
btr_cur_unmark_extern_fields(
/*=========================*/
buf_block_t* block, /*!< in/out: index page */
rec_t* rec, /*!< in/out: record in a clustered index */
dict_index_t* index, /*!< in: index of the page */
const rec_offs* offsets,/*!< in: array returned by rec_get_offsets() */
mtr_t* mtr); /*!< in: mtr, or NULL if not logged */
/***********************************************************//**
Frees the externally stored fields for a record, if the field is mentioned
in the update vector. */
static
void
btr_rec_free_updated_extern_fields(
/*===============================*/
dict_index_t* index, /*!< in: index of rec; the index tree MUST be
X-latched */
rec_t* rec, /*!< in: record */
buf_block_t* block, /*!< in: index page of rec */
const rec_offs* offsets,/*!< in: rec_get_offsets(rec, index) */
const upd_t* update, /*!< in: update vector */
bool rollback,/*!< in: performing rollback? */
mtr_t* mtr); /*!< in: mini-transaction handle which contains
an X-latch to record page and to the tree */
/***********************************************************//**
Frees the externally stored fields for a record. */
static
void
btr_rec_free_externally_stored_fields(
/*==================================*/
dict_index_t* index, /*!< in: index of the data, the index
tree MUST be X-latched */
rec_t* rec, /*!< in: record */
const rec_offs* offsets,/*!< in: rec_get_offsets(rec, index) */
buf_block_t* block, /*!< in: index page of rec */
bool rollback,/*!< in: performing rollback? */
mtr_t* mtr); /*!< in: mini-transaction handle which contains
an X-latch to record page and to the index
tree */
/*==================== B-TREE SEARCH =========================*/
/** Latches the leaf page or pages requested.
@param[in] block leaf page where the search converged
@param[in] latch_mode BTR_SEARCH_LEAF, ...
@param[in] cursor cursor
@param[in] mtr mini-transaction
@param[out] latch_leaves latched blocks and savepoints */
void
btr_cur_latch_leaves(
buf_block_t* block,
btr_latch_mode latch_mode,
btr_cur_t* cursor,
mtr_t* mtr,
btr_latch_leaves_t* latch_leaves)
{
compile_time_assert(int(MTR_MEMO_PAGE_S_FIX) == int(RW_S_LATCH));
compile_time_assert(int(MTR_MEMO_PAGE_X_FIX) == int(RW_X_LATCH));
compile_time_assert(int(MTR_MEMO_PAGE_SX_FIX) == int(RW_SX_LATCH));
ut_ad(block->page.id().space() == cursor->index()->table->space->id);
ut_ad(block->page.in_file());
ut_ad(srv_read_only_mode
|| mtr->memo_contains_flagged(&cursor->index()->lock,
MTR_MEMO_S_LOCK
| MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
auto rtr_info = cursor->rtr_info;
if (UNIV_LIKELY_NULL(rtr_info) && !cursor->index()->is_spatial()) {
rtr_info = nullptr;
}
const rw_lock_type_t mode = rw_lock_type_t(
latch_mode & (RW_X_LATCH | RW_S_LATCH));
static_assert(ulint{RW_S_LATCH} == ulint{BTR_SEARCH_LEAF}, "");
static_assert(ulint{RW_X_LATCH} == ulint{BTR_MODIFY_LEAF}, "");
static_assert(BTR_SEARCH_LEAF & BTR_SEARCH_TREE, "");
switch (latch_mode) {
default:
break;
uint32_t left_page_no;
uint32_t right_page_no;
ulint save;
case BTR_SEARCH_LEAF:
case BTR_MODIFY_LEAF:
case BTR_SEARCH_TREE:
if (UNIV_LIKELY_NULL(rtr_info)) {
rtr_info->tree_savepoints[RTR_MAX_LEVELS]
= mtr->get_savepoint();
}
latch_block:
if (latch_leaves) {
latch_leaves->savepoints[1] = mtr->get_savepoint();
latch_leaves->blocks[1] = block;
}
block->page.fix();
mtr->page_lock(block, mode);
if (UNIV_LIKELY_NULL(rtr_info)) {
rtr_info->tree_blocks[RTR_MAX_LEVELS] = block;
}
return;
case BTR_MODIFY_TREE:
/* It is exclusive for other operations which calls
btr_page_set_prev() */
ut_ad(mtr->memo_contains_flagged(&cursor->index()->lock,
MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
save = mtr->get_savepoint();
/* x-latch also siblings from left to right */
left_page_no = btr_page_get_prev(block->page.frame);
if (left_page_no != FIL_NULL) {
buf_block_t *b = btr_block_get(
*cursor->index(), left_page_no, RW_X_LATCH,
true, mtr);
if (latch_leaves) {
latch_leaves->savepoints[0] = save;
latch_leaves->blocks[0] = b;
}
if (UNIV_LIKELY_NULL(rtr_info)) {
rtr_info->tree_savepoints[RTR_MAX_LEVELS]
= save;
rtr_info->tree_blocks[RTR_MAX_LEVELS] = b;
}
save = mtr->get_savepoint();
}
if (latch_leaves) {
latch_leaves->savepoints[1] = mtr->get_savepoint();
latch_leaves->blocks[1] = block;
}
block->page.fix();
block->page.lock.x_lock();
mtr->memo_push(block, MTR_MEMO_PAGE_X_FIX);
#ifdef BTR_CUR_HASH_ADAPT
ut_ad(!btr_search_check_marked_free_index(block));
#endif
if (UNIV_LIKELY_NULL(rtr_info)) {
rtr_info->tree_savepoints[RTR_MAX_LEVELS + 1] = save;
rtr_info->tree_blocks[RTR_MAX_LEVELS + 1] = block;
}
right_page_no = btr_page_get_next(block->page.frame);
if (right_page_no != FIL_NULL) {
save = mtr->get_savepoint();
buf_block_t* b = btr_block_get(
*cursor->index(), right_page_no, RW_X_LATCH,
true, mtr);
if (latch_leaves) {
latch_leaves->savepoints[2] = save;
latch_leaves->blocks[2] = b;
}
if (UNIV_LIKELY_NULL(rtr_info)) {
rtr_info->tree_savepoints[RTR_MAX_LEVELS + 2]
= save;
rtr_info->tree_blocks[RTR_MAX_LEVELS + 2] = b;
}
}
return;
case BTR_SEARCH_PREV:
case BTR_MODIFY_PREV:
ut_ad(!rtr_info);
static_assert(BTR_SEARCH_PREV & BTR_SEARCH_LEAF, "");
static_assert(BTR_MODIFY_PREV & BTR_MODIFY_LEAF, "");
static_assert((BTR_SEARCH_PREV ^ BTR_MODIFY_PREV)
== (RW_S_LATCH ^ RW_X_LATCH), "");
/* Because we are holding index->lock, no page splits
or merges may run concurrently, and we may read
FIL_PAGE_PREV from a buffer-fixed, unlatched page. */
left_page_no = btr_page_get_prev(block->page.frame);
if (left_page_no != FIL_NULL) {
save = mtr->get_savepoint();
cursor->left_block = btr_block_get(
*cursor->index(), left_page_no,
mode, true, mtr);
if (latch_leaves) {
latch_leaves->savepoints[0] = save;
latch_leaves->blocks[0] = cursor->left_block;
}
}
goto latch_block;
case BTR_CONT_MODIFY_TREE:
ut_ad(cursor->index()->is_spatial());
return;
}
MY_ASSERT_UNREACHABLE();
}
/** Load the instant ALTER TABLE metadata from the clustered index
when loading a table definition.
@param[in,out] index clustered index definition
@param[in,out] mtr mini-transaction
@return error code
@retval DB_SUCCESS if no error occurred
@retval DB_CORRUPTION if any corruption was noticed */
static dberr_t btr_cur_instant_init_low(dict_index_t* index, mtr_t* mtr)
{
ut_ad(index->is_primary());
ut_ad(index->n_core_null_bytes == dict_index_t::NO_CORE_NULL_BYTES);
ut_ad(index->table->supports_instant());
ut_ad(index->table->is_readable());
dberr_t err;
const fil_space_t* space = index->table->space;
if (!space) {
corrupted:
err = DB_CORRUPTION;
unreadable:
ib::error() << "Table " << index->table->name
<< " has an unreadable root page";
index->table->corrupted = true;
index->table->file_unreadable = true;
return err;
}
buf_block_t* root = btr_root_block_get(index, RW_SX_LATCH, mtr, &err);
if (!root) {
goto unreadable;
}
if (btr_cur_instant_root_init(index, root->page.frame)) {
goto corrupted;
}
ut_ad(index->n_core_null_bytes != dict_index_t::NO_CORE_NULL_BYTES);
if (fil_page_get_type(root->page.frame) == FIL_PAGE_INDEX) {
ut_ad(!index->is_instant());
return DB_SUCCESS;
}
btr_cur_t cur;
/* Relax the assertion in rec_init_offsets(). */
ut_ad(!index->in_instant_init);
ut_d(index->in_instant_init = true);
err = cur.open_leaf(true, index, BTR_SEARCH_LEAF, mtr);
ut_d(index->in_instant_init = false);
if (err != DB_SUCCESS) {
index->table->file_unreadable = true;
index->table->corrupted = true;
return err;
}
ut_ad(page_cur_is_before_first(&cur.page_cur));
ut_ad(page_is_leaf(cur.page_cur.block->page.frame));
const rec_t* rec = page_cur_move_to_next(&cur.page_cur);
const ulint comp = dict_table_is_comp(index->table);
const ulint info_bits = rec ? rec_get_info_bits(rec, comp) : 0;
if (page_rec_is_supremum(rec)
|| !(info_bits & REC_INFO_MIN_REC_FLAG)) {
if (rec && !index->is_instant()) {
/* The FIL_PAGE_TYPE_INSTANT and PAGE_INSTANT may be
assigned even if instant ADD COLUMN was not
committed. Changes to these page header fields are not
undo-logged, but changes to the hidden metadata record
are. If the server is killed and restarted, the page
header fields could remain set even though no metadata
record is present. */
return DB_SUCCESS;
}
ib::error() << "Table " << index->table->name
<< " is missing instant ALTER metadata";
index->table->corrupted = true;
return DB_CORRUPTION;
}
if ((info_bits & ~REC_INFO_DELETED_FLAG) != REC_INFO_MIN_REC_FLAG
|| (comp && rec_get_status(rec) != REC_STATUS_INSTANT)) {
incompatible:
ib::error() << "Table " << index->table->name
<< " contains unrecognizable instant ALTER metadata";
index->table->corrupted = true;
return DB_CORRUPTION;
}
/* Read the metadata. We can get here on server restart
or when the table was evicted from the data dictionary cache
and is now being accessed again.
Here, READ COMMITTED and REPEATABLE READ should be equivalent.
Committing the ADD COLUMN operation would acquire
MDL_EXCLUSIVE and LOCK_X|LOCK_TABLE, which would prevent any
concurrent operations on the table, including table eviction
from the cache. */
if (info_bits & REC_INFO_DELETED_FLAG) {
/* This metadata record includes a BLOB that identifies
any dropped or reordered columns. */
ulint trx_id_offset = index->trx_id_offset;
/* If !index->trx_id_offset, the PRIMARY KEY contains
variable-length columns. For the metadata record,
variable-length columns should be written with zero
length. However, before MDEV-21088 was fixed, for
variable-length encoded PRIMARY KEY column of type
CHAR, we wrote more than zero bytes. That is why we
must determine the actual length of each PRIMARY KEY
column. The DB_TRX_ID will start right after any
PRIMARY KEY columns. */
ut_ad(index->n_uniq);
/* We cannot invoke rec_get_offsets() before
index->table->deserialise_columns(). Therefore,
we must duplicate some logic here. */
if (trx_id_offset) {
} else if (index->table->not_redundant()) {
/* The PRIMARY KEY contains variable-length columns.
For the metadata record, variable-length columns are
always written with zero length. The DB_TRX_ID will
start right after any fixed-length columns. */
/* OK, before MDEV-21088 was fixed, for
variable-length encoded PRIMARY KEY column of
type CHAR, we wrote more than zero bytes. In
order to allow affected tables to be accessed,
it would be nice to determine the actual
length of each PRIMARY KEY column. However, to
be able to do that, we should determine the
size of the null-bit bitmap in the metadata
record. And we cannot know that before reading
the metadata BLOB, whose starting point we are
trying to find here. (Although the PRIMARY KEY
columns cannot be NULL, we would have to know
where the lengths of variable-length PRIMARY KEY
columns start.)
So, unfortunately we cannot help users who
were affected by MDEV-21088 on a ROW_FORMAT=COMPACT
or ROW_FORMAT=DYNAMIC table. */
for (uint i = index->n_uniq; i--; ) {
trx_id_offset += index->fields[i].fixed_len;
}
} else if (rec_get_1byte_offs_flag(rec)) {
trx_id_offset = rec_1_get_field_end_info(
rec, index->n_uniq - 1);
ut_ad(!(trx_id_offset & REC_1BYTE_SQL_NULL_MASK));
trx_id_offset &= ~REC_1BYTE_SQL_NULL_MASK;
} else {
trx_id_offset = rec_2_get_field_end_info(
rec, index->n_uniq - 1);
ut_ad(!(trx_id_offset & REC_2BYTE_SQL_NULL_MASK));
trx_id_offset &= ~REC_2BYTE_SQL_NULL_MASK;
}
const byte* ptr = rec + trx_id_offset
+ (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
if (mach_read_from_4(ptr + BTR_EXTERN_LEN)) {
goto incompatible;
}
uint len = mach_read_from_4(ptr + BTR_EXTERN_LEN + 4);
if (!len
|| mach_read_from_4(ptr + BTR_EXTERN_OFFSET)
!= FIL_PAGE_DATA
|| mach_read_from_4(ptr + BTR_EXTERN_SPACE_ID)
!= space->id) {
goto incompatible;
}
buf_block_t* block = buf_page_get(
page_id_t(space->id,
mach_read_from_4(ptr + BTR_EXTERN_PAGE_NO)),
0, RW_S_LATCH, mtr);
if (!block) {
goto incompatible;
}
if (fil_page_get_type(block->page.frame) != FIL_PAGE_TYPE_BLOB
|| mach_read_from_4(&block->page.frame
[FIL_PAGE_DATA
+ BTR_BLOB_HDR_NEXT_PAGE_NO])
!= FIL_NULL
|| mach_read_from_4(&block->page.frame
[FIL_PAGE_DATA
+ BTR_BLOB_HDR_PART_LEN])
!= len) {
goto incompatible;
}
/* The unused part of the BLOB page should be zero-filled. */
for (const byte* b = block->page.frame
+ (FIL_PAGE_DATA + BTR_BLOB_HDR_SIZE) + len,
* const end = block->page.frame + srv_page_size
- BTR_EXTERN_LEN;
b < end; ) {
if (*b++) {
goto incompatible;
}
}
if (index->table->deserialise_columns(
&block->page.frame
[FIL_PAGE_DATA + BTR_BLOB_HDR_SIZE], len)) {
goto incompatible;
}
/* Proceed to initialize the default values of
any instantly added columns. */
}
mem_heap_t* heap = NULL;
rec_offs* offsets = rec_get_offsets(rec, index, NULL,
index->n_core_fields,
ULINT_UNDEFINED, &heap);
if (rec_offs_any_default(offsets)) {
inconsistent:
mem_heap_free(heap);
goto incompatible;
}
/* In fact, because we only ever append fields to the metadata
record, it is also OK to perform READ UNCOMMITTED and
then ignore any extra fields, provided that
trx_sys.is_registered(DB_TRX_ID). */
if (rec_offs_n_fields(offsets)
> ulint(index->n_fields) + !!index->table->instant
&& !trx_sys.is_registered(current_trx(),
row_get_rec_trx_id(rec, index,
offsets))) {
goto inconsistent;
}
for (unsigned i = index->n_core_fields; i < index->n_fields; i++) {
dict_col_t* col = index->fields[i].col;
const unsigned o = i + !!index->table->instant;
ulint len;
const byte* data = rec_get_nth_field(rec, offsets, o, &len);
ut_ad(!col->is_added());
ut_ad(!col->def_val.data);
col->def_val.len = len;
switch (len) {
case UNIV_SQL_NULL:
continue;
case 0:
col->def_val.data = field_ref_zero;
continue;
}
ut_ad(len != UNIV_SQL_DEFAULT);
if (!rec_offs_nth_extern(offsets, o)) {
col->def_val.data = mem_heap_dup(
index->table->heap, data, len);
} else if (len < BTR_EXTERN_FIELD_REF_SIZE
|| !memcmp(data + len - BTR_EXTERN_FIELD_REF_SIZE,
field_ref_zero,
BTR_EXTERN_FIELD_REF_SIZE)) {
col->def_val.len = UNIV_SQL_DEFAULT;
goto inconsistent;
} else {
col->def_val.data = btr_copy_externally_stored_field(
&col->def_val.len, data,
cur.page_cur.block->zip_size(),
len, index->table->heap);
}
}
mem_heap_free(heap);
return DB_SUCCESS;
}
/** Load the instant ALTER TABLE metadata from the clustered index
when loading a table definition.
@param[in,out] table table definition from the data dictionary
@return error code
@retval DB_SUCCESS if no error occurred */
dberr_t
btr_cur_instant_init(dict_table_t* table)
{
mtr_t mtr;
dict_index_t* index = dict_table_get_first_index(table);
mtr.start();
dberr_t err = index
? btr_cur_instant_init_low(index, &mtr)
: DB_CORRUPTION;
mtr.commit();
return(err);
}
/** Initialize the n_core_null_bytes on first access to a clustered
index root page.
@param[in] index clustered index that is on its first access
@param[in] page clustered index root page
@return whether the page is corrupted */
bool btr_cur_instant_root_init(dict_index_t* index, const page_t* page)
{
ut_ad(!index->is_dummy);
ut_ad(index->is_primary());
ut_ad(!index->is_instant());
ut_ad(index->table->supports_instant());
if (page_has_siblings(page)) {
return true;
}
/* This is normally executed as part of btr_cur_instant_init()
when dict_load_table_one() is loading a table definition.
Other threads should not access or modify the n_core_null_bytes,
n_core_fields before dict_load_table_one() returns.
This can also be executed during IMPORT TABLESPACE, where the
table definition is exclusively locked. */
switch (fil_page_get_type(page)) {
default:
return true;
case FIL_PAGE_INDEX:
/* The field PAGE_INSTANT is guaranteed 0 on clustered
index root pages of ROW_FORMAT=COMPACT or
ROW_FORMAT=DYNAMIC when instant ADD COLUMN is not used. */
if (page_is_comp(page) && page_get_instant(page)) {
return true;
}
index->n_core_null_bytes = static_cast<uint8_t>(
UT_BITS_IN_BYTES(unsigned(index->n_nullable)));
return false;
case FIL_PAGE_TYPE_INSTANT:
break;
}
const uint16_t n = page_get_instant(page);
if (n < index->n_uniq + DATA_ROLL_PTR) {
/* The PRIMARY KEY (or hidden DB_ROW_ID) and
DB_TRX_ID,DB_ROLL_PTR columns must always be present
as 'core' fields. */
return true;
}
if (n > REC_MAX_N_FIELDS) {
return true;
}
index->n_core_fields = n & dict_index_t::MAX_N_FIELDS;
const rec_t* infimum = page_get_infimum_rec(page);
const rec_t* supremum = page_get_supremum_rec(page);
if (!memcmp(infimum, "infimum", 8)
&& !memcmp(supremum, "supremum", 8)) {
if (n > index->n_fields) {
/* All fields, including those for instantly
added columns, must be present in the
data dictionary. */
return true;
}
ut_ad(!index->is_dummy);
ut_d(index->is_dummy = true);
index->n_core_null_bytes = static_cast<uint8_t>(
UT_BITS_IN_BYTES(index->get_n_nullable(n)));
ut_d(index->is_dummy = false);
return false;
}
if (memcmp(infimum, field_ref_zero, 8)
|| memcmp(supremum, field_ref_zero, 7)) {
/* The infimum and supremum records must either contain
the original strings, or they must be filled with zero
bytes, except for the bytes that we have repurposed. */
return true;
}
index->n_core_null_bytes = supremum[7];
return index->n_core_null_bytes > 128;
}
/** Optimistically latches the leaf page or pages requested.
@param[in] block guessed buffer block
@param[in] modify_clock modify clock value
@param[in,out] latch_mode BTR_SEARCH_LEAF, ...
@param[in,out] cursor cursor
@param[in] mtr mini-transaction
@return true if success */
TRANSACTIONAL_TARGET
bool
btr_cur_optimistic_latch_leaves(
buf_block_t* block,
ib_uint64_t modify_clock,
btr_latch_mode* latch_mode,
btr_cur_t* cursor,
mtr_t* mtr)
{
ut_ad(block->page.buf_fix_count());
ut_ad(block->page.in_file());
ut_ad(block->page.frame);
switch (*latch_mode) {
default:
MY_ASSERT_UNREACHABLE();
return(false);
case BTR_SEARCH_LEAF:
case BTR_MODIFY_LEAF:
return(buf_page_optimistic_get(*latch_mode, block,
modify_clock, mtr));
case BTR_SEARCH_PREV: /* btr_pcur_move_backward_from_page() */
case BTR_MODIFY_PREV: /* Ditto, or ibuf_insert() */
uint32_t curr_page_no, left_page_no;
{
transactional_shared_lock_guard<block_lock> g{
block->page.lock};
if (block->modify_clock != modify_clock) {
return false;
}
curr_page_no = block->page.id().page_no();
left_page_no = btr_page_get_prev(block->page.frame);
}
static_assert(BTR_SEARCH_PREV & BTR_SEARCH_LEAF, "");
static_assert(BTR_MODIFY_PREV & BTR_MODIFY_LEAF, "");
static_assert((BTR_SEARCH_PREV ^ BTR_MODIFY_PREV)
== (RW_S_LATCH ^ RW_X_LATCH), "");
const rw_lock_type_t mode = rw_lock_type_t(
*latch_mode & (RW_X_LATCH | RW_S_LATCH));
if (left_page_no != FIL_NULL) {
cursor->left_block = buf_page_get_gen(
page_id_t(cursor->index()->table->space_id,
left_page_no),
cursor->index()->table->space->zip_size(),
mode, nullptr, BUF_GET_POSSIBLY_FREED, mtr);
if (cursor->left_block
&& btr_page_get_next(
cursor->left_block->page.frame)
!= curr_page_no) {
release_left_block:
mtr->release_last_page();
return false;
}
} else {
cursor->left_block = nullptr;
}
if (buf_page_optimistic_get(mode, block, modify_clock, mtr)) {
if (btr_page_get_prev(block->page.frame)
== left_page_no) {
/* block was already buffer-fixed while
entering the function and
buf_page_optimistic_get() buffer-fixes
it again. */
ut_ad(2 <= block->page.buf_fix_count());
*latch_mode = btr_latch_mode(mode);
return(true);
}
mtr->release_last_page();
}
ut_ad(block->page.buf_fix_count());
if (cursor->left_block) {
goto release_left_block;
}
}
return false;
}
/**
Gets intention in btr_intention_t from latch_mode, and cleares the intention
at the latch_mode.
@param latch_mode in/out: pointer to latch_mode
@return intention for latching tree */
static
btr_intention_t btr_cur_get_and_clear_intention(btr_latch_mode *latch_mode)
{
btr_intention_t intention;
switch (*latch_mode & (BTR_LATCH_FOR_INSERT | BTR_LATCH_FOR_DELETE)) {
case BTR_LATCH_FOR_INSERT:
intention = BTR_INTENTION_INSERT;
break;
case BTR_LATCH_FOR_DELETE:
intention = BTR_INTENTION_DELETE;
break;
default:
/* both or unknown */
intention = BTR_INTENTION_BOTH;
}
*latch_mode = btr_latch_mode(
*latch_mode & ~(BTR_LATCH_FOR_INSERT | BTR_LATCH_FOR_DELETE));
return(intention);
}
/**
Gets the desired latch type for the root leaf (root page is root leaf)
at the latch mode.
@param latch_mode in: BTR_SEARCH_LEAF, ...
@return latch type */
static
rw_lock_type_t
btr_cur_latch_for_root_leaf(
ulint latch_mode)
{
switch (latch_mode) {
case BTR_SEARCH_LEAF:
case BTR_SEARCH_TREE:
case BTR_SEARCH_PREV:
return(RW_S_LATCH);
case BTR_MODIFY_LEAF:
case BTR_MODIFY_TREE:
case BTR_MODIFY_PREV:
return(RW_X_LATCH);
case BTR_CONT_MODIFY_TREE:
case BTR_CONT_SEARCH_TREE:
/* A root page should be latched already,
and don't need to be latched here.
fall through (RW_NO_LATCH) */
case BTR_NO_LATCHES:
return(RW_NO_LATCH);
}
MY_ASSERT_UNREACHABLE();
return(RW_NO_LATCH); /* avoid compiler warnings */
}
/** @return whether the distance between two records is at most the
specified value */
static bool
page_rec_distance_is_at_most(const rec_t *left, const rec_t *right, ulint val)
{
do
{
if (left == right)
return true;
left= page_rec_get_next_const(left);
}
while (left && val--);
return false;
}
/** Detects whether the modifying record might need a modifying tree structure.
@param[in] index index
@param[in] page page
@param[in] lock_intention lock intention for the tree operation
@param[in] rec record (current node_ptr)
@param[in] rec_size size of the record or max size of node_ptr
@param[in] zip_size ROW_FORMAT=COMPRESSED page size, or 0
@param[in] mtr mtr
@return true if tree modification is needed */
static
bool
btr_cur_will_modify_tree(
dict_index_t* index,
const page_t* page,
btr_intention_t lock_intention,
const rec_t* rec,
ulint rec_size,
ulint zip_size,
mtr_t* mtr)
{
ut_ad(!page_is_leaf(page));
ut_ad(mtr->memo_contains_flagged(&index->lock, MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
/* Pessimistic delete of the first record causes delete & insert
of node_ptr at upper level. And a subsequent page shrink is
possible. It causes delete of node_ptr at the upper level.
So we should pay attention also to 2nd record not only
first record and last record. Because if the "delete & insert" are
done for the different page, the 2nd record become
first record and following compress might delete the record and causes
the uppper level node_ptr modification. */
const ulint n_recs = page_get_n_recs(page);
if (lock_intention <= BTR_INTENTION_BOTH) {
compile_time_assert(BTR_INTENTION_DELETE < BTR_INTENTION_BOTH);
compile_time_assert(BTR_INTENTION_BOTH < BTR_INTENTION_INSERT);
if (!page_has_siblings(page)) {
return true;
}
ulint margin = rec_size;
if (lock_intention == BTR_INTENTION_BOTH) {
ulint level = btr_page_get_level(page);
/* This value is the worst expectation for the node_ptr
records to be deleted from this page. It is used to
expect whether the cursor position can be the left_most
record in this page or not. */
ulint max_nodes_deleted = 0;
/* By modifying tree operations from the under of this
level, logically (2 ^ (level - 1)) opportunities to
deleting records in maximum even unreally rare case. */
if (level > 7) {
/* TODO: adjust this practical limit. */
max_nodes_deleted = 64;
} else if (level > 0) {
max_nodes_deleted = (ulint)1 << (level - 1);
}
/* check delete will cause. (BTR_INTENTION_BOTH
or BTR_INTENTION_DELETE) */
if (n_recs <= max_nodes_deleted * 2
|| page_rec_is_first(rec, page)) {
/* The cursor record can be the left most record
in this page. */
return true;
}
if (page_has_prev(page)
&& page_rec_distance_is_at_most(
page_get_infimum_rec(page), rec,
max_nodes_deleted)) {
return true;
}
if (page_has_next(page)
&& page_rec_distance_is_at_most(
rec, page_get_supremum_rec(page),
max_nodes_deleted)) {
return true;
}
/* Delete at leftmost record in a page causes delete
& insert at its parent page. After that, the delete
might cause btr_compress() and delete record at its
parent page. Thus we should consider max deletes. */
margin *= max_nodes_deleted;
}
/* Safe because we already have SX latch of the index tree */
if (page_get_data_size(page)
< margin + BTR_CUR_PAGE_COMPRESS_LIMIT(index)) {
return(true);
}
}
if (lock_intention >= BTR_INTENTION_BOTH) {
/* check insert will cause. BTR_INTENTION_BOTH
or BTR_INTENTION_INSERT*/
/* Once we invoke the btr_cur_limit_optimistic_insert_debug,
we should check it here in advance, since the max allowable
records in a page is limited. */
LIMIT_OPTIMISTIC_INSERT_DEBUG(n_recs, return true);
/* needs 2 records' space for the case the single split and
insert cannot fit.
page_get_max_insert_size_after_reorganize() includes space
for page directory already */
ulint max_size
= page_get_max_insert_size_after_reorganize(page, 2);
if (max_size < BTR_CUR_PAGE_REORGANIZE_LIMIT + rec_size
|| max_size < rec_size * 2) {
return(true);
}
/* TODO: optimize this condition for ROW_FORMAT=COMPRESSED.
This is based on the worst case, and we could invoke
page_zip_available() on the block->page.zip. */
/* needs 2 records' space also for worst compress rate. */
if (zip_size
&& page_zip_empty_size(index->n_fields, zip_size)
<= rec_size * 2 + page_get_data_size(page)
+ page_dir_calc_reserved_space(n_recs + 2)) {
return(true);
}
}
return(false);
}
/** Detects whether the modifying record might need a opposite modification
to the intention.
@param[in] page page
@param[in] lock_intention lock intention for the tree operation
@param[in] rec record (current node_ptr)
@return true if tree modification is needed */
static
bool
btr_cur_need_opposite_intention(
const page_t* page,
btr_intention_t lock_intention,
const rec_t* rec)
{
switch (lock_intention) {
case BTR_INTENTION_DELETE:
return (page_has_prev(page) && page_rec_is_first(rec, page)) ||
(page_has_next(page) && page_rec_is_last(rec, page));
case BTR_INTENTION_INSERT:
return page_has_next(page) && page_rec_is_last(rec, page);
case BTR_INTENTION_BOTH:
return(false);
}
MY_ASSERT_UNREACHABLE();
return(false);
}
/**
@param[in] index b-tree
@return maximum size of a node pointer record in bytes */
static ulint btr_node_ptr_max_size(const dict_index_t* index)
{
if (dict_index_is_ibuf(index)) {
/* cannot estimate accurately */
/* This is universal index for change buffer.
The max size of the entry is about max key length * 2.
(index key + primary key to be inserted to the index)
(The max key length is UNIV_PAGE_SIZE / 16 * 3 at
ha_innobase::max_supported_key_length(),
considering MAX_KEY_LENGTH = 3072 at MySQL imposes
the 3500 historical InnoDB value for 16K page size case.)
For the universal index, node_ptr contains most of the entry.
And 512 is enough to contain ibuf columns and meta-data */
return srv_page_size / 8 * 3 + 512;
}
/* Each record has page_no, length of page_no and header. */
ulint comp = dict_table_is_comp(index->table);
ulint rec_max_size = comp
? REC_NODE_PTR_SIZE + 1 + REC_N_NEW_EXTRA_BYTES
+ UT_BITS_IN_BYTES(index->n_nullable)
: REC_NODE_PTR_SIZE + 2 + REC_N_OLD_EXTRA_BYTES
+ 2 * index->n_fields;
/* Compute the maximum possible record size. */
for (ulint i = 0; i < dict_index_get_n_unique_in_tree(index); i++) {
const dict_field_t* field
= dict_index_get_nth_field(index, i);
const dict_col_t* col
= dict_field_get_col(field);
ulint field_max_size;
ulint field_ext_max_size;
/* Determine the maximum length of the index field. */
field_max_size = dict_col_get_fixed_size(col, comp);
if (field_max_size) {
/* dict_index_add_col() should guarantee this */
ut_ad(!field->prefix_len
|| field->fixed_len == field->prefix_len);
/* Fixed lengths are not encoded
in ROW_FORMAT=COMPACT. */
rec_max_size += field_max_size;
continue;
}
field_max_size = dict_col_get_max_size(col);
if (UNIV_UNLIKELY(!field_max_size)) {
switch (col->mtype) {
case DATA_VARCHAR:
if (!comp
&& (!strcmp(index->table->name.m_name,
"SYS_FOREIGN")
|| !strcmp(index->table->name.m_name,
"SYS_FOREIGN_COLS"))) {
break;
}
/* fall through */
case DATA_VARMYSQL:
case DATA_CHAR:
case DATA_MYSQL:
/* CHAR(0) and VARCHAR(0) are possible
data type definitions in MariaDB.
The InnoDB internal SQL parser maps
CHAR to DATA_VARCHAR, so DATA_CHAR (or
DATA_MYSQL) is only coming from the
MariaDB SQL layer. */
if (comp) {
/* Add a length byte, because
fixed-length empty field are
encoded as variable-length.
For ROW_FORMAT=REDUNDANT,
these bytes were added to
rec_max_size before this loop. */
rec_max_size++;
}
continue;
}
/* SYS_FOREIGN.ID is defined as CHAR in the
InnoDB internal SQL parser, which translates
into the incorrect VARCHAR(0). InnoDB does
not enforce maximum lengths of columns, so
that is why any data can be inserted in the
first place.
Likewise, SYS_FOREIGN.FOR_NAME,
SYS_FOREIGN.REF_NAME, SYS_FOREIGN_COLS.ID, are
defined as CHAR, and also they are part of a key. */
ut_ad(!strcmp(index->table->name.m_name,
"SYS_FOREIGN")
|| !strcmp(index->table->name.m_name,
"SYS_FOREIGN_COLS"));
ut_ad(!comp);
ut_ad(col->mtype == DATA_VARCHAR);
rec_max_size += (srv_page_size == UNIV_PAGE_SIZE_MAX)
? REDUNDANT_REC_MAX_DATA_SIZE
: page_get_free_space_of_empty(FALSE) / 2;
} else if (field_max_size == NAME_LEN && i == 1
&& (!strcmp(index->table->name.m_name,
TABLE_STATS_NAME)
|| !strcmp(index->table->name.m_name,
INDEX_STATS_NAME))) {
/* Interpret "table_name" as VARCHAR(199) even
if it was incorrectly defined as VARCHAR(64).
While the caller of ha_innobase enforces the
maximum length on any data written, the InnoDB
internal SQL parser will happily write as much
data as is provided. The purpose of this hack
is to avoid InnoDB hangs after persistent
statistics on partitioned tables are
deleted. */
field_max_size = 199 * SYSTEM_CHARSET_MBMAXLEN;
}
field_ext_max_size = field_max_size < 256 ? 1 : 2;
if (field->prefix_len
&& field->prefix_len < field_max_size) {
field_max_size = field->prefix_len;
}
if (comp) {
/* Add the extra size for ROW_FORMAT=COMPACT.
For ROW_FORMAT=REDUNDANT, these bytes were
added to rec_max_size before this loop. */
rec_max_size += field_ext_max_size;
}
rec_max_size += field_max_size;
}
return rec_max_size;
}
/********************************************************************//**
Searches an index tree and positions a tree cursor on a given level.
NOTE: n_fields_cmp in tuple must be set so that it cannot be compared
to node pointer page number fields on the upper levels of the tree!
Note that if mode is PAGE_CUR_LE, which is used in inserts, then
cursor->up_match and cursor->low_match both will have sensible values.
If mode is PAGE_CUR_GE, then up_match will a have a sensible value.
If mode is PAGE_CUR_LE , cursor is left at the place where an insert of the
search tuple should be performed in the B-tree. InnoDB does an insert
immediately after the cursor. Thus, the cursor may end up on a user record,
or on a page infimum record.
@param level the tree level of search
@param tuple data tuple; NOTE: n_fields_cmp in tuple must be set so that
it cannot get compared to the node ptr page number field!
@param mode PAGE_CUR_L, ...; NOTE that if the search is made using a
unique prefix of a record, mode should be PAGE_CUR_LE, not
PAGE_CUR_GE, as the latter may end up on the previous page of
the record! Inserts should always be made using PAGE_CUR_LE
to search the position!
@param latch_mode BTR_SEARCH_LEAF, ..., ORed with at most one of BTR_INSERT,
BTR_DELETE_MARK, or BTR_DELETE;
cursor->left_block is used to store a pointer to the left
neighbor page
@param cursor tree cursor; the cursor page is s- or x-latched, but see also
above!
@param mtr mini-transaction
@param autoinc PAGE_ROOT_AUTO_INC to be written (0 if none)
@return DB_SUCCESS on success or error code otherwise */
TRANSACTIONAL_TARGET
dberr_t btr_cur_search_to_nth_level(ulint level,
const dtuple_t *tuple,
page_cur_mode_t mode,
btr_latch_mode latch_mode,
btr_cur_t *cursor, mtr_t *mtr,
ib_uint64_t autoinc)
{
page_t* page = NULL; /* remove warning */
buf_block_t* block;
buf_block_t* guess;
ulint height;
ulint up_match;
ulint up_bytes;
ulint low_match;
ulint low_bytes;
ulint rw_latch;
page_cur_mode_t page_mode;
page_cur_mode_t search_mode = PAGE_CUR_UNSUPP;
ulint buf_mode;
ulint node_ptr_max_size = srv_page_size / 2;
page_cur_t* page_cursor;
btr_op_t btr_op;
ulint root_height = 0; /* remove warning */
btr_intention_t lock_intention;
buf_block_t* tree_blocks[BTR_MAX_LEVELS];
ulint tree_savepoints[BTR_MAX_LEVELS];
ulint n_blocks = 0;
ulint n_releases = 0;
bool detected_same_key_root = false;
ulint leftmost_from_level = 0;
buf_block_t** prev_tree_blocks = NULL;
ulint* prev_tree_savepoints = NULL;
ulint prev_n_blocks = 0;
ulint prev_n_releases = 0;
bool need_path = true;
bool rtree_parent_modified = false;
bool mbr_adj = false;
bool found = false;
dict_index_t * const index = cursor->index();
DBUG_ENTER("btr_cur_search_to_nth_level");
#ifdef BTR_CUR_ADAPT
btr_search_t* info;
#endif /* BTR_CUR_ADAPT */
mem_heap_t* heap = NULL;
rec_offs offsets_[REC_OFFS_NORMAL_SIZE];
rec_offs* offsets = offsets_;
rec_offs offsets2_[REC_OFFS_NORMAL_SIZE];
rec_offs* offsets2 = offsets2_;
rec_offs_init(offsets_);
rec_offs_init(offsets2_);
/* Currently, PAGE_CUR_LE is the only search mode used for searches
ending to upper levels */
ut_ad(level == 0 || mode == PAGE_CUR_LE
|| RTREE_SEARCH_MODE(mode));
ut_ad(dict_index_check_search_tuple(index, tuple));
ut_ad(!dict_index_is_ibuf(index) || ibuf_inside(mtr));
ut_ad(dtuple_check_typed(tuple));
ut_ad(!(index->type & DICT_FTS));
ut_ad(index->page != FIL_NULL);
MEM_UNDEFINED(&cursor->up_match, sizeof cursor->up_match);
MEM_UNDEFINED(&cursor->up_bytes, sizeof cursor->up_bytes);
MEM_UNDEFINED(&cursor->low_match, sizeof cursor->low_match);
MEM_UNDEFINED(&cursor->low_bytes, sizeof cursor->low_bytes);
#ifdef UNIV_DEBUG
cursor->up_match = ULINT_UNDEFINED;
cursor->low_match = ULINT_UNDEFINED;
#endif /* UNIV_DEBUG */
const bool latch_by_caller = latch_mode & BTR_ALREADY_S_LATCHED;
ut_ad(!latch_by_caller
|| srv_read_only_mode
|| mtr->memo_contains_flagged(&index->lock, MTR_MEMO_S_LOCK
| MTR_MEMO_SX_LOCK));
/* These flags are mutually exclusive, they are lumped together
with the latch mode for historical reasons. It's possible for
none of the flags to be set. */
switch (UNIV_EXPECT(latch_mode & BTR_DELETE, 0)) {
default:
btr_op = BTR_NO_OP;
break;
case BTR_INSERT:
btr_op = (latch_mode & BTR_IGNORE_SEC_UNIQUE)
? BTR_INSERT_IGNORE_UNIQUE_OP
: BTR_INSERT_OP;
break;
case BTR_DELETE:
btr_op = BTR_DELETE_OP;
ut_a(cursor->purge_node);
break;
case BTR_DELETE_MARK:
btr_op = BTR_DELMARK_OP;
break;
}
/* Operations on the insert buffer tree cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !dict_index_is_ibuf(index));
/* Operations on the clustered index cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !dict_index_is_clust(index));
/* Operations on the temporary table(indexes) cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !index->table->is_temporary());
/* Operation on the spatial index cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !dict_index_is_spatial(index));
lock_intention = btr_cur_get_and_clear_intention(&latch_mode);
/* Turn the flags unrelated to the latch mode off. */
latch_mode = BTR_LATCH_MODE_WITHOUT_FLAGS(latch_mode);
ut_ad(!latch_by_caller
|| latch_mode == BTR_SEARCH_LEAF
|| latch_mode == BTR_SEARCH_TREE
|| latch_mode == BTR_MODIFY_LEAF);
ut_ad(autoinc == 0 || dict_index_is_clust(index));
ut_ad(autoinc == 0
|| latch_mode == BTR_MODIFY_TREE
|| latch_mode == BTR_MODIFY_LEAF);
ut_ad(autoinc == 0 || level == 0);
cursor->flag = BTR_CUR_BINARY;
#ifndef BTR_CUR_ADAPT
guess = NULL;
#else
info = btr_search_get_info(index);
guess = info->root_guess;
#ifdef BTR_CUR_HASH_ADAPT
# ifdef UNIV_SEARCH_PERF_STAT
info->n_searches++;
# endif
/* We do a dirty read of btr_search_enabled below,
and btr_search_guess_on_hash() will have to check it again. */
if (!btr_search_enabled) {
} else if (autoinc == 0
&& latch_mode <= BTR_MODIFY_LEAF
# ifdef PAGE_CUR_LE_OR_EXTENDS
&& mode != PAGE_CUR_LE_OR_EXTENDS
# endif /* PAGE_CUR_LE_OR_EXTENDS */
&& info->last_hash_succ
&& !(tuple->info_bits & REC_INFO_MIN_REC_FLAG)
&& !index->is_spatial() && !index->table->is_temporary()
&& btr_search_guess_on_hash(index, info, tuple, mode,
latch_mode, cursor, mtr)) {
/* Search using the hash index succeeded */
ut_ad(cursor->up_match != ULINT_UNDEFINED
|| mode != PAGE_CUR_GE);
ut_ad(cursor->up_match != ULINT_UNDEFINED
|| mode != PAGE_CUR_LE);
ut_ad(cursor->low_match != ULINT_UNDEFINED
|| mode != PAGE_CUR_LE);
++btr_cur_n_sea;
DBUG_RETURN(DB_SUCCESS);
} else {
++btr_cur_n_non_sea;
}
# endif /* BTR_CUR_HASH_ADAPT */
#endif /* BTR_CUR_ADAPT */
/* If the hash search did not succeed, do binary search down the
tree */
/* Store the position of the tree latch we push to mtr so that we
know how to release it when we have latched leaf node(s) */
ulint savepoint = mtr_set_savepoint(mtr);
rw_lock_type_t upper_rw_latch;
switch (latch_mode) {
case BTR_MODIFY_TREE:
/* Most of delete-intended operations are purging.
Free blocks and read IO bandwidth should be prior
for them, when the history list is glowing huge. */
if (lock_intention == BTR_INTENTION_DELETE
&& buf_pool.n_pend_reads
&& trx_sys.history_size_approx()
> BTR_CUR_FINE_HISTORY_LENGTH) {
x_latch_index:
mtr_x_lock_index(index, mtr);
} else if (index->is_spatial()
&& lock_intention <= BTR_INTENTION_BOTH) {
/* X lock the if there is possibility of
pessimistic delete on spatial index. As we could
lock upward for the tree */
goto x_latch_index;
} else {
mtr_sx_lock_index(index, mtr);
}
upper_rw_latch = RW_X_LATCH;
break;
case BTR_CONT_MODIFY_TREE:
ut_ad(srv_read_only_mode
|| mtr->memo_contains_flagged(&index->lock,
MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
if (index->is_spatial()) {
/* If we are about to locate parent page for split
and/or merge operation for R-Tree index, X latch
the parent */
upper_rw_latch = RW_X_LATCH;
break;
}
/* fall through */
case BTR_CONT_SEARCH_TREE:
/* Do nothing */
ut_ad(srv_read_only_mode
|| mtr->memo_contains_flagged(&index->lock,
MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
upper_rw_latch = RW_NO_LATCH;
break;
default:
if (!srv_read_only_mode) {
if (!latch_by_caller) {
ut_ad(latch_mode != BTR_SEARCH_TREE);
mtr_s_lock_index(index, mtr);
}
upper_rw_latch = RW_S_LATCH;
} else {
upper_rw_latch = RW_NO_LATCH;
}
}
const rw_lock_type_t root_leaf_rw_latch = btr_cur_latch_for_root_leaf(
latch_mode);
page_cursor = btr_cur_get_page_cur(cursor);
page_cursor->index = index;
const ulint zip_size = index->table->space->zip_size();
/* Start with the root page. */
page_id_t page_id(index->table->space_id, index->page);
if (root_leaf_rw_latch == RW_X_LATCH) {
node_ptr_max_size = btr_node_ptr_max_size(index);
}
up_match = 0;
up_bytes = 0;
low_match = 0;
low_bytes = 0;
height = ULINT_UNDEFINED;
/* We use these modified search modes on non-leaf levels of the
B-tree. These let us end up in the right B-tree leaf. In that leaf
we use the original search mode. */
switch (mode) {
case PAGE_CUR_GE:
page_mode = PAGE_CUR_L;
break;
case PAGE_CUR_G:
page_mode = PAGE_CUR_LE;
break;
default:
#ifdef PAGE_CUR_LE_OR_EXTENDS
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE
|| RTREE_SEARCH_MODE(mode)
|| mode == PAGE_CUR_LE_OR_EXTENDS);
#else /* PAGE_CUR_LE_OR_EXTENDS */
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE
|| RTREE_SEARCH_MODE(mode));
#endif /* PAGE_CUR_LE_OR_EXTENDS */
page_mode = mode;
break;
}
/* Loop and search until we arrive at the desired level */
btr_latch_leaves_t latch_leaves = {{NULL, NULL, NULL}, {0, 0, 0}};
search_loop:
buf_mode = BUF_GET;
rw_latch = RW_NO_LATCH;
rtree_parent_modified = false;
if (height != 0) {
/* We are about to fetch the root or a non-leaf page. */
if ((latch_mode != BTR_MODIFY_TREE || height == level)
&& !prev_tree_blocks) {
/* If doesn't have SX or X latch of index,
each pages should be latched before reading. */
if (height == ULINT_UNDEFINED
&& upper_rw_latch == RW_S_LATCH
&& autoinc) {
/* needs sx-latch of root page
for writing PAGE_ROOT_AUTO_INC */
rw_latch = RW_SX_LATCH;
} else {
rw_latch = upper_rw_latch;
}
}
} else if (latch_mode <= BTR_MODIFY_LEAF) {
rw_latch = latch_mode;
if (btr_op != BTR_NO_OP
&& ibuf_should_try(index, btr_op != BTR_INSERT_OP)) {
/* Try to buffer the operation if the leaf
page is not in the buffer pool. */
buf_mode = btr_op == BTR_DELETE_OP
? BUF_GET_IF_IN_POOL_OR_WATCH
: BUF_GET_IF_IN_POOL;
}
}
retry_page_get:
ut_ad(n_blocks < BTR_MAX_LEVELS);
tree_savepoints[n_blocks] = mtr_set_savepoint(mtr);
dberr_t err;
block = buf_page_get_gen(page_id, zip_size, rw_latch, guess,
buf_mode, mtr, &err,
height == 0 && !index->is_clust());
if (!block) {
switch (err) {
case DB_SUCCESS:
/* change buffering */
break;
case DB_DECRYPTION_FAILED:
btr_decryption_failed(*index);
/* fall through */
default:
goto func_exit;
}
/* This must be a search to perform an insert/delete
mark/ delete; try using the insert/delete buffer */
ut_ad(height == 0);
ut_ad(cursor->thr);
switch (btr_op) {
default:
MY_ASSERT_UNREACHABLE();
break;
case BTR_INSERT_OP:
case BTR_INSERT_IGNORE_UNIQUE_OP:
ut_ad(buf_mode == BUF_GET_IF_IN_POOL);
ut_ad(!dict_index_is_spatial(index));
if (ibuf_insert(IBUF_OP_INSERT, tuple, index,
page_id, zip_size, cursor->thr)) {
cursor->flag = BTR_CUR_INSERT_TO_IBUF;
goto func_exit;
}
break;
case BTR_DELMARK_OP:
ut_ad(buf_mode == BUF_GET_IF_IN_POOL);
ut_ad(!dict_index_is_spatial(index));
if (ibuf_insert(IBUF_OP_DELETE_MARK, tuple,
index, page_id, zip_size,
cursor->thr)) {
cursor->flag = BTR_CUR_DEL_MARK_IBUF;
goto func_exit;
}
break;
case BTR_DELETE_OP:
ut_ad(buf_mode == BUF_GET_IF_IN_POOL_OR_WATCH);
ut_ad(!dict_index_is_spatial(index));
auto& chain = buf_pool.page_hash.cell_get(
page_id.fold());
if (!row_purge_poss_sec(cursor->purge_node,
index, tuple)) {
/* The record cannot be purged yet. */
cursor->flag = BTR_CUR_DELETE_REF;
} else if (ibuf_insert(IBUF_OP_DELETE, tuple,
index, page_id, zip_size,
cursor->thr)) {
/* The purge was buffered. */
cursor->flag = BTR_CUR_DELETE_IBUF;
} else {
/* The purge could not be buffered. */
buf_pool.watch_unset(page_id, chain);
break;
}
buf_pool.watch_unset(page_id, chain);
goto func_exit;
}
/* Insert to the insert/delete buffer did not succeed, we
must read the page from disk. */
buf_mode = BUF_GET;
goto retry_page_get;
}
tree_blocks[n_blocks] = block;
if (height && prev_tree_blocks) {
/* also latch left sibling */
ut_ad(rw_latch == RW_NO_LATCH);
rw_latch = upper_rw_latch;
/* Because we are holding index->lock, no page splits
or merges may run concurrently, and we may read
FIL_PAGE_PREV from a buffer-fixed, unlatched page. */
uint32_t left_page_no = btr_page_get_prev(block->page.frame);
if (left_page_no != FIL_NULL) {
ut_ad(prev_n_blocks < leftmost_from_level);
prev_tree_savepoints[prev_n_blocks]
= mtr_set_savepoint(mtr);
buf_block_t* get_block = buf_page_get_gen(
page_id_t(page_id.space(), left_page_no),
zip_size, rw_latch, NULL, buf_mode,
mtr, &err);
if (!get_block) {
if (err == DB_DECRYPTION_FAILED) {
btr_decryption_failed(*index);
}
goto func_exit;
}
prev_tree_blocks[prev_n_blocks++] = get_block;
/* BTR_MODIFY_TREE doesn't update prev/next_page_no,
without their parent page's lock. So, not needed to
retry here, because we have the parent page's lock. */
}
mtr->s_lock_register(tree_savepoints[n_blocks]);
block->page.lock.s_lock();
}
page = buf_block_get_frame(block);
if (height == ULINT_UNDEFINED
&& page_is_leaf(page)
&& rw_latch != RW_NO_LATCH
&& rw_latch != root_leaf_rw_latch) {
/* The root page is also a leaf page (root_leaf).
We should reacquire the page, because the root page
is latched differently from leaf pages. */
ut_ad(root_leaf_rw_latch != RW_NO_LATCH);
ut_ad(rw_latch == RW_S_LATCH || rw_latch == RW_SX_LATCH);
ut_ad(rw_latch == RW_S_LATCH || autoinc);
ut_ad(!autoinc || root_leaf_rw_latch == RW_X_LATCH);
ut_ad(n_blocks == 0);
mtr_release_block_at_savepoint(
mtr, tree_savepoints[n_blocks],
tree_blocks[n_blocks]);
upper_rw_latch = root_leaf_rw_latch;
goto search_loop;
}
#ifdef UNIV_ZIP_DEBUG
if (rw_latch != RW_NO_LATCH) {
const page_zip_des_t* page_zip
= buf_block_get_page_zip(block);
ut_a(!page_zip || page_zip_validate(page_zip, page, index));
}
#endif /* UNIV_ZIP_DEBUG */
ut_ad(fil_page_index_page_check(page));
ut_ad(index->id == btr_page_get_index_id(page));
if (height == ULINT_UNDEFINED) {
/* We are in the root node */
height = btr_page_get_level(page);
root_height = height;
cursor->tree_height = root_height + 1;
if (dict_index_is_spatial(index)) {
ut_ad(cursor->rtr_info);
/* If SSN in memory is not initialized, fetch
it from root page */
if (!rtr_get_current_ssn_id(index)) {
/* FIXME: do this in dict_load_table_one() */
index->set_ssn(page_get_ssn_id(page) + 1);
}
/* Save the MBR */
cursor->rtr_info->thr = cursor->thr;
rtr_get_mbr_from_tuple(tuple, &cursor->rtr_info->mbr);
}
#ifdef BTR_CUR_ADAPT
info->root_guess = block;
#endif
}
if (height == 0) {
if (rw_latch == RW_NO_LATCH) {
btr_cur_latch_leaves(block, latch_mode, cursor, mtr,
&latch_leaves);
}
switch (latch_mode) {
case BTR_MODIFY_TREE:
case BTR_CONT_MODIFY_TREE:
case BTR_CONT_SEARCH_TREE:
break;
default:
if (!latch_by_caller
&& !srv_read_only_mode) {
/* Release the tree s-latch */
mtr_release_s_latch_at_savepoint(
mtr, savepoint,
&index->lock);
}
/* release upper blocks */
if (prev_tree_blocks) {
ut_ad(!autoinc);
for (;
prev_n_releases < prev_n_blocks;
prev_n_releases++) {
mtr_release_block_at_savepoint(
mtr,
prev_tree_savepoints[
prev_n_releases],
prev_tree_blocks[
prev_n_releases]);
}
}
for (; n_releases < n_blocks; n_releases++) {
if (n_releases == 0
&& (autoinc)) {
/* keep the root page latch */
ut_ad(mtr->memo_contains_flagged(
tree_blocks[n_releases],
MTR_MEMO_PAGE_SX_FIX
| MTR_MEMO_PAGE_X_FIX));
continue;
}
mtr_release_block_at_savepoint(
mtr, tree_savepoints[n_releases],
tree_blocks[n_releases]);
}
}
page_mode = mode;
}
if (dict_index_is_spatial(index)) {
/* Remember the page search mode */
search_mode = page_mode;
/* Some adjustment on search mode, when the
page search mode is PAGE_CUR_RTREE_LOCATE
or PAGE_CUR_RTREE_INSERT, as we are searching
with MBRs. When it is not the target level, we
should search all sub-trees that "CONTAIN" the
search range/MBR. When it is at the target
level, the search becomes PAGE_CUR_LE */
if (page_mode == PAGE_CUR_RTREE_LOCATE
&& level == height) {
if (level == 0) {
page_mode = PAGE_CUR_LE;
} else {
page_mode = PAGE_CUR_RTREE_GET_FATHER;
}
}
if (page_mode == PAGE_CUR_RTREE_INSERT) {
page_mode = (level == height)
? PAGE_CUR_LE
: PAGE_CUR_RTREE_INSERT;
ut_ad(!page_is_leaf(page) || page_mode == PAGE_CUR_LE);
}
/* "need_path" indicates if we need to tracking the parent
pages, if it is not spatial comparison, then no need to
track it */
if (page_mode < PAGE_CUR_CONTAIN) {
need_path = false;
}
up_match = 0;
low_match = 0;
if (latch_mode == BTR_MODIFY_TREE
|| latch_mode == BTR_CONT_MODIFY_TREE
|| latch_mode == BTR_CONT_SEARCH_TREE) {
/* Tree are locked, no need for Page Lock to protect
the "path" */
cursor->rtr_info->need_page_lock = false;
}
}
page_cursor->block = block;
if (dict_index_is_spatial(index) && page_mode >= PAGE_CUR_CONTAIN) {
ut_ad(need_path);
found = rtr_cur_search_with_match(
block, index, tuple, page_mode, page_cursor,
cursor->rtr_info);
/* Need to use BTR_MODIFY_TREE to do the MBR adjustment */
if (search_mode == PAGE_CUR_RTREE_INSERT
&& cursor->rtr_info->mbr_adj) {
static_assert(BTR_MODIFY_TREE
== (8 | BTR_MODIFY_LEAF), "");
if (!(latch_mode & 8)) {
/* Parent MBR needs updated, should retry
with BTR_MODIFY_TREE */
goto func_exit;
}
rtree_parent_modified = true;
cursor->rtr_info->mbr_adj = false;
mbr_adj = true;
}
if (found && page_mode == PAGE_CUR_RTREE_GET_FATHER) {
cursor->low_match =
DICT_INDEX_SPATIAL_NODEPTR_SIZE + 1;
}
#ifdef BTR_CUR_HASH_ADAPT
} else if (height == 0 && btr_search_enabled
&& !(tuple->info_bits & REC_INFO_MIN_REC_FLAG)
&& index->is_btree()) {
/* The adaptive hash index is only used when searching
for leaf pages (height==0), but not in r-trees.
We only need the byte prefix comparison for the purpose
of updating the adaptive hash index. */
if (page_cur_search_with_match_bytes(
tuple, page_mode, &up_match, &up_bytes,
&low_match, &low_bytes, page_cursor)) {
err = DB_CORRUPTION;
goto func_exit;
}
#endif /* BTR_CUR_HASH_ADAPT */
} else {
/* Search for complete index fields. */
up_bytes = low_bytes = 0;
if (page_cur_search_with_match(
tuple, page_mode, &up_match,
&low_match, page_cursor,
need_path ? cursor->rtr_info : nullptr)) {
err = DB_CORRUPTION;
goto func_exit;
}
}
/* If this is the desired level, leave the loop */
ut_ad(height == btr_page_get_level(page_cur_get_page(page_cursor)));
/* Add Predicate lock if it is serializable isolation
and only if it is in the search case */
if (dict_index_is_spatial(index)
&& cursor->rtr_info->need_prdt_lock
&& mode != PAGE_CUR_RTREE_INSERT
&& mode != PAGE_CUR_RTREE_LOCATE
&& mode >= PAGE_CUR_CONTAIN) {
lock_prdt_t prdt;
{
trx_t* trx = thr_get_trx(cursor->thr);
TMLockTrxGuard g{TMLockTrxArgs(*trx)};
lock_init_prdt_from_mbr(
&prdt, &cursor->rtr_info->mbr, mode,
trx->lock.lock_heap);
}
if (rw_latch == RW_NO_LATCH && height != 0) {
block->page.lock.s_lock();
}
lock_prdt_lock(block, &prdt, index, LOCK_S,
LOCK_PREDICATE, cursor->thr);
if (rw_latch == RW_NO_LATCH && height != 0) {
block->page.lock.s_unlock();
}
}
if (level != height) {
const rec_t* node_ptr;
ut_ad(height > 0);
height--;
guess = NULL;
node_ptr = page_cur_get_rec(page_cursor);
offsets = rec_get_offsets(node_ptr, index, offsets, 0,
ULINT_UNDEFINED, &heap);
/* If the rec is the first or last in the page for
pessimistic delete intention, it might cause node_ptr insert
for the upper level. We should change the intention and retry.
*/
if (latch_mode == BTR_MODIFY_TREE
&& btr_cur_need_opposite_intention(
page, lock_intention, node_ptr)) {
need_opposite_intention:
ut_ad(upper_rw_latch == RW_X_LATCH);
if (n_releases > 0) {
/* release root block */
mtr_release_block_at_savepoint(
mtr, tree_savepoints[0],
tree_blocks[0]);
}
/* release all blocks */
for (; n_releases <= n_blocks; n_releases++) {
mtr_release_block_at_savepoint(
mtr, tree_savepoints[n_releases],
tree_blocks[n_releases]);
}
lock_intention = BTR_INTENTION_BOTH;
page_id.set_page_no(index->page);
up_match = 0;
low_match = 0;
height = ULINT_UNDEFINED;
n_blocks = 0;
n_releases = 0;
goto search_loop;
}
if (dict_index_is_spatial(index)) {
if (page_rec_is_supremum(node_ptr)) {
cursor->low_match = 0;
cursor->up_match = 0;
goto func_exit;
}
/* If we are doing insertion or record locating,
remember the tree nodes we visited */
if (page_mode == PAGE_CUR_RTREE_INSERT
|| (search_mode == PAGE_CUR_RTREE_LOCATE
&& (latch_mode != BTR_MODIFY_LEAF))) {
bool add_latch = false;
if (latch_mode == BTR_MODIFY_TREE
&& rw_latch == RW_NO_LATCH) {
ut_ad(mtr->memo_contains_flagged(
&index->lock, MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
block->page.lock.s_lock();
add_latch = true;
}
/* Store the parent cursor location */
#ifdef UNIV_DEBUG
ulint num_stored = rtr_store_parent_path(
block, cursor, latch_mode,
height + 1, mtr);
#else
rtr_store_parent_path(
block, cursor, latch_mode,
height + 1, mtr);
#endif
if (page_mode == PAGE_CUR_RTREE_INSERT) {
btr_pcur_t* r_cursor =
rtr_get_parent_cursor(
cursor, height + 1,
true);
/* If it is insertion, there should
be only one parent for each level
traverse */
#ifdef UNIV_DEBUG
ut_ad(num_stored == 1);
#endif
node_ptr = btr_pcur_get_rec(r_cursor);
}
if (add_latch) {
block->page.lock.s_unlock();
}
ut_ad(!page_rec_is_supremum(node_ptr));
}
ut_ad(page_mode == search_mode
|| (page_mode == PAGE_CUR_WITHIN
&& search_mode == PAGE_CUR_RTREE_LOCATE));
page_mode = search_mode;
}
/* If the first or the last record of the page
or the same key value to the first record or last record,
the another page might be chosen when BTR_CONT_MODIFY_TREE.
So, the parent page should not released to avoiding deadlock
with blocking the another search with the same key value. */
if (!detected_same_key_root
&& lock_intention == BTR_INTENTION_BOTH
&& !dict_index_is_unique(index)
&& latch_mode == BTR_MODIFY_TREE
&& (up_match >= rec_offs_n_fields(offsets) - 1
|| low_match >= rec_offs_n_fields(offsets) - 1)) {
const rec_t* first_rec = page_rec_get_next_const(
page_get_infimum_rec(page));
ulint matched_fields;
ut_ad(upper_rw_latch == RW_X_LATCH);
if (UNIV_UNLIKELY(!first_rec)) {
corrupted:
err = DB_CORRUPTION;
goto func_exit;
}
if (node_ptr == first_rec
|| page_rec_is_last(node_ptr, page)) {
detected_same_key_root = true;
} else {
matched_fields = 0;
offsets2 = rec_get_offsets(
first_rec, index, offsets2,
0, ULINT_UNDEFINED, &heap);
cmp_rec_rec(node_ptr, first_rec,
offsets, offsets2, index, false,
&matched_fields);
if (matched_fields
>= rec_offs_n_fields(offsets) - 1) {
detected_same_key_root = true;
} else if (const rec_t* last_rec
= page_rec_get_prev_const(
page_get_supremum_rec(
page))) {
matched_fields = 0;
offsets2 = rec_get_offsets(
last_rec, index, offsets2,
0, ULINT_UNDEFINED, &heap);
cmp_rec_rec(
node_ptr, last_rec,
offsets, offsets2, index,
false, &matched_fields);
if (matched_fields
>= rec_offs_n_fields(offsets) - 1) {
detected_same_key_root = true;
}
} else {
goto corrupted;
}
}
}
/* If the page might cause modify_tree,
we should not release the parent page's lock. */
if (!detected_same_key_root
&& latch_mode == BTR_MODIFY_TREE
&& !btr_cur_will_modify_tree(
index, page, lock_intention, node_ptr,
node_ptr_max_size, zip_size, mtr)
&& !rtree_parent_modified) {
ut_ad(upper_rw_latch == RW_X_LATCH);
ut_ad(n_releases <= n_blocks);
/* we can release upper blocks */
for (; n_releases < n_blocks; n_releases++) {
if (n_releases == 0) {
/* we should not release root page
to pin to same block. */
continue;
}
/* release unused blocks to unpin */
mtr_release_block_at_savepoint(
mtr, tree_savepoints[n_releases],
tree_blocks[n_releases]);
}
}
if (height == level
&& latch_mode == BTR_MODIFY_TREE) {
ut_ad(upper_rw_latch == RW_X_LATCH);
/* we should sx-latch root page, if released already.
It contains seg_header. */
if (n_releases > 0) {
mtr->sx_latch_at_savepoint(
tree_savepoints[0],
tree_blocks[0]);
}
/* x-latch the branch blocks not released yet. */
for (ulint i = n_releases; i <= n_blocks; i++) {
mtr->x_latch_at_savepoint(
tree_savepoints[i],
tree_blocks[i]);
}
}
/* We should consider prev_page of parent page, if the node_ptr
is the leftmost of the page. because BTR_SEARCH_PREV and
BTR_MODIFY_PREV latches prev_page of the leaf page. */
if ((latch_mode == BTR_SEARCH_PREV
|| latch_mode == BTR_MODIFY_PREV)
&& !prev_tree_blocks) {
/* block should be latched for consistent
btr_page_get_prev() */
ut_ad(mtr->memo_contains_flagged(
block, MTR_MEMO_PAGE_S_FIX
| MTR_MEMO_PAGE_X_FIX));
if (page_has_prev(page)
&& page_rec_is_first(node_ptr, page)) {
if (leftmost_from_level == 0) {
leftmost_from_level = height + 1;
}
} else {
leftmost_from_level = 0;
}
if (height == 0 && leftmost_from_level > 0) {
/* should retry to get also prev_page
from level==leftmost_from_level. */
prev_tree_blocks = static_cast<buf_block_t**>(
ut_malloc_nokey(sizeof(buf_block_t*)
* leftmost_from_level));
prev_tree_savepoints = static_cast<ulint*>(
ut_malloc_nokey(sizeof(ulint)
* leftmost_from_level));
/* back to the level (leftmost_from_level+1) */
ulint idx = n_blocks
- (leftmost_from_level - 1);
page_id.set_page_no(
tree_blocks[idx]->page.id().page_no());
for (ulint i = n_blocks
- (leftmost_from_level - 1);
i <= n_blocks; i++) {
mtr_release_block_at_savepoint(
mtr, tree_savepoints[i],
tree_blocks[i]);
}
n_blocks -= (leftmost_from_level - 1);
height = leftmost_from_level;
ut_ad(n_releases == 0);
/* replay up_match, low_match */
up_match = 0;
low_match = 0;
rtr_info_t* rtr_info = need_path
? cursor->rtr_info : NULL;
for (ulint i = 0; i < n_blocks; i++) {
page_cursor->block = tree_blocks[i];
if (page_cur_search_with_match(
tuple,
page_mode, &up_match,
&low_match, page_cursor,
rtr_info)) {
err = DB_CORRUPTION;
goto func_exit;
}
}
goto search_loop;
}
}
/* Go to the child node */
page_id.set_page_no(
btr_node_ptr_get_child_page_no(node_ptr, offsets));
n_blocks++;
if (UNIV_UNLIKELY(height == 0 && dict_index_is_ibuf(index))) {
/* We're doing a search on an ibuf tree and we're one
level above the leaf page. */
ut_ad(level == 0);
buf_mode = BUF_GET;
rw_latch = RW_NO_LATCH;
goto retry_page_get;
}
if (dict_index_is_spatial(index)
&& page_mode >= PAGE_CUR_CONTAIN
&& page_mode != PAGE_CUR_RTREE_INSERT) {
ut_ad(need_path);
rtr_node_path_t* path =
cursor->rtr_info->path;
if (!path->empty() && found) {
ut_ad(path->back().page_no
== page_id.page_no());
path->pop_back();
#ifdef UNIV_DEBUG
if (page_mode == PAGE_CUR_RTREE_LOCATE
&& (latch_mode != BTR_MODIFY_LEAF)) {
btr_pcur_t* cur
= cursor->rtr_info->parent_path->back(
).cursor;
rec_t* my_node_ptr
= btr_pcur_get_rec(cur);
offsets = rec_get_offsets(
my_node_ptr, index, offsets,
0, ULINT_UNDEFINED, &heap);
ulint my_page_no
= btr_node_ptr_get_child_page_no(
my_node_ptr, offsets);
ut_ad(page_id.page_no() == my_page_no);
}
#endif
}
}
goto search_loop;
} else if (!dict_index_is_spatial(index)
&& latch_mode == BTR_MODIFY_TREE
&& lock_intention == BTR_INTENTION_INSERT
&& page_has_next(page)
&& page_rec_is_last(page_cur_get_rec(page_cursor), page)) {
/* btr_insert_into_right_sibling() might cause
deleting node_ptr at upper level */
guess = NULL;
if (height == 0) {
/* release the leaf pages if latched */
for (uint i = 0; i < 3; i++) {
if (latch_leaves.blocks[i] != NULL) {
mtr_release_block_at_savepoint(
mtr, latch_leaves.savepoints[i],
latch_leaves.blocks[i]);
latch_leaves.blocks[i] = NULL;
}
}
}
goto need_opposite_intention;
}
if (level != 0) {
ut_ad(!autoinc);
if (upper_rw_latch == RW_NO_LATCH) {
ut_ad(latch_mode == BTR_CONT_MODIFY_TREE
|| latch_mode == BTR_CONT_SEARCH_TREE);
btr_block_get(
*index, page_id.page_no(),
latch_mode == BTR_CONT_MODIFY_TREE
? RW_X_LATCH : RW_SX_LATCH, false, mtr, &err);
} else {
ut_ad(mtr->memo_contains_flagged(block,
upper_rw_latch));
if (latch_by_caller) {
ut_ad(latch_mode == BTR_SEARCH_TREE);
/* to exclude modifying tree operations
should sx-latch the index. */
ut_ad(mtr->memo_contains(index->lock,
MTR_MEMO_SX_LOCK));
/* because has sx-latch of index,
can release upper blocks. */
for (; n_releases < n_blocks; n_releases++) {
mtr_release_block_at_savepoint(
mtr,
tree_savepoints[n_releases],
tree_blocks[n_releases]);
}
}
}
if (page_mode <= PAGE_CUR_LE) {
cursor->low_match = low_match;
cursor->up_match = up_match;
}
} else {
cursor->low_match = low_match;
cursor->low_bytes = low_bytes;
cursor->up_match = up_match;
cursor->up_bytes = up_bytes;
if (autoinc) {
page_set_autoinc(tree_blocks[0], autoinc, mtr, false);
}
#ifdef BTR_CUR_HASH_ADAPT
/* We do a dirty read of btr_search_enabled here. We
will properly check btr_search_enabled again in
btr_search_build_page_hash_index() before building a
page hash index, while holding search latch. */
if (!btr_search_enabled) {
} else if (tuple->info_bits & REC_INFO_MIN_REC_FLAG) {
/* This may be a search tuple for
btr_pcur_t::restore_position(). */
ut_ad(tuple->is_metadata()
|| (tuple->is_metadata(tuple->info_bits
^ REC_STATUS_INSTANT)));
} else if (index->is_spatial()) {
} else if (index->table->is_temporary()) {
} else if (rec_is_metadata(btr_cur_get_rec(cursor), *index)) {
/* Only user records belong in the adaptive
hash index. */
} else {
btr_search_info_update(index, cursor);
}
#endif /* BTR_CUR_HASH_ADAPT */
ut_ad(cursor->up_match != ULINT_UNDEFINED
|| mode != PAGE_CUR_GE);
ut_ad(cursor->up_match != ULINT_UNDEFINED
|| mode != PAGE_CUR_LE);
ut_ad(cursor->low_match != ULINT_UNDEFINED
|| mode != PAGE_CUR_LE);
}
/* For spatial index, remember what blocks are still latched */
if (dict_index_is_spatial(index)
&& (latch_mode == BTR_MODIFY_TREE
|| latch_mode == BTR_MODIFY_LEAF)) {
for (ulint i = 0; i < n_releases; i++) {
cursor->rtr_info->tree_blocks[i] = NULL;
cursor->rtr_info->tree_savepoints[i] = 0;
}
for (ulint i = n_releases; i <= n_blocks; i++) {
cursor->rtr_info->tree_blocks[i] = tree_blocks[i];
cursor->rtr_info->tree_savepoints[i] = tree_savepoints[i];
}
}
func_exit:
if (UNIV_LIKELY_NULL(heap)) {
mem_heap_free(heap);
}
ut_free(prev_tree_blocks);
ut_free(prev_tree_savepoints);
if (mbr_adj) {
/* remember that we will need to adjust parent MBR */
cursor->rtr_info->mbr_adj = true;
}
DBUG_RETURN(err);
}
dberr_t btr_cur_t::open_leaf(bool first, dict_index_t *index,
btr_latch_mode latch_mode, mtr_t *mtr)
{
ulint node_ptr_max_size= srv_page_size / 2;
btr_intention_t lock_intention;
ulint n_blocks= 0;
mem_heap_t *heap= nullptr;
rec_offs offsets_[REC_OFFS_NORMAL_SIZE];
rec_offs *offsets= offsets_;
dberr_t err;
rec_offs_init(offsets_);
const bool latch_by_caller= latch_mode & BTR_ALREADY_S_LATCHED;
latch_mode = btr_latch_mode(latch_mode & ~BTR_ALREADY_S_LATCHED);
lock_intention= btr_cur_get_and_clear_intention(&latch_mode);
/* This function doesn't need to lock left page of the leaf page */
if (latch_mode == BTR_SEARCH_PREV)
latch_mode= BTR_SEARCH_LEAF;
else if (latch_mode == BTR_MODIFY_PREV)
latch_mode= BTR_MODIFY_LEAF;
/* Store the position of the tree latch we push to mtr so that we
know how to release it when we have latched the leaf node */
auto savepoint= mtr->get_savepoint();
rw_lock_type_t upper_rw_latch= RW_X_LATCH;
switch (latch_mode) {
case BTR_CONT_MODIFY_TREE:
case BTR_CONT_SEARCH_TREE:
abort();
break;
case BTR_MODIFY_TREE:
/* Most of delete-intended operations are purging. Free blocks
and read IO bandwidth should be prioritized for them, when the
history list is growing huge. */
savepoint++;
if (lock_intention == BTR_INTENTION_DELETE
&& buf_pool.n_pend_reads
&& trx_sys.history_size_approx() > BTR_CUR_FINE_HISTORY_LENGTH)
mtr_x_lock_index(index, mtr);
else
mtr_sx_lock_index(index, mtr);
break;
default:
ut_ad(!latch_by_caller ||
mtr->memo_contains_flagged(&index->lock,
MTR_MEMO_SX_LOCK | MTR_MEMO_S_LOCK));
upper_rw_latch= RW_S_LATCH;
if (latch_by_caller)
break;
ut_ad(latch_mode != BTR_SEARCH_TREE);
savepoint++;
mtr_s_lock_index(index, mtr);
}
ut_ad(savepoint == mtr->get_savepoint());
const rw_lock_type_t root_leaf_rw_latch=
btr_cur_latch_for_root_leaf(latch_mode);
page_cur.index = index;
uint32_t page= index->page;
const auto zip_size= index->table->space->zip_size();
if (root_leaf_rw_latch == RW_X_LATCH)
node_ptr_max_size= btr_node_ptr_max_size(index);
for (ulint height= ULINT_UNDEFINED;;)
{
ut_ad(n_blocks < BTR_MAX_LEVELS);
ut_ad(savepoint + n_blocks == mtr->get_savepoint());
const rw_lock_type_t rw_latch= height && latch_mode != BTR_MODIFY_TREE
? upper_rw_latch
: RW_NO_LATCH;
buf_block_t* block=
btr_block_get(*index, page, rw_latch, !height && !index->is_clust(), mtr,
&err);
ut_ad(!block == (err != DB_SUCCESS));
if (!block)
{
if (err == DB_DECRYPTION_FAILED)
btr_decryption_failed(*index);
break;
}
if (first)
page_cur_set_before_first(block, &page_cur);
else
page_cur_set_after_last(block, &page_cur);
const uint32_t l= btr_page_get_level(block->page.frame);
if (height == ULINT_UNDEFINED)
{
/* We are in the root node */
height= l;
if (height);
else if (upper_rw_latch != root_leaf_rw_latch)
{
/* We should retry to get the page, because the root page
is latched with different level as a leaf page. */
ut_ad(n_blocks == 0);
ut_ad(root_leaf_rw_latch != RW_NO_LATCH);
upper_rw_latch= root_leaf_rw_latch;
mtr->rollback_to_savepoint(savepoint);
continue;
}
else
{
reached_leaf:
const auto leaf_savepoint= mtr->get_savepoint();
ut_ad(leaf_savepoint);
if (rw_latch == RW_NO_LATCH)
btr_cur_latch_leaves(block, latch_mode, this, mtr);
switch (latch_mode) {
case BTR_MODIFY_TREE:
case BTR_CONT_MODIFY_TREE:
case BTR_CONT_SEARCH_TREE:
break;
default:
/* Release index->lock if needed, and the non-leaf pages. */
mtr->rollback_to_savepoint(savepoint - !latch_by_caller,
leaf_savepoint - 1);
}
break;
}
}
else if (UNIV_UNLIKELY(height != l))
{
corrupted:
err= DB_CORRUPTION;
break;
}
if (!height)
goto reached_leaf;
height--;
if (first
? !page_cur_move_to_next(&page_cur)
: !page_cur_move_to_prev(&page_cur))
goto corrupted;
const rec_t *node_ptr= page_cur.rec;
offsets= rec_get_offsets(node_ptr, index, offsets, 0, ULINT_UNDEFINED,
&heap);
ut_ad(latch_mode != BTR_MODIFY_TREE || upper_rw_latch == RW_X_LATCH);
if (latch_mode != BTR_MODIFY_TREE);
else if (btr_cur_need_opposite_intention(block->page.frame,
lock_intention, node_ptr))
{
/* If the rec is the first or last in the page for pessimistic
delete intention, it might cause node_ptr insert for the upper
level. We should change the intention and retry. */
mtr->rollback_to_savepoint(savepoint);
lock_intention= BTR_INTENTION_BOTH;
page= index->page;
height= ULINT_UNDEFINED;
n_blocks= 0;
continue;
}
else
{
if (!btr_cur_will_modify_tree(index, block->page.frame,
lock_intention, node_ptr,
node_ptr_max_size, zip_size, mtr))
{
ut_ad(n_blocks);
/* release buffer-fixes on pages that will not be modified
(except the root) */
if (n_blocks > 1)
{
mtr->rollback_to_savepoint(savepoint + 1, savepoint + n_blocks - 1);
n_blocks= 1;
}
}
if (!height)
{
if (page == index->page)
mtr->upgrade_buffer_fix(savepoint, RW_X_LATCH);
else
{
/* The U-latch protects BTR_SEG_HEAP, BTR_SEG_TOP. */
mtr->upgrade_buffer_fix(savepoint, RW_SX_LATCH);
/* Upgrade buffer-fix to exclusive latches on all remaining pages. */
for (ulint i= 1; i <= n_blocks; i++)
mtr->upgrade_buffer_fix(savepoint + i, RW_X_LATCH);
}
}
}
/* Go to the child node */
page= btr_node_ptr_get_child_page_no(node_ptr, offsets);
n_blocks++;
}
if (UNIV_LIKELY_NULL(heap))
mem_heap_free(heap);
return err;
}
/**********************************************************************//**
Positions a cursor at a randomly chosen position within a B-tree.
@return true if the index is available and we have put the cursor, false
if the index is unavailable */
bool
btr_cur_open_at_rnd_pos(
dict_index_t* index, /*!< in: index */
btr_latch_mode latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */
btr_cur_t* cursor, /*!< in/out: B-tree cursor */
mtr_t* mtr) /*!< in: mtr */
{
page_cur_t* page_cursor;
ulint node_ptr_max_size = srv_page_size / 2;
ulint height;
rec_t* node_ptr;
btr_intention_t lock_intention;
buf_block_t* tree_blocks[BTR_MAX_LEVELS];
ulint tree_savepoints[BTR_MAX_LEVELS];
ulint n_blocks = 0;
ulint n_releases = 0;
mem_heap_t* heap = NULL;
rec_offs offsets_[REC_OFFS_NORMAL_SIZE];
rec_offs* offsets = offsets_;
rec_offs_init(offsets_);
ut_ad(!index->is_spatial());
lock_intention = btr_cur_get_and_clear_intention(&latch_mode);
ulint savepoint = mtr_set_savepoint(mtr);
rw_lock_type_t upper_rw_latch;
switch (latch_mode) {
case BTR_MODIFY_TREE:
/* Most of delete-intended operations are purging.
Free blocks and read IO bandwidth should be prior
for them, when the history list is glowing huge. */
if (lock_intention == BTR_INTENTION_DELETE
&& buf_pool.n_pend_reads
&& trx_sys.history_size_approx()
> BTR_CUR_FINE_HISTORY_LENGTH) {
mtr_x_lock_index(index, mtr);
} else {
mtr_sx_lock_index(index, mtr);
}
upper_rw_latch = RW_X_LATCH;
break;
case BTR_SEARCH_PREV:
case BTR_MODIFY_PREV:
/* This function doesn't support left uncle
page lock for left leaf page lock, when
needed. */
case BTR_SEARCH_TREE:
case BTR_CONT_MODIFY_TREE:
case BTR_CONT_SEARCH_TREE:
ut_ad(0);
/* fall through */
default:
if (!srv_read_only_mode) {
mtr_s_lock_index(index, mtr);
upper_rw_latch = RW_S_LATCH;
} else {
upper_rw_latch = RW_NO_LATCH;
}
}
DBUG_EXECUTE_IF("test_index_is_unavailable",
return(false););
if (index->page == FIL_NULL) {
/* Since we don't hold index lock until just now, the index
could be modified by others, for example, if this is a
statistics updater for referenced table, it could be marked
as unavailable by 'DROP TABLE' in the mean time, since
we don't hold lock for statistics updater */
return(false);
}
const rw_lock_type_t root_leaf_rw_latch = btr_cur_latch_for_root_leaf(
latch_mode);
page_cursor = btr_cur_get_page_cur(cursor);
page_cursor->index = index;
page_id_t page_id(index->table->space_id, index->page);
const ulint zip_size = index->table->space->zip_size();
dberr_t err;
if (root_leaf_rw_latch == RW_X_LATCH) {
node_ptr_max_size = btr_node_ptr_max_size(index);
}
height = ULINT_UNDEFINED;
for (;;) {
page_t* page;
ut_ad(n_blocks < BTR_MAX_LEVELS);
tree_savepoints[n_blocks] = mtr_set_savepoint(mtr);
const rw_lock_type_t rw_latch = height
&& latch_mode != BTR_MODIFY_TREE
? upper_rw_latch : RW_NO_LATCH;
buf_block_t* block = buf_page_get_gen(page_id, zip_size,
rw_latch, NULL, BUF_GET,
mtr, &err,
height == 0
&& !index->is_clust());
tree_blocks[n_blocks] = block;
ut_ad((block != NULL) == (err == DB_SUCCESS));
if (!block) {
if (err == DB_DECRYPTION_FAILED) {
btr_decryption_failed(*index);
}
break;
}
page = buf_block_get_frame(block);
if (height == ULINT_UNDEFINED
&& page_is_leaf(page)
&& rw_latch != RW_NO_LATCH
&& rw_latch != root_leaf_rw_latch) {
/* We should retry to get the page, because the root page
is latched with different level as a leaf page. */
ut_ad(root_leaf_rw_latch != RW_NO_LATCH);
ut_ad(rw_latch == RW_S_LATCH);
ut_ad(n_blocks == 0);
mtr_release_block_at_savepoint(
mtr, tree_savepoints[n_blocks],
tree_blocks[n_blocks]);
upper_rw_latch = root_leaf_rw_latch;
continue;
}
ut_ad(fil_page_index_page_check(page));
ut_ad(index->id == btr_page_get_index_id(page));
if (height == ULINT_UNDEFINED) {
/* We are in the root node */
height = btr_page_get_level(page);
}
if (height == 0) {
if (rw_latch == RW_NO_LATCH
|| srv_read_only_mode) {
btr_cur_latch_leaves(block, latch_mode, cursor,
mtr);
}
/* btr_cur_t::open_leaf() and
btr_cur_search_to_nth_level() release
tree s-latch here.*/
switch (latch_mode) {
case BTR_MODIFY_TREE:
case BTR_CONT_MODIFY_TREE:
case BTR_CONT_SEARCH_TREE:
break;
default:
/* Release the tree s-latch */
if (!srv_read_only_mode) {
mtr_release_s_latch_at_savepoint(
mtr, savepoint,
&index->lock);
}
/* release upper blocks */
for (; n_releases < n_blocks; n_releases++) {
mtr_release_block_at_savepoint(
mtr,
tree_savepoints[n_releases],
tree_blocks[n_releases]);
}
}
}
page_cursor->block = block;
page_cur_open_on_rnd_user_rec(page_cursor);
if (height == 0) {
break;
}
ut_ad(height > 0);
height--;
node_ptr = page_cur_get_rec(page_cursor);
offsets = rec_get_offsets(node_ptr, page_cursor->index,
offsets, 0, ULINT_UNDEFINED, &heap);
/* If the rec is the first or last in the page for
pessimistic delete intention, it might cause node_ptr insert
for the upper level. We should change the intention and retry.
*/
if (latch_mode == BTR_MODIFY_TREE
&& btr_cur_need_opposite_intention(
page, lock_intention, node_ptr)) {
ut_ad(upper_rw_latch == RW_X_LATCH);
/* release all blocks */
for (; n_releases <= n_blocks; n_releases++) {
mtr_release_block_at_savepoint(
mtr, tree_savepoints[n_releases],
tree_blocks[n_releases]);
}
lock_intention = BTR_INTENTION_BOTH;
page_id.set_page_no(dict_index_get_page(index));
height = ULINT_UNDEFINED;
n_blocks = 0;
n_releases = 0;
continue;
}
if (latch_mode == BTR_MODIFY_TREE
&& !btr_cur_will_modify_tree(
page_cursor->index, page, lock_intention,
node_ptr, node_ptr_max_size, zip_size, mtr)) {
ut_ad(upper_rw_latch == RW_X_LATCH);
ut_ad(n_releases <= n_blocks);
/* we can release upper blocks */
for (; n_releases < n_blocks; n_releases++) {
if (n_releases == 0) {
/* we should not release root page
to pin to same block. */
continue;
}
/* release unused blocks to unpin */
mtr_release_block_at_savepoint(
mtr, tree_savepoints[n_releases],
tree_blocks[n_releases]);
}
}
if (height == 0
&& latch_mode == BTR_MODIFY_TREE) {
ut_ad(upper_rw_latch == RW_X_LATCH);
/* we should sx-latch root page, if released already.
It contains seg_header. */
if (n_releases > 0) {
mtr->sx_latch_at_savepoint(
tree_savepoints[0],
tree_blocks[0]);
}
/* x-latch the branch blocks not released yet. */
for (ulint i = n_releases; i <= n_blocks; i++) {
mtr->x_latch_at_savepoint(
tree_savepoints[i],
tree_blocks[i]);
}
}
/* Go to the child node */
page_id.set_page_no(
btr_node_ptr_get_child_page_no(node_ptr, offsets));
n_blocks++;
}
if (UNIV_LIKELY_NULL(heap)) {
mem_heap_free(heap);
}
return err == DB_SUCCESS;
}
/*==================== B-TREE INSERT =========================*/
/*************************************************************//**
Inserts a record if there is enough space, or if enough space can
be freed by reorganizing. Differs from btr_cur_optimistic_insert because
no heuristics is applied to whether it pays to use CPU time for
reorganizing the page or not.
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
if this 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 pointer to inserted record if succeed, else NULL */
static MY_ATTRIBUTE((nonnull, warn_unused_result))
rec_t*
btr_cur_insert_if_possible(
/*=======================*/
btr_cur_t* cursor, /*!< in: cursor on page after which to insert;
cursor stays valid */
const dtuple_t* tuple, /*!< in: tuple to insert; the size info need not
have been stored to tuple */
rec_offs** offsets,/*!< out: offsets on *rec */
mem_heap_t** heap, /*!< in/out: pointer to memory heap, or NULL */
ulint n_ext, /*!< in: number of externally stored columns */
mtr_t* mtr) /*!< in/out: mini-transaction */
{
page_cur_t* page_cursor;
rec_t* rec;
ut_ad(dtuple_check_typed(tuple));
ut_ad(mtr->memo_contains_flagged(btr_cur_get_block(cursor),
MTR_MEMO_PAGE_X_FIX));
page_cursor = btr_cur_get_page_cur(cursor);
/* Now, try the insert */
rec = page_cur_tuple_insert(page_cursor, tuple, offsets, heap, n_ext,
mtr);
/* If the record did not fit, reorganize.
For compressed pages, page_cur_tuple_insert()
attempted this already. */
if (!rec && !page_cur_get_page_zip(page_cursor)
&& btr_page_reorganize(page_cursor, mtr) == DB_SUCCESS) {
rec = page_cur_tuple_insert(page_cursor, tuple, offsets, heap,
n_ext, mtr);
}
ut_ad(!rec || rec_offs_validate(rec, page_cursor->index, *offsets));
return(rec);
}
/*************************************************************//**
For an insert, checks the locks and does the undo logging if desired.
@return DB_SUCCESS, DB_LOCK_WAIT, DB_FAIL, or error number */
UNIV_INLINE MY_ATTRIBUTE((warn_unused_result, nonnull(2,3,5,6)))
dberr_t
btr_cur_ins_lock_and_undo(
/*======================*/
ulint flags, /*!< in: undo logging and locking flags: if
not zero, the parameters index and thr
should be specified */
btr_cur_t* cursor, /*!< in: cursor on page after which to insert */
dtuple_t* entry, /*!< in/out: entry to insert */
que_thr_t* thr, /*!< in: query thread or NULL */
mtr_t* mtr, /*!< in/out: mini-transaction */
bool* inherit)/*!< out: true if the inserted new record maybe
should inherit LOCK_GAP type locks from the
successor record */
{
if (!(~flags | (BTR_NO_UNDO_LOG_FLAG | BTR_KEEP_SYS_FLAG))) {
return DB_SUCCESS;
}
/* Check if we have to wait for a lock: enqueue an explicit lock
request if yes */
rec_t* rec = btr_cur_get_rec(cursor);
dict_index_t* index = cursor->index();
ut_ad(!dict_index_is_online_ddl(index)
|| dict_index_is_clust(index)
|| (flags & BTR_CREATE_FLAG));
ut_ad((flags & BTR_NO_UNDO_LOG_FLAG)
|| !index->table->skip_alter_undo);
ut_ad(mtr->is_named_space(index->table->space));
/* Check if there is predicate or GAP lock preventing the insertion */
if (!(flags & BTR_NO_LOCKING_FLAG)) {
const unsigned type = index->type;
if (UNIV_UNLIKELY(type & DICT_SPATIAL)) {
lock_prdt_t prdt;
rtr_mbr_t mbr;
rtr_get_mbr_from_tuple(entry, &mbr);
/* Use on stack MBR variable to test if a lock is
needed. If so, the predicate (MBR) will be allocated
from lock heap in lock_prdt_insert_check_and_lock() */
lock_init_prdt_from_mbr(&prdt, &mbr, 0, nullptr);
if (dberr_t err = lock_prdt_insert_check_and_lock(
rec, btr_cur_get_block(cursor),
index, thr, mtr, &prdt)) {
return err;
}
*inherit = false;
} else {
ut_ad(!dict_index_is_online_ddl(index)
|| index->is_primary()
|| (flags & BTR_CREATE_FLAG));
#ifdef WITH_WSREP
trx_t* trx= thr_get_trx(thr);
/* If transaction scanning an unique secondary
key is wsrep high priority thread (brute
force) this scanning may involve GAP-locking
in the index. As this locking happens also
when applying replication events in high
priority applier threads, there is a
probability for lock conflicts between two
wsrep high priority threads. To avoid this
GAP-locking we mark that this transaction
is using unique key scan here. */
if ((type & (DICT_CLUSTERED | DICT_UNIQUE)) == DICT_UNIQUE
&& trx->is_wsrep()
&& wsrep_thd_is_BF(trx->mysql_thd, false)) {
trx->wsrep = 3;
}
#endif /* WITH_WSREP */
if (dberr_t err = lock_rec_insert_check_and_lock(
rec, btr_cur_get_block(cursor),
index, thr, mtr, inherit)) {
return err;
}
}
}
if (!index->is_primary() || !page_is_leaf(page_align(rec))) {
return DB_SUCCESS;
}
constexpr roll_ptr_t dummy_roll_ptr = roll_ptr_t{1}
<< ROLL_PTR_INSERT_FLAG_POS;
roll_ptr_t roll_ptr = dummy_roll_ptr;
if (!(flags & BTR_NO_UNDO_LOG_FLAG)) {
if (dberr_t err = trx_undo_report_row_operation(
thr, index, entry, NULL, 0, NULL, NULL,
&roll_ptr)) {
return err;
}
if (roll_ptr != dummy_roll_ptr) {
dfield_t* r = dtuple_get_nth_field(entry,
index->db_trx_id());
trx_write_trx_id(static_cast<byte*>(r->data),
thr_get_trx(thr)->id);
}
}
if (!(flags & BTR_KEEP_SYS_FLAG)) {
dfield_t* r = dtuple_get_nth_field(
entry, index->db_roll_ptr());
ut_ad(r->len == DATA_ROLL_PTR_LEN);
trx_write_roll_ptr(static_cast<byte*>(r->data), roll_ptr);
}
return DB_SUCCESS;
}
/**
Prefetch siblings of the leaf for the pessimistic operation.
@param block leaf page
@param index index of the page */
static void btr_cur_prefetch_siblings(const buf_block_t *block,
const dict_index_t *index)
{
ut_ad(page_is_leaf(block->page.frame));
if (index->is_ibuf())
return;
const page_t *page= block->page.frame;
uint32_t prev= mach_read_from_4(my_assume_aligned<4>(page + FIL_PAGE_PREV));
uint32_t next= mach_read_from_4(my_assume_aligned<4>(page + FIL_PAGE_NEXT));
fil_space_t *space= index->table->space;
if (prev == FIL_NULL);
else if (space->acquire())
buf_read_page_background(space, page_id_t(space->id, prev),
block->zip_size());
if (next == FIL_NULL);
else if (space->acquire())
buf_read_page_background(space, page_id_t(space->id, next),
block->zip_size());
}
/*************************************************************//**
Tries to perform an insert to a page in an index tree, next to cursor.
It is assumed that mtr holds an x-latch on the page. The operation does
not succeed if there is too little space on the page. If there is just
one record on the page, the insert will always succeed; this is to
prevent trying to split a page with just one record.
@return DB_SUCCESS, DB_LOCK_WAIT, DB_FAIL, or error number */
dberr_t
btr_cur_optimistic_insert(
/*======================*/
ulint flags, /*!< in: undo logging and locking flags: if not
zero, the parameters index and thr should be
specified */
btr_cur_t* cursor, /*!< in: cursor on page after which to insert;
cursor stays valid */
rec_offs** offsets,/*!< out: offsets on *rec */
mem_heap_t** heap, /*!< in/out: pointer to memory heap */
dtuple_t* entry, /*!< in/out: entry to insert */
rec_t** rec, /*!< out: pointer to inserted record if
succeed */
big_rec_t** big_rec,/*!< out: big rec vector whose fields have to
be stored externally by the caller */
ulint n_ext, /*!< in: number of externally stored columns */
que_thr_t* thr, /*!< in/out: query thread; can be NULL if
!(~flags
& (BTR_NO_LOCKING_FLAG
| BTR_NO_UNDO_LOG_FLAG)) */
mtr_t* mtr) /*!< in/out: mini-transaction;
if this function returns DB_SUCCESS on
a leaf page of a secondary index in a
compressed tablespace, the caller must
mtr_commit(mtr) before latching
any further pages */
{
big_rec_t* big_rec_vec = NULL;
dict_index_t* index;
page_cur_t* page_cursor;
buf_block_t* block;
page_t* page;
rec_t* dummy;
bool leaf;
bool reorg __attribute__((unused));
bool inherit = true;
ulint rec_size;
dberr_t err;
ut_ad(thr || !(~flags & (BTR_NO_LOCKING_FLAG | BTR_NO_UNDO_LOG_FLAG)));
*big_rec = NULL;
block = btr_cur_get_block(cursor);
page = buf_block_get_frame(block);
index = cursor->index();
ut_ad(mtr->memo_contains_flagged(block, MTR_MEMO_PAGE_X_FIX));
ut_ad(!dict_index_is_online_ddl(index)
|| dict_index_is_clust(index)
|| (flags & BTR_CREATE_FLAG));
ut_ad(dtuple_check_typed(entry));
#ifdef HAVE_valgrind
if (block->page.zip.data) {
MEM_CHECK_DEFINED(page, srv_page_size);
MEM_CHECK_DEFINED(block->page.zip.data, block->zip_size());
}
#endif /* HAVE_valgrind */
leaf = page_is_leaf(page);
if (UNIV_UNLIKELY(entry->is_alter_metadata())) {
ut_ad(leaf);
goto convert_big_rec;
}
/* Calculate the record size when entry is converted to a record */
rec_size = rec_get_converted_size(index, entry, n_ext);
if (page_zip_rec_needs_ext(rec_size, page_is_comp(page),
dtuple_get_n_fields(entry),
block->zip_size())) {
convert_big_rec:
/* The record is so big that we have to store some fields
externally on separate database pages */
big_rec_vec = dtuple_convert_big_rec(index, 0, entry, &n_ext);
if (UNIV_UNLIKELY(big_rec_vec == NULL)) {
return(DB_TOO_BIG_RECORD);
}
rec_size = rec_get_converted_size(index, entry, n_ext);
}
if (block->page.zip.data && page_zip_is_too_big(index, entry)) {
if (big_rec_vec != NULL) {
dtuple_convert_back_big_rec(index, entry, big_rec_vec);
}
return(DB_TOO_BIG_RECORD);
}
LIMIT_OPTIMISTIC_INSERT_DEBUG(page_get_n_recs(page),
goto fail);
if (block->page.zip.data && leaf
&& (page_get_data_size(page) + rec_size
>= dict_index_zip_pad_optimal_page_size(index))) {
/* If compression padding tells us that insertion will
result in too packed up page i.e.: which is likely to
cause compression failure then don't do an optimistic
insertion. */
fail:
err = DB_FAIL;
/* prefetch siblings of the leaf for the pessimistic
operation, if the page is leaf. */
if (page_is_leaf(page)) {
btr_cur_prefetch_siblings(block, index);
}
fail_err:
if (big_rec_vec) {
dtuple_convert_back_big_rec(index, entry, big_rec_vec);
}
return(err);
}
ulint max_size = page_get_max_insert_size_after_reorganize(page, 1);
if (max_size < rec_size) {
goto fail;
}
const ulint n_recs = page_get_n_recs(page);
if (UNIV_UNLIKELY(n_recs >= 8189)) {
ut_ad(srv_page_size == 65536);
goto fail;
}
if (page_has_garbage(page)) {
if (max_size < BTR_CUR_PAGE_REORGANIZE_LIMIT
&& n_recs > 1
&& page_get_max_insert_size(page, 1) < rec_size) {
goto fail;
}
}
/* If there have been many consecutive inserts to the
clustered index leaf page of an uncompressed table, check if
we have to split the page to reserve enough free space for
future updates of records. */
if (leaf && !block->page.zip.data && dict_index_is_clust(index)
&& page_get_n_recs(page) >= 2
&& dict_index_get_space_reserve() + rec_size > max_size
&& (btr_page_get_split_rec_to_right(cursor, &dummy)
|| btr_page_get_split_rec_to_left(cursor))) {
goto fail;
}
page_cursor = btr_cur_get_page_cur(cursor);
DBUG_LOG("ib_cur",
"insert " << index->name << " (" << index->id << ") by "
<< ib::hex(thr ? thr->graph->trx->id : 0)
<< ' ' << rec_printer(entry).str());
DBUG_EXECUTE_IF("do_page_reorganize",
ut_a(!n_recs || btr_page_reorganize(page_cursor, mtr)
== DB_SUCCESS););
/* Now, try the insert */
{
const rec_t* page_cursor_rec = page_cur_get_rec(page_cursor);
/* Check locks and write to the undo log,
if specified */
err = btr_cur_ins_lock_and_undo(flags, cursor, entry,
thr, mtr, &inherit);
if (err != DB_SUCCESS) {
goto fail_err;
}
#ifdef UNIV_DEBUG
if (!(flags & BTR_CREATE_FLAG)
&& index->is_primary() && page_is_leaf(page)) {
const dfield_t* trx_id = dtuple_get_nth_field(
entry, dict_col_get_clust_pos(
dict_table_get_sys_col(index->table,
DATA_TRX_ID),
index));
ut_ad(trx_id->len == DATA_TRX_ID_LEN);
ut_ad(trx_id[1].len == DATA_ROLL_PTR_LEN);
ut_ad(*static_cast<const byte*>
(trx_id[1].data) & 0x80);
if (flags & BTR_NO_UNDO_LOG_FLAG) {
ut_ad(!memcmp(trx_id->data, reset_trx_id,
DATA_TRX_ID_LEN));
} else {
ut_ad(thr->graph->trx->id);
ut_ad(thr->graph->trx->bulk_insert
|| thr->graph->trx->id
== trx_read_trx_id(
static_cast<const byte*>(
trx_id->data))
|| index->table->is_temporary());
}
}
#endif
*rec = page_cur_tuple_insert(page_cursor, entry, offsets, heap,
n_ext, mtr);
reorg = page_cursor_rec != page_cur_get_rec(page_cursor);
}
if (*rec) {
} else if (block->page.zip.data) {
ut_ad(!index->table->is_temporary());
/* Reset the IBUF_BITMAP_FREE bits, because
page_cur_tuple_insert() will have attempted page
reorganize before failing. */
if (leaf
&& !dict_index_is_clust(index)) {
ibuf_reset_free_bits(block);
}
goto fail;
} else {
ut_ad(!reorg);
reorg = true;
/* If the record did not fit, reorganize */
err = btr_page_reorganize(page_cursor, mtr);
if (err != DB_SUCCESS
|| page_get_max_insert_size(page, 1) != max_size
|| !(*rec = page_cur_tuple_insert(page_cursor, entry,
offsets, heap, n_ext,
mtr))) {
err = DB_CORRUPTION;
goto fail_err;
}
}
#ifdef BTR_CUR_HASH_ADAPT
if (!leaf) {
} else if (entry->info_bits & REC_INFO_MIN_REC_FLAG) {
ut_ad(entry->is_metadata());
ut_ad(index->is_instant());
ut_ad(flags == BTR_NO_LOCKING_FLAG);
} else if (index->table->is_temporary()) {
} else {
srw_spin_lock* ahi_latch = btr_search_sys.get_latch(*index);
if (!reorg && cursor->flag == BTR_CUR_HASH) {
btr_search_update_hash_node_on_insert(
cursor, ahi_latch);
} else {
btr_search_update_hash_on_insert(cursor, ahi_latch);
}
}
#endif /* BTR_CUR_HASH_ADAPT */
if (!(flags & BTR_NO_LOCKING_FLAG) && inherit) {
lock_update_insert(block, *rec);
}
if (leaf
&& !dict_index_is_clust(index)
&& !index->table->is_temporary()) {
/* Update the free bits of the B-tree page in the
insert buffer bitmap. */
/* The free bits in the insert buffer bitmap must
never exceed the free space on a page. It is safe to
decrement or reset the bits in the bitmap in a
mini-transaction that is committed before the
mini-transaction that affects the free space. */
/* It is unsafe to increment the bits in a separately
committed mini-transaction, because in crash recovery,
the free bits could momentarily be set too high. */
if (block->page.zip.data) {
/* Update the bits in the same mini-transaction. */
ibuf_update_free_bits_zip(block, mtr);
} else {
/* Decrement the bits in a separate
mini-transaction. */
ibuf_update_free_bits_if_full(
block, max_size,
rec_size + PAGE_DIR_SLOT_SIZE);
}
}
*big_rec = big_rec_vec;
return(DB_SUCCESS);
}
/*************************************************************//**
Performs an insert on a page of an index tree. It is assumed that mtr
holds an x-latch on the tree and on the cursor page. If the insert is
made on the leaf level, to avoid deadlocks, mtr must also own x-latches
to brothers of page, if those brothers exist.
@return DB_SUCCESS or error number */
dberr_t
btr_cur_pessimistic_insert(
/*=======================*/
ulint flags, /*!< in: undo logging and locking flags: if not
zero, the parameter thr should be
specified; if no undo logging is specified,
then the caller must have reserved enough
free extents in the file space so that the
insertion will certainly succeed */
btr_cur_t* cursor, /*!< in: cursor after which to insert;
cursor stays valid */
rec_offs** offsets,/*!< out: offsets on *rec */
mem_heap_t** heap, /*!< in/out: pointer to memory heap
that can be emptied */
dtuple_t* entry, /*!< in/out: entry to insert */
rec_t** rec, /*!< out: pointer to inserted record if
succeed */
big_rec_t** big_rec,/*!< out: big rec vector whose fields have to
be stored externally by the caller */
ulint n_ext, /*!< in: number of externally stored columns */
que_thr_t* thr, /*!< in/out: query thread; can be NULL if
!(~flags
& (BTR_NO_LOCKING_FLAG
| BTR_NO_UNDO_LOG_FLAG)) */
mtr_t* mtr) /*!< in/out: mini-transaction */
{
dict_index_t* index = cursor->index();
big_rec_t* big_rec_vec = NULL;
bool inherit = false;
uint32_t n_reserved = 0;
ut_ad(dtuple_check_typed(entry));
ut_ad(thr || !(~flags & (BTR_NO_LOCKING_FLAG | BTR_NO_UNDO_LOG_FLAG)));
*big_rec = NULL;
ut_ad(mtr->memo_contains_flagged(&index->lock, MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
ut_ad(mtr->memo_contains_flagged(btr_cur_get_block(cursor),
MTR_MEMO_PAGE_X_FIX));
ut_ad(!dict_index_is_online_ddl(index)
|| dict_index_is_clust(index)
|| (flags & BTR_CREATE_FLAG));
cursor->flag = BTR_CUR_BINARY;
/* Check locks and write to undo log, if specified */
dberr_t err = btr_cur_ins_lock_and_undo(flags, cursor, entry,
thr, mtr, &inherit);
if (err != DB_SUCCESS) {
return(err);
}
/* First reserve enough free space for the file segments of
the index tree, so that the insert will not fail because of
lack of space */
if (!index->is_ibuf()
&& (err = fsp_reserve_free_extents(&n_reserved, index->table->space,
uint32_t(cursor->tree_height / 16
+ 3),
FSP_NORMAL, mtr))
!= DB_SUCCESS) {
return err;
}
if (page_zip_rec_needs_ext(rec_get_converted_size(index, entry, n_ext),
index->table->not_redundant(),
dtuple_get_n_fields(entry),
btr_cur_get_block(cursor)->zip_size())
|| UNIV_UNLIKELY(entry->is_alter_metadata()
&& !dfield_is_ext(
dtuple_get_nth_field(
entry,
index->first_user_field())))) {
/* The record is so big that we have to store some fields
externally on separate database pages */
if (UNIV_LIKELY_NULL(big_rec_vec)) {
/* This should never happen, but we handle
the situation in a robust manner. */
ut_ad(0);
dtuple_convert_back_big_rec(index, entry, big_rec_vec);
}
big_rec_vec = dtuple_convert_big_rec(index, 0, entry, &n_ext);
if (big_rec_vec == NULL) {
index->table->space->release_free_extents(n_reserved);
return(DB_TOO_BIG_RECORD);
}
}
*rec = index->page == btr_cur_get_block(cursor)->page.id().page_no()
? btr_root_raise_and_insert(flags, cursor, offsets, heap,
entry, n_ext, mtr, &err)
: btr_page_split_and_insert(flags, cursor, offsets, heap,
entry, n_ext, mtr, &err);
if (!*rec) {
goto func_exit;
}
ut_ad(page_rec_get_next(btr_cur_get_rec(cursor)) == *rec
|| dict_index_is_spatial(index));
if (!(flags & BTR_NO_LOCKING_FLAG)) {
ut_ad(!index->table->is_temporary());
if (dict_index_is_spatial(index)) {
/* Do nothing */
} else {
/* The cursor might be moved to the other page
and the max trx id field should be updated after
the cursor was fixed. */
if (!dict_index_is_clust(index)) {
page_update_max_trx_id(
btr_cur_get_block(cursor),
btr_cur_get_page_zip(cursor),
thr_get_trx(thr)->id, mtr);
}
if (!page_rec_is_infimum(btr_cur_get_rec(cursor))
|| !page_has_prev(btr_cur_get_page(cursor))) {
/* split and inserted need to call
lock_update_insert() always. */
inherit = true;
}
}
}
if (!page_is_leaf(btr_cur_get_page(cursor))) {
ut_ad(!big_rec_vec);
} else {
#ifdef BTR_CUR_HASH_ADAPT
if (entry->info_bits & REC_INFO_MIN_REC_FLAG) {
ut_ad(entry->is_metadata());
ut_ad(index->is_instant());
ut_ad(flags & BTR_NO_LOCKING_FLAG);
ut_ad(!(flags & BTR_CREATE_FLAG));
} else if (index->table->is_temporary()) {
} else {
btr_search_update_hash_on_insert(
cursor, btr_search_sys.get_latch(*index));
}
#endif /* BTR_CUR_HASH_ADAPT */
if (inherit && !(flags & BTR_NO_LOCKING_FLAG)) {
lock_update_insert(btr_cur_get_block(cursor), *rec);
}
}
err = DB_SUCCESS;
func_exit:
index->table->space->release_free_extents(n_reserved);
*big_rec = big_rec_vec;
return err;
}
/*==================== B-TREE UPDATE =========================*/
/*************************************************************//**
For an update, checks the locks and does the undo logging.
@return DB_SUCCESS, DB_LOCK_WAIT, or error number */
UNIV_INLINE MY_ATTRIBUTE((warn_unused_result))
dberr_t
btr_cur_upd_lock_and_undo(
/*======================*/
ulint flags, /*!< in: undo logging and locking flags */
btr_cur_t* cursor, /*!< in: cursor on record to update */
const rec_offs* offsets,/*!< in: rec_get_offsets() on cursor */
const upd_t* update, /*!< in: update vector */
ulint cmpl_info,/*!< in: compiler info on secondary index
updates */
que_thr_t* thr, /*!< in: query thread
(can be NULL if BTR_NO_LOCKING_FLAG) */
mtr_t* mtr, /*!< in/out: mini-transaction */
roll_ptr_t* roll_ptr)/*!< out: roll pointer */
{
dict_index_t* index;
const rec_t* rec;
dberr_t err;
ut_ad((thr != NULL) || (flags & BTR_NO_LOCKING_FLAG));
rec = btr_cur_get_rec(cursor);
index = cursor->index();
ut_ad(rec_offs_validate(rec, index, offsets));
ut_ad(mtr->is_named_space(index->table->space));
if (!dict_index_is_clust(index)) {
ut_ad(dict_index_is_online_ddl(index)
== !!(flags & BTR_CREATE_FLAG));
/* We do undo logging only when we update a clustered index
record */
return(lock_sec_rec_modify_check_and_lock(
flags, btr_cur_get_block(cursor), rec,
index, thr, mtr));
}
/* Check if we have to wait for a lock: enqueue an explicit lock
request if yes */
if (!(flags & BTR_NO_LOCKING_FLAG)) {
err = lock_clust_rec_modify_check_and_lock(
btr_cur_get_block(cursor), rec, index,
offsets, thr);
if (err != DB_SUCCESS) {
return(err);
}
}
/* Append the info about the update in the undo log */
return((flags & BTR_NO_UNDO_LOG_FLAG)
? DB_SUCCESS
: trx_undo_report_row_operation(
thr, index, NULL, update,
cmpl_info, rec, offsets, roll_ptr));
}
/** Write DB_TRX_ID,DB_ROLL_PTR to a clustered index entry.
@param[in,out] entry clustered index entry
@param[in] index clustered index
@param[in] trx_id DB_TRX_ID
@param[in] roll_ptr DB_ROLL_PTR */
static void btr_cur_write_sys(
dtuple_t* entry,
const dict_index_t* index,
trx_id_t trx_id,
roll_ptr_t roll_ptr)
{
dfield_t* t = dtuple_get_nth_field(entry, index->db_trx_id());
ut_ad(t->len == DATA_TRX_ID_LEN);
trx_write_trx_id(static_cast<byte*>(t->data), trx_id);
dfield_t* r = dtuple_get_nth_field(entry, index->db_roll_ptr());
ut_ad(r->len == DATA_ROLL_PTR_LEN);
trx_write_roll_ptr(static_cast<byte*>(r->data), roll_ptr);
}
MY_ATTRIBUTE((warn_unused_result))
/** Update DB_TRX_ID, DB_ROLL_PTR in a clustered index record.
@param[in,out] block clustered index leaf page
@param[in,out] rec clustered index record
@param[in] index clustered index
@param[in] offsets rec_get_offsets(rec, index)
@param[in] trx transaction
@param[in] roll_ptr DB_ROLL_PTR value
@param[in,out] mtr mini-transaction
@return error code */
static dberr_t btr_cur_upd_rec_sys(buf_block_t *block, rec_t *rec,
dict_index_t *index, const rec_offs *offsets,
const trx_t *trx, roll_ptr_t roll_ptr,
mtr_t *mtr)
{
ut_ad(index->is_primary());
ut_ad(rec_offs_validate(rec, index, offsets));
if (UNIV_LIKELY_NULL(block->page.zip.data))
{
page_zip_write_trx_id_and_roll_ptr(block, rec, offsets, index->db_trx_id(),
trx->id, roll_ptr, mtr);
return DB_SUCCESS;
}
ulint offset= index->trx_id_offset;
if (!offset)
offset= row_get_trx_id_offset(index, offsets);
compile_time_assert(DATA_TRX_ID + 1 == DATA_ROLL_PTR);
/* During IMPORT the trx id in the record can be in the future, if
the .ibd file is being imported from another instance. During IMPORT
roll_ptr will be 0. */
ut_ad(roll_ptr == 0 ||
lock_check_trx_id_sanity(trx_read_trx_id(rec + offset),
rec, index, offsets));
byte sys[DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN];
trx_write_trx_id(sys, trx->id);
trx_write_roll_ptr(sys + DATA_TRX_ID_LEN, roll_ptr);
ulint d= 0;
const byte *src= nullptr;
byte *dest= rec + offset;
ulint len= DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;
if (UNIV_LIKELY(index->trx_id_offset))
{
const rec_t *prev= page_rec_get_prev_const(rec);
if (UNIV_UNLIKELY(!prev || prev == rec))
return DB_CORRUPTION;
else if (page_rec_is_infimum(prev));
else
for (src= prev + offset; d < DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN; d++)
if (src[d] != sys[d])
break;
if (d > 6 && memcmp(dest, sys, d))
{
/* We save space by replacing a single record
WRITE,page_offset(dest),byte[13]
with two records:
MEMMOVE,page_offset(dest),d(1 byte),offset(1..3 bytes),
WRITE|0x80,0,byte[13-d]
The single WRITE record would be x+13 bytes long, with x>2.
The MEMMOVE record would be up to x+1+3 = x+4 bytes, and the
second WRITE would be 1+1+13-d = 15-d bytes.
The total size is: x+13 versus x+4+15-d = x+19-d bytes.
To save space, we must have d>6, that is, the complete DB_TRX_ID and
the first byte(s) of DB_ROLL_PTR must match the previous record. */
memcpy(dest, src, d);
mtr->memmove(*block, page_offset(dest), page_offset(src), d);
dest+= d;
len-= d;
/* DB_TRX_ID,DB_ROLL_PTR must be unique in each record when
DB_TRX_ID refers to an active transaction. */
ut_ad(len);
}
else
d= 0;
}
if (UNIV_LIKELY(len)) /* extra safety, to avoid corrupting the log */
mtr->memcpy<mtr_t::MAYBE_NOP>(*block, dest, sys + d, len);
return DB_SUCCESS;
}
/*************************************************************//**
See if there is enough place in the page modification log to log
an update-in-place.
@retval false if out of space; IBUF_BITMAP_FREE will be reset
outside mtr if the page was recompressed
@retval true if enough place;
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE if this is
a secondary index leaf page. This has to be done either within the
same mini-transaction, or by invoking ibuf_reset_free_bits() before
mtr_commit(mtr). */
bool
btr_cur_update_alloc_zip_func(
/*==========================*/
page_zip_des_t* page_zip,/*!< in/out: compressed page */
page_cur_t* cursor, /*!< in/out: B-tree page cursor */
#ifdef UNIV_DEBUG
rec_offs* offsets,/*!< in/out: offsets of the cursor record */
#endif /* UNIV_DEBUG */
ulint length, /*!< in: size needed */
bool create, /*!< in: true=delete-and-insert,
false=update-in-place */
mtr_t* mtr) /*!< in/out: mini-transaction */
{
dict_index_t* index = cursor->index;
/* Have a local copy of the variables as these can change
dynamically. */
const page_t* page = page_cur_get_page(cursor);
ut_ad(page_zip == page_cur_get_page_zip(cursor));
ut_ad(!dict_index_is_ibuf(index));
ut_ad(rec_offs_validate(page_cur_get_rec(cursor), index, offsets));
if (page_zip_available(page_zip, dict_index_is_clust(index),
length, create)) {
return(true);
}
if (!page_zip->m_nonempty && !page_has_garbage(page)) {
/* The page has been freshly compressed, so
reorganizing it will not help. */
return(false);
}
if (create && page_is_leaf(page)
&& (length + page_get_data_size(page)
>= dict_index_zip_pad_optimal_page_size(index))) {
return(false);
}
if (btr_page_reorganize(cursor, mtr) == DB_SUCCESS) {
rec_offs_make_valid(page_cur_get_rec(cursor), index,
page_is_leaf(page), offsets);
/* After recompressing a page, we must make sure that the free
bits in the insert buffer bitmap will not exceed the free
space on the page. Because this function will not attempt
recompression unless page_zip_available() fails above, it is
safe to reset the free bits if page_zip_available() fails
again, below. The free bits can safely be reset in a separate
mini-transaction. If page_zip_available() succeeds below, we
can be sure that the btr_page_reorganize() above did not reduce
the free space available on the page. */
if (page_zip_available(page_zip, dict_index_is_clust(index),
length, create)) {
return true;
}
}
if (!dict_index_is_clust(index)
&& !index->table->is_temporary()
&& page_is_leaf(page)) {
ibuf_reset_free_bits(page_cur_get_block(cursor));
}
return(false);
}
/** Apply an update vector to a record. No field size changes are allowed.
This is usually invoked on a clustered index. The only use case for a
secondary index is row_ins_sec_index_entry_by_modify() or its
counterpart in ibuf_insert_to_index_page().
@param[in,out] rec index record
@param[in] index the index of the record
@param[in] offsets rec_get_offsets(rec, index)
@param[in] update update vector
@param[in,out] block index page
@param[in,out] mtr mini-transaction */
void btr_cur_upd_rec_in_place(rec_t *rec, const dict_index_t *index,
const rec_offs *offsets, const upd_t *update,
buf_block_t *block, mtr_t *mtr)
{
ut_ad(rec_offs_validate(rec, index, offsets));
ut_ad(!index->table->skip_alter_undo);
ut_ad(!block->page.zip.data || index->table->not_redundant());
#ifdef UNIV_DEBUG
if (rec_offs_comp(offsets)) {
switch (rec_get_status(rec)) {
case REC_STATUS_ORDINARY:
break;
case REC_STATUS_INSTANT:
ut_ad(index->is_instant());
break;
case REC_STATUS_NODE_PTR:
case REC_STATUS_INFIMUM:
case REC_STATUS_SUPREMUM:
ut_ad("wrong record status in update" == 0);
}
}
#endif /* UNIV_DEBUG */
static_assert(REC_INFO_BITS_SHIFT == 0, "compatibility");
if (UNIV_LIKELY_NULL(block->page.zip.data)) {
ut_ad(rec_offs_comp(offsets));
byte* info_bits = &rec[-REC_NEW_INFO_BITS];
const bool flip_del_mark = (*info_bits ^ update->info_bits)
& REC_INFO_DELETED_FLAG;
*info_bits &= byte(~REC_INFO_BITS_MASK);
*info_bits |= update->info_bits;
if (flip_del_mark) {
page_zip_rec_set_deleted(block, rec, update->info_bits
& REC_INFO_DELETED_FLAG, mtr);
}
} else {
byte* info_bits = &rec[rec_offs_comp(offsets)
? -REC_NEW_INFO_BITS
: -REC_OLD_INFO_BITS];
mtr->write<1,mtr_t::MAYBE_NOP>(*block, info_bits,
(*info_bits
& ~REC_INFO_BITS_MASK)
| update->info_bits);
}
for (ulint i = 0; i < update->n_fields; i++) {
const upd_field_t* uf = upd_get_nth_field(update, i);
if (upd_fld_is_virtual_col(uf) && !index->has_virtual()) {
continue;
}
const ulint n = uf->field_no;
ut_ad(!dfield_is_ext(&uf->new_val)
== !rec_offs_nth_extern(offsets, n));
ut_ad(!rec_offs_nth_default(offsets, n));
if (UNIV_UNLIKELY(dfield_is_null(&uf->new_val))) {
if (rec_offs_nth_sql_null(offsets, n)) {
ut_ad(index->table->is_instant());
ut_ad(n >= index->n_core_fields);
continue;
}
ut_ad(!index->table->not_redundant());
switch (ulint size = rec_get_nth_field_size(rec, n)) {
case 0:
break;
case 1:
mtr->write<1,mtr_t::MAYBE_NOP>(
*block,
rec_get_field_start_offs(rec, n) + rec,
0U);
break;
default:
mtr->memset(
block,
page_offset(rec_get_field_start_offs(
rec, n) + rec),
size, 0);
}
ulint l = rec_get_1byte_offs_flag(rec)
? (n + 1) : (n + 1) * 2;
byte* b = rec - REC_N_OLD_EXTRA_BYTES - l;
compile_time_assert(REC_1BYTE_SQL_NULL_MASK << 8
== REC_2BYTE_SQL_NULL_MASK);
mtr->write<1>(*block, b,
byte(*b | REC_1BYTE_SQL_NULL_MASK));
continue;
}
ulint len;
byte* data = rec_get_nth_field(rec, offsets, n, &len);
if (UNIV_LIKELY_NULL(block->page.zip.data)) {
ut_ad(len == uf->new_val.len);
memcpy(data, uf->new_val.data, len);
continue;
}
if (UNIV_UNLIKELY(len != uf->new_val.len)) {
ut_ad(len == UNIV_SQL_NULL);
ut_ad(!rec_offs_comp(offsets));
len = uf->new_val.len;
ut_ad(len == rec_get_nth_field_size(rec, n));
ulint l = rec_get_1byte_offs_flag(rec)
? (n + 1) : (n + 1) * 2;
byte* b = rec - REC_N_OLD_EXTRA_BYTES - l;
compile_time_assert(REC_1BYTE_SQL_NULL_MASK << 8
== REC_2BYTE_SQL_NULL_MASK);
mtr->write<1>(*block, b,
byte(*b & ~REC_1BYTE_SQL_NULL_MASK));
}
if (len) {
mtr->memcpy<mtr_t::MAYBE_NOP>(*block, data,
uf->new_val.data, len);
}
}
if (UNIV_LIKELY_NULL(block->page.zip.data)) {
page_zip_write_rec(block, rec, index, offsets, 0, mtr);
}
}
/*************************************************************//**
Updates a record when the update causes no size changes in its fields.
We assume here that the ordering fields of the record do not change.
@return locking or undo log related error code, or
@retval DB_SUCCESS on success
@retval DB_ZIP_OVERFLOW if there is not enough space left
on the compressed page (IBUF_BITMAP_FREE was reset outside mtr) */
dberr_t
btr_cur_update_in_place(
/*====================*/
ulint flags, /*!< in: undo logging and locking flags */
btr_cur_t* cursor, /*!< in: cursor on the record to update;
cursor stays valid and positioned on the
same record */
rec_offs* offsets,/*!< in/out: offsets on cursor->page_cur.rec */
const upd_t* update, /*!< in: update vector */
ulint cmpl_info,/*!< in: compiler info on secondary index
updates */
que_thr_t* thr, /*!< in: query thread */
trx_id_t trx_id, /*!< in: transaction id */
mtr_t* mtr) /*!< in/out: mini-transaction; if this
is a secondary index, the caller must
mtr_commit(mtr) before latching any
further pages */
{
dict_index_t* index;
dberr_t err;
rec_t* rec;
roll_ptr_t roll_ptr = 0;
ulint was_delete_marked;
ut_ad(page_is_leaf(cursor->page_cur.block->page.frame));
rec = btr_cur_get_rec(cursor);
index = cursor->index();
ut_ad(!index->is_ibuf());
ut_ad(rec_offs_validate(rec, index, offsets));
ut_ad(!!page_rec_is_comp(rec) == dict_table_is_comp(index->table));
ut_ad(trx_id > 0 || (flags & BTR_KEEP_SYS_FLAG)
|| index->table->is_temporary());
/* The insert buffer tree should never be updated in place. */
ut_ad(!dict_index_is_ibuf(index));
ut_ad(dict_index_is_online_ddl(index) == !!(flags & BTR_CREATE_FLAG)
|| dict_index_is_clust(index));
ut_ad(thr_get_trx(thr)->id == trx_id
|| (flags & ulint(~(BTR_KEEP_POS_FLAG | BTR_KEEP_IBUF_BITMAP)))
== (BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG
| BTR_CREATE_FLAG | BTR_KEEP_SYS_FLAG));
ut_ad(fil_page_index_page_check(btr_cur_get_page(cursor)));
ut_ad(btr_page_get_index_id(btr_cur_get_page(cursor)) == index->id);
ut_ad(!(update->info_bits & REC_INFO_MIN_REC_FLAG));
DBUG_LOG("ib_cur",
"update-in-place " << index->name << " (" << index->id
<< ") by " << ib::hex(trx_id) << ": "
<< rec_printer(rec, offsets).str());
buf_block_t* block = btr_cur_get_block(cursor);
page_zip_des_t* page_zip = buf_block_get_page_zip(block);
/* Check that enough space is available on the compressed page. */
if (UNIV_LIKELY_NULL(page_zip)) {
ut_ad(!index->table->is_temporary());
if (!btr_cur_update_alloc_zip(
page_zip, btr_cur_get_page_cur(cursor),
offsets, rec_offs_size(offsets),
false, mtr)) {
return(DB_ZIP_OVERFLOW);
}
rec = btr_cur_get_rec(cursor);
}
/* Do lock checking and undo logging */
err = btr_cur_upd_lock_and_undo(flags, cursor, offsets,
update, cmpl_info,
thr, mtr, &roll_ptr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
/* We may need to update the IBUF_BITMAP_FREE
bits after a reorganize that was done in
btr_cur_update_alloc_zip(). */
goto func_exit;
}
if (!(flags & BTR_KEEP_SYS_FLAG)) {
err = btr_cur_upd_rec_sys(block, rec, index, offsets,
thr_get_trx(thr), roll_ptr, mtr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
goto func_exit;
}
}
was_delete_marked = rec_get_deleted_flag(
rec, page_is_comp(buf_block_get_frame(block)));
/* In delete-marked records, DB_TRX_ID must always refer to an
existing undo log record. */
ut_ad(!was_delete_marked
|| !dict_index_is_clust(index)
|| row_get_rec_trx_id(rec, index, offsets));
#ifdef BTR_CUR_HASH_ADAPT
{
srw_spin_lock* ahi_latch = block->index
? btr_search_sys.get_latch(*index) : NULL;
if (ahi_latch) {
/* TO DO: Can we skip this if none of the fields
index->search_info->curr_n_fields
are being updated? */
/* The function row_upd_changes_ord_field_binary
does not work on a secondary index. */
if (!dict_index_is_clust(index)
|| row_upd_changes_ord_field_binary(
index, update, thr, NULL, NULL)) {
ut_ad(!(update->info_bits
& REC_INFO_MIN_REC_FLAG));
/* Remove possible hash index pointer
to this record */
btr_search_update_hash_on_delete(cursor);
}
ahi_latch->wr_lock(SRW_LOCK_CALL);
}
assert_block_ahi_valid(block);
#endif /* BTR_CUR_HASH_ADAPT */
btr_cur_upd_rec_in_place(rec, index, offsets, update, block,
mtr);
#ifdef BTR_CUR_HASH_ADAPT
if (ahi_latch) {
ahi_latch->wr_unlock();
}
}
#endif /* BTR_CUR_HASH_ADAPT */
if (was_delete_marked
&& !rec_get_deleted_flag(
rec, page_is_comp(buf_block_get_frame(block)))) {
/* The new updated record owns its possible externally
stored fields */
btr_cur_unmark_extern_fields(block, rec, index, offsets, mtr);
}
ut_ad(err == DB_SUCCESS);
func_exit:
if (page_zip
&& !(flags & BTR_KEEP_IBUF_BITMAP)
&& !dict_index_is_clust(index)
&& page_is_leaf(buf_block_get_frame(block))) {
/* Update the free bits in the insert buffer. */
ut_ad(!index->table->is_temporary());
ibuf_update_free_bits_zip(block, mtr);
}
return(err);
}
/** Trim a metadata record during the rollback of instant ALTER TABLE.
@param[in] entry metadata tuple
@param[in] index primary key
@param[in] update update vector for the rollback */
ATTRIBUTE_COLD
static void btr_cur_trim_alter_metadata(dtuple_t* entry,
const dict_index_t* index,
const upd_t* update)
{
ut_ad(index->is_instant());
ut_ad(update->is_alter_metadata());
ut_ad(entry->is_alter_metadata());
ut_ad(update->fields[0].field_no == index->first_user_field());
ut_ad(update->fields[0].new_val.ext);
ut_ad(update->fields[0].new_val.len == FIELD_REF_SIZE);
ut_ad(entry->n_fields - 1 == index->n_fields);
const byte* ptr = static_cast<const byte*>(
update->fields[0].new_val.data);
ut_ad(!mach_read_from_4(ptr + BTR_EXTERN_LEN));
ut_ad(mach_read_from_4(ptr + BTR_EXTERN_LEN + 4) > 4);
ut_ad(mach_read_from_4(ptr + BTR_EXTERN_OFFSET) == FIL_PAGE_DATA);
ut_ad(mach_read_from_4(ptr + BTR_EXTERN_SPACE_ID)
== index->table->space->id);
ulint n_fields = update->fields[1].field_no;
ut_ad(n_fields <= index->n_fields);
if (n_fields != index->n_uniq) {
ut_ad(n_fields
>= index->n_core_fields);
entry->n_fields = n_fields;
return;
}
/* This is based on dict_table_t::deserialise_columns()
and btr_cur_instant_init_low(). */
mtr_t mtr;
mtr.start();
buf_block_t* block = buf_page_get(
page_id_t(index->table->space->id,
mach_read_from_4(ptr + BTR_EXTERN_PAGE_NO)),
0, RW_S_LATCH, &mtr);
if (!block) {
ut_ad("corruption" == 0);
mtr.commit();
return;
}
ut_ad(fil_page_get_type(block->page.frame) == FIL_PAGE_TYPE_BLOB);
ut_ad(mach_read_from_4(&block->page.frame
[FIL_PAGE_DATA + BTR_BLOB_HDR_NEXT_PAGE_NO])
== FIL_NULL);
ut_ad(mach_read_from_4(&block->page.frame
[FIL_PAGE_DATA + BTR_BLOB_HDR_PART_LEN])
== mach_read_from_4(ptr + BTR_EXTERN_LEN + 4));
n_fields = mach_read_from_4(
&block->page.frame[FIL_PAGE_DATA + BTR_BLOB_HDR_SIZE])
+ index->first_user_field();
/* Rollback should not increase the number of fields. */
ut_ad(n_fields <= index->n_fields);
ut_ad(n_fields + 1 <= entry->n_fields);
/* dict_index_t::clear_instant_alter() cannot be invoked while
rollback of an instant ALTER TABLE transaction is in progress
for an is_alter_metadata() record. */
ut_ad(n_fields >= index->n_core_fields);
mtr.commit();
entry->n_fields = n_fields + 1;
}
/** Trim an update tuple due to instant ADD COLUMN, if needed.
For normal records, the trailing instantly added fields that match
the initial default values are omitted.
For the special metadata record on a table on which instant
ADD COLUMN has already been executed, both ADD COLUMN and the
rollback of ADD COLUMN need to be handled specially.
@param[in,out] entry index entry
@param[in] index index
@param[in] update update vector
@param[in] thr execution thread */
static inline
void
btr_cur_trim(
dtuple_t* entry,
const dict_index_t* index,
const upd_t* update,
const que_thr_t* thr)
{
if (!index->is_instant()) {
} else if (UNIV_UNLIKELY(update->is_metadata())) {
/* We are either updating a metadata record
(instant ALTER TABLE on a table where instant ALTER was
already executed) or rolling back such an operation. */
ut_ad(!upd_get_nth_field(update, 0)->orig_len);
ut_ad(entry->is_metadata());
if (thr->graph->trx->in_rollback) {
/* This rollback can occur either as part of
ha_innobase::commit_inplace_alter_table() rolling
back after a failed innobase_add_instant_try(),
or as part of crash recovery. Either way, the
table will be in the data dictionary cache, with
the instantly added columns going to be removed
later in the rollback. */
ut_ad(index->table->cached);
/* The DB_TRX_ID,DB_ROLL_PTR are always last,
and there should be some change to roll back.
The first field in the update vector is the
first instantly added column logged by
innobase_add_instant_try(). */
ut_ad(update->n_fields > 2);
if (update->is_alter_metadata()) {
btr_cur_trim_alter_metadata(
entry, index, update);
return;
}
ut_ad(!entry->is_alter_metadata());
ulint n_fields = upd_get_nth_field(update, 0)
->field_no;
ut_ad(n_fields + 1 >= entry->n_fields);
entry->n_fields = n_fields;
}
} else {
entry->trim(*index);
}
}
/*************************************************************//**
Tries to update a record on a page in an index tree. It is assumed that mtr
holds an x-latch on the page. The operation does not succeed if there is too
little space on the page or if the update would result in too empty a page,
so that tree compression is recommended. We assume here that the ordering
fields of the record do not change.
@return error code, including
@retval DB_SUCCESS on success
@retval DB_OVERFLOW if the updated record does not fit
@retval DB_UNDERFLOW if the page would become too empty
@retval DB_ZIP_OVERFLOW if there is not enough space left
on the compressed page (IBUF_BITMAP_FREE was reset outside mtr) */
dberr_t
btr_cur_optimistic_update(
/*======================*/
ulint flags, /*!< in: undo logging and locking flags */
btr_cur_t* cursor, /*!< in: cursor on the record to update;
cursor stays valid and positioned on the
same record */
rec_offs** offsets,/*!< out: offsets on cursor->page_cur.rec */
mem_heap_t** heap, /*!< in/out: pointer to NULL or memory heap */
const upd_t* update, /*!< in: update vector; this must also
contain trx id and roll ptr fields */
ulint cmpl_info,/*!< in: compiler info on secondary index
updates */
que_thr_t* thr, /*!< in: query thread */
trx_id_t trx_id, /*!< in: transaction id */
mtr_t* mtr) /*!< in/out: mini-transaction; if this
is a secondary index, the caller must
mtr_commit(mtr) before latching any
further pages */
{
dict_index_t* index;
page_cur_t* page_cursor;
dberr_t err;
buf_block_t* block;
page_t* page;
page_zip_des_t* page_zip;
rec_t* rec;
ulint max_size;
ulint new_rec_size;
ulint old_rec_size;
ulint max_ins_size = 0;
dtuple_t* new_entry;
roll_ptr_t roll_ptr;
ulint i;
block = btr_cur_get_block(cursor);
page = buf_block_get_frame(block);
rec = btr_cur_get_rec(cursor);
index = cursor->index();
ut_ad(index->has_locking());
ut_ad(trx_id > 0 || (flags & BTR_KEEP_SYS_FLAG)
|| index->table->is_temporary());
ut_ad(!!page_rec_is_comp(rec) == dict_table_is_comp(index->table));
ut_ad(mtr->memo_contains_flagged(block, MTR_MEMO_PAGE_X_FIX));
/* This is intended only for leaf page updates */
ut_ad(page_is_leaf(page));
/* The insert buffer tree should never be updated in place. */
ut_ad(!dict_index_is_ibuf(index));
ut_ad(dict_index_is_online_ddl(index) == !!(flags & BTR_CREATE_FLAG)
|| dict_index_is_clust(index));
ut_ad(thr_get_trx(thr)->id == trx_id
|| (flags & ulint(~(BTR_KEEP_POS_FLAG | BTR_KEEP_IBUF_BITMAP)))
== (BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG
| BTR_CREATE_FLAG | BTR_KEEP_SYS_FLAG));
ut_ad(fil_page_index_page_check(page));
ut_ad(btr_page_get_index_id(page) == index->id);
*offsets = rec_get_offsets(rec, index, *offsets, index->n_core_fields,
ULINT_UNDEFINED, heap);
#if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
ut_a(!rec_offs_any_null_extern(rec, *offsets)
|| thr_get_trx(thr) == trx_roll_crash_recv_trx);
#endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */
if (UNIV_LIKELY(!update->is_metadata())
&& !row_upd_changes_field_size_or_external(index, *offsets,
update)) {
/* The simplest and the most common case: the update does not
change the size of any field and none of the updated fields is
externally stored in rec or update, and there is enough space
on the compressed page to log the update. */
return(btr_cur_update_in_place(
flags, cursor, *offsets, update,
cmpl_info, thr, trx_id, mtr));
}
if (rec_offs_any_extern(*offsets)) {
any_extern:
ut_ad(!index->is_ibuf());
/* Externally stored fields are treated in pessimistic
update */
/* prefetch siblings of the leaf for the pessimistic
operation. */
btr_cur_prefetch_siblings(block, index);
return(DB_OVERFLOW);
}
if (rec_is_metadata(rec, *index) && index->table->instant) {
goto any_extern;
}
for (i = 0; i < upd_get_n_fields(update); i++) {
if (dfield_is_ext(&upd_get_nth_field(update, i)->new_val)) {
goto any_extern;
}
}
DBUG_LOG("ib_cur",
"update " << index->name << " (" << index->id << ") by "
<< ib::hex(trx_id) << ": "
<< rec_printer(rec, *offsets).str());
page_cursor = btr_cur_get_page_cur(cursor);
if (!*heap) {
*heap = mem_heap_create(
rec_offs_size(*offsets)
+ DTUPLE_EST_ALLOC(rec_offs_n_fields(*offsets)));
}
new_entry = row_rec_to_index_entry(rec, index, *offsets, *heap);
ut_ad(!dtuple_get_n_ext(new_entry));
/* The page containing the clustered index record
corresponding to new_entry is latched in mtr.
Thus the following call is safe. */
row_upd_index_replace_new_col_vals_index_pos(new_entry, index, update,
*heap);
btr_cur_trim(new_entry, index, update, thr);
old_rec_size = rec_offs_size(*offsets);
new_rec_size = rec_get_converted_size(index, new_entry, 0);
page_zip = buf_block_get_page_zip(block);
#ifdef UNIV_ZIP_DEBUG
ut_a(!page_zip || page_zip_validate(page_zip, page, index));
#endif /* UNIV_ZIP_DEBUG */
if (page_zip) {
ut_ad(!index->table->is_temporary());
if (page_zip_rec_needs_ext(new_rec_size, page_is_comp(page),
dict_index_get_n_fields(index),
block->zip_size())) {
goto any_extern;
}
if (!btr_cur_update_alloc_zip(
page_zip, page_cursor, *offsets,
new_rec_size, true, mtr)) {
return(DB_ZIP_OVERFLOW);
}
rec = page_cur_get_rec(page_cursor);
}
/* We limit max record size to 16k even for 64k page size. */
if (new_rec_size >= COMPRESSED_REC_MAX_DATA_SIZE ||
(!dict_table_is_comp(index->table)
&& new_rec_size >= REDUNDANT_REC_MAX_DATA_SIZE)) {
err = DB_OVERFLOW;
goto func_exit;
}
if (UNIV_UNLIKELY(new_rec_size
>= (page_get_free_space_of_empty(page_is_comp(page))
/ 2))) {
/* We may need to update the IBUF_BITMAP_FREE
bits after a reorganize that was done in
btr_cur_update_alloc_zip(). */
err = DB_OVERFLOW;
goto func_exit;
}
if (UNIV_UNLIKELY(page_get_data_size(page)
- old_rec_size + new_rec_size
< BTR_CUR_PAGE_COMPRESS_LIMIT(index))) {
/* We may need to update the IBUF_BITMAP_FREE
bits after a reorganize that was done in
btr_cur_update_alloc_zip(). */
/* The page would become too empty */
err = DB_UNDERFLOW;
goto func_exit;
}
/* We do not attempt to reorganize if the page is compressed.
This is because the page may fail to compress after reorganization. */
max_size = page_zip
? page_get_max_insert_size(page, 1)
: (old_rec_size
+ page_get_max_insert_size_after_reorganize(page, 1));
if (!page_zip) {
max_ins_size = page_get_max_insert_size_after_reorganize(
page, 1);
}
if (!(((max_size >= BTR_CUR_PAGE_REORGANIZE_LIMIT)
&& (max_size >= new_rec_size))
|| (page_get_n_recs(page) <= 1))) {
/* We may need to update the IBUF_BITMAP_FREE
bits after a reorganize that was done in
btr_cur_update_alloc_zip(). */
/* There was not enough space, or it did not pay to
reorganize: for simplicity, we decide what to do assuming a
reorganization is needed, though it might not be necessary */
err = DB_OVERFLOW;
goto func_exit;
}
/* Do lock checking and undo logging */
err = btr_cur_upd_lock_and_undo(flags, cursor, *offsets,
update, cmpl_info,
thr, mtr, &roll_ptr);
if (err != DB_SUCCESS) {
/* We may need to update the IBUF_BITMAP_FREE
bits after a reorganize that was done in
btr_cur_update_alloc_zip(). */
goto func_exit;
}
/* Ok, we may do the replacement. Store on the page infimum the
explicit locks on rec, before deleting rec (see the comment in
btr_cur_pessimistic_update). */
if (index->has_locking()) {
lock_rec_store_on_page_infimum(block, rec);
}
if (UNIV_UNLIKELY(update->is_metadata())) {
ut_ad(new_entry->is_metadata());
ut_ad(index->is_instant());
/* This can be innobase_add_instant_try() performing a
subsequent instant ADD COLUMN, or its rollback by
row_undo_mod_clust_low(). */
ut_ad(flags & BTR_NO_LOCKING_FLAG);
} else {
btr_search_update_hash_on_delete(cursor);
}
page_cur_delete_rec(page_cursor, *offsets, mtr);
if (!page_cur_move_to_prev(page_cursor)) {
return DB_CORRUPTION;
}
if (!(flags & BTR_KEEP_SYS_FLAG)) {
btr_cur_write_sys(new_entry, index, trx_id, roll_ptr);
}
rec = btr_cur_insert_if_possible(cursor, new_entry, offsets, heap,
0/*n_ext*/, mtr);
if (UNIV_UNLIKELY(!rec)) {
goto corrupted;
}
if (UNIV_UNLIKELY(update->is_metadata())) {
/* We must empty the PAGE_FREE list, because if this
was a rollback, the shortened metadata record
would have too many fields, and we would be unable to
know the size of the freed record. */
err = btr_page_reorganize(page_cursor, mtr);
if (err != DB_SUCCESS) {
goto func_exit;
}
} else {
/* Restore the old explicit lock state on the record */
lock_rec_restore_from_page_infimum(*block, rec,
block->page.id());
}
ut_ad(err == DB_SUCCESS);
if (!page_cur_move_to_next(page_cursor)) {
corrupted:
err = DB_CORRUPTION;
}
func_exit:
if (!(flags & BTR_KEEP_IBUF_BITMAP)
&& !dict_index_is_clust(index)) {
/* Update the free bits in the insert buffer. */
if (page_zip) {
ut_ad(!index->table->is_temporary());
ibuf_update_free_bits_zip(block, mtr);
} else if (!index->table->is_temporary()) {
ibuf_update_free_bits_low(block, max_ins_size, mtr);
}
}
if (err != DB_SUCCESS) {
/* prefetch siblings of the leaf for the pessimistic
operation. */
btr_cur_prefetch_siblings(block, index);
}
return(err);
}
/*************************************************************//**
If, in a split, a new supremum record was created as the predecessor of the
updated record, the supremum record must inherit exactly the locks on the
updated record. In the split it may have inherited locks from the successor
of the updated record, which is not correct. This function restores the
right locks for the new supremum. */
static
dberr_t
btr_cur_pess_upd_restore_supremum(
/*==============================*/
buf_block_t* block, /*!< in: buffer block of rec */
const rec_t* rec, /*!< in: updated record */
mtr_t* mtr) /*!< in: mtr */
{
page_t* page;
page = buf_block_get_frame(block);
if (page_rec_get_next(page_get_infimum_rec(page)) != rec) {
/* Updated record is not the first user record on its page */
return DB_SUCCESS;
}
const uint32_t prev_page_no = btr_page_get_prev(page);
const page_id_t block_id{block->page.id()};
const page_id_t prev_id(block_id.space(), prev_page_no);
dberr_t err;
buf_block_t* prev_block
= buf_page_get_gen(prev_id, 0, RW_NO_LATCH, nullptr,
BUF_PEEK_IF_IN_POOL, mtr, &err);
/* Since we already held an x-latch on prev_block, it must
be available and not be corrupted unless the buffer pool got
corrupted somehow. */
if (UNIV_UNLIKELY(!prev_block)) {
return err;
}
ut_ad(!memcmp_aligned<4>(prev_block->page.frame + FIL_PAGE_NEXT,
block->page.frame + FIL_PAGE_OFFSET, 4));
/* We must already have an x-latch on prev_block! */
ut_ad(mtr->memo_contains_flagged(prev_block, MTR_MEMO_PAGE_X_FIX));
lock_rec_reset_and_inherit_gap_locks(*prev_block, block_id,
PAGE_HEAP_NO_SUPREMUM,
page_rec_get_heap_no(rec));
return DB_SUCCESS;
}
/*************************************************************//**
Performs an update of a record on a page of a tree. It is assumed
that mtr holds an x-latch on the tree and on the cursor page. If the
update is made on the leaf level, to avoid deadlocks, mtr must also
own x-latches to brothers of page, if those brothers exist. We assume
here that the ordering fields of the record do not change.
@return DB_SUCCESS or error code */
dberr_t
btr_cur_pessimistic_update(
/*=======================*/
ulint flags, /*!< in: undo logging, locking, and rollback
flags */
btr_cur_t* cursor, /*!< in/out: cursor on the record to update;
cursor may become invalid if *big_rec == NULL
|| !(flags & BTR_KEEP_POS_FLAG) */
rec_offs** offsets,/*!< out: offsets on cursor->page_cur.rec */
mem_heap_t** offsets_heap,
/*!< in/out: pointer to memory heap
that can be emptied */
mem_heap_t* entry_heap,
/*!< in/out: memory heap for allocating
big_rec and the index tuple */
big_rec_t** big_rec,/*!< out: big rec vector whose fields have to
be stored externally by the caller */
upd_t* update, /*!< in/out: update vector; this is allowed to
also contain trx id and roll ptr fields.
Non-updated columns that are moved offpage will
be appended to this. */
ulint cmpl_info,/*!< in: compiler info on secondary index
updates */
que_thr_t* thr, /*!< in: query thread */
trx_id_t trx_id, /*!< in: transaction id */
mtr_t* mtr) /*!< in/out: mini-transaction; must be
committed before latching any further pages */
{
big_rec_t* big_rec_vec = NULL;
big_rec_t* dummy_big_rec;
dict_index_t* index;
buf_block_t* block;
page_zip_des_t* page_zip;
rec_t* rec;
page_cur_t* page_cursor;
dberr_t err;
dberr_t optim_err;
roll_ptr_t roll_ptr;
bool was_first;
uint32_t n_reserved = 0;
*offsets = NULL;
*big_rec = NULL;
block = btr_cur_get_block(cursor);
page_zip = buf_block_get_page_zip(block);
index = cursor->index();
ut_ad(index->has_locking());
ut_ad(mtr->memo_contains_flagged(&index->lock, MTR_MEMO_X_LOCK |
MTR_MEMO_SX_LOCK));
ut_ad(mtr->memo_contains_flagged(block, MTR_MEMO_PAGE_X_FIX));
#ifdef UNIV_ZIP_DEBUG
ut_a(!page_zip
|| page_zip_validate(page_zip, block->page.frame, index));
#endif /* UNIV_ZIP_DEBUG */
ut_ad(!page_zip || !index->table->is_temporary());
/* The insert buffer tree should never be updated in place. */
ut_ad(!dict_index_is_ibuf(index));
ut_ad(trx_id > 0 || (flags & BTR_KEEP_SYS_FLAG)
|| index->table->is_temporary());
ut_ad(dict_index_is_online_ddl(index) == !!(flags & BTR_CREATE_FLAG)
|| dict_index_is_clust(index));
ut_ad(thr_get_trx(thr)->id == trx_id
|| (flags & ulint(~BTR_KEEP_POS_FLAG))
== (BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG
| BTR_CREATE_FLAG | BTR_KEEP_SYS_FLAG));
err = optim_err = btr_cur_optimistic_update(
flags | BTR_KEEP_IBUF_BITMAP,
cursor, offsets, offsets_heap, update,
cmpl_info, thr, trx_id, mtr);
switch (err) {
case DB_ZIP_OVERFLOW:
case DB_UNDERFLOW:
case DB_OVERFLOW:
break;
default:
err_exit:
/* We suppressed this with BTR_KEEP_IBUF_BITMAP.
For DB_ZIP_OVERFLOW, the IBUF_BITMAP_FREE bits were
already reset by btr_cur_update_alloc_zip() if the
page was recompressed. */
if (page_zip
&& optim_err != DB_ZIP_OVERFLOW
&& !dict_index_is_clust(index)
&& page_is_leaf(block->page.frame)) {
ut_ad(!index->table->is_temporary());
ibuf_update_free_bits_zip(block, mtr);
}
if (big_rec_vec != NULL) {
dtuple_big_rec_free(big_rec_vec);
}
return(err);
}
rec = btr_cur_get_rec(cursor);
ut_ad(rec_offs_validate(rec, index, *offsets));
dtuple_t* new_entry;
const bool is_metadata = rec_is_metadata(rec, *index);
if (UNIV_UNLIKELY(is_metadata)) {
ut_ad(update->is_metadata());
ut_ad(flags & BTR_NO_LOCKING_FLAG);
ut_ad(index->is_instant());
new_entry = row_metadata_to_tuple(
rec, index, *offsets, entry_heap,
update->info_bits, !thr_get_trx(thr)->in_rollback);
ut_ad(new_entry->n_fields
== ulint(index->n_fields)
+ update->is_alter_metadata());
} else {
new_entry = row_rec_to_index_entry(rec, index, *offsets,
entry_heap);
}
/* The page containing the clustered index record
corresponding to new_entry is latched in mtr. If the
clustered index record is delete-marked, then its externally
stored fields cannot have been purged yet, because then the
purge would also have removed the clustered index record
itself. Thus the following call is safe. */
row_upd_index_replace_new_col_vals_index_pos(new_entry, index, update,
entry_heap);
btr_cur_trim(new_entry, index, update, thr);
/* We have to set appropriate extern storage bits in the new
record to be inserted: we have to remember which fields were such */
ut_ad(!page_is_comp(block->page.frame) || !rec_get_node_ptr_flag(rec));
ut_ad(rec_offs_validate(rec, index, *offsets));
if ((flags & BTR_NO_UNDO_LOG_FLAG)
&& rec_offs_any_extern(*offsets)) {
/* We are in a transaction rollback undoing a row
update: we must free possible externally stored fields
which got new values in the update, if they are not
inherited values. They can be inherited if we have
updated the primary key to another value, and then
update it back again. */
ut_ad(big_rec_vec == NULL);
ut_ad(dict_index_is_clust(index));
ut_ad(thr_get_trx(thr)->in_rollback);
DEBUG_SYNC_C("blob_rollback_middle");
btr_rec_free_updated_extern_fields(
index, rec, block, *offsets, update, true, mtr);
}
ulint n_ext = index->is_primary() ? dtuple_get_n_ext(new_entry) : 0;
if (page_zip_rec_needs_ext(
rec_get_converted_size(index, new_entry, n_ext),
page_is_comp(block->page.frame),
dict_index_get_n_fields(index),
block->zip_size())
|| (UNIV_UNLIKELY(update->is_alter_metadata())
&& !dfield_is_ext(dtuple_get_nth_field(
new_entry,
index->first_user_field())))) {
big_rec_vec = dtuple_convert_big_rec(index, update, new_entry, &n_ext);
if (UNIV_UNLIKELY(big_rec_vec == NULL)) {
/* We cannot goto return_after_reservations,
because we may need to update the
IBUF_BITMAP_FREE bits, which was suppressed by
BTR_KEEP_IBUF_BITMAP. */
#ifdef UNIV_ZIP_DEBUG
ut_a(!page_zip
|| page_zip_validate(page_zip, block->page.frame,
index));
#endif /* UNIV_ZIP_DEBUG */
index->table->space->release_free_extents(n_reserved);
err = DB_TOO_BIG_RECORD;
goto err_exit;
}
ut_ad(page_is_leaf(block->page.frame));
ut_ad(dict_index_is_clust(index));
ut_ad(flags & BTR_KEEP_POS_FLAG);
}
/* Do lock checking and undo logging */
err = btr_cur_upd_lock_and_undo(flags, cursor, *offsets,
update, cmpl_info,
thr, mtr, &roll_ptr);
if (err != DB_SUCCESS) {
goto err_exit;
}
if (optim_err == DB_OVERFLOW) {
/* First reserve enough free space for the file segments
of the index tree, so that the update will not fail because
of lack of space */
err = fsp_reserve_free_extents(
&n_reserved, index->table->space,
uint32_t(cursor->tree_height / 16 + 3),
flags & BTR_NO_UNDO_LOG_FLAG
? FSP_CLEANING : FSP_NORMAL,
mtr);
if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
err = DB_OUT_OF_FILE_SPACE;
goto err_exit;
}
}
if (!(flags & BTR_KEEP_SYS_FLAG)) {
btr_cur_write_sys(new_entry, index, trx_id, roll_ptr);
}
const ulint max_ins_size = page_zip
? 0
: page_get_max_insert_size_after_reorganize(block->page.frame,
1);
if (UNIV_UNLIKELY(is_metadata)) {
ut_ad(new_entry->is_metadata());
ut_ad(index->is_instant());
/* This can be innobase_add_instant_try() performing a
subsequent instant ALTER TABLE, or its rollback by
row_undo_mod_clust_low(). */
ut_ad(flags & BTR_NO_LOCKING_FLAG);
} else {
btr_search_update_hash_on_delete(cursor);
/* Store state of explicit locks on rec on the page
infimum record, before deleting rec. The page infimum
acts as a dummy carrier of the locks, taking care also
of lock releases, before we can move the locks back on
the actual record. There is a special case: if we are
inserting on the root page and the insert causes a
call of btr_root_raise_and_insert. Therefore we cannot
in the lock system delete the lock structs set on the
root page even if the root page carries just node
pointers. */
lock_rec_store_on_page_infimum(block, rec);
}
#ifdef UNIV_ZIP_DEBUG
ut_a(!page_zip
|| page_zip_validate(page_zip, block->page.frame, index));
#endif /* UNIV_ZIP_DEBUG */
page_cursor = btr_cur_get_page_cur(cursor);
page_cur_delete_rec(page_cursor, *offsets, mtr);
if (!page_cur_move_to_prev(page_cursor)) {
err = DB_CORRUPTION;
goto return_after_reservations;
}
rec = btr_cur_insert_if_possible(cursor, new_entry,
offsets, offsets_heap, n_ext, mtr);
if (rec) {
page_cursor->rec = rec;
if (UNIV_UNLIKELY(is_metadata)) {
/* We must empty the PAGE_FREE list, because if this
was a rollback, the shortened metadata record
would have too many fields, and we would be unable to
know the size of the freed record. */
err = btr_page_reorganize(page_cursor, mtr);
if (err != DB_SUCCESS) {
goto return_after_reservations;
}
rec = page_cursor->rec;
rec_offs_make_valid(rec, index, true, *offsets);
if (page_cursor->block->page.id().page_no()
== index->page) {
btr_set_instant(page_cursor->block, *index,
mtr);
}
} else {
lock_rec_restore_from_page_infimum(
*btr_cur_get_block(cursor), rec,
block->page.id());
}
if (!rec_get_deleted_flag(rec, rec_offs_comp(*offsets))
|| rec_is_alter_metadata(rec, *index)) {
/* The new inserted record owns its possible externally
stored fields */
btr_cur_unmark_extern_fields(btr_cur_get_block(cursor),
rec, index, *offsets, mtr);
} else {
/* In delete-marked records, DB_TRX_ID must
always refer to an existing undo log record. */
ut_ad(row_get_rec_trx_id(rec, index, *offsets));
}
bool adjust = big_rec_vec && (flags & BTR_KEEP_POS_FLAG);
ut_ad(!adjust || page_is_leaf(block->page.frame));
if (btr_cur_compress_if_useful(cursor, adjust, mtr)) {
if (adjust) {
rec_offs_make_valid(page_cursor->rec, index,
true, *offsets);
}
} else if (!dict_index_is_clust(index)
&& page_is_leaf(block->page.frame)) {
/* Update the free bits in the insert buffer.
This is the same block which was skipped by
BTR_KEEP_IBUF_BITMAP. */
if (page_zip) {
ut_ad(!index->table->is_temporary());
ibuf_update_free_bits_zip(block, mtr);
} else if (!index->table->is_temporary()) {
ibuf_update_free_bits_low(block, max_ins_size,
mtr);
}
}
if (!srv_read_only_mode
&& !big_rec_vec
&& page_is_leaf(block->page.frame)
&& !dict_index_is_online_ddl(index)) {
#if 0 // FIXME: this used to be a no-op, and will cause trouble if enabled
mtr->release(index->lock);
#endif
/* NOTE: We cannot release root block latch here, because it
has segment header and already modified in most of cases.*/
}
err = DB_SUCCESS;
goto return_after_reservations;
} else {
/* If the page is compressed and it initially
compresses very well, and there is a subsequent insert
of a badly-compressing record, it is possible for
btr_cur_optimistic_update() to return DB_UNDERFLOW and
btr_cur_insert_if_possible() to return FALSE. */
ut_a(page_zip || optim_err != DB_UNDERFLOW);
/* Out of space: reset the free bits.
This is the same block which was skipped by
BTR_KEEP_IBUF_BITMAP. */
if (!dict_index_is_clust(index)
&& !index->table->is_temporary()
&& page_is_leaf(block->page.frame)) {
ibuf_reset_free_bits(block);
}
}
if (big_rec_vec != NULL) {
ut_ad(page_is_leaf(block->page.frame));
ut_ad(dict_index_is_clust(index));
ut_ad(flags & BTR_KEEP_POS_FLAG);
/* btr_page_split_and_insert() in
btr_cur_pessimistic_insert() invokes
mtr->release(index->lock).
We must keep the index->lock when we created a
big_rec, so that row_upd_clust_rec() can store the
big_rec in the same mini-transaction. */
ut_ad(mtr->memo_contains_flagged(&index->lock, MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
mtr_sx_lock_index(index, mtr);
}
/* Was the record to be updated positioned as the first user
record on its page? */
was_first = page_cur_is_before_first(page_cursor);
/* Lock checks and undo logging were already performed by
btr_cur_upd_lock_and_undo(). We do not try
btr_cur_optimistic_insert() because
btr_cur_insert_if_possible() already failed above. */
err = btr_cur_pessimistic_insert(BTR_NO_UNDO_LOG_FLAG
| BTR_NO_LOCKING_FLAG
| BTR_KEEP_SYS_FLAG,
cursor, offsets, offsets_heap,
new_entry, &rec,
&dummy_big_rec, n_ext, NULL, mtr);
ut_a(err == DB_SUCCESS);
ut_a(rec);
ut_a(dummy_big_rec == NULL);
ut_ad(rec_offs_validate(rec, cursor->index(), *offsets));
page_cursor->rec = rec;
/* 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 (dict_index_is_sec_or_ibuf(index)
&& !index->table->is_temporary()) {
/* Update PAGE_MAX_TRX_ID in the index page header.
It was not updated by btr_cur_pessimistic_insert()
because of BTR_NO_LOCKING_FLAG. */
page_update_max_trx_id(btr_cur_get_block(cursor),
btr_cur_get_page_zip(cursor),
trx_id, mtr);
}
if (!rec_get_deleted_flag(rec, rec_offs_comp(*offsets))) {
/* The new inserted record owns its possible externally
stored fields */
#ifdef UNIV_ZIP_DEBUG
ut_a(!page_zip
|| page_zip_validate(page_zip, block->page.frame, index));
#endif /* UNIV_ZIP_DEBUG */
btr_cur_unmark_extern_fields(btr_cur_get_block(cursor), rec,
index, *offsets, mtr);
} else {
/* In delete-marked records, DB_TRX_ID must
always refer to an existing undo log record. */
ut_ad(row_get_rec_trx_id(rec, index, *offsets));
}
if (UNIV_UNLIKELY(is_metadata)) {
/* We must empty the PAGE_FREE list, because if this
was a rollback, the shortened metadata record
would have too many fields, and we would be unable to
know the size of the freed record. */
err = btr_page_reorganize(page_cursor, mtr);
if (err != DB_SUCCESS) {
goto return_after_reservations;
}
rec = page_cursor->rec;
} else {
lock_rec_restore_from_page_infimum(
*btr_cur_get_block(cursor), rec, block->page.id());
}
/* If necessary, restore also the correct lock state for a new,
preceding supremum record created in a page split. While the old
record was nonexistent, the supremum might have inherited its locks
from a wrong record. */
if (!was_first) {
err = btr_cur_pess_upd_restore_supremum(
btr_cur_get_block(cursor), rec, mtr);
}
return_after_reservations:
#ifdef UNIV_ZIP_DEBUG
ut_a(err ||
!page_zip || page_zip_validate(btr_cur_get_page_zip(cursor),
btr_cur_get_page(cursor), index));
#endif /* UNIV_ZIP_DEBUG */
index->table->space->release_free_extents(n_reserved);
*big_rec = big_rec_vec;
return(err);
}
/*==================== B-TREE DELETE MARK AND UNMARK ===============*/
/** Modify the delete-mark flag of a record.
@tparam flag the value of the delete-mark flag
@param[in,out] block buffer block
@param[in,out] rec record on a physical index page
@param[in,out] mtr mini-transaction */
template<bool flag>
void btr_rec_set_deleted(buf_block_t *block, rec_t *rec, mtr_t *mtr)
{
if (page_rec_is_comp(rec))
{
byte *b= &rec[-REC_NEW_INFO_BITS];
const byte v= flag
? (*b | REC_INFO_DELETED_FLAG)
: (*b & byte(~REC_INFO_DELETED_FLAG));
if (*b == v);
else if (UNIV_LIKELY_NULL(block->page.zip.data))
{
*b= v;
page_zip_rec_set_deleted(block, rec, flag, mtr);
}
else
mtr->write<1>(*block, b, v);
}
else
{
ut_ad(!block->page.zip.data);
byte *b= &rec[-REC_OLD_INFO_BITS];
const byte v = flag
? (*b | REC_INFO_DELETED_FLAG)
: (*b & byte(~REC_INFO_DELETED_FLAG));
mtr->write<1,mtr_t::MAYBE_NOP>(*block, b, v);
}
}
template void btr_rec_set_deleted<false>(buf_block_t *, rec_t *, mtr_t *);
template void btr_rec_set_deleted<true>(buf_block_t *, rec_t *, mtr_t *);
/***********************************************************//**
Marks a clustered index record deleted. Writes an undo log record to
undo log on this delete marking. Writes in the trx id field the id
of the deleting transaction, and in the roll ptr field pointer to the
undo log record created.
@return DB_SUCCESS, DB_LOCK_WAIT, or error number */
dberr_t
btr_cur_del_mark_set_clust_rec(
/*===========================*/
buf_block_t* block, /*!< in/out: buffer block of the record */
rec_t* rec, /*!< in/out: record */
dict_index_t* index, /*!< in: clustered index of the record */
const rec_offs* offsets,/*!< in: rec_get_offsets(rec) */
que_thr_t* thr, /*!< in: query thread */
const dtuple_t* entry, /*!< in: dtuple for the deleting record, also
contains the virtual cols if there are any */
mtr_t* mtr) /*!< in/out: mini-transaction */
{
roll_ptr_t roll_ptr;
dberr_t err;
trx_t* trx;
ut_ad(dict_index_is_clust(index));
ut_ad(rec_offs_validate(rec, index, offsets));
ut_ad(!!page_rec_is_comp(rec) == dict_table_is_comp(index->table));
ut_ad(buf_block_get_frame(block) == page_align(rec));
ut_ad(page_rec_is_leaf(rec));
ut_ad(mtr->is_named_space(index->table->space));
if (rec_get_deleted_flag(rec, rec_offs_comp(offsets))) {
/* We may already have delete-marked this record
when executing an ON DELETE CASCADE operation. */
ut_ad(row_get_rec_trx_id(rec, index, offsets)
== thr_get_trx(thr)->id);
return(DB_SUCCESS);
}
err = trx_undo_report_row_operation(thr, index,
entry, NULL, 0, rec, offsets,
&roll_ptr);
if (err != DB_SUCCESS) {
return(err);
}
/* The search latch is not needed here, because
the adaptive hash index does not depend on the delete-mark
and the delete-mark is being updated in place. */
btr_rec_set_deleted<true>(block, rec, mtr);
trx = thr_get_trx(thr);
DBUG_LOG("ib_cur",
"delete-mark clust " << index->table->name
<< " (" << index->id << ") by "
<< ib::hex(trx->id) << ": "
<< rec_printer(rec, offsets).str());
return btr_cur_upd_rec_sys(block, rec, index, offsets, trx, roll_ptr,
mtr);
}
/*==================== B-TREE RECORD REMOVE =========================*/
/*************************************************************//**
Tries to compress a page of the tree if it seems useful. It is assumed
that mtr holds an x-latch on the tree and on the cursor page. To avoid
deadlocks, mtr must also own x-latches to brothers of page, if those
brothers exist. NOTE: it is assumed that the caller has reserved enough
free extents so that the compression will always succeed if done!
@return whether compression occurred */
bool
btr_cur_compress_if_useful(
/*=======================*/
btr_cur_t* cursor, /*!< in/out: cursor on the page to compress;
cursor does not stay valid if !adjust and
compression occurs */
bool adjust, /*!< in: whether the cursor position should be
adjusted even when compression occurs */
mtr_t* mtr) /*!< in/out: mini-transaction */
{
ut_ad(mtr->memo_contains_flagged(&cursor->index()->lock,
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK));
ut_ad(mtr->memo_contains_flagged(btr_cur_get_block(cursor),
MTR_MEMO_PAGE_X_FIX));
if (cursor->index()->is_spatial()) {
const trx_t* trx = cursor->rtr_info->thr
? thr_get_trx(cursor->rtr_info->thr)
: NULL;
const buf_block_t* block = btr_cur_get_block(cursor);
/* Check whether page lock prevents the compression */
if (!lock_test_prdt_page_lock(trx, block->page.id())) {
return(false);
}
}
return btr_cur_compress_recommendation(cursor, mtr)
&& btr_compress(cursor, adjust, mtr) == DB_SUCCESS;
}
/*******************************************************//**
Removes the record on which the tree cursor is positioned on a leaf page.
It is assumed that the mtr has an x-latch on the page where the cursor is
positioned, but no latch on the whole tree.
@return error code
@retval DB_FAIL if the page would become too empty */
dberr_t
btr_cur_optimistic_delete(
/*======================*/
btr_cur_t* cursor, /*!< in: cursor on leaf page, on the record to
delete; cursor stays valid: if deletion
succeeds, on function exit it points to the
successor of the deleted record */
ulint flags, /*!< in: BTR_CREATE_FLAG or 0 */
mtr_t* mtr) /*!< in: mtr; if this function returns
TRUE on a leaf page of a secondary
index, the mtr must be committed
before latching any further pages */
{
buf_block_t* block;
rec_t* rec;
mem_heap_t* heap = NULL;
rec_offs offsets_[REC_OFFS_NORMAL_SIZE];
rec_offs* offsets = offsets_;
rec_offs_init(offsets_);
ut_ad(flags == 0 || flags == BTR_CREATE_FLAG);
ut_ad(mtr->memo_contains_flagged(btr_cur_get_block(cursor),
MTR_MEMO_PAGE_X_FIX));
ut_ad(mtr->is_named_space(cursor->index()->table->space));
ut_ad(!cursor->index()->is_dummy);
/* This is intended only for leaf page deletions */
block = btr_cur_get_block(cursor);
ut_ad(block->page.id().space() == cursor->index()->table->space->id);
ut_ad(page_is_leaf(buf_block_get_frame(block)));
ut_ad(!dict_index_is_online_ddl(cursor->index())
|| cursor->index()->is_clust()
|| (flags & BTR_CREATE_FLAG));
rec = btr_cur_get_rec(cursor);
offsets = rec_get_offsets(rec, cursor->index(), offsets,
cursor->index()->n_core_fields,
ULINT_UNDEFINED, &heap);
dberr_t err = DB_SUCCESS;
if (rec_offs_any_extern(offsets)
|| !btr_cur_can_delete_without_compress(cursor,
rec_offs_size(offsets),
mtr)) {
/* prefetch siblings of the leaf for the pessimistic
operation. */
btr_cur_prefetch_siblings(block, cursor->index());
err = DB_FAIL;
goto func_exit;
}
if (UNIV_UNLIKELY(block->page.id().page_no() == cursor->index()->page
&& page_get_n_recs(block->page.frame) == 1
+ (cursor->index()->is_instant()
&& !rec_is_metadata(rec, *cursor->index()))
&& !cursor->index()
->must_avoid_clear_instant_add())) {
/* The whole index (and table) becomes logically empty.
Empty the whole page. That is, if we are deleting the
only user record, also delete the metadata record
if one exists for instant ADD COLUMN (not generic ALTER TABLE).
If we are deleting the metadata record and the
table becomes empty, clean up the whole page. */
dict_index_t* index = cursor->index();
const rec_t* first_rec = page_rec_get_next_const(
page_get_infimum_rec(block->page.frame));
if (UNIV_UNLIKELY(!first_rec)) {
err = DB_CORRUPTION;
goto func_exit;
}
ut_ad(!index->is_instant()
|| rec_is_metadata(first_rec, *index));
const bool is_metadata = rec_is_metadata(rec, *index);
/* We can remove the metadata when rolling back an
instant ALTER TABLE operation, or when deleting the
last user record on the page such that only metadata for
instant ADD COLUMN (not generic ALTER TABLE) remains. */
const bool empty_table = is_metadata
|| !index->is_instant()
|| (first_rec != rec
&& rec_is_add_metadata(first_rec, *index));
if (UNIV_LIKELY(empty_table)) {
if (UNIV_LIKELY(!is_metadata && !flags)) {
lock_update_delete(block, rec);
}
btr_page_empty(block, buf_block_get_page_zip(block),
index, 0, mtr);
if (index->is_instant()) {
/* MDEV-17383: free metadata BLOBs! */
index->clear_instant_alter();
}
page_cur_set_after_last(block,
btr_cur_get_page_cur(cursor));
goto func_exit;
}
}
{
page_t* page = buf_block_get_frame(block);
page_zip_des_t* page_zip= buf_block_get_page_zip(block);
if (UNIV_UNLIKELY(rec_get_info_bits(rec, page_rec_is_comp(rec))
& REC_INFO_MIN_REC_FLAG)) {
/* This should be rolling back instant ADD COLUMN.
If this is a recovered transaction, then
index->is_instant() will hold until the
insert into SYS_COLUMNS is rolled back. */
ut_ad(cursor->index()->table->supports_instant());
ut_ad(cursor->index()->is_primary());
ut_ad(!page_zip);
page_cur_delete_rec(btr_cur_get_page_cur(cursor),
offsets, mtr);
/* We must empty the PAGE_FREE list, because
after rollback, this deleted metadata record
would have too many fields, and we would be
unable to know the size of the freed record. */
err = btr_page_reorganize(btr_cur_get_page_cur(cursor),
mtr);
goto func_exit;
} else {
if (!flags) {
lock_update_delete(block, rec);
}
btr_search_update_hash_on_delete(cursor);
}
if (page_zip) {
#ifdef UNIV_ZIP_DEBUG
ut_a(page_zip_validate(page_zip, page,
cursor->index()));
#endif /* UNIV_ZIP_DEBUG */
page_cur_delete_rec(btr_cur_get_page_cur(cursor),
offsets, mtr);
#ifdef UNIV_ZIP_DEBUG
ut_a(page_zip_validate(page_zip, page,
cursor->index()));
#endif /* UNIV_ZIP_DEBUG */
/* On compressed pages, the IBUF_BITMAP_FREE
space is not affected by deleting (purging)
records, because it is defined as the minimum
of space available *without* reorganize, and
space available in the modification log. */
} else {
const ulint max_ins
= page_get_max_insert_size_after_reorganize(
page, 1);
page_cur_delete_rec(btr_cur_get_page_cur(cursor),
offsets, mtr);
/* The change buffer does not handle inserts
into non-leaf pages, into clustered indexes,
or into the change buffer. */
if (!cursor->index()->is_clust()
&& !cursor->index()->table->is_temporary()
&& !dict_index_is_ibuf(cursor->index())) {
ibuf_update_free_bits_low(block, max_ins, mtr);
}
}
}
func_exit:
if (UNIV_LIKELY_NULL(heap)) {
mem_heap_free(heap);
}
return err;
}
/*************************************************************//**
Removes the record on which the tree cursor is positioned. Tries
to compress the page if its fillfactor drops below a threshold
or if it is the only page on the level. It is assumed that mtr holds
an x-latch on the tree and on the cursor page. To avoid deadlocks,
mtr must also own x-latches to brothers of page, if those brothers
exist.
@return TRUE if compression occurred and FALSE if not or something
wrong. */
ibool
btr_cur_pessimistic_delete(
/*=======================*/
dberr_t* err, /*!< out: DB_SUCCESS or DB_OUT_OF_FILE_SPACE;
the latter may occur because we may have
to update node pointers on upper levels,
and in the case of variable length keys
these may actually grow in size */
ibool has_reserved_extents, /*!< in: TRUE if the
caller has already reserved enough free
extents so that he knows that the operation
will succeed */
btr_cur_t* cursor, /*!< in: cursor on the record to delete;
if compression does not occur, the cursor
stays valid: it points to successor of
deleted record on function exit */
ulint flags, /*!< in: BTR_CREATE_FLAG or 0 */
bool rollback,/*!< in: performing rollback? */
mtr_t* mtr) /*!< in: mtr */
{
buf_block_t* block;
page_t* page;
page_zip_des_t* page_zip;
dict_index_t* index;
rec_t* rec;
uint32_t n_reserved = 0;
ibool ret = FALSE;
mem_heap_t* heap;
rec_offs* offsets;
#ifdef UNIV_DEBUG
bool parent_latched = false;
#endif /* UNIV_DEBUG */
block = btr_cur_get_block(cursor);
page = buf_block_get_frame(block);
index = btr_cur_get_index(cursor);
ut_ad(flags == 0 || flags == BTR_CREATE_FLAG);
ut_ad(!dict_index_is_online_ddl(index)
|| dict_index_is_clust(index)
|| (flags & BTR_CREATE_FLAG));
ut_ad(mtr->memo_contains_flagged(&index->lock, MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
ut_ad(mtr->memo_contains_flagged(block, MTR_MEMO_PAGE_X_FIX));
ut_ad(mtr->is_named_space(index->table->space));
ut_ad(!index->is_dummy);
ut_ad(block->page.id().space() == index->table->space->id);
if (!has_reserved_extents) {
/* First reserve enough free space for the file segments
of the index tree, so that the node pointer updates will
not fail because of lack of space */
uint32_t n_extents = uint32_t(cursor->tree_height / 32 + 1);
*err = fsp_reserve_free_extents(&n_reserved,
index->table->space,
n_extents,
FSP_CLEANING, mtr);
if (UNIV_UNLIKELY(*err != DB_SUCCESS)) {
return(FALSE);
}
}
heap = mem_heap_create(1024);
rec = btr_cur_get_rec(cursor);
page_zip = buf_block_get_page_zip(block);
#ifdef UNIV_ZIP_DEBUG
ut_a(!page_zip || page_zip_validate(page_zip, page, index));
#endif /* UNIV_ZIP_DEBUG */
offsets = rec_get_offsets(rec, index, NULL, page_is_leaf(page)
? index->n_core_fields : 0,
ULINT_UNDEFINED, &heap);
if (rec_offs_any_extern(offsets)) {
btr_rec_free_externally_stored_fields(index,
rec, offsets, block,
rollback, mtr);
#ifdef UNIV_ZIP_DEBUG
ut_a(!page_zip || page_zip_validate(page_zip, page, index));
#endif /* UNIV_ZIP_DEBUG */
}
rec_t* next_rec = NULL;
bool min_mark_next_rec = false;
if (page_is_leaf(page)) {
const bool is_metadata = rec_is_metadata(
rec, page_rec_is_comp(rec));
if (UNIV_UNLIKELY(is_metadata)) {
/* This should be rolling back instant ALTER TABLE.
If this is a recovered transaction, then
index->is_instant() will hold until the
insert into SYS_COLUMNS is rolled back. */
ut_ad(rollback);
ut_ad(index->table->supports_instant());
ut_ad(index->is_primary());
} else if (flags == 0) {
lock_update_delete(block, rec);
}
if (block->page.id().page_no() != index->page) {
if (page_get_n_recs(page) < 2) {
goto discard_page;
}
} else if (page_get_n_recs(page) == 1
+ (index->is_instant() && !is_metadata)
&& !index->must_avoid_clear_instant_add()) {
/* The whole index (and table) becomes logically empty.
Empty the whole page. That is, if we are deleting the
only user record, also delete the metadata record
if one exists for instant ADD COLUMN
(not generic ALTER TABLE).
If we are deleting the metadata record
(in the rollback of instant ALTER TABLE) and the
table becomes empty, clean up the whole page. */
const rec_t* first_rec = page_rec_get_next_const(
page_get_infimum_rec(page));
if (UNIV_UNLIKELY(!first_rec)) {
*err = DB_CORRUPTION;
goto err_exit;
}
ut_ad(!index->is_instant()
|| rec_is_metadata(first_rec, *index));
if (is_metadata || !index->is_instant()
|| (first_rec != rec
&& rec_is_add_metadata(first_rec, *index))) {
btr_page_empty(block, page_zip, index, 0, mtr);
if (index->is_instant()) {
/* MDEV-17383: free metadata BLOBs! */
index->clear_instant_alter();
}
page_cur_set_after_last(
block,
btr_cur_get_page_cur(cursor));
ret = TRUE;
goto return_after_reservations;
}
}
if (UNIV_LIKELY(!is_metadata)) {
btr_search_update_hash_on_delete(cursor);
} else {
page_cur_delete_rec(btr_cur_get_page_cur(cursor),
offsets, mtr);
/* We must empty the PAGE_FREE list, because
after rollback, this deleted metadata record
would carry too many fields, and we would be
unable to know the size of the freed record. */
*err = btr_page_reorganize(btr_cur_get_page_cur(cursor),
mtr);
ut_ad(!ret);
goto err_exit;
}
} else if (UNIV_UNLIKELY(page_rec_is_first(rec, page))) {
if (page_rec_is_last(rec, page)) {
discard_page:
ut_ad(page_get_n_recs(page) == 1);
/* If there is only one record, drop
the whole page. */
btr_discard_page(cursor, mtr);
ret = TRUE;
goto return_after_reservations;
}
if (UNIV_UNLIKELY(!(next_rec = page_rec_get_next(rec)))) {
ut_ad(!ret);
*err = DB_CORRUPTION;
goto err_exit;
}
btr_cur_t cursor;
cursor.page_cur.index = index;
cursor.page_cur.block = block;
if (!page_has_prev(page)) {
/* 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 */
min_mark_next_rec = true;
} else if (index->is_spatial()) {
/* For rtree, if delete the leftmost node pointer,
we need to update parent page. */
rtr_mbr_t father_mbr;
rec_t* father_rec;
rec_offs* offsets;
ulint len;
rtr_page_get_father_block(NULL, heap, mtr, NULL,
&cursor);
father_rec = btr_cur_get_rec(&cursor);
offsets = rec_get_offsets(father_rec, index, NULL,
0, ULINT_UNDEFINED, &heap);
rtr_read_mbr(rec_get_nth_field(
father_rec, offsets, 0, &len), &father_mbr);
rtr_update_mbr_field(&cursor, offsets, NULL,
page, &father_mbr, next_rec, mtr);
ut_d(parent_latched = true);
} else {
/* Otherwise, if we delete the leftmost node pointer
on a page, we have to change the parent node pointer
so that it is equal to the new leftmost node pointer
on the page */
ret = btr_page_get_father(mtr, &cursor);
if (!ret) {
*err = DB_CORRUPTION;
goto err_exit;
}
*err = btr_cur_node_ptr_delete(&cursor, mtr);
if (*err != DB_SUCCESS) {
got_err:
ret = FALSE;
goto err_exit;
}
const ulint level = btr_page_get_level(page);
// FIXME: reuse the node_ptr from above
dtuple_t* node_ptr = dict_index_build_node_ptr(
index, next_rec, block->page.id().page_no(),
heap, level);
*err = btr_insert_on_non_leaf_level(
flags, index, level + 1, node_ptr, mtr);
if (*err != DB_SUCCESS) {
ret = FALSE;
goto got_err;
}
ut_d(parent_latched = true);
}
}
/* SPATIAL INDEX never use U locks; we can allow page merges
while holding X lock on the spatial index tree.
Do not allow merges of non-leaf B-tree pages unless it is
safe to do so. */
{
const bool allow_merge = page_is_leaf(page)
|| dict_index_is_spatial(index)
|| btr_cur_will_modify_tree(
index, page, BTR_INTENTION_DELETE, rec,
btr_node_ptr_max_size(index),
block->zip_size(), mtr);
page_cur_delete_rec(btr_cur_get_page_cur(cursor),
offsets, mtr);
if (min_mark_next_rec) {
btr_set_min_rec_mark(next_rec, *block, mtr);
}
#ifdef UNIV_ZIP_DEBUG
ut_a(!page_zip || page_zip_validate(page_zip, page, index));
#endif /* UNIV_ZIP_DEBUG */
ut_ad(!parent_latched
|| btr_check_node_ptr(index, block, mtr));
if (!ret && btr_cur_compress_recommendation(cursor, mtr)) {
if (UNIV_LIKELY(allow_merge)) {
ret = btr_cur_compress_if_useful(
cursor, FALSE, mtr);
} else {
ib::warn() << "Not merging page "
<< block->page.id()
<< " in index " << index->name
<< " of " << index->table->name;
ut_ad("MDEV-14637" == 0);
}
}
}
return_after_reservations:
*err = DB_SUCCESS;
err_exit:
mem_heap_free(heap);
if (!srv_read_only_mode
&& page_is_leaf(page)
&& !dict_index_is_online_ddl(index)) {
#if 0 // FIXME: this used to be a no-op, and will cause trouble if enabled
mtr->release(index->lock);
#endif
/* NOTE: We cannot release root block latch here, because it
has segment header and already modified in most of cases.*/
}
index->table->space->release_free_extents(n_reserved);
return(ret);
}
/** Delete the node pointer in a parent page.
@param[in,out] parent cursor pointing to parent record
@param[in,out] mtr mini-transaction */
dberr_t btr_cur_node_ptr_delete(btr_cur_t* parent, mtr_t* mtr)
{
ut_ad(mtr->memo_contains_flagged(btr_cur_get_block(parent),
MTR_MEMO_PAGE_X_FIX));
dberr_t err;
ibool compressed = btr_cur_pessimistic_delete(&err, TRUE, parent,
BTR_CREATE_FLAG, false,
mtr);
if (err == DB_SUCCESS && !compressed) {
btr_cur_compress_if_useful(parent, FALSE, mtr);
}
return err;
}
/** Represents the cursor for the number of rows estimation. The
content is used for level-by-level diving and estimation the number of rows
on each level. */
class btr_est_cur_t
{
/* Assume a page like:
records: (inf, a, b, c, d, sup)
index of the record: 0, 1, 2, 3, 4, 5
*/
/** Index of the record where the page cursor stopped on this level
(index in alphabetical order). In the above example, if the search stopped on
record 'c', then nth_rec will be 3. */
ulint m_nth_rec;
/** Number of the records on the page, not counting inf and sup.
In the above example n_recs will be 4. */
ulint m_n_recs;
/** Search tuple */
const dtuple_t &m_tuple;
/** Cursor search mode */
page_cur_mode_t m_mode;
/** Page cursor which is used for search */
page_cur_t m_page_cur;
/** Page id of the page to get on level down, can differ from
m_block->page.id at the moment when the child's page id is already found, but
the child's block has not fetched yet */
page_id_t m_page_id;
/** Current block */
buf_block_t *m_block;
/** mtr savepoint of the current block */
ulint m_savepoint;
/** Page search mode, can differ from m_mode for non-leaf pages, see c-tor
comments for details */
page_cur_mode_t m_page_mode;
/** Matched fields and bytes which are used for on-page search, see
btr_cur_t::(up|low)_(match|bytes) comments for details */
ulint m_up_match= 0;
ulint m_up_bytes= 0;
ulint m_low_match= 0;
ulint m_low_bytes= 0;
public:
btr_est_cur_t(dict_index_t *index, const dtuple_t &tuple,
page_cur_mode_t mode)
: m_tuple(tuple), m_mode(mode),
m_page_id(index->table->space_id, index->page), m_block(nullptr)
{
ut_ad(dict_index_check_search_tuple(index, &tuple));
ut_ad(dtuple_check_typed(&tuple));
m_page_cur.index = index;
/* We use these modified search modes on non-leaf levels of the B-tree.
These let us end up in the right B-tree leaf. In that leaf we use the
original search mode. */
switch (mode) {
case PAGE_CUR_GE:
m_page_mode= PAGE_CUR_L;
break;
case PAGE_CUR_G:
m_page_mode= PAGE_CUR_LE;
break;
default:
#ifdef PAGE_CUR_LE_OR_EXTENDS
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE ||
mode == PAGE_CUR_LE_OR_EXTENDS);
#else /* PAGE_CUR_LE_OR_EXTENDS */
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE);
#endif /* PAGE_CUR_LE_OR_EXTENDS */
m_page_mode= mode;
break;
}
}
/** Retrieve block with m_page_id, release the previously gotten block
if necessary. If this is a left border block cursor and both left and right
border blocks have the same parent, don't unlatch the parent, as it must be
latched to get the right block, and will be unlatched after the right block
is fetched.
@param level distance from the leaf page level; ULINT_UNDEFINED when
fetching the root page
@param mtr mtr
@param right_parent right border block parent, nullptr if the function
is called for the right block itself
@return true on success or false otherwise. */
bool fetch_child(ulint level, mtr_t &mtr, const buf_block_t *right_parent)
{
buf_block_t *parent_block= m_block;
ulint parent_savepoint= m_savepoint;
m_savepoint= mtr_set_savepoint(&mtr);
m_block= btr_block_get(*index(), m_page_id.page_no(), RW_S_LATCH, !level,
&mtr, nullptr);
if (parent_block && parent_block != right_parent)
mtr_release_block_at_savepoint(&mtr, parent_savepoint, parent_block);
return m_block &&
(level == ULINT_UNDEFINED ||
btr_page_get_level(buf_block_get_frame(m_block)) == level);
}
/** Sets page mode for leaves */
void set_page_mode_for_leaves() { m_page_mode= m_mode; }
/** Does search on the current page. If there is no border in m_tuple, then
just move the cursor to the most left or right record.
@param level current level on tree.
@param root_height root height
@param left true if this is left border, false otherwise.
@return true on success, false otherwise. */
bool search_on_page(ulint level, ulint root_height, bool left)
{
if (level != btr_page_get_level(m_block->page.frame))
return false;
m_n_recs= page_get_n_recs(m_block->page.frame);
if (dtuple_get_n_fields(&m_tuple) > 0)
{
m_up_bytes= m_low_bytes= 0;
m_page_cur.block= m_block;
if (page_cur_search_with_match(&m_tuple, m_page_mode,
&m_up_match, &m_low_match, &m_page_cur,
nullptr))
return false;
m_nth_rec= page_rec_get_n_recs_before(page_cur_get_rec(&m_page_cur));
}
else if (left)
{
page_cur_set_before_first(m_block, &m_page_cur);
if (level)
{
if (!page_cur_move_to_next(&m_page_cur))
return false;
m_nth_rec= 1;
}
else
m_nth_rec= 0;
}
else
{
m_nth_rec= m_n_recs;
if (!level)
{
page_cur_set_after_last(m_block, &m_page_cur);
++m_nth_rec;
}
else
{
m_page_cur.block= m_block;
m_page_cur.rec= page_rec_get_nth(m_block->page.frame, m_nth_rec);
}
}
return true;
}
/** Gets page id of the current record child.
@param offsets offsets array.
@param heap heap for offsets array */
void get_child(rec_offs **offsets, mem_heap_t **heap)
{
const rec_t *node_ptr= page_cur_get_rec(&m_page_cur);
/* FIXME: get the child page number directly without computing offsets */
*offsets= rec_get_offsets(node_ptr, index(), *offsets, 0, ULINT_UNDEFINED,
heap);
/* Go to the child node */
m_page_id.set_page_no(btr_node_ptr_get_child_page_no(node_ptr, *offsets));
}
/** @return true if left border should be counted */
bool should_count_the_left_border() const
{
if (dtuple_get_n_fields(&m_tuple) > 0)
{
ut_ad(!page_rec_is_infimum(page_cur_get_rec(&m_page_cur)));
return !page_rec_is_supremum(page_cur_get_rec(&m_page_cur));
}
ut_ad(page_rec_is_infimum(page_cur_get_rec(&m_page_cur)));
return false;
}
/** @return true if right border should be counted */
bool should_count_the_right_border() const
{
if (dtuple_get_n_fields(&m_tuple) > 0)
{
const rec_t *rec= page_cur_get_rec(&m_page_cur);
ut_ad(!(m_mode == PAGE_CUR_L && page_rec_is_supremum(rec)));
return (m_mode == PAGE_CUR_LE /* if the range is '<=' */
/* and the record was found */
&& m_low_match >= dtuple_get_n_fields(&m_tuple)) ||
(m_mode == PAGE_CUR_L /* or if the range is '<' */
/* and there are any records to match the criteria, i.e. if the
minimum record on the tree is 5 and x < 7 is specified then the
cursor will be positioned at 5 and we should count the border,
but if x < 2 is specified, then the cursor will be positioned at
'inf' and we should not count the border */
&& !page_rec_is_infimum(rec));
/* Notice that for "WHERE col <= 'foo'" the server passes to
ha_innobase::records_in_range(): min_key=NULL (left-unbounded) which is
expected max_key='foo' flag=HA_READ_AFTER_KEY (PAGE_CUR_G), which is
unexpected - one would expect flag=HA_READ_KEY_OR_PREV (PAGE_CUR_LE). In
this case the cursor will be positioned on the first record to the right
of the requested one (can also be positioned on the 'sup') and we should
not count the right border. */
}
ut_ad(page_rec_is_supremum(page_cur_get_rec(&m_page_cur)));
/* The range specified is without a right border, just 'x > 123'
or 'x >= 123' and search_on_page() positioned the cursor on the
supremum record on the rightmost page, which must not be counted. */
return false;
}
/** @return index */
const dict_index_t *index() const { return m_page_cur.index; }
/** @return current block */
const buf_block_t *block() const { return m_block; }
/** @return current page id */
page_id_t page_id() const { return m_page_id; }
/** Copies block pointer and savepoint from another btr_est_cur_t in the case
if both left and right border cursors point to the same block.
@param o reference to the other btr_est_cur_t object. */
void set_block(const btr_est_cur_t &o)
{
m_block= o.m_block;
m_savepoint= o.m_savepoint;
}
/** @return current record number. */
ulint nth_rec() const { return m_nth_rec; }
/** @return number of records in the current page. */
ulint n_recs() const { return m_n_recs; }
};
/** Estimate the number of rows between the left record of the path and the
right one(non-inclusive) for the certain level on a B-tree. This function
starts from the page next to the left page and reads a few pages to the right,
counting their records. If we reach the right page quickly then we know exactly
how many records there are between left and right records and we set
is_n_rows_exact to true. After some page is latched, the previous page is
unlatched. If we cannot reach the right page quickly then we calculate the
average number of records in the pages scanned so far and assume that all pages
that we did not scan up to the right page contain the same number of records,
then we multiply that average to the number of pages between right and left
records (which is n_rows_on_prev_level). In this case we set is_n_rows_exact to
false.
@param level current level.
@param left_cur the cursor of the left page.
@param right_page_no right page number.
@param n_rows_on_prev_level number of rows on the previous level.
@param[out] is_n_rows_exact true if exact rows number is returned.
@param[in,out] mtr mtr,
@return number of rows, not including the borders (exact or estimated). */
static ha_rows btr_estimate_n_rows_in_range_on_level(
ulint level, btr_est_cur_t &left_cur, uint32_t right_page_no,
ha_rows n_rows_on_prev_level, bool &is_n_rows_exact, mtr_t &mtr)
{
ha_rows n_rows= 0;
uint n_pages_read= 0;
/* Do not read more than this number of pages in order not to hurt
performance with this code which is just an estimation. If we read this many
pages before reaching right_page_no, then we estimate the average from the
pages scanned so far. */
static constexpr uint n_pages_read_limit= 9;
ulint savepoint= 0;
buf_block_t *block= nullptr;
const dict_index_t *index= left_cur.index();
/* Assume by default that we will scan all pages between left and right(non
inclusive) pages */
is_n_rows_exact= true;
/* Add records from the left page which are to the right of the record which
serves as a left border of the range, if any (we don't include the record
itself in this count). */
if (left_cur.nth_rec() <= left_cur.n_recs())
{
n_rows+= left_cur.n_recs() - left_cur.nth_rec();
}
/* Count the records in the pages between left and right (non inclusive)
pages */
const fil_space_t *space= index->table->space;
page_id_t page_id(space->id,
btr_page_get_next(buf_block_get_frame(left_cur.block())));
if (page_id.page_no() == FIL_NULL)
goto inexact;
do
{
page_t *page;
buf_block_t *prev_block= block;
ulint prev_savepoint= savepoint;
savepoint= mtr_set_savepoint(&mtr);
/* Fetch the page. */
block= btr_block_get(*index, page_id.page_no(), RW_S_LATCH, !level, &mtr,
nullptr);
if (prev_block)
mtr_release_block_at_savepoint(&mtr, prev_savepoint, prev_block);
if (!block || btr_page_get_level(buf_block_get_frame(block)) != level)
goto inexact;
page= buf_block_get_frame(block);
/* It is possible but highly unlikely that the page was originally written
by an old version of InnoDB that did not initialize FIL_PAGE_TYPE on other
than B-tree pages. For example, this could be an almost-empty BLOB page
that happens to contain the magic values in the fields
that we checked above. */
n_pages_read++;
n_rows+= page_get_n_recs(page);
page_id.set_page_no(btr_page_get_next(page));
if (n_pages_read == n_pages_read_limit)
{
/* We read too many pages or we reached the end of the level
without passing through right_page_no. */
goto inexact;
}
} while (page_id.page_no() != right_page_no);
if (block)
mtr_release_block_at_savepoint(&mtr, savepoint, block);
return (n_rows);
inexact:
if (block)
mtr_release_block_at_savepoint(&mtr, savepoint, block);
is_n_rows_exact= false;
/* We did interrupt before reaching right page */
if (n_pages_read > 0)
{
/* The number of pages on this level is
n_rows_on_prev_level, multiply it by the
average number of recs per page so far */
n_rows= n_rows_on_prev_level * n_rows / n_pages_read;
}
else
{
n_rows= 10;
}
return (n_rows);
}
/** Estimates the number of rows in a given index range. Do search in the left
page, then if there are pages between left and right ones, read a few pages to
the right, if the right page is reached, count the exact number of rows without
fetching the right page, the right page will be fetched in the caller of this
function and the amount of its rows will be added. If the right page is not
reached, count the estimated(see btr_estimate_n_rows_in_range_on_level() for
details) rows number, and fetch the right page. If leaves are reached, unlatch
non-leaf pages except the right leaf parent. After the right leaf page is
fetched, commit mtr.
@param[in] index index
@param[in] range_start range start
@param[in] range_end range end
@return estimated number of rows; */
ha_rows btr_estimate_n_rows_in_range(dict_index_t *index,
btr_pos_t *range_start,
btr_pos_t *range_end)
{
DBUG_ENTER("btr_estimate_n_rows_in_range");
if (UNIV_UNLIKELY(index->page == FIL_NULL || index->is_corrupted()))
DBUG_RETURN(0);
ut_ad(index->is_btree());
btr_est_cur_t p1(index, *range_start->tuple, range_start->mode);
btr_est_cur_t p2(index, *range_end->tuple, range_end->mode);
mtr_t mtr;
ulint height;
ulint root_height= 0; /* remove warning */
mem_heap_t *heap= NULL;
rec_offs offsets_[REC_OFFS_NORMAL_SIZE];
rec_offs *offsets= offsets_;
rec_offs_init(offsets_);
mtr.start();
/* Store the position of the tree latch we push to mtr so that we
know how to release it when we have latched leaf node(s) */
ulint savepoint= mtr_set_savepoint(&mtr);
mtr_s_lock_index(index, &mtr);
ha_rows table_n_rows= dict_table_get_n_rows(index->table);
height= ULINT_UNDEFINED;
/* This becomes true when the two paths do not pass through the same pages
anymore. */
bool diverged= false;
/* This is the height, i.e. the number of levels from the root, where paths
are not the same or adjacent any more. */
ulint divergence_height= ULINT_UNDEFINED;
bool should_count_the_left_border= true;
bool should_count_the_right_border= true;
bool is_n_rows_exact= true;
ha_rows n_rows= 0;
/* Loop and search until we arrive at the desired level. */
search_loop:
if (!p1.fetch_child(height, mtr, p2.block()))
goto error;
if (height == ULINT_UNDEFINED)
{
/* We are in the root node */
height= btr_page_get_level(buf_block_get_frame(p1.block()));
root_height= height;
}
if (!height)
{
p1.set_page_mode_for_leaves();
p2.set_page_mode_for_leaves();
}
if (p1.page_id() == p2.page_id())
p2.set_block(p1);
else
{
ut_ad(diverged);
if (divergence_height != ULINT_UNDEFINED) {
/* We need to call p1.search_on_page() here as
btr_estimate_n_rows_in_range_on_level() uses p1.m_n_recs and
p1.m_nth_rec. */
if (!p1.search_on_page(height, root_height, true))
goto error;
n_rows= btr_estimate_n_rows_in_range_on_level(
height, p1, p2.page_id().page_no(), n_rows, is_n_rows_exact, mtr);
}
if (!p2.fetch_child(height, mtr, nullptr))
goto error;
}
if (height == 0)
/* There is no need to unlach non-leaf pages here as they must already be
unlatched in btr_est_cur_t::fetch_child(). Try to search on pages after
index->lock unlatching to decrease contention. */
mtr_release_s_latch_at_savepoint(&mtr, savepoint, &index->lock);
/* There is no need to search on left page if
divergence_height != ULINT_UNDEFINED, as it was already searched before
btr_estimate_n_rows_in_range_on_level() call */
if (divergence_height == ULINT_UNDEFINED &&
!p1.search_on_page(height, root_height, true))
goto error;
if (!p2.search_on_page(height, root_height, false))
goto error;
if (!diverged && (p1.nth_rec() != p2.nth_rec()))
{
ut_ad(p1.page_id() == p2.page_id());
diverged= true;
if (p1.nth_rec() < p2.nth_rec())
{
/* We do not count the borders (nor the left nor the right one), thus
"- 1". */
n_rows= p2.nth_rec() - p1.nth_rec() - 1;
if (n_rows > 0)
{
/* There is at least one row between the two borders pointed to by p1
and p2, so on the level below the slots will point to non-adjacent
pages. */
divergence_height= root_height - height;
}
}
else
{
/* It is possible that p1->nth_rec > p2->nth_rec if, for example, we have
a single page tree which contains (inf, 5, 6, supr) and we select where x
> 20 and x < 30; in this case p1->nth_rec will point to the supr record
and p2->nth_rec will point to 6. */
n_rows= 0;
should_count_the_left_border= false;
should_count_the_right_border= false;
}
}
else if (diverged && divergence_height == ULINT_UNDEFINED)
{
if (p1.nth_rec() < p1.n_recs() || p2.nth_rec() > 1)
{
ut_ad(p1.page_id() != p2.page_id());
divergence_height= root_height - height;
n_rows= 0;
if (p1.nth_rec() < p1.n_recs())
{
n_rows+= p1.n_recs() - p1.nth_rec();
}
if (p2.nth_rec() > 1)
{
n_rows+= p2.nth_rec() - 1;
}
}
}
else if (divergence_height != ULINT_UNDEFINED)
{
/* All records before the right page was already counted. Add records from
p2->page_no which are to the left of the record which servers as a right
border of the range, if any (we don't include the record itself in this
count). */
if (p2.nth_rec() > 1)
n_rows+= p2.nth_rec() - 1;
}
if (height)
{
ut_ad(height > 0);
height--;
p1.get_child(&offsets, &heap);
p2.get_child(&offsets, &heap);
goto search_loop;
}
should_count_the_left_border=
should_count_the_left_border && p1.should_count_the_left_border();
should_count_the_right_border=
should_count_the_right_border && p2.should_count_the_right_border();
mtr.commit();
if (UNIV_LIKELY_NULL(heap))
mem_heap_free(heap);
range_start->page_id= p1.page_id();
range_end->page_id= p2.page_id();
/* Here none of the borders were counted. For example, if on the leaf level
we descended to:
(inf, a, b, c, d, e, f, sup)
^ ^
path1 path2
then n_rows will be 2 (c and d). */
if (is_n_rows_exact)
{
/* Only fiddle to adjust this off-by-one if the number is exact, otherwise
we do much grosser adjustments below. */
/* If both paths end up on the same record on the leaf level. */
if (p1.page_id() == p2.page_id() && p1.nth_rec() == p2.nth_rec())
{
/* n_rows can be > 0 here if the paths were first different and then
converged to the same record on the leaf level.
For example:
SELECT ... LIKE 'wait/synch/rwlock%'
mode1=PAGE_CUR_GE,
tuple1="wait/synch/rwlock"
path1[0]={nth_rec=58, n_recs=58,
page_no=3, page_level=1}
path1[1]={nth_rec=56, n_recs=55,
page_no=119, page_level=0}
mode2=PAGE_CUR_G
tuple2="wait/synch/rwlock"
path2[0]={nth_rec=57, n_recs=57,
page_no=3, page_level=1}
path2[1]={nth_rec=56, n_recs=55,
page_no=119, page_level=0} */
/* If the range is such that we should count both borders, then avoid
counting that record twice - once as a left border and once as a right
border. Some of the borders should not be counted, e.g. [3,3). */
n_rows= should_count_the_left_border && should_count_the_right_border;
}
else
n_rows+= should_count_the_left_border + should_count_the_right_border;
}
if (root_height > divergence_height && !is_n_rows_exact)
/* In trees whose height is > 1 our algorithm tends to underestimate:
multiply the estimate by 2: */
n_rows*= 2;
DBUG_EXECUTE_IF("bug14007649", DBUG_RETURN(n_rows););
/* Do not estimate the number of rows in the range to over 1 / 2 of the
estimated rows in the whole table */
if (n_rows > table_n_rows / 2 && !is_n_rows_exact)
{
n_rows= table_n_rows / 2;
/* If there are just 0 or 1 rows in the table, then we estimate all rows
are in the range */
if (n_rows == 0)
n_rows= table_n_rows;
}
DBUG_RETURN(n_rows);
error:
mtr.commit();
if (UNIV_LIKELY_NULL(heap))
mem_heap_free(heap);
DBUG_RETURN(0);
}
/*================== EXTERNAL STORAGE OF BIG FIELDS ===================*/
/***********************************************************//**
Gets the offset of the pointer to the externally stored part of a field.
@return offset of the pointer to the externally stored part */
static
ulint
btr_rec_get_field_ref_offs(
/*=======================*/
const rec_offs* offsets,/*!< in: array returned by rec_get_offsets() */
ulint n) /*!< in: index of the external field */
{
ulint field_ref_offs;
ulint local_len;
ut_a(rec_offs_nth_extern(offsets, n));
field_ref_offs = rec_get_nth_field_offs(offsets, n, &local_len);
ut_a(len_is_stored(local_len));
ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
return(field_ref_offs + local_len - BTR_EXTERN_FIELD_REF_SIZE);
}
/** Gets a pointer to the externally stored part of a field.
@param rec record
@param offsets rec_get_offsets(rec)
@param n index of the externally stored field
@return pointer to the externally stored part */
#define btr_rec_get_field_ref(rec, offsets, n) \
((rec) + btr_rec_get_field_ref_offs(offsets, n))
/** Gets the externally stored size of a record, in units of a database page.
@param[in] rec record
@param[in] offsets array returned by rec_get_offsets()
@return externally stored part, in units of a database page */
ulint
btr_rec_get_externally_stored_len(
const rec_t* rec,
const rec_offs* offsets)
{
ulint n_fields;
ulint total_extern_len = 0;
ulint i;
ut_ad(!rec_offs_comp(offsets) || !rec_get_node_ptr_flag(rec));
if (!rec_offs_any_extern(offsets)) {
return(0);
}
n_fields = rec_offs_n_fields(offsets);
for (i = 0; i < n_fields; i++) {
if (rec_offs_nth_extern(offsets, i)) {
ulint extern_len = mach_read_from_4(
btr_rec_get_field_ref(rec, offsets, i)
+ BTR_EXTERN_LEN + 4);
total_extern_len += ut_calc_align(
extern_len, ulint(srv_page_size));
}
}
return total_extern_len >> srv_page_size_shift;
}
/*******************************************************************//**
Sets the ownership bit of an externally stored field in a record. */
static
void
btr_cur_set_ownership_of_extern_field(
/*==================================*/
buf_block_t* block, /*!< in/out: index page */
rec_t* rec, /*!< in/out: clustered index record */
dict_index_t* index, /*!< in: index of the page */
const rec_offs* offsets,/*!< in: array returned by rec_get_offsets() */
ulint i, /*!< in: field number */
bool val, /*!< in: value to set */
mtr_t* mtr) /*!< in: mtr, or NULL if not logged */
{
byte* data;
ulint local_len;
ulint byte_val;
data = rec_get_nth_field(rec, offsets, i, &local_len);
ut_ad(rec_offs_nth_extern(offsets, i));
ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
local_len -= BTR_EXTERN_FIELD_REF_SIZE;
byte_val = mach_read_from_1(data + local_len + BTR_EXTERN_LEN);
if (val) {
byte_val &= ~BTR_EXTERN_OWNER_FLAG;
} else {
#if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
ut_a(!(byte_val & BTR_EXTERN_OWNER_FLAG));
#endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */
byte_val |= BTR_EXTERN_OWNER_FLAG;
}
if (UNIV_LIKELY_NULL(block->page.zip.data)) {
mach_write_to_1(data + local_len + BTR_EXTERN_LEN, byte_val);
page_zip_write_blob_ptr(block, rec, index, offsets, i, mtr);
} else {
mtr->write<1,mtr_t::MAYBE_NOP>(*block, data + local_len
+ BTR_EXTERN_LEN, byte_val);
}
}
/*******************************************************************//**
Marks non-updated off-page fields as disowned by this record. The ownership
must be transferred to the updated record which is inserted elsewhere in the
index tree. In purge only the owner of externally stored field is allowed
to free the field. */
void
btr_cur_disown_inherited_fields(
/*============================*/
buf_block_t* block, /*!< in/out: index page */
rec_t* rec, /*!< in/out: record in a clustered index */
dict_index_t* index, /*!< in: index of the page */
const rec_offs* offsets,/*!< in: array returned by rec_get_offsets() */
const upd_t* update, /*!< in: update vector */
mtr_t* mtr) /*!< in/out: mini-transaction */
{
ut_ad(rec_offs_validate(rec, index, offsets));
ut_ad(!rec_offs_comp(offsets) || !rec_get_node_ptr_flag(rec));
ut_ad(rec_offs_any_extern(offsets));
for (uint16_t i = 0; i < rec_offs_n_fields(offsets); i++) {
if (rec_offs_nth_extern(offsets, i)
&& !upd_get_field_by_field_no(update, i, false)) {
btr_cur_set_ownership_of_extern_field(
block, rec, index, offsets, i, false, mtr);
}
}
}
/*******************************************************************//**
Marks all extern fields in a record as owned by the record. This function
should be called if the delete mark of a record is removed: a not delete
marked record always owns all its extern fields. */
static
void
btr_cur_unmark_extern_fields(
/*=========================*/
buf_block_t* block, /*!< in/out: index page */
rec_t* rec, /*!< in/out: record in a clustered index */
dict_index_t* index, /*!< in: index of the page */
const rec_offs* offsets,/*!< in: array returned by rec_get_offsets() */
mtr_t* mtr) /*!< in: mtr, or NULL if not logged */
{
ut_ad(!rec_offs_comp(offsets) || !rec_get_node_ptr_flag(rec));
if (!rec_offs_any_extern(offsets)) {
return;
}
const ulint n = rec_offs_n_fields(offsets);
for (ulint i = 0; i < n; i++) {
if (rec_offs_nth_extern(offsets, i)) {
btr_cur_set_ownership_of_extern_field(
block, rec, index, offsets, i, true, mtr);
}
}
}
/*******************************************************************//**
Returns the length of a BLOB part stored on the header page.
@return part length */
static
uint32_t
btr_blob_get_part_len(
/*==================*/
const byte* blob_header) /*!< in: blob header */
{
return(mach_read_from_4(blob_header + BTR_BLOB_HDR_PART_LEN));
}
/*******************************************************************//**
Returns the page number where the next BLOB part is stored.
@return page number or FIL_NULL if no more pages */
static
uint32_t
btr_blob_get_next_page_no(
/*======================*/
const byte* blob_header) /*!< in: blob header */
{
return(mach_read_from_4(blob_header + BTR_BLOB_HDR_NEXT_PAGE_NO));
}
/** Deallocate a buffer block that was reserved for a BLOB part.
@param block buffer block
@param all flag whether to remove a ROW_FORMAT=COMPRESSED page
@param mtr mini-transaction to commit */
static void btr_blob_free(buf_block_t *block, bool all, mtr_t *mtr)
{
const page_id_t page_id(block->page.id());
ut_ad(mtr->memo_contains_flagged(block, MTR_MEMO_PAGE_X_FIX));
mtr->commit();
buf_pool_t::hash_chain &chain= buf_pool.page_hash.cell_get(page_id.fold());
mysql_mutex_lock(&buf_pool.mutex);
if (buf_page_t *bpage= buf_pool.page_hash.get(page_id, chain))
if (!buf_LRU_free_page(bpage, all) && all && bpage->zip.data)
/* Attempt to deallocate the redundant copy of the uncompressed page
if the whole ROW_FORMAT=COMPRESSED block cannot be deallocted. */
buf_LRU_free_page(bpage, false);
mysql_mutex_unlock(&buf_pool.mutex);
}
/** Helper class used while writing blob pages, during insert or update. */
struct btr_blob_log_check_t {
/** Persistent cursor on a clusterex index record with blobs. */
btr_pcur_t* m_pcur;
/** Mini transaction holding the latches for m_pcur */
mtr_t* m_mtr;
/** rec_get_offsets(rec, index); offset of clust_rec */
const rec_offs* m_offsets;
/** The block containing clustered record */
buf_block_t** m_block;
/** The clustered record pointer */
rec_t** m_rec;
/** The blob operation code */
enum blob_op m_op;
/** Constructor
@param[in] pcur persistent cursor on a clustered
index record with blobs.
@param[in] mtr mini-transaction holding latches for
pcur.
@param[in] offsets offsets of the clust_rec
@param[in,out] block record block containing pcur record
@param[in,out] rec the clustered record pointer
@param[in] op the blob operation code */
btr_blob_log_check_t(
btr_pcur_t* pcur,
mtr_t* mtr,
const rec_offs* offsets,
buf_block_t** block,
rec_t** rec,
enum blob_op op)
: m_pcur(pcur),
m_mtr(mtr),
m_offsets(offsets),
m_block(block),
m_rec(rec),
m_op(op)
{
ut_ad(rec_offs_validate(*m_rec, m_pcur->index(), m_offsets));
ut_ad((*m_block)->page.frame == page_align(*m_rec));
ut_ad(*m_rec == btr_pcur_get_rec(m_pcur));
}
/** Check if there is enough space in log file. Commit and re-start the
mini transaction. */
void check()
{
dict_index_t* index = m_pcur->index();
ulint offs = 0;
uint32_t page_no = FIL_NULL;
if (UNIV_UNLIKELY(m_op == BTR_STORE_INSERT_BULK)) {
offs = page_offset(*m_rec);
page_no = (*m_block)->page.id().page_no();
(*m_block)->page.fix();
ut_ad(page_no != FIL_NULL);
} else {
btr_pcur_store_position(m_pcur, m_mtr);
}
m_mtr->commit();
DEBUG_SYNC_C("blob_write_middle");
log_free_check();
DEBUG_SYNC_C("blob_write_middle_after_check");
const mtr_log_t log_mode = m_mtr->get_log_mode();
m_mtr->start();
m_mtr->set_log_mode(log_mode);
index->set_modified(*m_mtr);
if (UNIV_UNLIKELY(page_no != FIL_NULL)) {
m_pcur->btr_cur.page_cur.block = btr_block_get(
*index, page_no, RW_X_LATCH, false, m_mtr);
/* The page should not be evicted or corrupted while
we are holding a buffer-fix on it. */
m_pcur->btr_cur.page_cur.block->page.unfix();
m_pcur->btr_cur.page_cur.rec
= m_pcur->btr_cur.page_cur.block->page.frame
+ offs;
} else {
ut_ad(m_pcur->rel_pos == BTR_PCUR_ON);
mtr_sx_lock_index(index, m_mtr);
ut_a(m_pcur->restore_position(
BTR_MODIFY_LEAF_ALREADY_LATCHED,
m_mtr) == btr_pcur_t::SAME_ALL);
}
*m_block = btr_pcur_get_block(m_pcur);
*m_rec = btr_pcur_get_rec(m_pcur);
rec_offs_make_valid(*m_rec, index, true,
const_cast<rec_offs*>(m_offsets));
ut_ad(m_mtr->memo_contains_page_flagged(
*m_rec,
MTR_MEMO_PAGE_X_FIX | MTR_MEMO_PAGE_SX_FIX));
ut_ad((m_op == BTR_STORE_INSERT_BULK)
== !m_mtr->memo_contains_flagged(&index->lock,
MTR_MEMO_SX_LOCK
| MTR_MEMO_X_LOCK));
}
};
/*******************************************************************//**
Stores the fields in big_rec_vec to the tablespace and puts pointers to
them in rec. The extern flags in rec will have to be set beforehand.
The fields are stored on pages allocated from leaf node
file segment of the index tree.
TODO: If the allocation extends the tablespace, it will not be redo logged, in
any mini-transaction. Tablespace extension should be redo-logged, so that
recovery will not fail when the big_rec was written to the extended portion of
the file, in case the file was somehow truncated in the crash.
@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
dberr_t
btr_store_big_rec_extern_fields(
/*============================*/
btr_pcur_t* pcur, /*!< in: a persistent cursor */
rec_offs* offsets, /*!< in/out: rec_get_offsets() on
pcur. the "external storage" flags
in offsets will correctly correspond
to rec when this function returns */
const big_rec_t*big_rec_vec, /*!< in: vector containing fields
to be stored externally */
mtr_t* btr_mtr, /*!< in/out: mtr containing the
latches to the clustered index. can be
committed and restarted. */
enum blob_op op) /*! in: operation code */
{
byte* field_ref;
ulint extern_len;
ulint store_len;
ulint i;
mtr_t mtr;
mem_heap_t* heap = NULL;
page_zip_des_t* page_zip;
z_stream c_stream;
dberr_t error = DB_SUCCESS;
dict_index_t* index = pcur->index();
buf_block_t* rec_block = btr_pcur_get_block(pcur);
rec_t* rec = btr_pcur_get_rec(pcur);
ut_ad(rec_offs_validate(rec, index, offsets));
ut_ad(rec_offs_any_extern(offsets));
ut_ad(op == BTR_STORE_INSERT_BULK
|| btr_mtr->memo_contains_flagged(&index->lock, MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
ut_ad(btr_mtr->memo_contains_flagged(rec_block, MTR_MEMO_PAGE_X_FIX));
ut_ad(buf_block_get_frame(rec_block) == page_align(rec));
ut_a(dict_index_is_clust(index));
if (!fil_page_index_page_check(page_align(rec))) {
if (op != BTR_STORE_INSERT_BULK) {
return DB_PAGE_CORRUPTED;
}
}
btr_blob_log_check_t redo_log(pcur, btr_mtr, offsets, &rec_block,
&rec, op);
page_zip = buf_block_get_page_zip(rec_block);
if (page_zip) {
int err;
/* Zlib deflate needs 128 kilobytes for the default
window size, plus 512 << memLevel, plus a few
kilobytes for small objects. We use reduced memLevel
to limit the memory consumption, and preallocate the
heap, hoping to avoid memory fragmentation. */
heap = mem_heap_create(250000);
page_zip_set_alloc(&c_stream, heap);
err = deflateInit2(&c_stream, int(page_zip_level),
Z_DEFLATED, 15, 7, Z_DEFAULT_STRATEGY);
ut_a(err == Z_OK);
}
#if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
/* All pointers to externally stored columns in the record
must either be zero or they must be pointers to inherited
columns, owned by this record or an earlier record version. */
for (i = 0; i < big_rec_vec->n_fields; i++) {
field_ref = btr_rec_get_field_ref(
rec, offsets, big_rec_vec->fields[i].field_no);
ut_a(!(field_ref[BTR_EXTERN_LEN] & BTR_EXTERN_OWNER_FLAG));
/* Either this must be an update in place,
or the BLOB must be inherited, or the BLOB pointer
must be zero (will be written in this function). */
ut_a(op == BTR_STORE_UPDATE
|| (field_ref[BTR_EXTERN_LEN] & BTR_EXTERN_INHERITED_FLAG)
|| !memcmp(field_ref, field_ref_zero,
BTR_EXTERN_FIELD_REF_SIZE));
}
#endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */
/* Space available in compressed page to carry blob data */
const ulint payload_size_zip = rec_block->physical_size()
- FIL_PAGE_DATA;
/* Space available in uncompressed page to carry blob data */
const ulint payload_size = payload_size_zip
- (BTR_BLOB_HDR_SIZE + FIL_PAGE_DATA_END);
/* We have to create a file segment to the tablespace
for each field and put the pointer to the field in rec */
for (i = 0; i < big_rec_vec->n_fields; i++) {
const ulint field_no = big_rec_vec->fields[i].field_no;
field_ref = btr_rec_get_field_ref(rec, offsets, field_no);
#if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
/* A zero BLOB pointer should have been initially inserted. */
ut_a(!memcmp(field_ref, field_ref_zero,
BTR_EXTERN_FIELD_REF_SIZE));
#endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */
extern_len = big_rec_vec->fields[i].len;
MEM_CHECK_DEFINED(big_rec_vec->fields[i].data, extern_len);
ut_a(extern_len > 0);
uint32_t prev_page_no = FIL_NULL;
if (page_zip) {
int err = deflateReset(&c_stream);
ut_a(err == Z_OK);
c_stream.next_in = (Bytef*)
big_rec_vec->fields[i].data;
c_stream.avail_in = static_cast<uInt>(extern_len);
}
for (ulint blob_npages = 0;; ++blob_npages) {
buf_block_t* block;
const ulint commit_freq = 4;
uint32_t r_extents;
ut_ad(page_align(field_ref) == page_align(rec));
if (!(blob_npages % commit_freq)) {
redo_log.check();
field_ref = btr_rec_get_field_ref(
rec, offsets, field_no);
page_zip = buf_block_get_page_zip(rec_block);
}
mtr.start();
index->set_modified(mtr);
mtr.set_log_mode_sub(*btr_mtr);
rec_block->page.fix();
rec_block->page.lock.x_lock();
mtr.memo_push(rec_block, MTR_MEMO_PAGE_X_FIX);
#ifdef BTR_CUR_HASH_ADAPT
ut_ad(!btr_search_check_marked_free_index(rec_block));
#endif
uint32_t hint_prev = prev_page_no;
if (hint_prev == FIL_NULL) {
hint_prev = rec_block->page.id().page_no();
}
error = fsp_reserve_free_extents(
&r_extents, index->table->space, 1,
FSP_BLOB, &mtr, 1);
if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
alloc_fail:
mtr.commit();
goto func_exit;
}
block = btr_page_alloc(index, hint_prev + 1,
FSP_NO_DIR, 0, &mtr, &mtr,
&error);
index->table->space->release_free_extents(r_extents);
if (!block) {
goto alloc_fail;
}
const uint32_t space_id = block->page.id().space();
const uint32_t page_no = block->page.id().page_no();
if (prev_page_no == FIL_NULL) {
} else if (buf_block_t* prev_block =
buf_page_get_gen(page_id_t(space_id,
prev_page_no),
rec_block->zip_size(),
RW_X_LATCH, nullptr,
BUF_GET, &mtr, &error)) {
if (page_zip) {
mtr.write<4>(*prev_block,
prev_block->page.frame
+ FIL_PAGE_NEXT,
page_no);
memcpy_aligned<4>(
buf_block_get_page_zip(
prev_block)
->data + FIL_PAGE_NEXT,
prev_block->page.frame
+ FIL_PAGE_NEXT, 4);
} else {
mtr.write<4>(*prev_block,
BTR_BLOB_HDR_NEXT_PAGE_NO
+ FIL_PAGE_DATA
+ prev_block->page.frame,
page_no);
}
} else {
goto alloc_fail;
}
ut_ad(!page_has_siblings(block->page.frame));
ut_ad(!fil_page_get_type(block->page.frame));
if (page_zip) {
int err;
page_zip_des_t* blob_page_zip;
mtr.write<1>(*block,
FIL_PAGE_TYPE + 1
+ block->page.frame,
prev_page_no == FIL_NULL
? FIL_PAGE_TYPE_ZBLOB
: FIL_PAGE_TYPE_ZBLOB2);
block->page.zip.data[FIL_PAGE_TYPE + 1]
= block->page.frame[FIL_PAGE_TYPE + 1];
c_stream.next_out = block->page.frame
+ FIL_PAGE_DATA;
c_stream.avail_out = static_cast<uInt>(
payload_size_zip);
err = deflate(&c_stream, Z_FINISH);
ut_a(err == Z_OK || err == Z_STREAM_END);
ut_a(err == Z_STREAM_END
|| c_stream.avail_out == 0);
mtr.memcpy(*block,
FIL_PAGE_DATA,
page_zip_get_size(page_zip)
- FIL_PAGE_DATA
- c_stream.avail_out);
/* Copy the page to compressed storage,
because it will be flushed to disk
from there. */
blob_page_zip = buf_block_get_page_zip(block);
ut_ad(blob_page_zip);
ut_ad(page_zip_get_size(blob_page_zip)
== page_zip_get_size(page_zip));
memcpy(blob_page_zip->data, block->page.frame,
page_zip_get_size(page_zip));
if (err == Z_OK && prev_page_no != FIL_NULL) {
goto next_zip_page;
}
if (err == Z_STREAM_END) {
mach_write_to_4(field_ref
+ BTR_EXTERN_LEN, 0);
mach_write_to_4(field_ref
+ BTR_EXTERN_LEN + 4,
c_stream.total_in);
} else {
memset(field_ref + BTR_EXTERN_LEN,
0, 8);
}
if (prev_page_no == FIL_NULL) {
ut_ad(blob_npages == 0);
mach_write_to_4(field_ref
+ BTR_EXTERN_SPACE_ID,
space_id);
mach_write_to_4(field_ref
+ BTR_EXTERN_PAGE_NO,
page_no);
mach_write_to_4(field_ref
+ BTR_EXTERN_OFFSET,
FIL_PAGE_NEXT);
}
/* We compress a page when finish bulk insert.*/
if (UNIV_LIKELY(op != BTR_STORE_INSERT_BULK)) {
page_zip_write_blob_ptr(
rec_block, rec, index, offsets,
field_no, &mtr);
}
next_zip_page:
prev_page_no = page_no;
/* Commit mtr and release the
uncompressed page frame to save memory. */
btr_blob_free(block, FALSE, &mtr);
if (err == Z_STREAM_END) {
break;
}
} else {
mtr.write<1>(*block, FIL_PAGE_TYPE + 1
+ block->page.frame,
FIL_PAGE_TYPE_BLOB);
if (extern_len > payload_size) {
store_len = payload_size;
} else {
store_len = extern_len;
}
mtr.memcpy<mtr_t::MAYBE_NOP>(
*block,
FIL_PAGE_DATA + BTR_BLOB_HDR_SIZE
+ block->page.frame,
static_cast<const byte*>
(big_rec_vec->fields[i].data)
+ big_rec_vec->fields[i].len
- extern_len, store_len);
mtr.write<4>(*block, BTR_BLOB_HDR_PART_LEN
+ FIL_PAGE_DATA
+ block->page.frame,
store_len);
compile_time_assert(FIL_NULL == 0xffffffff);
mtr.memset(block, BTR_BLOB_HDR_NEXT_PAGE_NO
+ FIL_PAGE_DATA, 4, 0xff);
extern_len -= store_len;
ut_ad(!mach_read_from_4(BTR_EXTERN_LEN
+ field_ref));
mtr.write<4>(*rec_block,
BTR_EXTERN_LEN + 4 + field_ref,
big_rec_vec->fields[i].len
- extern_len);
if (prev_page_no == FIL_NULL) {
ut_ad(blob_npages == 0);
mtr.write<4,mtr_t::MAYBE_NOP>(
*rec_block,
field_ref + BTR_EXTERN_SPACE_ID,
space_id);
mtr.write<4>(*rec_block, field_ref
+ BTR_EXTERN_PAGE_NO,
page_no);
mtr.write<4>(*rec_block, field_ref
+ BTR_EXTERN_OFFSET,
FIL_PAGE_DATA);
}
prev_page_no = page_no;
mtr.commit();
if (extern_len == 0) {
break;
}
}
}
DBUG_EXECUTE_IF("btr_store_big_rec_extern",
error = DB_OUT_OF_FILE_SPACE;
goto func_exit;);
rec_offs_make_nth_extern(offsets, field_no);
}
func_exit:
if (page_zip) {
deflateEnd(&c_stream);
}
if (heap != NULL) {
mem_heap_free(heap);
}
#if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
/* All pointers to externally stored columns in the record
must be valid. */
for (i = 0; i < rec_offs_n_fields(offsets); i++) {
if (!rec_offs_nth_extern(offsets, i)) {
continue;
}
field_ref = btr_rec_get_field_ref(rec, offsets, i);
/* The pointer must not be zero if the operation
succeeded. */
ut_a(0 != memcmp(field_ref, field_ref_zero,
BTR_EXTERN_FIELD_REF_SIZE)
|| error != DB_SUCCESS);
/* The column must not be disowned by this record. */
ut_a(!(field_ref[BTR_EXTERN_LEN] & BTR_EXTERN_OWNER_FLAG));
}
#endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */
return(error);
}
/** Check the FIL_PAGE_TYPE on an uncompressed BLOB page.
@param block uncompressed BLOB page
@param op operation
@return whether the type is invalid */
static bool btr_check_blob_fil_page_type(const buf_block_t& block,
const char *op)
{
uint16_t type= fil_page_get_type(block.page.frame);
if (UNIV_LIKELY(type == FIL_PAGE_TYPE_BLOB));
else if (fil_space_t *space= fil_space_t::get(block.page.id().space()))
{
/* Old versions of InnoDB did not initialize FIL_PAGE_TYPE on BLOB
pages. Do not print anything about the type mismatch when reading
a BLOB page that may be from old versions. */
bool fail= space->full_crc32() || DICT_TF_HAS_ATOMIC_BLOBS(space->flags);
if (fail)
sql_print_error("InnoDB: FIL_PAGE_TYPE=%u on BLOB %s file %s page %u",
type, op, space->chain.start->name,
block.page.id().page_no());
space->release();
return fail;
}
return false;
}
/*******************************************************************//**
Frees the space in an externally stored field to the file space
management if the field in data is owned by the externally stored field,
in a rollback we may have the additional condition that the field must
not be inherited. */
void
btr_free_externally_stored_field(
/*=============================*/
dict_index_t* index, /*!< in: index of the data, the index
tree MUST be X-latched; if the tree
height is 1, then also the root page
must be X-latched! (this is relevant
in the case this function is called
from purge where 'data' is located on
an undo log page, not an index
page) */
byte* field_ref, /*!< in/out: field reference */
const rec_t* rec, /*!< in: record containing field_ref, for
page_zip_write_blob_ptr(), or NULL */
const rec_offs* offsets, /*!< in: rec_get_offsets(rec, index),
or NULL */
buf_block_t* block, /*!< in/out: page of field_ref */
ulint i, /*!< in: field number of field_ref;
ignored if rec == NULL */
bool rollback, /*!< in: performing rollback? */
mtr_t* local_mtr) /*!< in: mtr
containing the latch to data an an
X-latch to the index tree */
{
const uint32_t space_id = mach_read_from_4(
field_ref + BTR_EXTERN_SPACE_ID);
ut_ad(index->is_primary());
ut_ad(block->page.lock.have_x());
ut_ad(local_mtr->memo_contains_flagged(&index->lock, MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
ut_ad(local_mtr->memo_contains_page_flagged(field_ref,
MTR_MEMO_PAGE_X_FIX));
ut_ad(!rec || rec_offs_validate(rec, index, offsets));
ut_ad(!rec || field_ref == btr_rec_get_field_ref(rec, offsets, i));
ut_ad(index->table->space_id == index->table->space->id);
ut_ad(local_mtr->is_named_space(index->table->space));
if (UNIV_UNLIKELY(!memcmp(field_ref, field_ref_zero,
BTR_EXTERN_FIELD_REF_SIZE))) {
/* In the rollback, we may encounter a clustered index
record with some unwritten off-page columns. There is
nothing to free then. */
ut_a(rollback);
return;
}
ut_ad(!(mach_read_from_4(field_ref + BTR_EXTERN_LEN)
& ~((BTR_EXTERN_OWNER_FLAG
| BTR_EXTERN_INHERITED_FLAG) << 24)));
ut_ad(space_id == index->table->space_id);
const ulint ext_zip_size = index->table->space->zip_size();
/* !rec holds in a call from purge when field_ref is in an undo page */
ut_ad(rec || !block->page.zip.data);
for (;;) {
mtr_t mtr;
mtr.start();
mtr.set_spaces(*local_mtr);
mtr.set_log_mode_sub(*local_mtr);
ut_ad(!index->table->is_temporary()
|| local_mtr->get_log_mode() == MTR_LOG_NO_REDO);
const uint32_t page_no = mach_read_from_4(
field_ref + BTR_EXTERN_PAGE_NO);
buf_block_t* ext_block;
if (/* There is no external storage data */
page_no == FIL_NULL
/* This field does not own the externally stored field */
|| (mach_read_from_1(field_ref + BTR_EXTERN_LEN)
& BTR_EXTERN_OWNER_FLAG)
/* Rollback and inherited field */
|| (rollback
&& (mach_read_from_1(field_ref + BTR_EXTERN_LEN)
& BTR_EXTERN_INHERITED_FLAG))) {
skip_free:
/* Do not free */
mtr.commit();
return;
}
ext_block = buf_page_get(page_id_t(space_id, page_no),
ext_zip_size, RW_X_LATCH, &mtr);
if (!ext_block) {
goto skip_free;
}
/* The buffer pool block containing the BLOB pointer is
exclusively latched by local_mtr. To satisfy some design
constraints, we must recursively latch it in mtr as well. */
block->fix();
block->page.lock.x_lock();
mtr.memo_push(block, MTR_MEMO_PAGE_X_FIX);
#ifdef BTR_CUR_HASH_ADAPT
ut_ad(!btr_search_check_marked_free_index(block));
#endif
const page_t* page = buf_block_get_frame(ext_block);
if (ext_zip_size) {
/* Note that page_zip will be NULL
in row_purge_upd_exist_or_extern(). */
switch (fil_page_get_type(page)) {
case FIL_PAGE_TYPE_ZBLOB:
case FIL_PAGE_TYPE_ZBLOB2:
break;
default:
MY_ASSERT_UNREACHABLE();
}
const uint32_t next_page_no = mach_read_from_4(
page + FIL_PAGE_NEXT);
btr_page_free(index, ext_block, &mtr, true,
local_mtr->memo_contains(
*index->table->space));
if (UNIV_LIKELY_NULL(block->page.zip.data)) {
mach_write_to_4(field_ref + BTR_EXTERN_PAGE_NO,
next_page_no);
memset(field_ref + BTR_EXTERN_LEN + 4, 0, 4);
page_zip_write_blob_ptr(block, rec, index,
offsets, i, &mtr);
} else {
mtr.write<4>(*block,
BTR_EXTERN_PAGE_NO + field_ref,
next_page_no);
mtr.write<4,mtr_t::MAYBE_NOP>(*block,
BTR_EXTERN_LEN
+ 4 + field_ref,
0U);
}
} else {
ut_ad(!block->page.zip.data);
btr_check_blob_fil_page_type(*ext_block, "purge");
const uint32_t next_page_no = mach_read_from_4(
page + FIL_PAGE_DATA
+ BTR_BLOB_HDR_NEXT_PAGE_NO);
btr_page_free(index, ext_block, &mtr, true,
local_mtr->memo_contains(
*index->table->space));
mtr.write<4>(*block, BTR_EXTERN_PAGE_NO + field_ref,
next_page_no);
/* Zero out the BLOB length. If the server
crashes during the execution of this function,
trx_rollback_all_recovered() could
dereference the half-deleted BLOB, fetching a
wrong prefix for the BLOB. */
mtr.write<4,mtr_t::MAYBE_NOP>(*block,
BTR_EXTERN_LEN + 4
+ field_ref, 0U);
}
/* Commit mtr and release the BLOB block to save memory. */
btr_blob_free(ext_block, TRUE, &mtr);
}
}
/***********************************************************//**
Frees the externally stored fields for a record. */
static
void
btr_rec_free_externally_stored_fields(
/*==================================*/
dict_index_t* index, /*!< in: index of the data, the index
tree MUST be X-latched */
rec_t* rec, /*!< in/out: record */
const rec_offs* offsets,/*!< in: rec_get_offsets(rec, index) */
buf_block_t* block, /*!< in: index page of rec */
bool rollback,/*!< in: performing rollback? */
mtr_t* mtr) /*!< in: mini-transaction handle which contains
an X-latch to record page and to the index
tree */
{
ulint n_fields;
ulint i;
ut_ad(rec_offs_validate(rec, index, offsets));
ut_ad(mtr->memo_contains_page_flagged(rec, MTR_MEMO_PAGE_X_FIX));
ut_ad(index->is_primary());
ut_ad(page_rec_is_leaf(rec));
/* Free possible externally stored fields in the record */
ut_ad(dict_table_is_comp(index->table) == !!rec_offs_comp(offsets));
n_fields = rec_offs_n_fields(offsets);
for (i = 0; i < n_fields; i++) {
if (rec_offs_nth_extern(offsets, i)) {
btr_free_externally_stored_field(
index, btr_rec_get_field_ref(rec, offsets, i),
rec, offsets, block, i, rollback, mtr);
}
}
}
/***********************************************************//**
Frees the externally stored fields for a record, if the field is mentioned
in the update vector. */
static
void
btr_rec_free_updated_extern_fields(
/*===============================*/
dict_index_t* index, /*!< in: index of rec; the index tree MUST be
X-latched */
rec_t* rec, /*!< in/out: record */
buf_block_t* block, /*!< in: index page of rec */
const rec_offs* offsets,/*!< in: rec_get_offsets(rec, index) */
const upd_t* update, /*!< in: update vector */
bool rollback,/*!< in: performing rollback? */
mtr_t* mtr) /*!< in: mini-transaction handle which contains
an X-latch to record page and to the tree */
{
ulint n_fields;
ulint i;
ut_ad(rec_offs_validate(rec, index, offsets));
ut_ad(mtr->memo_contains_page_flagged(rec, MTR_MEMO_PAGE_X_FIX));
/* Free possible externally stored fields in the record */
n_fields = upd_get_n_fields(update);
for (i = 0; i < n_fields; i++) {
const upd_field_t* ufield = upd_get_nth_field(update, i);
if (rec_offs_nth_extern(offsets, ufield->field_no)) {
ulint len;
byte* data = rec_get_nth_field(
rec, offsets, ufield->field_no, &len);
ut_a(len >= BTR_EXTERN_FIELD_REF_SIZE);
btr_free_externally_stored_field(
index, data + len - BTR_EXTERN_FIELD_REF_SIZE,
rec, offsets, block,
ufield->field_no, rollback, mtr);
}
}
}
/*******************************************************************//**
Copies the prefix of an uncompressed BLOB. The clustered index record
that points to this BLOB must be protected by a lock or a page latch.
@return number of bytes written to buf */
static
ulint
btr_copy_blob_prefix(
/*=================*/
byte* buf, /*!< out: the externally stored part of
the field, or a prefix of it */
uint32_t len, /*!< in: length of buf, in bytes */
page_id_t id, /*!< in: page identifier of the first BLOB page */
uint32_t offset) /*!< in: offset on the first BLOB page */
{
ulint copied_len = 0;
for (;;) {
mtr_t mtr;
buf_block_t* block;
const page_t* page;
const byte* blob_header;
ulint part_len;
ulint copy_len;
mtr_start(&mtr);
block = buf_page_get(id, 0, RW_S_LATCH, &mtr);
if (!block || btr_check_blob_fil_page_type(*block, "read")) {
mtr.commit();
return copied_len;
}
page = buf_block_get_frame(block);
blob_header = page + offset;
part_len = btr_blob_get_part_len(blob_header);
copy_len = ut_min(part_len, len - copied_len);
memcpy(buf + copied_len,
blob_header + BTR_BLOB_HDR_SIZE, copy_len);
copied_len += copy_len;
id.set_page_no(btr_blob_get_next_page_no(blob_header));
mtr_commit(&mtr);
if (id.page_no() == FIL_NULL || copy_len != part_len) {
MEM_CHECK_DEFINED(buf, copied_len);
return(copied_len);
}
/* On other BLOB pages except the first the BLOB header
always is at the page data start: */
offset = FIL_PAGE_DATA;
ut_ad(copied_len <= len);
}
}
/** Copies the prefix of a compressed BLOB.
The clustered index record that points to this BLOB must be protected
by a lock or a page latch.
@param[out] buf the externally stored part of the field,
or a prefix of it
@param[in] len length of buf, in bytes
@param[in] zip_size ROW_FORMAT=COMPRESSED page size
@param[in] id page identifier of the BLOB pages
@return number of bytes written to buf */
static
ulint
btr_copy_zblob_prefix(
byte* buf,
uint32_t len,
ulint zip_size,
page_id_t id,
uint32_t offset)
{
ulint page_type = FIL_PAGE_TYPE_ZBLOB;
mem_heap_t* heap;
int err;
z_stream d_stream;
d_stream.next_out = buf;
d_stream.avail_out = static_cast<uInt>(len);
d_stream.next_in = Z_NULL;
d_stream.avail_in = 0;
/* Zlib inflate needs 32 kilobytes for the default
window size, plus a few kilobytes for small objects. */
heap = mem_heap_create(40000);
page_zip_set_alloc(&d_stream, heap);
ut_ad(zip_size);
ut_ad(ut_is_2pow(zip_size));
ut_ad(id.space());
err = inflateInit(&d_stream);
ut_a(err == Z_OK);
for (;;) {
buf_page_t* bpage;
uint32_t next_page_no;
/* There is no latch on bpage directly. Instead,
bpage is protected by the B-tree page latch that
is being held on the clustered index record, or,
in row_merge_copy_blobs(), by an exclusive table lock. */
bpage = buf_page_get_zip(id, zip_size);
if (UNIV_UNLIKELY(!bpage)) {
ib::error() << "Cannot load compressed BLOB " << id;
goto func_exit;
}
if (UNIV_UNLIKELY
(fil_page_get_type(bpage->zip.data) != page_type)) {
ib::error() << "Unexpected type "
<< fil_page_get_type(bpage->zip.data)
<< " of compressed BLOB page " << id;
ut_ad(0);
goto end_of_blob;
}
next_page_no = mach_read_from_4(bpage->zip.data + offset);
if (UNIV_LIKELY(offset == FIL_PAGE_NEXT)) {
/* When the BLOB begins at page header,
the compressed data payload does not
immediately follow the next page pointer. */
offset = FIL_PAGE_DATA;
} else {
offset += 4;
}
d_stream.next_in = bpage->zip.data + offset;
d_stream.avail_in = uInt(zip_size - offset);
err = inflate(&d_stream, Z_NO_FLUSH);
switch (err) {
case Z_OK:
if (!d_stream.avail_out) {
goto end_of_blob;
}
break;
case Z_STREAM_END:
if (next_page_no == FIL_NULL) {
goto end_of_blob;
}
/* fall through */
default:
inflate_error:
ib::error() << "inflate() of compressed BLOB page "
<< id
<< " returned " << err
<< " (" << d_stream.msg << ")";
case Z_BUF_ERROR:
goto end_of_blob;
}
if (next_page_no == FIL_NULL) {
if (!d_stream.avail_in) {
ib::error()
<< "Unexpected end of compressed "
<< "BLOB page " << id;
} else {
err = inflate(&d_stream, Z_FINISH);
switch (err) {
case Z_STREAM_END:
case Z_BUF_ERROR:
break;
default:
goto inflate_error;
}
}
end_of_blob:
bpage->lock.s_unlock();
bpage->unfix();
goto func_exit;
}
bpage->lock.s_unlock();
bpage->unfix();
/* On other BLOB pages except the first
the BLOB header always is at the page header: */
id.set_page_no(next_page_no);
offset = FIL_PAGE_NEXT;
page_type = FIL_PAGE_TYPE_ZBLOB2;
}
func_exit:
inflateEnd(&d_stream);
mem_heap_free(heap);
MEM_CHECK_DEFINED(buf, d_stream.total_out);
return(d_stream.total_out);
}
/** Copies the prefix of an externally stored field of a record.
The clustered index record that points to this BLOB must be protected
by a lock or a page latch.
@param[out] buf the externally stored part of the
field, or a prefix of it
@param[in] len length of buf, in bytes
@param[in] zip_size ROW_FORMAT=COMPRESSED page size, or 0
@param[in] id page identifier of the first BLOB page
@param[in] offset offset on the first BLOB page
@return number of bytes written to buf */
static
ulint
btr_copy_externally_stored_field_prefix_low(
byte* buf,
uint32_t len,
ulint zip_size,
page_id_t id,
uint32_t offset)
{
if (len == 0)
return 0;
return zip_size
? btr_copy_zblob_prefix(buf, len, zip_size, id, offset)
: btr_copy_blob_prefix(buf, len, id, offset);
}
/** Copies the prefix of an externally stored field of a record.
The clustered index record must be protected by a lock or a page latch.
@param[out] buf the field, or a prefix of it
@param[in] len length of buf, in bytes
@param[in] zip_size ROW_FORMAT=COMPRESSED page size, or 0
@param[in] data 'internally' stored part of the field
containing also the reference to the external part; must be protected by
a lock or a page latch
@param[in] local_len length of data, in bytes
@return the length of the copied field, or 0 if the column was being
or has been deleted */
ulint
btr_copy_externally_stored_field_prefix(
byte* buf,
ulint len,
ulint zip_size,
const byte* data,
ulint local_len)
{
ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
local_len -= BTR_EXTERN_FIELD_REF_SIZE;
if (UNIV_UNLIKELY(local_len >= len)) {
memcpy(buf, data, len);
return(len);
}
memcpy(buf, data, local_len);
data += local_len;
ut_a(memcmp(data, field_ref_zero, BTR_EXTERN_FIELD_REF_SIZE));
if (!mach_read_from_4(data + BTR_EXTERN_LEN + 4)) {
/* The externally stored part of the column has been
(partially) deleted. Signal the half-deleted BLOB
to the caller. */
return(0);
}
uint32_t space_id = mach_read_from_4(data + BTR_EXTERN_SPACE_ID);
uint32_t page_no = mach_read_from_4(data + BTR_EXTERN_PAGE_NO);
uint32_t offset = mach_read_from_4(data + BTR_EXTERN_OFFSET);
len -= local_len;
return(local_len
+ btr_copy_externally_stored_field_prefix_low(buf + local_len,
uint32_t(len),
zip_size,
page_id_t(
space_id,
page_no),
offset));
}
/** Copies an externally stored field of a record to mem heap.
The clustered index record must be protected by a lock or a page latch.
@param[out] len length of the whole field
@param[in] data 'internally' stored part of the field
containing also the reference to the external part; must be protected by
a lock or a page latch
@param[in] zip_size ROW_FORMAT=COMPRESSED page size, or 0
@param[in] local_len length of data
@param[in,out] heap mem heap
@return the whole field copied to heap */
byte*
btr_copy_externally_stored_field(
ulint* len,
const byte* data,
ulint zip_size,
ulint local_len,
mem_heap_t* heap)
{
byte* buf;
ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
local_len -= BTR_EXTERN_FIELD_REF_SIZE;
uint32_t space_id = mach_read_from_4(data + local_len
+ BTR_EXTERN_SPACE_ID);
uint32_t page_no = mach_read_from_4(data + local_len
+ BTR_EXTERN_PAGE_NO);
uint32_t offset = mach_read_from_4(data + local_len
+ BTR_EXTERN_OFFSET);
/* Currently a BLOB cannot be bigger than 4 GB; we
leave the 4 upper bytes in the length field unused */
uint32_t extern_len = mach_read_from_4(data + local_len
+ BTR_EXTERN_LEN + 4);
buf = (byte*) mem_heap_alloc(heap, local_len + extern_len);
memcpy(buf, data, local_len);
*len = local_len
+ btr_copy_externally_stored_field_prefix_low(buf + local_len,
extern_len,
zip_size,
page_id_t(
space_id,
page_no),
offset);
return(buf);
}
/** Copies an externally stored field of a record to mem heap.
@param[in] rec record in a clustered index; must be
protected by a lock or a page latch
@param[in] offset array returned by rec_get_offsets()
@param[in] zip_size ROW_FORMAT=COMPRESSED page size, or 0
@param[in] no field number
@param[out] len length of the field
@param[in,out] heap mem heap
@return the field copied to heap, or NULL if the field is incomplete */
byte*
btr_rec_copy_externally_stored_field(
const rec_t* rec,
const rec_offs* offsets,
ulint zip_size,
ulint no,
ulint* len,
mem_heap_t* heap)
{
ulint local_len;
const byte* data;
ut_a(rec_offs_nth_extern(offsets, no));
/* An externally stored field can contain some initial
data from the field, and in the last 20 bytes it has the
space id, page number, and offset where the rest of the
field data is stored, and the data length in addition to
the data stored locally. We may need to store some data
locally to get the local record length above the 128 byte
limit so that field offsets are stored in two bytes, and
the extern bit is available in those two bytes. */
data = rec_get_nth_field(rec, offsets, no, &local_len);
ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
if (UNIV_UNLIKELY
(!memcmp(data + local_len - BTR_EXTERN_FIELD_REF_SIZE,
field_ref_zero, BTR_EXTERN_FIELD_REF_SIZE))) {
/* The externally stored field was not written yet.
This record should only be seen by
trx_rollback_recovered() or any
TRX_ISO_READ_UNCOMMITTED transactions. */
return(NULL);
}
return(btr_copy_externally_stored_field(len, data,
zip_size, local_len, heap));
}
|