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
|
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal. All Rights Reserved. */
/* ./glimpse/index/build_in.c */
/* --------------------------------------------------------------
build_index(): build an index list from a set of files.
INPUT: a set of file names
char **name_list[];
a partition table
int p_table[];
OUTPUT: an index list;
char *index_list;
the index list is a char string as follows:
each entry of the index list contains two parts:
name and indices, where name is an ascii character string,
and indices is a list of short integer. (unsigned char)
We use newline as a 'record delimiter' (a 'record is logically
a word associated with its indices), and WORD_END_MARK to separate
a word from its list of indices (s.t. fscanf %s works).
Since we restrict the max number of partitions to be 255.
a byte is enough to represent the index value. Note that there
cannot be a partition #ed '\n'.
An example index list: (in logical view)
this 12 19 \n is 9 17 12 18 19 \n an 7 12 \n example 16 \n
-----------------------------------------------------------------------*/
#include "glimpse.h"
#include <errno.h>
#include "../missing_prototypes.h"
int merge_in(FILE *, FILE *, FILE *);
int init_hash_table();
#define debugt
#define BINARY 1
/* #define SW_DEBUG the original sw output of index set */
/* This flag must always be defined: it is used only in build_in.c */
/* #define UDI_DEBUG the original outputs of each indexed file */
/* Some variables used throughout */
#if BG_DEBUG
extern FILE *LOGFILE; /* file descriptor for LOG output */
#endif /*BG_DEBUG*/
extern FILE *STATFILE; /* file descriptor for statistical data about indexed files */
extern FILE *MESSAGEFILE; /* file descriptor for important messages meant for the user */
extern char INDEX_DIR[MAX_LINE_LEN];
extern char sync_path[MAX_LINE_LEN];
extern struct stat istbuf;
extern struct stat excstbuf;
extern struct stat incstbuf;
void insert_h();
void insert_index();
extern int ICurrentFileOffset;
extern int NextICurrentFileOffset;
/* Some options used throughout */
extern int OneFilePerBlock;
extern int IndexNumber;
extern int CountWords;
extern int StructuredIndex;
extern int InterpretSpecial;
extern int total_size;
extern int MAXWORDSPERFILE;
extern int NUMERICWORDPERCENT;
extern int AddToIndex;
extern int DeleteFromIndex;
extern int FastIndex;
extern int BuildDictionary;
extern int BuildDictionaryExisting;
extern int CompressAfterBuild;
extern int IncludeHigherPriority;
extern int FilenamesOnStdin;
extern int UseFilters;
extern int ByteLevelIndex;
extern int RecordLevelIndex;
extern int StoreByteOffset;
extern int rdelim_len;
extern char rdelim[MAX_LINE_LEN];
extern char old_rdelim[MAX_LINE_LEN];
/* int IndexUnderscore; */
extern int IndexableFile;
extern int MAX_INDEX_PERCENT;
extern int MAX_PER_MB;
extern int I_THRESHOLD;
extern int usemalloc;
extern int BigHashTable;
extern int AddedMaxWordsMessage;
extern int AddedMixedWordsMessage;
extern int icount; /* count the number of my_malloc for indices structure */
extern int hash_icount; /* to see how much was added to the current hash table */
extern int save_icount; /* to see how much was added to the index by the current file */
extern int numeric_icount; /* to see how many numeric words were there in the current file */
extern int num_filter;
extern int filter_len[MAX_FILTER];
extern CHAR *filter[MAX_FILTER];
extern CHAR *filter_command[MAX_FILTER];
extern int REAL_PARTITION, REAL_INDEX_BUF, MAX_ALL_INDEX, FILEMASK_SIZE;
extern int mask_int[32];
struct indices *deletedlist = NULL;
char **name_list[MAXNUM_INDIRECT];
unsigned int *disable_list = NULL;
int *size_list[MAXNUM_INDIRECT]; /* temporary area to store size of each file */
extern int p_table[MAX_PARTITION];
int p_size_list[MAX_PARTITION]; /* sum of the sizes of the files in each partition */
int part_num; /* number of partitions */
extern int memory_usage;
/* borrowd from getword.c */
extern int PrintedLongWordWarning;
extern int indexable_char[256];
extern char *getword();
extern int file_num;
extern int old_file_num;
extern int attr_num;
extern int bp; /* buffer pointer */
extern unsigned char word[MAX_WORD_BUF];
extern int FirstTraverse1;
extern struct indices *ip;
extern int HashTableSize;
struct token **hash_table; /*[MAX_64K_HASH];*/
build_index()
{
int i;
if (AddToIndex || FastIndex) {
FirstTraverse1 = OFF;
}
if ((total_size < LIMIT_64K_HASH*1024*1024) || !BigHashTable) {
hash_table = (struct token **)my_malloc(sizeof(struct token *) * MAX_64K_HASH);
HashTableSize = MAX_64K_HASH;
}
else {
hash_table = (struct token **)my_malloc(sizeof(struct token *) * MAX_256K_HASH);
HashTableSize = MAX_256K_HASH;
}
build_hash();
/* traverse1(); ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ removed on oct/8/96, bgopal, to see if crazysegvs disappear on lec */
return;
}
/* ----------------------------------------------------------------------
traverse()
function: traverse the hash list of indices = a hash list is a array of
linked list, where every node in a linked list contains a word whose
hash_value is the same.
While traversing the hash list, traverse() output a stream of index list.
It also frees the memory used in hash_table.
------------------------------------------------------------------------*/
#define CRAZYSEGV 0
traverse()
{
int numseencount = 0;
int numelements;
int numonline;
int i, j, attribute;
struct token *tp, *tp_old;
struct indices *ip, *ip_old;
#if !CRAZYSEGV
FILE *f_out;
#else
unsigned char onechar[4];
unsigned char onestring[MAX_LINE_LEN];
int f_out;
#endif
char s[MAX_LINE_LEN];
char *word;
int x = -1, y=0, diff, temp, even_words=1; /* 0 is an even number */
int fputcerr; /* added by dgh 5-8-96 */
#ifdef SW_DEBUG
printf("in traverse()\n");
#endif
sprintf(s, "%s/%s", INDEX_DIR, I2);
#if !CRAZYSEGV
if ((f_out = fopen(s, "w")) == NULL) {
#else
if ((f_out = open(s, O_WRONLY|O_CREAT|O_TRUNC, 0600)) == -1) {
#endif
fprintf(stderr, "Cannot open %s for writing\n", s);
exit(2);
}
for(i=0; i<HashTableSize; i++) {
if(hash_table[i] == NULL) continue;
tp = hash_table[i];
tp_old = tp;
while(tp != NULL) { /* traverse the token list */
word = tp->word;
while(*word != '\0') { /* copy the word to output */
#if !CRAZYSEGV
fputcerr=fputc(*word++, f_out);/* change from putc to fputc */
/* by dgh, 8-5-96 */
#else
write(f_out, word, 1);
word++;
#endif
}
/* Look for stop lists */
if (OneFilePerBlock && !ByteLevelIndex && (file_num > MaxNum8bPartition) && (tp->totalcount > (file_num * MAX_INDEX_PERCENT / 100))) {
#if !CRAZYSEGV
putc(ALL_INDEX_MARK, f_out);
#else
onechar[0] = ALL_INDEX_MARK;
write(f_out, onechar, 1);
#endif
if (StructuredIndex) { /* force big-endian as usual */
attribute = encode16b(tp->attribute);
#if !CRAZYSEGV
putc((attribute&0x0000ff00)>>8, f_out);
putc((attribute&0x000000ff), f_out);
#else
onechar[0] = (attribute&0x0000ff00)>>8;
onechar[1] = (attribute&0x000000ff);
write(f_out, onechar, 2);
#endif
}
#if !CRAZYSEGV
putc(DONT_CONFUSE_SORT, f_out);
#else
onechar[0] = DONT_CONFUSE_SORT;
write(f_out, onechar, 1);
#endif
goto next_token;
}
else if (ByteLevelIndex && (tp->totalcount > ( (((total_size>>20) > 0) && ((total_size>>20)*MAX_PER_MB < MAX_ALL_INDEX)) ? ((total_size>>20) * MAX_PER_MB) : MAX_ALL_INDEX) )) {
#if !CRAZYSEGV
putc(ALL_INDEX_MARK, f_out);
#else
onechar[0] = ALL_INDEX_MARK;
write(f_out, onechar, 1);
#endif
if (StructuredIndex) { /* force big-endian as usual */
attribute = encode16b(tp->attribute);
#if !CRAZYSEGV
putc((attribute&0x0000ff00)>>8, f_out);
putc((attribute&0x000000ff), f_out);
#else
onechar[0] = (attribute&0x0000ff00)>>8;
onechar[1] = (attribute&0x000000ff);
write(f_out, onechar, 2);
#endif
}
#if !CRAZYSEGV
putc(DONT_CONFUSE_SORT, f_out);
#else
onechar[0] = DONT_CONFUSE_SORT;
write(f_out, onechar, 2);
#endif
goto next_token;
}
#if !CRAZYSEGV
putc(WORD_END_MARK, f_out);
#else
onechar[0] = WORD_END_MARK;
write(f_out, onechar, 1);
#endif
if (StructuredIndex) { /* force big-endian as usual */
attribute = encode16b(tp->attribute);
#if !CRAZYSEGV
putc((attribute&0x0000ff00)>>8, f_out);
putc((attribute&0x000000ff), f_out);
#else
onechar[0] = (attribute&0x0000ff00)>>8;
onechar[1] = (attribute&0x000000ff);
write(f_out, onechar, 2);
#endif
}
numonline = 0;
x = -1;
y = 0;
even_words = 1;
ip = tp->ip; /* traverse the indices list */
ip_old = ip;
numelements = 0;
while(ip != NULL) {
numelements ++;
if (CountWords) {
#if !CRAZYSEGV
fprintf(f_out, "%d", ip->offset[0]);
#else
sprintf(onestring, "%d", ip->offset[0]);
write(f_out, onestring, strlen(onestring));
#endif
}
else {
if (ByteLevelIndex) {
for (j=0; j < INDEX_SET_SIZE; j++) {
if (ip->index[j] == INDEX_ELEM_FREE) continue;
if ((ip->offset[j] <= y) && (y > 0) && (x == ip->index[j])) { /* consecutive offsets not increasing in same file! */
fprintf(stderr, "ignoring (%d, %d) > (%d, %d)\n", x, y, ip->index[j], ip->offset[j]);
continue; /* error! */
}
if (numonline >= MAX_PER_LINE) {
/* terminate current line since it is too late to put ALL_INDEX_MARK now ... Unfortunate since sort is screwedup */
#if !CRAZYSEGV
putc('\n', f_out);
#else
onechar[0] = '\n';
write(f_out, onechar, 1);
#endif
#if 0
putc('\n', stdout);
#endif /*0*/
word = tp->word;
while(*word != '\0') { /* copy the word to output */
#if !CRAZYSEGV
putc(*word++, f_out);
#else
write(f_out, word, 1);
word ++;
#endif
}
#if !CRAZYSEGV
putc(WORD_END_MARK, f_out);
#else
onechar[0] = WORD_END_MARK;
write(f_out, onechar, 1);
#endif
if (StructuredIndex) { /* force big-endian as usual */
attribute = encode16b(tp->attribute);
#if !CRAZYSEGV
putc((attribute&0x0000ff00)>>8, f_out);
putc((attribute&0x000000ff), f_out);
#else
onechar[0] = (attribute&0x0000ff00)>>8;
onechar[1] = (attribute&0x000000ff);
write(f_out, onechar, 2);
#endif
}
numonline = 0;
x = -1; /* to force code below to output it as if it is a fresh file */
y = 0; /* must output first offset as is, rather than difference */
}
if (x != ip->index[j]) {
if (x != -1) {
temp = encode8b(0);
#if !CRAZYSEGV
putc(temp, f_out); /* can never ordinarily happen since ICurrentFileOffset is always ++d => delimiter (unless RecordLevelIndex) */
#else
onechar[0] = temp;
write(f_out, onechar, 1);
#endif
}
if (file_num <= MaxNum8bPartition) {
x = encode8b(ip->index[j]);
#if !CRAZYSEGV
putc(x&0x000000ff, f_out);
#else
onechar[0] = x&0x000000ff;
write(f_out, onechar, 1);
#endif
}
else if (file_num <= MaxNum16bPartition) {
x = encode16b(ip->index[j]);
#if !CRAZYSEGV
putc((x&0x0000ff00)>>8, f_out);
putc(x&0x000000ff, f_out);
#else
onechar[0] = (x&0x0000ff00)>>8;
onechar[1] = x&0x000000ff;
write(f_out, onechar, 2);
#endif
}
else {
x = encode24b(ip->index[j]);
#if !CRAZYSEGV
putc((x&0x00ff0000)>>16, f_out);
putc((x&0x0000ff00)>>8, f_out);
putc(x&0x000000ff, f_out);
#else
onechar[0] = (x&0x00ff0000)>>16;
onechar[1] = (x&0x0000ff00)>>8;
onechar[2] = x&0x000000ff;
write(f_out, onechar, 3);
#endif
}
x = ip->index[j]; /* for next round */
#if 0
printf("#######x=%d ", x);
#endif /*0*/
y = 0;
}
diff = ip->offset[j] - y;
y = ip->offset[j];
if (diff < MaxNum1BPartition) {
temp = encode8b(diff);
#if !CRAZYSEGV
putc(temp, f_out);
#else
onechar[0] = temp;
write(f_out, onechar, 1);
#endif
}
else if (diff < MaxNum2BPartition) {
temp = encode8b((diff/MaxNum8bPartition) | 0x40);
#if !CRAZYSEGV
putc(temp, f_out);
#else
onechar[0] = temp;
write(f_out, onechar, 1);
#endif
temp = encode8b(diff % MaxNum8bPartition);
#if !CRAZYSEGV
putc(temp, f_out);
#else
onechar[0] = temp;
write(f_out, onechar, 1);
#endif
}
else if (diff < MaxNum3BPartition) {
temp = encode8b((diff/MaxNum16bPartition) | 0x80);
#if !CRAZYSEGV
putc(temp, f_out);
#else
onechar[0] = temp;
write(f_out, onechar, 1);
#endif
temp = encode16b(diff % MaxNum16bPartition);
#if !CRAZYSEGV
putc((temp & 0x0000ff00) >> 8, f_out);
putc(temp & 0x000000ff, f_out);
#else
onechar[0] = (temp & 0x0000ff00) >> 8;
onechar[1] = temp & 0x000000ff;
write(f_out, onechar, 2);
#endif
}
else {
temp = encode8b((diff/MaxNum24bPartition) | 0xc0);
#if !CRAZYSEGV
putc(temp, f_out);
#else
onechar[0] = temp;
write(f_out, onechar, 1);
#endif
temp = encode24b(diff % MaxNum24bPartition);
#if !CRAZYSEGV
putc((temp & 0x00ff0000) >> 16, f_out);
putc((temp & 0x0000ff00) >> 8, f_out);
putc(temp & 0x000000ff, f_out);
#else
onechar[0] = (temp & 0x00ff0000) >> 16;
onechar[1] = (temp & 0x0000ff00) >> 8;
onechar[2] = temp & 0x000000ff;
write(f_out, onechar, 3);
#endif
}
numonline ++;
}
} /* ByteLevelIndex */
else if (OneFilePerBlock) {
if (file_num <= MaxNum8bPartition) {
for(j=0; j < INDEX_SET_SIZE; j++) {
if (ip->index[j] == INDEX_ELEM_FREE) continue;
#if !CRAZYSEGV
putc(encode8b(ip->index[j]), f_out);
#else
onechar[0] = encode8b(ip->index[j]);
write(f_out, onechar, 1);
#endif
}
}
else if (file_num <= MaxNum12bPartition) {
for(j=0; j < INDEX_SET_SIZE; j++) {
if (ip->index[j] == INDEX_ELEM_FREE) continue;
x = encode12b(ip->index[j]);
if (even_words) {
#if !CRAZYSEGV
putc(x & 0x000000ff, f_out); /* lsb */
#else
onechar[0] = x & 0x000000ff;
write(f_out, onechar, 1);
#endif
y = (x & 0x00000f00)>>8; /* msb */
even_words = 0;
}
else { /* odd number of words so far */
y |= (x&0x00000f00)>>4; /* msb of x into msb of y */
#if !CRAZYSEGV
putc(y, f_out);
putc(x&0x000000ff, f_out);
#else
onechar[0] = y;
onechar[1] = x&0x000000ff;
write(f_out, onechar, 2);
#endif
even_words = 1;
}
}
}
else if (file_num <= MaxNum16bPartition) {
for(j=0; j < INDEX_SET_SIZE; j++) {
if (ip->index[j] == INDEX_ELEM_FREE) continue;
x = encode16b(ip->index[j]);
#if !CRAZYSEGV
putc((x&0x0000ff00)>>8, f_out);
putc(x&0x000000ff, f_out);
#else
onechar[0] = (x&0x0000ff00)>>8;
onechar[1] = x&0x000000ff;
write(f_out, onechar, 2);
#endif
}
}
else {
for(j=0; j < INDEX_SET_SIZE; j++) {
if (ip->index[j] == INDEX_ELEM_FREE) continue;
x = encode24b(ip->index[j]);
#if !CRAZYSEGV
putc((x&0x00ff0000)>>16, f_out);
putc((x&0x0000ff00)>>8, f_out);
putc(x&0x000000ff, f_out);
#else
onechar[0] = (x&0x00ff0000)>>16;
onechar[1] = (x&0x0000ff00)>>8;
onechar[2] = x&0x000000ff;
write(f_out, onechar, 3);
#endif
}
}
} /* OneFilePerBlock */
else { /* normal partitions */
for(j=0; j < INDEX_SET_SIZE; j++) {
if (ip->index[j] == INDEX_ELEM_FREE) continue;
#if !CRAZYSEGV
putc(ip->index[j], f_out);
#else
onechar[0] = ip->index[j];
write(f_out, onechar, 1);
#endif
}
}
}
ip = ip->next_i; /* go to next indices */
indicesfree(ip_old, sizeof(struct indices));
ip_old = ip;
}
if (!ByteLevelIndex && OneFilePerBlock && !even_words && (file_num > MaxNum8bPartition) && (file_num <= MaxNum12bPartition)) {
#if !CRAZYSEGV
putc(y, f_out);
#else
onechar[0] = y;
write(f_out, onechar, 1);
#endif
}
next_token:
#if !CRAZYSEGV
if (putc('\n', f_out) == EOF) {
#else
onechar[0] = '\n';
if (write(f_out, onechar, 1) <= 0) {
#endif
fprintf(stderr, "Error: write failed at %s:%d\n", __FILE__, __LINE__);
exit(2);
}
tp = tp->next_t; /* go to next token */
#if 0
fprintf(stderr, "numelements=%d\n", numelements);
#endif /*0*/
#if BG_DEBUG
memory_usage -= (strlen(tp_old->word) + 1);
#endif /*BG_DEBUG*/
wordfree(tp_old->word, 0);
tokenfree(tp_old, sizeof(struct token));
tp_old = tp;
numseencount ++;
}
}
tokenallfree();
indicesallfree();
wordallfree();
#if BG_DEBUG
fprintf(stderr, "out of traverse(): saved/freed %d tokens: new usage: %d\n", numseencount, memory_usage);
#endif
#if !CRAZYSEGV
fflush(f_out);
fclose(f_out);
#else
close(f_out);
#endif
}
traverse1()
{
FILE *i1, *i2, *i3;
int ret;
char s[MAX_LINE_LEN], es1[MAX_LINE_LEN], es2[MAX_LINE_LEN], es3[MAX_LINE_LEN];
char s1[MAX_LINE_LEN];
extern int errno;
static int maxsortlinelen = 0;
int i;
if (maxsortlinelen <= 0) {
if (file_num < MaxNum8bPartition)
maxsortlinelen = round((MaxNum8bPartition * sizeof(int) + MAX_NAME_SIZE), MAX_LINE_LEN) * MAX_LINE_LEN;
else if (file_num < MaxNum12bPartition)
maxsortlinelen = round((MaxNum12bPartition * sizeof(int) + MAX_NAME_SIZE), MAX_LINE_LEN) * MAX_LINE_LEN;
else maxsortlinelen = MAX_SORTLINE_LEN;
}
traverse(); /* will produce .i2 and my_free allocated memory */
#if USESORT_Z_OPTION
#if DONTUSESORT_T_OPTION || SFS_COMPAT
sprintf(s, "exec %s -z %d '%s/%s' > '%s/%s'\n", SYSTEM_SORT, maxsortlinelen, escapesinglequote(INDEX_DIR, es1), I2, escapesinglequote(INDEX_DIR, es2), O2);
#else
sprintf(s, "exec %s -T '%s' -z %d '%s/%s' > '%s/%s'\n", SYSTEM_SORT, escapesinglequote(INDEX_DIR, es1), maxsortlinelen, escapesinglequote(INDEX_DIR, es2), I2, escapesinglequote(INDEX_DIR, es3), O2);
#endif
#else
#if DONTUSESORT_T_OPTION || SFS_COMPAT
sprintf(s, "exec %s '%s/%s' > '%s/%s'\n", SYSTEM_SORT, escapesinglequote(INDEX_DIR, es1), I2, escapesinglequote(INDEX_DIR, es2), O2);
#else
sprintf(s, "exec %s -T '%s' '%s/%s' > '%s/%s'\n", SYSTEM_SORT, escapesinglequote(INDEX_DIR, es1), escapesinglequote(INDEX_DIR, es2), I2, escapesinglequote(INDEX_DIR, es3), O2);
#endif
#endif
#ifdef SW_DEBUG
printf("%s", s);
#endif
if((ret=system(s)) != 0) {
sprintf(s1, "system('%s') failed at:\n\t File=%s, Line=%d, Errno=%d", s, __FILE__, __LINE__, errno);
perror(s1);
fprintf(stderr, "Please try to run the program again\n(If there's no memory, increase the swap area / don't use -M and -B options)\n");
exit(2);
}
#ifdef SW_DEBUG
printf("mv .o2 .i2\n"); fflush(stdout);
#endif
#if SFS_COMPAT
sprintf(s, "%s/%s", INDEX_DIR, O2);
sprintf(s1, "%s/%s", INDEX_DIR, I2);
rename(s, s1);
#else
sprintf(s, "exec %s '%s/%s' '%s/%s'\n", SYSTEM_MV, escapesinglequote(INDEX_DIR, es1), O2, escapesinglequote(INDEX_DIR, es2), I2);
system(s);
#endif
system(sync_path); /* sync() has a bug */
#if 0
printf("traversed\n");
sprintf(s, "exec %s -10 '%s/%s'\n", SYSTEM_HEAD, escapesinglequote(INDEX_DIR, es1), I2);
system(s);
#endif /*0*/
/*
* This flag is set from outside iff build-fast | build-addto option is set.
*/
if(FirstTraverse1) {
/* Mention whether numbers are indexed */
if(IndexNumber) sprintf(s, "exec %s %%1234567890 > '%s/%s'\n", SYSTEM_ECHO, escapesinglequote(INDEX_DIR, es1), INDEX_FILE);
else sprintf(s, "exec %s %% > '%s/%s'\n", SYSTEM_ECHO, escapesinglequote(INDEX_DIR,es1), INDEX_FILE);
system(s);
/* Put the magic number: 0 if not 1file/blk, numfiles otherwise */
if (OneFilePerBlock) {
if (ByteLevelIndex) sprintf(s, "exec %s %%-%d >> '%s/%s'\n", SYSTEM_ECHO, file_num, escapesinglequote(INDEX_DIR, es1), INDEX_FILE);
else sprintf(s, "exec %s %%%d >> '%s/%s'\n", SYSTEM_ECHO, file_num, escapesinglequote(INDEX_DIR, es1), INDEX_FILE);
}
else sprintf(s, "exec %s %%0 >> '%s/%s'\n", SYSTEM_ECHO, escapesinglequote(INDEX_DIR, es1), INDEX_FILE);
system(s);
/* Put the magic number: 0 if not structured index, 1 if so */
if (StructuredIndex) sprintf(s, "exec %s %%%d >> '%s/%s'\n", SYSTEM_ECHO, attr_num, escapesinglequote(INDEX_DIR, es1), INDEX_FILE);
else if (RecordLevelIndex) sprintf(s, "exec %s %%-2 %s >> '%s/%s'\n", SYSTEM_ECHO, old_rdelim, escapesinglequote(INDEX_DIR, es1), INDEX_FILE);
else sprintf(s, "exec %s %%0 >> '%s/%s'\n", SYSTEM_ECHO, escapesinglequote(INDEX_DIR, es1), INDEX_FILE);
system(s);
#ifdef SW_DEBUG
sprintf(s, "exec %s -l %s/.glimpse*\n", SYSTEM_LS, escapesinglequote(INDEX_DIR, es1));
system(s);
#endif
sprintf(s, "exec %s '%s/%s' >> '%s/%s'\n", SYSTEM_CAT, escapesinglequote(INDEX_DIR, es1), I2, escapesinglequote(INDEX_DIR, es2), INDEX_FILE);
system(s);
#if SFS_COMPAT
sprintf(s, "%s/%s", INDEX_DIR, I2);
unlink(s);
#else
sprintf(s, "exec %s '%s/%s'\n", SYSTEM_RM, escapesinglequote(INDEX_DIR, es1), I2);
system(s);
#endif
#ifdef SW_DEBUG
sprintf(s, "exec %s -l %s/.glimpse*\n", SYSTEM_LS, escapesinglequote(INDEX_DIR, es1));
system(s);
#endif
#if 0
printf("catted\n");
sprintf(s, "exec %s -10 '%s/%s'\n", SYSTEM_HEAD, escapesinglequote(INDEX_DIR, es1), INDEX_FILE);
system(s);
#endif /*0*/
FirstTraverse1 = 0;
system(sync_path); /* sync() has a bug */
return;
}
/* else not first-traverse */
sprintf(s, "%s/%s", INDEX_DIR, INDEX_FILE);
if((i1 = fopen(s, "r")) == NULL) { /* new stuff */
fprintf(stderr, "can't open %s for reading\n", s);
exit(2);
}
sprintf(s, "%s/%s", INDEX_DIR, I2);
if((i2 = fopen(s, "r")) == NULL) { /* old stuff */
fprintf(stderr, "can't open %s for reading\n", s);
exit(2);
}
sprintf(s, "%s/%s", INDEX_DIR, I3);
if((i3 = fopen(s, "w")) == NULL) { /* result */
fprintf(stderr, "can't open %s for writing\n", s);
exit(2);
}
/* Copy the 3 option fields (indexnumber, onefileperblock, structuredqueries) */
fgets(s, 256, i1);
s[255] = '\0';
fputs(s, i3);
fgets(s, 256, i1);
s[255] = '\0';
fputs(s, i3);
fgets(s, 256, i1);
s[255] = '\0';
fputs(s, i3);
merge_in(i2, i1, i3);
/* merge_in(i1, i2, i3); */
#ifdef BG_DEBUG
fprintf(stderr, "out of merge_in()\n");
#endif /*BG_DEBUG*/
fclose(i1);
fflush(i2);
fclose(i2);
fflush(i3);
fclose(i3);
system(sync_path); /* sync() has a bug */
#ifdef SW_DEBUG
printf("mv .i3 %s\n", INDEX_FILE); fflush(stdout);
#endif
#if SFS_COMPAT
sprintf(s, "%s/%s", INDEX_DIR, I3);
sprintf(s1, "%s/%s", INDEX_DIR, INDEX_FILE);
rename(s, s1);
#else
sprintf(s, "exec %s '%s/%s' '%s/%s'", SYSTEM_MV, escapesinglequote(INDEX_DIR, es1), I3, escapesinglequote(INDEX_DIR, es2), INDEX_FILE);
system(s);
#endif
/* #ifdef SW_DEBUG */
#if 0
printf("ls -l .i2 %s\n", INDEX_FILE); fflush(stdout);
sprintf(s, "exec %s -l %s/.glimpse*", SYSTEM_LS, escapesinglequote(INDEX_DIR, es1));
printf("%d\n", system(s));
#endif
#if 0
printf("merged\n");
sprintf(s, "exec %s -10 '%s/%s'\n", SYSTEM_HEAD, escapesinglequote(INDEX_DIR, es1), INDEX_FILE);
system(s);
#endif /*0*/
}
/* --------------------------------------------------------------------
build_hash():
input: a set of filenames in name_list[], a partition table p_table[]
output: a hash table hash_table[].
-----------------------------------------------------------------------*/
build_hash()
{
int fd; /* opened file number */
int i, pn; /* pn: current partition */
int num_read;
char word[256];
struct stat stbuf;
int offset;
int toread;
unsigned char *buffer; /* running pointer for getword = place where reads begin */
unsigned char *bx; /* running pointer for read-loop, initially buffer */
unsigned char *buffer_end; /* place where getword should stop */
unsigned char *buffer_begin;/* constant pointer to beginning */
unsigned char *next_record; /* pointer that tells where the current record ends: if buffer (returned by getword) is >= this, increment ICurrentFileOffset */
unsigned char *last_record; /* pointer that tells where the last record ends: may or may not be > buffer_end, but surely <= bx the last byte read */
int residue; /* extra variable to store buffer_begin + BLOCK_SIZE - buffer_end */
int tried_once = 0;
int attribute;
int ret;
char outname[MAX_LINE_LEN];
char *unlinkname = NULL;
int pid = getpid();
if (StructuredIndex) region_initialize();
init_hash_table();
#ifdef debug
printf("entering build_hash(), part_num=%d\n", part_num);
#endif
tried_once = 0;
try_again_1:
buffer_begin = buffer = (unsigned char *) my_malloc(sizeof(char)* BLOCK_SIZE + 10); /* always read in units of BLOCK_SIZE or less */
if(buffer == NULL) {
fprintf(stderr, "not enough memory in build_hash\n");
if (tried_once) return;
traverse1();
init_hash_table();
tried_once = 1;
goto try_again_1;
}
bx = buffer;
if (OneFilePerBlock) {
for(i=0; i<file_num; i++) {
unlinkname = NULL;
if ((disable_list != NULL) && (i<old_file_num) && (disable_list[block2index(i)] & mask_int[i%(8*sizeof(int))])) continue;
if (LIST_GET(name_list, i) == NULL) continue;
if ((ret = tuncompress_file(LIST_GET(name_list, i), outname, TC_EASYSEARCH | TC_OVERWRITE | TC_NOPROMPT)) > 0) { /* do not remove old .TZ file */
if (StructuredIndex && (-1 == region_create(outname))) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
continue;
}
if (((fd = my_open(outname, O_RDONLY, 0)) == -1) ) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
if (StructuredIndex) region_destroy(); /* cannot happen! */
unlink(outname);
continue;
}
unlinkname = outname;
goto index_file1;
}
/* Try to apply the filter */
sprintf(outname, "%s/.glimpse_apply.%d", INDEX_DIR, pid);
if ((ret = apply_filter(LIST_GET(name_list, i), outname)) == 1) {
/* Some pattern matched AND some filter was successful */
if (StructuredIndex && (-1 == region_create(outname))) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
continue;
}
if (((fd = my_open(outname, O_RDONLY)) == -1) ) { /* error: shouldn't have returned 1! */
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
if (StructuredIndex) region_destroy(); /* cannot happen! */
unlink(outname);
continue;
}
unlinkname = outname;
goto index_file1;
}
else if (ret == 2) {
/* Some pattern matched but no filter was successful */
if (filetype(LIST_GET(name_list, i), 0, NULL, NULL)) { /* try to index input file if it satisfies filetype */
remove_filename(i, -1);
unlink(outname);
continue;
}
unlinkname = outname;
}
if (StructuredIndex && (-1 == region_create(LIST_GET(name_list, i)))) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
continue;
}
if (((fd = my_open(LIST_GET(name_list, i), O_RDONLY, 0)) == -1) ) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
if (StructuredIndex) region_destroy(); /* cannot happen! */
if (unlinkname != NULL) unlink(unlinkname);
continue;
}
index_file1:
#ifdef SW_DEBUG
if (AddToIndex || FastIndex) printf("adding words of %s in %d\n", LIST_GET(name_list,i), i);
printf("%s\n", LIST_GET(name_list, i));
#endif
/* my_stat(LIST_GET(name_list, i), &stbuf); Chris Dalton */
fstat(fd, &stbuf);
#ifdef SW_DEBUG
printf("filesize: %d\n", stbuf.st_size);
#endif
#ifdef UDI_DEBUG
printf("%s ", LIST_GET(name_list, i));
printf("size: %d ", stbuf.st_size);
#endif
/* buffer always points to a BLOCK_SIZE block of allocated memory */
buffer = buffer_begin;
residue = 0;
if (RecordLevelIndex) {
if (!StoreByteOffset) NextICurrentFileOffset = ICurrentFileOffset = 1;
else NextICurrentFileOffset = ICurrentFileOffset = 0;
}
for (offset = 0; offset < stbuf.st_size; offset += BLOCK_SIZE) {
offset -= residue;
if (!RecordLevelIndex) NextICurrentFileOffset = ICurrentFileOffset = offset;
toread = offset + BLOCK_SIZE >= stbuf.st_size ? stbuf.st_size - offset : BLOCK_SIZE;
lseek(fd, offset, SEEK_SET);
bx= buffer;
num_read = 0;
while ((toread > 0) && ((num_read = read(fd, bx, toread)) < toread)) {
if (num_read <= 0) {
buffer = bx;
fprintf(stderr, "read error on file %s at offset %d\n", LIST_GET(name_list, i), offset);
goto break_break1; /* C doesn't have break; break; */
}
bx += num_read;
toread -= num_read;
}
if (num_read >= toread) {
bx += num_read;
toread -= num_read;
}
buffer_end = bx;
residue = 0;
if (buffer_end == buffer_begin + BLOCK_SIZE) {
if (RecordLevelIndex) {
buffer_end = backward_delimiter(buffer_end /* NOT bx */, buffer, rdelim, rdelim_len, 0);
}
else {
while ((INDEXABLE(*(buffer_end-1))) && (buffer_end > buffer_begin + MAX_WORD_SIZE)) buffer_end --;
}
residue = buffer_begin + BLOCK_SIZE - buffer_end;
/* if (residue > 0) printf("residue = %d in %s at %d\n", residue, LIST_GET(name_list, i), offset); */
}
if (RecordLevelIndex) {
next_record = forward_delimiter(buffer, buffer_end, rdelim, rdelim_len, 0);
}
bx = buffer;
PrintedLongWordWarning = 0;
while ((buffer=(unsigned char *) getword(LIST_GET(name_list, i), word, buffer, buffer_end, &attribute, &next_record)) < buffer_end) {
if (RecordLevelIndex) {
if (buffer >= next_record) {
next_record = forward_delimiter(buffer, buffer_end, rdelim, rdelim_len, 0);
if (StoreByteOffset) ICurrentFileOffset += next_record - buffer;
else ICurrentFileOffset ++;
}
}
/* printf("%s\n", word); */
if(word[0] == '\0') continue;
if(icount - hash_icount >= I_THRESHOLD) {
#if BG_DEBUG
fprintf(LOGFILE, "reached I_THRESHOLD at %d\n", icount - hash_icount);
#endif /*BG_DEBUG*/
traverse1();
init_hash_table();
hash_icount = icount;
}
insert_h(word, i, attribute);
}
if (word[0] != '\0') {
/* printf("%s\n", word); */
if(icount - hash_icount >= I_THRESHOLD) {
#if BG_DEBUG
fprintf(LOGFILE, "reached I_THRESHOLD at %d\n", icount - hash_icount);
#endif /*BG_DEBUG*/
traverse1();
init_hash_table();
hash_icount = icount;
}
insert_h(word, i, attribute);
}
if (RecordLevelIndex) {
if (buffer >= next_record) {
/* next_record = forward_delimiter(buffer, buffer_end, rdelim, rdelim_len, 0); */
ICurrentFileOffset ++;
}
}
buffer = buffer_begin;
next_record = buffer;
}
break_break1:
close(fd);
if (unlinkname != NULL) unlink(unlinkname);
#ifdef UDI_DEBUG
printf("add to index: %d\n",icount-save_icount);
#endif
if ((MAXWORDSPERFILE > 0) && (icount-save_icount > MAXWORDSPERFILE)) {
fprintf(MESSAGEFILE, "%d words are contributed by %s\n",
icount-save_icount, LIST_GET(name_list, i));
AddedMaxWordsMessage = ON;
}
if (IndexNumber && NUMERICWORDPERCENT && (numeric_icount * 100 > (icount - save_icount) * NUMERICWORDPERCENT) && (icount - save_icount > MIN_WORDS)) {
fprintf(MESSAGEFILE, "NUMBERS occur in %d%% of %d words contributed by %s\n", (numeric_icount * 100)/(icount - save_icount), icount - save_icount, LIST_GET(name_list, i));
AddedMixedWordsMessage = ON;
}
numeric_icount=0;
save_icount=icount;
if (StructuredIndex) region_destroy();
}
traverse1();
init_hash_table();
hash_icount = icount;
my_free(buffer_begin, BLOCK_SIZE + 10);
return;
}
for(pn=1; pn < part_num; pn++) /* partition # 0 is not accessed */
{
if (pn == '\n') continue; /* There cannot be a partition # '\n' or 0: see partition.c */
for(i=p_table[pn]; i<p_table[pn+1]; i++) {
unlinkname = NULL;
if ((disable_list != NULL) && (i<old_file_num) && (disable_list[block2index(i)] & mask_int[i%(8*sizeof(int))])) continue;
if (LIST_GET(name_list, i) == NULL) continue;
if (BuildDictionaryExisting) {
if (((fd = my_open(LIST_GET(name_list, i), O_RDONLY, 0)) == -1) ) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
continue;
}
if (!CompressAfterBuild) unlinkname = LIST_GET(name_list, i); /* not needed anymore */
goto index_file2;
}
if ((ret = tuncompress_file(LIST_GET(name_list, i), outname, TC_EASYSEARCH | TC_OVERWRITE | TC_NOPROMPT)) > 0) { /* do not remove old .TZ file */
if (StructuredIndex && (-1 == region_create(outname))) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
continue;
}
if (((fd = my_open(outname, O_RDONLY, 0)) == -1) ) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
if (StructuredIndex) region_destroy(); /* cannot happen! */
unlink(outname);
continue;
}
if (BuildDictionary && CompressAfterBuild) strcpy(LIST_GET(name_list, i), outname); /* name of clear file will be smaller, so enough space */
else unlinkname = outname;
goto index_file2;
}
/* Try to apply the filter */
sprintf(outname, "%s/.glimpse_apply.%d", INDEX_DIR, pid);
if ((ret = apply_filter(LIST_GET(name_list, i), outname)) == 1) {
/* Some pattern matched AND some filter was successful */
if (StructuredIndex && (-1 == region_create(outname))) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
continue;
}
if (((fd = my_open(outname, O_RDONLY)) == -1) ) { /* error: shouldn't have returned 1! */
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
if (StructuredIndex) region_destroy(); /* cannot happen! */
unlink(outname);
continue;
}
unlinkname = outname;
goto index_file2;
}
else if (ret == 2) {
/* Some pattern matched but no filter was successful */
if (filetype(LIST_GET(name_list, i), 0, NULL, NULL)) { /* try to index input file if it satisfies filetype */
remove_filename(i, -1);
unlink(outname);
continue;
}
unlinkname = outname;
}
if (StructuredIndex && (-1 == region_create(LIST_GET(name_list, i)))) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
continue;
}
if (((fd = my_open(LIST_GET(name_list, i), O_RDONLY)) == -1) ) {
fprintf(stderr, "permission denied or non-existent file: %s\n", LIST_GET(name_list, i));
remove_filename(i, -1);
if (StructuredIndex) region_destroy(); /* cannot happen! */
if (unlinkname != NULL) unlink(unlinkname);
continue;
}
index_file2:
#ifdef SW_DEBUG
if (AddToIndex || FastIndex) printf("adding words of %s in %d\n", LIST_GET(name_list, i), pn);
printf("%s\n", LIST_GET(name_list, i));
#endif
/* my_stat(LIST_GET(name_list, i), &stbuf); Chris Dalton */
fstat(fd, &stbuf);
#ifdef SW_DEBUG
printf("filesize: %d\n", stbuf.st_size);
#endif
#ifdef UDI_DEBUG
printf("%s ", LIST_GET(name_list, i));
printf("size: %d ", stbuf.st_size);
#endif
/* buffer always points to a BLOCK_SIZE block of allocated memory */
buffer = buffer_begin;
residue = 0;
if (RecordLevelIndex) {
if (!StoreByteOffset) NextICurrentFileOffset = ICurrentFileOffset = 1;
else NextICurrentFileOffset = ICurrentFileOffset = 0;
}
for (offset = 0; offset < stbuf.st_size; offset += BLOCK_SIZE) {
offset -= residue;
if (!RecordLevelIndex) NextICurrentFileOffset = ICurrentFileOffset = offset;
toread = offset + BLOCK_SIZE >= stbuf.st_size ? stbuf.st_size - offset : BLOCK_SIZE;
lseek(fd, offset, SEEK_SET);
bx= buffer;
num_read = 0;
while ((toread > 0) && ((num_read = read(fd, bx, toread)) < toread)) {
if (num_read <= 0) {
buffer = bx;
fprintf(stderr, "read error on file %s at offset %d\n", LIST_GET(name_list, i), offset);
goto break_break2; /* C doesn't have break; break; */
}
bx += num_read;
toread -= num_read;
}
if (num_read >= toread) {
bx += num_read;
toread -= num_read;
}
buffer_end = bx;
residue = 0;
if (buffer_end == buffer_begin + BLOCK_SIZE) {
if (RecordLevelIndex) {
buffer_end = backward_delimiter(buffer_end /* NOT bx */, buffer, rdelim, rdelim_len, 0);
}
else {
while ((INDEXABLE(*(buffer_end-1))) && (buffer_end > buffer_begin + MAX_WORD_SIZE)) buffer_end --;
}
residue = buffer_begin + BLOCK_SIZE - buffer_end;
/* if (residue > 0) printf("residue = %d in %s at %d\n", residue, LIST_GET(name_list, i), offset); */
}
if (RecordLevelIndex) {
next_record = forward_delimiter(buffer, buffer_end, rdelim, rdelim_len, 0);
}
bx = buffer;
PrintedLongWordWarning = 0;
while ((buffer=(unsigned char *) getword(LIST_GET(name_list, i), word, buffer, buffer_end, &attribute, &next_record)) < buffer_end) {
if (RecordLevelIndex) {
if (buffer >= next_record) {
next_record = forward_delimiter(buffer, buffer_end, rdelim, rdelim_len, 0);
if (StoreByteOffset) ICurrentFileOffset += next_record - buffer;
else ICurrentFileOffset ++;
}
}
/* printf("%s\n", word); */
if(word[0] == '\0') continue;
if(icount - hash_icount >= I_THRESHOLD) {
#if BG_DEBUG
fprintf(LOGFILE, "reached I_THRESHOLD at %d\n", icount - hash_icount);
#endif /*BG_DEBUG*/
traverse1();
init_hash_table();
hash_icount = icount;
}
insert_h(word, pn, attribute);
}
if (word[0] != '\0') {
/* printf("%s\n", word); */
if(icount - hash_icount >= I_THRESHOLD) {
#if BG_DEBUG
fprintf(LOGFILE, "reached I_THRESHOLD at %d\n", icount - hash_icount);
#endif /*BG_DEBUG*/
traverse1();
init_hash_table();
hash_icount = icount;
}
insert_h(word, pn, attribute);
}
if (RecordLevelIndex) {
if (buffer >= next_record) {
/* next_record = forward_delimiter(buffer, buffer_end, rdelim, rdelim_len, 0); */
ICurrentFileOffset ++;
}
}
buffer = buffer_begin;
next_record = buffer;
}
break_break2:
close(fd);
if (unlinkname != NULL) unlink(unlinkname);
#ifdef UDI_DEBUG
printf("add to index: %d\n",icount-save_icount);
#endif
if ((MAXWORDSPERFILE > 0) && (icount-save_icount > MAXWORDSPERFILE)) {
fprintf(MESSAGEFILE, "%d words are contributed by %s\n",
icount-save_icount, LIST_GET(name_list, i));
AddedMaxWordsMessage = ON;
}
if (IndexNumber && NUMERICWORDPERCENT && (numeric_icount * 100 > (icount - save_icount) * NUMERICWORDPERCENT) && (icount - save_icount > MIN_WORDS)) {
fprintf(MESSAGEFILE, "NUMBERS occur in %d%% of %d words contributed by %s\n", (numeric_icount * 100)/(icount - save_icount), icount - save_icount, LIST_GET(name_list, i));
AddedMixedWordsMessage = ON;
}
numeric_icount=0;
save_icount=icount;
if (StructuredIndex) region_destroy();
}
}
traverse1();
init_hash_table();
hash_icount = icount;
my_free(buffer_begin, BLOCK_SIZE + 10);
}
init_hash_table()
{
int i;
for(i=0; i<HashTableSize; i++) hash_table[i] = (struct token *)NULL;
return;
}
/* ------------------------------------------------------------------------
input: a word (a string), a hash table (each entry points to a list of
tokens. (a token is a structure containing 'word' and a pointer
to a list of indices)).
function: insert the word to appropriate position in the table.
if the inserted word is already in the data structure, then
update the list of indices corresponding to that 'word'.
otherwise create a new token.
THERE ARE NO STATE CHANGES UNLESS WE ARE SURE THAT MALLOCS WON'T FAIL: BG
---------------------------------------------------------------------------*/
void
insert_h(word, pn, attribute)
char *word;
int pn;
int attribute;
{
int hash_value=0;
struct token *tp;
struct token *tp_bak;
struct indices *iip;
int wordlen = strlen(word);
int j;
static int tried_once = 0;
/* all words with same attribute at same place in hash table */
hash_value = hashword(word, wordlen);
tp_bak = tp = hash_table[hash_value];
while(tp != NULL) {
if((strcmp(word, tp->word) == 0) && (tp->attribute == attribute)) {
insert_index(tp, pn);
tried_once = 0;
return; /* already in there */
}
tp_bak = tp;
tp = tp->next_t;
}
/* this is a new word, insert it */
if((tp = (struct token *) tokenalloc(sizeof(struct token))) == NULL) {
tp_bak = NULL;
if (tried_once == 1) {
fprintf(stderr, "not enough memory in insert_h1 at icount=%d. skipping...\n", icount);
tried_once = 0;
return; /* ignore word altogether */
}
traverse1();
init_hash_table();
tried_once = 1; /* memory allocation failed in malloc#1 */
insert_h(word, pn, attribute); /* next call can't fail here since traverse() calls *allfree() */
return;
}
if((tp->word = (char *) wordalloc(sizeof(char) * (wordlen+1))) == NULL) {
tp_bak = NULL;
if (tried_once == 2) {
fprintf(stderr, "not enough memory in insert_h2 at icount=%d. skipping...\n", icount);
tokenfree(tp, sizeof(struct token));
tried_once = 0;
return; /* ignore word altogether */
}
tokenfree(tp, sizeof(struct token));
traverse1();
init_hash_table();
tried_once = 2; /* memory allocation failed in malloc#2 */
insert_h(word, pn, attribute); /* next call can't fail here or above since traverse() calls *allfree() */
return;
}
strcpy(tp->word, word);
tp->attribute = attribute;
/* the index list has a first index */
if((iip = (struct indices *) indicesalloc(sizeof(struct indices))) == NULL) {
tp_bak = NULL;
if (tried_once == 3) {
fprintf(stderr, "not enough memory in insert_h3 at icount=%d. skipping...\n", icount);
wordfree(tp->word, wordlen + 1);
tokenfree(tp, sizeof(struct token));
tried_once = 0;
return; /* ignore word altogether */
}
wordfree(tp->word, wordlen + 1);
tokenfree(tp, sizeof(struct token));
traverse1();
init_hash_table();
tried_once = 3; /* memory allocation failed in malloc#3 */
insert_h(word, pn, attribute); /* next call can't fail here or above or above-above since traverse() calls *allfree() */
return;
}
icount++;
if (IndexNumber && NUMERICWORDPERCENT) {
int i=0;
while(word[i] != '\0') {
if (!isalpha(((unsigned char *)word)[i])) break;
i++;
}
if (word[i] != '\0') numeric_icount ++;
}
#ifdef SW_DEBUG
if((icount & 01777) == 0) printf("icount = %d\n", icount);
#endif
if (!CountWords) {
iip->index[0] = pn;
iip->offset[0] = ICurrentFileOffset;
}
for (j=1; j<INDEX_SET_SIZE; j++)
iip->index[j] = INDEX_ELEM_FREE;
/* assign both head and tail */
iip->next_i = NULL;
tp->ip = iip;
tp->lastip = iip;
if(tp_bak == NULL) hash_table[hash_value] = tp;
else tp_bak->next_t = tp;
tp->next_t = NULL;
tp->totalcount = 1;
tried_once = 0; /* now sure that there has been no memory allocation failure while inserting this word */
return;
}
/* -------------------------------------------------------------------
insert_index(): insert an index, i.e., pn, into an indices structure.
The indices structure is a linked list where the 'first' one is always
the active indices structure. When the active one is filled with 8 indices
an indicies structure is created and becomes the active one.
tp points to the token structure. so, tp->ip is always the active
indices structure.
THERE ARE NO STATE CHANGES UNLESS WE ARE SURE THAT MALLOCS WON'T FAIL: BG
------------------------------------------------------------------- */
void
insert_index(tp, pn)
struct token *tp; /* insert a index into a indices structure */
int pn;
{
struct indices *iip, *temp;
struct indices *ip = (ByteLevelIndex ? tp->lastip : tp->ip);
static int tried_once = 0;
int j;
if (CountWords) { /* I am not interested in maintaining where a word occurs: only the number of times it occurs */
ip->offset[0] ++;
return;
}
/* Check for stop-list */
if (OneFilePerBlock && !ByteLevelIndex && (file_num > MaxNum8bPartition) && (tp->totalcount > (file_num * MAX_INDEX_PERCENT / 100))) return;
if (ByteLevelIndex && (tp->totalcount > ( (((total_size>>20) > 0) && ((total_size>>20)*MAX_PER_MB < MAX_ALL_INDEX)) ? ((total_size>>20) * MAX_PER_MB) : MAX_ALL_INDEX) )) return;
if (ByteLevelIndex) {
for (j=INDEX_SET_SIZE; j>0; j--) {
if(ip->index[j-1] == INDEX_ELEM_FREE) continue;
if ((ip->index[j-1] == pn) && (ip->offset[j-1] == ICurrentFileOffset)) return; /* in identical position */
else break;
}
}
else {
for (j=INDEX_SET_SIZE; j>0; j--) {
if(ip->index[j-1] == INDEX_ELEM_FREE) continue;
if (ip->index[j-1] == pn) return; /* current word is not the first appearance in partition pn */
else break;
}
}
/* ip->index[j] is the place to insert new pn provided j < INDEX_SET_SIZE */
if(j < INDEX_SET_SIZE) {
ip->offset[j] = ICurrentFileOffset;
ip->index[j] = pn;
return;
}
if((iip = (struct indices *) indicesalloc(sizeof(struct indices)))==NULL) {
if (tried_once == 1) {
fprintf(stderr, "not enough memory in insert_index at icount=%d. skipping...\n", icount);
tried_once = 0;
return; /* ignore index altogether */
}
traverse1();
init_hash_table();
tried_once = 1; /* memory allocation failed in malloc#1 */
insert_index(tp, pn);
return;
}
icount++;
if (ByteLevelIndex) {
/* insert at the end */
tp->lastip->next_i = iip;
iip->next_i = NULL;
tp->lastip = iip;
}
else {
iip->next_i = tp->ip;
tp->ip = iip;
}
iip->offset[0] = ICurrentFileOffset;
iip->index[0] = pn;
for (j=1; j<INDEX_SET_SIZE; j++)
iip->index[j] = INDEX_ELEM_FREE;
tp->totalcount ++;
if ( (OneFilePerBlock && !ByteLevelIndex && (file_num > MaxNum8bPartition) && (tp->totalcount > (file_num * MAX_INDEX_PERCENT / 100))) ||
(ByteLevelIndex && (tp->totalcount > ( (((total_size>>20) > 0) && ((total_size>>20)*MAX_PER_MB < MAX_ALL_INDEX)) ? ((total_size>>20) * MAX_PER_MB) : MAX_ALL_INDEX) )) ) {
for (iip=tp->ip; iip != NULL; temp = iip, iip = iip->next_i, indicesfree(temp, sizeof(struct indices)));
tp->ip = NULL; /* never need to insert anything else here */
}
/*
printf("returning from insert_index()\n");
fflush(stderr);
*/
tried_once = 0;
return;
}
/* Scan the indexed "word" from an index line: see io.c/merge_splits() */
scanword(word, buffer, buffer_end, attr)
unsigned char *word, *buffer, *buffer_end;
unsigned int *attr;
{
int i = MAX_WORD_SIZE;
while ((i-- != 0) && (buffer <= buffer_end) && (*buffer != ALL_INDEX_MARK) && (*buffer != WORD_END_MARK) && (*buffer != '\n') && (*buffer != '\0'))
*word ++ = *buffer ++;
*word = '\0';
*attr = encode16b(0);
if (StructuredIndex) {
if ((*buffer == ALL_INDEX_MARK) || (*buffer == WORD_END_MARK)) {
buffer ++;
*attr = ((*buffer) << 8) | (*(buffer + 1));
}
}
}
/* Globals used in merge, and also in glimpse's main.c */
extern unsigned int *src_index_set;
extern unsigned int *dest_index_set;
extern unsigned char *src_index_buf;
extern unsigned char *dest_index_buf;
extern unsigned char *merge_index_buf;
/* merge index file f1 and f2, then put the result in index file f3 */
merge_in(f1, f2, f3)
FILE *f1, *f2, *f3;
{
int src_mark, dest_mark;
int src_num, dest_num;
int src_end_pt, dest_end_pt;
int cmp=0; /* the result of strcmp */
int bdx, bdx1, bdx2, merge_len, i, j;
int TAIL1=0;
char word1[MAX_WORD_SIZE+6]; /* used only for strcmp() */
char word2[MAX_WORD_SIZE+6]; /* used only for strcmp() */
unsigned int attr1, attr2;
int x=0, y=0, even_words = 1;
int merge_buf_size=REAL_INDEX_BUF;
/* LOOK OUT FOR: [memset, fgets, endpt-forloop, scanword] 4-tuples: invariant */
#if debug
printf("in merge_in()\n"); fflush(stdout);
#endif
memset(dest_index_buf, '\0', REAL_INDEX_BUF);
if (fgets(dest_index_buf, REAL_INDEX_BUF, f2) == NULL) dest_index_buf[0] = '\0';
else dest_index_buf[REAL_INDEX_BUF - 1] = '\0';
dest_end_pt = strlen(dest_index_buf);
scanword(word2, dest_index_buf, dest_index_buf+dest_end_pt, &attr2);
#ifdef debug
printf("word2 = %s\n", word2);
#endif
memset(src_index_buf, '\0', REAL_INDEX_BUF);
while(fgets(src_index_buf, REAL_INDEX_BUF, f1)) {
src_index_buf[REAL_INDEX_BUF - 1] = '\0';
src_end_pt = strlen(src_index_buf);
scanword(word1, src_index_buf, src_index_buf+src_end_pt, &attr1);
#ifdef debug
printf("word1 = %s\n", word1);
#endif
while (((cmp = strncmp(word1, word2, MAX_WORD_SIZE+4)) > 0) || (StructuredIndex && (cmp == 0) && (attr1 > attr2))) {
fputs(dest_index_buf, f3);
memset(dest_index_buf, '\0', dest_end_pt+2);
if(fgets(dest_index_buf, REAL_INDEX_BUF, f2) == NULL) {
dest_index_buf[REAL_INDEX_BUF - 1] = '\0';
TAIL1 = ON;
break;
}
dest_index_buf[REAL_INDEX_BUF - 1] = '\0';
dest_end_pt = strlen(dest_index_buf);
scanword(word2, dest_index_buf, dest_index_buf+dest_end_pt, &attr2);
}
if(TAIL1 == ON) break;
if ((cmp == 0) && (attr1 == attr2)) { /* we need to join the index of word1 and word2 */
#ifdef debug
printf("joining src_index_buf and dest_index_buf\n");
printf("src_index_buf = %s", src_index_buf);
printf("dest_index_buf = %s", dest_index_buf);
#endif
if (!CountWords && !ByteLevelIndex) { /* have to look for common indices and exclude them */
int oldbdx1, oldbdx2;
merge_index_buf[0] = '\0';
merge_len = 0;
oldbdx1 = bdx1 = 0;
/* src_index_buf[src_end_pt] is '\0', src_index_buf[src_end_pt-1] is '\n' */
while((bdx1 < src_end_pt) && (src_index_buf[bdx1] != WORD_END_MARK) && (src_index_buf[bdx1] != ALL_INDEX_MARK)) bdx1 ++;
if ((bdx1 > oldbdx1) && (bdx1 < src_end_pt)) { /* src_index_buf[bdx1] is the word-end-mark */
src_mark = src_index_buf[bdx1];
src_index_buf[bdx1] = '\0'; /* terminate word */
strcpy(merge_index_buf, src_index_buf); /* save the word itself */
merge_len = strlen(src_index_buf); /* merge_index_buf[merge_len] is '\0', merge_index_buf[merge_len-1] is a part of the word */
bdx1 ++; /* skip word end marker */
if (StructuredIndex) bdx1 += 2; /* skip attribute field */
}
even_words = 1;
src_num = 0;
if (OneFilePerBlock) memset((char *)src_index_set, '\0', sizeof(int)*REAL_PARTITION);
else memset((char *)src_index_set, '\0', sizeof(int) * (MAX_PARTITION + 1));
while(bdx1 < src_end_pt - 1) {
if (OneFilePerBlock) {
x = 0;
if (file_num <= MaxNum8bPartition) {
x = decode8b(src_index_buf[bdx1]);
bdx1 ++;
}
else if (file_num <= MaxNum12bPartition) {
if (even_words) {
x = ((src_index_buf[bdx1+1] & 0x0000000f) << 8) | src_index_buf[bdx1];
x = decode12b(x);
bdx1 += 2;
even_words = 0;
}
else { /* odd number of words so far */
x = ((src_index_buf[bdx1-1] & 0x000000f0) << 4) | src_index_buf[bdx1];
x = decode12b(x);
bdx1 ++;
even_words = 1;
}
}
else if (file_num <= MaxNum16bPartition) {
x = (src_index_buf[bdx1] << 8) | src_index_buf[bdx1+1];
x = decode16b(x);
bdx1 += 2;
}
else {
x = (src_index_buf[bdx1] << 16) | (src_index_buf[bdx1+1] << 8) | src_index_buf[bdx1+2];
x = decode24b(x);
bdx1 += 3;
}
src_index_set[block2index(x)] |= mask_int[x % (8*sizeof(int))];
src_num ++;
}
else src_index_set[src_num++] = src_index_buf[bdx1++];
}
oldbdx2 = bdx2 = 0;
/* dest_index_buf[dest_end_pt] is '\0', dest_index_buf[dest_end_pt-1] is '\n' */
while((bdx2 < dest_end_pt) && (dest_index_buf[bdx2] != WORD_END_MARK) && (dest_index_buf[bdx2] != ALL_INDEX_MARK)) bdx2 ++;
if ((bdx2 > oldbdx2) && (bdx2 < dest_end_pt)) { /* dest_index_buf[bdx2] is the word-end-mark */
dest_mark = dest_index_buf[bdx2];
dest_index_buf[bdx2] = '\0'; /* terminate word */
if (merge_len == 0) {
strcpy(merge_index_buf, dest_index_buf); /* save the word itself */
merge_len = strlen(merge_index_buf);
/* merge_index_buf[merge_len] is '\0', merge_index_buf[merge_len-1] is a part of the word */
}
bdx2 ++; /* skip word end marker */
if (StructuredIndex) bdx2 += 2; /* skip attribute field */
}
even_words = 1;
dest_num = 0;
if (OneFilePerBlock) memset((char *)dest_index_set, '\0', sizeof(int)*REAL_PARTITION);
else memset((char *)dest_index_set, '\0', sizeof(int) * (MAX_PARTITION + 1));
while(bdx2 < dest_end_pt - 1) {
if (OneFilePerBlock) {
x = 0;
if (file_num <= MaxNum8bPartition) {
x = decode8b(dest_index_buf[bdx2]);
bdx2 ++;
}
else if (file_num <= MaxNum12bPartition) {
if (even_words) {
x = ((dest_index_buf[bdx2+1] & 0x0000000f) << 8) | dest_index_buf[bdx2];
x = decode12b(x);
bdx2 += 2;
even_words = 0;
}
else { /* odd number of words so far */
x = ((dest_index_buf[bdx2-1] & 0x000000f0) << 4) | dest_index_buf[bdx2];
x = decode12b(x);
bdx2 ++;
even_words = 1;
}
}
else if (file_num <= MaxNum16bPartition) {
x = (dest_index_buf[bdx2] << 8) | dest_index_buf[bdx2+1];
x = decode16b(x);
bdx2 += 2;
}
else {
x = (dest_index_buf[bdx2] << 16) | (dest_index_buf[bdx2+1] << 8) | dest_index_buf[bdx2+2];
x = decode24b(x);
bdx2 += 3;
}
dest_index_set[block2index(x)] |= mask_int[x % (8*sizeof(int))];
dest_num ++;
}
else dest_index_set[dest_num++] = dest_index_buf[bdx2++];
}
even_words = 1;
/* prevent buffer overflows */
if (merge_len > 0) {
if (merge_len > merge_buf_size - 6) {
merge_buf_size += 20;
merge_index_buf = realloc(merge_index_buf, merge_buf_size);
fprintf(stderr,"Realloc + 20 new size is %d",merge_buf_size);
memset(merge_index_buf + merge_len,'\0',merge_buf_size-merge_len);
}
if(OneFilePerBlock &&
((src_mark == ALL_INDEX_MARK) ||
(dest_mark == ALL_INDEX_MARK) ||
((file_num > MaxNum8bPartition) && (src_num + dest_num > file_num*MAX_INDEX_PERCENT / 100)) )) {
merge_index_buf[merge_len++] = ALL_INDEX_MARK;
if (StructuredIndex) {
merge_index_buf[merge_len++] = (attr1 & 0xff00) >> 8;
merge_index_buf[merge_len++] = (attr1 & 0xff);
}
if (file_num <= MaxNum8bPartition)
merge_index_buf[merge_len ++] = encode8b(DONT_CONFUSE_SORT);
else if (file_num <= MaxNum12bPartition) {
merge_index_buf[merge_len ++] = (encode12b(DONT_CONFUSE_SORT) & 0x00000f00) >> 8;
merge_index_buf[merge_len ++] = encode12b(DONT_CONFUSE_SORT) & 0x000000ff;
}
else if (file_num <= MaxNum16bPartition) {
merge_index_buf[merge_len ++] = (encode16b(DONT_CONFUSE_SORT) & 0x0000ff00) >> 8;
merge_index_buf[merge_len ++] = encode16b(DONT_CONFUSE_SORT) & 0x000000ff;
}
else {
merge_index_buf[merge_len ++] = (encode24b(DONT_CONFUSE_SORT) & 0x00ff0000) >> 16;
merge_index_buf[merge_len ++] = (encode24b(DONT_CONFUSE_SORT) & 0x0000ff00) >> 8;
merge_index_buf[merge_len ++] = encode24b(DONT_CONFUSE_SORT) & 0x000000ff;
}
goto final_merge;
}
merge_index_buf[merge_len++] = WORD_END_MARK;
if (StructuredIndex) {
merge_index_buf[merge_len++] = (attr1 & 0xff00) >> 8;
merge_index_buf[merge_len++] = (attr1 & 0xff);
}
if (OneFilePerBlock) {
for (i=0; i<round(file_num, 8*sizeof(int)); i++) dest_index_set[i] |= src_index_set[i]; /* take union */
for (i=0; i<round(file_num, 8*sizeof(int)); i++) {
if (merge_len > (merge_buf_size - 3*8*sizeof(int) - 1)) {
merge_buf_size *= 2;
merge_index_buf = realloc(merge_index_buf, merge_buf_size);
fprintf(stderr,"Had to Realloc merge buffer (#2), new size is %d\n",merge_buf_size);
memset(merge_index_buf + merge_len,'\0',merge_buf_size-merge_len);
}
if (dest_index_set[i])
for (j=0; j<8*sizeof(int); j++)
if (dest_index_set[i] & mask_int[j]) {
x = i*8*sizeof(int) + j;
if (file_num <= MaxNum8bPartition) {
merge_index_buf[merge_len++] = encode8b(x);
}
else if (file_num <= MaxNum12bPartition) {
x = encode12b(x);
if (even_words) {
merge_index_buf[merge_len++] = x & 0x000000ff; /* lsb */
y = (x & 0x00000f00)>>8; /* msb */
even_words = 0;
}
else { /* odd number of words so far */
y |= (x&0x00000f00)>>4; /* msb of x into msb of y */
merge_index_buf[merge_len ++] = y;
merge_index_buf[merge_len ++] = x&0x000000ff;
even_words = 1;
}
}
else if (file_num <= MaxNum16bPartition) {
x = encode16b(x);
merge_index_buf[merge_len ++] = (x&0x0000ff00)>>8;
merge_index_buf[merge_len ++] = x&0x000000ff;
}
else {
x = encode24b(x);
merge_index_buf[merge_len ++] = (x&0x00ff0000)>>16;
merge_index_buf[merge_len ++] = (x&0x0000ff00)>>8;
merge_index_buf[merge_len ++] = x&0x000000ff;
}
}
}
if (!even_words && (file_num > MaxNum8bPartition) && (file_num <= MaxNum12bPartition))
merge_index_buf[merge_len ++] = y;
}
else { /* normal indexing */
for (i=0; i<src_num; i++) {
merge_index_buf[merge_len++] = src_index_set[i];
/* Prevent buffer overflow */
if (merge_len > (merge_buf_size - 3)) {
merge_buf_size *= 2;
merge_index_buf = realloc(merge_index_buf, merge_buf_size);
fprintf(stderr,"Had to Realloc merge buffer (#3), new size is %d\n",merge_index_buf);
memset(merge_index_buf + merge_len,'\0',merge_buf_size-merge_len);
}
}
for (j=0; j<dest_num; j++) {
for (i=0; i<src_num; i++)
if (dest_index_set[j] == src_index_set[i]) break;
if (i>=src_num) /* did not find match */
merge_index_buf[merge_len++] = dest_index_set[j];
/* Prevent buffer overflow */
if (merge_len > (merge_buf_size - 2)) {
merge_buf_size *= 2;
merge_index_buf = realloc(merge_index_buf, merge_buf_size);
fprintf(stderr,"Realloc #4, new size is %d\n",merge_index_buf);
memset(merge_index_buf + merge_len,'\0',merge_buf_size-merge_len);
}
/* Doesn't matter if dest_index_set is int-array (merge_index_buf being char array) since dest_index_set has only a char */
}
}
final_merge:
merge_index_buf[merge_len++] = '\n';
merge_index_buf[merge_len] = '\0';
fputs(merge_index_buf, f3);
/* fprintf(stderr, "%d+%d=%d ", src_end_pt, dest_end_pt, merge_len); */
} /* merge_len > 0 */
}
else if (CountWords) { /* indices are frequencies, so just merge them: OneFilPerBlock is ignored */
strcpy(merge_index_buf, src_index_buf);
bdx = strlen(merge_index_buf); /* merge_index_buf[bdx] is '\0', merge_index_buf[bdx-1] is '\n' */
if (bdx > 1) bdx--; /* now merge_index_buf[bdx] is '\n', merge_index_buf[bdx-1] is the last index */
bdx2 = 0;
/* find the first index */
if (IndexNumber) while(isalnum(dest_index_buf[bdx2])) bdx2 ++;
else while(isalpha(dest_index_buf[bdx2])) bdx2++;
/* to skip over the word-end marker of dest_index_buf (which is a blank) */
if (bdx2 > 0) bdx2 ++;
if (StructuredIndex) bdx2 += 2; /* this is a nop since CountWords and StructuredIndex don't make sense together */
if (bdx >= 1) {
merge_index_buf[bdx++] = ' '; /* blank separated fscanf-able list of integers representing counts */
}
/* append the indices of word1 to the buffer */
if (dest_index_buf[bdx2] > 0) {
while((dest_index_buf[bdx2]>0)&&(bdx2<REAL_INDEX_BUF)) merge_index_buf[bdx++] = dest_index_buf[bdx2++]; /* '\n' gets copied */
merge_index_buf[bdx] = '\0';
}
/* else, no need to copy anything */
fputs(merge_index_buf, f3);
}
else { /* indices are actual occurrences (ByteLevelIndex), so just cat them one after the other, src first since that is i2, the 1st one */
/* First put out the word */
bdx1 = 0;
while ((bdx1<src_end_pt) && (src_index_buf[bdx1] != WORD_END_MARK) && (src_index_buf[bdx1] != ALL_INDEX_MARK) && (src_index_buf[bdx1] != '\n') && (src_index_buf[bdx1] != '\0'))
putc(src_index_buf[bdx1 ++], f3);
/* Now check what end-mark we should put */
if ((bdx1 >= src_end_pt) || (src_index_buf[bdx1] == ALL_INDEX_MARK) || (src_end_pt + dest_end_pt >= MAX_SORTLINE_LEN)) {
putc(ALL_INDEX_MARK, f3);
if (StructuredIndex) {
putc((attr1&0xff00) >> 8, f3);
putc((attr1&0xff), f3);
}
putc(DONT_CONFUSE_SORT, f3);
putc('\n', f3);
}
else { /* dest can be all index mark */
bdx2 = 0;
while ((bdx2<dest_end_pt) && (dest_index_buf[bdx2] != WORD_END_MARK) && (dest_index_buf[bdx2] != ALL_INDEX_MARK) && (dest_index_buf[bdx2] != '\n') && (dest_index_buf[bdx2] != '\0')) bdx2 ++;
if ((bdx2 >= dest_end_pt) || (dest_index_buf[bdx2] == ALL_INDEX_MARK)) {
putc(ALL_INDEX_MARK, f3);
if (StructuredIndex) {
putc((attr1&0xff00) >> 8, f3);
putc((attr1&0xff), f3);
}
putc(DONT_CONFUSE_SORT, f3);
putc('\n', f3);
}
else { /* we have to put out both the lists */
putc(WORD_END_MARK, f3);
bdx1 ++; /* skip over WORD_END_MARK */
if (StructuredIndex) {
putc((attr1&0xff00) >> 8, f3);
putc((attr1&0xff), f3);
bdx1 += 2;
}
while ((bdx1 < src_end_pt) && (src_index_buf[bdx1] != '\n') && (src_index_buf[bdx1] != '\0')) putc(src_index_buf[bdx1++], f3);
fputc(encode8b(0), f3); /* instead of the '\n' after end of src_index_buf */
bdx2 ++; /* skip over WORD_END_MARK */
if (StructuredIndex) bdx2 += 2;
while ((bdx2 < dest_end_pt) && (dest_index_buf[bdx2] != '\n') && (dest_index_buf[bdx2] != '\0')) putc(dest_index_buf[bdx2++], f3);
putc('\n', f3);
}
}
}
#if debug
printf("merge_index_buf = %s", merge_index_buf);
#endif /*debug*/
memset(dest_index_buf, '\0', dest_end_pt+2);
if(fgets(dest_index_buf, REAL_INDEX_BUF, f2) == 0) {
dest_index_buf[REAL_INDEX_BUF - 1] = '\0';
TAIL1 = ON;
break;
}
dest_index_buf[REAL_INDEX_BUF - 1] = '\0';
dest_end_pt = strlen(dest_index_buf);
scanword(word2, dest_index_buf, dest_index_buf+dest_end_pt, &attr2);
}
else { /* word1 < word2, so output src_index_buf */
fputs(src_index_buf, f3);
}
memset(src_index_buf, '\0', src_end_pt+2);
}
if(TAIL1) {
if(cmp != 0) fputs(src_index_buf, f3);
memset(src_index_buf, '\0', src_end_pt+2);
while(fgets(src_index_buf, REAL_INDEX_BUF, f1)) {
src_index_buf[REAL_INDEX_BUF - 1] = '\0';
src_end_pt = strlen(src_index_buf);
fputs(src_index_buf, f3);
memset(src_index_buf, '\0', src_end_pt+2);
}
}
else { /* output the tail of f2 */
fputs(dest_index_buf, f3);
memset(dest_index_buf, '\0', dest_end_pt+2);
while(fgets(dest_index_buf, REAL_INDEX_BUF, f2)) {
dest_index_buf[REAL_INDEX_BUF - 1] = '\0';
dest_end_pt = strlen(dest_index_buf);
fputs(dest_index_buf, f3);
memset(dest_index_buf, '\0', dest_end_pt+2);
}
}
return;
}
remove_filename(fileindex, new_partition)
int fileindex, new_partition;
{
if ((fileindex < 0) || (fileindex >= MaxNum24bPartition)) return;
#if BG_DEBUG
fprintf(LOGFILE, "removing %s from index\n", LIST_GET(name_list, fileindex));
memory_usage -= (strlen(LIST_GET(name_list, fileindex)) + 2);
#endif /*BG_DEBUG*/
my_free(LIST_GET(name_list, fileindex), 0);
LIST_SUREGET(name_list, fileindex) = NULL;
if ((disable_list != NULL) && (fileindex < old_file_num)) disable_list[block2index(fileindex)] |= mask_int[fileindex % (8*sizeof(int))];
}
/* returns the set of deleted files in the struct indices format: note that by construction, the list is sorted according to fileindex */
struct indices*
get_removed_indices()
{
int i, j;
char *name;
struct indices *head = NULL, **tail, *new;
tail = &head;
for (i=0; i<file_num; i++) {
name=LIST_GET(name_list, i);
#if 0
if ((name == NULL) || (name[0] == '\0')) printf("DEL %d\n", i);
#endif
if ((name == NULL) || (name[0] == '\0')) {
if ((*tail == NULL) || ((*tail)->index[INDEX_SET_SIZE - 1] != INDEX_ELEM_FREE)) {
new = (struct indices *)my_malloc(sizeof(struct indices));
memset(new, '\0', sizeof(struct indices));
for (j=0; j<INDEX_SET_SIZE; j++) new->index[j] = INDEX_ELEM_FREE;
if (*tail != NULL) {
(*tail)->next_i = new;
tail = &(*tail)->next_i;
}
else head = new;
}
for (j=0; j<INDEX_SET_SIZE; j++) if ((*tail)->index[j] == INDEX_ELEM_FREE) break;
/* j must be < INDEX_SET_SIZE */
(*tail)->index[j] = i;
}
}
return head;
}
/* returns a -ve number if there is no newfileindex for this file (deleted from index), or the new index otherwise */
/* length_of_deletedlist = MaxNum24bPartition + 1 - get_new_index(deletedlist, MaxNum24bPartition + 1); */
get_new_index(deletedlist, oldfileindex)
struct indices *deletedlist;
int oldfileindex;
{
int j;
int reduction = 0;
struct indices *head = deletedlist;
while (head!=NULL) {
for (j=0; j<INDEX_SET_SIZE; j++) {
if (head->index[j] == INDEX_ELEM_FREE) return oldfileindex; /* crossed the limit */
else if (oldfileindex == head->index[j]) return -1; /* oldfileindex has been deleted now */
else if (oldfileindex > head->index[j]) oldfileindex --;
else /* oldfileindex < head->index[j] */ return oldfileindex; /* no more have been deleted before oldfileindex */
}
head = head->next_i;
}
return oldfileindex; /* crossed the limit */
}
delete_removed_indices(deletedlist)
struct indices *deletedlist;
{
int j;
int reduction = 0;
struct indices *head = deletedlist, *temp;
while (head!=NULL) {
temp = head;
head = head->next_i;
my_free(temp, sizeof(struct indices));
}
return reduction;
}
/* The savings in index space are not worth it if you call this function w/o OneFilePerBlock: returns new_file_num (after purging index of deleted files) */
int
purge_index()
{
int new_file_num;
int i, firsti; /* firsti used to eliminate dead words from index */
char s[MAX_LINE_LEN], es1[MAX_LINE_LEN], es2[MAX_LINE_LEN], temp_rdelim[MAX_LINE_LEN];
char s1[MAX_LINE_LEN];
int j;
unsigned char c;
FILE *i_in;
FILE *i_out;
int offset, index;
char indexnumberbuf[256];
int onefileperblock, structuredindex;
int even_words, dest_even_words;
int delim = encode8b(0);
int x, y, new_x;
int prevy, diff, oldj;
if (!OneFilePerBlock) return file_num;
#if 0
/* initialized in glimpse.c now */
if ((deletedlist = get_removed_indices()) == NULL) return file_num;
printf("into purge_index()\n");
#endif
new_file_num = file_num - ( MaxNum24bPartition + 1 - get_new_index(deletedlist, MaxNum24bPartition + 1) ); /* since encoding may change */
/* else, we have already done merge-splits and the result is in INDEX_FILE */
#if 0
printf("purging indexing from %d to %d files\n", file_num, new_file_num);
#endif
sprintf(s, "%s/%s", INDEX_DIR, INDEX_FILE);
if((i_in = fopen(s, "r")) == NULL) {
fprintf(stderr, "can't open %s for reading\n", s);
exit(2);
}
sprintf(s, "%s/.glimpse_merge.%d", INDEX_DIR, getpid());
if ((i_out = fopen(s, "w")) == NULL) {
fprintf(stderr, "cannot open for writing: %s\n", s);
exit(2);
}
/* modified the original in glimpse's main.c */
fgets(indexnumberbuf, 256, i_in);
fputs(indexnumberbuf, i_out);
fscanf(i_in, "%%%d\n", &onefileperblock);
if (ByteLevelIndex) fprintf(i_out, "%%-%d\n", new_file_num); /* #of files might have changed due to -f/-a */
else fprintf(i_out, "%%%d\n", new_file_num); /* This was the stupidest thing of all! */
if ( !fscanf(i_in, "%%%d\n", &structuredindex) ) /* New file format may not have delimiter. Fixed 10/25/99 as per mhubin. --GV */
fscanf(i_in, "%%%d%s\n", &structuredindex, temp_rdelim);
if (structuredindex <= 0) structuredindex = 0;
if (RecordLevelIndex) fprintf(i_out, "%%-2 %s\n", old_rdelim); /* robint@zedcor.com */
else fprintf(i_out, "%%%d\n", attr_num); /* attributes might have been added during last merge */
while (fgets(src_index_buf, REAL_INDEX_BUF, i_in)) {
i = 0;
j = 0;
while ((j < REAL_INDEX_BUF) && (src_index_buf[j] != WORD_END_MARK) && (src_index_buf[j] != ALL_INDEX_MARK) && (src_index_buf[j] != '\0') && (src_index_buf[j] != '\n'))
dest_index_buf[i++] = src_index_buf[j++];
if ((j >= REAL_INDEX_BUF) || (src_index_buf[j] == '\0') || (src_index_buf[j] == '\n')) continue;
/* else it is WORD_END_MARK or ALL_INDEX_MARK */
c = src_index_buf[j];
dest_index_buf[i++] = src_index_buf[j++];
if (structuredindex) { /* convert all attributes to 2B to make merge_in()s easy in build_in.c */
if (structuredindex < MaxNum8bPartition - 1) {
dest_index_buf[i++] = src_index_buf[j++];
}
else {
dest_index_buf[i++] = src_index_buf[j++];
dest_index_buf[i++] = src_index_buf[j++];
}
}
if (c == ALL_INDEX_MARK) {
dest_index_buf[i++] = DONT_CONFUSE_SORT;
dest_index_buf[i++] = '\n';
dest_index_buf[i] = '\0';
fputs(dest_index_buf, i_out);
continue;
}
/* src_index_buf[j] points to the first byte of the block numbers, dest_index_buf[i] points to the first empty byte to fill up */
firsti = i;
/*
* Main while loop: Copied from glimpse/get_index.c/get_set() with minor modifications.
*/
even_words = 1;
dest_even_words = 1;
while((j<REAL_INDEX_BUF) && (src_index_buf[j] != '\n') && (src_index_buf[j] != '\0')) {
/* First get the file name */
x = 0;
if (ByteLevelIndex) {
if (OneFilePerBlock <= MaxNum8bPartition) {
x = decode8b(src_index_buf[j]);
j ++;
}
else if (OneFilePerBlock <= MaxNum16bPartition) {
x = (src_index_buf[j] << 8) | src_index_buf[j+1];
x = decode16b(x);
j += 2;
}
else {
x = (src_index_buf[j] << 16) | (src_index_buf[j+1] << 8) | src_index_buf[j+2];
x = decode24b(x);
j += 3;
}
}
else if (OneFilePerBlock <= MaxNum8bPartition) {
x = decode8b(src_index_buf[j]);
j ++;
}
else if (OneFilePerBlock <= MaxNum12bPartition) {
if (even_words) {
x = ((src_index_buf[j+1] & 0x0000000f) << 8) | src_index_buf[j];
x = decode12b(x);
j += 2;
even_words = 0;
}
else { /* odd number of words so far */
x = ((src_index_buf[j-1] & 0x000000f0) << 4) | src_index_buf[j];
x = decode12b(x);
j ++;
even_words = 1;
}
}
else if (OneFilePerBlock <= MaxNum16bPartition) {
x = (src_index_buf[j] << 8) | src_index_buf[j+1];
x = decode16b(x);
j += 2;
}
else {
x = (src_index_buf[j] << 16) | (src_index_buf[j+1] << 8) | src_index_buf[j+2];
x = decode24b(x);
j += 3;
}
/*
* This if-decoder: Copied from glimpse/get_index.c/get_set() with minor modifications.
*/
if ((new_x = get_new_index(deletedlist, x)) >= 0) { /* since encoding may change */
if (ByteLevelIndex) {
if (new_file_num <= MaxNum8bPartition) {
x = encode8b(new_x);
dest_index_buf[i++] = (x&0x000000ff);
}
else if (new_file_num <= MaxNum16bPartition) {
x = encode16b(new_x);
dest_index_buf[i++] = ((x&0x0000ff00)>>8);
dest_index_buf[i++] = (x&0x000000ff);
}
else {
x = encode24b(new_x);
dest_index_buf[i++] = ((x&0x00ff0000)>>16);
dest_index_buf[i++] = ((x&0x0000ff00)>>8);
dest_index_buf[i++] = (x&0x000000ff);
}
} /* ByteLevelIndex */
else if (OneFilePerBlock) {
if (new_file_num <= MaxNum8bPartition) {
dest_index_buf[i++] = (encode8b(new_x));
}
else if (new_file_num <= MaxNum12bPartition) {
x = encode12b(new_x);
if (dest_even_words) {
dest_index_buf[i++] = (x & 0x000000ff); /* lsb */
y = (x & 0x00000f00)>>8; /* msb */
dest_even_words = 0;
}
else { /* odd number of words so far */
y |= (x&0x00000f00)>>4; /* msb of x into msb of y */
dest_index_buf[i++] = (y);
dest_index_buf[i++] = (x&0x000000ff);
dest_even_words = 1;
}
}
else if (file_num <= MaxNum16bPartition) {
x = encode16b(new_x);
dest_index_buf[i++] = ((x&0x0000ff00)>>8);
dest_index_buf[i++] = (x&0x000000ff);
}
else {
x = encode24b(new_x);
dest_index_buf[i++] = ((x&0x00ff0000)>>16);
dest_index_buf[i++] = ((x&0x0000ff00)>>8);
dest_index_buf[i++] = (x&0x000000ff);
}
} /* OneFilePerBlock */
}
/* else don't put anything */
prevy = 0;
if (ByteLevelIndex) {
while ((j<REAL_INDEX_BUF) && (src_index_buf[j] != '\n') && (src_index_buf[j] != '\0')) {
oldj = j;
y = decode8b(src_index_buf[j]);
if ((y & 0x000000c0) == 0) { /* one byte offset */
diff = y&0x0000003f;
y = prevy + diff;
j ++;
}
else if ((y & 0x000000c0) == 0x40) { /* two byte offset */
diff = decode8b(src_index_buf[j+1]);
y = prevy + (((y & 0x0000003f) * MaxNum8bPartition) + diff);
j += 2;
}
else if ((y & 0x000000c0) == 0x80) { /* three byte offset */
diff = decode16b((src_index_buf[j+1] << 8) | src_index_buf[j+2]);
y = prevy + (((y & 0x0000003f) * MaxNum16bPartition) + diff);
j += 3;
}
else { /* four byte offset */
diff = decode24b((src_index_buf[j+1] << 16) | (src_index_buf[j+2] << 8) | src_index_buf[j+3]);
y = prevy + (((y & 0x0000003f) * MaxNum24bPartition) + diff);
j += 4;
}
prevy = y;
if (new_x >= 0) {
memcpy(&dest_index_buf[i], &src_index_buf[oldj], j - oldj);
i += j - oldj;
}
if ((j<REAL_INDEX_BUF) && (src_index_buf[j] == delim)) { /* look at offsets corr. to a new file now */
if (new_x >= 0) {
dest_index_buf[i++] = src_index_buf[j++];
}
else j ++;
break;
}
}
/* else don't put anything */
}
}
if (!ByteLevelIndex && OneFilePerBlock && !dest_even_words && (new_file_num > MaxNum8bPartition) && (new_file_num <= MaxNum12bPartition))
dest_index_buf[i++] = (y);
if (firsti < i) {
dest_index_buf[i++] = '\n';
dest_index_buf[i] = '\0';
fputs(dest_index_buf, i_out);
}
else { /* word with no references, so ditch it: fill up junk for safety */
dest_index_buf[i] = '\n';
dest_index_buf[i+1] = '\n';
}
}
fclose(i_in);
fflush(i_out);
fclose(i_out);
#if SFS_COMPAT
sprintf(s, "%s/.glimpse_merge.%d", INDEX_DIR, getpid());
sprintf(s1, "%s/%s", INDEX_DIR, INDEX_FILE);
rename(s, s1);
#else
sprintf(s, "exec %s '%s/.glimpse_merge.%d' '%s/%s'", SYSTEM_MV, escapesinglequote(INDEX_DIR, es1), getpid(), escapesinglequote(INDEX_DIR, es2), INDEX_FILE);
system(s);
#endif
return new_file_num;
}
/* This does not effect global variables other than disable_list: used only in incremental indexing */
initialize_disable_list(files)
int files;
{
int _FILEMASK_SIZE = ((files + 1)/(8*sizeof(int)) + 4);
int _REAL_PARTITION = (_FILEMASK_SIZE + 4);
if (disable_list == NULL) disable_list = (unsigned int *)my_malloc(sizeof(int)*_REAL_PARTITION);
memset(disable_list, '\0', sizeof(int) * _REAL_PARTITION); /* nothing is disabled initially */
}
|