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


#define QDBM_INTERNAL  1

#include "odeum.h"
#include "myconf.h"

#define OD_NAMEMAX     256               /* max size of a database name */
#define OD_DIRMODE     00755             /* permission of a creating directory */
#define OD_PATHBUFSIZ  1024              /* size of a path buffer */
#define OD_NUMBUFSIZ   32                /* size of a buffer for a number */
#define OD_MAPPBNUM    127               /* bucket size of a petit map handle */
#define OD_DOCSNAME    "docs"            /* name of the database for documents */
#define OD_INDEXNAME   "index"           /* name of the database for inverted index */
#define OD_RDOCSNAME   "rdocs"           /* name of the database for reverse dictionary */
#define OD_DOCSBNUM    2039              /* initial bucket number of document database */
#define OD_DOCSDNUM    17                /* division number of document database */
#define OD_DOCSALIGN   -4                /* alignment of document database */
#define OD_DOCSFBP     32                /* size of free block pool of document database */
#define OD_INDEXBNUM   32749             /* initial bucket number of inverted index */
#define OD_INDEXDNUM   7                 /* division number of inverted index */
#define OD_INDEXALIGN  -2                /* alignment of inverted index */
#define OD_INDEXFBP    32                /* size of free block pool of inverted index */
#define OD_RDOCSLRM    81                /* records in a leaf node of reverse dictionary */
#define OD_RDOCSNIM    192               /* records in a non-leaf node of reverse dictionary */
#define OD_RDOCSLCN    128               /* number of leaf cache of reverse dictionary */
#define OD_RDOCSNCN    32                /* number of non-leaf cache of reverse dictionary */
#define OD_CACHEBNUM   262139            /* number of buckets for dirty buffers */
#define OD_CACHESIZ    8388608           /* max bytes to use memory for dirty buffers */
#define OD_CFLIVERAT   0.8               /* ratio of usable cache region */
#define OD_CFBEGSIZ    2048              /* beginning size of flushing frequent words */
#define OD_CFENDSIZ    64                /* lower limit of flushing frequent words */
#define OD_CFRFRAT     0.2               /* ratio of flushing rare words a time */
#define OD_OTCBBUFSIZ  1024              /* size of a buffer for call back functions */
#define OD_OTPERWORDS  10000             /* frequency of call back in merging index */
#define OD_OTPERDOCS   1000              /* frequency of call back in merging docs */
#define OD_MDBRATIO    2.5               /* ratio of bucket number and document number */
#define OD_MIBRATIO    1.5               /* ratio of bucket number and word number */
#define OD_MIARATIO    0.75              /* ratio of alignment to the first words */
#define OD_MIWUNIT     32                /* writing unit of merging inverted index */
#define OD_DMAXEXPR    "dmax"            /* key of max number of the document ID */
#define OD_DNUMEXPR    "dnum"            /* key of number of the documents */
#define OD_URIEXPR     "1"               /* map key of URI */
#define OD_ATTRSEXPR   "2"               /* map key of attributes */
#define OD_NWORDSEXPR  "3"               /* map key of normal words */
#define OD_AWORDSEXPR  "4"               /* map key of as-is words */
#define OD_WTOPRATE    0.1               /* ratio of top words */
#define OD_WTOPBONUS   5000              /* bonus points of top words */
#define OD_KEYCRATIO   1.75              /* ratio of number to max of keyword candidates */
#define OD_WOCCRPOINT  10000             /* points per occurence */
#define OD_SPACECHARS  "\t\n\v\f\r "     /* space characters */
#define OD_DELIMCHARS  "!\"#$%&'()*/<=>?[\\]^`{|}~"  /* delimiter characters */
#define OD_GLUECHARS   "+,-.:;@"         /* glueing characters */
#define OD_MAXWORDLEN  48                /* max length of a word */

typedef struct {                         /* type of structure for word counting */
  const char *word;                      /* pointer to the word */
  int num;                               /* frequency of the word */
} ODWORD;

enum {                                   /* enumeration for events binded to each character */
  OD_EVWORD,                             /* word */
  OD_EVSPACE,                            /* space */
  OD_EVDELIM,                            /* delimiter */
  OD_EVGLUE                              /* glue */
};


/* private global variables */
int odindexbnum = OD_INDEXBNUM;
int odindexdnum = OD_INDEXDNUM;
int odcachebnum = OD_CACHEBNUM;
int odcachesiz = OD_CACHESIZ;
void (*odotcb)(const char *, ODEUM *, const char *) = NULL;


/* private function prototypes */
static ODEUM *odopendb(const char *name, int omode, int docsbnum, int indexbnum,
                       const char *fname);
static int odcacheflush(ODEUM *odeum, const char *fname);
static int odcacheflushfreq(ODEUM *odeum, const char *fname, int min);
static int odcacheflushrare(ODEUM *odeum, const char *fname, double ratio);
static int odsortindex(ODEUM *odeum, const char *fname);
static int odsortcompare(const void *a, const void *b);
static int odpurgeindex(ODEUM *odeum, const char *fname);
static CBMAP *odpairsmap(const ODPAIR *pairs, int num);
static int odwordcompare(const void *a, const void *b);
static int odmatchoperator(ODEUM *odeum, CBLIST *tokens);
static ODPAIR *odparsesubexpr(ODEUM *odeum, CBLIST *tokens, CBLIST *nwords, int *np,
                              CBLIST *errors);
static ODPAIR *odparseexpr(ODEUM *odeum, CBLIST *tokens, CBLIST *nwords, int *np,
                           CBLIST *errors);
static void odfixtokens(ODEUM *odeum, CBLIST *tokens);
static void odcleannormalized(ODEUM *odeum, CBLIST *nwords);



/*************************************************************************************************
 * public objects
 *************************************************************************************************/


/* Get a database handle. */
ODEUM *odopen(const char *name, int omode){
  assert(name);
  return odopendb(name, omode, OD_DOCSBNUM, odindexbnum, "odopen");
}


/* Close a database handle. */
int odclose(ODEUM *odeum){
  char numbuf[OD_NUMBUFSIZ];
  int err;
  assert(odeum);
  err = FALSE;
  if(odotcb) odotcb("odclose", odeum, "closing the connection");
  if(odeum->wmode){
    if(odotcb) odotcb("odclose", odeum, "writing meta information");
    sprintf(numbuf, "%d", odeum->dmax);
    if(!vlput(odeum->rdocsdb, OD_DMAXEXPR, sizeof(OD_DMAXEXPR), numbuf, -1, VL_DOVER)) err = TRUE;
    sprintf(numbuf, "%d", odeum->dnum);
    if(!vlput(odeum->rdocsdb, OD_DNUMEXPR, sizeof(OD_DNUMEXPR), numbuf, -1, VL_DOVER)) err = TRUE;
    if(!odcacheflushfreq(odeum, "odclose", OD_CFENDSIZ)) err = TRUE;
    if(!odcacheflushrare(odeum, "odclose", OD_CFRFRAT)) err = TRUE;
    if(!odcacheflush(odeum, "odclose")) err = TRUE;
    if(!odsortindex(odeum, "odclose")) err = TRUE;
    cbmapclose(odeum->cachemap);
    cbmapclose(odeum->sortmap);
  }
  if(!vlclose(odeum->rdocsdb)) err = TRUE;
  if(!crclose(odeum->indexdb)) err = TRUE;
  if(!crclose(odeum->docsdb)) err = TRUE;
  free(odeum->name);
  free(odeum);
  return err ? FALSE : TRUE;
}


