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

#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation				// gcc: Class implementation
#endif

#include "xt_config.h"

#if defined(XT_WIN)
#include <windows.h>
#endif

#include <stdlib.h>
#include <time.h>
#include <ctype.h>

#ifdef DRIZZLED
#include <drizzled/common.h>
#include <drizzled/plugin.h>
#include <mysys/my_alloc.h>
#include <mysys/hash.h>
#include <drizzled/field.h>
#include <drizzled/session.h>
#include <drizzled/data_home.h>
#include <drizzled/error.h>
#include <drizzled/table.h>
#include <drizzled/field/timestamp.h>
#include <drizzled/server_includes.h>
#include <drizzled/plugin/info_schema_table.h>
extern "C" char **session_query(Session *session);
#define my_strdup(a,b) strdup(a)

using drizzled::plugin::Registry;
using drizzled::plugin::ColumnInfo;
using drizzled::plugin::InfoSchemaTable;
using drizzled::plugin::InfoSchemaMethods;

#else
#include <mysql/plugin.h>
#include "sql_class.h"
#include "sql_priv.h"
#include "sql_lex.h"
#endif

#include "ha_pbxt.h"
#include "ha_xtsys.h"

#include "strutil_xt.h"
#include "database_xt.h"
#include "cache_xt.h"
#include "trace_xt.h"
#include "heap_xt.h"
#include "myxt_xt.h"
#include "datadic_xt.h"
#ifdef PBMS_ENABLED
#include "pbms_enabled.h"
#endif
#include "tabcache_xt.h"
#include "systab_xt.h"
#include "xaction_xt.h"
#include "backup_xt.h"
#include "heap_xt.h"

#ifdef DEBUG
//#define XT_USE_SYS_PAR_DEBUG_SIZES
//#define PBXT_HANDLER_TRACE
//#define PBXT_TRACE_RETURN
//#define XT_PRINT_INDEX_OPT
//#define XT_SHOW_DUMPS_TRACE
//#define XT_UNIT_TEST
//#define LOAD_TABLE_ON_OPEN
//#define CHECK_TABLE_LOADS

/* Enable to trace the statements executed by the engine: */
//#define TRACE_STATEMENTS

/* Enable to print the trace to the stdout, instead of
 * to the trace log.
 */
//#define PRINT_STATEMENTS
#endif

#ifndef DRIZZLED
static handler	*pbxt_create_handler(handlerton *hton, TABLE_SHARE *table, MEM_ROOT *mem_root);
static int		pbxt_init(void *p);
static int		pbxt_end(void *p);
static int		pbxt_panic(handlerton *hton, enum ha_panic_function flag);
static void		pbxt_drop_database(handlerton *hton, char *path);
static int		pbxt_close_connection(handlerton *hton, THD* thd);
static int		pbxt_commit(handlerton *hton, THD *thd, bool all);
static int		pbxt_rollback(handlerton *hton, THD *thd, bool all);
static int		pbxt_prepare(handlerton *hton, THD *thd, bool all);
static int		pbxt_recover(handlerton *hton, XID *xid_list, uint len);
static int		pbxt_commit_by_xid(handlerton *hton, XID *xid);
static int		pbxt_rollback_by_xid(handlerton *hton, XID *xid);
static int		pbxt_start_consistent_snapshot(handlerton *hton, THD *thd);
#endif
static void		ha_aquire_exclusive_use(XTThreadPtr self, XTSharePtr share, ha_pbxt *mine);
static void		ha_release_exclusive_use(XTThreadPtr self, XTSharePtr share);
static void		ha_close_open_tables(XTThreadPtr self, XTSharePtr share, ha_pbxt *mine);

#ifdef TRACE_STATEMENTS

#ifdef PRINT_STATEMENTS
#define STAT_TRACE(y, x)		printf("%s: %s\n", y ? y->t_name : "-unknown-", x)
#else
#define STAT_TRACE(y, x)		xt_ttraceq(y, x)
#endif

#else

#define STAT_TRACE(y, x)

#endif

#ifdef PBXT_HANDLER_TRACE
#define PBXT_ALLOW_PRINTING

#define XT_TRACE_CALL()				ha_trace_function(__FUNC__, NULL)
#define XT_TRACE_METHOD()			ha_trace_function(__FUNC__, pb_share->sh_table_path->ps_path)

#ifdef PBXT_TRACE_RETURN
#define XT_RETURN(x)				do { printf("%d\n", (int) (x)); return (x); } while (0)
#define XT_RETURN_VOID				do { printf("out\n"); return; } while (0)
#else
#define XT_RETURN(x)				return (x)
#define XT_RETURN_VOID				return
#endif

#else

#define XT_TRACE_CALL()
#define XT_TRACE_METHOD()
#define XT_RETURN(x)				return (x)
#define XT_RETURN_VOID				return

#endif

#ifdef PBXT_ALLOW_PRINTING
#define XT_PRINT0(y, x)				do { XTThreadPtr s = (y); printf("%s " x, s ? s->t_name : "-unknown-"); } while (0)
#define XT_PRINT1(y, x, a)			do { XTThreadPtr s = (y); printf("%s " x, s ? s->t_name : "-unknown-", a); } while (0)
#define XT_PRINT2(y, x, a, b)		do { XTThreadPtr s = (y); printf("%s " x, s ? s->t_name : "-unknown-", a, b); } while (0)
#define XT_PRINT3(y, x, a, b, c)	do { XTThreadPtr s = (y); printf("%s " x, s ? s->t_name : "-unknown-", a, b, c); } while (0)
#else
#define XT_PRINT0(y, x)
#define XT_PRINT1(y, x, a)
#define XT_PRINT2(y, x, a, b)
#define XT_PRINT3(y, x, a, b, c)
#endif


#define TS(x)					(x)->s

handlerton				*pbxt_hton;
bool					pbxt_inited = false;		// Variable for checking the init state of hash
xtBool					pbxt_ignore_case = true;
const char				*pbxt_extensions[]= { ".xtr", ".xtd", ".xtl", ".xti", ".xt", "", NULL };
#ifdef XT_CRASH_DEBUG
xtBool					pbxt_crash_debug = TRUE;
#else
xtBool					pbxt_crash_debug = FALSE;
#endif


/* Variables for pbxt share methods */
static xt_mutex_type	pbxt_database_mutex;		// Prevent a database from being opened while it is being dropped
static XTHashTabPtr		pbxt_share_tables;			// Hash used to track open tables
static char				*pbxt_index_cache_size;
static char				*pbxt_record_cache_size;
static char				*pbxt_log_cache_size;
static char				*pbxt_log_file_threshold;
static char				*pbxt_transaction_buffer_size;
static char				*pbxt_log_buffer_size;
static char				*pbxt_checkpoint_frequency;
static char				*pbxt_data_log_threshold;
static char				*pbxt_data_file_grow_size;
static char				*pbxt_row_file_grow_size;
static int				pbxt_max_threads;
static my_bool			pbxt_support_xa;

#ifndef DRIZZLED
// drizzle complains it's not used
static XTXactEnumXARec	pbxt_xa_enum;
#endif

#ifdef DEBUG
#define XT_SHARE_LOCK_WAIT		5000
#else
#define XT_SHARE_LOCK_WAIT		500
#endif

/* 
 * Lock timeout in 1/1000ths of a second
 */
#define XT_SHARE_LOCK_TIMEOUT	30000

/*
 * -----------------------------------------------------------------------
 * SYSTEM VARIABLES
 *
 */
 
//#define XT_FOR_TEAMDRIVE

typedef struct HAVarParams {
	const char		*vp_var;						/* Variable name. */
	const char		*vp_def;						/* Default value. */
	const char		*vp_min;						/* Minimum allowed value. */
	const char		*vp_max4;						/* Maximum allowed value on 32-bit processors. */
	const char		*vp_max8;						/* Maximum allowed value on 64-bit processors. */
} HAVarParamsRec, *HAVarParamsPtr;

#ifdef XT_USE_SYS_PAR_DEBUG_SIZES
static HAVarParamsRec vp_index_cache_size = { "pbxt_index_cache_size", "32MB", "8MB", "2GB", "2000GB" };
static HAVarParamsRec vp_record_cache_size = { "pbxt_record_cache_size", "32MB", "8MB", "2GB", "2000GB" };
static HAVarParamsRec vp_log_cache_size = { "pbxt_log_cache_size", "16MB", "4MB", "2GB", "2000GB" };
static HAVarParamsRec vp_checkpoint_frequency = { "pbxt_checkpoint_frequency", "28MB", "512K", "1GB", "24GB" };
static HAVarParamsRec vp_log_file_threshold = { "pbxt_log_file_threshold", "32MB", "1MB", "2GB", "256TB" };
static HAVarParamsRec vp_transaction_buffer_size = { "pbxt_transaction_buffer_size", "1MB", "128K", "1GB", "24GB" };
static HAVarParamsRec vp_log_buffer_size = { "pbxt_log_buffer_size", "256K", "128K", "1GB", "24GB" };
static HAVarParamsRec vp_data_log_threshold = { "pbxt_data_log_threshold", "400K", "400K", "2GB", "256TB" };
static HAVarParamsRec vp_data_file_grow_size = { "pbxt_data_file_grow_size", "2MB", "128K", "1GB", "2GB" };
static HAVarParamsRec vp_row_file_grow_size = { "pbxt_row_file_grow_size", "256K", "32K", "1GB", "2GB" };
#define XT_DL_DEFAULT_XLOG_COUNT		3
#define XT_DL_DEFAULT_GARBAGE_LEVEL		10
#else
static HAVarParamsRec vp_index_cache_size = { "pbxt_index_cache_size", "32MB", "8MB", "2GB", "2000GB" };
static HAVarParamsRec vp_record_cache_size = { "pbxt_record_cache_size", "32MB", "8MB", "2GB", "2000GB" };
static HAVarParamsRec vp_log_cache_size = { "pbxt_log_cache_size", "16MB", "4MB", "2GB", "2000GB" };
static HAVarParamsRec vp_checkpoint_frequency = { "pbxt_checkpoint_frequency", "28MB", "512K", "1GB", "24GB" };
static HAVarParamsRec vp_log_file_threshold = { "pbxt_log_file_threshold", "32MB", "1MB", "2GB", "256TB" };
static HAVarParamsRec vp_transaction_buffer_size = { "pbxt_transaction_buffer_size", "1MB", "128K", "1GB", "24GB" };
static HAVarParamsRec vp_log_buffer_size = { "pbxt_log_buffer_size", "256K", "128K", "1GB", "24GB" };
static HAVarParamsRec vp_data_log_threshold = { "pbxt_data_log_threshold", "64MB", "1MB", "2GB", "256TB" };
static HAVarParamsRec vp_data_file_grow_size = { "pbxt_data_file_grow_size", "2MB", "128K", "1GB", "2GB" };
static HAVarParamsRec vp_row_file_grow_size = { "pbxt_row_file_grow_size", "256K", "32K", "1GB", "2GB" };
#define XT_DL_DEFAULT_XLOG_COUNT		3
#define XT_DL_DEFAULT_GARBAGE_LEVEL		50
#endif

#define XT_AUTO_INCREMENT_DEF			0

#ifdef XT_MAC
#ifdef DEBUG
/* For debugging on the Mac, we check the re-use logs: */
#define XT_OFFLINE_LOG_FUNCTION_DEF		XT_RECYCLE_LOGS
#else
#define XT_OFFLINE_LOG_FUNCTION_DEF		XT_DELETE_LOGS
#endif
#else
#define XT_OFFLINE_LOG_FUNCTION_DEF		XT_RECYCLE_LOGS
#endif

/* TeamDrive, uses special auto-increment, and
 * we keep the logs for the moment:
 */
#ifdef XT_FOR_TEAMDRIVE
#undef XT_OFFLINE_LOG_FUNCTION_DEF
#define XT_OFFLINE_LOG_FUNCTION_DEF		XT_KEEP_LOGS
//#undef XT_AUTO_INCREMENT_DEF
//#define XT_AUTO_INCREMENT_DEF			1
#endif

#ifdef PBXT_HANDLER_TRACE
static void ha_trace_function(const char *function, char *table)
{
	char		func_buf[50], *ptr;
	XTThreadPtr	thread = xt_get_self(); 

	if ((ptr = const_cast<char *>(strchr(function, '(')))) {
		ptr--;
		while (ptr > function) {
			if (!(isalnum(*ptr) || *ptr == '_'))
				break;
			ptr--;
		}
		ptr++;
		xt_strcpy(50, func_buf, ptr);
		if ((ptr = strchr(func_buf, '(')))
			*ptr = 0;
	}
	else
		xt_strcpy(50, func_buf, function);
	if (table)
		printf("%s %s (%s)\n", thread ? thread->t_name : "-unknown-", func_buf, table);
	else
		printf("%s %s\n", thread ? thread->t_name : "-unknown-", func_buf);
}
#endif

/*
 * -----------------------------------------------------------------------
 * SHARED TABLE DATA
 *
 */

static xtBool ha_hash_comp(void *key, void *data)
{
	XTSharePtr	share = (XTSharePtr) data;

	return strcmp((char *) key, share->sh_table_path->ps_path) == 0;
}

static xtHashValue ha_hash(xtBool is_key, void *key_data)
{
	XTSharePtr	share = (XTSharePtr) key_data;

	if (is_key)
		return xt_ht_hash((char *) key_data);
	return xt_ht_hash(share->sh_table_path->ps_path);
}

static xtBool ha_hash_comp_ci(void *key, void *data)
{
	XTSharePtr	share = (XTSharePtr) data;

	return strcasecmp((char *) key, share->sh_table_path->ps_path) == 0;
}

static xtHashValue ha_hash_ci(xtBool is_key, void *key_data)
{
	XTSharePtr	share = (XTSharePtr) key_data;

	if (is_key)
		return xt_ht_casehash((char *) key_data);
	return xt_ht_casehash(share->sh_table_path->ps_path);
}

static void ha_open_share(XTThreadPtr self, XTShareRec *share)
{
	xt_lock_mutex(self, (xt_mutex_type *) share->sh_ex_mutex);
	pushr_(xt_unlock_mutex, share->sh_ex_mutex);

	if (!share->sh_table) {
		share->sh_table = xt_use_table(self, share->sh_table_path, FALSE, FALSE);
		share->sh_dic_key_count = share->sh_table->tab_dic.dic_key_count;
		share->sh_dic_keys = share->sh_table->tab_dic.dic_keys;
		share->sh_recalc_selectivity = FALSE;
	}

	freer_(); // xt_ht_unlock(pbxt_share_tables)
}

static void ha_close_share(XTThreadPtr self, XTShareRec *share)
{
	XTTableHPtr tab;

	if ((tab = share->sh_table)) {
		/* Save this, in case the share is re-opened. */
		share->sh_min_auto_inc = tab->tab_auto_inc;

		xt_heap_release(self, tab);
		share->sh_table = NULL;
	}

	/* This are only references: */
	share->sh_dic_key_count = 0;
	share->sh_dic_keys = NULL;
}

static void ha_cleanup_share(XTThreadPtr self, XTSharePtr share)
{
	ha_close_share(self, share);

	if (share->sh_table_path) {
		xt_free(self, share->sh_table_path);
		share->sh_table_path = NULL;
	}

	if (share->sh_ex_cond) {
		thr_lock_delete(&share->sh_lock);
		xt_delete_cond(self, (xt_cond_type *) share->sh_ex_cond);
		share->sh_ex_cond = NULL;
	}

	if (share->sh_ex_mutex) {
		xt_delete_mutex(self, (xt_mutex_type *) share->sh_ex_mutex);
		share->sh_ex_mutex = NULL;
	}

	xt_free(self, share);
}

static void ha_hash_free(XTThreadPtr self, void *data)
{
	XTSharePtr	share = (XTSharePtr) data;

	ha_cleanup_share(self, share);
}

/*
 * This structure contains information that is common to all handles.
 * (i.e. it is table specific).
 */
static XTSharePtr ha_get_share(XTThreadPtr self, const char *table_path, bool open_table)
{
	XTShareRec	*share;

	enter_();
	xt_ht_lock(self, pbxt_share_tables);
	pushr_(xt_ht_unlock, pbxt_share_tables);

	// Check if the table exists...
	if (!(share = (XTSharePtr) xt_ht_get(self, pbxt_share_tables, (void *) table_path))) {
		share = (XTSharePtr) xt_calloc(self, sizeof(XTShareRec));		
		pushr_(ha_cleanup_share, share);

		share->sh_ex_mutex = (xt_mutex_type *) xt_new_mutex(self);
		share->sh_ex_cond = (xt_cond_type *) xt_new_cond(self);

		thr_lock_init(&share->sh_lock);

		share->sh_use_count = 0;
		share->sh_table_path = (XTPathStrPtr) xt_dup_string(self, table_path);

		if (open_table)
			ha_open_share(self, share);

		popr_(); // Discard ha_cleanup_share(share);

		xt_ht_put(self, pbxt_share_tables, share);
	}

	share->sh_use_count++;
	freer_(); // xt_ht_unlock(pbxt_share_tables)

	return_(share);
}

/*
 * Free shared information.
 */
static void ha_unget_share(XTThreadPtr self, XTSharePtr share)
{
	xt_ht_lock(self, pbxt_share_tables);
	pushr_(xt_ht_unlock, pbxt_share_tables);

	if (!--share->sh_use_count)
		xt_ht_del(self, pbxt_share_tables, share->sh_table_path);

	freer_(); // xt_ht_unlock(pbxt_share_tables)
}

static xtBool ha_unget_share_removed(XTThreadPtr self, XTSharePtr share)
{
	xtBool removed = FALSE;

	xt_ht_lock(self, pbxt_share_tables);
	pushr_(xt_ht_unlock, pbxt_share_tables);

	if (!--share->sh_use_count) {
		removed = TRUE;
		xt_ht_del(self, pbxt_share_tables, share->sh_table_path);
	}

	freer_(); // xt_ht_unlock(pbxt_share_tables)
	return removed;
}

static inline void thd_init_xact(THD *thd, XTThreadPtr self, bool set_table_trans)
{
	self->st_xact_mode = thd_tx_isolation(thd) <= ISO_READ_COMMITTED ? XT_XACT_COMMITTED_READ : XT_XACT_REPEATABLE_READ;
	self->st_ignore_fkeys = (thd_test_options(thd, OPTION_NO_FOREIGN_KEY_CHECKS)) != 0;
	self->st_auto_commit = (thd_test_options(thd,(OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))) == 0;
	if (set_table_trans) {
#ifdef DRIZZLED
		self->st_table_trans = FALSE;
#else
		self->st_table_trans = thd_sql_command(thd) == SQLCOM_LOCK_TABLES;
#endif
	}
	self->st_abort_trans = FALSE;
	self->st_stat_ended = FALSE;
	self->st_stat_trans = FALSE;
	XT_PRINT0(self, "xt_xn_begin\n");
	xt_xres_wait_for_recovery(self, XT_RECOVER_SWEPT);
}

/*
 * -----------------------------------------------------------------------
 * PUBLIC FUNCTIONS
 *
 */

xtPublic void xt_ha_unlock_table(XTThreadPtr self, void *share)
{
	ha_release_exclusive_use(self, (XTSharePtr) share);
	ha_unget_share(self, (XTSharePtr) share);
}

xtPublic void xt_ha_close_global_database(XTThreadPtr self)
{
	if (pbxt_database) {
		xt_heap_release(self, pbxt_database);
		pbxt_database = NULL;
	}
}

/*
 * Open a PBXT database given the path of a table.
 * This function also returns the name of the table.
 *
 * We use the pbxt_database_mutex to lock this
 * operation to make sure it does not occur while
 * some other thread is doing a "closeall".
 */
xtPublic void xt_ha_open_database_of_table(XTThreadPtr self, XTPathStrPtr XT_UNUSED(table_path))
{
#ifdef XT_USE_GLOBAL_DB
	if (!self->st_database) {
		if (!pbxt_database) {
			xt_open_database(self, mysql_real_data_home, TRUE);
			/* {GLOBAL-DB}
			 * This can be done at the same time as the recovery thread,
			 * strictly speaking I need a lock.
			 */
			if (!pbxt_database) {
				pbxt_database = self->st_database;
				xt_heap_reference(self, pbxt_database);
			}
		}
		else
			xt_use_database(self, pbxt_database, XT_FOR_USER);
	}
#else
	char db_path[PATH_MAX];

	xt_strcpy(PATH_MAX, db_path, (char *) table_path);
	xt_remove_last_name_of_path(db_path);
	xt_remove_dir_char(db_path);

	if (self->st_database && xt_tab_compare_paths(self->st_database->db_name, xt_last_name_of_path(db_path)) == 0)
		/* This thread already has this database open! */
		return;

	/* Auto commit before changing the database: */
	if (self->st_xact_data) {
		/* PMC - This probably indicates something strange is happening:
		 *
		 * This sequence generates this error:
		 *
		 * delimiter |
		 * 
		 * create temporary table t3 (id int)|
		 * 
		 * create function f10() returns int
		 * begin
		 *   drop temporary table if exists t3;
		 *   create temporary table t3 (id int) engine=myisam;
		 *   insert into t3 select id from t4;
		 *   return (select count(*) from t3);
		 * end|
		 * 
		 * select f10()|
		 *
		 * An error is generated because the same thread is used
		 * to open table t4 (at the start of the functions), and
		 * then to drop table t3. To drop t3 we need to
		 * switch the database, so we land up here!
		 */
		xt_throw_xterr(XT_CONTEXT, XT_ERR_CANNOT_CHANGE_DB);
		/*
		 if (!xt_xn_commit(self))
		 	throw_();
		 */
	}

	xt_lock_mutex(self, &pbxt_database_mutex);
	pushr_(xt_unlock_mutex, &pbxt_database_mutex);
	xt_open_database(self, db_path, FALSE);
	freer_(); // xt_unlock_mutex(&pbxt_database_mutex);
#endif
}

xtPublic XTThreadPtr xt_ha_set_current_thread(THD *thd, XTExceptionPtr e)
{
	XTThreadPtr	self;
	static int	ha_thread_count = 0, ha_id;

	if (!(self = (XTThreadPtr) *thd_ha_data(thd, pbxt_hton))) {
//		const			Security_context *sctx;
		char			name[120];
		char			ha_id_str[50];

		ha_id = ++ha_thread_count;
		sprintf(ha_id_str, "_%d", ha_id);
		xt_strcpy(120,name,"user"); // TODO: Fix this hack
/*
		sctx = &thd->main_security_ctx;

		if (sctx->user) {
			xt_strcpy(120, name, sctx->user);
			xt_strcat(120, name, "@");
		}
		else
			*name = 0;
		if (sctx->host)
			xt_strcat(120, name, sctx->host);
		else if (sctx->ip)
			xt_strcat(120, name, sctx->ip);
		else if (thd->proc_info)
			xt_strcat(120, name, (char *) thd->proc_info);
		else
			xt_strcat(120, name, "system");
*/
		xt_strcat(120, name, ha_id_str);
		if (!(self = xt_create_thread(name, FALSE, TRUE, e)))
			return NULL;

		self->st_xact_mode = XT_XACT_REPEATABLE_READ;

		*thd_ha_data(thd, pbxt_hton) = (void *) self;
	}
	return self;
}

xtPublic void xt_ha_close_connection(THD* thd)
{
	XTThreadPtr		self;

	if ((self = (XTThreadPtr) *thd_ha_data(thd, pbxt_hton))) {
		*thd_ha_data(thd, pbxt_hton) = NULL;
		xt_free_thread(self);
	}
}

xtPublic XTThreadPtr xt_ha_thd_to_self(THD *thd)
{
	return (XTThreadPtr) *thd_ha_data(thd, pbxt_hton);
}

/* The first bit is 1. */
static u_int ha_get_max_bit(MX_BITMAP *map)
{
#ifdef DRIZZLED
	return map->getFirstSet();
#else
	my_bitmap_map	*data_ptr = map->bitmap;
	my_bitmap_map	*end_ptr = map->last_word_ptr;
	my_bitmap_map	b;
	u_int			cnt = map->n_bits;

	for (; end_ptr >= data_ptr; end_ptr--) {
		if ((b = *end_ptr)) {
			my_bitmap_map mask;
			
			if (end_ptr == map->last_word_ptr && map->last_word_mask)
				mask = map->last_word_mask >> 1;
			else
				mask = 0x80000000;
			while (!(b & mask)) {
				b = b << 1;
				/* Should not happen, but if it does, we hang! */
				if (!b)
					return map->n_bits;
				cnt--;
			}
			return cnt;
		}
		if (end_ptr == map->last_word_ptr)
			cnt = ((cnt-1) / 32) * 32;
		else
			cnt -= 32;
	}
	return 0;
#endif
}

/*
 * -----------------------------------------------------------------------
 * SUPPORT FUNCTIONS
 *
 */

/*
 * In PBXT, as in MySQL: thread == connection.
 *
 * So we simply attach a PBXT thread to a MySQL thread.
 */
static XTThreadPtr ha_set_current_thread(THD *thd, int *err)
{
	XTThreadPtr		self;
	XTExceptionRec	e;

	if (!(self = xt_ha_set_current_thread(thd, &e))) {
		xt_log_exception(NULL, &e, XT_LOG_DEFAULT);
		*err = e.e_xt_err;
		return NULL;
	}
	return self;
}

xtPublic int xt_ha_pbxt_to_mysql_error(int xt_err)
{
	switch (xt_err) {
		case XT_NO_ERR:
			return(0);
		case XT_ERR_DUPLICATE_KEY:
				return HA_ERR_FOUND_DUPP_KEY;
		case XT_ERR_DEADLOCK:
				return HA_ERR_LOCK_DEADLOCK;
		case XT_ERR_RECORD_CHANGED:
			/* If we generate HA_ERR_RECORD_CHANGED instead of HA_ERR_LOCK_WAIT_TIMEOUT
			 * then sysbench does not work because it does not handle this error.
			 */
			//return HA_ERR_LOCK_WAIT_TIMEOUT; // but HA_ERR_RECORD_CHANGED is the correct error for a optimistic lock failure.
			return HA_ERR_RECORD_CHANGED;
		case XT_ERR_LOCK_TIMEOUT:
			return HA_ERR_LOCK_WAIT_TIMEOUT;
		case XT_ERR_TABLE_IN_USE:
				return HA_ERR_WRONG_COMMAND;
		case XT_ERR_TABLE_NOT_FOUND:
			return HA_ERR_NO_SUCH_TABLE;
		case XT_ERR_TABLE_EXISTS:
			return HA_ERR_TABLE_EXIST;
		case XT_ERR_CANNOT_CHANGE_DB:
			return ER_TRG_IN_WRONG_SCHEMA;
		case XT_ERR_COLUMN_NOT_FOUND:
			return HA_ERR_CANNOT_ADD_FOREIGN;
		case XT_ERR_NO_REFERENCED_ROW:
		case XT_ERR_REF_TABLE_NOT_FOUND:
		case XT_ERR_REF_TYPE_WRONG:
			return HA_ERR_NO_REFERENCED_ROW;
		case XT_ERR_ROW_IS_REFERENCED:
			return HA_ERR_ROW_IS_REFERENCED;
		case XT_ERR_COLUMN_IS_NOT_NULL:
		case XT_ERR_INCORRECT_NO_OF_COLS:
		case XT_ERR_FK_ON_TEMP_TABLE:
		case XT_ERR_FK_REF_TEMP_TABLE:
			return HA_ERR_CANNOT_ADD_FOREIGN;
		case XT_ERR_DUPLICATE_FKEY:
			return HA_ERR_FOREIGN_DUPLICATE_KEY;
		case XT_ERR_RECORD_DELETED:
			return HA_ERR_RECORD_DELETED;
	}
	return(-1);			// Unknown error
}

