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
|
/***************** Xindex C++ Class Xindex Code (.CPP) *****************/
/* Name: XINDEX.CPP Version 2.9 */
/* */
/* (C) Copyright to the author Olivier BERTRAND 2004-2014 */
/* */
/* This file contains the class XINDEX implementation code. */
/***********************************************************************/
/***********************************************************************/
/* Include relevant sections of the System header files. */
/***********************************************************************/
#include "my_global.h"
#if defined(WIN32)
#include <io.h>
#include <fcntl.h>
#include <errno.h>
//#include <windows.h>
#else // !WIN32
#if defined(UNIX)
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#else // !UNIX
#include <io.h>
#endif // !UNIX
#include <fcntl.h>
#endif // !WIN32
/***********************************************************************/
/* Include required application header files */
/* global.h is header containing all global Plug declarations. */
/* plgdbsem.h is header containing the DB applic. declarations. */
/* kindex.h is header containing the KINDEX class definition. */
/***********************************************************************/
#include "global.h"
#include "plgdbsem.h"
#include "osutil.h"
#include "maputil.h"
//nclude "filter.h"
#include "tabcol.h"
#include "xindex.h"
#include "xobject.h"
//nclude "scalfnc.h"
//nclude "array.h"
#include "filamtxt.h"
#include "tabdos.h"
#include "tabvct.h"
/***********************************************************************/
/* Macro or external routine definition */
/***********************************************************************/
#define NZ 8
#define NW 5
#define MAX_INDX 10
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
#endif
/***********************************************************************/
/* DB external variables. */
/***********************************************************************/
extern MBLOCK Nmblk; /* Used to initialize MBLOCK's */
#if defined(XMAP)
extern my_bool xmap;
#endif // XMAP
/***********************************************************************/
/* Last two parameters are true to enable type checking, and last one */
/* to have rows filled by blanks to be compatible with QRY blocks. */
/***********************************************************************/
PVBLK AllocValBlock(PGLOBAL, void *, int, int, int, int,
bool check = true, bool blank = true, bool un = false);
/***********************************************************************/
/* Check whether we have to create/update permanent indexes. */
/***********************************************************************/
int PlgMakeIndex(PGLOBAL g, PSZ name, PIXDEF pxdf, bool add)
{
int rc;
PTABLE tablep;
PTDBASE tdbp;
PCATLG cat = PlgGetCatalog(g, true);
/*********************************************************************/
/* Open a new table in mode read and with only the keys columns. */
/*********************************************************************/
tablep = new(g) XTAB(name);
if (!(tdbp = (PTDBASE)cat->GetTable(g, tablep)))
rc = RC_NF;
else if (!tdbp->GetDef()->Indexable()) {
sprintf(g->Message, MSG(TABLE_NO_INDEX), name);
rc = RC_NF;
} else if ((rc = tdbp->MakeIndex(g, pxdf, add)) == RC_INFO)
rc = RC_OK; // No or remote index
return rc;
} // end of PlgMakeIndex
/* -------------------------- Class INDEXDEF ------------------------- */
/***********************************************************************/
/* INDEXDEF Constructor. */
/***********************************************************************/
INDEXDEF::INDEXDEF(char *name, bool uniq, int n)
{
//To_Def = NULL;
Next = NULL;
ToKeyParts = NULL;
Name = name;
Unique = uniq;
Invalid = false;
AutoInc = false;
Dynamic = false;
Mapped = false;
Nparts = 0;
ID = n;
//Offset = 0;
//Offhigh = 0;
//Size = 0;
MaxSame = 1;
} // end of INDEXDEF constructor
/***********************************************************************/
/* Set the max same values for each colum after making the index. */
/***********************************************************************/
void INDEXDEF::SetMxsame(PXINDEX x)
{
PKPDEF kdp;
PXCOL xcp;
for (kdp = ToKeyParts, xcp = x->To_KeyCol;
kdp && xcp; kdp = kdp->Next, xcp = xcp->Next)
kdp->Mxsame = xcp->Mxs;
} // end of SetMxsame
/* -------------------------- Class KPARTDEF ------------------------- */
/***********************************************************************/
/* KPARTDEF Constructor. */
/***********************************************************************/
KPARTDEF::KPARTDEF(PSZ name, int n)
{
Next = NULL;
Name = name;
Mxsame = 0;
Ncol = n;
Klen = 0;
} // end of KPARTDEF constructor
/* -------------------------- XXBASE Class --------------------------- */
/***********************************************************************/
/* XXBASE public constructor. */
/***********************************************************************/
XXBASE::XXBASE(PTDBDOS tbxp, bool b) : CSORT(b),
To_Rec((int*&)Record.Memp)
{
Tbxp = tbxp;
Record = Nmblk;
Cur_K = -1;
Old_K = -1;
Num_K = 0;
Ndif = 0;
Bot = Top = Inf = Sup = 0;
Op = OP_EQ;
To_KeyCol = NULL;
Mul = false;
Srtd = false;
Dynamic = false;
Val_K = -1;
Nblk = Sblk = 0;
Thresh = 7;
ID = -1;
Nth = 0;
} // end of XXBASE constructor
/***********************************************************************/
/* Make file output of XINDEX contents. */
/***********************************************************************/
void XXBASE::Print(PGLOBAL g, FILE *f, uint n)
{
char m[64];
memset(m, ' ', n); // Make margin string
m[n] = '\0';
fprintf(f, "%sXINDEX: Tbxp=%p Num=%d\n", m, Tbxp, Num_K);
} // end of Print
/***********************************************************************/
/* Make string output of XINDEX contents. */
/***********************************************************************/
void XXBASE::Print(PGLOBAL g, char *ps, uint z)
{
*ps = '\0';
strncat(ps, "Xindex", z);
} // end of Print
/* -------------------------- XINDEX Class --------------------------- */
/***********************************************************************/
/* XINDEX public constructor. */
/***********************************************************************/
XINDEX::XINDEX(PTDBDOS tdbp, PIXDEF xdp, PXLOAD pxp, PCOL *cp, PXOB *xp, int k)
: XXBASE(tdbp, !xdp->IsUnique())
{
Xdp = xdp;
ID = xdp->GetID();
Tdbp = tdbp;
X = pxp;
To_LastCol = NULL;
To_LastVal = NULL;
To_Cols = cp;
To_Vals = xp;
Mul = !xdp->IsUnique();
Srtd = false;
Nk = xdp->GetNparts();
Nval = (k) ? k : Nk;
Incr = 0;
//Defoff = xdp->GetOffset();
//Defhigh = xdp->GetOffhigh();
//Size = xdp->GetSize();
MaxSame = xdp->GetMaxSame();
} // end of XINDEX constructor
/***********************************************************************/
/* XINDEX Reset: re-initialize a Xindex block. */
/***********************************************************************/
void XINDEX::Reset(void)
{
for (PXCOL kp = To_KeyCol; kp; kp = kp->Next)
kp->Val_K = kp->Ndf;
Cur_K = Num_K;
Old_K = -1; // Needed to avoid not setting CurBlk for Update
Op = (Op == OP_FIRST || Op == OP_NEXT) ? OP_FIRST :
(Op == OP_FSTDIF || Op == OP_NXTDIF) ? OP_FSTDIF : OP_EQ;
Nth = 0;
} // end of Reset
/***********************************************************************/
/* XINDEX Close: terminate index and free all allocated data. */
/* Do not reset values that are used at return to make. */
/***********************************************************************/
void XINDEX::Close(void)
{
// Close file or view of file
if (X)
X->Close();
// De-allocate data
PlgDBfree(Record);
PlgDBfree(Index);
PlgDBfree(Offset);
for (PXCOL kcp = To_KeyCol; kcp; kcp = kcp->Next) {
// Column values cannot be retrieved from key anymore
if (kcp->Colp)
kcp->Colp->SetKcol(NULL);
// De-allocate Key data
kcp->FreeData();
} // endfor kcp
} // end of Close
/***********************************************************************/
/* XINDEX compare routine for C Quick/Insertion sort. */
/***********************************************************************/
int XINDEX::Qcompare(int *i1, int *i2)
{
register int k;
register PXCOL kcp;
for (kcp = To_KeyCol, k = 0; kcp; kcp = kcp->Next)
if ((k = kcp->Compare(*i1, *i2)))
break;
//num_comp++;
return k;
} // end of Qcompare
/***********************************************************************/
/* AddColumns: here we try to determine whether it is worthwhile to */
/* add to the keys the values of the columns selected for this table. */
/* Sure enough, it is done while records are read and permit to avoid */
/* reading the table while doing the join (Dynamic index only) */
/***********************************************************************/
bool XINDEX::AddColumns(PIXDEF xdp)
{
if (!Dynamic)
return false; // Not applying to static index
else if (IsMul())
return false; // Not done yet for multiple index
else if (Tbxp->GetAmType() == TYPE_AM_VCT && ((PTDBVCT)Tbxp)->IsSplit())
return false; // This would require to read additional files
else
return true;
} // end of AddColumns
/***********************************************************************/
/* Make: Make and index on key column(s). */
/***********************************************************************/
bool XINDEX::Make(PGLOBAL g, PIXDEF sxp)
{
/*********************************************************************/
/* Table can be accessed through an index. */
/*********************************************************************/
int k, nk = Nk, rc = RC_OK;
int *bof, i, j, n, ndf, nkey;
PKPDEF kdfp = Xdp->GetToKeyParts();
bool brc = false;
PCOL colp;
PFIL filp = Tdbp->GetFilter();
PXCOL kp, addcolp, prev = NULL, kcp = NULL;
//PDBUSER dup = (PDBUSER)g->Activityp->Aptr;
#if defined(_DEBUG)
assert(X || Nk == 1);
#endif // _DEBUG
/*********************************************************************/
/* Allocate the storage that will contain the keys and the file */
/* positions corresponding to them. */
/*********************************************************************/
if ((n = Tdbp->GetMaxSize(g)) < 0)
return true;
else if (!n) {
Num_K = Ndif = 0;
MaxSame = 1;
// The if condition was suppressed because this may be an existing
// index that is now void because all table lines were deleted.
// if (sxp)
goto nox; // Truncate eventually existing index file
// else
// return false;
} // endif n
// File position must be stored
Record.Size = n * sizeof(int);
if (!PlgDBalloc(g, NULL, Record)) {
sprintf(g->Message, MSG(MEM_ALLOC_ERR), "index", n);
goto err; // Error
} // endif
/*********************************************************************/
/* Allocate the KXYCOL blocks used to store column values. */
/*********************************************************************/
for (k = 0; k < Nk; k++) {
colp = To_Cols[k];
if (!kdfp) {
sprintf(g->Message, MSG(INT_COL_ERROR),
(colp) ? colp->GetName() : "???");
goto err; // Error
} // endif kdfp
kcp = new(g) KXYCOL(this);
if (kcp->Init(g, colp, n, true, kdfp->Klen))
goto err; // Error
if (prev) {
kcp->Previous = prev;
prev->Next = kcp;
} else
To_KeyCol = kcp;
prev = kcp;
kdfp = kdfp->Next;
} // endfor k
To_LastCol = prev;
if (AddColumns(sxp)) {
PCOL kolp = To_Cols[0]; // Temporary while imposing Nk = 1
i = 0;
// Allocate the accompanying
for (colp = Tbxp->GetColumns(); colp; colp = colp->GetNext()) {
// Count how many columns to add
// for (k = 0; k < Nk; k++)
// if (colp == To_Cols[k])
// break;
// if (k == nk)
if (colp != kolp)
i++;
} // endfor colp
if (i && i < 10) // Should be a parameter
for (colp = Tbxp->GetColumns(); colp; colp = colp->GetNext()) {
// for (k = 0; k < Nk; k++)
// if (colp == To_Cols[k])
// break;
// if (k < nk)
if (colp == kolp)
continue; // This is a key column
kcp = new(g) KXYCOL(this);
if (kcp->Init(g, colp, n, true, 0))
return true;
if (trace)
htrc("Adding colp=%p Buf_Type=%d size=%d\n",
colp, colp->GetResultType(), n);
nk++;
prev->Next = kcp;
prev = kcp;
} // endfor colp
} // endif AddColumns
#if 0
/*********************************************************************/
/* Get the starting information for progress. */
/*********************************************************************/
dup->Step = (char*)PlugSubAlloc(g, NULL, 128);
sprintf((char*)dup->Step, MSG(BUILD_INDEX), Xdp->GetName(), Tdbp->Name);
dup->ProgMax = Tdbp->GetProgMax(g);
dup->ProgCur = 0;
#endif // 0
/*********************************************************************/
/* Standard init: read the file and construct the index table. */
/* Note: reading will be sequential as To_Kindex is not set. */
/*********************************************************************/
for (i = nkey = 0; rc != RC_EF; i++) {
#if 0
if (!dup->Step) {
strcpy(g->Message, MSG(QUERY_CANCELLED));
longjmp(g->jumper[g->jump_level], 99);
} // endif Step
#endif // 0
/*******************************************************************/
/* Read a valid record from table file. */
/*******************************************************************/
rc = Tdbp->ReadDB(g);
// Update progress information
// dup->ProgCur = Tdbp->GetProgCur();
// Check return code and do whatever must be done according to it
switch (rc) {
case RC_OK:
if (ApplyFilter(g, filp))
break;
// passthru
case RC_NF:
continue;
case RC_EF:
goto end_of_file;
default:
sprintf(g->Message, MSG(RC_READING), rc, Tdbp->Name);
goto err;
} // endswitch rc
/*******************************************************************/
/* Get and Store the file position of the last read record for */
/* future direct access. */
/*******************************************************************/
if (nkey == n) {
sprintf(g->Message, MSG(TOO_MANY_KEYS), nkey);
return true;
} else
To_Rec[nkey] = Tdbp->GetRecpos();
/*******************************************************************/
/* Get the keys and place them in the key blocks. */
/*******************************************************************/
for (k = 0, kcp = To_KeyCol;
k < nk && kcp;
k++, kcp = kcp->Next) {
// colp = To_Cols[k];
colp = kcp->Colp;
if (!colp->GetStatus(BUF_READ))
colp->ReadColumn(g);
else
colp->Reset();
kcp->SetValue(colp, nkey);
} // endfor k
nkey++; // A new valid key was found
} // endfor i
end_of_file:
// Update progress information
//dup->ProgCur = Tdbp->GetProgMax(g);
/*********************************************************************/
/* Record the Index size and eventually resize memory allocation. */
/*********************************************************************/
if ((Num_K = nkey) < n) {
PlgDBrealloc(g, NULL, Record, Num_K * sizeof(int));
for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
kcp->ReAlloc(g, Num_K);
} // endif Num_K
/*********************************************************************/
/* Sort the index so we can use an optimized Find algorithm. */
/* Note: for a unique index we use the non conservative sort */
/* version because normally all index values are different. */
/* This was set at CSORT class construction. */
/* For all indexes, an offset array is made so we can check the */
/* uniqueness of unique indexes. */
/*********************************************************************/
Index.Size = Num_K * sizeof(int);
if (!PlgDBalloc(g, NULL, Index)) {
sprintf(g->Message, MSG(MEM_ALLOC_ERR), "index", Num_K);
goto err; // Error
} // endif alloc
Offset.Size = (Num_K + 1) * sizeof(int);
if (!PlgDBalloc(g, NULL, Offset)) {
sprintf(g->Message, MSG(MEM_ALLOC_ERR), "offset", Num_K + 1);
goto err; // Error
} // endif alloc
// We must separate keys and added columns before sorting
addcolp = To_LastCol->Next;
To_LastCol->Next = NULL;
// Call the sort program, it returns the number of distinct values
if ((Ndif = Qsort(g, Num_K)) < 0)
goto err; // Error during sort
if (trace)
htrc("Make: Nk=%d n=%d Num_K=%d Ndif=%d addcolp=%p BlkFil=%p X=%p\n",
Nk, n, Num_K, Ndif, addcolp, Tdbp->To_BlkFil, X);
// Check whether the unique index is unique indeed
if (!Mul)
if (Ndif < Num_K) {
strcpy(g->Message, MSG(INDEX_NOT_UNIQ));
brc = true;
goto err;
} else
PlgDBfree(Offset); // Not used anymore
// Restore kcp list
To_LastCol->Next = addcolp;
// Use the index to physically reorder the xindex
Srtd = Reorder(g);
if (Ndif < Num_K) {
// Resize the offset array
PlgDBrealloc(g, NULL, Offset, (Ndif + 1) * sizeof(int));
// Initial value of MaxSame
MaxSame = Pof[1] - Pof[0];
// Resize the Key array by only keeping the distinct values
for (i = 1; i < Ndif; i++) {
for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
kcp->Move(i, Pof[i]);
MaxSame = MY_MAX(MaxSame, Pof[i + 1] - Pof[i]);
} // endfor i
for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
kcp->ReAlloc(g, Ndif);
} else {
Mul = false; // Current index is unique
PlgDBfree(Offset); // Not used anymore
MaxSame = 1; // Reset it when remaking an index
} // endif Ndif
/*********************************************************************/
/* Now do the reduction of the index. Indeed a multi-column index */
/* can be used for only some of the first columns. For instance if */
/* an index is defined for column A, B, C PlugDB can use it for */
/* only the column A or the columns A, B. */
/* What we do here is to reduce the data so column A will contain */
/* only the sorted distinct values of A, B will contain data such */
/* as only distinct values of A,B are stored etc. */
/* This implies that for each column set an offset array is made */
/* except if the subset originally contains unique values. */
/*********************************************************************/
// Update progress information
//dup->Step = STEP(REDUCE_INDEX);
ndf = Ndif;
To_LastCol->Mxs = MaxSame;
for (kcp = To_LastCol->Previous; kcp; kcp = kcp->Previous) {
if (!(bof = kcp->MakeOffset(g, ndf)))
goto err;
else
*bof = 0;
for (n = 0, i = j = 1; i < ndf; i++)
for (kp = kcp; kp; kp = kp->Previous)
if (kp->Compare(n, i)) {
// Values are not equal to last ones
bof[j++] = n = i;
break;
} // endif Compare
if (j < ndf) {
// Sub-index is multiple
bof[j] = ndf;
ndf = j; // New number of distinct values
// Resize the Key array by only keeping the distinct values
for (kp = kcp; kp; kp = kp->Previous) {
for (i = 1; i < ndf; i++)
kp->Move(i, bof[i]);
kp->ReAlloc(g, ndf);
} // endif kcp
// Resize the offset array
kcp->MakeOffset(g, ndf);
// Calculate the max same value for this column
kcp->Mxs = ColMaxSame(kcp);
} else {
// Current sub-index is unique
kcp->MakeOffset(g, 0); // The offset is not used anymore
kcp->Mxs = 1; // Unique
} // endif j
} // endfor kcp
/*********************************************************************/
/* For sorted columns and fixed record size, file position can be */
/* calculated, so the Record array can be discarted. */
/* Not true for DBF tables because of eventual soft deleted lines. */
/* Note: for Num_K = 1 any non null value is Ok. */
/*********************************************************************/
if (Srtd && !filp && Tdbp->Ftype != RECFM_VAR
&& Tdbp->Txfp->GetAmType() != TYPE_AM_DBF) {
Incr = (Num_K > 1) ? To_Rec[1] : Num_K;
PlgDBfree(Record);
} // endif Srtd
/*********************************************************************/
/* Check whether a two-tier find algorithm can be implemented. */
/* It is currently implemented only for single key indexes. */
/*********************************************************************/
if (Nk == 1 && ndf >= 65536) {
// Implement a two-tier find algorithm
for (Sblk = 256; (Sblk * Sblk * 4) < ndf; Sblk *= 2) ;
Nblk = (ndf -1) / Sblk + 1;
if (To_KeyCol->MakeBlockArray(g, Nblk, Sblk))
goto err; // Error
} // endif Num_K
nox:
/*********************************************************************/
/* No valid record read yet for secondary file. */
/*********************************************************************/
Cur_K = Num_K;
/*********************************************************************/
/* Save the xindex so it has not to be recalculated. */
/*********************************************************************/
if (X) {
if (SaveIndex(g, sxp))
brc = true;
} else { // Dynamic index
// Indicate that key column values can be found from KEYCOL's
for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
kcp->Colp->SetKcol(kcp);
Tdbp->SetFilter(NULL); // Not used anymore
} // endif X
err:
// We don't need the index anymore
if (X || brc)
Close();
if (brc)
printf("%s\n", g->Message);
return brc;
} // end of Make
/***********************************************************************/
/* Return the max size of the intermediate column. */
/***********************************************************************/
int XINDEX::ColMaxSame(PXCOL kp)
{
int *kof, i, ck1, ck2, ckn = 1;
PXCOL kcp;
// Calculate the max same value for this column
for (i = 0; i < kp->Ndf; i++) {
ck1 = i;
ck2 = i + 1;
for (kcp = kp; kcp; kcp = kcp->Next) {
if (!(kof = (kcp->Next) ? kcp->Kof : Pof))
break;
ck1 = kof[ck1];
ck2 = kof[ck2];
} // endfor kcp
ckn = MY_MAX(ckn, ck2 - ck1);
} // endfor i
return ckn;
} // end of ColMaxSame
/***********************************************************************/
/* Reorder: use the sort index to reorder the data in storage so */
/* it will be physically sorted and sort index can be removed. */
/***********************************************************************/
bool XINDEX::Reorder(PGLOBAL g)
{
register int i, j, k, n;
bool sorted = true;
PXCOL kcp;
#if 0
PDBUSER dup = (PDBUSER)g->Activityp->Aptr;
if (Num_K > 500000) {
// Update progress information
dup->Step = STEP(REORDER_INDEX);
dup->ProgMax = Num_K;
dup->ProgCur = 0;
} else
dup = NULL;
#endif // 0
if (!Pex)
return Srtd;
for (i = 0; i < Num_K; i++) {
if (Pex[i] == Num_K) { // Already moved
continue;
} else if (Pex[i] == i) { // Already placed
// if (dup)
// dup->ProgCur++;
continue;
} // endif's Pex
sorted = false;
for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
kcp->Save(i);
n = To_Rec[i];
for (j = i;; j = k) {
k = Pex[j];
Pex[j] = Num_K; // Mark position as set
if (k == i) {
for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
kcp->Restore(j);
To_Rec[j] = n;
break; // end of loop
} else {
for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
kcp->Move(j, k); // Move k to j
To_Rec[j] = To_Rec[k];
} // endif k
// if (dup)
// dup->ProgCur++;
} // endfor j
} // endfor i
// The index is not used anymore
PlgDBfree(Index);
return sorted;
} // end of Reorder
/***********************************************************************/
/* Save the index values for this table. */
/* The problem here is to avoid name duplication, because more than */
/* one data file can have the same name (but different types) and/or */
/* the same data file can be used with different block sizes. This is */
/* why we use Ofn that defaults to the file name but can be set to a */
/* different name if necessary. */
/***********************************************************************/
bool XINDEX::SaveIndex(PGLOBAL g, PIXDEF sxp)
{
char *ftype;
char fn[_MAX_PATH];
int n[NZ], nof = (Mul) ? (Ndif + 1) : 0;
int id = -1, size = 0;
bool sep, rc = false;
PXCOL kcp = To_KeyCol;
PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
//PDBUSER dup = PlgGetUser(g);
//dup->Step = STEP(SAVING_INDEX);
//dup->ProgMax = 15 + 16 * Nk;
//dup->ProgCur = 0;
switch (Tdbp->Ftype) {
case RECFM_VAR: ftype = ".dnx"; break;
case RECFM_FIX: ftype = ".fnx"; break;
case RECFM_BIN: ftype = ".bnx"; break;
case RECFM_VCT: ftype = ".vnx"; break;
case RECFM_DBF: ftype = ".dbx"; break;
default:
sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
return true;
} // endswitch Ftype
if ((sep = defp->GetBoolCatInfo("SepIndex", false))) {
// Index is saved in a separate file
#if !defined(UNIX)
char drive[_MAX_DRIVE];
#else
char *drive = NULL;
#endif
char direc[_MAX_DIR];
char fname[_MAX_FNAME];
_splitpath(defp->GetOfn(), drive, direc, fname, NULL);
strcat(strcat(fname, "_"), Xdp->GetName());
_makepath(fn, drive, direc, fname, ftype);
sxp = NULL;
} else {
id = ID;
strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
} // endif sep
PlugSetPath(fn, fn, Tdbp->GetPath());
if (X->Open(g, fn, id, (sxp) ? MODE_INSERT : MODE_WRITE)) {
printf("%s\n", g->Message);
return true;
} // endif Open
if (!Ndif)
goto end; // Void index
/*********************************************************************/
/* Write the index values on the index file. */
/*********************************************************************/
n[0] = ID + MAX_INDX; // To check validity
n[1] = Nk; // The number of indexed columns
n[2] = nof; // The offset array size or 0
n[3] = Num_K; // The index size
n[4] = Incr; // Increment of record positions
n[5] = Nblk; n[6] = Sblk;
n[7] = Srtd ? 1 : 0; // Values are sorted in the file
if (trace) {
htrc("Saving index %s\n", Xdp->GetName());
htrc("ID=%d Nk=%d nof=%d Num_K=%d Incr=%d Nblk=%d Sblk=%d Srtd=%d\n",
ID, Nk, nof, Num_K, Incr, Nblk, Sblk, Srtd);
} // endif trace
size = X->Write(g, n, NZ, sizeof(int), rc);
//dup->ProgCur = 1;
if (Mul) // Write the offset array
size += X->Write(g, Pof, nof, sizeof(int), rc);
//dup->ProgCur = 5;
if (!Incr) // Write the record position array(s)
size += X->Write(g, To_Rec, Num_K, sizeof(int), rc);
//dup->ProgCur = 15;
for (; kcp; kcp = kcp->Next) {
n[0] = kcp->Ndf; // Number of distinct sub-values
n[1] = (kcp->Kof) ? kcp->Ndf + 1 : 0; // 0 if unique
n[2] = (kcp == To_KeyCol) ? Nblk : 0;
n[3] = kcp->Klen; // To be checked later
n[4] = kcp->Type; // To be checked later
size += X->Write(g, n, NW, sizeof(int), rc);
// dup->ProgCur += 1;
if (n[2])
size += X->Write(g, kcp->To_Bkeys, Nblk, kcp->Klen, rc);
// dup->ProgCur += 5;
size += X->Write(g, kcp->To_Keys, n[0], kcp->Klen, rc);
// dup->ProgCur += 5;
if (n[1])
size += X->Write(g, kcp->Kof, n[1], sizeof(int), rc);
// dup->ProgCur += 5;
} // endfor kcp
if (trace)
htrc("Index %s saved, Size=%d\n", Xdp->GetName(), size);
end:
X->Close(fn, id);
return rc;
} // end of SaveIndex
/***********************************************************************/
/* Init: Open and Initialize a Key Index. */
/***********************************************************************/
bool XINDEX::Init(PGLOBAL g)
{
#if defined(XMAP)
if (xmap)
return MapInit(g);
#endif // XMAP
/*********************************************************************/
/* Table will be accessed through an index table. */
/* If sorting is required, this will be done later. */
/*********************************************************************/
char *ftype;
char fn[_MAX_PATH];
int k, n, nv[NZ], id = -1;
bool estim = false;
PCOL colp;
PXCOL prev = NULL, kcp = NULL;
PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
/*********************************************************************/
/* Get the estimated table size. */
/* Note: for fixed tables we must use cardinality to avoid the call */
/* to MaxBlkSize that could reduce the cardinality value. */
/*********************************************************************/
if (Tdbp->Cardinality(NULL)) {
// For DBF tables, Cardinality includes bad or soft deleted lines
// that are not included in the index, and can be larger then the
// index size.
estim = (Tdbp->Ftype == RECFM_DBF);
n = Tdbp->Cardinality(g); // n is exact table size
} else {
// Variable table not optimized
estim = true; // n is an estimate of the size
n = Tdbp->GetMaxSize(g);
} // endif Cardinality
if (n <= 0)
return !(n == 0); // n < 0 error, n = 0 void table
/*********************************************************************/
/* Get the first key column. */
/*********************************************************************/
if (!Nk || !To_Cols || (!To_Vals && Op != OP_FIRST && Op != OP_FSTDIF)) {
strcpy(g->Message, MSG(NO_KEY_COL));
return true; // Error
} else
colp = To_Cols[0];
switch (Tdbp->Ftype) {
case RECFM_VAR: ftype = ".dnx"; break;
case RECFM_FIX: ftype = ".fnx"; break;
case RECFM_BIN: ftype = ".bnx"; break;
case RECFM_VCT: ftype = ".vnx"; break;
case RECFM_DBF: ftype = ".dbx"; break;
default:
sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
return true;
} // endswitch Ftype
if (defp->SepIndex()) {
// Index was saved in a separate file
#if !defined(UNIX)
char drive[_MAX_DRIVE];
#else
char *drive = NULL;
#endif
char direc[_MAX_DIR];
char fname[_MAX_FNAME];
_splitpath(defp->GetOfn(), drive, direc, fname, NULL);
strcat(strcat(fname, "_"), Xdp->GetName());
_makepath(fn, drive, direc, fname, ftype);
} else {
id = ID;
strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
} // endif sep
PlugSetPath(fn, fn, Tdbp->GetPath());
if (trace)
htrc("Index %s file: %s\n", Xdp->GetName(), fn);
/*********************************************************************/
/* Open the index file and check its validity. */
/*********************************************************************/
if (X->Open(g, fn, id, MODE_READ))
goto err; // No saved values
// Now start the reading process.
if (X->Read(g, nv, NZ - 1, sizeof(int)))
goto err;
if (nv[0] >= MAX_INDX) {
// New index format
if (X->Read(g, nv + 7, 1, sizeof(int)))
goto err;
Srtd = nv[7] != 0;
nv[0] -= MAX_INDX;
} else
Srtd = false;
if (trace)
htrc("nv=%d %d %d %d %d %d %d (%d)\n",
nv[0], nv[1], nv[2], nv[3], nv[4], nv[5], nv[6], Srtd);
// The test on ID was suppressed because MariaDB can change an index ID
// when other indexes are added or deleted
if (/*nv[0] != ID ||*/ nv[1] != Nk) {
sprintf(g->Message, MSG(BAD_INDEX_FILE), fn);
if (trace)
htrc("nv[0]=%d ID=%d nv[1]=%d Nk=%d\n", nv[0], ID, nv[1], Nk);
goto err;
} // endif
if (nv[2]) {
Mul = true;
Ndif = nv[2];
// Allocate the storage that will contain the offset array
Offset.Size = Ndif * sizeof(int);
if (!PlgDBalloc(g, NULL, Offset)) {
sprintf(g->Message, MSG(MEM_ALLOC_ERR), "offset", Ndif);
goto err;
} // endif
if (X->Read(g, Pof, Ndif, sizeof(int)))
goto err;
Ndif--; // nv[2] is offset size, equal to Ndif + 1
} else {
Mul = false;
Ndif = nv[3];
} // endif nv[2]
if (nv[3] < n && estim)
n = nv[3]; // n was just an evaluated max value
if (nv[3] != n) {
sprintf(g->Message, MSG(OPT_NOT_MATCH), fn);
goto err;
} // endif
Num_K = nv[3];
Incr = nv[4];
Nblk = nv[5];
Sblk = nv[6];
if (!Incr) {
/*******************************************************************/
/* Allocate the storage that will contain the file positions. */
/*******************************************************************/
Record.Size = Num_K * sizeof(int);
if (!PlgDBalloc(g, NULL, Record)) {
sprintf(g->Message, MSG(MEM_ALLOC_ERR), "index", Num_K);
goto err;
} // endif
if (X->Read(g, To_Rec, Num_K, sizeof(int)))
goto err;
} else
Srtd = true; // Sorted positions can be calculated
/*********************************************************************/
/* Allocate the KXYCOL blocks used to store column values. */
/*********************************************************************/
for (k = 0; k < Nk; k++) {
if (k == Nval)
To_LastVal = prev;
if (X->Read(g, nv, NW, sizeof(int)))
goto err;
colp = To_Cols[k];
if (nv[4] != colp->GetResultType() || !colp->GetValue() ||
(nv[3] != colp->GetValue()->GetClen() && nv[4] != TYPE_STRING)) {
sprintf(g->Message, MSG(XCOL_MISMATCH), colp->GetName());
goto err; // Error
} // endif GetKey
kcp = new(g) KXYCOL(this);
if (kcp->Init(g, colp, nv[0], true, (int)nv[3]))
goto err; // Error
/*******************************************************************/
/* Read the index values from the index file. */
/*******************************************************************/
if (k == 0 && Nblk) {
if (kcp->MakeBlockArray(g, Nblk, 0))
goto err;
// Read block values
if (X->Read(g, kcp->To_Bkeys, Nblk, kcp->Klen))
goto err;
} // endif Nblk
// Read the entire (small) index
if (X->Read(g, kcp->To_Keys, nv[0], kcp->Klen))
goto err;
if (nv[1]) {
if (!kcp->MakeOffset(g, nv[1] - 1))
goto err;
// Read the offset array
if (X->Read(g, kcp->Kof, nv[1], sizeof(int)))
goto err;
} // endif n[1]
if (!kcp->Prefix)
// Indicate that the key column value can be found from KXYCOL
colp->SetKcol(kcp);
if (prev) {
kcp->Previous = prev;
prev->Next = kcp;
} else
To_KeyCol = kcp;
prev = kcp;
} // endfor k
To_LastCol = prev;
if (Mul && prev) {
// Last key offset is the index offset
kcp->Koff = Offset;
kcp->Koff.Sub = true;
} // endif Mul
X->Close();
/*********************************************************************/
/* No valid record read yet for secondary file. */
/*********************************************************************/
Cur_K = Num_K;
return false;
err:
Close();
return true;
} // end of Init
#if defined(XMAP)
/***********************************************************************/
/* Init: Open and Initialize a Key Index. */
/***********************************************************************/
bool XINDEX::MapInit(PGLOBAL g)
{
/*********************************************************************/
/* Table will be accessed through an index table. */
/* If sorting is required, this will be done later. */
/*********************************************************************/
const char *ftype;
BYTE *mbase;
char fn[_MAX_PATH];
int *nv, k, n, id = -1;
bool estim;
PCOL colp;
PXCOL prev = NULL, kcp = NULL;
PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
PDBUSER dup = PlgGetUser(g);
/*********************************************************************/
/* Get the estimated table size. */
/* Note: for fixed tables we must use cardinality to avoid the call */
/* to MaxBlkSize that could reduce the cardinality value. */
/*********************************************************************/
if (Tdbp->Cardinality(NULL)) {
// For DBF tables, Cardinality includes bad or soft deleted lines
// that are not included in the index, and can be larger then the
// index size.
estim = (Tdbp->Ftype == RECFM_DBF);
n = Tdbp->Cardinality(g); // n is exact table size
} else {
// Variable table not optimized
estim = true; // n is an estimate of the size
n = Tdbp->GetMaxSize(g);
} // endif Cardinality
if (n <= 0)
return !(n == 0); // n < 0 error, n = 0 void table
/*********************************************************************/
/* Get the first key column. */
/*********************************************************************/
if (!Nk || !To_Cols || (!To_Vals && Op != OP_FIRST && Op != OP_FSTDIF)) {
strcpy(g->Message, MSG(NO_KEY_COL));
return true; // Error
} else
colp = To_Cols[0];
switch (Tdbp->Ftype) {
case RECFM_VAR: ftype = ".dnx"; break;
case RECFM_FIX: ftype = ".fnx"; break;
case RECFM_BIN: ftype = ".bnx"; break;
case RECFM_VCT: ftype = ".vnx"; break;
case RECFM_DBF: ftype = ".dbx"; break;
default:
sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
return true;
} // endswitch Ftype
if (defp->SepIndex()) {
// Index was save in a separate file
#if !defined(UNIX)
char drive[_MAX_DRIVE];
#else
char *drive = NULL;
#endif
char direc[_MAX_DIR];
char fname[_MAX_FNAME];
_splitpath(defp->GetOfn(), drive, direc, fname, NULL);
strcat(strcat(fname, "_"), Xdp->GetName());
_makepath(fn, drive, direc, fname, ftype);
} else {
id = ID;
strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
} // endif SepIndex
PlugSetPath(fn, fn, Tdbp->GetPath());
if (trace)
htrc("Index %s file: %s\n", Xdp->GetName(), fn);
/*********************************************************************/
/* Get a view on the part of the index file containing this index. */
/*********************************************************************/
if (!(mbase = (BYTE*)X->FileView(g, fn)))
goto err;
if (id >= 0) {
// Get offset from the header
IOFF *noff = (IOFF*)mbase;
// Position the memory base at the offset of this index
mbase += noff[id].Low;
} // endif id
// Now start the mapping process.
nv = (int*)mbase;
if (nv[0] >= MAX_INDX) {
// New index format
Srtd = nv[7] != 0;
nv[0] -= MAX_INDX;
mbase += NZ * sizeof(int);
} else {
Srtd = false;
mbase += (NZ - 1) * sizeof(int);
} // endif nv
if (trace)
htrc("nv=%d %d %d %d %d %d %d %d\n",
nv[0], nv[1], nv[2], nv[3], nv[4], nv[5], nv[6], Srtd);
// The test on ID was suppressed because MariaDB can change an index ID
// when other indexes are added or deleted
if (/*nv[0] != ID ||*/ nv[1] != Nk) {
// Not this index
sprintf(g->Message, MSG(BAD_INDEX_FILE), fn);
if (trace)
htrc("nv[0]=%d ID=%d nv[1]=%d Nk=%d\n", nv[0], ID, nv[1], Nk);
goto err;
} // endif nv
if (nv[2]) {
// Set the offset array memory block
Offset.Memp = mbase;
Offset.Size = nv[2] * sizeof(int);
Offset.Sub = true;
Mul = true;
Ndif = nv[2] - 1;
mbase += Offset.Size;
} else {
Mul = false;
Ndif = nv[3];
} // endif nv[2]
if (nv[3] < n && estim)
n = nv[3]; // n was just an evaluated max value
if (nv[3] != n) {
sprintf(g->Message, MSG(OPT_NOT_MATCH), fn);
goto err;
} // endif
Num_K = nv[3];
Incr = nv[4];
Nblk = nv[5];
Sblk = nv[6];
if (!Incr) {
/*******************************************************************/
/* Point to the storage that contains the file positions. */
/*******************************************************************/
Record.Size = Num_K * sizeof(int);
Record.Memp = mbase;
Record.Sub = true;
mbase += Record.Size;
} else
Srtd = true; // Sorted positions can be calculated
/*********************************************************************/
/* Allocate the KXYCOL blocks used to store column values. */
/*********************************************************************/
for (k = 0; k < Nk; k++) {
if (k == Nval)
To_LastVal = prev;
nv = (int*)mbase;
mbase += (NW * sizeof(int));
colp = To_Cols[k];
if (nv[4] != colp->GetResultType() || !colp->GetValue() ||
(nv[3] != colp->GetValue()->GetClen() && nv[4] != TYPE_STRING)) {
sprintf(g->Message, MSG(XCOL_MISMATCH), colp->GetName());
goto err; // Error
} // endif GetKey
kcp = new(g) KXYCOL(this);
if (!(mbase = kcp->MapInit(g, colp, nv, mbase)))
goto err;
if (!kcp->Prefix)
// Indicate that the key column value can be found from KXYCOL
colp->SetKcol(kcp);
if (prev) {
kcp->Previous = prev;
prev->Next = kcp;
} else
To_KeyCol = kcp;
prev = kcp;
} // endfor k
To_LastCol = prev;
if (Mul && prev)
// Last key offset is the index offset
kcp->Koff = Offset;
/*********************************************************************/
/* No valid record read yet for secondary file. */
/*********************************************************************/
Cur_K = Num_K;
return false;
err:
Close();
return true;
} // end of MapInit
#endif // XMAP
/***********************************************************************/
/* Get Ndif and Num_K from the index file. */
/***********************************************************************/
bool XINDEX::GetAllSizes(PGLOBAL g,/* int &ndif,*/ int &numk)
{
char *ftype;
char fn[_MAX_PATH];
int nv[NZ], id = -1; // n
//bool estim = false;
bool rc = true;
PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
// ndif = numk = 0;
numk = 0;
#if 0
/*********************************************************************/
/* Get the estimated table size. */
/* Note: for fixed tables we must use cardinality to avoid the call */
/* to MaxBlkSize that could reduce the cardinality value. */
/*********************************************************************/
if (Tdbp->Cardinality(NULL)) {
// For DBF tables, Cardinality includes bad or soft deleted lines
// that are not included in the index, and can be larger then the
// index size.
estim = (Tdbp->Ftype == RECFM_DBF);
n = Tdbp->Cardinality(g); // n is exact table size
} else {
// Variable table not optimized
estim = true; // n is an estimate of the size
n = Tdbp->GetMaxSize(g);
} // endif Cardinality
if (n <= 0)
return !(n == 0); // n < 0 error, n = 0 void table
/*********************************************************************/
/* Check the key part number. */
/*********************************************************************/
if (!Nk) {
strcpy(g->Message, MSG(NO_KEY_COL));
return true; // Error
} // endif Nk
#endif // 0
switch (Tdbp->Ftype) {
case RECFM_VAR: ftype = ".dnx"; break;
case RECFM_FIX: ftype = ".fnx"; break;
case RECFM_BIN: ftype = ".bnx"; break;
case RECFM_VCT: ftype = ".vnx"; break;
case RECFM_DBF: ftype = ".dbx"; break;
default:
sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
return true;
} // endswitch Ftype
if (defp->SepIndex()) {
// Index was saved in a separate file
#if !defined(UNIX)
char drive[_MAX_DRIVE];
#else
char *drive = NULL;
#endif
char direc[_MAX_DIR];
char fname[_MAX_FNAME];
_splitpath(defp->GetOfn(), drive, direc, fname, NULL);
strcat(strcat(fname, "_"), Xdp->GetName());
_makepath(fn, drive, direc, fname, ftype);
} else {
id = ID;
strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
} // endif sep
PlugSetPath(fn, fn, Tdbp->GetPath());
if (trace)
htrc("Index %s file: %s\n", Xdp->GetName(), fn);
/*********************************************************************/
/* Open the index file and check its validity. */
/*********************************************************************/
if (X->Open(g, fn, id, MODE_READ))
goto err; // No saved values
// Get offset from XDB file
//if (X->Seek(g, Defoff, Defhigh, SEEK_SET))
// goto err;
// Now start the reading process.
if (X->Read(g, nv, NZ, sizeof(int)))
goto err;
if (trace)
htrc("nv=%d %d %d %d\n", nv[0], nv[1], nv[2], nv[3]);
// The test on ID was suppressed because MariaDB can change an index ID
// when other indexes are added or deleted
if (/*nv[0] != ID ||*/ nv[1] != Nk) {
sprintf(g->Message, MSG(BAD_INDEX_FILE), fn);
if (trace)
htrc("nv[0]=%d ID=%d nv[1]=%d Nk=%d\n", nv[0], ID, nv[1], Nk);
goto err;
} // endif
#if 0
if (nv[2]) {
Mul = true;
Ndif = nv[2] - 1; // nv[2] is offset size, equal to Ndif + 1
} else {
Mul = false;
Ndif = nv[3];
} // endif nv[2]
if (nv[3] < n && estim)
n = nv[3]; // n was just an evaluated max value
if (nv[3] != n) {
sprintf(g->Message, MSG(OPT_NOT_MATCH), fn);
goto err;
} // endif
#endif // 0
Num_K = nv[3];
#if 0
if (Nk > 1) {
if (nv[2] && X->Seek(g, nv[2] * sizeof(int), 0, SEEK_CUR))
goto err;
if (!nv[4] && X->Seek(g, Num_K * sizeof(int), 0, SEEK_CUR))
goto err;
if (X->Read(g, nv, NW, sizeof(int)))
goto err;
PCOL colp = *To_Cols;
if (nv[4] != colp->GetResultType() ||
(nv[3] != colp->GetValue()->GetClen() && nv[4] != TYPE_STRING)) {
sprintf(g->Message, MSG(XCOL_MISMATCH), colp->GetName());
goto err; // Error
} // endif GetKey
Ndif = nv[0];
} // endif Nk
#endif // 0
/*********************************************************************/
/* Set size values. */
/*********************************************************************/
//ndif = Ndif;
numk = Num_K;
rc = false;
err:
X->Close();
return rc;
} // end of GetAllSizes
/***********************************************************************/
/* RANGE: Tell how many records exist for a given value, for an array */
/* of values, or in a given value range. */
/***********************************************************************/
int XINDEX::Range(PGLOBAL g, int limit, bool incl)
{
int i, k, n = 0;
PXOB *xp = To_Vals;
PXCOL kp = To_KeyCol;
OPVAL op = Op;
switch (limit) {
case 1: Op = (incl) ? OP_GE : OP_GT; break;
case 2: Op = (incl) ? OP_GT : OP_GE; break;
default: return 0;
} // endswitch limit
/*********************************************************************/
/* Currently only range of constant values with an EQ operator is */
/* implemented. Find the number of rows for each given values. */
/*********************************************************************/
if (xp[0]->GetType() == TYPE_CONST) {
for (i = 0; kp; kp = kp->Next) {
kp->Valp->SetValue_pval(xp[i]->GetValue(), !kp->Prefix);
if (++i == Nval) break;
} // endfor kp
if ((k = FastFind(Nval)) < Num_K)
n = k;
// if (limit)
// n = (Mul) ? k : kp->Val_K;
// else
// n = (Mul) ? Pof[kp->Val_K + 1] - k : 1;
} else {
strcpy(g->Message, MSG(RANGE_NO_JOIN));
n = -1; // Logical error
} // endif'f Type
Op = op;
return n;
} // end of Range
/***********************************************************************/
/* Return the size of the group (equal values) of the current value. */
/***********************************************************************/
int XINDEX::GroupSize(void)
{
#if defined(_DEBUG)
assert(To_LastCol->Val_K >= 0 && To_LastCol->Val_K < Ndif);
#endif // _DEBUG
if (Nval == Nk)
return (Pof) ? Pof[To_LastCol->Val_K + 1] - Pof[To_LastCol->Val_K]
: 1;
#if defined(_DEBUG)
assert(To_LastVal);
#endif // _DEBUG
// Index whose only some columns are used
int ck1, ck2;
ck1 = To_LastVal->Val_K;
ck2 = ck1 + 1;
#if defined(_DEBUG)
assert(ck1 >= 0 && ck1 < To_LastVal->Ndf);
#endif // _DEBUG
for (PXCOL kcp = To_LastVal; kcp; kcp = kcp->Next) {
ck1 = (kcp->Kof) ? kcp->Kof[ck1] : ck1;
ck2 = (kcp->Kof) ? kcp->Kof[ck2] : ck2;
} // endfor kcp
return ck2 - ck1;
} // end of GroupSize
/***********************************************************************/
/* Find Cur_K and Val_K's of the next distinct value of the index. */
/* Returns false if Ok, true if there are no more different values. */
/***********************************************************************/
bool XINDEX::NextValDif(void)
{
int curk;
PXCOL kcp = (To_LastVal) ? To_LastVal : To_LastCol;
if (++kcp->Val_K < kcp->Ndf) {
Cur_K = curk = kcp->Val_K;
// (Cur_K return is currently not used by SQLGBX)
for (PXCOL kp = kcp; kp; kp = kp->Next)
Cur_K = (kp->Kof) ? kp->Kof[Cur_K] : Cur_K;
} else
return true;
for (kcp = kcp->Previous; kcp; kcp = kcp->Previous) {
if (kcp->Kof && curk < kcp->Kof[kcp->Val_K + 1])
break; // all previous columns have same value
curk = ++kcp->Val_K; // This is a break, get new column value
} // endfor kcp
return false;
} // end of NextValDif
/***********************************************************************/
/* XINDEX: Find Cur_K and Val_K's of next index entry. */
/* If eq is true next values must be equal to last ones up to Nval. */
/* Returns false if Ok, true if there are no more (equal) values. */
/***********************************************************************/
bool XINDEX::NextVal(bool eq)
{
int n, neq = Nk + 1, curk;
PXCOL kcp;
if (Cur_K == Num_K)
return true;
else
curk = ++Cur_K;
for (n = Nk, kcp = To_LastCol; kcp; n--, kcp = kcp->Previous) {
if (kcp->Kof) {
if (curk == kcp->Kof[kcp->Val_K + 1])
neq = n;
} else {
#ifdef _DEBUG
assert(curk == kcp->Val_K + 1);
#endif // _DEBUG
neq = n;
} // endif Kof
#ifdef _DEBUG
assert(kcp->Val_K < kcp->Ndf);
#endif // _DEBUG
// If this is not a break...
if (neq > n)
break; // all previous columns have same value
curk = ++kcp->Val_K; // This is a break, get new column value
} // endfor kcp
// Return true if no more values or, in case of "equal" values,
// if the last used column value has changed
return (Cur_K == Num_K || (eq && neq <= Nval));
} // end of NextVal
/***********************************************************************/
/* XINDEX: Find Cur_K and Val_K's of previous index entry. */
/* Returns false if Ok, true if there are no more values. */
/***********************************************************************/
bool XINDEX::PrevVal(void)
{
int n, neq = Nk + 1, curk;
PXCOL kcp;
if (Cur_K == 0)
return true;
else
curk = --Cur_K;
for (n = Nk, kcp = To_LastCol; kcp; n--, kcp = kcp->Previous) {
if (kcp->Kof) {
if (curk < kcp->Kof[kcp->Val_K])
neq = n;
} else {
#ifdef _DEBUG
assert(curk == kcp->Val_K -1);
#endif // _DEBUG
neq = n;
} // endif Kof
#ifdef _DEBUG
assert(kcp->Val_K >= 0);
#endif // _DEBUG
// If this is not a break...
if (neq > n)
break; // all previous columns have same value
curk = --kcp->Val_K; // This is a break, get new column value
} // endfor kcp
return false;
} // end of PrevVal
/***********************************************************************/
/* XINDEX: Fetch a physical or logical record. */
/***********************************************************************/
int XINDEX::Fetch(PGLOBAL g)
{
int n;
PXCOL kp;
if (Num_K == 0)
return -1; // means end of file
/*********************************************************************/
/* Table read through a sorted index. */
/*********************************************************************/
switch (Op) {
case OP_NEXT: // Read next
if (NextVal(false))
return -1; // End of indexed file
break;
case OP_FIRST: // Read first
for (Cur_K = 0, kp = To_KeyCol; kp; kp = kp->Next)
kp->Val_K = 0;
Op = OP_NEXT;
break;
case OP_SAME: // Read next same
// Logically the key values should be the same as before
if (trace > 1)
htrc("looking for next same value\n");
if (NextVal(true)) {
Op = OP_EQ;
return -2; // no more equal values
} // endif NextVal
break;
case OP_NXTDIF: // Read next dif
// while (!NextVal(true)) ;
// if (Cur_K >= Num_K)
// return -1; // End of indexed file
if (NextValDif())
return -1; // End of indexed file
break;
case OP_FSTDIF: // Read first diff
for (Cur_K = 0, kp = To_KeyCol; kp; kp = kp->Next)
kp->Val_K = 0;
Op = (Mul || Nval < Nk) ? OP_NXTDIF : OP_NEXT;
break;
case OP_LAST: // Read last key
for (Cur_K = Num_K - 1, kp = To_KeyCol; kp; kp = kp->Next)
kp->Val_K = kp->Kblp->GetNval() - 1;
Op = OP_NEXT;
break;
case OP_PREV: // Read previous
if (PrevVal())
return -1; // End of indexed file
break;
default: // Should be OP_EQ
// if (Tbxp->Key_Rank < 0) {
/***************************************************************/
/* Look for the first key equal to the link column values */
/* and return its rank whithin the index table. */
/***************************************************************/
for (n = 0, kp = To_KeyCol; n < Nval && kp; n++, kp = kp->Next)
if (kp->InitFind(g, To_Vals[n]))
return -1; // No more constant values
Nth++;
if (trace > 1)
htrc("Fetch: Looking for new value\n");
Cur_K = FastFind(Nval);
if (Cur_K >= Num_K)
/*************************************************************/
/* Rank not whithin index table, signal record not found. */
/*************************************************************/
return -2;
else if (Mul || Nval < Nk)
Op = OP_SAME;
} // endswitch Op
/*********************************************************************/
/* If rank is equal to stored rank, record is already there. */
/*********************************************************************/
if (Cur_K == Old_K)
return -3; // Means record already there
else
Old_K = Cur_K; // Store rank of newly read record
/*********************************************************************/
/* Return the position of the required record. */
/*********************************************************************/
return (Incr) ? Cur_K * Incr : To_Rec[Cur_K];
} // end of Fetch
/***********************************************************************/
/* FastFind: Returns the index of matching record in a join using an */
/* optimized algorithm based on dichotomie and optimized comparing. */
/***********************************************************************/
int XINDEX::FastFind(int nv)
{
register int curk, sup, inf, i= 0, k, n = 2;
register PXCOL kp, kcp;
assert((int)nv == Nval);
if (Nblk && Op == OP_EQ) {
// Look in block values to find in which block to search
sup = Nblk;
inf = -1;
while (n && sup - inf > 1) {
i = (inf + sup) >> 1;
n = To_KeyCol->CompBval(i);
if (n < 0)
sup = i;
else
inf = i;
} // endwhile
if (inf < 0)
return Num_K;
// i = inf;
inf *= Sblk;
if ((sup = inf + Sblk) > To_KeyCol->Ndf)
sup = To_KeyCol->Ndf;
inf--;
} else {
inf = -1;
sup = To_KeyCol->Ndf;
} // endif Nblk
for (k = 0, kcp = To_KeyCol; kcp; kcp = kcp->Next) {
while (sup - inf > 1) {
i = (inf + sup) >> 1;
n = kcp->CompVal(i);
if (n < 0)
sup = i;
else if (n > 0)
inf = i;
else
break;
} // endwhile
if (n) {
if (Op != OP_EQ) {
// Currently only OP_GT or OP_GE
kcp->Val_K = curk = sup;
// Check for value changes in previous key parts
for (kp = kcp->Previous; kp; kp = kp->Previous)
if (kp->Kof && curk < kp->Kof[kp->Val_K + 1])
break;
else
curk = ++kp->Val_K;
n = 0;
} // endif Op
break;
} // endif n
kcp->Val_K = i;
if (++k == Nval) {
if (Op == OP_GT) { // n is always 0
curk = ++kcp->Val_K; // Increment value by 1
// Check for value changes in previous key parts
for (kp = kcp->Previous; kp; kp = kp->Previous)
if (kp->Kof && curk < kp->Kof[kp->Val_K + 1])
break; // Not changed
else
curk = ++kp->Val_K;
} // endif Op
break; // So kcp remains pointing the last tested block
} // endif k
if (kcp->Kof) {
inf = kcp->Kof[i] - 1;
sup = kcp->Kof[i + 1];
} else {
inf = i - 1;
sup = i + 1;
} // endif Kof
} // endfor k, kcp
if (n) {
// Record not found
for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
kcp->Val_K = kcp->Ndf; // Not a valid value
return Num_K;
} // endif n
for (curk = kcp->Val_K; kcp; kcp = kcp->Next) {
kcp->Val_K = curk;
curk = (kcp->Kof) ? kcp->Kof[kcp->Val_K] : kcp->Val_K;
} // endfor kcp
return curk;
} // end of FastFind
/* -------------------------- XINDXS Class --------------------------- */
/***********************************************************************/
/* XINDXS public constructor. */
/***********************************************************************/
XINDXS::XINDXS(PTDBDOS tdbp, PIXDEF xdp, PXLOAD pxp, PCOL *cp, PXOB *xp)
: XINDEX(tdbp, xdp, pxp, cp, xp)
{
Srtd = To_Cols[0]->GetOpt() == 2;
} // end of XINDXS constructor
/***********************************************************************/
/* XINDXS compare routine for C Quick/Insertion sort. */
/***********************************************************************/
int XINDXS::Qcompare(int *i1, int *i2)
{
//num_comp++;
return To_KeyCol->Compare(*i1, *i2);
} // end of Qcompare
/***********************************************************************/
/* Range: Tell how many records exist for given value(s): */
/* If limit=0 return range for these values. */
/* If limit=1 return the start of range. */
/* If limit=2 return the end of range. */
/***********************************************************************/
int XINDXS::Range(PGLOBAL g, int limit, bool incl)
{
int k, n = 0;
PXOB xp = To_Vals[0];
PXCOL kp = To_KeyCol;
OPVAL op = Op;
switch (limit) {
case 1: Op = (incl) ? OP_GE : OP_GT; break;
case 2: Op = (incl) ? OP_GT : OP_GE; break;
default: Op = OP_EQ;
} // endswitch limit
/*********************************************************************/
/* Currently only range of constant values with an EQ operator is */
/* implemented. Find the number of rows for each given values. */
/*********************************************************************/
if (xp->GetType() == TYPE_CONST) {
kp->Valp->SetValue_pval(xp->GetValue(), !kp->Prefix);
k = FastFind(Nval);
if (k < Num_K || Op != OP_EQ)
if (limit)
n = (Mul) ? k : kp->Val_K;
else
n = (Mul) ? Pof[kp->Val_K + 1] - k : 1;
} else {
strcpy(g->Message, MSG(RANGE_NO_JOIN));
n = -1; // Logical error
} // endif'f Type
Op = op;
return n;
} // end of Range
/***********************************************************************/
/* Return the size of the group (equal values) of the current value. */
/***********************************************************************/
int XINDXS::GroupSize(void)
{
#if defined(_DEBUG)
assert(To_KeyCol->Val_K >= 0 && To_KeyCol->Val_K < Ndif);
#endif // _DEBUG
return (Pof) ? Pof[To_KeyCol->Val_K + 1] - Pof[To_KeyCol->Val_K]
: 1;
} // end of GroupSize
/***********************************************************************/
/* XINDXS: Find Cur_K and Val_K of previous index value. */
/* Returns false if Ok, true if there are no more values. */
/***********************************************************************/
bool XINDXS::PrevVal(void)
{
if (--Cur_K < 0)
return true;
if (Mul) {
if (Cur_K < Pof[To_KeyCol->Val_K])
To_KeyCol->Val_K--;
} else
To_KeyCol->Val_K = Cur_K;
return false;
} // end of PrevVal
/***********************************************************************/
/* XINDXS: Find Cur_K and Val_K of next index value. */
/* If b is true next value must be equal to last one. */
/* Returns false if Ok, true if there are no more (equal) values. */
/***********************************************************************/
bool XINDXS::NextVal(bool eq)
{
bool rc;
if (To_KeyCol->Val_K == Ndif)
return true;
if (Mul) {
int limit = Pof[To_KeyCol->Val_K + 1];
#ifdef _DEBUG
assert(Cur_K < limit);
assert(To_KeyCol->Val_K < Ndif);
#endif // _DEBUG
if (++Cur_K == limit) {
To_KeyCol->Val_K++;
rc = (eq || limit == Num_K);
} else
rc = false;
} else
rc = (To_KeyCol->Val_K = ++Cur_K) == Num_K || eq;
return rc;
} // end of NextVal
/***********************************************************************/
/* XINDXS: Fetch a physical or logical record. */
/***********************************************************************/
int XINDXS::Fetch(PGLOBAL g)
{
if (Num_K == 0)
return -1; // means end of file
/*********************************************************************/
/* Table read through a sorted index. */
/*********************************************************************/
switch (Op) {
case OP_NEXT: // Read next
if (NextVal(false))
return -1; // End of indexed file
break;
case OP_FIRST: // Read first
To_KeyCol->Val_K = Cur_K = 0;
Op = OP_NEXT;
break;
case OP_SAME: // Read next same
if (trace > 1)
htrc("looking for next same value\n");
if (!Mul || NextVal(true)) {
Op = OP_EQ;
return -2; // No more equal values
} // endif Mul
break;
case OP_NXTDIF: // Read next dif
if (++To_KeyCol->Val_K == Ndif)
return -1; // End of indexed file
Cur_K = Pof[To_KeyCol->Val_K];
break;
case OP_FSTDIF: // Read first diff
To_KeyCol->Val_K = Cur_K = 0;
Op = (Mul) ? OP_NXTDIF : OP_NEXT;
break;
case OP_LAST: // Read first
Cur_K = Num_K - 1;
To_KeyCol->Val_K = Ndif - 1;
Op = OP_PREV;
break;
case OP_PREV: // Read previous
if (PrevVal())
return -1; // End of indexed file
break;
default: // Should be OP_EQ
/*****************************************************************/
/* Look for the first key equal to the link column values */
/* and return its rank whithin the index table. */
/*****************************************************************/
if (To_KeyCol->InitFind(g, To_Vals[0]))
return -1; // No more constant values
else
Nth++;
if (trace > 1)
htrc("Fetch: Looking for new value\n");
Cur_K = FastFind(1);
if (Cur_K >= Num_K)
// Rank not whithin index table, signal record not found
return -2;
else if (Mul)
Op = OP_SAME;
} // endswitch Op
/*********************************************************************/
/* If rank is equal to stored rank, record is already there. */
/*********************************************************************/
if (Cur_K == Old_K)
return -3; // Means record already there
else
Old_K = Cur_K; // Store rank of newly read record
/*********************************************************************/
/* Return the position of the required record. */
/*********************************************************************/
return (Incr) ? Cur_K * Incr : To_Rec[Cur_K];
} // end of Fetch
/***********************************************************************/
/* FastFind: Returns the index of matching indexed record using an */
/* optimized algorithm based on dichotomie and optimized comparing. */
/***********************************************************************/
int XINDXS::FastFind(int nk)
{
register int sup, inf, i= 0, n = 2;
register PXCOL kcp = To_KeyCol;
if (Nblk && Op == OP_EQ) {
// Look in block values to find in which block to search
sup = Nblk;
inf = -1;
while (n && sup - inf > 1) {
i = (inf + sup) >> 1;
n = kcp->CompBval(i);
if (n < 0)
sup = i;
else
inf = i;
} // endwhile
if (inf < 0)
return Num_K;
// i = inf;
inf *= Sblk;
if ((sup = inf + Sblk) > Ndif)
sup = Ndif;
inf--;
} else {
inf = -1;
sup = Ndif;
} // endif Nblk
while (sup - inf > 1) {
i = (inf + sup) >> 1;
n = kcp->CompVal(i);
if (n < 0)
sup = i;
else if (n > 0)
inf = i;
else
break;
} // endwhile
if (!n && Op == OP_GT) {
++i;
} else if (n && Op != OP_EQ) {
// Currently only OP_GT or OP_GE
i = sup;
n = 0;
} // endif sup
// Loop on kcp because of dynamic indexing
for (; kcp; kcp = kcp->Next)
kcp->Val_K = i; // Used by FillValue
return ((n) ? Num_K : (Mul) ? Pof[i] : i);
} // end of FastFind
/* -------------------------- XLOAD Class --------------------------- */
/***********************************************************************/
/* XLOAD constructor. */
/***********************************************************************/
XLOAD::XLOAD(void)
{
Hfile = INVALID_HANDLE_VALUE;
NewOff.Val = 0LL;
} // end of XLOAD constructor
/***********************************************************************/
/* Close the index huge file. */
/***********************************************************************/
void XLOAD::Close(void)
{
if (Hfile != INVALID_HANDLE_VALUE) {
CloseFileHandle(Hfile);
Hfile = INVALID_HANDLE_VALUE;
} // endif Hfile
} // end of Close
/* --------------------------- XFILE Class --------------------------- */
/***********************************************************************/
/* XFILE constructor. */
/***********************************************************************/
XFILE::XFILE(void) : XLOAD()
{
Xfile = NULL;
#if defined(XMAP)
Mmp = NULL;
#endif // XMAP
} // end of XFILE constructor
/***********************************************************************/
/* Xopen function: opens a file using native API's. */
/***********************************************************************/
bool XFILE::Open(PGLOBAL g, char *filename, int id, MODE mode)
{
char *pmod;
bool rc;
IOFF noff[MAX_INDX];
/*********************************************************************/
/* Open the index file according to mode. */
/*********************************************************************/
switch (mode) {
case MODE_READ: pmod = "rb"; break;
case MODE_WRITE: pmod = "wb"; break;
case MODE_INSERT: pmod = "ab"; break;
default:
sprintf(g->Message, MSG(BAD_FUNC_MODE), "Xopen", mode);
return true;
} // endswitch mode
if (!(Xfile= global_fopen(g, MSGID_OPEN_ERROR_AND_STRERROR, filename, pmod))) {
if (trace)
htrc("Open: %s\n", g->Message);
return true;
} // endif Xfile
if (mode == MODE_INSERT) {
/*******************************************************************/
/* Position the cursor at end of file so ftell returns file size. */
/*******************************************************************/
if (fseek(Xfile, 0, SEEK_END)) {
sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Xseek");
return true;
} // endif
NewOff.Low = (int)ftell(Xfile);
} else if (mode == MODE_WRITE) {
if (id >= 0) {
// New not sep index file. Write the header.
memset(noff, 0, sizeof(noff));
Write(g, noff, sizeof(IOFF), MAX_INDX, rc);
fseek(Xfile, 0, SEEK_END);
NewOff.Low = (int)ftell(Xfile);
} // endif id
} else if (mode == MODE_READ && id >= 0) {
// Get offset from the header
if (fread(noff, sizeof(IOFF), MAX_INDX, Xfile) != MAX_INDX) {
sprintf(g->Message, MSG(XFILE_READERR), errno);
return true;
} // endif MAX_INDX
// Position the cursor at the offset of this index
if (fseek(Xfile, noff[id].Low, SEEK_SET)) {
sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Xseek");
return true;
} // endif
} // endif mode
return false;
} // end of Open
/***********************************************************************/
/* Move into an index file. */
/***********************************************************************/
bool XFILE::Seek(PGLOBAL g, int low, int high, int origin)
{
#if defined(_DEBUG)
assert(high == 0);
#endif // !_DEBUG
if (fseek(Xfile, low, origin)) {
sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Xseek");
return true;
} // endif
//ftell(Xfile);
return false;
} // end of Seek
/***********************************************************************/
/* Read from the index file. */
/***********************************************************************/
bool XFILE::Read(PGLOBAL g, void *buf, int n, int size)
{
if (fread(buf, size, n, Xfile) != (size_t)n) {
sprintf(g->Message, MSG(XFILE_READERR), errno);
return true;
} // endif size
return false;
} // end of Read
/***********************************************************************/
/* Write on index file, set rc and return the number of bytes written */
/***********************************************************************/
int XFILE::Write(PGLOBAL g, void *buf, int n, int size, bool& rc)
{
int niw = (int)fwrite(buf, size, n, Xfile);
if (niw != n) {
sprintf(g->Message, MSG(XFILE_WRITERR), strerror(errno));
rc = true;
} // endif size
return niw * size;
} // end of Write
/***********************************************************************/
/* Update the file header and close the index file. */
/***********************************************************************/
void XFILE::Close(char *fn, int id)
{
if (id >= 0 && fn && Xfile) {
fclose(Xfile);
if ((Xfile = fopen(fn, "r+b")))
if (!fseek(Xfile, id * sizeof(IOFF), SEEK_SET))
fwrite(&NewOff, sizeof(int), 2, Xfile);
} // endif id
Close();
} // end of Close
/***********************************************************************/
/* Close the index file. */
/***********************************************************************/
void XFILE::Close(void)
{
XLOAD::Close();
if (Xfile) {
fclose(Xfile);
Xfile = NULL;
} // endif Xfile
#if defined(XMAP)
if (Mmp && CloseMemMap(Mmp->memory, Mmp->lenL))
printf("Error closing mapped index\n");
#endif // XMAP
} // end of Close
#if defined(XMAP)
/*********************************************************************/
/* Map the entire index file. */
/*********************************************************************/
void *XFILE::FileView(PGLOBAL g, char *fn)
{
HANDLE h;
Mmp = (MMP)PlugSubAlloc(g, NULL, sizeof(MEMMAP));
h = CreateFileMap(g, fn, Mmp, MODE_READ, false);
if (h == INVALID_HANDLE_VALUE || (!Mmp->lenH && !Mmp->lenL)) {
if (!(*g->Message))
strcpy(g->Message, MSG(FILE_MAP_ERR));
CloseFileHandle(h); // Not used anymore
return NULL; // No saved values
} // endif h
CloseFileHandle(h); // Not used anymore
return Mmp->memory;
} // end of FileView
#endif // XMAP
/* -------------------------- XHUGE Class --------------------------- */
/***********************************************************************/
/* Xopen function: opens a file using native API's. */
/***********************************************************************/
bool XHUGE::Open(PGLOBAL g, char *filename, int id, MODE mode)
{
IOFF noff[MAX_INDX];
if (Hfile != INVALID_HANDLE_VALUE) {
sprintf(g->Message, MSG(FILE_OPEN_YET), filename);
return true;
} // endif
if (trace)
htrc(" Xopen: filename=%s id=%d mode=%d\n", filename, id, mode);
#if defined(WIN32)
LONG high = 0;
DWORD rc, drc, access, share, creation;
/*********************************************************************/
/* Create the file object according to access mode */
/*********************************************************************/
switch (mode) {
case MODE_READ:
access = GENERIC_READ;
share = FILE_SHARE_READ;
creation = OPEN_EXISTING;
break;
case MODE_WRITE:
access = GENERIC_WRITE;
share = 0;
creation = CREATE_ALWAYS;
break;
case MODE_INSERT:
access = GENERIC_WRITE;
share = 0;
creation = OPEN_EXISTING;
break;
default:
sprintf(g->Message, MSG(BAD_FUNC_MODE), "Xopen", mode);
return true;
} // endswitch
Hfile = CreateFile(filename, access, share, NULL, creation,
FILE_ATTRIBUTE_NORMAL, NULL);
if (Hfile == INVALID_HANDLE_VALUE) {
rc = GetLastError();
sprintf(g->Message, MSG(OPEN_ERROR), rc, mode, filename);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0,
(LPTSTR)filename, sizeof(filename), NULL);
strcat(g->Message, filename);
return true;
} // endif Hfile
if (trace)
htrc(" access=%p share=%p creation=%d handle=%p fn=%s\n",
access, share, creation, Hfile, filename);
if (mode == MODE_INSERT) {
/*******************************************************************/
/* In Insert mode we must position the cursor at end of file. */
/*******************************************************************/
rc = SetFilePointer(Hfile, 0, &high, FILE_END);
if (rc == INVALID_SET_FILE_POINTER && (drc = GetLastError()) != NO_ERROR) {
sprintf(g->Message, MSG(ERROR_IN_SFP), drc);
CloseHandle(Hfile);
Hfile = INVALID_HANDLE_VALUE;
return true;
} // endif
NewOff.Low = (int)rc;
NewOff.High = (int)high;
} else if (mode == MODE_WRITE) {
if (id >= 0) {
// New not sep index file. Write the header.
memset(noff, 0, sizeof(noff));
rc = WriteFile(Hfile, noff, sizeof(noff), &drc, NULL);
NewOff.Low = (int)drc;
} // endif id
} else if (mode == MODE_READ && id >= 0) {
// Get offset from the header
rc = ReadFile(Hfile, noff, sizeof(noff), &drc, NULL);
if (!rc) {
sprintf(g->Message, MSG(XFILE_READERR), GetLastError());
return true;
} // endif rc
// Position the cursor at the offset of this index
rc = SetFilePointer(Hfile, noff[id].Low,
(PLONG)&noff[id].High, FILE_BEGIN);
if (rc == INVALID_SET_FILE_POINTER) {
sprintf(g->Message, MSG(FUNC_ERRNO), GetLastError(), "SetFilePointer");
return true;
} // endif
} // endif Mode
#else // UNIX
int oflag = O_LARGEFILE; // Enable file size > 2G
mode_t pmod = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
/*********************************************************************/
/* Create the file object according to access mode */
/*********************************************************************/
switch (mode) {
case MODE_READ:
oflag |= O_RDONLY;
break;
case MODE_WRITE:
oflag |= O_WRONLY | O_CREAT | O_TRUNC;
// pmod = S_IREAD | S_IWRITE;
break;
case MODE_INSERT:
oflag |= (O_WRONLY | O_APPEND);
break;
default:
sprintf(g->Message, MSG(BAD_FUNC_MODE), "Xopen", mode);
return true;
} // endswitch
Hfile= global_open(g, MSGID_OPEN_ERROR_AND_STRERROR, filename, oflag, pmod);
if (Hfile == INVALID_HANDLE_VALUE) {
/*rc = errno;*/
if (trace)
htrc("Open: %s\n", g->Message);
return true;
} // endif Hfile
if (trace)
htrc(" oflag=%p mode=%d handle=%d fn=%s\n",
oflag, mode, Hfile, filename);
if (mode == MODE_INSERT) {
/*******************************************************************/
/* Position the cursor at end of file so ftell returns file size. */
/*******************************************************************/
if (!(NewOff.Val = (longlong)lseek64(Hfile, 0LL, SEEK_END))) {
sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Seek");
return true;
} // endif
if (trace)
htrc("INSERT: NewOff=%lld\n", NewOff.Val);
} else if (mode == MODE_WRITE) {
if (id >= 0) {
// New not sep index file. Write the header.
memset(noff, 0, sizeof(noff));
NewOff.Low = write(Hfile, &noff, sizeof(noff));
} // endif id
if (trace)
htrc("WRITE: NewOff=%lld\n", NewOff.Val);
} else if (mode == MODE_READ && id >= 0) {
// Get offset from the header
if (read(Hfile, noff, sizeof(noff)) != sizeof(noff)) {
sprintf(g->Message, MSG(READ_ERROR), "Index file", strerror(errno));
return true;
} // endif read
if (trace)
htrc("noff[%d]=%lld\n", id, noff[id].Val);
// Position the cursor at the offset of this index
if (lseek64(Hfile, noff[id].Val, SEEK_SET) < 0) {
sprintf(g->Message, "(XHUGE)lseek64: %s (%lld)", strerror(errno), noff[id].Val);
printf("%s\n", g->Message);
// sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Hseek");
return true;
} // endif lseek64
} // endif mode
#endif // UNIX
return false;
} // end of Open
/***********************************************************************/
/* Go to position in a huge file. */
/***********************************************************************/
bool XHUGE::Seek(PGLOBAL g, int low, int high, int origin)
{
#if defined(WIN32)
LONG hi = high;
DWORD rc = SetFilePointer(Hfile, low, &hi, origin);
if (rc == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
sprintf(g->Message, MSG(FUNC_ERROR), "Xseek");
return true;
} // endif
#else // UNIX
off64_t pos = (off64_t)low
+ (off64_t)high * ((off64_t)0x100 * (off64_t)0x1000000);
if (lseek64(Hfile, pos, origin) < 0) {
sprintf(g->Message, MSG(ERROR_IN_LSK), errno);
if (trace)
htrc("lseek64 error %d\n", errno);
return true;
} // endif lseek64
if (trace)
htrc("Seek: low=%d high=%d\n", low, high);
#endif // UNIX
return false;
} // end of Seek
/***********************************************************************/
/* Read from a huge index file. */
/***********************************************************************/
bool XHUGE::Read(PGLOBAL g, void *buf, int n, int size)
{
bool rc = false;
#if defined(WIN32)
bool brc;
DWORD nbr, count = (DWORD)(n * size);
brc = ReadFile(Hfile, buf, count, &nbr, NULL);
if (brc) {
if (nbr != count) {
strcpy(g->Message, MSG(EOF_INDEX_FILE));
rc = true;
} // endif nbr
} else {
char *buf[256];
DWORD drc = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, drc, 0,
(LPTSTR)buf, sizeof(buf), NULL);
sprintf(g->Message, MSG(READ_ERROR), "index file", buf);
rc = true;
} // endif brc
#else // UNIX
ssize_t count = (ssize_t)(n * size);
if (trace)
htrc("Hfile=%d n=%d size=%d count=%d\n", Hfile, n, size, count);
if (read(Hfile, buf, count) != count) {
sprintf(g->Message, MSG(READ_ERROR), "Index file", strerror(errno));
if (trace)
htrc("read error %d\n", errno);
rc = true;
} // endif nbr
#endif // UNIX
return rc;
} // end of Read
/***********************************************************************/
/* Write on a huge index file. */
/***********************************************************************/
int XHUGE::Write(PGLOBAL g, void *buf, int n, int size, bool& rc)
{
#if defined(WIN32)
bool brc;
DWORD nbw, count = (DWORD)n * (DWORD) size;
brc = WriteFile(Hfile, buf, count, &nbw, NULL);
if (!brc) {
char msg[256];
DWORD drc = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, drc, 0,
(LPTSTR)msg, sizeof(msg), NULL);
sprintf(g->Message, MSG(WRITING_ERROR), "index file", msg);
rc = true;
} // endif size
return (int)nbw;
#else // UNIX
ssize_t nbw;
size_t count = (size_t)n * (size_t)size;
nbw = write(Hfile, buf, count);
if (nbw != (signed)count) {
sprintf(g->Message, MSG(WRITING_ERROR),
"index file", strerror(errno));
rc = true;
} // endif nbw
return (int)nbw;
#endif // UNIX
} // end of Write
/***********************************************************************/
/* Update the file header and close the index file. */
/***********************************************************************/
void XHUGE::Close(char *fn, int id)
{
if (trace)
htrc("XHUGE::Close: fn=%s id=%d NewOff=%lld\n", fn, id, NewOff.Val);
#if defined(WIN32)
if (id >= 0 && fn) {
CloseFileHandle(Hfile);
Hfile = CreateFile(fn, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (Hfile != INVALID_HANDLE_VALUE)
if (SetFilePointer(Hfile, id * sizeof(IOFF), NULL, FILE_BEGIN)
!= INVALID_SET_FILE_POINTER) {
DWORD nbw;
WriteFile(Hfile, &NewOff, sizeof(IOFF), &nbw, NULL);
} // endif SetFilePointer
} // endif id
#else // !WIN32
if (id >= 0 && fn) {
if (Hfile != INVALID_HANDLE_VALUE) {
if (lseek64(Hfile, id * sizeof(IOFF), SEEK_SET) >= 0) {
ssize_t nbw = write(Hfile, &NewOff, sizeof(IOFF));
if (nbw != (signed)sizeof(IOFF))
htrc("Error writing index file header: %s\n", strerror(errno));
} else
htrc("(XHUGE::Close)lseek64: %s (%d)\n", strerror(errno), id);
} else
htrc("(XHUGE)error reopening %s: %s\n", fn, strerror(errno));
} // endif id
#endif // !WIN32
XLOAD::Close();
} // end of Close
#if defined(XMAP)
/***********************************************************************/
/* Don't know whether this is possible for huge files. */
/***********************************************************************/
void *XHUGE::FileView(PGLOBAL g, char *fn)
{
strcpy(g->Message, MSG(NO_PART_MAP));
return NULL;
} // end of FileView
#endif // XMAP
/* -------------------------- XXROW Class --------------------------- */
/***********************************************************************/
/* XXROW Public Constructor. */
/***********************************************************************/
XXROW::XXROW(PTDBDOS tdbp) : XXBASE(tdbp, false)
{
Srtd = true;
Tdbp = tdbp;
Valp = NULL;
} // end of XXROW constructor
/***********************************************************************/
/* XXROW Reset: re-initialize a Kindex block. */
/***********************************************************************/
void XXROW::Reset(void)
{
#if defined(_DEBUG)
assert(Tdbp->GetLink()); // This a join index
#endif // _DEBUG
} // end of Reset
/***********************************************************************/
/* Init: Open and Initialize a Key Index. */
/***********************************************************************/
bool XXROW::Init(PGLOBAL g)
{
/*********************************************************************/
/* Table will be accessed through an index table. */
/* To_Link should not be NULL. */
/*********************************************************************/
if (!Tdbp->GetLink() || Tbxp->GetKnum() != 1)
return true;
if ((*Tdbp->GetLink())->GetResultType() != TYPE_INT) {
strcpy(g->Message, MSG(TYPE_MISMATCH));
return true;
} else
Valp = (*Tdbp->GetLink())->GetValue();
if ((Num_K = Tbxp->Cardinality(g)) < 0)
return true; // Not a fixed file
/*********************************************************************/
/* The entire table is indexed, no need to construct the index. */
/*********************************************************************/
Cur_K = Num_K;
return false;
} // end of Init
/***********************************************************************/
/* RANGE: Tell how many record exist in a given value range. */
/***********************************************************************/
int XXROW::Range(PGLOBAL g, int limit, bool incl)
{
int n = Valp->GetIntValue();
switch (limit) {
case 1: n += ((incl) ? 0 : 1); break;
case 2: n += ((incl) ? 1 : 0); break;
default: n = 1;
} // endswitch limit
return n;
} // end of Range
/***********************************************************************/
/* XXROW: Fetch a physical or logical record. */
/***********************************************************************/
int XXROW::Fetch(PGLOBAL g)
{
if (Num_K == 0)
return -1; // means end of file
/*********************************************************************/
/* Look for a key equal to the link column of previous table, */
/* and return its rank whithin the index table. */
/*********************************************************************/
Cur_K = FastFind(1);
if (Cur_K >= Num_K)
/*******************************************************************/
/* Rank not whithin index table, signal record not found. */
/*******************************************************************/
return -2; // Means record not found
/*********************************************************************/
/* If rank is equal to stored rank, record is already there. */
/*********************************************************************/
if (Cur_K == Old_K)
return -3; // Means record already there
else
Old_K = Cur_K; // Store rank of newly read record
return Cur_K;
} // end of Fetch
/***********************************************************************/
/* FastFind: Returns the index of matching record in a join. */
/***********************************************************************/
int XXROW::FastFind(int nk)
{
int n = Valp->GetIntValue();
if (n < 0)
return (Op == OP_EQ) ? (-1) : 0;
else if (n > Num_K)
return Num_K;
else
return (Op == OP_GT) ? n : (n - 1);
} // end of FastFind
/* ------------------------- KXYCOL Classes -------------------------- */
/***********************************************************************/
/* KXYCOL public constructor. */
/***********************************************************************/
KXYCOL::KXYCOL(PKXBASE kp) : To_Keys(Keys.Memp),
To_Bkeys(Bkeys.Memp), Kof((CPINT&)Koff.Memp)
{
Next = NULL;
Previous = NULL;
Kxp = kp;
Colp = NULL;
IsSorted = false;
Asc = true;
Keys = Nmblk;
Kblp = NULL;
Bkeys = Nmblk;
Blkp = NULL;
Valp = NULL;
Klen = 0;
Kprec = 0;
Type = TYPE_ERROR;
Prefix = false;
Koff = Nmblk;
Val_K = 0;
Ndf = 0;
Mxs = 0;
} // end of KXYCOL constructor
/***********************************************************************/
/* KXYCOL Init: initialize and allocate storage. */
/* Key length kln can be smaller than column length for CHAR columns. */
/***********************************************************************/
bool KXYCOL::Init(PGLOBAL g, PCOL colp, int n, bool sm, int kln)
{
int len = colp->GetLength(), prec = colp->GetScale();
// Currently no indexing on NULL columns
if (colp->IsNullable() && kln) {
sprintf(g->Message, "Cannot index nullable column %s", colp->GetName());
return true;
} // endif nullable
if (kln && len > kln && colp->GetResultType() == TYPE_STRING) {
len = kln;
Prefix = true;
} // endif kln
if (trace)
htrc("KCOL(%p) Init: col=%s n=%d type=%d sm=%d\n",
this, colp->GetName(), n, colp->GetResultType(), sm);
// Allocate the Value object used when moving items
Type = colp->GetResultType();
if (!(Valp = AllocateValue(g, Type, len, prec, colp->IsUnsigned())))
return true;
Klen = Valp->GetClen();
Keys.Size = n * Klen;
if (!PlgDBalloc(g, NULL, Keys)) {
sprintf(g->Message, MSG(KEY_ALLOC_ERROR), Klen, n);
return true; // Error
} // endif
// Allocate the Valblock. The last parameter is to have rows filled
// by blanks (if true) or keep the zero ending char (if false).
// Currently we set it to true to be compatible with QRY blocks,
// and the one before last is to enable length/type checking, set to
// true if not a prefix key.
Kblp = AllocValBlock(g, To_Keys, Type, n, len, prec, !Prefix, true);
Asc = sm; // Sort mode: Asc=true Desc=false
Ndf = n;
// Store this information to avoid sorting when already done
if (Asc)
IsSorted = colp->GetOpt() == 2;
//SetNulls(colp->IsNullable()); for when null columns will be indexable
Colp = colp;
return false;
} // end of Init
#if defined(XMAP)
/***********************************************************************/
/* KXYCOL MapInit: initialize and address storage. */
/* Key length kln can be smaller than column length for CHAR columns. */
/***********************************************************************/
BYTE* KXYCOL::MapInit(PGLOBAL g, PCOL colp, int *n, BYTE *m)
{
int len = colp->GetLength(), prec = colp->GetScale();
if (n[3] && colp->GetLength() > n[3]
&& colp->GetResultType() == TYPE_STRING) {
len = n[3];
Prefix = true;
} // endif kln
Type = colp->GetResultType();
if (trace)
htrc("MapInit(%p): colp=%p type=%d n=%d len=%d m=%p\n",
this, colp, Type, n[0], len, m);
// Allocate the Value object used when moving items
Valp = AllocateValue(g, Type, len, prec, colp->IsUnsigned());
Klen = Valp->GetClen();
if (n[2]) {
Bkeys.Size = n[2] * Klen;
Bkeys.Memp = m;
Bkeys.Sub = true;
// Allocate the Valblk containing initial block key values
Blkp = AllocValBlock(g, To_Bkeys, Type, n[2], len, prec, true, true);
} // endif nb
Keys.Size = n[0] * Klen;
Keys.Memp = m + Bkeys.Size;
Keys.Sub = true;
// Allocate the Valblock. Last two parameters are to have rows filled
// by blanks (if true) or keep the zero ending char (if false).
// Currently we set it to true to be compatible with QRY blocks,
// and last one to enable type checking (no conversion).
Kblp = AllocValBlock(g, To_Keys, Type, n[0], len, prec, !Prefix, true);
if (n[1]) {
Koff.Size = n[1] * sizeof(int);
Koff.Memp = m + Bkeys.Size + Keys.Size;
Koff.Sub = true;
} // endif n[1]
Ndf = n[0];
//IsSorted = colp->GetOpt() < 0;
IsSorted = false;
Colp = colp;
return m + Bkeys.Size + Keys.Size + Koff.Size;
} // end of MapInit
#endif // XMAP
/***********************************************************************/
/* Allocate the offset block used by intermediate key columns. */
/***********************************************************************/
int *KXYCOL::MakeOffset(PGLOBAL g, int n)
{
if (!Kof) {
// Calculate the initial size of the offset
Koff.Size = (n + 1) * sizeof(int);
// Allocate the required memory
if (!PlgDBalloc(g, NULL, Koff)) {
strcpy(g->Message, MSG(KEY_ALLOC_ERR));
return NULL; // Error
} // endif
} else if (n) {
// This is a reallocation call
PlgDBrealloc(g, NULL, Koff, (n + 1) * sizeof(int));
} else
PlgDBfree(Koff);
return (int*)Kof;
} // end of MakeOffset
/***********************************************************************/
/* Make a front end array of key values that are the first value of */
/* each blocks (of size n). This to reduce paging in FastFind. */
/***********************************************************************/
bool KXYCOL::MakeBlockArray(PGLOBAL g, int nb, int size)
{
int i, k;
// Calculate the size of the block array in the index
Bkeys.Size = nb * Klen;
// Allocate the required memory
if (!PlgDBalloc(g, NULL, Bkeys)) {
sprintf(g->Message, MSG(KEY_ALLOC_ERROR), Klen, nb);
return true; // Error
} // endif
// Allocate the Valblk used to contains initial block key values
Blkp = AllocValBlock(g, To_Bkeys, Type, nb, Klen, Kprec);
// Populate the array with values
for (i = k = 0; i < nb; i++, k += size)
Blkp->SetValue(Kblp, i, k);
return false;
} // end of MakeBlockArray
/***********************************************************************/
/* KXYCOL SetValue: read column value for nth array element. */
/***********************************************************************/
void KXYCOL::SetValue(PCOL colp, int i)
{
#if defined(_DEBUG)
assert (Kblp != NULL);
#endif
Kblp->SetValue(colp->GetValue(), i);
} // end of SetValue
/***********************************************************************/
/* InitFind: initialize finding the rank of column value in index. */
/***********************************************************************/
bool KXYCOL::InitFind(PGLOBAL g, PXOB xp)
{
if (xp->GetType() == TYPE_CONST) {
if (Kxp->Nth)
return true;
Valp->SetValue_pval(xp->GetValue(), !Prefix);
} else {
xp->Reset();
xp->Eval(g);
Valp->SetValue_pval(xp->GetValue(), false);
// Valp->SetValue_pval(xp->GetValue(), !Prefix);
} // endif Type
return false;
} // end of InitFind
/***********************************************************************/
/* InitBinFind: initialize Value to the value pointed by vp. */
/***********************************************************************/
void KXYCOL::InitBinFind(void *vp)
{
Valp->SetBinValue(vp);
} // end of InitBinFind
/***********************************************************************/
/* KXYCOL FillValue: called by COLBLK::Eval when a column value is */
/* already in storage in the corresponding KXYCOL. */
/***********************************************************************/
void KXYCOL::FillValue(PVAL valp)
{
valp->SetValue_pvblk(Kblp, Val_K);
// Set null when applicable (NIY)
//if (valp->GetNullable())
// valp->SetNull(valp->IsZero());
} // end of FillValue
/***********************************************************************/
/* KXYCOL: Compare routine for one numeric value. */
/***********************************************************************/
int KXYCOL::Compare(int i1, int i2)
{
// Do the actual comparison between values.
register int k = Kblp->CompVal(i1, i2);
if (trace > 2)
htrc("Compare done result=%d\n", k);
return (Asc) ? k : -k;
} // end of Compare
/***********************************************************************/
/* KXYCOL: Compare the ith key to the stored Value. */
/***********************************************************************/
int KXYCOL::CompVal(int i)
{
// Do the actual comparison between numerical values.
if (trace > 2) {
register int k = (int)Kblp->CompVal(Valp, (int)i);
htrc("Compare done result=%d\n", k);
return k;
} else
return Kblp->CompVal(Valp, i);
} // end of CompVal
/***********************************************************************/
/* KXYCOL: Compare the key to the stored block value. */
/***********************************************************************/
int KXYCOL::CompBval(int i)
{
// Do the actual comparison between key values.
return Blkp->CompVal(Valp, i);
} // end of CompBval
/***********************************************************************/
/* KXYCOL ReAlloc: ReAlloc To_Data if it is not suballocated. */
/***********************************************************************/
void KXYCOL::ReAlloc(PGLOBAL g, int n)
{
PlgDBrealloc(g, NULL, Keys, n * Klen);
Kblp->ReAlloc(To_Keys, n);
Ndf = n;
} // end of ReAlloc
/***********************************************************************/
/* KXYCOL FreeData: Free To_Keys if it is not suballocated. */
/***********************************************************************/
void KXYCOL::FreeData(void)
{
PlgDBfree(Keys);
Kblp = NULL;
PlgDBfree(Bkeys);
Blkp = NULL;
PlgDBfree(Koff);
Ndf = 0;
} // end of FreeData
|