/* Store a document. */
int odput(ODEUM *odeum, ODDOC *doc, int wmax, int over){
  char *tmp, *zbuf;
  const char *word, *ctmp;
  int i, docid, tsiz, wsiz, wnum, tmax, num, zsiz;
  double ival;
  ODPAIR pair;
  CBMAP *map;
  CBLIST *tlist;
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return FALSE;
  }
  if(!odeum->wmode){
    dpecodeset(DP_EMODE, __FILE__, __LINE__);
    return FALSE;
  }
  if((tmp = vlget(odeum->rdocsdb, doc->uri, -1, &tsiz)) != NULL){
    if(!over){
      free(tmp);
      dpecodeset(DP_EKEEP, __FILE__, __LINE__);
      return FALSE;
    }
    if(tsiz != sizeof(int) || !odoutbyid(odeum, *(int *)tmp)){
      free(tmp);
      dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
      odeum->fatal = TRUE;
      return FALSE;
    }
    free(tmp);
  }
  odeum->dmax++;
  odeum->dnum++;
  docid = odeum->dmax;
  map = cbmapopen();
  cbmapput(map, OD_URIEXPR, sizeof(OD_URIEXPR), doc->uri, -1, TRUE);
  tmp = cbmapdump(doc->attrs, &tsiz);
  cbmapput(map, OD_ATTRSEXPR, sizeof(OD_ATTRSEXPR), tmp, tsiz, TRUE);
  free(tmp);
  if(wmax < 0 || wmax > cblistnum(doc->nwords)) wmax = cblistnum(doc->nwords);
  tlist = cblistopen();
  for(i = 0; i < wmax; i++){
    ctmp = cblistval(doc->nwords, i, &wsiz);
    cblistpush(tlist, ctmp, wsiz);
  }
  tmp = cblistdump(tlist, &tsiz);
  cbmapput(map, OD_NWORDSEXPR, sizeof(OD_NWORDSEXPR), tmp, tsiz, TRUE);
  free(tmp);
  cblistclose(tlist);
  tlist = cblistopen();
  for(i = 0; i < wmax; i++){
    ctmp = cblistval(doc->awords, i, &wsiz);
    if(strcmp(ctmp, cblistval(doc->nwords, i, NULL))){
      cblistpush(tlist, ctmp, wsiz);
    } else {
      cblistpush(tlist, "\0", 1);
    }
  }
  tmp = cblistdump(tlist, &tsiz);
  cbmapput(map, OD_AWORDSEXPR, sizeof(OD_AWORDSEXPR), tmp, tsiz, TRUE);
  free(tmp);
  cblistclose(tlist);
  tmp = cbmapdump(map, &tsiz);
  cbmapclose(map);
  if(_qdbm_deflate){
    if(!(zbuf = _qdbm_deflate(tmp, tsiz, &zsiz, _QDBM_ZMRAW))){
      free(tmp);
      dpecodeset(DP_EMISC, __FILE__, __LINE__);
      odeum->fatal = TRUE;
      return FALSE;
    }
    free(tmp);
    tmp = zbuf;
    tsiz = zsiz;
  }
  if(!crput(odeum->docsdb, (char *)&docid, sizeof(int), tmp, tsiz, CR_DKEEP)){
    free(tmp);
    if(dpecode == DP_EKEEP) dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
    odeum->fatal = TRUE;
    return FALSE;
  }
  free(tmp);
  if(!vlput(odeum->rdocsdb, doc->uri, -1, (char *)&docid, sizeof(int), VL_DOVER)){
    odeum->fatal = TRUE;
    return FALSE;
  }
  map = cbmapopen();
  wnum = cblistnum(doc->nwords);
  tmax = (int)(wnum * OD_WTOPRATE);
  for(i = 0; i < wnum; i++){
    word = cblistval(doc->nwords, i, &wsiz);
    if(wsiz < 1) continue;
    if((ctmp = cbmapget(map, word, wsiz, NULL)) != NULL){
      num = *(int *)ctmp + OD_WOCCRPOINT;
    } else {
      num = i <= tmax ? OD_WTOPBONUS + OD_WOCCRPOINT : OD_WOCCRPOINT;
    }
    cbmapput(map, word, wsiz, (char *)&num, sizeof(int), TRUE);
  }
  ival = odlogarithm(wnum);
  ival = (ival * ival * ival) / 8.0;
  if(ival < 8.0) ival = 8.0;
  cbmapiterinit(map);
  while((word = cbmapiternext(map, &wsiz)) != NULL){
    pair.id = docid;
    pair.score = (int)(*(int *)cbmapget(map, word, wsiz, NULL) / ival);
    cbmapputcat(odeum->cachemap, word, wsiz, (char *)&pair, sizeof(pair));
    cbmapmove(odeum->cachemap, word, wsiz, FALSE);
    odeum->cacheasiz += sizeof(pair);
    cbmapput(odeum->sortmap, word, wsiz, "", 0, FALSE);
  }
  cbmapclose(map);
  if(odeum->cacheasiz > odcachesiz){
    for(i = OD_CFBEGSIZ; odeum->cacheasiz > odcachesiz * OD_CFLIVERAT && i >= OD_CFENDSIZ;
        i /= 2){
      if(!odcacheflushfreq(odeum, "odput", i)) return FALSE;
    }
    while(odeum->cacheasiz > odcachesiz * OD_CFLIVERAT){
      if(!odcacheflushrare(odeum, "odput", OD_CFRFRAT)) return FALSE;
    }
  }
  doc->id = docid;
  odeum->ldid = docid;
  return TRUE;
}


/* Delete a document by a URL. */
int odout(ODEUM *odeum, const char *uri){
  char *tmp;
  int tsiz, docid;
  assert(odeum && uri);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return FALSE;
  }
  if(!odeum->wmode){
    dpecodeset(DP_EMODE, __FILE__, __LINE__);
    return FALSE;
  }
  if(!(tmp = vlget(odeum->rdocsdb, uri, -1, &tsiz))){
    if(dpecode != DP_ENOITEM) odeum->fatal = TRUE;
    return FALSE;
  }
  if(tsiz != sizeof(int)){
    free(tmp);
    dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
    odeum->fatal = TRUE;
    return FALSE;
  }
  docid = *(int *)tmp;
  free(tmp);
  return odoutbyid(odeum, docid);
}


/* Delete a document specified by an ID number. */
int odoutbyid(ODEUM *odeum, int id){
  char *tmp, *zbuf;
  const char *uritmp;
  int tsiz, uritsiz, zsiz;
  CBMAP *map;
  assert(odeum && id > 0);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return FALSE;
  }
  if(!odeum->wmode){
    dpecodeset(DP_EMODE, __FILE__, __LINE__);
    return FALSE;
  }
  if(!(tmp = crget(odeum->docsdb, (char *)&id, sizeof(int), 0, -1, &tsiz))){
    if(dpecode != DP_ENOITEM) odeum->fatal = TRUE;
    return FALSE;
  }
  if(_qdbm_inflate){
    if(!(zbuf = _qdbm_inflate(tmp, tsiz, &zsiz, _QDBM_ZMRAW))){
      free(tmp);
      dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
      odeum->fatal = TRUE;
      return FALSE;
    }
    free(tmp);
    tmp = zbuf;
    tsiz = zsiz;
  }
  map = cbmapload(tmp, tsiz);
  free(tmp);
  uritmp = cbmapget(map, OD_URIEXPR, sizeof(OD_URIEXPR), &uritsiz);
  if(!uritmp || !vlout(odeum->rdocsdb, uritmp, uritsiz)){
    cbmapclose(map);
    dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
    odeum->fatal = TRUE;
    return FALSE;
  }
  cbmapclose(map);
  if(!crout(odeum->docsdb, (char *)&id, sizeof(int))){
    odeum->fatal = TRUE;
    return FALSE;
  }
  odeum->dnum--;
  return TRUE;
}


/* Retrieve a document by a URI. */
ODDOC *odget(ODEUM *odeum, const char *uri){
  char *tmp;
  int tsiz, docid;
  assert(odeum && uri);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return NULL;
  }
  if(!(tmp = vlget(odeum->rdocsdb, uri, -1, &tsiz))){
    if(dpecode != DP_ENOITEM) odeum->fatal = TRUE;
    return NULL;
  }
  if(tsiz != sizeof(int)){
    free(tmp);
    dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
    odeum->fatal = TRUE;
    return NULL;
  }
  docid = *(int *)tmp;
  free(tmp);
  return odgetbyid(odeum, docid);
}


/* Retrieve a document by an ID number. */
ODDOC *odgetbyid(ODEUM *odeum, int id){
  char *tmp, *zbuf;
  const char *uritmp, *attrstmp, *nwordstmp, *awordstmp, *asis, *normal;
  int i, tsiz, uritsiz, attrstsiz, nwordstsiz, awordstsiz, zsiz, asiz, nsiz;
  ODDOC *doc;
  CBMAP *map;
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return NULL;
  }
  if(id < 1){
    dpecodeset(DP_ENOITEM, __FILE__, __LINE__);
    return NULL;
  }
  if(!(tmp = crget(odeum->docsdb, (char *)&id, sizeof(int), 0, -1, &tsiz))){
    if(dpecode != DP_ENOITEM) odeum->fatal = TRUE;
    return NULL;
  }
  if(_qdbm_inflate){
    if(!(zbuf = _qdbm_inflate(tmp, tsiz, &zsiz, _QDBM_ZMRAW))){
      free(tmp);
      dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
      odeum->fatal = TRUE;
      return NULL;
    }
    free(tmp);
    tmp = zbuf;
    tsiz = zsiz;
  }
  map = cbmapload(tmp, tsiz);
  free(tmp);
  uritmp = cbmapget(map, OD_URIEXPR, sizeof(OD_URIEXPR), &uritsiz);
  attrstmp = cbmapget(map, OD_ATTRSEXPR, sizeof(OD_ATTRSEXPR), &attrstsiz);
  nwordstmp = cbmapget(map, OD_NWORDSEXPR, sizeof(OD_NWORDSEXPR), &nwordstsiz);
  awordstmp = cbmapget(map, OD_AWORDSEXPR, sizeof(OD_AWORDSEXPR), &awordstsiz);
  if(!uritmp || !attrstmp || !nwordstmp || !awordstmp){
    cbmapclose(map);
    dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
    odeum->fatal = TRUE;
    return NULL;
  }
  doc = cbmalloc(sizeof(ODDOC));
  doc->id = id;
  doc->uri = cbmemdup(uritmp, uritsiz);
  doc->attrs = cbmapload(attrstmp, attrstsiz);
  doc->nwords = cblistload(nwordstmp, nwordstsiz);
  doc->awords = cblistload(awordstmp, awordstsiz);
  cbmapclose(map);
  for(i = 0; i < cblistnum(doc->awords); i++){
    asis = cblistval(doc->awords, i, &asiz);
    if(asiz == 1 && asis[0] == '\0'){
      normal = cblistval(doc->nwords, i, &nsiz);
      cblistover(doc->awords, i, normal, nsiz);
    }
  }
  return doc;
}


/* Retrieve the ID of the document specified by a URI. */
int odgetidbyuri(ODEUM *odeum, const char *uri){
  char *tmp;
  int tsiz, docid;
  assert(odeum && uri);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return -1;
  }
  if(!(tmp = vlget(odeum->rdocsdb, uri, -1, &tsiz))){
    if(dpecode != DP_ENOITEM) odeum->fatal = TRUE;
    return -1;
  }
  if(tsiz != sizeof(int)){
    free(tmp);
    dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
    odeum->fatal = TRUE;
    return -1;
  }
  docid = *(int *)tmp;
  free(tmp);
  return docid;
}