xtPublic int xt_ha_pbxt_thread_error_for_mysql(THD *thd, const XTThreadPtr self, int ignore_dup_key)
{
	int		xt_err = self->t_exception.e_xt_err;
	xtBool	dup_key = FALSE;

	XT_PRINT2(self, "xt_ha_pbxt_thread_error_for_mysql xt_err=%d auto commit=%d\n", (int) xt_err, (int) self->st_auto_commit);
	switch (xt_err) {
		case XT_NO_ERR:
			break;
		case XT_ERR_DUPLICATE_KEY:
		case XT_ERR_DUPLICATE_FKEY:
			/* Let MySQL call rollback as and when it wants to for duplicate
			 * key.
			 *
			 * In addition, we are not allowed to do an auto-rollback
			 * inside a sub-statement (function() or procedure())
			 * For example:
			 * 
			 * delimiter |
			 *
			 * create table t3 (c1 char(1) primary key not null)|
			 * 
			 * create function bug12379()
			 *   returns integer
			 * begin
			 *    insert into t3 values('X');
			 *    insert into t3 values('X');
			 *    return 0;
			 * end|
			 * 
			 * --error 1062
			 * select bug12379()|
			 *
			 *
			 * Not doing an auto-rollback should solve this problem in the
			 * case of duplicate key (but not in others - like deadlock)!
			 * I don't think this situation is handled correctly by MySQL.
			 */

			/* If we are in auto-commit mode (and we are not ignoring
			 * duplicate keys) then rollback the transaction automatically.
			 */
			dup_key = TRUE;
			if (!ignore_dup_key && self->st_auto_commit)
				goto abort_transaction;
			break;
		case XT_ERR_DEADLOCK:
		case XT_ERR_NO_REFERENCED_ROW:
		case XT_ERR_ROW_IS_REFERENCED:
			goto abort_transaction;
		case XT_ERR_RECORD_CHANGED:
			/* MySQL also handles the locked error. NOTE: There is no automatic
			 * rollback!
			 */
			break;
		default:
			xt_log_exception(self, &self->t_exception, XT_LOG_DEFAULT);
			abort_transaction:
			/* PMC 2006-08-30: It should be that this is not necessary!
			 *
			 * It is only necessary to call ha_rollback() if the engine
			 * aborts the transaction.
			 *
			 * On the other hand, I shouldn't need to rollback the
			 * transaction because, if I return an error, MySQL
			 * should do it for me.
			 *
			 * Unfortunately, when auto-commit is off, MySQL does not
			 * rollback automatically (for example when a deadlock
			 * is provoked).
			 *
			 * And when we have a multi update we cannot rely on this
			 * either (see comment above).
			 */
			if (self->st_xact_data) {
				/*
				 * GOTCHA:
				 * A result of the "st_abort_trans = TRUE" below is that
				 * the following code results in an empty set.
				 * The reason is "ignore_dup_key" is not set so
				 * the duplicate key leads to an error which causes
				 * the transaction to be aborted.
				 * The delayed inserts are all execute in one transaction.
				 * 
				 * CREATE TABLE t1 (
				 * c1 INT(11) NOT NULL AUTO_INCREMENT,
				 * c2 INT(11) DEFAULT NULL,
				 * PRIMARY KEY (c1)
				 * );
				 * SET insert_id= 14;
				 * INSERT DELAYED INTO t1 VALUES(NULL, 11), (NULL, 12);
				 * INSERT DELAYED INTO t1 VALUES(14, 91);
				 * INSERT DELAYED INTO t1 VALUES (NULL, 92), (NULL, 93);
				 * FLUSH TABLE t1;
				 * SELECT * FROM t1;
				 */
				if (self->st_lock_count == 0) {
					/* No table locks, must rollback immediately
					 * (there will be no possibility later!
					 */
					XT_PRINT1(self, "xt_xn_rollback xt_err=%d\n", xt_err);
					if (!xt_xn_rollback(self))
						xt_log_exception(self, &self->t_exception, XT_LOG_DEFAULT);
				}
				else {
					/* Locks are held on tables.
					 * Only rollback after locks are released.
					 */
					/* I do not think this is required, because
					 * I tell mysql to rollback below, 
					 * besides it is a hack!
					 self->st_auto_commit = TRUE;
					 */
					self->st_abort_trans = TRUE;
				}
				/* Only tell MySQL to rollback if we automatically rollback.
				 * Note: calling this with (thd, FALSE), cause sp.test to fail.
				 */
				if (!dup_key) {
					if (thd)
						thd_mark_transaction_to_rollback(thd, TRUE);
				}
			}
			break;
	}
	return xt_ha_pbxt_to_mysql_error(xt_err);
}

static void ha_conditional_close_database(XTThreadPtr self, XTThreadPtr other_thr, void *db)
{
	if (other_thr->st_database == (XTDatabaseHPtr) db)
		xt_unuse_database(self, other_thr);
}

/*
 * This is only called from drop database, so we know that
 * no thread is actually using the database. This means that it
 * must be safe to close the database.
 */
xtPublic void xt_ha_all_threads_close_database(XTThreadPtr self, XTDatabaseHPtr db)
{
	xt_lock_mutex(self, &pbxt_database_mutex);
	pushr_(xt_unlock_mutex, &pbxt_database_mutex);
	xt_do_to_all_threads(self, ha_conditional_close_database, db);
	freer_(); // xt_unlock_mutex(&pbxt_database_mutex);
}

static int ha_log_pbxt_thread_error_for_mysql(int ignore_dup_key)
{
	return xt_ha_pbxt_thread_error_for_mysql(current_thd, myxt_get_self(), ignore_dup_key);
}

/*
 * -----------------------------------------------------------------------
 * STATIC HOOKS
 *
 */
static xtWord8 ha_set_variable(char **value, HAVarParamsPtr vp)
{
	xtWord8	result;
	xtWord8	mi, ma;
	char	*mm;

	if (!*value)
		*value = getenv(vp->vp_var);
	if (!*value)
		*value = (char *) vp->vp_def;
	result = xt_byte_size_to_int8(*value);
	mi = (xtWord8) xt_byte_size_to_int8(vp->vp_min);
	if (result < mi) {
		result = mi;
		*value = (char *) vp->vp_min;
	}
	if (sizeof(size_t) == 8)
		mm = (char *) vp->vp_max8;
	else
		mm = (char *) vp->vp_max4;
	ma = (xtWord8) xt_byte_size_to_int8(mm);
	if (result > ma) {
		result = ma;
		*value = mm;
	}
	return result;
}

static void pbxt_call_init(XTThreadPtr self)
{
	xtInt8	index_cache_size;
	xtInt8	record_cache_size;
	xtInt8	log_cache_size;
	xtInt8	log_file_threshold;
	xtInt8	transaction_buffer_size;
	xtInt8	log_buffer_size;
	xtInt8	checkpoint_frequency;
	xtInt8	data_log_threshold;
	xtInt8	data_file_grow_size;
	xtInt8	row_file_grow_size;

	xt_logf(XT_NT_INFO, "PrimeBase XT (PBXT) Engine %s loaded...\n", xt_get_version());
	xt_logf(XT_NT_INFO, "Paul McCullagh, PrimeBase Technologies GmbH, http://www.primebase.org\n");

	index_cache_size = ha_set_variable(&pbxt_index_cache_size, &vp_index_cache_size);
	record_cache_size = ha_set_variable(&pbxt_record_cache_size, &vp_record_cache_size);
	log_cache_size = ha_set_variable(&pbxt_log_cache_size, &vp_log_cache_size);
	log_file_threshold = ha_set_variable(&pbxt_log_file_threshold, &vp_log_file_threshold);
	transaction_buffer_size = ha_set_variable(&pbxt_transaction_buffer_size, &vp_transaction_buffer_size);
	log_buffer_size = ha_set_variable(&pbxt_log_buffer_size, &vp_log_buffer_size);
	checkpoint_frequency = ha_set_variable(&pbxt_checkpoint_frequency, &vp_checkpoint_frequency);
	data_log_threshold = ha_set_variable(&pbxt_data_log_threshold, &vp_data_log_threshold);
	data_file_grow_size = ha_set_variable(&pbxt_data_file_grow_size, &vp_data_file_grow_size);
	row_file_grow_size = ha_set_variable(&pbxt_row_file_grow_size, &vp_row_file_grow_size);

	xt_db_log_file_threshold = (xtLogOffset) log_file_threshold;
	xt_db_log_buffer_size = (size_t) xt_align_offset(log_buffer_size, 512);
	xt_db_transaction_buffer_size = (size_t) xt_align_offset(transaction_buffer_size, 512);
	xt_db_checkpoint_frequency = (size_t) checkpoint_frequency;
	xt_db_data_log_threshold = (off_t) data_log_threshold;
	xt_db_data_file_grow_size = (size_t) data_file_grow_size;
	xt_db_row_file_grow_size = (size_t) row_file_grow_size;

#ifdef DRIZZLED
	pbxt_ignore_case = TRUE;
#else
	pbxt_ignore_case = lower_case_table_names != 0;
#endif
	if (pbxt_ignore_case)
		pbxt_share_tables = xt_new_hashtable(self, ha_hash_comp_ci, ha_hash_ci, ha_hash_free, TRUE, FALSE);
	else
		pbxt_share_tables = xt_new_hashtable(self, ha_hash_comp, ha_hash, ha_hash_free, TRUE, FALSE);

	xt_thread_wait_init(self);
	xt_fs_init(self);
	xt_lock_installation(self, mysql_real_data_home);
	XTSystemTableShare::startUp(self);
	xt_init_databases(self);
	xt_ind_init(self, (size_t) index_cache_size);
	xt_tc_init(self, (size_t) record_cache_size);
	xt_xlog_init(self, (size_t) log_cache_size);
}

static void pbxt_call_exit(XTThreadPtr self)
{
	xt_logf(XT_NT_INFO, "PrimeBase XT Engine shutdown...\n");

#ifdef TRACE_STATEMENTS
	xt_dump_trace();
#endif
#ifdef XT_USE_GLOBAL_DB
	xt_ha_close_global_database(self);
#endif
#ifdef DEBUG
	//xt_stop_database_threads(self, FALSE);
	xt_stop_database_threads(self, TRUE);
#else
	xt_stop_database_threads(self, TRUE);
#endif
	/* This will tell the freeer to quit ASAP: */
	xt_quit_freeer(self);
	/* We conditional stop the freeer here, because if we are
	 * in startup, then the free will be hanging.
	 * {FREEER-HANG}
	 *
	 * This problem has been solved by MySQL!
	 */
	xt_stop_freeer(self);
	xt_exit_databases(self);
	XTSystemTableShare::shutDown(self);
	xt_xlog_exit(self);
	xt_tc_exit(self);
	xt_ind_exit(self);
	xt_unlock_installation(self, mysql_real_data_home);
	xt_fs_exit(self);
	xt_thread_wait_exit(self);
	if (pbxt_share_tables) {
		xt_free_hashtable(self, pbxt_share_tables);
		pbxt_share_tables = NULL;
	}
}

/*
 * Shutdown the PBXT sub-system.
 */
static void ha_exit(XTThreadPtr self)
{
	xt_xres_terminate_recovery(self);

	/* Wrap things up... */
	xt_unuse_database(self, self);	/* Just in case the main thread has a database in use (for testing)? */
	/* This may cause the streaming engine to cleanup connections and 
	 * tables belonging to this engine. This in turn may require some of
	 * the stuff below (like xt_create_thread() called from pbxt_close_table()! */
#ifdef PBMS_ENABLED
	pbms_finalize();
#endif
	pbxt_call_exit(self);
	xt_exit_threading(self);
	xt_exit_memory();
	xt_exit_logging();
	xt_p_mutex_destroy(&pbxt_database_mutex);		
	pbxt_inited = false;
}

/*
 * Outout the PBXT status. Return FALSE on error.
 */
#ifdef DRIZZLED
bool PBXTStorageEngine::show_status(Session *thd, stat_print_fn *stat_print, enum ha_stat_type)
#else
static bool pbxt_show_status(handlerton *XT_UNUSED(hton), THD* thd, 
                          stat_print_fn* stat_print,
                          enum ha_stat_type XT_UNUSED(stat_type))
#endif
{
	XTThreadPtr			self;	
	int					err = 0;
	XTStringBufferRec	strbuf = { 0, 0, 0 };
	bool				not_ok = FALSE;

	if (!(self = ha_set_current_thread(thd, &err)))
		return FALSE;

#ifdef XT_SHOW_DUMPS_TRACE
	//if (pbxt_database)
	//	xt_dump_xlogs(pbxt_database, 0);
	xt_trace("// %s - dump\n", xt_trace_clock_diff(NULL));
	xt_dump_trace();
#endif
#ifdef XT_TRACK_CONNECTIONS
	xt_dump_conn_tracking();
#endif

	try_(a) {
		myxt_get_status(self, &strbuf);
	}
	catch_(a) {
		not_ok = TRUE;
	}
	cont_(a);

	if (!not_ok) {
		if (stat_print(thd, "PBXT", 4, "", 0, strbuf.sb_cstring, (uint) strbuf.sb_len))
			not_ok = TRUE;
	}
	xt_sb_set_size(self, &strbuf, 0);

	return not_ok;
}

/*
 * Initialize the PBXT sub-system.
 *
 * return 1 on error, else 0.
 */
#ifdef DRIZZLED
static int pbxt_init(Registry &registry)
#else
static int pbxt_init(void *p)
#endif
{
	int init_err = 0;

	XT_PRINT0(NULL, "pbxt_init\n");

	if (sizeof(xtWordPS) != sizeof(void *)) {
		printf("PBXT: This won't work, I require that sizeof(xtWordPS) == sizeof(void *)!\n");
		XT_RETURN(1);
	}

	/* GOTCHA: This will "detect" if are loading the plug-in
	 * with different --with-debug option to MySQL.
	 *
	 * In this case, you will get an error when loading the
	 * library that some symbol was not found.
	 */
	void *dummy = my_malloc(100, MYF(0));
	my_free((byte *) dummy);

 	if (!pbxt_inited) {
		XTThreadPtr self = NULL;

 		xt_p_mutex_init_with_autoname(&pbxt_database_mutex, NULL);

#ifdef DRIZZLED
		pbxt_hton= new PBXTStorageEngine(std::string("PBXT"));
		registry.add(pbxt_hton);
#else
		pbxt_hton = (handlerton *) p;
		pbxt_hton->state = SHOW_OPTION_YES;
		pbxt_hton->db_type = DB_TYPE_PBXT; // Wow! I have my own!
		pbxt_hton->close_connection = pbxt_close_connection; /* close_connection, cleanup thread related data. */
		pbxt_hton->commit = pbxt_commit; /* commit */
		pbxt_hton->rollback = pbxt_rollback; /* rollback */
		if (pbxt_support_xa) {
			pbxt_hton->prepare = pbxt_prepare;
			pbxt_hton->recover = pbxt_recover;
			pbxt_hton->commit_by_xid = pbxt_commit_by_xid;
			pbxt_hton->rollback_by_xid = pbxt_rollback_by_xid;
		}
		else {
			pbxt_hton->prepare = NULL;
			pbxt_hton->recover = NULL;
			pbxt_hton->commit_by_xid = NULL;
			pbxt_hton->rollback_by_xid = NULL;
		}
		pbxt_hton->create = pbxt_create_handler; /* Create a new handler */
		pbxt_hton->drop_database = pbxt_drop_database; /* Drop a database */
		pbxt_hton->panic = pbxt_panic; /* Panic call */
		pbxt_hton->show_status = pbxt_show_status;
		pbxt_hton->flags = HTON_NO_FLAGS; /* HTON_CAN_RECREATE - Without this flags TRUNCATE uses delete_all_rows() */
		pbxt_hton->slot = (uint)-1; /* assign invald value, so we know when it's inited later */
		pbxt_hton->start_consistent_snapshot = pbxt_start_consistent_snapshot;
#if defined(MYSQL_SUPPORTS_BACKUP) && defined(XT_ENABLE_ONLINE_BACKUP)
		pbxt_hton->get_backup_engine = pbxt_backup_engine;
#endif
#endif
		if (!xt_init_logging())					/* Initialize logging */
			goto error_1;

#ifdef PBMS_ENABLED
		PBMSResultRec result;
		if (!pbms_initialize("PBXT", false, &result)) {
			xt_logf(XT_NT_ERROR, "pbms_initialize() Error: %s", result.mr_message);
			goto error_2;
		}
#endif

		if (!xt_init_memory())					/* Initialize memory */
			goto error_3;

		/* +7 assumes:
		 * We are not using multiple database, and:
		 * +1 Main thread.
		 * +1 Compactor thread
		 * +1 Writer thread
		 * +1 Checkpointer thread
		 * +1 Sweeper thread
		 * +1 Free'er thread
		 * +1 Temporary thread (e.g. TempForClose, TempForEnd)
		 */
#ifndef DRIZZLED
		if (pbxt_max_threads == 0)
			pbxt_max_threads = max_connections + 7;
#endif
		self = xt_init_threading(pbxt_max_threads);				/* Create the main self: */
		if (!self)
			goto error_3;

 		pbxt_inited = true;

		try_(a) {
			/* Initialize all systems */
			pbxt_call_init(self);

			/* Conditional unit test: */
#ifdef XT_UNIT_TEST
			//xt_unit_test_create_threads(self);
			xt_unit_test_read_write_locks(self);
			//xt_unit_test_mutex_locks(self);
#endif

			/* {OPEN-DB-SWEEPER-WAIT}
			 * I have to start the freeer before I open and recover the database
			 * because it we run out of cache while waiting for the sweeper
			 * we will hang!
			 */
			xt_start_freeer(self);

#ifdef XT_USE_GLOBAL_DB
			/* Open the global database. */
			ASSERT(!pbxt_database);
			{
				THD *curr_thd = current_thd;
				THD *thd = NULL;

#ifndef DRIZZLED
#if MYSQL_VERSION_ID < 50147
				/* A hack which is no longer required after 5.1.46 */
				extern myxt_mutex_t LOCK_plugin;
#endif

				/* {MYSQL QUIRK}
				 * I have to release this lock for PBXT recovery to
				 * work, because it needs to open .frm files.
				 * So, I unlock, but during INSTALL PLUGIN this is
				 * risky, because we are in multi-threaded
				 * mode!
				 *
				 * Although, as far as I can tell from the MySQL code,
				 * INSTALL PLUGIN should still work ok, during
				 * concurrent access, because we are not
				 * relying on pointer/memory that may be changed by
				 * other users.
				 *
				 * Only real problem, 2 threads try to load the same
				 * plugin at the same time.
				 */
#if MYSQL_VERSION_ID < 50147
				myxt_mutex_unlock(&LOCK_plugin);
#endif
#endif

				/* Can't do this here yet, because I need a THD! */
				try_(b) {
					/* {MYSQL QUIRK}
					 * Sometime we have a THD,
					 * sometimes we don't.
					 * So far, I have noticed that during INSTALL PLUGIN,
					 * we have one, otherwize not.
					 */
					if (!curr_thd) {
						if (!(thd = (THD *) myxt_create_thread()))
							xt_throw(self);
					}

					xt_xres_start_database_recovery(self);
				}
				catch_(b) {
					/* It is possible that the error was reset by cleanup code.
					 * Set a generic error code in that case.
					 */
					/* PMC - This is not necessary in because exceptions are 
					 * now preserved, in exception handler cleanup.
					*/
					if (!self->t_exception.e_xt_err)
						xt_register_error(XT_REG_CONTEXT, XT_SYSTEM_ERROR, 0, "Initialization failed"); 
					xt_log_exception(self, &self->t_exception, XT_LOG_DEFAULT);
					init_err = 1;
				}
				cont_(b);

				if (thd)
					myxt_destroy_thread(thd, FALSE);
#ifndef DRIZZLED
#if MYSQL_VERSION_ID < 50147
				myxt_mutex_lock(&LOCK_plugin);
#endif
#endif
			}
#endif
		}
		catch_(a) {
			xt_log_exception(self, &self->t_exception, XT_LOG_DEFAULT);
			init_err = 1;
		}
		cont_(a);

		if (init_err) {
			/* {FREEER-HANG} The free-er will be hung in:
				#0	0x91fc6a2e in semaphore_wait_signal_trap
				#1	0x91fce505 in pthread_mutex_lock
				#2	0x00489633 in safe_mutex_lock at thr_mutex.c:149
				#3	0x002dfca9 in plugin_thdvar_init at sql_plugin.cc:2398
				#4	0x000d6a12 in THD::init at sql_class.cc:715
				#5	0x000de9d3 in THD::THD at sql_class.cc:597
				#6	0x000debe1 in THD::THD at sql_class.cc:631
				#7	0x00e207a4 in myxt_create_thread at myxt_xt.cc:2666
				#8	0x00e3134b in tabc_fr_run_thread at tabcache_xt.cc:982
				#9	0x00e422ca in xt_thread_main at thread_xt.cc:1006
				#10	0x91ff7c55 in _pthread_start
				#11	0x91ff7b12 in thread_start
			 *
			 * so it is not good trying to stop it here!
			 *
			 * With regard to this problem, see {OPEN-DB-SWEEPER-WAIT}
			 * Due to this problem, I will probably have to hack
			 * the mutex so that the freeer can get started...
			 *
			 * NOPE! problem has gone in 6.0.9. Also not a problem in
			 * 5.1.29.
			 */
			
			/* {OPEN-DB-SWEEPER-WAIT} 
			 * I have to stop the freeer here because it was
			 * started before opening the database.
			 */

			/* {FREEER-HANG-ON-INIT-ERROR}
			 * pbxt_init is called with LOCK_plugin and if it fails and tries to exit
			 * the freeer here it hangs because the freeer calls THD::~THD which tries
			 * to aquire the same lock and hangs. OTOH MySQL calls pbxt_end() after
			 * an unsuccessful call to pbxt_init, so we defer cleaup, except 
			 * releasing 'self'
			 */
			xt_free_thread(self);
			goto error_3;
		}
		xt_free_thread(self);
 	}
	XT_RETURN(init_err);

	error_3:
#ifdef PBMS_ENABLED
	pbms_finalize();

	error_2:
#endif

	error_1:
	XT_RETURN(1);
}

#ifdef DRIZZLED
static int pbxt_end(Registry &registry)
#else
static int pbxt_end(void *)
#endif
{
	XTThreadPtr		self;
	int				err = 0;

	XT_TRACE_CALL();

	if (pbxt_inited) {
		XTExceptionRec	e;

		/* This flag also means "shutting down". */
		pbxt_inited = FALSE; 
		self = xt_create_thread("TempForEnd", FALSE, TRUE, &e);
		if (self) {
			self->t_main = TRUE;
			ha_exit(self);
		}
	}

#ifdef DRIZZLED
	registry.remove(pbxt_hton);
#endif
	XT_RETURN(err);
}

#ifndef DRIZZLED
static int pbxt_panic(handlerton *hton, enum ha_panic_function flag)
{
	return pbxt_end(hton);
}
#endif

/*
 * Kill the PBXT thread associated with the MySQL thread.
 */
