1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758 13759 13760 13761 13762 13763 13764 13765 13766 13767 13768 13769 13770 13771 13772 13773 13774 13775 13776 13777 13778 13779 13780 13781 13782 13783 13784 13785 13786 13787 13788 13789 13790 13791 13792 13793 13794 13795 13796 13797 13798 13799 13800 13801 13802 13803 13804 13805 13806 13807 13808 13809 13810 13811 13812 13813 13814 13815 13816 13817 13818 13819 13820 13821 13822 13823 13824 13825 13826 13827 13828 13829 13830 13831 13832 13833 13834 13835 13836 13837 13838 13839 13840 13841 13842 13843 13844 13845 13846 13847 13848 13849 13850 13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 13865 13866 13867 13868 13869 13870 13871 13872 13873 13874 13875 13876 13877 13878 13879 13880 13881 13882 13883 13884 13885 13886 13887 13888 13889 13890 13891 13892 13893 13894 13895 13896 13897 13898 13899 13900 13901 13902 13903 13904 13905 13906 13907 13908 13909 13910 13911 13912 13913 13914 13915 13916 13917 13918 13919 13920 13921 13922 13923 13924 13925 13926 13927 13928 13929 13930 13931 13932 13933 13934 13935 13936 13937 13938 13939 13940 13941 13942 13943 13944 13945 13946 13947 13948 13949 13950 13951 13952 13953 13954 13955 13956 13957 13958 13959 13960 13961 13962 13963 13964 13965 13966 13967 13968 13969 13970 13971 13972 13973 13974 13975 13976 13977 13978 13979 13980 13981 13982 13983 13984 13985 13986 13987 13988 13989 13990 13991 13992 13993 13994 13995 13996 13997 13998 13999 14000 14001 14002 14003 14004 14005 14006 14007 14008 14009 14010 14011 14012 14013 14014 14015 14016 14017 14018 14019 14020 14021 14022 14023 14024 14025 14026 14027 14028 14029 14030 14031 14032 14033 14034 14035 14036 14037 14038 14039 14040 14041 14042 14043 14044 14045 14046 14047 14048 14049 14050 14051 14052 14053 14054 14055 14056 14057 14058 14059 14060 14061 14062 14063 14064 14065 14066 14067 14068 14069 14070 14071 14072 14073 14074 14075 14076 14077 14078 14079 14080 14081 14082 14083 14084 14085 14086 14087 14088 14089 14090 14091 14092 14093 14094 14095 14096 14097 14098 14099 14100 14101 14102 14103 14104 14105 14106 14107 14108 14109 14110 14111 14112 14113 14114 14115 14116 14117 14118 14119 14120 14121 14122 14123 14124 14125 14126 14127 14128 14129 14130 14131 14132 14133 14134 14135 14136 14137 14138 14139 14140 14141 14142 14143 14144 14145 14146 14147 14148 14149 14150 14151 14152 14153 14154 14155 14156 14157 14158 14159 14160 14161 14162 14163 14164 14165 14166 14167 14168 14169 14170 14171 14172 14173 14174 14175 14176 14177 14178 14179 14180 14181 14182 14183 14184 14185 14186 14187 14188 14189 14190 14191 14192 14193 14194 14195 14196 14197 14198 14199 14200 14201 14202 14203 14204 14205 14206 14207 14208 14209 14210 14211 14212 14213 14214 14215 14216 14217 14218 14219 14220 14221 14222 14223 14224 14225 14226 14227 14228 14229 14230 14231 14232 14233 14234 14235 14236 14237 14238 14239 14240 14241 14242 14243 14244 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 14320 14321 14322 14323 14324 14325 14326 14327 14328 14329 14330 14331 14332 14333 14334 14335 14336 14337 14338 14339 14340 14341 14342 14343 14344 14345 14346 14347 14348 14349 14350 14351 14352 14353 14354 14355 14356 14357 14358 14359 14360 14361 14362 14363 14364 14365 14366 14367 14368 14369 14370 14371 14372 14373 14374 14375 14376 14377 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 14402 14403 14404 14405 14406 14407 14408 14409 14410 14411 14412 14413 14414 14415 14416 14417 14418 14419 14420 14421 14422 14423 14424 14425 14426 14427 14428 14429 14430 14431 14432 14433 14434 14435 14436 14437 14438 14439 14440 14441 14442 14443 14444 14445 14446 14447 14448 14449 14450 14451 14452 14453 14454 14455 14456 14457 14458 14459 14460 14461 14462 14463 14464 14465 14466 14467 14468 14469 14470 14471 14472 14473 14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 14524 14525 14526 14527 14528 14529 14530 14531 14532 14533 14534 14535 14536 14537 14538 14539 14540 14541 14542 14543 14544 14545 14546 14547 14548 14549 14550 14551 14552 14553 14554 14555 14556 14557 14558 14559 14560 14561 14562 14563 14564 14565 14566 14567 14568 14569 14570 14571 14572 14573 14574 14575 14576 14577 14578 14579 14580 14581 14582 14583 14584 14585 14586 14587 14588 14589 14590 14591 14592 14593 14594 14595 14596 14597 14598 14599 14600 14601 14602 14603 14604 14605 14606 14607 14608 14609 14610 14611 14612 14613 14614 14615 14616 14617 14618 14619 14620 14621 14622 14623 14624 14625 14626 14627 14628 14629 14630 14631 14632 14633 14634 14635 14636 14637 14638 14639 14640 14641 14642 14643 14644 14645 14646 14647 14648 14649 14650 14651 14652 14653 14654 14655 14656 14657 14658 14659 14660 14661 14662 14663 14664 14665 14666 14667 14668 14669 14670 14671 14672 14673 14674 14675 14676 14677 14678 14679 14680 14681 14682 14683 14684 14685 14686 14687 14688 14689 14690 14691 14692 14693 14694 14695 14696 14697 14698 14699 14700 14701 14702 14703 14704 14705 14706 14707 14708 14709 14710 14711 14712 14713 14714 14715 14716 14717 14718 14719 14720 14721 14722 14723 14724 14725 14726 14727 14728 14729 14730 14731 14732 14733 14734 14735 14736 14737 14738 14739 14740 14741 14742 14743 14744 14745 14746 14747 14748 14749 14750 14751 14752 14753 14754 14755 14756 14757 14758 14759 14760 14761 14762 14763 14764 14765 14766 14767 14768 14769 14770 14771 14772 14773 14774 14775 14776 14777 14778 14779 14780 14781 14782 14783 14784 14785 14786 14787 14788 14789 14790 14791 14792 14793 14794 14795 14796 14797 14798 14799 14800 14801 14802 14803 14804 14805 14806 14807 14808 14809 14810 14811 14812 14813 14814 14815 14816 14817 14818 14819 14820 14821 14822 14823 14824 14825 14826 14827 14828 14829 14830 14831 14832 14833 14834 14835 14836 14837 14838 14839 14840 14841 14842 14843 14844 14845 14846 14847 14848 14849 14850 14851 14852 14853 14854 14855 14856 14857 14858 14859 14860 14861 14862 14863 14864 14865 14866 14867 14868 14869 14870 14871 14872 14873 14874 14875 14876 14877 14878 14879 14880 14881 14882 14883 14884 14885 14886 14887 14888 14889 14890 14891 14892 14893 14894 14895 14896 14897 14898 14899 14900 14901 14902 14903 14904 14905 14906 14907 14908 14909 14910 14911 14912 14913 14914 14915 14916 14917 14918 14919 14920 14921 14922 14923 14924 14925 14926 14927 14928 14929 14930 14931 14932 14933 14934 14935 14936 14937 14938 14939 14940 14941 14942 14943 14944 14945 14946 14947 14948 14949 14950 14951 14952 14953 14954 14955 14956 14957 14958 14959 14960 14961 14962 14963 14964 14965 14966 14967 14968 14969 14970 14971 14972 14973 14974 14975 14976 14977 14978 14979 14980 14981 14982 14983 14984 14985 14986 14987 14988 14989 14990 14991 14992 14993 14994 14995 14996 14997 14998 14999 15000 15001 15002 15003 15004 15005 15006 15007 15008 15009 15010 15011 15012 15013 15014 15015 15016 15017 15018 15019 15020 15021 15022 15023 15024 15025 15026 15027 15028 15029 15030 15031 15032 15033 15034 15035 15036 15037 15038 15039 15040 15041 15042 15043 15044 15045 15046 15047 15048 15049 15050 15051 15052 15053 15054 15055 15056 15057 15058 15059 15060 15061 15062 15063 15064 15065 15066 15067 15068 15069 15070 15071 15072 15073 15074 15075 15076 15077 15078 15079 15080 15081 15082 15083 15084 15085 15086 15087 15088 15089 15090 15091 15092 15093 15094 15095 15096 15097 15098 15099 15100 15101 15102 15103 15104 15105 15106 15107 15108 15109 15110 15111 15112 15113 15114 15115 15116 15117 15118 15119 15120 15121 15122 15123 15124 15125 15126 15127 15128 15129 15130 15131 15132 15133 15134 15135 15136 15137 15138 15139 15140 15141 15142 15143 15144 15145 15146 15147 15148 15149 15150 15151 15152 15153 15154 15155 15156 15157 15158 15159 15160 15161 15162 15163 15164 15165 15166 15167 15168 15169 15170 15171 15172 15173 15174 15175 15176 15177 15178 15179 15180 15181 15182 15183 15184 15185 15186 15187 15188 15189 15190 15191 15192 15193 15194 15195 15196 15197 15198 15199 15200 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211 15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223 15224 15225 15226 15227 15228 15229 15230 15231 15232 15233 15234 15235 15236 15237 15238 15239 15240 15241 15242 15243 15244 15245 15246 15247 15248 15249 15250 15251 15252 15253 15254 15255 15256 15257 15258 15259 15260 15261 15262 15263 15264 15265 15266 15267 15268 15269 15270 15271 15272 15273 15274 15275 15276 15277 15278 15279 15280 15281 15282 15283 15284 15285 15286 15287 15288 15289 15290 15291 15292 15293 15294 15295 15296 15297 15298 15299 15300 15301 15302 15303 15304 15305 15306 15307 15308 15309 15310 15311 15312 15313 15314 15315 15316 15317 15318 15319 15320 15321 15322 15323 15324 15325 15326 15327 15328 15329 15330 15331 15332 15333 15334 15335 15336 15337 15338 15339 15340 15341 15342 15343 15344 15345 15346 15347 15348 15349 15350 15351 15352 15353 15354 15355 15356 15357 15358 15359 15360 15361 15362 15363 15364 15365 15366 15367 15368 15369 15370 15371 15372 15373 15374 15375 15376 15377 15378 15379 15380 15381 15382 15383 15384 15385 15386 15387 15388 15389 15390 15391 15392 15393 15394 15395 15396 15397 15398 15399 15400 15401 15402 15403 15404 15405 15406 15407 15408 15409 15410 15411 15412 15413 15414 15415 15416 15417 15418 15419 15420 15421 15422 15423 15424 15425 15426 15427 15428 15429 15430 15431 15432 15433 15434 15435 15436 15437 15438 15439 15440 15441 15442 15443 15444 15445 15446 15447 15448 15449 15450 15451 15452 15453 15454 15455 15456 15457 15458 15459 15460 15461 15462 15463 15464 15465 15466 15467 15468 15469 15470 15471 15472 15473 15474 15475 15476 15477 15478 15479 15480 15481 15482 15483 15484 15485 15486 15487 15488 15489 15490 15491 15492 15493 15494 15495 15496 15497 15498 15499 15500 15501 15502 15503 15504 15505 15506 15507 15508 15509 15510 15511 15512 15513 15514 15515 15516 15517 15518 15519 15520 15521 15522 15523 15524 15525 15526 15527 15528 15529 15530 15531 15532 15533 15534 15535 15536 15537 15538 15539 15540 15541 15542 15543 15544 15545 15546 15547 15548 15549 15550 15551 15552 15553 15554 15555 15556 15557 15558 15559 15560 15561 15562 15563 15564 15565 15566 15567 15568 15569 15570 15571 15572 15573 15574 15575 15576 15577 15578 15579 15580 15581 15582 15583 15584 15585 15586 15587 15588 15589 15590 15591 15592 15593 15594 15595 15596 15597 15598 15599 15600 15601 15602 15603 15604 15605 15606 15607 15608 15609 15610 15611 15612 15613 15614 15615 15616 15617 15618 15619 15620 15621 15622 15623 15624 15625 15626 15627 15628 15629 15630 15631 15632 15633 15634 15635 15636 15637 15638 15639 15640 15641 15642 15643 15644 15645 15646 15647 15648 15649 15650 15651 15652 15653 15654 15655 15656 15657 15658 15659 15660 15661 15662 15663 15664 15665 15666 15667 15668 15669 15670 15671 15672 15673 15674 15675 15676 15677 15678 15679 15680 15681 15682 15683 15684 15685 15686 15687 15688 15689 15690 15691 15692 15693 15694 15695 15696 15697 15698 15699 15700 15701 15702 15703 15704 15705 15706 15707 15708 15709 15710 15711 15712 15713 15714 15715 15716 15717 15718 15719 15720 15721 15722 15723 15724 15725 15726 15727 15728 15729 15730 15731 15732 15733 15734 15735 15736 15737 15738 15739 15740 15741 15742 15743 15744 15745 15746 15747 15748 15749 15750 15751 15752 15753 15754 15755 15756 15757 15758 15759 15760 15761 15762 15763 15764 15765 15766 15767 15768 15769 15770 15771 15772 15773 15774 15775 15776 15777 15778 15779 15780 15781 15782 15783 15784 15785 15786 15787 15788 15789 15790 15791 15792 15793 15794 15795 15796 15797 15798 15799 15800 15801 15802 15803 15804 15805 15806 15807 15808 15809 15810 15811 15812 15813 15814 15815 15816 15817 15818 15819 15820 15821 15822 15823 15824 15825 15826 15827 15828 15829 15830 15831 15832 15833 15834 15835 15836 15837 15838 15839 15840 15841 15842 15843 15844 15845 15846 15847 15848 15849 15850 15851 15852 15853 15854 15855 15856 15857 15858 15859 15860 15861 15862 15863 15864 15865 15866 15867 15868 15869 15870 15871 15872 15873 15874 15875 15876 15877 15878 15879 15880 15881 15882 15883 15884 15885 15886 15887 15888 15889 15890 15891 15892 15893 15894 15895 15896 15897 15898 15899 15900 15901 15902 15903 15904 15905 15906 15907 15908 15909 15910 15911 15912 15913 15914 15915 15916 15917 15918 15919 15920 15921 15922 15923 15924 15925 15926 15927 15928 15929 15930 15931 15932 15933 15934 15935 15936 15937 15938 15939 15940 15941 15942 15943 15944 15945 15946 15947 15948 15949 15950 15951 15952 15953 15954 15955 15956 15957 15958 15959 15960 15961 15962 15963 15964 15965 15966 15967 15968 15969 15970 15971 15972 15973 15974 15975 15976 15977 15978 15979 15980 15981 15982 15983 15984 15985 15986 15987 15988 15989 15990 15991 15992 15993 15994 15995 15996 15997 15998 15999 16000 16001 16002 16003 16004 16005 16006 16007 16008 16009 16010 16011 16012 16013 16014 16015 16016 16017 16018 16019 16020 16021 16022 16023 16024 16025 16026 16027 16028 16029 16030 16031 16032 16033 16034 16035 16036 16037 16038 16039 16040 16041 16042 16043 16044 16045 16046 16047 16048 16049 16050 16051 16052 16053 16054 16055 16056 16057 16058 16059 16060 16061 16062 16063 16064 16065 16066 16067 16068 16069 16070 16071 16072 16073 16074 16075 16076 16077 16078 16079 16080 16081 16082 16083 16084 16085 16086 16087 16088 16089 16090 16091 16092 16093 16094 16095 16096 16097 16098 16099 16100 16101 16102 16103 16104 16105 16106 16107 16108 16109 16110 16111 16112 16113 16114 16115 16116 16117 16118 16119 16120 16121 16122 16123 16124 16125 16126 16127 16128 16129 16130 16131 16132 16133 16134 16135 16136 16137 16138 16139 16140 16141 16142 16143 16144 16145 16146 16147 16148 16149 16150 16151 16152 16153 16154 16155 16156 16157 16158 16159 16160 16161 16162 16163 16164 16165 16166 16167 16168 16169 16170 16171 16172 16173 16174 16175 16176 16177 16178 16179 16180 16181 16182 16183 16184 16185 16186 16187 16188 16189 16190 16191 16192 16193 16194 16195 16196 16197 16198 16199 16200 16201 16202 16203 16204 16205 16206 16207 16208 16209 16210 16211 16212 16213 16214 16215 16216 16217 16218 16219 16220 16221 16222 16223 16224 16225 16226 16227 16228 16229 16230 16231 16232 16233 16234 16235 16236 16237 16238 16239 16240 16241 16242 16243 16244 16245 16246 16247 16248 16249 16250 16251 16252 16253 16254 16255 16256 16257 16258 16259 16260 16261 16262 16263 16264 16265 16266 16267 16268 16269 16270 16271 16272 16273 16274 16275 16276 16277 16278 16279 16280 16281 16282 16283 16284 16285 16286 16287 16288 16289 16290 16291 16292 16293 16294 16295 16296 16297 16298 16299 16300 16301 16302 16303 16304 16305 16306 16307 16308 16309 16310 16311 16312 16313 16314 16315 16316 16317 16318 16319 16320 16321 16322 16323 16324 16325 16326 16327 16328 16329 16330 16331 16332 16333 16334 16335 16336 16337 16338 16339 16340 16341 16342 16343 16344 16345 16346 16347 16348 16349 16350 16351 16352 16353 16354 16355 16356 16357 16358 16359 16360 16361 16362 16363 16364 16365 16366 16367 16368 16369 16370 16371 16372 16373 16374 16375 16376 16377 16378 16379 16380 16381 16382 16383 16384 16385 16386 16387 16388 16389 16390 16391 16392 16393 16394 16395 16396 16397 16398 16399 16400 16401 16402 16403 16404 16405 16406 16407 16408 16409 16410 16411 16412 16413 16414 16415 16416 16417 16418 16419 16420 16421 16422 16423 16424 16425 16426 16427 16428 16429 16430 16431 16432 16433 16434 16435 16436 16437 16438 16439 16440 16441 16442 16443 16444 16445 16446 16447 16448 16449 16450 16451 16452 16453 16454 16455 16456 16457 16458 16459 16460 16461 16462 16463 16464 16465 16466 16467 16468 16469 16470 16471 16472 16473 16474 16475 16476 16477 16478 16479 16480 16481 16482 16483 16484 16485 16486 16487 16488 16489 16490 16491 16492 16493 16494 16495 16496 16497 16498 16499 16500 16501 16502 16503 16504 16505 16506 16507 16508 16509 16510 16511 16512 16513 16514 16515 16516 16517 16518 16519 16520 16521 16522 16523 16524 16525 16526 16527 16528 16529 16530 16531 16532 16533 16534 16535 16536 16537 16538 16539 16540 16541 16542 16543 16544 16545 16546 16547 16548 16549 16550 16551 16552 16553 16554 16555 16556 16557 16558 16559 16560 16561 16562 16563 16564 16565 16566 16567 16568 16569 16570 16571 16572 16573 16574 16575 16576 16577 16578 16579 16580 16581 16582 16583 16584 16585 16586 16587 16588 16589 16590 16591 16592 16593 16594 16595 16596 16597 16598 16599 16600 16601 16602 16603 16604 16605 16606 16607 16608 16609 16610 16611 16612 16613 16614 16615 16616 16617 16618 16619 16620 16621 16622 16623 16624 16625 16626 16627 16628 16629 16630 16631 16632 16633 16634 16635 16636 16637 16638 16639 16640 16641 16642 16643 16644 16645 16646 16647 16648 16649 16650 16651 16652 16653 16654 16655 16656 16657 16658 16659 16660 16661 16662 16663 16664 16665 16666 16667 16668 16669 16670 16671 16672 16673 16674 16675 16676 16677 16678 16679 16680 16681 16682 16683 16684 16685 16686 16687 16688 16689 16690 16691 16692 16693 16694 16695 16696 16697 16698 16699 16700 16701 16702 16703 16704 16705 16706 16707 16708 16709 16710 16711 16712 16713 16714 16715 16716 16717 16718 16719 16720 16721 16722 16723 16724 16725 16726 16727 16728 16729 16730 16731 16732 16733 16734 16735 16736 16737 16738 16739 16740 16741 16742 16743 16744 16745 16746 16747 16748 16749 16750 16751 16752 16753 16754 16755 16756 16757 16758 16759 16760 16761 16762 16763 16764 16765 16766 16767 16768 16769 16770 16771 16772 16773 16774 16775 16776 16777 16778 16779 16780 16781 16782 16783 16784 16785 16786 16787 16788 16789 16790 16791 16792 16793 16794 16795 16796 16797 16798 16799 16800 16801 16802 16803 16804 16805 16806 16807 16808 16809 16810 16811 16812 16813 16814 16815 16816 16817 16818 16819 16820 16821 16822 16823 16824 16825 16826 16827 16828 16829 16830 16831 16832 16833 16834 16835 16836 16837 16838 16839 16840 16841 16842 16843 16844 16845 16846 16847 16848 16849 16850 16851 16852 16853 16854 16855 16856 16857 16858 16859 16860 16861 16862 16863 16864 16865 16866 16867 16868 16869 16870 16871 16872 16873 16874 16875 16876 16877 16878 16879 16880 16881 16882 16883 16884 16885 16886 16887 16888 16889 16890 16891 16892 16893 16894 16895 16896 16897 16898 16899 16900 16901 16902 16903 16904 16905 16906 16907 16908 16909 16910 16911 16912 16913 16914 16915 16916 16917 16918 16919 16920 16921 16922 16923 16924 16925 16926 16927 16928 16929 16930 16931 16932 16933 16934 16935 16936 16937 16938 16939 16940 16941 16942 16943 16944 16945 16946 16947 16948 16949 16950 16951 16952 16953 16954 16955 16956 16957 16958 16959 16960 16961 16962 16963 16964 16965 16966 16967 16968 16969 16970 16971 16972 16973 16974 16975 16976 16977 16978 16979 16980 16981 16982 16983 16984 16985 16986 16987 16988 16989 16990 16991 16992 16993 16994 16995 16996 16997 16998 16999 17000 17001 17002 17003 17004 17005 17006 17007 17008 17009 17010 17011 17012 17013 17014 17015 17016 17017 17018 17019 17020 17021 17022 17023 17024 17025 17026 17027 17028 17029 17030 17031 17032 17033 17034 17035 17036 17037 17038 17039 17040 17041 17042 17043 17044 17045 17046 17047 17048 17049 17050 17051 17052 17053 17054 17055 17056 17057 17058 17059 17060 17061 17062 17063 17064 17065 17066 17067 17068 17069 17070 17071 17072 17073 17074 17075 17076 17077 17078 17079 17080 17081 17082 17083 17084 17085 17086 17087 17088 17089 17090 17091 17092 17093 17094 17095 17096 17097 17098 17099 17100 17101 17102 17103 17104 17105 17106 17107 17108 17109 17110 17111 17112 17113 17114 17115 17116 17117 17118 17119 17120 17121 17122 17123 17124 17125 17126 17127 17128 17129 17130 17131 17132 17133 17134 17135 17136 17137 17138 17139 17140 17141 17142 17143 17144 17145 17146 17147 17148 17149 17150 17151 17152 17153 17154 17155 17156 17157 17158 17159 17160 17161 17162 17163 17164 17165 17166 17167 17168 17169 17170 17171 17172 17173 17174 17175 17176 17177 17178 17179 17180 17181 17182 17183 17184 17185 17186 17187 17188 17189 17190 17191 17192 17193 17194 17195 17196 17197 17198 17199 17200 17201 17202 17203 17204 17205 17206 17207 17208 17209 17210 17211 17212 17213 17214 17215 17216 17217 17218 17219 17220 17221 17222 17223 17224 17225 17226 17227 17228 17229 17230 17231 17232 17233 17234 17235 17236 17237 17238 17239 17240 17241 17242 17243 17244 17245 17246 17247 17248 17249 17250 17251 17252 17253 17254 17255 17256 17257 17258 17259 17260 17261 17262 17263 17264 17265 17266 17267 17268 17269 17270 17271 17272 17273 17274 17275 17276 17277 17278 17279 17280 17281 17282 17283 17284 17285 17286 17287 17288 17289 17290 17291 17292 17293 17294 17295 17296 17297 17298 17299 17300 17301 17302 17303 17304 17305 17306 17307 17308 17309 17310 17311 17312 17313 17314 17315 17316 17317 17318 17319 17320 17321 17322 17323 17324 17325 17326 17327 17328 17329 17330 17331 17332 17333 17334 17335 17336 17337 17338 17339 17340 17341 17342 17343 17344 17345 17346 17347 17348 17349 17350 17351 17352 17353 17354 17355 17356 17357 17358 17359 17360 17361 17362 17363 17364 17365 17366 17367 17368 17369 17370 17371 17372 17373 17374 17375 17376 17377 17378 17379 17380 17381 17382 17383 17384 17385 17386 17387 17388 17389 17390 17391 17392 17393 17394 17395 17396 17397 17398 17399 17400 17401 17402 17403 17404 17405 17406 17407 17408 17409 17410 17411 17412 17413 17414 17415 17416 17417 17418 17419 17420 17421 17422 17423 17424 17425 17426 17427 17428 17429 17430 17431 17432 17433 17434 17435 17436 17437 17438 17439 17440 17441 17442 17443 17444 17445 17446 17447 17448 17449 17450 17451 17452 17453 17454 17455 17456 17457 17458 17459 17460 17461 17462 17463 17464 17465 17466 17467 17468 17469 17470 17471 17472 17473 17474 17475 17476 17477 17478 17479 17480 17481 17482 17483 17484 17485 17486 17487 17488 17489 17490 17491 17492 17493 17494 17495 17496 17497 17498 17499 17500 17501 17502 17503 17504 17505 17506 17507 17508 17509 17510 17511 17512 17513 17514 17515 17516 17517 17518 17519 17520 17521 17522 17523 17524 17525 17526 17527 17528 17529 17530 17531 17532 17533 17534 17535 17536 17537 17538 17539 17540 17541 17542 17543 17544 17545 17546 17547 17548 17549 17550 17551 17552 17553 17554 17555 17556 17557 17558 17559 17560 17561 17562 17563 17564 17565 17566 17567 17568 17569 17570 17571 17572 17573 17574 17575 17576 17577 17578 17579 17580 17581 17582 17583 17584 17585 17586 17587 17588 17589 17590 17591 17592 17593 17594 17595 17596 17597 17598 17599 17600 17601 17602 17603 17604 17605 17606 17607 17608 17609 17610 17611 17612 17613 17614 17615 17616 17617 17618 17619 17620 17621 17622 17623 17624 17625 17626 17627 17628 17629 17630 17631 17632 17633 17634 17635 17636 17637 17638 17639 17640 17641 17642 17643 17644 17645 17646 17647 17648 17649 17650 17651 17652 17653 17654 17655 17656 17657 17658 17659 17660 17661 17662 17663 17664 17665 17666 17667 17668 17669 17670 17671 17672 17673 17674 17675 17676 17677 17678 17679 17680 17681 17682 17683 17684 17685 17686 17687 17688 17689 17690 17691 17692 17693 17694 17695 17696 17697 17698 17699 17700 17701 17702 17703 17704 17705 17706 17707 17708 17709 17710 17711 17712 17713 17714 17715 17716 17717 17718 17719 17720 17721 17722 17723 17724 17725 17726 17727 17728 17729 17730 17731 17732 17733 17734 17735 17736 17737 17738 17739 17740 17741 17742 17743 17744 17745 17746 17747 17748 17749 17750 17751 17752 17753 17754 17755 17756 17757 17758 17759 17760 17761 17762 17763 17764 17765 17766 17767 17768 17769 17770 17771 17772 17773 17774 17775 17776 17777 17778 17779 17780 17781 17782 17783 17784 17785 17786 17787 17788 17789 17790 17791 17792 17793 17794 17795 17796 17797 17798 17799 17800 17801 17802 17803 17804 17805 17806 17807 17808 17809 17810 17811 17812 17813 17814 17815 17816 17817 17818 17819 17820 17821 17822 17823 17824 17825 17826 17827 17828 17829 17830 17831 17832 17833 17834 17835 17836 17837 17838 17839 17840 17841 17842 17843 17844 17845 17846 17847 17848 17849 17850 17851 17852 17853 17854 17855 17856 17857 17858 17859 17860 17861 17862 17863 17864 17865 17866 17867 17868 17869 17870 17871 17872 17873 17874 17875 17876 17877 17878 17879 17880 17881 17882 17883 17884 17885 17886 17887 17888 17889 17890 17891 17892 17893 17894 17895 17896 17897 17898 17899 17900 17901 17902 17903 17904 17905 17906 17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918 17919 17920 17921 17922 17923 17924 17925 17926 17927 17928 17929 17930 17931 17932 17933 17934 17935 17936 17937 17938 17939 17940 17941 17942 17943 17944 17945 17946 17947 17948 17949 17950 17951 17952 17953 17954 17955 17956 17957 17958 17959 17960 17961 17962 17963 17964 17965 17966 17967 17968 17969 17970 17971 17972 17973 17974 17975 17976 17977 17978 17979 17980 17981 17982 17983 17984 17985 17986 17987 17988 17989 17990 17991 17992 17993 17994 17995 17996 17997 17998 17999 18000 18001 18002 18003 18004 18005 18006 18007 18008 18009 18010 18011 18012 18013 18014 18015 18016 18017 18018 18019 18020 18021 18022 18023 18024 18025 18026 18027 18028 18029 18030 18031 18032 18033 18034 18035 18036 18037 18038 18039 18040 18041 18042 18043 18044 18045 18046 18047 18048 18049 18050 18051 18052 18053 18054 18055 18056 18057 18058 18059 18060 18061 18062 18063 18064 18065 18066 18067 18068 18069 18070 18071 18072 18073 18074 18075 18076 18077 18078 18079 18080 18081 18082 18083 18084 18085 18086 18087 18088 18089 18090 18091 18092 18093 18094 18095 18096 18097 18098 18099 18100 18101 18102 18103 18104 18105 18106 18107 18108 18109 18110 18111 18112 18113 18114 18115 18116 18117 18118 18119 18120 18121 18122 18123 18124 18125 18126 18127 18128 18129 18130 18131 18132 18133 18134 18135 18136 18137 18138 18139 18140 18141 18142 18143 18144 18145 18146 18147 18148 18149 18150 18151 18152 18153 18154 18155 18156 18157 18158 18159 18160 18161 18162 18163 18164 18165 18166 18167 18168 18169 18170 18171 18172 18173 18174 18175 18176 18177 18178 18179 18180 18181 18182 18183 18184 18185 18186 18187 18188 18189 18190 18191 18192 18193 18194 18195 18196 18197 18198 18199 18200 18201 18202 18203 18204 18205 18206 18207 18208 18209 18210 18211 18212 18213 18214 18215 18216 18217 18218 18219 18220 18221 18222 18223 18224 18225 18226 18227 18228 18229 18230 18231 18232 18233 18234 18235 18236 18237 18238 18239 18240 18241 18242 18243 18244 18245 18246 18247 18248 18249 18250 18251 18252 18253 18254 18255 18256 18257 18258 18259 18260 18261 18262 18263 18264 18265 18266 18267 18268 18269 18270 18271 18272 18273 18274 18275 18276 18277 18278 18279 18280 18281 18282 18283 18284 18285 18286 18287 18288 18289 18290 18291 18292 18293 18294 18295 18296 18297 18298 18299 18300 18301 18302 18303 18304 18305 18306 18307 18308 18309 18310 18311 18312 18313 18314 18315 18316 18317 18318 18319 18320 18321 18322 18323 18324 18325 18326 18327 18328 18329 18330 18331 18332 18333 18334 18335 18336 18337 18338 18339 18340 18341 18342 18343 18344 18345 18346 18347 18348 18349 18350 18351 18352 18353 18354 18355 18356 18357 18358 18359 18360 18361 18362 18363 18364 18365 18366 18367 18368 18369 18370 18371 18372 18373 18374 18375 18376 18377 18378 18379 18380 18381 18382 18383 18384 18385 18386 18387 18388 18389 18390 18391 18392 18393 18394 18395 18396 18397 18398 18399 18400 18401 18402 18403 18404 18405 18406 18407 18408 18409 18410 18411 18412 18413 18414 18415 18416 18417 18418 18419 18420 18421 18422 18423 18424 18425 18426 18427 18428 18429 18430 18431 18432 18433 18434 18435 18436 18437 18438 18439 18440 18441 18442 18443 18444 18445 18446 18447 18448 18449 18450 18451 18452 18453 18454 18455 18456 18457 18458 18459 18460 18461 18462 18463 18464 18465 18466 18467 18468 18469 18470 18471 18472 18473 18474 18475 18476 18477 18478 18479 18480 18481 18482 18483 18484 18485 18486 18487 18488 18489 18490 18491 18492 18493 18494 18495 18496 18497 18498 18499 18500 18501 18502 18503 18504 18505 18506 18507 18508 18509 18510 18511 18512 18513 18514 18515 18516 18517 18518 18519 18520 18521 18522 18523 18524 18525 18526 18527 18528 18529 18530 18531 18532 18533 18534 18535 18536 18537 18538 18539 18540 18541 18542 18543 18544 18545 18546 18547 18548 18549 18550 18551 18552 18553 18554 18555 18556 18557 18558 18559 18560 18561 18562 18563 18564 18565 18566 18567 18568 18569 18570 18571 18572 18573 18574 18575 18576 18577 18578 18579 18580 18581 18582 18583 18584 18585 18586 18587 18588 18589 18590 18591 18592 18593 18594 18595 18596 18597 18598 18599 18600 18601 18602 18603 18604 18605 18606 18607 18608 18609 18610 18611 18612 18613 18614 18615 18616 18617 18618 18619 18620 18621 18622 18623 18624 18625 18626 18627 18628 18629 18630 18631 18632 18633 18634 18635 18636 18637 18638 18639 18640 18641 18642 18643 18644 18645 18646 18647 18648 18649 18650 18651 18652 18653 18654 18655 18656 18657 18658 18659 18660 18661 18662 18663 18664 18665 18666 18667 18668 18669 18670 18671 18672 18673 18674 18675 18676 18677 18678 18679 18680 18681 18682 18683 18684 18685 18686 18687 18688 18689 18690 18691 18692 18693 18694
|
// Copyright (C) 2009-2018 Brazil
// Copyright (C) 2018-2025 Sutou Kouhei <kou@clear-code.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "grn.h"
#include <fcntl.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#ifdef WIN32
# include <io.h>
# include <share.h>
#endif /* WIN32 */
#include "grn_ctx.hpp"
#include "grn_ii.h"
#include "grn_ii_select_cursor.h"
#include "grn_ctx_impl.h"
#include "grn_table.h"
#include "grn_token_cursor.h"
#include "grn_pat.h"
#include "grn_db.h"
#include "grn_obj.h"
#include "grn_output.h"
#include "grn_progress.h"
#include "grn_report.h"
#include "grn_scorer.h"
#include "grn_util.h"
#include "grn_posting.h"
#include "grn_vector.h"
#include "grn_selector.h"
#include "grn_wal.h"
#include <condition_variable>
#include <map>
#include <memory>
#include <mutex>
#include <vector>
#ifdef GRN_SUPPORT_REGEXP
# define GRN_II_SELECT_ENABLE_SEQUENTIAL_SEARCH_TEXT
#endif
#ifdef GRN_II_SELECT_ENABLE_SEQUENTIAL_SEARCH_TEXT
# include "grn_string.h"
# include "grn_onigmo.h"
#endif
/* #define GRN_II_TOKEN_INFO_DEBUG */
#ifdef GRN_II_TOKEN_INFO_DEBUG
# define P_NOTE(...) printf(__VA_ARGS__)
# define P_TI(ctx, token_info) grn_p_token_info((ctx), (token_info))
# define P_TIS(ctx, token_info, n) grn_p_token_infos((ctx), (token_info), (n))
#else
# define P_NOTE(...)
# define P_TI(ctx, token_info)
# define P_TIS(ctx, token_info, n)
#endif
/* P is for physical? */
#define MAX_PSEG 0x20000
#define MAX_PSEG_SMALL 0x00200
/* MAX_PSEG_MEDIUM has enough space for the following source:
* * Single source.
* * Source is a fixed size column or _key of a table.
* * Source column is a scalar column.
* * Lexicon doesn't have tokenizer.
*/
#define MAX_PSEG_MEDIUM 0x10000
#define MAX_PSEG_LARGE 0x40000
#define S_CHUNK (1 << GRN_II_W_CHUNK)
#define W_SEGMENT 18
#define S_SEGMENT (1 << W_SEGMENT)
#define W_ARRAY_ELEMENT 3
#define S_ARRAY_ELEMENT (1 << W_ARRAY_ELEMENT)
#define W_ARRAY (W_SEGMENT - W_ARRAY_ELEMENT)
#define ARRAY_MASK_IN_A_SEGMENT ((1 << W_ARRAY) - 1)
#define S_GARBAGE (1 << 12)
#define CHUNK_SPLIT 0x80000000
#define CHUNK_SPLIT_THRESHOLD 0x60000
#define CHUNK_SPLIT_OFF(tid) ((tid) & ~CHUNK_SPLIT)
#define MAX_N_ELEMENTS 5
#ifndef S_IRUSR
# define S_IRUSR 0400
#endif /* S_IRUSR */
#ifndef S_IWUSR
# define S_IWUSR 0200
#endif /* S_IWUSR */
static bool grn_ii_cursor_set_min_enable = true;
static double grn_ii_select_too_many_index_match_ratio_text = -1;
static double grn_ii_select_too_many_index_match_ratio_reference = -1;
static double grn_ii_estimate_size_for_query_reduce_ratio = 0.9;
static bool grn_ii_overlap_token_skip_enable = false;
static uint32_t grn_ii_builder_block_threshold_force = 0;
static uint32_t grn_ii_max_n_segments_small = MAX_PSEG_SMALL;
static uint32_t grn_ii_max_n_chunks_small = GRN_II_MAX_CHUNK_SMALL;
static int64_t grn_ii_reduce_expire_threshold = 32;
static bool grn_ii_dump_index_source_on_merge = false;
extern "C" void
grn_ii_init_from_env(void)
{
{
char grn_ii_cursor_set_min_enable_env[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_CURSOR_SET_MIN_ENABLE",
grn_ii_cursor_set_min_enable_env,
GRN_ENV_BUFFER_SIZE);
if (strcmp(grn_ii_cursor_set_min_enable_env, "no") == 0) {
grn_ii_cursor_set_min_enable = false;
} else {
grn_ii_cursor_set_min_enable = true;
}
}
{
char grn_ii_select_too_many_index_match_ratio_text_env[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_SELECT_TOO_MANY_INDEX_MATCH_RATIO_TEXT",
grn_ii_select_too_many_index_match_ratio_text_env,
GRN_ENV_BUFFER_SIZE);
if (grn_ii_select_too_many_index_match_ratio_text_env[0]) {
grn_ii_select_too_many_index_match_ratio_text =
atof(grn_ii_select_too_many_index_match_ratio_text_env);
} else {
/* For backward compatibility. */
char grn_ii_select_too_many_index_match_ratio_env[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_SELECT_TOO_MANY_INDEX_MATCH_RATIO",
grn_ii_select_too_many_index_match_ratio_env,
GRN_ENV_BUFFER_SIZE);
if (grn_ii_select_too_many_index_match_ratio_env[0]) {
grn_ii_select_too_many_index_match_ratio_text =
atof(grn_ii_select_too_many_index_match_ratio_env);
}
}
}
{
char grn_ii_select_too_many_index_match_ratio_reference_env
[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_SELECT_TOO_MANY_INDEX_MATCH_RATIO_REFERENCE",
grn_ii_select_too_many_index_match_ratio_reference_env,
GRN_ENV_BUFFER_SIZE);
if (grn_ii_select_too_many_index_match_ratio_reference_env[0]) {
grn_ii_select_too_many_index_match_ratio_reference =
atof(grn_ii_select_too_many_index_match_ratio_reference_env);
}
}
{
char grn_ii_estimate_size_for_query_reduce_ratio_env[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_ESTIMATE_SIZE_FOR_QUERY_REDUCE_RATIO",
grn_ii_estimate_size_for_query_reduce_ratio_env,
GRN_ENV_BUFFER_SIZE);
if (grn_ii_estimate_size_for_query_reduce_ratio_env[0]) {
grn_ii_estimate_size_for_query_reduce_ratio =
atof(grn_ii_estimate_size_for_query_reduce_ratio_env);
}
}
{
char grn_ii_overlap_token_skip_enable_env[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_OVERLAP_TOKEN_SKIP_ENABLE",
grn_ii_overlap_token_skip_enable_env,
GRN_ENV_BUFFER_SIZE);
if (strcmp(grn_ii_overlap_token_skip_enable_env, "yes") == 0) {
grn_ii_overlap_token_skip_enable = true;
} else {
grn_ii_overlap_token_skip_enable = false;
}
}
{
char grn_ii_builder_block_threshold_env[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_BUILDER_BLOCK_THRESHOLD",
grn_ii_builder_block_threshold_env,
GRN_ENV_BUFFER_SIZE);
if (grn_ii_builder_block_threshold_env[0]) {
grn_ii_builder_block_threshold_force =
grn_atoui(grn_ii_builder_block_threshold_env,
grn_ii_builder_block_threshold_env +
strlen(grn_ii_builder_block_threshold_env),
NULL);
} else {
grn_ii_builder_block_threshold_force = 0;
}
}
{
char grn_ii_max_n_segments_small_env[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_MAX_N_SEGMENTS_SMALL",
grn_ii_max_n_segments_small_env,
GRN_ENV_BUFFER_SIZE);
if (grn_ii_max_n_segments_small_env[0]) {
grn_ii_max_n_segments_small =
grn_atoui(grn_ii_max_n_segments_small_env,
grn_ii_max_n_segments_small_env +
strlen(grn_ii_max_n_segments_small_env),
NULL);
if (grn_ii_max_n_segments_small > MAX_PSEG) {
grn_ii_max_n_segments_small = MAX_PSEG;
}
}
}
{
char grn_ii_max_n_chunks_small_env[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_MAX_N_CHUNKS_SMALL",
grn_ii_max_n_chunks_small_env,
GRN_ENV_BUFFER_SIZE);
if (grn_ii_max_n_chunks_small_env[0]) {
grn_ii_max_n_chunks_small = grn_atoui(
grn_ii_max_n_chunks_small_env,
grn_ii_max_n_chunks_small_env + strlen(grn_ii_max_n_chunks_small_env),
NULL);
if (grn_ii_max_n_chunks_small > GRN_II_MAX_CHUNK) {
grn_ii_max_n_chunks_small = GRN_II_MAX_CHUNK;
}
}
}
{
char grn_ii_reduce_expire_threshold_env[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_REDUCE_EXPIRE_THRESHOLD",
grn_ii_reduce_expire_threshold_env,
GRN_ENV_BUFFER_SIZE);
if (grn_ii_reduce_expire_threshold_env[0]) {
grn_ii_reduce_expire_threshold =
grn_atoll(grn_ii_reduce_expire_threshold_env,
grn_ii_reduce_expire_threshold_env +
strlen(grn_ii_reduce_expire_threshold_env),
NULL);
}
}
{
char grn_ii_dump_index_source_on_merge_env[GRN_ENV_BUFFER_SIZE];
grn_getenv("GRN_II_DUMP_INDEX_SOURCE_ON_MERGE",
grn_ii_dump_index_source_on_merge_env,
GRN_ENV_BUFFER_SIZE);
if (strcmp(grn_ii_dump_index_source_on_merge_env, "yes") == 0) {
grn_ii_dump_index_source_on_merge = true;
} else {
grn_ii_dump_index_source_on_merge = false;
}
}
}
extern "C" void
grn_ii_cursor_set_min_enable_set(bool enable)
{
grn_ii_cursor_set_min_enable = enable;
}
extern "C" bool
grn_ii_cursor_set_min_enable_get(void)
{
return grn_ii_cursor_set_min_enable;
}
static void
grn_ii_get_term(grn_ctx *ctx, grn_ii *ii, grn_id tid, grn_obj *term)
{
if (tid == GRN_ID_NIL) {
return;
}
char key[GRN_TABLE_MAX_KEY_SIZE];
int key_size;
key_size =
grn_table_get_key(ctx, ii->lexicon, tid, key, GRN_TABLE_MAX_KEY_SIZE);
if (key_size != 0) {
grn_obj key_buf;
GRN_OBJ_INIT(&key_buf,
GRN_BULK,
GRN_OBJ_DO_SHALLOW_COPY,
ii->lexicon->header.domain);
GRN_TEXT_SET(ctx, &key_buf, key, key_size);
grn_inspect(ctx, term, &key_buf);
GRN_OBJ_FIN(ctx, &key_buf);
}
}
/* segment */
static inline uint32_t
grn_ii_get_array_pseg_inline(grn_ii *ii, uint32_t lseg)
{
if (lseg < GRN_II_MAX_LSEG) {
return ii->header.common->ainfo[lseg];
} else {
return ii->header.large->ainfo_extend[lseg - GRN_II_MAX_LSEG];
}
}
static inline void
grn_ii_set_array_pseg_inline(grn_ii *ii, uint32_t lseg, uint32_t pseg)
{
if (lseg < GRN_II_MAX_LSEG) {
ii->header.common->ainfo[lseg] = pseg;
} else {
ii->header.large->ainfo_extend[lseg - GRN_II_MAX_LSEG] = pseg;
}
}
static inline uint32_t
grn_ii_get_buffer_pseg_inline(grn_ii *ii, uint32_t lseg)
{
if (lseg < GRN_II_MAX_LSEG) {
return ii->header.common->binfo[lseg];
} else {
return ii->header.large->binfo_extend[lseg - GRN_II_MAX_LSEG];
}
}
static inline uint32_t *
grn_ii_get_buffer_pseg_address_inline(grn_ii *ii, uint32_t lseg)
{
if (lseg < GRN_II_MAX_LSEG) {
return &(ii->header.common->binfo[lseg]);
} else {
return &(ii->header.large->binfo_extend[lseg - GRN_II_MAX_LSEG]);
}
}
static inline void
grn_ii_set_buffer_pseg_inline(grn_ii *ii, uint32_t lseg, uint32_t pseg)
{
if (lseg < GRN_II_MAX_LSEG) {
ii->header.common->binfo[lseg] = pseg;
} else {
ii->header.large->binfo_extend[lseg - GRN_II_MAX_LSEG] = pseg;
}
}
static inline uint32_t
grn_ii_n_logical_segments_inline(grn_ii *ii)
{
if (ii->header.common->flags & GRN_OBJ_INDEX_LARGE) {
return GRN_II_MAX_LSEG + GRN_II_MAX_LSEG_EXTEND;
} else {
return GRN_II_MAX_LSEG;
}
}
static inline uint32_t
segment_get(grn_ctx *ctx, grn_ii *ii)
{
grn_ii_header_common *header = ii->header.common;
uint32_t pseg;
if (header->bgqtail == ((header->bgqhead + 1) & (GRN_II_BGQSIZE - 1))) {
pseg = header->bgqbody[header->bgqtail];
header->bgqtail = (header->bgqtail + 1) & (GRN_II_BGQSIZE - 1);
} else {
pseg = header->pnext;
if (!pseg) {
uint32_t i;
uint32_t pmax = 0;
const uint32_t max_segment = ii->seg->header->max_segment;
auto used = static_cast<char *>(GRN_CALLOC(max_segment));
if (!used) {
return max_segment;
}
const uint32_t n_logical_segments = grn_ii_n_logical_segments_inline(ii);
for (i = 0; i < n_logical_segments; i++) {
pseg = grn_ii_get_array_pseg_inline(ii, i);
if (pseg != GRN_II_PSEG_NOT_ASSIGNED) {
if (pseg > pmax) {
pmax = pseg;
}
used[pseg] = 1;
}
pseg = grn_ii_get_buffer_pseg_inline(ii, i);
if (pseg != GRN_II_PSEG_NOT_ASSIGNED) {
if (pseg > pmax) {
pmax = pseg;
}
used[pseg] = 1;
}
}
for (pseg = 0; pseg < max_segment && used[pseg]; pseg++)
;
GRN_FREE(used);
header->pnext = pmax + 1;
} else if (ii->header.common->pnext < ii->seg->header->max_segment) {
header->pnext++;
}
}
return pseg;
}
static inline grn_rc
segment_get_clear(grn_ctx *ctx, grn_ii *ii, uint32_t *pseg)
{
uint32_t seg = segment_get(ctx, ii);
if (seg < ii->seg->header->max_segment) {
void *p = grn_io_seg_ref(ctx, ii->seg, seg);
if (!p) {
return GRN_NO_MEMORY_AVAILABLE;
}
memset(p, 0, S_SEGMENT);
grn_io_seg_unref(ctx, ii->seg, seg);
*pseg = seg;
return GRN_SUCCESS;
} else {
return GRN_NO_MEMORY_AVAILABLE;
}
}
static inline grn_rc
buffer_segment_new(grn_ctx *ctx, grn_ii *ii, uint32_t *segno)
{
uint32_t lseg, pseg;
if (*segno == GRN_II_PSEG_NOT_ASSIGNED) {
const uint32_t n_logical_segments = grn_ii_n_logical_segments_inline(ii);
for (lseg = 0; lseg < n_logical_segments; lseg++) {
if (grn_ii_get_buffer_pseg_inline(ii, lseg) == GRN_II_PSEG_NOT_ASSIGNED) {
break;
}
}
if (lseg == n_logical_segments) {
return GRN_NO_MEMORY_AVAILABLE;
}
*segno = lseg;
} else {
if (grn_ii_get_buffer_pseg_inline(ii, *segno) != GRN_II_PSEG_NOT_ASSIGNED) {
return GRN_INVALID_ARGUMENT;
}
lseg = *segno;
}
pseg = segment_get(ctx, ii);
if (pseg < ii->seg->header->max_segment) {
grn_ii_set_buffer_pseg_inline(ii, lseg, pseg);
if (lseg >= ii->header.common->bmax) {
ii->header.common->bmax = lseg + 1;
}
return GRN_SUCCESS;
} else {
return GRN_NO_MEMORY_AVAILABLE;
}
}
static grn_rc
buffer_segment_reserve(grn_ctx *ctx,
grn_ii *ii,
uint32_t *lseg0,
uint32_t *pseg0,
uint32_t *lseg1,
uint32_t *pseg1)
{
uint32_t i = 0;
const uint32_t n_logical_segments = grn_ii_n_logical_segments_inline(ii);
for (;; i++) {
if (i == n_logical_segments) {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][segment][reserve] "
"couldn't find a free buffer: <%.*s>: max:<%u>",
name_size,
name,
n_logical_segments);
return ctx->rc;
}
if (grn_ii_get_buffer_pseg_inline(ii, i) == GRN_II_PSEG_NOT_ASSIGNED) {
break;
}
}
*lseg0 = i++;
for (;; i++) {
if (i == n_logical_segments) {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][segment][reserve] "
"couldn't find two free buffers: "
"<%.*s>: "
"found:<%u>, max:<%u>",
name_size,
name,
*lseg0,
n_logical_segments);
return ctx->rc;
}
if (grn_ii_get_buffer_pseg_inline(ii, i) == GRN_II_PSEG_NOT_ASSIGNED) {
break;
}
}
*lseg1 = i;
if ((*pseg0 = segment_get(ctx, ii)) == ii->seg->header->max_segment) {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][segment][reserve] "
"couldn't allocate a free segment: <%.*s>: "
"buffer:<%u>, max:<%u>",
name_size,
name,
*lseg0,
ii->seg->header->max_segment);
return ctx->rc;
}
if ((*pseg1 = segment_get(ctx, ii)) == ii->seg->header->max_segment) {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][segment][reserve] "
"couldn't allocate two free segments: "
"<%.*s>: "
"found:<%u>, not-found:<%u>, max:<%u>",
name_size,
name,
*lseg0,
*lseg1,
ii->seg->header->max_segment);
return ctx->rc;
}
return ctx->rc;
}
static inline void
buffer_garbage_enqueue(grn_ii *ii, uint32_t lseg)
{
const uint32_t pseg = grn_ii_get_buffer_pseg_inline(ii, lseg);
if (pseg != GRN_II_PSEG_NOT_ASSIGNED) {
grn_ii_header_common *header = ii->header.common;
header->bgqbody[header->bgqhead] = header->binfo[lseg];
header->bgqhead = (header->bgqhead + 1) & (GRN_II_BGQSIZE - 1);
GRN_ASSERT(header->bgqhead != header->bgqtail);
}
}
static inline void
buffer_segment_update(grn_ii *ii, uint32_t lseg, uint32_t pseg)
{
buffer_garbage_enqueue(ii, lseg);
// smb_wmb();
grn_ii_set_buffer_pseg_inline(ii, lseg, pseg);
if (lseg >= ii->header.common->bmax) {
ii->header.common->bmax = lseg + 1;
}
}
static inline void
buffer_segment_clear(grn_ii *ii, uint32_t lseg)
{
buffer_garbage_enqueue(ii, lseg);
// smb_wmb();
grn_ii_set_buffer_pseg_inline(ii, lseg, GRN_II_PSEG_NOT_ASSIGNED);
}
/* chunk */
#define HEADER_CHUNK_AT(ii, offset) \
((((ii)->header.common->chunks[((offset) >> 3)]) >> ((offset) & 7)) & 1)
#define HEADER_CHUNK_ON(ii, offset) \
(((ii)->header.common->chunks[((offset) >> 3)]) |= (1 << ((offset) & 7)))
#define HEADER_CHUNK_OFF(ii, offset) \
(((ii)->header.common->chunks[((offset) >> 3)]) &= ~(1 << ((offset) & 7)))
#define N_GARBAGES_TH 1
#define N_GARBAGES ((S_GARBAGE - (sizeof(uint32_t) * 4)) / (sizeof(uint32_t)))
typedef struct {
uint32_t head;
uint32_t tail;
uint32_t nrecs;
uint32_t next;
uint32_t recs[N_GARBAGES];
} grn_ii_ginfo;
#define WIN_MAP(ctx, chunk, iw, seg, pos, size, mode) \
grn_io_win_map(ctx, \
chunk, \
iw, \
((seg) >> GRN_II_N_CHUNK_VARIATION), \
(((seg) & ((1 << GRN_II_N_CHUNK_VARIATION) - 1)) \
<< GRN_II_W_LEAST_CHUNK) + \
(pos), \
size, \
mode)
/*
static int new_histogram[32];
static int free_histogram[32];
*/
/*
N = grn_ii_max_n_chunks_small for GRN_OBJ_INDEX_SMALL
N = GRN_II_MAX_N_CHUNKS_MEDIUM for GRN_OBJ_INDEX_MEDIUM
N = GRN_II_MAX_N_CHUNKS for GRN_OBJ_INDEX_LARGE
N = GRN_II_MAX_N_CHUNKS without index size flag
S_CHUNK = (1 << GRN_II_W_CHUNK(22)) = 4MiB
+------------+ +------------+
| chunk0 | ... | chunkN |
+------------+ +------------+
|<--S_CHUNK->|
ii->header.common->chunks has flags whether the target chunk is
assigned or not. We can manipulate the flags by HEADER_CHUNK_AT(),
HEADER_CHUNK_ON() and HEADER_CHUNK_OFF(). Here is a snapshot when we
only assign chunk0 and chunk2:
+------------+------------+------------+ +------------+
| chunk0 | chunk1 | chunk2 | ... | chunkN |
+------------+------------+------------+ +------------+
ON OFF ON OFF
If requested size is greater than S_CHUNK, continuous non-assigned
chunks are assigned. For example, if S_CHUNK + 100 is requested and
chunk1 is assigned, chunk0 isn't assigned. chunk2 and chunk3 are
assigned.
+------------+------------+------------+------------+ +------------+
| chunk0 | chunk1 | chunk2 | chunk3 | ... | chunkN |
+------------+------------+------------+------------+ +------------+
OFF ON OFF -> ON OFF -> ON OFF
If requested size is smaller than or equal to S_CHUNK, a subchunk is
assigned. Subchunks in the same chunk has the same size. The
smallest subchunk size is (1 << GRN_II_W_LEAST_CHUNK(8)) = 256
bytes. Subchunk size is power of 2. For example, chunk0 uses 512
bytes for subchunk size, chunk0 can have 812 (S_CHUNK / 512)
subchunks.
+-------------+
| chunk0 |
+-------------+
<->|<->|...|<-> subchunks
512|512| |512
If one of subchunk is assigned, HEADER_CHUNK_AT() of the chunk is true.
+-------------+
| chunk0 |
+-------------+
<------>|<->|... subchunks
ASSIGNED|NOT|
ON
Chunks that are assigning subchunks are keeping in
ii->header.common.free_chunks. free_chunks keeps a chunk per
subchunk size. For example, chunk0 is kept for 256 subchunk size and
chunk1 is kept for 512 subchunk size.
chunk_new() returns the (offset >> GRN_II_W_LEAST_CHUNK(8)) of
subchunk not the index of chunk. For example, if the third subchunk
of chunk0 that uses 512 bytes for subchunk size is assigned, 4 (1024
>> 8) is returned.
+-------------+
| chunk0 |
+-------------+
<0>|<1>|<2>|...
0 2 4
chunk_new() wants to reuse freed subchunks as much as possible to
reduce disk usage. ii->header.common.garbages keeps freed subchunks
to reuse later.
TODO: More description for garbages.
*/
static grn_rc
chunk_free(grn_ctx *ctx, grn_ii *ii, uint32_t offset, uint32_t size);
static grn_rc
chunk_new(grn_ctx *ctx, grn_ii *ii, uint32_t *res, uint32_t size)
{
uint32_t n_chunks;
n_chunks = ii->chunk->header->max_segment;
/*
if (size) {
int es = size - 1;
grn_bit_scan_rev_result m;
GRN_BIT_SCAN_REV(es, m);
m++;
new_histogram[m]++;
}
*/
if (size > S_CHUNK) {
uint32_t i, j;
uint32_t n = (size + S_CHUNK - 1) >> GRN_II_W_CHUNK;
for (i = 0, j = -1; i < n_chunks; i++) {
if (HEADER_CHUNK_AT(ii, i)) {
j = i;
} else {
if (i == j + n) {
j++;
*res = j << GRN_II_N_CHUNK_VARIATION;
for (; j <= i; j++) {
HEADER_CHUNK_ON(ii, j);
}
return GRN_SUCCESS;
}
}
}
{
GRN_DEFINE_NAME(ii);
MERR("[ii][chunk][new] index is full: "
"<%.*s>: "
"size:<%u>, n-chunks:<%u>",
name_size,
name,
size,
n_chunks);
}
return ctx->rc;
} else {
uint32_t *vp;
grn_bit_scan_rev_result m;
if (size > (1 << GRN_II_W_LEAST_CHUNK)) {
int es = size - 1;
GRN_BIT_SCAN_REV(es, m);
m++;
} else {
m = GRN_II_W_LEAST_CHUNK;
}
grn_ii_header_common *header = ii->header.common;
if (header->ngarbages[m - GRN_II_W_LEAST_CHUNK] > N_GARBAGES_TH) {
uint32_t *gseg;
grn_io_win iw, iw_;
iw_.addr = NULL;
gseg = &header->garbages[m - GRN_II_W_LEAST_CHUNK];
while (*gseg != GRN_II_PSEG_NOT_ASSIGNED) {
auto ginfo = static_cast<grn_ii_ginfo *>(
WIN_MAP(ctx, ii->chunk, &iw, *gseg, 0, S_GARBAGE, GRN_IO_RDWR));
// GRN_IO_SEG_MAP2(ii->chunk, *gseg, ginfo);
if (!ginfo) {
if (iw_.addr) {
grn_io_win_unmap(ctx, &iw_);
}
{
GRN_DEFINE_NAME(ii);
MERR("[ii][chunk][new] failed to allocate garbage segment: "
"<%.*s>: "
"n-garbages:<%u>, size:<%u>, n-chunks:<%u>",
name_size,
name,
header->ngarbages[m - GRN_II_W_LEAST_CHUNK],
size,
n_chunks);
}
return ctx->rc;
}
if (ginfo->next != GRN_II_PSEG_NOT_ASSIGNED ||
ginfo->nrecs > N_GARBAGES_TH) {
*res = ginfo->recs[ginfo->tail];
if (++ginfo->tail == N_GARBAGES) {
ginfo->tail = 0;
}
ginfo->nrecs--;
header->ngarbages[m - GRN_II_W_LEAST_CHUNK]--;
uint32_t current_garbage_segment = *gseg;
bool no_garbage_info = (ginfo->nrecs == 0);
if (no_garbage_info) {
*gseg = ginfo->next;
}
if (iw_.addr) {
grn_io_win_unmap(ctx, &iw_);
}
grn_io_win_unmap(ctx, &iw);
if (no_garbage_info) {
chunk_free(ctx, ii, current_garbage_segment, S_GARBAGE);
}
return GRN_SUCCESS;
}
if (iw_.addr) {
grn_io_win_unmap(ctx, &iw_);
}
iw_ = iw;
gseg = &ginfo->next;
}
if (iw_.addr) {
grn_io_win_unmap(ctx, &iw_);
}
}
vp = &header->free_chunks[m - GRN_II_W_LEAST_CHUNK];
if (*vp == GRN_II_PSEG_NOT_ASSIGNED) {
uint32_t i = 0;
while (HEADER_CHUNK_AT(ii, i)) {
if (++i >= n_chunks) {
GRN_DEFINE_NAME(ii);
MERR("[ii][chunk][new] failed to find a free chunk: "
"<%.*s>: "
"index:<%u>, size:<%u>, n-chunks:<%u>",
name_size,
name,
m - GRN_II_W_LEAST_CHUNK,
size,
n_chunks);
return ctx->rc;
}
}
HEADER_CHUNK_ON(ii, i);
*vp = i << GRN_II_N_CHUNK_VARIATION;
}
*res = *vp;
/* Next subchunk. */
*vp += 1 << (m - GRN_II_W_LEAST_CHUNK);
/* (*vp % GRN_II_N_CHUNK_VARIATION) == 0 */
if (!(*vp & ((1 << GRN_II_N_CHUNK_VARIATION) - 1))) {
/* All subchunks are consumed. */
*vp = GRN_II_PSEG_NOT_ASSIGNED;
}
return GRN_SUCCESS;
}
}
static grn_rc
chunk_free(grn_ctx *ctx, grn_ii *ii, uint32_t offset, uint32_t size)
{
/*
if (size) {
int es = size - 1;
grn_bit_scan_rev_result m;
GRN_BIT_SCAN_REV(es, m);
m++;
free_histogram[m]++;
}
*/
grn_io_win iw, iw_;
grn_ii_ginfo *ginfo;
uint32_t seg, *gseg;
seg = offset >> GRN_II_N_CHUNK_VARIATION;
if (size > S_CHUNK) {
int n = (size + S_CHUNK - 1) >> GRN_II_W_CHUNK;
for (; n--; seg++) {
HEADER_CHUNK_OFF(ii, seg);
}
return GRN_SUCCESS;
}
grn_bit_scan_rev_result m;
if (size > (1 << GRN_II_W_LEAST_CHUNK)) {
int es = size - 1;
GRN_BIT_SCAN_REV(es, m);
m++;
} else {
m = GRN_II_W_LEAST_CHUNK;
}
gseg = &ii->header.common->garbages[m - GRN_II_W_LEAST_CHUNK];
iw_.addr = NULL;
while (*gseg != GRN_II_PSEG_NOT_ASSIGNED) {
ginfo = static_cast<grn_ii_ginfo *>(
WIN_MAP(ctx, ii->chunk, &iw, *gseg, 0, S_GARBAGE, GRN_IO_RDWR));
// GRN_IO_SEG_MAP2(ii->chunk, *gseg, ginfo);
if (!ginfo) {
if (iw_.addr) {
grn_io_win_unmap(ctx, &iw_);
}
return GRN_NO_MEMORY_AVAILABLE;
}
if (ginfo->nrecs < N_GARBAGES) {
break;
}
if (iw_.addr) {
grn_io_win_unmap(ctx, &iw_);
}
iw_ = iw;
gseg = &ginfo->next;
}
if (*gseg == GRN_II_PSEG_NOT_ASSIGNED) {
grn_rc rc;
if ((rc = chunk_new(ctx, ii, gseg, S_GARBAGE))) {
if (iw_.addr) {
grn_io_win_unmap(ctx, &iw_);
}
return rc;
}
ginfo = static_cast<grn_ii_ginfo *>(
WIN_MAP(ctx, ii->chunk, &iw, *gseg, 0, S_GARBAGE, GRN_IO_RDWR));
/*
uint32_t i = 0;
while (HEADER_CHUNK_AT(ii, i)) {
if (++i >= ii->chunk->header->max_segment) {
return GRN_NO_MEMORY_AVAILABLE;
}
}
HEADER_CHUNK_ON(ii, i);
*gseg = i;
GRN_IO_SEG_MAP2(ii->chunk, *gseg, ginfo);
*/
if (!ginfo) {
if (iw_.addr) {
grn_io_win_unmap(ctx, &iw_);
}
return GRN_NO_MEMORY_AVAILABLE;
}
ginfo->head = 0;
ginfo->tail = 0;
ginfo->nrecs = 0;
ginfo->next = GRN_II_PSEG_NOT_ASSIGNED;
}
if (iw_.addr) {
grn_io_win_unmap(ctx, &iw_);
}
ginfo->recs[ginfo->head] = offset;
if (++ginfo->head == N_GARBAGES) {
ginfo->head = 0;
}
ginfo->nrecs++;
grn_io_win_unmap(ctx, &iw);
ii->header.common->ngarbages[m - GRN_II_W_LEAST_CHUNK]++;
return GRN_SUCCESS;
}
#define DATA_USE_P_FOR_ENC (1 << 0) /* Use PFor */
#define DATA_ODD (1 << 2) /* Variable size data */
typedef struct {
uint32_t *data;
uint32_t data_size;
uint32_t flags;
} datavec;
static grn_rc
datavec_reset(
grn_ctx *ctx, grn_ii *ii, datavec *dv, size_t unitsize, size_t totalsize)
{
uint32_t i;
if (!dv[0].data || dv[ii->n_elements].data < dv[0].data + totalsize) {
if (dv[0].data) {
GRN_FREE(dv[0].data);
}
if (!(dv[0].data = static_cast<uint32_t *>(
GRN_MALLOC(totalsize * sizeof(uint32_t))))) {
MERR("[ii][data-vector][reset] failed to allocate data: "
"length:<%u>, "
"unit-size:<%" GRN_FMT_SIZE ">, "
"total-size:<%" GRN_FMT_SIZE ">",
ii->n_elements,
unitsize,
totalsize);
return ctx->rc;
}
dv[ii->n_elements].data = dv[0].data + totalsize;
}
for (i = 1; i < ii->n_elements; i++) {
dv[i].data = dv[i - 1].data + unitsize;
}
return GRN_SUCCESS;
}
static grn_rc
datavec_init(
grn_ctx *ctx, grn_ii *ii, datavec *dv, size_t unitsize, size_t totalsize)
{
uint32_t i;
if (totalsize == 0) {
memset(dv, 0, sizeof(datavec) * (ii->n_elements + 1));
} else {
if (!(dv[0].data = static_cast<uint32_t *>(
GRN_MALLOC(totalsize * sizeof(uint32_t))))) {
MERR("[ii][data-vector][init] failed to allocate data: "
"length:<%u>, "
"unit-size:<%" GRN_FMT_SIZE ">, "
"total-size:<%" GRN_FMT_SIZE ">",
ii->n_elements,
unitsize,
totalsize);
return ctx->rc;
}
dv[ii->n_elements].data = dv[0].data + totalsize;
dv[0].flags = 0;
for (i = 1; i < ii->n_elements; i++) {
dv[i].data = dv[i - 1].data + unitsize;
dv[i].flags = 0;
}
}
if (ii->header.common->flags & GRN_OBJ_WITH_POSITION) {
dv[ii->n_elements - 1].flags = DATA_ODD;
}
return GRN_SUCCESS;
}
static void
datavec_fin(grn_ctx *ctx, datavec *dv)
{
if (dv[0].data) {
GRN_FREE(dv[0].data);
}
}
static inline bool
data_records_use_p_for_enc(uint32_t n_records, uint32_t max_record_id)
{
return n_records >= 16 && (n_records > (max_record_id >> 8));
}
static inline bool
data_sections_use_p_for_enc(uint32_t n_records)
{
return n_records >= 3;
}
static inline bool
data_positions_use_p_for_enc(uint32_t n_positions, uint32_t max_position)
{
return n_positions >= 32 && n_positions > (max_position >> 13);
}
static inline void
datavec_set_data_size(grn_ctx *ctx,
grn_ii *ii,
datavec *dv,
uint32_t n_records,
grn_id max_record_id,
uint32_t n_positions,
uint32_t max_position)
{
int i = 0;
bool records_use_p_for_enc =
data_records_use_p_for_enc(n_records, max_record_id);
bool sections_use_p_for_enc = data_sections_use_p_for_enc(n_records);
/* record IDs */
dv[i].data_size = n_records;
if (records_use_p_for_enc) {
dv[i++].flags |= DATA_USE_P_FOR_ENC;
} else {
dv[i++].flags &= ~DATA_USE_P_FOR_ENC;
}
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
/* section IDs */
dv[i].data_size = n_records;
if (sections_use_p_for_enc) {
dv[i++].flags |= DATA_USE_P_FOR_ENC;
} else {
dv[i++].flags &= ~DATA_USE_P_FOR_ENC;
}
}
/* term frequencies */
dv[i].data_size = n_records;
if (sections_use_p_for_enc) {
dv[i++].flags |= DATA_USE_P_FOR_ENC;
} else {
dv[i++].flags &= ~DATA_USE_P_FOR_ENC;
}
if ((ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
/* weights */
dv[i].data_size = n_records;
if (sections_use_p_for_enc) {
dv[i++].flags |= DATA_USE_P_FOR_ENC;
} else {
dv[i++].flags &= ~DATA_USE_P_FOR_ENC;
}
}
if (ii->header.common->flags & GRN_OBJ_WITH_POSITION) {
/* positions */
dv[i].data_size = n_positions;
if (data_positions_use_p_for_enc(n_positions, max_position)) {
dv[i++].flags |= DATA_USE_P_FOR_ENC;
} else {
dv[i++].flags &= ~DATA_USE_P_FOR_ENC;
}
}
}
typedef struct {
grn_ii *ii;
grn_id term_id;
uint32_t nth_element;
uint32_t n_odd_values;
uint32_t *values;
uint32_t n_values;
uint32_t flags;
size_t offset;
size_t size;
/* Only for encoder */
uint8_t *output;
/* Only for decoder */
const uint8_t *input;
} grn_codec_data;
static inline void
grn_codec_error(grn_ctx *ctx,
grn_rc rc,
grn_codec_data *data,
const char *message,
const char *file,
uint32_t line,
const char *function)
{
grn_obj term;
GRN_DEFINE_NAME(data->ii);
GRN_TEXT_INIT(&term, 0);
if (data->term_id != GRN_ID_NIL) {
grn_ii_get_term(ctx, data->ii, data->term_id, &term);
}
grn_error_set(ctx,
GRN_ERROR,
rc,
file,
line,
function,
"%s: "
"<%.*s>: "
"<%.*s>(%u): "
"n-values(%u): "
"n-odd-values(%u): "
"dv[%u/%u]: "
"data[%" GRN_FMT_SIZE "/%" GRN_FMT_SIZE "]",
message,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
data->term_id,
data->n_values,
data->n_odd_values,
data->nth_element,
data->ii->n_elements,
data->offset,
data->size);
GRN_OBJ_FIN(ctx, &term);
}
static inline size_t
grn_enc_estimate_byte(grn_ctx *ctx, grn_codec_data *data)
{
return GRN_B_ENC_MAX_SIZE * data->n_values;
}
static inline uint8_t *
grn_enc_byte(grn_ctx *ctx, grn_codec_data *data)
{
uint8_t *output = data->output;
uint32_t i;
for (i = 0; i < data->n_values; i++) {
GRN_B_ENC(data->values[i], output);
}
return output;
}
static inline const uint8_t *
grn_dec_byte_check(grn_ctx *ctx,
grn_codec_data *data,
uint32_t *value,
const uint8_t *input,
const uint8_t *input_end)
{
if (input >= input_end) {
grn_codec_error(ctx,
GRN_FILE_CORRUPT,
data,
"[ii][dec][byte] "
"input is short: at least 1 byte is needed",
__FILE__,
__LINE__,
__FUNCTION__);
return NULL;
}
uint32_t v = *input++;
data->offset++;
switch (v >> 4) {
case 0x08:
if (v == 0x8f) {
if (input + sizeof(uint32_t) > input_end) {
grn_codec_error(ctx,
GRN_FILE_CORRUPT,
data,
"[ii][dec][byte] "
"input is short: at least more 4 bytes are needed",
__FILE__,
__LINE__,
__FUNCTION__);
return NULL;
}
grn_memcpy(&v, input, sizeof(uint32_t));
input += sizeof(uint32_t);
data->offset += sizeof(uint32_t);
}
break;
case 0x09:
if (input + 3 > input_end) {
grn_codec_error(ctx,
GRN_FILE_CORRUPT,
data,
"[ii][dec][byte] "
"input is short: at least more 3 bytes are needed",
__FILE__,
__LINE__,
__FUNCTION__);
return NULL;
}
v = (v - 0x90) * 0x100 + *input++;
v = v * 0x100 + *input++;
v = v * 0x100 + *input++ + 0x20408f;
data->offset += 3;
break;
case 0x0a:
case 0x0b:
if (input + 2 > input_end) {
grn_codec_error(ctx,
GRN_FILE_CORRUPT,
data,
"[ii][dec][byte] "
"input is short: at least more 2 bytes are needed",
__FILE__,
__LINE__,
__FUNCTION__);
return NULL;
}
v = (v - 0xa0) * 0x100 + *input++;
v = v * 0x100 + *input++ + 0x408f;
data->offset += 2;
break;
case 0x0c:
case 0x0d:
case 0x0e:
case 0x0f:
if (input + 1 > input_end) {
grn_codec_error(ctx,
GRN_FILE_CORRUPT,
data,
"[ii][dec][byte] "
"input is short: at least more 1 byte is needed",
__FILE__,
__LINE__,
__FUNCTION__);
return NULL;
}
v = (v - 0xc0) * 0x100 + *input++ + 0x8f;
break;
}
*value = v;
return input;
}
static inline const uint8_t *
grn_dec_byte(grn_ctx *ctx, grn_codec_data *data)
{
const uint8_t *input = data->input;
const uint8_t *input_end = input + data->size;
uint32_t i;
for (i = 0; i < data->n_values && input; i++) {
input = grn_dec_byte_check(ctx, data, &(data->values[i]), input, input_end);
}
return input;
}
/* PFor implementation: start */
/*
* PFor (Patched Frame of Reference) encode: M. Zukowski , S. Heman,
* N. Nes, P. Boncz, Super-Scalar RAM-CPU Cache Compression,
* Proceedings of the 22nd International Conference on Data
* Engineering, 2006
*
* Delta decoding must be done by caller. So pack()/unpack() only do
* PFor (not PForDelta).
*/
/*
* We use PForDelta not NewPForDelta nor OptPForDelta because "An
* Experimental Study of Bitmap Compression vs. Inverted List
* Compression"
* https://www.cs.purdue.edu/homes/csjgwang/pubs/SIGMOD17-Bitmap.pdf
* shows that PForDelta is faster than others. (They are smaller than
* PForDelta.)
*
* We use PForDelta not Roaring bitmaps because PForDelta is faster
* and smaller for posting list. And we need a cursor interface for
* searching. Roaring bitmaps' intersection and union may be faster
* but it's not a cursor interface. See also the above paper.
*/
#define UNIT_SIZE 0x80
#define UNIT_MASK (UNIT_SIZE - 1)
/* clang-format off */
/* <generated> */
static uint8_t *
pack_1(uint32_t *p, uint8_t *rp)
{
uint8_t v;
v = *p++ << 7;
v += *p++ << 6;
v += *p++ << 5;
v += *p++ << 4;
v += *p++ << 3;
v += *p++ << 2;
v += *p++ << 1;
*rp++ = v + *p++;
return rp;
}
static const uint8_t *
unpack_1(uint32_t *p, const uint8_t *dp)
{
*p++ = (*dp >> 7);
*p++ = ((*dp >> 6) & 0x1);
*p++ = ((*dp >> 5) & 0x1);
*p++ = ((*dp >> 4) & 0x1);
*p++ = ((*dp >> 3) & 0x1);
*p++ = ((*dp >> 2) & 0x1);
*p++ = ((*dp >> 1) & 0x1);
*p++ = (*dp++ & 0x1);
return dp;
}
static uint8_t *
pack_2(uint32_t *p, uint8_t *rp)
{
uint8_t v;
v = *p++ << 6;
v += *p++ << 4;
v += *p++ << 2;
*rp++ = v + *p++;
v = *p++ << 6;
v += *p++ << 4;
v += *p++ << 2;
*rp++ = v + *p++;
return rp;
}
static const uint8_t *
unpack_2(uint32_t *p, const uint8_t *dp)
{
*p++ = (*dp >> 6);
*p++ = ((*dp >> 4) & 0x3);
*p++ = ((*dp >> 2) & 0x3);
*p++ = (*dp++ & 0x3);
*p++ = (*dp >> 6);
*p++ = ((*dp >> 4) & 0x3);
*p++ = ((*dp >> 2) & 0x3);
*p++ = (*dp++ & 0x3);
return dp;
}
static uint8_t *
pack_3(uint32_t *p, uint8_t *rp)
{
uint8_t v;
v = *p++ << 5;
v += *p++ << 2;
*rp++ = v + (*p >> 1); v = *p++ << 7;
v += *p++ << 4;
v += *p++ << 1;
*rp++ = v + (*p >> 2); v = *p++ << 6;
v += *p++ << 3;
*rp++ = v + *p++;
return rp;
}
static const uint8_t *
unpack_3(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
*p++ = (*dp >> 5);
*p++ = ((*dp >> 2) & 0x7);
v = ((*dp++ << 1) & 0x7); *p++ = v + (*dp >> 7);
*p++ = ((*dp >> 4) & 0x7);
*p++ = ((*dp >> 1) & 0x7);
v = ((*dp++ << 2) & 0x7); *p++ = v + (*dp >> 6);
*p++ = ((*dp >> 3) & 0x7);
*p++ = (*dp++ & 0x7);
return dp;
}
static uint8_t *
pack_4(uint32_t *p, uint8_t *rp)
{
uint8_t v;
v = *p++ << 4;
*rp++ = v + *p++;
v = *p++ << 4;
*rp++ = v + *p++;
v = *p++ << 4;
*rp++ = v + *p++;
v = *p++ << 4;
*rp++ = v + *p++;
return rp;
}
static const uint8_t *
unpack_4(uint32_t *p, const uint8_t *dp)
{
*p++ = (*dp >> 4);
*p++ = (*dp++ & 0xf);
*p++ = (*dp >> 4);
*p++ = (*dp++ & 0xf);
*p++ = (*dp >> 4);
*p++ = (*dp++ & 0xf);
*p++ = (*dp >> 4);
*p++ = (*dp++ & 0xf);
return dp;
}
static uint8_t *
pack_5(uint32_t *p, uint8_t *rp)
{
uint8_t v;
v = *p++ << 3;
*rp++ = v + (*p >> 2); v = *p++ << 6;
v += *p++ << 1;
*rp++ = v + (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 1); v = *p++ << 7;
v += *p++ << 2;
*rp++ = v + (*p >> 3); v = *p++ << 5;
*rp++ = v + *p++;
return rp;
}
static const uint8_t *
unpack_5(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
*p++ = (*dp >> 3);
v = ((*dp++ << 2) & 0x1f); *p++ = v + (*dp >> 6);
*p++ = ((*dp >> 1) & 0x1f);
v = ((*dp++ << 4) & 0x1f); *p++ = v + (*dp >> 4);
v = ((*dp++ << 1) & 0x1f); *p++ = v + (*dp >> 7);
*p++ = ((*dp >> 2) & 0x1f);
v = ((*dp++ << 3) & 0x1f); *p++ = v + (*dp >> 5);
*p++ = (*dp++ & 0x1f);
return dp;
}
static uint8_t *
pack_6(uint32_t *p, uint8_t *rp)
{
uint8_t v;
v = *p++ << 2;
*rp++ = v + (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 2); v = *p++ << 6;
*rp++ = v + *p++;
v = *p++ << 2;
*rp++ = v + (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 2); v = *p++ << 6;
*rp++ = v + *p++;
return rp;
}
static const uint8_t *
unpack_6(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
*p++ = (*dp >> 2);
v = ((*dp++ << 4) & 0x3f); *p++ = v + (*dp >> 4);
v = ((*dp++ << 2) & 0x3f); *p++ = v + (*dp >> 6);
*p++ = (*dp++ & 0x3f);
*p++ = (*dp >> 2);
v = ((*dp++ << 4) & 0x3f); *p++ = v + (*dp >> 4);
v = ((*dp++ << 2) & 0x3f); *p++ = v + (*dp >> 6);
*p++ = (*dp++ & 0x3f);
return dp;
}
static uint8_t *
pack_7(uint32_t *p, uint8_t *rp)
{
uint8_t v;
v = *p++ << 1;
*rp++ = v + (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 1); v = *p++ << 7;
*rp++ = v + *p++;
return rp;
}
static const uint8_t *
unpack_7(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
*p++ = (*dp >> 1);
v = ((*dp++ << 6) & 0x7f); *p++ = v + (*dp >> 2);
v = ((*dp++ << 5) & 0x7f); *p++ = v + (*dp >> 3);
v = ((*dp++ << 4) & 0x7f); *p++ = v + (*dp >> 4);
v = ((*dp++ << 3) & 0x7f); *p++ = v + (*dp >> 5);
v = ((*dp++ << 2) & 0x7f); *p++ = v + (*dp >> 6);
v = ((*dp++ << 1) & 0x7f); *p++ = v + (*dp >> 7);
*p++ = (*dp++ & 0x7f);
return dp;
}
static uint8_t *
pack_8(uint32_t *p, uint8_t *rp)
{
*rp++ = *p++;
*rp++ = *p++;
*rp++ = *p++;
*rp++ = *p++;
*rp++ = *p++;
*rp++ = *p++;
*rp++ = *p++;
*rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_8(uint32_t *p, const uint8_t *dp)
{
*p++ = *dp++;
*p++ = *dp++;
*p++ = *dp++;
*p++ = *dp++;
*p++ = *dp++;
*p++ = *dp++;
*p++ = *dp++;
*p++ = *dp++;
return dp;
}
static uint8_t *
pack_9(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_9(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 1; *p++ = v + (*dp >> 7);
v = ((*dp++ << 2) & 0x1ff); *p++ = v + (*dp >> 6);
v = ((*dp++ << 3) & 0x1ff); *p++ = v + (*dp >> 5);
v = ((*dp++ << 4) & 0x1ff); *p++ = v + (*dp >> 4);
v = ((*dp++ << 5) & 0x1ff); *p++ = v + (*dp >> 3);
v = ((*dp++ << 6) & 0x1ff); *p++ = v + (*dp >> 2);
v = ((*dp++ << 7) & 0x1ff); *p++ = v + (*dp >> 1);
v = ((*dp++ << 8) & 0x1ff); *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_10(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_10(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 4) & 0x3ff); *p++ = v + (*dp >> 4);
v = ((*dp++ << 6) & 0x3ff); *p++ = v + (*dp >> 2);
v = ((*dp++ << 8) & 0x3ff); *p++ = v + *dp++;
v = *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 4) & 0x3ff); *p++ = v + (*dp >> 4);
v = ((*dp++ << 6) & 0x3ff); *p++ = v + (*dp >> 2);
v = ((*dp++ << 8) & 0x3ff); *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_11(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 9); *rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_11(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 3; *p++ = v + (*dp >> 5);
v = ((*dp++ << 6) & 0x7ff); *p++ = v + (*dp >> 2);
v = ((*dp++ << 9) & 0x7ff); v += *dp++ << 1; *p++ = v + (*dp >> 7);
v = ((*dp++ << 4) & 0x7ff); *p++ = v + (*dp >> 4);
v = ((*dp++ << 7) & 0x7ff); *p++ = v + (*dp >> 1);
v = ((*dp++ << 10) & 0x7ff); v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 5) & 0x7ff); *p++ = v + (*dp >> 3);
v = ((*dp++ << 8) & 0x7ff); *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_12(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_12(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 8) & 0xfff); *p++ = v + *dp++;
v = *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 8) & 0xfff); *p++ = v + *dp++;
v = *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 8) & 0xfff); *p++ = v + *dp++;
v = *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 8) & 0xfff); *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_13(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 9); *rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 11); *rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_13(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 5; *p++ = v + (*dp >> 3);
v = ((*dp++ << 10) & 0x1fff); v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 7) & 0x1fff); *p++ = v + (*dp >> 1);
v = ((*dp++ << 12) & 0x1fff); v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 9) & 0x1fff); v += *dp++ << 1; *p++ = v + (*dp >> 7);
v = ((*dp++ << 6) & 0x1fff); *p++ = v + (*dp >> 2);
v = ((*dp++ << 11) & 0x1fff); v += *dp++ << 3; *p++ = v + (*dp >> 5);
v = ((*dp++ << 8) & 0x1fff); *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_14(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_14(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 12) & 0x3fff); v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 10) & 0x3fff); v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 8) & 0x3fff); *p++ = v + *dp++;
v = *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 12) & 0x3fff); v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 10) & 0x3fff); v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 8) & 0x3fff); *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_15(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 13); *rp++ = (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 11); *rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 9); *rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_15(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 7; *p++ = v + (*dp >> 1);
v = ((*dp++ << 14) & 0x7fff); v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 13) & 0x7fff); v += *dp++ << 5; *p++ = v + (*dp >> 3);
v = ((*dp++ << 12) & 0x7fff); v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 11) & 0x7fff); v += *dp++ << 3; *p++ = v + (*dp >> 5);
v = ((*dp++ << 10) & 0x7fff); v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 9) & 0x7fff); v += *dp++ << 1; *p++ = v + (*dp >> 7);
v = ((*dp++ << 8) & 0x7fff); *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_16(uint32_t *p, uint8_t *rp)
{
*rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_16(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 8; *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_17(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 9); *rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 11); *rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 13); *rp++ = (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 15); *rp++ = (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_17(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 9; v += *dp++ << 1; *p++ = v + (*dp >> 7);
v = ((*dp++ << 10) & 0x1ffff); v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 11) & 0x1ffff); v += *dp++ << 3; *p++ = v + (*dp >> 5);
v = ((*dp++ << 12) & 0x1ffff); v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 13) & 0x1ffff); v += *dp++ << 5; *p++ = v + (*dp >> 3);
v = ((*dp++ << 14) & 0x1ffff); v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 15) & 0x1ffff); v += *dp++ << 7; *p++ = v + (*dp >> 1);
v = ((*dp++ << 16) & 0x1ffff); v += *dp++ << 8; *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_18(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_18(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 10; v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 12) & 0x3ffff); v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 14) & 0x3ffff); v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 16) & 0x3ffff); v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 10; v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 12) & 0x3ffff); v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 14) & 0x3ffff); v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 16) & 0x3ffff); v += *dp++ << 8; *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_19(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 11); *rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 17); *rp++ = (*p >> 9); *rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 15); *rp++ = (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 18); *rp++ = (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 13); *rp++ = (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_19(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 11; v += *dp++ << 3; *p++ = v + (*dp >> 5);
v = ((*dp++ << 14) & 0x7ffff); v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 17) & 0x7ffff); v += *dp++ << 9; v += *dp++ << 1;
*p++ = v + (*dp >> 7);
v = ((*dp++ << 12) & 0x7ffff); v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 15) & 0x7ffff); v += *dp++ << 7; *p++ = v + (*dp >> 1);
v = ((*dp++ << 18) & 0x7ffff); v += *dp++ << 10; v += *dp++ << 2;
*p++ = v + (*dp >> 6);
v = ((*dp++ << 13) & 0x7ffff); v += *dp++ << 5; *p++ = v + (*dp >> 3);
v = ((*dp++ << 16) & 0x7ffff); v += *dp++ << 8; *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_20(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_20(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 12; v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 16) & 0xfffff); v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 12; v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 16) & 0xfffff); v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 12; v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 16) & 0xfffff); v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 12; v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 16) & 0xfffff); v += *dp++ << 8; *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_21(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 13); *rp++ = (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 18); *rp++ = (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 15); *rp++ = (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 17); *rp++ = (*p >> 9); *rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 19); *rp++ = (*p >> 11); *rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_21(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 13; v += *dp++ << 5; *p++ = v + (*dp >> 3);
v = ((*dp++ << 18) & 0x1fffff); v += *dp++ << 10; v += *dp++ << 2;
*p++ = v + (*dp >> 6);
v = ((*dp++ << 15) & 0x1fffff); v += *dp++ << 7; *p++ = v + (*dp >> 1);
v = ((*dp++ << 20) & 0x1fffff); v += *dp++ << 12; v += *dp++ << 4;
*p++ = v + (*dp >> 4);
v = ((*dp++ << 17) & 0x1fffff); v += *dp++ << 9; v += *dp++ << 1;
*p++ = v + (*dp >> 7);
v = ((*dp++ << 14) & 0x1fffff); v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 19) & 0x1fffff); v += *dp++ << 11; v += *dp++ << 3;
*p++ = v + (*dp >> 5);
v = ((*dp++ << 16) & 0x1fffff); v += *dp++ << 8; *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_22(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 18); *rp++ = (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 18); *rp++ = (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_22(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 14; v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 20) & 0x3fffff); v += *dp++ << 12; v += *dp++ << 4;
*p++ = v + (*dp >> 4);
v = ((*dp++ << 18) & 0x3fffff); v += *dp++ << 10; v += *dp++ << 2;
*p++ = v + (*dp >> 6);
v = ((*dp++ << 16) & 0x3fffff); v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 14; v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 20) & 0x3fffff); v += *dp++ << 12; v += *dp++ << 4;
*p++ = v + (*dp >> 4);
v = ((*dp++ << 18) & 0x3fffff); v += *dp++ << 10; v += *dp++ << 2;
*p++ = v + (*dp >> 6);
v = ((*dp++ << 16) & 0x3fffff); v += *dp++ << 8; *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_23(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 15); *rp++ = (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 22); *rp++ = (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 21); *rp++ = (*p >> 13); *rp++ = (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 19); *rp++ = (*p >> 11); *rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 18); *rp++ = (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 17); *rp++ = (*p >> 9); *rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_23(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 15; v += *dp++ << 7; *p++ = v + (*dp >> 1);
v = ((*dp++ << 22) & 0x7fffff); v += *dp++ << 14; v += *dp++ << 6;
*p++ = v + (*dp >> 2);
v = ((*dp++ << 21) & 0x7fffff); v += *dp++ << 13; v += *dp++ << 5;
*p++ = v + (*dp >> 3);
v = ((*dp++ << 20) & 0x7fffff); v += *dp++ << 12; v += *dp++ << 4;
*p++ = v + (*dp >> 4);
v = ((*dp++ << 19) & 0x7fffff); v += *dp++ << 11; v += *dp++ << 3;
*p++ = v + (*dp >> 5);
v = ((*dp++ << 18) & 0x7fffff); v += *dp++ << 10; v += *dp++ << 2;
*p++ = v + (*dp >> 6);
v = ((*dp++ << 17) & 0x7fffff); v += *dp++ << 9; v += *dp++ << 1;
*p++ = v + (*dp >> 7);
v = ((*dp++ << 16) & 0x7fffff); v += *dp++ << 8; *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_24(uint32_t *p, uint8_t *rp)
{
*rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_24(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_25(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 17); *rp++ = (*p >> 9); *rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 18); *rp++ = (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 19); *rp++ = (*p >> 11); *rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 21); *rp++ = (*p >> 13); *rp++ = (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 22); *rp++ = (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 23); *rp++ = (*p >> 15); *rp++ = (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_25(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 17; v += *dp++ << 9; v += *dp++ << 1; *p++ = v + (*dp >> 7);
v = ((*dp++ << 18) & 0x1ffffff); v += *dp++ << 10; v += *dp++ << 2;
*p++ = v + (*dp >> 6);
v = ((*dp++ << 19) & 0x1ffffff); v += *dp++ << 11; v += *dp++ << 3;
*p++ = v + (*dp >> 5);
v = ((*dp++ << 20) & 0x1ffffff); v += *dp++ << 12; v += *dp++ << 4;
*p++ = v + (*dp >> 4);
v = ((*dp++ << 21) & 0x1ffffff); v += *dp++ << 13; v += *dp++ << 5;
*p++ = v + (*dp >> 3);
v = ((*dp++ << 22) & 0x1ffffff); v += *dp++ << 14; v += *dp++ << 6;
*p++ = v + (*dp >> 2);
v = ((*dp++ << 23) & 0x1ffffff); v += *dp++ << 15; v += *dp++ << 7;
*p++ = v + (*dp >> 1);
v = ((*dp++ << 24) & 0x1ffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_26(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 18); *rp++ = (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 22); *rp++ = (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 18); *rp++ = (*p >> 10); *rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 22); *rp++ = (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_26(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 18; v += *dp++ << 10; v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 20) & 0x3ffffff); v += *dp++ << 12; v += *dp++ << 4;
*p++ = v + (*dp >> 4);
v = ((*dp++ << 22) & 0x3ffffff); v += *dp++ << 14; v += *dp++ << 6;
*p++ = v + (*dp >> 2);
v = ((*dp++ << 24) & 0x3ffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
v = *dp++ << 18; v += *dp++ << 10; v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 20) & 0x3ffffff); v += *dp++ << 12; v += *dp++ << 4;
*p++ = v + (*dp >> 4);
v = ((*dp++ << 22) & 0x3ffffff); v += *dp++ << 14; v += *dp++ << 6;
*p++ = v + (*dp >> 2);
v = ((*dp++ << 24) & 0x3ffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_27(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 19); *rp++ = (*p >> 11); *rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 22); *rp++ = (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 25); *rp++ = (*p >> 17); *rp++ = (*p >> 9);
*rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 23); *rp++ = (*p >> 15); *rp++ = (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 26); *rp++ = (*p >> 18); *rp++ = (*p >> 10);
*rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 21); *rp++ = (*p >> 13); *rp++ = (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_27(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 19; v += *dp++ << 11; v += *dp++ << 3; *p++ = v + (*dp >> 5);
v = ((*dp++ << 22) & 0x7ffffff); v += *dp++ << 14; v += *dp++ << 6;
*p++ = v + (*dp >> 2);
v = ((*dp++ << 25) & 0x7ffffff); v += *dp++ << 17; v += *dp++ << 9;
v += *dp++ << 1; *p++ = v + (*dp >> 7);
v = ((*dp++ << 20) & 0x7ffffff); v += *dp++ << 12; v += *dp++ << 4;
*p++ = v + (*dp >> 4);
v = ((*dp++ << 23) & 0x7ffffff); v += *dp++ << 15; v += *dp++ << 7;
*p++ = v + (*dp >> 1);
v = ((*dp++ << 26) & 0x7ffffff); v += *dp++ << 18; v += *dp++ << 10;
v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 21) & 0x7ffffff); v += *dp++ << 13; v += *dp++ << 5;
*p++ = v + (*dp >> 3);
v = ((*dp++ << 24) & 0x7ffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_28(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 20); *rp++ = (*p >> 12); *rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_28(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 20; v += *dp++ << 12; v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 24) & 0xfffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
v = *dp++ << 20; v += *dp++ << 12; v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 24) & 0xfffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
v = *dp++ << 20; v += *dp++ << 12; v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 24) & 0xfffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
v = *dp++ << 20; v += *dp++ << 12; v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 24) & 0xfffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_29(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 21); *rp++ = (*p >> 13); *rp++ = (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 26); *rp++ = (*p >> 18); *rp++ = (*p >> 10);
*rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 23); *rp++ = (*p >> 15); *rp++ = (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 28); *rp++ = (*p >> 20); *rp++ = (*p >> 12);
*rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 25); *rp++ = (*p >> 17); *rp++ = (*p >> 9);
*rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 22); *rp++ = (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 27); *rp++ = (*p >> 19); *rp++ = (*p >> 11);
*rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_29(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 21; v += *dp++ << 13; v += *dp++ << 5; *p++ = v + (*dp >> 3);
v = ((*dp++ << 26) & 0x1fffffff); v += *dp++ << 18; v += *dp++ << 10;
v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 23) & 0x1fffffff); v += *dp++ << 15; v += *dp++ << 7;
*p++ = v + (*dp >> 1);
v = ((*dp++ << 28) & 0x1fffffff); v += *dp++ << 20; v += *dp++ << 12;
v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 25) & 0x1fffffff); v += *dp++ << 17; v += *dp++ << 9;
v += *dp++ << 1; *p++ = v + (*dp >> 7);
v = ((*dp++ << 22) & 0x1fffffff); v += *dp++ << 14; v += *dp++ << 6;
*p++ = v + (*dp >> 2);
v = ((*dp++ << 27) & 0x1fffffff); v += *dp++ << 19; v += *dp++ << 11;
v += *dp++ << 3; *p++ = v + (*dp >> 5);
v = ((*dp++ << 24) & 0x1fffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_30(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 22); *rp++ = (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 28); *rp++ = (*p >> 20); *rp++ = (*p >> 12);
*rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 26); *rp++ = (*p >> 18); *rp++ = (*p >> 10);
*rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 22); *rp++ = (*p >> 14); *rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 28); *rp++ = (*p >> 20); *rp++ = (*p >> 12);
*rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 26); *rp++ = (*p >> 18); *rp++ = (*p >> 10);
*rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8);
*rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_30(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 22; v += *dp++ << 14; v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 28) & 0x3fffffff); v += *dp++ << 20; v += *dp++ << 12;
v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 26) & 0x3fffffff); v += *dp++ << 18; v += *dp++ << 10;
v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 24) & 0x3fffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
v = *dp++ << 22; v += *dp++ << 14; v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 28) & 0x3fffffff); v += *dp++ << 20; v += *dp++ << 12;
v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 26) & 0x3fffffff); v += *dp++ << 18; v += *dp++ << 10;
v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 24) & 0x3fffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_31(uint32_t *p, uint8_t *rp)
{
uint8_t v;
*rp++ = (*p >> 23); *rp++ = (*p >> 15); *rp++ = (*p >> 7); v = *p++ << 1;
*rp++ = v + (*p >> 30); *rp++ = (*p >> 22); *rp++ = (*p >> 14);
*rp++ = (*p >> 6); v = *p++ << 2;
*rp++ = v + (*p >> 29); *rp++ = (*p >> 21); *rp++ = (*p >> 13);
*rp++ = (*p >> 5); v = *p++ << 3;
*rp++ = v + (*p >> 28); *rp++ = (*p >> 20); *rp++ = (*p >> 12);
*rp++ = (*p >> 4); v = *p++ << 4;
*rp++ = v + (*p >> 27); *rp++ = (*p >> 19); *rp++ = (*p >> 11);
*rp++ = (*p >> 3); v = *p++ << 5;
*rp++ = v + (*p >> 26); *rp++ = (*p >> 18); *rp++ = (*p >> 10);
*rp++ = (*p >> 2); v = *p++ << 6;
*rp++ = v + (*p >> 25); *rp++ = (*p >> 17); *rp++ = (*p >> 9);
*rp++ = (*p >> 1); v = *p++ << 7;
*rp++ = v + (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8);
*rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_31(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 23; v += *dp++ << 15; v += *dp++ << 7; *p++ = v + (*dp >> 1);
v = ((*dp++ << 30) & 0x7fffffff); v += *dp++ << 22; v += *dp++ << 14;
v += *dp++ << 6; *p++ = v + (*dp >> 2);
v = ((*dp++ << 29) & 0x7fffffff); v += *dp++ << 21; v += *dp++ << 13;
v += *dp++ << 5; *p++ = v + (*dp >> 3);
v = ((*dp++ << 28) & 0x7fffffff); v += *dp++ << 20; v += *dp++ << 12;
v += *dp++ << 4; *p++ = v + (*dp >> 4);
v = ((*dp++ << 27) & 0x7fffffff); v += *dp++ << 19; v += *dp++ << 11;
v += *dp++ << 3; *p++ = v + (*dp >> 5);
v = ((*dp++ << 26) & 0x7fffffff); v += *dp++ << 18; v += *dp++ << 10;
v += *dp++ << 2; *p++ = v + (*dp >> 6);
v = ((*dp++ << 25) & 0x7fffffff); v += *dp++ << 17; v += *dp++ << 9;
v += *dp++ << 1; *p++ = v + (*dp >> 7);
v = ((*dp++ << 24) & 0x7fffffff); v += *dp++ << 16; v += *dp++ << 8;
*p++ = v + *dp++;
return dp;
}
static uint8_t *
pack_32(uint32_t *p, uint8_t *rp)
{
*rp++ = (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
*rp++ = (*p >> 24); *rp++ = (*p >> 16); *rp++ = (*p >> 8); *rp++ = *p++;
return rp;
}
static const uint8_t *
unpack_32(uint32_t *p, const uint8_t *dp)
{
uint32_t v;
v = *dp++ << 24; v += *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 24; v += *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 24; v += *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 24; v += *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 24; v += *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 24; v += *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 24; v += *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
v = *dp++ << 24; v += *dp++ << 16; v += *dp++ << 8; *p++ = v + *dp++;
return dp;
}
/* </generated> */
/* clang-format on */
static uint8_t *
pack_(uint32_t *p, uint32_t i, int w, uint8_t *rp)
{
while (i >= 8) {
/* clang-format off */
switch (w) {
case 0 : break;
case 1 : rp = pack_1(p, rp); break;
case 2 : rp = pack_2(p, rp); break;
case 3 : rp = pack_3(p, rp); break;
case 4 : rp = pack_4(p, rp); break;
case 5 : rp = pack_5(p, rp); break;
case 6 : rp = pack_6(p, rp); break;
case 7 : rp = pack_7(p, rp); break;
case 8 : rp = pack_8(p, rp); break;
case 9 : rp = pack_9(p, rp); break;
case 10 : rp = pack_10(p, rp); break;
case 11 : rp = pack_11(p, rp); break;
case 12 : rp = pack_12(p, rp); break;
case 13 : rp = pack_13(p, rp); break;
case 14 : rp = pack_14(p, rp); break;
case 15 : rp = pack_15(p, rp); break;
case 16 : rp = pack_16(p, rp); break;
case 17 : rp = pack_17(p, rp); break;
case 18 : rp = pack_18(p, rp); break;
case 19 : rp = pack_19(p, rp); break;
case 20 : rp = pack_20(p, rp); break;
case 21 : rp = pack_21(p, rp); break;
case 22 : rp = pack_22(p, rp); break;
case 23 : rp = pack_23(p, rp); break;
case 24 : rp = pack_24(p, rp); break;
case 25 : rp = pack_25(p, rp); break;
case 26 : rp = pack_26(p, rp); break;
case 27 : rp = pack_27(p, rp); break;
case 28 : rp = pack_28(p, rp); break;
case 29 : rp = pack_29(p, rp); break;
case 30 : rp = pack_30(p, rp); break;
case 31 : rp = pack_31(p, rp); break;
case 32 : rp = pack_32(p, rp); break;
}
/* clang-format on */
p += 8;
i -= 8;
}
{
int b;
uint8_t v;
uint32_t *pe = p + i;
for (b = 8 - w, v = 0; p < pe;) {
if (b > 0) {
v += *p++ << b;
b -= w;
} else if (b < 0) {
*rp++ = v + (*p >> -b);
b += 8;
v = 0;
} else {
*rp++ = v + *p++;
b = 8 - w;
v = 0;
}
}
if (b + w != 8) {
*rp++ = v;
}
return rp;
}
}
/* TODO: This is too rough. I hope that this is large enough. */
#define PACK_MAX_SIZE ((UNIT_SIZE / 8) * 32 + (UNIT_SIZE * GRN_B_ENC_MAX_SIZE))
/*
* w: b on the paper.
* ebuf, ep: exception values on the paper.
*/
static uint8_t *
pack(uint32_t *p, uint32_t i, uint8_t *freq, uint8_t *rp)
{
int32_t k, w;
uint8_t ebuf[UNIT_SIZE], *ep = ebuf;
uint32_t s, *pe = p + i, r, th = i - (i >> 3);
for (w = 0, s = 0; w <= 32; w++) {
if ((s += freq[w]) >= th) {
break;
}
}
if (i == s) {
*rp++ = w;
return pack_(p, i, w, rp);
}
r = 1 << w;
*rp++ = w + 0x80;
*rp++ = i - s;
if (r >= UNIT_SIZE) {
uint32_t first, *last = &first;
for (k = 0; p < pe; p++, k++) {
if (*p >= r) {
GRN_B_ENC(*p - r, ep);
*last = k;
last = p;
}
}
*last = 0;
*rp++ = (uint8_t)first;
} else {
for (k = 0; p < pe; p++, k++) {
if (*p >= r) {
*ep++ = k;
GRN_B_ENC(*p - r, ep);
*p = 0;
}
}
}
rp = pack_(p - i, i, w, rp);
grn_memcpy(rp, ebuf, ep - ebuf);
return rp + (ep - ebuf);
}
/*
* w: b on the paper.
* ne: the number of exception values on the paper.
*/
static const uint8_t *
unpack(grn_ctx *ctx,
grn_codec_data *data,
const uint8_t *dp,
const uint8_t *dpe,
int i,
uint32_t *rp)
{
uint8_t ne = 0, k = 0, w = *dp++;
uint32_t m, *p = rp;
if (w & 0x80) {
ne = *dp++;
w -= 0x80;
m = (1 << w) - 1;
if (m >= UNIT_MASK) {
k = *dp++;
}
} else {
m = (1 << w) - 1;
}
if (w) {
while (i >= 8) {
if (dp + w > dpe) {
return NULL;
}
/* clang-format off */
switch (w) {
case 1 : dp = unpack_1(p, dp); break;
case 2 : dp = unpack_2(p, dp); break;
case 3 : dp = unpack_3(p, dp); break;
case 4 : dp = unpack_4(p, dp); break;
case 5 : dp = unpack_5(p, dp); break;
case 6 : dp = unpack_6(p, dp); break;
case 7 : dp = unpack_7(p, dp); break;
case 8 : dp = unpack_8(p, dp); break;
case 9 : dp = unpack_9(p, dp); break;
case 10 : dp = unpack_10(p, dp); break;
case 11 : dp = unpack_11(p, dp); break;
case 12 : dp = unpack_12(p, dp); break;
case 13 : dp = unpack_13(p, dp); break;
case 14 : dp = unpack_14(p, dp); break;
case 15 : dp = unpack_15(p, dp); break;
case 16 : dp = unpack_16(p, dp); break;
case 17 : dp = unpack_17(p, dp); break;
case 18 : dp = unpack_18(p, dp); break;
case 19 : dp = unpack_19(p, dp); break;
case 20 : dp = unpack_20(p, dp); break;
case 21 : dp = unpack_21(p, dp); break;
case 22 : dp = unpack_22(p, dp); break;
case 23 : dp = unpack_23(p, dp); break;
case 24 : dp = unpack_24(p, dp); break;
case 25 : dp = unpack_25(p, dp); break;
case 26 : dp = unpack_26(p, dp); break;
case 27 : dp = unpack_27(p, dp); break;
case 28 : dp = unpack_28(p, dp); break;
case 29 : dp = unpack_29(p, dp); break;
case 30 : dp = unpack_30(p, dp); break;
case 31 : dp = unpack_31(p, dp); break;
case 32 : dp = unpack_32(p, dp); break;
}
/* clang-format on */
i -= 8;
p += 8;
}
{
int b;
uint32_t v, *pe;
for (b = 8 - w, v = 0, pe = p + i; p < pe && dp < dpe;) {
if (b > 0) {
*p++ = v + ((*dp >> b) & m);
b -= w;
v = 0;
} else if (b < 0) {
v += (*dp++ << -b) & m;
b += 8;
} else {
*p++ = v + (*dp++ & m);
b = 8 - w;
v = 0;
}
}
if (b + w != 8) {
dp++;
}
}
} else {
memset(p, 0, sizeof(uint32_t) * i);
}
if (ne) {
if (m >= UNIT_MASK) {
uint32_t *pp;
while (ne-- && dp) {
pp = &rp[k];
k = *pp;
dp = grn_dec_byte_check(ctx, data, pp, dp, dpe);
*pp += (m + 1);
}
} else {
while (ne-- && dp) {
k = *dp++;
dp = grn_dec_byte_check(ctx, data, &(rp[k]), dp, dpe);
rp[k] += (m + 1);
}
}
}
return dp;
}
static inline size_t
grn_enc_estimate_p_for(grn_ctx *ctx, grn_codec_data *data)
{
return PACK_MAX_SIZE * ((data->n_values / UNIT_SIZE) + 1);
}
static inline uint8_t *
grn_enc_p_for(grn_ctx *ctx, grn_codec_data *data)
{
uint8_t *output = data->output;
uint32_t *values = data->values;
uint32_t n_values = data->n_values;
uint32_t unit[UNIT_SIZE];
uint8_t freq[33];
while (n_values >= UNIT_SIZE) {
memset(freq, 0, 33);
uint32_t i;
for (i = 0; i < UNIT_SIZE; i++) {
unit[i] = values[i];
if (unit[i]) {
grn_bit_scan_rev_result w;
GRN_BIT_SCAN_REV(unit[i], w);
freq[w + 1]++;
} else {
freq[0]++;
}
}
data->offset = output - data->output;
output = pack(unit, UNIT_SIZE, freq, output);
values += UNIT_SIZE;
n_values -= UNIT_SIZE;
}
if (n_values > 0) {
memset(freq, 0, 33);
uint32_t i;
for (i = 0; i < n_values; i++) {
unit[i] = values[i];
if (unit[i]) {
grn_bit_scan_rev_result w;
GRN_BIT_SCAN_REV(unit[i], w);
freq[w + 1]++;
} else {
freq[0]++;
}
}
data->offset = output - data->output;
output = pack(unit, n_values, freq, output);
}
return output;
}
static inline const uint8_t *
grn_dec_p_for(grn_ctx *ctx, grn_codec_data *data)
{
const uint8_t *input = data->input;
const uint8_t *input_end = input + data->size;
uint32_t *values = data->values;
uint32_t n_values = data->n_values;
for (; n_values >= UNIT_SIZE; n_values -= UNIT_SIZE) {
data->offset = input - data->input;
const uint8_t *next_input =
unpack(ctx, data, input, input_end, UNIT_SIZE, values);
if (!next_input) {
grn_codec_error(ctx,
GRN_FILE_CORRUPT,
data,
"[ii][dec][p-for] "
"failed to unpack PFor encoded fixed size data",
__FILE__,
__LINE__,
__FUNCTION__);
return NULL;
}
input = next_input;
values += UNIT_SIZE;
}
if (n_values > 0) {
data->offset = input - data->input;
const uint8_t *next_input =
unpack(ctx, data, input, input_end, n_values, values);
if (!next_input) {
grn_codec_error(
ctx,
GRN_FILE_CORRUPT,
data,
"[ii][dec][p-for] failed to unpack PFor encoded rest data",
__FILE__,
__LINE__,
__FUNCTION__);
return NULL;
}
input = next_input;
values += n_values;
}
return input;
}
/* PFor implementation: end */
/* Binary format used in grn_encv()/grn_decv().
*
* df: Data Frequency: The number of postings
*
* The uint32_t value encoded by GRN_B_ENC:
* +========+========+========+========+
* |12345678|12345678|12345678|12345678|
* +========+========+========+========+
*
* Case 1: If all data don't use PFor:
*
* +========+========+========+========+
* |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDD1| The last bit is important!
* +========+========+========+========+
* A = ((df << 1) >> 24) & 0xff
* B = ((df << 1) >> 16) & 0xff
* C = ((df << 1) >> 8) & 0xff
* D = ((df << 1) >> 0) & 0xff
* for (i = 0; i < ii->n_elements; i++) {
* for (j = 0; j < dv[i].data_size; j++) {
* +========+========+========+========+
* |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD| dv[i].data[j]
* +========+========+========+========+
* A = (dv[i].data[j] >> 24) & 0xff
* B = (dv[i].data[j] >> 16) & 0xff
* C = (dv[i].data[j] >> 8) & 0xff
* D = (dv[i].data[j] >> 0) & 0xff
* }
* }
*
* Case 2: If any data use PFor:
*
* use_p_for_flags: It's 32bit flags that show that the i-th data
* uses PFor. For example, 0x02 means that the
* second data only uses PFor.
* positions_gap: the_number_positions - df.
*
* +========+========+========+========+
* |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDD0| The last bit is important!
* +========+========+========+========+
* A = ((use_p_for_flags << 1) >> 24) & 0xff
* B = ((use_p_for_flags << 1) >> 16) & 0xff
* C = ((use_p_for_flags << 1) >> 8) & 0xff
* D = ((use_p_for_flags << 1) >> 0) & 0xff
* +========+========+========+========+
* |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD| df
* +========+========+========+========+
* A = (df >> 24) & 0xff
* B = (df >> 16) & 0xff
* C = (df >> 8) & 0xff
* D = (df >> 0) & 0xff
* if (ii has positions) {
* +========+========+========+========+
* |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD| positions_gap
* +========+========+========+========+
* A = (pgap >> 24) & 0xff
* B = (pgap >> 16) & 0xff
* C = (pgap >> 8) & 0xff
* D = (pgap >> 0) & 0xff
* }
* for (i = 0; i < ii->n_elements; i++) {
* if (dv[i].flags & DATA_USE_P_FOR_ENC) {
* Encode dv[i].data by PFor (unit size is 128)
* } else {
* Same as case 1
* for (j = 0; j < dv[i].data_size; j++) {
* +========+========+========+========+
* |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD| dv[i].data[j]
* +========+========+========+========+
* A = (dv[i].data[j] >> 24) & 0xff
* B = (dv[i].data[j] >> 16) & 0xff
* C = (dv[i].data[j] >> 8) & 0xff
* D = (dv[i].data[j] >> 0) & 0xff
* }
* }
* }
* */
static ssize_t
grn_encv(grn_ctx *ctx,
grn_ii *ii,
grn_id term_id,
datavec *dv,
uint8_t *res,
size_t res_size)
{
uint8_t *rp = res;
ssize_t estimated = 0;
/* f in df is for frequency */
uint32_t positions_gap, l, df, data_size;
if (!(df = dv[0].data_size)) {
return 0;
}
grn_codec_data codec_data = {};
codec_data.ii = ii;
codec_data.term_id = term_id;
uint32_t use_p_for_enc_flags = 0;
for (data_size = 0, l = 0; l < ii->n_elements; l++) {
uint32_t dl = dv[l].data_size;
if (dl < df || ((dl > df) && (l != ii->n_elements - 1))) {
/* invalid argument */
return -1;
}
if (dv[l].flags & DATA_USE_P_FOR_ENC) {
use_p_for_enc_flags += 1 << l;
}
data_size += dl;
}
positions_gap = data_size - df * ii->n_elements;
if (use_p_for_enc_flags == 0) {
if (rp) {
GRN_B_ENC((df << 1) + 1, rp);
for (l = 0; l < ii->n_elements; l++) {
codec_data.nth_element = l;
codec_data.values = dv[l].data;
codec_data.n_values = dv[l].data_size;
codec_data.flags = dv[l].flags;
codec_data.offset = 0;
codec_data.output = rp;
codec_data.size = res_size - (rp - res);
rp = grn_enc_byte(ctx, &codec_data);
}
} else {
codec_data.n_values = 1;
estimated += grn_enc_estimate_byte(ctx, &codec_data);
for (l = 0; l < ii->n_elements; l++) {
codec_data.nth_element = l;
codec_data.values = dv[l].data;
codec_data.n_values = dv[l].data_size;
codec_data.flags = dv[l].flags;
estimated += grn_enc_estimate_byte(ctx, &codec_data);
}
}
} else {
codec_data.n_values = 2;
codec_data.flags = 0;
if (rp) {
uint32_t values[2];
values[0] = (use_p_for_enc_flags << 1);
values[1] = df;
codec_data.values = values;
codec_data.offset = 0;
codec_data.output = rp;
codec_data.size = res_size - (rp - res);
rp = grn_enc_byte(ctx, &codec_data);
} else {
estimated += grn_enc_estimate_byte(ctx, &codec_data);
}
if (dv[ii->n_elements - 1].flags & DATA_ODD) {
codec_data.n_values = 1;
if (rp) {
codec_data.values = &positions_gap;
codec_data.offset = 0;
codec_data.output = rp;
codec_data.size = res_size - (rp - res);
rp = grn_enc_byte(ctx, &codec_data);
} else {
estimated += grn_enc_estimate_byte(ctx, &codec_data);
}
} else {
GRN_ASSERT(positions_gap == 0);
}
for (l = 0; l < ii->n_elements; l++) {
codec_data.nth_element = l;
codec_data.values = dv[l].data;
codec_data.n_values = dv[l].data_size;
codec_data.flags = dv[l].flags;
codec_data.offset = 0;
codec_data.output = rp;
codec_data.size = res_size - (rp - res);
if (dv[l].flags & DATA_USE_P_FOR_ENC) {
if (rp) {
rp = grn_enc_p_for(ctx, &codec_data);
} else {
estimated += grn_enc_estimate_p_for(ctx, &codec_data);
}
} else {
if (rp) {
rp = grn_enc_byte(ctx, &codec_data);
} else {
estimated += grn_enc_estimate_byte(ctx, &codec_data);
}
}
}
}
if (rp) {
return rp - res;
} else {
return estimated;
}
}
static int
grn_decv(grn_ctx *ctx,
grn_ii *ii,
grn_id term_id,
const uint8_t *data,
uint32_t data_size,
datavec *dv)
{
size_t size;
uint32_t df = 0, l, i, *rp;
const uint8_t *dp = data;
const uint8_t *dpe = data + data_size;
if (!data_size) {
dv[0].data_size = 0;
return 0;
}
grn_codec_data codec_data = {};
codec_data.ii = ii;
codec_data.term_id = term_id;
codec_data.offset = 0;
codec_data.input = dp;
codec_data.size = dpe - dp;
dp = grn_dec_byte_check(ctx, &codec_data, &df, dp, dpe);
if (!dp) {
return 0;
}
if ((df & 1)) {
df >>= 1;
size = data_size;
if (dv[ii->n_elements].data < dv[0].data + size) {
if (dv[0].data) {
GRN_FREE(dv[0].data);
dv[0].data = NULL;
}
if (!(rp =
static_cast<uint32_t *>(GRN_MALLOC(size * sizeof(uint32_t))))) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
if (term_id != GRN_ID_NIL) {
grn_ii_get_term(ctx, ii, term_id, &term);
}
ERR(GRN_NO_MEMORY_AVAILABLE,
"[ii][decv] failed to extend buffer for data vector: "
"<%.*s>: "
"<%.*s>(%u): "
"%" GRN_FMT_SIZE " -> %" GRN_FMT_SIZE ": "
"PFor(nothing)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
term_id,
dv[ii->n_elements].data - dv[0].data,
size);
GRN_OBJ_FIN(ctx, &term);
return 0;
}
dv[ii->n_elements].data = rp + size;
} else {
rp = dv[0].data;
}
for (l = 0; l < ii->n_elements; l++) {
dv[l].data = rp;
codec_data.nth_element = l;
codec_data.offset = 0;
codec_data.input = dp;
codec_data.size = dpe - dp;
if (l < ii->n_elements - 1) {
for (i = 0; i < df && dp; i++, rp++) {
dp = grn_dec_byte_check(ctx, &codec_data, rp, dp, dpe);
}
} else {
for (i = 0; dp < dpe && dp; i++, rp++) {
dp = grn_dec_byte_check(ctx, &codec_data, rp, dp, dpe);
}
}
if (!dp) {
return 0;
}
dv[l].data_size = i;
}
} else {
uint32_t n, rest;
uint32_t use_p_for_enc_flags = df >> 1;
codec_data.offset = 0;
codec_data.input = dp;
codec_data.size = dpe - dp;
dp = grn_dec_byte_check(ctx, &codec_data, &df, dp, dpe);
if (!dp) {
return 0;
}
if (dv[ii->n_elements - 1].flags & DATA_ODD) {
codec_data.offset = 0;
codec_data.input = dp;
codec_data.size = dpe - dp;
dp = grn_dec_byte_check(ctx, &codec_data, &rest, dp, dpe);
if (!dp) {
return 0;
}
} else {
rest = 0;
}
codec_data.n_odd_values = rest;
size = df * ii->n_elements + rest;
if (dv[ii->n_elements].data < dv[0].data + size) {
if (dv[0].data) {
GRN_FREE(dv[0].data);
dv[0].data = NULL;
}
if (!(rp =
static_cast<uint32_t *>(GRN_MALLOC(size * sizeof(uint32_t))))) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
if (term_id != GRN_ID_NIL) {
grn_ii_get_term(ctx, ii, term_id, &term);
}
ERR(GRN_NO_MEMORY_AVAILABLE,
"[ii][decv] failed to extend buffer for data vector: "
"<%.*s>: "
"<%.*s>(%u): "
"%" GRN_FMT_SIZE " -> %" GRN_FMT_SIZE ": "
"PFor(%#x)", /* Binary number is better but %b doesn't exist. */
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
term_id,
dv[ii->n_elements].data - dv[0].data,
size,
use_p_for_enc_flags);
GRN_OBJ_FIN(ctx, &term);
return 0;
}
dv[ii->n_elements].data = rp + size;
} else {
rp = dv[0].data;
}
for (l = 0; l < ii->n_elements; l++) {
dv[l].data = rp;
dv[l].data_size = n = (l < ii->n_elements - 1) ? df : df + rest;
if (use_p_for_enc_flags & (1 << l)) {
dv[l].flags |= DATA_USE_P_FOR_ENC;
} else {
dv[l].flags &= ~DATA_USE_P_FOR_ENC;
}
codec_data.nth_element = l;
codec_data.values = dv[l].data;
codec_data.n_values = dv[l].data_size;
codec_data.flags = dv[l].flags;
codec_data.offset = 0;
codec_data.input = dp;
codec_data.size = dpe - dp;
if (codec_data.flags & DATA_USE_P_FOR_ENC) {
dp = grn_dec_p_for(ctx, &codec_data);
} else {
dp = grn_dec_byte(ctx, &codec_data);
}
if (!dp) {
return 0;
}
rp += dv[l].data_size;
}
if (dp != dpe) {
codec_data.n_values = df;
codec_data.offset = dp - data;
codec_data.input = data;
codec_data.size = data_size;
grn_codec_error(ctx,
GRN_FILE_CORRUPT,
&codec_data,
"[ii][decv] input size and used size aren't matched",
__FILE__,
__LINE__,
__FUNCTION__);
return 0;
}
}
return rp - dv[0].data;
}
extern "C" int
grn_b_enc(grn_ctx *ctx, uint32_t *data, uint32_t data_size, uint8_t **res)
{
uint8_t *rp;
uint32_t *dp, i;
*res = rp =
static_cast<uint8_t *>(GRN_MALLOC(data_size * sizeof(uint32_t) * 2));
GRN_B_ENC(data_size, rp);
for (i = data_size, dp = data; i; i--, dp++) {
GRN_B_ENC(*dp, rp);
}
return rp - *res;
}
extern "C" int
grn_b_dec(grn_ctx *ctx, uint8_t *data, uint32_t data_size, uint32_t **res)
{
uint32_t i, *rp, orig_size;
uint8_t *dp = data;
GRN_B_DEC(orig_size, dp);
*res = rp = static_cast<uint32_t *>(GRN_MALLOC(orig_size * sizeof(uint32_t)));
for (i = orig_size; i; i--, rp++) {
GRN_B_DEC(*rp, dp);
}
return orig_size;
}
/* buffer */
typedef struct {
uint32_t tid;
uint32_t size_in_chunk;
uint32_t pos_in_chunk;
uint16_t size_in_buffer;
uint16_t pos_in_buffer;
} buffer_term;
typedef struct {
uint16_t step;
uint16_t jump;
} buffer_rec;
typedef struct {
uint32_t chunk;
uint32_t chunk_size;
uint32_t buffer_free;
uint16_t nterms;
uint16_t nterms_void;
} buffer_header;
typedef struct {
buffer_header header;
buffer_term terms[(S_SEGMENT - sizeof(buffer_header)) / sizeof(buffer_term)];
} buffer;
#define GRN_II_POS_LSEG_SHIFT_SIZE (32 - GRN_II_W_LSEG)
#define GRN_II_POS_LSEG_SHIFT_SIZE_LARGE (32 - GRN_II_W_LSEG_LARGE)
#define GRN_II_POS_LOFFSET_SHIFT_SIZE ((16 - GRN_II_W_LOFFSET) + 2)
#define GRN_II_POS_LOFFSET_SHIFT_SIZE_LARGE ((16 - GRN_II_W_LOFFSET_LARGE) + 2)
static inline uint32_t
grn_ii_pos_lseg(grn_ii *ii, uint32_t pos)
{
if (ii->header.common->flags & GRN_OBJ_INDEX_LARGE) {
return pos >> GRN_II_POS_LSEG_SHIFT_SIZE_LARGE;
} else {
return pos >> GRN_II_POS_LSEG_SHIFT_SIZE;
}
}
static inline uint32_t
grn_ii_pos_loffset(grn_ii *ii, uint32_t pos)
{
if (ii->header.common->flags & GRN_OBJ_INDEX_LARGE) {
return (pos & ((1 << GRN_II_W_LOFFSET_LARGE) - 1))
<< GRN_II_POS_LOFFSET_SHIFT_SIZE_LARGE;
} else {
return (pos & ((1 << GRN_II_W_LOFFSET) - 1))
<< GRN_II_POS_LOFFSET_SHIFT_SIZE;
}
}
static inline uint32_t
grn_ii_pos_pack(grn_ii *ii, uint32_t lseg, uint32_t loffset)
{
if (ii->header.common->flags & GRN_OBJ_INDEX_LARGE) {
return (lseg << GRN_II_POS_LSEG_SHIFT_SIZE_LARGE) +
(loffset >> GRN_II_POS_LOFFSET_SHIFT_SIZE_LARGE);
} else {
return (lseg << GRN_II_POS_LSEG_SHIFT_SIZE) +
(loffset >> GRN_II_POS_LOFFSET_SHIFT_SIZE);
}
}
#define POS_LOFFSET_HEADER sizeof(buffer_header)
#define POS_LOFFSET_TERM sizeof(buffer_term)
static inline bool
grn_ii_pos_is_available(grn_ctx *ctx, uint32_t flags, const char *context)
{
if (flags & GRN_OBJ_INDEX_LARGE) {
if ((POS_LOFFSET_HEADER % 4) != 0) {
ERR(GRN_OPERATION_NOT_SUPPORTED,
"%s INDEX_LARGE requires 32bit aligned offset header: "
"<%" GRN_FMT_SIZE ">",
context,
POS_LOFFSET_HEADER);
return false;
}
if ((POS_LOFFSET_TERM % 4) != 0) {
ERR(GRN_OPERATION_NOT_SUPPORTED,
"%s INDEX_LARGE requires 32bit aligned offset term: "
"<%" GRN_FMT_SIZE ">",
context,
POS_LOFFSET_TERM);
return false;
}
}
return true;
}
#define POS_IS_EMBED(pos) ((pos) & 1)
#define POS_EMBED_RID(rid) (((rid) << 1) | 1)
#define POS_EMBED_RID_SID(rid, sid) ((((rid) << 12) + ((sid) << 1)) | 1)
#define POS_RID_EXTRACT(pos) ((pos) >> 1)
#define POS_RID_SID_EXTRACT_RID(pos) BIT31_12((pos))
#define POS_RID_SID_EXTRACT_SID(pos) BIT11_01((pos))
static inline uint32_t
buffer_open(
grn_ctx *ctx, grn_ii *ii, uint32_t pos, buffer_term **bt, buffer **b)
{
const uint32_t lseg = grn_ii_pos_lseg(ii, pos);
const uint32_t pseg = grn_ii_get_buffer_pseg_inline(ii, lseg);
if (pseg != GRN_II_PSEG_NOT_ASSIGNED) {
auto p = static_cast<byte *>(grn_io_seg_ref(ctx, ii->seg, pseg));
if (!p) {
return GRN_II_PSEG_NOT_ASSIGNED;
}
if (b) {
*b = (buffer *)p;
}
if (bt) {
*bt = (buffer_term *)(p + grn_ii_pos_loffset(ii, pos));
}
}
return pseg;
}
static inline grn_rc
buffer_close(grn_ctx *ctx, grn_ii *ii, uint32_t pseg)
{
if (pseg >= ii->seg->header->max_segment) {
GRN_LOG(ctx, GRN_LOG_NOTICE, "invalid pseg buffer_close(%d)", pseg);
return GRN_INVALID_ARGUMENT;
}
grn_io_seg_unref(ctx, ii->seg, pseg);
return GRN_SUCCESS;
}
typedef struct {
uint32_t rid;
uint32_t sid;
} docid;
#define BUFFER_REC_DEL(r) ((r)->jump = 1)
#define BUFFER_REC_DELETED(r) ((r)->jump == 1)
#define BUFFER_REC_AT(b, pos) ((buffer_rec *)(b) + (pos))
#define BUFFER_REC_POS(b, rec) ((uint16_t)((rec) - (buffer_rec *)(b)))
static inline void
buffer_term_dump(grn_ctx *ctx, grn_ii *ii, buffer *b, buffer_term *bt)
{
int pos, rid, sid;
uint8_t *p;
buffer_rec *r;
if (!grn_logger_pass(ctx, GRN_LOG_DEBUG)) {
return;
}
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"b=(%x %u %u %u)",
b->header.chunk,
b->header.chunk_size,
b->header.buffer_free,
b->header.nterms);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"bt=(%u %u %u %u %u)",
bt->tid,
bt->size_in_chunk,
bt->pos_in_chunk,
bt->size_in_buffer,
bt->pos_in_buffer);
for (pos = bt->pos_in_buffer; pos; pos = r->step) {
r = BUFFER_REC_AT(b, pos);
p = GRN_NEXT_ADDR(r);
GRN_B_DEC(rid, p);
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_DEC(sid, p);
} else {
sid = 1;
}
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"%d=(%d:%d),(%d:%d)",
pos,
r->jump,
r->step,
rid,
sid);
}
}
static inline grn_rc
check_jump(grn_ctx *ctx, grn_ii *ii, buffer *b, buffer_rec *r, int j)
{
uint16_t i = BUFFER_REC_POS(b, r);
uint8_t *p;
buffer_rec *r2;
docid id, id2;
if (!j) {
return GRN_SUCCESS;
}
p = GRN_NEXT_ADDR(r);
GRN_B_DEC(id.rid, p);
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_DEC(id.sid, p);
} else {
id.sid = 1;
}
if (j == 1) {
GRN_LOG(ctx, GRN_LOG_DEBUG, "deleting! %d(%d:%d)", i, id.rid, id.sid);
return GRN_SUCCESS;
}
r2 = BUFFER_REC_AT(b, j);
p = GRN_NEXT_ADDR(r2);
GRN_B_DEC(id2.rid, p);
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_DEC(id2.sid, p);
} else {
id2.sid = 1;
}
if (r2->step == i) {
GRN_LOG(ctx,
GRN_LOG_EMERG,
"cycle! %d(%d:%d)<->%d(%d:%d)",
i,
id.rid,
id.sid,
j,
id2.rid,
id2.sid);
return GRN_FILE_CORRUPT;
}
if (id2.rid < id.rid || (id2.rid == id.rid && id2.sid <= id.sid)) {
GRN_LOG(ctx,
GRN_LOG_CRIT,
"invalid jump! %d(%d:%d)(%d:%d)->%d(%d:%d)(%d:%d)",
i,
r->jump,
r->step,
id.rid,
id.sid,
j,
r2->jump,
r2->step,
id2.rid,
id2.sid);
return GRN_FILE_CORRUPT;
}
return GRN_SUCCESS;
}
static inline grn_rc
set_jump_r(grn_ctx *ctx, grn_ii *ii, buffer *b, buffer_rec *from, int to)
{
int i, j, max_jump = 100;
buffer_rec *r, *r2;
for (r = from, j = to; j > 1 && max_jump--; r = BUFFER_REC_AT(b, r->step)) {
r2 = BUFFER_REC_AT(b, j);
if (r == r2) {
break;
}
if (BUFFER_REC_DELETED(r2)) {
break;
}
if (j == (i = r->jump)) {
break;
}
if (j == r->step) {
break;
}
if (check_jump(ctx, ii, b, r, j)) {
ERR(GRN_FILE_CORRUPT, "check_jump failed");
return ctx->rc;
}
r->jump = j;
j = i;
if (!r->step) {
return GRN_FILE_CORRUPT;
}
}
return GRN_SUCCESS;
}
#define GET_NUM_BITS(x, n) \
do { \
n = x; \
n = (n & 0x55555555) + ((n >> 1) & 0x55555555); \
n = (n & 0x33333333) + ((n >> 2) & 0x33333333); \
n = (n & 0x0F0F0F0F) + ((n >> 4) & 0x0F0F0F0F); \
n = (n & 0x00FF00FF) + ((n >> 8) & 0x00FF00FF); \
n = (n & 0x0000FFFF) + ((n >> 16) & 0x0000FFFF); \
} while (0)
static inline grn_rc
buffer_put(grn_ctx *ctx,
grn_ii *ii,
buffer *b,
buffer_term *bt,
buffer_rec *rnew,
uint8_t *bs,
grn_ii_updspec *u,
int size)
{
uint8_t *p;
docid id_curr = {0, 0}, id_start = {0, 0}, id_post = {0, 0};
buffer_rec *r_curr, *r_start = NULL;
uint16_t last = 0, *lastp = &bt->pos_in_buffer, pos = BUFFER_REC_POS(b, rnew);
int vdelta = 0, delta, delta0 = 0, vhops = 0, nhops = 0, reset = 1;
grn_memcpy(GRN_NEXT_ADDR(rnew), bs, size - sizeof(buffer_rec));
for (;;) {
if (!*lastp) {
rnew->step = 0;
rnew->jump = 0;
// smb_wmb();
*lastp = pos;
if (bt->size_in_buffer++ > 1) {
buffer_rec *rhead = BUFFER_REC_AT(b, bt->pos_in_buffer);
rhead->jump = pos;
if (!(bt->size_in_buffer & 1)) {
int n;
buffer_rec *r = BUFFER_REC_AT(b, rhead->step), *r2;
GET_NUM_BITS(bt->size_in_buffer, n);
while (n-- && (r->jump > 1)) {
r2 = BUFFER_REC_AT(b, r->jump);
if (BUFFER_REC_DELETED(r2)) {
break;
}
r = r2;
}
if (r != rnew) {
set_jump_r(ctx, ii, b, r, last);
if (ctx->rc != GRN_SUCCESS) {
break;
}
}
}
}
break;
}
r_curr = BUFFER_REC_AT(b, *lastp);
p = GRN_NEXT_ADDR(r_curr);
GRN_B_DEC(id_curr.rid, p);
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_DEC(id_curr.sid, p);
} else {
id_curr.sid = 1;
}
if (id_curr.rid < id_post.rid ||
(id_curr.rid == id_post.rid && id_curr.sid < id_post.sid)) {
{
GRN_DEFINE_NAME(ii);
CRIT(GRN_FILE_CORRUPT,
"[ii][buffer][put] loop is found: "
"<%.*s>: "
"(%d:%d)->(%d:%d)",
name_size,
name,
id_post.rid,
id_post.sid,
id_curr.rid,
id_curr.sid);
}
buffer_term_dump(ctx, ii, b, bt);
bt->pos_in_buffer = 0;
bt->size_in_buffer = 0;
lastp = &bt->pos_in_buffer;
continue;
}
id_post.rid = id_curr.rid;
id_post.sid = id_curr.sid;
if (u->rid < id_curr.rid ||
(u->rid == id_curr.rid && u->sid <= id_curr.sid)) {
uint16_t step = *lastp, jump = r_curr->jump;
if (u->rid == id_curr.rid) {
if (u->sid == 0) {
while (id_curr.rid == u->rid) {
BUFFER_REC_DEL(r_curr);
if (!(step = r_curr->step)) {
break;
}
r_curr = BUFFER_REC_AT(b, step);
p = GRN_NEXT_ADDR(r_curr);
GRN_B_DEC(id_curr.rid, p);
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_DEC(id_curr.sid, p);
} else {
id_curr.sid = 1;
}
}
} else if (u->sid == id_curr.sid) {
BUFFER_REC_DEL(r_curr);
step = r_curr->step;
}
}
rnew->step = step;
rnew->jump = check_jump(ctx, ii, b, rnew, jump) ? 0 : jump;
// smb_wmb();
*lastp = pos;
break;
}
if (reset) {
r_start = r_curr;
id_start.rid = id_curr.rid;
id_start.sid = id_curr.sid;
if (!(delta0 = u->rid - id_start.rid)) {
delta0 = u->sid - id_start.sid;
}
nhops = 0;
vhops = 1;
vdelta = delta0 >> 1;
} else {
if (!(delta = id_curr.rid - id_start.rid)) {
delta = id_curr.sid - id_start.sid;
}
if (vdelta < delta) {
vdelta += (delta0 >> ++vhops);
r_start = r_curr;
}
if (nhops > vhops) {
set_jump_r(ctx, ii, b, r_start, *lastp);
if (ctx->rc != GRN_SUCCESS) {
break;
}
} else {
nhops++;
}
}
last = *lastp;
lastp = &r_curr->step;
reset = 0;
{
uint16_t posj = r_curr->jump;
if (posj > 1) {
buffer_rec *rj = BUFFER_REC_AT(b, posj);
if (!BUFFER_REC_DELETED(rj)) {
docid idj;
p = GRN_NEXT_ADDR(rj);
GRN_B_DEC(idj.rid, p);
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_DEC(idj.sid, p);
} else {
idj.sid = 1;
}
if (idj.rid < u->rid || (idj.rid == u->rid && idj.sid < u->sid)) {
last = posj;
lastp = &rj->step;
} else {
reset = 1;
}
}
}
}
}
return ctx->rc;
}
/* array */
static inline uint32_t *
array_at(grn_ctx *ctx, grn_ii *ii, uint32_t id)
{
if (id > GRN_ID_MAX) {
return NULL;
}
const uint16_t seg = id >> W_ARRAY;
const uint32_t pseg = grn_ii_get_array_pseg_inline(ii, seg);
if (pseg == GRN_II_PSEG_NOT_ASSIGNED) {
return NULL;
}
auto p = static_cast<byte *>(grn_io_seg_ref(ctx, ii->seg, pseg));
if (!p) {
return NULL;
}
return (uint32_t *)(p + (id & ARRAY_MASK_IN_A_SEGMENT) * S_ARRAY_ELEMENT);
}
static inline uint32_t *
array_get(grn_ctx *ctx, grn_ii *ii, uint32_t id)
{
byte *p = NULL;
if (id > GRN_ID_MAX) {
return NULL;
}
const uint16_t seg = id >> W_ARRAY;
uint32_t pseg = grn_ii_get_array_pseg_inline(ii, seg);
if (pseg == GRN_II_PSEG_NOT_ASSIGNED) {
if (segment_get_clear(ctx, ii, &pseg)) {
return NULL;
}
grn_ii_set_array_pseg_inline(ii, seg, pseg);
if (seg >= ii->header.common->amax) {
ii->header.common->amax = seg + 1;
}
}
p = static_cast<byte *>(grn_io_seg_ref(ctx, ii->seg, pseg));
if (!p) {
return NULL;
}
return (uint32_t *)(p + (id & ARRAY_MASK_IN_A_SEGMENT) * S_ARRAY_ELEMENT);
}
static inline void
array_unref(grn_ctx *ctx, grn_ii *ii, uint32_t id)
{
const uint32_t pseg = grn_ii_get_array_pseg_inline(ii, id >> W_ARRAY);
grn_io_seg_unref(ctx, ii->seg, pseg);
}
/* updspec */
extern "C" grn_ii_updspec *
grn_ii_updspec_open(grn_ctx *ctx, uint32_t rid, uint32_t sid)
{
auto u = static_cast<grn_ii_updspec *>(GRN_CALLOC(sizeof(grn_ii_updspec)));
if (!u) {
return NULL;
}
u->rid = rid;
u->sid = sid;
u->weight = 0;
u->tf = 0;
u->atf = 0;
u->pos = NULL;
u->tail = NULL;
// u->vnodes = NULL;
return u;
}
extern "C" grn_rc
grn_ii_updspec_add(grn_ctx *ctx, grn_ii_updspec *u, int pos, int32_t weight)
{
u->atf++;
if (u->tf >= GRN_II_MAX_TF) {
return GRN_SUCCESS;
}
auto p = static_cast<_grn_ii_pos *>(GRN_CALLOC(sizeof(struct _grn_ii_pos)));
if (!p) {
return GRN_NO_MEMORY_AVAILABLE;
}
u->weight += weight;
p->pos = pos;
p->next = NULL;
if (u->tail) {
u->tail->next = p;
} else {
u->pos = p;
}
u->tail = p;
u->tf++;
return GRN_SUCCESS;
}
extern "C" int
grn_ii_updspec_cmp(grn_ii_updspec *a, grn_ii_updspec *b)
{
struct _grn_ii_pos *pa, *pb;
if (a->rid != b->rid) {
return a->rid - b->rid;
}
if (a->sid != b->sid) {
return a->sid - b->sid;
}
if (a->weight != b->weight) {
return a->weight - b->weight;
}
if (a->tf != b->tf) {
return a->tf - b->tf;
}
for (pa = a->pos, pb = b->pos; pa && pb; pa = pa->next, pb = pb->next) {
if (pa->pos != pb->pos) {
return pa->pos - pb->pos;
}
}
if (pa) {
return 1;
}
if (pb) {
return -1;
}
return 0;
}
extern "C" grn_rc
grn_ii_updspec_close(grn_ctx *ctx, grn_ii_updspec *u)
{
struct _grn_ii_pos *p = u->pos, *q;
while (p) {
q = p->next;
GRN_FREE(p);
p = q;
}
GRN_FREE(u);
return GRN_SUCCESS;
}
static inline uint8_t *
encode_rec(
grn_ctx *ctx, grn_ii *ii, grn_ii_updspec *u, unsigned int *size, int deletep)
{
struct _grn_ii_pos *pp;
uint32_t lpos, tf, weight;
if (deletep) {
tf = 0;
weight = 0;
} else {
tf = u->tf;
weight = u->weight;
}
auto br = static_cast<uint8_t *>(GRN_MALLOC((tf + 4) * 5));
if (!br) {
return NULL;
}
auto p = br;
GRN_B_ENC(u->rid, p);
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_ENC(u->sid, p);
} else {
u->sid = 1;
}
GRN_B_ENC(tf, p);
if ((ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
GRN_B_ENC(weight, p);
}
if ((ii->header.common->flags & GRN_OBJ_WITH_POSITION)) {
for (lpos = 0, pp = u->pos; pp && tf--; lpos = pp->pos, pp = pp->next) {
GRN_B_ENC(pp->pos - lpos, p);
}
}
while (((intptr_t)p & 0x03)) {
*p++ = 0;
}
*size = (unsigned int)((p - br) + sizeof(buffer_rec));
return br;
}
typedef struct {
grn_ii *ii;
grn_hash *h;
} lexicon_deletable_arg;
#ifdef CASCADE_DELETE_LEXICON
static int
lexicon_deletable(grn_ctx *ctx, grn_obj *lexicon, grn_id tid, void *arg)
{
uint32_t *a;
grn_hash *h = ((lexicon_deletable_arg *)arg)->h;
grn_ii *ii = ((lexicon_deletable_arg *)arg)->ii;
if (!h) {
return 0;
}
if ((a = array_at(ctx, ii, tid))) {
if (a[0]) {
array_unref(ctx, ii, tid);
return 0;
}
array_unref(ctx, ii, tid);
}
{
grn_ii_updspec **u;
if (!grn_hash_get(ctx, h, &tid, sizeof(grn_id), (void **)&u)) {
return (ERRP(ctx, GRN_ERROR)) ? 0 : 1;
}
if (!(*u)->tf || !(*u)->sid) {
return 1;
}
return 0;
}
}
#endif /* CASCADE_DELETE_LEXICON */
static inline void
lexicon_delete(grn_ctx *ctx, grn_ii *ii, uint32_t tid, grn_hash *h)
{
#ifdef CASCADE_DELETE_LEXICON
lexicon_deletable_arg arg = {ii, h};
grn_table_delete_optarg optarg = {0, lexicon_deletable, &arg};
grn_table_delete_data data;
grn_table_delete_data_init(ctx, &data, ii->lexicon);
data.id = tid;
data.optarg = &optarg;
grn_table_delete_by_id_without_lock(ctx, &data);
grn_table_delete_data_fin(ctx, &data);
#endif /* CASCADE_DELETE_LEXICON */
}
typedef struct {
grn_id rid;
uint32_t sid;
uint32_t tf;
uint32_t weight;
uint32_t flags;
} docinfo;
static docinfo null_docinfo = {};
typedef struct {
uint32_t segno;
uint32_t size;
uint32_t dgap;
} chunk_info;
typedef struct {
grn_log_level log_level;
const char *tag;
grn_ii *ii;
const uint8_t *chunk;
buffer_term *term;
uint16_t nth_term;
uint16_t n_terms;
uint32_t nth_chunk;
uint32_t n_chunks;
datavec data_vector[MAX_N_ELEMENTS + 1];
char name[GRN_TABLE_MAX_KEY_SIZE];
int name_size;
grn_obj inspected_term;
grn_obj inspected_entries;
uint32_t n_inspected_entries;
} merge_dump_source_data;
#define MERGE_DUMP_SOURCE_BATCH_SIZE 10
typedef enum {
MERGE_DUMP_SOURCE_ENTRY_BUFFER,
MERGE_DUMP_SOURCE_ENTRY_CHUNK,
} merge_dump_source_entry_type;
static void
merge_dump_source_flush_entries(grn_ctx *ctx, merge_dump_source_data *data)
{
if (GRN_TEXT_LEN(&(data->inspected_entries)) == 0) {
return;
}
GRN_LOG(ctx,
data->log_level,
"%.*s",
(int)GRN_TEXT_LEN(&(data->inspected_entries)),
GRN_TEXT_VALUE(&(data->inspected_entries)));
GRN_BULK_REWIND(&(data->inspected_entries));
data->n_inspected_entries = 0;
}
static void
merge_dump_source_add_entry(grn_ctx *ctx,
merge_dump_source_data *data,
merge_dump_source_entry_type type,
docinfo *info,
uint8_t *position_gaps)
{
if (GRN_TEXT_LEN(&(data->inspected_entries)) > 0) {
GRN_TEXT_PUTC(ctx, &(data->inspected_entries), ' ');
}
grn_text_printf(ctx,
&(data->inspected_entries),
"(%u:%u:%u:%u)",
info->rid,
info->sid,
info->tf,
info->weight);
if (data->ii->header.common->flags & GRN_OBJ_WITH_POSITION) {
uint32_t i;
uint32_t position = 0;
GRN_TEXT_PUTC(ctx, &(data->inspected_entries), '[');
for (i = 0; i < info->tf; i++) {
uint32_t position_gap;
if (i > 0) {
GRN_TEXT_PUTC(ctx, &(data->inspected_entries), ',');
}
if (type == MERGE_DUMP_SOURCE_ENTRY_BUFFER) {
GRN_B_DEC(position_gap, position_gaps);
} else {
position_gap = ((uint32_t *)position_gaps)[i];
}
position += position_gap;
grn_text_printf(ctx, &(data->inspected_entries), "%u", position);
}
GRN_TEXT_PUTC(ctx, &(data->inspected_entries), ']');
}
data->n_inspected_entries++;
if (data->n_inspected_entries == MERGE_DUMP_SOURCE_BATCH_SIZE) {
merge_dump_source_flush_entries(ctx, data);
}
}
static void
merge_dump_source_chunk_raw(grn_ctx *ctx,
merge_dump_source_data *data,
const uint8_t *chunk_start,
const uint8_t *chunk_end)
{
const uint8_t *chunk_current = chunk_start;
int decoded_size;
if ((chunk_end - chunk_current) == 0) {
GRN_LOG(ctx,
data->log_level,
"%s[%.*s][%d/%d] <%.*s>(%u): %u/%u: no data",
data->tag,
data->name_size,
data->name,
data->nth_term,
data->n_terms,
(int)GRN_TEXT_LEN(&(data->inspected_term)),
GRN_TEXT_VALUE(&(data->inspected_term)),
CHUNK_SPLIT_OFF(data->term->tid),
data->nth_chunk,
data->n_chunks);
return;
}
decoded_size = grn_decv(ctx,
data->ii,
CHUNK_SPLIT_OFF(data->term->tid),
chunk_current,
chunk_end - chunk_current,
data->data_vector);
if (decoded_size == 0) {
GRN_LOG(ctx,
data->log_level,
"%s[%.*s][%d/%d] <%.*s>(%u): %u/%u: failed to decode",
data->tag,
data->name_size,
data->name,
data->nth_term,
data->n_terms,
(int)GRN_TEXT_LEN(&(data->inspected_term)),
GRN_TEXT_VALUE(&(data->inspected_term)),
CHUNK_SPLIT_OFF(data->term->tid),
data->nth_chunk,
data->n_chunks);
return;
}
{
uint32_t n_documents;
const uint32_t *record_id_gaps;
const uint32_t *section_id_gaps = NULL;
const uint32_t *n_terms_list;
const uint32_t *weights = NULL;
const uint32_t *position_gaps = NULL;
{
int i = 0;
n_documents = data->data_vector[i].data_size;
record_id_gaps = data->data_vector[i++].data;
if (data->ii->header.common->flags & GRN_OBJ_WITH_SECTION) {
section_id_gaps = data->data_vector[i++].data;
}
n_terms_list = data->data_vector[i++].data;
if (data->ii->header.common->flags & GRN_OBJ_WITH_WEIGHT) {
weights = data->data_vector[i++].data;
}
if (data->ii->header.common->flags & GRN_OBJ_WITH_POSITION) {
position_gaps = data->data_vector[i++].data;
}
}
{
uint32_t i;
grn_id record_id = GRN_ID_NIL;
uint32_t section_id = 0;
for (i = 0; i < n_documents; i++) {
uint32_t record_id_gap = record_id_gaps[i];
docinfo info;
record_id += record_id_gap;
info.rid = record_id;
if (data->ii->header.common->flags & GRN_OBJ_WITH_SECTION) {
if (record_id_gap > 0) {
section_id = 0;
}
section_id += 1 + section_id_gaps[i];
} else {
section_id = 1;
}
info.sid = section_id;
info.tf = 1 + n_terms_list[i];
if (data->ii->header.common->flags & GRN_OBJ_WITH_WEIGHT) {
info.weight = weights[i];
} else {
info.weight = 0;
}
merge_dump_source_add_entry(ctx,
data,
MERGE_DUMP_SOURCE_ENTRY_CHUNK,
&info,
(uint8_t *)position_gaps);
}
merge_dump_source_flush_entries(ctx, data);
}
}
}
static void
merge_dump_source_chunk(grn_ctx *ctx, merge_dump_source_data *data)
{
const uint8_t *chunk_start = data->chunk + data->term->pos_in_chunk;
const uint8_t *chunk_end = chunk_start + data->term->size_in_chunk;
if (data->term->tid & CHUNK_SPLIT) {
const uint8_t *chunk_current = chunk_start;
GRN_B_DEC(data->n_chunks, chunk_current);
GRN_LOG(ctx,
data->log_level,
"%s[%.*s][%d/%d] <%.*s>(%u): chunk: %u",
data->tag,
data->name_size,
data->name,
data->nth_term,
data->n_terms,
(int)GRN_TEXT_LEN(&(data->inspected_term)),
GRN_TEXT_VALUE(&(data->inspected_term)),
CHUNK_SPLIT_OFF(data->term->tid),
data->n_chunks);
for (data->nth_chunk = 0; data->nth_chunk < data->n_chunks;
data->nth_chunk++) {
chunk_info info;
if (!(chunk_current < chunk_end)) {
GRN_LOG(ctx,
data->log_level,
"%s[%.*s][%d/%d] <%.*s>(%u): chunk: %u/%u: no data",
data->tag,
data->name_size,
data->name,
data->nth_term,
data->n_terms,
(int)GRN_TEXT_LEN(&(data->inspected_term)),
GRN_TEXT_VALUE(&(data->inspected_term)),
CHUNK_SPLIT_OFF(data->term->tid),
data->nth_chunk,
data->n_chunks);
return;
}
GRN_B_DEC(info.segno, chunk_current);
GRN_B_DEC(info.size, chunk_current);
GRN_B_DEC(info.dgap, chunk_current);
GRN_LOG(ctx,
data->log_level,
"%s[%.*s][%d/%d] <%.*s>(%u): chunk: %u/%u: "
"segment:<%d>, size:<%d>, gap:<%d>",
data->tag,
data->name_size,
data->name,
data->nth_term,
data->n_terms,
(int)GRN_TEXT_LEN(&(data->inspected_term)),
GRN_TEXT_VALUE(&(data->inspected_term)),
CHUNK_SPLIT_OFF(data->term->tid),
data->nth_chunk,
data->n_chunks,
info.segno,
info.size,
info.dgap);
if (info.size == 0) {
continue;
}
{
grn_io_win iw;
auto sub_chunk = static_cast<uint8_t *>(WIN_MAP(ctx,
data->ii->chunk,
&iw,
info.segno,
0,
info.size,
GRN_IO_RDONLY));
if (!sub_chunk) {
GRN_LOG(ctx,
data->log_level,
"%s[%.*s][%d/%d] <%.*s>(%u): chunk: "
"%u/%u: failed to open sub chunk",
data->tag,
data->name_size,
data->name,
data->nth_term,
data->n_terms,
(int)GRN_TEXT_LEN(&(data->inspected_term)),
GRN_TEXT_VALUE(&(data->inspected_term)),
CHUNK_SPLIT_OFF(data->term->tid),
data->nth_chunk,
data->n_chunks);
continue;
}
merge_dump_source_chunk_raw(ctx,
data,
sub_chunk,
sub_chunk + info.size);
grn_io_win_unmap(ctx, &iw);
}
}
if (chunk_current < chunk_end) {
GRN_LOG(ctx,
data->log_level,
"%s[%.*s][%d/%d] <%.*s>(%u): chunk: %u",
data->tag,
data->name_size,
data->name,
data->nth_term,
data->n_terms,
(int)GRN_TEXT_LEN(&(data->inspected_term)),
GRN_TEXT_VALUE(&(data->inspected_term)),
CHUNK_SPLIT_OFF(data->term->tid),
data->n_chunks);
merge_dump_source_chunk_raw(ctx, data, chunk_current, chunk_end);
}
} else {
data->nth_chunk = 0;
data->n_chunks = 0;
GRN_LOG(ctx,
data->log_level,
"%s[%.*s][%d/%d] <%.*s>(%u): chunk: %u",
data->tag,
data->name_size,
data->name,
data->nth_term,
data->n_terms,
(int)GRN_TEXT_LEN(&(data->inspected_term)),
GRN_TEXT_VALUE(&(data->inspected_term)),
data->term->tid,
data->n_chunks);
merge_dump_source_chunk_raw(ctx, data, chunk_start, chunk_end);
}
}
static void
merge_dump_source(grn_ctx *ctx,
grn_ii *ii,
buffer *buf,
const uint8_t *chunk,
grn_log_level log_level)
{
merge_dump_source_data data;
if (!grn_logger_pass(ctx, log_level)) {
return;
}
data.log_level = log_level;
data.tag = "[ii][merge][source]";
data.ii = ii;
data.chunk = chunk;
data.term = NULL;
data.nth_term = 0;
data.n_terms = buf->header.nterms;
data.nth_chunk = 0;
data.n_chunks = 0;
datavec_init(ctx, ii, data.data_vector, 0, 0);
{
GRN_DEFINE_NAME(ii);
grn_memcpy(data.name, name, name_size);
data.name_size = name_size;
}
GRN_TEXT_INIT(&(data.inspected_term), 0);
GRN_TEXT_INIT(&(data.inspected_entries), 0);
data.n_inspected_entries = 0;
GRN_LOG(ctx,
data.log_level,
"%s[%.*s] %u",
data.tag,
data.name_size,
data.name,
data.n_terms);
for (data.nth_term = 0; data.nth_term < data.n_terms; data.nth_term++) {
uint16_t position;
data.term = buf->terms + data.nth_term;
if (data.term->tid == 0) {
GRN_LOG(ctx,
data.log_level,
"%s[%.*s][%d] void",
data.tag,
data.name_size,
data.name,
data.nth_term);
continue;
}
GRN_BULK_REWIND(&(data.inspected_term));
grn_ii_get_term(ctx,
ii,
CHUNK_SPLIT_OFF(data.term->tid),
&(data.inspected_term));
GRN_LOG(ctx,
data.log_level,
"%s[%.*s][%d/%d] <%.*s>(%u): chunk:<%u:%u>, buffer:<%u:%u>",
data.tag,
data.name_size,
data.name,
data.nth_term,
data.n_terms,
(int)GRN_TEXT_LEN(&(data.inspected_term)),
GRN_TEXT_VALUE(&(data.inspected_term)),
CHUNK_SPLIT_OFF(data.term->tid),
data.term->pos_in_chunk,
data.term->size_in_chunk,
data.term->pos_in_buffer,
data.term->size_in_buffer);
position = data.term->pos_in_buffer;
while (position > 0) {
buffer_rec *record = BUFFER_REC_AT(buf, position);
uint8_t *record_data = GRN_NEXT_ADDR(record);
docinfo info;
grn_text_printf(ctx,
&(data.inspected_entries),
"record: %u: <%u:%u%s>",
position,
record->step,
record->jump,
BUFFER_REC_DELETED(record) ? ":deleted" : "");
position = record->step;
if (BUFFER_REC_DELETED(record)) {
merge_dump_source_flush_entries(ctx, &data);
continue;
}
GRN_TEXT_PUTC(ctx, &(data.inspected_entries), ':');
GRN_B_DEC(info.rid, record_data);
if (ii->header.common->flags & GRN_OBJ_WITH_SECTION) {
GRN_B_DEC(info.sid, record_data);
} else {
info.sid = 1;
}
GRN_B_DEC(info.tf, record_data);
if (ii->header.common->flags & GRN_OBJ_WITH_WEIGHT) {
GRN_B_DEC(info.weight, record_data);
} else {
info.weight = 0;
}
merge_dump_source_add_entry(ctx,
&data,
MERGE_DUMP_SOURCE_ENTRY_BUFFER,
&info,
record_data);
merge_dump_source_flush_entries(ctx, &data);
}
if (chunk && data.term->size_in_chunk > 0) {
merge_dump_source_chunk(ctx, &data);
}
}
datavec_fin(ctx, data.data_vector);
GRN_OBJ_FIN(ctx, &(data.inspected_term));
GRN_OBJ_FIN(ctx, &(data.inspected_entries));
}
typedef struct {
grn_ii *ii;
bool succeeded;
grn_obj source_chunks;
grn_obj dest_chunks;
} grn_merging_data;
static void
grn_merging_data_init(grn_ctx *ctx, grn_merging_data *data, grn_ii *ii)
{
data->ii = ii;
data->succeeded = false;
GRN_UINT32_INIT(&(data->source_chunks), GRN_OBJ_VECTOR);
GRN_UINT32_INIT(&(data->dest_chunks), GRN_OBJ_VECTOR);
}
static void
grn_merging_data_fin(grn_ctx *ctx, grn_merging_data *data)
{
grn_obj *target_chunks;
if (data->succeeded) {
target_chunks = &(data->source_chunks);
} else {
target_chunks = &(data->dest_chunks);
}
const size_t n_target_chunks = GRN_UINT32_VECTOR_SIZE(target_chunks) / 2;
size_t i;
for (i = 0; i < n_target_chunks; i++) {
const uint32_t segno = GRN_UINT32_VALUE_AT(target_chunks, i * 2);
const uint32_t size = GRN_UINT32_VALUE_AT(target_chunks, i * 2 + 1);
chunk_free(ctx, data->ii, segno, size);
}
GRN_OBJ_FIN(ctx, &(data->source_chunks));
GRN_OBJ_FIN(ctx, &(data->dest_chunks));
}
static void
grn_merging_data_add_source_chunk(grn_ctx *ctx,
grn_merging_data *data,
uint32_t segno,
uint32_t size)
{
GRN_UINT32_PUT(ctx, &(data->source_chunks), segno);
GRN_UINT32_PUT(ctx, &(data->source_chunks), size);
}
static void
grn_merging_data_add_dest_chunk(grn_ctx *ctx,
grn_merging_data *data,
uint32_t segno,
uint32_t size)
{
GRN_UINT32_PUT(ctx, &(data->dest_chunks), segno);
GRN_UINT32_PUT(ctx, &(data->dest_chunks), size);
}
typedef struct {
buffer *buf;
const uint8_t *data;
docinfo id;
uint16_t next_position;
} merger_buffer_data;
typedef struct {
const uint8_t *data_start;
const uint8_t *data_end;
const uint8_t *data;
docinfo id;
uint32_t n_documents;
const uint32_t *record_id_gaps;
const uint32_t *section_id_gaps;
const uint32_t *tfs;
const uint32_t *weights;
const uint32_t *position_gaps;
const uint32_t *position_gaps_end;
} merger_chunk_data;
typedef struct {
grn_ii *ii;
grn_id term_id;
docinfo last_id;
uint64_t position;
uint64_t max_position;
grn_merging_data *merging_data;
struct {
merger_buffer_data buffer;
merger_chunk_data chunk;
} source;
struct {
uint32_t *record_id_gaps;
uint32_t *section_id_gaps;
uint32_t *tfs;
uint32_t *weights;
uint32_t *position_gaps;
uint32_t *position_gaps_end;
} dest;
} merger_data;
static inline void
merger_init_chunk_data(grn_ctx *ctx, merger_data *data, datavec *rdv)
{
merger_chunk_data *chunk_data = &(data->source.chunk);
grn_ii *ii = data->ii;
int j = 0;
memset(&(chunk_data->id), 0, sizeof(docinfo));
chunk_data->n_documents = rdv[j].data_size;
chunk_data->record_id_gaps = rdv[j++].data;
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
chunk_data->section_id_gaps = rdv[j++].data;
}
chunk_data->tfs = rdv[j++].data;
if ((ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
chunk_data->weights = rdv[j++].data;
}
chunk_data->position_gaps = rdv[j].data;
chunk_data->position_gaps_end = chunk_data->position_gaps + rdv[j].data_size;
}
static inline void
merger_report_error(grn_ctx *ctx,
merger_data *data,
const char *message,
docinfo *posting1,
docinfo *posting2)
{
merger_buffer_data *buffer_data = &(data->source.buffer);
merger_chunk_data *chunk_data = &(data->source.chunk);
grn_obj term;
GRN_DEFINE_NAME(data->ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, data->ii, CHUNK_SPLIT_OFF(data->term_id), &term);
if (posting1 && posting2) {
CRIT(GRN_FILE_CORRUPT,
"[ii][broken] %s: <%.*s>: <%.*s>(%u): (%u:%u) -> (%u:%u)",
message,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(data->term_id),
posting1->rid,
posting1->sid,
posting2->rid,
posting2->sid);
} else {
CRIT(GRN_FILE_CORRUPT,
"[ii][broken] %s: <%.*s>: <%.*s>(%u): (%u:%u)",
message,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(data->term_id),
posting1->rid,
posting1->sid);
}
GRN_OBJ_FIN(ctx, &term);
merge_dump_source(ctx,
data->ii,
buffer_data->buf,
chunk_data->data_start,
GRN_LOG_CRIT);
}
static inline void
merger_get_next_chunk(grn_ctx *ctx, merger_data *data)
{
merger_chunk_data *chunk_data = &(data->source.chunk);
if (chunk_data->n_documents == 0) {
chunk_data->id.rid = 0;
} else {
uint32_t record_id_gap = *chunk_data->record_id_gaps;
chunk_data->record_id_gaps++;
chunk_data->id.rid += record_id_gap;
if (record_id_gap > 0) {
chunk_data->id.sid = 0;
}
chunk_data->position_gaps += chunk_data->id.tf;
chunk_data->id.tf = 1 + *chunk_data->tfs;
chunk_data->tfs++;
if ((data->ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
chunk_data->id.weight = *chunk_data->weights;
chunk_data->weights++;
}
if ((data->ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
chunk_data->id.sid += 1 + *chunk_data->section_id_gaps;
chunk_data->section_id_gaps++;
} else {
chunk_data->id.sid = 1;
}
chunk_data->n_documents--;
}
}
static inline void
merger_put_next_id(grn_ctx *ctx, merger_data *data, docinfo *id)
{
uint32_t record_id_gap = id->rid - data->last_id.rid;
*(data->dest.record_id_gaps) = record_id_gap;
data->dest.record_id_gaps++;
if ((data->ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
uint32_t section_id_gap;
if (record_id_gap > 0) {
section_id_gap = id->sid - 1;
} else {
section_id_gap = id->sid - data->last_id.sid - 1;
}
*(data->dest.section_id_gaps) = section_id_gap;
data->dest.section_id_gaps++;
}
*(data->dest.tfs) = id->tf - 1;
data->dest.tfs++;
if ((data->ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
*(data->dest.weights) = id->weight;
data->dest.weights++;
}
data->last_id.rid = id->rid;
data->last_id.sid = id->sid;
}
static inline void
merger_put_next_chunk(grn_ctx *ctx, merger_data *data)
{
merger_chunk_data *chunk_data = &(data->source.chunk);
if (chunk_data->id.rid > 0) {
if (chunk_data->id.tf > 0) {
if (data->last_id.rid > chunk_data->id.rid ||
(data->last_id.rid == chunk_data->id.rid &&
data->last_id.sid >= chunk_data->id.sid)) {
merger_report_error(ctx,
data,
"the last posting is larger than "
"the next posting in chunk",
&(data->last_id),
&(chunk_data->id));
return;
}
merger_put_next_id(ctx, data, &(chunk_data->id));
if ((data->ii->header.common->flags & GRN_OBJ_WITH_POSITION)) {
uint32_t i;
uint32_t rest_n_positions =
(uint32_t)(chunk_data->position_gaps_end - chunk_data->position_gaps);
uint32_t rest_n_dest_positions =
(uint32_t)(data->dest.position_gaps_end - data->dest.position_gaps);
if (chunk_data->id.tf > rest_n_positions) {
grn_obj term;
GRN_DEFINE_NAME(data->ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, data->ii, CHUNK_SPLIT_OFF(data->term_id), &term);
GRN_LOG(
ctx,
GRN_LOG_WARNING,
"[ii][merge][put][chunk] "
"the number of terms are larger than the number of positions: "
"<%.*s>: <%.*s>(%u): (%u:%u): (%u:%u)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(data->term_id),
chunk_data->id.rid,
chunk_data->id.sid,
chunk_data->id.tf,
rest_n_positions);
GRN_OBJ_FIN(ctx, &term);
}
if (chunk_data->id.tf > rest_n_dest_positions) {
/* TODO: Make this case error. */
grn_obj term;
GRN_DEFINE_NAME(data->ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, data->ii, CHUNK_SPLIT_OFF(data->term_id), &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][merge][put][chunk] "
"the number of terms are larger than "
"the rest destination number of positions: "
"<%.*s>: <%.*s>(%u): (%u:%u): (%u:%u)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(data->term_id),
chunk_data->id.rid,
chunk_data->id.sid,
chunk_data->id.tf,
rest_n_dest_positions);
GRN_OBJ_FIN(ctx, &term);
}
for (i = 0; i < chunk_data->id.tf; i++) {
*(data->dest.position_gaps) = chunk_data->position_gaps[i];
data->dest.position_gaps++;
data->position += chunk_data->position_gaps[i];
}
if (data->position > data->max_position) {
data->max_position = data->position;
}
}
} else {
merger_report_error(ctx,
data,
"invalid posting in chunk",
&(chunk_data->id),
NULL);
return;
}
}
merger_get_next_chunk(ctx, data);
}
static inline void
merger_get_next_buffer(grn_ctx *ctx, merger_data *data)
{
merger_buffer_data *buffer_data = &(data->source.buffer);
if (buffer_data->next_position == 0) {
buffer_data->id.rid = 0;
} else {
docinfo last_id = {buffer_data->id.rid, buffer_data->id.sid, 0, 0, 0};
buffer_rec *record =
BUFFER_REC_AT(buffer_data->buf, buffer_data->next_position);
buffer_data->data = GRN_NEXT_ADDR(record);
GRN_B_DEC(buffer_data->id.rid, buffer_data->data);
if ((data->ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_DEC(buffer_data->id.sid, buffer_data->data);
} else {
buffer_data->id.sid = 1;
}
if (last_id.rid > buffer_data->id.rid ||
(last_id.rid == buffer_data->id.rid &&
last_id.sid >= buffer_data->id.sid)) {
merger_report_error(ctx,
data,
"postings in block aren't sorted",
&last_id,
&(buffer_data->id));
return;
}
buffer_data->next_position = record->step;
}
}
static inline void
merger_put_next_buffer(grn_ctx *ctx, merger_data *data)
{
merger_buffer_data *buffer_data = &(data->source.buffer);
if (buffer_data->id.rid > 0 && buffer_data->id.sid > 0) {
GRN_B_DEC(buffer_data->id.tf, buffer_data->data);
if (buffer_data->id.tf > 0) {
if (data->last_id.rid > buffer_data->id.rid ||
(data->last_id.rid == buffer_data->id.rid &&
data->last_id.sid >= buffer_data->id.sid)) {
merger_report_error(ctx,
data,
"the last posting is larger than "
"the next posting in buffer",
&(data->last_id),
&(buffer_data->id));
return;
}
if ((data->ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
GRN_B_DEC(buffer_data->id.weight, buffer_data->data);
}
merger_put_next_id(ctx, data, &(buffer_data->id));
if ((data->ii->header.common->flags & GRN_OBJ_WITH_POSITION)) {
uint32_t rest_n_dest_positiongs =
(uint32_t)(data->dest.position_gaps_end - data->dest.position_gaps);
if (buffer_data->id.tf > rest_n_dest_positiongs) {
/* TODO: Make this case error. */
merger_buffer_data *buffer_data = &(data->source.buffer);
grn_obj term;
GRN_DEFINE_NAME(data->ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, data->ii, CHUNK_SPLIT_OFF(data->term_id), &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][merge][put][buffer] "
"the number of terms are larger than "
"the rest destination number of positions: "
"<%.*s>: <%.*s>(%u): (%u:%u): (%u:%u)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(data->term_id),
buffer_data->id.rid,
buffer_data->id.sid,
buffer_data->id.tf,
rest_n_dest_positiongs);
GRN_OBJ_FIN(ctx, &term);
}
while (buffer_data->id.tf--) {
GRN_B_DEC(*(data->dest.position_gaps), buffer_data->data);
data->position += *(data->dest.position_gaps);
data->dest.position_gaps++;
}
if (data->position > data->max_position) {
data->max_position = data->position;
}
}
}
}
merger_get_next_buffer(ctx, data);
}
static inline bool
merger_merge(grn_ctx *ctx, merger_data *data)
{
merger_buffer_data *buffer_data = &(data->source.buffer);
merger_chunk_data *chunk_data = &(data->source.chunk);
if (buffer_data->id.rid > 0) {
if (chunk_data->id.rid > 0) {
if (chunk_data->id.rid < buffer_data->id.rid) {
merger_put_next_chunk(ctx, data);
if (ctx->rc != GRN_SUCCESS) {
return false;
}
} else {
if (buffer_data->id.rid < chunk_data->id.rid) {
merger_put_next_buffer(ctx, data);
if (ctx->rc != GRN_SUCCESS) {
return false;
}
} else {
if (buffer_data->id.sid > 0) {
if (chunk_data->id.sid < buffer_data->id.sid) {
merger_put_next_chunk(ctx, data);
if (ctx->rc != GRN_SUCCESS) {
return false;
}
} else {
if (buffer_data->id.sid == chunk_data->id.sid) {
merger_get_next_chunk(ctx, data);
if (ctx->rc != GRN_SUCCESS) {
return false;
}
}
merger_put_next_buffer(ctx, data);
if (ctx->rc != GRN_SUCCESS) {
return false;
}
}
} else {
merger_get_next_chunk(ctx, data);
if (ctx->rc != GRN_SUCCESS) {
return false;
}
}
}
}
} else {
merger_put_next_buffer(ctx, data);
if (ctx->rc != GRN_SUCCESS) {
return false;
}
}
} else {
if (chunk_data->id.rid > 0) {
merger_put_next_chunk(ctx, data);
if (ctx->rc != GRN_SUCCESS) {
return false;
}
} else {
return false;
}
}
return true;
}
static grn_rc
chunk_flush(
grn_ctx *ctx, grn_ii *ii, chunk_info *cinfo, uint8_t *enc, uint32_t encsize)
{
uint32_t dcn;
grn_io_win dw;
if (encsize) {
chunk_new(ctx, ii, &dcn, encsize);
if (ctx->rc == GRN_SUCCESS) {
auto dc = static_cast<uint8_t *>(
WIN_MAP(ctx, ii->chunk, &dw, dcn, 0, encsize, GRN_IO_WRONLY));
if (dc) {
grn_memcpy(dc, enc, encsize);
grn_io_win_unmap(ctx, &dw);
cinfo->segno = dcn;
cinfo->size = encsize;
} else {
chunk_free(ctx, ii, dcn, encsize);
{
GRN_DEFINE_NAME(ii);
MERR("[ii][chunk][flush] failed to allocate a destination chunk: "
"<%.*s>:"
"segment:<%u>, size:<%u>",
name_size,
name,
dcn,
encsize);
}
}
}
} else {
cinfo->segno = 0;
cinfo->size = 0;
}
return ctx->rc;
}
static grn_rc
chunk_merge(grn_ctx *ctx,
merger_data *data,
chunk_info *cinfo,
grn_id rid,
datavec *dv,
int32_t *balance)
{
merger_buffer_data *buffer_data = &(data->source.buffer);
merger_chunk_data *chunk_data = &(data->source.chunk);
grn_ii *ii = data->ii;
grn_id term_id = CHUNK_SPLIT_OFF(data->term_id);
grn_io_win sw;
uint32_t segno = cinfo->segno;
uint32_t size = cinfo->size;
uint32_t ndf = 0;
auto scp = static_cast<const uint8_t *>(
WIN_MAP(ctx, ii->chunk, &sw, segno, 0, size, GRN_IO_RDONLY));
datavec rdv[MAX_N_ELEMENTS + 1];
size_t bufsize = S_SEGMENT * ii->n_elements;
if (!scp) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, term_id, &term);
MERR("[ii][chunk][merge] failed to allocate a source chunk: "
"<%.*s>: "
"<%.*s>(%u): "
"record:<%u>, segment:<%u>, size:<%u>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
term_id,
rid,
segno,
size);
GRN_OBJ_FIN(ctx, &term);
return ctx->rc;
}
data->last_id = null_docinfo;
chunk_data->section_id_gaps = NULL;
chunk_data->weights = NULL;
datavec_init(ctx, ii, rdv, 0, 0);
if (ctx->rc != GRN_SUCCESS) {
grn_io_win_unmap(ctx, &sw);
{
GRN_DEFINE_NAME(ii);
ERR(ctx->rc,
"[ii][chunk][merge] failed to initialize data vector: <%.*s>",
name_size,
name);
}
return ctx->rc;
}
{
int decoded_size;
decoded_size = grn_decv(ctx, ii, term_id, scp, cinfo->size, rdv);
if (decoded_size == 0) {
grn_obj term;
grn_rc rc = ctx->rc;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, term_id, &term);
if (rc == GRN_SUCCESS) {
rc = GRN_UNKNOWN_ERROR;
}
ERR(rc,
"[ii][chunk][merge] failed to decode: "
"<%.*s>: "
"<%.*s>(%u): "
"<%u>: "
"<%u>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
term_id,
rid,
cinfo->size);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
bufsize += decoded_size;
}
merger_init_chunk_data(ctx, data, rdv);
/* (df in chunk list) = a[1] - chunk_data->n_documents; */
datavec_reset(ctx, ii, dv, chunk_data->n_documents + S_SEGMENT, bufsize);
if (ctx->rc != GRN_SUCCESS) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, term_id, &term);
ERR(ctx->rc,
"[ii][chunk][merge] failed to reset data vector: "
"<%.*s>: "
"<%.*s>(%u)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
term_id);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
{
int j = 0;
data->dest.record_id_gaps = dv[j++].data;
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
data->dest.section_id_gaps = dv[j++].data;
}
data->dest.tfs = dv[j++].data;
if ((ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
data->dest.weights = dv[j++].data;
}
data->dest.position_gaps = dv[j++].data;
data->dest.position_gaps_end = dv[j].data;
}
merger_get_next_chunk(ctx, data);
if (ctx->rc != GRN_SUCCESS) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, term_id, &term);
ERR(ctx->rc,
"[ii][chunk][merge] failed to get the next chunk posting: "
"<%.*s>: "
"<%.*s>(%u)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
term_id);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
data->position = 0;
do {
if (!merger_merge(ctx, data)) {
break;
}
} while (buffer_data->id.rid <= rid || chunk_data->id.rid);
if (ctx->rc != GRN_SUCCESS) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, term_id, &term);
ERR(ctx->rc,
"[ii][chunk][merge] failed to merge: "
"<%.*s>: "
"<%.*s>(%u)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
term_id);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
ndf = data->dest.record_id_gaps - dv[0].data;
{
const uint32_t n_positions =
data->dest.position_gaps - dv[ii->n_elements - 1].data;
datavec_set_data_size(ctx,
ii,
dv,
ndf,
data->last_id.rid,
n_positions,
data->max_position);
const ssize_t encsize_estimated = grn_encv(ctx, ii, term_id, dv, NULL, 0);
if (encsize_estimated == -1) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, term_id, &term);
MERR("[ii][chunk][merge] failed to estimate encode buffer size: "
"<%.*s>: "
"<%.*s>(%u): "
"record:<%u>, "
"segment:<%u>, "
"size:<%u>, "
"estimated-buffer-size: <%" GRN_FMT_SSIZE ">",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
term_id,
rid,
segno,
size,
encsize_estimated);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
if (encsize_estimated == 0) {
chunk_flush(ctx, ii, cinfo, NULL, 0);
} else {
auto enc = static_cast<uint8_t *>(GRN_MALLOC(encsize_estimated));
if (!enc) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, term_id, &term);
MERR("[ii][chunk][merge] failed to allocate a encode buffer: "
"<%.*s>: "
"<%.*s>(%u): "
"record:<%u>, "
"segment:<%u>, "
"size:<%u>, "
"estimated-buffer-size: <%" GRN_FMT_SSIZE ">",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
term_id,
rid,
segno,
size,
encsize_estimated);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
const ssize_t encsize =
grn_encv(ctx, ii, term_id, dv, enc, encsize_estimated);
if (encsize == -1) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, term_id, &term);
MERR("[ii][chunk][merge] failed to encode: "
"<%.*s>: "
"<%.*s>(%u): "
"record:<%u>, "
"segment:<%u>, "
"size:<%u>, "
"estimated-buffer-size: <%" GRN_FMT_SSIZE ">",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
term_id,
rid,
segno,
size,
encsize_estimated);
GRN_OBJ_FIN(ctx, &term);
GRN_FREE(enc);
goto exit;
}
chunk_flush(ctx, ii, cinfo, enc, encsize);
if (ctx->rc == GRN_SUCCESS) {
grn_merging_data_add_dest_chunk(ctx,
data->merging_data,
cinfo->segno,
cinfo->size);
}
GRN_FREE(enc);
}
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
grn_merging_data_add_source_chunk(ctx, data->merging_data, segno, size);
}
*balance += (ndf - chunk_data->n_documents);
exit:
datavec_fin(ctx, rdv);
grn_io_win_unmap(ctx, &sw);
return ctx->rc;
}
static void
buffer_merge_dump_datavec(grn_ctx *ctx, grn_ii *ii, datavec *dv, datavec *rdv)
{
uint32_t i, j;
grn_obj buffer;
GRN_TEXT_INIT(&buffer, 0);
for (i = 0; i < ii->n_elements; i++) {
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"rdv[%d] data_size=%d, flags=%d",
i,
rdv[i].data_size,
rdv[i].flags);
GRN_BULK_REWIND(&buffer);
for (j = 0; j < rdv[i].data_size;) {
grn_text_printf(ctx, &buffer, " %d", rdv[i].data[j]);
j++;
if (!(j % 32) || j == rdv[i].data_size) {
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"rdv[%d].data[%d]%.*s",
i,
j,
(int)GRN_TEXT_LEN(&buffer),
GRN_TEXT_VALUE(&buffer));
GRN_BULK_REWIND(&buffer);
}
}
}
for (i = 0; i < ii->n_elements; i++) {
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"dv[%d] data_size=%d, flags=%d",
i,
dv[i].data_size,
dv[i].flags);
GRN_BULK_REWIND(&buffer);
for (j = 0; j < dv[i].data_size;) {
grn_text_printf(ctx, &buffer, " %d", dv[i].data[j]);
j++;
if (!(j % 32) || j == dv[i].data_size) {
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"dv[%d].data[%d]%.*s",
i,
j,
(int)GRN_TEXT_LEN(&buffer),
GRN_TEXT_VALUE(&buffer));
GRN_BULK_REWIND(&buffer);
}
}
}
GRN_OBJ_FIN(ctx, &buffer);
}
static void
buffer_merge_ensure_dc(grn_ctx *ctx,
grn_ii *ii,
uint8_t **dc,
uint8_t **dcp,
size_t *dc_size,
size_t required_size,
const char *tag)
{
size_t dc_used_size = *dcp - *dc;
if (required_size <= (*dc_size - dc_used_size)) {
return;
}
size_t new_dc_size = *dc_size * 2;
while (required_size > (new_dc_size - dc_used_size)) {
new_dc_size *= 2;
}
auto new_dc = static_cast<uint8_t *>(GRN_REALLOC(*dc, new_dc_size));
if (new_dc) {
GRN_DEFINE_NAME(ii);
GRN_LOG(ctx,
GRN_LOG_INFO,
"[ii][buffer][merge]%s destination chunk is expanded: "
"<%.*s>: "
"<%" GRN_FMT_SIZE "> -> <%" GRN_FMT_SIZE ">",
tag,
name_size,
name,
*dc_size,
new_dc_size);
*dcp = new_dc + dc_used_size;
*dc = new_dc;
*dc_size = new_dc_size;
} else {
grn_rc rc = ctx->rc;
if (rc == GRN_SUCCESS) {
rc = GRN_NO_MEMORY_AVAILABLE;
}
GRN_DEFINE_NAME(ii);
ERR(rc,
"[ii][buffer][merge]%s failed to expand destination chunk: "
"<%.*s>: "
"<%" GRN_FMT_SIZE "> -> <%" GRN_FMT_SIZE ">",
tag,
name_size,
name,
*dc_size,
new_dc_size);
}
}
static inline grn_rc
buffer_merge_internal(grn_ctx *ctx,
grn_ii *ii,
uint32_t seg,
grn_hash *h,
buffer *sb,
const uint8_t *sc,
buffer *db,
uint8_t **dc_output,
grn_merging_data *merging_data)
{
buffer_term *bt;
uint8_t *dcp = NULL;
datavec dv[MAX_N_ELEMENTS + 1];
datavec rdv[MAX_N_ELEMENTS + 1];
uint16_t n = db->header.nterms, nterms_void = 0;
size_t unitsize = (S_SEGMENT + sb->header.chunk_size / sb->header.nterms) * 2;
// size_t unitsize = (S_SEGMENT + sb->header.chunk_size) * 2 + (1<<24);
size_t totalsize = unitsize * ii->n_elements;
// todo : realloc
datavec_init(ctx, ii, dv, unitsize, totalsize);
if (ctx->rc != GRN_SUCCESS) {
GRN_DEFINE_NAME(ii);
ERR(ctx->rc,
"[ii][buffer][merge] failed to initialize data vector: "
"<%.*s>: "
"unit-size:<%" GRN_FMT_SIZE ">, "
"total-size:<%" GRN_FMT_SIZE ">",
name_size,
name,
unitsize,
totalsize);
return ctx->rc;
}
datavec_init(ctx, ii, rdv, 0, 0);
if (ctx->rc != GRN_SUCCESS) {
datavec_fin(ctx, dv);
GRN_DEFINE_NAME(ii);
ERR(ctx->rc,
"[ii][buffer][merge] failed to initialize data vector for read: "
"<%.*s>",
name_size,
name);
return ctx->rc;
}
size_t dc_size = (sb->header.chunk_size + S_SEGMENT) * 2;
auto dc = static_cast<uint8_t *>(GRN_MALLOC(dc_size));
if (!dc) {
grn_rc rc = ctx->rc;
if (rc == GRN_SUCCESS) {
rc = GRN_NO_MEMORY_AVAILABLE;
}
GRN_DEFINE_NAME(ii);
ERR(rc,
"[ii][buffer][merge] failed to allocate destination chunk: "
"<%.*s>: <%" GRN_FMT_SIZE ">",
name_size,
name,
dc_size);
goto exit;
}
dcp = dc;
for (bt = db->terms; n; n--, bt++) {
merger_data data = {};
merger_buffer_data *buffer_data = &(data.source.buffer);
merger_chunk_data *chunk_data = &(data.source.chunk);
int32_t balance = 0;
uint32_t nchunks = 0;
uint32_t nvchunks = 0;
chunk_info *cinfo = NULL;
grn_id crid = GRN_ID_NIL;
if (!bt->tid) {
nterms_void++;
continue;
}
data.ii = ii;
data.term_id = bt->tid;
data.merging_data = merging_data;
buffer_data->buf = sb;
chunk_data->data_start = sc;
if (bt->pos_in_buffer == 0) {
if (bt->size_in_buffer > 0) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, CHUNK_SPLIT_OFF(bt->tid), &term);
GRN_LOG(ctx,
GRN_WARN,
"[ii][buffer][merge] invalid size for buffer term: "
"<%.*s>: "
"<%.*s>(%u): "
"size:<%u>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(bt->tid),
bt->size_in_buffer);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
if (bt->size_in_chunk > 0) {
buffer_merge_ensure_dc(ctx,
ii,
&dc,
&dcp,
&dc_size,
bt->size_in_chunk,
"[copy-chunk]");
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
grn_memcpy(dcp,
chunk_data->data_start + bt->pos_in_chunk,
bt->size_in_chunk);
bt->pos_in_chunk = (uint32_t)(dcp - dc);
dcp += bt->size_in_chunk;
}
continue;
}
buffer_data->next_position = bt->pos_in_buffer;
merger_get_next_buffer(ctx, &data);
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
if (chunk_data->data_start && bt->size_in_chunk > 0) {
size_t size = S_SEGMENT * ii->n_elements;
chunk_data->data = chunk_data->data_start + bt->pos_in_chunk;
chunk_data->data_end = chunk_data->data + bt->size_in_chunk;
if ((bt->tid & CHUNK_SPLIT)) {
uint32_t i;
GRN_B_DEC(nchunks, chunk_data->data);
if (!(cinfo = GRN_MALLOCN(chunk_info, nchunks + 1))) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, CHUNK_SPLIT_OFF(bt->tid), &term);
MERR("[ii][buffer][merge] failed to allocate chunk info: "
"<%.*s>: "
"<%.*s>(%u): "
"segment:<%u>, "
"n-chunks:<%u>, "
"unit-size:<%" GRN_FMT_SIZE ">, "
"total-size:<%" GRN_FMT_SIZE ">",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(bt->tid),
seg,
nchunks,
unitsize,
totalsize);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
grn_id dgap_keep = GRN_ID_NIL;
for (i = 0; i < nchunks; i++) {
GRN_B_DEC(cinfo[i].segno, chunk_data->data);
GRN_B_DEC(cinfo[i].size, chunk_data->data);
GRN_B_DEC(cinfo[i].dgap, chunk_data->data);
crid += cinfo[i].dgap;
if (buffer_data->id.rid <= crid) {
chunk_merge(ctx, &data, &cinfo[i], crid, dv, &balance);
if (ctx->rc != GRN_SUCCESS) {
if (cinfo) {
GRN_FREE(cinfo);
}
{
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, CHUNK_SPLIT_OFF(bt->tid), &term);
ERR(ctx->rc,
"[ii][buffer][merge] failed to merge chunk: "
"<%.*s>: "
"<%.*s>(%u): "
"chunk:<%u>, "
"n-chunks:<%u>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(bt->tid),
i,
nchunks);
GRN_OBJ_FIN(ctx, &term);
}
goto exit;
}
}
if (cinfo[i].size == 0) {
dgap_keep += cinfo[i].dgap;
} else {
cinfo[i].dgap += dgap_keep;
dgap_keep = GRN_ID_NIL;
nvchunks++;
}
}
}
if (chunk_data->data_end > chunk_data->data) {
int decoded_size;
decoded_size = grn_decv(ctx,
ii,
CHUNK_SPLIT_OFF(bt->tid),
chunk_data->data,
chunk_data->data_end - chunk_data->data,
rdv);
if (decoded_size == 0) {
if (cinfo) {
GRN_FREE(cinfo);
}
{
grn_obj term;
grn_rc rc = ctx->rc;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, CHUNK_SPLIT_OFF(bt->tid), &term);
if (rc == GRN_SUCCESS) {
rc = GRN_UNKNOWN_ERROR;
}
ERR(rc,
"[ii][buffer][merge] failed to decode: "
"<%.*s>: "
"<%.*s>(%u): "
"n-chunks:<%u>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(bt->tid),
nvchunks);
GRN_OBJ_FIN(ctx, &term);
}
goto exit;
}
size += decoded_size;
merger_init_chunk_data(ctx, &data, rdv);
datavec_reset(ctx, ii, dv, chunk_data->n_documents + S_SEGMENT, size);
if (ctx->rc != GRN_SUCCESS) {
if (cinfo) {
GRN_FREE(cinfo);
}
{
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, CHUNK_SPLIT_OFF(bt->tid), &term);
ERR(ctx->rc,
"[ii][buffer][merge] failed to reset data vector: "
"<%.*s>: "
"<%.*s>(%u): "
"unit-size:<%" GRN_FMT_SIZE ">, "
"total-size:<%" GRN_FMT_SIZE ">",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(bt->tid),
(size_t)(chunk_data->n_documents + S_SEGMENT),
size);
GRN_OBJ_FIN(ctx, &term);
}
goto exit;
}
}
}
{
int j = 0;
data.dest.record_id_gaps = dv[j++].data;
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
data.dest.section_id_gaps = dv[j++].data;
}
data.dest.tfs = dv[j++].data;
if ((ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
data.dest.weights = dv[j++].data;
}
data.dest.position_gaps = dv[j++].data;
data.dest.position_gaps_end = dv[j].data;
}
data.last_id = null_docinfo;
merger_get_next_chunk(ctx, &data);
if (ctx->rc != GRN_SUCCESS) {
if (cinfo) {
GRN_FREE(cinfo);
}
goto exit;
}
data.position = 0;
while (merger_merge(ctx, &data)) {
}
if (ctx->rc != GRN_SUCCESS) {
if (cinfo) {
GRN_FREE(cinfo);
}
{
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, CHUNK_SPLIT_OFF(bt->tid), &term);
ERR(ctx->rc,
"[ii][buffer][merge] failed to merge chunk: "
"<%.*s>: "
"<%.*s>(%u)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
CHUNK_SPLIT_OFF(bt->tid));
GRN_OBJ_FIN(ctx, &term);
}
goto exit;
}
{
/* TODO: Is n_documents better? */
uint32_t ndf = data.dest.record_id_gaps - dv[0].data;
grn_id tid = CHUNK_SPLIT_OFF(bt->tid);
uint32_t *a = array_at(ctx, ii, tid);
if (!a) {
/* TODO: warning */
GRN_LOG(ctx, GRN_LOG_DEBUG, "array_entry not found tid=%d", tid);
memset(bt, 0, sizeof(buffer_term));
nterms_void++;
} else {
if (!ndf && !nvchunks) {
a[0] = 0;
a[1] = 0;
lexicon_delete(ctx, ii, tid, h);
memset(bt, 0, sizeof(buffer_term));
nterms_void++;
} else if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION) &&
!nvchunks && ndf == 1 && data.last_id.rid < 0x100000 &&
data.last_id.sid < 0x800 && data.last_id.tf == 1 &&
data.last_id.weight == 0) {
a[0] = POS_EMBED_RID_SID(data.last_id.rid, data.last_id.sid);
if (ii->header.common->flags & GRN_OBJ_WITH_POSITION) {
a[1] = data.dest.position_gaps[-1];
} else {
a[1] = 0;
}
memset(bt, 0, sizeof(buffer_term));
nterms_void++;
} else if (!(ii->header.common->flags & GRN_OBJ_WITH_SECTION) &&
!nvchunks && ndf == 1 && data.last_id.tf == 1 &&
data.last_id.weight == 0) {
a[0] = POS_EMBED_RID(data.last_id.rid);
if (ii->header.common->flags & GRN_OBJ_WITH_POSITION) {
a[1] = data.dest.position_gaps[-1];
} else {
a[1] = 0;
}
memset(bt, 0, sizeof(buffer_term));
nterms_void++;
} else {
const uint32_t n_positions =
data.dest.position_gaps - dv[ii->n_elements - 1].data;
datavec_set_data_size(ctx,
ii,
dv,
ndf,
data.last_id.rid,
n_positions,
data.max_position);
const size_t dc_offset = dcp - dc;
a[1] = (bt->size_in_chunk ? a[1] : 0) +
(ndf - chunk_data->n_documents) + balance;
if (nvchunks) {
buffer_merge_ensure_dc(ctx,
ii,
&dc,
&dcp,
&dc_size,
GRN_B_ENC_MAX_SIZE * (1 + 3 * nchunks),
"[encode-chunk-metadata]");
if (ctx->rc != GRN_SUCCESS) {
if (cinfo) {
GRN_FREE(cinfo);
}
array_unref(ctx, ii, tid);
goto exit;
}
GRN_B_ENC(nvchunks, dcp);
for (uint32_t i = 0; i < nchunks; i++) {
if (cinfo[i].size) {
GRN_B_ENC(cinfo[i].segno, dcp);
GRN_B_ENC(cinfo[i].size, dcp);
GRN_B_ENC(cinfo[i].dgap, dcp);
}
}
}
const ssize_t estimated_encsize = grn_encv(ctx, ii, tid, dv, NULL, 0);
if (estimated_encsize == -1) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
ERR(ctx->rc,
"[ii][buffer][merge] failed to estimate encode buffer size: "
"<%.*s>: "
"<%.*s>(%u)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid);
GRN_OBJ_FIN(ctx, &term);
buffer_merge_dump_datavec(ctx, ii, dv, rdv);
if (cinfo) {
GRN_FREE(cinfo);
}
array_unref(ctx, ii, tid);
goto exit;
}
buffer_merge_ensure_dc(ctx,
ii,
&dc,
&dcp,
&dc_size,
estimated_encsize,
"[encode-data-vector]");
if (ctx->rc != GRN_SUCCESS) {
buffer_merge_dump_datavec(ctx, ii, dv, rdv);
if (cinfo) {
GRN_FREE(cinfo);
}
array_unref(ctx, ii, tid);
goto exit;
}
const ssize_t encsize =
grn_encv(ctx, ii, tid, dv, dcp, dc_size - (dcp - dc));
if (encsize == -1) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
ERR(ctx->rc,
"[ii][buffer][merge] failed to encode: "
"<%.*s>: "
"<%.*s>(%u): "
"<%" GRN_FMT_SSIZE ">",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
estimated_encsize);
GRN_OBJ_FIN(ctx, &term);
buffer_merge_dump_datavec(ctx, ii, dv, rdv);
if (cinfo) {
GRN_FREE(cinfo);
}
array_unref(ctx, ii, tid);
goto exit;
}
if (encsize > CHUNK_SPLIT_THRESHOLD &&
(cinfo || (cinfo = GRN_MALLOCN(chunk_info, nchunks + 1))) &&
!chunk_flush(ctx, ii, &cinfo[nchunks], dcp, encsize)) {
uint32_t i;
cinfo[nchunks].dgap = data.last_id.rid - crid;
nvchunks++;
dcp = dc + dc_offset;
GRN_B_ENC(nvchunks, dcp);
for (i = 0; i <= nchunks; i++) {
if (cinfo[i].size) {
GRN_B_ENC(cinfo[i].segno, dcp);
GRN_B_ENC(cinfo[i].size, dcp);
GRN_B_ENC(cinfo[i].dgap, dcp);
}
}
/* TODO: format */
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"split (%d) encsize=%" GRN_FMT_SSIZE,
tid,
encsize);
bt->tid |= CHUNK_SPLIT;
} else {
dcp += encsize;
if (!nvchunks) {
bt->tid &= ~CHUNK_SPLIT;
}
}
bt->pos_in_chunk = (uint32_t)dc_offset;
bt->size_in_chunk = (uint32_t)(dcp - (dc + dc_offset));
bt->size_in_buffer = 0;
bt->pos_in_buffer = 0;
}
array_unref(ctx, ii, tid);
}
}
if (cinfo) {
GRN_FREE(cinfo);
}
}
db->header.chunk_size = (uint32_t)(dcp - dc);
db->header.buffer_free =
S_SEGMENT - sizeof(buffer_header) - db->header.nterms * sizeof(buffer_term);
db->header.nterms_void = nterms_void;
if (grn_ii_dump_index_source_on_merge) {
merge_dump_source(ctx, ii, sb, sc, GRN_LOG_DEBUG);
}
exit:
if (ctx->rc == GRN_SUCCESS) {
*dc_output = dc;
} else {
if (dc) {
GRN_FREE(dc);
}
}
datavec_fin(ctx, dv);
datavec_fin(ctx, rdv);
return ctx->rc;
}
static grn_rc
buffer_merge(grn_ctx *ctx,
grn_ii *ii,
uint32_t seg,
grn_hash *h,
buffer *sb,
const uint8_t *sc,
buffer *db,
uint8_t **dc_output,
grn_merging_data *merging_data)
{
GRN_SLOW_LOG_PUSH(ctx, GRN_LOG_DEBUG);
grn_rc rc =
buffer_merge_internal(ctx, ii, seg, h, sb, sc, db, dc_output, merging_data);
GRN_SLOW_LOG_POP_BEGIN(ctx, GRN_LOG_DEBUG, elapsed_time)
{
GRN_DEFINE_NAME(ii);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][buffer][merge][slow][%f] "
"<%.*s>: "
"physical-segment:<%u>, "
"logical-segment:<%u>",
elapsed_time,
name_size,
name,
grn_ii_get_buffer_pseg_inline(ii, seg),
seg);
}
GRN_SLOW_LOG_POP_END(ctx);
return rc;
}
static void
fake_map(grn_ctx *ctx,
grn_io *io,
grn_io_win *iw,
void *addr,
uint32_t seg,
uint32_t size)
{
iw->ctx = ctx;
iw->diff = 0;
iw->io = io;
iw->mode = GRN_IO_WRONLY;
iw->segment = ((seg) >> GRN_II_N_CHUNK_VARIATION);
iw->offset =
(((seg) & ((1 << GRN_II_N_CHUNK_VARIATION) - 1)) << GRN_II_W_LEAST_CHUNK);
iw->size = size;
iw->cached = 0;
iw->addr = addr;
}
static inline grn_rc
buffer_flush_internal(grn_ctx *ctx, grn_ii *ii, uint32_t lseg, grn_hash *h)
{
grn_io_win sw, dw;
buffer *sb;
uint32_t ds, pseg, scn, dcn = 0;
if (grn_ii_get_buffer_pseg_inline(ii, lseg) == GRN_II_PSEG_NOT_ASSIGNED) {
GRN_DEFINE_NAME(ii);
CRIT(GRN_FILE_CORRUPT,
"[ii][buffer][flush] invalid segment: "
"<%.*s>: "
"request:<%u>, max:<%u>",
name_size,
name,
lseg,
ii->seg->header->max_segment);
return ctx->rc;
}
ds = segment_get(ctx, ii);
if (ds == ii->seg->header->max_segment) {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][flush] segment is full: "
"<%.*s>: "
"request:<%u>, max:<%u>",
name_size,
name,
lseg,
ii->seg->header->max_segment);
return ctx->rc;
}
if (ds == GRN_II_PSEG_NOT_ASSIGNED) {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][flush] couldn't assign a segment: "
"<%.*s>: "
"request:<%u>, max:<%u>",
name_size,
name,
lseg,
ii->seg->header->max_segment);
return ctx->rc;
}
pseg = buffer_open(ctx, ii, grn_ii_pos_pack(ii, lseg, 0), NULL, &sb);
if (pseg == GRN_II_PSEG_NOT_ASSIGNED) {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][flush] failed to open buffer: "
"<%.*s>: "
"segment:<%u>, position:<%u>, max:<%u>",
name_size,
name,
lseg,
grn_ii_pos_pack(ii, lseg, 0),
ii->seg->header->max_segment);
return ctx->rc;
}
{
auto db = static_cast<buffer *>(grn_io_seg_ref(ctx, ii->seg, ds));
if (db) {
const uint8_t *sc = NULL;
if ((scn = sb->header.chunk) == GRN_II_PSEG_NOT_ASSIGNED ||
(sc = static_cast<const uint8_t *>(WIN_MAP(ctx,
ii->chunk,
&sw,
scn,
0,
sb->header.chunk_size,
GRN_IO_RDONLY)))) {
uint16_t n = sb->header.nterms;
memset(db, 0, S_SEGMENT);
grn_memcpy(db->terms, sb->terms, n * sizeof(buffer_term));
db->header.nterms = n;
uint8_t *dc = NULL;
grn_merging_data merging_data;
grn_merging_data_init(ctx, &merging_data, ii);
buffer_merge(ctx, ii, lseg, h, sb, sc, db, &dc, &merging_data);
if (ctx->rc == GRN_SUCCESS) {
const uint32_t actual_chunk_size = db->header.chunk_size;
bool need_chunk_free = true;
if (actual_chunk_size > 0) {
chunk_new(ctx, ii, &dcn, actual_chunk_size);
if (ctx->rc == GRN_SUCCESS) {
need_chunk_free = true;
}
}
if (ctx->rc == GRN_SUCCESS) {
grn_rc rc;
db->header.chunk =
(actual_chunk_size > 0) ? dcn : GRN_II_PSEG_NOT_ASSIGNED;
fake_map(ctx, ii->chunk, &dw, dc, dcn, actual_chunk_size);
rc = grn_io_win_unmap(ctx, &dw);
if (rc == GRN_SUCCESS) {
dc = NULL;
buffer_segment_update(ii, lseg, ds);
ii->header.common->total_chunk_size += actual_chunk_size;
need_chunk_free = false;
merging_data.succeeded = true;
if (scn != GRN_II_PSEG_NOT_ASSIGNED) {
chunk_free(ctx, ii, scn, sb->header.chunk_size);
ii->header.common->total_chunk_size -= sb->header.chunk_size;
}
} else {
GRN_DEFINE_NAME(ii);
ERR(rc,
"[ii][buffer][flush] failed to unmap a destination chunk: "
"<%.*s> : "
"segment:<%u>, destination-segment:<%u>, actual-size:<%u>",
name_size,
name,
lseg,
dcn,
actual_chunk_size);
}
}
if (need_chunk_free) {
chunk_free(ctx, ii, dcn, actual_chunk_size);
}
if (dc) {
GRN_FREE(dc);
}
}
grn_merging_data_fin(ctx, &merging_data);
if (scn != GRN_II_PSEG_NOT_ASSIGNED) {
grn_io_win_unmap(ctx, &sw);
}
} else {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][flush] failed to map a source chunk: "
"<%.*s>: "
"segment:<%u>, source-segment:<%u>, chunk-size:<%u>",
name_size,
name,
lseg,
scn,
sb->header.chunk_size);
}
grn_io_seg_unref(ctx, ii->seg, ds);
} else {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][flush] failed to allocate a destination segment: "
"<%.*s>: "
"segment:<%u>, destination-segment:<%u>",
name_size,
name,
lseg,
ds);
}
buffer_close(ctx, ii, pseg);
}
return ctx->rc;
}
static grn_rc
buffer_flush(grn_ctx *ctx, grn_ii *ii, uint32_t lseg, grn_hash *h)
{
GRN_SLOW_LOG_PUSH(ctx, GRN_LOG_DEBUG);
grn_rc rc = buffer_flush_internal(ctx, ii, lseg, h);
GRN_SLOW_LOG_POP_BEGIN(ctx, GRN_LOG_DEBUG, elapsed_time)
{
GRN_DEFINE_NAME(ii);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][buffer][flush][slow][%f] "
"<%.*s>: "
"physical-segment:<%u>, "
"logical-segment:<%u>",
elapsed_time,
name_size,
name,
grn_ii_get_buffer_pseg_inline(ii, lseg),
lseg);
}
GRN_SLOW_LOG_POP_END(ctx);
return rc;
}
extern "C" void
grn_ii_buffer_check(grn_ctx *ctx, grn_ii *ii, uint32_t lseg)
{
grn_io_win sw;
buffer *sb;
uint8_t *sc = NULL;
uint32_t pseg, scn, nterms_with_corrupt_chunk = 0, nterm_with_chunk = 0;
uint32_t ndeleted_terms_with_value = 0;
buffer_term *bt;
datavec rdv[MAX_N_ELEMENTS + 1];
uint16_t n;
int nterms_void = 0;
int size_in_buffer = 0;
grn_obj buf;
size_t lower_bound;
int64_t nloops = 0, nviolations = 0;
if (grn_ii_get_buffer_pseg_inline(ii, lseg) == GRN_II_PSEG_NOT_ASSIGNED) {
GRN_OUTPUT_BOOL(false);
return;
}
pseg = buffer_open(ctx, ii, grn_ii_pos_pack(ii, lseg, 0), NULL, &sb);
if (pseg == GRN_II_PSEG_NOT_ASSIGNED) {
GRN_OUTPUT_BOOL(false);
return;
}
lower_bound =
(sb->header.buffer_free + sizeof(buffer_term) * sb->header.nterms) /
sizeof(buffer_rec);
datavec_init(ctx, ii, rdv, 0, 0);
GRN_OUTPUT_MAP_OPEN("BUFFER", -1);
GRN_OUTPUT_CSTR("buffer id");
GRN_OUTPUT_INT64(lseg);
if ((scn = sb->header.chunk) == GRN_II_PSEG_NOT_ASSIGNED) {
GRN_OUTPUT_CSTR("void chunk size");
GRN_OUTPUT_INT64(sb->header.chunk_size);
} else {
if ((sc = static_cast<uint8_t *>(WIN_MAP(ctx,
ii->chunk,
&sw,
scn,
0,
sb->header.chunk_size,
GRN_IO_RDONLY)))) {
GRN_OUTPUT_CSTR("chunk size");
GRN_OUTPUT_INT64(sb->header.chunk_size);
} else {
GRN_OUTPUT_CSTR("unmappable chunk size");
GRN_OUTPUT_INT64(sb->header.chunk_size);
}
}
GRN_OUTPUT_CSTR("buffer term");
GRN_OUTPUT_ARRAY_OPEN("TERMS", sb->header.nterms);
GRN_OBJ_INIT(&buf, GRN_BULK, 0, ii->lexicon->header.domain);
for (bt = sb->terms, n = sb->header.nterms; n; n--, bt++) {
grn_id tid, tid_;
char key[GRN_TABLE_MAX_KEY_SIZE];
int key_size;
uint32_t nchunks = 0;
chunk_info *cinfo = NULL;
grn_id crid = GRN_ID_NIL;
merger_data data = {};
merger_chunk_data *chunk_data = &(data.source.chunk);
if (!bt->tid && !bt->pos_in_buffer && !bt->size_in_buffer) {
nterms_void++;
continue;
}
GRN_OUTPUT_ARRAY_OPEN("TERM", -1);
tid = CHUNK_SPLIT_OFF(bt->tid);
key_size =
grn_table_get_key(ctx, ii->lexicon, tid, key, GRN_TABLE_MAX_KEY_SIZE);
tid_ = grn_table_get(ctx, ii->lexicon, key, key_size);
GRN_TEXT_SET(ctx, &buf, key, key_size);
GRN_OUTPUT_OBJ(&buf, NULL);
GRN_OUTPUT_INT64(bt->tid);
GRN_OUTPUT_INT64(tid_);
size_in_buffer += bt->size_in_buffer;
if (tid != tid_ && (bt->size_in_buffer || bt->size_in_chunk)) {
ndeleted_terms_with_value++;
}
GRN_OUTPUT_INT64(bt->size_in_buffer);
GRN_OUTPUT_INT64(bt->size_in_chunk);
data.ii = ii;
data.term_id = bt->tid;
data.source.buffer.buf = sb;
data.source.buffer.next_position = bt->pos_in_buffer;
chunk_data->data_start = sc;
merger_get_next_buffer(ctx, &data);
if (sc && bt->size_in_chunk) {
size_t size = S_SEGMENT * ii->n_elements;
chunk_data->data = chunk_data->data_start + bt->pos_in_chunk;
chunk_data->data_end = chunk_data->data + bt->size_in_chunk;
if ((bt->tid & CHUNK_SPLIT)) {
uint32_t i;
GRN_B_DEC(nchunks, chunk_data->data);
if (!(cinfo = GRN_MALLOCN(chunk_info, nchunks + 1))) {
datavec_fin(ctx, rdv);
GRN_OBJ_FIN(ctx, &buf);
return;
}
for (i = 0; i < nchunks; i++) {
GRN_B_DEC(cinfo[i].segno, chunk_data->data);
GRN_B_DEC(cinfo[i].size, chunk_data->data);
GRN_B_DEC(cinfo[i].dgap, chunk_data->data);
crid += cinfo[i].dgap;
}
}
if (chunk_data->data_end > chunk_data->data) {
size += grn_decv(ctx,
ii,
crid,
chunk_data->data,
chunk_data->data_end - chunk_data->data,
rdv);
merger_init_chunk_data(ctx, &data, rdv);
GRN_OUTPUT_INT64(chunk_data->n_documents);
GRN_OUTPUT_INT64(chunk_data->position_gaps_end -
chunk_data->position_gaps);
{
int j = 0;
j++;
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
j++;
}
if (chunk_data->n_documents != rdv[j].data_size) {
nterms_with_corrupt_chunk++;
}
j++;
if ((ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
j++;
}
}
nterm_with_chunk++;
}
}
{
uint16_t pos;
grn_id rid, sid, rid_ = 0, sid_ = 0;
uint8_t *p;
buffer_rec *r;
for (pos = bt->pos_in_buffer; pos; pos = r->step) {
if (pos < lower_bound) {
nviolations++;
}
r = BUFFER_REC_AT(sb, pos);
p = GRN_NEXT_ADDR(r);
GRN_B_DEC(rid, p);
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_DEC(sid, p);
} else {
sid = 1;
}
if (rid < rid_ || (rid == rid_ && sid < sid_)) {
nloops++;
}
rid_ = rid;
sid_ = sid;
}
}
GRN_OUTPUT_ARRAY_CLOSE();
if (cinfo) {
GRN_FREE(cinfo);
}
}
GRN_OBJ_FIN(ctx, &buf);
GRN_OUTPUT_ARRAY_CLOSE();
GRN_OUTPUT_CSTR("buffer free");
GRN_OUTPUT_INT64(sb->header.buffer_free);
GRN_OUTPUT_CSTR("size in buffer");
GRN_OUTPUT_INT64(size_in_buffer);
GRN_OUTPUT_CSTR("nterms");
GRN_OUTPUT_INT64(sb->header.nterms);
if (nterms_void != sb->header.nterms_void) {
GRN_OUTPUT_CSTR("nterms void gap");
GRN_OUTPUT_INT64(nterms_void - sb->header.nterms_void);
}
GRN_OUTPUT_CSTR("nterms with chunk");
GRN_OUTPUT_INT64(nterm_with_chunk);
if (nterms_with_corrupt_chunk) {
GRN_OUTPUT_CSTR("nterms with corrupt chunk");
GRN_OUTPUT_INT64(nterms_with_corrupt_chunk);
}
if (ndeleted_terms_with_value) {
GRN_OUTPUT_CSTR("number of deleted terms with value");
GRN_OUTPUT_INT64(ndeleted_terms_with_value);
}
if (nloops) {
GRN_OUTPUT_CSTR("number of loops");
GRN_OUTPUT_INT64(nloops);
}
if (nviolations) {
GRN_OUTPUT_CSTR("number of violations");
GRN_OUTPUT_INT64(nviolations);
}
GRN_OUTPUT_MAP_CLOSE();
datavec_fin(ctx, rdv);
if (sc) {
grn_io_win_unmap(ctx, &sw);
}
buffer_close(ctx, ii, pseg);
}
typedef struct {
buffer_term *bt;
const char *key;
uint32_t key_size;
} term_sort;
static int
term_compar(const void *t1, const void *t2)
{
int r;
const term_sort *x = (term_sort *)t1, *y = (term_sort *)t2;
if (x->key_size > y->key_size) {
r = memcmp(x->key, y->key, y->key_size);
return r ? r : (int)(x->key_size - y->key_size);
} else {
r = memcmp(x->key, y->key, x->key_size);
return r ? r : (int)(x->key_size - y->key_size);
}
}
static grn_rc
term_split(grn_ctx *ctx, grn_obj *lexicon, buffer *sb, buffer *db0, buffer *db1)
{
uint16_t i, n, *nt;
buffer_term *bt;
uint32_t s, th = (sb->header.chunk_size + sb->header.nterms) >> 1;
auto ts =
static_cast<term_sort *>(GRN_MALLOC(sb->header.nterms * sizeof(term_sort)));
if (!ts) {
return GRN_NO_MEMORY_AVAILABLE;
}
for (i = 0, n = sb->header.nterms, bt = sb->terms; n; bt++, n--) {
if (bt->tid) {
grn_id tid = CHUNK_SPLIT_OFF(bt->tid);
ts[i].key = _grn_table_key(ctx, lexicon, tid, &ts[i].key_size);
ts[i].bt = bt;
i++;
}
}
qsort(ts, i, sizeof(term_sort), term_compar);
memset(db0, 0, S_SEGMENT);
bt = db0->terms;
nt = &db0->header.nterms;
for (s = 0; n + 1 < i && s <= th; n++, bt++) {
grn_memcpy(bt, ts[n].bt, sizeof(buffer_term));
(*nt)++;
s += ts[n].bt->size_in_chunk + 1;
}
memset(db1, 0, S_SEGMENT);
bt = db1->terms;
nt = &db1->header.nterms;
for (; n < i; n++, bt++) {
grn_memcpy(bt, ts[n].bt, sizeof(buffer_term));
(*nt)++;
}
GRN_FREE(ts);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"d0=%d d1=%d",
db0->header.nterms,
db1->header.nterms);
return GRN_SUCCESS;
}
static void
array_update(grn_ctx *ctx, grn_ii *ii, uint32_t dls, buffer *db)
{
uint16_t n;
buffer_term *bt;
uint32_t loffset = POS_LOFFSET_HEADER;
uint32_t *a;
for (n = db->header.nterms, bt = db->terms; n; n--, bt++) {
if (bt->tid) {
grn_id tid = CHUNK_SPLIT_OFF(bt->tid);
if ((a = array_at(ctx, ii, tid))) {
a[0] = grn_ii_pos_pack(ii, dls, loffset);
array_unref(ctx, ii, tid);
} else {
GRN_LOG(ctx, GRN_LOG_WARNING, "array_at failed (%d)", tid);
}
}
loffset += POS_LOFFSET_HEADER;
}
}
static inline grn_rc
buffer_split_internal(grn_ctx *ctx, grn_ii *ii, uint32_t lseg, grn_hash *h)
{
grn_io_win sw, dw0, dw1;
buffer *sb, *db0 = NULL, *db1 = NULL;
uint32_t dps0 = 0, dps1 = 0, dls0 = 0, dls1 = 0, sps, scn, dcn0 = 0, dcn1 = 0;
if (grn_ii_get_buffer_pseg_inline(ii, lseg) == GRN_II_PSEG_NOT_ASSIGNED) {
GRN_DEFINE_NAME(ii);
CRIT(GRN_FILE_CORRUPT,
"[ii][buffer][split] invalid segment: "
"<%.*s> :"
"request:<%u>, max:<%u>",
name_size,
name,
lseg,
ii->seg->header->max_segment);
return ctx->rc;
}
buffer_segment_reserve(ctx, ii, &dls0, &dps0, &dls1, &dps1);
if (ctx->rc != GRN_SUCCESS) {
GRN_DEFINE_NAME(ii);
ERR(ctx->rc,
"[ii][buffer][split] failed to reserve buffer segments: "
"<%.*s> :"
"request:<%u>, max:<%u>",
name_size,
name,
lseg,
ii->seg->header->max_segment);
return ctx->rc;
}
sps = buffer_open(ctx, ii, grn_ii_pos_pack(ii, lseg, 0), NULL, &sb);
if (sps == GRN_II_PSEG_NOT_ASSIGNED) {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][split] failed to open buffer: "
"<%.*s> :"
"segment:<%u>, position:<%u>, max-segment:<%u>",
name_size,
name,
lseg,
grn_ii_pos_pack(ii, lseg, 0),
ii->seg->header->max_segment);
} else {
db0 = static_cast<buffer *>(grn_io_seg_ref(ctx, ii->seg, dps0));
if (db0) {
db1 = static_cast<buffer *>(grn_io_seg_ref(ctx, ii->seg, dps1));
if (db1) {
const uint8_t *sc = NULL;
if ((scn = sb->header.chunk) == GRN_II_PSEG_NOT_ASSIGNED ||
(sc = static_cast<const uint8_t *>(WIN_MAP(ctx,
ii->chunk,
&sw,
scn,
0,
sb->header.chunk_size,
GRN_IO_RDONLY)))) {
term_split(ctx, ii->lexicon, sb, db0, db1);
uint8_t *dc0 = NULL;
grn_merging_data merging_data;
grn_merging_data_init(ctx, &merging_data, ii);
buffer_merge(ctx, ii, lseg, h, sb, sc, db0, &dc0, &merging_data);
if (ctx->rc == GRN_SUCCESS) {
const uint32_t actual_db0_chunk_size = db0->header.chunk_size;
bool need_db0_chunk_free = false;
if (actual_db0_chunk_size > 0) {
chunk_new(ctx, ii, &dcn0, actual_db0_chunk_size);
if (ctx->rc == GRN_SUCCESS) {
need_db0_chunk_free = true;
}
}
if (ctx->rc == GRN_SUCCESS) {
grn_rc rc;
db0->header.chunk =
actual_db0_chunk_size ? dcn0 : GRN_II_PSEG_NOT_ASSIGNED;
fake_map(ctx, ii->chunk, &dw0, dc0, dcn0, actual_db0_chunk_size);
rc = grn_io_win_unmap(ctx, &dw0);
if (rc == GRN_SUCCESS) {
dc0 = NULL;
uint8_t *dc1 = NULL;
buffer_merge(ctx,
ii,
lseg,
h,
sb,
sc,
db1,
&dc1,
&merging_data);
if (ctx->rc == GRN_SUCCESS) {
const uint32_t actual_db1_chunk_size = db1->header.chunk_size;
bool need_db1_chunk_free = false;
if (actual_db1_chunk_size > 0) {
chunk_new(ctx, ii, &dcn1, actual_db1_chunk_size);
if (ctx->rc == GRN_SUCCESS) {
need_db1_chunk_free = true;
}
}
if (ctx->rc == GRN_SUCCESS) {
fake_map(ctx,
ii->chunk,
&dw1,
dc1,
dcn1,
actual_db1_chunk_size);
rc = grn_io_win_unmap(ctx, &dw1);
if (rc == GRN_SUCCESS) {
dc1 = NULL;
db1->header.chunk = (actual_db1_chunk_size > 0)
? dcn1
: GRN_II_PSEG_NOT_ASSIGNED;
buffer_segment_update(ii, dls0, dps0);
buffer_segment_update(ii, dls1, dps1);
array_update(ctx, ii, dls0, db0);
array_update(ctx, ii, dls1, db1);
buffer_segment_clear(ii, lseg);
ii->header.common->total_chunk_size +=
actual_db0_chunk_size + actual_db1_chunk_size;
need_db0_chunk_free = false;
need_db1_chunk_free = false;
merging_data.succeeded = true;
if (scn != GRN_II_PSEG_NOT_ASSIGNED) {
chunk_free(ctx, ii, scn, sb->header.chunk_size);
ii->header.common->total_chunk_size -=
sb->header.chunk_size;
}
} else {
GRN_DEFINE_NAME(ii);
ERR(rc,
"[ii][buffer][merge] "
"failed to unmap a destination chunk2: "
"<%.*s>: "
"segment:<%u>, "
"destination-chunk1:<%u>, "
"destination-chunk2:<%u>, "
"actual-size1:<%u>, "
"actual-size2:<%u>",
name_size,
name,
lseg,
dcn0,
dcn1,
actual_db0_chunk_size,
actual_db1_chunk_size);
}
}
if (need_db1_chunk_free) {
chunk_free(ctx, ii, dcn1, actual_db1_chunk_size);
}
if (dc1) {
GRN_FREE(dc1);
}
}
} else {
GRN_DEFINE_NAME(ii);
ERR(rc,
"[ii][buffer][merge] "
"failed to unmap a destination chunk1: "
"<%.*s> :"
"segment:<%u>, "
"destination-chunk1:<%u>, "
"actual-size1:<%u>",
name_size,
name,
lseg,
dcn0,
actual_db0_chunk_size);
}
}
if (need_db0_chunk_free) {
chunk_free(ctx, ii, dcn0, actual_db0_chunk_size);
}
if (dc0) {
GRN_FREE(dc0);
}
}
grn_merging_data_fin(ctx, &merging_data);
if (scn != GRN_II_PSEG_NOT_ASSIGNED) {
grn_io_win_unmap(ctx, &sw);
}
} else {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][split] failed to map a source chunk: "
"<%.*s> :"
"segment:<%u>, "
"source-segment:<%u>, "
"chunk-size:<%u>",
name_size,
name,
lseg,
scn,
sb->header.chunk_size);
}
grn_io_seg_unref(ctx, ii->seg, dps1);
} else {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][split] failed to allocate a destination segment2: "
"<%.*s>: "
"segment:<%u>, "
"destination-segment1:<%u>, "
"destination-segment2:<%u>",
name_size,
name,
lseg,
dps0,
dps1);
}
grn_io_seg_unref(ctx, ii->seg, dps0);
} else {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][split] failed to allocate a destination segment1: "
"<%.*s>: "
"segment:<%u>, "
"destination-segment1:<%u>, "
"destination-segment2:<%u>",
name_size,
name,
lseg,
dps0,
dps1);
}
buffer_close(ctx, ii, sps);
}
return ctx->rc;
}
static grn_rc
buffer_split(grn_ctx *ctx, grn_ii *ii, uint32_t lseg, grn_hash *h)
{
GRN_SLOW_LOG_PUSH(ctx, GRN_LOG_DEBUG);
grn_rc rc = buffer_split_internal(ctx, ii, lseg, h);
GRN_SLOW_LOG_POP_BEGIN(ctx, GRN_LOG_DEBUG, elapsed_time)
{
GRN_DEFINE_NAME(ii);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][buffer][split][slow][%f] "
"<%.*s>: "
"physical-segment:<%u>, "
"logical-segment:<%u>",
elapsed_time,
name_size,
name,
grn_ii_get_buffer_pseg_inline(ii, lseg),
lseg);
}
GRN_SLOW_LOG_POP_END(ctx);
return rc;
}
#define SCALE_FACTOR 2048
#define MAX_NTERMS 8192
#define SPLIT_COND(ii, buffer) \
((buffer)->header.nterms > 1024 || \
((buffer)->header.nterms > 1 && (buffer)->header.chunk_size * 100 > \
(ii)->header.common->total_chunk_size))
static inline void
buffer_new_find_segment(grn_ctx *ctx,
grn_ii *ii,
int size,
grn_id tid,
grn_hash *h,
buffer **b,
uint32_t *lseg,
uint32_t *pseg)
{
uint32_t *a;
a = array_at(ctx, ii, tid);
if (!a) {
return;
}
for (;;) {
uint32_t pos = a[0];
if (!pos || POS_IS_EMBED(pos)) {
break;
}
*pseg = buffer_open(ctx, ii, pos, NULL, b);
if (*pseg == GRN_II_PSEG_NOT_ASSIGNED) {
break;
}
if ((*b)->header.buffer_free >= size + sizeof(buffer_term)) {
*lseg = grn_ii_pos_lseg(ii, pos);
break;
}
buffer_close(ctx, ii, *pseg);
if (SPLIT_COND(ii, (*b))) {
/* ((S_SEGMENT - sizeof(buffer_header) + ii->header.common->bmax -
(*b)->header.nterms * sizeof(buffer_term)) * 4 <
(*b)->header.chunk_size) */
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"nterms=%d chunk=%d total=%" GRN_FMT_INT64U,
(*b)->header.nterms,
(*b)->header.chunk_size,
ii->header.common->total_chunk_size >> 10);
if (buffer_split(ctx, ii, grn_ii_pos_lseg(ii, pos), h)) {
break;
}
} else {
if (S_SEGMENT - sizeof(buffer_header) -
(*b)->header.nterms * sizeof(buffer_term) <
size + sizeof(buffer_term)) {
break;
}
if (buffer_flush(ctx, ii, grn_ii_pos_lseg(ii, pos), h)) {
break;
}
}
}
array_unref(ctx, ii, tid);
}
static inline void
buffer_new_lexicon_pat(grn_ctx *ctx,
grn_ii *ii,
int size,
grn_id id,
grn_hash *h,
buffer **b,
uint32_t *lseg,
uint32_t *pseg)
{
grn_pat_cursor *cursor;
char key[GRN_TABLE_MAX_KEY_SIZE];
int key_size;
key_size =
grn_table_get_key(ctx, ii->lexicon, id, key, GRN_TABLE_MAX_KEY_SIZE);
if (ii->lexicon->header.flags & GRN_OBJ_KEY_VAR_SIZE) {
grn_obj *tokenizer = NULL;
grn_table_get_info(ctx, ii->lexicon, NULL, NULL, &tokenizer, NULL, NULL);
if (tokenizer) {
/* For natural language */
cursor = grn_pat_cursor_open(ctx,
(grn_pat *)(ii->lexicon),
key,
key_size,
NULL,
0,
0,
-1,
GRN_CURSOR_ASCENDING | GRN_CURSOR_GT);
if (cursor) {
grn_id tid;
while (ctx->rc == GRN_SUCCESS && *lseg == GRN_II_PSEG_NOT_ASSIGNED &&
(tid = grn_pat_cursor_next(ctx, cursor))) {
buffer_new_find_segment(ctx, ii, size, tid, h, b, lseg, pseg);
}
grn_pat_cursor_close(ctx, cursor);
}
} else {
/* For binary data or text data */
int target_key_size = key_size;
int reduced_key_size = 0;
bool is_binary_data = (ctx->encoding == GRN_ENC_NONE);
while (*lseg == GRN_II_PSEG_NOT_ASSIGNED && target_key_size > 0) {
grn_id tid;
cursor = grn_pat_cursor_open(ctx,
(grn_pat *)(ii->lexicon),
key,
target_key_size,
NULL,
0,
0,
-1,
GRN_CURSOR_PREFIX);
if (!cursor) {
break;
}
if (reduced_key_size == 0) {
while (ctx->rc == GRN_SUCCESS && *lseg == GRN_II_PSEG_NOT_ASSIGNED &&
(tid = grn_pat_cursor_next(ctx, cursor))) {
buffer_new_find_segment(ctx, ii, size, tid, h, b, lseg, pseg);
}
} else {
while (ctx->rc == GRN_SUCCESS && *lseg == GRN_II_PSEG_NOT_ASSIGNED &&
(tid = grn_pat_cursor_next(ctx, cursor))) {
void *current_key;
int current_key_size;
current_key_size =
grn_pat_cursor_get_key(ctx, cursor, ¤t_key);
if (memcmp(((char *)current_key) + target_key_size,
key + target_key_size,
reduced_key_size) == 0) {
continue;
}
buffer_new_find_segment(ctx, ii, size, tid, h, b, lseg, pseg);
}
}
grn_pat_cursor_close(ctx, cursor);
if (reduced_key_size == 0) {
reduced_key_size = 1;
} else {
if (is_binary_data) {
reduced_key_size *= 2;
}
}
target_key_size -= reduced_key_size;
}
}
} else {
/* For other data */
cursor = grn_pat_cursor_open(ctx,
(grn_pat *)(ii->lexicon),
NULL,
0,
key,
key_size,
0,
-1,
GRN_CURSOR_PREFIX);
if (cursor) {
grn_id tid;
while (ctx->rc == GRN_SUCCESS && *lseg == GRN_II_PSEG_NOT_ASSIGNED &&
(tid = grn_pat_cursor_next(ctx, cursor))) {
buffer_new_find_segment(ctx, ii, size, tid, h, b, lseg, pseg);
}
grn_pat_cursor_close(ctx, cursor);
}
}
}
static inline void
buffer_new_lexicon_other(grn_ctx *ctx,
grn_ii *ii,
int size,
grn_id id,
grn_hash *h,
buffer **b,
uint32_t *lseg,
uint32_t *pseg)
{
GRN_TABLE_EACH_BEGIN(ctx, ii->lexicon, cursor, tid)
{
if (ctx->rc != GRN_SUCCESS || *lseg != GRN_II_PSEG_NOT_ASSIGNED) {
break;
}
buffer_new_find_segment(ctx, ii, size, tid, h, b, lseg, pseg);
}
GRN_TABLE_EACH_END(ctx, cursor);
}
static inline uint32_t
buffer_new_internal(grn_ctx *ctx,
grn_ii *ii,
int size,
uint32_t *pos,
buffer_term **bt,
buffer_rec **br,
buffer **bp,
grn_id id,
grn_hash *h)
{
buffer *b = NULL;
uint16_t offset;
uint32_t lseg = GRN_II_PSEG_NOT_ASSIGNED, pseg = GRN_II_PSEG_NOT_ASSIGNED;
if (S_SEGMENT - sizeof(buffer_header) < size + sizeof(buffer_term)) {
GRN_DEFINE_NAME(ii);
MERR("[ii][buffer][new] requested size is too large: "
"<%.*s> :"
"requested:<%" GRN_FMT_SIZE ">, max:<%" GRN_FMT_SIZE ">",
name_size,
name,
(size_t)(size + sizeof(buffer_term)),
(size_t)(S_SEGMENT - sizeof(buffer_header)));
return GRN_II_PSEG_NOT_ASSIGNED;
}
if (ii->lexicon->header.type == GRN_TABLE_PAT_KEY) {
buffer_new_lexicon_pat(ctx, ii, size, id, h, &b, &lseg, &pseg);
} else {
buffer_new_lexicon_other(ctx, ii, size, id, h, &b, &lseg, &pseg);
}
if (lseg == GRN_II_PSEG_NOT_ASSIGNED) {
if (buffer_segment_new(ctx, ii, &lseg) ||
(pseg = buffer_open(ctx, ii, grn_ii_pos_pack(ii, lseg, 0), NULL, &b)) ==
GRN_II_PSEG_NOT_ASSIGNED) {
return GRN_II_PSEG_NOT_ASSIGNED;
}
memset(b, 0, S_SEGMENT);
b->header.buffer_free = S_SEGMENT - sizeof(buffer_header);
b->header.chunk = GRN_II_PSEG_NOT_ASSIGNED;
}
if (b->header.nterms_void) {
for (offset = 0; offset < b->header.nterms; offset++) {
if (!b->terms[offset].tid) {
break;
}
}
if (offset == b->header.nterms) {
GRN_LOG(ctx, GRN_LOG_DEBUG, "inconsistent buffer(%d)", lseg);
b->header.nterms_void = 0;
b->header.nterms++;
b->header.buffer_free -= size + sizeof(buffer_term);
} else {
b->header.nterms_void--;
b->header.buffer_free -= size;
}
} else {
offset = b->header.nterms++;
b->header.buffer_free -= size + sizeof(buffer_term);
}
*pos =
grn_ii_pos_pack(ii, lseg, POS_LOFFSET_HEADER + POS_LOFFSET_TERM * offset);
*bt = &b->terms[offset];
*br = (buffer_rec *)(((byte *)&b->terms[b->header.nterms]) +
b->header.buffer_free);
*bp = b;
return pseg;
}
static inline uint32_t
buffer_new(grn_ctx *ctx,
grn_ii *ii,
int size,
uint32_t *pos,
buffer_term **bt,
buffer_rec **br,
buffer **bp,
grn_id id,
grn_hash *h)
{
GRN_SLOW_LOG_PUSH(ctx, GRN_LOG_DEBUG);
uint32_t pseg = buffer_new_internal(ctx, ii, size, pos, bt, br, bp, id, h);
GRN_SLOW_LOG_POP_BEGIN(ctx, GRN_LOG_DEBUG, elapsed_time)
{
GRN_DEFINE_NAME(ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, id, &term);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][buffer][new][slow][%f] "
"<%.*s>: "
"<%.*s>(%u): "
"size:<%u>, "
"physical-segment:<%u>",
elapsed_time,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
id,
size,
pseg);
GRN_OBJ_FIN(ctx, &term);
}
GRN_SLOW_LOG_POP_END(ctx);
return pseg;
}
/* ii */
static grn_ii *
_grn_ii_create(
grn_ctx *ctx, grn_ii *ii, const char *path, grn_obj *lexicon, uint32_t flags)
{
int i;
uint32_t header_size;
uint32_t max_n_segments;
uint32_t max_n_chunks;
grn_io *seg, *chunk;
char path2[PATH_MAX];
grn_ii_header_common *header;
grn_table_flags lflags;
grn_encoding encoding;
grn_obj *tokenizer;
/*
for (i = 0; i < 32; i++) {
new_histogram[i] = 0;
free_histogram[i] = 0;
}
*/
if (grn_table_get_info(ctx,
lexicon,
&lflags,
&encoding,
&tokenizer,
NULL,
NULL)) {
return NULL;
}
if (path && strlen(path) + 6 >= PATH_MAX) {
return NULL;
}
if (!grn_ii_pos_is_available(ctx, flags, "[ii][create]")) {
return NULL;
}
if (flags & GRN_OBJ_INDEX_SMALL) {
header_size = sizeof(grn_ii_header_normal);
max_n_segments = grn_ii_max_n_segments_small;
max_n_chunks = grn_ii_max_n_chunks_small;
} else if (flags & GRN_OBJ_INDEX_MEDIUM) {
header_size = sizeof(grn_ii_header_normal);
max_n_segments = MAX_PSEG_MEDIUM;
max_n_chunks = GRN_II_MAX_CHUNK_MEDIUM;
} else if (flags & GRN_OBJ_INDEX_LARGE) {
header_size = sizeof(grn_ii_header_large);
max_n_segments = MAX_PSEG_LARGE;
max_n_chunks = GRN_II_MAX_CHUNK;
} else {
header_size = sizeof(grn_ii_header_normal);
max_n_segments = MAX_PSEG;
max_n_chunks = GRN_II_MAX_CHUNK;
}
seg = grn_io_create(ctx,
path,
header_size,
S_SEGMENT,
max_n_segments,
GRN_IO_AUTO,
GRN_IO_EXPIRE_SEGMENT);
if (!seg) {
return NULL;
}
if (path) {
grn_strcpy(path2, PATH_MAX, path);
grn_strcat(path2, PATH_MAX, ".c");
chunk = grn_io_create(ctx,
path2,
0,
S_CHUNK,
max_n_chunks,
GRN_IO_AUTO,
GRN_IO_EXPIRE_SEGMENT);
} else {
chunk = grn_io_create(ctx, NULL, 0, S_CHUNK, max_n_chunks, GRN_IO_AUTO, 0);
}
if (!chunk) {
grn_io_close(ctx, seg);
grn_io_remove(ctx, path);
return NULL;
}
header = static_cast<grn_ii_header_common *>(grn_io_header(seg));
grn_io_set_type(seg, GRN_COLUMN_INDEX);
for (i = 0; i < GRN_II_MAX_LSEG; i++) {
header->ainfo[i] = GRN_II_PSEG_NOT_ASSIGNED;
header->binfo[i] = GRN_II_PSEG_NOT_ASSIGNED;
}
if (flags & GRN_OBJ_INDEX_LARGE) {
grn_ii_header_large *header_large = (grn_ii_header_large *)header;
for (i = 0; i < GRN_II_MAX_LSEG_EXTEND; i++) {
header_large->ainfo_extend[i] = GRN_II_PSEG_NOT_ASSIGNED;
header_large->binfo_extend[i] = GRN_II_PSEG_NOT_ASSIGNED;
}
}
for (i = 0; i <= GRN_II_N_CHUNK_VARIATION; i++) {
header->free_chunks[i] = GRN_II_PSEG_NOT_ASSIGNED;
header->garbages[i] = GRN_II_PSEG_NOT_ASSIGNED;
}
header->flags = flags;
ii->seg = seg;
ii->chunk = chunk;
ii->lexicon = grn_ctx_at(ctx, DB_OBJ(lexicon)->id);
ii->lflags = lflags;
ii->encoding = encoding;
ii->header.common = header;
ii->n_elements = 2;
if ((flags & GRN_OBJ_WITH_SECTION)) {
ii->n_elements++;
}
if ((flags & GRN_OBJ_WITH_WEIGHT)) {
ii->n_elements++;
}
if ((flags & GRN_OBJ_WITH_POSITION)) {
ii->n_elements++;
}
ii->wal_touched = false;
return ii;
}
extern "C" grn_ii *
grn_ii_create(grn_ctx *ctx, const char *path, grn_obj *lexicon, uint32_t flags)
{
auto ii = static_cast<grn_ii *>(GRN_CALLOC(sizeof(grn_ii)));
if (!ii) {
return NULL;
}
GRN_DB_OBJ_SET_TYPE(ii, GRN_COLUMN_INDEX);
if (!_grn_ii_create(ctx, ii, path, lexicon, flags)) {
GRN_FREE(ii);
return NULL;
}
return ii;
}
extern "C" grn_rc
grn_ii_remove(grn_ctx *ctx, const char *path)
{
grn_rc rc;
char buffer[PATH_MAX];
if (!path || strlen(path) > PATH_MAX - 4) {
return GRN_INVALID_ARGUMENT;
}
grn_rc wal_rc = grn_wal_remove(ctx, path, "[ii]");
if ((rc = grn_io_remove(ctx, path))) {
goto exit;
}
grn_snprintf(buffer, PATH_MAX, PATH_MAX, "%s.c", path);
rc = grn_io_remove(ctx, buffer);
if (rc == GRN_SUCCESS) {
rc = wal_rc;
}
exit:
return rc;
}
extern "C" grn_rc
grn_ii_truncate(grn_ctx *ctx, grn_ii *ii)
{
grn_rc rc;
const char *io_segpath;
char *segpath = NULL;
grn_obj *lexicon;
uint32_t flags;
if ((io_segpath = grn_io_path(ii->seg)) && *io_segpath != '\0') {
if (!(segpath = GRN_STRDUP(io_segpath))) {
ERR(GRN_NO_MEMORY_AVAILABLE, "cannot duplicate path: <%s>", io_segpath);
return GRN_NO_MEMORY_AVAILABLE;
}
} else {
segpath = NULL;
}
lexicon = ii->lexicon;
flags = ii->header.common->flags;
if ((rc = grn_io_close(ctx, ii->seg))) {
goto exit;
}
if ((rc = grn_io_close(ctx, ii->chunk))) {
goto exit;
}
ii->seg = NULL;
ii->chunk = NULL;
if (segpath && (rc = grn_ii_remove(ctx, segpath))) {
goto exit;
}
if (!_grn_ii_create(ctx, ii, segpath, lexicon, flags)) {
rc = GRN_UNKNOWN_ERROR;
goto exit;
}
grn_obj_unref(ctx, lexicon);
exit:
if (segpath) {
GRN_FREE(segpath);
}
return rc;
}
extern "C" grn_ii *
grn_ii_open(grn_ctx *ctx, const char *path, grn_obj *lexicon)
{
grn_io *seg, *chunk;
char path2[PATH_MAX];
grn_ii_header_common *header;
uint32_t io_type;
grn_table_flags lflags;
grn_encoding encoding;
grn_obj *tokenizer;
if (grn_table_get_info(ctx,
lexicon,
&lflags,
&encoding,
&tokenizer,
NULL,
NULL)) {
return NULL;
}
if (strlen(path) + 6 >= PATH_MAX) {
return NULL;
}
grn_strcpy(path2, PATH_MAX, path);
grn_strcat(path2, PATH_MAX, ".c");
seg = grn_io_open(ctx, path, GRN_IO_AUTO);
if (!seg) {
return NULL;
}
chunk = grn_io_open(ctx, path2, GRN_IO_AUTO);
if (!chunk) {
grn_io_close(ctx, seg);
return NULL;
}
header = static_cast<grn_ii_header_common *>(grn_io_header(seg));
io_type = grn_io_get_type(seg);
if (io_type != GRN_COLUMN_INDEX) {
ERR(GRN_INVALID_FORMAT,
"[column][index] file type must be %#04x: <%#04x>",
GRN_COLUMN_INDEX,
io_type);
grn_io_close(ctx, seg);
grn_io_close(ctx, chunk);
return NULL;
}
auto ii = static_cast<grn_ii *>(GRN_CALLOC(sizeof(grn_ii)));
if (!ii) {
grn_io_close(ctx, seg);
grn_io_close(ctx, chunk);
return NULL;
}
GRN_DB_OBJ_SET_TYPE(ii, GRN_COLUMN_INDEX);
ii->seg = seg;
ii->chunk = chunk;
ii->lexicon = grn_ctx_at(ctx, DB_OBJ(lexicon)->id);
ii->lflags = lflags;
ii->encoding = encoding;
ii->header.common = header;
ii->n_elements = 2;
if ((header->flags & GRN_OBJ_WITH_SECTION)) {
ii->n_elements++;
}
if ((header->flags & GRN_OBJ_WITH_WEIGHT)) {
ii->n_elements++;
}
if ((header->flags & GRN_OBJ_WITH_POSITION)) {
ii->n_elements++;
}
ii->wal_touched = false;
return ii;
}
extern "C" grn_rc
grn_ii_close(grn_ctx *ctx, grn_ii *ii)
{
grn_rc rc = GRN_SUCCESS;
if (!ii) {
return GRN_INVALID_ARGUMENT;
}
if (ii->seg->path[0] != '\0' &&
GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_PRIMARY) {
grn_obj_flush(ctx, (grn_obj *)ii);
}
{
grn_rc sub_rc = grn_io_close(ctx, ii->seg);
if (rc == GRN_SUCCESS) {
rc = sub_rc;
}
}
{
grn_rc sub_rc = grn_io_close(ctx, ii->chunk);
if (rc == GRN_SUCCESS) {
rc = sub_rc;
}
}
grn_obj_unref(ctx, ii->lexicon);
GRN_FREE(ii);
/*
{
int i;
for (i = 0; i < 32; i++) {
GRN_LOG(ctx, GRN_LOG_DEBUG, "new[%d]=%d free[%d]=%d",
i, new_histogram[i],
i, free_histogram[i]);
}
}
*/
return rc;
}
extern "C" grn_rc
grn_ii_info(grn_ctx *ctx, grn_ii *ii, uint64_t *seg_size, uint64_t *chunk_size)
{
grn_rc rc;
if (seg_size) {
if ((rc = grn_io_size(ctx, ii->seg, seg_size))) {
return rc;
}
}
if (chunk_size) {
if ((rc = grn_io_size(ctx, ii->chunk, chunk_size))) {
return rc;
}
}
return GRN_SUCCESS;
}
extern "C" grn_column_flags
grn_ii_get_flags(grn_ctx *ctx, grn_ii *ii)
{
if (!ii) {
return 0;
}
return ii->header.common->flags;
}
extern "C" uint32_t
grn_ii_get_n_elements(grn_ctx *ctx, grn_ii *ii)
{
if (!ii) {
return 0;
}
return ii->n_elements;
}
extern "C" grn_obj *
grn_ii_get_lexicon(grn_ctx *ctx, grn_ii *ii)
{
if (!ii) {
return NULL;
}
return ii->lexicon;
}
extern "C" void
grn_ii_expire(grn_ctx *ctx, grn_ii *ii)
{
int64_t threshold = ii->chunk->max_map_seg / 2;
/*
grn_io_expire(ctx, ii->seg, 128, 1000000);
*/
if (0 <= grn_ii_reduce_expire_threshold &&
grn_ii_reduce_expire_threshold < threshold) {
threshold = grn_ii_reduce_expire_threshold;
}
if (ii->chunk->nmaps > threshold) {
grn_io_expire(ctx, ii->chunk, 0, 1000000);
}
}
extern "C" grn_rc
grn_ii_flush(grn_ctx *ctx, grn_ii *ii)
{
grn_rc rc;
rc = grn_io_flush(ctx, ii->seg);
if (rc == GRN_SUCCESS) {
rc = grn_io_flush(ctx, ii->chunk);
}
if (rc == GRN_SUCCESS) {
ii->wal_touched = false;
}
return rc;
}
extern "C" size_t
grn_ii_get_disk_usage(grn_ctx *ctx, grn_ii *ii)
{
size_t usage;
usage = grn_io_get_disk_usage(ctx, ii->seg);
usage += grn_io_get_disk_usage(ctx, ii->chunk);
return usage;
}
extern "C" void
grn_ii_set_visibility(grn_ctx *ctx, grn_ii *ii, bool is_visible)
{
if (is_visible) {
ii->header.common->flags &= ~GRN_OBJ_INVISIBLE;
} else {
ii->header.common->flags |= GRN_OBJ_INVISIBLE;
}
}
#define BIT11_01(x) ((x >> 1) & 0x7ff)
#define BIT31_12(x) (x >> 12)
static inline grn_rc
grn_ii_wal_touch(grn_ctx *ctx, grn_ii *ii, const char *tag)
{
if (ii->wal_touched) {
return GRN_SUCCESS;
}
if (GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_NONE) {
return GRN_SUCCESS;
}
grn_rc rc = grn_wal_touch(ctx, (grn_obj *)ii, false, tag);
if (rc == GRN_SUCCESS) {
ii->wal_touched = true;
}
return rc;
}
static inline grn_rc
grn_ii_update_one_internal(
grn_ctx *ctx, grn_ii *ii, grn_id tid, grn_ii_updspec *u, grn_hash *h)
{
const char *tag = "[ii][update][one]";
buffer *b;
uint8_t *bs;
buffer_rec *br = NULL;
buffer_term *bt;
uint32_t pseg = 0, pos = 0, size, *a;
if (!tid) {
return ctx->rc;
}
if (!u->tf || !u->sid) {
return grn_ii_delete_one(ctx, ii, tid, u, h);
}
if (u->sid > ii->header.common->smax) {
ii->header.common->smax = u->sid;
}
if (!(a = array_get(ctx, ii, tid))) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s failed to allocate an array: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u)",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid);
GRN_OBJ_FIN(ctx, &term);
return ctx->rc;
}
if (!(bs = encode_rec(ctx, ii, u, &size, 0))) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s failed to encode a record: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u)",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
if (grn_ii_wal_touch(ctx, ii, tag) != GRN_SUCCESS) {
goto exit;
}
for (;;) {
if (a[0]) {
if (!POS_IS_EMBED(a[0])) {
pos = a[0];
if ((pseg = buffer_open(ctx, ii, pos, &bt, &b)) ==
GRN_II_PSEG_NOT_ASSIGNED) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s failed to allocate a buffer: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"segment:<%u>, "
"free:<%u>, "
"required:<%u>",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid,
pos,
b->header.buffer_free,
size);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
if (b->header.buffer_free < size) {
int bfb = b->header.buffer_free;
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"flushing a[0]=%d seg=%d(%p) free=%d",
a[0],
grn_ii_pos_lseg(ii, a[0]),
b,
b->header.buffer_free);
buffer_close(ctx, ii, pseg);
if (SPLIT_COND(ii, b)) {
/*((S_SEGMENT - sizeof(buffer_header) + ii->header.common->bmax -
b->header.nterms * sizeof(buffer_term)) * 4 <
b->header.chunk_size)*/
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"nterms=%d chunk=%d total=%" GRN_FMT_INT64U,
b->header.nterms,
b->header.chunk_size,
ii->header.common->total_chunk_size >> 10);
buffer_split(ctx, ii, grn_ii_pos_lseg(ii, pos), h);
if (ctx->rc != GRN_SUCCESS) {
grn_obj term;
char errbuf[GRN_CTX_MSGSIZE];
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
ERR(ctx->rc,
"%s failed to split a buffer: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"segment:<%u>, "
"free:<%u>, "
"required:<%u>, "
"%s",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid,
pos,
b->header.buffer_free,
size,
errbuf);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
continue;
}
buffer_flush(ctx, ii, grn_ii_pos_lseg(ii, pos), h);
if (ctx->rc != GRN_SUCCESS) {
grn_obj term;
char errbuf[GRN_CTX_MSGSIZE];
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
ERR(ctx->rc,
"%s failed to flush a buffer: "
"<%.*s>: "
"<%u>:<%u>:<%u>: "
"term:<%.*s>, "
"segment:<%u>, "
"free:<%u>, "
"required:<%u>: "
"%s",
tag,
name_size,
name,
u->rid,
u->sid,
tid,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
pos,
b->header.buffer_free,
size,
errbuf);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
if (a[0] != pos) {
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"grn_ii_update_one: a[0] changed %d->%d",
a[0],
pos);
continue;
}
if ((pseg = buffer_open(ctx, ii, pos, &bt, &b)) ==
GRN_II_PSEG_NOT_ASSIGNED) {
GRN_LOG(ctx, GRN_LOG_CRIT, "buffer not found a[0]=%d", a[0]);
{
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s failed to reallocate a buffer: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"segment:<%u>, "
"new-segment:<%u>, "
"free:<%u>, "
"required:<%u>",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid,
pos,
a[0],
b->header.buffer_free,
size);
GRN_OBJ_FIN(ctx, &term);
}
goto exit;
}
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"flushed a[0]=%d seg=%d(%p) free=%d->%d nterms=%d v=%d",
a[0],
grn_ii_pos_lseg(ii, a[0]),
b,
bfb,
b->header.buffer_free,
b->header.nterms,
b->header.nterms_void);
if (b->header.buffer_free < size) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s buffer is full: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"segment:<%u>, "
"new-segment:<%u>, "
"free:<%u>, "
"required:<%u>",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid,
pos,
a[0],
b->header.buffer_free,
size);
GRN_OBJ_FIN(ctx, &term);
buffer_close(ctx, ii, pseg);
/* todo: direct merge */
goto exit;
}
}
b->header.buffer_free -= size;
br = (buffer_rec *)(((byte *)&b->terms[b->header.nterms]) +
b->header.buffer_free);
} else {
grn_ii_updspec u2;
uint32_t size2 = 0, v = a[0];
struct _grn_ii_pos pos2;
pos2.pos = a[1];
pos2.next = NULL;
u2.pos = &pos2;
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
u2.rid = POS_RID_SID_EXTRACT_RID(v);
u2.sid = POS_RID_SID_EXTRACT_SID(v);
} else {
u2.rid = POS_RID_EXTRACT(v);
u2.sid = 1;
}
u2.tf = 1;
u2.weight = 0;
if (u2.rid != u->rid || u2.sid != u->sid) {
uint8_t *bs2 = encode_rec(ctx, ii, &u2, &size2, 0);
if (!bs2) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s failed to encode a record2: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u)",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u2.rid,
u2.sid);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
pseg = buffer_new(ctx, ii, size + size2, &pos, &bt, &br, &b, tid, h);
if (pseg == GRN_II_PSEG_NOT_ASSIGNED) {
GRN_FREE(bs2);
{
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s failed to create a buffer2: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"size:<%u>",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u2.rid,
u2.sid,
size + size2);
GRN_OBJ_FIN(ctx, &term);
}
goto exit;
}
bt->tid = tid;
bt->size_in_chunk = 0;
bt->pos_in_chunk = 0;
bt->size_in_buffer = 0;
bt->pos_in_buffer = 0;
buffer_put(ctx, ii, b, bt, br, bs2, &u2, size2);
if (ctx->rc != GRN_SUCCESS) {
GRN_FREE(bs2);
buffer_close(ctx, ii, pseg);
{
grn_obj term;
char errbuf[GRN_CTX_MSGSIZE];
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
MERR("%s failed to put to buffer: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"%s",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u2.rid,
u2.sid,
errbuf);
GRN_OBJ_FIN(ctx, &term);
}
goto exit;
}
br = (buffer_rec *)(((byte *)br) + size2);
GRN_FREE(bs2);
}
}
}
break;
}
if (!br) {
if (u->tf == 1 && u->weight == 0) {
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
if (u->rid < 0x100000 && u->sid < 0x800) {
a[0] = (u->rid << 12) + (u->sid << 1) + 1;
a[1] = u->pos->pos;
goto exit;
}
} else {
a[0] = (u->rid << 1) + 1;
a[1] = u->pos->pos;
goto exit;
}
}
pseg = buffer_new(ctx, ii, size, &pos, &bt, &br, &b, tid, h);
if (pseg == GRN_II_PSEG_NOT_ASSIGNED) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s failed to create a buffer: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"size:<%u>",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid,
size);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
bt->tid = tid;
bt->size_in_chunk = 0;
bt->pos_in_chunk = 0;
bt->size_in_buffer = 0;
bt->pos_in_buffer = 0;
}
buffer_put(ctx, ii, b, bt, br, bs, u, size);
buffer_close(ctx, ii, pseg);
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
if (!a[0] || POS_IS_EMBED(a[0])) {
a[0] = pos;
}
exit:
array_unref(ctx, ii, tid);
if (bs) {
GRN_FREE(bs);
}
if (u->tf != u->atf) {
grn_obj *source_table = grn_ctx_at(ctx, DB_OBJ(ii)->range);
GRN_DEFINE_NAME_CUSTOM(source_table, source_table_name);
grn_obj_unref(ctx, source_table);
GRN_DEFINE_NAME(ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"%s too many postings: "
"<%.*s>: "
"<%.*s>(%u): "
"record:<%.*s>(%u:%u), "
"n-postings:<%d>, "
"n-discarded-postings:<%d>",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
source_table_name_size,
source_table_name,
u->rid,
u->sid,
u->atf,
u->atf - u->tf);
GRN_OBJ_FIN(ctx, &term);
}
grn_ii_expire(ctx, ii);
return ctx->rc;
}
extern "C" grn_rc
grn_ii_update_one(
grn_ctx *ctx, grn_ii *ii, grn_id tid, grn_ii_updspec *u, grn_hash *h)
{
GRN_SLOW_LOG_PUSH(ctx, GRN_LOG_DEBUG);
grn_rc rc = grn_ii_update_one_internal(ctx, ii, tid, u, h);
GRN_SLOW_LOG_POP_BEGIN(ctx, GRN_LOG_DEBUG, elapsed_time)
{
GRN_DEFINE_NAME(ii);
grn_obj *source_table = grn_ctx_at(ctx, DB_OBJ(ii)->range);
GRN_DEFINE_NAME_CUSTOM(source_table, source_table_name);
grn_obj_unref(ctx, source_table);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][update][one][slow][%f] "
"<%.*s>: "
"<%.*s>(%u): "
"record:<%.*s>(%u:%u), "
"n-postings:<%d>, "
"n-discarded-postings:<%d>",
elapsed_time,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
source_table_name_size,
source_table_name,
u->rid,
u->sid,
u->atf,
u->atf - u->tf);
GRN_OBJ_FIN(ctx, &term);
}
GRN_SLOW_LOG_POP_END(ctx);
return rc;
}
static inline grn_rc
grn_ii_delete_one_internal(
grn_ctx *ctx, grn_ii *ii, grn_id tid, grn_ii_updspec *u, grn_hash *h)
{
const char *tag = "[ii][delete][one]";
buffer *b;
uint8_t *bs = NULL;
buffer_rec *br;
buffer_term *bt;
uint32_t pseg, size, *a;
if (!tid) {
return ctx->rc;
}
if (!(a = array_at(ctx, ii, tid))) {
return ctx->rc;
}
if (grn_ii_wal_touch(ctx, ii, tag) != GRN_SUCCESS) {
return ctx->rc;
}
for (;;) {
const uint32_t pos = a[0];
if (pos == 0) {
goto exit;
}
if (POS_IS_EMBED(pos)) {
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
const uint32_t rid = POS_RID_SID_EXTRACT_RID(pos);
const uint32_t sid = POS_RID_SID_EXTRACT_SID(pos);
if (u->rid == rid && (!u->sid || u->sid == sid)) {
a[0] = 0;
lexicon_delete(ctx, ii, tid, h);
}
} else {
const uint32_t rid = POS_RID_EXTRACT(pos);
if (u->rid == rid) {
a[0] = 0;
lexicon_delete(ctx, ii, tid, h);
}
}
goto exit;
}
if (!(bs = encode_rec(ctx, ii, u, &size, 1))) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s failed to encode a record: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u)",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
if ((pseg = buffer_open(ctx, ii, pos, &bt, &b)) ==
GRN_II_PSEG_NOT_ASSIGNED) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s failed to allocate a buffer: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"position:<%u>",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid,
pos);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
if (b->header.buffer_free < size) {
const uint32_t pos_keep = pos;
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"flushing! b=%p free=%d, seg(%d)",
b,
b->header.buffer_free,
grn_ii_pos_lseg(ii, pos));
buffer_close(ctx, ii, pseg);
buffer_flush(ctx, ii, grn_ii_pos_lseg(ii, pos), h);
if (ctx->rc != GRN_SUCCESS) {
grn_obj term;
char errbuf[GRN_CTX_MSGSIZE];
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
ERR(ctx->rc,
"%s failed to flush a buffer: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"position:<%u>: "
"%s",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid,
pos,
errbuf);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
if (a[0] != pos_keep) {
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"grn_ii_delete_one: a[0] changed %d->%d)",
a[0],
pos_keep);
continue;
}
if ((pseg = buffer_open(ctx, ii, a[0], &bt, &b)) ==
GRN_II_PSEG_NOT_ASSIGNED) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s failed to reallocate a buffer: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"position:<%u>",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid,
a[0]);
GRN_OBJ_FIN(ctx, &term);
goto exit;
}
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"flushed! b=%p free=%d, seg(%d)",
b,
b->header.buffer_free,
grn_ii_pos_lseg(ii, a[0]));
if (b->header.buffer_free < size) {
grn_obj term;
GRN_DEFINE_NAME(ii);
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
MERR("%s buffer is full: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u): "
"segment:<%u>, free:<%u>, required:<%u>",
tag,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
u->rid,
u->sid,
a[0],
b->header.buffer_free,
size);
GRN_OBJ_FIN(ctx, &term);
buffer_close(ctx, ii, pseg);
goto exit;
}
}
b->header.buffer_free -= size;
br = (buffer_rec *)(((byte *)&b->terms[b->header.nterms]) +
b->header.buffer_free);
buffer_put(ctx, ii, b, bt, br, bs, u, size);
buffer_close(ctx, ii, pseg);
break;
}
exit:
array_unref(ctx, ii, tid);
if (bs) {
GRN_FREE(bs);
}
return ctx->rc;
}
extern "C" grn_rc
grn_ii_delete_one(
grn_ctx *ctx, grn_ii *ii, grn_id tid, grn_ii_updspec *u, grn_hash *h)
{
GRN_SLOW_LOG_PUSH(ctx, GRN_LOG_DEBUG);
grn_rc rc = grn_ii_delete_one_internal(ctx, ii, tid, u, h);
GRN_SLOW_LOG_POP_BEGIN(ctx, GRN_LOG_DEBUG, elapsed_time)
{
GRN_DEFINE_NAME(ii);
grn_obj *source_table = grn_ctx_at(ctx, DB_OBJ(ii)->range);
GRN_DEFINE_NAME_CUSTOM(source_table, source_table_name);
grn_obj_unref(ctx, source_table);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, ii, tid, &term);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][delete][one][slow][%f] "
"<%.*s>: "
"<%.*s>(%u): "
"record:<%.*s>(%u:%u)",
elapsed_time,
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
tid,
source_table_name_size,
source_table_name,
u->rid,
u->sid);
GRN_OBJ_FIN(ctx, &term);
}
GRN_SLOW_LOG_POP_END(ctx);
return rc;
}
#define CHUNK_USED 1
#define BUFFER_USED 2
#define SOLE_DOC_USED 4
#define SOLE_POS_USED 8
struct _grn_ii_cursor {
grn_db_obj obj;
grn_ctx *ctx;
grn_ii *ii;
grn_id id;
grn_posting *post;
grn_id min; /* Minimum record ID */
grn_id max;
grn_posting_internal pc;
grn_posting_internal pb;
uint32_t cdf; /* Document frequency */
uint32_t *cdp;
uint32_t *crp; /* Record ID */
uint32_t *csp; /* Section ID */
uint32_t *ctp; /* Term frequency */
uint32_t *cwp; /* Weight */
uint32_t *cpp; /* Position */
uint8_t *bp;
uint32_t nelements;
uint32_t nchunks;
uint32_t curr_chunk;
chunk_info *cinfo;
grn_io_win iw;
uint8_t *cp;
uint8_t *cpe;
datavec rdv[MAX_N_ELEMENTS + 1];
buffer *buf;
uint16_t stat;
uint16_t nextb;
uint32_t buffer_pseg;
int flags;
uint32_t *ppseg;
int weight;
uint32_t prev_chunk_rid;
float scale;
float *scales;
size_t n_scales;
};
static bool
buffer_is_reused(grn_ctx *ctx, grn_ii *ii, grn_ii_cursor *c)
{
if (*c->ppseg != c->buffer_pseg) {
uint32_t i;
for (i = ii->header.common->bgqtail; i != ii->header.common->bgqhead;
i = (i + 1) & (GRN_II_BGQSIZE - 1)) {
if (ii->header.common->bgqbody[i] == c->buffer_pseg) {
return false;
}
}
return true;
}
return false;
}
static int
chunk_is_reused(
grn_ctx *ctx, grn_ii *ii, grn_ii_cursor *c, uint32_t offset, uint32_t size)
{
if (*c->ppseg != c->buffer_pseg) {
uint32_t i, gseg;
if (size > S_CHUNK) {
return 1;
}
grn_bit_scan_rev_result m;
if (size > (1 << GRN_II_W_LEAST_CHUNK)) {
int es = size - 1;
GRN_BIT_SCAN_REV(es, m);
m++;
} else {
m = GRN_II_W_LEAST_CHUNK;
}
gseg = ii->header.common->garbages[m - GRN_II_W_LEAST_CHUNK];
while (gseg != GRN_II_PSEG_NOT_ASSIGNED) {
grn_io_win iw;
auto ginfo = static_cast<grn_ii_ginfo *>(
WIN_MAP(ctx, ii->chunk, &iw, gseg, 0, S_GARBAGE, GRN_IO_RDWR));
if (!ginfo) {
break;
}
for (i = 0; i < ginfo->nrecs; i++) {
if (ginfo->recs[i] == offset) {
grn_io_win_unmap(ctx, &iw);
return 0;
}
}
gseg = ginfo->next;
grn_io_win_unmap(ctx, &iw);
}
return 1;
}
return 0;
}
#define GRN_II_CURSOR_CMP(c1, c2) \
(((c1)->post->rid > (c2)->post->rid) || \
(((c1)->post->rid == (c2)->post->rid) && \
(((c1)->post->sid > (c2)->post->sid) || \
(((c1)->post->sid == (c2)->post->sid) && \
((c1)->post->pos > (c2)->post->pos)))))
extern "C" grn_ii_cursor *
grn_ii_cursor_open(grn_ctx *ctx,
grn_ii *ii,
grn_id tid,
grn_id min,
grn_id max,
int nelements,
int flags)
{
grn_ii_cursor *c = NULL;
uint32_t pos, *a;
if (!(a = array_at(ctx, ii, tid))) {
return NULL;
}
for (;;) {
c = NULL;
if (!(pos = a[0])) {
goto exit;
}
c = static_cast<grn_ii_cursor *>(GRN_CALLOC(sizeof(grn_ii_cursor)));
if (!c) {
goto exit;
}
c->ctx = ctx;
c->ii = ii;
c->id = tid;
c->min = min;
c->max = max;
c->nelements = nelements;
c->flags = flags;
c->weight = 0;
c->scale = 1.0;
c->scales = NULL;
c->n_scales = 0;
datavec_init(ctx, ii, c->rdv, 0, 0);
if (POS_IS_EMBED(pos)) {
c->stat = 0;
if ((ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
c->pb.rid = POS_RID_SID_EXTRACT_RID(pos);
c->pb.sid = POS_RID_SID_EXTRACT_SID(pos);
} else {
c->pb.rid = POS_RID_EXTRACT(pos);
c->pb.sid = 1;
}
c->pb.tf = 1;
c->pb.weight = 0;
c->pb.weight_float = (float)(c->pb.weight);
c->pb.pos = a[1];
c->pb.scale = c->scale;
} else {
uint32_t chunk;
buffer_term *bt;
c->buffer_pseg = buffer_open(ctx, ii, pos, &bt, &c->buf);
if (c->buffer_pseg == GRN_II_PSEG_NOT_ASSIGNED) {
GRN_FREE(c);
c = NULL;
goto exit;
}
const uint32_t lseg = grn_ii_pos_lseg(ii, pos);
c->ppseg = grn_ii_get_buffer_pseg_address_inline(ii, lseg);
if (bt->size_in_chunk &&
(chunk = c->buf->header.chunk) != GRN_II_PSEG_NOT_ASSIGNED) {
if (!(c->cp = static_cast<uint8_t *>(WIN_MAP(ctx,
ii->chunk,
&c->iw,
chunk,
bt->pos_in_chunk,
bt->size_in_chunk,
GRN_IO_RDONLY)))) {
buffer_close(ctx, ii, c->buffer_pseg);
GRN_FREE(c);
c = NULL;
goto exit;
}
if (buffer_is_reused(ctx, ii, c)) {
grn_ii_cursor_close(ctx, c);
continue;
}
c->cpe = c->cp + bt->size_in_chunk;
if ((bt->tid & CHUNK_SPLIT)) {
uint32_t i;
grn_id crid;
GRN_B_DEC(c->nchunks, c->cp);
if (chunk_is_reused(ctx, ii, c, chunk, c->buf->header.chunk_size)) {
grn_ii_cursor_close(ctx, c);
continue;
}
if (!(c->cinfo = GRN_MALLOCN(chunk_info, c->nchunks))) {
buffer_close(ctx, ii, c->buffer_pseg);
grn_io_win_unmap(ctx, &c->iw);
GRN_FREE(c);
c = NULL;
goto exit;
}
for (i = 0, crid = GRN_ID_NIL; i < c->nchunks; i++) {
GRN_B_DEC(c->cinfo[i].segno, c->cp);
GRN_B_DEC(c->cinfo[i].size, c->cp);
GRN_B_DEC(c->cinfo[i].dgap, c->cp);
crid += c->cinfo[i].dgap;
if (crid < min) {
c->pc.rid = crid;
c->curr_chunk = i + 1;
}
}
if (chunk_is_reused(ctx, ii, c, chunk, c->buf->header.chunk_size)) {
grn_ii_cursor_close(ctx, c);
continue;
}
}
}
c->nextb = bt->pos_in_buffer;
c->stat = CHUNK_USED | BUFFER_USED;
}
if (pos == a[0]) {
break;
}
grn_ii_cursor_close(ctx, c);
}
exit:
array_unref(ctx, ii, tid);
return c;
}
extern "C" grn_ii *
grn_ii_cursor_get_ii(grn_ctx *ctx, grn_ii_cursor *cursor)
{
return cursor->ii;
}
static inline float
grn_ii_cursor_resolve_scale(grn_ctx *ctx,
grn_ii_cursor *cursor,
uint32_t section_id)
{
if (cursor->n_scales > 0) {
if (section_id <= cursor->n_scales) {
return cursor->scales[section_id - 1];
} else {
return 0.0;
}
} else {
return cursor->scale;
}
}
extern "C" grn_rc
grn_ii_cursor_set_scale(grn_ctx *ctx, grn_ii_cursor *cursor, float scale)
{
cursor->scale = scale;
cursor->scales = NULL;
cursor->n_scales = 0;
if (!cursor->buf) {
cursor->pb.scale = grn_ii_cursor_resolve_scale(ctx, cursor, cursor->pb.sid);
}
return ctx->rc;
}
extern "C" grn_rc
grn_ii_cursor_set_scales(grn_ctx *ctx,
grn_ii_cursor *cursor,
float *scales,
size_t n_scales)
{
cursor->scale = 0.0;
cursor->scales = scales;
cursor->n_scales = n_scales;
if (!cursor->buf) {
cursor->pb.scale = grn_ii_cursor_resolve_scale(ctx, cursor, cursor->pb.sid);
}
return ctx->rc;
}
static inline void
grn_ii_cursor_set_min(grn_ctx *ctx, grn_ii_cursor *c, grn_id min)
{
if (c->min >= min) {
return;
}
if (grn_ii_cursor_set_min_enable) {
grn_id old_min = c->min;
c->min = min;
if (c->buf && c->pc.rid != GRN_ID_NIL && c->pc.rid < c->min &&
c->prev_chunk_rid < c->min && c->curr_chunk < c->nchunks) {
uint32_t i;
uint32_t skip_chunk = 0;
grn_id rid = c->prev_chunk_rid;
if (c->curr_chunk > 0) {
i = c->curr_chunk - 1;
} else {
i = 0;
}
for (; i < c->nchunks; i++) {
rid += c->cinfo[i].dgap;
if (rid < c->min) {
skip_chunk = i + 1;
} else {
rid -= c->cinfo[i].dgap;
break;
}
}
if (skip_chunk > c->curr_chunk) {
grn_id old_rid = c->pc.rid;
grn_id old_prev_chunk_rid = c->prev_chunk_rid;
uint32_t old_chunk = c->curr_chunk;
bool old_chunk_used = (c->stat & CHUNK_USED);
c->pc.rid = rid;
c->pc.rest = 0;
c->prev_chunk_rid = rid - c->cinfo[skip_chunk - 1].dgap;
c->curr_chunk = skip_chunk;
c->crp = c->cdp + c->cdf;
c->stat |= CHUNK_USED;
if (grn_logger_pass(ctx, GRN_LOG_DEBUG)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][cursor][min][%.*s][%.*s][%u] skip: "
"<%p>: "
"rid(%u->%u): "
"prev-chunk-rid(%u->%u): "
"min(%u->%u): "
"chunk(%u->%u): "
"chunk-used(%s->%s)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c,
old_rid,
rid,
old_prev_chunk_rid,
c->prev_chunk_rid,
old_min,
min,
old_chunk,
c->curr_chunk,
old_chunk_used ? "true" : "false",
(c->stat & CHUNK_USED) ? "true" : "false");
GRN_OBJ_FIN(ctx, &term);
}
}
}
}
}
typedef struct {
bool include_garbage;
} grn_ii_cursor_next_options;
static inline grn_posting *
grn_ii_cursor_next_internal(grn_ctx *ctx,
grn_ii_cursor *c,
grn_ii_cursor_next_options *options)
{
const bool include_garbage = options->include_garbage;
if (c->buf) {
for (;;) {
if (c->stat & CHUNK_USED) {
for (;;) {
if (c->crp < c->cdp + c->cdf) {
uint32_t dgap = *c->crp++;
c->pc.rid += dgap;
if (dgap) {
c->pc.sid = 0;
}
if ((c->ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
c->pc.sid += 1 + *c->csp++;
} else {
c->pc.sid = 1;
}
c->cpp += c->pc.rest;
c->pc.rest = c->pc.tf = 1 + *c->ctp++;
if ((c->ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
c->pc.weight = *c->cwp++;
} else {
c->pc.weight = 0;
}
c->pc.weight_float = (float)(c->pc.weight);
c->pc.pos = 0;
c->pc.scale = grn_ii_cursor_resolve_scale(ctx, c, c->pc.sid);
/*
{
static int count = 0;
int tf = c->pc.tf, pos = 0, *pp = (int *)c->cpp;
grn_obj buf;
GRN_TEXT_INIT(&buf, 0);
grn_text_itoa(ctx, &buf, c->pc.rid);
GRN_TEXT_PUTC(ctx, &buf, ':');
grn_text_itoa(ctx, &buf, c->pc.sid);
GRN_TEXT_PUTC(ctx, &buf, ':');
grn_text_itoa(ctx, &buf, c->pc.tf);
GRN_TEXT_PUTC(ctx, &buf, '(');
while (tf--) {
pos += *pp++;
count++;
grn_text_itoa(ctx, &buf, pos);
if (tf) { GRN_TEXT_PUTC(ctx, &buf, ':'); }
}
GRN_TEXT_PUTC(ctx, &buf, ')');
GRN_TEXT_PUTC(ctx, &buf, '\0');
GRN_LOG(ctx, GRN_LOG_DEBUG, "posting(%d):%s", count,
GRN_TEXT_VALUE(&buf)); GRN_OBJ_FIN(ctx, &buf);
}
*/
if (c->pc.rid < c->min || c->pc.scale <= 0.0) {
continue;
}
} else {
if (c->curr_chunk <= c->nchunks) {
if (c->curr_chunk == c->nchunks) {
if (c->cp < c->cpe) {
int decoded_size;
decoded_size =
grn_decv(ctx, c->ii, c->id, c->cp, c->cpe - c->cp, c->rdv);
if (decoded_size == 0) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][cursor][next][chunk][last][%.*s][%.*s][%u] "
"failed to decode the last chunk. "
"Another thread might change "
"the chunk while decoding: "
"<%p>: <%d>: <%" GRN_FMT_INT64D ">",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c,
c->curr_chunk,
c->cinfo ? (int64_t)(c->cinfo[c->curr_chunk].segno)
: -1);
GRN_OBJ_FIN(ctx, &term);
c->pc.rid = GRN_ID_NIL;
break;
}
if (buffer_is_reused(ctx, c->ii, c)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][cursor][next][chunk][last][%.*s][%.*s][%u] "
"buffer is reused by another thread: <%p>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c);
GRN_OBJ_FIN(ctx, &term);
c->pc.rid = GRN_ID_NIL;
break;
}
if (chunk_is_reused(ctx,
c->ii,
c,
c->buf->header.chunk,
c->buf->header.chunk_size)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][cursor][next][chunk][last][%.*s][%.*s][%u] "
"chunk is reused by another thread: "
"<%p>: <%d>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c,
c->buf->header.chunk);
GRN_OBJ_FIN(ctx, &term);
c->pc.rid = GRN_ID_NIL;
break;
}
} else {
c->pc.rid = GRN_ID_NIL;
break;
}
} else {
uint8_t *cp;
grn_io_win iw;
uint32_t size = c->cinfo[c->curr_chunk].size;
if (size && (cp = static_cast<uint8_t *>(
WIN_MAP(ctx,
c->ii->chunk,
&iw,
c->cinfo[c->curr_chunk].segno,
0,
size,
GRN_IO_RDONLY)))) {
int decoded_size;
decoded_size = grn_decv(ctx, c->ii, c->id, cp, size, c->rdv);
grn_io_win_unmap(ctx, &iw);
if (decoded_size == 0) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][cursor][next][chunk][%.*s][%.*s][%u] "
"failed to decode the next chunk. "
"Another thread might change "
"the chunk while decoding: "
"<%p>: <%d>: <%" GRN_FMT_INT64D ">",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c,
c->curr_chunk,
c->cinfo ? (int64_t)(c->cinfo[c->curr_chunk].segno)
: -1);
GRN_OBJ_FIN(ctx, &term);
c->pc.rid = GRN_ID_NIL;
break;
}
if (buffer_is_reused(ctx, c->ii, c)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][cursor][next][chunk][%.*s][%.*s][%u] "
"buffer is reused by another thread: <%p>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c);
GRN_OBJ_FIN(ctx, &term);
c->pc.rid = GRN_ID_NIL;
break;
}
if (chunk_is_reused(ctx,
c->ii,
c,
c->cinfo[c->curr_chunk].segno,
size)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][cursor][next][chunk][%.*s][%.*s][%u] "
"chunk is reused by another thread: "
"<%p>: <%d>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c,
c->cinfo[c->curr_chunk].segno);
GRN_OBJ_FIN(ctx, &term);
c->pc.rid = GRN_ID_NIL;
break;
}
} else {
c->pc.rid = GRN_ID_NIL;
break;
}
}
{
int j = 0;
c->cdf = c->rdv[j].data_size;
c->crp = c->cdp = c->rdv[j++].data;
if ((c->ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
c->csp = c->rdv[j++].data;
}
c->ctp = c->rdv[j++].data;
if ((c->ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
c->cwp = c->rdv[j++].data;
}
if ((c->ii->header.common->flags & GRN_OBJ_WITH_POSITION)) {
c->cpp = c->rdv[j].data;
}
}
c->prev_chunk_rid = c->pc.rid;
c->pc.rid = GRN_ID_NIL;
c->pc.sid = 0;
c->pc.rest = 0;
c->curr_chunk++;
continue;
} else {
c->pc.rid = GRN_ID_NIL;
}
}
break;
}
}
if (c->stat & BUFFER_USED) {
for (;;) {
if (c->nextb) {
uint32_t lrid = c->pb.rid, lsid = c->pb.sid; /* for check */
buffer_rec *br = BUFFER_REC_AT(c->buf, c->nextb);
if (buffer_is_reused(ctx, c->ii, c)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][cursor][next][buffer][%.*s][%.*s][%u] "
"buffer is reused by another thread: "
"<%p>: <%d>: <%d>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c,
c->buffer_pseg,
*c->ppseg);
GRN_OBJ_FIN(ctx, &term);
c->pb.rid = GRN_ID_NIL;
break;
}
c->bp = GRN_NEXT_ADDR(br);
GRN_B_DEC(c->pb.rid, c->bp);
if ((c->ii->header.common->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_DEC(c->pb.sid, c->bp);
} else {
c->pb.sid = 1;
}
if (lrid > c->pb.rid || (lrid == c->pb.rid && lsid >= c->pb.sid)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
ERR(GRN_FILE_CORRUPT,
"[ii][broken][cursor][next][buffer][%.*s][%.*s][%u] "
"posting in list in buffer isn't sorted: "
"(%d:%d) -> (%d:%d) (%d->%d)",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
lrid,
lsid,
c->pb.rid,
c->pb.sid,
c->buffer_pseg,
*c->ppseg);
GRN_OBJ_FIN(ctx, &term);
c->pb.rid = GRN_ID_NIL;
break;
}
c->pb.scale = grn_ii_cursor_resolve_scale(ctx, c, c->pb.sid);
if (c->pb.rid < c->min || c->pb.scale <= 0.0) {
c->pb.rid = 0;
if (br->jump > 0 && !BUFFER_REC_DELETED(br)) {
buffer_rec *jump_br = BUFFER_REC_AT(c->buf, br->jump);
if (BUFFER_REC_DELETED(jump_br)) {
c->nextb = br->step;
} else {
uint8_t *jump_bp;
uint32_t jump_rid;
jump_bp = GRN_NEXT_ADDR(jump_br);
GRN_B_DEC(jump_rid, jump_bp);
if (jump_rid < c->min) {
c->nextb = br->jump;
} else {
c->nextb = br->step;
}
}
} else {
c->nextb = br->step;
}
continue;
}
c->nextb = br->step;
GRN_B_DEC(c->pb.tf, c->bp);
if ((c->ii->header.common->flags & GRN_OBJ_WITH_WEIGHT)) {
GRN_B_DEC(c->pb.weight, c->bp);
} else {
c->pb.weight = 0;
}
c->pb.weight_float = (float)(c->pb.weight);
c->pb.rest = c->pb.tf;
c->pb.pos = 0;
} else {
c->pb.rid = 0;
}
break;
}
}
if (c->pb.rid) {
if (c->pc.rid) {
if (c->pc.rid < c->pb.rid) {
c->stat = CHUNK_USED;
if (include_garbage || (c->pc.tf && c->pc.sid)) {
c->post = (grn_posting *)(&c->pc);
break;
}
} else {
if (c->pb.rid < c->pc.rid) {
c->stat = BUFFER_USED;
if (include_garbage || (c->pb.tf && c->pb.sid)) {
c->post = (grn_posting *)(&c->pb);
break;
}
} else {
if (c->pb.sid) {
if (c->pc.sid < c->pb.sid) {
c->stat = CHUNK_USED;
if (include_garbage || (c->pc.tf && c->pc.sid)) {
c->post = (grn_posting *)(&c->pc);
break;
}
} else {
c->stat = BUFFER_USED;
if (c->pb.sid == c->pc.sid) {
c->stat |= CHUNK_USED;
}
if (include_garbage || (c->pb.tf)) {
c->post = (grn_posting *)(&c->pb);
break;
}
}
} else {
c->stat = CHUNK_USED;
}
}
}
} else {
c->stat = BUFFER_USED;
if (include_garbage || (c->pb.tf && c->pb.sid)) {
c->post = (grn_posting *)(&c->pb);
break;
}
}
} else {
if (c->pc.rid) {
c->stat = CHUNK_USED;
if (include_garbage || (c->pc.tf && c->pc.sid)) {
c->post = (grn_posting *)(&c->pc);
break;
}
} else {
c->post = NULL;
return NULL;
}
}
}
} else {
if (c->stat & SOLE_DOC_USED) {
c->post = NULL;
return NULL;
} else {
c->post = (grn_posting *)(&c->pb);
c->stat |= SOLE_DOC_USED;
if (c->post->rid < c->min) {
c->post = NULL;
return NULL;
}
if (c->pb.scale <= 0.0) {
c->post = NULL;
return NULL;
}
}
}
return c->post;
}
extern "C" grn_posting *
grn_ii_cursor_next(grn_ctx *ctx, grn_ii_cursor *c)
{
grn_ii_cursor_next_options options = {/* .include_garbage = */ false};
return grn_ii_cursor_next_internal(ctx, c, &options);
}
extern "C" grn_posting *
grn_ii_cursor_next_pos(grn_ctx *ctx, grn_ii_cursor *c)
{
uint32_t gap;
if ((c->ii->header.common->flags & GRN_OBJ_WITH_POSITION)) {
if (c->nelements == c->ii->n_elements) {
if (c->buf) {
if (c->post == (grn_posting *)(&c->pc)) {
if (c->pc.rest) {
c->pc.rest--;
c->pc.pos += *c->cpp++;
} else {
return NULL;
}
} else if (c->post == (grn_posting *)(&c->pb)) {
if (buffer_is_reused(ctx, c->ii, c)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][cursor][next][pos][buffer][%.*s][%.*s][%u] "
"buffer(%d,%d) is reused by another thread: %p",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c->buffer_pseg,
*c->ppseg,
c);
GRN_OBJ_FIN(ctx, &term);
return NULL;
}
if (c->pb.rest) {
c->pb.rest--;
GRN_B_DEC(gap, c->bp);
c->pb.pos += gap;
} else {
return NULL;
}
} else {
return NULL;
}
} else {
if (c->stat & SOLE_POS_USED) {
return NULL;
} else {
c->stat |= SOLE_POS_USED;
}
}
}
} else {
if (c->stat & SOLE_POS_USED) {
return NULL;
} else {
c->stat |= SOLE_POS_USED;
}
}
return c->post;
}
extern "C" grn_rc
grn_ii_cursor_close(grn_ctx *ctx, grn_ii_cursor *c)
{
if (!c) {
return GRN_INVALID_ARGUMENT;
}
datavec_fin(ctx, c->rdv);
if (c->cinfo) {
GRN_FREE(c->cinfo);
}
if (c->buf) {
buffer_close(ctx, c->ii, c->buffer_pseg);
}
if (c->cp) {
grn_io_win_unmap(ctx, &c->iw);
}
GRN_FREE(c);
return GRN_SUCCESS;
}
extern "C" uint32_t
grn_ii_get_chunksize(grn_ctx *ctx, grn_ii *ii, grn_id tid)
{
uint32_t res, pos, *a;
a = array_at(ctx, ii, tid);
if (!a) {
return 0;
}
if ((pos = a[0])) {
if (POS_IS_EMBED(pos)) {
res = 0;
} else {
buffer *buf;
uint32_t pseg;
buffer_term *bt;
if ((pseg = buffer_open(ctx, ii, pos, &bt, &buf)) ==
GRN_II_PSEG_NOT_ASSIGNED) {
res = 0;
} else {
res = bt->size_in_chunk;
buffer_close(ctx, ii, pseg);
}
}
} else {
res = 0;
}
array_unref(ctx, ii, tid);
return res;
}
/* This doesn't return the number of records that have the token. But
* this returns a similar number based of size in buffer. Buffer may
* have deleted tokens. One record may use multiple buffers. (To be
* confirmed.) So the estimated size may not match the number of
* records. */
extern "C" uint32_t
grn_ii_estimate_size(grn_ctx *ctx, grn_ii *ii, grn_id tid)
{
uint32_t res, pos, *a;
a = array_at(ctx, ii, tid);
if (!a) {
return 0;
}
if ((pos = a[0])) {
if (POS_IS_EMBED(pos)) {
res = 1;
} else {
buffer *buf;
uint32_t pseg;
buffer_term *bt;
if ((pseg = buffer_open(ctx, ii, pos, &bt, &buf)) ==
GRN_II_PSEG_NOT_ASSIGNED) {
res = 0;
} else {
res = a[1] + bt->size_in_buffer + 2;
buffer_close(ctx, ii, pseg);
}
}
} else {
res = 0;
}
array_unref(ctx, ii, tid);
return res;
}
extern "C" int
grn_ii_entry_info(grn_ctx *ctx,
grn_ii *ii,
grn_id tid,
unsigned int *a,
unsigned int *chunk,
unsigned int *chunk_size,
unsigned int *buffer_free,
unsigned int *nterms,
unsigned int *nterms_void,
unsigned int *bt_tid,
unsigned int *size_in_chunk,
unsigned int *pos_in_chunk,
unsigned int *size_in_buffer,
unsigned int *pos_in_buffer)
{
buffer *b;
buffer_term *bt;
uint32_t pseg, *ap;
ERRCLR(NULL);
ap = array_at(ctx, ii, tid);
if (!ap) {
return 0;
}
a[0] = *ap;
array_unref(ctx, ii, tid);
if (!a[0]) {
return 1;
}
if (POS_IS_EMBED(a[0])) {
return 2;
}
if ((pseg = buffer_open(ctx, ii, a[0], &bt, &b)) ==
GRN_II_PSEG_NOT_ASSIGNED) {
return 3;
}
*chunk = b->header.chunk;
*chunk_size = b->header.chunk_size;
*buffer_free = b->header.buffer_free;
*nterms = b->header.nterms;
*bt_tid = bt->tid;
*size_in_chunk = bt->size_in_chunk;
*pos_in_chunk = bt->pos_in_chunk;
*size_in_buffer = bt->size_in_buffer;
*pos_in_buffer = bt->pos_in_buffer;
buffer_close(ctx, ii, pseg);
return 4;
}
extern "C" uint32_t
grn_ii_get_array_pseg(grn_ii *ii, uint32_t lseg)
{
return grn_ii_get_array_pseg_inline(ii, lseg);
}
extern "C" uint32_t
grn_ii_get_buffer_pseg(grn_ii *ii, uint32_t lseg)
{
return grn_ii_get_buffer_pseg_inline(ii, lseg);
}
extern "C" uint32_t
grn_ii_n_logical_segments(grn_ii *ii)
{
return grn_ii_n_logical_segments_inline(ii);
}
extern "C" const char *
grn_ii_path(grn_ii *ii)
{
return grn_io_path(ii->seg);
}
extern "C" uint32_t
grn_ii_max_section(grn_ii *ii)
{
return ii->header.common->smax;
}
/* private classes */
/* b-heap */
typedef struct {
int n_entries;
int n_bins;
grn_ii_cursor **bins;
} cursor_heap;
extern "C" void
grn_inspect_cursor_heap(grn_ctx *ctx, grn_obj *buffer, cursor_heap *heap)
{
grn_text_printf(ctx,
buffer,
"#<cursor_heap: n_entries=%d n_bins=%d\n",
heap->n_entries,
heap->n_bins);
grn_text_printf(ctx, buffer, " bins:\n");
int i;
for (i = 0; i < heap->n_bins; i++) {
grn_text_printf(ctx, buffer, " [%d] ", i);
grn_ii_get_term(ctx, heap->bins[i]->ii, heap->bins[i]->id, buffer);
grn_text_printf(ctx, buffer, "\n");
}
grn_text_printf(ctx, buffer, ">");
}
static inline cursor_heap *
cursor_heap_open(grn_ctx *ctx, int max)
{
auto h = static_cast<cursor_heap *>(GRN_CALLOC(sizeof(cursor_heap)));
if (!h) {
return NULL;
}
h->bins =
static_cast<grn_ii_cursor **>(GRN_MALLOC(sizeof(grn_ii_cursor *) * max));
if (!h->bins) {
GRN_FREE(h);
return NULL;
}
h->n_entries = 0;
h->n_bins = max;
return h;
}
static inline grn_rc
cursor_heap_push(grn_ctx *ctx,
cursor_heap *h,
grn_ii *ii,
grn_id tid,
uint32_t offset2,
int weight,
grn_id min)
{
int n, n2;
grn_ii_cursor *c, *c2;
if (h->n_entries >= h->n_bins) {
int max = h->n_bins * 2;
auto bins = static_cast<grn_ii_cursor **>(
GRN_REALLOC(h->bins, sizeof(grn_ii_cursor *) * max));
GRN_LOG(ctx, GRN_LOG_DEBUG, "expanded cursor_heap to %d,%p", max, bins);
if (!bins) {
return GRN_NO_MEMORY_AVAILABLE;
}
h->n_bins = max;
h->bins = bins;
}
{
if (!(c = grn_ii_cursor_open(ctx,
ii,
tid,
min,
GRN_ID_MAX,
ii->n_elements,
0))) {
GRN_LOG(ctx, GRN_LOG_ERROR, "cursor open failed");
return ctx->rc;
}
if (!grn_ii_cursor_next(ctx, c)) {
grn_ii_cursor_close(ctx, c);
return GRN_END_OF_DATA;
}
if (!grn_ii_cursor_next_pos(ctx, c)) {
if (grn_logger_pass(ctx, GRN_LOG_ERROR)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_ERROR,
"[ii][cursor][heap][push][%.*s][%.*s][%u] "
"invalid cursor: <%p>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c);
GRN_OBJ_FIN(ctx, &term);
}
grn_ii_cursor_close(ctx, c);
return GRN_END_OF_DATA;
}
if (weight) {
c->weight = weight;
}
n = h->n_entries++;
while (n) {
n2 = (n - 1) >> 1;
c2 = h->bins[n2];
if (GRN_II_CURSOR_CMP(c, c2)) {
break;
}
h->bins[n] = c2;
n = n2;
}
h->bins[n] = c;
}
return GRN_SUCCESS;
}
static inline grn_rc
cursor_heap_push2(cursor_heap *h)
{
grn_rc rc = GRN_SUCCESS;
return rc;
}
static inline grn_ii_cursor *
cursor_heap_min(cursor_heap *h)
{
return h->n_entries ? h->bins[0] : NULL;
}
static inline void
cursor_heap_recalc_min(cursor_heap *h)
{
int n = 0, n1, n2, m;
if ((m = h->n_entries) > 1) {
grn_ii_cursor *c = h->bins[0], *c1, *c2;
for (;;) {
n1 = n * 2 + 1;
n2 = n1 + 1;
c1 = n1 < m ? h->bins[n1] : NULL;
c2 = n2 < m ? h->bins[n2] : NULL;
if (c1 && GRN_II_CURSOR_CMP(c, c1)) {
if (c2 && GRN_II_CURSOR_CMP(c, c2) && GRN_II_CURSOR_CMP(c1, c2)) {
h->bins[n] = c2;
n = n2;
} else {
h->bins[n] = c1;
n = n1;
}
} else {
if (c2 && GRN_II_CURSOR_CMP(c, c2)) {
h->bins[n] = c2;
n = n2;
} else {
h->bins[n] = c;
break;
}
}
}
}
}
static inline void
cursor_heap_pop(grn_ctx *ctx, cursor_heap *h, grn_id min)
{
if (h->n_entries) {
grn_ii_cursor *c = h->bins[0];
grn_ii_cursor_set_min(ctx, c, min);
if (!grn_ii_cursor_next(ctx, c)) {
grn_ii_cursor_close(ctx, c);
h->bins[0] = h->bins[--h->n_entries];
} else if (!grn_ii_cursor_next_pos(ctx, c)) {
if (grn_logger_pass(ctx, GRN_LOG_ERROR)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_ERROR,
"[ii][cursor][heap][pop][%.*s][%.*s][%u] invalid cursor: <%p>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c);
GRN_OBJ_FIN(ctx, &term);
}
grn_ii_cursor_close(ctx, c);
h->bins[0] = h->bins[--h->n_entries];
}
if (h->n_entries > 1) {
cursor_heap_recalc_min(h);
}
}
}
static inline void
cursor_heap_pop_pos(grn_ctx *ctx, cursor_heap *h)
{
if (h->n_entries) {
grn_ii_cursor *c = h->bins[0];
if (!grn_ii_cursor_next_pos(ctx, c)) {
if (!grn_ii_cursor_next(ctx, c)) {
grn_ii_cursor_close(ctx, c);
h->bins[0] = h->bins[--h->n_entries];
} else if (!grn_ii_cursor_next_pos(ctx, c)) {
if (grn_logger_pass(ctx, GRN_LOG_ERROR)) {
GRN_DEFINE_NAME(c->ii);
grn_obj term;
GRN_TEXT_INIT(&term, 0);
grn_ii_get_term(ctx, c->ii, c->id, &term);
GRN_LOG(ctx,
GRN_LOG_ERROR,
"[ii][cursor][heap][pop][position][%.*s][%.*s][%u] "
"invalid cursor: <%p>",
name_size,
name,
(int)GRN_TEXT_LEN(&term),
GRN_TEXT_VALUE(&term),
c->id,
c);
GRN_OBJ_FIN(ctx, &term);
}
grn_ii_cursor_close(ctx, c);
h->bins[0] = h->bins[--h->n_entries];
}
}
if (h->n_entries > 1) {
cursor_heap_recalc_min(h);
}
}
}
static inline void
cursor_heap_close(grn_ctx *ctx, cursor_heap *h)
{
int i;
if (!h) {
return;
}
for (i = h->n_entries; i--;) {
grn_ii_cursor_close(ctx, h->bins[i]);
}
GRN_FREE(h->bins);
GRN_FREE(h);
}
static grn_rc
grn_vector2updspecs(grn_ctx *ctx,
grn_ii *ii,
grn_id rid,
unsigned int original_section,
unsigned int section,
grn_obj *in,
grn_obj *out,
grn_tokenize_mode mode,
grn_obj *posting)
{
grn_id tid;
grn_section *v;
grn_token_cursor *token_cursor;
grn_ii_updspec **u;
grn_hash *h = (grn_hash *)out;
grn_obj *lexicon = ii->lexicon;
if (in->u.v.body) {
grn_obj *source_column = nullptr;
auto n_sources = DB_OBJ(ii)->source_size / sizeof(grn_id);
if (n_sources > 0 && original_section > 0) {
auto source_ids = static_cast<grn_id *>(DB_OBJ(ii)->source);
auto source_id = source_ids[original_section - 1];
source_column = grn_ctx_at(ctx, source_id);
}
bool have_tokenizer = grn_table_have_tokenizer(ctx, lexicon);
const char *head = GRN_BULK_HEAD(in->u.v.body);
int32_t i;
int32_t n = (int32_t)(in->u.v.n_sections);
for (i = 0, v = in->u.v.sections; i < n; i++, v++) {
unsigned int token_flags = 0;
if (v->length && (token_cursor = grn_token_cursor_open(ctx,
lexicon,
head + v->offset,
v->length,
mode,
token_flags))) {
grn_token_cursor_set_source_id(ctx, token_cursor, rid);
if (v->domain != GRN_ID_NIL) {
grn_token_cursor_set_query_domain(ctx, token_cursor, v->domain);
}
if (source_column) {
grn_token_cursor_set_source_column(ctx, token_cursor, source_column);
}
while (!token_cursor->status) {
if ((tid = grn_token_cursor_next(ctx, token_cursor))) {
if (posting) {
GRN_RECORD_PUT(ctx, posting, tid);
}
if (!grn_hash_add(ctx,
h,
&tid,
sizeof(grn_id),
(void **)&u,
NULL)) {
break;
}
if (!*u) {
if (!(*u = grn_ii_updspec_open(ctx, rid, section))) {
GRN_DEFINE_NAME(ii);
MERR("[ii][update][spec] failed to create an update spec: "
"<%.*s>: "
"record:<%u>:<%u>, token:<%u>:<%d>:<%f>",
name_size,
name,
rid,
section,
tid,
token_cursor->pos,
v->weight);
grn_token_cursor_close(ctx, token_cursor);
return ctx->rc;
}
}
if (grn_ii_updspec_add(ctx,
*u,
have_tokenizer ? token_cursor->pos : i,
(int32_t)(v->weight))) {
GRN_DEFINE_NAME(ii);
MERR("[ii][update][spec] failed to add to update spec: "
"<%.*s>: "
"record:<%u>:<%u>, token:<%u>:<%d>:<%f>",
name_size,
name,
rid,
section,
tid,
token_cursor->pos,
v->weight);
grn_token_cursor_close(ctx, token_cursor);
return ctx->rc;
}
}
}
grn_token_cursor_close(ctx, token_cursor);
}
}
if (source_column) {
grn_obj_unref(ctx, source_column);
}
}
return ctx->rc;
}
static grn_rc
grn_uvector2updspecs_data(grn_ctx *ctx,
grn_ii *ii,
grn_id rid,
unsigned int section,
grn_obj *in,
grn_obj *out,
grn_tokenize_mode mode,
grn_obj *posting)
{
grn_rc rc = GRN_SUCCESS;
grn_hash *h = (grn_hash *)out;
grn_obj *lexicon = ii->lexicon;
grn_obj *tokenizer;
bool need_cast;
grn_obj element_buffer;
grn_obj casted_element_buffer;
int i, n;
unsigned int element_size;
tokenizer = grn_obj_get_info(ctx, lexicon, GRN_INFO_DEFAULT_TOKENIZER, NULL);
need_cast = (!tokenizer && lexicon->header.domain != in->header.domain);
if (need_cast) {
GRN_VALUE_FIX_SIZE_INIT(&element_buffer, 0, in->header.domain);
GRN_VALUE_FIX_SIZE_INIT(&casted_element_buffer,
GRN_OBJ_VECTOR,
lexicon->header.domain);
} else {
GRN_VOID_INIT(&element_buffer);
GRN_VOID_INIT(&casted_element_buffer);
}
n = grn_uvector_size(ctx, in);
element_size = grn_uvector_element_size(ctx, in);
for (i = 0; i < n; i++) {
grn_token_cursor *token_cursor;
unsigned int token_flags = 0;
const char *element;
element = GRN_BULK_HEAD(in) + (element_size * i);
if (need_cast) {
GRN_BULK_REWIND(&element_buffer);
GRN_BULK_REWIND(&casted_element_buffer);
grn_bulk_write(ctx, &element_buffer, element, element_size);
grn_rc cast_rc =
grn_obj_cast(ctx, &element_buffer, &casted_element_buffer, true);
if (cast_rc != GRN_SUCCESS) {
GRN_DEFINE_NAME(ii);
{
grn_obj *domain =
grn_ctx_at(ctx, casted_element_buffer.header.domain);
char domain_name[GRN_TABLE_MAX_KEY_SIZE];
int domain_name_size;
domain_name_size =
grn_obj_name(ctx, domain, domain_name, GRN_TABLE_MAX_KEY_SIZE);
grn_obj inspected_element;
GRN_TEXT_INIT(&inspected_element, 0);
grn_inspect(ctx, &inspected_element, &element_buffer);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][updspec][uvector][data][%.*s] "
"failed to cast to <%.*s>: <%.*s>",
name_size,
name,
domain_name_size,
domain_name,
(int)GRN_TEXT_LEN(&inspected_element),
GRN_TEXT_VALUE(&inspected_element));
GRN_OBJ_FIN(ctx, &inspected_element);
}
}
element = GRN_BULK_HEAD(&casted_element_buffer);
element_size = GRN_BULK_VSIZE(&casted_element_buffer);
}
token_cursor = grn_token_cursor_open(ctx,
lexicon,
element,
element_size,
mode,
token_flags);
if (!token_cursor) {
continue;
}
grn_token_cursor_set_query_domain(ctx, token_cursor, in->header.domain);
while (!token_cursor->status) {
grn_id tid;
if ((tid = grn_token_cursor_next(ctx, token_cursor))) {
grn_ii_updspec **u;
int pos;
if (posting) {
GRN_RECORD_PUT(ctx, posting, tid);
}
if (!grn_hash_add(ctx, h, &tid, sizeof(grn_id), (void **)&u, NULL)) {
break;
}
if (!*u) {
if (!(*u = grn_ii_updspec_open(ctx, rid, section))) {
GRN_LOG(ctx,
GRN_LOG_ALERT,
"grn_ii_updspec_open on grn_uvector2updspecs_data failed!");
grn_token_cursor_close(ctx, token_cursor);
rc = GRN_NO_MEMORY_AVAILABLE;
goto exit;
}
}
if (tokenizer) {
pos = token_cursor->pos;
} else {
pos = i;
}
if (grn_ii_updspec_add(ctx, *u, pos, 0)) {
GRN_LOG(ctx,
GRN_LOG_ALERT,
"grn_ii_updspec_add on grn_uvector2updspecs failed!");
grn_token_cursor_close(ctx, token_cursor);
rc = GRN_NO_MEMORY_AVAILABLE;
goto exit;
}
}
}
grn_token_cursor_close(ctx, token_cursor);
}
exit:
GRN_OBJ_FIN(ctx, &element_buffer);
GRN_OBJ_FIN(ctx, &casted_element_buffer);
return rc;
}
static grn_rc
grn_uvector2updspecs_id(grn_ctx *ctx,
grn_ii *ii,
grn_id rid,
unsigned int section,
grn_obj *in,
grn_obj *out)
{
int i, n;
grn_ii_updspec **u;
grn_hash *h = (grn_hash *)out;
n = grn_vector_size(ctx, in);
for (i = 0; i < n; i++) {
grn_id id;
uint32_t weight;
id = grn_uvector_get_element(ctx, in, i, &weight);
if (!grn_hash_add(ctx, h, &id, sizeof(grn_id), (void **)&u, NULL)) {
break;
}
if (!*u) {
if (!(*u = grn_ii_updspec_open(ctx, rid, section))) {
GRN_LOG(ctx,
GRN_LOG_ALERT,
"grn_ii_updspec_open on grn_ii_update failed!");
return GRN_NO_MEMORY_AVAILABLE;
}
}
if (grn_ii_updspec_add(ctx, *u, i, weight)) {
GRN_LOG(ctx,
GRN_LOG_ALERT,
"grn_ii_updspec_add on grn_ii_update failed!");
return GRN_NO_MEMORY_AVAILABLE;
}
}
return GRN_SUCCESS;
}
static grn_rc
grn_uvector2updspecs(grn_ctx *ctx,
grn_ii *ii,
grn_id rid,
unsigned int section,
grn_obj *in,
grn_obj *out,
grn_tokenize_mode mode,
grn_obj *posting)
{
if (grn_type_id_is_builtin(ctx, in->header.domain)) {
return grn_uvector2updspecs_data(ctx,
ii,
rid,
section,
in,
out,
mode,
posting);
} else {
return grn_uvector2updspecs_id(ctx, ii, rid, section, in, out);
}
}
static inline grn_rc
grn_ii_column_update_internal(grn_ctx *ctx,
grn_ii *ii,
grn_id rid,
unsigned int original_section,
unsigned int section,
grn_obj *oldvalue,
grn_obj *newvalue,
grn_obj *posting)
{
grn_id *tp;
bool do_grn_ii_updspec_cmp = true;
grn_ii_updspec **u, **un;
grn_obj *old_value = oldvalue;
grn_obj *new_value = newvalue;
grn_obj old_buffer;
grn_obj new_buffer;
grn_obj buf, *post = NULL;
if (!ii) {
ERR(GRN_INVALID_ARGUMENT, "[ii][column][update] ii is NULL");
return ctx->rc;
}
if (!ii->lexicon) {
ERR(GRN_INVALID_ARGUMENT, "[ii][column][update] lexicon is NULL");
return ctx->rc;
}
if (rid == GRN_ID_NIL) {
GRN_DEFINE_NAME(ii);
ERR(GRN_INVALID_ARGUMENT,
"[ii][column][update] record ID is nil: <%.*s>",
name_size,
name);
return ctx->rc;
}
if (old_value && old_value->header.type == GRN_VOID) {
old_value = NULL;
}
if (new_value && new_value->header.type == GRN_VOID) {
new_value = NULL;
}
if (old_value || new_value) {
bool is_text_vector_index = true;
if (old_value) {
if (!(old_value->header.type == GRN_VECTOR &&
grn_type_id_is_text_family(ctx, old_value->header.domain))) {
is_text_vector_index = false;
}
}
if (new_value) {
if (!(new_value->header.type == GRN_VECTOR &&
grn_type_id_is_text_family(ctx, new_value->header.domain))) {
is_text_vector_index = false;
}
}
if (is_text_vector_index) {
grn_obj *tokenizer;
grn_table_get_info(ctx, ii->lexicon, NULL, NULL, &tokenizer, NULL, NULL);
if (tokenizer) {
grn_obj old_elem, new_elem;
unsigned int i, max_n;
unsigned int old_n = 0, new_n = 0;
if (old_value) {
old_n = grn_vector_size(ctx, old_value);
GRN_OBJ_INIT(&old_elem,
GRN_BULK,
GRN_OBJ_DO_SHALLOW_COPY,
old_value->header.domain);
}
if (new_value) {
new_n = grn_vector_size(ctx, new_value);
GRN_OBJ_INIT(&new_elem,
GRN_BULK,
GRN_OBJ_DO_SHALLOW_COPY,
new_value->header.domain);
}
max_n = (old_n > new_n) ? old_n : new_n;
for (i = 0; i < max_n; i++) {
grn_rc rc;
grn_obj *old_p = NULL, *new_p = NULL;
if (i < old_n) {
const char *value;
unsigned int size;
size =
grn_vector_get_element(ctx, old_value, i, &value, NULL, NULL);
GRN_TEXT_SET_REF(&old_elem, value, size);
old_p = &old_elem;
}
if (i < new_n) {
const char *value;
unsigned int size;
size =
grn_vector_get_element(ctx, new_value, i, &value, NULL, NULL);
GRN_TEXT_SET_REF(&new_elem, value, size);
new_p = &new_elem;
}
rc = grn_ii_column_update_internal(ctx,
ii,
rid,
original_section,
section + i,
old_p,
new_p,
posting);
if (rc != GRN_SUCCESS) {
break;
}
}
if (old_value) {
GRN_OBJ_FIN(ctx, &old_elem);
}
if (new_value) {
GRN_OBJ_FIN(ctx, &new_elem);
}
return ctx->rc;
}
}
}
if (posting) {
GRN_RECORD_INIT(&buf, GRN_OBJ_VECTOR, grn_obj_id(ctx, ii->lexicon));
post = &buf;
}
if (grn_io_lock(ctx, ii->seg, grn_lock_timeout)) {
return ctx->rc;
}
if (new_value) {
unsigned char type = (ii->obj.header.domain == new_value->header.domain)
? GRN_UVECTOR
: new_value->header.type;
switch (type) {
case GRN_BULK:
{
if (grn_bulk_is_zero(ctx, new_value)) {
do_grn_ii_updspec_cmp = false;
}
grn_obj *new_value_before = new_value;
GRN_OBJ_INIT(&new_buffer,
GRN_VECTOR,
GRN_OBJ_DO_SHALLOW_COPY,
new_value->header.domain);
new_buffer.u.v.body = new_value;
new_value = &new_buffer;
grn_vector_delimit(ctx, new_value, 0, new_value_before->header.domain);
if (new_value_before != newvalue) {
grn_obj_close(ctx, new_value_before);
}
}
/* fallthru */
case GRN_VECTOR:
{
grn_obj *new_value_before = new_value;
new_value = (grn_obj *)grn_hash_create(ctx,
NULL,
sizeof(grn_id),
sizeof(grn_ii_updspec *),
GRN_HASH_TINY);
if (!new_value) {
GRN_DEFINE_NAME(ii);
MERR(
"[ii][column][update][new][vector] failed to create a hash table: "
"<%.*s>: ",
name_size,
name);
} else {
grn_vector2updspecs(ctx,
ii,
rid,
original_section,
section,
new_value_before,
new_value,
GRN_TOKEN_ADD,
post);
}
if (new_value_before != newvalue) {
grn_obj_close(ctx, new_value_before);
}
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
}
break;
case GRN_UVECTOR:
{
grn_obj *new_value_before = new_value;
new_value = (grn_obj *)grn_hash_create(ctx,
NULL,
sizeof(grn_id),
sizeof(grn_ii_updspec *),
GRN_HASH_TINY);
if (!new_value) {
GRN_DEFINE_NAME(ii);
MERR(
"[ii][column][update][new][uvector] failed to create a hash table: "
"<%.*s>: ",
name_size,
name);
} else {
if (new_value_before->header.type == GRN_UVECTOR) {
grn_uvector2updspecs(ctx,
ii,
rid,
section,
new_value_before,
new_value,
GRN_TOKEN_ADD,
post);
} else {
grn_obj uvector;
uint32_t weight = 0;
GRN_VALUE_FIX_SIZE_INIT(&uvector,
GRN_OBJ_VECTOR,
new_value_before->header.domain);
if (new_value_before->header.impl_flags & GRN_OBJ_WITH_WEIGHT) {
uvector.header.impl_flags |= GRN_OBJ_WITH_WEIGHT;
}
grn_uvector_add_element(ctx,
&uvector,
GRN_RECORD_VALUE(new_value_before),
weight);
grn_uvector2updspecs(ctx,
ii,
rid,
section,
&uvector,
new_value,
GRN_TOKEN_ADD,
post);
GRN_OBJ_FIN(ctx, &uvector);
}
}
if (new_value_before != newvalue) {
grn_obj_close(ctx, new_value_before);
}
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
}
break;
case GRN_TABLE_HASH_KEY:
break;
default:
{
GRN_DEFINE_NAME(ii);
ERR(GRN_INVALID_ARGUMENT,
"[ii][column][update][new] invalid object: "
"<%.*s>: "
"<%s>(%#x)",
name_size,
name,
grn_obj_type_to_string(type),
type);
}
goto exit;
}
}
if (posting) {
grn_ii_updspec *u_;
uint32_t offset = 0;
grn_id tid_ = 0, gap, tid, *tpe;
grn_table_sort_optarg arg = {
GRN_TABLE_SORT_ASC | GRN_TABLE_SORT_AS_NUMBER |
GRN_TABLE_SORT_AS_UNSIGNED,
NULL,
NULL,
NULL,
0,
};
grn_array *sorted = grn_array_create(ctx, NULL, sizeof(grn_id), 0);
grn_hash_sort(ctx, (grn_hash *)new_value, -1, sorted, &arg);
GRN_TEXT_PUT(ctx,
posting,
((grn_hash *)new_value)->n_entries,
sizeof(uint32_t));
GRN_ARRAY_EACH(ctx, sorted, 0, 0, id, &tp, {
grn_hash_get_key(ctx, (grn_hash *)new_value, *tp, &tid, sizeof(grn_id));
gap = tid - tid_;
GRN_TEXT_PUT(ctx, posting, &gap, sizeof(grn_id));
tid_ = tid;
});
GRN_ARRAY_EACH(ctx, sorted, 0, 0, id, &tp, {
grn_hash_get_value(ctx, (grn_hash *)new_value, *tp, &u_);
u_->offset = offset++;
GRN_TEXT_PUT(ctx, posting, &u_->tf, sizeof(int32_t));
});
tpe = (grn_id *)GRN_BULK_CURR(post);
for (tp = (grn_id *)GRN_BULK_HEAD(post); tp < tpe; tp++) {
grn_hash_get(ctx,
(grn_hash *)new_value,
(void *)tp,
sizeof(grn_id),
(void **)&u);
GRN_TEXT_PUT(ctx, posting, &(*u)->offset, sizeof(int32_t));
}
GRN_OBJ_FIN(ctx, post);
grn_array_close(ctx, sorted);
}
if (old_value) {
unsigned char type = (ii->obj.header.domain == old_value->header.domain)
? GRN_UVECTOR
: old_value->header.type;
switch (type) {
case GRN_BULK:
{
// const char *str = GRN_BULK_HEAD(old);
// unsigned int str_len = GRN_BULK_VSIZE(old);
grn_obj *old_value_before = old_value;
GRN_OBJ_INIT(&old_buffer,
GRN_VECTOR,
GRN_OBJ_DO_SHALLOW_COPY,
old_value->header.domain);
old_buffer.u.v.body = old_value;
old_value = &old_buffer;
grn_vector_delimit(ctx, old_value, 0, old_value_before->header.domain);
if (old_value_before != oldvalue) {
grn_obj_close(ctx, old_value_before);
}
}
/* fallthru */
case GRN_VECTOR:
{
grn_obj *old_value_keep = old_value;
old_value = (grn_obj *)grn_hash_create(ctx,
NULL,
sizeof(grn_id),
sizeof(grn_ii_updspec *),
GRN_HASH_TINY);
if (!old_value) {
GRN_DEFINE_NAME(ii);
MERR(
"[ii][column][update][old][vector] failed to create a hash table: "
"<%.*s>: ",
name_size,
name);
} else {
grn_vector2updspecs(ctx,
ii,
rid,
original_section,
section,
old_value_keep,
old_value,
GRN_TOKEN_DEL,
NULL);
}
if (old_value_keep != oldvalue) {
grn_obj_close(ctx, old_value_keep);
}
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
}
break;
case GRN_UVECTOR:
{
grn_obj *old_value_keep = old_value;
old_value = (grn_obj *)grn_hash_create(ctx,
NULL,
sizeof(grn_id),
sizeof(grn_ii_updspec *),
GRN_HASH_TINY);
if (!old_value) {
GRN_DEFINE_NAME(ii);
MERR(
"[ii][column][update][old][uvector] failed to create a hash table: "
"<%.*s>: ",
name_size,
name);
} else {
if (old_value_keep->header.type == GRN_UVECTOR) {
grn_uvector2updspecs(ctx,
ii,
rid,
section,
old_value_keep,
old_value,
GRN_TOKEN_DEL,
NULL);
} else {
grn_obj uvector;
uint32_t weight = 0;
GRN_VALUE_FIX_SIZE_INIT(&uvector,
GRN_OBJ_VECTOR,
old_value_keep->header.domain);
if (old_value_keep->header.impl_flags & GRN_OBJ_WITH_WEIGHT) {
uvector.header.impl_flags |= GRN_OBJ_WITH_WEIGHT;
}
grn_uvector_add_element(ctx,
&uvector,
GRN_RECORD_VALUE(old_value_keep),
weight);
grn_uvector2updspecs(ctx,
ii,
rid,
section,
&uvector,
old_value,
GRN_TOKEN_DEL,
NULL);
GRN_OBJ_FIN(ctx, &uvector);
}
}
if (old_value_keep != oldvalue) {
grn_obj_close(ctx, old_value_keep);
}
if (ctx->rc != GRN_SUCCESS) {
goto exit;
}
}
break;
case GRN_TABLE_HASH_KEY:
break;
default:
{
GRN_DEFINE_NAME(ii);
ERR(GRN_INVALID_ARGUMENT,
"[ii][column][update][old] invalid object: "
"<%.*s>: "
"<%s>(%#x)",
name_size,
name,
grn_obj_type_to_string(type),
type);
}
goto exit;
}
}
if (old_value) {
grn_id eid;
grn_hash *o = (grn_hash *)old_value;
grn_hash *n = (grn_hash *)new_value;
GRN_HASH_EACH(ctx, o, id, &tp, NULL, &u, {
if (n && (eid = grn_hash_get(ctx, n, tp, sizeof(grn_id), (void **)&un))) {
if (do_grn_ii_updspec_cmp && !grn_ii_updspec_cmp(*u, *un)) {
grn_ii_updspec_close(ctx, *un);
grn_hash_delete_by_id(ctx, n, eid, NULL);
}
} else {
grn_ii_delete_one(ctx, ii, *tp, *u, n);
}
if (ctx->rc != GRN_SUCCESS) {
break;
}
});
}
if (new_value) {
grn_hash *n = (grn_hash *)new_value;
GRN_HASH_EACH(ctx, n, id, &tp, NULL, &u, {
grn_ii_update_one(ctx, ii, *tp, *u, n);
if (ctx->rc != GRN_SUCCESS) {
break;
}
});
} else {
if (!section) {
/* todo: delete key when all sections deleted */
}
}
exit:
grn_io_unlock(ctx, ii->seg);
if (old_value && old_value->header.type == GRN_TABLE_HASH_KEY) {
grn_hash *o = (grn_hash *)old_value;
GRN_HASH_EACH(ctx, o, id, &tp, NULL, &u, {
grn_ii_updspec_close(ctx, *u);
});
if (old_value != oldvalue) {
grn_obj_close(ctx, old_value);
}
}
if (new_value && new_value->header.type == GRN_TABLE_HASH_KEY) {
grn_hash *n = (grn_hash *)new_value;
GRN_HASH_EACH(ctx, n, id, &tp, NULL, &u, {
grn_ii_updspec_close(ctx, *u);
});
if (new_value != newvalue) {
grn_obj_close(ctx, new_value);
}
}
return ctx->rc;
}
extern "C" grn_rc
grn_ii_column_update(grn_ctx *ctx,
grn_ii *ii,
grn_id rid,
unsigned int section,
grn_obj *oldvalue,
grn_obj *newvalue,
grn_obj *posting)
{
GRN_SLOW_LOG_PUSH(ctx, GRN_LOG_DEBUG);
grn_rc rc = grn_ii_column_update_internal(ctx,
ii,
rid,
section,
section,
oldvalue,
newvalue,
posting);
GRN_SLOW_LOG_POP_BEGIN(ctx, GRN_LOG_DEBUG, elapsed_time)
{
GRN_DEFINE_NAME(ii);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][column][update][slow][%f] "
"<%.*s>: "
"record:<%u>:<%u>",
elapsed_time,
name_size,
name,
rid,
section);
}
GRN_SLOW_LOG_POP_END(ctx);
return rc;
}
/* token_info */
typedef struct {
/* All token cursors of the token info. If the target token is
* expanded (EX_* except EX_NONE), token cursors of multiple tokens
* may be included. */
cursor_heap *cursors;
/* The number of tokens from the first token in the given query or a
* phrase in the given query. */
uint32_t offset;
/* The number of tokens from the first token in the input. */
int32_t pos;
/* The estimated size (see grn_ii_estimate_size() for estimated
* size) of all tokens including this token_info. */
uint32_t size;
/* The number of the token info. If the target token is expanded
* (EX_* except EX_NONE), there may be multiple tokens in a token
* info. */
int ntoken;
/* The current posting of the token info. */
grn_posting *p;
/* The ID of phrase group that the token info belongs.
*
* This is used only for GRN_OP_*NEAR_PHRASE_PRODUCT. */
uint32_t phrase_group_id;
/* The ID of phrase that the token info belongs.
*
* This is used only for GRN_OP_*NEAR_PHRASE*. */
uint32_t phrase_id;
/* The number of tokens in the phrase that the token info
* belongs. This counts not used tokens. For example, "abc" with
* TokenNgram is tokenized to "ab", "bc" and "c" but "c" isn't
* used. Because we can find "abc" only with "ab" and
* "bc". n_tokens_in_phrase is 3 because it counts
* "c". n_used_tokens_in_phrase is 2 because it doesn't count "c".
*
* This is used only for GRN_OP_*NEAR_PHRASE*. */
uint32_t n_tokens_in_phrase;
/* The number of used tokens in the phrase that the token info
* belongs. See also the above n_tokens_in_phrase document.
*
* This is used only for GRN_OP_*NEAR_PHRASE*. */
uint32_t n_used_tokens_in_phrase;
/* Whether the token info must be the last token or not. If the
* token info is appeared not at the last, the query isn't matched.
*
* This is used only for GRN_OP_*NEAR_PHRASE*. */
bool must_last;
} token_info;
extern "C" void
grn_inspect_token_info(grn_ctx *ctx, grn_obj *buffer, token_info *info)
{
grn_text_printf(ctx, buffer, "#<token_info:\n");
grn_text_printf(ctx, buffer, " cursors: ");
grn_obj cursor_heap_inspected;
GRN_TEXT_INIT(&cursor_heap_inspected, 0);
grn_inspect_cursor_heap(ctx, &cursor_heap_inspected, info->cursors);
grn_inspect_indent(ctx, buffer, &cursor_heap_inspected, " ");
GRN_OBJ_FIN(ctx, &cursor_heap_inspected);
grn_text_printf(ctx, buffer, "\n");
grn_text_printf(ctx, buffer, " offset:%u\n", info->offset);
grn_text_printf(ctx, buffer, " pos:%d\n", info->pos);
grn_text_printf(ctx, buffer, " size:%u\n", info->size);
grn_text_printf(ctx, buffer, " n_tokens:%d\n", info->ntoken);
grn_text_printf(
ctx,
buffer,
" posting:#<posting: rid:%u sid:%u pos:%u tf:%u weight:%u>\n",
info->p->rid,
info->p->sid,
info->p->pos,
info->p->tf,
info->p->weight);
grn_text_printf(ctx, buffer, " phrase_group_id:%u\n", info->phrase_group_id);
grn_text_printf(ctx, buffer, " phrase_id:%u\n", info->phrase_id);
grn_text_printf(ctx,
buffer,
" n_tokens_in_phrase:%u\n",
info->n_tokens_in_phrase);
grn_text_printf(ctx,
buffer,
" n_used_tokens_in_phrase:%u\n",
info->n_used_tokens_in_phrase);
grn_text_printf(ctx,
buffer,
" must_last:%s\n",
info->must_last ? "true" : "false");
grn_text_printf(ctx, buffer, ">");
}
extern "C" void
grn_p_token_info(grn_ctx *ctx, token_info *info)
{
grn_obj inspected;
GRN_TEXT_INIT(&inspected, 0);
grn_inspect_token_info(ctx, &inspected, info);
printf("%.*s\n", (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected));
GRN_OBJ_FIN(ctx, &inspected);
}
extern "C" void
grn_inspect_token_infos(grn_ctx *ctx,
grn_obj *buffer,
token_info **infos,
uint32_t n)
{
uint32_t i;
for (i = 0; i < n; i++) {
if (i > 0) {
GRN_TEXT_PUTC(ctx, buffer, '\n');
}
grn_text_printf(ctx, buffer, "[%u]\n", i);
grn_inspect_token_info(ctx, buffer, infos[i]);
}
}
extern "C" void
grn_p_token_infos(grn_ctx *ctx, token_info **infos, uint32_t n)
{
grn_obj inspected;
GRN_TEXT_INIT(&inspected, 0);
grn_inspect_token_infos(ctx, &inspected, infos, n);
printf("%.*s\n", (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected));
GRN_OBJ_FIN(ctx, &inspected);
}
#define EX_NONE 0
#define EX_PREFIX 1
#define EX_SUFFIX 2
#define EX_BOTH 3
#define EX_FUZZY 4
struct _btr_node {
struct _btr_node *car;
struct _btr_node *cdr;
token_info *ti;
};
typedef struct _btr_node btr_node;
typedef struct {
int n;
token_info *min;
token_info *max;
btr_node *root;
btr_node *nodes;
uint32_t n_must_lasts;
} btr;
typedef enum {
GRN_WV_NONE = 0,
GRN_WV_STATIC,
GRN_WV_DYNAMIC,
GRN_WV_CONSTANT
} grn_wv_mode;
typedef struct {
uint32_t n_phrases;
btr *btree;
} phrase_group;
typedef struct {
grn_ii *ii;
grn_obj *lexicon;
const char *query;
unsigned int query_len;
grn_hash *result_set;
grn_operator op;
grn_select_optarg *optarg;
grn_operator mode;
grn_wv_mode wv_mode;
grn_scorer_score_func *score_func;
grn_scorer_matched_record record;
grn_id previous_min;
grn_id current_min;
bool set_min_enable_for_and_query;
grn_fuzzy_search_optarg *fuzzy_search_optarg;
grn_obj *query_options;
bool only_skip_token;
token_info **token_infos;
uint32_t n_token_infos;
uint32_t n_phrase_groups;
phrase_group *phrase_groups;
uint32_t n_phrases;
token_info *current_token_info;
btr *bt;
int max_interval;
int additional_last_interval;
grn_obj *max_element_intervals;
int min_interval;
btr *check_element_intervals_btree;
int32_t pos;
grn_id rid;
uint32_t sid;
grn_id next_rid;
uint32_t next_sid;
bool may_match;
int32_t start_pos;
int32_t end_pos;
int32_t total_score;
uint32_t n_occurrences;
} grn_ii_select_data;
static inline grn_token_cursor *
grn_ii_select_data_open_token_cursor(grn_ctx *ctx,
grn_ii_select_data *data,
const char *text,
size_t text_length,
grn_tokenize_mode mode)
{
uint32_t token_flags = GRN_TOKEN_CURSOR_ENABLE_TOKENIZED_DELIMITER;
grn_token_cursor *token_cursor = grn_token_cursor_open(ctx,
data->lexicon,
text,
text_length,
mode,
token_flags);
if (!token_cursor) {
return NULL;
}
if (data->query_options) {
grn_token_cursor_set_query_options(ctx, token_cursor, data->query_options);
if (ctx->rc != GRN_SUCCESS) {
grn_token_cursor_close(ctx, token_cursor);
return NULL;
}
}
if (data->optarg->query_domain != GRN_ID_NIL) {
grn_token_cursor_set_query_domain(ctx,
token_cursor,
data->optarg->query_domain);
}
return token_cursor;
}
static inline void
token_info_expand_both(grn_ctx *ctx,
grn_ii_select_data *data,
const char *key,
unsigned int key_size,
token_info *ti)
{
int s = 0;
grn_hash *h, *g;
uint32_t *offset2;
grn_hash_cursor *c;
grn_id *tp, *tq;
if ((h = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, 0))) {
grn_operator mode = GRN_OP_PREFIX;
if (data->lexicon->header.type == GRN_TABLE_HASH_KEY) {
mode = GRN_OP_EXACT;
}
/* TODO: use cursor like EX_PREFIX */
grn_table_search(ctx,
data->lexicon,
key,
key_size,
mode,
(grn_obj *)h,
GRN_OP_OR);
if (GRN_HASH_SIZE(h)) {
if ((ti->cursors = cursor_heap_open(ctx, GRN_HASH_SIZE(h) + 256))) {
if ((c = grn_hash_cursor_open(ctx, h, NULL, 0, NULL, 0, 0, -1, 0))) {
uint32_t key2_size;
const char *key2;
while (grn_hash_cursor_next(ctx, c)) {
grn_hash_cursor_get_key(ctx, c, (void **)&tp);
key2 = _grn_table_key(ctx, data->lexicon, *tp, &key2_size);
if (!key2) {
break;
}
if ((data->lexicon->header.type != GRN_TABLE_PAT_KEY) ||
!(data->lexicon->header.flags & GRN_OBJ_KEY_WITH_SIS) ||
key2_size <= 2) { // todo: refine
if ((s = grn_ii_estimate_size(ctx, data->ii, *tp))) {
cursor_heap_push(ctx,
ti->cursors,
data->ii,
*tp,
0,
0,
data->previous_min);
ti->ntoken++;
ti->size += s;
}
} else {
if ((g = grn_hash_create(ctx,
NULL,
sizeof(grn_id),
0,
GRN_HASH_TINY))) {
/* TODO: use cursor like EX_PREFIX */
grn_pat_suffix_search(ctx,
(grn_pat *)(data->lexicon),
key2,
key2_size,
g);
GRN_HASH_EACH(ctx, g, id, &tq, NULL, &offset2, {
if ((s = grn_ii_estimate_size(ctx, data->ii, *tq))) {
cursor_heap_push(ctx,
ti->cursors,
data->ii,
*tq,
/* *offset2 */ 0,
0,
data->previous_min);
ti->ntoken++;
ti->size += s;
}
});
grn_hash_close(ctx, g);
}
}
}
grn_hash_cursor_close(ctx, c);
}
}
}
grn_hash_close(ctx, h);
}
}
static inline grn_rc
token_info_close(grn_ctx *ctx, token_info *ti)
{
cursor_heap_close(ctx, ti->cursors);
GRN_FREE(ti);
return GRN_SUCCESS;
}
/*
* If tid is GRN_ID_NIL, caller must specify valid key and key_size
* (not NULL and 0). tid may be retrieved by grn_table_get(ctx,
* data->lexicon, key, key_size) if tid is needed and tid is GRN_ID_NIL.
*
* If key is NULL, caller must specify valid tid (not GRN_ID_NIL). key
* and key_size may be retrieved by _grn_table_key(ctx, data->lexicon,
* tid, &key_size) if key is needed and key is NULL.
*
* Note that we should not normalize the input multiple times. Some
* normalizers such as NormalizerTable aren't idempotent. If we
* normalize the input multipe times with a non-idempotent normalizer,
* the tokenized result is broken.
*/
static inline token_info *
token_info_open(grn_ctx *ctx,
grn_ii_select_data *data,
grn_id tid,
const char *key,
unsigned int key_size,
uint32_t offset,
int mode)
{
uint32_t s = 0;
grn_hash *h;
grn_id *tp;
if (tid == GRN_ID_NIL && !key) {
return NULL;
}
auto ti = static_cast<token_info *>(GRN_CALLOC(sizeof(token_info)));
if (!ti) {
return NULL;
}
ti->cursors = NULL;
ti->size = 0;
ti->ntoken = 0;
ti->offset = offset;
ti->phrase_id = 0;
ti->must_last = false;
switch (mode) {
case EX_BOTH:
if (!key) {
key = _grn_table_key(ctx, data->lexicon, tid, &key_size);
}
token_info_expand_both(ctx, data, key, key_size, ti);
break;
case EX_NONE:
if (tid == GRN_ID_NIL) {
tid = grn_table_get(ctx, data->lexicon, key, key_size);
}
if (tid != GRN_ID_NIL && (s = grn_ii_estimate_size(ctx, data->ii, tid)) &&
(ti->cursors = cursor_heap_open(ctx, 1))) {
cursor_heap_push(ctx,
ti->cursors,
data->ii,
tid,
0,
0,
data->previous_min);
ti->ntoken++;
ti->size = s;
}
break;
case EX_PREFIX:
if (data->lexicon->header.type == GRN_TABLE_HASH_KEY) {
if (tid == GRN_ID_NIL) {
tid = grn_table_get(ctx, data->lexicon, key, key_size);
}
if (tid != GRN_ID_NIL) {
ti->cursors = cursor_heap_open(ctx, 1);
/* TODO: report error on !ti->cursors */
if (ti->cursors) {
s = grn_ii_estimate_size(ctx, data->ii, tid);
if (s > 0) {
cursor_heap_push(ctx,
ti->cursors,
data->ii,
tid,
0,
0,
data->previous_min);
ti->ntoken++;
ti->size += s;
}
}
}
} else {
if (!key) {
key = _grn_table_key(ctx, data->lexicon, tid, &key_size);
}
GRN_TABLE_EACH_BEGIN_MIN(ctx,
data->lexicon,
cursor,
id,
key,
key_size,
GRN_CURSOR_PREFIX)
{
if (!ti->cursors) {
/* This is heuristic. 0.1% tokens may be matched. Must be
* greater than 0. */
unsigned int initial_capacity =
(grn_table_size(ctx, data->lexicon) / 1000) + 1;
ti->cursors = cursor_heap_open(ctx, initial_capacity);
if (!ti->cursors) {
/* TODO: report error */
break;
}
}
s = grn_ii_estimate_size(ctx, data->ii, id);
if (s > 0) {
cursor_heap_push(ctx,
ti->cursors,
data->ii,
id,
0,
0,
data->previous_min);
ti->ntoken++;
ti->size += s;
}
}
GRN_TABLE_EACH_END(ctx, cursor);
}
break;
case EX_SUFFIX:
if ((h = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, 0))) {
grn_operator mode = GRN_OP_SUFFIX;
if (data->lexicon->header.type == GRN_TABLE_HASH_KEY) {
mode = GRN_OP_EXACT;
}
if (!key) {
key = _grn_table_key(ctx, data->lexicon, tid, &key_size);
}
/* TODO: use cursor like EX_PREFIX */
grn_table_search(ctx,
data->lexicon,
key,
key_size,
mode,
(grn_obj *)h,
GRN_OP_OR);
if (GRN_HASH_SIZE(h)) {
if ((ti->cursors = cursor_heap_open(ctx, GRN_HASH_SIZE(h)))) {
uint32_t *offset2;
GRN_HASH_EACH(ctx, h, id, &tp, NULL, &offset2, {
if ((s = grn_ii_estimate_size(ctx, data->ii, *tp))) {
cursor_heap_push(ctx,
ti->cursors,
data->ii,
*tp,
/* *offset2 */ 0,
0,
data->previous_min);
ti->ntoken++;
ti->size += s;
}
});
}
}
grn_hash_close(ctx, h);
}
break;
case EX_FUZZY:
if ((h = (grn_hash *)grn_table_create(ctx,
NULL,
0,
NULL,
GRN_OBJ_TABLE_HASH_KEY |
GRN_OBJ_WITH_SUBREC,
grn_ctx_at(ctx, GRN_DB_UINT32),
NULL))) {
if (!key) {
key = _grn_table_key(ctx, data->lexicon, tid, &key_size);
}
grn_ctx_trace_log_emit_string(ctx,
"ii.select.fuzzy.input",
key,
key_size);
/* TODO: use cursor like EX_PREFIX */
grn_table_fuzzy_search(ctx,
data->lexicon,
key,
key_size,
data->fuzzy_search_optarg,
(grn_obj *)h,
GRN_OP_OR);
if (GRN_HASH_SIZE(h)) {
if ((ti->cursors = cursor_heap_open(ctx, GRN_HASH_SIZE(h)))) {
grn_rset_recinfo *ri;
GRN_HASH_EACH(ctx, h, id, &tp, NULL, (void **)&ri, {
if ((s = grn_ii_estimate_size(ctx, data->ii, *tp))) {
cursor_heap_push(ctx,
ti->cursors,
data->ii,
*tp,
0,
(int)(ri->score - 1),
data->previous_min);
ti->ntoken++;
ti->size += s;
grn_ctx_trace_log_emit_record_key(ctx,
"ii.select.fuzzy.input.actual",
data->ii->lexicon,
*tp);
}
});
}
}
grn_obj_close(ctx, (grn_obj *)h);
}
break;
}
if (ctx->rc != GRN_SUCCESS) {
token_info_close(ctx, ti);
return NULL;
}
if (cursor_heap_push2(ti->cursors)) {
token_info_close(ctx, ti);
return NULL;
}
{
grn_ii_cursor *ic;
if (ti->cursors && (ic = cursor_heap_min(ti->cursors))) {
grn_posting *p = ic->post;
ti->pos = p->pos - ti->offset;
ti->p = p;
} else {
token_info_close(ctx, ti);
ti = NULL;
}
}
return ti;
}
static inline grn_rc
token_info_skip(grn_ctx *ctx, token_info *ti, uint32_t rid, uint32_t sid)
{
grn_ii_cursor *c;
grn_posting *p;
for (;;) {
if (!(c = cursor_heap_min(ti->cursors))) {
return GRN_END_OF_DATA;
}
p = c->post;
if (p->rid > rid || (p->rid == rid && p->sid >= sid)) {
break;
}
cursor_heap_pop(ctx, ti->cursors, rid);
}
ti->pos = p->pos - ti->offset;
ti->p = p;
return GRN_SUCCESS;
}
static inline grn_rc
token_info_skip_pos(
grn_ctx *ctx, token_info *ti, uint32_t rid, uint32_t sid, int32_t pos)
{
grn_ii_cursor *c;
grn_posting *p;
pos += ti->offset;
for (;;) {
if (!(c = cursor_heap_min(ti->cursors))) {
return GRN_END_OF_DATA;
}
p = c->post;
if (p->rid != rid || p->sid != sid || (int32_t)(p->pos) >= pos) {
break;
}
cursor_heap_pop_pos(ctx, ti->cursors);
}
ti->pos = p->pos - ti->offset;
ti->p = p;
return GRN_SUCCESS;
}
static inline int
token_compare(const void *a, const void *b)
{
const token_info *t1 = *((token_info **)a), *t2 = *((token_info **)b);
if (t1->phrase_group_id == t2->phrase_group_id) {
if (t1->phrase_id == t2->phrase_id) {
return t1->size - t2->size;
} else {
return t1->phrase_id - t2->phrase_id;
}
} else {
return t1->phrase_group_id - t2->phrase_group_id;
}
}
#define TOKEN_CANDIDATE_NODE_SIZE 32
#define TOKEN_CANDIDATE_ADJACENT_MAX_SIZE 16
#define TOKEN_CANDIDATE_QUEUE_SIZE 64
#define TOKEN_CANDIDATE_SIZE 16
typedef struct {
grn_id tid;
const unsigned char *token;
uint32_t token_size;
const unsigned char *source_token;
uint32_t source_token_size;
int32_t pos;
grn_token_cursor_status status;
int ef;
uint32_t estimated_size;
uint8_t adjacent[TOKEN_CANDIDATE_ADJACENT_MAX_SIZE]; /* Index of adjacent node
from top */
uint8_t n_adjacent;
} token_candidate_node;
typedef struct {
uint32_t
*candidates; /* Standing bits indicate index of token_candidate_node */
int top;
int rear;
int size;
} token_candidate_queue;
typedef struct {
grn_ii_select_data *select_data;
token_candidate_node *nodes;
uint32_t n_nodes;
uint32_t max_estimated_size;
uint32_t selected_candidate;
} token_candidate_data;
static inline void
token_candidate_adjacent_set(grn_ctx *ctx,
grn_token_cursor *token_cursor,
token_candidate_node *top,
token_candidate_node *curr)
{
bool exists_adjacent = false;
token_candidate_node *adj;
for (adj = top; adj < curr; adj++) {
if (token_cursor->curr <= adj->token + adj->token_size) {
if (adj->n_adjacent < TOKEN_CANDIDATE_ADJACENT_MAX_SIZE) {
adj->adjacent[adj->n_adjacent] = curr - top;
adj->n_adjacent++;
exists_adjacent = true;
}
}
}
if (!exists_adjacent) {
adj = curr - 1;
if (adj->n_adjacent < TOKEN_CANDIDATE_ADJACENT_MAX_SIZE) {
adj->adjacent[adj->n_adjacent] = curr - top;
adj->n_adjacent++;
}
}
}
static inline grn_rc
token_candidate_init(grn_ctx *ctx,
token_candidate_data *data,
grn_ii_select_data *select_data,
grn_token_cursor *token_cursor,
grn_id tid,
int ef)
{
data->select_data = select_data;
size_t size = TOKEN_CANDIDATE_NODE_SIZE;
data->nodes = static_cast<token_candidate_node *>(
GRN_MALLOC(TOKEN_CANDIDATE_NODE_SIZE * sizeof(token_candidate_node)));
if (!data->nodes) {
return GRN_NO_MEMORY_AVAILABLE;
}
token_candidate_node *top = data->nodes;
token_candidate_node *curr = top;
#define TOKEN_CANDIDATE_NODE_SET() \
{ \
grn_token *token = grn_token_cursor_get_token(ctx, token_cursor); \
curr->tid = tid; \
curr->token = token_cursor->curr; \
curr->token_size = token_cursor->curr_size; \
curr->source_token_size = grn_token_get_source_length(ctx, token); \
if (curr->source_token_size > 0) { \
curr->source_token = NULL; \
} else { \
curr->source_token = \
token_cursor->orig + grn_token_get_source_offset(ctx, token); \
} \
curr->pos = token_cursor->pos; \
curr->status = token_cursor->status; \
curr->ef = ef; \
curr->estimated_size = grn_ii_estimate_size(ctx, select_data->ii, tid); \
curr->n_adjacent = 0; \
}
TOKEN_CANDIDATE_NODE_SET();
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][overlap_token_skip] tid=%u pos=%d estimated_size=%u",
curr->tid,
curr->pos,
curr->estimated_size);
data->max_estimated_size = curr->estimated_size;
curr++;
while (token_cursor->status == GRN_TOKEN_CURSOR_DOING) {
if ((size_t)(curr - top) >= size) {
auto nodes = static_cast<token_candidate_node *>(
GRN_REALLOC(data->nodes,
(curr - top + TOKEN_CANDIDATE_NODE_SIZE) *
sizeof(token_candidate_node)));
if (!nodes) {
GRN_FREE(data->nodes);
data->nodes = NULL;
return GRN_NO_MEMORY_AVAILABLE;
}
top = data->nodes;
curr = top + size;
size += TOKEN_CANDIDATE_NODE_SIZE;
}
tid = grn_token_cursor_next(ctx, token_cursor);
if (token_cursor->status != GRN_TOKEN_CURSOR_DONE_SKIP) {
grn_token *token;
token = grn_token_cursor_get_token(ctx, token_cursor);
if (grn_token_get_force_prefix_search(ctx, token)) {
ef |= EX_PREFIX;
}
TOKEN_CANDIDATE_NODE_SET();
token_candidate_adjacent_set(ctx, token_cursor, top, curr);
if (curr->estimated_size > data->max_estimated_size) {
data->max_estimated_size = curr->estimated_size;
}
curr++;
}
}
data->n_nodes = curr - top;
return GRN_SUCCESS;
#undef TOKEN_CANDIDATE_NODE_SET
}
static inline grn_rc
token_candidate_queue_init(grn_ctx *ctx, token_candidate_queue *q)
{
q->top = 0;
q->rear = 0;
q->size = TOKEN_CANDIDATE_QUEUE_SIZE;
q->candidates = static_cast<uint32_t *>(
GRN_MALLOC(TOKEN_CANDIDATE_QUEUE_SIZE * sizeof(uint32_t)));
if (!q->candidates) {
q->size = 0;
return GRN_NO_MEMORY_AVAILABLE;
}
return GRN_SUCCESS;
}
static inline grn_rc
token_candidate_enqueue(grn_ctx *ctx,
token_candidate_queue *q,
uint32_t candidate)
{
if (q->rear >= q->size) {
if (!(q->candidates = static_cast<uint32_t *>(GRN_REALLOC(
q->candidates,
(q->rear + TOKEN_CANDIDATE_QUEUE_SIZE) * sizeof(uint32_t))))) {
q->size = 0;
return GRN_NO_MEMORY_AVAILABLE;
}
q->size += TOKEN_CANDIDATE_QUEUE_SIZE;
}
*(q->candidates + q->rear) = candidate;
q->rear++;
return GRN_SUCCESS;
}
static inline grn_rc
token_candidate_dequeue(grn_ctx *ctx,
token_candidate_queue *q,
uint32_t *candidate)
{
if (q->top == q->rear) {
return GRN_END_OF_DATA;
}
*candidate = *(q->candidates + q->top);
q->top++;
return GRN_SUCCESS;
}
static inline void
token_candidate_queue_fin(grn_ctx *ctx, token_candidate_queue *q)
{
GRN_FREE(q->candidates);
}
static inline token_candidate_node *
token_candidate_last_node(grn_ctx *ctx,
token_candidate_data *data,
uint32_t candidate,
int offset)
{
grn_bit_scan_rev_result i;
GRN_BIT_SCAN_REV(candidate, i);
return data->nodes + i + offset;
}
static inline uint64_t
token_candidate_score(grn_ctx *ctx,
token_candidate_data *data,
uint32_t candidate,
int offset)
{
grn_bit_scan_rev_result i, last;
uint64_t score = 0;
GRN_BIT_SCAN_REV(candidate, last);
for (i = 0; i <= last; i++) {
if (candidate & (1 << i)) {
token_candidate_node *node = data->nodes + i + offset;
if (node->estimated_size > 0) {
score += data->max_estimated_size / node->estimated_size;
}
}
}
return score;
}
static inline grn_rc
token_candidate_select(grn_ctx *ctx,
token_candidate_data *data,
uint32_t offset,
uint32_t limit)
{
if (offset + limit > (data->n_nodes - 1)) {
limit = (data->n_nodes - 1) - offset;
}
token_candidate_queue q;
grn_rc rc = token_candidate_queue_init(ctx, &q);
if (rc != GRN_SUCCESS) {
return rc;
}
uint32_t candidate;
uint64_t max_score = 0;
uint32_t min_n_nodes = 0;
rc = token_candidate_enqueue(ctx, &q, 1);
if (rc != GRN_SUCCESS) {
goto exit;
}
while (token_candidate_dequeue(ctx, &q, &candidate) != GRN_END_OF_DATA) {
token_candidate_node *candidate_last_node =
token_candidate_last_node(ctx, data, candidate, offset);
int i;
for (i = 0; i < candidate_last_node->n_adjacent; i++) {
uint32_t adjacent;
uint32_t new_candidate;
uint32_t n_nodes = 0;
adjacent = candidate_last_node->adjacent[i] - offset;
if (adjacent > limit) {
break;
}
new_candidate = candidate | (1 << adjacent);
GET_NUM_BITS(new_candidate, n_nodes);
if (min_n_nodes > 0 && n_nodes > min_n_nodes + 1) {
goto exit;
}
rc = token_candidate_enqueue(ctx, &q, new_candidate);
if (rc != GRN_SUCCESS) {
goto exit;
}
if (adjacent == limit) {
if (min_n_nodes == 0) {
min_n_nodes = n_nodes;
}
if (min_n_nodes <= n_nodes && n_nodes <= min_n_nodes + 1) {
uint64_t score =
token_candidate_score(ctx, data, new_candidate, offset);
if (score > max_score) {
max_score = score;
data->selected_candidate = new_candidate;
}
}
}
}
}
rc = GRN_SUCCESS;
exit:
token_candidate_queue_fin(ctx, &q);
return rc;
}
static inline grn_rc
token_candidate_build(grn_ctx *ctx, token_candidate_data *data, int offset)
{
grn_rc rc = GRN_END_OF_DATA;
grn_bit_scan_rev_result last = 0;
GRN_BIT_SCAN_REV(data->selected_candidate, last);
grn_bit_scan_rev_result i;
for (i = 1; i <= last; i++) {
if (data->selected_candidate & (1 << i)) {
token_info *ti;
token_candidate_node *node = data->nodes + i + offset;
switch (node->status) {
case GRN_TOKEN_CURSOR_DOING:
ti = token_info_open(ctx,
data->select_data,
node->tid,
reinterpret_cast<const char *>(node->source_token),
node->source_token_size,
node->pos,
EX_NONE);
break;
case GRN_TOKEN_CURSOR_DONE:
if (node->tid != GRN_ID_NIL) {
ti =
token_info_open(ctx,
data->select_data,
node->tid,
reinterpret_cast<const char *>(node->source_token),
node->source_token_size,
node->pos,
node->ef & EX_PREFIX);
break;
} /* else fallthru */
default:
ti = token_info_open(ctx,
data->select_data,
node->tid,
(const char *)(node->token),
node->token_size,
node->pos,
node->ef & EX_PREFIX);
break;
}
if (!ti) {
goto exit;
}
data->select_data->token_infos[data->select_data->n_token_infos++] = ti;
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][overlap_token_skip] tid=%u pos=%d estimated_size=%u",
node->tid,
node->pos,
node->estimated_size);
}
}
rc = GRN_SUCCESS;
exit:
return rc;
}
static inline grn_rc
token_info_build_skipping_overlap(grn_ctx *ctx,
grn_ii_select_data *select_data,
grn_token_cursor *token_cursor,
grn_id tid,
int ef)
{
token_candidate_data data;
grn_rc rc =
token_candidate_init(ctx, &data, select_data, token_cursor, tid, ef);
if (rc != GRN_SUCCESS) {
return rc;
}
uint32_t offset = 0;
uint32_t limit = TOKEN_CANDIDATE_SIZE - 1;
while (offset < data.n_nodes - 1) {
data.selected_candidate = 0;
rc = token_candidate_select(ctx, &data, offset, limit);
if (rc != GRN_SUCCESS) {
goto exit;
}
rc = token_candidate_build(ctx, &data, offset);
if (rc != GRN_SUCCESS) {
goto exit;
}
offset += limit;
}
rc = GRN_SUCCESS;
exit:
if (data.nodes) {
GRN_FREE(data.nodes);
}
return rc;
}
static inline grn_rc
token_info_build_phrase(grn_ctx *ctx,
grn_ii_select_data *data,
const char *phrase,
unsigned int phrase_len)
{
token_info *ti;
grn_rc rc = GRN_END_OF_DATA;
grn_token_cursor *token_cursor =
grn_ii_select_data_open_token_cursor(ctx,
data,
phrase,
phrase_len,
GRN_TOKENIZE_GET);
if (!token_cursor) {
return ctx->rc;
}
if (data->mode == GRN_OP_UNSPLIT) {
if ((ti = token_info_open(ctx,
data,
GRN_ID_NIL,
(char *)token_cursor->orig,
token_cursor->orig_blen,
0,
EX_BOTH))) {
data->token_infos[data->n_token_infos++] = ti;
rc = GRN_SUCCESS;
}
} else {
grn_id tid;
int default_ef = EX_NONE;
grn_token *token;
switch (data->mode) {
case GRN_OP_PREFIX:
default_ef = EX_PREFIX;
break;
case GRN_OP_SUFFIX:
default_ef = EX_SUFFIX;
break;
case GRN_OP_PARTIAL:
default_ef = EX_BOTH;
break;
default:
default_ef = EX_NONE;
break;
}
tid = grn_token_cursor_next(ctx, token_cursor);
if (ctx->rc != GRN_SUCCESS) {
rc = ctx->rc;
goto exit;
}
token = grn_token_cursor_get_token(ctx, token_cursor);
const unsigned char *key = NULL;
uint32_t size = 0;
if (tid == GRN_ID_NIL) {
key = token_cursor->orig;
size = token_cursor->orig_blen;
} else {
size = grn_token_get_source_length(ctx, token);
if (size > 0) {
key = token_cursor->orig + grn_token_get_source_offset(ctx, token);
}
}
int ef = default_ef;
if (grn_token_get_force_prefix_search(ctx, token)) {
ef |= EX_PREFIX;
}
switch (token_cursor->status) {
case GRN_TOKEN_CURSOR_DOING:
ti = token_info_open(ctx,
data,
tid,
reinterpret_cast<const char *>(key),
size,
token_cursor->pos,
ef & EX_SUFFIX);
break;
case GRN_TOKEN_CURSOR_DONE:
case GRN_TOKEN_CURSOR_NOT_FOUND:
ti = token_info_open(ctx,
data,
tid,
reinterpret_cast<const char *>(key),
size,
0,
ef);
break;
case GRN_TOKEN_CURSOR_DONE_SKIP:
data->only_skip_token = true;
goto exit;
default:
goto exit;
}
if (!ti) {
goto exit;
}
data->token_infos[data->n_token_infos++] = ti;
if (grn_ii_overlap_token_skip_enable) {
rc = token_info_build_skipping_overlap(ctx, data, token_cursor, tid, ef);
goto exit;
}
while (token_cursor->status == GRN_TOKEN_CURSOR_DOING) {
grn_token *token;
tid = grn_token_cursor_next(ctx, token_cursor);
token = grn_token_cursor_get_token(ctx, token_cursor);
const unsigned char *key = NULL;
uint32_t size = 0;
if (tid == GRN_ID_NIL) {
key = token_cursor->curr;
size = token_cursor->curr_size;
} else {
size = grn_token_get_source_length(ctx, token);
if (size > 0) {
key = token_cursor->orig + grn_token_get_source_offset(ctx, token);
}
}
ef = default_ef;
if (grn_token_get_force_prefix_search(ctx, token)) {
ef |= EX_PREFIX;
}
switch (token_cursor->status) {
case GRN_TOKEN_CURSOR_DONE_SKIP:
continue;
case GRN_TOKEN_CURSOR_DOING:
ti = token_info_open(ctx,
data,
tid,
reinterpret_cast<const char *>(key),
size,
token_cursor->pos,
EX_NONE);
break;
default:
ti = token_info_open(ctx,
data,
tid,
reinterpret_cast<const char *>(key),
size,
token_cursor->pos,
ef & EX_PREFIX);
break;
}
if (!ti) {
goto exit;
}
data->token_infos[data->n_token_infos++] = ti;
}
rc = GRN_SUCCESS;
}
exit:
grn_token_cursor_close(ctx, token_cursor);
return rc;
}
static inline grn_rc
token_info_build(grn_ctx *ctx, grn_ii_select_data *data)
{
data->only_skip_token = false;
return token_info_build_phrase(ctx, data, data->query, data->query_len);
}
static inline grn_rc
token_info_build_fuzzy(grn_ctx *ctx, grn_ii_select_data *data)
{
token_info *ti;
grn_rc rc = GRN_END_OF_DATA;
if (data->fuzzy_search_optarg->flags & GRN_TABLE_FUZZY_SEARCH_SKIP_TOKENIZE) {
ti = token_info_open(ctx,
data,
GRN_ID_NIL,
data->query,
data->query_len,
1,
EX_FUZZY);
if (!ti) {
return rc;
}
data->token_infos[data->n_token_infos++] = ti;
return GRN_SUCCESS;
}
grn_token_cursor *token_cursor =
grn_ii_select_data_open_token_cursor(ctx,
data,
data->query,
data->query_len,
GRN_TOKENIZE_ONLY);
data->only_skip_token = false;
if (!token_cursor) {
return ctx->rc;
}
grn_id tid = grn_token_cursor_next(ctx, token_cursor);
switch (token_cursor->status) {
case GRN_TOKEN_CURSOR_DONE_SKIP:
data->only_skip_token = true;
goto exit;
case GRN_TOKEN_CURSOR_DOING:
case GRN_TOKEN_CURSOR_DONE:
ti = token_info_open(ctx,
data,
tid,
(const char *)token_cursor->curr,
token_cursor->curr_size,
token_cursor->pos,
EX_FUZZY);
break;
default:
ti = NULL;
break;
}
if (!ti) {
goto exit;
}
data->token_infos[data->n_token_infos++] = ti;
while (token_cursor->status == GRN_TOKEN_CURSOR_DOING) {
tid = grn_token_cursor_next(ctx, token_cursor);
const unsigned char *key = NULL;
uint32_t key_size = 0;
if (tid == GRN_ID_NIL) {
key = token_cursor->curr;
key_size = token_cursor->curr_size;
} else {
grn_token *token = grn_token_cursor_get_token(ctx, token_cursor);
key_size = grn_token_get_source_length(ctx, token);
if (key_size > 0) {
key = token_cursor->orig + grn_token_get_source_offset(ctx, token);
}
}
switch (token_cursor->status) {
case GRN_TOKEN_CURSOR_DONE_SKIP:
continue;
case GRN_TOKEN_CURSOR_DOING:
case GRN_TOKEN_CURSOR_DONE:
ti = token_info_open(ctx,
data,
tid,
reinterpret_cast<const char *>(key),
key_size,
token_cursor->pos,
EX_FUZZY);
break;
default:
break;
}
if (!ti) {
goto exit;
}
data->token_infos[data->n_token_infos++] = ti;
}
rc = GRN_SUCCESS;
exit:
grn_token_cursor_close(ctx, token_cursor);
return rc;
}
static inline bool
token_info_build_near_phrase_is_same_phrase(grn_ctx *ctx,
grn_ii_select_data *data,
uint32_t start1,
uint32_t n1,
uint32_t start2,
uint32_t n2)
{
uint32_t i1 = 0;
uint32_t i2 = 0;
while (i1 < n1 && i2 < n2) {
token_info *token_info1 = data->token_infos[start1 + i1];
token_info *token_info2 = data->token_infos[start2 + i2];
if (token_info1->offset != token_info2->offset) {
if (token_info1->offset > token_info2->offset) {
i2++;
} else {
i1++;
}
continue;
}
if (token_info1->must_last != token_info2->must_last) {
return false;
}
cursor_heap *cursors1 = token_info1->cursors;
cursor_heap *cursors2 = token_info2->cursors;
if (cursors1->n_entries != cursors2->n_entries) {
return false;
}
int j;
for (j = 0; j < cursors1->n_entries; j++) {
if (cursors1->bins[j]->id != cursors2->bins[j]->id) {
return false;
}
}
i1++;
i2++;
}
return i1 == n1 && i2 == n2;
}
static inline bool
token_info_build_near_phrase_have_same_phrase(grn_ctx *ctx,
grn_ii_select_data *data,
uint32_t n_token_infos_before,
uint32_t phrase_group_id)
{
uint32_t n_target_tokens = data->n_token_infos - n_token_infos_before;
uint32_t i = 0;
token_info *token_info;
for (i = 0; i < n_token_infos_before;
i += token_info->n_used_tokens_in_phrase) {
token_info = data->token_infos[i];
if (token_info->phrase_group_id != phrase_group_id) {
continue;
}
if (token_info_build_near_phrase_is_same_phrase(
ctx,
data,
i,
token_info->n_used_tokens_in_phrase,
n_token_infos_before,
n_target_tokens)) {
return true;
}
}
return false;
}
static inline grn_rc
token_info_build_near_phrase(grn_ctx *ctx, grn_ii_select_data *data)
{
const char *tag = "[ii][token-info][build]";
grn_rc rc = GRN_SUCCESS;
data->only_skip_token = false;
grn_obj buffer;
GRN_TEXT_INIT(&buffer, 0);
const char *current = data->query;
const char *query_end = current + data->query_len;
const char *last_char = NULL;
int last_char_len = 0;
bool in_paren = false;
uint32_t phrase_group_id = 0;
uint32_t n_phrases_in_group = 0;
bool have_no_phrase_group = false;
uint32_t phrase_id = 0;
const bool is_product_mode =
(data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT);
if (is_product_mode) {
phrase_group_id--;
}
while (current < query_end) {
int space_char_len = grn_isspace(current, ctx->encoding);
if (space_char_len > 0) {
current += space_char_len;
continue;
}
const char *phrase = NULL;
size_t phrase_len = 0;
bool use_buffer = false;
GRN_BULK_REWIND(&buffer);
bool escaping = false;
int char_len = grn_charlen(ctx, current, query_end);
if (char_len == 0) {
ERR(GRN_INVALID_ARGUMENT,
"%s invalid character: <%0x>: <%.*s>",
tag,
current[0],
(int)(data->query_len),
data->query);
goto exit;
}
if (is_product_mode) {
if (char_len == 1 && current[0] == GRN_QUERY_PARENL) {
if (in_paren) {
ERR(GRN_INVALID_ARGUMENT,
"%s parenthesis must not be nested: <%.*s|%.*s|%.*s>",
tag,
(int)(current - data->query),
data->query,
char_len,
current,
(int)((data->query + data->query_len) - current - char_len),
current + char_len);
goto exit;
}
in_paren = true;
phrase_group_id++;
n_phrases_in_group = 0;
current += char_len;
continue;
} else if (char_len == 1 && current[0] == GRN_QUERY_PARENR) {
if (!in_paren) {
ERR(GRN_INVALID_ARGUMENT,
"%s unbalanced close parenthesis is detected: <%.*s|%.*s|%.*s>",
tag,
(int)(current - data->query),
data->query,
char_len,
current,
(int)((data->query + data->query_len) - current - char_len),
current + char_len);
goto exit;
}
in_paren = false;
current += char_len;
if (n_phrases_in_group == 0) {
have_no_phrase_group = true;
}
continue;
} else {
if (!in_paren) {
ERR(GRN_INVALID_ARGUMENT,
"%s all phrases must be in parenthesis: <%.*s|%.*s|%.*s>",
tag,
(int)(current - data->query),
data->query,
char_len,
current,
(int)((data->query + data->query_len) - current - char_len),
current + char_len);
goto exit;
}
}
}
if (char_len == 1 && current[0] == GRN_QUERY_QUOTEL) {
use_buffer = true;
current += char_len;
while (current < query_end) {
char_len = grn_charlen(ctx, current, query_end);
if (char_len == 0) {
ERR(GRN_INVALID_ARGUMENT,
"%s invalid character: <%0x>: <%.*s>",
tag,
current[0],
(int)(data->query_len),
data->query);
goto exit;
}
if (escaping) {
escaping = false;
last_char = NULL;
last_char_len = 0;
} else {
if (char_len == 1) {
if (current[0] == GRN_QUERY_ESCAPE) {
escaping = true;
current += char_len;
continue;
} else if (current[0] == GRN_QUERY_QUOTER) {
current += char_len;
break;
}
}
last_char = current;
last_char_len = char_len;
}
GRN_TEXT_PUT(ctx, &buffer, current, char_len);
current += char_len;
}
phrase = GRN_TEXT_VALUE(&buffer);
phrase_len = GRN_TEXT_LEN(&buffer);
} else {
phrase = current;
while (current < query_end) {
if (!escaping) {
space_char_len = grn_isspace(current, ctx->encoding);
if (space_char_len > 0) {
break;
}
}
char_len = grn_charlen(ctx, current, query_end);
if (char_len == 0) {
ERR(GRN_INVALID_ARGUMENT,
"%s invalid character: <%0x>: <%.*s>",
tag,
current[0],
(int)(data->query_len),
data->query);
goto exit;
}
if (escaping) {
escaping = false;
last_char = NULL;
last_char_len = 0;
} else {
if (char_len == 1) {
if (current[0] == GRN_QUERY_ESCAPE) {
if (!use_buffer) {
use_buffer = true;
GRN_TEXT_PUT(ctx, &buffer, phrase, current - phrase);
}
current += char_len;
escaping = true;
continue;
} else if (current[0] == GRN_QUERY_QUOTEL) {
break;
} else if (is_product_mode && (current[0] == GRN_QUERY_PARENL ||
current[0] == GRN_QUERY_PARENR)) {
break;
}
}
last_char = current;
last_char_len = char_len;
}
if (use_buffer) {
GRN_TEXT_PUT(ctx, &buffer, current, char_len);
}
current += char_len;
}
if (use_buffer) {
phrase = GRN_TEXT_VALUE(&buffer);
phrase_len = GRN_TEXT_LEN(&buffer);
} else {
phrase_len = current - phrase;
}
}
if (phrase_len == 0) {
continue;
}
bool must_last = false;
if (!(data->mode == GRN_OP_ORDERED_NEAR_PHRASE ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT)) {
if (last_char_len == 1 && *last_char == '$') {
phrase_len -= last_char_len;
must_last = true;
}
}
uint32_t n_before = data->n_token_infos;
rc = token_info_build_phrase(ctx, data, phrase, phrase_len);
if (is_product_mode) {
if (!(rc == GRN_SUCCESS || rc == GRN_END_OF_DATA)) {
goto exit;
}
if (rc == GRN_END_OF_DATA ||
token_info_build_near_phrase_have_same_phrase(ctx,
data,
n_before,
phrase_group_id)) {
rc = GRN_SUCCESS;
uint32_t i;
for (i = n_before; i < data->n_token_infos; i++) {
token_info_close(ctx, data->token_infos[i]);
}
data->n_token_infos = n_before;
continue;
}
n_phrases_in_group++;
} else {
if (rc != GRN_SUCCESS) {
goto exit;
}
}
if (data->n_token_infos > n_before) {
data->token_infos[data->n_token_infos - 1]->must_last = must_last;
}
must_last = false;
uint32_t n_tokens_in_phrase = 0;
{
grn_token_cursor *token_cursor =
grn_ii_select_data_open_token_cursor(ctx,
data,
phrase,
phrase_len,
GRN_TOKENIZE_ONLY);
if (token_cursor) {
while (token_cursor->status == GRN_TOKEN_CURSOR_DOING) {
grn_token_cursor_next(ctx, token_cursor);
n_tokens_in_phrase++;
}
grn_token_cursor_close(ctx, token_cursor);
}
}
uint32_t i;
uint32_t n_used_tokens_in_phrase = data->n_token_infos - n_before;
for (i = n_before; i < data->n_token_infos; i++) {
data->token_infos[i]->n_tokens_in_phrase = n_tokens_in_phrase;
data->token_infos[i]->n_used_tokens_in_phrase = n_used_tokens_in_phrase;
data->token_infos[i]->phrase_group_id = phrase_group_id;
data->token_infos[i]->phrase_id = phrase_id;
}
phrase_id++;
}
if (is_product_mode) {
if (in_paren) {
ERR(GRN_INVALID_ARGUMENT, "%s close parenthesis is missing", tag);
goto exit;
}
if (have_no_phrase_group) {
rc = GRN_END_OF_DATA;
goto exit;
}
}
data->n_phrase_groups = phrase_group_id + 1;
data->n_phrases = phrase_id;
if (data->mode == GRN_OP_ORDERED_NEAR_PHRASE) {
if (data->n_token_infos > 0) {
data->token_infos[data->n_token_infos - 1]->must_last = true;
}
}
exit:
if (rc == GRN_SUCCESS && ctx->rc != GRN_SUCCESS) {
rc = ctx->rc;
}
GRN_OBJ_FIN(ctx, &buffer);
return rc;
}
static void
token_info_clear_offset(token_info **tis, uint32_t n)
{
token_info **tie;
for (tie = tis + n; tis < tie; tis++) {
(*tis)->offset = 0;
}
}
static inline void
bt_zap(btr *bt)
{
bt->n = 0;
bt->min = NULL;
bt->max = NULL;
bt->root = NULL;
bt->n_must_lasts = 0;
}
static inline btr *
bt_open(grn_ctx *ctx, int size)
{
auto bt = static_cast<btr *>(GRN_CALLOC(sizeof(btr)));
if (bt) {
bt_zap(bt);
bt->nodes = static_cast<btr_node *>(GRN_MALLOC(sizeof(btr_node) * size));
if (!bt->nodes) {
GRN_FREE(bt);
bt = NULL;
}
}
return bt;
}
static inline void
bt_close(grn_ctx *ctx, btr *bt)
{
if (!bt) {
return;
}
GRN_FREE(bt->nodes);
GRN_FREE(bt);
}
/* Adds a token info to the given tree. btr::min and btr::max are updated
* are updated when the given token info is minimum or maximum token info
* in the given tree. */
static inline void
bt_push(btr *bt, token_info *ti)
{
bool min_p = true;
bool max_p = true;
int32_t pos = ti->pos;
btr_node *node, *new_node, **last;
new_node = bt->nodes + bt->n++;
new_node->ti = ti;
new_node->car = NULL;
new_node->cdr = NULL;
for (last = &bt->root; (node = *last);) {
if (pos < node->ti->pos) {
last = &node->car;
max_p = false;
} else {
last = &node->cdr;
min_p = false;
}
}
*last = new_node;
if (min_p) {
bt->min = ti;
}
if (max_p) {
bt->max = ti;
}
if (ti->must_last) {
bt->n_must_lasts++;
}
}
/* Drops the minimum token info. Note that you can't push a token info to
* this btr after this function is called. */
static inline void
bt_pop(btr *bt)
{
btr_node *min;
btr_node *parent = NULL;
btr_node **last;
for (last = &bt->root; (min = *last) && min->car;
parent = min, last = &min->car) {
}
if (min) {
*last = min->cdr;
if (min->cdr) {
btr_node *new_min;
for (new_min = min->cdr; new_min->car; new_min = new_min->car)
;
bt->min = new_min->ti;
} else if (parent) {
bt->min = parent->ti;
} else {
bt->min = NULL;
}
if (last == &(bt->root)) {
bt->max = bt->min;
}
min->cdr = NULL;
bt->n--;
if (min->ti->must_last) {
bt->n_must_lasts--;
}
}
}
/* Replace the token info of the minimum node. Note that bt->min may
* not be correct until you call bt_reorder_min(). */
static inline void
bt_replace_min(btr *bt, token_info *ti)
{
btr_node *min, **last;
if (ti->must_last) {
bt->n_must_lasts++;
}
for (last = &bt->root; (min = *last) && min->car; last = &min->car)
;
if (min) {
if (min->ti->must_last) {
bt->n_must_lasts--;
}
min->ti = ti;
bt->min = ti;
}
}
/* Re-order the minimum token info. The minimum token info must be
* updated before you call this function. */
static inline void
bt_reorder_min(btr *bt)
{
btr_node *node, *min, **last;
for (last = &bt->root; (min = *last) && min->car; last = &min->car)
;
if (min) {
bool min_p = true;
bool max_p = true;
int32_t pos = min->ti->pos;
*last = min->cdr;
min->cdr = NULL;
for (last = &bt->root; (node = *last);) {
if (pos < node->ti->pos) {
last = &node->car;
max_p = false;
} else {
last = &node->cdr;
min_p = false;
}
}
*last = min;
if (max_p) {
bt->max = min->ti;
}
if (!min_p) {
btr_node *new_min;
for (new_min = bt->root; new_min->car; new_min = new_min->car)
;
bt->min = new_min->ti;
}
}
}
/* select */
static inline void
grn_rset_add_record(grn_ctx *ctx,
grn_hash *rset,
grn_rset_posinfo *posinfo,
double score,
grn_operator op)
{
grn_id id;
grn_rset_recinfo *recinfo;
switch (op) {
case GRN_OP_OR:
id =
grn_hash_add(ctx, rset, posinfo, rset->key_size, (void **)&recinfo, NULL);
if (id != GRN_ID_NIL) {
if (rset->obj.header.flags & GRN_OBJ_WITH_SUBREC) {
grn_table_add_subrec(ctx, (grn_obj *)rset, recinfo, score, posinfo, 1);
grn_selector_data_current_add_score(ctx,
(grn_obj *)rset,
id,
posinfo->rid,
score);
}
}
break;
case GRN_OP_AND:
id = grn_hash_get(ctx, rset, posinfo, rset->key_size, (void **)&recinfo);
if (id != GRN_ID_NIL) {
if (rset->obj.header.flags & GRN_OBJ_WITH_SUBREC) {
recinfo->n_subrecs |= GRN_RSET_UTIL_BIT;
grn_table_add_subrec(ctx, (grn_obj *)rset, recinfo, score, posinfo, 1);
grn_selector_data_current_add_score(ctx,
(grn_obj *)rset,
id,
posinfo->rid,
score);
}
}
break;
case GRN_OP_AND_NOT:
id = grn_hash_get(ctx, rset, posinfo, rset->key_size, (void **)&recinfo);
if (id != GRN_ID_NIL) {
grn_hash_delete_by_id(ctx, rset, id, NULL);
}
break;
case GRN_OP_ADJUST:
id = grn_hash_get(ctx, rset, posinfo, rset->key_size, (void **)&recinfo);
if (id != GRN_ID_NIL) {
if (rset->obj.header.flags & GRN_OBJ_WITH_SUBREC) {
recinfo->score += score;
grn_selector_data_current_add_score(ctx,
(grn_obj *)rset,
id,
posinfo->rid,
score);
}
}
break;
default:
break;
}
}
/* TODO: It may be better that we record the min ID when add/delete a
* record. */
/* TODO: It may be useful that we just return approximate value. For
* example, we can stop min ID search when we find an ID that is less
* than grn_table_size(ctx, grn_ctx_at(ctx,
* result_set->header.domain))/100. */
extern "C" grn_id
grn_result_set_get_min_id(grn_ctx *ctx, grn_hash *result_set)
{
GRN_API_ENTER;
if (!result_set) {
GRN_API_RETURN(GRN_ID_NIL);
}
if (GRN_HASH_SIZE(result_set) == 0) {
GRN_API_RETURN(GRN_ID_NIL);
}
grn_id min_id = GRN_ID_MAX;
GRN_HASH_EACH_BEGIN(ctx, result_set, cursor, id)
{
void *key;
grn_hash_cursor_get_key(ctx, cursor, &key);
grn_id id = *((grn_id *)key);
if (id < min_id) {
min_id = id;
}
}
GRN_HASH_EACH_END(ctx, cursor);
GRN_API_RETURN(min_id);
}
extern "C" grn_rc
grn_result_set_add_record(grn_ctx *ctx,
grn_hash *result_set,
grn_posting *posting,
grn_operator op)
{
GRN_API_ENTER;
grn_rset_add_record(ctx,
result_set,
(grn_rset_posinfo *)posting,
((grn_posting_internal *)posting)->weight_float,
op);
GRN_API_RETURN(ctx->rc);
}
extern "C" grn_rc
grn_result_set_add_table(grn_ctx *ctx,
grn_hash *result_set,
grn_obj *table,
double score,
grn_operator op)
{
GRN_API_ENTER;
const int flags = GRN_CURSOR_BY_ID | GRN_CURSOR_ASCENDING;
grn_table_cursor *cursor =
grn_table_cursor_open(ctx, table, NULL, 0, NULL, 0, 0, -1, flags);
if (cursor) {
grn_result_set_add_table_cursor(ctx, result_set, cursor, score, op);
grn_table_cursor_close(ctx, cursor);
}
GRN_API_RETURN(ctx->rc);
}
extern "C" grn_rc
grn_result_set_add_table_cursor(grn_ctx *ctx,
grn_hash *result_set,
grn_table_cursor *cursor,
double score,
grn_operator op)
{
GRN_API_ENTER;
grn_obj *table = grn_table_cursor_table(ctx, cursor);
if (result_set->obj.header.domain != DB_OBJ(table)->id) {
grn_obj result_set_inspected;
grn_obj table_inspected;
GRN_TEXT_INIT(&result_set_inspected, 0);
GRN_TEXT_INIT(&table_inspected, 0);
grn_inspect_limited(ctx, &result_set_inspected, (grn_obj *)result_set);
grn_inspect_limited(ctx, &table_inspected, table);
ERR(GRN_INVALID_ARGUMENT,
"[result-set][add-table-cursor] "
"table must be the domain of the result set: %.*s: %.*s",
(int)GRN_TEXT_LEN(&result_set_inspected),
GRN_TEXT_VALUE(&result_set_inspected),
(int)GRN_TEXT_LEN(&table_inspected),
GRN_TEXT_VALUE(&table_inspected));
GRN_OBJ_FIN(ctx, &result_set_inspected);
GRN_OBJ_FIN(ctx, &table_inspected);
GRN_API_RETURN(ctx->rc);
}
grn_rc rc;
if (op == GRN_OP_OR || op == GRN_OP_AND) {
rc = grn_hash_add_table_cursor(ctx, result_set, cursor, score, op);
} else {
grn_rset_posinfo posinfo = {};
while ((posinfo.rid = grn_table_cursor_next(ctx, cursor))) {
grn_rset_add_record(ctx, result_set, &posinfo, score, op);
if (ctx->rc != GRN_SUCCESS) {
break;
}
}
rc = ctx->rc;
}
GRN_API_RETURN(rc);
}
extern "C" grn_rc
grn_result_set_add_index_cursor(grn_ctx *ctx,
grn_hash *result_set,
grn_obj *cursor,
double additional_score,
double weight,
grn_operator op)
{
GRN_API_ENTER;
grn_obj *index_column = grn_index_cursor_get_index_column(ctx, cursor);
if (result_set->obj.header.domain != DB_OBJ(index_column)->range) {
grn_obj result_set_inspected;
grn_obj index_column_inspected;
GRN_TEXT_INIT(&result_set_inspected, 0);
GRN_TEXT_INIT(&index_column_inspected, 0);
grn_inspect_limited(ctx, &result_set_inspected, (grn_obj *)result_set);
grn_inspect_limited(ctx, &index_column_inspected, index_column);
ERR(GRN_INVALID_ARGUMENT,
"[result-set][add-index-cursor] "
"not an index column for the result set: %.*s: %.*s",
(int)GRN_TEXT_LEN(&result_set_inspected),
GRN_TEXT_VALUE(&result_set_inspected),
(int)GRN_TEXT_LEN(&index_column_inspected),
GRN_TEXT_VALUE(&index_column_inspected));
GRN_OBJ_FIN(ctx, &result_set_inspected);
GRN_OBJ_FIN(ctx, &index_column_inspected);
GRN_API_RETURN(ctx->rc);
}
grn_rc rc;
if (op == GRN_OP_OR || op == GRN_OP_AND) {
rc = grn_hash_add_index_cursor(ctx,
result_set,
cursor,
additional_score,
weight,
op);
} else {
grn_id term_id;
grn_posting *posting;
while ((posting = grn_index_cursor_next(ctx, cursor, &term_id))) {
grn_rset_posinfo posinfo = {
posting->rid,
posting->sid,
posting->pos,
};
grn_posting_internal *posting_internal = (grn_posting_internal *)posting;
double score =
(posting_internal->weight_float + additional_score) * weight;
grn_rset_add_record(ctx, result_set, &posinfo, score, op);
if (ctx->rc != GRN_SUCCESS) {
break;
}
}
rc = ctx->rc;
}
GRN_API_RETURN(rc);
}
extern "C" grn_rc
grn_result_set_add_ii_cursor(grn_ctx *ctx,
grn_hash *result_set,
grn_ii_cursor *cursor,
double additional_score,
double weight,
grn_operator op)
{
GRN_API_ENTER;
grn_obj *index_column = (grn_obj *)(cursor->ii);
if (result_set->obj.header.domain != DB_OBJ(index_column)->range) {
grn_obj result_set_inspected;
grn_obj index_column_inspected;
GRN_TEXT_INIT(&result_set_inspected, 0);
GRN_TEXT_INIT(&index_column_inspected, 0);
grn_inspect_limited(ctx, &result_set_inspected, (grn_obj *)result_set);
grn_inspect_limited(ctx, &index_column_inspected, index_column);
ERR(GRN_INVALID_ARGUMENT,
"[result-set][add-ii-cursor] "
"not an index column for the result set: %.*s: %.*s",
(int)GRN_TEXT_LEN(&result_set_inspected),
GRN_TEXT_VALUE(&result_set_inspected),
(int)GRN_TEXT_LEN(&index_column_inspected),
GRN_TEXT_VALUE(&index_column_inspected));
GRN_OBJ_FIN(ctx, &result_set_inspected);
GRN_OBJ_FIN(ctx, &index_column_inspected);
GRN_API_RETURN(ctx->rc);
}
grn_rc rc;
if (op == GRN_OP_OR || op == GRN_OP_AND) {
rc = grn_hash_add_ii_cursor(ctx,
result_set,
cursor,
additional_score,
weight,
op);
} else {
grn_posting *posting;
while ((posting = grn_ii_cursor_next(ctx, cursor))) {
grn_rset_posinfo posinfo = {
posting->rid,
posting->sid,
posting->pos,
};
grn_posting_internal *posting_internal = (grn_posting_internal *)posting;
double score =
(posting_internal->weight_float + additional_score) * weight;
grn_rset_add_record(ctx, result_set, &posinfo, score, op);
if (ctx->rc != GRN_SUCCESS) {
break;
}
}
rc = ctx->rc;
}
GRN_API_RETURN(rc);
}
extern "C" grn_rc
grn_result_set_add_ii_select_cursor(grn_ctx *ctx,
grn_hash *result_set,
grn_ii_select_cursor *cursor,
grn_operator op)
{
GRN_API_ENTER;
grn_obj *index_column = (grn_obj *)grn_ii_select_cursor_get_ii(ctx, cursor);
if (result_set->obj.header.domain != DB_OBJ(index_column)->range) {
grn_obj result_set_inspected;
grn_obj index_column_inspected;
GRN_TEXT_INIT(&result_set_inspected, 0);
GRN_TEXT_INIT(&index_column_inspected, 0);
grn_inspect_limited(ctx, &result_set_inspected, (grn_obj *)result_set);
grn_inspect_limited(ctx, &index_column_inspected, index_column);
ERR(GRN_INVALID_ARGUMENT,
"[result-set][add-ii-select-cursor] "
"not an index column for the result set: %.*s: %.*s",
(int)GRN_TEXT_LEN(&result_set_inspected),
GRN_TEXT_VALUE(&result_set_inspected),
(int)GRN_TEXT_LEN(&index_column_inspected),
GRN_TEXT_VALUE(&index_column_inspected));
GRN_OBJ_FIN(ctx, &result_set_inspected);
GRN_OBJ_FIN(ctx, &index_column_inspected);
GRN_API_RETURN(ctx->rc);
}
grn_rc rc;
if (op == GRN_OP_OR || op == GRN_OP_AND) {
rc = grn_hash_add_ii_select_cursor(ctx, result_set, cursor, op);
} else {
grn_ii_select_cursor_posting *posting;
while ((posting = grn_ii_select_cursor_next(ctx, cursor))) {
grn_rset_posinfo posinfo = {
posting->rid,
posting->sid,
posting->pos,
};
grn_rset_add_record(ctx, result_set, &posinfo, posting->score, op);
if (ctx->rc != GRN_SUCCESS) {
break;
}
}
rc = ctx->rc;
}
GRN_API_RETURN(rc);
}
extern "C" grn_rc
grn_result_set_copy(grn_ctx *ctx,
grn_hash *result_set,
grn_hash *output_result_set)
{
GRN_API_ENTER;
GRN_HASH_EACH_BEGIN(ctx, result_set, cursor, id)
{
void *key = NULL;
uint32_t key_size = 0;
void *value = NULL;
grn_hash_cursor_get_key_value(ctx, cursor, &key, &key_size, &value);
void *output_value = NULL;
grn_id id = grn_hash_add(ctx,
output_result_set,
key,
(int)key_size,
&output_value,
NULL);
if (id == GRN_ID_NIL) {
if (ctx->rc == GRN_SUCCESS) {
continue;
} else {
break;
}
}
grn_rset_recinfo *ri = static_cast<grn_rset_recinfo *>(value);
grn_rset_recinfo *output_ri = static_cast<grn_rset_recinfo *>(output_value);
grn_memcpy(output_ri, ri, (output_result_set)->value_size);
output_ri->score = 0;
}
GRN_HASH_EACH_END(ctx, cursor);
GRN_API_RETURN(ctx->rc);
}
static inline void
res_add(grn_ctx *ctx,
grn_hash *s,
grn_rset_posinfo *pi,
double score,
grn_operator op)
{
grn_rset_add_record(ctx, s, pi, score, op);
}
extern "C" grn_rc
grn_ii_posting_add(grn_ctx *ctx, grn_posting *pos, grn_hash *s, grn_operator op)
{
grn_rset_add_record(ctx, s, (grn_rset_posinfo *)(pos), pos->weight, op);
return ctx->rc;
}
extern "C" grn_rc
grn_ii_posting_add_float(grn_ctx *ctx,
grn_posting *pos,
grn_hash *s,
grn_operator op)
{
return grn_result_set_add_record(ctx, s, pos, op);
}
static float
get_weight(grn_ctx *ctx, grn_ii_select_data *data)
{
switch (data->wv_mode) {
case GRN_WV_NONE:
return 1;
case GRN_WV_STATIC:
if (data->sid == 0) {
return 0;
} else if (data->sid <= (uint32_t)(data->optarg->vector_size)) {
if (data->optarg->weight_vector) {
return (float)(data->optarg->weight_vector[data->sid - 1]);
} else {
return data->optarg->weight_vector_float[data->sid - 1];
}
} else {
return 0;
}
case GRN_WV_DYNAMIC:
/* todo : support hash with keys
if (s->keys) {
uint32_t key_size;
const char *key = _grn_table_key(ctx, s->keys, rid, &key_size);
// todo : change grn_select_optarg
return key ? optarg->func(s, key, key_size, sid, optarg->func_arg) : 0;
}
*/
/* todo : cast */
{
int weight = data->optarg->func(ctx,
data->result_set,
(void *)(intptr_t)(data->rid),
data->sid,
data->optarg->func_arg);
return (float)weight;
}
case GRN_WV_CONSTANT:
if (data->optarg->vector_size < 0) {
return data->optarg->weight_float;
} else {
return (float)(data->optarg->vector_size);
}
default:
return 1;
}
}
static void
grn_ii_select_data_init(grn_ctx *ctx,
grn_ii_select_data *data,
grn_select_optarg *optarg)
{
data->optarg = optarg;
data->mode = GRN_OP_EXACT;
data->wv_mode = GRN_WV_NONE;
data->score_func = NULL;
data->previous_min = GRN_ID_NIL;
data->current_min = GRN_ID_NIL;
data->set_min_enable_for_and_query = false;
data->only_skip_token = false;
data->query_options = NULL;
data->token_infos = NULL;
data->n_token_infos = 0;
data->n_phrase_groups = 0;
data->phrase_groups = NULL;
data->n_phrases = 0;
data->bt = NULL;
data->check_element_intervals_btree = NULL;
data->rid = 0;
data->sid = 0;
data->next_rid = 0;
data->next_sid = 0;
if (!optarg) {
return;
}
data->mode = optarg->mode;
if (optarg->func) {
data->wv_mode = GRN_WV_DYNAMIC;
} else if (optarg->vector_size != 0) {
if (optarg->weight_vector || optarg->weight_vector_float) {
data->wv_mode = GRN_WV_STATIC;
} else {
data->wv_mode = GRN_WV_CONSTANT;
}
}
if (optarg->match_info) {
if (optarg->match_info->flags & GRN_MATCH_INFO_GET_MIN_RECORD_ID) {
data->previous_min = optarg->match_info->min;
data->set_min_enable_for_and_query = true;
}
}
if (optarg->scorer) {
grn_proc *scorer = (grn_proc *)(optarg->scorer);
data->score_func = scorer->callbacks.scorer.score;
data->record.table = grn_ctx_at(ctx, data->result_set->obj.header.domain);
data->record.lexicon = data->lexicon;
data->record.id = GRN_ID_NIL;
GRN_RECORD_INIT(&(data->record.terms),
GRN_OBJ_VECTOR,
data->lexicon->header.domain);
GRN_UINT32_INIT(&(data->record.term_weights), GRN_OBJ_VECTOR);
data->record.total_term_weights = 0;
data->record.n_documents = grn_table_size(ctx, data->record.table);
data->record.n_occurrences = 0;
data->record.n_candidates = 0;
data->record.n_tokens = 0;
data->record.weight = 0;
data->record.args_expr = optarg->scorer_args_expr;
data->record.args_expr_offset = optarg->scorer_args_expr_offset;
}
data->fuzzy_search_optarg = &(optarg->fuzzy);
data->query_options = optarg->query_options;
}
static bool
grn_ii_select_data_init_token_infos(grn_ctx *ctx,
grn_ii_select_data *data,
const char *tag)
{
if (data->query_len == 0) {
return false;
}
data->token_infos = static_cast<token_info **>(
GRN_MALLOC(sizeof(token_info *) * data->query_len * 2));
if (!data->token_infos) {
char errbuf[GRN_CTX_MSGSIZE];
grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
ERR(ctx->rc, "%s failed to allocate token info container: %s", tag, errbuf);
return false;
}
if (data->mode == GRN_OP_FUZZY) {
grn_rc rc = token_info_build_fuzzy(ctx, data);
if (rc != GRN_SUCCESS) {
return false;
}
if (data->n_token_infos == 0) {
return false;
}
} else if (data->mode == GRN_OP_NEAR_PHRASE ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE ||
data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
grn_rc rc = token_info_build_near_phrase(ctx, data);
if (rc != GRN_SUCCESS) {
return false;
}
} else {
grn_rc rc = token_info_build(ctx, data);
if (rc != GRN_SUCCESS) {
return false;
}
}
if (data->n_token_infos == 0) {
return false;
}
P_NOTE("original token infos\n");
P_TIS(ctx, data->token_infos, data->n_token_infos);
if (data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
data->phrase_groups = static_cast<phrase_group *>(
GRN_MALLOC(sizeof(phrase_group) * data->n_phrase_groups));
if (!data->phrase_groups) {
return false;
}
uint32_t i;
for (i = 0; i < data->n_phrase_groups; i++) {
uint32_t i_token_info;
uint32_t current_phrase_id = (uint32_t)-1;
uint32_t n_phrases = 0;
for (i_token_info = 0; i_token_info < data->n_token_infos;
i_token_info++) {
const token_info *token_info = data->token_infos[i_token_info];
if (token_info->phrase_id != current_phrase_id &&
data->token_infos[i_token_info]->phrase_group_id == i) {
n_phrases++;
}
current_phrase_id = token_info->phrase_id;
}
data->phrase_groups[i].n_phrases = n_phrases;
data->phrase_groups[i].btree = bt_open(ctx, n_phrases);
}
} else {
data->phrase_groups = NULL;
}
if (data->mode == GRN_OP_NEAR_NO_OFFSET) {
token_info_clear_offset(data->token_infos, data->n_token_infos);
data->mode = GRN_OP_NEAR;
}
switch (data->mode) {
case GRN_OP_NEAR:
case GRN_OP_NEAR_PHRASE:
case GRN_OP_ORDERED_NEAR_PHRASE:
case GRN_OP_NEAR_PHRASE_PRODUCT:
case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT:
{
size_t btree_size;
if (data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
btree_size = data->n_phrase_groups;
} else {
btree_size = data->n_token_infos;
}
data->bt = bt_open(ctx, btree_size);
if (!data->bt) {
char errbuf[GRN_CTX_MSGSIZE];
grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
ERR(ctx->rc, "%s failed to allocate btree: %s", tag, errbuf);
return false;
}
data->max_interval = data->optarg->max_interval;
data->additional_last_interval = data->optarg->additional_last_interval;
data->max_element_intervals = data->optarg->max_element_intervals;
data->min_interval = data->optarg->min_interval;
if ((data->mode == GRN_OP_NEAR || data->mode == GRN_OP_NEAR_PHRASE ||
data->mode == GRN_OP_NEAR_PHRASE_PRODUCT) &&
data->max_element_intervals &&
GRN_INT32_VECTOR_SIZE(data->max_element_intervals) > 0) {
data->check_element_intervals_btree = bt_open(ctx, btree_size);
if (!data->check_element_intervals_btree) {
char errbuf[GRN_CTX_MSGSIZE];
grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
ERR(ctx->rc,
"%s failed to allocate btree for checking element intervals: %s",
tag,
errbuf);
return false;
}
}
}
break;
default:
break;
}
qsort(data->token_infos,
data->n_token_infos,
sizeof(token_info *),
token_compare);
P_NOTE("sorted token infos\n");
P_TIS(ctx, data->token_infos, data->n_token_infos);
if (data->mode == GRN_OP_NEAR_PHRASE) {
bool have_must_last = false;
uint32_t i;
for (i = 0; i < data->n_token_infos; i++) {
if (data->token_infos[i]->must_last) {
if (have_must_last) {
/* Multiple last tokens query are never matched. */
return false;
}
have_must_last = true;
}
}
}
return true;
}
static void
grn_ii_select_data_fin(grn_ctx *ctx, grn_ii_select_data *data)
{
if (data->score_func) {
GRN_OBJ_FIN(ctx, &(data->record.terms));
GRN_OBJ_FIN(ctx, &(data->record.term_weights));
}
if (data->set_min_enable_for_and_query && !data->only_skip_token) {
if (data->current_min > data->previous_min) {
data->optarg->match_info->min = data->current_min;
}
}
if (data->only_skip_token && data->optarg && data->optarg->match_info) {
data->optarg->match_info->flags |= GRN_MATCH_INFO_ONLY_SKIP_TOKEN;
}
if (data->phrase_groups) {
uint32_t i;
for (i = 0; i < data->n_phrase_groups; i++) {
bt_close(ctx, data->phrase_groups[i].btree);
}
GRN_FREE(data->phrase_groups);
}
if (data->token_infos) {
token_info **token_info;
for (token_info = data->token_infos;
token_info < data->token_infos + data->n_token_infos;
token_info++) {
if (*token_info) {
token_info_close(ctx, *token_info);
}
}
GRN_FREE(data->token_infos);
}
if (data->bt) {
bt_close(ctx, data->bt);
}
if (data->check_element_intervals_btree) {
bt_close(ctx, data->check_element_intervals_btree);
}
}
static grn_rc
grn_ii_similar_search_internal(grn_ctx *ctx, grn_ii_select_data *data)
{
int *w1;
uint32_t limit;
grn_id tid, *tp, max_size;
grn_rc rc = GRN_SUCCESS;
grn_hash *h;
grn_token_cursor *token_cursor;
if (data->query_len == 0) {
return GRN_SUCCESS;
}
if (!data->lexicon || !data->query || !data->query_len || !data->result_set ||
!data->optarg) {
return GRN_INVALID_ARGUMENT;
}
if (!(h = grn_hash_create(ctx, NULL, sizeof(grn_id), sizeof(int), 0))) {
return GRN_NO_MEMORY_AVAILABLE;
}
token_cursor = grn_ii_select_data_open_token_cursor(ctx,
data,
data->query,
data->query_len,
GRN_TOKENIZE_GET);
if (!token_cursor) {
grn_hash_close(ctx, h);
return ctx->rc;
}
if (!(max_size = data->optarg->max_size)) {
max_size = 1048576;
}
while (token_cursor->status != GRN_TOKEN_CURSOR_DONE &&
token_cursor->status != GRN_TOKEN_CURSOR_DONE_SKIP) {
if ((tid = grn_token_cursor_next(ctx, token_cursor))) {
if (grn_hash_add(ctx, h, &tid, sizeof(grn_id), (void **)&w1, NULL)) {
(*w1)++;
}
}
if (tid && token_cursor->curr_size) {
if (data->mode == GRN_OP_UNSPLIT) {
grn_table_search(ctx,
data->lexicon,
token_cursor->curr,
token_cursor->curr_size,
GRN_OP_PREFIX,
(grn_obj *)h,
GRN_OP_OR);
}
if (data->mode == GRN_OP_PARTIAL) {
grn_table_search(ctx,
data->lexicon,
token_cursor->curr,
token_cursor->curr_size,
GRN_OP_SUFFIX,
(grn_obj *)h,
GRN_OP_OR);
}
}
}
grn_token_cursor_close(ctx, token_cursor);
{
grn_hash_cursor *c =
grn_hash_cursor_open(ctx, h, NULL, 0, NULL, 0, 0, -1, 0);
if (!c) {
GRN_LOG(ctx,
GRN_LOG_ALERT,
"grn_hash_cursor_open on grn_ii_similar_search failed !");
grn_hash_close(ctx, h);
return GRN_NO_MEMORY_AVAILABLE;
}
while (grn_hash_cursor_next(ctx, c)) {
uint32_t es;
grn_hash_cursor_get_key_value(ctx, c, (void **)&tp, NULL, (void **)&w1);
if ((es = grn_ii_estimate_size(ctx, data->ii, *tp))) {
*w1 += max_size / es;
} else {
grn_hash_cursor_delete(ctx, c, NULL);
}
}
grn_hash_cursor_close(ctx, c);
}
limit = data->optarg->similarity_threshold
? ((uint32_t)(data->optarg->similarity_threshold) > GRN_HASH_SIZE(h)
? GRN_HASH_SIZE(h)
: (uint32_t)(data->optarg->similarity_threshold))
: (GRN_HASH_SIZE(h) >> 3) + 1;
if (GRN_HASH_SIZE(h)) {
grn_id j, id;
float w2;
int rep;
grn_ii_cursor *c;
grn_posting *pos;
grn_table_sort_optarg arg = {GRN_TABLE_SORT_DESC | GRN_TABLE_SORT_BY_VALUE |
GRN_TABLE_SORT_AS_NUMBER,
NULL,
NULL,
NULL,
0};
grn_array *sorted = grn_array_create(ctx, NULL, sizeof(grn_id), 0);
if (!sorted) {
GRN_LOG(ctx,
GRN_LOG_ALERT,
"grn_hash_sort on grn_ii_similar_search failed !");
grn_hash_close(ctx, h);
return GRN_NO_MEMORY_AVAILABLE;
}
grn_hash_sort(ctx, h, limit, sorted, &arg);
/* todo support subrec
rep = (s->record_unit == GRN_REC_POSITION || s->subrec_unit ==
GRN_REC_POSITION);
*/
rep = 0;
for (j = 1; j <= limit; j++) {
grn_array_get_value(ctx, sorted, j, &id);
_grn_hash_get_key_value(ctx, h, id, (void **)&tp, (void **)&w1);
if (!*tp || !(c = grn_ii_cursor_open(ctx,
data->ii,
*tp,
GRN_ID_NIL,
GRN_ID_MAX,
rep ? data->ii->n_elements
: data->ii->n_elements - 1,
0))) {
GRN_LOG(ctx, GRN_LOG_ERROR, "cursor open failed (%d)", *tp);
continue;
}
if (rep) {
while (grn_ii_cursor_next(ctx, c)) {
pos = c->post;
data->rid = pos->rid;
data->sid = pos->sid;
if ((w2 = get_weight(ctx, data)) > 0) {
while (grn_ii_cursor_next_pos(ctx, c)) {
float pos_weight = grn_posting_get_weight_float(ctx, pos);
res_add(ctx,
data->result_set,
(grn_rset_posinfo *)pos,
*w1 * w2 * (1 + pos_weight),
data->op);
}
}
}
} else {
while (grn_ii_cursor_next(ctx, c)) {
pos = c->post;
data->rid = pos->rid;
data->sid = pos->sid;
if ((w2 = get_weight(ctx, data)) > 0) {
float pos_weight = grn_posting_get_weight_float(ctx, pos);
res_add(ctx,
data->result_set,
(grn_rset_posinfo *)pos,
*w1 * w2 * (pos->tf + pos_weight),
data->op);
}
}
}
grn_ii_cursor_close(ctx, c);
}
grn_array_close(ctx, sorted);
}
grn_hash_close(ctx, h);
grn_ii_resolve_sel_and(ctx, data->result_set, data->op);
// grn_hash_cursor_clear(r);
return rc;
}
extern "C" grn_rc
grn_ii_similar_search(grn_ctx *ctx,
grn_ii *ii,
const char *string,
unsigned int string_len,
grn_hash *s,
grn_operator op,
grn_select_optarg *optarg)
{
grn_ii_select_data data;
data.ii = ii;
data.lexicon = data.ii->lexicon;
data.query = string;
data.query_len = string_len;
data.result_set = s;
data.op = op;
grn_ii_select_data_init(ctx, &data, optarg);
return grn_ii_similar_search_internal(ctx, &data);
}
#define TERM_EXTRACT_EACH_POST 0
#define TERM_EXTRACT_EACH_TERM 1
static grn_rc
grn_ii_term_extract_internal(grn_ctx *ctx, grn_ii_select_data *data)
{
grn_rset_posinfo pi;
grn_id tid;
const char *p, *pe;
grn_obj *nstr;
const char *normalized;
unsigned int normalized_length_in_bytes;
grn_ii_cursor *c;
grn_posting *pos;
int skip, rep, policy;
grn_rc rc = GRN_SUCCESS;
if (data->query_len == 0) {
return GRN_SUCCESS;
}
if (!data->ii || !data->query || !data->query_len || !data->result_set ||
!data->optarg) {
return GRN_INVALID_ARGUMENT;
}
if (!(nstr = grn_string_open(ctx, data->query, data->query_len, NULL, 0))) {
return GRN_INVALID_ARGUMENT;
}
policy = data->optarg->max_interval;
/* todo support subrec
if (policy == TERM_EXTRACT_EACH_POST) {
if ((rc = grn_records_reopen(s, GRN_REC_SECTION, GRN_REC_NONE, 0))) { goto
exit; }
}
rep = (s->record_unit == GRN_REC_POSITION || s->subrec_unit ==
GRN_REC_POSITION);
*/
rep = 0;
grn_string_get_normalized(ctx,
nstr,
&normalized,
&normalized_length_in_bytes,
NULL);
for (p = normalized, pe = p + normalized_length_in_bytes; p < pe; p += skip) {
if ((tid = grn_table_lcp_search(ctx, data->ii->lexicon, p, pe - p))) {
if (policy == TERM_EXTRACT_EACH_POST) {
if (!(skip = grn_table_get_key(ctx, data->ii->lexicon, tid, NULL, 0))) {
break;
}
} else {
if (!(skip = (int)grn_charlen(ctx, p, pe))) {
break;
}
}
if (!(c = grn_ii_cursor_open(ctx,
data->ii,
tid,
GRN_ID_NIL,
GRN_ID_MAX,
rep ? data->ii->n_elements
: data->ii->n_elements - 1,
0))) {
GRN_LOG(ctx, GRN_LOG_ERROR, "cursor open failed (%d)", tid);
continue;
}
if (rep) {
while (grn_ii_cursor_next(ctx, c)) {
pos = c->post;
data->rid = pos->rid;
data->sid = pos->sid;
while (grn_ii_cursor_next_pos(ctx, c)) {
res_add(ctx,
data->result_set,
(grn_rset_posinfo *)pos,
get_weight(ctx, data),
data->op);
}
}
} else {
while (grn_ii_cursor_next(ctx, c)) {
if (policy == TERM_EXTRACT_EACH_POST) {
pi.rid = c->post->rid;
pi.sid = p - normalized;
res_add(ctx, data->result_set, &pi, pi.sid + 1, data->op);
} else {
pos = c->post;
data->rid = pos->rid;
data->sid = pos->sid;
res_add(ctx,
data->result_set,
(grn_rset_posinfo *)pos,
get_weight(ctx, data),
data->op);
}
}
}
grn_ii_cursor_close(ctx, c);
} else {
if (!(skip = (int)grn_charlen(ctx, p, pe))) {
break;
}
}
}
grn_obj_close(ctx, nstr);
return rc;
}
extern "C" grn_rc
grn_ii_term_extract(grn_ctx *ctx,
grn_ii *ii,
const char *string,
unsigned int string_len,
grn_hash *s,
grn_operator op,
grn_select_optarg *optarg)
{
grn_ii_select_data data;
data.ii = ii;
data.lexicon = data.ii->lexicon;
data.query = string;
data.query_len = string_len;
data.result_set = s;
data.op = op;
grn_ii_select_data_init(ctx, &data, optarg);
return grn_ii_term_extract_internal(ctx, &data);
}
struct grn_ii_select_cursor_ {
grn_ii_select_data data;
bool per_occurrence_mode;
grn_ii_select_cursor_posting posting;
bool done;
grn_ii_select_cursor_posting unshifted_posting;
bool have_unshifted_posting;
};
static void
grn_ii_select_cursor_init_internal(grn_ctx *ctx, grn_ii_select_cursor *cursor)
{
cursor->per_occurrence_mode = false;
cursor->done = false;
cursor->have_unshifted_posting = false;
}
static grn_rc
grn_ii_select_cursor_close(grn_ctx *ctx, grn_ii_select_cursor *cursor)
{
if (!cursor) {
return GRN_SUCCESS;
}
grn_ii_select_data_fin(ctx, &(cursor->data));
GRN_FREE(cursor);
return GRN_SUCCESS;
}
static grn_ii_select_cursor *
grn_ii_select_cursor_open(grn_ctx *ctx,
grn_ii *ii,
const char *string,
unsigned int string_len,
grn_select_optarg *optarg)
{
const char *tag = "[ii][select][cursor][open]";
if (string_len == 0) {
ERR(GRN_INVALID_ARGUMENT, "%s empty string", tag);
return NULL;
}
auto cursor = static_cast<grn_ii_select_cursor *>(
GRN_CALLOC(sizeof(grn_ii_select_cursor)));
if (!cursor) {
char errbuf[GRN_CTX_MSGSIZE];
grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
ERR(ctx->rc, "%s failed to allocate cursor: %s", tag, errbuf);
return NULL;
}
cursor->data.ii = ii;
cursor->data.lexicon = cursor->data.ii->lexicon;
cursor->data.query = string;
cursor->data.query_len = string_len;
cursor->data.result_set = NULL;
cursor->data.op = GRN_OP_OR;
grn_ii_select_data_init(ctx, &(cursor->data), optarg);
switch (cursor->data.mode) {
case GRN_OP_EXACT:
case GRN_OP_FUZZY:
case GRN_OP_NEAR:
case GRN_OP_NEAR_NO_OFFSET:
break;
default:
ERR(GRN_INVALID_ARGUMENT,
"%s EXACT, FUZZY, NEAR and NEAR_NO_OFFSET are only supported mode: %s",
tag,
grn_operator_to_string(cursor->data.mode));
grn_ii_select_cursor_close(ctx, cursor);
return NULL;
}
if (!grn_ii_select_data_init_token_infos(ctx, &(cursor->data), tag)) {
grn_ii_select_cursor_close(ctx, cursor);
return NULL;
}
if (cursor->data.n_token_infos == 0) {
grn_ii_select_cursor_close(ctx, cursor);
return NULL;
}
GRN_LOG(ctx,
GRN_LOG_INFO,
"%s n=%d <%.*s>",
tag,
cursor->data.n_token_infos,
string_len,
string);
grn_ii_select_cursor_init_internal(ctx, cursor);
return cursor;
}
static void
grn_ii_select_cursor_set_per_occurrence_mode(grn_ctx *ctx,
grn_ii_select_cursor *cursor,
bool mode)
{
cursor->per_occurrence_mode = mode;
}
static inline bool
grn_ii_select_skip_pos(grn_ctx *ctx, grn_ii_select_data *data, int32_t pos)
{
if (token_info_skip_pos(ctx,
data->current_token_info,
data->rid,
data->sid,
pos) != GRN_SUCCESS) {
return false;
}
if (data->current_token_info->p->rid != data->rid ||
data->current_token_info->p->sid != data->sid) {
if (!(data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT)) {
data->next_rid = data->current_token_info->p->rid;
data->next_sid = data->current_token_info->p->sid;
}
return false;
}
return true;
}
/* This function must return a "must_last" token info if it exists.
* btr::n_must_lasts is the number of "must_last" token infos and
* we use btr::n_must_lasts as the number of phrases that must be
* last. So if this function doesn't return a "must_last" token info,
* we misjudge the number of phrases that must be last.
*/
static inline bool
grn_ii_select_data_find_phrase(grn_ctx *ctx,
grn_ii_select_data *data,
uint32_t phrase_id,
int32_t start_pos,
uint32_t start_n_tokens_in_phrase)
{
token_info **tip;
token_info **tis = data->token_infos;
token_info **tie = tis + data->n_token_infos;
token_info **ti_last = NULL;
token_info *must_last_ti = NULL;
uint32_t n_tokens_in_same_pos = 0;
for (tip = tis;; tip++) {
if (tip == tie) {
tip = tis;
}
if (!ti_last) {
ti_last = tip;
} else if (ti_last == tip) {
n_tokens_in_same_pos = 0;
start_pos++;
}
data->current_token_info = *tip;
if (data->current_token_info->phrase_id != phrase_id) {
continue;
}
if (data->current_token_info->must_last) {
must_last_ti = data->current_token_info;
}
if (!grn_ii_select_skip_pos(ctx, data, start_pos)) {
return false;
}
if (data->current_token_info->pos == start_pos &&
data->current_token_info->n_tokens_in_phrase >=
start_n_tokens_in_phrase) {
n_tokens_in_same_pos++;
} else {
n_tokens_in_same_pos = 1;
if (data->current_token_info->pos > start_pos) {
start_n_tokens_in_phrase = 0;
ti_last = tip;
}
start_pos = data->current_token_info->pos;
}
if (n_tokens_in_same_pos ==
data->current_token_info->n_used_tokens_in_phrase &&
data->current_token_info->n_tokens_in_phrase >=
start_n_tokens_in_phrase) {
if (must_last_ti) {
data->current_token_info = must_last_ti;
}
P_NOTE("%s: found: record:%u phrase:%u/%u\n",
__func__,
data->rid,
data->current_token_info->phrase_id,
data->current_token_info->phrase_group_id);
P_TI(ctx, data->current_token_info);
return true;
}
}
return false;
}
static inline bool
grn_ii_select_data_find_phrase_product(grn_ctx *ctx,
grn_ii_select_data *data,
int32_t start_pos,
uint32_t start_n_tokens_in_phrase)
{
uint32_t phrase_group_id = data->current_token_info->phrase_group_id;
phrase_group *group = &(data->phrase_groups[phrase_group_id]);
bool skipped = false;
token_info *min_before = group->btree->min;
while (true) {
data->current_token_info = group->btree->min;
if (grn_ii_select_data_find_phrase(ctx,
data,
data->current_token_info->phrase_id,
start_pos,
start_n_tokens_in_phrase)) {
skipped = true;
bt_reorder_min(group->btree);
token_info *min = group->btree->min;
if (min == min_before) {
break;
}
if (min->pos == min_before->pos &&
min->n_tokens_in_phrase > min_before->n_tokens_in_phrase) {
break;
}
min_before = min;
} else {
bt_pop(group->btree);
if (!group->btree->min) {
break;
}
}
}
if (!skipped) {
return false;
}
P_NOTE("%s: found: record:%u\n", __func__, data->rid);
P_TI(ctx, group->btree->min);
bt_replace_min(data->bt, group->btree->min);
return true;
}
static inline bool
grn_ii_select_data_is_matched_near_phrase_real(grn_ctx *ctx,
grn_ii_select_data *data,
int interval)
{
if (interval < data->min_interval) {
return false;
}
if (data->max_interval < 0) {
return true;
}
if (data->additional_last_interval == 0) {
return (interval <= data->max_interval);
}
token_info *min_token_info = NULL;
int min_without_last_token = -1;
int max_without_last_token = -1;
if (data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
uint32_t i;
uint32_t n = data->n_phrase_groups;
for (i = 0; i < n - 1; i++) {
token_info *ti = data->phrase_groups[i].btree->min;
if (min_without_last_token == -1 || ti->pos < min_without_last_token) {
min_without_last_token = ti->pos;
min_token_info = ti;
}
if (max_without_last_token == -1 || ti->pos > max_without_last_token) {
max_without_last_token = ti->pos;
}
}
} else if (data->mode == GRN_OP_NEAR_PHRASE_PRODUCT) {
uint32_t i;
uint32_t n = data->n_phrase_groups;
for (i = 0; i < n; i++) {
token_info *ti = data->phrase_groups[i].btree->min;
if (ti->must_last) {
continue;
}
if (min_without_last_token == -1 || ti->pos < min_without_last_token) {
min_without_last_token = ti->pos;
min_token_info = ti;
}
if (max_without_last_token == -1 || ti->pos > max_without_last_token) {
max_without_last_token = ti->pos;
}
}
} else {
uint32_t i;
uint32_t n = data->n_token_infos;
for (i = 0; i < n; i++) {
token_info *ti = data->token_infos[i];
if (ti->must_last) {
continue;
}
if (min_without_last_token == -1 || ti->pos < min_without_last_token) {
min_without_last_token = ti->pos;
min_token_info = ti;
}
if (max_without_last_token == -1 || ti->pos > max_without_last_token) {
max_without_last_token = ti->pos;
}
}
}
if (!min_token_info) {
return true;
}
int interval_without_last_token = max_without_last_token -
min_without_last_token -
min_token_info->n_tokens_in_phrase;
if (data->additional_last_interval < 0) {
return (interval_without_last_token <= data->max_interval);
} else {
return (
(interval <= (data->max_interval + data->additional_last_interval)) &&
(interval_without_last_token <= data->max_interval));
}
}
static inline bool
grn_ii_select_data_is_matched_near_phrase(grn_ctx *ctx,
grn_ii_select_data *data,
int interval)
{
bool matched =
grn_ii_select_data_is_matched_near_phrase_real(ctx, data, interval);
P_NOTE("%s: %s: record:%u interval:%d min:%d max:%d/%d\n",
__func__,
matched ? "matched" : "not matched",
data->rid,
interval,
data->min_interval,
data->max_interval,
data->additional_last_interval);
return matched;
}
static inline bool
grn_ii_select_data_check_near_element_intervals(grn_ctx *ctx,
grn_ii_select_data *data)
{
if (!data->max_element_intervals) {
return true;
}
uint32_t n_max_element_intervals =
GRN_INT32_VECTOR_SIZE(data->max_element_intervals);
if (n_max_element_intervals == 0) {
return true;
}
if (data->mode == GRN_OP_NEAR || data->mode == GRN_OP_NEAR_PHRASE ||
data->mode == GRN_OP_NEAR_PHRASE_PRODUCT) {
uint32_t i;
uint32_t n = data->bt->n;
bt_zap(data->check_element_intervals_btree);
for (i = 0; i < n; i++) {
bt_push(data->check_element_intervals_btree, data->bt->nodes[i].ti);
}
if (n > n_max_element_intervals + 1) {
n = n_max_element_intervals + 1;
}
int32_t previous_pos = data->check_element_intervals_btree->min->pos;
int32_t previous_n_tokens_in_phrase =
data->check_element_intervals_btree->min->n_tokens_in_phrase;
for (i = 1; i < n; i++) {
bt_pop(data->check_element_intervals_btree);
token_info *min = data->check_element_intervals_btree->min;
int32_t pos = min->pos;
int32_t max_element_interval =
GRN_INT32_VALUE_AT(data->max_element_intervals, i - 1);
int32_t interval = pos - previous_pos - previous_n_tokens_in_phrase;
if (max_element_interval >= 0 && interval > max_element_interval) {
return false;
}
if (interval < data->min_interval) {
return false;
}
previous_pos = pos;
previous_n_tokens_in_phrase = min->n_tokens_in_phrase;
}
return true;
} else if (data->mode == GRN_OP_ORDERED_NEAR_PHRASE) {
uint32_t i_token_info;
uint32_t n_token_infos = data->n_token_infos;
uint32_t i_interval;
uint32_t n_intervals = data->n_phrases - 1;
if (n_intervals > n_max_element_intervals) {
n_intervals = n_max_element_intervals;
}
uint32_t previous_phrase_id = data->token_infos[0]->phrase_id;
int32_t previous_pos = data->token_infos[0]->pos;
int32_t previous_n_tokens_in_phrase =
data->token_infos[0]->n_tokens_in_phrase;
for (i_token_info = 1, i_interval = 0;
i_token_info < n_token_infos && i_interval < n_intervals;
i_token_info++) {
token_info *token_info = data->token_infos[i_token_info];
if (token_info->phrase_id == previous_phrase_id) {
continue;
}
previous_phrase_id = token_info->phrase_id;
int32_t pos = token_info->pos;
int32_t max_element_interval =
GRN_INT32_VALUE_AT(data->max_element_intervals, i_interval);
int32_t interval = pos - previous_pos - previous_n_tokens_in_phrase;
if (max_element_interval >= 0 && interval > max_element_interval) {
return false;
}
if (interval < data->min_interval) {
return false;
}
previous_pos = pos;
previous_n_tokens_in_phrase = token_info->n_tokens_in_phrase;
i_interval++;
}
return true;
} else if (data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
uint32_t i;
uint32_t n = data->n_phrase_groups;
if (n > n_max_element_intervals + 1) {
n = n_max_element_intervals + 1;
}
int32_t previous_pos = data->phrase_groups[0].btree->min->pos;
int32_t previous_n_tokens_in_phrase =
data->phrase_groups[0].btree->min->n_tokens_in_phrase;
for (i = 1; i < n; i++) {
token_info *min = data->phrase_groups[i].btree->min;
int32_t pos = min->pos;
int32_t max_element_interval =
GRN_INT32_VALUE_AT(data->max_element_intervals, i - 1);
int32_t interval = pos - previous_pos - previous_n_tokens_in_phrase;
if (max_element_interval >= 0 && interval > max_element_interval) {
return false;
}
if (interval < data->min_interval) {
return false;
}
previous_pos = pos;
previous_n_tokens_in_phrase = min->n_tokens_in_phrase;
}
return true;
} else {
return false;
}
}
static inline bool
grn_ii_select_cursor_next_prepare(grn_ctx *ctx, grn_ii_select_cursor *cursor)
{
grn_ii_select_data *data = &(cursor->data);
uint32_t i;
uint32_t n = data->n_token_infos;
if (data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
uint32_t min_rid = 0;
uint32_t min_sid = 0;
for (i = 0; i < n; i++) {
token_info *ti = data->token_infos[i];
if (ti->phrase_group_id == 0) {
if ((ti->p->rid < data->next_rid) ||
(ti->p->rid == data->next_rid && ti->p->sid < data->next_sid)) {
continue;
}
if (min_rid == 0 || ti->p->rid < min_rid ||
(ti->p->rid == min_rid && ti->p->sid < min_sid)) {
min_rid = ti->p->rid;
min_sid = ti->p->sid;
}
} else {
token_info_skip(ctx, ti, min_rid, min_sid);
}
}
data->rid = min_rid;
data->sid = min_sid;
data->next_rid = data->rid;
data->next_sid = data->sid + 1;
} else {
data->rid = data->token_infos[0]->p->rid;
data->sid = data->token_infos[0]->p->sid;
data->next_rid = data->rid;
data->next_sid = data->sid + 1;
for (i = 1; i < n; i++) {
data->current_token_info = data->token_infos[i];
if (token_info_skip(ctx,
data->current_token_info,
data->rid,
data->sid) != GRN_SUCCESS) {
return false;
}
if (data->current_token_info->p->rid != data->rid ||
data->current_token_info->p->sid != data->sid) {
data->next_rid = data->current_token_info->p->rid;
data->next_sid = data->current_token_info->p->sid;
break;
}
}
data->may_match = (i == data->n_token_infos);
}
return true;
}
static inline bool
grn_ii_select_cursor_next_skip(grn_ctx *ctx, grn_ii_select_cursor *cursor)
{
grn_ii_select_data *data = &(cursor->data);
if (data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
bool skipped = false;
uint32_t i;
uint32_t n = data->n_token_infos;
for (i = 0; i < n; i++) {
token_info *ti = data->token_infos[i];
if (ti->phrase_group_id != 0) {
break;
}
if (token_info_skip(ctx, ti, data->next_rid, data->next_sid) ==
GRN_SUCCESS) {
skipped = true;
}
}
return skipped;
} else {
return token_info_skip(ctx,
data->token_infos[0],
data->next_rid,
data->next_sid) == GRN_SUCCESS;
}
}
static inline bool
grn_ii_select_cursor_next_find_one_token(grn_ctx *ctx,
grn_ii_select_cursor *cursor,
const char *tag)
{
grn_ii_select_data *data = &(cursor->data);
token_info *ti = data->token_infos[0];
data->start_pos = data->end_pos = data->pos = ti->p->pos;
if (cursor->per_occurrence_mode) {
data->n_occurrences = 1;
} else {
data->n_occurrences = ti->p->tf;
}
data->total_score = ti->p->weight + ti->cursors->bins[0]->weight;
if (data->score_func) {
GRN_RECORD_PUT(ctx, &(data->record.terms), ti->cursors->bins[0]->id);
GRN_UINT32_PUT(ctx, &(data->record.term_weights), data->total_score);
data->record.n_occurrences = data->n_occurrences;
data->record.n_candidates = ti->size;
data->record.n_tokens = ti->ntoken;
}
return true;
}
static inline bool
grn_ii_select_cursor_next_find_near(grn_ctx *ctx,
grn_ii_select_cursor *cursor,
const char *tag)
{
grn_ii_select_data *data = &(cursor->data);
bt_zap(data->bt);
bool need_check = true;
if (data->mode == GRN_OP_NEAR_PHRASE ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE) {
uint32_t phrase_id;
for (phrase_id = 0; phrase_id < data->n_phrases; phrase_id++) {
if (!grn_ii_select_data_find_phrase(ctx, data, phrase_id, data->pos, 0)) {
need_check = false;
break;
}
/* TODO: Can we update data->pos to reduce needless search? */
bt_push(data->bt, data->current_token_info);
}
} else if (data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
uint32_t i;
uint32_t phrase_id = 0;
for (i = 0; i < data->n_phrase_groups; i++) {
phrase_group *group = &(data->phrase_groups[i]);
bt_zap(group->btree);
uint32_t end_phrase_id = phrase_id + group->n_phrases;
bool have_phrase = false;
for (; phrase_id < end_phrase_id; phrase_id++) {
if (grn_ii_select_data_find_phrase(ctx,
data,
phrase_id,
data->pos,
0)) {
have_phrase = true;
bt_push(group->btree, data->current_token_info);
/* TODO: Can we update data->pos to reduce needless search? */
}
}
if (!have_phrase) {
need_check = false;
break;
}
bt_push(data->bt, group->btree->min);
}
} else {
uint32_t i;
uint32_t n = data->n_token_infos;
for (i = 0; i < n; i++) {
token_info *ti = data->token_infos[i];
bt_push(data->bt, ti);
}
}
if (!need_check) {
return true;
}
for (;;) {
data->current_token_info = data->bt->min;
token_info *min_token_info = data->bt->min;
token_info *max_token_info = data->bt->max;
int32_t min = min_token_info->pos;
uint32_t min_n_tokens_in_phrase = min_token_info->n_tokens_in_phrase;
int32_t max = max_token_info->pos;
if (min > max) {
char ii_name[GRN_TABLE_MAX_KEY_SIZE];
int ii_name_size;
ii_name_size = grn_obj_name(ctx,
(grn_obj *)(data->ii),
ii_name,
GRN_TABLE_MAX_KEY_SIZE);
ERR(GRN_FILE_CORRUPT,
"%s[%s] "
"max position must be larger than min position: "
"min:<%d> max:<%d> ii:<%.*s> string:<%.*s>",
tag,
grn_operator_to_string(data->mode),
min,
max,
ii_name_size,
ii_name,
data->query_len,
data->query);
return false;
}
int32_t interval = max - min;
if (data->mode == GRN_OP_NEAR_PHRASE ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE ||
data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
interval -= data->current_token_info->n_tokens_in_phrase;
}
P_NOTE("%s record:%u interval:%d min:%d max:%d\n",
__func__,
data->rid,
interval,
min,
max);
P_TI(ctx, min_token_info);
P_TI(ctx, max_token_info);
bool matched = false;
if (data->mode == GRN_OP_ORDERED_NEAR_PHRASE) {
matched = true;
uint32_t i;
uint32_t n = data->n_token_infos;
int32_t pos = data->token_infos[0]->pos;
for (i = 1; i < n; i++) {
token_info *ti = data->token_infos[i];
if (ti->pos < pos) {
matched = false;
break;
}
pos = ti->pos;
}
if (matched) {
matched =
grn_ii_select_data_is_matched_near_phrase(ctx, data, interval);
}
} else if (data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
matched = true;
uint32_t i;
uint32_t n = data->n_phrase_groups;
int32_t pos = data->phrase_groups[0].btree->min->pos;
for (i = 1; i < n; i++) {
token_info *ti = data->phrase_groups[i].btree->min;
if (ti->pos < pos) {
matched = false;
break;
}
pos = ti->pos;
}
if (matched) {
matched =
grn_ii_select_data_is_matched_near_phrase(ctx, data, interval);
}
} else if (data->bt->n_must_lasts > 0) {
matched =
(data->bt->n_must_lasts == 1 && data->bt->max->must_last &&
grn_ii_select_data_is_matched_near_phrase(ctx, data, interval));
} else {
matched = ((data->max_interval < 0 || interval <= data->max_interval) &&
interval >= data->min_interval);
}
if (matched) {
matched = grn_ii_select_data_check_near_element_intervals(ctx, data);
}
if (matched) {
data->n_occurrences++;
int32_t next_pos = max + 1;
if (data->mode == GRN_OP_NEAR_PHRASE ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE) {
if (!grn_ii_select_data_find_phrase(ctx,
data,
data->current_token_info->phrase_id,
next_pos,
0)) {
break;
}
} else if (data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
if (!grn_ii_select_data_find_phrase_product(ctx, data, next_pos, 0)) {
break;
}
} else {
if (!grn_ii_select_skip_pos(ctx, data, next_pos)) {
break;
}
}
} else {
if (data->mode == GRN_OP_NEAR_PHRASE ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE) {
int32_t next_pos = min + 1;
if (!grn_ii_select_data_find_phrase(ctx,
data,
data->current_token_info->phrase_id,
next_pos,
0)) {
break;
}
} else if (data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
uint32_t next_tokens_in_phrase = min_token_info->n_tokens_in_phrase + 1;
if (!grn_ii_select_data_find_phrase_product(ctx,
data,
min,
next_tokens_in_phrase)) {
break;
}
} else {
int32_t next_pos = max - data->max_interval;
if (next_pos <= min) {
next_pos = min + 1;
}
if (!grn_ii_select_skip_pos(ctx, data, next_pos)) {
break;
}
}
}
bt_reorder_min(data->bt);
if ((data->mode == GRN_OP_NEAR_PHRASE ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE ||
data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) &&
data->bt->min->pos == min &&
data->bt->min->n_tokens_in_phrase == min_n_tokens_in_phrase) {
break;
}
}
return true;
}
static inline bool
grn_ii_select_cursor_next_find(grn_ctx *ctx,
grn_ii_select_cursor *cursor,
const char *tag)
{
grn_ii_select_data *data = &(cursor->data);
uint32_t count = 0;
int32_t score = 0;
token_info **tis = data->token_infos;
token_info **tie = data->token_infos + data->n_token_infos;
token_info **tip;
for (tip = tis;; tip++) {
if (tip == tie) {
tip = tis;
}
data->current_token_info = *tip;
if (!grn_ii_select_skip_pos(ctx, data, data->pos)) {
break;
}
if (data->current_token_info->pos == (int32_t)(data->pos)) {
if (count == 0) {
data->start_pos = data->pos;
}
score += data->current_token_info->p->weight +
data->current_token_info->cursors->bins[0]->weight;
count++;
if ((int32_t)(data->current_token_info->p->pos) > data->end_pos) {
data->end_pos = data->current_token_info->p->pos;
}
} else {
score = data->current_token_info->p->weight +
data->current_token_info->cursors->bins[0]->weight;
count = 1;
data->start_pos = data->pos = data->current_token_info->pos;
data->end_pos = data->current_token_info->p->pos;
if (data->n_occurrences == 0 && data->score_func) {
GRN_BULK_REWIND(&(data->record.terms));
GRN_BULK_REWIND(&(data->record.term_weights));
data->record.n_candidates = 0;
data->record.n_tokens = 0;
}
}
if (data->n_occurrences == 0 && data->score_func) {
GRN_RECORD_PUT(ctx,
&(data->record.terms),
data->current_token_info->cursors->bins[0]->id);
GRN_UINT32_PUT(ctx,
&(data->record.term_weights),
data->current_token_info->p->weight +
data->current_token_info->cursors->bins[0]->weight);
data->record.n_candidates += data->current_token_info->size;
data->record.n_tokens += data->current_token_info->ntoken;
}
if (count == data->n_token_infos) {
if ((int32_t)(data->current_token_info->p->pos) > data->end_pos) {
data->end_pos = data->current_token_info->p->pos;
}
data->total_score += score;
score = 0;
count = 0;
data->pos++;
data->n_occurrences++;
if (cursor->per_occurrence_mode) {
break;
}
}
}
return true;
}
extern "C" grn_ii_select_cursor_posting *
grn_ii_select_cursor_next(grn_ctx *ctx, grn_ii_select_cursor *cursor)
{
const char *tag = "[ii][select][cursor][next]";
if (cursor->have_unshifted_posting) {
cursor->have_unshifted_posting = false;
return &(cursor->unshifted_posting);
}
if (cursor->done) {
return NULL;
}
grn_ii_select_data *data = &(cursor->data);
for (;;) {
data->may_match = true;
if (!grn_ii_select_cursor_next_prepare(ctx, cursor)) {
goto exit;
}
float weight = get_weight(ctx, data);
if (data->may_match && fpclassify(weight) == FP_ZERO) {
data->may_match = false;
}
if (data->may_match) {
if (data->op != GRN_OP_OR) {
if (data->result_set) {
grn_rset_posinfo pi = {data->rid, data->sid, 0};
grn_id id = grn_hash_get(ctx,
data->result_set,
&pi,
data->result_set->key_size,
NULL);
data->may_match = (id != GRN_ID_NIL);
}
}
}
if (!data->may_match) {
if (!grn_ii_select_cursor_next_skip(ctx, cursor)) {
goto exit;
}
continue;
}
data->start_pos = 0;
data->end_pos = 0;
data->total_score = 0;
data->n_occurrences = 0;
data->pos = 0;
if (data->score_func) {
GRN_BULK_REWIND(&(data->record.terms));
GRN_BULK_REWIND(&(data->record.term_weights));
data->record.n_candidates = 0;
data->record.n_tokens = 0;
}
if (data->n_token_infos == 1) {
if (!grn_ii_select_cursor_next_find_one_token(ctx, cursor, tag)) {
goto exit;
}
} else if (data->mode == GRN_OP_NEAR || data->mode == GRN_OP_NEAR_PHRASE ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE ||
data->mode == GRN_OP_NEAR_PHRASE_PRODUCT ||
data->mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) {
if (!grn_ii_select_cursor_next_find_near(ctx, cursor, tag)) {
goto exit;
}
} else {
if (!grn_ii_select_cursor_next_find(ctx, cursor, tag)) {
goto exit;
}
}
if (data->n_occurrences == 0) {
if (!grn_ii_select_cursor_next_skip(ctx, cursor)) {
goto exit;
}
continue;
}
double record_score;
if (data->score_func) {
data->record.id = data->rid;
data->record.weight = (int)weight;
data->record.n_occurrences = data->n_occurrences;
data->record.total_term_weights = data->total_score;
record_score = data->score_func(ctx, &(data->record)) * (double)weight;
} else {
record_score = (data->n_occurrences + data->total_score) * (double)weight;
}
if (data->set_min_enable_for_and_query) {
if (data->current_min == GRN_ID_NIL) {
data->current_min = data->rid;
}
}
cursor->posting.rid = data->rid;
cursor->posting.sid = data->sid;
cursor->posting.pos = data->pos;
cursor->posting.start_pos = data->start_pos;
cursor->posting.end_pos = data->end_pos;
cursor->posting.tf = data->n_occurrences;
cursor->posting.score = record_score;
if (cursor->per_occurrence_mode) {
if (token_info_skip_pos(ctx,
data->token_infos[0],
data->rid,
data->sid,
data->end_pos + 1) != GRN_SUCCESS) {
cursor->done = true;
}
} else {
if (!grn_ii_select_cursor_next_skip(ctx, cursor)) {
cursor->done = true;
}
}
return &(cursor->posting);
}
exit:
cursor->done = true;
return NULL;
}
static void
grn_ii_select_cursor_unshift(grn_ctx *ctx,
grn_ii_select_cursor *cursor,
grn_ii_select_cursor_posting *posting)
{
cursor->unshifted_posting = *posting;
cursor->have_unshifted_posting = true;
}
extern "C" grn_ii *
grn_ii_select_cursor_get_ii(grn_ctx *ctx, grn_ii_select_cursor *cursor)
{
return cursor->data.ii;
}
static void
grn_ii_parse_regexp_query_flush(grn_ctx *ctx,
grn_obj *parsed_strings,
grn_obj *parsed_n_following_characters,
grn_obj *string,
uint32_t *n_following_characters_start,
uint32_t *n_following_characters_end)
{
if (GRN_TEXT_LEN(string) > 0) {
grn_vector_add_element(ctx,
parsed_strings,
GRN_TEXT_VALUE(string),
GRN_TEXT_LEN(string),
0,
GRN_DB_TEXT);
GRN_BULK_REWIND(string);
GRN_UINT32_PUT(ctx,
parsed_n_following_characters,
*n_following_characters_start);
GRN_UINT32_PUT(ctx,
parsed_n_following_characters,
*n_following_characters_end);
}
*n_following_characters_start = 0;
*n_following_characters_end = 0;
}
static grn_rc
grn_ii_parse_regexp_query(grn_ctx *ctx,
const char *log_tag,
const char *string,
unsigned int string_len,
grn_obj *parsed_strings,
grn_obj *parsed_n_following_characters)
{
bool escaping = false;
bool in_paren = false;
const char *all_off_options = "?-mix:";
size_t all_off_options_len = strlen(all_off_options);
int nth_char = 0;
const char *current = string;
const char *string_end = string + string_len;
grn_obj buffer;
uint32_t n_following_characters_start = 0;
uint32_t n_following_characters_end = 0;
GRN_TEXT_INIT(&buffer, 0);
while (current < string_end) {
const char *target;
int char_len;
char_len = grn_charlen(ctx, current, string_end);
if (char_len == 0) {
GRN_OBJ_FIN(ctx, &buffer);
ERR(GRN_INVALID_ARGUMENT,
"%s invalid encoding character: <%.*s|%#x|>",
log_tag,
(int)(current - string),
string,
*current);
return ctx->rc;
}
target = current;
current += char_len;
if (escaping) {
escaping = false;
if (char_len == 1) {
switch (*target) {
case 'A':
if (nth_char == 0) {
target = GRN_TOKENIZER_BEGIN_MARK_UTF8;
char_len = GRN_TOKENIZER_BEGIN_MARK_UTF8_LEN;
}
break;
case 'z':
if (current == string_end ||
(in_paren && grn_charlen(ctx, current, string_end) == 1 &&
*current == ')' && (current + 1) == string_end)) {
target = GRN_TOKENIZER_END_MARK_UTF8;
char_len = GRN_TOKENIZER_END_MARK_UTF8_LEN;
}
break;
default:
break;
}
}
} else {
if (n_following_characters_start > 0 &&
(!(char_len == 1 && (*target == '.' || *target == '*')))) {
grn_ii_parse_regexp_query_flush(ctx,
parsed_strings,
parsed_n_following_characters,
&buffer,
&n_following_characters_start,
&n_following_characters_end);
}
if (char_len == 1) {
if (*target == '\\') {
escaping = true;
continue;
} else if (*target == '(' &&
(size_t)(string_end - current) >= all_off_options_len &&
memcmp(current, all_off_options, all_off_options_len) == 0) {
current += all_off_options_len;
in_paren = true;
continue;
} else if (*target == ')') {
in_paren = false;
continue;
} else if (*target == '.') {
n_following_characters_start++;
n_following_characters_end++;
continue;
} else if (*target == '*') {
/* ".*" is only supported for now. */
if (n_following_characters_start > 0) {
n_following_characters_start--;
n_following_characters_end--;
}
grn_ii_parse_regexp_query_flush(ctx,
parsed_strings,
parsed_n_following_characters,
&buffer,
&n_following_characters_start,
&n_following_characters_end);
continue;
}
}
}
GRN_TEXT_PUT(ctx, &buffer, target, char_len);
nth_char++;
}
grn_ii_parse_regexp_query_flush(ctx,
parsed_strings,
parsed_n_following_characters,
&buffer,
&n_following_characters_start,
&n_following_characters_end);
size_t parsed_n_following_characters_size =
GRN_UINT32_VECTOR_SIZE(parsed_n_following_characters);
if (parsed_n_following_characters_size > 0 &&
(GRN_UINT32_VALUE_AT(parsed_n_following_characters,
parsed_n_following_characters_size - 1) > 0)) {
/* 0 means no limit for now. It may be changed when we implement full
* the number of following characters range support. */
GRN_UINT32_VALUE_AT(parsed_n_following_characters,
parsed_n_following_characters_size - 1) = 0;
GRN_TEXT_SET(ctx,
&buffer,
GRN_TOKENIZER_END_MARK_UTF8,
GRN_TOKENIZER_END_MARK_UTF8_LEN);
grn_ii_parse_regexp_query_flush(ctx,
parsed_strings,
parsed_n_following_characters,
&buffer,
&n_following_characters_start,
&n_following_characters_end);
}
GRN_OBJ_FIN(ctx, &buffer);
return GRN_SUCCESS;
}
typedef struct {
grn_ii_select_cursor *cursor;
uint32_t n_following_characters_start;
uint32_t n_following_characters_end;
uint32_t next_start_pos_offset;
} grn_ii_select_regexp_chunk;
static grn_rc
grn_ii_select_regexp(grn_ctx *ctx,
grn_ii *ii,
const char *string,
unsigned int string_len,
grn_hash *s,
grn_operator op,
grn_select_optarg *optarg)
{
grn_rc rc;
grn_obj parsed_strings;
grn_obj parsed_n_following_characters;
unsigned int n_parsed_strings;
if (string_len == 0) {
return GRN_SUCCESS;
}
GRN_TEXT_INIT(&parsed_strings, GRN_OBJ_VECTOR);
GRN_UINT32_INIT(&parsed_n_following_characters, GRN_OBJ_VECTOR);
rc = grn_ii_parse_regexp_query(ctx,
"[ii][select][regexp]",
string,
string_len,
&parsed_strings,
&parsed_n_following_characters);
if (rc != GRN_SUCCESS) {
GRN_OBJ_FIN(ctx, &parsed_strings);
return rc;
}
if (optarg) {
optarg->mode = GRN_OP_EXACT;
}
n_parsed_strings = grn_vector_size(ctx, &parsed_strings);
if (n_parsed_strings == 1 &&
GRN_UINT32_VALUE_AT(&parsed_n_following_characters, 0) == 0) {
const char *parsed_string;
unsigned int parsed_string_len;
parsed_string_len = grn_vector_get_element(ctx,
&parsed_strings,
0,
&parsed_string,
NULL,
NULL);
rc =
grn_ii_select(ctx, ii, parsed_string, parsed_string_len, s, op, optarg);
} else {
unsigned int i;
bool have_error = false;
auto chunks = static_cast<grn_ii_select_regexp_chunk *>(
GRN_CALLOC(sizeof(grn_ii_select_regexp_chunk) * n_parsed_strings));
for (i = 0; i < n_parsed_strings; i++) {
const char *parsed_string;
unsigned int parsed_string_len;
parsed_string_len = grn_vector_get_element(ctx,
&parsed_strings,
i,
&parsed_string,
NULL,
NULL);
chunks[i].cursor = grn_ii_select_cursor_open(ctx,
ii,
parsed_string,
parsed_string_len,
optarg);
if (!chunks[i].cursor) {
have_error = true;
break;
}
grn_ii_select_cursor_set_per_occurrence_mode(ctx, chunks[i].cursor, true);
chunks[i].n_following_characters_start =
GRN_UINT32_VALUE_AT(&parsed_n_following_characters, i * 2);
chunks[i].n_following_characters_end =
GRN_UINT32_VALUE_AT(&parsed_n_following_characters, i * 2 + 1);
uint32_t next_start_pos_offset = chunks[i].n_following_characters_start;
if (chunks[i].n_following_characters_start > 0) {
size_t n_characters = 0;
const char *current = parsed_string;
const char *parsed_string_end = parsed_string + parsed_string_len;
while (current < parsed_string_end) {
int char_len = grn_charlen(ctx, current, parsed_string_end);
if (char_len == 0) { /* must not happen */
break;
}
current += char_len;
n_characters++;
}
if (n_characters > 1) {
/* text: "abcd", query: "a." case:
* tokens: "ab", "bc", "cd", "d"
* n_characters: 1
* n_following_characters: 1
* There are no token that is ignored after the "ab" token.
*
* text: "abcd", query: "ab." case:
* tokens: "ab", "bc", "cd", "d"
* n_characters: 2
* n_following_characters: 1
* The "bc" token must be ignored because the cursor returns
* the position for the "ab" token.
*/
next_start_pos_offset++;
}
next_start_pos_offset++;
}
chunks[i].next_start_pos_offset = next_start_pos_offset;
}
while (!have_error) {
grn_ii_select_cursor_posting *posting;
posting = grn_ii_select_cursor_next(ctx, chunks[0].cursor);
if (!posting) {
break;
}
uint32_t pos = posting->end_pos;
uint32_t n_following_characters_start =
chunks[0].n_following_characters_start;
uint32_t n_following_characters_end =
chunks[0].n_following_characters_end;
uint32_t next_start_pos = pos + chunks[0].next_start_pos_offset;
for (i = 1; i < n_parsed_strings; i++) {
grn_ii_select_cursor_posting *posting_i;
for (;;) {
posting_i = grn_ii_select_cursor_next(ctx, chunks[i].cursor);
if (!posting_i) {
break;
}
if (posting_i->rid > posting->rid) {
grn_ii_select_cursor_unshift(ctx, chunks[i].cursor, posting_i);
break;
}
if (posting_i->sid > posting->sid) {
grn_ii_select_cursor_unshift(ctx, chunks[i].cursor, posting_i);
break;
}
if (posting_i->rid == posting->rid &&
posting_i->sid == posting->sid) {
if (n_following_characters_start == 0) {
if (posting_i->start_pos > pos) {
grn_ii_select_cursor_unshift(ctx, chunks[i].cursor, posting_i);
break;
}
} else {
if (posting_i->start_pos >= next_start_pos) {
grn_ii_select_cursor_unshift(ctx, chunks[i].cursor, posting_i);
break;
}
}
}
}
if (!posting_i) {
break;
}
if (posting_i->rid != posting->rid) {
break;
}
if (posting_i->sid != posting->sid) {
break;
}
if (n_following_characters_start > 0) {
if (n_following_characters_end == 0) { /* 0 means no limit for now. */
if (posting_i->start_pos < next_start_pos) {
break;
}
} else {
if (posting_i->start_pos != next_start_pos) {
break;
}
}
}
pos = posting_i->end_pos;
n_following_characters_start = chunks[i].n_following_characters_start;
n_following_characters_end = chunks[i].n_following_characters_end;
next_start_pos = pos + chunks[i].next_start_pos_offset;
}
if (i == n_parsed_strings && n_following_characters_start == 0) {
grn_rset_posinfo pi = {posting->rid, posting->sid, pos};
double record_score = 1.0;
res_add(ctx, s, &pi, record_score, op);
if (ctx->rc != GRN_SUCCESS) {
rc = ctx->rc;
break;
}
}
}
for (i = 0; i < n_parsed_strings; i++) {
if (chunks[i].cursor) {
grn_ii_select_cursor_close(ctx, chunks[i].cursor);
}
}
GRN_FREE(chunks);
}
GRN_OBJ_FIN(ctx, &parsed_strings);
GRN_OBJ_FIN(ctx, &parsed_n_following_characters);
if (optarg) {
optarg->mode = GRN_OP_REGEXP;
}
return rc;
}
typedef struct {
uint32_t n_occurs;
double score;
} grn_ii_quorum_match_record_data;
extern "C" grn_rc
grn_ii_quorum_match(grn_ctx *ctx, grn_ii *ii, grn_ii_select_data *data)
{
size_t record_key_size;
grn_hash *n_occurs;
grn_token_cursor *token_cursor;
int ii_cursor_flags = 0;
record_key_size = sizeof(grn_id);
if (ii->header.common->flags & GRN_OBJ_WITH_SECTION) {
record_key_size += sizeof(uint32_t);
ii_cursor_flags |= GRN_OBJ_WITH_SECTION;
}
n_occurs = grn_hash_create(ctx,
NULL,
record_key_size,
sizeof(grn_ii_quorum_match_record_data),
0);
if (!n_occurs) {
return GRN_NO_MEMORY_AVAILABLE;
}
token_cursor = grn_ii_select_data_open_token_cursor(ctx,
data,
data->query,
data->query_len,
GRN_TOKENIZE_GET);
if (!token_cursor) {
grn_rc rc = ctx->rc;
char ii_name[GRN_TABLE_MAX_KEY_SIZE];
int ii_name_len;
char errbuf[GRN_CTX_MSGSIZE];
if (rc == GRN_SUCCESS) {
rc = GRN_UNKNOWN_ERROR;
}
ii_name_len =
grn_obj_name(ctx, (grn_obj *)ii, ii_name, GRN_TABLE_MAX_KEY_SIZE);
grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
ERR(rc,
"[ii][quorum-match] failed to create token cursor: <%.*s>: <%.*s>%s%s",
ii_name_len,
ii_name,
data->query_len,
data->query,
errbuf[0] ? ": " : "",
errbuf[0] ? errbuf : "");
grn_hash_close(ctx, n_occurs);
return rc;
}
while (token_cursor->status != GRN_TOKEN_CURSOR_DONE &&
token_cursor->status != GRN_TOKEN_CURSOR_DONE_SKIP) {
grn_id tid;
if ((tid = grn_token_cursor_next(ctx, token_cursor)) != GRN_ID_NIL) {
grn_ii_cursor *cursor;
cursor = grn_ii_cursor_open(ctx,
ii,
tid,
GRN_ID_NIL,
GRN_ID_MAX,
ii->n_elements,
ii_cursor_flags);
while (grn_ii_cursor_next(ctx, cursor)) {
grn_ii_quorum_match_record_data *record_data;
data->rid = cursor->post->rid;
data->sid = cursor->post->sid;
float weight = get_weight(ctx, data);
if (fpclassify(weight) == FP_ZERO) {
continue;
}
if (grn_hash_add(ctx,
n_occurs,
cursor->post,
record_key_size,
(void **)&record_data,
NULL)) {
record_data->n_occurs++;
record_data->score += weight;
}
}
grn_ii_cursor_close(ctx, cursor);
}
}
grn_token_cursor_close(ctx, token_cursor);
{
grn_rset_posinfo posinfo;
grn_hash_cursor *cursor;
memset(&posinfo, 0, sizeof(grn_rset_posinfo));
cursor = grn_hash_cursor_open(ctx, n_occurs, NULL, 0, NULL, 0, 0, -1, 0);
if (!cursor) {
grn_rc rc = ctx->rc;
char ii_name[GRN_TABLE_MAX_KEY_SIZE];
int ii_name_len;
char errbuf[GRN_CTX_MSGSIZE];
if (rc == GRN_SUCCESS) {
rc = GRN_UNKNOWN_ERROR;
}
ii_name_len =
grn_obj_name(ctx, (grn_obj *)ii, ii_name, GRN_TABLE_MAX_KEY_SIZE);
grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
ERR(
rc,
"[ii][quorum-match] failed to create count cursor: <%.*s>: <%.*s>%s%s",
ii_name_len,
ii_name,
data->query_len,
data->query,
errbuf[0] ? ": " : "",
errbuf[0] ? errbuf : "");
grn_hash_close(ctx, n_occurs);
return rc;
}
{
const uint32_t quorum_threshold = data->optarg->quorum_threshold;
while (grn_hash_cursor_next(ctx, cursor) != GRN_ID_NIL) {
grn_posting *posting;
grn_ii_quorum_match_record_data *record_data;
grn_hash_cursor_get_key_value(ctx,
cursor,
(void **)&posting,
NULL,
(void **)&record_data);
if (record_data->n_occurs >= quorum_threshold) {
double score = record_data->score;
grn_memcpy(&posinfo, posting, record_key_size);
if (data->score_func) {
data->record.id = posting->rid;
data->record.weight = (int)(score / record_data->n_occurs);
data->record.n_occurrences = record_data->n_occurs;
data->record.total_term_weights = data->record.weight;
score =
data->score_func(ctx, &(data->record)) * data->record.weight;
}
res_add(ctx, data->result_set, &posinfo, score, data->op);
}
}
}
grn_hash_cursor_close(ctx, cursor);
}
grn_hash_close(ctx, n_occurs);
grn_ii_resolve_sel_and(ctx, data->result_set, data->op);
return GRN_SUCCESS;
}
static bool
grn_ii_select_sequential_search_reference_should_use(grn_ctx *ctx,
grn_ii_select_data *data)
{
int n_sources;
if (grn_ii_select_too_many_index_match_ratio_reference < 0.0) {
return false;
}
if (data->op != GRN_OP_AND) {
return false;
}
if (data->mode != GRN_OP_EXACT) {
return false;
}
n_sources = DB_OBJ(data->ii)->source_size / sizeof(grn_id);
if (n_sources == 0) {
return false;
}
{
grn_obj *tokenizer;
grn_table_get_info(ctx, data->lexicon, NULL, NULL, &tokenizer, NULL, NULL);
if (tokenizer) {
return false;
}
}
{
int i;
auto source_ids = static_cast<grn_id *>(DB_OBJ(data->ii)->source);
for (i = 0; i < n_sources; i++) {
grn_id source_id = source_ids[i];
grn_obj *source;
source = grn_ctx_at(ctx, source_id);
if (!source) {
return false;
}
const bool is_reference_column = grn_obj_is_reference_column(ctx, source);
grn_obj_unref(ctx, source);
if (!is_reference_column) {
return false;
}
}
}
{
uint32_t n_existing_records = GRN_HASH_SIZE(data->result_set);
uint32_t i;
for (i = 0; i < data->n_token_infos; i++) {
token_info *info = data->token_infos[i];
if (n_existing_records <=
(info->size * grn_ii_select_too_many_index_match_ratio_reference)) {
grn_obj reason;
GRN_TEXT_INIT(&reason, 0);
grn_text_printf(
ctx,
&reason,
"enough filtered and "
"index search will match many records: "
"%u <= %.2f: "
"n-existing-records:%u "
"estimated-size:%u "
"ratio:%.2f%%",
n_existing_records,
info->size * grn_ii_select_too_many_index_match_ratio_reference,
n_existing_records,
info->size,
grn_ii_select_too_many_index_match_ratio_reference * 100);
GRN_TEXT_PUTC(ctx, &reason, '\0');
grn_report_index_not_used(ctx,
"[ii][select]",
"[reference]",
(grn_obj *)(data->ii),
GRN_TEXT_VALUE(&reason));
GRN_OBJ_FIN(ctx, &reason);
return true;
}
}
return false;
}
}
static bool
grn_ii_select_sequential_search_reference(grn_ctx *ctx,
grn_ii_select_data *data)
{
const grn_id query_id = data->token_infos[0]->cursors[0].bins[0]->id;
const grn_id lexicon_id = DB_OBJ(data->ii->lexicon)->id;
auto source_ids = static_cast<grn_id *>(DB_OBJ(data->ii)->source);
const size_t n_sources = DB_OBJ(data->ii)->source_size / sizeof(grn_id);
size_t i;
for (i = 0; i < n_sources; i++) {
const grn_id source_id = source_ids[i];
grn_obj *source = grn_ctx_at(ctx, source_id);
grn_obj value;
switch (source->header.flags & GRN_OBJ_COLUMN_TYPE_MASK) {
case GRN_OBJ_COLUMN_SCALAR:
GRN_RECORD_INIT(&value, 0, lexicon_id);
break;
case GRN_OBJ_COLUMN_VECTOR:
GRN_RECORD_INIT(&value, GRN_OBJ_VECTOR, lexicon_id);
break;
default:
grn_obj_unref(ctx, source);
return false;
}
GRN_HASH_EACH_BEGIN(ctx, data->result_set, cursor, id)
{
void *key;
grn_hash_cursor_get_key(ctx, cursor, &key);
grn_id source_table_id = *((grn_id *)key);
GRN_BULK_REWIND(&value);
grn_obj_get_value(ctx, source, source_table_id, &value);
switch (value.header.type) {
case GRN_BULK:
if (GRN_RECORD_VALUE(&value) == query_id) {
grn_rset_posinfo info;
info.rid = source_table_id;
info.sid = i + 1;
info.pos = 0;
data->rid = info.rid;
data->sid = info.sid;
float score = get_weight(ctx, data);
res_add(ctx, data->result_set, &info, score, data->op);
}
break;
case GRN_UVECTOR:
{
uint32_t n_elements = grn_vector_size(ctx, &value);
uint32_t j;
uint32_t n_founds = 0;
for (j = 0; j < n_elements; j++) {
if (GRN_RECORD_VALUE_AT(&value, j) == query_id) {
n_founds++;
}
}
if (n_founds > 0) {
grn_rset_posinfo info;
info.rid = source_table_id;
info.sid = i + 1;
info.pos = 0;
data->rid = info.rid;
data->sid = info.sid;
float score = get_weight(ctx, data);
res_add(ctx, data->result_set, &info, score * n_founds, data->op);
}
}
break;
default:
break;
}
}
GRN_HASH_EACH_END(ctx, cursor);
GRN_OBJ_FIN(ctx, &value);
grn_obj_unref(ctx, source);
}
return true;
}
#ifdef GRN_II_SELECT_ENABLE_SEQUENTIAL_SEARCH_TEXT
static bool
grn_ii_select_sequential_search_text_should_use(grn_ctx *ctx,
grn_ii_select_data *data)
{
if (grn_ii_select_too_many_index_match_ratio_text < 0.0) {
return false;
}
if (data->op != GRN_OP_AND) {
return false;
}
if (data->mode != GRN_OP_EXACT) {
return false;
}
size_t n_sources = DB_OBJ(data->ii)->source_size / sizeof(grn_id);
if (n_sources == 0) {
return false;
}
{
size_t i;
auto source_ids = static_cast<grn_id *>(DB_OBJ(data->ii)->source);
for (i = 0; i < n_sources; i++) {
grn_id source_id = source_ids[i];
grn_obj *source;
source = grn_ctx_at(ctx, source_id);
if (!source) {
return false;
}
grn_id value_type_id;
if (grn_obj_is_table(ctx, source)) {
value_type_id = source->header.domain;
} else {
value_type_id = grn_obj_get_range(ctx, source);
}
const bool is_text_family =
grn_type_id_is_text_family(ctx, value_type_id);
;
grn_obj_unref(ctx, source);
if (!is_text_family) {
return false;
}
}
}
{
uint32_t n_existing_records = GRN_HASH_SIZE(data->result_set);
uint32_t i;
for (i = 0; i < data->n_token_infos; i++) {
token_info *info = data->token_infos[i];
if (n_existing_records <=
(info->size * grn_ii_select_too_many_index_match_ratio_text)) {
grn_obj reason;
GRN_TEXT_INIT(&reason, 0);
grn_text_printf(ctx,
&reason,
"enough filtered and "
"index search will match many records: "
"%u <= %.2f: "
"n-existing-records:%u "
"estimated-size:%u "
"ratio:%.2f%%",
n_existing_records,
info->size *
grn_ii_select_too_many_index_match_ratio_text,
n_existing_records,
info->size,
grn_ii_select_too_many_index_match_ratio_text * 100);
GRN_TEXT_PUTC(ctx, &reason, '\0');
grn_report_index_not_used(ctx,
"[ii][select]",
"[text]",
(grn_obj *)(data->ii),
GRN_TEXT_VALUE(&reason));
GRN_OBJ_FIN(ctx, &reason);
return true;
}
}
return false;
}
}
static void
grn_ii_select_sequential_search_text_body(grn_ctx *ctx,
grn_ii_select_data *data,
grn_encoding encoding,
OnigRegex regex)
{
grn_obj buffer;
GRN_TEXT_INIT(&buffer, 0);
auto source_ids = static_cast<grn_id *>(DB_OBJ(data->ii)->source);
const size_t n_sources = DB_OBJ(data->ii)->source_size / sizeof(grn_id);
size_t i;
for (i = 0; i < n_sources; i++) {
const grn_id source_id = source_ids[i];
grn_obj *source = grn_ctx_at(ctx, source_id);
const bool source_is_table = grn_obj_is_table(ctx, source);
GRN_HASH_EACH_BEGIN(ctx, data->result_set, cursor, id)
{
void *key;
grn_hash_cursor_get_key(ctx, cursor, &key);
grn_id source_table_id = *((grn_id *)key);
GRN_BULK_REWIND(&buffer);
if (source_is_table) {
grn_table_get_key2(ctx, source, source_table_id, &buffer);
} else {
grn_obj_get_value(ctx, source, source_table_id, &buffer);
}
OnigPosition position;
grn_obj *value;
const char *normalized_value;
unsigned int normalized_value_length;
value = grn_string_open_(ctx,
GRN_TEXT_VALUE(&buffer),
GRN_TEXT_LEN(&buffer),
data->ii->lexicon,
0,
encoding);
grn_string_get_normalized(ctx,
value,
&normalized_value,
&normalized_value_length,
NULL);
const OnigUChar *onig_value =
reinterpret_cast<const OnigUChar *>(normalized_value);
position = onig_search(regex,
onig_value,
onig_value + normalized_value_length,
onig_value,
onig_value + normalized_value_length,
NULL,
0);
if (position != ONIG_MISMATCH) {
grn_rset_posinfo info;
info.rid = source_table_id;
info.sid = i + 1;
info.pos = 0;
data->rid = info.rid;
data->sid = info.rid;
float score = get_weight(ctx, data);
res_add(ctx, data->result_set, &info, score, data->op);
}
grn_obj_unlink(ctx, value);
}
GRN_HASH_EACH_END(ctx, cursor);
grn_obj_unref(ctx, source);
}
GRN_OBJ_FIN(ctx, &buffer);
}
static bool
grn_ii_select_sequential_search_text(grn_ctx *ctx, grn_ii_select_data *data)
{
bool processed = false;
{
grn_encoding encoding;
grn_table_get_info(ctx,
data->ii->lexicon,
NULL,
&encoding,
NULL,
NULL,
NULL);
int nflags = 0;
grn_obj *query = grn_string_open_(ctx,
data->query,
data->query_len,
data->ii->lexicon,
nflags,
encoding);
const char *normalized_query;
unsigned int normalized_query_length;
grn_string_get_normalized(ctx,
query,
&normalized_query,
&normalized_query_length,
NULL);
{
OnigRegex regex;
regex = grn_onigmo_new(ctx,
normalized_query,
normalized_query_length,
ONIG_OPTION_NONE,
ONIG_SYNTAX_ASIS,
"[ii][select][sequential]");
if (regex) {
grn_ii_select_sequential_search_text_body(ctx, data, encoding, regex);
onig_free(regex);
processed = true;
} else {
processed = false;
}
}
grn_obj_unlink(ctx, query);
}
return processed;
}
#endif
static bool
grn_ii_select_sequential_search(grn_ctx *ctx, grn_ii_select_data *data)
{
if (grn_ii_select_sequential_search_reference_should_use(ctx, data)) {
return grn_ii_select_sequential_search_reference(ctx, data);
}
#ifdef GRN_II_SELECT_ENABLE_SEQUENTIAL_SEARCH_TEXT
else if (grn_ii_select_sequential_search_text_should_use(ctx, data)) {
return grn_ii_select_sequential_search_text(ctx, data);
}
#endif
return false;
}
extern "C" grn_rc
grn_ii_select(grn_ctx *ctx,
grn_ii *ii,
const char *string,
unsigned int string_len,
grn_hash *s,
grn_operator op,
grn_select_optarg *optarg)
{
const char *tag = "[ii][select]";
int rep, orp;
if (!ii) {
return GRN_INVALID_ARGUMENT;
}
grn_ii_select_cursor cursor;
grn_ii_select_data *data;
data = &(cursor.data);
data->ii = ii;
data->lexicon = data->ii->lexicon;
if (!data->lexicon || !s) {
return GRN_INVALID_ARGUMENT;
}
grn_selector_data_current_set_default_tag_raw(ctx, string, string_len);
data->query = string;
data->query_len = string_len;
data->result_set = s;
data->op = op;
grn_ii_select_data_init(ctx, data, optarg);
if (data->mode == GRN_OP_SIMILAR) {
grn_rc rc = grn_ii_similar_search_internal(ctx, data);
grn_ii_select_data_fin(ctx, data);
return rc;
}
if (data->mode == GRN_OP_TERM_EXTRACT) {
grn_rc rc = grn_ii_term_extract_internal(ctx, data);
grn_ii_select_data_fin(ctx, data);
return rc;
}
if (data->mode == GRN_OP_REGEXP) {
grn_rc rc =
grn_ii_select_regexp(ctx, ii, string, string_len, s, op, optarg);
grn_ii_select_data_fin(ctx, data);
return rc;
}
if (data->mode == GRN_OP_QUORUM) {
grn_rc rc = grn_ii_quorum_match(ctx, ii, data);
grn_ii_select_data_fin(ctx, data);
return rc;
}
/* todo : support subrec
rep = (s->record_unit == GRN_REC_POSITION || s->subrec_unit ==
GRN_REC_POSITION); orp = (s->record_unit == GRN_REC_POSITION || op ==
GRN_OP_OR);
*/
rep = 0;
orp = op == GRN_OP_OR;
if (!grn_ii_select_data_init_token_infos(ctx, data, tag)) {
goto exit;
}
if (grn_logger_pass(ctx, GRN_LOG_INFO)) {
if (grn_type_id_is_text_family(ctx, ii->lexicon->header.domain)) {
GRN_LOG(ctx,
GRN_LOG_INFO,
"%s n=%d (%.*s)",
tag,
data->n_token_infos,
string_len,
string);
} else {
grn_obj inspected;
GRN_TEXT_INIT(&inspected, 0);
grn_inspect_key(ctx, &inspected, ii->lexicon, string, string_len);
GRN_LOG(ctx,
GRN_LOG_INFO,
"%s n=%d (%.*s)",
tag,
data->n_token_infos,
(int)GRN_TEXT_LEN(&inspected),
GRN_TEXT_VALUE(&inspected));
GRN_OBJ_FIN(ctx, &inspected);
}
}
/* todo : array as result
if (n == 1 && (*tis)->cursors->n_entries == 1 && op == GRN_OP_OR
&& !GRN_HASH_SIZE(s) && !s->garbages
&& s->record_unit == GRN_REC_DOCUMENT && !s->max_n_subrecs
&& grn_ii_max_section(ii) == 1) {
grn_ii_cursor *c = (*tis)->cursors->bins[0];
if ((rc = grn_hash_array_init(s, (*tis)->size + 32768))) { goto exit; }
do {
grn_rset_recinfo *ri;
grn_posting *p = c->post;
if ((weight = get_weight(ctx, s, p->rid, p->sid, wvm, optarg))) {
GRN_HASH_INT_ADD(s, p, ri);
ri->score = (p->tf + p->score) * weight;
ri->n_subrecs = 1;
}
} while (grn_ii_cursor_next(ctx, c));
goto exit;
}
*/
if (grn_ii_select_sequential_search(ctx, data)) {
goto exit;
}
grn_ii_select_cursor_init_internal(ctx, &cursor);
grn_result_set_add_ii_select_cursor(ctx, s, &cursor, op);
exit:
if (!data->only_skip_token) {
grn_ii_resolve_sel_and(ctx, s, op);
}
grn_ii_select_data_fin(ctx, data);
// grn_hash_cursor_clear(r);
#ifdef DEBUG
{
uint32_t i;
uint32_t n_segments = ii->seg->header->max_segment;
uint32_t n_nrefs = 0;
grn_io_mapinfo *info = ii->seg->maps;
for (i = 0; i < n_segments; i++, info++) {
if (info->nref > 0) {
n_nrefs++;
}
}
GRN_LOG(ctx, GRN_LOG_INFO, "n_nrefs=%u", n_nrefs);
}
#endif /* DEBUG */
return ctx->rc;
}
static void
grn_select_optarg_init_by_search_optarg(grn_ctx *ctx,
grn_select_optarg *select_optarg,
grn_search_optarg *search_optarg)
{
memset(select_optarg, 0, sizeof(grn_select_optarg));
select_optarg->mode = GRN_OP_EXACT;
if (!search_optarg) {
return;
}
switch (search_optarg->mode) {
case GRN_OP_NEAR:
case GRN_OP_NEAR_NO_OFFSET:
select_optarg->mode = search_optarg->mode;
select_optarg->max_interval = search_optarg->max_interval;
select_optarg->max_element_intervals = search_optarg->max_element_intervals;
select_optarg->min_interval = search_optarg->min_interval
? *search_optarg->min_interval
: GRN_II_DEFAULT_NEAR_MIN_INTERVAL;
break;
case GRN_OP_NEAR_PHRASE:
case GRN_OP_ORDERED_NEAR_PHRASE:
case GRN_OP_NEAR_PHRASE_PRODUCT:
case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT:
select_optarg->mode = search_optarg->mode;
select_optarg->max_interval = search_optarg->max_interval;
select_optarg->additional_last_interval =
search_optarg->additional_last_interval;
select_optarg->max_element_intervals = search_optarg->max_element_intervals;
select_optarg->min_interval = search_optarg->min_interval
? *search_optarg->min_interval
: GRN_II_DEFAULT_NEAR_MIN_INTERVAL;
break;
case GRN_OP_SIMILAR:
select_optarg->mode = search_optarg->mode;
select_optarg->similarity_threshold = search_optarg->similarity_threshold;
break;
case GRN_OP_REGEXP:
select_optarg->mode = search_optarg->mode;
break;
case GRN_OP_FUZZY:
select_optarg->mode = search_optarg->mode;
select_optarg->fuzzy = search_optarg->fuzzy;
break;
case GRN_OP_QUORUM:
select_optarg->mode = search_optarg->mode;
select_optarg->quorum_threshold = search_optarg->quorum_threshold;
break;
default:
break;
}
if (search_optarg->vector_size != 0) {
select_optarg->weight_vector = search_optarg->weight_vector;
select_optarg->weight_vector_float = search_optarg->weight_vector_float;
select_optarg->vector_size = search_optarg->vector_size;
select_optarg->weight_float = search_optarg->weight_float;
}
select_optarg->scorer = search_optarg->scorer;
select_optarg->scorer_args_expr = search_optarg->scorer_args_expr;
select_optarg->scorer_args_expr_offset =
search_optarg->scorer_args_expr_offset;
select_optarg->fuzzy = search_optarg->fuzzy;
select_optarg->match_info = &(search_optarg->match_info);
select_optarg->query_options = search_optarg->query_options;
if (search_optarg->query_domain) {
select_optarg->query_domain = *(search_optarg->query_domain);
} else {
select_optarg->query_domain = GRN_ID_NIL;
}
}
static uint32_t
grn_ii_estimate_size_for_query_regexp(grn_ctx *ctx,
grn_ii *ii,
const char *query,
unsigned int query_len,
grn_search_optarg *optarg)
{
grn_rc rc;
grn_obj parsed_strings;
grn_obj parsed_n_following_characters;
uint32_t size;
GRN_TEXT_INIT(&parsed_strings, GRN_OBJ_VECTOR);
GRN_UINT32_INIT(&parsed_n_following_characters, GRN_OBJ_VECTOR);
rc = grn_ii_parse_regexp_query(ctx,
"[ii][estimate-size][query][regexp]",
query,
query_len,
&parsed_strings,
&parsed_n_following_characters);
if (rc != GRN_SUCCESS) {
GRN_OBJ_FIN(ctx, &parsed_strings);
return 0;
}
if (optarg) {
optarg->mode = GRN_OP_EXACT;
}
unsigned int i;
unsigned int n_parsed_strings = grn_vector_size(ctx, &parsed_strings);
for (i = 0; i < n_parsed_strings; i++) {
const char *parsed_string;
unsigned int parsed_string_len;
parsed_string_len = grn_vector_get_element(ctx,
&parsed_strings,
0,
&parsed_string,
NULL,
NULL);
size_t estimated_size = grn_ii_estimate_size_for_query(ctx,
ii,
parsed_string,
parsed_string_len,
optarg);
if (i == 0) {
size = estimated_size;
} else {
if (estimated_size < size) {
size = estimated_size;
}
}
}
GRN_OBJ_FIN(ctx, &parsed_strings);
if (optarg) {
optarg->mode = GRN_OP_REGEXP;
}
return size;
}
extern "C" uint32_t
grn_ii_estimate_size_for_query(grn_ctx *ctx,
grn_ii *ii,
const char *query,
unsigned int query_len,
grn_search_optarg *optarg)
{
const char *tag = "[ii][estimate][size][query]";
if (query_len == 0) {
return 0;
}
grn_select_optarg select_optarg;
grn_select_optarg_init_by_search_optarg(ctx, &select_optarg, optarg);
grn_ii_select_data data;
data.ii = ii;
data.lexicon = data.ii->lexicon;
data.query = query;
data.query_len = query_len;
grn_ii_select_data_init(ctx, &data, &select_optarg);
if (data.mode == GRN_OP_REGEXP) {
grn_ii_select_data_fin(ctx, &data);
return grn_ii_estimate_size_for_query_regexp(ctx,
ii,
query,
query_len,
optarg);
}
if (!grn_ii_select_data_init_token_infos(ctx, &data, tag)) {
grn_ii_select_data_fin(ctx, &data);
return 0;
}
uint32_t i;
double estimated_size = 0;
double normalized_ratio = 1.0;
for (i = 0; i < data.n_token_infos; i++) {
token_info *ti = data.token_infos[i];
double term_estimated_size;
term_estimated_size = ((double)ti->size / ti->ntoken);
if (i == 0) {
estimated_size = term_estimated_size;
} else {
if (term_estimated_size < estimated_size) {
estimated_size = term_estimated_size;
}
normalized_ratio *= grn_ii_estimate_size_for_query_reduce_ratio;
}
}
estimated_size *= normalized_ratio;
if (estimated_size > 0.0 && estimated_size < 1.0) {
estimated_size = 1.0;
}
grn_ii_select_data_fin(ctx, &data);
return (uint32_t)estimated_size;
}
extern "C" uint32_t
grn_ii_estimate_size_for_lexicon_cursor(grn_ctx *ctx,
grn_ii *ii,
grn_table_cursor *lexicon_cursor)
{
grn_id term_id;
uint32_t estimated_size = 0;
while ((term_id = grn_table_cursor_next(ctx, lexicon_cursor)) != GRN_ID_NIL) {
uint32_t term_estimated_size;
term_estimated_size = grn_ii_estimate_size(ctx, ii, term_id);
estimated_size += term_estimated_size;
}
return estimated_size;
}
extern "C" grn_rc
grn_ii_sel(grn_ctx *ctx,
grn_ii *ii,
const char *string,
unsigned int string_len,
grn_hash *s,
grn_operator op,
grn_search_optarg *optarg)
{
ERRCLR(ctx);
if (grn_logger_pass(ctx, GRN_LOG_INFO)) {
grn_id query_domain = GRN_ID_NIL;
if (optarg && optarg->query_domain) {
query_domain = *(optarg->query_domain);
}
if (query_domain == GRN_ID_NIL) {
query_domain = ii->lexicon->header.domain;
}
if (grn_type_id_is_text_family(ctx, query_domain)) {
GRN_LOG(ctx, GRN_LOG_INFO, "grn_ii_sel > (%.*s)", string_len, string);
} else {
grn_obj inspected;
GRN_TEXT_INIT(&inspected, 0);
if (query_domain == ii->lexicon->header.domain) {
grn_inspect_key(ctx, &inspected, ii->lexicon, string, string_len);
} else {
grn_obj query;
GRN_OBJ_INIT(&query, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, query_domain);
GRN_TEXT_SET(ctx, &query, string, string_len);
grn_inspect(ctx, &inspected, &query);
GRN_OBJ_FIN(ctx, &query);
}
GRN_LOG(ctx,
GRN_LOG_INFO,
"grn_ii_sel > (%.*s)",
(int)GRN_TEXT_LEN(&inspected),
GRN_TEXT_VALUE(&inspected));
GRN_OBJ_FIN(ctx, &inspected);
}
}
if (!s) {
return GRN_INVALID_ARGUMENT;
}
uint16_t start_depth = grn_ctx_trace_log_get_current_depth(ctx);
grn_ctx_trace_log_push(ctx);
grn_ctx_trace_log_emit_string(ctx, "ii.select.input", string, string_len);
grn_ctx_trace_log_emit_cstring(ctx,
"ii.select.operator",
grn_operator_to_string(op));
{
grn_select_optarg arg;
grn_select_optarg_init_by_search_optarg(ctx, &arg, optarg);
/* todo : support subrec
grn_rset_init(ctx, s, GRN_REC_DOCUMENT, 0, GRN_REC_NONE, 0, 0);
*/
grn_ctx_trace_log_push(ctx);
if (grn_ii_select(ctx, ii, string, string_len, s, op, &arg)) {
GRN_LOG(ctx, GRN_LOG_ERROR, "grn_ii_select on grn_ii_sel() failed!");
goto exit;
}
GRN_LOG(ctx, GRN_LOG_INFO, "exact: %d", GRN_HASH_SIZE(s));
grn_ctx_trace_log_emit_uint32(ctx,
"ii.select.exact.n_hits",
GRN_HASH_SIZE(s));
grn_ctx_trace_log_pop(ctx);
if (op == GRN_OP_OR || ctx->impl->force_match_escalation) {
grn_id min = GRN_ID_NIL;
if (((int64_t)GRN_HASH_SIZE(s) <= ctx->impl->match_escalation_threshold ||
ctx->impl->force_match_escalation) &&
((arg.fuzzy.flags & GRN_TABLE_FUZZY_SEARCH_USE_MAX_DISTANCE_RATIO)
? (arg.fuzzy.max_distance_ratio > FLT_EPSILON)
: (arg.fuzzy.max_distance > 0))) {
arg.mode = GRN_OP_FUZZY;
if (arg.match_info) {
if (arg.match_info->flags & GRN_MATCH_INFO_GET_MIN_RECORD_ID) {
min = arg.match_info->min;
arg.match_info->min = GRN_ID_NIL;
}
}
grn_ctx_trace_log_push(ctx);
if (grn_ii_select(ctx, ii, string, string_len, s, op, &arg)) {
GRN_LOG(ctx,
GRN_LOG_ERROR,
"grn_ii_select on grn_ii_sel(fuzzy) failed!");
goto exit;
}
GRN_LOG(ctx, GRN_LOG_INFO, "fuzzy: %d", GRN_HASH_SIZE(s));
grn_ctx_trace_log_emit_uint32(ctx,
"ii.select.fuzzy.n_hits",
GRN_HASH_SIZE(s));
grn_ctx_trace_log_pop(ctx);
if (arg.match_info) {
if (arg.match_info->flags & GRN_MATCH_INFO_GET_MIN_RECORD_ID) {
if (min > GRN_ID_NIL && min < arg.match_info->min) {
arg.match_info->min = min;
}
}
}
}
if ((int64_t)GRN_HASH_SIZE(s) <= ctx->impl->match_escalation_threshold ||
ctx->impl->force_match_escalation) {
arg.mode = GRN_OP_UNSPLIT;
if (arg.match_info) {
if (arg.match_info->flags & GRN_MATCH_INFO_GET_MIN_RECORD_ID) {
min = arg.match_info->min;
arg.match_info->min = GRN_ID_NIL;
}
}
if (grn_ii_select(ctx, ii, string, string_len, s, op, &arg)) {
GRN_LOG(ctx,
GRN_LOG_ERROR,
"grn_ii_select on grn_ii_sel(unsplit) failed!");
goto exit;
}
GRN_LOG(ctx, GRN_LOG_INFO, "unsplit: %d", GRN_HASH_SIZE(s));
grn_ctx_trace_log_emit_uint32(ctx,
"ii.select.unsplit.n_hits",
GRN_HASH_SIZE(s));
grn_ctx_trace_log_pop(ctx);
if (arg.match_info) {
if (arg.match_info->flags & GRN_MATCH_INFO_GET_MIN_RECORD_ID) {
if (min > GRN_ID_NIL && min < arg.match_info->min) {
arg.match_info->min = min;
}
}
}
}
if ((int64_t)GRN_HASH_SIZE(s) <= ctx->impl->match_escalation_threshold ||
ctx->impl->force_match_escalation) {
arg.mode = GRN_OP_PARTIAL;
if (arg.match_info) {
if (arg.match_info->flags & GRN_MATCH_INFO_GET_MIN_RECORD_ID) {
min = arg.match_info->min;
arg.match_info->min = GRN_ID_NIL;
}
}
grn_ctx_trace_log_push(ctx);
if (grn_ii_select(ctx, ii, string, string_len, s, op, &arg)) {
GRN_LOG(ctx,
GRN_LOG_ERROR,
"grn_ii_select on grn_ii_sel(partial) failed!");
goto exit;
}
GRN_LOG(ctx, GRN_LOG_INFO, "partial: %d", GRN_HASH_SIZE(s));
grn_ctx_trace_log_emit_uint32(ctx,
"ii.select.partial.n_hits",
GRN_HASH_SIZE(s));
grn_ctx_trace_log_pop(ctx);
if (arg.match_info) {
if (arg.match_info->flags & GRN_MATCH_INFO_GET_MIN_RECORD_ID) {
if (min > GRN_ID_NIL && min < arg.match_info->min) {
arg.match_info->min = min;
}
}
}
}
}
GRN_LOG(ctx, GRN_LOG_INFO, "hits=%d", GRN_HASH_SIZE(s));
grn_ctx_trace_log_emit_uint32(ctx, "ii.select.n_hits", GRN_HASH_SIZE(s));
grn_ctx_trace_log_pop(ctx);
}
exit:
grn_ctx_trace_log_set_current_depth(ctx, start_depth);
return ctx->rc;
}
extern "C" grn_rc
grn_ii_select_by_id(
grn_ctx *ctx, grn_ii *ii, grn_id id, grn_hash *result_set, grn_operator op)
{
GRN_API_ENTER;
grn_rc rc = grn_ii_at(ctx, ii, id, result_set, op);
GRN_API_RETURN(rc);
}
extern "C" grn_rc
grn_ii_at(grn_ctx *ctx, grn_ii *ii, grn_id id, grn_hash *s, grn_operator op)
{
int rep = 0;
grn_ii_cursor *c;
if ((c = grn_ii_cursor_open(ctx,
ii,
id,
GRN_ID_NIL,
GRN_ID_MAX,
rep ? ii->n_elements : ii->n_elements - 1,
0))) {
grn_result_set_add_ii_cursor(ctx, s, c, 1, 1, op);
grn_ii_cursor_close(ctx, c);
}
return ctx->rc;
}
extern "C" void
grn_ii_resolve_sel_and(grn_ctx *ctx, grn_hash *s, grn_operator op)
{
if (op == GRN_OP_AND &&
!(ctx->flags & GRN_CTX_TEMPORARY_DISABLE_II_RESOLVE_SEL_AND)) {
grn_id eid;
grn_rset_recinfo *ri;
grn_hash_cursor *c =
grn_hash_cursor_open(ctx, s, NULL, 0, NULL, 0, 0, -1, 0);
if (c) {
while ((eid = grn_hash_cursor_next(ctx, c))) {
grn_hash_cursor_get_value(ctx, c, (void **)&ri);
if ((ri->n_subrecs & GRN_RSET_UTIL_BIT)) {
ri->n_subrecs &= ~GRN_RSET_UTIL_BIT;
} else {
grn_hash_delete_by_id(ctx, s, eid, NULL);
}
}
grn_hash_cursor_close(ctx, c);
}
}
}
extern "C" void
grn_ii_cursor_inspect(grn_ctx *ctx, grn_ii_cursor *c, grn_obj *buf)
{
grn_obj key_buf;
char key[GRN_TABLE_MAX_KEY_SIZE];
int key_size;
int i = 0;
grn_ii_cursor_next_options options = {/* .include_garbage = */ true};
GRN_TEXT_PUTS(ctx, buf, " #<");
key_size =
grn_table_get_key(ctx, c->ii->lexicon, c->id, key, GRN_TABLE_MAX_KEY_SIZE);
GRN_OBJ_INIT(&key_buf, GRN_BULK, 0, c->ii->lexicon->header.domain);
GRN_TEXT_SET(ctx, &key_buf, key, key_size);
grn_inspect(ctx, buf, &key_buf);
GRN_OBJ_FIN(ctx, &key_buf);
GRN_TEXT_PUTS(ctx, buf, "\n elements:[\n ");
while (grn_ii_cursor_next_internal(ctx, c, &options)) {
grn_posting *pos = c->post;
if (i > 0) {
GRN_TEXT_PUTS(ctx, buf, ",\n ");
}
i++;
GRN_TEXT_PUTS(ctx, buf, "{status:");
if (pos->tf && pos->sid) {
GRN_TEXT_PUTS(ctx, buf, "available");
} else {
GRN_TEXT_PUTS(ctx, buf, "garbage");
}
GRN_TEXT_PUTS(ctx, buf, ", rid:");
grn_text_lltoa(ctx, buf, pos->rid);
GRN_TEXT_PUTS(ctx, buf, ", sid:");
grn_text_lltoa(ctx, buf, pos->sid);
GRN_TEXT_PUTS(ctx, buf, ", pos:");
grn_text_lltoa(ctx, buf, pos->pos);
GRN_TEXT_PUTS(ctx, buf, ", tf:");
grn_text_lltoa(ctx, buf, pos->tf);
GRN_TEXT_PUTS(ctx, buf, ", weight:");
grn_text_f32toa(ctx, buf, grn_posting_get_weight_float(ctx, pos));
GRN_TEXT_PUTS(ctx, buf, ", rest:");
grn_text_lltoa(ctx, buf, pos->rest);
GRN_TEXT_PUTS(ctx, buf, "}");
}
GRN_TEXT_PUTS(ctx, buf, "\n ]\n >");
}
extern "C" void
grn_ii_inspect_values(grn_ctx *ctx, grn_ii *ii, grn_obj *buf)
{
grn_table_cursor *tc;
GRN_TEXT_PUTS(ctx, buf, "[");
if ((tc = grn_table_cursor_open(ctx,
ii->lexicon,
NULL,
0,
NULL,
0,
0,
-1,
GRN_CURSOR_ASCENDING))) {
int i = 0;
grn_id tid;
grn_ii_cursor *c;
while ((tid = grn_table_cursor_next(ctx, tc))) {
if (i > 0) {
GRN_TEXT_PUTS(ctx, buf, ",");
}
i++;
GRN_TEXT_PUTS(ctx, buf, "\n");
if ((c = grn_ii_cursor_open(ctx,
ii,
tid,
GRN_ID_NIL,
GRN_ID_MAX,
ii->n_elements,
GRN_OBJ_WITH_POSITION |
GRN_OBJ_WITH_SECTION))) {
grn_ii_cursor_inspect(ctx, c, buf);
grn_ii_cursor_close(ctx, c);
}
}
grn_table_cursor_close(ctx, tc);
}
GRN_TEXT_PUTS(ctx, buf, "]");
}
/********************** buffered index builder ***********************/
typedef struct {
unsigned int sid; /* Section ID */
uint32_t weight; /* Weight */
const char *p; /* Value address */
uint32_t len; /* Value length */
char *buf; /* Buffer address */
uint32_t cap; /* Buffer size */
} ii_buffer_value;
/* ii_buffer_counter is associated with a combination of a block an a term. */
typedef struct {
uint32_t nrecs; /* Number of records or sections */
uint32_t nposts; /* Number of occurrences */
/* Information of the last value */
grn_id last_rid; /* Record ID */
uint32_t last_sid; /* Section ID */
uint32_t last_tf; /* Term frequency */
uint32_t last_weight; /* Total weight */
uint32_t last_pos; /* Token position */
/* Meaning of offset_* is different before/after encoding. */
/* Before encoding: size in encoded sequence */
/* After encoding: Offset in encoded sequence */
uint32_t offset_rid; /* Record ID */
uint32_t offset_sid; /* Section ID */
uint32_t offset_tf; /* Term frequency */
uint32_t offset_weight; /* Weight */
uint32_t offset_pos; /* Token position */
} ii_buffer_counter;
typedef struct {
off64_t head;
off64_t tail;
uint32_t nextsize;
uint8_t *buffer;
uint32_t buffersize;
uint8_t *bufcur;
uint32_t rest;
grn_id tid;
uint32_t nrecs;
uint32_t nposts;
grn_id *recs;
uint32_t *tfs;
uint32_t *posts;
} ii_buffer_block;
struct _grn_ii_buffer {
grn_obj *lexicon; /* Global lexicon */
grn_obj *tmp_lexicon; /* Temporary lexicon for each block */
ii_buffer_block *blocks; /* Blocks */
uint32_t nblocks; /* Number of blocks */
int tmpfd; /* Descriptor of temporary file */
char tmpfpath[PATH_MAX]; /* Path of temporary file */
uint64_t update_buffer_size;
// stuff for parsing
off64_t filepos; /* Write position of temporary file */
grn_id *block_buf; /* Buffer for the current block */
size_t block_buf_size; /* Size of block_buf */
size_t block_pos; /* Write position of block_buf */
ii_buffer_counter *counters; /* Status of terms */
uint32_t ncounters; /* Number of counters */
size_t total_size;
size_t curr_size;
ii_buffer_value *values; /* Values in block */
unsigned int nvalues; /* Number of values in block */
unsigned int max_nvalues; /* Size of values */
grn_id last_rid;
// stuff for merging
grn_ii *ii;
uint32_t lseg;
uint32_t dseg;
buffer *term_buffer;
datavec data_vectors[MAX_N_ELEMENTS + 1];
uint8_t *packed_buf;
size_t packed_buf_size;
size_t packed_len;
size_t total_chunk_size;
};
/*
* The temporary file format is roughly as follows:
*
* File = Block...
* Block = Unit...
* Unit = TermChunk (key order)
* NextUnitSize (The first unit size is kept on memory)
* Chunk = Term...
* Term = ID (gtid)
* NumRecordsOrSections (nrecs), NumOccurrences (nposts)
* RecordID... (rid, diff)
* [SectionID... (sid, diff)]
* TermFrequency... (tf, diff)
* [Weight... (weight, diff)]
* [Position... (pos, diff)]
*/
/*
* ==========================================================================
* The following part provides constants, structures and functions for static
* indexing.
* ==========================================================================
*/
#define GRN_II_BUILDER_BUFFER_CHUNK_SIZE (S_CHUNK >> 2)
#define GRN_II_BUILDER_MAX_LEXICON_CACHE_SIZE (1 << 24)
#define GRN_II_BUILDER_MIN_BLOCK_THRESHOLD 1
#define GRN_II_BUILDER_MAX_BLOCK_THRESHOLD (1 << 28)
#define GRN_II_BUILDER_MIN_FILE_BUF_SIZE (1 << 12)
#define GRN_II_BUILDER_MAX_FILE_BUF_SIZE (1 << 30)
#define GRN_II_BUILDER_MIN_BLOCK_BUF_SIZE (1 << 12)
#define GRN_II_BUILDER_MAX_BLOCK_BUF_SIZE (1 << 30)
#define GRN_II_BUILDER_MIN_CHUNK_THRESHOLD 1
#define GRN_II_BUILDER_MAX_CHUNK_THRESHOLD (1 << 28)
#define GRN_II_BUILDER_MIN_BUFFER_MAX_N_TERMS 1
#define GRN_II_BUILDER_MAX_BUFFER_MAX_N_TERMS \
((S_SEGMENT - sizeof(buffer_header)) / sizeof(buffer_term))
struct grn_ii_builder_options {
uint32_t lexicon_cache_size; /* Cache size of temporary lexicon */
/* A block is flushed if builder->n reaches this value. */
uint32_t block_threshold;
uint32_t file_buf_size; /* Buffer size for buffered output */
uint32_t block_buf_size; /* Buffer size for buffered input */
/* A chunk is flushed if chunk->n reaches this value. */
uint32_t chunk_threshold;
uint32_t buffer_max_n_terms; /* Maximum number of terms in each buffer */
};
static const grn_ii_builder_options grn_ii_builder_default_options = {
0x80000, /* lexicon_cache_size */
0x4000000, /* block_threshold */
0x10000, /* file_buf_size */
0x10000, /* block_buf_size */
0x1000, /* chunk_threshold */
0x3000, /* buffer_max_n_terms */
};
/* grn_ii_builder_options_init fills options with the default options. */
void
grn_ii_builder_options_init(grn_ii_builder_options *options)
{
*options = grn_ii_builder_default_options;
}
/* grn_ii_builder_options_fix fixes out-of-range options. */
static void
grn_ii_builder_options_fix(grn_ii_builder_options *options)
{
if (options->lexicon_cache_size > GRN_II_BUILDER_MAX_LEXICON_CACHE_SIZE) {
options->lexicon_cache_size = GRN_II_BUILDER_MAX_LEXICON_CACHE_SIZE;
}
if (options->block_threshold < GRN_II_BUILDER_MIN_BLOCK_THRESHOLD) {
options->block_threshold = GRN_II_BUILDER_MIN_BLOCK_THRESHOLD;
}
if (options->block_threshold > GRN_II_BUILDER_MAX_BLOCK_THRESHOLD) {
options->block_threshold = GRN_II_BUILDER_MAX_BLOCK_THRESHOLD;
}
if (options->file_buf_size < GRN_II_BUILDER_MIN_FILE_BUF_SIZE) {
options->file_buf_size = GRN_II_BUILDER_MIN_FILE_BUF_SIZE;
}
if (options->file_buf_size > GRN_II_BUILDER_MAX_FILE_BUF_SIZE) {
options->file_buf_size = GRN_II_BUILDER_MAX_FILE_BUF_SIZE;
}
if (options->block_buf_size < GRN_II_BUILDER_MIN_BLOCK_BUF_SIZE) {
options->block_buf_size = GRN_II_BUILDER_MIN_BLOCK_BUF_SIZE;
}
if (options->block_buf_size > GRN_II_BUILDER_MAX_BLOCK_BUF_SIZE) {
options->block_buf_size = GRN_II_BUILDER_MAX_BLOCK_BUF_SIZE;
}
if (options->chunk_threshold < GRN_II_BUILDER_MIN_CHUNK_THRESHOLD) {
options->chunk_threshold = GRN_II_BUILDER_MIN_CHUNK_THRESHOLD;
}
if (options->chunk_threshold > GRN_II_BUILDER_MAX_CHUNK_THRESHOLD) {
options->chunk_threshold = GRN_II_BUILDER_MAX_CHUNK_THRESHOLD;
}
if (options->buffer_max_n_terms < GRN_II_BUILDER_MIN_BUFFER_MAX_N_TERMS) {
options->buffer_max_n_terms = GRN_II_BUILDER_MIN_BUFFER_MAX_N_TERMS;
}
if (options->buffer_max_n_terms > GRN_II_BUILDER_MAX_BUFFER_MAX_N_TERMS) {
options->buffer_max_n_terms = GRN_II_BUILDER_MAX_BUFFER_MAX_N_TERMS;
}
}
#define GRN_II_BUILDER_TERM_INPLACE_SIZE \
(sizeof(grn_ii_builder_term) - offsetof(grn_ii_builder_term, dummy))
typedef struct {
grn_id rid; /* Last record ID */
uint32_t sid; /* Last section ID */
/* Last position (GRN_OBJ_WITH_POSITION) or frequency. */
uint32_t pos_or_freq;
uint32_t offset; /* Buffer write offset */
uint32_t size; /* Buffer size */
uint32_t dummy; /* Padding */
uint8_t *buf; /* Buffer (to be freed) */
} grn_ii_builder_term;
/* grn_ii_builder_term_is_inplace returns whether a term buffer is inplace. */
static inline bool
grn_ii_builder_term_is_inplace(grn_ii_builder_term *term)
{
return term->size == GRN_II_BUILDER_TERM_INPLACE_SIZE;
}
/* grn_ii_builder_term_get_buf returns a term buffer. */
static inline uint8_t *
grn_ii_builder_term_get_buf(grn_ii_builder_term *term)
{
if (grn_ii_builder_term_is_inplace(term)) {
return (uint8_t *)&term->dummy;
} else {
return term->buf;
}
}
/*
* grn_ii_builder_term_init initializes a term. Note that an initialized term
* must be finalized by grn_ii_builder_term_fin.
*/
static void
grn_ii_builder_term_init(grn_ctx *ctx, grn_ii_builder_term *term)
{
term->rid = GRN_ID_NIL;
term->sid = 0;
term->pos_or_freq = 0;
term->offset = 0;
term->size = GRN_II_BUILDER_TERM_INPLACE_SIZE;
}
/* grn_ii_builder_term_fin finalizes a term. */
static void
grn_ii_builder_term_fin(grn_ctx *ctx, grn_ii_builder_term *term)
{
if (!grn_ii_builder_term_is_inplace(term)) {
GRN_FREE(term->buf);
}
}
/* grn_ii_builder_term_reinit reinitializes a term. */
static void
grn_ii_builder_term_reinit(grn_ctx *ctx, grn_ii_builder_term *term)
{
grn_ii_builder_term_fin(ctx, term);
grn_ii_builder_term_init(ctx, term);
}
/* grn_ii_builder_term_extend extends a term buffer. */
static grn_rc
grn_ii_builder_term_extend(grn_ctx *ctx, grn_ii_builder_term *term)
{
uint8_t *buf;
uint32_t size = term->size * 2;
if (grn_ii_builder_term_is_inplace(term)) {
buf = (uint8_t *)GRN_MALLOC(size);
if (!buf) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for term buffer: size = %u",
size);
return ctx->rc;
}
grn_memcpy(buf, &term->dummy, term->offset);
} else {
buf = (uint8_t *)GRN_REALLOC(term->buf, size);
if (!buf) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to reallocate memory for term buffer: size = %u",
size);
return ctx->rc;
}
}
term->buf = buf;
term->size = size;
return GRN_SUCCESS;
}
/* grn_ii_builder_term_append appends an integer to a term buffer. */
static inline grn_rc
grn_ii_builder_term_append(grn_ctx *ctx,
grn_ii_builder_term *term,
uint64_t value)
{
uint8_t *p;
if (value < (uint64_t)1 << 5) {
if (term->offset + 1 > term->size) {
grn_rc rc = grn_ii_builder_term_extend(ctx, term);
if (rc != GRN_SUCCESS) {
return rc;
}
}
p = grn_ii_builder_term_get_buf(term) + term->offset;
p[0] = (uint8_t)value;
term->offset++;
return GRN_SUCCESS;
} else if (value < (uint64_t)1 << 13) {
if (term->offset + 2 > term->size) {
grn_rc rc = grn_ii_builder_term_extend(ctx, term);
if (rc != GRN_SUCCESS) {
return rc;
}
}
p = grn_ii_builder_term_get_buf(term) + term->offset;
p[0] = (uint8_t)((value & 0x1f) | (1 << 5));
p[1] = (uint8_t)(value >> 5);
term->offset += 2;
return GRN_SUCCESS;
} else {
uint8_t i, n;
if (value < (uint64_t)1 << 21) {
n = 3;
} else if (value < (uint64_t)1 << 29) {
n = 4;
} else if (value < (uint64_t)1 << 37) {
n = 5;
} else if (value < (uint64_t)1 << 45) {
n = 6;
} else if (value < (uint64_t)1 << 53) {
n = 7;
} else {
n = 8;
}
if (term->offset + n > term->size) {
grn_rc rc = grn_ii_builder_term_extend(ctx, term);
if (rc != GRN_SUCCESS) {
return rc;
}
}
p = grn_ii_builder_term_get_buf(term) + term->offset;
p[0] = (uint8_t)(value & 0x1f) | ((n - 1) << 5);
value >>= 5;
for (i = 1; i < n; i++) {
p[i] = (uint8_t)value;
value >>= 8;
}
term->offset += n;
return GRN_SUCCESS;
}
}
typedef struct {
uint64_t offset; /* File offset */
uint32_t rest; /* Remaining size */
uint8_t *buf; /* Buffer (to be freed) */
uint8_t *cur; /* Current pointer */
uint8_t *end; /* End pointer */
uint32_t tid; /* Term ID */
} grn_ii_builder_block;
/*
* grn_ii_builder_block_init initializes a block. Note that an initialized
* block must be finalized by grn_ii_builder_block_fin.
*/
static void
grn_ii_builder_block_init(grn_ctx *ctx, grn_ii_builder_block *block)
{
block->offset = 0;
block->rest = 0;
block->buf = NULL;
block->cur = NULL;
block->end = NULL;
block->tid = GRN_ID_NIL;
}
/* grn_ii_builder_block_fin finalizes a block. */
static void
grn_ii_builder_block_fin(grn_ctx *ctx, grn_ii_builder_block *block)
{
if (block->buf) {
GRN_FREE(block->buf);
}
}
/*
* grn_ii_builder_block_next reads the next integer. Note that this function
* returns GRN_END_OF_DATA if it reaches the end of a block.
*/
static inline grn_rc
grn_ii_builder_block_next(grn_ctx *ctx,
grn_ii_builder_block *block,
uint64_t *value)
{
uint8_t n;
if (block->cur == block->end) {
return GRN_END_OF_DATA;
}
n = (*block->cur >> 5) + 1;
if (n > block->end - block->cur) {
return GRN_END_OF_DATA;
}
*value = 0;
switch (n) {
case 8:
*value |= (uint64_t)block->cur[7] << 53;
case 7:
*value |= (uint64_t)block->cur[6] << 45;
case 6:
*value |= (uint64_t)block->cur[5] << 37;
case 5:
*value |= (uint64_t)block->cur[4] << 29;
case 4:
*value |= (uint64_t)block->cur[3] << 21;
case 3:
*value |= (uint64_t)block->cur[2] << 13;
case 2:
*value |= (uint64_t)block->cur[1] << 5;
case 1:
*value |= block->cur[0] & 0x1f;
break;
}
block->cur += n;
return GRN_SUCCESS;
}
typedef struct {
grn_ii *ii; /* Inverted index */
uint32_t buf_id; /* Buffer ID */
uint32_t buf_seg_id; /* Buffer segment ID */
buffer *buf; /* Buffer (to be unreferenced) */
uint32_t chunk_id; /* Chunk ID */
uint32_t chunk_seg_id; /* Chunk segment ID */
uint8_t *chunk; /* Chunk (to be unreferenced) */
uint32_t chunk_offset; /* Chunk write position */
uint32_t chunk_size; /* Chunk size */
} grn_ii_builder_buffer;
/*
* grn_ii_builder_buffer_init initializes a buffer. Note that a buffer must be
* finalized by grn_ii_builder_buffer_fin.
*/
static void
grn_ii_builder_buffer_init(grn_ctx *ctx, grn_ii_builder_buffer *buf, grn_ii *ii)
{
buf->ii = ii;
buf->buf_id = 0;
buf->buf_seg_id = 0;
buf->buf = NULL;
buf->chunk_id = 0;
buf->chunk_seg_id = 0;
buf->chunk = NULL;
buf->chunk_offset = 0;
buf->chunk_size = 0;
}
/* grn_ii_builder_buffer_fin finalizes a buffer. */
static void
grn_ii_builder_buffer_fin(grn_ctx *ctx, grn_ii_builder_buffer *buf)
{
if (buf->buf) {
grn_io_seg_unref(ctx, buf->ii->seg, buf->buf_seg_id);
}
if (buf->chunk) {
grn_io_seg_unref(ctx, buf->ii->chunk, buf->chunk_seg_id);
}
}
/* grn_ii_builder_buffer_is_assigned returns whether a buffer is assigned. */
static bool
grn_ii_builder_buffer_is_assigned(grn_ctx *ctx, grn_ii_builder_buffer *buf)
{
return buf->buf != NULL;
}
/* grn_ii_builder_buffer_assign assigns a buffer. */
static grn_rc
grn_ii_builder_buffer_assign(grn_ctx *ctx,
grn_ii_builder_buffer *buf,
size_t min_chunk_size)
{
void *seg;
size_t chunk_size;
grn_rc rc;
/* Create a buffer. */
buf->buf_id = GRN_II_PSEG_NOT_ASSIGNED;
rc = buffer_segment_new(ctx, buf->ii, &buf->buf_id);
if (rc != GRN_SUCCESS) {
if (ctx->rc != GRN_SUCCESS) {
ERR(rc,
"[ii][builder][buffer][assign] "
"failed to allocate segment for buffer");
}
return rc;
}
buf->buf_seg_id = grn_ii_get_buffer_pseg_inline(buf->ii, buf->buf_id);
seg = grn_io_seg_ref(ctx, buf->ii->seg, buf->buf_seg_id);
if (!seg) {
if (ctx->rc == GRN_SUCCESS) {
ERR(GRN_UNKNOWN_ERROR,
"[ii][builder][buffer][assign] "
"failed to access buffer segment: buffer_id=<%u> segment_id=<%u>",
buf->buf_id,
buf->buf_seg_id);
}
return ctx->rc;
}
buf->buf = (buffer *)seg;
/* Create a chunk. */
chunk_size = GRN_II_BUILDER_BUFFER_CHUNK_SIZE;
while (chunk_size < min_chunk_size) {
chunk_size *= 2;
}
rc = chunk_new(ctx, buf->ii, &buf->chunk_id, chunk_size);
if (rc != GRN_SUCCESS) {
return rc;
}
const size_t seg_offset =
(buf->chunk_id & ((1 << GRN_II_N_CHUNK_VARIATION) - 1))
<< GRN_II_W_LEAST_CHUNK;
const uint32_t chunk_capacity =
buf->ii->chunk->header->segment_size - seg_offset;
if (chunk_capacity < chunk_size) {
ERR(GRN_NOT_ENOUGH_SPACE,
"[ii][builder][buffer][assign] "
"too large chunk size: "
"chunk_size=<%" GRN_FMT_SIZE "> "
"chunk_capacity=<%u> "
"chunk_id=<%u> "
"segment_id=<%u>",
chunk_size,
chunk_capacity,
buf->chunk_id,
buf->chunk_seg_id);
return ctx->rc;
}
buf->chunk_seg_id = buf->chunk_id >> GRN_II_N_CHUNK_VARIATION;
seg = grn_io_seg_ref(ctx, buf->ii->chunk, buf->chunk_seg_id);
if (!seg) {
if (ctx->rc == GRN_SUCCESS) {
ERR(GRN_UNKNOWN_ERROR,
"[ii][builder][buffer][assign] "
"failed to access chunk segment: chunk_id=<%u> segment_id=<%u>",
buf->chunk_id,
buf->chunk_seg_id);
}
return ctx->rc;
}
buf->chunk = ((uint8_t *)seg) + seg_offset;
buf->chunk_offset = 0;
buf->chunk_size = chunk_size;
buf->buf->header.chunk = buf->chunk_id;
buf->buf->header.chunk_size = chunk_size;
buf->buf->header.buffer_free = S_SEGMENT - sizeof(buffer_header);
buf->buf->header.nterms = 0;
buf->buf->header.nterms_void = 0;
buf->ii->header.common->total_chunk_size += chunk_size;
return GRN_SUCCESS;
}
/* grn_ii_builder_buffer_flush flushes a buffer. */
static grn_rc
grn_ii_builder_buffer_flush(grn_ctx *ctx, grn_ii_builder_buffer *buf)
{
grn_ii *ii;
buf->buf->header.buffer_free = S_SEGMENT - sizeof(buffer_header) -
buf->buf->header.nterms * sizeof(buffer_term);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][builder][buffer][flush] "
"n_terms=<%u> "
"chunk_offset=<%u> "
"chunk_size=<%u> "
"total=<%" GRN_FMT_INT64U "KB>",
buf->buf->header.nterms,
buf->chunk_offset,
buf->buf->header.chunk_size,
buf->ii->header.common->total_chunk_size >> 10);
ii = buf->ii;
grn_ii_builder_buffer_fin(ctx, buf);
grn_ii_builder_buffer_init(ctx, buf, ii);
return GRN_SUCCESS;
}
typedef struct {
grn_ii *ii; /* Building inverted index */
grn_id tid; /* Term ID */
uint32_t n; /* Number of integers in buffers */
grn_id rid; /* Record ID */
uint32_t rid_gap; /* Record ID gap */
uint64_t pos_sum; /* Sum of position gaps */
uint32_t offset; /* Write offset */
uint32_t size; /* Buffer size */
grn_id *rid_buf; /* Buffer for record IDs (to be freed) */
uint32_t *sid_buf; /* Buffer for section IDs (to be freed) */
uint32_t *freq_buf; /* Buffer for frequencies (to be freed) */
uint32_t *weight_buf; /* Buffer for weights (to be freed) */
uint32_t pos_offset; /* Write offset of pos_buf */
uint32_t pos_size; /* Buffer size of pos_buf */
uint32_t *pos_buf; /* Buffer for positions (to be freed) */
size_t enc_offset; /* Write offset of enc_buf */
size_t enc_size; /* Buffer size of enc_buf */
uint8_t *enc_buf; /* Buffer for encoded data (to be freed) */
} grn_ii_builder_chunk;
/*
* grn_ii_builder_chunk_init initializes a chunk. Note that an initialized
* chunk must be finalized by grn_ii_builder_chunk_fin.
*/
static void
grn_ii_builder_chunk_init(grn_ctx *ctx, grn_ii_builder_chunk *chunk, grn_ii *ii)
{
chunk->ii = ii;
chunk->tid = GRN_ID_NIL;
chunk->n = 0;
chunk->rid = GRN_ID_NIL;
chunk->rid_gap = 0;
chunk->pos_sum = 0;
chunk->offset = 0;
chunk->size = 0;
chunk->rid_buf = NULL;
chunk->sid_buf = NULL;
chunk->freq_buf = NULL;
chunk->weight_buf = NULL;
chunk->pos_offset = 0;
chunk->pos_size = 0;
chunk->pos_buf = NULL;
chunk->enc_offset = 0;
chunk->enc_size = 0;
chunk->enc_buf = NULL;
}
/* grn_ii_builder_chunk_fin finalizes a chunk. */
static void
grn_ii_builder_chunk_fin(grn_ctx *ctx, grn_ii_builder_chunk *chunk)
{
if (chunk->enc_buf) {
GRN_FREE(chunk->enc_buf);
}
if (chunk->pos_buf) {
GRN_FREE(chunk->pos_buf);
}
if (chunk->weight_buf) {
GRN_FREE(chunk->weight_buf);
}
if (chunk->freq_buf) {
GRN_FREE(chunk->freq_buf);
}
if (chunk->sid_buf) {
GRN_FREE(chunk->sid_buf);
}
if (chunk->rid_buf) {
GRN_FREE(chunk->rid_buf);
}
}
/*
* grn_ii_builder_chunk_clear clears stats except rid and buffers except
* enc_buf.
*/
static void
grn_ii_builder_chunk_clear(grn_ctx *ctx, grn_ii_builder_chunk *chunk)
{
chunk->n = 0;
chunk->rid_gap = 0;
chunk->pos_sum = 0;
chunk->offset = 0;
chunk->pos_offset = 0;
}
/*
* grn_ii_builder_chunk_extend_bufs extends buffers except pos_buf and enc_buf.
*/
static grn_rc
grn_ii_builder_chunk_extend_bufs(grn_ctx *ctx,
grn_ii_builder_chunk *chunk,
uint32_t ii_flags)
{
uint32_t *buf;
uint32_t size = chunk->size ? chunk->size * 2 : 1;
size_t n_bytes = size * sizeof(uint32_t);
buf = (uint32_t *)GRN_REALLOC(chunk->rid_buf, n_bytes);
if (!buf) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for record IDs: n_bytes = %" GRN_FMT_SIZE,
n_bytes);
return ctx->rc;
}
chunk->rid_buf = buf;
if (ii_flags & GRN_OBJ_WITH_SECTION) {
buf = (uint32_t *)GRN_REALLOC(chunk->sid_buf, n_bytes);
if (!buf) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for section IDs:"
" n_bytes = %" GRN_FMT_SIZE,
n_bytes);
return ctx->rc;
}
chunk->sid_buf = buf;
}
buf = (uint32_t *)GRN_REALLOC(chunk->freq_buf, n_bytes);
if (!buf) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for frequencies: n_bytes = %" GRN_FMT_SIZE,
n_bytes);
return ctx->rc;
}
chunk->freq_buf = buf;
if (ii_flags & GRN_OBJ_WITH_WEIGHT) {
buf = (uint32_t *)GRN_REALLOC(chunk->weight_buf, n_bytes);
if (!buf) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for weights: n_bytes = %" GRN_FMT_SIZE,
n_bytes);
return ctx->rc;
}
chunk->weight_buf = buf;
}
chunk->size = size;
return GRN_SUCCESS;
}
/* grn_ii_builder_chunk_extend_pos_buf extends pos_buf. */
static grn_rc
grn_ii_builder_chunk_extend_pos_buf(grn_ctx *ctx, grn_ii_builder_chunk *chunk)
{
uint32_t *buf;
uint32_t size = chunk->pos_size ? chunk->pos_size * 2 : 1;
size_t n_bytes = size * sizeof(uint32_t);
buf = (uint32_t *)GRN_REALLOC(chunk->pos_buf, n_bytes);
if (!buf) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for positions: n_bytes = %" GRN_FMT_SIZE,
n_bytes);
return ctx->rc;
}
chunk->pos_buf = buf;
chunk->pos_size = size;
return GRN_SUCCESS;
}
/*
* grn_ii_builder_chunk_reserve_enc_buf estimates a size that is enough to
* store encoded data and allocates memory to enc_buf.
*/
static grn_rc
grn_ii_builder_chunk_reserve_enc_buf(grn_ctx *ctx,
grn_ii_builder_chunk *chunk,
uint32_t n_cinfos)
{
size_t rich_size =
(chunk->n + 4) * sizeof(uint32_t) + n_cinfos * sizeof(chunk_info);
if (chunk->enc_size < rich_size) {
size_t size = chunk->enc_size ? chunk->enc_size * 2 : 1;
while (size < rich_size) {
size *= 2;
}
auto buf = static_cast<uint8_t *>(GRN_REALLOC(chunk->enc_buf, size));
if (!buf) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for encoding: size = %" GRN_FMT_SIZE,
size);
return ctx->rc;
}
chunk->enc_buf = buf;
chunk->enc_size = size;
}
chunk->enc_offset = 0;
return GRN_SUCCESS;
}
/* grn_ii_builder_chunk_encode encodes a chunk buffer. */
static void
grn_ii_builder_chunk_encode_buf(grn_ctx *ctx,
grn_ii_builder_chunk *chunk,
grn_codec_data *data)
{
data->offset = chunk->enc_size - (data->output - chunk->enc_buf);
if (data->flags & DATA_USE_P_FOR_ENC) {
data->output = grn_enc_p_for(ctx, data);
} else {
data->output = grn_enc_byte(ctx, data);
}
chunk->enc_offset = data->output - chunk->enc_buf;
}
/* grn_ii_builder_chunk_encode encodes a chunk. */
static grn_rc
grn_ii_builder_chunk_encode(grn_ctx *ctx,
grn_ii_builder_chunk *chunk,
chunk_info *cinfos,
uint32_t n_cinfos)
{
grn_rc rc;
uint8_t *p;
uint8_t shift = 0, use_p_for_enc_flags = 0;
/* Choose an encoding. */
uint32_t rid_flags = 0;
if (data_records_use_p_for_enc(chunk->offset, chunk->rid)) {
rid_flags |= DATA_USE_P_FOR_ENC;
}
uint32_t rest_flags = 0;
if (data_sections_use_p_for_enc(chunk->offset)) {
rest_flags |= DATA_USE_P_FOR_ENC;
}
uint32_t pos_flags = DATA_ODD;
/* record IDs */
if (rid_flags & DATA_USE_P_FOR_ENC) {
use_p_for_enc_flags |= 1 << shift;
}
shift++;
/* section IDs */
if (chunk->sid_buf) {
if (rest_flags & DATA_USE_P_FOR_ENC) {
use_p_for_enc_flags |= 1 << shift;
}
shift++;
}
/* term frequencies */
if (rest_flags & DATA_USE_P_FOR_ENC) {
use_p_for_enc_flags |= 1 << shift;
}
shift++;
/* weights */
if (chunk->weight_buf) {
if (rest_flags & DATA_USE_P_FOR_ENC) {
use_p_for_enc_flags |= 1 << shift;
}
shift++;
}
/* positions */
if (chunk->pos_buf) {
if (data_positions_use_p_for_enc(chunk->pos_offset, chunk->pos_sum)) {
pos_flags |= DATA_USE_P_FOR_ENC;
use_p_for_enc_flags |= 1 << shift;
}
shift++;
}
rc = grn_ii_builder_chunk_reserve_enc_buf(ctx, chunk, n_cinfos);
if (rc != GRN_SUCCESS) {
return rc;
}
/* Encode a header. */
p = chunk->enc_buf;
if (n_cinfos > 0) {
uint32_t i;
GRN_B_ENC(n_cinfos, p);
for (i = 0; i < n_cinfos; i++) {
GRN_B_ENC(cinfos[i].segno, p);
GRN_B_ENC(cinfos[i].size, p);
GRN_B_ENC(cinfos[i].dgap, p);
}
}
if (use_p_for_enc_flags) {
GRN_B_ENC(use_p_for_enc_flags << 1, p);
GRN_B_ENC(chunk->offset, p);
if (chunk->pos_buf) {
GRN_B_ENC(chunk->pos_offset - chunk->offset, p);
}
} else {
GRN_B_ENC((chunk->offset << 1) | 1, p);
}
chunk->enc_offset = p - chunk->enc_buf;
/* Encode a body. */
grn_codec_data data = {};
data.ii = chunk->ii;
data.term_id = chunk->tid;
data.values = chunk->rid_buf;
data.n_values = chunk->offset;
data.flags = rid_flags;
data.output = chunk->enc_buf + chunk->enc_offset;
grn_ii_builder_chunk_encode_buf(ctx, chunk, &data);
data.flags = rest_flags;
if (chunk->sid_buf) {
data.values = chunk->sid_buf;
grn_ii_builder_chunk_encode_buf(ctx, chunk, &data);
}
data.values = chunk->freq_buf;
grn_ii_builder_chunk_encode_buf(ctx, chunk, &data);
if (chunk->weight_buf) {
data.values = chunk->weight_buf;
grn_ii_builder_chunk_encode_buf(ctx, chunk, &data);
}
if (chunk->pos_buf) {
data.values = chunk->pos_buf;
data.n_values = chunk->pos_offset;
data.flags = pos_flags;
grn_ii_builder_chunk_encode_buf(ctx, chunk, &data);
}
return GRN_SUCCESS;
}
namespace grn::ii {
template <typename Each>
grn_rc
table_each_by_key(grn_ctx *ctx, grn_obj *table, Each &&each)
{
switch (table->header.type) {
case GRN_TABLE_HASH_KEY:
{
auto hash = reinterpret_cast<grn_hash *>(table);
if (grn_hash_size(ctx, hash) == 0) {
return GRN_SUCCESS;
}
// grn_hash can't use GRN_CURSOR_BY_KEY. So we sort by key
// manually. This will not be an ignorable overhead but it's
// an easy implementation for now. We may use an offline index
// construction algorithm that doesn't rely on
// GRN_CURSOR_BY_KEY in a future but we haven't done it.
auto sorted = grn_array_create(ctx, nullptr, sizeof(grn_id), 0);
if (!sorted) {
return ctx->rc;
}
grn_table_sort_optarg arg = {
GRN_TABLE_SORT_ASC | GRN_TABLE_SORT_BY_KEY,
nullptr,
nullptr,
nullptr,
0,
};
grn_hash_sort(ctx, hash, -1, sorted, &arg);
grn_rc rc = GRN_SUCCESS;
GRN_ARRAY_EACH_BEGIN(ctx, sorted, cursor, GRN_ID_NIL, GRN_ID_MAX, id)
{
grn_id hash_id = GRN_ID_NIL;
auto value_size =
grn_array_get_value(ctx,
sorted,
id,
reinterpret_cast<void *>(&hash_id));
if (value_size == 0) {
continue;
}
rc = each(hash_id);
if (rc != GRN_SUCCESS) {
break;
}
}
GRN_ARRAY_EACH_END(ctx, cursor);
grn_array_close(ctx, sorted);
return rc;
}
case GRN_TABLE_PAT_KEY:
{
auto pat = reinterpret_cast<grn_pat *>(table);
grn_rc rc = GRN_SUCCESS;
GRN_PAT_EACH_BEGIN(ctx, pat, cursor, id)
{
rc = each(id);
if (rc != GRN_SUCCESS) {
break;
}
}
GRN_PAT_EACH_END(ctx, cursor);
return rc;
}
case GRN_TABLE_DAT_KEY:
{
auto dat = reinterpret_cast<grn_dat *>(table);
grn_rc rc = GRN_SUCCESS;
GRN_DAT_EACH_BEGIN(ctx, dat, cursor, id)
{
rc = each(id);
if (rc != GRN_SUCCESS) {
break;
}
}
GRN_DAT_EACH_END(ctx, cursor);
return rc;
}
default:
// must not happen
return GRN_INVALID_ARGUMENT;
}
}
// Builds a block from multiple records. You can build a block by
// calling flush() after you append multiple records by
// append_record().
class BlockBuilder {
public:
BlockBuilder(grn_ctx *parent_ctx,
grn_ctx *ctx,
grn_ii *ii,
const grn_ii_builder_options *options,
grn_obj *src_table,
grn_obj *srcs,
grn_obj **src_token_columns,
uint8_t sid_bits)
: parent_ctx_(parent_ctx),
ctx_(ctx),
ii_(ii),
options_(options),
lexicon_(nullptr),
have_tokenizer_(false),
have_normalizers_(false),
get_key_optimizable_(false),
n_(0),
rid_(GRN_ID_NIL),
sid_(0),
pos_(0),
terms_(nullptr),
n_terms_(0),
max_n_terms_(0),
terms_size_(0),
src_table_(src_table),
srcs_(srcs),
src_token_columns_(src_token_columns),
sid_bits_(sid_bits),
values_(GRN_PTR_VECTOR_SIZE(srcs_)),
n_processed_records_(0)
{
const size_t n = GRN_PTR_VECTOR_SIZE(srcs_);
for (size_t i = 0; i < n; i++) {
GRN_VOID_INIT(&values_[i]);
}
}
~BlockBuilder()
{
const size_t n = GRN_PTR_VECTOR_SIZE(srcs_);
for (size_t i = 0; i < n; i++) {
GRN_OBJ_FIN(ctx_, &values_[i]);
}
if (lexicon_) {
grn_obj_close(ctx_, lexicon_);
}
finalize_terms();
if (parent_ctx_) {
grn_ctx_release_child(parent_ctx_, ctx_);
}
}
// Prepares this builder.
//
// This must be called once before you call append_record().
grn_rc
prepare()
{
auto rc = create_lexicon();
if (rc != GRN_SUCCESS) {
return rc;
}
return GRN_SUCCESS;
}
// Appends a record.
//
// `rid` must be ordered by ascending. The following is valid:
//
// block_builder.append_record(1);
// block_builder.append_record(2);
// block_builder.append_record(3);
//
// But the following is invalid:
//
// block_builder.append_record(1);
// block_builder.append_record(3);
// block_builder.append_record(2); // 2 is smaller than 3
grn_rc
append_record(grn_id rid)
{
const size_t n = GRN_PTR_VECTOR_SIZE(srcs_);
for (size_t i = 0; i < n; i++) {
grn_obj *value = &values_[i];
grn_obj *src = GRN_PTR_VALUE_AT(srcs_, i);
grn_obj *token_column = src_token_columns_[i];
if (token_column) {
auto rc = grn_obj_reinit_for(ctx_, value, token_column);
if (rc != GRN_SUCCESS) {
return rc;
}
if (!grn_obj_get_value(ctx_, token_column, rid, value)) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_UNKNOWN_ERROR, "failed to get tokens: rid = %u", rid);
}
return ctx_->rc;
}
auto sid = static_cast<uint32_t>(i + 1);
rc = append_tokens(rid, sid, value);
if (rc != GRN_SUCCESS) {
return rc;
}
} else {
auto rc = grn_obj_reinit_for(ctx_, value, src);
if (rc != GRN_SUCCESS) {
return rc;
}
if (GRN_OBJ_TABLEP(src)) {
auto len = grn_table_get_key2(ctx_, src, rid, value);
if (len <= 0) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_UNKNOWN_ERROR,
"failed to get key: rid = %u, len = %d",
rid,
len);
}
return ctx_->rc;
}
} else {
if (!grn_obj_get_value(ctx_, src, rid, value)) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_UNKNOWN_ERROR, "failed to get value: rid = %u", rid);
}
return ctx_->rc;
}
}
auto sid = static_cast<uint32_t>(i + 1);
rc = append_obj(src, rid, sid, value);
if (rc != GRN_SUCCESS) {
return rc;
}
}
}
return finish_record();
}
// Whether we need to flush this block.
bool
need_flush()
{
return n_ >= options_->block_threshold;
}
// Whether there are any data in this block.
bool
have_data()
{
return n_ > 0;
}
// Number of processed records in this block.
size_t
n_processed_records()
{
return n_processed_records_;
}
// Flushes all terms in this block.
template <typename FlushTerm>
grn_rc
flush(FlushTerm &&flush_term)
{
if (n_ == 0) {
/* Do nothing if there are no output data. */
return GRN_SUCCESS;
}
// Flush terms.
//
// This assumes that both of each block builder (here) and
// reader (see Builder::commit()) use the same order to write
// built blocks and read the written blocks. This is implemented
// by sorting by key (term) in both of local lexicons and the
// global lexicon (ii_->lexicon). Sorting by key is efficient
// with grn_pat and grn_dat but not with grn_hash. So we may
// change this algorithm later.
auto rc = table_each_by_key(ctx_, lexicon_, [&](grn_id tid) {
return flush_term(&(terms_[tid - 1]), lexicon_, tid);
});
/* Clear the current data. */
rc = grn_table_truncate(ctx_, lexicon_);
if (rc != GRN_SUCCESS) {
return rc;
}
rid_ = GRN_ID_NIL;
n_terms_ = 0;
n_ = 0;
n_processed_records_ = 0;
return GRN_SUCCESS;
}
grn_rc
append_tokens(grn_id rid, uint32_t sid, grn_obj *tokens)
{
start_value(rid, sid);
grn_obj *src_lexicon = ii_->lexicon;
size_t n_tokens = grn_uvector_size(ctx_, tokens);
size_t i;
for (i = 0; i < n_tokens; i++) {
float weight;
grn_id tid;
grn_id src_tid =
grn_uvector_get_element_record(ctx_, tokens, i, &weight);
uint32_t token_value_size;
const char *token_value =
_grn_table_key(ctx_, src_lexicon, src_tid, &token_value_size);
switch (lexicon_->header.type) {
case GRN_TABLE_PAT_KEY:
tid = grn_pat_add(ctx_,
reinterpret_cast<grn_pat *>(lexicon_),
token_value,
token_value_size,
NULL,
NULL);
break;
case GRN_TABLE_DAT_KEY:
tid = grn_dat_add(ctx_,
reinterpret_cast<grn_dat *>(lexicon_),
token_value,
token_value_size,
NULL,
NULL);
break;
case GRN_TABLE_HASH_KEY:
tid = grn_hash_add(ctx_,
reinterpret_cast<grn_hash *>(lexicon_),
token_value,
token_value_size,
NULL,
NULL);
break;
default:
/* This case must not be happen. */
tid = GRN_ID_NIL;
break;
}
if (tid == GRN_ID_NIL) {
/* TODO: Token value may not be a string. */
auto ctx = ctx_;
ERR(GRN_INVALID_ARGUMENT,
"[ii][builder][append-tokens] failed to add a token: <%.*s>",
static_cast<int>(token_value_size),
token_value);
return ctx->rc;
}
uint32_t pos = pos_ + i;
grn_rc rc =
append_token(rid, sid, static_cast<uint32_t>(weight), tid, pos);
if (rc != GRN_SUCCESS) {
return rc;
}
}
return GRN_SUCCESS;
}
grn_rc
finish_record()
{
++n_processed_records_;
return GRN_SUCCESS;
}
private:
grn_ctx *parent_ctx_;
grn_ctx *ctx_;
grn_ii *ii_;
const grn_ii_builder_options *options_;
grn_obj *lexicon_; /* Block lexicon (to be closed) */
bool have_tokenizer_; /* Whether lexicon has tokenizer */
bool have_normalizers_; /* Whether lexicon has at least one normalizers */
bool get_key_optimizable_; /* Whether grn_table_get_key() is optimizable */
uint32_t n_; /* Number of integers appended to the current block */
grn_id rid_; /* Record ID */
uint32_t sid_; /* Section ID */
uint32_t pos_; /* Position */
grn_ii_builder_term *terms_; /* Terms (to be freed) */
uint32_t n_terms_; /* Number of distinct terms */
uint32_t max_n_terms_; /* Maximum number of distinct terms */
uint32_t terms_size_; /* Buffer size of terms */
grn_obj *src_table_; /* Source table */
// Source columns. This refers source columns in Builder. So this
// should not be freed.
grn_obj *srcs_;
// Source token columns. This refers source token columns in
// Builder. So this should not be freed.
grn_obj **src_token_columns_;
uint8_t sid_bits_; /* Number of bits for section ID */
// Buffers for source column values. We use one grn_obj per column
// to reuse effectively.
std::vector<grn_obj> values_;
size_t n_processed_records_; /* Number of processed records */
// Creates a block lexicon.
grn_rc
create_lexicon()
{
grn_table_flags flags;
grn_obj *domain = grn_ctx_at(ctx_, ii_->lexicon->header.domain);
grn_obj *range = grn_ctx_at(ctx_, DB_OBJ(ii_->lexicon)->range);
auto rc = grn_table_get_info(ctx_,
ii_->lexicon,
&flags,
nullptr,
nullptr,
nullptr,
nullptr);
if (rc != GRN_SUCCESS) {
return rc;
}
flags &= ~GRN_OBJ_PERSISTENT;
lexicon_ =
grn_table_create(ctx_, nullptr, 0, nullptr, flags, domain, range);
if (!lexicon_) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_UNKNOWN_ERROR, "[index] failed to create a block lexicon");
}
return ctx_->rc;
}
{
grn_obj tokenizer;
GRN_TEXT_INIT(&tokenizer, 0);
grn_table_get_default_tokenizer_string(ctx_, ii_->lexicon, &tokenizer);
if (GRN_TEXT_LEN(&tokenizer) > 0) {
have_tokenizer_ = true;
rc = grn_obj_set_info(ctx_,
lexicon_,
GRN_INFO_DEFAULT_TOKENIZER,
&tokenizer);
}
GRN_OBJ_FIN(ctx_, &tokenizer);
}
if (rc == GRN_SUCCESS) {
grn_obj normalizers;
GRN_TEXT_INIT(&normalizers, 0);
grn_table_get_normalizers_string(ctx_, ii_->lexicon, &normalizers);
if (GRN_TEXT_LEN(&normalizers) > 0) {
have_normalizers_ = true;
rc = grn_obj_set_info(ctx_,
lexicon_,
GRN_INFO_NORMALIZERS,
&normalizers);
}
GRN_OBJ_FIN(ctx_, &normalizers);
}
if (rc == GRN_SUCCESS) {
grn_obj token_filters;
GRN_TEXT_INIT(&token_filters, 0);
grn_table_get_token_filters_string(ctx_, ii_->lexicon, &token_filters);
if (GRN_TEXT_LEN(&token_filters) > 0) {
rc = grn_obj_set_info(ctx_,
lexicon_,
GRN_INFO_TOKEN_FILTERS,
&token_filters);
}
GRN_OBJ_FIN(ctx_, &token_filters);
}
if (rc != GRN_SUCCESS) {
return rc;
}
if ((flags & GRN_OBJ_TABLE_TYPE_MASK) == GRN_OBJ_TABLE_PAT_KEY) {
auto lexicon_pat = reinterpret_cast<grn_pat *>(lexicon_);
get_key_optimizable_ = !grn_pat_is_key_encoded(ctx_, lexicon_pat);
if (options_->lexicon_cache_size) {
rc = grn_pat_cache_enable(ctx_,
lexicon_pat,
options_->lexicon_cache_size);
if (rc != GRN_SUCCESS) {
return rc;
}
}
} else {
get_key_optimizable_ = true;
}
return GRN_SUCCESS;
}
void
finalize_terms()
{
if (!terms_) {
return;
}
uint32_t i;
for (i = 0; i < max_n_terms_; i++) {
grn_ii_builder_term_fin(ctx_, &(terms_[i]));
}
auto ctx = ctx_;
GRN_FREE(terms_);
/* To avoid double finalization. */
terms_ = nullptr;
}
// Extends a buffer for terms in order to make terms[n_terms - 1]
// available.
grn_rc
extend_terms(uint32_t new_n_terms)
{
if (new_n_terms <= n_terms_) {
return GRN_SUCCESS;
}
if (new_n_terms > max_n_terms_) {
uint32_t i;
if (new_n_terms > terms_size_) {
/* Resize builder->terms_ for new terms. */
uint32_t new_terms_size = terms_size_ ? terms_size_ * 2 : 1;
while (new_terms_size < new_n_terms) {
new_terms_size *= 2;
}
auto ctx = ctx_;
auto new_n_bytes = new_terms_size * sizeof(grn_ii_builder_term);
auto new_terms = static_cast<grn_ii_builder_term *>(
GRN_REALLOC(terms_, new_n_bytes));
if (!new_terms) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for terms: n_bytes = %" GRN_FMT_SIZE,
new_n_bytes);
return ctx->rc;
}
terms_ = new_terms;
terms_size_ = new_terms_size;
}
/* Initialize new terms. */
for (i = max_n_terms_; i < new_n_terms; i++) {
grn_ii_builder_term_init(ctx_, &(terms_[i]));
}
max_n_terms_ = new_n_terms;
}
n_ += new_n_terms - n_terms_;
n_terms_ = new_n_terms;
return GRN_SUCCESS;
}
// Gets a term associated with tid.
grn_rc
get_term(grn_id tid, grn_ii_builder_term **term)
{
uint32_t n_terms = tid;
if (n_terms > n_terms_) {
grn_rc rc = extend_terms(n_terms);
if (rc != GRN_SUCCESS) {
return rc;
}
}
*term = &(terms_[tid - 1]);
return GRN_SUCCESS;
}
// Appends a token.
grn_rc
append_token(
grn_id rid, uint32_t sid, uint32_t weight, grn_id tid, uint32_t pos)
{
uint32_t ii_flags = ii_->header.common->flags;
grn_ii_builder_term *term;
{
auto rc = get_term(tid, &term);
if (rc != GRN_SUCCESS) {
return rc;
}
}
if (rid != term->rid || sid != term->sid) {
uint64_t rsid;
if (term->rid != GRN_ID_NIL) {
if (ii_flags & GRN_OBJ_WITH_POSITION) {
/* Append the end of positions. */
auto rc = grn_ii_builder_term_append(ctx_, term, 0);
if (rc != GRN_SUCCESS) {
return rc;
}
n_++;
} else {
/* Append a frequency if positions are not available. */
auto rc = grn_ii_builder_term_append(ctx_, term, term->pos_or_freq);
if (rc != GRN_SUCCESS) {
return rc;
}
n_++;
}
}
rsid =
(static_cast<uint64_t>(rid - term->rid) << sid_bits_) | (sid - 1);
auto rc = grn_ii_builder_term_append(ctx_, term, rsid);
if (rc != GRN_SUCCESS) {
return rc;
}
n_++;
if (ii_flags & GRN_OBJ_WITH_WEIGHT) {
auto rc = grn_ii_builder_term_append(ctx_, term, weight);
if (rc != GRN_SUCCESS) {
return rc;
}
n_++;
}
term->rid = rid;
term->sid = sid;
term->pos_or_freq = 0;
}
if (ii_flags & GRN_OBJ_WITH_POSITION) {
auto rc =
grn_ii_builder_term_append(ctx_, term, pos - term->pos_or_freq);
if (rc != GRN_SUCCESS) {
return rc;
}
n_++;
term->pos_or_freq = pos;
} else {
term->pos_or_freq++;
}
return GRN_SUCCESS;
}
void
start_value(grn_id rid, uint32_t sid)
{
if (rid != rid_) {
rid_ = rid;
sid_ = sid;
pos_ = 1;
} else if (sid != sid_) {
sid_ = sid;
pos_ = 1;
} else if (have_tokenizer_) {
/* Insert a space between values. */
pos_++;
}
}
// Appends a value. Note that values must be appended in ascending
// rid and sid order.
grn_rc
append_value(grn_obj *src,
grn_id rid,
uint32_t sid,
uint32_t weight,
const char *value,
uint32_t value_size,
grn_id value_domain,
bool force_as_is)
{
uint32_t pos = 0;
grn_token_cursor *cursor;
start_value(rid, sid);
if (value_size == 0) {
pos = pos_;
} else {
if (force_as_is || (!have_tokenizer_ && !have_normalizers_)) {
grn_id tid;
uint32_t max_key_size = 0;
bool too_large_key = false;
switch (lexicon_->header.type) {
case GRN_TABLE_PAT_KEY:
if (value_size >= GRN_TABLE_MAX_KEY_SIZE) {
tid = GRN_ID_NIL;
max_key_size = GRN_TABLE_MAX_KEY_SIZE;
too_large_key = true;
} else {
tid = grn_pat_add(ctx_,
reinterpret_cast<grn_pat *>(lexicon_),
value,
value_size,
NULL,
NULL);
}
break;
case GRN_TABLE_DAT_KEY:
if (value_size >= GRN_TABLE_MAX_KEY_SIZE) {
tid = GRN_ID_NIL;
max_key_size = GRN_TABLE_MAX_KEY_SIZE;
too_large_key = true;
} else {
tid = grn_dat_add(ctx_,
reinterpret_cast<grn_dat *>(lexicon_),
value,
value_size,
NULL,
NULL);
}
break;
case GRN_TABLE_HASH_KEY:
{
auto lexicon_hash = reinterpret_cast<grn_hash *>(lexicon_);
if (value_size > lexicon_hash->key_size) {
tid = GRN_ID_NIL;
max_key_size = lexicon_hash->key_size;
too_large_key = true;
} else {
tid = grn_hash_add(ctx_,
lexicon_hash,
value,
value_size,
NULL,
NULL);
}
}
break;
case GRN_TABLE_NO_KEY:
tid = *reinterpret_cast<const grn_id *>(value);
break;
default:
tid = GRN_ID_NIL;
break;
}
if (too_large_key) {
auto ctx = ctx_;
GRN_DEFINE_NAME(src);
grn_obj record;
GRN_RECORD_INIT(&record, 0, DB_OBJ(src_table_)->id);
GRN_RECORD_SET(ctx, &record, rid);
grn_obj inspected_record;
GRN_TEXT_INIT(&inspected_record, 0);
grn_inspect(ctx, &inspected_record, &record);
GRN_LOG(ctx,
GRN_LOG_WARNING,
"[ii][builder][append-value][%.*s] ignore too long token. "
"Token must be less than or equal to %u: <%u>: %.*s",
name_size,
name,
max_key_size,
value_size,
(int)GRN_TEXT_LEN(&inspected_record),
GRN_TEXT_VALUE(&inspected_record));
GRN_OBJ_FIN(ctx, &inspected_record);
GRN_OBJ_FIN(ctx, &record);
}
if (tid != GRN_ID_NIL) {
pos = pos_;
auto rc = append_token(rid, sid, weight, tid, pos);
if (rc != GRN_SUCCESS) {
return rc;
}
}
} else {
cursor = grn_token_cursor_open(ctx_,
lexicon_,
value,
value_size,
GRN_TOKEN_ADD,
0);
if (!cursor) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_UNKNOWN_ERROR,
"grn_token_cursor_open failed: value = <%.*s>",
value_size,
value);
}
return ctx_->rc;
}
grn_token_cursor_set_query_domain(ctx_, cursor, value_domain);
while (cursor->status == GRN_TOKEN_CURSOR_DOING) {
grn_id tid = grn_token_cursor_next(ctx_, cursor);
if (tid != GRN_ID_NIL) {
pos = pos_ + cursor->pos;
auto rc = append_token(rid, sid, weight, tid, pos);
if (rc != GRN_SUCCESS) {
break;
}
}
}
grn_token_cursor_close(ctx_, cursor);
}
}
pos_ = pos + 1;
return ctx_->rc;
}
// Appends a BULK, UVECTOR or VECTOR object.
grn_rc
append_obj(grn_obj *src, grn_id rid, uint32_t sid, grn_obj *obj)
{
switch (obj->header.type) {
case GRN_BULK:
if (grn_id_maybe_table(ctx_, obj->header.domain)) {
grn_id id = GRN_RECORD_VALUE(obj);
grn_obj key_buffer;
GRN_VOID_INIT(&key_buffer);
uint32_t key_size;
const char *key;
if (get_key_optimizable_) {
key = _grn_table_key(ctx_, ii_->lexicon, id, &key_size);
} else {
GRN_BULK_REWIND(&key_buffer);
grn_table_get_key2(ctx_, ii_->lexicon, id, &key_buffer);
key = GRN_BULK_HEAD(&key_buffer);
key_size = GRN_BULK_VSIZE(&key_buffer);
}
grn_rc rc = append_value(src,
rid,
sid,
0,
key,
key_size,
ii_->lexicon->header.domain,
true);
GRN_OBJ_FIN(ctx_, &key_buffer);
return rc;
} else {
return append_value(src,
rid,
sid,
0,
GRN_TEXT_VALUE(obj),
GRN_TEXT_LEN(obj),
obj->header.domain,
false);
}
case GRN_UVECTOR:
{
grn_rc rc = GRN_SUCCESS;
uint32_t i, n_values = grn_uvector_size(ctx_, obj);
if (grn_id_maybe_table(ctx_, obj->header.domain)) {
grn_obj key_buffer;
GRN_VOID_INIT(&key_buffer);
for (i = 0; i < n_values; i++) {
grn_id id;
float weight;
id = grn_uvector_get_element_record(ctx_, obj, i, &weight);
const char *key = nullptr;
uint32_t key_size = 0;
if (id == GRN_ID_NIL) {
key = nullptr;
key_size = 0;
} else if (get_key_optimizable_) {
key = _grn_table_key(ctx_, ii_->lexicon, id, &key_size);
} else {
GRN_BULK_REWIND(&key_buffer);
grn_table_get_key2(ctx_, ii_->lexicon, id, &key_buffer);
key = GRN_BULK_HEAD(&key_buffer);
key_size = GRN_BULK_VSIZE(&key_buffer);
}
rc = append_value(src,
rid,
sid,
(uint32_t)weight,
key,
key_size,
ii_->lexicon->header.domain,
true);
if (rc != GRN_SUCCESS) {
break;
}
}
GRN_OBJ_FIN(ctx_, &key_buffer);
} else {
const char *p = GRN_BULK_HEAD(obj);
uint32_t value_size = grn_uvector_element_size(ctx_, obj);
for (i = 0; i < n_values; i++) {
rc = append_value(src,
rid,
sid,
0,
p,
value_size,
obj->header.domain,
false);
if (rc != GRN_SUCCESS) {
break;
}
p += value_size;
}
}
return rc;
}
case GRN_VECTOR:
if (obj->u.v.body) {
/*
* Note that the following sections and n_sections don't correspond to
* source columns.
*/
int i, n_secs = obj->u.v.n_sections;
grn_section *secs = obj->u.v.sections;
const char *head = GRN_BULK_HEAD(obj->u.v.body);
for (i = 0; i < n_secs; i++) {
grn_rc rc;
grn_section *sec = &secs[i];
if (sec->length == 0) {
continue;
}
if ((ii_->header.common->flags & GRN_OBJ_WITH_SECTION) &&
have_tokenizer_) {
sid = i + 1;
}
rc = append_value(src,
rid,
sid,
(uint32_t)(sec->weight),
head + sec->offset,
sec->length,
sec->domain,
false);
if (rc != GRN_SUCCESS) {
return rc;
}
}
}
return GRN_SUCCESS;
default:
{
auto ctx = ctx_;
ERR(GRN_INVALID_ARGUMENT, "[index] invalid object assigned as value");
return ctx->rc;
}
}
}
};
struct Builder {
Builder(grn_ctx *ctx, grn_ii *ii, const grn_ii_builder_options *options)
: ctx_(ctx),
ii_(ii)
{
progress_needed_ = (grn_ctx_get_progress_callback(ctx_) != nullptr);
if (progress_needed_) {
progress_.type = GRN_PROGRESS_INDEX;
progress_.value.index.phase = GRN_PROGRESS_INDEX_INITIALIZE;
progress_.value.index.n_target_records = 0;
progress_.value.index.n_processed_records = 0;
progress_.value.index.n_target_terms = 0;
progress_.value.index.n_processed_terms = 0;
grn_ctx_call_progress_callback(ctx, &progress_);
}
if (options) {
options_ = *options;
} else {
options_ = grn_ii_builder_default_options;
}
if (grn_ii_builder_block_threshold_force > 0) {
options_.block_threshold = grn_ii_builder_block_threshold_force;
}
grn_ii_builder_options_fix(&options_);
src_table_ = nullptr;
n_records_ = 0;
GRN_PTR_INIT(&srcs_, GRN_OBJ_VECTOR, GRN_ID_NIL);
src_token_columns_ = nullptr;
sid_bits_ = 0;
sid_mask_ = 0;
path_[0] = '\0';
fd_ = -1;
file_buf_ = nullptr;
file_buf_offset_ = 0;
blocks_ = nullptr;
n_blocks_ = 0;
blocks_size_ = 0;
grn_ii_builder_buffer_init(ctx, &buf_, ii);
grn_ii_builder_chunk_init(ctx, &chunk_, ii);
df_ = 0;
cinfos_ = nullptr;
n_cinfos_ = 0;
cinfos_size_ = 0;
}
~Builder()
{
if (progress_needed_) {
progress_.value.index.phase = GRN_PROGRESS_INDEX_FINALIZE;
grn_ctx_call_progress_callback(ctx_, &progress_);
}
if (cinfos_) {
auto ctx = ctx_;
GRN_FREE(cinfos_);
}
grn_ii_builder_chunk_fin(ctx_, &chunk_);
grn_ii_builder_buffer_fin(ctx_, &buf_);
if (blocks_) {
uint32_t i;
for (i = 0; i < n_blocks_; i++) {
grn_ii_builder_block_fin(ctx_, &(blocks_[i]));
}
auto ctx = ctx_;
GRN_FREE(blocks_);
}
if (file_buf_) {
auto ctx = ctx_;
GRN_FREE(file_buf_);
}
if (fd_ != -1) {
grn_close(fd_);
if (grn_unlink(path_) == 0) {
GRN_LOG(ctx_,
GRN_LOG_INFO,
"[ii][builder][fin] removed path: <%s>",
path_);
} else {
auto ctx = ctx_;
ERRNO_ERR("[ii][builder][fin] failed to remove path: <%s>", path_);
}
}
const uint32_t n_srcs = GRN_PTR_VECTOR_SIZE(&srcs_);
if (n_srcs > 0) {
uint32_t i;
for (i = 0; i < n_srcs; i++) {
grn_obj_unref(ctx_, GRN_PTR_VALUE_AT(&srcs_, i));
}
}
GRN_OBJ_FIN(ctx_, &srcs_);
if (src_token_columns_) {
uint32_t i;
for (i = 0; i < n_srcs; i++) {
grn_obj_unref(ctx_, src_token_columns_[i]);
}
auto ctx = ctx_;
GRN_FREE(src_token_columns_);
}
if (src_table_) {
grn_obj_unref(ctx_, src_table_);
}
if (progress_needed_) {
progress_.value.index.phase = GRN_PROGRESS_INDEX_DONE;
grn_ctx_call_progress_callback(ctx_, &progress_);
}
}
// Flushes buffered data as a block.
grn_rc
flush_file_buf()
{
if (file_buf_offset_) {
auto size = grn_write(fd_, file_buf_, file_buf_offset_);
if (static_cast<uint64_t>(size) != file_buf_offset_) {
auto ctx = ctx_;
SERR("failed to write data: expected = %u, actual = %" GRN_FMT_INT64D,
file_buf_offset_,
static_cast<int64_t>(size));
}
file_buf_offset_ = 0;
}
return GRN_SUCCESS;
}
// Creates a temporary file and allocates memory for buffered
// output.
grn_rc
create_file()
{
auto ctx = ctx_;
grn_snprintf(path_,
PATH_MAX,
PATH_MAX,
"%sXXXXXX",
grn_io_path(ii_->seg));
fd_ = grn_mkstemp(path_);
if (fd_ == -1) {
SERR("failed to create a temporary file: path = \"%s\"", path_);
return ctx->rc;
}
file_buf_ =
reinterpret_cast<uint8_t *>(GRN_MALLOC(options_.file_buf_size));
if (!file_buf_) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for buffered output: size = %u",
options_.file_buf_size);
return ctx->rc;
}
return GRN_SUCCESS;
}
// Flushes a term and clears it
grn_rc
flush_term(grn_ii_builder_term *term,
grn_obj *local_lexicon,
grn_id local_tid)
{
/* Append sentinels. */
if (term->rid != GRN_ID_NIL) {
grn_rc rc;
if (ii_->header.common->flags & GRN_OBJ_WITH_POSITION) {
rc = grn_ii_builder_term_append(ctx_, term, 0);
} else {
rc = grn_ii_builder_term_append(ctx_, term, term->pos_or_freq);
}
if (rc != GRN_SUCCESS) {
return rc;
}
}
{
auto rc = grn_ii_builder_term_append(ctx_, term, 0);
if (rc != GRN_SUCCESS) {
return rc;
}
}
{
/* Put the global term ID. */
int key_size;
char key[GRN_TABLE_MAX_KEY_SIZE];
uint8_t *p;
uint32_t rest, value;
grn_id global_tid = GRN_ID_NIL;
key_size = grn_table_get_key(ctx_,
local_lexicon,
local_tid,
key,
GRN_TABLE_MAX_KEY_SIZE);
if (!key_size) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_UNKNOWN_ERROR, "failed to get key: tid = %u", local_tid);
}
return ctx_->rc;
}
/* Don't normalize key. */
switch (ii_->lexicon->header.type) {
case GRN_TABLE_PAT_KEY:
GRN_TABLE_LOCK_BEGIN(ctx_, ii_->lexicon)
{
global_tid = grn_pat_add(ctx_,
reinterpret_cast<grn_pat *>(ii_->lexicon),
key,
key_size,
nullptr,
nullptr);
}
GRN_TABLE_LOCK_END(ctx_);
break;
case GRN_TABLE_DAT_KEY:
GRN_TABLE_LOCK_BEGIN(ctx_, ii_->lexicon)
{
global_tid = grn_dat_add(ctx_,
reinterpret_cast<grn_dat *>(ii_->lexicon),
key,
key_size,
nullptr,
nullptr);
}
GRN_TABLE_LOCK_END(ctx_);
break;
case GRN_TABLE_HASH_KEY:
GRN_TABLE_LOCK_BEGIN(ctx_, ii_->lexicon)
{
global_tid =
grn_hash_add(ctx_,
reinterpret_cast<grn_hash *>(ii_->lexicon),
key,
key_size,
nullptr,
nullptr);
}
GRN_TABLE_LOCK_END(ctx_);
break;
default:
global_tid = GRN_ID_NIL;
break;
}
if (global_tid == GRN_ID_NIL) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_UNKNOWN_ERROR,
"failed to get global term ID: tid = %u, key = \"%.*s\"",
local_tid,
key_size,
key);
}
return ctx_->rc;
}
rest = options_.file_buf_size - file_buf_offset_;
if (rest < 10) {
auto rc = flush_file_buf();
if (rc != GRN_SUCCESS) {
return rc;
}
}
value = global_tid;
p = file_buf_ + file_buf_offset_;
if (value < 1U << 5) {
p[0] = static_cast<uint8_t>(value);
file_buf_offset_++;
} else if (value < 1U << 13) {
p[0] = static_cast<uint8_t>((value & 0x1f) | (1 << 5));
p[1] = static_cast<uint8_t>(value >> 5);
file_buf_offset_ += 2;
} else {
uint8_t i, n;
if (value < 1U << 21) {
n = 3;
} else if (value < 1U << 29) {
n = 4;
} else {
n = 5;
}
p[0] = static_cast<uint8_t>(value & 0x1f) | ((n - 1) << 5);
value >>= 5;
for (i = 1; i < n; i++) {
p[i] = static_cast<uint8_t>(value);
value >>= 8;
}
file_buf_offset_ += n;
}
}
/* Flush a term buffer. */
auto term_buf = grn_ii_builder_term_get_buf(term);
if (term->offset > options_.file_buf_size) {
ssize_t size;
auto rc = flush_file_buf();
if (rc != GRN_SUCCESS) {
return rc;
}
size = grn_write(fd_, term_buf, term->offset);
if (static_cast<uint64_t>(size) != term->offset) {
auto ctx = ctx_;
SERR("failed to write data: expected = %u, actual = %" GRN_FMT_INT64D,
term->offset,
static_cast<int64_t>(size));
}
} else {
uint32_t rest = options_.file_buf_size - file_buf_offset_;
if (term->offset <= rest) {
grn_memcpy(file_buf_ + file_buf_offset_, term_buf, term->offset);
file_buf_offset_ += term->offset;
} else {
grn_memcpy(file_buf_ + file_buf_offset_, term_buf, rest);
file_buf_offset_ += rest;
auto rc = flush_file_buf();
if (rc != GRN_SUCCESS) {
return rc;
}
file_buf_offset_ = term->offset - rest;
grn_memcpy(file_buf_, term_buf + rest, file_buf_offset_);
}
}
grn_ii_builder_term_reinit(ctx_, term);
return GRN_SUCCESS;
}
// Registers a block.
grn_rc
register_block()
{
uint64_t file_offset = grn_lseek(fd_, 0, SEEK_CUR);
if (file_offset == static_cast<uint64_t>(-1)) {
auto ctx = ctx_;
SERR("failed to get file offset");
return ctx->rc;
}
if (n_blocks_ >= blocks_size_) {
uint32_t new_blocks_size = 1;
while (new_blocks_size <= n_blocks_) {
new_blocks_size *= 2;
}
auto ctx = ctx_;
auto n_bytes = new_blocks_size * sizeof(grn_ii_builder_block);
auto new_blocks = reinterpret_cast<grn_ii_builder_block *>(
GRN_REALLOC(blocks_, n_bytes));
if (!new_blocks) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for block: n_bytes = %" GRN_FMT_SIZE,
n_bytes);
return ctx->rc;
}
blocks_ = new_blocks;
blocks_size_ = new_blocks_size;
}
auto block = &(blocks_[n_blocks_]);
grn_ii_builder_block_init(ctx_, block);
if (n_blocks_ == 0) {
block->offset = 0;
} else {
auto prev_block = &(blocks_[n_blocks_ - 1]);
block->offset = prev_block->offset + prev_block->rest;
}
block->rest = static_cast<uint32_t>(file_offset - block->offset);
n_blocks_++;
return GRN_SUCCESS;
}
// Flushes a block to a temporary file.
grn_rc
flush_block_builder(BlockBuilder *block_builder)
{
auto rc = block_builder->flush(
[&](grn_ii_builder_term *term, grn_obj *lexicon, grn_id tid) {
if (fd_ == -1) {
auto rc = create_file();
if (rc != GRN_SUCCESS) {
return rc;
}
}
return flush_term(term, lexicon, tid);
});
if (rc == GRN_SUCCESS) {
rc = flush_file_buf();
}
if (rc == GRN_SUCCESS) {
// Register a block in a temporary file.
rc = register_block();
}
return rc;
}
// Reads values from source columns and appends the values in parallel.
grn_rc
append_srcs_parallel(grn::TaskExecutor *executor, size_t n_records_per_task)
{
std::mutex mutex;
std::condition_variable cv;
std::map<int, std::unique_ptr<BlockBuilder>> processed_block_builders;
auto limit = static_cast<int>(n_records_per_task);
for (int offset = 0; offset < static_cast<int>(n_records_);
offset += limit) {
auto execute = [&, offset, limit]() {
auto child_ctx = grn_ctx_pull_child(ctx_);
auto block_builder =
std::make_unique<BlockBuilder>(ctx_,
child_ctx,
ii_,
&options_,
src_table_,
&srcs_,
src_token_columns_,
sid_bits_);
auto rc = block_builder->prepare();
if (rc != GRN_SUCCESS) {
return false;
}
auto cursor = grn_table_cursor_open(child_ctx,
src_table_,
nullptr,
0,
nullptr,
0,
offset,
limit,
GRN_CURSOR_BY_ID);
if (cursor) {
while (true) {
auto rid = grn_table_cursor_next(child_ctx, cursor);
if (rid == GRN_ID_NIL) {
break;
}
rc = block_builder->append_record(rid);
if (rc != GRN_SUCCESS) {
break;
}
}
grn_table_cursor_close(child_ctx, cursor);
}
{
std::lock_guard<std::mutex> lock(mutex);
processed_block_builders.insert(
std::make_pair(offset, std::move(block_builder)));
}
cv.notify_one();
return rc == GRN_SUCCESS;
};
if (!executor->execute(offset,
execute,
"[index][builder][append-src][parallel]")) {
executor->wait_all();
return ctx_->rc;
}
}
for (int offset = 0; offset < static_cast<int>(n_records_);
offset += limit) {
std::unique_ptr<BlockBuilder> block_builder;
{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&] {
return ctx_->rc != GRN_SUCCESS ||
processed_block_builders.find(offset) !=
processed_block_builders.end();
});
if (ctx_->rc == GRN_SUCCESS) {
auto iter = processed_block_builders.find(offset);
block_builder = std::move(iter->second);
processed_block_builders.erase(iter);
}
}
if (ctx_->rc != GRN_SUCCESS) {
break;
}
if (!block_builder) {
break;
}
auto n_processed_records = block_builder->n_processed_records();
if (block_builder->have_data()) {
auto rc = flush_block_builder(block_builder.get());
if (rc != GRN_SUCCESS) {
break;
}
}
if (progress_needed_) {
progress_.value.index.n_processed_records += n_processed_records;
grn_ctx_call_progress_callback(ctx_, &progress_);
}
}
executor->wait_all();
return ctx_->rc;
}
// Reads values from source columns and appends the values in sequential.
grn_rc
append_srcs_sequential()
{
BlockBuilder block_builder(nullptr,
ctx_,
ii_,
&options_,
src_table_,
&srcs_,
src_token_columns_,
sid_bits_);
auto rc = block_builder.prepare();
if (rc != GRN_SUCCESS) {
return rc;
}
/* Create a cursor to get records in the ID order. */
GRN_TABLE_EACH_BEGIN_FLAGS(ctx_,
src_table_,
cursor,
rid,
GRN_CURSOR_BY_ID)
{
/* Read source values and append it. */
rc = block_builder.append_record(rid);
if (rc == GRN_SUCCESS && block_builder.need_flush()) {
rc = flush_block_builder(&block_builder);
}
if (rc != GRN_SUCCESS) {
break;
}
if (progress_needed_) {
progress_.value.index.n_processed_records++;
grn_ctx_call_progress_callback(ctx_, &progress_);
}
}
GRN_TABLE_EACH_END(ctx_, cursor);
if (rc == GRN_SUCCESS && block_builder.have_data()) {
rc = flush_block_builder(&block_builder);
}
return rc;
}
grn_rc
build(grn_obj *tokenizer)
{
grn_tokenizer_build_data data;
data.source_table = src_table_;
data.source_columns = &srcs_;
data.lexicon = ii_->lexicon;
data.index_column = reinterpret_cast<grn_obj *>(ii_);
BlockBuilder block_builder(nullptr,
ctx_,
ii_,
&options_,
src_table_,
&srcs_,
src_token_columns_,
sid_bits_);
auto rc = block_builder.prepare();
if (rc != GRN_SUCCESS) {
return rc;
}
struct UserData {
Builder *builder;
BlockBuilder *block_builder;
grn_id current_rid;
uint32_t current_sid;
bool progress_needed;
grn_progress *progress;
} user_data;
user_data.builder = this;
user_data.block_builder = &block_builder;
user_data.current_rid = GRN_ID_NIL;
user_data.current_sid = 0;
user_data.progress_needed = progress_needed_;
user_data.progress = &progress_;
data.processed_n_records_func = [](grn_ctx *ctx,
grn_tokenizer_build_data *data,
uint32_t n_records,
void *user_data) {
auto user_data_ = static_cast<UserData *>(user_data);
if (user_data_->progress_needed) {
user_data_->progress->value.index.n_processed_records += n_records;
grn_ctx_call_progress_callback(ctx, user_data_->progress);
}
return GRN_SUCCESS;
};
data.start_vectorize_func = [](grn_ctx *ctx,
grn_tokenizer_build_data *data,
uint32_t n_total_records,
void *user_data) {
auto user_data_ = static_cast<UserData *>(user_data);
if (user_data_->progress_needed) {
user_data_->progress->value.index.phase =
GRN_PROGRESS_INDEX_VECTORIZE;
user_data_->progress->value.index.n_target_records = n_total_records;
user_data_->progress->value.index.n_processed_records = 0;
grn_ctx_call_progress_callback(ctx, user_data_->progress);
}
return GRN_SUCCESS;
};
data.start_cluster_func = [](grn_ctx *ctx,
grn_tokenizer_build_data *data,
uint32_t n_total_records,
void *user_data) {
auto user_data_ = static_cast<UserData *>(user_data);
if (user_data_->progress_needed) {
user_data_->progress->value.index.phase = GRN_PROGRESS_INDEX_CLUSTER;
user_data_->progress->value.index.n_target_records = n_total_records;
user_data_->progress->value.index.n_processed_records = 0;
grn_ctx_call_progress_callback(ctx, user_data_->progress);
}
return GRN_SUCCESS;
};
data.start_load_func = [](grn_ctx *ctx,
grn_tokenizer_build_data *data,
uint32_t n_total_records,
void *user_data) {
auto user_data_ = static_cast<UserData *>(user_data);
if (user_data_->progress_needed) {
user_data_->progress->value.index.phase = GRN_PROGRESS_INDEX_LOAD;
user_data_->progress->value.index.n_target_records = n_total_records;
user_data_->progress->value.index.n_processed_records = 0;
grn_ctx_call_progress_callback(ctx, user_data_->progress);
}
return GRN_SUCCESS;
};
data.start_record_func = [](grn_ctx *ctx,
grn_tokenizer_build_data *data,
grn_id rid,
void *user_data) {
auto user_data_ = static_cast<UserData *>(user_data);
user_data_->current_rid = rid;
return GRN_SUCCESS;
};
data.start_section_func = [](grn_ctx *ctx,
grn_tokenizer_build_data *data,
uint32_t sid,
void *user_data) {
auto user_data_ = static_cast<UserData *>(user_data);
user_data_->current_sid = sid;
return GRN_SUCCESS;
};
data.append_tokens_func = [](grn_ctx *ctx,
grn_tokenizer_build_data *data,
grn_obj *tokens,
void *user_data) {
auto user_data_ = static_cast<UserData *>(user_data);
return user_data_->block_builder->append_tokens(user_data_->current_rid,
user_data_->current_sid,
tokens);
};
data.finish_section_func = [](grn_ctx *ctx,
grn_tokenizer_build_data *data,
void *user_data) { return GRN_SUCCESS; };
data.finish_record_func = [](grn_ctx *ctx,
grn_tokenizer_build_data *data,
void *user_data) {
auto user_data_ = static_cast<UserData *>(user_data);
user_data_->block_builder->finish_record();
if (user_data_->block_builder->need_flush()) {
auto rc =
user_data_->builder->flush_block_builder(user_data_->block_builder);
if (rc != GRN_SUCCESS) {
return rc;
}
}
if (user_data_->builder->progress_needed_) {
user_data_->builder->progress_.value.index.n_processed_records++;
grn_ctx_call_progress_callback(ctx,
&(user_data_->builder->progress_));
}
return GRN_SUCCESS;
};
data.user_data = &user_data;
rc = grn_tokenizer_build(ctx_, tokenizer, &data);
if (rc == GRN_SUCCESS && block_builder.have_data()) {
rc = flush_block_builder(&block_builder);
}
return rc;
}
// Reads values from source columns and appends the values.
grn_rc
append_srcs()
{
if (grn_table_have_tokenizer(ctx_, ii_->lexicon)) {
auto tokenizer = grn_obj_get_info(ctx_,
ii_->lexicon,
GRN_INFO_DEFAULT_TOKENIZER,
nullptr);
if (grn_tokenizer_have_build_func(ctx_, tokenizer)) {
return build(tokenizer);
}
}
if (progress_needed_) {
progress_.value.index.phase = GRN_PROGRESS_INDEX_LOAD;
progress_.value.index.n_target_records = n_records_;
progress_.value.index.n_processed_records = 0;
grn_ctx_call_progress_callback(ctx_, &progress_);
}
auto task_executor = grn_ctx_get_task_executor(ctx_);
if (!task_executor->is_parallel()) {
return append_srcs_sequential();
}
// This is a heuristic rule. We may want to revisit this.
const size_t n_postings_per_source = 1024; // No reason.
const size_t max_n_records_per_task =
options_.block_threshold /
(n_postings_per_source * GRN_PTR_VECTOR_SIZE(&srcs_));
const size_t max_n_records_per_worker =
n_records_ / task_executor->get_n_workers();
const size_t min_n_records_per_task = 10240; // No reason.
const size_t n_records_per_task =
std::max(std::min(max_n_records_per_task, max_n_records_per_worker),
min_n_records_per_task);
if (n_records_ < n_records_per_task) {
return append_srcs_sequential();
} else {
return append_srcs_parallel(task_executor, n_records_per_task);
}
}
// Sets a source table.
grn_rc
set_src_table()
{
src_table_ = grn_ctx_at(ctx_, DB_OBJ(ii_)->range);
if (!src_table_) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_INVALID_ARGUMENT,
"source table is null: range = %d",
DB_OBJ(ii_)->range);
}
return ctx_->rc;
}
n_records_ = grn_table_size(ctx_, src_table_);
return GRN_SUCCESS;
}
// Calculates sid_bits_ and sid_mask_.
grn_rc
set_sid_bits()
{
/* Calculate the number of bits required to represent a section ID. */
if (GRN_PTR_VECTOR_SIZE(&srcs_) == 1 &&
grn_table_have_tokenizer(ctx_, ii_->lexicon) &&
(GRN_PTR_VALUE_AT(&srcs_, 0)->header.flags & GRN_OBJ_COLUMN_VECTOR) !=
0) {
/* If the source column is a vector column and the index has a
* tokenizer, the maximum sid equals to the maximum number of
* elements. */
size_t max_elems = 0;
grn_table_cursor *cursor;
grn_obj obj;
cursor = grn_table_cursor_open(ctx_,
src_table_,
NULL,
0,
NULL,
0,
0,
-1,
GRN_CURSOR_BY_ID);
if (!cursor) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_OBJECT_CORRUPT, "[index] failed to open table cursor");
}
return ctx_->rc;
}
GRN_TEXT_INIT(&obj, 0);
for (;;) {
grn_id rid = grn_table_cursor_next(ctx_, cursor);
if (rid == GRN_ID_NIL) {
break;
}
GRN_BULK_REWIND(&obj);
if (!grn_obj_get_value(ctx_,
GRN_PTR_VALUE_AT(&srcs_, 0),
rid,
&obj)) {
continue;
}
if (obj.u.v.n_sections > max_elems) {
max_elems = obj.u.v.n_sections;
}
}
GRN_OBJ_FIN(ctx_, &obj);
grn_table_cursor_close(ctx_, cursor);
while ((static_cast<uint32_t>(1) << sid_bits_) < max_elems) {
sid_bits_++;
}
}
if (sid_bits_ == 0) {
const uint32_t n_srcs = GRN_PTR_VECTOR_SIZE(&srcs_);
while ((static_cast<uint32_t>(1) << sid_bits_) < n_srcs) {
sid_bits_++;
}
}
sid_mask_ = (static_cast<uint64_t>(1) << sid_bits_) - 1;
return GRN_SUCCESS;
}
// Sets source columns.
grn_rc
set_srcs()
{
uint32_t n_srcs = ii_->obj.source_size / sizeof(grn_id);
auto source = static_cast<grn_id *>(ii_->obj.source);
if (!source || n_srcs == 0) {
auto ctx = ctx_;
ERR(GRN_INVALID_ARGUMENT,
"source is not available: source = %p, source_size = %u",
ii_->obj.source,
ii_->obj.source_size);
return ctx->rc;
}
for (size_t i = 0; i < n_srcs; i++) {
grn_obj *src = grn_ctx_at(ctx_, source[i]);
if (!src) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_OBJECT_CORRUPT, "source not found: id = %d", source[i]);
}
return ctx_->rc;
}
GRN_PTR_PUT(ctx_, &srcs_, src);
}
{
auto ctx = ctx_;
src_token_columns_ = GRN_MALLOCN(grn_obj *, n_srcs);
}
if (!src_token_columns_) {
return GRN_NO_MEMORY_AVAILABLE;
}
{
grn_obj token_columns;
GRN_PTR_INIT(&token_columns, GRN_OBJ_VECTOR, GRN_ID_NIL);
for (size_t i = 0; i < n_srcs; i++) {
src_token_columns_[i] = nullptr;
auto src = GRN_PTR_VALUE_AT(&srcs_, i);
grn_column_get_all_token_columns(ctx_, src, &token_columns);
size_t n_token_columns = GRN_PTR_VECTOR_SIZE(&token_columns);
for (size_t j = 0; j < n_token_columns; j++) {
grn_obj *token_column = GRN_PTR_VALUE_AT(&token_columns, j);
if (!src_token_columns_[i] &&
DB_OBJ(token_column)->range == DB_OBJ(ii_->lexicon)->id) {
src_token_columns_[i] = token_column;
} else {
grn_obj_unref(ctx_, token_column);
}
}
GRN_BULK_REWIND(&token_columns);
}
GRN_OBJ_FIN(ctx_, &token_columns);
}
return set_sid_bits();
}
// Appends values in source columns.
grn_rc
append_source()
{
auto rc = set_src_table();
if (rc != GRN_SUCCESS) {
return rc;
}
if (n_records_ == 0) {
/* Nothing to do because there are no values. */
return ctx_->rc;
}
rc = set_srcs();
if (rc != GRN_SUCCESS) {
return rc;
}
rc = append_srcs();
if (rc != GRN_SUCCESS) {
return rc;
}
return GRN_SUCCESS;
}
// Reads the next data from a temporary file and fill a block
// buffer.
grn_rc
fill_block(uint32_t block_id)
{
grn_ii_builder_block *block = &(blocks_[block_id]);
if (!block->rest) {
return GRN_END_OF_DATA;
}
if (!block->buf) {
auto ctx = ctx_;
block->buf =
reinterpret_cast<uint8_t *>(GRN_MALLOC(options_.block_buf_size));
if (!block->buf) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for buffered input: size = %u",
options_.block_buf_size);
return ctx->rc;
}
}
/* Move the remaining data to the head. */
uint32_t buf_rest = block->end - block->cur;
if (buf_rest) {
grn_memmove(block->buf, block->cur, buf_rest);
}
block->cur = block->buf;
block->end = block->buf + buf_rest;
/* Read the next data. */
uint64_t file_offset = grn_lseek(fd_, block->offset, SEEK_SET);
if (file_offset != block->offset) {
auto ctx = ctx_;
SERR("failed to seek file: expected = %" GRN_FMT_INT64U
", actual = %" GRN_FMT_INT64D,
block->offset,
file_offset);
return ctx->rc;
}
buf_rest = options_.block_buf_size - buf_rest;
if (block->rest < buf_rest) {
buf_rest = block->rest;
}
ssize_t size = grn_read(fd_, block->end, buf_rest);
if (size <= 0) {
auto ctx = ctx_;
SERR("failed to read data: expected = %u, actual = %" GRN_FMT_INT64D,
buf_rest,
static_cast<int64_t>(size));
return ctx->rc;
}
block->offset += size;
block->rest -= size;
block->end += size;
return GRN_SUCCESS;
}
// Reads the next value from a block.
grn_rc
read_from_block(uint32_t block_id, uint64_t *value)
{
grn_ii_builder_block *block = &(blocks_[block_id]);
grn_rc rc = grn_ii_builder_block_next(ctx_, block, value);
if (rc == GRN_SUCCESS) {
return GRN_SUCCESS;
} else if (rc == GRN_END_OF_DATA) {
rc = fill_block(block_id);
if (rc != GRN_SUCCESS) {
return rc;
}
return grn_ii_builder_block_next(ctx_, block, value);
}
return rc;
}
// Tries to pack a chunk.
grn_rc
pack_chunk(bool *packed)
{
*packed = false;
if (chunk_.offset != 1) { /* df != 1 */
return GRN_SUCCESS;
}
if (chunk_.weight_buf && chunk_.weight_buf[0]) { /* weight != 0 */
return GRN_SUCCESS;
}
if (chunk_.freq_buf[0] != 0) { /* freq != 1 */
return GRN_SUCCESS;
}
grn_id rid = chunk_.rid_buf[0];
uint32_t *a = nullptr;
if (chunk_.sid_buf) {
if (rid >= 0x100000) {
return GRN_SUCCESS;
}
uint32_t sid = chunk_.sid_buf[0] + 1;
if (sid >= 0x800) {
return GRN_SUCCESS;
}
a = array_get(ctx_, ii_, chunk_.tid);
if (!a) {
auto ctx = ctx_;
GRN_DEFINE_NAME(ii_);
grn_obj token;
GRN_TEXT_INIT(&token, 0);
grn_ii_get_term(ctx, ii_, chunk_.tid, &token);
MERR("[ii][builder][chunk][pack] failed to allocate an array: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u:%u)",
name_size,
name,
static_cast<int>(GRN_TEXT_LEN(&token)),
GRN_TEXT_VALUE(&token),
chunk_.tid,
rid,
sid);
GRN_OBJ_FIN(ctx, &token);
return ctx->rc;
}
a[0] = POS_EMBED_RID_SID(rid, sid);
} else {
a = array_get(ctx_, ii_, chunk_.tid);
if (!a) {
auto ctx = ctx_;
GRN_DEFINE_NAME(ii_);
grn_obj token;
GRN_TEXT_INIT(&token, 0);
grn_ii_get_term(ctx, ii_, chunk_.tid, &token);
MERR("[ii][builder][chunk][pack] failed to allocate an array: "
"<%.*s>: "
"<%.*s>(%u): "
"(%u)",
name_size,
name,
static_cast<int>(GRN_TEXT_LEN(&token)),
GRN_TEXT_VALUE(&token),
chunk_.tid,
rid);
GRN_OBJ_FIN(ctx, &token);
return ctx->rc;
}
a[0] = POS_EMBED_RID(rid);
}
uint32_t pos = 0;
if (chunk_.pos_buf) {
pos = chunk_.pos_buf[0];
}
a[1] = pos;
array_unref(ctx_, ii_, chunk_.tid);
*packed = true;
grn_ii_builder_chunk_clear(ctx_, &chunk_);
return GRN_SUCCESS;
}
// Returns a new cinfo.
grn_rc
get_cinfo(chunk_info **cinfo)
{
if (n_cinfos_ == cinfos_size_) {
uint32_t size = (cinfos_size_ > 0) ? (cinfos_size_ * 2) : 1;
size_t n_bytes = size * sizeof(chunk_info);
auto ctx = ctx_;
auto cinfos =
reinterpret_cast<chunk_info *>(GRN_REALLOC(cinfos_, n_bytes));
if (!cinfos) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to allocate memory for cinfos: n_bytes = %" GRN_FMT_SIZE,
n_bytes);
return ctx->rc;
}
cinfos_ = cinfos;
cinfos_size_ = size;
}
*cinfo = &(cinfos_[n_cinfos_++]);
return GRN_SUCCESS;
}
// Flushes a chunk.
grn_rc
flush_chunk()
{
auto rc = grn_ii_builder_chunk_encode(ctx_, &chunk_, NULL, 0);
if (rc != GRN_SUCCESS) {
return rc;
}
uint8_t *in = chunk_.enc_buf;
uint32_t in_size = chunk_.enc_offset;
uint32_t chunk_id;
rc = chunk_new(ctx_, ii_, &chunk_id, chunk_.enc_offset);
if (rc != GRN_SUCCESS) {
return rc;
}
/* Copy to the first segment. */
uint32_t seg_id = chunk_id >> GRN_II_N_CHUNK_VARIATION;
uint32_t seg_offset = (chunk_id & ((1 << GRN_II_N_CHUNK_VARIATION) - 1))
<< GRN_II_W_LEAST_CHUNK;
void *seg = grn_io_seg_ref(ctx_, ii_->chunk, seg_id);
if (!seg) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_UNKNOWN_ERROR,
"failed access chunk segment: chunk_id = %u, seg_id = %u",
chunk_id,
seg_id);
}
return ctx_->rc;
}
uint32_t seg_rest = S_CHUNK - seg_offset;
if (in_size <= seg_rest) {
grn_memcpy(static_cast<uint8_t *>(seg) + seg_offset, in, in_size);
in_size = 0;
} else {
grn_memcpy(static_cast<uint8_t *>(seg) + seg_offset, in, seg_rest);
in += seg_rest;
in_size -= seg_rest;
}
grn_io_seg_unref(ctx_, ii_->chunk, seg_id);
/* Copy to the next segments. */
while (in_size) {
seg_id++;
seg = grn_io_seg_ref(ctx_, ii_->chunk, seg_id);
if (!seg) {
if (ctx_->rc == GRN_SUCCESS) {
auto ctx = ctx_;
ERR(GRN_UNKNOWN_ERROR,
"failed access chunk segment: chunk_id = %u, seg_id = %u",
chunk_id,
seg_id);
}
return ctx_->rc;
}
if (in_size <= S_CHUNK) {
grn_memcpy(seg, in, in_size);
in_size = 0;
} else {
grn_memcpy(seg, in, S_CHUNK);
in += S_CHUNK;
in_size -= S_CHUNK;
}
grn_io_seg_unref(ctx_, ii_->chunk, seg_id);
}
/* Append a cinfo. */
chunk_info *cinfo = nullptr;
rc = get_cinfo(&cinfo);
if (rc != GRN_SUCCESS) {
return rc;
}
cinfo->segno = chunk_id;
cinfo->size = chunk_.enc_offset;
cinfo->dgap = chunk_.rid_gap;
buf_.ii->header.common->total_chunk_size += chunk_.enc_offset;
grn_ii_builder_chunk_clear(ctx_, &chunk_);
return GRN_SUCCESS;
}
// Reads values from a block to a chunk.
grn_rc
read_to_chunk(uint32_t block_id)
{
const uint32_t ii_flags = ii_->header.common->flags;
uint32_t rid = GRN_ID_NIL;
uint32_t last_sid = 0;
for (;;) {
uint64_t value;
auto rc = read_from_block(block_id, &value);
if (rc != GRN_SUCCESS) {
return rc;
}
if (!value) {
break;
}
if (chunk_.offset == chunk_.size) {
rc = grn_ii_builder_chunk_extend_bufs(ctx_, &chunk_, ii_flags);
if (rc != GRN_SUCCESS) {
return rc;
}
}
/* Read record ID. */
uint32_t gap =
static_cast<uint32_t>(value >> sid_bits_); /* In-block gap */
uint32_t freq;
if (gap) {
if (chunk_.n >= options_.chunk_threshold) {
const double threshold_scale = 1 + log(df_ + 1);
const double threshold = options_.chunk_threshold * threshold_scale;
if (chunk_.n >= threshold) {
if (grn_logger_pass(ctx_, GRN_LOG_DEBUG)) {
auto ctx = ctx_;
GRN_DEFINE_NAME(ii_);
grn_obj token;
GRN_TEXT_INIT(&token, 0);
grn_ii_get_term(ctx_, ii_, chunk_.tid, &token);
GRN_LOG(ctx_,
GRN_LOG_DEBUG,
"[ii][builder][read-to-chunk] flush"
"<%.*s>: "
"<%.*s>(%u): "
"n=<%u> "
"df=<%u> "
"threshold=<%.1f>",
name_size,
name,
static_cast<int>(GRN_TEXT_LEN(&token)),
GRN_TEXT_VALUE(&token),
chunk_.tid,
chunk_.n,
df_,
threshold);
GRN_OBJ_FIN(ctx, &token);
}
rc = flush_chunk();
if (rc != GRN_SUCCESS) {
return rc;
}
}
}
last_sid = 0;
}
rid += gap;
gap = rid - chunk_.rid; /* Global gap */
chunk_.rid_buf[chunk_.offset] = (chunk_.offset > 0) ? gap : rid;
chunk_.n++;
chunk_.rid = rid;
chunk_.rid_gap += gap;
df_++;
/* Read section ID. */
if (ii_flags & GRN_OBJ_WITH_SECTION) {
auto sid = static_cast<uint32_t>((value & sid_mask_) + 1);
chunk_.sid_buf[chunk_.offset] = sid - last_sid - 1;
chunk_.n++;
last_sid = sid;
}
/* Read weight. */
if (ii_flags & GRN_OBJ_WITH_WEIGHT) {
uint32_t weight;
rc = read_from_block(block_id, &value);
if (rc != GRN_SUCCESS) {
return rc;
}
weight = static_cast<uint32_t>(value);
chunk_.weight_buf[chunk_.offset] = weight;
chunk_.n++;
}
/* Read positions or a frequency. */
if (ii_flags & GRN_OBJ_WITH_POSITION) {
uint32_t pos = -1;
freq = 0;
for (;;) {
rc = read_from_block(block_id, &value);
if (rc != GRN_SUCCESS) {
return rc;
}
if (!value) {
break;
}
if (chunk_.pos_offset == chunk_.pos_size) {
rc = grn_ii_builder_chunk_extend_pos_buf(ctx_, &chunk_);
if (rc != GRN_SUCCESS) {
return rc;
}
}
if (pos == static_cast<uint32_t>(-1)) {
chunk_.pos_buf[chunk_.pos_offset] =
static_cast<uint32_t>(value - 1);
chunk_.pos_sum += value - 1;
} else {
chunk_.pos_buf[chunk_.pos_offset] = static_cast<uint32_t>(value);
chunk_.pos_sum += value;
}
chunk_.n++;
pos += static_cast<uint32_t>(value);
chunk_.pos_offset++;
freq++;
}
} else {
rc = read_from_block(block_id, &value);
if (rc != GRN_SUCCESS) {
return rc;
}
freq = static_cast<uint32_t>(value);
}
chunk_.freq_buf[chunk_.offset] = freq - 1;
chunk_.n++;
chunk_.offset++;
}
uint64_t value;
auto rc = read_from_block(block_id, &value);
if (rc == GRN_SUCCESS) {
blocks_[block_id].tid = static_cast<grn_id>(value);
} else if (rc == GRN_END_OF_DATA) {
blocks_[block_id].tid = GRN_ID_NIL;
} else {
return rc;
}
return GRN_SUCCESS;
}
// Registers chunks.
grn_rc
register_chunks()
{
auto rc = grn_ii_builder_chunk_encode(ctx_, &chunk_, cinfos_, n_cinfos_);
if (rc != GRN_SUCCESS) {
return rc;
}
if (!grn_ii_builder_buffer_is_assigned(ctx_, &buf_)) {
rc = grn_ii_builder_buffer_assign(ctx_, &buf_, chunk_.enc_offset);
if (rc != GRN_SUCCESS) {
return rc;
}
}
uint32_t buf_tid = buf_.buf->header.nterms;
if (buf_tid >= options_.buffer_max_n_terms ||
buf_.chunk_size - buf_.chunk_offset < chunk_.enc_offset) {
rc = grn_ii_builder_buffer_flush(ctx_, &buf_);
if (rc != GRN_SUCCESS) {
return rc;
}
rc = grn_ii_builder_buffer_assign(ctx_, &buf_, chunk_.enc_offset);
if (rc != GRN_SUCCESS) {
return rc;
}
buf_tid = 0;
}
buffer_term *buf_term = &(buf_.buf->terms[buf_tid]);
buf_term->tid = chunk_.tid;
if (n_cinfos_ > 0) {
buf_term->tid |= CHUNK_SPLIT;
}
buf_term->size_in_buffer = 0;
buf_term->pos_in_buffer = 0;
buf_term->size_in_chunk = chunk_.enc_offset;
buf_term->pos_in_chunk = buf_.chunk_offset;
if (grn_logger_pass(ctx_, GRN_LOG_DEBUG)) {
auto ctx = ctx_;
GRN_DEFINE_NAME(ii_);
grn_obj token;
GRN_TEXT_INIT(&token, 0);
grn_ii_get_term(ctx, ii_, chunk_.tid, &token);
GRN_LOG(ctx,
GRN_LOG_DEBUG,
"[ii][builder][register][chunks] "
"<%.*s>: "
"<%.*s>(%u): "
"n_chunks=<%u> "
"chunk=<%u>/<%u>(%u)/(%u) "
"encoded_chunk_size=<%" GRN_FMT_SIZE ">",
name_size,
name,
static_cast<int>(GRN_TEXT_LEN(&token)),
GRN_TEXT_VALUE(&token),
chunk_.tid,
n_cinfos_,
buf_.chunk_offset,
buf_.chunk_size,
buf_.chunk_size - buf_.chunk_offset,
buf_.ii->chunk->header->segment_size,
chunk_.enc_offset);
GRN_OBJ_FIN(ctx, &token);
}
grn_memcpy(buf_.chunk + buf_.chunk_offset,
chunk_.enc_buf,
chunk_.enc_offset);
buf_.chunk_offset += chunk_.enc_offset;
uint32_t *a = array_get(ctx_, ii_, chunk_.tid);
if (!a) {
auto ctx = ctx_;
GRN_DEFINE_NAME(ii_);
grn_obj token;
GRN_TEXT_INIT(&token, 0);
grn_ii_get_term(ctx_, ii_, chunk_.tid, &token);
MERR("[ii][builder][register][chunks] "
"failed to allocate an array in segment: "
"<%.*s>: "
"<%.*s>(%u): "
"max_n_segments=<%u>",
name_size,
name,
static_cast<int>(GRN_TEXT_LEN(&token)),
GRN_TEXT_VALUE(&token),
chunk_.tid,
ii_->seg->header->max_segment);
GRN_OBJ_FIN(ctx, &token);
return ctx->rc;
}
a[0] = grn_ii_pos_pack(ii_,
buf_.buf_id,
POS_LOFFSET_HEADER + POS_LOFFSET_TERM * buf_tid);
a[1] = df_;
array_unref(ctx_, ii_, chunk_.tid);
buf_.buf->header.nterms++;
n_cinfos_ = 0;
grn_ii_builder_chunk_clear(ctx_, &chunk_);
return GRN_SUCCESS;
}
grn_rc
commit()
{
if (progress_needed_) {
progress_.value.index.phase = GRN_PROGRESS_INDEX_COMMIT;
progress_.value.index.n_target_terms =
grn_table_size(ctx_, ii_->lexicon);
grn_ctx_call_progress_callback(ctx_, &progress_);
}
for (uint32_t i = 0; i < n_blocks_; i++) {
uint64_t value;
auto rc = read_from_block(i, &value);
if (rc != GRN_SUCCESS) {
return rc;
}
blocks_[i].tid = static_cast<grn_id>(value);
}
// See also the comment in BlockBuilder::flush().
auto rc = table_each_by_key(ctx_, ii_->lexicon, [&](grn_id tid) {
chunk_.tid = tid;
chunk_.rid = GRN_ID_NIL;
df_ = 0;
for (uint32_t i = 0; i < n_blocks_; i++) {
if (tid == blocks_[i].tid) {
auto rc = read_to_chunk(i);
if (rc != GRN_SUCCESS) {
return rc;
}
}
}
if (chunk_.n == 0) {
/* This term does not appear. */
return GRN_SUCCESS;
}
if (n_cinfos_ == 0) {
bool packed;
auto rc = pack_chunk(&packed);
if (rc != GRN_SUCCESS) {
return rc;
}
if (packed) {
return GRN_SUCCESS;
}
}
auto rc = register_chunks();
if (rc != GRN_SUCCESS) {
return rc;
}
if (progress_needed_) {
progress_.value.index.n_processed_terms++;
grn_ctx_call_progress_callback(ctx_, &progress_);
}
return GRN_SUCCESS;
});
if (rc != GRN_SUCCESS) {
return rc;
}
if (grn_ii_builder_buffer_is_assigned(ctx_, &buf_)) {
rc = grn_ii_builder_buffer_flush(ctx_, &buf_);
if (rc != GRN_SUCCESS) {
return rc;
}
}
return GRN_SUCCESS;
}
grn_ctx *ctx_;
bool progress_needed_; /* Whether progress callback is needed for
performance */
grn_progress progress_; /* Progress information */
grn_ii *ii_; /* Building inverted index */
grn_ii_builder_options options_; /* Options */
grn_obj *src_table_; /* Source table */
unsigned int n_records_; /* Number of records in source table */
grn_obj srcs_; /* Source columns (to be freed) */
grn_obj **src_token_columns_; /* Source token columns (to be freed) */
uint8_t sid_bits_; /* Number of bits for section ID */
uint64_t sid_mask_; /* Mask bits for section ID */
/* A temporary file to save blocks. */
char path_[PATH_MAX]; /* File path */
int fd_; /* File descriptor (to be closed) */
uint8_t *file_buf_; /* File buffer for buffered output (to be freed) */
uint32_t file_buf_offset_; /* File buffer write offset */
grn_ii_builder_block *blocks_; /* Blocks (to be freed) */
uint32_t n_blocks_; /* Number of blocks */
uint32_t blocks_size_; /* Buffer size of blocks */
grn_ii_builder_buffer buf_; /* Buffer (to be finalized) */
grn_ii_builder_chunk chunk_; /* Chunk (to be finalized) */
uint32_t df_; /* Document frequency (number of sections) */
chunk_info *cinfos_; /* Chunk headers (to be freed) */
uint32_t n_cinfos_; /* Number of chunks */
uint32_t cinfos_size_; /* Size of cinfos */
};
} // namespace grn::ii
extern "C" grn_rc
grn_ii_build(grn_ctx *ctx, grn_ii *ii, const grn_ii_builder_options *options)
{
grn_rc rc = GRN_SUCCESS;
{
grn::ii::Builder builder(ctx, ii, options);
rc = builder.append_source();
if (rc == GRN_SUCCESS) {
rc = builder.commit();
}
}
if (rc != GRN_SUCCESS) {
grn_obj_flush(ctx, reinterpret_cast<grn_obj *>(ii));
}
return rc;
}
extern "C" grn_rc
grn_ii_wal_recover(grn_ctx *ctx, grn_ii *ii)
{
if (GRN_CTX_GET_WAL_ROLE(ctx) != GRN_WAL_ROLE_PRIMARY) {
return GRN_SUCCESS;
}
if (!grn_wal_exist(ctx, (grn_obj *)ii)) {
if (grn_io_is_locked(ii->seg)) {
grn_io_clear_lock(ii->seg);
grn_obj_touch(ctx, (grn_obj *)ii, NULL);
grn_obj_flush(ctx, (grn_obj *)ii);
}
return GRN_SUCCESS;
}
grn_obj_set_error(ctx,
(grn_obj *)ii,
GRN_FUNCTION_NOT_IMPLEMENTED,
GRN_ID_NIL,
"[ii][wal][recover]",
"not implemented");
return ctx->rc;
}
extern "C" grn_rc
grn_ii_warm(grn_ctx *ctx, grn_ii *ii)
{
grn_rc rc = grn_io_warm(ctx, ii->seg);
if (rc == GRN_SUCCESS) {
rc = grn_io_warm(ctx, ii->chunk);
}
return rc;
}
|