/* Check whether the document specified by an ID number exists. */
int odcheck(ODEUM *odeum, int id){
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return FALSE;
  }
  if(id < 1){
    dpecodeset(DP_ENOITEM, __FILE__, __LINE__);
    return FALSE;
  }
  return crvsiz(odeum->docsdb, (char *)&id, sizeof(int)) != -1;
}


/* Search the inverted index for documents including a word. */
ODPAIR *odsearch(ODEUM *odeum, const char *word, int max, int *np){
  char *tmp;
  int tsiz;
  assert(odeum && word && np);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return NULL;
  }
  if(odeum->wmode && cbmaprnum(odeum->sortmap) > 0 &&
     (!odcacheflush(odeum, "odsearch") || !odsortindex(odeum, "odsearch"))){
    odeum->fatal = TRUE;
    return NULL;
  }
  max = max < 0 ? -1 : max * sizeof(ODPAIR);
  if(!(tmp = crget(odeum->indexdb, word, -1, 0, max, &tsiz))){
    if(dpecode != DP_ENOITEM){
      odeum->fatal = TRUE;
      return NULL;
    }
    *np = 0;
    return cbmalloc(1);
  }
  *np = tsiz / sizeof(ODPAIR);
  return (ODPAIR *)tmp;
}


/* Get the number of documents including a word. */
int odsearchdnum(ODEUM *odeum, const char *word){
  int rv;
  assert(odeum && word);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return -1;
  }
  rv = crvsiz(odeum->indexdb, word, -1);
  return rv < 0 ? -1 : rv / sizeof(ODPAIR);
}


/* Initialize the iterator of a database handle. */
int oditerinit(ODEUM *odeum){
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return FALSE;
  }
  return criterinit(odeum->docsdb);
}


/* Get the next key of the iterator. */
ODDOC *oditernext(ODEUM *odeum){
  char *tmp;
  int tsiz, docsid;
  ODDOC *doc;
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return NULL;
  }
  doc = NULL;
  while(TRUE){
    if(!(tmp = criternext(odeum->docsdb, &tsiz))){
      if(dpecode != DP_ENOITEM) odeum->fatal = TRUE;
      return NULL;
    }
    if(tsiz != sizeof(int)){
      free(tmp);
      dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
      odeum->fatal = TRUE;
      return NULL;
    }
    docsid = *(int *)tmp;
    free(tmp);
    if((doc = odgetbyid(odeum, docsid)) != NULL) break;
    if(dpecode != DP_ENOITEM){
      odeum->fatal = TRUE;
      return NULL;
    }
  }
  return doc;
}


/* Synchronize updating contents with the files and the devices. */
int odsync(ODEUM *odeum){
  char numbuf[OD_NUMBUFSIZ];
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return FALSE;
  }
  if(!odeum->wmode){
    dpecodeset(DP_EMODE, __FILE__, __LINE__);
    return FALSE;
  }
  if(odotcb) odotcb("odsync", odeum, "writing meta information");
  sprintf(numbuf, "%d", odeum->dmax);
  if(!vlput(odeum->rdocsdb, OD_DMAXEXPR, sizeof(OD_DMAXEXPR), numbuf, -1, VL_DOVER)){
    odeum->fatal = TRUE;
    return FALSE;
  }
  sprintf(numbuf, "%d", odeum->dnum);
  if(!vlput(odeum->rdocsdb, OD_DNUMEXPR, sizeof(OD_DNUMEXPR), numbuf, -1, VL_DOVER)){
    odeum->fatal = TRUE;
    return FALSE;
  }
  if(!odcacheflush(odeum, "odsync")){
    odeum->fatal = TRUE;
    return FALSE;
  }
  if(!odsortindex(odeum, "odsync")){
    odeum->fatal = TRUE;
    return FALSE;
  }
  if(odotcb) odotcb("odsync", odeum, "synchronizing the document database");
  if(!crsync(odeum->docsdb)){
    odeum->fatal = TRUE;
    return FALSE;
  }
  if(odotcb) odotcb("odsync", odeum, "synchronizing the inverted index");
  if(!crsync(odeum->indexdb)){
    odeum->fatal = TRUE;
    return FALSE;
  }
  if(odotcb) odotcb("odsync", odeum, "synchronizing the reverse dictionary");
  if(!vlsync(odeum->rdocsdb)){
    odeum->fatal = TRUE;
    return FALSE;
  }
  return TRUE;
}


/* Optimize a database. */
int odoptimize(ODEUM *odeum){
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return FALSE;
  }
  if(!odeum->wmode){
    dpecodeset(DP_EMODE, __FILE__, __LINE__);
    return FALSE;
  }
  if(!odcacheflush(odeum, "odoptimize")){
    odeum->fatal = TRUE;
    return FALSE;
  }
  if(odeum->ldid < 1 || odeum->ldid != odeum->dnum){
    if(!odpurgeindex(odeum, "odoptimize")){
      odeum->fatal = TRUE;
      return FALSE;
    }
  }
  if(odeum->ldid > 0){
    if(!odsortindex(odeum, "odoptimize")){
      odeum->fatal = TRUE;
      return FALSE;
    }
  }
  if(odotcb) odotcb("odoptimize", odeum, "optimizing the document database");
  if(!croptimize(odeum->docsdb, -1)){
    odeum->fatal = TRUE;
    return FALSE;
  }
  if(odotcb) odotcb("odoptimize", odeum, "optimizing the inverted index");
  if(!croptimize(odeum->indexdb, -1)){
    odeum->fatal = TRUE;
    return FALSE;
  }
  if(odotcb) odotcb("odoptimize", odeum, "optimizing the reverse dictionary");
  if(!vloptimize(odeum->rdocsdb)){
    odeum->fatal = TRUE;
    return FALSE;
  }
  return TRUE;
}


/* Get the name of a database. */
char *odname(ODEUM *odeum){
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return NULL;
  }
  return cbmemdup(odeum->name, -1);
}


/* Get the total size of database files. */
double odfsiz(ODEUM *odeum){
  double fsiz, rv;
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return -1;
  }
  fsiz = 0;
  if((rv = crfsizd(odeum->docsdb)) < 0) return -1.0;
  fsiz += rv;
  if((rv = crfsizd(odeum->indexdb)) < 0) return -1.0;
  fsiz += rv;
  if((rv = vlfsiz(odeum->rdocsdb)) == -1) return -1.0;
  fsiz += rv;
  return fsiz;
}


/* Get the total number of the elements of the bucket arrays for the inverted index. */
int odbnum(ODEUM *odeum){
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return -1;
  }
  return crbnum(odeum->indexdb);
}


/* Get the total number of the used elements of the bucket arrays in the inverted index. */
int odbusenum(ODEUM *odeum){
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return -1;
  }
  return crbusenum(odeum->indexdb);
}


/* Get the number of the documents stored in a database. */
int oddnum(ODEUM *odeum){
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return -1;
  }
  return odeum->dnum;
}


/* Get the number of the words stored in a database. */
int odwnum(ODEUM *odeum){
  assert(odeum);
  if(odeum->fatal){
    dpecodeset(DP_EFATAL, __FILE__, __LINE__);
    return -1;
  }
  return crrnum(odeum->indexdb);
}


/* Check whether a database handle is a writer or not. */
int odwritable(ODEUM *odeum){
  assert(odeum);
  return odeum->wmode;
}


/* Check whether a database has a fatal error or not. */
int odfatalerror(ODEUM *odeum){
  assert(odeum);
  return odeum->fatal;
}


/* Get the inode number of a database directory. */
int odinode(ODEUM *odeum){
  assert(odeum);
  return odeum->inode;
}


/* Get the last modified time of a database. */
time_t odmtime(ODEUM *odeum){
  assert(odeum);
  return crmtime(odeum->indexdb);
}