#ifdef DRIZZLED
int PBXTStorageEngine::close_connection(Session *thd)
{
	PBXTStorageEngine * const hton = this;
#else
static int pbxt_close_connection(handlerton *hton, THD* thd)
{
#endif
	XTThreadPtr		self;

	XT_TRACE_CALL();
	if ((self = (XTThreadPtr) *thd_ha_data(thd, hton))) {
		*thd_ha_data(thd, hton) = NULL;
		/* Required because freeing the thread could cause
		 * free of database which could call xt_close_file_ns()!
		 */
		xt_set_self(self);
		xt_free_thread(self);
	}
	return 0;
}

/*
 * Currently does nothing because it was all done
 * when the last PBXT table was removed from the 
 * database.
 */
#ifdef DRIZZLED
void PBXTStorageEngine::drop_database(char *)
#else
static void pbxt_drop_database(handlerton *XT_UNUSED(hton), char *XT_UNUSED(path))
#endif
{
	XT_TRACE_CALL();
}

/*
 * NOTES ON TRANSACTIONS:
 *
 * 1. If self->st_lock_count == 0 and transaction can be ended immediately.
 *    If not, we must wait until the last lock is released on the last handler
 *    to ensure that the tables are flushed before the transaction is
 *    committed or aborted.
 *
 * 2. all (below) indicates, within a BEGIN/END (i.e. auto_commit off) whether
 *    the statement or the entire transation is being terminated.
 *    We currently ignore statement termination.
 * 
 * 3. If in BEGIN/END we must call ha_rollback() if we abort the transaction
 *    internally.
 *
 * NOTE ON CONSISTENT SNAPSHOTS:
 * 
 * PBXT itself doesn't need this functiona as its transaction mechanism provides
 * consistent snapshots for all transactions by default. This function is needed
 * only for multi-engine cases like this:
 *
 * CREATE TABLE t1 ... ENGINE=INNODB
 * CREATE TABLE t2 ... ENGINE=PBXT
 * START TRANSACTION WITH CONSISTENT SNAPSHOT
 * SELECT * FROM t1 <-- at this point we need to know about the snapshot
 */

static int pbxt_start_consistent_snapshot(handlerton *hton, THD *thd)
{
	int err          = 0;
	XTThreadPtr self = ha_set_current_thread(thd, &err);

	if (!self->st_database && pbxt_database) {
		xt_ha_open_database_of_table(self, (XTPathStrPtr) NULL);
	}

	thd_init_xact(thd, self, true);

	if (xt_xn_begin(self)) {
		trans_register_ha(thd, TRUE, hton);	
	} else {
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
	}

	/*
	 * As of MySQL 5.1.41 the return value is not checked, so the server might assume 
	 * everything is fine even it isn't. InnoDB returns 0 on success.
	 */
	return err;
}

/*
 * Commit the PBXT transaction of the given thread.
 * thd is the MySQL thread structure.
 * pbxt_thr is a pointer the the PBXT thread structure.
 *
 */
#ifdef DRIZZLED
int PBXTStorageEngine::commit(Session *thd, bool all)
{
	PBXTStorageEngine * const hton = this;
#else
static int pbxt_commit(handlerton *hton, THD *thd, bool all)
{
#endif
	int			err = 0;
	XTThreadPtr	self;

	if ((self = (XTThreadPtr) *thd_ha_data(thd, hton))) {
		XT_PRINT2(self, "%s pbxt_commit all=%d\n", all ? "END CONN XACT" : "END STAT", all);

		if (self->st_xact_data) {
			/* There are no table locks, commit immediately in all cases
			 * except when this is a statement commit with an explicit
			 * transaction (!all && !self->st_auto_commit).
			 */
			if (all || self->st_auto_commit) {
				XT_PRINT0(self, "xt_xn_commit in pbxt_commit\n");

				if (!xt_xn_commit(self))
					err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
			}
		}
		if (!all)
			self->st_stat_trans = FALSE;
	}
	return err;
}

#ifdef DRIZZLED
int PBXTStorageEngine::rollback(Session *thd, bool all)
{
	PBXTStorageEngine * const hton = this;
#else
static int pbxt_rollback(handlerton *hton, THD *thd, bool all)
{
#endif
	int			err = 0;
	XTThreadPtr	self;

	if ((self = (XTThreadPtr) *thd_ha_data(thd, hton))) {
		XT_PRINT2(self, "%s pbxt_rollback all=%d\n", all ? "CONN END XACT" : "STAT END", all);

		if (self->st_xact_data) {
			/* There are no table locks, rollback immediately in all cases
			 * except when this is a statement commit with an explicit
			 * transaction (!all && !self->st_auto_commit).
			 *
			 * Note, the only reason for a rollback of a operation is
			 * due to an error. In this case PBXT has already
			 * undone the effects of the operation.
			 *
			 * However, this is not the same as statement rollback
			 * which can involve a number of operations.
			 *
			 * TODO: Implement statement rollback.
			 */
			if (all || self->st_auto_commit) {
				XT_PRINT0(self, "xt_xn_rollback\n");
				if (!xt_xn_rollback(self))
					err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
			}
		}
		if (!all)
			self->st_stat_trans = FALSE;
	}
	return 0;
}

#ifdef DRIZZLED
Cursor *PBXTStorageEngine::create(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
	PBXTStorageEngine * const hton = this;
#else
static handler *pbxt_create_handler(handlerton *hton, TABLE_SHARE *table, MEM_ROOT *mem_root)
{
#endif
	if (table && XTSystemTableShare::isSystemTable(table->path.str))
		return new (mem_root) ha_xtsys(hton, table);
	else
		return new (mem_root) ha_pbxt(hton, table);
}

/*
 * -----------------------------------------------------------------------
 * 2-PHASE COMMIT
 *
 */

#ifndef DRIZZLED

static int pbxt_prepare(handlerton *hton, THD *thd, bool all)
{
	int			err = 0;
	XTThreadPtr	self;

	XT_TRACE_CALL();
	if ((self = (XTThreadPtr) *thd_ha_data(thd, hton))) {
		XT_PRINT1(self, "pbxt_commit all=%d\n", all);

		if (self->st_xact_data) {
			/* There are no table locks, commit immediately in all cases
			 * except when this is a statement commit with an explicit
			 * transaction (!all && !self->st_auto_commit).
			 */
			if (all || self->st_auto_commit) {
				XID xid;

				XT_PRINT0(self, "xt_xn_prepare in pbxt_prepare\n");
				thd_get_xid(thd, (MYSQL_XID*) &xid);

				if (!xt_xn_prepare(xid.length(), (xtWord1 *) &xid, self))
					err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
			}
		}
	}
	return err;
}

static XTThreadPtr ha_temp_open_global_database(handlerton *hton, THD **ret_thd, int *temp_thread, const char *thread_name, int *err)
{
	THD			*thd;
	XTThreadPtr	self = NULL;

	*temp_thread = 0;
	if ((thd = current_thd))
		self = (XTThreadPtr) *thd_ha_data(thd, hton);
	else {
		//thd = (THD *) myxt_create_thread();
		//*temp_thread |= 2;
	}

	if (!self) {
		XTExceptionRec e;

		if (!(self = xt_create_thread(thread_name, FALSE, TRUE, &e))) {
			*err = xt_ha_pbxt_to_mysql_error(e.e_xt_err);
			xt_log_exception(NULL, &e, XT_LOG_DEFAULT);
			return NULL;
		}
		*temp_thread |= 1;
	}

	xt_xres_wait_for_recovery(self, XT_RECOVER_DONE);

	try_(a) {
		xt_open_database(self, mysql_real_data_home, TRUE);
	}
	catch_(a) {
		*err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
		if ((*temp_thread & 1))
			xt_free_thread(self);
		if (*temp_thread & 2)
			myxt_destroy_thread(thd, FALSE);
		self = NULL;
	}
	cont_(a);

	*ret_thd = thd;
	return self;
}

static void ha_temp_close_database(XTThreadPtr self, THD *thd, int temp_thread)
{
	xt_unuse_database(self, self);
	if (temp_thread & 1)
		xt_free_thread(self);
	if (temp_thread & 2)
		myxt_destroy_thread(thd, TRUE);
}

/* Return all prepared transactions, found during recovery.
 * This function returns a count. If len is returned, the
 * function will be called again.
 */
static int pbxt_recover(handlerton *hton, XID *xid_list, uint len)
{
	xtBool				temp_thread;
	XTThreadPtr			self;
	XTDatabaseHPtr		db;
	uint				count = 0;
	XTXactPreparePtr	xap;
	int					err;
	THD					*thd;

	if (!(self = ha_temp_open_global_database(hton, &thd, &temp_thread, "TempForRecover", &err)))
		return 0;

	db = self->st_database;

	for (count=0; count<len; count++) {
		xap = xt_xn_enum_xa_data(db, &pbxt_xa_enum);
		if (!xap)
			break;
		memcpy(&xid_list[count], xap->xp_xa_data, xap->xp_data_len);
	}

	ha_temp_close_database(self, thd, temp_thread);
	return (int) count;
}

static int pbxt_commit_by_xid(handlerton *hton, XID *xid)
{
	xtBool				temp_thread;
	XTThreadPtr			self;
	XTDatabaseHPtr		db;
	int					err = 0;
	XTXactPreparePtr	xap;
	THD					*thd;

	XT_TRACE_CALL();

	if (!(self = ha_temp_open_global_database(hton, &thd, &temp_thread, "TempForCommitXA", &err)))
		return err;
	db = self->st_database;

	if ((xap = xt_xn_find_xa_data(db, xid->length(), (xtWord1 *) xid, TRUE, self))) {
		if ((self->st_xact_data = xt_xn_get_xact(db, xap->xp_xact_id, self))) {
			self->st_xact_data->xd_flags &= ~XT_XN_XAC_PREPARED;  // Prepared transactions cannot be swept!
			if (!xt_xn_commit(self))
				err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
		}
		xt_xn_delete_xa_data(db, xap, TRUE, self);
	}

	ha_temp_close_database(self, thd, temp_thread);
	return 0;
}

static int pbxt_rollback_by_xid(handlerton *hton, XID *xid)
{
	int					temp_thread;
	XTThreadPtr			self;
	XTDatabaseHPtr		db;
	int					err = 0;
	XTXactPreparePtr	xap;
	THD					*thd;

	XT_TRACE_CALL();

	if (!(self = ha_temp_open_global_database(hton, &thd, &temp_thread, "TempForRollbackXA", &err)))
		return err;
	db = self->st_database;

	if ((xap = xt_xn_find_xa_data(db, xid->length(), (xtWord1 *) xid, TRUE, self))) {
		if ((self->st_xact_data = xt_xn_get_xact(db, xap->xp_xact_id, self))) {
			self->st_xact_data->xd_flags &= ~XT_XN_XAC_PREPARED;  // Prepared transactions cannot be swept!
			if (!xt_xn_rollback(self))
				err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
		}
		xt_xn_delete_xa_data(db, xap, TRUE, self);
	}

	ha_temp_close_database(self, thd, temp_thread);
	return 0;
}

#endif

/*
 * -----------------------------------------------------------------------
 * HANDLER LOCKING FUNCTIONS
 *
 * These functions are used get a lock on all handles of a particular table.
 *
 */

static void ha_add_to_handler_list(XTThreadPtr self, XTSharePtr share, ha_pbxt *handler)
{
	xt_lock_mutex(self, (xt_mutex_type *) share->sh_ex_mutex);
	pushr_(xt_unlock_mutex, share->sh_ex_mutex);

	handler->pb_ex_next = share->sh_handlers;
	handler->pb_ex_prev = NULL;
	if (share->sh_handlers)
		share->sh_handlers->pb_ex_prev = handler;
	share->sh_handlers = handler;

	freer_(); // xt_unlock_mutex(share->sh_ex_mutex)
}

static void ha_remove_from_handler_list(XTThreadPtr self, XTSharePtr share, ha_pbxt *handler)
{
	xt_lock_mutex(self, (xt_mutex_type *) share->sh_ex_mutex);
	pushr_(xt_unlock_mutex, share->sh_ex_mutex);

	/* Move front pointer: */
	if (share->sh_handlers == handler)
		share->sh_handlers = handler->pb_ex_next;

	/* Remove from list: */
	if (handler->pb_ex_prev)
		handler->pb_ex_prev->pb_ex_next = handler->pb_ex_next;
	if (handler->pb_ex_next)
		handler->pb_ex_next->pb_ex_prev = handler->pb_ex_prev;

	freer_(); // xt_unlock_mutex(share->sh_ex_mutex)
}

/*
 * Aquire exclusive use of a table, by waiting for all
 * threads to complete use of all handlers of the table.
 * At the same time we hold up all threads
 * that want to use handlers belonging to the table.
 *
 * But we do not hold up threads that close the handlers.
 */
static void ha_aquire_exclusive_use(XTThreadPtr self, XTSharePtr share, ha_pbxt *mine)
{
	ha_pbxt	*handler;
	time_t	end_time = time(NULL) + XT_SHARE_LOCK_TIMEOUT / 1000;

	XT_PRINT1(self, "ha_aquire_exclusive_use (%s) PBXT X lock\n", share->sh_table_path->ps_path);
	/* GOTCHA: It is possible to hang here, if you hold
	 * onto the sh_ex_mutex lock, before we really
	 * have the exclusive lock (i.e. before all
	 * handlers are no longer in use.
	 * The reason is, because reopen() is not possible
	 * when some other thread holds sh_ex_mutex.
	 * So this can prevent a thread from completing its
	 * use of a handler, when prevents exclusive use
	 * here.
	 */
	xt_lock_mutex(self, (xt_mutex_type *) share->sh_ex_mutex);
	pushr_(xt_unlock_mutex, share->sh_ex_mutex);

	/* Wait until we can get an exclusive lock: */
	while (share->sh_table_lock) {
		xt_timed_wait_cond(self, (xt_cond_type *) share->sh_ex_cond, (xt_mutex_type *) share->sh_ex_mutex, XT_SHARE_LOCK_WAIT);
		if (time(NULL) > end_time) {
			freer_(); // xt_unlock_mutex(share->sh_ex_mutex)
			xt_throw_taberr(XT_CONTEXT, XT_ERR_LOCK_TIMEOUT, share->sh_table_path);
		}
	}

	/* This tells readers (and other exclusive lockers) that someone has an exclusive lock. */
	share->sh_table_lock = TRUE;
	
	/* Wait for all open handlers use count to go to 0 */	
	retry:
	handler = share->sh_handlers;
	while (handler) {
		if (handler == mine || !handler->pb_ex_in_use)
			handler = handler->pb_ex_next;
		else {
			/* Wait a bit, and try again: */
			xt_timed_wait_cond(self, (xt_cond_type *) share->sh_ex_cond, (xt_mutex_type *) share->sh_ex_mutex, XT_SHARE_LOCK_WAIT);
			if (time(NULL) > end_time) {
				freer_(); // xt_unlock_mutex(share->sh_ex_mutex)
				xt_throw_taberr(XT_CONTEXT, XT_ERR_LOCK_TIMEOUT, share->sh_table_path);
			}
			/* Handler may have been freed, check from the begining again: */
			goto retry;
		}
	}

	freer_(); // xt_unlock_mutex(share->sh_ex_mutex)
}

/*
 * If you have exclusively locked the table, you can close all handler
 * open tables.
 *
 * Call ha_close_open_tables() to get an exclusive lock.
 */
static void ha_close_open_tables(XTThreadPtr self, XTSharePtr share, ha_pbxt *mine)
{
	ha_pbxt *handler;

	xt_lock_mutex(self, (xt_mutex_type *) share->sh_ex_mutex);
	pushr_(xt_unlock_mutex, share->sh_ex_mutex);

	/* Now that we know no handler is in use, we can close all the
	 * open tables...
	 */
	handler = share->sh_handlers;
	while (handler) {
		if (handler != mine && handler->pb_open_tab) {
			xt_db_return_table_to_pool_ns(handler->pb_open_tab);
			handler->pb_open_tab = NULL;
		}
		handler = handler->pb_ex_next;
	}

	freer_(); // xt_unlock_mutex(share->sh_ex_mutex)
}

#ifdef PBXT_ALLOW_PRINTING
static void ha_release_exclusive_use(XTThreadPtr self, XTSharePtr share)
#else
static void ha_release_exclusive_use(XTThreadPtr XT_UNUSED(self), XTSharePtr share)
#endif
{
	XT_PRINT1(self, "ha_release_exclusive_use (%s) PBXT X UNLOCK\n", share->sh_table_path->ps_path);
	xt_lock_mutex_ns((xt_mutex_type *) share->sh_ex_mutex);
	share->sh_table_lock = FALSE;
	xt_broadcast_cond_ns((xt_cond_type *) share->sh_ex_cond);
	xt_unlock_mutex_ns((xt_mutex_type *) share->sh_ex_mutex);
}

static xtBool ha_wait_for_shared_use(ha_pbxt *mine, XTSharePtr share)
{
	time_t	end_time = time(NULL) + XT_SHARE_LOCK_TIMEOUT / 1000;

	XT_PRINT1(xt_get_self(), "ha_wait_for_shared_use (%s) share lock wait...\n", share->sh_table_path->ps_path);
	mine->pb_ex_in_use = 0;
	xt_lock_mutex_ns((xt_mutex_type *) share->sh_ex_mutex);
	while (share->sh_table_lock) {
		/* Wake up the exclusive locker (may be waiting). He can try to continue: */
		xt_broadcast_cond_ns((xt_cond_type *) share->sh_ex_cond);

		if (!xt_timed_wait_cond(NULL, (xt_cond_type *) share->sh_ex_cond, (xt_mutex_type *) share->sh_ex_mutex, XT_SHARE_LOCK_WAIT)) {
			xt_unlock_mutex_ns((xt_mutex_type *) share->sh_ex_mutex);
			return FAILED;
		}

		if (time(NULL) > end_time) {
			xt_unlock_mutex_ns((xt_mutex_type *) share->sh_ex_mutex);
			xt_register_taberr(XT_REG_CONTEXT, XT_ERR_LOCK_TIMEOUT, share->sh_table_path);
			return FAILED;
		}
	}
	mine->pb_ex_in_use = 1;
	xt_unlock_mutex_ns((xt_mutex_type *) share->sh_ex_mutex);
	return OK;
}

xtPublic int ha_pbxt::reopen()
{
	THD				*thd = current_thd;
	int				err = 0;
	XTThreadPtr		self;	

	if (!(self = ha_set_current_thread(thd, &err)))
		return xt_ha_pbxt_to_mysql_error(err);

	try_(a) {
		xt_ha_open_database_of_table(self, pb_share->sh_table_path);

		ha_open_share(self, pb_share);

		if (!(pb_open_tab = xt_db_open_table_using_tab(pb_share->sh_table, self)))
			xt_throw(self);
		pb_open_tab->ot_thread = self;

		/* {TABLE-STATS}
		 * We no longer use the information that a table
		 * was opened in order to know when to calculate
		 * statistics.
		 */
		if (!pb_open_tab->ot_table->tab_ind_stat_calc_time) {
#ifdef LOAD_TABLE_ON_OPEN
			xt_tab_load_table(self, pb_open_tab);
#else
			xt_tab_load_row_pointers(self, pb_open_tab);
#endif
			xt_ind_set_index_selectivity(pb_open_tab, self);
			/* If the number of rows is less than 150 we will recalculate the
			 * selectity of the indices, as soon as the number of rows
			 * exceeds 200 (see [**])
			 */
#ifdef XT_ROW_COUNT_CORRECTED
			/* {CORRECTED-ROW-COUNT} */
			pb_share->sh_recalc_selectivity = (pb_share->sh_table->tab_row_eof_id - 1 - pb_share->sh_table->tab_row_fnum) < 150;
#else
			/* {FREE-ROWS-BAD} */
			pb_share->sh_recalc_selectivity = (pb_share->sh_table->tab_row_eof_id - 1 /* - pb_share->sh_table->tab_row_fnum */) < 150;
#endif
		}

		/* I am not doing this anymore because it was only required
		 * for DELETE FROM table;, which is now implemented
		 * by deleting each row.
		 * TRUNCATE TABLE does not preserve the counter value.
		 */
		//init_auto_increment(pb_share->sh_min_auto_inc);
		init_auto_increment(0);
	}
	catch_(a) {
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
	}
	cont_(a);
	
	return err;
}

/*
 * -----------------------------------------------------------------------
 * INFORMATION SCHEMA FUNCTIONS
 *
 */

static int pbxt_statistics_fill_table(THD *thd, TABLE_LIST *tables, COND *cond)
{
	XTThreadPtr		self = NULL;	
	int				err = 0;

	if (!pbxt_hton) {
		/* Can't do if PBXT is not loaded! */
		XTExceptionRec	e;

		xt_exception_xterr(&e, XT_CONTEXT, XT_ERR_PBXT_NOT_INSTALLED);
		xt_log_exception(NULL, &e, XT_LOG_DEFAULT);
		/* Just return an empty set: */
		return 0;
	}

	if (!(self = ha_set_current_thread(thd, &err)))
		return xt_ha_pbxt_to_mysql_error(err);


	try_(a) {
		/* If the thread has no open database, and the global
		 * database is already open, then open
		 * the database. Otherwise the statement will be
		 * executed without an open database, which means
		 * that the related statistics will be missing.
		 *
		 * This includes all background threads.
		 */
		if (!self->st_database && pbxt_database) {
			xt_ha_open_database_of_table(self, (XTPathStrPtr) NULL);
		}

		err = myxt_statistics_fill_table(self, thd, tables, cond, (void*) system_charset_info);
	}
	catch_(a) {
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
	}
	cont_(a);
	return err;
}

#ifdef DRIZZLED
ColumnInfo pbxt_statistics_fields_info[]=
{
	ColumnInfo("ID", 4, MYSQL_TYPE_LONG,  0, 0, "The ID of the statistic", SKIP_OPEN_TABLE),
        ColumnInfo("Name", 40, MYSQL_TYPE_STRING, 0, 0, "The name of the statistic", SKIP_OPEN_TABLE),
        ColumnInfo("Value", 8, MYSQL_TYPE_LONGLONG, 0, 0, "The accumulated value", SKIP_OPEN_TABLE),
	ColumnInfo()
};

class PBXTStatisticsMethods : public InfoSchemaMethods
{
public:
  int fillTable(Session *session, TableList *tables, COND *cond)
  {
        return pbxt_statistics_fill_table(session, tables, cond);
  }
};
#else
ST_FIELD_INFO pbxt_statistics_fields_info[]=
{
	{ "ID",		4,	MYSQL_TYPE_LONG,		0, 0, "The ID of the statistic", SKIP_OPEN_TABLE},
	{ "Name",	40, MYSQL_TYPE_STRING,		0, 0, "The name of the statistic", SKIP_OPEN_TABLE},
	{ "Value",	8,	MYSQL_TYPE_LONGLONG,	0, 0, "The accumulated value", SKIP_OPEN_TABLE},
	{ 0,		0,	MYSQL_TYPE_STRING,		0, 0, 0, SKIP_OPEN_TABLE}
};
#endif

#ifdef DRIZZLED
static InfoSchemaTable	*pbxt_statistics_table;
static PBXTStatisticsMethods pbxt_statistics_methods;
static int pbxt_init_statistics(Registry &registry)
#else
static int pbxt_init_statistics(void *p)
#endif
{
#ifdef DRIZZLED
	//pbxt_statistics_table = (InfoSchemaTable *)xt_calloc_ns(sizeof(InfoSchemaTable));
	//pbxt_statistics_table->table_name= "PBXT_STATISTICS";
	pbxt_statistics_table = new InfoSchemaTable("PBXT_STATISTICS");
	pbxt_statistics_table->setColumnInfo(pbxt_statistics_fields_info);
	pbxt_statistics_table->setInfoSchemaMethods(&pbxt_statistics_methods);
	registry.add(pbxt_statistics_table);
#else
	ST_SCHEMA_TABLE *pbxt_statistics_table = (ST_SCHEMA_TABLE *) p;
	pbxt_statistics_table->fields_info = pbxt_statistics_fields_info;
	pbxt_statistics_table->fill_table = pbxt_statistics_fill_table;
#endif

#if defined(XT_WIN) && defined(XT_COREDUMP)
	void register_crash_filter();

	if (pbxt_crash_debug)
		register_crash_filter();
#endif

	return 0;
}

#ifdef DRIZZLED
static int pbxt_exit_statistics(Registry &registry)
#else
static int pbxt_exit_statistics(void *XT_UNUSED(p))
#endif
{
#ifdef DRIZZLED
	registry.remove(pbxt_statistics_table);
	delete pbxt_statistics_table;
#endif
	return(0);
}

/*
 * -----------------------------------------------------------------------
 * DYNAMIC HOOKS
 *
 */

ha_pbxt::ha_pbxt(handlerton *hton, TABLE_SHARE *table_arg) : handler(hton, table_arg)
{
	pb_share = NULL;
	pb_open_tab = NULL;
	pb_key_read = FALSE;
	pb_ignore_dup_key = 0;
	pb_lock_table = FALSE;
	pb_table_locked = 0;
	pb_ex_next = NULL;
	pb_ex_prev = NULL;
	pb_ex_in_use = 0;
	pb_in_stat = FALSE;
}

/*
 * If frm_error() is called then we will use this to to find out what file extentions
 * exist for the storage engine. This is also used by the default rename_table and
 * delete_table method in handler.cc.
 */
#ifdef DRIZZLED
const char **PBXTStorageEngine::bas_ext() const
#else
const char **ha_pbxt::bas_ext() const
#endif
{
	return pbxt_extensions;
}

/*
 * Specify the caching type: HA_CACHE_TBL_NONTRANSACT, HA_CACHE_TBL_NOCACHE
 * HA_CACHE_TBL_ASKTRANSACT, HA_CACHE_TBL_TRANSACT
 */
MX_UINT8_T ha_pbxt::table_cache_type()
{
	return HA_CACHE_TBL_TRANSACT; /* Use transactional query cache */
}

MX_TABLE_TYPES_T ha_pbxt::table_flags() const
{
	return (
		/* We need this flag because records are not packed
		 * into a table which means #ROWID != offset
		 */
		HA_REC_NOT_IN_SEQ |
		/* Since PBXT caches read records itself, I believe
		 * this to be the case.
		 */
		HA_FAST_KEY_READ |
		/*
		 * I am assuming a "key" means a unique index.
		 * Of course a primary key does not allow nulls.
		 */
		HA_NULL_IN_KEY |
		/*
		 * This is necessary because a MySQL blob can be
		 * fairly small.
		 */
		HA_CAN_INDEX_BLOBS |
		/*
		 * Due to transactional influences, this will be
		 * the case.
		 * Although the count is good enough for practical
		 * purposes!
		HA_NOT_EXACT_COUNT |
		 */
#ifndef DRIZZLED
		/*
		 * This basically means we have a file with the name of
		 * database table (which we do).
		 */
		HA_FILE_BASED |
#endif
		/*
		 * Not sure what this does (but MyISAM and InnoDB have it)?!
		 * Could it mean that we support the handler functions.
		 */
		HA_CAN_SQL_HANDLER |
		/*
		 * This is not true, we cannot insert delayed, but a
		 * really cannot see what's wrong with inserting normally
		 * when asked to insert delayed!
		 * And the functionallity is required to pass the alter_table
		 * test.
		 *
		 * Disabled because of MySQL bug #40505
		 */
		/*HA_CAN_INSERT_DELAYED |*/
#if MYSQL_VERSION_ID > 50119
		/* We can do row logging, but not statement, because
		 * MVCC is not serializable!
		 */
		HA_BINLOG_ROW_CAPABLE |
#endif
                /*
                 * ha_pbxt::repair() method does not return HA_ADMIN_NOT_IMPLEMENTED
                 */
                HA_CAN_REPAIR |
		/*
		 * Auto-increment is allowed on a partial key.
		 */
		HA_AUTO_PART_KEY);
}

/*
 * The following query from the DBT1 test is VERY slow
 * if we do not set HA_READ_ORDER.
 * The reason is that it must scan all duplicates, then
 * sort.
 *
 * SELECT o_id, o_carrier_id, o_entry_d, o_ol_cnt
 * FROM orders FORCE INDEX (o_w_id)
 * WHERE o_w_id = 2
   * AND o_d_id = 1
   * AND o_c_id = 500
 * ORDER BY o_id DESC limit 1;
 *
 */
#define FLAGS_ARE_READ_DYNAMICALLY

MX_ULONG_T ha_pbxt::index_flags(uint XT_UNUSED(inx), uint XT_UNUSED(part), bool XT_UNUSED(all_parts)) const
{
	/* It would be nice if the dynamic version of this function works,
	 * but it does not. MySQL loads this information when the table is openned,
	 * and then it is fixed.
	 *
	 * The problem is, I have had to remove the HA_READ_ORDER option although
	 * it applies to PBXT. PBXT returns entries in index order during an index
	 * scan in _almost_ all cases.
	 *
	 * A number of cases are demostrated here: [(11)]
	 *
	 * If involves the following conditions:
	 * - a SELECT FOR UPDATE, UPDATE or DELETE statement
	 * - an ORDER BY, or join that requires the sort order
	 * - another transaction which updates the index while it is being
	 *   scanned.
	 *
	 * In this "obscure" case, the index scan may return index
	 * entries in the wrong order.
	 */
#ifdef FLAGS_ARE_READ_DYNAMICALLY
	/* If were are in an update (SELECT FOR UPDATE, UPDATE or DELETE), then
	 * it may be that we return the rows from an index in the wrong
	 * order! This is due to the fact that update reads wait for transactions
	 * to commit and this means that index entries may change position during
	 * the scan!
	 */
	if (pb_open_tab && pb_open_tab->ot_for_update)
		return (HA_READ_NEXT | HA_READ_PREV | HA_READ_RANGE | HA_KEYREAD_ONLY);
	/* If I understand HA_KEYREAD_ONLY then this means I do not
	 * need to fetch the record associated with an index
	 * key.
	 */
	return (HA_READ_NEXT | HA_READ_PREV | HA_READ_ORDER | HA_READ_RANGE | HA_KEYREAD_ONLY);
#else
	return (HA_READ_NEXT | HA_READ_PREV | HA_READ_RANGE | HA_KEYREAD_ONLY);
#endif
}

void ha_pbxt::internal_close(THD *thd, struct XTThread *self)
{
	if (pb_share) {
		xtBool			removed;
		XTOpenTablePtr	ot;

		try_(a) {
			/* This lock must be held when we remove the handler's
			 * open table because ha_close_open_tables() can run
			 * concurrently.
			 */
			xt_lock_mutex_ns(pb_share->sh_ex_mutex);
			if ((ot = pb_open_tab)) {
				pb_open_tab->ot_thread = self;
				if (self->st_database != pb_open_tab->ot_table->tab_db)
					xt_ha_open_database_of_table(self, pb_share->sh_table_path);
				pb_open_tab = NULL;
				pushr_(xt_db_return_table_to_pool, ot);
			}
			xt_unlock_mutex_ns(pb_share->sh_ex_mutex);

			ha_remove_from_handler_list(self, pb_share, this);

			/* Someone may be waiting for me to complete: */
			xt_broadcast_cond_ns((xt_cond_type *) pb_share->sh_ex_cond);

			removed = ha_unget_share_removed(self, pb_share);

			if (ot) {
				/* Flush the table if this was the last handler: */
				/* This is not necessary but has the affect that
				 * FLUSH TABLES; does a checkpoint!
				 */
				if (removed) {
					/* GOTCHA:
					 * This was killing performance as the number of threads increased!
					 *
					 * When MySQL runs out of table handlers because the table
					 * handler cache is too small, it starts to close handlers.
					 * (open_cache.records > table_cache_size)
					 *
					 * Which can lead to closing all handlers for a particular table.
					 *
					 * It does this while holding lock_OPEN!
					 * So this code below leads to a sync operation while lock_OPEN
					 * is held. The result is that the whole server comes to a stop.
					 */
					if (!thd || thd_sql_command(thd) == SQLCOM_FLUSH) // FLUSH TABLES
						xt_sync_flush_table(self, ot);
					else {
						/* This change is a result of a problem mentioned by Arjen.
						 * REPAIR and ALTER lead to the following sequence:
						 * 1. tab  -- copy --> tmp1
						 * 2. tab  -- rename --> tmp2
						 * 3. tmp1 -- rename --> tab
						 * 4. delete tmp2
						 *
						 * PBXT flushes a table before rename.
						 * In the sequence above results in a table flush in step 3 which can
						 * take a very long time.
						 *
						 * The problem is, during this time frame we have only temp tables.
						 * A crash in this state leaves the database in a bad state.
						 *
						 * To reduce the time in this state, the flush needs to be done
						 * elsewhere. The code below causes the flish to occur after
						 * step 1:
						 */ 
						switch (thd_sql_command(thd)) {
							case SQLCOM_REPAIR:
							case SQLCOM_RENAME_TABLE:
							case SQLCOM_OPTIMIZE:
							case SQLCOM_ANALYZE:
							case SQLCOM_ALTER_TABLE:
							case SQLCOM_CREATE_INDEX:
								xt_sync_flush_table(self, ot);
								break;
						}
					}
				}
				freer_(); // xt_db_return_table_to_pool(ot);
			}
		}
		catch_(a) {
			xt_log_and_clear_exception(self);
		}
		cont_(a);

		pb_share = NULL;
	}
}

/*
 * Used for opening tables. The name will be the name of the file.
 * A table is opened when it needs to be opened. For instance
 * when a request comes in for a select on the table (tables are not
 * open and closed for each request, they are cached).

 * Called from handler.cc by handler::ha_open(). The server opens all tables by
 * calling ha_open() which then calls the handler specific open().
 */
int ha_pbxt::open(const char *table_path, int XT_UNUSED(mode), uint XT_UNUSED(test_if_locked))
{
	THD			*thd = current_thd;
	int			err = 0;
	XTThreadPtr	self;

	ref_length = XT_RECORD_OFFS_SIZE;

	if (!(self = ha_set_current_thread(thd, &err)))
		return xt_ha_pbxt_to_mysql_error(err);

	XT_PRINT1(self, "open (%s)\n", table_path);

	pb_ex_in_use = 1;
	try_(a) {
		xt_ha_open_database_of_table(self, (XTPathStrPtr) table_path);

		pb_share = ha_get_share(self, table_path, false);
		ha_add_to_handler_list(self, pb_share, this);
		if (pb_share->sh_table_lock) {
			if (!ha_wait_for_shared_use(this, pb_share))
				xt_throw(self);
		}

		ha_open_share(self, pb_share);

		thr_lock_data_init(&pb_share->sh_lock, &pb_lock, NULL);
		if (!(pb_open_tab = xt_db_open_table_using_tab(pb_share->sh_table, self)))
			xt_throw(self);
		pb_open_tab->ot_thread = self;

		/* {TABLE-STATS} */
		if (!pb_open_tab->ot_table->tab_ind_stat_calc_time) {
#ifdef LOAD_TABLE_ON_OPEN
			xt_tab_load_table(self, pb_open_tab);
#else
			xt_tab_load_row_pointers(self, pb_open_tab);
#endif

			xt_ind_set_index_selectivity(pb_open_tab, self);
#ifdef XT_ROW_COUNT_CORRECTED
			/* {CORRECTED-ROW-COUNT} */
			pb_share->sh_recalc_selectivity = (pb_share->sh_table->tab_row_eof_id - 1 - pb_share->sh_table->tab_row_fnum) < 150;
#else
			/* {FREE-ROWS-BAD} */
			pb_share->sh_recalc_selectivity = (pb_share->sh_table->tab_row_eof_id - 1 /* - pb_share->sh_table->tab_row_fnum */) < 150;
#endif
		}

		init_auto_increment(0);
	}
	catch_(a) {
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
		internal_close(thd, self);
	}
	cont_(a);

	if (!err)
		info(HA_STATUS_NO_LOCK | HA_STATUS_VARIABLE | HA_STATUS_CONST);

	pb_ex_in_use = 0;
	if (pb_share) {
		/* Someone may be waiting for me to complete: */
		if (pb_share->sh_table_lock)
			xt_broadcast_cond_ns((xt_cond_type *) pb_share->sh_ex_cond);
	}
	return err;
}


/*
	Closes a table. We call the free_share() function to free any resources
	that we have allocated in the "shared" structure.

	Called from sql_base.cc, sql_select.cc, and table.cc.
	In sql_select.cc it is only used to close up temporary tables or during
	the process where a temporary table is converted over to being a
	myisam table.
	For sql_base.cc look at close_data_tables().
*/
int ha_pbxt::close(void)
{
	THD						*thd = current_thd;
	volatile int			err = 0;
	volatile XTThreadPtr	self;

	if (thd)
		self = ha_set_current_thread(thd, (int *) &err);
	else {
		XTExceptionRec e;

		if (!(self = xt_create_thread("TempForClose", FALSE, TRUE, &e))) {
			xt_log_exception(NULL, &e, XT_LOG_DEFAULT);
			return 0;
		}
	}

	XT_PRINT1(self, "close (%s)\n", pb_share && pb_share->sh_table_path->ps_path ? pb_share->sh_table_path->ps_path : "unknown");

	if (self) {
		try_(a) {
			internal_close(thd, self);
		}
		catch_(a) {
			err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
		}
		cont_(a);

		if (!thd)
			xt_free_thread(self);
	}
	else
		xt_log(XT_NS_CONTEXT, XT_LOG_WARNING, "Unable to release table reference\n");
		
	return err;
}

void ha_pbxt::init_auto_increment(xtWord8 min_auto_inc)
{
	XTTableHPtr	tab;
	xtWord8		nr = 0;
	int			err;

	/* Get the value of the auto-increment value by
	 * loading the highest value from the index...
	 */
	tab = pb_open_tab->ot_table;

	/* Cannot do this if the index version is bad! */
	if (tab->tab_dic.dic_disable_index)
		return;

	xt_spinlock_lock(&tab->tab_ainc_lock);
	if (table->found_next_number_field && !tab->tab_auto_inc) {
		Field		*tmp_fie = table->next_number_field;
		THD			*tmp_thd = table->in_use;
		xtBool		xn_started = FALSE;
		XTThreadPtr	self = pb_open_tab->ot_thread;

		/*
		 * A table may be opened by a thread with a running
		 * transaction!
		 * Since get_auto_increment() does not do an update,
		 * it should be OK to use the transaction we already
		 * have to get the next auto-increment value.
		 */
		if (!self->st_xact_data) {
			self->st_xact_mode = XT_XACT_REPEATABLE_READ;
			self->st_ignore_fkeys = FALSE;
			self->st_auto_commit = TRUE;
			self->st_table_trans = FALSE;
			self->st_abort_trans = FALSE;
			self->st_stat_ended = FALSE;
			self->st_stat_trans = FALSE;
			self->st_is_update = NULL;
			if (!xt_xn_begin(self)) {
				xt_spinlock_unlock(&tab->tab_ainc_lock);
				xt_throw(self);
			}
			xn_started = TRUE;
		}

		/* Setup the conditions for the next call! */
		table->in_use = current_thd;
		table->next_number_field = table->found_next_number_field;

		extra(HA_EXTRA_KEYREAD);
		table->mark_columns_used_by_index_no_reset(TS(table)->next_number_index, table->read_set);
		column_bitmaps_signal();
 		index_init(TS(table)->next_number_index, 0);
		if (!TS(table)->next_number_key_offset) {
			// Autoincrement at key-start
			err = index_last(table->record[1]);
			if (!err && !table->next_number_field->is_null(TS(table)->rec_buff_length)) {
				/* {PRE-INC} */
				nr = (xtWord8) table->next_number_field->val_int_offset(TS(table)->rec_buff_length);
			}
		}
		else {
			/* Do an index scan to find the largest value! */
			/* The standard method will not work because it forces
			 * us to lock that table!
			 */
			xtWord8 val;

			err = index_first(table->record[1]);
			while (!err) {
				/* {PRE-INC} */
				val = (xtWord8) table->next_number_field->val_int_offset(TS(table)->rec_buff_length);
				if (val > nr)
					nr = val;
				err = index_next(table->record[1]);
			}
		}

		index_end();
		extra(HA_EXTRA_NO_KEYREAD);

		/* {PRE-INC}
		 * I have changed this from post increment to pre-increment!
		 * The reason is:
		 * When using post increment we are not able to return
		 * the last valid value in the range.
		 *
		 * Here the test example:
		 *
		 * drop table if exists t1;
		 * create table t1 (i tinyint unsigned not null auto_increment primary key) engine=pbxt;
		 * insert into t1 set i = 254;
		 * insert into t1 set i = null;
		 *
		 * With post-increment, this last insert fails because on post increment
		 * the value overflows!
		 *
		 * Pre-increment means we store the current max, and increment
		 * before returning the next value.
		 *
		 * This will work in this situation.
		 */
		tab->tab_auto_inc = nr;
		if (tab->tab_auto_inc < tab->tab_dic.dic_min_auto_inc)
			tab->tab_auto_inc = tab->tab_dic.dic_min_auto_inc-1;
		if (tab->tab_auto_inc < min_auto_inc)
			tab->tab_auto_inc = min_auto_inc-1;

		/* Restore the changed values: */
		table->next_number_field = tmp_fie;
		table->in_use = tmp_thd;

		if (xn_started) {
			XT_PRINT0(self, "xt_xn_commit in init_auto_increment\n");
			xt_xn_commit(self);
		}
	}
	xt_spinlock_unlock(&tab->tab_ainc_lock);
}

void ha_pbxt::get_auto_increment(MX_ULONGLONG_T offset, MX_ULONGLONG_T increment,
                                 MX_ULONGLONG_T XT_UNUSED(nb_desired_values),
                                 MX_ULONGLONG_T *first_value,
                                 MX_ULONGLONG_T *nb_reserved_values)
{
	register XTTableHPtr	tab;
	MX_ULONGLONG_T			nr, nr_less_inc;

	ASSERT_NS(pb_ex_in_use);

	tab = pb_open_tab->ot_table;

	/* {PRE-INC}
	 * Assume that nr contains the last value returned!
	 * We will increment and then return the value.
	 */
	xt_spinlock_lock(&tab->tab_ainc_lock);
	nr = (MX_ULONGLONG_T) tab->tab_auto_inc;
	nr_less_inc = nr;
	if (nr < offset)
		nr = offset;
	else if (increment > 1 && ((nr - offset) % increment) != 0)
		nr += increment - ((nr - offset) % increment);
	else
		nr += increment;
	if (table->next_number_field->cmp((const unsigned char *)&nr_less_inc, (const unsigned char *)&nr) < 0)
		tab->tab_auto_inc = (xtWord8) (nr);
	else
		nr = ~0;	/* indicate error to the caller */
	xt_spinlock_unlock(&tab->tab_ainc_lock);

	*first_value = nr;
	*nb_reserved_values = 1;
}

/* GOTCHA: We need to use signed value here because of the test
 * (from auto_increment.test):
 * create table t1 (a int not null auto_increment primary key);
 * insert into t1 values (NULL);
 * insert into t1 values (-1);
 * insert into t1 values (NULL);
 */
xtPublic void ha_set_auto_increment(XTOpenTablePtr ot, Field *nr)
{
	register XTTableHPtr	tab;
	MX_ULONGLONG_T			nr_int_val;
	
	nr_int_val = nr->val_int();
	tab = ot->ot_table;

	if (nr->cmp((const unsigned char *)&tab->tab_auto_inc) > 0) {
		xt_spinlock_lock(&tab->tab_ainc_lock);

		if (nr->cmp((const unsigned char *)&tab->tab_auto_inc) > 0) {
			/* {PRE-INC}
			 * We increment later, so just set the value!
			MX_ULONGLONG_T nr_int_val_plus_one = nr_int_val + 1;
			if (nr->cmp((const unsigned char *)&nr_int_val_plus_one) < 0)
				tab->tab_auto_inc = nr_int_val_plus_one;
			else
			 */
			tab->tab_auto_inc = nr_int_val;
		}
		xt_spinlock_unlock(&tab->tab_ainc_lock);
	}

	if (xt_db_auto_increment_mode == 1) {
		if (nr_int_val > (MX_ULONGLONG_T) tab->tab_dic.dic_min_auto_inc) {
			/* Do this every 100 calls: */
#ifdef DEBUG
			tab->tab_dic.dic_min_auto_inc = nr_int_val + 5;
#else
			tab->tab_dic.dic_min_auto_inc = nr_int_val + 100;
#endif
			ot->ot_thread = xt_get_self();
			if (!xt_tab_write_min_auto_inc(ot))
				xt_log_and_clear_exception(ot->ot_thread);
		}
	}
}

/*
static void dump_buf(unsigned char *buf, int len)
{
	int i;
	
	for (i=0; i<len; i++) printf("%2c", buf[i] <= 127 ? buf[i] : '.');
	printf("\n");
	for (i=0; i<len; i++) printf("%02x", buf[i]);
	printf("\n");
}
*/

/*
 * write_row() inserts a row. No extra() hint is given currently if a bulk load
 * is happeneding. buf() is a byte array of data. You can use the field
 * information to extract the data from the native byte array type.
 * Example of this would be:
 * for (Field **field=table->field ; *field ; field++)
 * {
 *		...
 * }

 * See ha_tina.cc for an example of extracting all of the data as strings.
 * ha_berekly.cc has an example of how to store it intact by "packing" it
 * for ha_berkeley's own native storage type.

 * See the note for update_row() on auto_increments and timestamps. This
 * case also applied to write_row().

 * Called from item_sum.cc, item_sum.cc, sql_acl.cc, sql_insert.cc,
 * sql_insert.cc, sql_select.cc, sql_table.cc, sql_udf.cc, and sql_update.cc.
 */
int ha_pbxt::write_row(byte *buf)
{
	int err = 0;

	ASSERT_NS(pb_ex_in_use);

	XT_PRINT1(pb_open_tab->ot_thread, "write_row (%s)\n", pb_share->sh_table_path->ps_path);
	XT_DISABLED_TRACE(("INSERT tx=%d val=%d\n", (int) pb_open_tab->ot_thread->st_xact_data->xd_start_xn_id, (int) XT_GET_DISK_4(&buf[1])));
	//statistic_increment(ha_write_count,&LOCK_status);
#ifdef PBMS_ENABLED
	PBMSResultRec result;
	err = pbms_write_row_blobs(table, buf, &result);
	if (err) {
		xt_logf(XT_NT_ERROR, "pbms_write_row_blobs() Error: %s", result.mr_message);
		return err;
	}
#endif

	/* {START-STAT-HACK} previously position of start statement hack. */

	xt_xlog_check_long_writer(pb_open_tab->ot_thread);

	if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT)
		table->timestamp_field->set_time();

	if (table->next_number_field && buf == table->record[0]) {
		int update_err = update_auto_increment();
		if (update_err) {
			ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
			err = update_err;
			goto done;
		}
		ha_set_auto_increment(pb_open_tab, table->next_number_field);
	}

	if (!xt_tab_new_record(pb_open_tab, (xtWord1 *) buf)) {
		err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);

		/*
		 * This is needed to allow the same row to be updated multiple times in case of bulk REPLACE.
		 * This happens during execution of LOAD DATA...REPLACE MySQL first tries to INSERT the row 
		 * and if it gets dup-key error it tries UPDATE, so the same row can be overwriten multiple 
		 * times within the same statement
		 */
		if (err == HA_ERR_FOUND_DUPP_KEY && pb_open_tab->ot_thread->st_is_update) {
			/* Pop the update stack: */
			//pb_open_tab->ot_thread->st_update_id++;
			XTOpenTablePtr curr = pb_open_tab->ot_thread->st_is_update;

			pb_open_tab->ot_thread->st_is_update = curr->ot_prev_update;
			curr->ot_prev_update = NULL;
		}
	}

	done:
#ifdef PBMS_ENABLED
	pbms_completed(table, (err == 0));
#endif
	return err;
}

#ifdef UNUSED_CODE
static int equ_bin(const byte *a, const char *b)
{
	while (*a && *b) {
		if (*a != *b)
			return 0;
		a++;
		b++;
	}
	return 1;
}
static void dump_bin(const byte *a_in, int offset, int len_in)
{
	const byte	*a = a_in;
	int			len = len_in;
	
	a += offset;
	while (len > 0) {
		xt_trace("%02X", (int) *a);
		a++;
		len--;
	}
	xt_trace("==");
	a = a_in;
	len = len_in;
	a += offset;
	while (len > 0) {
		xt_trace("%c", (*a > 8 && *a < 127) ? *a : '.');
		a++;
		len--;
	}
	xt_trace("\n");
}
#endif

/*
 * Yes, update_row() does what you expect, it updates a row. old_data will have
 * the previous row record in it, while new_data will have the newest data in
 * it. Keep in mind that the server can do updates based on ordering if an ORDER BY
 * clause was used. Consecutive ordering is not guarenteed.
 *
 * Called from sql_select.cc, sql_acl.cc, sql_update.cc, and sql_insert.cc.
 */
int ha_pbxt::update_row(const byte * old_data, byte * new_data)
{
	int						err = 0;
	register XTThreadPtr	self = pb_open_tab->ot_thread;

	ASSERT_NS(pb_ex_in_use);

	XT_PRINT1(self, "update_row (%s)\n", pb_share->sh_table_path->ps_path);
	XT_DISABLED_TRACE(("UPDATE tx=%d val=%d\n", (int) self->st_xact_data->xd_start_xn_id, (int) XT_GET_DISK_4(&new_data[1])));
	//statistic_increment(ha_update_count,&LOCK_status);

	/* {START-STAT-HACK} previously position of start statement hack. */

	xt_xlog_check_long_writer(self);

	/* {UPDATE-STACK} */
	if (self->st_is_update != pb_open_tab) {
		/* Push the update stack: */
		pb_open_tab->ot_prev_update = self->st_is_update;
		self->st_is_update = pb_open_tab;
		pb_open_tab->ot_update_id++;
	}

	if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
		table->timestamp_field->set_time();

#ifdef PBMS_ENABLED
	PBMSResultRec result;

	err = pbms_delete_row_blobs(table, old_data, &result);
	if (err) {
		xt_logf(XT_NT_ERROR, "update_row:pbms_delete_row_blobs() Error: %s", result.mr_message);
		return err;
	}
	err = pbms_write_row_blobs(table, new_data, &result);
	if (err) { 
		xt_logf(XT_NT_ERROR, "update_row:pbms_write_row_blobs() Error: %s", result.mr_message);
		goto pbms_done;
	}
#endif

	/* GOTCHA: We need to check the auto-increment value on update
	 * because of the following test (which fails for InnoDB) -
	 * auto_increment.test:
	 * create table t1 (a int not null auto_increment primary key, val int);
	 * insert into t1 (val) values (1);
	 * update t1 set a=2 where a=1;
	 * insert into t1 (val) values (1);
	 */
	if (table->found_next_number_field && new_data == table->record[0]) {
		MX_LONGLONG_T	nr;
		my_bitmap_map	*old_map;

		old_map = mx_tmp_use_all_columns(table, table->read_set);
		nr = table->found_next_number_field->val_int();
		ha_set_auto_increment(pb_open_tab, table->found_next_number_field);
		mx_tmp_restore_column_map(table, old_map);
	}

	if (!xt_tab_update_record(pb_open_tab, (xtWord1 *) old_data, (xtWord1 *) new_data))
		err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);

	pb_open_tab->ot_table->tab_locks.xt_remove_temp_lock(pb_open_tab, TRUE);
	
#ifdef PBMS_ENABLED
	pbms_done:
	pbms_completed(table, (err == 0));
#endif

	return err;
}

/*
 * This will delete a row. buf will contain a copy of the row to be deleted.
 * The server will call this right after the current row has been called (from
 * either a previous rnd_next() or index call).
 *
 * Called in sql_acl.cc and sql_udf.cc to manage internal table information.
 * Called in sql_delete.cc, sql_insert.cc, and sql_select.cc. In sql_select it is
 * used for removing duplicates while in insert it is used for REPLACE calls.
*/
int ha_pbxt::delete_row(const byte * buf)
{
	int err = 0;

	ASSERT_NS(pb_ex_in_use);

	XT_PRINT1(pb_open_tab->ot_thread, "delete_row (%s)\n", pb_share->sh_table_path->ps_path);
	XT_DISABLED_TRACE(("DELETE tx=%d val=%d\n", (int) pb_open_tab->ot_thread->st_xact_data->xd_start_xn_id, (int) XT_GET_DISK_4(&buf[1])));
	//statistic_increment(ha_delete_count,&LOCK_status);

#ifdef PBMS_ENABLED
	PBMSResultRec result;

	err = pbms_delete_row_blobs(table, buf, &result);
	if (err) {
		xt_logf(XT_NT_ERROR, "pbms_delete_row_blobs() Error: %s", result.mr_message);
		return err;
	}
#endif

	/* {START-STAT-HACK} previously position of start statement hack. */

	xt_xlog_check_long_writer(pb_open_tab->ot_thread);

	if (!xt_tab_delete_record(pb_open_tab, (xtWord1 *) buf))
		err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);

	pb_open_tab->ot_table->tab_locks.xt_remove_temp_lock(pb_open_tab, TRUE);

#ifdef PBMS_ENABLED
	pbms_completed(table, (err == 0));
#endif
	return err;
}