/* Merge plural database directories. */
int odmerge(const char *name, const CBLIST *elemnames){
  ODEUM *odeum, **elems;
  CURIA *curia, *ecuria;
  VILLA *villa, *evilla;
  ODPAIR *pairs;
  char *word, *kbuf, *vbuf, *dbuf, otmsg[OD_OTCBBUFSIZ];
  char *wpunit[OD_MIWUNIT], *vpunit[OD_MIWUNIT];
  int i, j, k, num, dnum, wnum, dbnum, ibnum, tnum, wsunit[OD_MIWUNIT], vsunit[OD_MIWUNIT];
  int err, *bases, sum, max, wsiz, ksiz, vsiz, uend, unum, pnum, align, id, nid, dsiz;
  assert(name && elemnames);
  num = cblistnum(elemnames);
  elems = cbmalloc(num * sizeof(ODEUM *) + 1);
  dnum = 0;
  wnum = 0;
  for(i = 0; i < num; i++){
    if(!(elems[i] = odopen(cblistval(elemnames, i, NULL), OD_OREADER))){
      for(i -= 1; i >= 0; i--){
        odclose(elems[i]);
      }
      free(elems);
      return FALSE;
    }
    dnum += oddnum(elems[i]);
    wnum += odwnum(elems[i]);
  }
  dbnum = (int)(dnum * OD_MDBRATIO / OD_DOCSDNUM);
  ibnum = (int)(wnum * OD_MIBRATIO / odindexdnum);
  if(!(odeum = odopendb(name, OD_OWRITER | OD_OCREAT | OD_OTRUNC, dbnum, ibnum, "odmerge"))){
    for(i = 0; i < num; i++){
      odclose(elems[i]);
    }
    free(elems);
    return FALSE;
  }
  err = FALSE;
  if(odotcb) odotcb("odmerge", odeum, "calculating the base ID numbers");
  bases = cbmalloc(num * sizeof(int) + 1);
  sum = 0;
  for(i = 0; i < num; i++){
    ecuria = elems[i]->docsdb;
    max = 0;
    if(!criterinit(ecuria) && dpecode != DP_ENOITEM) err = TRUE;
    while((kbuf = criternext(ecuria, &ksiz)) != NULL){
      if(ksiz == sizeof(int)){
        if(*(int *)kbuf > max) max = *(int *)kbuf;
      }
      free(kbuf);
    }
    bases[i] = sum;
    sum += max;
  }
  curia = odeum->indexdb;
  for(i = 0; i < num; i++){
    if(odotcb){
      sprintf(otmsg, "merging the inverted index (%d/%d)", i + 1, num);
      odotcb("odmerge", odeum, otmsg);
    }
    ecuria = elems[i]->indexdb;
    tnum = 0;
    uend = FALSE;
    if(!criterinit(ecuria) && dpecode != DP_ENOITEM) err = TRUE;
    while(!uend){
      for(unum = 0; unum < OD_MIWUNIT; unum++){
        if(!(word = criternext(ecuria, &wsiz))){
          uend = TRUE;
          break;
        }
        if(!(vbuf = crget(ecuria, word, wsiz, 0, -1, &vsiz))){
          err = TRUE;
          free(word);
          break;
        }
        wpunit[unum] = word;
        wsunit[unum] = wsiz;
        vpunit[unum] = vbuf;
        vsunit[unum] = vsiz;
      }
      for(j = 0; j < unum; j++){
        word = wpunit[j];
        wsiz = wsunit[j];
        vbuf = vpunit[j];
        vsiz = vsunit[j];
        pairs = (ODPAIR *)vbuf;
        pnum = vsiz / sizeof(ODPAIR);
        for(k = 0; k < pnum; k++){
          pairs[k].id += bases[i];
        }
        align = (int)(i < num - 1 ? vsiz * (num - i) * OD_MIARATIO : OD_INDEXALIGN);
        if(!crsetalign(curia, align)) err = TRUE;
        if(!crput(curia, word, wsiz, vbuf, vsiz, CR_DCAT)) err = TRUE;
        free(vbuf);
        free(word);
        if(odotcb && (tnum + 1) % OD_OTPERWORDS == 0){
          sprintf(otmsg, "... (%d/%d)", tnum + 1, crrnum(ecuria));
          odotcb("odmerge", odeum, otmsg);
        }
        tnum++;
      }
    }
  }
  if(odotcb) odotcb("odmerge", odeum, "sorting the inverted index");
  tnum = 0;
  if(!criterinit(curia) && dpecode != DP_ENOITEM) err = TRUE;
  while((word = criternext(curia, &wsiz)) != NULL){
    if((vbuf = crget(curia, word, wsiz, 0, -1, &vsiz)) != NULL){
      if(vsiz > sizeof(ODPAIR)){
        pairs = (ODPAIR *)vbuf;
        pnum = vsiz / sizeof(ODPAIR);
        qsort(pairs, pnum, sizeof(ODPAIR), odsortcompare);
        if(!crput(curia, word, wsiz, vbuf, vsiz, CR_DOVER)) err = TRUE;
      }
      free(vbuf);
    }
    free(word);
    if(odotcb && (tnum + 1) % OD_OTPERWORDS == 0){
      sprintf(otmsg, "... (%d/%d)", tnum + 1, crrnum(curia));
      odotcb("odmerge", odeum, otmsg);
    }
    tnum++;
  }
  if(odotcb) odotcb("odmerge", odeum, "synchronizing the inverted index");
  if(!crsync(curia)) err = TRUE;
  dnum = 0;
  curia = odeum->docsdb;
  villa = odeum->rdocsdb;
  for(i = 0; i < num; i++){
    if(odotcb){
      sprintf(otmsg, "merging the document database (%d/%d)", i + 1, num);
      odotcb("odmerge", odeum, otmsg);
    }
    evilla = elems[i]->rdocsdb;
    ecuria = elems[i]->docsdb;
    tnum = 0;
    if(!vlcurfirst(evilla) && dpecode != DP_ENOITEM) err = TRUE;
    while(TRUE){
      if(!(kbuf = vlcurkey(evilla, &ksiz))) break;
      if((ksiz == sizeof(OD_DMAXEXPR) && !memcmp(kbuf, OD_DMAXEXPR, ksiz)) ||
         (ksiz == sizeof(OD_DNUMEXPR) && !memcmp(kbuf, OD_DNUMEXPR, ksiz))){
        free(kbuf);
        if(!vlcurnext(evilla)) break;
        continue;
      }
      if(!(vbuf = vlcurval(evilla, &vsiz))){
        free(kbuf);
        if(!vlcurnext(evilla)) break;
        continue;
      }
      if(vsiz != sizeof(int)){
        free(vbuf);
        free(kbuf);
        if(!vlcurnext(evilla)) break;
        continue;
      }
      id =  *(int *)vbuf;
      nid = id + bases[i];
      if(vlput(villa, kbuf, ksiz, (char *)&nid, sizeof(int), VL_DKEEP)){
        if((dbuf = crget(ecuria, (char *)&id, sizeof(int), 0, -1, &dsiz)) != NULL){
          if(crput(curia, (char *)&nid, sizeof(int), dbuf, dsiz, CR_DKEEP)){
            dnum++;
          } else {
            err = TRUE;
          }
          free(dbuf);
        } else {
          err = TRUE;
        }
      } else if(dpecode != DP_EKEEP){
        err = TRUE;
      }
      free(vbuf);
      free(kbuf);
      odeum->dnum++;
      if(odotcb && (tnum + 1) % OD_OTPERDOCS == 0){
        sprintf(otmsg, "... (%d/%d)", tnum + 1, crrnum(ecuria));
        odotcb("odmerge", odeum, otmsg);
      }
      tnum++;
      if(!vlcurnext(evilla)) break;
    }
  }
  odeum->dnum = dnum;
  odeum->dmax = dnum;
  free(bases);
  if(odotcb) odotcb("odmerge", odeum, "synchronizing the document index");
  if(!crsync(curia)) err = TRUE;
  if(!odclose(odeum)) err = TRUE;
  for(i = 0; i < num; i++){
    if(!odclose(elems[i])) err = TRUE;
  }
  free(elems);
  return err ? FALSE : TRUE;
}


/* Remove a database directory. */
int odremove(const char *name){
  char docsname[OD_PATHBUFSIZ], indexname[OD_PATHBUFSIZ], rdocsname[OD_PATHBUFSIZ];
  char path[OD_PATHBUFSIZ];
  const char *file;
  struct stat sbuf;
  CBLIST *list;
  int i;
  assert(name);
  sprintf(docsname, "%s%c%s", name, MYPATHCHR, OD_DOCSNAME);
  sprintf(indexname, "%s%c%s", name, MYPATHCHR, OD_INDEXNAME);
  sprintf(rdocsname, "%s%c%s", name, MYPATHCHR, OD_RDOCSNAME);
  if(lstat(name, &sbuf) == -1){
    dpecodeset(DP_ESTAT, __FILE__, __LINE__);
    return FALSE;
  }
  if(lstat(docsname, &sbuf) != -1 && !crremove(docsname)) return FALSE;
  if(lstat(indexname, &sbuf) != -1 && !crremove(indexname)) return FALSE;
  if(lstat(rdocsname, &sbuf) != -1 && !vlremove(rdocsname)) return FALSE;
  if((list = cbdirlist(name)) != NULL){
    for(i = 0; i < cblistnum(list); i++){
      file = cblistval(list, i, NULL);
      if(!strcmp(file, MYCDIRSTR) || !strcmp(file, MYPDIRSTR)) continue;
      sprintf(path, "%s%c%s", name, MYPATHCHR, file);
      if(lstat(path, &sbuf) == -1) continue;
      if(S_ISDIR(sbuf.st_mode)){
        if(!crremove(path)) return FALSE;
      } else {
        if(!dpremove(path)) return FALSE;
      }
    }
    cblistclose(list);
  }
  if(rmdir(name) == -1){
    dpecodeset(DP_ERMDIR, __FILE__, __LINE__);
    return FALSE;
  }
  return TRUE;
}


/* Get a document handle. */
ODDOC *oddocopen(const char *uri){
  ODDOC *doc;
  assert(uri);
  doc = cbmalloc(sizeof(ODDOC));
  doc->id = -1;
  doc->uri = cbmemdup(uri, -1);
  doc->attrs = cbmapopenex(OD_MAPPBNUM);
  doc->nwords = cblistopen();
  doc->awords = cblistopen();
  return doc;
}


/* Close a document handle. */
void oddocclose(ODDOC *doc){
  assert(doc);
  cblistclose(doc->awords);
  cblistclose(doc->nwords);
  cbmapclose(doc->attrs);
  free(doc->uri);
  free(doc);
}


/* Add an attribute to a document. */
void oddocaddattr(ODDOC *doc, const char *name, const char *value){
  assert(doc && name && value);
  cbmapput(doc->attrs, name, -1, value, -1, TRUE);
}


/* Add a word to a document. */
void oddocaddword(ODDOC *doc, const char *normal, const char *asis){
  assert(doc && normal && asis);
  cblistpush(doc->nwords, normal, -1);
  cblistpush(doc->awords, asis, -1);
}


/* Get the ID number of a document. */
int oddocid(const ODDOC *doc){
  assert(doc);
  return doc->id;
}


/* Get the URI of a document. */
const char *oddocuri(const ODDOC *doc){
  assert(doc);
  return doc->uri;
}


/* Get the value of an attribute of a document. */
const char *oddocgetattr(const ODDOC *doc, const char *name){
  assert(doc && name);
  return cbmapget(doc->attrs, name, -1, NULL);
}


/* Get the list handle contains words in normalized form of a document. */
const CBLIST *oddocnwords(const ODDOC *doc){
  assert(doc);
  return doc->nwords;
}


/* Get the list handle contains words in appearance form of a document. */
const CBLIST *oddocawords(const ODDOC *doc){
  assert(doc);
  return doc->awords;
}


/* Get the map handle contains keywords in normalized form and their scores. */
CBMAP *oddocscores(const ODDOC *doc, int max, ODEUM *odeum){
  const CBLIST *nwords;
  CBMAP *map, *kwmap;
  const char *word, *ctmp;
  char numbuf[OD_NUMBUFSIZ];
  ODWORD *owords;
  int i, wsiz, wnum, hnum, mnum, nbsiz;
  double ival;
  assert(doc && max >= 0);
  map = cbmapopen();
  nwords = oddocnwords(doc);
  for(i = 0; i < cblistnum(nwords); i++){
    word = cblistval(nwords, i, &wsiz);
    if(wsiz < 1) continue;
    if((ctmp = cbmapget(map, word, wsiz, NULL)) != NULL){
      wnum = *(int *)ctmp + OD_WOCCRPOINT;
    } else {
      wnum = OD_WOCCRPOINT;
    }
    cbmapput(map, word, wsiz, (char *)&wnum, sizeof(int), TRUE);
  }
  mnum = cbmaprnum(map);
  owords = cbmalloc(mnum * sizeof(ODWORD) + 1);
  cbmapiterinit(map);
  for(i = 0; (word = cbmapiternext(map, &wsiz)) != NULL; i++){
    owords[i].word = word;
    owords[i].num = *(int *)cbmapget(map, word, wsiz, NULL);
  }
  qsort(owords, mnum, sizeof(ODWORD), odwordcompare);
  if(odeum){
    if(mnum > max * OD_KEYCRATIO) mnum = (int)(max * OD_KEYCRATIO);
    for(i = 0; i < mnum; i++){
      if((hnum = odsearchdnum(odeum, owords[i].word)) < 0) hnum = 0;
      ival = odlogarithm(hnum);
      ival = (ival * ival * ival) / 8.0;
      if(ival < 8.0) ival = 8.0;
      owords[i].num = (int)(owords[i].num / ival);
    }
    qsort(owords, mnum, sizeof(ODWORD), odwordcompare);
  }
  if(mnum > max) mnum = max;
  kwmap = cbmapopenex(OD_MAPPBNUM);
  for(i = 0; i < mnum; i++){
    nbsiz = sprintf(numbuf, "%d", owords[i].num);
    cbmapput(kwmap, owords[i].word, -1, numbuf, nbsiz, TRUE);
  }
  free(owords);
  cbmapclose(map);
  return kwmap;
}


/* Break a text into words in appearance form. */
CBLIST *odbreaktext(const char *text){
  const char *word;
  CBLIST *elems, *words;
  int i, j, dif, wsiz, pv, delim;
  assert(text);
  words = cblistopen();
  elems = cbsplit(text, -1, OD_SPACECHARS);
  for(i = 0; i < cblistnum(elems); i++){
    word = cblistval(elems, i, &wsiz);
    delim = FALSE;
    j = 0;
    pv = 0;
    while(TRUE){
      dif = j - pv;
      if(j >= wsiz){
        if(dif > 0 && dif <= OD_MAXWORDLEN) cblistpush(words, word + pv, j - pv);
        break;
      }
      if(delim){
        if(!strchr(OD_DELIMCHARS, word[j])){
          if(dif > 0 && dif <= OD_MAXWORDLEN) cblistpush(words, word + pv, j - pv);
          pv = j;
          delim = FALSE;
        }
      } else {
        if(strchr(OD_DELIMCHARS, word[j])){
          if(dif > 0 && dif <= OD_MAXWORDLEN) cblistpush(words, word + pv, j - pv);
          pv = j;
          delim = TRUE;
        }
      }
      j++;
    }
  }
  cblistclose(elems);
  return words;
}


/* Make the normalized form of a word. */
char *odnormalizeword(const char *asis){
  char *nword;
  int i;
  assert(asis);
  for(i = 0; asis[i] != '\0'; i++){
    if(!strchr(OD_DELIMCHARS, asis[i])) break;
  }
  if(asis[i] == '\0') return cbmemdup("", 0);
  nword = cbmemdup(asis, -1);
  for(i = 0; nword[i] != '\0'; i++){
    if(nword[i] >= 'A' && nword[i] <= 'Z') nword[i] += 'a' - 'A';
  }
  while(i >= 0){
    if(strchr(OD_GLUECHARS, nword[i])){
      nword[i] = '\0';
    } else {
      break;
    }
    i--;
  }
  return nword;
}


/* Get the common elements of two sets of documents. */
ODPAIR *odpairsand(ODPAIR *apairs, int anum, ODPAIR *bpairs, int bnum, int *np){
  CBMAP *map;
  ODPAIR *result;
  const char *tmp;
  int i, rnum;
  assert(apairs && anum >= 0 && bpairs && bnum >= 0);
  map = odpairsmap(bpairs, bnum);
  result = cbmalloc(sizeof(ODPAIR) * anum + 1);
  rnum = 0;
  for(i = 0; i < anum; i++){
    if(!(tmp = cbmapget(map, (char *)&(apairs[i].id), sizeof(int), NULL))) continue;
    result[rnum].id = apairs[i].id;
    result[rnum].score = apairs[i].score + *(int *)tmp;
    rnum++;
  }
  cbmapclose(map);
  qsort(result, rnum, sizeof(ODPAIR), odsortcompare);
  *np = rnum;
  return result;
}


/* Get the sum of elements of two sets of documents. */
ODPAIR *odpairsor(ODPAIR *apairs, int anum, ODPAIR *bpairs, int bnum, int *np){
  CBMAP *map;
  ODPAIR *result;
  const char *tmp;
  int i, score, rnum;
  assert(apairs && anum >= 0 && bpairs && bnum >= 0);
  map = odpairsmap(bpairs, bnum);
  for(i = 0; i < anum; i++){
    score = 0;
    if((tmp = cbmapget(map, (char *)&(apairs[i].id), sizeof(int), NULL)) != NULL)
      score = *(int *)tmp;
    score += apairs[i].score;
    cbmapput(map, (char *)&(apairs[i].id), sizeof(int),
             (char *)&score, sizeof(int), TRUE);
  }
  rnum = cbmaprnum(map);
  result = cbmalloc(rnum * sizeof(ODPAIR) + 1);
  cbmapiterinit(map);
  for(i = 0; (tmp = cbmapiternext(map, NULL)) != NULL; i++){
    result[i].id = *(int *)tmp;
    result[i].score = *(int *)cbmapget(map, tmp, sizeof(int), NULL);
  }
  cbmapclose(map);
  qsort(result, rnum, sizeof(ODPAIR), odsortcompare);
  *np = rnum;
  return result;
}


/* Get the difference set of documents. */
ODPAIR *odpairsnotand(ODPAIR *apairs, int anum, ODPAIR *bpairs, int bnum, int *np){
  CBMAP *map;
  ODPAIR *result;
  const char *tmp;
  int i, rnum;
  assert(apairs && anum >= 0 && bpairs && bnum >= 0);
  map = odpairsmap(bpairs, bnum);
  result = cbmalloc(sizeof(ODPAIR) * anum + 1);
  rnum = 0;
  for(i = 0; i < anum; i++){
    if((tmp = cbmapget(map, (char *)&(apairs[i].id), sizeof(int), NULL)) != NULL) continue;
    result[rnum].id = apairs[i].id;
    result[rnum].score = apairs[i].score;
    rnum++;
  }
  cbmapclose(map);
  qsort(result, rnum, sizeof(ODPAIR), odsortcompare);
  *np = rnum;
  return result;
}


/* Sort a set of documents in descending order of scores. */
void odpairssort(ODPAIR *pairs, int pnum){
  assert(pairs && pnum >= 0);
  qsort(pairs, pnum, sizeof(ODPAIR), odsortcompare);
}


/* Get the natural logarithm of a number. */
double odlogarithm(double x){
  int i;
  if(x <= 1.0) return 0.0;
  x = x * x * x * x * x * x * x * x * x * x;
  for(i = 0; x > 1.0; i++){
    x /= 2.718281828459;
  }
  return (double)i / 10.0;
}


/* Get the cosine of the angle of two vectors. */
double odvectorcosine(const int *avec, const int *bvec, int vnum){
  double rv;
  assert(avec && bvec && vnum >= 0);
  rv = odvecinnerproduct(avec, bvec, vnum) /
    ((odvecabsolute(avec, vnum) * odvecabsolute(bvec, vnum)));
  return rv > 0.0 ? rv : 0.0;
}


/* Set the global tuning parameters. */
void odsettuning(int ibnum, int idnum, int cbnum, int csiz){
  if(ibnum > 0) odindexbnum = ibnum;
  if(idnum > 0) odindexdnum = idnum;
  if(cbnum > 0) odcachebnum = dpprimenum(cbnum);
  if(csiz > 0) odcachesiz = csiz;
}