/*
 * -----------------------------------------------------------------------
 * INDEX METHODS
 */

/*
 * This looks like a hack, but actually, it is OK.
 * It depends on the setup done by the super-class. It involves an extra
 * range check that we need to do if a "new" record is returned during
 * an index scan.
 *
 * A new record is returned if a row is updated (by another transaction)
 * during the index scan. If an update is detected, then the scan stops
 * and waits for the transaction to end.
 *
 * If the transaction commits, then the updated row is returned instead
 * of the row it would have returned when doing a consistant read
 * (repeatable read).
 *
 * These new records can appear out of index order, and may not even
 * belong to the index range that we are concerned with.
 *
 * Notice that there is not check for the start of the range. It appears
 * that this is not necessary, MySQL seems to have no problem ignoring
 * such values.
 *
 * A number of test have been given below which demonstrate the use
 * of the function.
 *
 * They also demonstrate the ORDER BY problem described here: [(11)].
 *
 * DROP TABLE IF EXISTS test_tab, test_tab_1, test_tab_2;
 * CREATE TABLE test_tab (ID int primary key, Value int, Name varchar(20), index(Value, Name)) ENGINE=pbxt;
 * INSERT test_tab values(1, 1, 'A');
 * INSERT test_tab values(2, 1, 'B');
 * INSERT test_tab values(3, 1, 'C');
 * INSERT test_tab values(4, 2, 'D');
 * INSERT test_tab values(5, 2, 'E');
 * INSERT test_tab values(6, 2, 'F');
 * INSERT test_tab values(7, 2, 'G');
 * 
 * select * from test_tab where value = 1 order by value, name for update;
 * 
 * -- Test: 1
 * -- C1
 * begin;
 * select * from test_tab where id = 5 for update;
 * 
 * -- C2
 * begin;
 * select * from test_tab where value = 2 order by value, name for update;
 * 
 * -- C1
 * update test_tab set value = 3 where id = 6;
 * commit;
 * 
 * -- Test: 2
 * -- C1
 * begin;
 * select * from test_tab where id = 5 for update;
 * 
 * -- C2
 * begin;
 * select * from test_tab where value >= 2 order by value, name for update;
 * 
 * -- C1
 * update test_tab set value = 3 where id = 6;
 * commit;
 * 
 * -- Test: 3
 * -- C1
 * begin;
 * select * from test_tab where id = 5 for update;
 * 
 * -- C2
 * begin;
 * select * from test_tab where value = 2 order by value, name for update;
 * 
 * -- C1
 * update test_tab set value = 1 where id = 6;
 * commit;
 */

int ha_pbxt::xt_index_in_range(register XTOpenTablePtr XT_UNUSED(ot), register XTIndexPtr ind,
	register XTIdxSearchKeyPtr search_key, xtWord1 *buf)
{
	/* If search key is given, this means we want an exact match. */
	if (search_key) {
		xtWord1 key_buf[XT_INDEX_MAX_KEY_SIZE];

		myxt_create_key_from_row(ind, key_buf, buf, NULL);
		search_key->sk_on_key = myxt_compare_key(ind, search_key->sk_key_value.sv_flags, search_key->sk_key_value.sv_length,
			search_key->sk_key_value.sv_key, key_buf) == 0;
		return search_key->sk_on_key;
	}

	/* Otherwise, check the end of the range. */
	if (end_range)
		return compare_key(end_range) <= 0;
	return 1;
}

int ha_pbxt::xt_index_next_read(register XTOpenTablePtr ot, register XTIndexPtr ind, xtBool key_only,
	register XTIdxSearchKeyPtr search_key, byte *buf)
{
	xt_xlog_check_long_writer(ot->ot_thread);

	if (key_only) {
		/* We only need to read the data from the key: */
		while (ot->ot_curr_rec_id) {
			if (search_key && !search_key->sk_on_key)
				break;

			switch (xt_tab_visible(ot)) {
				case FALSE:
					if (xt_idx_next(ot, ind, search_key))
						break;
				case XT_ERR:
					goto failed;
				case XT_NEW:
					if (!xt_idx_read(ot, ind, (xtWord1 *) buf))
						goto failed;
					if (xt_index_in_range(ot, ind, search_key, buf)) {
						return 0;
					}
					if (!xt_idx_next(ot, ind, search_key))
						goto failed;
					break;
				case XT_RETRY:
					/* We cannot start from the beginning again, if we have
					 * already output rows!
					 * And we need the orginal search key.
					 *
					 * The case in which this occurs is:
					 *
					 * T1: UPDATE tbl_file SET GlobalID = 'DBCD5C4514210200825501089884844_6M' WHERE ID = 39
					 * Locks a particular row.
					 *
					 * T2: SELECT ID,Flags FROM tbl_file WHERE SpaceID = 1 AND Path = '/zi/America/' AND 
					 * Name = 'Cuiaba' AND Flags IN ( 0,1,4,5 ) FOR UPDATE
					 * scans the index and stops on the lock (of the before image) above.
					 *
					 * T1 quits, the sweeper deletes the record updated by T1?!
					 * BUG: Cleanup should wait until T2 is complete!
					 *
					 * T2 continues, and returns XT_RETRY.
					 *
					 * At this stage T2 has already returned some rows, so it may not retry from the
					 * start. Instead it tries to locate the last record it tried to lock.
					 * This record is gone (or not visible), so it finds the next one.
					 *
					 * POTENTIAL BUG: If cleanup does not wait until T2 is complete, then
					 * I may miss the update record, if it is moved before the index scan
					 * position.
					 */
					if (!pb_ind_row_count && search_key) {
						if (!xt_idx_search(pb_open_tab, ind, search_key))
							return ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
					}
					else {
						if (!xt_idx_research(pb_open_tab, ind))
							goto failed;
					}
					break;
				default:
					if (!xt_idx_read(ot, ind, (xtWord1 *) buf))
						goto failed;
					return 0;
			}
		}
	}
	else {
		while (ot->ot_curr_rec_id) {
			if (search_key && !search_key->sk_on_key)
				break;

			switch (xt_tab_read_record(ot, (xtWord1 *) buf)) {
				case FALSE:
					XT_DISABLED_TRACE(("not visi tx=%d rec=%d\n", (int) ot->ot_thread->st_xact_data->xd_start_xn_id, (int) ot->ot_curr_rec_id));
					if (xt_idx_next(ot, ind, search_key))
						break;
				case XT_ERR:
					goto failed;
				case XT_NEW:
					if (xt_index_in_range(ot, ind, search_key, buf))
						return 0;
					if (!xt_idx_next(ot, ind, search_key))
						goto failed;
					break;
				case XT_RETRY:
					if (!pb_ind_row_count && search_key) {
						if (!xt_idx_search(pb_open_tab, ind, search_key))
							return ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
					}
					else {
						if (!xt_idx_research(pb_open_tab, ind))
							goto failed;
					}
					break;
				default:
					XT_DISABLED_TRACE(("visible tx=%d rec=%d\n", (int) ot->ot_thread->st_xact_data->xd_start_xn_id, (int) ot->ot_curr_rec_id));
					return 0;
			}
		}
	}
	return HA_ERR_END_OF_FILE;

	failed:
	return ha_log_pbxt_thread_error_for_mysql(FALSE);
}

int ha_pbxt::xt_index_prev_read(XTOpenTablePtr ot, XTIndexPtr ind, xtBool key_only,
	register XTIdxSearchKeyPtr search_key, byte *buf)
{
	if (key_only) {
		/* We only need to read the data from the key: */
		while (ot->ot_curr_rec_id) {
			if (search_key && !search_key->sk_on_key)
				break;

			switch (xt_tab_visible(ot)) {
				case FALSE:
					if (xt_idx_prev(ot, ind, search_key))
						break;
				case XT_ERR:
					goto failed;
				case XT_NEW:
					if (!xt_idx_read(ot, ind, (xtWord1 *) buf))
						goto failed;
					if (xt_index_in_range(ot, ind, search_key, buf))
						return 0;
					if (!xt_idx_next(ot, ind, search_key))
						goto failed;
					break;
				case XT_RETRY:
					if (!pb_ind_row_count && search_key) {
						if (!xt_idx_search_prev(pb_open_tab, ind, search_key))
							return ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
					}
					else {
						if (!xt_idx_research(pb_open_tab, ind))
							goto failed;
					}
					break;
				default:
					if (!xt_idx_read(ot, ind, (xtWord1 *) buf))
						goto failed;
					return 0;
			}
		}
	}
	else {
		/* We need to read the entire record: */
		while (ot->ot_curr_rec_id) {
			if (search_key && !search_key->sk_on_key)
				break;

			switch (xt_tab_read_record(ot, (xtWord1 *) buf)) {
				case FALSE:
					if (xt_idx_prev(ot, ind, search_key))
						break;
				case XT_ERR:
					goto failed;
				case XT_NEW:
					if (xt_index_in_range(ot, ind, search_key, buf))
						return 0;
					if (!xt_idx_next(ot, ind, search_key))
						goto failed;
					break;
				case XT_RETRY:
					if (!pb_ind_row_count && search_key) {
						if (!xt_idx_search_prev(pb_open_tab, ind, search_key))
							return ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
					}
					else {
						if (!xt_idx_research(pb_open_tab, ind))
							goto failed;
					}
					break;
				default:
					return 0;
			}
		}
	}
	return HA_ERR_END_OF_FILE;

	failed:
	return ha_log_pbxt_thread_error_for_mysql(FALSE);
}

int ha_pbxt::index_init(uint idx, bool XT_UNUSED(sorted))
{
	XTIndexPtr	ind;
	XTThreadPtr	thread = pb_open_tab->ot_thread;

	/* select count(*) from smalltab_PBXT;
	 * ignores the error below, and continues to
	 * call index_first!
	 */
	active_index = idx;

	if (pb_open_tab->ot_table->tab_dic.dic_disable_index) {
		active_index = MAX_KEY;
		xt_tab_set_index_error(pb_open_tab->ot_table);
		return ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
	}

	/* The number of columns required: */
	if (pb_open_tab->ot_is_modify) {

		pb_open_tab->ot_cols_req = table->read_set->MX_BIT_SIZE();
#ifdef XT_PRINT_INDEX_OPT
		ind = (XTIndexPtr) pb_share->sh_dic_keys[idx];

		printf("index_init %s index %d cols req=%d/%d read_bits=%X write_bits=%X index_bits=%X\n", pb_open_tab->ot_table->tab_name->ps_path, (int) idx, pb_open_tab->ot_cols_req, pb_open_tab->ot_cols_req, (int) *table->read_set->bitmap, (int) *table->write_set->bitmap, (int) *ind->mi_col_map.bitmap);
#endif
		/* {START-STAT-HACK} previously position of start statement hack,
		 * previous comment to code below: */
		/* Start a statement based transaction as soon
		 * as a read is done for a modify type statement!
		 * Previously, this was done too late!
		 */
	}
	else {
		pb_open_tab->ot_cols_req = ha_get_max_bit(table->read_set);

		/* Check for index coverage!
		 *
		 * Given the following table:
		 *
		 * CREATE TABLE `customer` (
		 * `c_id` int(11) NOT NULL DEFAULT '0',
		 * `c_d_id` int(11) NOT NULL DEFAULT '0',
		 * `c_w_id` int(11) NOT NULL DEFAULT '0',
		 * `c_first` varchar(16) DEFAULT NULL,
		 * `c_middle` char(2) DEFAULT NULL,
		 * `c_last` varchar(16) DEFAULT NULL,
		 * `c_street_1` varchar(20) DEFAULT NULL,
		 * `c_street_2` varchar(20) DEFAULT NULL,
		 * `c_city` varchar(20) DEFAULT NULL,
		 * `c_state` char(2) DEFAULT NULL,
		 * `c_zip` varchar(9) DEFAULT NULL,
		 * `c_phone` varchar(16) DEFAULT NULL,
		 * `c_since` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
		 * `c_credit` char(2) DEFAULT NULL,
		 * `c_credit_lim` decimal(24,12) DEFAULT NULL,
		 * `c_discount` double DEFAULT NULL,
		 * `c_balance` decimal(24,12) DEFAULT NULL,
		 * `c_ytd_payment` decimal(24,12) DEFAULT NULL,
		 * `c_payment_cnt` double DEFAULT NULL,
		 * `c_delivery_cnt` double DEFAULT NULL,
		 * `c_data` text,
		 * PRIMARY KEY (`c_w_id`,`c_d_id`,`c_id`),
		 * KEY `c_w_id` (`c_w_id`,`c_d_id`,`c_last`,`c_first`,`c_id`)
		 * ) ENGINE=PBXT;
		 *
		 * MySQL does not recognize index coverage on the followin select:
		 *
		 * SELECT c_id FROM customer WHERE c_w_id = 3 AND c_d_id = 8 AND 
		 * c_last = 'EINGATIONANTI' ORDER BY c_first ASC LIMIT 1;
		 *
		 * TODO: Find out why this is necessary, MyISAM does not
		 * seem to have this problem!
		 */
		ind = (XTIndexPtr) pb_share->sh_dic_keys[idx];
		if (MX_BIT_IS_SUBSET(table->read_set, &ind->mi_col_map))
			pb_key_read = TRUE;
#ifdef XT_PRINT_INDEX_OPT
		printf("index_init %s index %d cols req=%d/%d read_bits=%X write_bits=%X index_bits=%X converage=%d\n", pb_open_tab->ot_table->tab_name->ps_path, (int) idx, pb_open_tab->ot_cols_req, table->read_set->MX_BIT_SIZE(), (int) *table->read_set->bitmap, (int) *table->write_set->bitmap, (int) *ind->mi_col_map.bitmap, (int) (MX_BIT_IS_SUBSET(table->read_set, &ind->mi_col_map) != 0));
#endif
	}
	
	xt_xlog_check_long_writer(thread);

	pb_open_tab->ot_thread->st_statistics.st_scan_index++;
	return 0;
}

int ha_pbxt::index_end()
{
	int err = 0;

	XT_TRACE_METHOD();

	XTThreadPtr thread = pb_open_tab->ot_thread;

	/*
	 * the assertion below is not always held, because the sometimes handler is unlocked
	 * before this function is called
	 */
	/*ASSERT_NS(pb_ex_in_use);*/

	if (pb_open_tab->ot_ind_rhandle) {
		xt_ind_release_handle(pb_open_tab->ot_ind_rhandle, FALSE, thread);
		pb_open_tab->ot_ind_rhandle = NULL;
	}

	/*
	 * make permanent the lock for the last scanned row
	 */
	if (pb_open_tab)
		pb_open_tab->ot_table->tab_locks.xt_make_lock_permanent(pb_open_tab, &thread->st_lock_list);

	xt_xlog_check_long_writer(thread);

	active_index = MAX_KEY;
	XT_RETURN(err);
}

#ifdef XT_TRACK_RETURNED_ROWS
void ha_start_scan(XTOpenTablePtr ot, u_int index)
{
	xt_ttracef(ot->ot_thread, "SCAN %d:%d\n", (int) ot->ot_table->tab_id, (int) index);
	ot->ot_rows_ret_curr = 0;
	for (u_int i=0; i<ot->ot_rows_ret_max; i++)
		ot->ot_rows_returned[i] = 0;
}

void ha_return_row(XTOpenTablePtr ot, u_int index)
{
	xt_ttracef(ot->ot_thread, "%d:%d ROW=%d:%d\n",
		(int) ot->ot_table->tab_id, (int) index, (int) ot->ot_curr_row_id, (int) ot->ot_curr_rec_id);
	ot->ot_rows_ret_curr++;
	if (ot->ot_curr_row_id >= ot->ot_rows_ret_max) {
		if (!xt_realloc_ns((void **) &ot->ot_rows_returned, (ot->ot_curr_row_id+1) * sizeof(xtRecordID)))
			ASSERT_NS(FALSE);
		memset(&ot->ot_rows_returned[ot->ot_rows_ret_max], 0, (ot->ot_curr_row_id+1 - ot->ot_rows_ret_max) * sizeof(xtRecordID));
		ot->ot_rows_ret_max = ot->ot_curr_row_id+1;
	}
	if (!ot->ot_curr_row_id || !ot->ot_curr_rec_id || ot->ot_rows_returned[ot->ot_curr_row_id]) {
		char *sql = *thd_query(current_thd);

		xt_ttracef(ot->ot_thread, "DUP %d:%d %s\n",
			(int) ot->ot_table->tab_id, (int) index, *thd_query(current_thd));
		xt_dump_trace();
		printf("ERROR: row=%d rec=%d newr=%d, already returned!\n", (int) ot->ot_curr_row_id, (int) ot->ot_rows_returned[ot->ot_curr_row_id], (int) ot->ot_curr_rec_id);
		printf("ERROR: %s\n", sql);
#ifdef XT_WIN
		FatalAppExit(0, "Debug Me!");
#endif
	}
	else
		ot->ot_rows_returned[ot->ot_curr_row_id] = ot->ot_curr_rec_id;
}
#endif

int ha_pbxt::index_read_xt(byte * buf, uint idx, const byte *key, uint key_len, enum ha_rkey_function find_flag)
{
	int					err = 0;
	XTIndexPtr			ind;
	int					prefix = 0;
	XTIdxSearchKeyRec	search_key;

	if (idx == MAX_KEY) {
		err = HA_ERR_WRONG_INDEX;
		goto done;
	}
#ifdef XT_TRACK_RETURNED_ROWS
	ha_start_scan(pb_open_tab, idx);
#endif

	/* This call starts a search on this handler! */
	pb_ind_row_count = 0;

	ASSERT_NS(pb_ex_in_use);

	XT_PRINT1(pb_open_tab->ot_thread, "index_read_xt (%s)\n", pb_share->sh_table_path->ps_path);
	XT_DISABLED_TRACE(("search tx=%d val=%d update=%d\n", (int) pb_open_tab->ot_thread->st_xact_data->xd_start_xn_id, (int) XT_GET_DISK_4(key), pb_modified));
	ind = (XTIndexPtr) pb_share->sh_dic_keys[idx];

	switch (find_flag) {
		case HA_READ_PREFIX_LAST:
		case HA_READ_PREFIX_LAST_OR_PREV:
			prefix = SEARCH_PREFIX;
		case HA_READ_BEFORE_KEY:
		case HA_READ_KEY_OR_PREV: // I assume you want to be positioned on the last entry in the key duplicate list!! 
			xt_idx_prep_key(ind, &search_key, ((find_flag == HA_READ_BEFORE_KEY) ? 0 : XT_SEARCH_AFTER_KEY) | prefix, (xtWord1 *) key, (size_t) key_len);
			if (!xt_idx_search_prev(pb_open_tab, ind, &search_key))
				err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
			else
				err = xt_index_prev_read(pb_open_tab, ind, pb_key_read,
					(find_flag == HA_READ_PREFIX_LAST) ? &search_key : NULL, buf);
			break;
		case HA_READ_PREFIX:
			prefix = SEARCH_PREFIX;
		case HA_READ_KEY_EXACT:
		case HA_READ_KEY_OR_NEXT:
		case HA_READ_AFTER_KEY:
		default:
			xt_idx_prep_key(ind, &search_key, ((find_flag == HA_READ_AFTER_KEY) ? XT_SEARCH_AFTER_KEY : 0) | prefix, (xtWord1 *) key, key_len);
			if (!xt_idx_search(pb_open_tab, ind, &search_key))
				err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
			else {
				err = xt_index_next_read(pb_open_tab, ind, pb_key_read,
					(find_flag == HA_READ_KEY_EXACT || find_flag == HA_READ_PREFIX) ? &search_key : NULL, buf);
				if (err == HA_ERR_END_OF_FILE && find_flag == HA_READ_AFTER_KEY)
					err = HA_ERR_KEY_NOT_FOUND;			
			}
			break;
	}

	pb_ind_row_count++;
#ifdef XT_TRACK_RETURNED_ROWS
	if (!err)
		ha_return_row(pb_open_tab, idx);
#endif
	XT_DISABLED_TRACE(("search tx=%d val=%d err=%d\n", (int) pb_open_tab->ot_thread->st_xact_data->xd_start_xn_id, (int) XT_GET_DISK_4(key), err));
	done:
	if (err)
		table->status = STATUS_NOT_FOUND;
	else {
		pb_open_tab->ot_thread->st_statistics.st_row_select++;
		table->status = 0;
	}
	return err;
}

/*
 * Positions an index cursor to the index specified in the handle. Fetches the
 * row if available. If the key value is null, begin at the first key of the
 * index.
 */
int ha_pbxt::index_read(byte * buf, const byte * key, uint key_len, enum ha_rkey_function find_flag)
{
	//statistic_increment(ha_read_key_count,&LOCK_status);
	return index_read_xt(buf, active_index, key, key_len, find_flag);
}

int ha_pbxt::index_read_idx(byte * buf, uint idx, const byte *key, uint key_len, enum ha_rkey_function find_flag)
{
	//statistic_increment(ha_read_key_count,&LOCK_status);
	return index_read_xt(buf, idx, key, key_len, find_flag);
}

int ha_pbxt::index_read_last(byte * buf, const byte * key, uint key_len)
{
	//statistic_increment(ha_read_key_count,&LOCK_status);
	return index_read_xt(buf, active_index, key, key_len, HA_READ_PREFIX_LAST);
}

/*
 * Used to read forward through the index.
 */
int ha_pbxt::index_next(byte * buf)
{
	int			err = 0;
	XTIndexPtr	ind;

	XT_TRACE_METHOD();
	//statistic_increment(ha_read_next_count,&LOCK_status);
	ASSERT_NS(pb_ex_in_use);

	if (active_index == MAX_KEY) {
		err = HA_ERR_WRONG_INDEX;
		goto done;
	}
	ind = (XTIndexPtr) pb_share->sh_dic_keys[active_index];

	if (!xt_idx_next(pb_open_tab, ind, NULL))
		err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
	else
		err = xt_index_next_read(pb_open_tab, ind, pb_key_read, NULL, buf);

	pb_ind_row_count++;
#ifdef XT_TRACK_RETURNED_ROWS
	if (!err)
		ha_return_row(pb_open_tab, active_index);
#endif
	done:
	if (err)
		table->status = STATUS_NOT_FOUND;
	else {
		pb_open_tab->ot_thread->st_statistics.st_row_select++;
		table->status = 0;
	}
	XT_RETURN(err);
}

/*
 * I have implemented this because there is currently a
 * bug in handler::index_next_same().
 *
 * drop table if exists t1;
 * CREATE TABLE t1 (a int, b int, primary key(a,b))
 * PARTITION BY KEY(b,a) PARTITIONS 2;
 * insert into t1 values (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6);
 * select * from t1 where a = 4;
 * 
 */
int ha_pbxt::index_next_same(byte * buf, const byte *key, uint length)
{
	int					err = 0;
	XTIndexPtr			ind;
	XTIdxSearchKeyRec	search_key;

	XT_TRACE_METHOD();
	//statistic_increment(ha_read_next_count,&LOCK_status);
	ASSERT_NS(pb_ex_in_use);

	if (active_index == MAX_KEY) {
		err = HA_ERR_WRONG_INDEX;
		goto done;
	}
	ind = (XTIndexPtr) pb_share->sh_dic_keys[active_index];

	search_key.sk_key_value.sv_flags = HA_READ_KEY_EXACT;
	search_key.sk_key_value.sv_rec_id = 0;
	search_key.sk_key_value.sv_row_id = 0;
	search_key.sk_key_value.sv_key = search_key.sk_key_buf;
	search_key.sk_key_value.sv_length = myxt_create_key_from_key(ind, search_key.sk_key_buf, (xtWord1 *) key, (u_int) length);
	search_key.sk_on_key = TRUE;

	if (!xt_idx_next(pb_open_tab, ind, &search_key))
		err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
	else
		err = xt_index_next_read(pb_open_tab, ind, pb_key_read, &search_key, buf);

	pb_ind_row_count++;
#ifdef XT_TRACK_RETURNED_ROWS
	if (!err)
		ha_return_row(pb_open_tab, active_index);
#endif
	done:
	if (err)
		table->status = STATUS_NOT_FOUND;
	else {
		pb_open_tab->ot_thread->st_statistics.st_row_select++;
		table->status = 0;
	}
	XT_RETURN(err);
}

/*
 * Used to read backwards through the index.
 */
int ha_pbxt::index_prev(byte * buf)
{
	int			err = 0;
	XTIndexPtr	ind;

	XT_TRACE_METHOD();
	//statistic_increment(ha_read_prev_count,&LOCK_status);
	ASSERT_NS(pb_ex_in_use);

	if (active_index == MAX_KEY) {
		err = HA_ERR_WRONG_INDEX;
		goto done;
	}
	ind = (XTIndexPtr) pb_share->sh_dic_keys[active_index];

	if (!xt_idx_prev(pb_open_tab, ind, NULL))
		err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
	else
		err = xt_index_prev_read(pb_open_tab, ind, pb_key_read, NULL, buf);

	pb_ind_row_count++;
#ifdef XT_TRACK_RETURNED_ROWS
	if (!err)
		ha_return_row(pb_open_tab, active_index);
#endif
	done:
	if (err)
		table->status = STATUS_NOT_FOUND;
	else {
		pb_open_tab->ot_thread->st_statistics.st_row_select++;
		table->status = 0;
	}
	XT_RETURN(err);
}

/*
 * index_first() asks for the first key in the index.
 */
int ha_pbxt::index_first(byte * buf)
{
	int					err = 0;
	XTIndexPtr			ind;
	XTIdxSearchKeyRec	search_key;

	XT_TRACE_METHOD();
	//statistic_increment(ha_read_first_count,&LOCK_status);
	ASSERT_NS(pb_ex_in_use);

	/* This is required because MySQL ignores the error returned
	 * init init_index sometimes, for example:
	 *
     * if (!table->file->inited)
     *    table->file->ha_index_init(tab->index, tab->sorted);
     *  if ((error=tab->table->file->index_first(tab->table->record[0])))
	 */
	if (active_index == MAX_KEY) {
		err = HA_ERR_WRONG_INDEX;
		goto done;
	}

#ifdef XT_TRACK_RETURNED_ROWS
	ha_start_scan(pb_open_tab, active_index);
#endif
	pb_ind_row_count = 0;

	ind = (XTIndexPtr) pb_share->sh_dic_keys[active_index];

	xt_idx_prep_key(ind, &search_key, XT_SEARCH_FIRST_FLAG, NULL, 0);
	if (!xt_idx_search(pb_open_tab, ind, &search_key))
		err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
	else
		err = xt_index_next_read(pb_open_tab, ind, pb_key_read, NULL, buf);

	pb_ind_row_count++;
#ifdef XT_TRACK_RETURNED_ROWS
	if (!err)
		ha_return_row(pb_open_tab, active_index);
#endif
	done:
	if (err)
		table->status = STATUS_NOT_FOUND;
	else {
		pb_open_tab->ot_thread->st_statistics.st_row_select++;
		table->status = 0;
	}
	XT_RETURN(err);
}

/*
 * index_last() asks for the last key in the index.
 */
int ha_pbxt::index_last(byte * buf)
{
	int					err = 0;
	XTIndexPtr			ind;
	XTIdxSearchKeyRec	search_key;

	XT_TRACE_METHOD();
	//statistic_increment(ha_read_last_count,&LOCK_status);
	ASSERT_NS(pb_ex_in_use);

	if (active_index == MAX_KEY) {
		err = HA_ERR_WRONG_INDEX;
		goto done;
	}

#ifdef XT_TRACK_RETURNED_ROWS
	ha_start_scan(pb_open_tab, active_index);
#endif
	pb_ind_row_count = 0;

	ind = (XTIndexPtr) pb_share->sh_dic_keys[active_index];

	xt_idx_prep_key(ind, &search_key, XT_SEARCH_AFTER_LAST_FLAG, NULL, 0);
	if (!xt_idx_search_prev(pb_open_tab, ind, &search_key))
		err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
	else
		err = xt_index_prev_read(pb_open_tab, ind, pb_key_read, NULL, buf);

	pb_ind_row_count++;
#ifdef XT_TRACK_RETURNED_ROWS
	if (!err)
		ha_return_row(pb_open_tab, active_index);
#endif
	done:
	if (err)
		table->status = STATUS_NOT_FOUND;
	else {
		pb_open_tab->ot_thread->st_statistics.st_row_select++;
		table->status = 0;
	}
	XT_RETURN(err);
}

/*
 * -----------------------------------------------------------------------
 * RAMDOM/SEQUENTIAL READ METHODS
 */
 
/*
 * rnd_init() is called when the system wants the storage engine to do a table
 * scan.
 * See the example in the introduction at the top of this file to see when
 * rnd_init() is called.
 *
 * Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc,
 * and sql_update.cc.
 */
int ha_pbxt::rnd_init(bool scan)
{
	int			err = 0;
	XTThreadPtr	thread = pb_open_tab->ot_thread;

	XT_PRINT1(thread, "rnd_init (%s)\n", pb_share->sh_table_path->ps_path);
	XT_DISABLED_TRACE(("seq scan tx=%d\n", (int) thread->st_xact_data->xd_start_xn_id));

	/* Call xt_tab_seq_exit() to make sure the resources used by the previous
	 * scan are freed. In particular make sure cache page ref count is decremented.
	 * This is needed as rnd_init() can be called mulitple times w/o matching calls 
	 * to rnd_end(). Our experience is that currently this is done in queries like:
	 *
	 * SELECT t1.c1,t2.c1 FROM t1 LEFT JOIN t2 USING (c1);
	 * UPDATE t1 LEFT JOIN t2 USING (c1) SET t1.c1 = t2.c1 WHERE t1.c1 = t2.c1;
	 *
	 * when scanning inner tables. It is important to understand that in such case
	 * multiple calls to rnd_init() are not semantically equal to a new query. For
	 * example we cannot make row locks permanent as we do in rnd_end(), as 
	 * ha_pbxt::unlock_row still can be called.
	 */
	xt_tab_seq_exit(pb_open_tab);

	/* The number of columns required: */
	if (pb_open_tab->ot_is_modify) {
		pb_open_tab->ot_cols_req = table->read_set->MX_BIT_SIZE();
		/* {START-STAT-HACK} previously position of start statement hack,
		 * previous comment to code below: */
		/* Start a statement based transaction as soon
		 * as a read is done for a modify type statement!
		 * Previously, this was done too late!
		 */
	}
	else {
		pb_open_tab->ot_cols_req = ha_get_max_bit(table->read_set);

		/*
		 * in case of queries like SELECT COUNT(*) FROM t
		 * table->read_set is empty. Otoh, ot_cols_req == 0 can be treated
		 * as "all columns" by some internal code (see e.g. myxt_load_row), 
		 * which makes such queries very ineffective for the records with 
		 * extended part. Setting column count to 1 makes sure that the 
		 * extended part will not be acessed in most cases.
		 */

		if (pb_open_tab->ot_cols_req == 0)
			pb_open_tab->ot_cols_req = 1;
	}

	ASSERT_NS(pb_ex_in_use);
	if (scan) {
		if (!xt_tab_seq_init(pb_open_tab))
			err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
	}
	else
		xt_tab_seq_reset(pb_open_tab);

	xt_xlog_check_long_writer(thread);

	return err;
}

int ha_pbxt::rnd_end()
{
	XT_TRACE_METHOD();

	/*
	 * make permanent the lock for the last scanned row
	 */
	XTThreadPtr thread = pb_open_tab->ot_thread;
	if (pb_open_tab)
		pb_open_tab->ot_table->tab_locks.xt_make_lock_permanent(pb_open_tab, &thread->st_lock_list);

	xt_xlog_check_long_writer(thread);

	xt_tab_seq_exit(pb_open_tab);
	XT_RETURN(0);
}

/*
 * This is called for each row of the table scan. When you run out of records
 * you should return HA_ERR_END_OF_FILE. Fill buff up with the row information.
 * The Field structure for the table is the key to getting data into buf
 * in a manner that will allow the server to understand it.
 *
 * Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc,
 * and sql_update.cc.
 */
int ha_pbxt::rnd_next(byte *buf)
{
	int		err = 0;
	xtBool	eof;

	XT_TRACE_METHOD();
	ASSERT_NS(pb_ex_in_use);
	//statistic_increment(ha_read_rnd_next_count, &LOCK_status);
	xt_xlog_check_long_writer(pb_open_tab->ot_thread);

	if (!xt_tab_seq_next(pb_open_tab, (xtWord1 *) buf, &eof))
		err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
	else if (eof)
		err = HA_ERR_END_OF_FILE;

	if (err)
		table->status = STATUS_NOT_FOUND;
	else {
		pb_open_tab->ot_thread->st_statistics.st_row_select++;
		table->status = 0;
	}
	XT_RETURN(err);
}

/*
 * position() is called after each call to rnd_next() if the data needs
 * to be ordered. You can do something like the following to store
 * the position:
 * ha_store_ptr(ref, ref_length, current_position);
 *
 * The server uses ref to store data. ref_length in the above case is
 * the size needed to store current_position. ref is just a byte array
 * that the server will maintain. If you are using offsets to mark rows, then
 * current_position should be the offset. If it is a primary key like in
 * BDB, then it needs to be a primary key.
 *
 * Called from filesort.cc, sql_select.cc, sql_delete.cc and sql_update.cc.
 */
void ha_pbxt::position(const byte *XT_UNUSED(record))
{
	XT_TRACE_METHOD();
	ASSERT_NS(pb_ex_in_use);
	/*
	 * I changed this from using little endian to big endian.
	 *
	 * The reason is because sometime the pointer are sorted.
	 * When they are are sorted a binary compare is used.
	 * A binary compare sorts big endian values correctly!
	 *
	 * Take the followin example:
	 *
	 * create table t1 (a int, b text);
	 * insert into t1 values (1, 'aa'), (1, 'bb'), (1, 'cc');
	 * select group_concat(b) from t1 group by a;
	 *
	 * With little endian pointers the result is:
	 * aa,bb,cc
	 *
	 * With big-endian pointer the result is:
	 * aa,cc,bb
	 *
	 */
	(void) ASSERT_NS(XT_RECORD_OFFS_SIZE == 4);
	mi_int4store((xtWord1 *) ref, pb_open_tab->ot_curr_rec_id);
	XT_RETURN_VOID;
}

/*
 * Given the #ROWID retrieve the record.
 *
 * Called from filesort.cc records.cc sql_insert.cc sql_select.cc sql_update.cc.
 */
int ha_pbxt::rnd_pos(byte * buf, byte *pos)
{
	int err = 0;

	XT_TRACE_METHOD();
	ASSERT_NS(pb_ex_in_use);
	//statistic_increment(ha_read_rnd_count, &LOCK_status);
	XT_PRINT1(pb_open_tab->ot_thread, "rnd_pos (%s)\n", pb_share->sh_table_path->ps_path);

	pb_open_tab->ot_curr_rec_id = mi_uint4korr((xtWord1 *) pos);
	switch (xt_tab_dirty_read_record(pb_open_tab, (xtWord1 *) buf)) {
		case FALSE:
			err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
			break;
		default:
			break;
	}		

	if (err)
		table->status = STATUS_NOT_FOUND;
	else {
		pb_open_tab->ot_thread->st_statistics.st_row_select++;
		table->status = 0;
	}
	XT_RETURN(err);
}

/*
 * -----------------------------------------------------------------------
 * INFO METHODS
 */
 