/* Break a text into words and store appearance forms and normalized form into lists. */
void odanalyzetext(ODEUM *odeum, const char *text, CBLIST *awords, CBLIST *nwords){
  char aword[OD_MAXWORDLEN+1], *wp;
  int lev, wsiz;
  assert(odeum && text && awords);
  lev = OD_EVSPACE;
  wsiz = 0;
  for(; *text != '\0'; text++){
    switch(odeum->statechars[*(unsigned char *)text]){
    case OD_EVWORD:
      if(wsiz > 0 && lev == OD_EVDELIM){
        cblistpush(awords, aword, wsiz);
        if(nwords) cblistpush(nwords, "", 0);
        wsiz = 0;
      }
      if(wsiz <= OD_MAXWORDLEN){
        aword[wsiz++] = *text;
      }
      lev = OD_EVWORD;
      break;
    case OD_EVGLUE:
      if(wsiz > 0 && lev == OD_EVDELIM){
        cblistpush(awords, aword, wsiz);
        if(nwords) cblistpush(nwords, "", 0);
        wsiz = 0;
      }
      if(wsiz <= OD_MAXWORDLEN){
        aword[wsiz++] = *text;
      }
      lev = OD_EVGLUE;
      break;
    case OD_EVDELIM:
      if(wsiz > 0 && lev != OD_EVDELIM){
        cblistpush(awords, aword, wsiz);
        if(nwords){
          wp = aword;
          aword[wsiz] = '\0';
          while(*wp != '\0'){
            if(*wp >= 'A' && *wp <= 'Z') *wp += 'a' - 'A';
            wp++;
          }
          wp--;
          while(wp >= aword && odeum->statechars[*(unsigned char *)wp] == OD_EVGLUE){
            wsiz--;
            wp--;
          }
          cblistpush(nwords, aword, wsiz);
        }
        wsiz = 0;
      }
      if(wsiz <= OD_MAXWORDLEN){
        aword[wsiz++] = *text;
      }
      lev = OD_EVDELIM;
      break;
    default:
      if(wsiz > 0){
        cblistpush(awords, aword, wsiz);
        if(nwords){
          if(lev == OD_EVDELIM){
            cblistpush(nwords, "", 0);
          } else {
            wp = aword;
            aword[wsiz] = '\0';
            while(*wp != '\0'){
              if(*wp >= 'A' && *wp <= 'Z') *wp += 'a' - 'A';
              wp++;
            }
            wp--;
            while(wp >= aword && odeum->statechars[*(unsigned char *)wp] == OD_EVGLUE){
              wsiz--;
              wp--;
            }
            cblistpush(nwords, aword, wsiz);
          }
        }
        wsiz = 0;
      }
      lev = OD_EVSPACE;
      break;
    }
  }
  if(wsiz > 0){
    cblistpush(awords, aword, wsiz);
    if(nwords){
      if(lev == OD_EVDELIM){
        cblistpush(nwords, "", 0);
      } else {
        wp = aword;
        aword[wsiz] = '\0';
        while(*wp != '\0'){
          if(*wp >= 'A' && *wp <= 'Z') *wp += 'a' - 'A';
          wp++;
        }
        wp--;
        while(wp >= aword && odeum->statechars[*(unsigned char *)wp] == OD_EVGLUE){
          wsiz--;
          wp--;
        }
        cblistpush(nwords, aword, wsiz);
      }
    }
    wsiz = 0;
  }
}


/* Set the classes of characters used by `odanalyzetext'. */
void odsetcharclass(ODEUM *odeum, const char *spacechars, const char *delimchars,
                    const char *gluechars){
  assert(odeum && spacechars && delimchars && gluechars);
  memset(odeum->statechars, OD_EVWORD, sizeof(odeum->statechars));
  for(; *spacechars != '\0'; spacechars++){
    odeum->statechars[*(unsigned char *)spacechars] = OD_EVSPACE;
  }
  for(; *delimchars != '\0'; delimchars++){
    odeum->statechars[*(unsigned char *)delimchars] = OD_EVDELIM;
  }
  for(; *gluechars != '\0'; gluechars++){
    odeum->statechars[*(unsigned char *)gluechars] = OD_EVGLUE;
  }
}


/* Query a database using a small boolean query language. */
ODPAIR *odquery(ODEUM *odeum, const char *query, int *np, CBLIST *errors){
  CBLIST *tokens = cblistopen();
  CBLIST *nwords = cblistopen();
  ODPAIR *results = NULL;
  assert(odeum && query && np);
  odanalyzetext(odeum, query, tokens, nwords);
  odcleannormalized(odeum, nwords);
  odfixtokens(odeum, tokens);
  results = odparseexpr(odeum, tokens, nwords, np, errors);
  cblistclose(tokens);
  cblistclose(nwords);
  return results;
}



/*************************************************************************************************
 * features for experts
 *************************************************************************************************/


/* Get the internal database handle for documents. */
CURIA *odidbdocs(ODEUM *odeum){
  assert(odeum);
  return odeum->docsdb;
}


/* Get the internal database handle for the inverted index. */
CURIA *odidbindex(ODEUM *odeum){
  assert(odeum);
  return odeum->indexdb;
}


/* Get the internal database handle for the reverse dictionary. */
VILLA *odidbrdocs(ODEUM *odeum){
  assert(odeum);
  return odeum->rdocsdb;
}


/* Set the call back function called in merging. */
void odsetotcb(void (*otcb)(const char *, ODEUM *, const char *)){
  odotcb = otcb;
}


/* Get the positive one of square roots of a number. */
double odsquareroot(double x){
  double c, rv;
  if(x <= 0.0) return 0.0;
  c = x > 1.0 ? x : 1;
  do {
    rv = c;
    c = (x / c + c) * 0.5;
  } while(c < rv);
  return rv;
}


/* Get the absolute of a vector. */
double odvecabsolute(const int *vec, int vnum){
  double rv;
  int i;
  assert(vec && vnum >= 0);
  rv = 0;
  for(i = 0; i < vnum; i++){
    rv += (double)vec[i] * (double)vec[i];
  }
  return odsquareroot(rv);
}


/* Get the inner product of two vectors. */
double odvecinnerproduct(const int *avec, const int *bvec, int vnum){
  double rv;
  int i;
  assert(avec && bvec && vnum >= 0);
  rv = 0;
  for(i = 0; i < vnum; i++){
    rv += (double)avec[i] * (double)bvec[i];
  }
  return rv;
}



/*************************************************************************************************
 * private objects
 *************************************************************************************************/


/* Get a database handle.
   `name' specifies the name of a database directory.
   `omode' specifies the connection mode.
   `docsbnum` specifies the number of buckets of the document database.
   `indexbnum` specifies the number of buckets of the index database.
   `fname' specifies the name of caller function.
   The return value is the database handle or `NULL' if it is not successful. */
static ODEUM *odopendb(const char *name, int omode, int docsbnum, int indexbnum,
                       const char *fname){
  int cromode, vlomode, inode, dmax, dnum;
  char docsname[OD_PATHBUFSIZ], indexname[OD_PATHBUFSIZ], rdocsname[OD_PATHBUFSIZ], *tmp;
  struct stat sbuf;
  CURIA *docsdb, *indexdb;
  VILLA *rdocsdb;
  CBMAP *cachemap;
  CBMAP *sortmap;
  ODEUM *odeum;
  assert(name);
  if(strlen(name) > OD_NAMEMAX){
    dpecodeset(DP_EMISC, __FILE__, __LINE__);
    return NULL;
  }
  cromode = CR_OREADER;
  vlomode = VL_OREADER;
  if(omode & OD_OWRITER){
    cromode = CR_OWRITER;
    vlomode = VL_OWRITER | VL_OZCOMP | VL_OYCOMP;
    if(omode & OD_OCREAT){
      cromode |= CR_OCREAT;
      vlomode |= VL_OCREAT;
    }
    if(omode & OD_OTRUNC){
      cromode |= CR_OTRUNC;
      vlomode |= VL_OTRUNC;
    }
  }
  if(omode & OD_ONOLCK){
    cromode |= CR_ONOLCK;
    vlomode |= VL_ONOLCK;
  }
  if(omode & OD_OLCKNB){
    cromode |= CR_OLCKNB;
    vlomode |= VL_OLCKNB;
  }
  sprintf(docsname, "%s%c%s", name, MYPATHCHR, OD_DOCSNAME);
  sprintf(indexname, "%s%c%s", name, MYPATHCHR, OD_INDEXNAME);
  sprintf(rdocsname, "%s%c%s", name, MYPATHCHR, OD_RDOCSNAME);
  docsdb = NULL;
  indexdb = NULL;
  rdocsdb = NULL;
  if((omode & OD_OWRITER) && (omode & OD_OCREAT)){
    if(mkdir(name, OD_DIRMODE) == -1 && errno != EEXIST){
      dpecodeset(DP_EMKDIR, __FILE__, __LINE__);
      return NULL;
    }
  }
  if(lstat(name, &sbuf) == -1){
    dpecodeset(DP_ESTAT, __FILE__, __LINE__);
    return NULL;
  }
  inode = sbuf.st_ino;
  if(!(docsdb = cropen(docsname, cromode, docsbnum, OD_DOCSDNUM))) return NULL;
  if(!(indexdb = cropen(indexname, cromode, indexbnum, odindexdnum))){
    crclose(docsdb);
    return NULL;
  }
  if(omode & OD_OWRITER){
    if(!crsetalign(docsdb, OD_DOCSALIGN) || !crsetfbpsiz(docsdb, OD_DOCSFBP) ||
       !crsetalign(indexdb, OD_INDEXALIGN) || !crsetfbpsiz(indexdb, OD_INDEXFBP)){
      crclose(indexdb);
      crclose(docsdb);
      return NULL;
    }
  }
  if(!(rdocsdb = vlopen(rdocsname, vlomode, VL_CMPLEX))){
    crclose(indexdb);
    crclose(docsdb);
    return NULL;
  }
  vlsettuning(rdocsdb, OD_RDOCSLRM, OD_RDOCSNIM, OD_RDOCSLCN, OD_RDOCSNCN);
  if(omode & OD_OWRITER){
    cachemap = cbmapopenex(odcachebnum);
    sortmap = cbmapopenex(odcachebnum);
  } else {
    cachemap = NULL;
    sortmap = NULL;
  }
  if(vlrnum(rdocsdb) > 0){
    dmax = -1;
    dnum = -1;
    if((tmp = vlget(rdocsdb, OD_DMAXEXPR, sizeof(OD_DMAXEXPR), NULL)) != NULL){
      dmax = atoi(tmp);
      free(tmp);
    }
    if((tmp = vlget(rdocsdb, OD_DNUMEXPR, sizeof(OD_DNUMEXPR), NULL)) != NULL){
      dnum = atoi(tmp);
      free(tmp);
    }
    if(dmax < 0 || dnum < 0){
      if(sortmap) cbmapclose(sortmap);
      if(cachemap) cbmapclose(cachemap);
      vlclose(rdocsdb);
      crclose(indexdb);
      crclose(docsdb);
      dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
      return NULL;
    }
  } else {
    dmax = 0;
    dnum = 0;
  }
  odeum = cbmalloc(sizeof(ODEUM));
  odeum->name = cbmemdup(name, -1);
  odeum->wmode = omode & OD_OWRITER;
  odeum->fatal = FALSE;
  odeum->inode = inode;
  odeum->docsdb = docsdb;
  odeum->indexdb = indexdb;
  odeum->rdocsdb = rdocsdb;
  odeum->cachemap = cachemap;
  odeum->cacheasiz = 0;
  odeum->sortmap = sortmap;
  odeum->dmax = dmax;
  odeum->dnum = dnum;
  odeum->ldid = -1;
  odsetcharclass(odeum, OD_SPACECHARS, OD_DELIMCHARS, OD_GLUECHARS);
  if(odotcb) odotcb(fname, odeum, "the connection was established");
  return odeum;
}