/*
	::info() is used to return information to the optimizer.
	Currently this table handler doesn't implement most of the fields
	really needed. SHOW also makes use of this data
	Another note, you will probably want to have the following in your
	code:
	if (records < 2)
		records = 2;
	The reason is that the server will optimize for cases of only a single
	record. If in a table scan you don't know the number of records
	it will probably be better to set records to two so you can return
	as many records as you need.
	Along with records a few more variables you may wish to set are:
		records
		deleted
		data_file_length
		index_file_length
		delete_length
		check_time
	Take a look at the public variables in handler.h for more information.

	Called in:
		filesort.cc
		ha_heap.cc
		item_sum.cc
		opt_sum.cc
		sql_delete.cc
		sql_delete.cc
		sql_derived.cc
		sql_select.cc
		sql_select.cc
		sql_select.cc
		sql_select.cc
		sql_select.cc
		sql_show.cc
		sql_show.cc
		sql_show.cc
		sql_show.cc
		sql_table.cc
		sql_union.cc
		sql_update.cc

*/
#if MYSQL_VERSION_ID < 50114
void ha_pbxt::info(uint flag)
#else
int ha_pbxt::info(uint flag)
#endif
{
	XTOpenTablePtr	ot;
	int				in_use;

	XT_TRACE_METHOD();
	
	if (!(in_use = pb_ex_in_use)) {
		pb_ex_in_use = 1;
		if (pb_share && pb_share->sh_table_lock) {
			/* If some thread has an exclusive lock, then
			 * we wait for the lock to be removed:
			 */
#if MYSQL_VERSION_ID < 50114
			ha_wait_for_shared_use(this, pb_share);
			pb_ex_in_use = 1;
#else
			if (!ha_wait_for_shared_use(this, pb_share))
				return ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
#endif
		}
	}

	if ((ot = pb_open_tab)) {
		if (flag & HA_STATUS_VARIABLE) {
				register XTTableHPtr tab = ot->ot_table;

			/* {FREE-ROWS-BAD}
			 * Free row count is not reliable, so ignore it.
			 * The problem is if tab_row_fnum > tab_row_eof_id - 1 then
			 * we have a very bad result.
			 *
			 * If stats.records+EXTRA_RECORDS == 0 as returned by 
			 * estimate_rows_upper_bound(), then filesort will crash here:
			 *
			 * make_sortkey(param,sort_keys[idx++],ref_pos);
			 * 
			 * #0	0x000bf69c in Field_long::sort_string at field.cc:3766
			 * #1	0x0022e1f1 in make_sortkey at filesort.cc:769
			 * #2	0x0022f1cf in find_all_keys at filesort.cc:619
			 * #3	0x00230eec in filesort at filesort.cc:243
			 * #4	0x001b9d89 in mysql_update at sql_update.cc:415
			 * #5	0x0010db12 in mysql_execute_command at sql_parse.cc:2959
			 * #6	0x0011480d in mysql_parse at sql_parse.cc:5787
			 * #7	0x00115afb in dispatch_command at sql_parse.cc:1200
			 * #8	0x00116de2 in do_command at sql_parse.cc:857
			 * #9	0x00101ee4 in handle_one_connection at sql_connect.cc:1115
			 *
			 * The problem is that sort_keys is allocated to handle just 1 vector.
			 * Sorting one vector crashes. Although I could not find a check for
			 * the actual number of vectors. But it must assume that it has at
			 * least EXTRA_RECORDS vectors.
			 */
#ifdef XT_ROW_COUNT_CORRECTED
			if (tab->tab_row_eof_id <= tab->tab_row_fnum ||
				(!tab->tab_row_free_id && tab->tab_row_fnum))
				xt_tab_check_free_lists(NULL, ot, false, true);
			stats.records = (ha_rows) tab->tab_row_eof_id - 1;
			if (stats.records >= tab->tab_row_fnum) {
				stats.deleted = tab->tab_row_fnum;
				stats.records -= stats.deleted;
			}
			else {
				stats.deleted = 0;
				stats.records = 2;
			}
#else
			stats.deleted = /* tab->tab_row_fnum */ 0;
			stats.records = (ha_rows) (tab->tab_row_eof_id - 1 /* - stats.deleted */);
#endif
			stats.data_file_length = xt_rec_id_to_rec_offset(tab, tab->tab_rec_eof_id);
			stats.index_file_length = xt_ind_node_to_offset(tab, tab->tab_ind_eof);
			stats.delete_length = tab->tab_rec_fnum * ot->ot_rec_size;
			//check_time = info.check_time;
			stats.mean_rec_length = (ulong) ot->ot_rec_size;
		}

		if (flag & HA_STATUS_CONST) {
			ha_rows		rec_per_key;
			XTIndexPtr	ind;
			TABLE_SHARE	*share= TS(table);

			stats.max_data_file_length = 0x00FFFFFF;
			stats.max_index_file_length = 0x00FFFFFF;
			//stats.create_time = info.create_time;
			ref_length = XT_RECORD_OFFS_SIZE;
			//share->db_options_in_use = info.options;
			stats.block_size = XT_INDEX_PAGE_SIZE;

			if (share->tmp_table == NO_TMP_TABLE)
#ifdef DRIZZLED
#define WHICH_MUTEX			mutex
#elif MYSQL_VERSION_ID >= 50404
#define WHICH_MUTEX			LOCK_ha_data
#else
#define WHICH_MUTEX			mutex
#endif

				mysql_mutex_lock(&share->WHICH_MUTEX);
#ifdef DRIZZLED
			set_prefix(share->keys_in_use, share->keys);
			share->keys_for_keyread&= share->keys_in_use;
#else
			share->keys_in_use.set_prefix(share->keys);
			//share->keys_in_use.intersect_extended(info.key_map);
			share->keys_for_keyread.intersect(share->keys_in_use);
			//share->db_record_offset = info.record_offset;
#endif
			for (u_int i = 0; i < share->keys; i++) {
				ind = pb_share->sh_dic_keys[i];

				rec_per_key = 0;
				if (ind->mi_seg_count == 1 && (ind->mi_flags & HA_NOSAME))
					rec_per_key = 1;
				else {
					rec_per_key = 1;	
				}
				for (u_int j = 0; j < table->key_info[i].key_parts; j++)
	 				table->key_info[i].rec_per_key[j] = (ulong) rec_per_key;
			}
			if (share->tmp_table == NO_TMP_TABLE)
				mysql_mutex_unlock(&share->WHICH_MUTEX);
	  		/*
			 Set data_file_name and index_file_name to point at the symlink value
			 if table is symlinked (Ie;  Real name is not same as generated name)
	   		*/
	   		/*
			data_file_name = index_file_name = 0;
			fn_format(name_buff, file->filename, "", MI_NAME_DEXT, 2);
			if (strcmp(name_buff, info.data_file_name))
				data_file_name = info.data_file_name;
			strmov(fn_ext(name_buff), MI_NAME_IEXT);
			if (strcmp(name_buff, info.index_file_name))
				index_file_name = info.index_file_name;
			*/
		}

 		if (flag & HA_STATUS_ERRKEY)
	 		errkey = ot->ot_err_index_no;

		/* {PRE-INC}
		 * We assume they want the next value to be returned!
		 *
		 * At least, this is what works for the following code:
		 *
		 * create table t1 (a int auto_increment primary key)
		 * auto_increment=100
		 * engine=pbxt
		 * partition by list (a)
		 * (partition p0 values in (1, 98,99, 100, 101));
		 * create index inx on t1 (a);
		 * insert into t1 values (null);
		 * select * from t1;
		 */
		if (flag & HA_STATUS_AUTO)
			stats.auto_increment_value = (ulonglong) ot->ot_table->tab_auto_inc+1;
	}
	else
		errkey = (uint) -1;

	if (!in_use) {
		pb_ex_in_use = 0;
		if (pb_share) {
			/* Someone may be waiting for me to complete: */
			if (pb_share->sh_table_lock)
				xt_broadcast_cond_ns((xt_cond_type *) pb_share->sh_ex_cond);
		}
	}
#if MYSQL_VERSION_ID < 50114
	XT_RETURN_VOID;
#else
	XT_RETURN(0);
#endif
}

/*
 * extra() is called whenever the server wishes to send a hint to
 * the storage engine. The myisam engine implements the most hints.
 * ha_innodb.cc has the most exhaustive list of these hints.
 */
int ha_pbxt::extra(enum ha_extra_function operation)
{
	int err = 0;

	XT_PRINT2(xt_get_self(), "ha_pbxt::extra (%s) operation=%d\n", pb_share->sh_table_path->ps_path, operation);

	switch (operation) {
		case HA_EXTRA_RESET_STATE:
			pb_key_read = FALSE;
			pb_ignore_dup_key = 0;
			/* As far as I can tell, this function is called for
			 * every table at the end of a statement.
			 *
			 * So, during a LOCK TABLES ... UNLOCK TABLES, I use
			 * this to find the end of a statement.
			 * start_stmt() indicates the start of a statement,
			 * and is also called once for each table in the
			 * statement.
			 *
			 * So the statement boundary is indicated by 
			 * self->st_stat_count == 0
			 *
			 * GOTCHA: I cannot end the transaction here!
			 * I must end it in start_stmt().
			 * The reason is because there are situations
			 * where this would end a transaction that
			 * was begin by external_lock().
			 *
			 * An example of this is when a function
			 * is called when doing CREATE TABLE SELECT.
			 */
			if (pb_in_stat) {
				/* NOTE: pb_in_stat is just used to avoid getting
				 * self, if it is not necessary!!
				 */
				XTThreadPtr self;

				pb_in_stat = FALSE;

				if (!(self = ha_set_current_thread(pb_mysql_thd, &err)))
					return xt_ha_pbxt_to_mysql_error(err);

				if (self->st_stat_count > 0) {
					self->st_stat_count--;
					if (self->st_stat_count == 0)
						self->st_stat_ended = TRUE;
				}

				/* This is the end of a statement, I can turn any locks into perminant locks now: */
				if (pb_open_tab)
					pb_open_tab->ot_table->tab_locks.xt_make_lock_permanent(pb_open_tab, &self->st_lock_list);
			}
			if (pb_open_tab)
				pb_open_tab->ot_for_update = 0;
			break;
		case HA_EXTRA_KEYREAD:
			/* This means we so not need to read the entire record. */
			pb_key_read = TRUE;
			break;
		case HA_EXTRA_NO_KEYREAD:
			pb_key_read = FALSE;
			break;
		case HA_EXTRA_IGNORE_DUP_KEY:
			/* NOTE!!! Calls to extra(HA_EXTRA_IGNORE_DUP_KEY) can be nested!
			 * In fact, the calls are from different threads, so
			 * strictly speaking I should protect this variable!!
			 * Here is the sequence that produces the duplicate call:
			 *
			 * drop table if exists t1;
			 * CREATE TABLE t1 (x int not null, y int, primary key (x)) engine=pbxt;
			 * insert into t1 values (1, 3), (4, 1);
			 * replace DELAYED into t1 (x, y) VALUES (4, 2);
			 * select * from t1 order by x;
			 *
			 */
			pb_ignore_dup_key++;
			break;
		case HA_EXTRA_NO_IGNORE_DUP_KEY:
			pb_ignore_dup_key--;
			break;
		case HA_EXTRA_KEYREAD_PRESERVE_FIELDS:
			/* MySQL needs all fields */
			pb_key_read = FALSE;
			break;
		default:
			break;
	}

	return err;
}


/*
 * Deprecated and likely to be removed in the future. Storage engines normally
 * just make a call like:
 * ha_pbxt::extra(HA_EXTRA_RESET);
 * to handle it.
 */
int ha_pbxt::reset(void)
{
	XT_TRACE_METHOD();
	extra(HA_EXTRA_RESET_STATE);
	XT_RETURN(0);
}

void ha_pbxt::unlock_row()
{
	XT_TRACE_METHOD();
	if (pb_open_tab)
		pb_open_tab->ot_table->tab_locks.xt_remove_temp_lock(pb_open_tab, FALSE);
}

/*
 * Used to delete all rows in a table. Both for cases of truncate and
 * for cases where the optimizer realizes that all rows will be
 * removed as a result of a SQL statement.
 *
 * Called from item_sum.cc by Item_func_group_concat::clear(),
 * Item_sum_count_distinct::clear(), and Item_func_group_concat::clear().
 * Called from sql_delete.cc by mysql_delete().
 * Called from sql_select.cc by JOIN::reinit().
 * Called from sql_union.cc by st_select_lex_unit::exec().
 */
int ha_pbxt::delete_all_rows()
{
	THD				*thd = current_thd;
	int				err = 0;
	XTThreadPtr		self;
	XTDDTable		*tab_def = NULL;
	char			path[PATH_MAX];

	XT_TRACE_METHOD();

	if (thd_sql_command(thd) != SQLCOM_TRUNCATE) {
		/* Just like InnoDB we only handle TRUNCATE TABLE
		 * by recreating the table.
		 * DELETE FROM t must be handled by deleting
		 * each row because it may be part of a transaction,
		 * and there may be foreign key actions.
		 */
		XT_RETURN (my_errno = HA_ERR_WRONG_COMMAND);
	}

	if (!(self = ha_set_current_thread(thd, &err)))
		return xt_ha_pbxt_to_mysql_error(err);

	try_(a) {
		XTDictionaryRec dic;

		memset(&dic, 0, sizeof(dic));

		dic = pb_share->sh_table->tab_dic;
		xt_strcpy(PATH_MAX, path, pb_share->sh_table->tab_name->ps_path);

		if ((tab_def = dic.dic_table))
			tab_def->reference();

		if (!(thd_test_options(thd,OPTION_NO_FOREIGN_KEY_CHECKS)))
			tab_def->deleteAllRows(self);

		/* We should have a table lock! */
		//ASSERT(pb_lock_table);
		if (!pb_table_locked) {
			ha_aquire_exclusive_use(self, pb_share, this);
			pushr_(ha_release_exclusive_use, pb_share);
		}
		ha_close_open_tables(self, pb_share, NULL);

		/* This is required in the case of delete_all_rows, because we must
		 * ensure that the handlers no longer reference the old
		 * table, so that it will not be used again. The table
		 * must be re-openned, because the ID has changed!
		 *
		 * 0.9.86+ Must check if this is still necessary.
		 *
		 * the ha_close_share(self, pb_share) call was moved from above
		 * (before tab_def = dic.dic_table), because of a crash.
		 * Test case:
		 *
		 * set storage_engine = pbxt;
		 * create table t1 (s1 int primary key);
		 * insert into t1 values (1);
		 * create table t2 (s1 int, foreign key (s1) references t1 (s1));
		 * insert into t2 values (1); 
		 * truncate table t1; -- this should fail because of FK constraint
		 * alter table t1 engine = myisam; -- this caused crash
		 *
		 */
		ha_close_share(self, pb_share);

		/* MySQL documentation requires us to reset auto increment value to 1
		 * on truncate even if the table was created with a different value. 
		 * This is also consistent with other engines.
		 */
		dic.dic_min_auto_inc = 1;

		xt_create_table(self, (XTPathStrPtr) path, &dic);
		if (!pb_table_locked)
			freer_(); // ha_release_exclusive_use(pb_share)
	}
	catch_(a) {
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
	}
	cont_(a);

	if (tab_def)
		tab_def->release(self);

	XT_RETURN(err);
}

/*
 * TODO: Implement!
 * Assuming a key (a,b,c)
 * 
 * rec_per_key[0] = SELECT COUNT(*)/COUNT(DISTINCT a) FROM t;
 * rec_per_key[1] = SELECT COUNT(*)/COUNT(DISTINCT a,b) FROM t;
 * rec_per_key[2] = SELECT COUNT(*)/COUNT(DISTINCT a,b,c) FROM t;
 *
 * After this is implemented, the selectivity can serve as
 * a quick estimate of records_in_range().
 *
 * After you have done this, you need to redo the index_merge*
 * tests. Restore the standard result to check if we
 * now agree with the MyISAM strategy.
 * 
 */
int ha_pbxt::analyze(THD *thd, HA_CHECK_OPT *XT_UNUSED(check_opt))
{
	int				err = 0;
	XTDatabaseHPtr	db;
	xtXactID		my_xn_id;
	xtXactID		clean_xn_id = 0;
	uint			cnt = 10;

	XT_TRACE_METHOD();

	if (!pb_open_tab) {
		if ((err = reopen()))
			XT_RETURN(err);
	}

	/* Wait until the sweeper is no longer busy!
	 * If you want an accurate count(*) value, then call
	 * ANALYZE TABLE first. This function waits until the
	 * sweeper has completed.
	 */
	db = pb_open_tab->ot_table->tab_db;
	
	/*
	 * Wait until everything is cleaned up before this transaction.
	 * But this will only work if the we quit out transaction!
	 *
	 * GOTCHA: When a PBXT table is partitioned, then analyze() is
	 * called for each component. The first calls xt_xn_commit().
	 * All following calls have no transaction!:
	 *
	 * CREATE TABLE t1 (a int)
	 * PARTITION BY LIST (a)
	 * (PARTITION x1 VALUES IN (10), PARTITION x2 VALUES IN (20));
	 * 
	 * analyze table t1;
	 * 
	 */
	if (pb_open_tab->ot_thread && pb_open_tab->ot_thread->st_xact_data) {
		my_xn_id = pb_open_tab->ot_thread->st_xact_data->xd_start_xn_id;
		XT_PRINT0(xt_get_self(), "xt_xn_commit\n");
		xt_xn_commit(pb_open_tab->ot_thread);
	}
	else
		my_xn_id = db->db_xn_to_clean_id;

	while ((!db->db_sw_idle || xt_xn_is_before(db->db_xn_to_clean_id, my_xn_id)) && !thd_killed(thd)) {
		xt_busy_wait();

		/*
		 * It is possible that the sweeper gets stuck because
		 * it has no dictionary information!
		 * As in the example below.
		 *
		 * create table t4 (
		 *   pk_col int auto_increment primary key, a1 char(64), a2 char(64), b char(16), c char(16) not null, d char(16), dummy char(64) default ' '
		 * ) engine=pbxt;
		 *
		 * insert into t4 (a1, a2, b, c, d, dummy) select * from t1;
		 * 
		 * create index idx12672_0 on t4 (a1);
		 * create index idx12672_1 on t4 (a1,a2,b,c);
		 * create index idx12672_2 on t4 (a1,a2,b);
		 * analyze table t1;
		 */
		if (db->db_sw_idle) {
			/* This will make sure we don't wait forever: */
			if (clean_xn_id != db->db_xn_to_clean_id) {
				clean_xn_id = db->db_xn_to_clean_id;
				cnt = 10;
			}
			else {
				cnt--;
				if (!cnt)
					break;
			}
			xt_wakeup_sweeper(db);
		}
	}

	XT_RETURN(err);
}

int ha_pbxt::repair(THD *XT_UNUSED(thd), HA_CHECK_OPT *XT_UNUSED(check_opt))
{
	return(HA_ADMIN_TRY_ALTER);
}

/*
 * This is mapped to "ALTER TABLE tablename TYPE=PBXT", which rebuilds
 * the table in MySQL.
 */
int ha_pbxt::optimize(THD *XT_UNUSED(thd), HA_CHECK_OPT *XT_UNUSED(check_opt))
{
	return(HA_ADMIN_TRY_ALTER);
}

#ifdef DEBUG
extern int pbxt_mysql_trace_on;
#endif

int ha_pbxt::check(THD* thd, HA_CHECK_OPT* XT_UNUSED(check_opt))
{
	int				err = 0;
	XTThreadPtr		self;

	if (!(self = ha_set_current_thread(thd, &err)))
		return xt_ha_pbxt_to_mysql_error(err);
	if (self->st_lock_count)
		ASSERT(self->st_xact_data);

	if (!pb_table_locked) {
		ha_aquire_exclusive_use(self, pb_share, this);
		pushr_(ha_release_exclusive_use, pb_share);
	}

#ifdef CHECK_TABLE_LOADS
	xt_tab_load_table(self, pb_open_tab);
#endif
	xt_check_table(self, pb_open_tab);

	if (!pb_table_locked)
		freer_(); // ha_release_exclusive_use(pb_share)

	//pbxt_mysql_trace_on = TRUE;
	return 0;
}

/*
 * This function is called:
 * For each table in LOCK TABLES,
 * OR
 * For each table in a statement.
 *
 * It is called with F_UNLCK:
 * in UNLOCK TABLES
 * OR
 * at the end of a statement.
 *
 */
xtPublic int ha_pbxt::external_lock(THD *thd, int lock_type)
{
	int				err = 0;
	XTThreadPtr		self;
	
	if (!(self = ha_set_current_thread(thd, &err)))
		return xt_ha_pbxt_to_mysql_error(err);

	/* F_UNLCK is set when this function is called at end
	 * of statement or UNLOCK TABLES
	 */
	if (lock_type == F_UNLCK) {
		/* This is not TRUE if external_lock() FAILED!
		 * Can we rely on external_unlock being called when
		 * external_lock() fails? Currently yes, but it does
		 * not make sense!
		ASSERT_NS(pb_ex_in_use);
		*/

		XT_PRINT1(self, "EXTERNAL_LOCK (%s) lock_type=UNLOCK\n", pb_share->sh_table_path->ps_path);

		/* Make any temporary locks on this table permanent.
		 *
		 * This is required here because of the following example:
		 * create table t1 (a int NOT NULL, b int, primary key (a));
		 * create table t2 (a int NOT NULL, b int, primary key (a));
		 * insert into t1 values (0, 10),(1, 11),(2, 12);
		 * insert into t2 values (1, 21),(2, 22),(3, 23);
		 * update t1 set b= (select b from t2 where t1.a = t2.a);
		 * update t1 set b= (select b from t2 where t1.a = t2.a);
		 * select * from t1;
		 * drop table t1, t2;
		 *
		 */

		/* GOTCHA! It's weird, but, if this function returns an error
		 * on lock, then UNLOCK is called?!
		 * This should not be done, because if lock fails, it should be
		 * assumed that no UNLOCK is required.
		 * Basically, I have to assume that some code will presume this,
		 * although the function lock_external() calls unlock, even
		 * when lock fails.
		 * The result is, that my lock count can go wrong. So I could
		 * change the lock method, and increment the lock count, even
		 * if it fails. However, the consequences are more serious,
		 * if some code decides not to call UNLOCK after lock fails.
		 * The result is that I would have a permanent too high lock,
		 * count and nothing will work.
		 * So instead, I handle the fact that I might too many unlocks
		 * here.
		 */
		if (self->st_lock_count > 0)
			self->st_lock_count--;
		if (!self->st_lock_count) {
			/* This section handles "auto-commit"... */

#ifdef XT_IMPLEMENT_NO_ACTION
			/* {NO-ACTION-BUG}
			 * This is required here because it marks the end of a statement.
			 * If we are in a non-auto-commit mode, then we cannot
			 * wait for st_is_update to be set by the begining of a new transaction.
			 */
			if (self->st_restrict_list.bl_count) {
				if (!xt_tab_restrict_rows(&self->st_restrict_list, self))
					err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
			}
#endif

			if (self->st_xact_data) {
				if (self->st_auto_commit) {
					/*
					 * Normally I could assume that if the transaction
					 * has not been aborted by now, then it should be committed.
					 *
					 * Unfortunately, this is not the case!
					 *
					 * create table t1 (id int primary key) engine = pbxt;
					 * create table t2 (id int) engine = pbxt;
					 * 
					 * insert into t1 values ( 1 ) ;
					 * insert into t1 values ( 2 ) ;
					 * insert into t2 values ( 1 ) ;
					 * insert into t2 values ( 2 ) ;
					 * 
					 * --This statement is returns an error calls ha_autocommit_or_rollback():
					 * update t1 set t1.id=1 where t1.id=2;
					 * 
					 * --This statement is returns no error and calls ha_autocommit_or_rollback():
					 * update t1,t2 set t1.id=3, t2.id=3 where t1.id=2 and t2.id = t1.id;
					 * 
					 * --But this statement returns an error and does not call ha_autocommit_or_rollback():
					 * update t1,t2 set t1.id=1, t2.id=1 where t1.id=3 and t2.id = t1.id;
					 * 
					 * The result is, I cannot rely on ha_autocommit_or_rollback() being called :(
					 * So I have to abort myself here...
					 */
					if (pb_open_tab)
						pb_open_tab->ot_table->tab_locks.xt_make_lock_permanent(pb_open_tab, &self->st_lock_list);

					if (self->st_abort_trans) {
						XT_PRINT0(self, "xt_xn_rollback in unlock\n");
						if (!xt_xn_rollback(self))
							err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
					}
					else {
						XT_PRINT0(self, "xt_xn_commit in unlock\n");
						if (!xt_xn_commit(self))
							err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
					}
				}
			}

			/* If the previous statement was "for update", then set the visibilty
			 * so that non- for update SELECTs will see what the for update select
			 * (or update statement) just saw.
			 */
			if (pb_open_tab) {
				if (pb_open_tab->ot_for_update) {
					self->st_visible_time = self->st_database->db_xn_end_time;
					pb_open_tab->ot_for_update = 0;
				}

				if (pb_share->sh_recalc_selectivity) {
#ifdef XT_ROW_COUNT_CORRECTED
					/* {CORRECTED-ROW-COUNT} */
					if ((pb_share->sh_table->tab_row_eof_id - 1 - pb_share->sh_table->tab_row_fnum) >= 200)
#else
					/* {FREE-ROWS-BAD} */
					if ((pb_share->sh_table->tab_row_eof_id - 1 /* - pb_share->sh_table->tab_row_fnum */) >= 200)
#endif
					{
						/* [**] */
						pb_share->sh_recalc_selectivity = FALSE;
						xt_ind_set_index_selectivity(pb_open_tab, self);
#ifdef XT_ROW_COUNT_CORRECTED
						/* {CORRECTED-ROW-COUNT} */
						pb_share->sh_recalc_selectivity = (pb_share->sh_table->tab_row_eof_id - 1 - pb_share->sh_table->tab_row_fnum) < 150;
#else
						/* {FREE-ROWS-BAD} */
						pb_share->sh_recalc_selectivity = (pb_share->sh_table->tab_row_eof_id - 1 /* - pb_share->sh_table->tab_row_fnum */) < 150;
#endif
					}
				}
			}

			if (self->st_stat_modify)
				self->st_statistics.st_stat_write++;
			else
				self->st_statistics.st_stat_read++;
			self->st_stat_modify = FALSE;
		}

		if (pb_table_locked) {
			pb_table_locked--;
			if (!pb_table_locked)
				ha_release_exclusive_use(self, pb_share);
		}

		/* No longer in use: */
		pb_ex_in_use = 0;
		/* Someone may be waiting for me to complete: */
		if (pb_share->sh_table_lock)
			xt_broadcast_cond_ns((xt_cond_type *) pb_share->sh_ex_cond);
	}
	else {
		XT_PRINT2(self, "ha_pbxt::EXTERNAL_LOCK (%s) lock_type=%d\n", pb_share->sh_table_path->ps_path, lock_type);
		
		if (pb_lock_table) {
			pb_ex_in_use = 1;
			try_(a) {
				if (!pb_table_locked)
					ha_aquire_exclusive_use(self, pb_share, this);
				pb_table_locked++;

				ha_close_open_tables(self, pb_share, this);

				if (!pb_share->sh_table) {
					xt_ha_open_database_of_table(self, pb_share->sh_table_path);

					ha_open_share(self, pb_share);
				}
			}
			catch_(a) {
				err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
				pb_ex_in_use = 0;
				goto complete;
			}
			cont_(a);

			/* Occurs if you do:
			 * truncate table t1;
			 * truncate table t1;
			 */
			if (!pb_open_tab) {
				if ((err = reopen())) {
					pb_ex_in_use = 0;
					goto complete;
				}
			}
		}
		else {
			pb_ex_in_use = 1;
			if (pb_share->sh_table_lock && !pb_table_locked) {
				/* If some thread has an exclusive lock, then
				 * we wait for the lock to be removed:
				 */
				if (!ha_wait_for_shared_use(this, pb_share)) {
					err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
					goto complete;
				}
			}

			if (!pb_open_tab) {
				if ((err = reopen())) {
					pb_ex_in_use = 0;
					goto complete;
				}
			}

			/* Set the current thread for this open table: */
			pb_open_tab->ot_thread = self;

			/* If this is a set, then it is in UPDATE/DELETE TABLE ...
			 * or SELECT ... FOR UPDATE
			 */	
			pb_open_tab->ot_is_modify = FALSE;
			if ((pb_open_tab->ot_for_update = (lock_type == F_WRLCK))) {
				switch ((int) thd_sql_command(thd)) {
					case SQLCOM_DELETE:
#ifndef DRIZZLED
					case SQLCOM_DELETE_MULTI:
#endif
						/* turn DELETE IGNORE into normal DELETE. The IGNORE option causes problems because 
						 * when a record is deleted we add an xlog record which we cannot "rollback" later
						 * when we find that an FK-constraint has failed. 
						 */
						thd->lex->ignore = false;
					case SQLCOM_UPDATE:
#ifndef DRIZZLED
					case SQLCOM_UPDATE_MULTI:
#endif
					case SQLCOM_REPLACE:
					case SQLCOM_REPLACE_SELECT:
					case SQLCOM_INSERT:
					case SQLCOM_INSERT_SELECT:
						pb_open_tab->ot_is_modify = TRUE;
						self->st_stat_modify = TRUE;
						break;
					case SQLCOM_CREATE_TABLE:
					case SQLCOM_CREATE_INDEX:
					case SQLCOM_ALTER_TABLE:
					case SQLCOM_TRUNCATE:
					case SQLCOM_DROP_TABLE:
					case SQLCOM_DROP_INDEX:
					case SQLCOM_LOAD:
#ifndef DRIZZLED
					case SQLCOM_REPAIR:
#endif
					case SQLCOM_OPTIMIZE:
						self->st_stat_modify = TRUE;
						break;
				}
			}

			if (pb_open_tab->ot_is_modify && pb_open_tab->ot_table->tab_dic.dic_disable_index) {
				xt_tab_set_index_error(pb_open_tab->ot_table);
				err = ha_log_pbxt_thread_error_for_mysql(pb_ignore_dup_key);
				goto complete;
			}
		}

		/* Record the associated MySQL thread: */
		pb_mysql_thd = thd;

		if (self->st_database != pb_share->sh_table->tab_db) {				
			try_(b) {
				/* PBXT does not permit multiple databases us one statement,
				 * or in a single transaction!
				 *
				 * Example query:
				 *
				 * update mysqltest_1.t1, mysqltest_2.t2 set a=10,d=10;
				 */
				if (self->st_lock_count > 0)
					xt_throw_xterr(XT_CONTEXT, XT_ERR_MULTIPLE_DATABASES);

				xt_ha_open_database_of_table(self, pb_share->sh_table_path);
			}
			catch_(b) {
				err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
				pb_ex_in_use = 0;
				goto complete;
			}
			cont_(b);
		}

		/* See {IS-UPDATE-STAT} nad {UPDATE-STACK} */
		self->st_is_update = NULL;

		/* Auto begin a transaction (if one is not already running): */
		if (!self->st_xact_data) {
			/* Transaction mode numbers must be identical! */
			(void) ASSERT_NS(ISO_READ_UNCOMMITTED == XT_XACT_UNCOMMITTED_READ);
			(void) ASSERT_NS(ISO_SERIALIZABLE == XT_XACT_SERIALIZABLE);

			thd_init_xact(thd, self, true);

			if (!xt_xn_begin(self)) {
				err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
				pb_ex_in_use = 0;
				goto complete;
			}

			/*
			 * {START-TRANS} GOTCHA: trans_register_ha() is not mentioned in the documentation.
			 * It must be called to inform MySQL that we have a transaction (see start_stmt).
			 *
			 * Here are some tests that confirm whether things are done correctly:
			 *
			 * drop table if exists t1, t2;
			 * create table t1 (c1 int);
			 * insert t1 values (1);
			 * select * from t1;
			 * rename table t1 to t2;
			 *
			 * rename will generate an error if MySQL thinks a transaction is
			 * still running.
			 *
			 * create table t1 (a text character set utf8, b text character set latin1);
			 * insert t1 values (0x4F736E616272C3BC636B, 0x4BF66C6E);
			 * select * from t1;
			 * --exec $MYSQL_DUMP --tab=$MYSQLTEST_VARDIR/tmp/ test
			 * --exec $MYSQL test < $MYSQLTEST_VARDIR/tmp/t1.sql
			 * --exec $MYSQL_IMPORT test $MYSQLTEST_VARDIR/tmp/t1.txt
			 * select * from t1;
			 *
			 * This test forces a begin transaction in start_stmt()
			 *
			 * drop tables if exists t1;
			 * create table t1 (c1 int);
			 * lock tables t1 write;
			 * insert t1 values (1);
			 * insert t1 values (2);
			 * unlock tables;
			 *
			 * The second select will return an empty result of the
			 * MySQL is not informed that a transaction is running (auto-commit 
			 * in external_lock comes too late)!
			 *
			 */
			if (!self->st_auto_commit) {
				trans_register_ha(thd, TRUE, pbxt_hton);
				XT_PRINT0(self, "CONN START XACT - ha_pbxt::external_lock --> trans_register_ha\n");
			}
		}

		/* Start a statment transaction: */
		/* {START-STAT-HACK} The problem that ha_commit_trans() is not
		 * called by MySQL seems to be fixed (tests confirm this).
		 * Here is the previous comment when this code was execute 
		 * here {START-STAT-HACK}
		 *
		 * GOTCHA: I have a huge problem with the transaction statement.
		 * It is not ALWAYS committed (I mean ha_commit_trans() is
		 * not always called - for example in SELECT).
		 *
		 * If I call trans_register_ha() but ha_commit_trans() is not called
		 * then MySQL thinks a transaction is still running (while
		 * I have committed the auto-transaction in ha_pbxt::external_lock()).
		 *
		 * This causes all kinds of problems, like transactions
		 * are killed when they should not be.
		 *
		 * To prevent this, I only inform MySQL that a transaction
		 * has beens started when an update is performed. I have determined that
		 * ha_commit_trans() is only guarenteed to be called if an update is done.
		 * --------
		 *
		 * So, this is the correct place to start a statement transaction.
		 *
		 * Note: if trans_register_ha() is not called before ha_write_row(), then 
		 * PBXT is not registered correctly as a modification transaction.
		 * (mark_trx_read_write call in ha_write_row).
		 * This leads to 2-phase commit not being called as it should when
		 * binary logging is enabled.
		 */
		if (!pb_open_tab->ot_thread->st_stat_trans) {
			trans_register_ha(pb_mysql_thd, FALSE, pbxt_hton);
			XT_PRINT0(pb_open_tab->ot_thread, "STAT START - ha_pbxt::external_lock --> trans_register_ha\n");
			pb_open_tab->ot_thread->st_stat_trans = TRUE;
		}

		if (lock_type == F_WRLCK || self->st_xact_mode < XT_XACT_REPEATABLE_READ)
			self->st_visible_time = self->st_database->db_xn_end_time;

#ifdef TRACE_STATEMENTS
		if (self->st_lock_count == 0)
			STAT_TRACE(self, *thd_query(thd));
#endif
		self->st_lock_count++;
	}

	complete:
	return err;
}

/*
 * This function is called for each table in a statement
 * after LOCK TABLES has been used.
 *
 * Currently I only use this function to set the
 * current thread of the table handle. 
 *
 * GOTCHA: The prototype of start_stmt() has changed
 * from version 4.1 to 5.1!
 */
int ha_pbxt::start_stmt(THD *thd, thr_lock_type lock_type)
{
	int				err = 0;
	XTThreadPtr		self;

	ASSERT_NS(pb_ex_in_use);

	if (!(self = ha_set_current_thread(thd, &err)))
		return xt_ha_pbxt_to_mysql_error(err);

	XT_PRINT2(self, "ha_pbxt::start_stmt (%s) lock_type=%d\n", pb_share->sh_table_path->ps_path, (int) lock_type);

	if (!pb_open_tab) {
		if ((err = reopen()))
			goto complete;
	}

	ASSERT_NS(pb_open_tab->ot_thread == self);
	ASSERT_NS(thd == pb_mysql_thd);
	ASSERT_NS(self->st_database == pb_open_tab->ot_table->tab_db);

	if (self->st_stat_ended) {
		self->st_stat_ended = FALSE;
		self->st_stat_trans = FALSE;

#ifdef XT_IMPLEMENT_NO_ACTION
		if (self->st_restrict_list.bl_count) {
			if (!xt_tab_restrict_rows(&self->st_restrict_list, self)) {
				err = xt_ha_pbxt_thread_error_for_mysql(pb_mysql_thd, self, pb_ignore_dup_key);
			}
		}
#endif

		/* This section handles "auto-commit"... */
		if (self->st_xact_data && self->st_auto_commit && self->st_table_trans) {
			if (self->st_abort_trans) {
				XT_PRINT0(self, "xt_xn_rollback in start_stmt\n");
				if (!xt_xn_rollback(self))
					err = xt_ha_pbxt_thread_error_for_mysql(pb_mysql_thd, self, pb_ignore_dup_key);
			}
			else {
				XT_PRINT0(self, "xt_xn_commit in start_stmt\n");
				if (!xt_xn_commit(self))
					err = xt_ha_pbxt_thread_error_for_mysql(pb_mysql_thd, self, pb_ignore_dup_key);
			}
		}

		if (self->st_stat_modify)
			self->st_statistics.st_stat_write++;
		else
			self->st_statistics.st_stat_read++;
		self->st_stat_modify = FALSE;

		/* If the previous statement was "for update", then set the visibilty
		 * so that non- for update SELECTs will see what the for update select
		 * (or update statement) just saw.
		 */
		if (pb_open_tab->ot_for_update)
			self->st_visible_time = self->st_database->db_xn_end_time;
	}

	pb_open_tab->ot_for_update =
		(lock_type != TL_READ && 
		 lock_type != TL_READ_WITH_SHARED_LOCKS &&
#ifndef DRIZZLED
		 lock_type != TL_READ_HIGH_PRIORITY && 
#endif
		 lock_type != TL_READ_NO_INSERT);
	pb_open_tab->ot_is_modify = FALSE;
	if (pb_open_tab->ot_for_update) {
		switch ((int) thd_sql_command(thd)) {
			case SQLCOM_UPDATE:
			case SQLCOM_DELETE:
#ifndef DRIZZLED
			case SQLCOM_UPDATE_MULTI:
			case SQLCOM_DELETE_MULTI:
#endif
			case SQLCOM_REPLACE:
			case SQLCOM_REPLACE_SELECT:
			case SQLCOM_INSERT:
			case SQLCOM_INSERT_SELECT:
				pb_open_tab->ot_is_modify = TRUE;
				self->st_stat_modify = TRUE;
				break;
			case SQLCOM_CREATE_TABLE:
			case SQLCOM_CREATE_INDEX:
			case SQLCOM_ALTER_TABLE:
			case SQLCOM_TRUNCATE:
			case SQLCOM_DROP_TABLE:
			case SQLCOM_DROP_INDEX:
			case SQLCOM_LOAD:
#ifndef DRIZZLED
			case SQLCOM_REPAIR:
#endif
			case SQLCOM_OPTIMIZE:
				self->st_stat_modify = TRUE;
				break;
		}
	}

	/* {IS-UPDATE-STAT} This is required at this level!
	 * No matter how often it is called, it is still the start of a
	 * statement. We need to make sure statements that are NOT mistaken
	 * for different type of statement.
	 *
	 * Here is an example:
	 * select * from t1 where data = getcount("bar")
	 *
	 * If the procedure getcount() addresses another table.
	 * then open and close of the statements in getcount()
	 * are nested within an open close of the select t1
	 * statement.
	 */
	/* {UPDATE-STACK}
	 * Add to this I add the following:
	 * A trigger in the middle of an update also causes nested
	 * statements. If I reset st_is_update, then then
	 * when the trigger returns the system thinks we
	 * are in a different update statement, and may
	 * update the same row again.
	 */
	if (self->st_is_update == pb_open_tab) {
		/* Pop the update stack: */
		XTOpenTablePtr curr = pb_open_tab->ot_thread->st_is_update;

		pb_open_tab->ot_thread->st_is_update = curr->ot_prev_update;
		curr->ot_prev_update = NULL;
	}

	/* See comment {START-TRANS} */
	if (!self->st_xact_data) {

		thd_init_xact(thd, self, false);

		if (!xt_xn_begin(self)) {
			err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
			goto complete;
		}
		if (!self->st_auto_commit) {
			trans_register_ha(thd, TRUE, pbxt_hton);
			XT_PRINT0(self, "START CONN XACT - ha_pbxt::start_stmt --> trans_register_ha\n");
		}
	}

	/* Start a statment (see {START-STAT-HACK}): */
	if (!pb_open_tab->ot_thread->st_stat_trans) {
		trans_register_ha(pb_mysql_thd, FALSE, pbxt_hton);
		XT_PRINT0(pb_open_tab->ot_thread, "START STAT - ha_pbxt::start_stmt --> trans_register_ha\n");
		pb_open_tab->ot_thread->st_stat_trans = TRUE;
	}

	if (pb_open_tab->ot_for_update || self->st_xact_mode < XT_XACT_REPEATABLE_READ)
		self->st_visible_time = self->st_database->db_xn_end_time;

	pb_in_stat = TRUE;

	self->st_stat_count++;

	complete:
	return err;
}

/*
 * The idea with handler::store_lock() is the following:
 *
 * The statement decided which locks we should need for the table
 * for updates/deletes/inserts we get WRITE locks, for SELECT... we get
 * read locks.
 *
 * Before adding the lock into the table lock handler (see thr_lock.c)
 * mysqld calls store lock with the requested locks. Store lock can now
 * modify a write lock to a read lock (or some other lock), ignore the
 * lock (if we don't want to use MySQL table locks at all) or add locks
 * for many tables (like we do when we are using a MERGE handler).
 *
 * When releasing locks, store_lock() are also called. In this case one
 * usually doesn't have to do anything.
 *
 * In some exceptional cases MySQL may send a request for a TL_IGNORE;
 * This means that we are requesting the same lock as last time and this
 * should also be ignored. (This may happen when someone does a flush
 * table when we have opened a part of the tables, in which case mysqld
 * closes and reopens the tables and tries to get the same locks at last
 * time). In the future we will probably try to remove this.
 *
 * Called from lock.cc by get_lock_data().
 */
THR_LOCK_DATA **ha_pbxt::store_lock(THD *thd, THR_LOCK_DATA **to, enum thr_lock_type lock_type)
{
	/*
	 * TL_READ means concurrent INSERTs are allowed. This is a problem as in this mode
	 * PBXT is not compatible with MyISAM which allows INSERTs but isolates them from
	 * current "transaction" (started by LOCK TABLES, ended by UNLOCK TABLES). PBXT 
	 * used to allow INSERTs and made them visible to the locker (on commit). 
	 * While MySQL manual doesn't state anything regarding row visibility limitations 
	 * we choose to convert local locks into normal read locks for better compatibility 
	 * with MyISAM.
	 */
	if (lock_type == TL_READ)
		lock_type = TL_READ_NO_INSERT;

	if (lock_type != TL_IGNORE && pb_lock.type == TL_UNLOCK) {
		/* Set to TRUE for operations that require a table lock: */
		switch (thd_sql_command(thd)) {
			case SQLCOM_TRUNCATE:
				/* GOTCHA:
				 * The problem is, if I do not do this, then
				 * TRUNCATE TABLE deadlocks with a normal update of the table!
				 * The reason is:
				 *
				 * external_lock() is called before MySQL actually locks the
				 * table. In external_lock(), the table is shared locked,
				 * by indicating that the handler is in use.
				 *
				 * Then later, in delete_all_rows(), a exclusive lock must be
				 * obtained. If an UPDATE or INSERT has also gained a shared
				 * lock in the meantime, then TRUNCATE TABLE hangs.
				 *
				 * By setting pb_lock_table we indicate that an exclusive lock
				 * should be gained in external_lock().
				 *
				 * This is the locking behaviour:
				 *
				 * TRUNCATE TABLE:
				 * XT SHARE LOCK (mysql_lock_tables calls external_lock)
				 * MySQL WRITE LOCK (mysql_lock_tables)
				 * ...
				 * XT EXCLUSIVE LOCK (delete_all_rows)
				 *
				 * INSERT:
				 * XT SHARED LOCK (mysql_lock_tables calls external_lock)
				 * MySQL WRITE_ALLOW_WRITE LOCK (mysql_lock_tables)
				 *
				 * If the locking for INSERT is done in the ... phase
				 * above, then we have a deadlock because 
				 * WRITE_ALLOW_WRITE conflicts with WRITE.
				 *
				 * Making TRUNCATE TABLE take a WRITE_ALLOW_WRITE LOCK, will
				 * not solve the problem because then 2 TRUNCATE TABLES
				 * can deadlock due to lock escalation.
				 *
				 * What may work is if MySQL were to lock BEFORE calling
				 * external_lock()!
				 *
				 * However, using this method, TRUNCATE TABLE does deadlock
				 * with other operations such as ALTER TABLE!
				 *
				 * This is handled with a lock timeout. Assuming 
				 * TRUNCATE TABLE will be mixed with DML this is the
				 * best solution!
				 */
				pb_lock_table = TRUE;
				break;
			default:
				pb_lock_table = FALSE;
				break;
		}

#ifdef PBXT_HANDLER_TRACE
		pb_lock.type = lock_type;
#endif
		/* GOTCHA: Before it was OK to weaken the lock after just checking
		 * that !thd->in_lock_tables. However, when starting a procedure, MySQL
		 * simulates a LOCK TABLES statement.
		 *
		 * So we need to be more specific here, and check what the actual statement
		 * type. Before doing this I got a deadlock (undetected) on the following test.
		 * However, now we get a failed assertion in ha_rollback_trans():
		 * TODO: Check this with InnoDB!
		 *
		 * DBUG_ASSERT(0);
		 * my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
		 *
		 * drop table if exists t3;
		 * create table t3 (a smallint primary key) engine=pbxt;
		 * insert into t3 (a) values (40);
		 * insert into t3 (a) values (50);
		 * 
		 * delimiter |
		 * 
		 * drop function if exists t3_update|
		 * 
		 * create function t3_update() returns int
		 * begin
		 *   insert into t3 values (10);
		 *   return 100;
		 * end|
		 * 
		 * delimiter ;
		 * 
		 * CONN 1:
		 * 
		 * begin;
		 * update t3 set a = 5 where a = 50;
		 * 
		 * CONN 2:
		 * 
		 * begin;
		 * update t3 set a = 4 where a = 40;
		 * 
		 * CONN 1:
		 * 
		 * update t3 set a = 4 where a = 40; // Hangs waiting CONN 2.
		 * 
		 * CONN 2:
		 * 
		 * select t3_update(); // Hangs waiting for table lock.
		 * 
		 */
		if ((lock_type >= TL_WRITE_CONCURRENT_INSERT && lock_type <= TL_WRITE) && 
#ifndef DRIZZLED
			!(thd_in_lock_tables(thd) && thd_sql_command(thd) == SQLCOM_LOCK_TABLES) &&
#endif
			!thd_tablespace_op(thd) &&
			thd_sql_command(thd) != SQLCOM_TRUNCATE &&
			thd_sql_command(thd) != SQLCOM_OPTIMIZE &&
			thd_sql_command(thd) != SQLCOM_CREATE_TABLE) {
			lock_type = TL_WRITE_ALLOW_WRITE;
		}

		/* In queries of type INSERT INTO t1 SELECT ... FROM t2 ...
		 * MySQL would use the lock TL_READ_NO_INSERT on t2, and that
		 * would conflict with TL_WRITE_ALLOW_WRITE, blocking all inserts
		 * to t2. Convert the lock to a normal read lock to allow
		 * concurrent inserts to t2.
		 * 
		 * (This one from InnoDB)

                 * Stewart: removed SQLCOM_CALL, not sure of implications.
		 */
		if (lock_type == TL_READ_NO_INSERT
#ifndef DRIZZLED
			&& (!thd_in_lock_tables(thd)
			 || thd_sql_command(thd) == SQLCOM_CALL
			)
#endif
			)
		{
			lock_type = TL_READ;
		}

		XT_PRINT3(xt_get_self(), "store_lock (%s) %d->%d\n", pb_share->sh_table_path->ps_path, pb_lock.type, lock_type);
		pb_lock.type = lock_type;
	}
#ifdef PBXT_HANDLER_TRACE
	else {
		XT_PRINT3(xt_get_self(), "store_lock (%s) %d->%d (ignore/unlock)\n", pb_share->sh_table_path->ps_path, lock_type, lock_type);
	}
#endif
	*to++= &pb_lock;
	return to;
}

/*
 * Used to delete a table. By the time delete_table() has been called all
 * opened references to this table will have been closed (and your globally
 * shared references released. The variable name will just be the name of
 * the table. You will need to remove any files you have created at this point.
 *
 * Called from handler.cc by delete_table and ha_create_table(). Only used
 * during create if the table_flag HA_DROP_BEFORE_CREATE was specified for
 * the storage engine.
*/
#ifdef DRIZZLED
int PBXTStorageEngine::doDropTable(Session &, std::string table_path_str)
#else
int ha_pbxt::delete_table(const char *table_path)
#endif
{
	THD				*thd = current_thd;
	int				err = 0;
	XTThreadPtr		self = NULL;
	XTSharePtr		share;

#ifdef DRIZZLED
	const char *table_path = table_path_str.c_str();
#endif

	STAT_TRACE(self, *thd_query(thd));
	XT_PRINT1(self, "delete_table (%s)\n", table_path);

	if (XTSystemTableShare::isSystemTable(table_path))
		return delete_system_table(table_path);

	if (!(self = ha_set_current_thread(thd, &err)))
		return xt_ha_pbxt_to_mysql_error(err);

	self->st_ignore_fkeys = (thd_test_options(thd, OPTION_NO_FOREIGN_KEY_CHECKS)) != 0;

	try_(a) {
		xt_ha_open_database_of_table(self, (XTPathStrPtr) table_path);

		ASSERT(xt_get_self() == self);
		try_(b) {
			/* NOTE: MySQL does not drop a table by first locking it!
			 * We also cannot use pb_share because the handler used
			 * to delete a table is not openned correctly.
			 */
			share = ha_get_share(self, table_path, false);
			pushr_(ha_unget_share, share);
			ha_aquire_exclusive_use(self, share, NULL);
			pushr_(ha_release_exclusive_use, share);
			ha_close_open_tables(self, share, NULL);

			xt_drop_table(self, (XTPathStrPtr) table_path, thd_sql_command(thd) == SQLCOM_DROP_DB);

			freer_(); // ha_release_exclusive_use(share)
			freer_(); // ha_unget_share(share)
		}
		catch_(b) {
			/* In MySQL if the table does not exist, just log the error and continue. This is
 			 * needed to delete table in the case when CREATE TABLE fails and no PBXT disk
 			 * structures were created. 
 			 * Drizzle unlike MySQL iterates over all handlers and tries to delete table. It
 			 * stops after when a handler returns TRUE, so in Drizzle we need to report error.  
			 */
#ifndef DRIZZLED
			if (self->t_exception.e_xt_err == XT_ERR_TABLE_NOT_FOUND)
				xt_log_and_clear_exception(self);
			else
#endif
				throw_();
		}
		cont_(b);

		/*
		 * If there are no more PBXT tables in the database, we
		 * "drop the database", which deletes all PBXT resources
		 * in the database.
		 */
		/* We now only drop the pbxt system data,
		 * when the PBXT database is dropped.
		 */
#ifndef XT_USE_GLOBAL_DB
		if (!xt_table_exists(self->st_database)) {
			xt_ha_all_threads_close_database(self, self->st_database);
			xt_drop_database(self, self->st_database);
			xt_unuse_database(self, self);
			xt_ha_close_global_database(self);
		}
#endif
	}
	catch_(a) {
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
#ifdef DRIZZLED
		if (err == HA_ERR_NO_SUCH_TABLE)
			err = ENOENT;
#endif
	}
	cont_(a);
	
#ifdef PBMS_ENABLED
	/* Call pbms_delete_table_with_blobs() last because it cannot be undone. */
	if (!err) {
		PBMSResultRec result;

		if (pbms_delete_table_with_blobs(table_path, &result)) {
			xt_logf(XT_NT_WARNING, "pbms_delete_table_with_blobs() Error: %s", result.mr_message);
		}
		
		pbms_completed(NULL, true);
	}
#endif

	return err;
}

#ifdef DRIZZLED
int PBXTStorageEngine::delete_system_table(const char *table_path)
#else
int ha_pbxt::delete_system_table(const char *table_path)
#endif
{
	THD				*thd = current_thd;
	XTExceptionRec	e;
	int				err = 0;
	XTThreadPtr		self;

	if (!(self = xt_ha_set_current_thread(thd, &e)))
		return xt_ha_pbxt_to_mysql_error(e.e_xt_err);

	try_(a) {
		xt_ha_open_database_of_table(self, (XTPathStrPtr) table_path);

		if (xt_table_exists(self->st_database))
			xt_throw_xterr(XT_CONTEXT, XT_ERR_PBXT_TABLE_EXISTS);

		XTSystemTableShare::setSystemTableDeleted(table_path);

		if (!XTSystemTableShare::doesSystemTableExist()) {
			xt_ha_all_threads_close_database(self, self->st_database);
			xt_drop_database(self, self->st_database);
			xt_unuse_database(self, self);
			xt_ha_close_global_database(self);
		}
	}
	catch_(a) {
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
	}
	cont_(a);

	return err;
}

/*
 * Renames a table from one name to another from alter table call.
 * This function can be used to move a table from one database to
 * another.
 */
#ifdef DRIZZLED
int PBXTStorageEngine::doRenameTable(Session *,
                                     const char *from,
                                     const char *to)
#else
int ha_pbxt::rename_table(const char *from, const char *to)
#endif
{
	THD				*thd = current_thd;
	int				err = 0;
	XTThreadPtr		self;
	XTSharePtr		share;
	XTDatabaseHPtr	to_db;

	if (XTSystemTableShare::isSystemTable(from))
		return rename_system_table(from, to);

	if (!(self = ha_set_current_thread(thd, &err)))
		return xt_ha_pbxt_to_mysql_error(err);

	XT_PRINT2(self, "rename_table (%s -> %s)\n", from, to);

#ifdef PBMS_ENABLED
	PBMSResultRec result;

	err = pbms_rename_table_with_blobs(from, to, &result);
	if (err) {
		xt_logf(XT_NT_ERROR, "pbms_rename_table_with_blobs() Error: %s", result.mr_message);
		return err;
	}
#endif

	try_(a) {
		xt_ha_open_database_of_table(self, (XTPathStrPtr) to);
		to_db = self->st_database;

		xt_ha_open_database_of_table(self, (XTPathStrPtr) from);

		if (self->st_database != to_db)
			xt_throw_xterr(XT_CONTEXT, XT_ERR_CANNOT_CHANGE_DB);

		/*
		 * NOTE: MySQL does not lock before calling rename table!
		 *
		 * We cannot use pb_share because rename_table() is
		 * called without correctly initializing
		 * the handler!
		 */
		share = ha_get_share(self, from, true);
		pushr_(ha_unget_share, share);
		ha_aquire_exclusive_use(self, share, NULL);
		pushr_(ha_release_exclusive_use, share);
		ha_close_open_tables(self, share, NULL);

		self->st_ignore_fkeys = (thd_test_options(thd, OPTION_NO_FOREIGN_KEY_CHECKS)) != 0;
		xt_rename_table(self, (XTPathStrPtr) from, (XTPathStrPtr) to);

		freer_(); // ha_release_exclusive_use(share)
		freer_(); // ha_unget_share(share)

		/*
		 * If there are no more PBXT tables in the database, we
		 * "drop the database", which deletes all PBXT resources
		 * in the database.
		 */
#ifdef XT_USE_GLOBAL_DB
		/* We now only drop the pbxt system data,
		 * when the PBXT database is dropped.
		 */
		if (!xt_table_exists(self->st_database)) {
			xt_ha_all_threads_close_database(self, self->st_database);
			xt_drop_database(self, self->st_database);
		}
#endif
	}
	catch_(a) {
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
	}
	cont_(a);
	
#ifdef PBMS_ENABLED
	pbms_completed(NULL, (err == 0));
#endif

	XT_RETURN(err);
}

#ifdef DRIZZLED
int PBXTStorageEngine::rename_system_table(const char *XT_UNUSED(from), const char *XT_UNUSED(to))
#else
int ha_pbxt::rename_system_table(const char *XT_UNUSED(from), const char *XT_UNUSED(to))
#endif
{
	return ER_NOT_SUPPORTED_YET;
}

uint ha_pbxt::max_supported_key_length() const
{
	return XT_INDEX_MAX_KEY_SIZE;
}

uint ha_pbxt::max_supported_key_part_length() const
{
	/* There is a little overhead in order to fit! */
	return XT_INDEX_MAX_KEY_SIZE-4;
}

/*
 * Called in test_quick_select to determine if indexes should be used.
 *
 * As far as I can tell, time is measured in "disk reads". So the
 * calculation below means the system reads about 20 rows per read.
 *
 * For example a sequence scan uses a read buffer which reads a
 * number of rows at once, or a sequential scan can make use
 * of the cache (so it need to read less).
 */
double ha_pbxt::scan_time()
{
	double result = (double) (stats.records + stats.deleted) / 38.0 + 2;
	return result;
}

/*
 * The next method will never be called if you do not implement indexes.
 */
double ha_pbxt::read_time(uint XT_UNUSED(index), uint ranges, ha_rows rows)
{
	double result = rows2double(ranges+rows);
	return result;
}

/*
 * Given a starting key, and an ending key estimate the number of rows that
 * will exist between the two. end_key may be empty which in case determine
 * if start_key matches any rows.
 * 
 * Called from opt_range.cc by check_quick_keys().
 *
 */
ha_rows ha_pbxt::records_in_range(uint inx, key_range *min_key, key_range *max_key)
{
	XTIndexPtr		ind;
	key_part_map	keypart_map;
	u_int			segement = 0;
	ha_rows			result;

	if (min_key)
		keypart_map = min_key->keypart_map;
	else if (max_key)
		keypart_map = max_key->keypart_map;
	else
		return 1;
	ind = (XTIndexPtr) pb_share->sh_dic_keys[inx];
	
	while (keypart_map & 1) {
		segement++;
		keypart_map = keypart_map >> 1;
	}

	if (segement < 1 || segement > ind->mi_seg_count)
		result = 1;
	else
		result = ind->mi_seg[segement-1].is_recs_in_range;
#ifdef XT_PRINT_INDEX_OPT
	printf("records_in_range %s index %d cols req=%d/%d read_bits=%X write_bits=%X index_bits=%X --> %d\n", pb_open_tab->ot_table->tab_name->ps_path, (int) inx, segement, ind->mi_seg_count, (int) *table->read_set->bitmap, (int) *table->write_set->bitmap, (int) *ind->mi_col_map.bitmap, (int) result);
#endif
	return result;
}