/* Flush the cache for dirty buffer of words.
   `odeum' specifies a database handle.
   `fname' specifies the name of caller function.
   If successful, the return value is true, else, it is false. */
static int odcacheflush(ODEUM *odeum, const char *fname){
  const char *kbuf, *vbuf;
  char otmsg[OD_OTCBBUFSIZ];
  int i, rnum, ksiz, vsiz;
  assert(odeum);
  if((rnum = cbmaprnum(odeum->cachemap)) < 1) return TRUE;
  if(odotcb) odotcb(fname, odeum, "flushing caches");
  cbmapiterinit(odeum->cachemap);
  for(i = 0; (kbuf = cbmapiternext(odeum->cachemap, &ksiz)) != NULL; i++){
    vbuf = cbmapget(odeum->cachemap, kbuf, ksiz, &vsiz);
    if(!crput(odeum->indexdb, kbuf, ksiz, vbuf, vsiz, CR_DCAT)){
      odeum->fatal = TRUE;
      return FALSE;
    }
    if(odotcb && (i + 1) % OD_OTPERWORDS == 0){
      sprintf(otmsg, "... (%d/%d)", i + 1, rnum);
      odotcb(fname, odeum, otmsg);
    }
  }
  cbmapclose(odeum->cachemap);
  odeum->cachemap = cbmapopenex(odcachebnum);
  odeum->cacheasiz = 0;
  return TRUE;
}


/* Flush all frequent words in the cache for dirty buffer of words.
   `odeum' specifies a database handle.
   `fname' specifies the name of caller function.
   `min' specifies the minimum size of frequent words.
   If successful, the return value is true, else, it is false. */
static int odcacheflushfreq(ODEUM *odeum, const char *fname, int min){
  const char *kbuf, *vbuf;
  char otmsg[OD_OTCBBUFSIZ];
  int rnum, ksiz, vsiz;
  assert(odeum);
  if((rnum = cbmaprnum(odeum->cachemap)) < 1) return TRUE;
  if(odotcb){
    sprintf(otmsg, "flushing frequent words: min=%d asiz=%d rnum=%d)",
            min, odeum->cacheasiz, rnum);
    odotcb(fname, odeum, otmsg);
  }
  cbmapiterinit(odeum->cachemap);
  while((kbuf = cbmapiternext(odeum->cachemap, &ksiz)) != NULL){
    vbuf = cbmapget(odeum->cachemap, kbuf, ksiz, &vsiz);
    if(vsiz >= sizeof(ODPAIR) * min){
      if(!crput(odeum->indexdb, kbuf, ksiz, vbuf, vsiz, CR_DCAT)){
        odeum->fatal = TRUE;
        return FALSE;
      }
      cbmapout(odeum->cachemap, kbuf, ksiz);
      odeum->cacheasiz -= vsiz;
    }
  }
  if(odotcb){
    sprintf(otmsg, "... (done): min=%d asiz=%d rnum=%d)",
            min, odeum->cacheasiz, cbmaprnum(odeum->cachemap));
    odotcb(fname, odeum, otmsg);
  }
  return TRUE;
}


/* Flush the half of rare words in the cache for dirty buffer of words.
   `odeum' specifies a database handle.
   `fname' specifies the name of caller function.
   `ratio' specifies the ratio of rare words.
   If successful, the return value is true, else, it is false. */
static int odcacheflushrare(ODEUM *odeum, const char *fname, double ratio){
  const char *kbuf, *vbuf;
  char otmsg[OD_OTCBBUFSIZ];
  int i, rnum, limit, ksiz, vsiz;
  assert(odeum);
  if((rnum = cbmaprnum(odeum->cachemap)) < 1) return TRUE;
  if(odotcb){
    sprintf(otmsg, "flushing rare words: ratio=%.2f asiz=%d rnum=%d)",
            ratio, odeum->cacheasiz, rnum);
    odotcb(fname, odeum, otmsg);
  }
  cbmapiterinit(odeum->cachemap);
  limit = (int)(rnum * ratio);
  for(i = 0; i < limit && (kbuf = cbmapiternext(odeum->cachemap, &ksiz)) != NULL; i++){
    vbuf = cbmapget(odeum->cachemap, kbuf, ksiz, &vsiz);
    if(!crput(odeum->indexdb, kbuf, ksiz, vbuf, vsiz, CR_DCAT)){
      odeum->fatal = TRUE;
      return FALSE;
    }
    cbmapout(odeum->cachemap, kbuf, ksiz);
    odeum->cacheasiz -= vsiz;
  }
  if(odotcb){
    sprintf(otmsg, "... (done): ratio=%.2f asiz=%d rnum=%d)",
            ratio, odeum->cacheasiz, cbmaprnum(odeum->cachemap));
    odotcb(fname, odeum, otmsg);
  }
  return TRUE;
}


/* Sort the records of inverted index.
   `odeum' specifies a database handle.
   `fname' specifies the name of caller function.
   If successful, the return value is true, else, it is false. */
static int odsortindex(ODEUM *odeum, const char *fname){
  const char *word;
  char *tmp, otmsg[OD_OTCBBUFSIZ];
  int i, rnum, wsiz, tsiz;
  ODPAIR *pairs;
  assert(odeum);
  if((rnum = cbmaprnum(odeum->sortmap)) < 1) return TRUE;
  if(odotcb) odotcb(fname, odeum, "sorting the inverted index");
  cbmapiterinit(odeum->sortmap);
  for(i = 0; (word = cbmapiternext(odeum->sortmap, &wsiz)) != NULL; i++){
    if((tmp = crget(odeum->indexdb, word, wsiz, 0, -1, &tsiz)) != NULL){
      if(tsiz > sizeof(ODPAIR)){
        pairs = (ODPAIR *)tmp;
        qsort(pairs, tsiz / sizeof(ODPAIR), sizeof(ODPAIR), odsortcompare);
        if(!crput(odeum->indexdb, word, wsiz, tmp, tsiz, CR_DOVER)){
          free(tmp);
          return FALSE;
        }
      }
      free(tmp);
    } else if(dpecode != DP_ENOITEM){
      return FALSE;
    }
    if(odotcb && (i + 1) % OD_OTPERWORDS == 0){
      sprintf(otmsg, "... (%d/%d)", i + 1, rnum);
      odotcb(fname, odeum, otmsg);
    }
  }
  cbmapclose(odeum->sortmap);
  odeum->sortmap = cbmapopenex(odcachebnum);
  return TRUE;
}


/* Compare two pairs of structures of a search result.
   `a' specifies the pointer to the region of one pair.
   `b' specifies the pointer to the region of the other pair.
   The return value is positive if the former is big, negative if the latter is big, 0 if both
   are equivalent. */
static int odsortcompare(const void *a, const void *b){
  ODPAIR *ap, *bp;
  int rv;
  assert(a && b);
  ap = (ODPAIR *)a;
  bp = (ODPAIR *)b;
  rv = bp->score - ap->score;
  if(rv != 0) return rv;
  return ap->id - bp->id;
}


/* Purge the elements of the deleted documents from the inverted index.
   `odeum' specifies a database handle.
   `fname' specifies the name of caller function.
   If successful, the return value is true, else, it is false. */
static int odpurgeindex(ODEUM *odeum, const char *fname){
  ODPAIR *pairs;
  char *kbuf, *vbuf, otmsg[OD_OTCBBUFSIZ];
  int i, rnum, tnum, ksiz, vsiz, pnum, wi;
  assert(odeum);
  if((rnum = crrnum(odeum->indexdb)) < 1) return TRUE;
  if(odotcb) odotcb(fname, odeum, "purging dispensable regions");
  if(!criterinit(odeum->indexdb)) return FALSE;
  tnum = 0;
  while(TRUE){
    if(!(kbuf = criternext(odeum->indexdb, &ksiz))){
      if(dpecode != DP_ENOITEM) return FALSE;
      break;
    }
    if(!(vbuf = crget(odeum->indexdb, kbuf, ksiz, 0, -1, &vsiz))){
      dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
      free(kbuf);
      return FALSE;
    }
    pairs = (ODPAIR *)vbuf;
    pnum = vsiz / sizeof(ODPAIR);
    wi = 0;
    for(i = 0; i < pnum; i++){
      if(crvsiz(odeum->docsdb, (char *)&(pairs[i].id), sizeof(int)) != -1){
        pairs[wi++] = pairs[i];
      }
    }
    if(wi > 0){
      if(!crput(odeum->indexdb, kbuf, ksiz, vbuf, wi * sizeof(ODPAIR), CR_DOVER)){
        free(vbuf);
        free(kbuf);
        return FALSE;
      }
    } else {
      if(!crout(odeum->indexdb, kbuf, ksiz)){
        free(vbuf);
        free(kbuf);
        return FALSE;
      }
    }
    free(vbuf);
    free(kbuf);
    if(odotcb && (tnum + 1) % OD_OTPERWORDS == 0){
      sprintf(otmsg, "... (%d/%d)", tnum + 1, rnum);
      odotcb(fname, odeum, otmsg);
    }
    tnum++;
  }
  return TRUE;
}


/* Create a map of a document array.
   `pairs' specifies the pointer to a document array.
   `num' specifies the number of elements of the array.
   The return value is a map of the document array. */
static CBMAP *odpairsmap(const ODPAIR *pairs, int num){
  CBMAP *map;
  int i;
  assert(pairs && num >= 0);
  map = cbmapopen();
  for(i = 0; i < num; i++){
    cbmapput(map, (char *)&(pairs[i].id), sizeof(int),
             (char *)&(pairs[i].score), sizeof(int), TRUE);
  }
  return map;
}


/* compare two pairs of structures of words in a document.
   `a' specifies the pointer to the region of one word.
   `b' specifies the pointer to the region of the other word.
   The return value is positive if the former is big, negative if the latter is big, 0 if both
   are equivalent. */
static int odwordcompare(const void *a, const void *b){
  ODWORD *ap, *bp;
  int rv;
  assert(a && b);
  ap = (ODWORD *)a;
  bp = (ODWORD *)b;
  if((rv = bp->num - ap->num) != 0) return rv;
  if((rv = strlen(bp->word) - strlen(ap->word)) != 0) return rv;
  return strcmp(ap->word, bp->word);
}


/* Match an operator without taking it off the token list.
   `odeum' specifies a database handle.
   `tokens' specifies a list handle of tokens.
   The return value is whether the next token is an operator. */
static int odmatchoperator(ODEUM *odeum, CBLIST *tokens){
  const char *tk = NULL;
  int tk_len = 0;
  tk = cblistval(tokens, 0, &tk_len);
  if(tk && (tk[0] == '&' || tk[0] == '|' || tk[0] == '!')) return 1;
  return 0;
}


/* Implements the subexpr part of the grammar.
   `odeum' specifies a database handle.
   `tokens' specifies a list handle of tokens.
   `nwords' specifies a list handle of normalized words.
   `np' specifies the pointer to a variable to which the number of the elements of the return
   value is assigned.
   `errors' specifies a list handle into which error messages are stored.
   The return value is the pointer to an array of document IDs. */
static ODPAIR *odparsesubexpr(ODEUM *odeum, CBLIST *tokens, CBLIST *nwords, int *np,
                              CBLIST *errors){
  char *tk = NULL;
  int tk_len = 0;
  char *nword = NULL;  /* used to do the actual search, should match with tokens */
  ODPAIR *result = NULL;
  int result_num = 0;
  int i;
  double ival;
  if((tk = cblistshift(tokens, &tk_len)) != NULL){
    assert(tk != NULL);
    if(tk[0] == '('){
      free(tk);
      /* recurse into expr */
      result = odparseexpr(odeum, tokens, nwords, &result_num, errors);
      /* match right token RPAREN */
      tk = cblistshift(tokens, &tk_len);
      /* print an error if either we didn't get anything or we didn't get a ) */
      if(tk == NULL){
        if(errors) cblistpush(errors, "Expression ended without closing ')'", -1);
      } else if(tk[0] != ')'){
        if(errors) cblistpush(errors, "Un-balanced parenthesis.", -1);
      }
    } else if(odeum->statechars[*(unsigned char *)tk] == 0){
      /* Perform odsearch with the next norm word that isn't an operator. */
      nword = cblistshift(nwords, NULL);
      assert(nword != NULL);
      if((result = odsearch(odeum, nword, -1, &result_num)) != NULL){
        /* TF-IDF tuning */
        ival = odlogarithm(result_num);
        ival = (ival * ival) / 4.0;
        if(ival < 4.0) ival = 4.0;
        for(i = 0; i < result_num; i++){
          result[i].score = (int)(result[i].score / ival);
        }
      }
      free(nword);
    } else {
      if(errors) cblistpush(errors, "Invalid sub-expression.  Expected '(' or WORD.", -1);
      result = cbmalloc(1);
      result_num = 0;
    }
    /* done with the token */
    free(tk);
  }
  *np = result_num;
  return result;
}


/* Implements the actual recursive decent parser for the mini query language.
   `odeum' specifies a database handle.
   `tokens' specifies a list handle of tokens.
   `nwords' specifies a list handle of normalized words.
   `np' specifies the pointer to a variable to which the number of the elements of the return
   value is assigned.
   `errors' specifies a list handle into which error messages are stored.
   The return value is the pointer to an array of document IDs.
   It simply parses an initial subexpr, and then loops over as many (operator subexpr)
   sequences as it can find.  The odmatchoperator function handles injecting a default &
   between consecutive words. */
static ODPAIR *odparseexpr(ODEUM *odeum, CBLIST *tokens, CBLIST *nwords, int *np,
                           CBLIST *errors){
  ODPAIR *left = NULL;
  ODPAIR *right = NULL;
  ODPAIR *temp = NULL;
  int left_num = 0;
  int right_num = 0;
  int temp_num = 0;
  char *op = NULL;
  int op_len = 0;
  if(!(left = odparsesubexpr(odeum, tokens, nwords, &left_num, errors))) return NULL;
  /* expr ::= subexpr ( op subexpr )* */
  while(odmatchoperator(odeum, tokens)){
    op = cblistshift(tokens, &op_len);
    if(!(right = odparsesubexpr(odeum, tokens, nwords, &right_num, errors))){
      free(op);
      free(left);
      return NULL;
    }
    switch(op[0]){
    case '&':
      temp = odpairsand(left, left_num, right, right_num, &temp_num);
      break;
    case '|':
      temp = odpairsor(left, left_num, right, right_num, &temp_num);
      break;
    case '!':
      temp = odpairsnotand(left, left_num, right, right_num, &temp_num);
      break;
    default:
      if(errors) cblistpush(errors, "Invalid operator.  Expected '&', '|', or '!'.", -1);
      break;
    }
    if(temp){
      /* an operator was done so we must swap it with the left */
      free(left); left = NULL;
      left = temp;
      left_num = temp_num;
    }
    free(op);
    if(right) free(right);
  }
  *np = left_num;
  return left;
}


/* Processes the tokens in order to break them up further.
   `odeum' specifies a database handle.
   `tokens' specifies a list handle of tokens. */
static void odfixtokens(ODEUM *odeum, CBLIST *tokens){
  const char *tk = NULL;
  int tk_len = 0;
  int i = 0;
  int lastword = 0;
  for(i = 0; i < cblistnum(tokens); i++){
    tk = cblistval(tokens, i, &tk_len);
    assert(tk);
    if(tk[0] == '&' || tk[0] == '|' || tk[0] == '!' || tk[0] == '(' || tk[0] == ')'){
      lastword = 0;
      if(tk_len > 1){
        /* need to break it up for the next loop around */
        tk = cblistremove(tokens, i, &tk_len);
        cblistinsert(tokens, i, tk, 1);
        cblistinsert(tokens, i+1, tk+1, tk_len-1);
        free((char *)tk);
      }
    } else if(odeum->statechars[*(unsigned char *)tk] == 0){
      /* if the last one was a word and this is a word then we need a default & between them */
      if(lastword){
        cblistinsert(tokens, i, "&", 1);
        i++;
      }
      lastword = 1;
    }
  }
}


/* Cleans out the parts of the normalized word list that are not considered words.
   `odeum' specifies a database handle.
   `tokens' specifies a list handle of tokens. */
static void odcleannormalized(ODEUM *odeum, CBLIST *nwords){
  char *tk = NULL;
  int tk_len = 0;
  int i = 0;
  for(i = 0; i < cblistnum(nwords); i++){
    tk = (char *)cblistval(nwords, i, &tk_len);
    if(tk_len == 0 || (!odeum->statechars[*(unsigned char *)tk] == 0)){
      /* not a word so delete it */
      tk = cblistremove(nwords, i, &tk_len);
      free(tk);
      i--;
    }
  }
}



/* END OF FILE */