/*
 * create() is called to create a table/database. The variable name will have the name
 * of the table. When create() is called you do not need to worry about opening
 * the table. Also, the FRM file will have already been created so adjusting
 * create_info will not do you any good. You can overwrite the frm file at this
 * point if you wish to change the table definition, but there are no methods
 * currently provided for doing that.

 * Called from handle.cc by ha_create_table().
*/
#ifdef DRIZZLED
int PBXTStorageEngine::doCreateTable(Session *, 
                                     const char *table_path, 
                                     Table &table_arg, 
                                     HA_CREATE_INFO &create_info, 
                                     drizzled::message::Table &XT_UNUSED(proto))
#else
int ha_pbxt::create(const char *table_path, TABLE *table_arg, HA_CREATE_INFO *create_info)
#endif
{
	THD				*thd = current_thd;
	int				err = 0;
	XTThreadPtr		self;
	XTDDTable		*tab_def = NULL;
	XTDictionaryRec	dic;

	if ((strcmp(table_path, "./pbxt/location") == 0) || (strcmp(table_path, "./pbxt/statistics") == 0))
		return 0;

	memset(&dic, 0, sizeof(dic));

	if (!(self = ha_set_current_thread(thd, &err)))
		return xt_ha_pbxt_to_mysql_error(err);
#ifdef DRIZZLED
	XT_PRINT2(self, "create (%s) %s\n", table_path, (create_info.options & HA_LEX_CREATE_TMP_TABLE) ? "temporary" : "");
#else
	XT_PRINT2(self, "create (%s) %s\n", table_path, (create_info->options & HA_LEX_CREATE_TMP_TABLE) ? "temporary" : "");
#endif

	STAT_TRACE(self, *thd_query(thd));

	try_(a) {
		xt_ha_open_database_of_table(self, (XTPathStrPtr) table_path);

#ifdef DRIZZLED
		for (uint i=0; i<TS(&table_arg)->keys; i++) {
			if (table_arg.key_info[i].key_length > XT_INDEX_MAX_KEY_SIZE)
				xt_throw_sulxterr(XT_CONTEXT, XT_ERR_KEY_TOO_LARGE, table_arg.key_info[i].name, (u_long) XT_INDEX_MAX_KEY_SIZE);
		}
#else
		for (uint i=0; i<TS(table_arg)->keys; i++) {
			if (table_arg->key_info[i].key_length > XT_INDEX_MAX_KEY_SIZE)
				xt_throw_sulxterr(XT_CONTEXT, XT_ERR_KEY_TOO_LARGE, table_arg->key_info[i].name, (u_long) XT_INDEX_MAX_KEY_SIZE);
		}
#endif

		/* ($) auto_increment_value will be zero if 
		 * AUTO_INCREMENT is not used. Otherwise
		 * Query was ALTER TABLE ... AUTO_INCREMENT = x; or 
		 * CREATE TABLE ... AUTO_INCREMENT = x;
		 */
#ifdef DRIZZLED
		tab_def = xt_ri_create_table(self, true, (XTPathStrPtr) table_path, *thd_query(thd), myxt_create_table_from_table(self, &table_arg));
		tab_def->checkForeignKeys(self, create_info.options & HA_LEX_CREATE_TMP_TABLE);
#else
		tab_def = xt_ri_create_table(self, true, (XTPathStrPtr) table_path, *thd_query(thd), myxt_create_table_from_table(self, table_arg));
		tab_def->checkForeignKeys(self, create_info->options & HA_LEX_CREATE_TMP_TABLE);
#endif

		dic.dic_table = tab_def;
#ifdef DRIZZLED
		dic.dic_my_table = &table_arg;
		dic.dic_tab_flags = (create_info.options & HA_LEX_CREATE_TMP_TABLE) ? XT_TAB_FLAGS_TEMP_TAB : 0;
		dic.dic_min_auto_inc = (xtWord8) create_info.auto_increment_value; /* ($) */
		dic.dic_def_ave_row_size = table_arg.s->getAvgRowLength();
#else
		dic.dic_my_table = table_arg;
		dic.dic_tab_flags = (create_info->options & HA_LEX_CREATE_TMP_TABLE) ? XT_TAB_FLAGS_TEMP_TAB : 0;
		dic.dic_min_auto_inc = (xtWord8) create_info->auto_increment_value; /* ($) */
		dic.dic_def_ave_row_size = (xtWord8) table_arg->s->avg_row_length;
#endif
		myxt_setup_dictionary(self, &dic);

		/*
		 * We used to ignore the value of foreign_key_checks flag and allowed creation
		 * of tables with "hanging" references. Now we validate FKs if foreign_key_checks != 0
		 */
		self->st_ignore_fkeys = (thd_test_options(thd, OPTION_NO_FOREIGN_KEY_CHECKS)) != 0;

		/*
		 * Previously I set delete_if_exists=TRUE because
		 * CREATE TABLE was being used to TRUNCATE.
		 * This was due to the flag HTON_CAN_RECREATE.
		 * Now I could set delete_if_exists=FALSE, but
		 * leaving it TRUE should not cause any problems.
		 */
		xt_create_table(self, (XTPathStrPtr) table_path, &dic);
	}
	catch_(a) {
		if (tab_def)
			tab_def->finalize(self);
		dic.dic_table = NULL;
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, FALSE);
	}
	cont_(a);

	/* Free the dictionary, but not 'table_arg'! */
	dic.dic_my_table = NULL;
	myxt_free_dictionary(self, &dic);

	XT_RETURN(err);
}

void ha_pbxt::update_create_info(HA_CREATE_INFO *create_info)
{
	XTOpenTablePtr	ot;

	if ((ot = pb_open_tab)) {
		if (!(create_info->used_fields & HA_CREATE_USED_AUTO)) {
			/* Fill in the minimum auto-increment value! */
			create_info->auto_increment_value = ot->ot_table->tab_dic.dic_min_auto_inc;
		}
	}
}

char *ha_pbxt::get_foreign_key_create_info()
{
	THD					*thd = current_thd;
	int					err = 0;
	XTThreadPtr			self;
	XTStringBufferRec	tab_def = { 0, 0, 0 };

	if (!(self = ha_set_current_thread(thd, &err))) {
		xt_ha_pbxt_to_mysql_error(err);
		return NULL;
	}

	if (!pb_open_tab) {
		if ((err = reopen()))
			return NULL;
	}

	if (!pb_open_tab->ot_table->tab_dic.dic_table)
		return NULL;

	try_(a) {
		pb_open_tab->ot_table->tab_dic.dic_table->loadForeignKeyString(self, &tab_def);
	}
	catch_(a) {
		xt_sb_set_size(self, &tab_def, 0);
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
	}
	cont_(a);

	return tab_def.sb_cstring;
}

void ha_pbxt::free_foreign_key_create_info(char* str)
{
	xt_free(NULL, str);
}

bool ha_pbxt::get_error_message(int XT_UNUSED(error), String *buf)
{
	THD				*thd = current_thd;
	int				err = 0;
	XTThreadPtr		self;

	if (!(self = ha_set_current_thread(thd, &err)))
		return FALSE;

	if (!self->t_exception.e_xt_err)
		return FALSE;

	buf->copy(self->t_exception.e_err_msg, (uint32) strlen(self->t_exception.e_err_msg), system_charset_info);
	return TRUE;
}

/* 
 * get info about FKs of the currently open table
 * used in 
 * 1. REPLACE; is > 0 if table is referred by a FOREIGN KEY 
 * 2. INFORMATION_SCHEMA tables: TABLE_CONSTRAINTS, REFERENTIAL_CONSTRAINTS
 * Return value: as of 5.1.24 it's ignored
 */

int ha_pbxt::get_foreign_key_list(THD *thd, List<FOREIGN_KEY_INFO> *f_key_list)
{
	int err = 0;
	XTThreadPtr	self;
	const char *action;

	if (!(self = ha_set_current_thread(thd, &err))) {
		return xt_ha_pbxt_to_mysql_error(err);
	}

	try_(a) {
		XTDDTable *table_dic = pb_open_tab->ot_table->tab_dic.dic_table;

		if (table_dic == NULL)
			xt_throw_errno(XT_CONTEXT, XT_ERR_NO_DICTIONARY);

		for (int i = 0, sz = table_dic->dt_fkeys.size(); i < sz; i++) {
			FOREIGN_KEY_INFO *fk_info= new	// assumed that C++ exceptions are disabled
				(thd_alloc(thd, sizeof(FOREIGN_KEY_INFO))) FOREIGN_KEY_INFO;

			if (fk_info == NULL)
				xt_throw_errno(XT_CONTEXT, XT_ENOMEM);

			XTDDForeignKey *fk = table_dic->dt_fkeys.itemAt(i);

			const char *path = fk->fk_ref_tab_name->ps_path;
			const char *ref_tbl_name = path + strlen(path);

			while (ref_tbl_name != path && !XT_IS_DIR_CHAR(*ref_tbl_name)) 
				ref_tbl_name--;

			const char * ref_db_name = ref_tbl_name - 1;

			while (ref_db_name != path && !XT_IS_DIR_CHAR(*ref_db_name)) 
				ref_db_name--;

			ref_tbl_name++;
			ref_db_name++;

			fk_info->foreign_id = thd_make_lex_string(thd, 0,
				fk->co_name, (uint) strlen(fk->co_name), 1);

			fk_info->referenced_db = thd_make_lex_string(thd, 0,
				ref_db_name, (uint) (ref_tbl_name - ref_db_name - 1), 1);

			fk_info->referenced_table = thd_make_lex_string(thd, 0,
				ref_tbl_name, (uint) strlen(ref_tbl_name), 1);

			fk_info->referenced_key_name = NULL;			

			XTIndex *ix = fk->getReferenceIndexPtr();
			if (ix == NULL) /* can be NULL if another thread changes referenced table at the moment */
				continue;
			
			XTDDTable *ref_table = fk->fk_ref_table;

			// might be a self-reference
			if ((ref_table == NULL) 
				&& (xt_tab_compare_names(path, table_dic->dt_table->tab_name->ps_path) == 0)) {
				ref_table = table_dic;
			}

			if (ref_table != NULL) {
				const XTList<XTDDIndex>& ix_list = ref_table->dt_indexes;
				for (int j = 0, sz2 = ix_list.size(); j < sz2; j++) {
					XTDDIndex *ddix = ix_list.itemAt(j);
					if (ddix->in_index ==  ix->mi_index_no) {
						const char *ix_name = 
							ddix->co_name ? ddix->co_name : ddix->co_ind_name;
						fk_info->referenced_key_name = thd_make_lex_string(thd, 0,
							ix_name, (uint) strlen(ix_name), 1);
						break;
					}
				}
			}

			action = XTDDForeignKey::actionTypeToString(fk->fk_on_delete);
			fk_info->delete_method = thd_make_lex_string(thd, 0,
				action, (uint) strlen(action), 1);
			action = XTDDForeignKey::actionTypeToString(fk->fk_on_update);
			fk_info->update_method = thd_make_lex_string(thd, 0,
				action, (uint) strlen(action), 1);

			const XTList<XTDDColumnRef>& cols = fk->co_cols;
			for (int j = 0, sz2 = cols.size(); j < sz2; j++) {
				XTDDColumnRef *col_ref= cols.itemAt(j);
				fk_info->foreign_fields.push_back(thd_make_lex_string(thd, 0,
					col_ref->cr_col_name, (uint) strlen(col_ref->cr_col_name), 1));
			}

			const XTList<XTDDColumnRef>& ref_cols = fk->fk_ref_cols;
			for (int j = 0, sz2 = ref_cols.size(); j < sz2; j++) {
				XTDDColumnRef *col_ref= ref_cols.itemAt(j);
				fk_info->referenced_fields.push_back(thd_make_lex_string(thd, 0,
					col_ref->cr_col_name, (uint) strlen(col_ref->cr_col_name), 1));
			}

			f_key_list->push_back(fk_info);
		}
	}
	catch_(a) {
		err = xt_ha_pbxt_thread_error_for_mysql(thd, self, pb_ignore_dup_key);
	}
	cont_(a);

	return err; 
}

uint ha_pbxt::referenced_by_foreign_key()
{
	XTDDTable *table_dic = pb_open_tab->ot_table->tab_dic.dic_table;

	if (!table_dic)
		return 0;
	/* Check the list of referencing tables: */
	return table_dic->dt_trefs ? 1 : 0;
}


struct st_mysql_sys_var
{
	MYSQL_PLUGIN_VAR_HEADER;
};

#if MYSQL_VERSION_ID < 60000
#if MYSQL_VERSION_ID >= 50124
#define USE_CONST_SAVE
#endif
#else
#if MYSQL_VERSION_ID >= 60005
#define USE_CONST_SAVE
#endif
#endif

#ifdef USE_CONST_SAVE
static void pbxt_record_cache_size_func(THD *XT_UNUSED(thd), struct st_mysql_sys_var *var, void *tgt, const void *save)
#else
static void pbxt_record_cache_size_func(THD *XT_UNUSED(thd), struct st_mysql_sys_var *var, void *tgt, void *save)
#endif
{
	xtInt8	record_cache_size;

	char *old= *(char **) tgt;
	*(char **)tgt= *(char **) save;
	if (var->flags & PLUGIN_VAR_MEMALLOC)
	{
		*(char **)tgt= my_strdup(*(char **) save, MYF(0));
		my_free(old);
	}
	record_cache_size = ha_set_variable(&pbxt_record_cache_size, &vp_record_cache_size);
	xt_tc_set_cache_size((size_t) record_cache_size);
#ifdef DEBUG
	char buffer[200];

	sprintf(buffer, "pbxt_record_cache_size=%llu\n", (u_llong) record_cache_size);
	xt_logf(XT_NT_INFO, buffer);
#endif
}

#ifndef DRIZZLED
struct st_mysql_storage_engine pbxt_storage_engine = {
	MYSQL_HANDLERTON_INTERFACE_VERSION
};
static st_mysql_information_schema pbxt_statitics = {
	MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION
};
#endif

#if MYSQL_VERSION_ID >= 50118
static MYSQL_SYSVAR_STR(index_cache_size, pbxt_index_cache_size,
  PLUGIN_VAR_READONLY,
  "The amount of memory allocated to the index cache, used only to cache index data.",
  NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(record_cache_size, pbxt_record_cache_size,
  PLUGIN_VAR_READONLY, // PLUGIN_VAR_OPCMDARG | PLUGIN_VAR_MEMALLOC,
  "The amount of memory allocated to the record cache used to cache table data.",
  NULL, pbxt_record_cache_size_func, NULL);

static MYSQL_SYSVAR_STR(log_cache_size, pbxt_log_cache_size,
  PLUGIN_VAR_READONLY,
  "The amount of memory allocated to the transaction log cache used to cache transaction log data.",
  NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(log_file_threshold, pbxt_log_file_threshold,
  PLUGIN_VAR_READONLY,
  "The size of a transaction log before rollover, and a new log is created.",
  NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(transaction_buffer_size, pbxt_transaction_buffer_size,
  PLUGIN_VAR_READONLY,
  "The size of the global transaction log buffer (the engine allocates 2 buffers of this size).",
  NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(log_buffer_size, pbxt_log_buffer_size,
  PLUGIN_VAR_READONLY,
  "The size of the buffer used to cache data from transaction and data logs during sequential scans, or when writing a data log.",
  NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(checkpoint_frequency, pbxt_checkpoint_frequency,
  PLUGIN_VAR_READONLY,
  "The size of the transaction data buffer which is allocate by each thread.",
  NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(data_log_threshold, pbxt_data_log_threshold,
  PLUGIN_VAR_READONLY,
  "The maximum size of a data log file.",
  NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(data_file_grow_size, pbxt_data_file_grow_size,
  PLUGIN_VAR_READONLY,
  "The amount by which the handle data files (.xtd) grow.",
  NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(row_file_grow_size, pbxt_row_file_grow_size,
  PLUGIN_VAR_READONLY,
  "The amount by which the row pointer files (.xtr) grow.",
  NULL, NULL, NULL);

static MYSQL_SYSVAR_INT(garbage_threshold, xt_db_garbage_threshold,
	PLUGIN_VAR_OPCMDARG,
	"The percentage of garbage in a repository file before it is compacted.",
	NULL, NULL, XT_DL_DEFAULT_GARBAGE_LEVEL, 0, 100, 1);

static MYSQL_SYSVAR_INT(log_file_count, xt_db_log_file_count,
	PLUGIN_VAR_OPCMDARG,
	"The minimum number of transaction logs used.",
	NULL, NULL, XT_DL_DEFAULT_XLOG_COUNT, 1, 20000, 1);

static MYSQL_SYSVAR_INT(auto_increment_mode, xt_db_auto_increment_mode,
	PLUGIN_VAR_OPCMDARG,
	"The auto-increment mode, 0 = MySQL standard (default), 1 = previous ID's never reused.",
	NULL, NULL, XT_AUTO_INCREMENT_DEF, 0, 1, 1);

/* {RN145} */
static MYSQL_SYSVAR_INT(offline_log_function, xt_db_offline_log_function,
	PLUGIN_VAR_OPCMDARG,
	"Determines what happens to transaction logs when the are moved offline, 0 = recycle logs (default), 1 = delete logs (default on Mac OS X), 2 = keep logs.",
	NULL, NULL, XT_OFFLINE_LOG_FUNCTION_DEF, 0, 2, 1);

/* {RN150} */
static MYSQL_SYSVAR_INT(sweeper_priority, xt_db_sweeper_priority,
	PLUGIN_VAR_OPCMDARG,
	"Determines the priority of the background sweeper process, 0 = low (default), 1 = normal (same as user threads), 2 = high.",
	NULL, NULL, XT_PRIORITY_LOW, XT_PRIORITY_LOW, XT_PRIORITY_HIGH, 1);

#ifdef DRIZZLED
static MYSQL_SYSVAR_INT(max_threads, pbxt_max_threads,
	PLUGIN_VAR_OPCMDARG | PLUGIN_VAR_READONLY,
	"The maximum number of threads used by PBXT",
	NULL, NULL, 500, 20, 20000, 1);
#else
static MYSQL_SYSVAR_INT(max_threads, pbxt_max_threads,
	PLUGIN_VAR_OPCMDARG | PLUGIN_VAR_READONLY,
	"The maximum number of threads used by PBXT, 0 = set according to MySQL max_connections.",
	NULL, NULL, 0, 0, 20000, 1);
#endif

#ifndef DEBUG
static MYSQL_SYSVAR_BOOL(support_xa, pbxt_support_xa,
	PLUGIN_VAR_OPCMDARG,
	"Enable PBXT support for the XA two-phase commit, default is enabled",
	NULL, NULL, TRUE);
#else
static MYSQL_SYSVAR_BOOL(support_xa, pbxt_support_xa,
	PLUGIN_VAR_OPCMDARG,
	"Enable PBXT support for the XA two-phase commit, default is disabled (due to assertion failure in MySQL)",
	/* The problem is, in MySQL an assertion fails in debug mode: 
	 * Assertion failed: (total_ha_2pc == (ulong) opt_bin_log+1), function ha_recover, file handler.cc, line 1557.
     */
	NULL, NULL, FALSE);
#endif

static MYSQL_SYSVAR_INT(flush_log_at_trx_commit, xt_db_flush_log_at_trx_commit,
	PLUGIN_VAR_OPCMDARG,
	"Determines whether the transaction log is written and/or flushed when a transaction is committed (no matter what the setting the log is written and flushed once per second), 0 = no write & no flush, 1 = write & flush (default), 2 = write & no flush.",
	NULL, NULL, 1, 0, 2, 1);

static struct st_mysql_sys_var* pbxt_system_variables[] = {
  MYSQL_SYSVAR(index_cache_size),
  MYSQL_SYSVAR(record_cache_size),
  MYSQL_SYSVAR(log_cache_size),
  MYSQL_SYSVAR(log_file_threshold),
  MYSQL_SYSVAR(transaction_buffer_size),
  MYSQL_SYSVAR(log_buffer_size),
  MYSQL_SYSVAR(checkpoint_frequency),
  MYSQL_SYSVAR(data_log_threshold),
  MYSQL_SYSVAR(data_file_grow_size),
  MYSQL_SYSVAR(row_file_grow_size),
  MYSQL_SYSVAR(garbage_threshold),
  MYSQL_SYSVAR(log_file_count),
  MYSQL_SYSVAR(auto_increment_mode),
  MYSQL_SYSVAR(offline_log_function),
  MYSQL_SYSVAR(sweeper_priority),
  MYSQL_SYSVAR(max_threads),
  MYSQL_SYSVAR(support_xa),
  MYSQL_SYSVAR(flush_log_at_trx_commit),
  NULL
};
#endif

#ifdef DRIZZLED
drizzle_declare_plugin(pbxt)
#else
mysql_declare_plugin(pbxt)
#endif
{
#ifndef DRIZZLED
	MYSQL_STORAGE_ENGINE_PLUGIN,
	&pbxt_storage_engine,
#endif
	"PBXT",
#ifdef DRIZZLED
	"1.0",
#endif
	"Paul McCullagh, PrimeBase Technologies GmbH",
	"High performance, multi-versioning transactional engine",
	PLUGIN_LICENSE_GPL,
	pbxt_init, /* Plugin Init */
	pbxt_end, /* Plugin Deinit */
#ifndef DRIZZLED
	0x0001 /* 0.1 */,
#endif
	NULL,                       /* status variables                */
#if MYSQL_VERSION_ID >= 50118
	pbxt_system_variables,		/* system variables                */
#else
	NULL,
#endif
	NULL						/* config options                  */
},
{
#ifndef DRIZZLED
	MYSQL_INFORMATION_SCHEMA_PLUGIN,
	&pbxt_statitics,
#endif
	"PBXT_STATISTICS",
#ifdef DRIZZLED
	"1.0",
#endif
	"Paul McCullagh, PrimeBase Technologies GmbH",
	"PBXT internal system statitics",
	PLUGIN_LICENSE_GPL,
	pbxt_init_statistics,						/* plugin init */
	pbxt_exit_statistics,						/* plugin deinit */
#ifndef DRIZZLED
	0x0005,
#endif
	NULL,										/* status variables */
	NULL,										/* system variables */
	NULL										/* config options */
}
#ifdef DRIZZLED
drizzle_declare_plugin_end;
#else
mysql_declare_plugin_end;
#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID > 50200
maria_declare_plugin(pbxt)
{ /* PBXT */
  MYSQL_STORAGE_ENGINE_PLUGIN,
  &pbxt_storage_engine,
  "PBXT",
  "Paul McCullagh, PrimeBase Technologies GmbH",
  "High performance, multi-versioning transactional engine",
  PLUGIN_LICENSE_GPL,
  pbxt_init, /* Plugin Init */
  pbxt_end, /* Plugin Deinit */
  0x0001 /* 0.1 */,
  NULL,                       /* status variables */
  pbxt_system_variables,      /* system variables */
  "1.0.11-7 Pre-GA",              /* string version */
  MariaDB_PLUGIN_MATURITY_GAMMA /* maturity */
},
{ /* PBXT_STATISTICS */
  MYSQL_INFORMATION_SCHEMA_PLUGIN,
  &pbxt_statitics,
  "PBXT_STATISTICS",
  "Paul McCullagh, PrimeBase Technologies GmbH",
  "PBXT internal system statitics",
  PLUGIN_LICENSE_GPL,
  pbxt_init_statistics,       /* plugin init */
  pbxt_exit_statistics,       /* plugin deinit */
  0x0005,
  NULL,                       /* status variables */
  NULL,                       /* system variables */
  "1.0.11-7 Pre-GA",          /* string version */
  MariaDB_PLUGIN_MATURITY_GAMMA /* maturity */
}
maria_declare_plugin_end;
#endif
#endif

#if defined(XT_WIN) && defined(XT_COREDUMP)

/*
 * WINDOWS CORE DUMP SUPPORT
 *
 * MySQL supports core dumping on Windows with --core-file command line option. 
 * However it creates dumps with the MiniDumpNormal option which saves only stack traces.
 *
 * We instead (or in addition) create dumps with MiniDumpWithoutOptionalData option
 * which saves all available information. To enable core dumping enable XT_COREDUMP
 * at compile time.
 * In addition, pbxt_crash_debug must be set to TRUE which is the case if XT_CRASH_DEBUG
 * is defined.
 * This switch is also controlled by creating a file called "no-debug" or "crash-debug"
 * in the pbxt database directory.
 */

typedef enum _MINIDUMP_TYPE {
    MiniDumpNormal                         = 0x0000,
    MiniDumpWithDataSegs                   = 0x0001,
    MiniDumpWithFullMemory                 = 0x0002,
    MiniDumpWithHandleData                 = 0x0004,
    MiniDumpFilterMemory                   = 0x0008,
    MiniDumpScanMemory                     = 0x0010,
    MiniDumpWithUnloadedModules            = 0x0020,
    MiniDumpWithIndirectlyReferencedMemory = 0x0040,
    MiniDumpFilterModulePaths              = 0x0080,
    MiniDumpWithProcessThreadData          = 0x0100,
    MiniDumpWithPrivateReadWriteMemory     = 0x0200,
} MINIDUMP_TYPE;

typedef struct _MINIDUMP_EXCEPTION_INFORMATION {
    DWORD ThreadId;
    PEXCEPTION_POINTERS ExceptionPointers;
    BOOL ClientPointers;
} MINIDUMP_EXCEPTION_INFORMATION, *PMINIDUMP_EXCEPTION_INFORMATION;

typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(
	HANDLE hProcess, 
	DWORD dwPid, 
	HANDLE hFile, 
	MINIDUMP_TYPE DumpType,
	void *ExceptionParam,
	void *UserStreamParam,
	void *CallbackParam
	);

char base_path[_MAX_PATH] = {0};
char dump_path[_MAX_PATH] = {0};

void core_dump(struct _EXCEPTION_POINTERS *pExceptionInfo)
{
	SECURITY_ATTRIBUTES	sa = { sizeof(SECURITY_ATTRIBUTES), 0, 0 };
	int i;
	HMODULE hDll = NULL;
	HANDLE hFile;
	MINIDUMPWRITEDUMP pDump;
	char *end_ptr = base_path;

	MINIDUMP_EXCEPTION_INFORMATION ExInfo, *ExInfoPtr = NULL;

	if (pExceptionInfo) {
		ExInfo.ThreadId = GetCurrentThreadId();
		ExInfo.ExceptionPointers = pExceptionInfo;
		ExInfo.ClientPointers = NULL;
		ExInfoPtr = &ExInfo;
	}

	end_ptr = base_path + strlen(base_path);

	strcat(base_path, "DBGHELP.DLL" );
	hDll = LoadLibrary(base_path);
	*end_ptr = 0;
	if (hDll==NULL) {
		int err;
		err = HRESULT_CODE(GetLastError());
		hDll = LoadLibrary( "DBGHELP.DLL" );
		if (hDll==NULL) {
			err = HRESULT_CODE(GetLastError());
			return;
		}
	}

	pDump = (MINIDUMPWRITEDUMP)GetProcAddress( hDll, "MiniDumpWriteDump" );
	if (!pDump) {
		int err;
		err = HRESULT_CODE(GetLastError());
		return;
	}

	for (i = 1; i < INT_MAX; i++) {
		sprintf(dump_path, "%sPBXTCore%08d.dmp", base_path, i);
		hFile = CreateFile( dump_path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_NEW,
							FILE_ATTRIBUTE_NORMAL, NULL );

		if ( hFile != INVALID_HANDLE_VALUE )
			break;

		if (HRESULT_CODE(GetLastError()) == ERROR_FILE_EXISTS )
			continue;

		return;
	}

	// write the dump
	BOOL bOK = pDump( GetCurrentProcess(), GetCurrentProcessId(), hFile, 
		MiniDumpWithPrivateReadWriteMemory, ExInfoPtr, NULL, NULL );

	CloseHandle(hFile);
}

LONG crash_filter( struct _EXCEPTION_POINTERS *pExceptionInfo )
{
	core_dump(pExceptionInfo);
	return EXCEPTION_EXECUTE_HANDLER;
}

void register_crash_filter()
{
	SetUnhandledExceptionFilter( (LPTOP_LEVEL_EXCEPTION_FILTER) crash_filter );
}

#endif // XT_WIN && XT_COREDUMP