This file is indexed.

/usr/include/dbstl_map.h is in libdb5.3-stl-dev 5.3.28-9+deb8u1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2009, 2013 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#ifndef _DB_STL_DB_MAP_H_
#define _DB_STL_DB_MAP_H_

#include <string>

#include "dbstl_common.h"
#include "dbstl_dbc.h"
#include "dbstl_container.h"
#include "dbstl_resource_manager.h"
#include "dbstl_element_ref.h"
#include "dbstl_base_iterator.h"

START_NS(dbstl)

using std::pair;
using std::make_pair;
using std::string;
// Forward declarations, DO NOT delete the default argument values
// because class templates defintions need them. No need for _export here.
//
template <Typename T>
class _DB_STL_set_value;

template <Typename kdt, Typename ddt, Typename value_type_sub = 
    ElementRef<ddt> >
class db_map_iterator;

template <Typename kdt, Typename ddt, Typename value_type_sub = 
    ElementRef<ddt>, Typename iterator_t = 
    db_map_iterator<kdt, ddt, value_type_sub> >
class db_map;

template<Typename kdt, Typename ddt, Typename value_type_sub = 
    ElementRef<ddt>, Typename iterator_t = 
    db_map_iterator<kdt, ddt, value_type_sub> >
class db_multimap;

template <Typename kdt>
class db_set_base_iterator;

template <Typename kdt, Typename value_type_sub = ElementRef<kdt> >
class db_set_iterator;

template <Typename kdt, Typename value_type_sub = ElementRef<kdt> >
class db_set;

template <Typename kdt, Typename value_type_sub = ElementRef<kdt> >
class db_multiset;

#if NO_MEMBER_FUNCTION_PARTIAL_SPECIALIZATION
template <Typename kdt, Typename datadt, Typename value_type_sub>
void assign_second0(pair<kdt, value_type_sub>& v, const datadt& d) ;
#endif

/** \ingroup dbstl_iterators
@{
\defgroup db_map_iterators Iterator classes for db_map and db_multimap.
db_map has two iterator class templates -- db_map_base_iterator and
db_map_iterator. They are the const iterator class and iterator class for
db_map and db_multimap. db_map_iterator inherits from db_map_base_iterator.

The two classes have identical behaviors to std::map::const_iterator and
std::map::iterator respectively. Note that the common public member function
behaviors are described in the db_base_iterator section.

The differences between the two classes are that the db_map_base_iterator can 
only be used to read its referenced value, while db_map_iterator allows both 
read and write access. If your access pattern is readonly, it is strongly 
recommended that you use the const iterator because it is faster and more 
efficient.
@{
*/
//////////////////////////////////////////////////////////////////////
// db_map_base_iterator class definition
//
// This class is a const iterator class for db_map and db_multimap, it can
// be used only to read data under the iterator, can't be used to write.
//
// Iterator const-ness implementation:
//
// const iterators can not update key/data pairs, other than this, 
// they can do anything else like non-const iterators, so we define
// db_map_base_iterator to be the const iterator which can only be used
// to read its underlying key/data pair, but not updating them; We 
// derive the db_map_iterator from the base iterator to be the 
// read-write iterator. We also maintain a "readonly" property in all
// iterators so that users can specify a db_map_iterator to be
// read only. db_map_base_iterator is more efficient to read data then
// db_map_iterator, so read only accesses are strongly recommended to be 
// done using a const iterator.
//
template <Typename kdt, Typename ddt, Typename csrddt = ddt>
class _exported db_map_base_iterator : public 
    db_base_iterator<ddt>
{
protected:
	typedef db_map_base_iterator<kdt, ddt, csrddt> self;
	typedef db_base_iterator<ddt> base;
	using base::replace_current_key;
public:
	typedef kdt key_type;
	typedef ddt data_type;
	typedef pair<kdt, ddt> value_type;
	// Not used in this class, but required to satisfy 
	// db_reverse_iterator type extraction.
	typedef ptrdiff_t difference_type;
	typedef difference_type distance_type;
	typedef value_type& reference;
	typedef value_type* pointer;
	typedef value_type value_type_wrap;
	// We have to use standard iterator tags to match the parameter 
	// list of stl internal functions, we can't use our own tag 
	// classes, so we don't write tag classes in dbstl.
	//
	typedef std::bidirectional_iterator_tag iterator_category;

	////////////////////////////////////////////////////////////////////
	//
	// Begin public constructors and destructor.
	/// @name Constructors and destructor
	/// Do not create iterators directly using these constructors, but
	/// call db_map::begin or db_multimap_begin to get instances of
	/// this class.
	/// \sa db_map::begin() db_multimap::begin()
	//@{
	/// Copy constructor.
	/// \param vi The other iterator of the same type to initialize this.
	db_map_base_iterator(const self& vi)
	    : db_base_iterator<ddt>(vi)
	{
		// Lazy-dup another cursor, cursor to iterator mapping 
		// is 1 to 1.
		pcsr_ = vi.pcsr_;
		curpair_base_.first = vi.curpair_base_.first;
		curpair_base_.second = vi.curpair_base_.second;
	}

	/// Base copy constructor.
	/// \param vi Initialize from a base class iterator.
	db_map_base_iterator(const base& vi) : base(vi), 
	    pcsr_(new cursor_type(vi.get_bulk_retrieval(), 
	    vi.is_rmw(), vi.is_directdb_get()))
	{
		
	}
	
	/// Constructor.
	/// \param powner The container which creates this iterator.
	/// \param b_bulk_retrieval The bulk read buffer size. 0 means 
	/// bulk read disabled.
	/// \param rmw Whether set DB_RMW flag in underlying cursor.
	/// \param directdbget Whether do direct database get rather than
	/// using key/data values cached in the iterator whenever read.
	/// \param readonly Whether open a read only cursor. Only effective 
	/// when using Berkeley DB Concurrent Data Store.
	explicit db_map_base_iterator(db_container*powner, 
	    u_int32_t b_bulk_retrieval = 0, bool rmw = false, 
	    bool directdbget = true, bool readonly = false)
	    : db_base_iterator<ddt>(
	    powner, directdbget, readonly, b_bulk_retrieval, rmw),
	    pcsr_(new cursor_type(b_bulk_retrieval, rmw, directdbget)) 
	{ 	
	}

	/// Default constructor, dose not create the cursor for now.
	db_map_base_iterator()
	{
	}

	// Use virtual because ElementRef<> uses a db_base_iterator* pointer
	// to refer to the iterator, and also use "dead_" flag to avoid 
	// multiple calls to the same destructor by ~ElementRef<>().
	/// Destructor.
	virtual ~db_map_base_iterator() 
	{
		this->dead_ = true;
		if (pcsr_)
			pcsr_->close();	
	}
	//@}

	////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////
	// Begin functions that shift iterator position.
	//
	// Do not throw exceptions here because it is likely and normal 
	// to iterate to the "end iterator". 
	//
	/// @name Iterator increment movement functions.
	/// The two functions moves the iterator one element backward, so that
	/// the element it sits on has a bigger key. The btree/hash key
	/// comparison routine determines which key is greater.
	/// Use ++iter rather than iter++ where possible to avoid two useless
	/// iterator copy constructions.
	//@{
	/// Pre-increment.
	/// \return This iterator after incremented.
	inline self& operator++()
	{
		next();
		
		return *this;
	}

	/// Post-increment.
	/// \return Another iterator having the old value of this iterator.
	inline self operator++(int)
	{
		self itr = *this;

		next();
		
		return itr;
	}
	//@}

	/// @name Iterator decrement movement functions.
	/// The two functions moves the iterator one element forward, so that
	/// the element it sits on has a smaller key. The btree/hash key 
	/// comparison routine determines which key is greater.
	/// Use --iter rather than iter-- where possible to avoid two useless
	/// iterator copy constructions.
	//@{
	/// Pre-decrement.
	/// \return This iterator after decremented.
	inline 	self& operator--() 
	{
		prev();
		
		return *this;
	}

	/// Post-decrement.
	/// \return Another iterator having the old value of this iterator.
	self operator--(int)
	{
		self itr = *this;
		prev();
		
		return itr;
	}
	//@}

	/// Assignment operator. This iterator will point to the same key/data
	/// pair as itr, and have the same configurations as itr.
	/// \param itr The right value of assignment.
	/// \return The reference of itr.
	/// \sa db_base_iterator::operator=(const self&)
	// We will duplicate the Dbc cursor here.
	inline const self& operator=(const self&itr) 
	{
		ASSIGNMENT_PREDCOND(itr)
		base::operator=(itr);

		curpair_base_.first = itr.curpair_base_.first;
		curpair_base_.second = itr.curpair_base_.second;
		if (pcsr_)
			pcsr_->close();
		pcsr_ = itr.pcsr_;
		
		return itr;
	}

	////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////
	// Begin iterator comparison functions.
	//
	/// \name Compare operators.
	/// Only equal comparison is supported.
	//@{
	/// Compare two iterators.
	/// Two iterators compare equal when they are both invalid or 
	/// both valid and underlying cursor compare equal(i.e. sitting on the
	/// same key/data pair).
	//
	// Note that the iterator itr or this iterator may be an invalid
	// one, i.e. its this->itr_status_ is INVALID_ITERATOR_POSITION. 
	// We do not distinguish between end and rend iterators although
	// we are able to do so, because they are never compared together.
	/// Equal comparison operator.
	/// \param itr The iterator to compare against.
	/// \return Returns true if equal, false otherwise.
	inline bool operator==(const self&itr) const
	{
		COMPARE_CHECK(itr)	
		if (((itr.itr_status_ == this->itr_status_) && 
		    (this->itr_status_ == INVALID_ITERATOR_POSITION)) ||
		    ((itr.itr_status_ == this->itr_status_) && 
		    (pcsr_->compare((itr.pcsr_.base_ptr())) == 0)))
			return true;
		return false;
		
	}

	/// Unequal comparison operator.
	/// \param itr The iterator to compare against.
	/// \return Returns false if equal, true otherwise.
	/// \sa bool operator==(const self&itr) const
	inline bool operator!=(const self&itr) const 
	{
		return !(*this == itr) ;
	}
	//@}
	////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////
	// Begin functions that retrieve values from the iterator.
	//
	// curpair_base_ is always kept updated on iterator movement, but if 
	// directdb_get_ is true, curpair_base_ is also updated here before 
	// making use of the value it references.
	// Even if this iterator is invalid, this call is allowed, the
	// default value of type T is returned.
	//
	// Note that the returned reference can only be used to read data,
	// can't be used to update data.
	/// \name Functions that retrieve values from the iterator.
	//@{
	/// Dereference operator.
	/// Return the reference to the cached data element, which is an 
	/// pair<Key_type, T>. You can only read its referenced data via 
	/// this iterator but can not update it.
	/// \return Current data element reference object, i.e. ElementHolder
	/// or ElementRef object.
	inline reference operator*() const 
	{
		
		if (this->directdb_get_) { 
			csrddt d;
			pcsr_->get_current_key_data(curpair_base_.first, d);
			assign_second0(curpair_base_, d);
		}
		// Returning reference, no copy construction.
		return curpair_base_;
	}
	 
	// curpair_base_ is always kept updated on iterator movement, but if 
	// directdb_get_ is true, curpair_base_ is also updated here before 
	// making use of the value it references.
	// Even if this iterator is invalid, this call is allowed, the
	// default value of type T is returned.
	//
	// Note that the returned reference can only be used to read data,
	// can't be used to update data.
	/// Arrow operator.
	/// Return the pointer to the cached data element, which is an 
	/// pair<Key_type, T>. You can only read its referenced data via 
	/// this iterator but can not update it.
	/// \return Current data element reference object's address, i.e. 
	/// address of ElementHolder or ElementRef object.
	inline pointer operator->() const
	{
		
		if (this->directdb_get_) {
			csrddt d;
			pcsr_->get_current_key_data(curpair_base_.first, d);
			assign_second0(curpair_base_, d);
		}

		return &curpair_base_;
	}
	//@}
	////////////////////////////////////////////////////////////////////
		
	////////////////////////////////////////////////////////////////////
	// Begin dbstl specific functions.
	//
	// Refresh the underlying cursor's current data and this object's 
	// curpair_base_. It need to be called only if directdb_get is 
	// disabled, and other iterators updated
	// the key/data pair this iterator points to and we are about to use 
	// this iterator to access that key/data pair. 
	// If direct db get is enabled, this method never needs to be called.
	/// @name dbstl specific functions
	//@{
	/// \brief Refresh iterator cached value.
	/// \param from_db If not doing direct database get and this parameter
	/// is true, we will retrieve data directly from db.
	/// \sa db_base_iterator::refresh(bool) 
	virtual int refresh(bool from_db = true) const
	{
		csrddt d;

		if (from_db && !this->directdb_get_)
			pcsr_->update_current_key_data_from_db(
			    DbCursorBase::SKIP_NONE);
		pcsr_->get_current_key_data(curpair_base_.first, d);
		assign_second0(curpair_base_, d);

		return 0;
	}

	// By calling this function, users can choose to close the underlying 
	// cursor before iterator destruction to get better performance 
	// potentially.
	/// \brief Close underlying Berkeley DB cursor of this iterator.
	/// \sa db_base_iterator::close_cursor() const
	inline void close_cursor() const
	{
		if (pcsr_)
			pcsr_->close();
	}

	/// Iterator movement function.
	/// Move this iterator to the specified key k, by default moves
	/// exactly to k, and update cached data element, you can
	/// also specify DB_SET_RANGE, to move to the biggest key smaller 
	/// than k. The btree/hash key comparison routine determines which
	/// key is bigger. When the iterator is on a multiple container, 
	/// move_to will move itself to the first key/data pair of the 
	/// identical keys.
	/// \param k The target key value to move to.
	/// \param flag Flags available: DB_SET(default) or DB_SET_RANGE.
	/// DB_SET will move this iterator exactly at k; DB_SET_RANGE moves
	/// this iterator to k or the smallest key greater than k. If fail
	/// to find such a key, this iterator will become invalid.
	/// \return 0 if succeed; non-0 otherwise, and this iterator becomes
	/// invalid. Call db_strerror with the return value to get the error 
	/// message.
	inline int move_to(const kdt& k, int flag = DB_SET) const
	{
		int ret;
		// Use tmpk2 to avoid k being modified.
		kdt tmpk2 = k;

		this->itr_status_ = (ret = pcsr_->move_to(tmpk2, flag));
		if (ret != 0) {
			this->inval_pos_type_ = base::IPT_UNSET;
			return ret;
		}

		refresh();

		return ret;
	}

	/// Modify bulk buffer size. 
	/// Bulk read is enabled when creating an
	/// iterator, so users later can only modify the bulk buffer size
	/// to another value, but can't enable/disable bulk read while an
	/// iterator is already alive. 
	/// \param sz The new size of the bulk read buffer of this iterator.
	/// \return Returns true if succeeded, false otherwise.
	/// \sa db_base_iterator::set_bulk_buffer(u_int32_t )
	bool set_bulk_buffer(u_int32_t sz)
	{
		bool ret = this->pcsr_->set_bulk_buffer(sz);
		if (ret)
			this->bulk_retrieval_ = 
			    this->pcsr_->get_bulk_bufsize();
		return ret;
	}

	/// \brief Get bulk retrieval buffer size in bytes.
	/// \return Return current bulk buffer size or 0 if bulk retrieval is
	/// not enabled.
	/// \sa db_base_iterator::get_bulk_bufsize()
	u_int32_t get_bulk_bufsize()
	{
		this->bulk_retrieval_ = pcsr_->get_bulk_bufsize();
		return this->bulk_retrieval_;
	}
	//@}
	////////////////////////////////////////////////////////////////////

protected:
	
	// The cursor_type is used to directly return the pair object, 
	// rather than a reference to it
	typedef DbCursor<kdt, csrddt> cursor_type;

	friend class db_map_iterator<kdt, ddt, ElementRef<ddt> >;
	friend class db_map_iterator<kdt, ddt, ElementHolder<ddt> >;
	// Use friend classes to hide internal members from users.
	friend class db_map<kdt, ddt, ElementHolder<ddt> >;
	friend class db_map<kdt, ddt, ElementHolder<kdt>, 
	    db_set_iterator<kdt, ElementHolder<kdt> > >;
	friend class db_map<kdt, _DB_STL_set_value<kdt>, ElementHolder<kdt>, 
	    db_set_iterator<kdt, ElementHolder<kdt> > >;

	friend class db_set<kdt, ElementHolder<kdt> >;
	friend class db_set_iterator<kdt, ElementHolder<kdt> >;
	friend class db_multiset<kdt, ElementHolder<kdt> >;
	friend class db_multimap<kdt, ddt, ElementHolder<ddt> >;
	friend class db_multimap<kdt, _DB_STL_set_value<kdt>, 
	    ElementHolder<kdt>, db_set_iterator<kdt, ElementHolder<kdt> > >;

	friend class db_map<kdt, ddt, ElementRef<ddt> >;
	friend class db_map<kdt, _DB_STL_set_value<kdt>, ElementRef<kdt>, 
	    db_set_iterator<kdt, ElementRef<kdt> > >;

	friend class db_set<kdt, ElementRef<kdt> >;
	friend class db_set_iterator<kdt, ElementRef<kdt> >;
	friend class db_multiset<kdt, ElementRef<kdt> >;
	friend class db_multimap<kdt, ddt, ElementRef<ddt> >;
	friend class db_multimap<kdt, _DB_STL_set_value<kdt>, ElementRef<kdt>,
	    db_set_iterator<kdt, ElementRef<kdt> > >;


	////////////////////////////////////////////////////////////////////
	// Begin db_map_base_iterator data members.
	//
	// Cursor of this iterator, note that each db_map_base_iterator has a  
	// unique DbCursor, not shared with any other iterator, and when copy 
	// constructing or assigning, the cursor is duplicated
	// when it is actually used to access db.
	//
	mutable LazyDupCursor<DbCursor<kdt, csrddt> > pcsr_;

	// In order for std::map style iterator to work, we need a pair 
	// here to store the key-value pair this iterator currently points
	// to in the db_map. 
	//
	// curpair_base_ is always kept updated on every cursor/iterator 
	// movement and initialized to point to the first key-value pair when
	// db_map<>::begin() is called.
	//
	mutable value_type curpair_base_;	
	////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////
	// Begin internal helper functions.
	//
	// Open the iterator and its cursor.
	//
	void open() const
	{	
		u_int32_t oflags = 0, coflags = 0;
		int ret;
		Db *pdb = this->owner_->get_db_handle();
		DbEnv *penv = pdb->get_env();

		coflags = this->owner_->get_cursor_open_flags();
		assert(this->owner_ != NULL);
		if (!this->read_only_ && penv != NULL) {
			BDBOP((penv->get_open_flags(&oflags)), ret);
			if ((oflags & DB_INIT_CDB) != 0)
				this->owner_->set_cursor_open_flags(coflags |= 
				    DB_WRITECURSOR);
		}
		if (!pcsr_)
			pcsr_.set_cursor(new DbCursor<kdt, csrddt>(
			    this->bulk_retrieval_, 
			    this->rmw_csr_, this->directdb_get_));
		this->itr_status_ = pcsr_->open((db_container*)this->owner_, 
		    coflags);
		
	}

	// Move this iterator as well as the underlying Dbc* cursor to 
	// first element and update cur_pair_.
	//
	int first() const
	{

		assert(this->owner_ != NULL);
		this->itr_status_ = pcsr_->first();
		if (this->itr_status_ == 0)	
			refresh();
		else
			this->inval_pos_type_ = base::IPT_UNSET;

		return this->itr_status_;
		
	}

	// Move this iterator as well as the underlying Dbc* cursor
	// to last effective(valid) element and update cur_pair_.
	//
	int last() const
	{

		assert(this->owner_ != NULL);
		this->itr_status_ = pcsr_->last();
		if (this->itr_status_ == 0)
			refresh();
		else
			this->inval_pos_type_ = base::IPT_UNSET;

		return this->itr_status_;
	}

	// Move this iterator as well as the underlying Dbc* cursor
	// to next element, then update its position flags and cur_pair_.
	//
	int next(int flags = DB_NEXT) const
	{

		assert(this->owner_ != NULL);
		
		if (this->itr_status_ == INVALID_ITERATOR_POSITION) {
			if (this->inval_pos_type_ == base::IPT_BEFORE_FIRST) {
				// This rend itr must have an non-NULL owner.
				open();
				// rend itr can go back to first element.
				this->itr_status_ = first();
			} else if (this->inval_pos_type_ == base::IPT_UNSET) {
				THROW0(InvalidIteratorException);
			}
			// Else, return itr_status_ in last line.
		} else {
			
			this->itr_status_ = pcsr_->next(flags);
			if (this->itr_status_ == 0)
				refresh();
			else 
				this->inval_pos_type_ = base::IPT_AFTER_LAST;
			
		}
		
		return this->itr_status_;
	}

	// Move this iterator as well as the underlying Dbc* cursor
	// to previous element.
	//
	int prev(int flags = DB_PREV) const
	{

		assert(this->owner_ != NULL);
		if (this->itr_status_ == INVALID_ITERATOR_POSITION) { 
			if (this->inval_pos_type_ == base::IPT_AFTER_LAST) {
				// This rend itr must have an non-NULL owner.
				open();
				// end itr can go back to last element.
				this->itr_status_ = last();
			} else if (this->inval_pos_type_ == base::IPT_UNSET) {
				THROW0(InvalidIteratorException);
			}
			// Else, return itr stat in last line.
		} else {
			this->itr_status_ = pcsr_->prev(flags);
			if (this->itr_status_ == 0)
				refresh();
			else 
				this->inval_pos_type_ = base::IPT_BEFORE_FIRST;
			
		}

		return this->itr_status_;
	}

	void set_curpair_base(const kdt& k, const csrddt &d) const
	{
		curpair_base_.first = k;
		assign_second0(curpair_base_, d);
	}

	////////////////////////////////////////////////////////////////////

protected: // Do not remove this line, otherwise assign_second0 may be public.
#ifndef DOXYGEN_CANNOT_SEE_THIS
#if NO_MEMBER_FUNCTION_PARTIAL_SPECIALIZATION
};// end of db_map_base_iterator<>
template <Typename kdt, Typename datadt, Typename ddt>
void assign_second0(pair<kdt, ddt>& v, const datadt& d) 
{
	v.second = d;
}

template<Typename kdt, Typename ddt>
void assign_second0(pair<kdt, ddt> &v, 
    const _DB_STL_set_value<kdt>&
    /* d unused, use v.first to assign v.second */) 
{
	v.second = v.first;
}
#else

template <Typename datadt>
inline void assign_second0(value_type& v, const datadt& d) const
{
	v.second = d;
}

template<>
inline void 
assign_second0(value_type &v, const _DB_STL_set_value<kdt>& 
    /* d unused, use v.first to assign v.second */) const
{
	v.second = v.first;
}

};// end of db_map_base_iterator<>

#endif

#else
};
#endif // DOXYGEN_CANNOT_SEE_THIS
//@} // db_map_iterators
//@} // dbstl_iterators

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// db_map_iterator class template definition
//
// db_map_iterator is the iterator class template for db_map and 
// db_multimap, it is also the base class for db_set_iterator. It can be
// used to both read and write the database.
//
// Template parameters info:
// kdt is "key data type", ddt is "data data type", value_type_sub is
// either ElementRef<ddt> (by default, in this case ElementRef inherits
// from ddt, so ddt must not be a primitive type) or ElementHolder<ddt>,
// in this case ElementHolder has a data member of type ddt, so suitable for
// primitive types, but don't apply it to classes otherwise you can't access
// members like this : *iterator.member = value.  
//
/// \ingroup dbstl_iterators 
//@{
/// \ingroup db_map_iterators
//@{
template <Typename kdt, Typename ddt, Typename value_type_sub>
class _exported db_map_iterator : public 
    db_map_base_iterator<kdt, typename value_type_sub::content_type, ddt>
{
protected:
	typedef db_map_iterator<kdt, ddt, value_type_sub> self;
	typedef typename value_type_sub::content_type realddt;
	using db_base_iterator<typename value_type_sub::content_type>::
	    replace_current_key;
public:
	typedef kdt key_type;
	typedef ddt data_type;
	typedef pair<kdt, ddt> value_type;
	typedef pair<kdt, value_type_sub> value_type_wrap;
	// Not used in this class, but required to satisfy 
	// db_reverse_iterator type extraction.
	typedef ptrdiff_t difference_type;
	typedef difference_type distance_type;
	typedef value_type_wrap& reference;
	typedef value_type_wrap* pointer;

	// We have to use standard iterator tags to match the parameter 
	// list of stl internal functions, we can't use our own tag 
	// classes, so we don't write tag classes in dbstl.
	//
	typedef std::bidirectional_iterator_tag iterator_category;
	
	// Refresh the underlying cursor's current data and this object's 
	// curpair_. It need to be called only if other iterators updated the 
	// key/data pair this iterator points to and we are about to use 
	// this iterator to access that key/data pair. If direct db get is
	// enabled, this method never needs to be called.
	/// \brief Refresh iterator cached value.
	/// \param from_db If not doing direct database get and this parameter
	/// is true, we will retrieve data directly from db.
	/// \sa db_base_iterator::refresh(bool )
	virtual int refresh(bool from_db = true) const
	{
		kdt k;
		ddt d;

		if (from_db && !this->directdb_get_)
			this->pcsr_->update_current_key_data_from_db(
			    DbCursorBase::SKIP_NONE);
		this->pcsr_->get_current_key_data(k, d);
		curpair_.first = k;
		assign_second(curpair_, d);
		this->set_curpair_base(k, d);

		return 0;
	}

	////////////////////////////////////////////////////////////////
	// Begin constructors and destructor definitions.
	/// \name Constructors and destructor
	/// Do not create iterators directly using these constructors, but
	/// call db_map::begin or db_multimap_begin to get instances of
	/// this class.
	/// \sa db_map::begin() db_multimap::begin()
	//@{
	/// Copy constructor.
	/// \param vi The other iterator of the same type to initialize this.
	db_map_iterator(const db_map_iterator<kdt, ddt, value_type_sub>& vi)
	    : db_map_base_iterator<kdt, realddt, ddt>(vi)
	{
		// Lazy-dup another cursor, cursor to iterator mapping 
		// is 1 to 1.
		curpair_.first = vi.curpair_.first;
		curpair_.second._DB_STL_CopyData(vi.curpair_.second);
		curpair_.second._DB_STL_SetIterator(this);
	}

	/// Base copy constructor.
	/// \param vi Initialize from a base class iterator.
	db_map_iterator(const db_map_base_iterator<kdt, realddt, ddt>& vi) :
	    db_map_base_iterator<kdt, realddt, ddt>(vi)

	{
		curpair_.second._DB_STL_SetIterator(this);
		curpair_.first = vi->first;
		curpair_.second._DB_STL_CopyData(vi->second);
	}

	/// Constructor.
	/// \param powner The container which creates this iterator.
	/// \param b_bulk_retrieval The bulk read buffer size. 0 means 
	/// bulk read disabled.
	/// \param brmw Whether set DB_RMW flag in underlying cursor.
	/// \param directdbget Whether do direct database get rather than
	/// using key/data values cached in the iterator whenever read.
	/// \param b_read_only Whether open a read only cursor. Only effective
	/// when using Berkeley DB Concurrent Data Store.
	explicit db_map_iterator(db_container*powner, 
	    u_int32_t b_bulk_retrieval = 0, bool brmw = false, 
	    bool directdbget = true, bool b_read_only = false)
	    : db_map_base_iterator<kdt, realddt, ddt>
	    (powner, b_bulk_retrieval, brmw, directdbget, b_read_only) 
	{ 
		curpair_.second._DB_STL_SetIterator(this);
	}

	/// Default constructor, dose not create the cursor for now.
	db_map_iterator() : db_map_base_iterator<kdt, realddt, ddt>()
	{
		curpair_.second._DB_STL_SetIterator(this);
	}

	// Use virtual because ElementRef<> uses a db_base_iterator* pointer 
	// to refer to the iterator, and also use "dead_" flag to avoid 
	// multiple call to the same destructor by ~ElementRef<>().
	/// Destructor.
	virtual ~db_map_iterator() 
	{
		// Required here though set in base destructor too.
		this->dead_ = true;
	}
	//@}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////
	// Begin functions that shift iterator position.
	//
	// Do not throw exceptions here because it is likely and normal 
	// to iterate to the "end iterator". 
	/// \name Iterator movement operators.
	//@{
	/// Pre-increment \sa db_map_base_iterator::operator++()
	/// \return This iterator after incremented.
	inline self& operator++()
	{
		this->next();
		
		return *this;
	}

	/// Post-increment \sa db_map_base_iterator::operator++(int)
	/// \return Another iterator having the old value of this iterator.
	inline self operator++(int)
	{
		self itr = *this;

		this->next();
		
		return itr;
	}

	/// Pre-decrement \sa db_map_base_iterator::operator--()
	/// \return This iterator after decremented.
	inline 	self& operator--() 
	{
		this->prev();
		
		return *this;
	}

	/// Post-decrement \sa db_map_base_iterator::operator--(int)
	/// \return Another iterator having the old value of this iterator.
	self operator--(int)
	{
		self itr = *this;
		this->prev();
		
		return itr;
	}
	//@}
	// Assignment operator, we will duplicate the Dbc cursor here.
	/// Assignment operator. This iterator will point to the same key/data
	/// pair as itr, and have the same configurations as itr.
	/// \param itr The right value of assignment.
	/// \return The reference of itr.
	/// \sa db_base_iterator::operator=(const self&)
	inline const self& operator=(const self&itr) 
	{
		ASSIGNMENT_PREDCOND(itr)
		base::operator=(itr);

		curpair_.first = itr.curpair_.first;

		// Only copy data from itr.curpair_ into curpair_, 
		// don't store into db. Note that we can not assign 
		// itr.curpair_ to curpair_ simply by curpair_ = itr.curpair_,
		// otherwise, ElementRef<>::operator= is called, which will
		// update the data element referenced by this iterator.
		//
		curpair_.second._DB_STL_CopyData(itr.curpair_.second);
		
		return itr;
	}
	////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////
	// Begin functions that retrieve values from the iterator.
	//
	// curpair_base_ is always kept updated on iterator movement, but if 
	// directdb_get_ is true, curpair_base_ is also updated here before 
	// making use of the value it references.
	// Even if this iterator is invalid, this call is allowed, the
	// default value of type T is returned.
	//
	/// \name Functions that retrieve values from the iterator.
	//@{
	/// Dereference operator.
	/// Return the reference to the cached data element, which is an
	/// pair<Key_type, ElementRef<T> > object if T is a class type or an
	/// pair<Key_type, ElementHolder<T> > object if T is a C++ primitive
	/// data type. 
	/// \return Current data element reference object, i.e. ElementHolder
	/// or ElementRef object.
	inline reference operator*() const 
	{
		
		if (this->directdb_get_) {
			ddt d;
			this->pcsr_->get_current_key_data(curpair_.first, d);
			assign_second(curpair_, d);
		}

		return curpair_;// returning reference, no copy construction
	}
	 
	// curpair_base_ is always kept updated on iterator movement, but if 
	// directdb_get_ is true, curpair_base_ is also updated here before 
	// making use of the value it references.
	// Even if this iterator is invalid, this call is allowed, the
	// default value of type T is returned.
	/// Arrow operator.
	/// Return the pointer to the cached data element, which is an
	/// pair<Key_type, ElementRef<T> > object if T is a class type or an
	/// pair<Key_type, ElementHolder<T> > object if T is a C++ primitive
	/// data type. 
	/// \return Current data element reference object's address, i.e. 
	/// address of ElementHolder or ElementRef object.
	inline pointer operator->() const
	{
	
		if (this->directdb_get_) {
			ddt d;
			this->pcsr_->get_current_key_data(curpair_.first, d);
			assign_second(curpair_, d);
		}
		return &curpair_;
	}
	//@}
	////////////////////////////////////////////////////////////////////

//@} // db_map_iterators
//@} // dbstl_iterators
protected:
	// The cursor_type is used to directly return the pair object, 
	// rather than a reference to it.
	typedef DbCursor<kdt, ddt> cursor_type;
	typedef db_map_base_iterator<kdt, realddt, ddt> base;
	typedef db_map_base_iterator<kdt, realddt> const_version;

	// Use friend classes to hide internal members from users.
	friend class db_map<kdt, ddt, value_type_sub>;
	friend class db_map<kdt, ddt, value_type_sub, 
	    db_set_iterator<kdt, value_type_sub> >;
	friend class db_set<kdt, value_type_sub>;
	friend class db_set_iterator<kdt, value_type_sub>;
	friend class db_multiset<kdt, value_type_sub>;
	friend class db_multimap<kdt, ddt, value_type_sub>;
	friend class db_multimap<kdt, _DB_STL_set_value<kdt>, value_type_sub,
	    db_set_iterator<kdt, value_type_sub > >;

	////////////////////////////////////////////////////////////////
	// Begin db_map_iterator data members.
	//
	// In order for std::map style iterator to work, we need a pair 
	// here to store the key-value pair this iterator currently points
	// to in the db_map. 
	//
	// curpair_ is always kept updated on every cursor/iterator movement
	// and initialized to point to the first key-value pair when
	// db_map<>::begin() is called.
	//
	mutable value_type_wrap curpair_;	
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin internal helper functions.
	//
	// Called by ElementRef<> object when this iterator belongs to the 
	// object---The only situation is in db_container::operator[] which
	// has to return an ElementRef/Holder object A, and its iterator has
	// to survive until A is destructed.
	virtual void delete_me() const
	{
		if (!this->dead_)
			delete this;
	}

	// Duplicate this iterator.
	virtual self* dup_itr() const
	{
		self *itr = new self(*this);
		// The curpair_ of itr does not delete itr, the independent 
		// one does.
		//itr->curpair_.second._DB_STL_SetDelItr();
		return itr;
	}

	// Replace the current key/data pair's data pointed to by this 
	// iterator's underlying Dbc* cursor with the parameter d.
	//
	virtual int replace_current(
	    const typename value_type_sub::content_type& d)
	{
		int ret;

		if (this->read_only_) {
			THROW(InvalidFunctionCall, (
"db_map_iterator<>::replace_current can't be called via a read only iterator"));
		}
		ret = this->pcsr_->replace(d);
		
		return ret;
	}

	// Used by set iterator to store another different key : 
	// remove the previous one then insert the new one.
	// It has to be defined in this class because db_set_iterator
	// inherits from db_map_iterator but we have no polymorphism when
	// using stl because the object are always used rather the 
	// pointer/reference.
	//
	virtual int replace_current_key(const kdt& k)
	{
		int ret;

		if (this->read_only_) {
			THROW(InvalidFunctionCall, (
"db_map_iterator<>::replace_current_key can't be called via a read only iterator"));
		}
		ret = this->pcsr_->replace_key(k);

		return ret;
	}

	////////////////////////////////////////////////////////////////


protected: // Do not remove this line, otherwise assign_second may be public.
#ifndef DOXYGEN_CANNOT_SEE_THIS
#if NO_MEMBER_FUNCTION_PARTIAL_SPECIALIZATION
};// end of db_map_iterator<>

template <Typename kdt, Typename datadt, Typename value_type_sub>
void assign_second(pair<kdt, value_type_sub>& v, const datadt& d) 
{
	v.second._DB_STL_CopyData(d);
}

template<Typename kdt, Typename value_type_sub>
void assign_second(pair<kdt, value_type_sub> &v, 
    const _DB_STL_set_value<kdt>&
    /* d unused, use v.first to assign v.second */) 
{
	v.second._DB_STL_CopyData(v.first);
}
#else

template <Typename datadt>
inline void assign_second(value_type_wrap& v, const datadt& d) const
{
	v.second._DB_STL_CopyData(d);
}

template<>
inline void 
assign_second(value_type_wrap &v, const _DB_STL_set_value<kdt>& 
    /* d unused, use v.first to assign v.second */) const
{
	v.second._DB_STL_CopyData(v.first);
}

};// end of db_map_iterator<>

#endif
#else
};
#endif // DOXYGEN_CANNOT_SEE_THIS
u_int32_t hash_default(Db *dbp, const void *key, u_int32_t len);

//////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//
// db_map container class definition
/// \ingroup dbstl_containers
//@{
/// db_map has identical methods to std::map and the semantics for each
/// method is identical to its std::map counterpart, except that it stores data
/// into underlying Berkeley DB btree or hash database. Passing a database
/// handle of btree or hash type creates a db_map equivalent to std::map and
/// std::hashmap respectively.
/// Database(dbp) and environment(penv) handle requirement(applies to all 
/// constructors in this class template):
/// 0. The dbp is opened inside the penv environment. Either one of the two 
/// handles can be NULL. If dbp is NULL, an anonymous database is created 
/// by dbstl. 
/// 1. Database type of dbp should be DB_BTREE or DB_HASH.
/// 2. No DB_DUP or DB_DUPSORT flag set in dbp.
/// 3. No DB_RECNUM flag set in dbp.
/// 4. No DB_TRUNCATE specified in dbp's database open flags.
/// 5. DB_THREAD must be set if you are sharing the dbp across 
/// multiple threads directly, or indirectly by sharing the container object 
/// across multiple threads.
/// \param kdt The key data type.
/// \param ddt The data data type. db_map stores key/data pairs.
/// \param value_type_sub Do not specify anything if ddt type is a 
/// class/struct type; Otherwise, specify ElementHolder<ddt> to it.
/// \param iterator_t Never specify anything to this type parameter. It is
/// only used internally.
/// \sa db_container db_container(Db*, DbEnv*) db_container(const db_container&)
template <Typename kdt, Typename ddt, Typename value_type_sub, 
    Typename iterator_t>
class _exported db_map : public db_container 
{

public:
	// iterator_t is default argument, see forward declaration at the
	// head of this file
	typedef iterator_t iterator;
	typedef typename iterator::const_version const_iterator;
	typedef db_reverse_iterator<iterator, const_iterator> reverse_iterator;
	typedef db_reverse_iterator<const_iterator, iterator>
		const_reverse_iterator;
	typedef kdt key_type;
	typedef ddt data_type;
	typedef value_type_sub data_type_wrap;
	typedef pair<kdt, ddt> value_type;
	typedef pair<kdt, value_type_sub> value_type_wrap;
	typedef const value_type const_value_type;
	typedef ptrdiff_t difference_type;
	typedef size_t size_type;
	// The following three types are not used in db_map, but we define
	// them to conform to stl specifications.
	typedef value_type_wrap& reference;
	typedef const value_type& const_reference;
	typedef value_type_wrap* pointer;
protected:
	typedef db_map<kdt, ddt, value_type_sub, iterator> self;
	typedef typename value_type_sub::content_type realddt;

	// This constructor is for db_multimap's constructors to call, 
	// because other constructors of this class will verify db handles 
	// and create one if needed. We need a special one that don't do 
	// anything. The BulkRetrievalOption is randomly picked, no special
	// implications at all.
	db_map(BulkRetrievalOption& arg){ delete &arg; }
public:
	////////////////////////////////////////////////////////////////
	// Begin inner class definitions.
	//
	// key_compare class definition, it is defined as an inner class,
	// using underlying btree/hash db's compare function
	//
	class key_compare 
	{
	private:
		Db*pdb;
	public:
		key_compare(Db*pdb1)
		{
			pdb = pdb1;
		}
		bool operator()(const kdt& k1, const kdt& k2) const
		{
			return compare_keys(pdb, k1, k2);
		}

	}; // key_compare class definition

	// value_compare class definition, it is defined as an inner class,
	// using key_compare class to do comparison. 
	//
	// The difference between key_compare and value_compare is the 
	// parameter its operator() function accepts, see the function
	// signature.
	//
	class value_compare 
	{
		key_compare kc;
	public:
		value_compare(Db*pdb) : kc(pdb)
		{
		
		}

		bool operator()(const value_type& v1, 
		    const value_type& v2) const
		{
			
			return kc(v1.first, v2.first);
		}

	}; // value_compare class definition

	class hasher
	{
	private:
		Db*pdb;
	public:
		hasher(Db*db){pdb = db;}
		size_t operator()(const kdt&k) const
		{
			DBTYPE dbtype;
			int ret;

			assert(pdb != NULL);
			ret = pdb->get_type(&dbtype);
			assert(ret == 0);
			if (dbtype != DB_HASH) {
				THROW(InvalidFunctionCall, (
				    "db_map<>::hasher"));
			}
			h_hash_fcn_t hash = NULL;
			BDBOP(pdb->get_h_hash(&hash), ret);
			if (hash == NULL)
				hash = hash_default;
			return hash(pdb, &k, sizeof(k));
		}
	}; // hasher

	class key_equal
	{
	private:
		Db*pdb;
	public:
		key_equal(Db*db){pdb = db;}
		bool operator()(const kdt& kk1, const kdt&kk2) const
		{
			DBTYPE dbtype;
			kdt k1 = kk1, k2 = kk2;
			int ret;

			dbstl_assert(pdb != NULL);
			ret = pdb->get_type(&dbtype);
			dbstl_assert(ret == 0);
			if (dbtype != DB_HASH) {
				THROW(InvalidFunctionCall, (
				    "db_map<>::key_equal"));
			}

			db_compare_fcn_t comp = NULL;
			BDBOP(pdb->get_h_compare(&comp), ret);
			if (comp == NULL)
				return memcmp(&kk1, &kk2, sizeof(kdt)) == 0;
			Dbt kd1(&k1, sizeof(k1)), kd2(&k2, sizeof(k2));
			
			return comp(pdb, &kd1, &kd2) == 0;


		}

	};// key_equal
	////////////////////////////////////////////////////////////////

	/// Function to get key compare functor.
	/// Used when this container is a hash_map, hash_multimap,
	/// hash_set or hash_multiset equivalent.
	/// \return key_equal type of compare functor.
	/// \sa http://www.sgi.com/tech/stl/hash_map.html
	inline key_equal key_eq() const
	{
		key_equal ke(this->get_db_handle());
		return ke;
	}

	/// Function to get hash key generating functor.
	/// Used when this container is a hash_map, hash_multimap,
	/// hash_set or hash_multiset equivalent.
	/// \return The hash key generating functor.
	/// \sa http://www.sgi.com/tech/stl/hash_map.html
	inline hasher hash_funct() const
	{
		hasher h(this->get_db_handle());
		return h;

	}

	/// Function to get value compare functor. Used when this container
	/// is a std::map, std::multimap, std::set or std::multiset equivalent.
	/// \return The value compare functor.
	/// \sa http://www.cplusplus.com/reference/stl/map/value_comp/
	inline value_compare value_comp() const
	{
		value_compare vc(this->get_db_handle());
		return vc;
	}
	
	/// Function to get key compare functor. Used when this container
	/// is a std::map, std::multimap, std::set or std::multiset equivalent.
	/// \return The key compare functor.
	/// \sa http://www.cplusplus.com/reference/stl/map/key_comp/
	inline key_compare key_comp() const 
	{
		key_compare kc(this->get_db_handle());
		return kc;
	}

	////////////////////////////////////////////////////////////////
	// Begin constructors and destructor definitions.
	/// \name Constructors and destructor
	//@{
	// We don't need the equal compare or allocator here, user need to 
	// call Db::set_bt_compare or Db::set_h_compare to set comparison
	// function.
	/// Create a std::map/hash_map equivalent associative container.
	/// See the handle requirement in class details to pass correct 
	/// database/environment handles.
	/// \param dbp The database handle.
	/// \param envp The database environment handle.
	/// \sa db_container(Db*, DbEnv*)
	explicit db_map(Db *dbp = NULL, DbEnv* envp = NULL)  : 
	    db_container(dbp, envp)
	{
		const char *errmsg;

		this->open_db_handles(dbp, envp, DB_BTREE, 
		    DB_CREATE | DB_THREAD, 0);
		
		if ((errmsg = verify_config(dbp, envp)) != NULL) {
			THROW(InvalidArgumentException, ("Db*", errmsg));
		}
		this->set_db_handle_int(dbp, envp);
	}

	/// Iteration constructor. Iterates between first and last, 
	/// setting a copy of each of the sequence of elements as the
	/// content of the container object. 
	/// Create a std::map/hash_map equivalent associative container.
	/// Insert a range of elements into the database. The range is
	/// [first, last), which contains elements that can
	/// be converted to type ddt automatically.
	/// See the handle requirement in class details to pass correct
	/// database/environment handles.
	/// This function supports auto-commit.
	/// \param dbp The database handle.
	/// \param envp The database environment handle.
	/// \param first The closed boundary of the range.
	/// \param last The open boundary of the range.
	/// \sa db_container(Db*, DbEnv*)
	template <class InputIterator> 
	db_map(Db *dbp, DbEnv* envp, InputIterator first, 
	    InputIterator last) : db_container(dbp, envp)
	{
		const char *errmsg;

		this->open_db_handles(dbp, envp, DB_BTREE, 
		    DB_CREATE | DB_THREAD, 0);
		if ((errmsg = verify_config(dbp, envp)) != NULL) {
			THROW(InvalidArgumentException, ("Db*", errmsg));
		}
		this->set_db_handle_int(dbp, envp);

		this->begin_txn();
		try {
			insert(first, last);
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
	}

	// Copy constructor. The object is initialized to have the same 
	// contents as the x map object, do not copy properties because
	// if we copy things like pdb, we are storing to the same db, so we
	// create a new database, use it as the backing db, and store data 
	// into it.
	/// Copy constructor.
	/// Create an database and insert all key/data pairs in x into this
	/// container. x's data members are not copied.
	/// This function supports auto-commit.
	/// \param x The other container to initialize this container.
	/// \sa db_container(const db_container&)
	db_map(const db_map<kdt, ddt, value_type_sub, iterator>& x) : 
	    db_container(x)
	{
		verify_db_handles(x);
		this->set_db_handle_int(this->clone_db_config(
		    x.get_db_handle()), x.get_db_env_handle());
		assert(this->get_db_handle() != NULL);
		
		this->begin_txn();
		try {
			copy_db((db_map<kdt, ddt, value_type_sub, iterator>&)x);
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
	}

	virtual ~db_map(){}
	//@}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin insert function definitions.
	/// Container content assignment operator.
	/// This function supports auto-commit.
	/// \param x The other container whose key/data pairs will be inserted
	/// into this container. Old content in this containers are discarded.
	/// \sa http://www.cplusplus.com/reference/stl/map/operator=/
	inline const self& operator=(const self& x)
	{
		ASSIGNMENT_PREDCOND(x)
		db_container::operator =(x);
		verify_db_handles(x);
		assert(this->get_db_handle() != NULL);
		this->begin_txn();
		try {
			copy_db((self &)x);
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
		return x;
	}

	/// \name Insert Functions
	/// They have similiar usage as their C++ STL equivalents.
	/// Note that when secondary index is enabled, each 
	/// db_container can create a db_multimap secondary container, 
	/// but the insert function is not functional for secondary containers.
	/// \sa http://www.cplusplus.com/reference/stl/map/insert/
	//@{
	//
	// Insert functions. Note that stl requires if the entry with x.key 
	// already exists, insert should not overwrite that entry and the 
	// insert should fail; but bdb Dbc::cursor(DB_KEYLAST) will replace
	// existing data with new one, so we will first find whether we
	// have this data, if have, return false;
	//
	// Can not internally use begin/commit_txn to wrap this call because 
	// it returns an iterator, which is closed after commit_txn(), and
	// reopening it is wrong in multithreaded access.
	/// Insert a single key/data pair if the key is not in the container.
	/// \param x The key/data pair to insert.
	/// \return A pair P, if insert OK, i.e. the inserted key wasn't in the
	/// container, P.first will be the iterator sitting on the inserted
	/// key/data pair, and P.second is true; otherwise P.first is an 
	/// invalid iterator and P.second is false.
	pair<iterator,bool> insert (const value_type& x ) 
	{
		pair<iterator,bool> ib;
		iterator witr;

		init_itr(witr);
		open_itr(witr);

		if (witr.move_to(x.first) == 0) {// has it
			ib.first = witr;
			ib.second = false;
			// Cursor movements are not logged, no need to 
			// use transaction here.
			return ib;
		} 
	
		witr.itr_status_ = witr.pcsr_->insert(x.first, x.second, 
		    DB_KEYLAST);
		assert(witr.itr_status_ == 0);
		witr.refresh(false);
		ib.first = witr;
		ib.second = true;

		return ib;
	}

	/// Insert with hint position. We ignore the hint position because 
	/// Berkeley DB knows better where to insert.
	/// \param position The hint position.
	/// \param x The key/data pair to insert.
	/// \return The iterator sitting on the inserted key/data pair, or an
	/// invalid iterator if the key was already in the container.
	inline iterator insert (iterator position, const value_type& x ) 
	{
		pair<iterator,bool> ib = insert(x);
		return ib.first;
	}

	// Member function template overload.
	/// Range insertion. Insert a range [first, last) of key/data pairs
	/// into this container.
	/// \param first The closed boundary of the range.
	/// \param last The open boundary of the range.
	void insert (const db_map_base_iterator<kdt, realddt, ddt>& first, 
	    const db_map_base_iterator<kdt, realddt, ddt>& last) 
	{
		db_map_base_iterator<kdt, realddt, ddt> ii;
		iterator witr;

		init_itr(witr);
		open_itr(witr);

		for (ii = first; ii != last; ++ii)
			witr.pcsr_->insert(ii->first, ii->second, 
			    DB_KEYLAST);	
	}

	/// Range insertion. Insert a range [first, last) of key/data pairs
	/// into this container.
	/// \param first The closed boundary of the range.
	/// \param last The open boundary of the range.
	template<typename InputIterator>
	void insert (InputIterator first, InputIterator last) 
	{
		InputIterator ii;
		iterator witr;

		init_itr(witr);
		open_itr(witr);

		for (ii = first; ii != last; ++ii)
			witr.pcsr_->insert(ii->first, ii->second, 
			    DB_KEYLAST);	
	}
	//@}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin functions that create iterators.
	/// \name Iterator Functions
	/// The parameters in begin functions of this group have identical
	/// meaning to thoes in db_vector::begin, refer to those functions
	/// for details.
	/// \sa db_vector::begin()
	//@{
	/// Begin a read-write or readonly iterator which sits on the first
	/// key/data pair of the database. 
	/// \param rmw Same as that of 
	/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \param bulkretrieval Same as that of 
	/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \param directdb_get Same as that of
	/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \param readonly Same as that of 
	/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \return The created iterator.
	/// \sa db_vector::begin(ReadModifyWriteOption, bool, 
	/// BulkRetrievalOption, bool)
	//
	iterator begin(ReadModifyWriteOption rmw = 
	    ReadModifyWriteOption::no_read_modify_write(),
	    bool readonly = false, BulkRetrievalOption bulkretrieval = 
	    BulkRetrievalOption::no_bulk_retrieval(), 
	    bool directdb_get = true) 
	{
		bool b_rmw;
		u_int32_t bulk_retrieval = 0;

		b_rmw = (rmw == ReadModifyWriteOption::read_modify_write());
		// Read only cursor don't need acquire write lock.
		if (readonly && b_rmw)
			b_rmw = false;
		if (readonly && bulkretrieval == BulkRetrievalOption::
		    BulkRetrieval)
			bulk_retrieval = bulkretrieval.bulk_buf_size();

		iterator itr(dynamic_cast<db_container*>(this), 
		    bulk_retrieval, b_rmw, directdb_get, readonly);

		open_itr(itr, readonly);
		itr.first();
		return itr;
	}

	/// Begin a read-only iterator.
	/// \param bulkretrieval Same as that of 
	/// begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \param directdb_get Same as that of
	/// begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \return The created const iterator.
	/// \sa db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption,
	/// bool);
	const_iterator begin(BulkRetrievalOption bulkretrieval = 
	    BulkRetrievalOption::no_bulk_retrieval(), 
	    bool directdb_get = true) const 
	{
		u_int32_t b_bulk_retrieval = (bulkretrieval == 
		    BulkRetrievalOption::BulkRetrieval ? 
		    bulkretrieval.bulk_buf_size() : 0);

		const_iterator itr((db_container*)this,
		     b_bulk_retrieval, false, directdb_get, true);

		open_itr(itr, true);
		itr.first();
		return itr;
	}

	/// \brief Create an open boundary iterator.
	/// \return Returns an invalid iterator denoting the position after 
	/// the last valid element of the container.
	/// \sa db_vector::end()
	inline iterator end()
	{
		iterator itr;

		// end() is at an invalid position. We don't know what key it 
		// refers, so itr_status_ and inval_pos_type are the only 
		// data members to identify an iterator's position.
		//
		itr.itr_status_ = INVALID_ITERATOR_POSITION;
		itr.inval_pos_type_ = iterator::IPT_AFTER_LAST;
		itr.owner_ = (db_container*)this;
		return itr;
	}

	/// \brief Create an open boundary iterator.
	/// \return Returns an invalid const iterator denoting the position 
	/// after the last valid element of the container.
	/// \sa db_vector::end() const
	inline const_iterator end() const
	{
		const_iterator itr;

		// end() is at an invalid position. We don't know what key it 
		// refers, so itr_status_ and inval_pos_type are the only 
		// data members to identify an iterator's position.
		//
		itr.itr_status_ = INVALID_ITERATOR_POSITION;
		itr.inval_pos_type_ = iterator::IPT_AFTER_LAST;
		itr.owner_ = (db_container*)this;
		return itr;
	}

	/// Begin a read-write or readonly reverse iterator which sits on the 
	/// first key/data pair of the database. 
	/// \param rmw Same as that of 
	/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \param bulkretrieval Same as that of 
	/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \param directdb_get Same as that of
	/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \param read_only Same as that of 
	/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \return The created iterator.
	/// \sa db_vector::begin(ReadModifyWriteOption, bool, 
	/// BulkRetrievalOption, bool)
	/// \sa db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption,
	/// bool);
	reverse_iterator rbegin(ReadModifyWriteOption rmw = 
	    ReadModifyWriteOption::no_read_modify_write(),
	    bool read_only = false, BulkRetrievalOption bulkretrieval = 
	    BulkRetrievalOption::no_bulk_retrieval(), 
	    bool directdb_get = true) 
	{
		u_int32_t bulk_retrieval = 0;

		iterator itr = end();
		itr.rmw_csr_ = (rmw == (
		    ReadModifyWriteOption::read_modify_write())) && !read_only;
		itr.directdb_get_ = directdb_get;
		itr.read_only_ = read_only;
		if (read_only && bulkretrieval == BulkRetrievalOption::
		    BulkRetrieval)
			bulk_retrieval = bulkretrieval.bulk_buf_size();
		itr.bulk_retrieval_ = bulk_retrieval;
		reverse_iterator ritr(itr);
		
		return ritr;
	}

	/// Begin a read-only reverse iterator.
	/// \param bulkretrieval Same as that of 
	/// begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \param directdb_get Same as that of
	/// begin(ReadModifyWrite, bool, BulkRetrievalOption, bool);
	/// \return The created const iterator.
	/// \sa db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, 
	/// bool);
	const_reverse_iterator rbegin(BulkRetrievalOption bulkretrieval = 
	    BulkRetrievalOption::no_bulk_retrieval(), 
	    bool directdb_get = true) const
	{
		const_iterator itr = end();
		itr.bulk_retrieval_ = (bulkretrieval == 
		    BulkRetrievalOption::BulkRetrieval ? 
		    bulkretrieval.bulk_buf_size() : 0);
		itr.directdb_get_ = directdb_get;
		itr.read_only_ = true;
		const_reverse_iterator ritr(itr);
		
		return ritr;
	}

	/// \brief Create an open boundary iterator.
	/// \return Returns an invalid iterator denoting the position 
	/// before the first valid element of the container.
	/// \sa db_vector::rend()
	inline reverse_iterator rend()
	{
		reverse_iterator ritr;
		ritr.inval_pos_type_ = iterator::IPT_BEFORE_FIRST;
		return ritr;
	}

	/// \brief Create an open boundary iterator.
	/// \return Returns an invalid const iterator denoting the position 
	/// before the first valid element of the container. 
	/// \sa db_vector::rend() const
	inline const_reverse_iterator rend() const
	{
		const_reverse_iterator ritr;
		ritr.inval_pos_type_ = iterator::IPT_BEFORE_FIRST;
		return ritr;
	}
	//@} // iterator functions
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	//
	// Begin functions that return container metadata.
	/// \name Metadata Functions
	/// These functions return metadata about the container.
	//@{
	/// Get container category.
	/// Determines whether this container object is a std::map<> 
	/// equivalent(when returns false) or that of hash_map<> 
	/// class(when returns true). This method is not in stl, but it
	/// may be called by users because some operations are not supported
	/// by both type(map/hash_map) of containers, you need to call this
	/// function to distinguish the two types. dbstl will not stop you
	/// from calling the wrong methods of this class.
	/// \return Returns true if this container is a hash container based
	/// on a Berkeley DB hash database; returns false if it is based on a
	/// Berkeley DB btree database.
	//
	inline bool is_hash() const
	{
		DBTYPE dbtype = DB_UNKNOWN;
		int ret;

		assert(this->get_db_handle() != NULL);
		ret = this->get_db_handle()->get_type(&dbtype);
		assert(ret == 0);
		return dbtype == DB_HASH;
	}

	/// Only for std::hash_map, return number of hash bucket in use.
	/// This function supports auto-commit.
	/// \return The number of hash buckets of the database.
	size_type bucket_count() const
	{
		DBTYPE dbtype;
		u_int32_t flags;
		void *sp;
		size_type sz;
		int ret;
		DbTxn*txn;
		
		assert(this->get_db_handle() != NULL);
		ret = this->get_db_handle()->get_type(&dbtype);
		assert(ret == 0);
		if (dbtype != DB_HASH) {
			THROW(InvalidFunctionCall, ("db_map<>::bucket_count"));
			
		}
		flags = DB_FAST_STAT;
		
		// Here we use current_txn(), so we will get a valid 
		// transaction handle if we are using explicit transactions; 
		// and NULL if we are using autocommit, in which case bdb 
		// internal auto commit will be enabled automatically.
		//
		txn = ResourceManager::instance()->
		    current_txn(this->get_db_handle()->get_env());
		BDBOP(this->get_db_handle()->stat(txn, &sp, flags), ret);
		
		sz = (size_type)(((DB_HASH_STAT*)sp)->hash_buckets);
		free(sp);
		return sz;
	}
	
	/// Get container size.
	// Return size of the map, can control whether compute
	// accurately(slower if db is huge) or not.
	/// This function supports auto-commit.
	/// \return Return the number of key/data pairs in the container.
	/// \param accurate This function uses database's statistics to get
	/// the number of key/data pairs. The statistics mechanism will either
	/// scan the whole database to find the accurate number or use the
	/// number of last accurate scanning, and thus much faster. If there 
	/// are millions of key/data pairs, the scanning can take some while,
	/// so in that case you may want to set the "accurate" parameter to 
	/// false.
	size_type size(bool accurate = true) const
	{
		u_int32_t flags;
		void *sp;
		DBTYPE dbtype;
		size_t sz;
		int ret;
		DbTxn*txn;

		flags = accurate ? 0 : DB_FAST_STAT;
		BDBOP(this->get_db_handle()->get_type(&dbtype), ret);

		// Here we use current_txn(), so we will get a valid 
		// transaction handle if we are using explicit transactions; 
		// and NULL if we are using autocommit, in which case bdb 
		// internal auto commit will be enabled automatically.
		//
		txn = ResourceManager::instance()->
		    current_txn(this->get_db_handle()->get_env());
		BDBOP(this->get_db_handle()->stat(txn, &sp, flags), ret);

		assert((dbtype == DB_BTREE) || (dbtype == DB_HASH));
		// dbtype is BTREE OR HASH, no others.
		sz = dbtype == DB_BTREE ? ((DB_BTREE_STAT*)sp)->
		    bt_ndata : ((DB_HASH_STAT*)sp)->hash_ndata;
		free(sp);
		return sz;
	}

	/// Get max size.
	/// The returned size is not the actual limit of database. See the
	/// Berkeley DB limits to get real max size.
	/// \return A meaningless huge number.
	/// \sa db_vector::max_size()
	inline size_type max_size() const
	{
		return SIZE_T_MAX;
	}

	/// Returns whether this container is empty.
	/// This function supports auto-commit.
	/// \return True if empty, false otherwise.
	bool empty() const
	{
		// If we fail to move to the first record, the db is 
		// supposed to be empty.
		const_iterator witr;
		bool ret;

		try {
			this->begin_txn();
			init_itr(witr);
			open_itr(witr, true);
			ret = witr.first() != 0;
			this->commit_txn();
			return ret;
		} catch (...) {
			this->abort_txn();
			throw;
		}
	}
	//@}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin element accessors.
	//
	// Don't use transaction wrapper(begin/commit_txn) here because 
	// even insert(the only logged operation) is only part of the 
	// whole expression---the use case is dmmap[key] = value; 
	// So insert and another put call should
	// be atomic, so there must be an outside transaction.
	//
	// As stated in STL specification, this method can't have "const" 
	// modifier because it is likely to insert a new record.
	/// Retrieve data element by key.
	/// This function returns an reference to the underlying data element
	/// of the specified key x. The returned object can be used to read or
	/// write the data element of the key/data pair. 
	/// Do use a data_type_wrap of db_map or value_type::second_type(they 
	/// are the same) type of variable to hold the return value of this 
	/// function.
	/// \param x The target key to get value from.
	/// \return Data element reference.
	//
	data_type_wrap operator[] (const key_type& x)
	{
		iterator witr, *pitr;
		int ret;

		init_itr(witr);
		open_itr(witr, false);
		
		if (witr.move_to(x) != 0) {
			ddt d;//default value
			DbstlInitializeDefault<ddt> initdef(d);
			// Insert (x, d) as place holder.
			witr.pcsr_->insert(x, d, DB_KEYLAST);
			// Should be OK this time.
			ret = witr.move_to(x);
			assert(ret == 0);
			// Return the reference to the data item of x.
		}
		
		//witr->curpair_.second._DB_STL_SetDelItr();
		pitr = new iterator(witr);
		data_type_wrap ref(pitr->curpair_.second);
		ref._DB_STL_SetDelItr();
		return ref;
	}

	// Only returns a right-value, no left value for assignment, so 
	// directly return the value rather than the ElementRef/ElementHolder 
	// wrapper. Must use a const reference to this container to call this
	// const function.
	//
	/// Retrieve data element by key.
	/// This function returns the value of the underlying data element of
	/// specified key x. You can only read the element, but unable to 
	/// update the element via the return value of this function. And you
	/// need to use the container's const reference to call this method.
	/// \param x The target key to get value from.
	/// \return Data element, read only, can't be used to modify it.
	const ddt operator[] (const key_type& x) const
	{
		iterator witr;

		init_itr(witr);
		open_itr(witr);
		
		// x is supposed to be in this map.
		if (witr.move_to(x) != 0) {
			THROW0(NoSuchKeyException);
			
		}
		return witr.curpair_.second._DB_STL_value();
	}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin functions that erase elements from the container.
	//
	// Can not reopen external/outside iterator's cursor, pos must
	// already be in a transactional context.
	// There is identical function in db_multimap<> and db_multiset
	// for this function, we MUST keep the code consistent on update!
	// Go to db_multimap<>::erase(const key_type&) to see why.
	/// \name Erase Functions
	/// \sa http://www.cplusplus.com/reference/stl/map/erase/
	//@{
	/// Erase a key/data pair at specified position.
	/// \param pos An valid iterator of this container to erase.
	inline void erase (iterator pos) 
	{
		if (pos == end())
			return;
		pos.pcsr_->del();
	}

	/// Erase elements by key.
	/// All key/data pairs with specified key x will be removed from 
	/// underlying database.
	/// This function supports auto-commit.
	/// \param x The key to remove from the container.
	/// \return The number of key/data pairs removed.
	// There is identical function in db_multimap<> and db_multiset
	// for this function, we MUST keep the code consistent on update!
	// Go to db_multimap<>::erase(const key_type&) to see why.
	//
	size_type erase (const key_type& x) 
	{
		size_type cnt;
		iterator itr;

		this->begin_txn();
		try {
			pair<iterator, iterator> rg = equal_range(x);
			for (itr = rg.first, cnt = 0; itr != rg.second; ++itr) {
				cnt++;
				itr.pcsr_->del();
			}
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
		return cnt;
	}

	// Can not be auto commit because first and last are already open.
	// There is identical function in db_multimap<> and db_multiset
	// for this function, we MUST keep the code consistent on update!
	// Go to db_multimap<>::erase(const key_type&) to see why.
	/// Range erase. Erase all key/data pairs within the valid range 
	/// [first, last).
	/// \param first The closed boundary of the range.
	/// \param last The open boundary of the range.
	inline void erase (iterator first, iterator last) 
	{
		iterator i;

		for (i = first; i != last; ++i)
			i.pcsr_->del();
	}
	//@}
	
	/// Swap content with container mp.
	/// This function supports auto-commit.
	/// \param mp The container to swap content with.
	/// \param b_truncate: See db_vector::swap() for details.
	/// \sa http://www.cplusplus.com/reference/stl/map/swap/ 
	/// db_vector::clear()
	void swap (db_map<kdt, ddt, value_type_sub>& mp, bool b_truncate = true)
	{
		Db *swapdb = NULL;
		std::string dbfname(64, '\0');
		
		verify_db_handles(mp);
		this->begin_txn();
		try {
			swapdb = this->clone_db_config(this->get_db_handle(), 
			    dbfname);
			db_map<kdt, ddt, value_type_sub> tmap(swapdb, 
			    swapdb->get_env(), begin(), end());
			clear(b_truncate);// Clear this db_map<> object.
			typename db_map<kdt, ddt, value_type_sub>::
			    iterator itr1, itr2;
			itr1 = mp.begin();
			itr2 = mp.end();
			insert(itr1, itr2);
			mp.clear(b_truncate);
			itr1 = tmap.begin();
			itr2 = tmap.end();
			mp.insert(itr1, itr2);
			tmap.clear();
			
			swapdb->close(0);
			if (dbfname[0] != '\0') {
				swapdb = new Db(NULL, DB_CXX_NO_EXCEPTIONS);
				swapdb->remove(dbfname.c_str(), NULL, 0);
				swapdb->close(0);
				delete swapdb;
			}
		} catch (...) {
			this->abort_txn();
			throw;
		}	
		this->commit_txn();
	}

	/// Clear contents in this container.
	/// This function supports auto-commit.
	/// \param b_truncate See db_vector::clear(bool) for details.
	/// \sa db_vector::clear(bool)
	void clear (bool b_truncate = true) 
	{
		int ret;
		u_int32_t flag;
		DbEnv *penv = this->get_db_handle()->get_env();

		if (b_truncate) {
			ResourceManager::instance()->close_db_cursors(
			    this->get_db_handle());
			BDBOP2(this->get_db_handle()->truncate(
			    ResourceManager::instance()->current_txn(penv), 
			    NULL, 0), ret, this->abort_txn());
		} else {
			ReadModifyWriteOption brmw(
			    ReadModifyWriteOption::no_read_modify_write());

			BDBOP(penv->get_open_flags(&flag), ret);

			// DB_RMW flag requires locking subsystem started.
			if ((flag & DB_INIT_LOCK) || (flag & DB_INIT_CDB) || 
			    (flag & DB_INIT_TXN))
				brmw = 
				    ReadModifyWriteOption::read_modify_write();
			try {
				// In if branch, truncate is capable of 
				// autocommit internally.
				this->begin_txn();
				erase(begin(brmw, false), end());
				this->commit_txn();
			} catch (...) {
				this->abort_txn();
				throw;
			}
		}
	}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin functions that searches a key in the map.
	/// \name Searching Functions
	/// The following functions are returning iterators, and they by 
	/// default return read-write iterators. If you intend to use the 
	/// returned iterator only to read, you should call the const version
	/// of each function using a const reference to this container.
	/// Using const iterators can potentially promote concurrency a lot.
	/// You can also set the readonly parameter to each non-const version
	/// of the functions to true if you don't use the returned iterator
	/// to write, which also promotes concurrency and overall performance.
	//@{
	/// Find the key/data pair with specified key x. 
	/// \param x The target key to find.
	/// \return The valid const iterator sitting on the key x, or an 
	/// invalid one.
	/// \sa http://www.cplusplus.com/reference/stl/map/find/
	const_iterator find (const key_type& x) const
	{
		const_iterator witr;

		init_itr(witr);
		open_itr(witr, true);
		if (witr.move_to(x)) 
			return ((self *)this)->end();

		return witr;
	}

	/// Find the greatest key less than or equal to x.
	/// \param x The target key to find.
	/// \return The valid const iterator sitting on the key, or an 
	/// invalid one.
	/// \sa http://www.cplusplus.com/reference/stl/map/lower_bound/
	const_iterator lower_bound (const key_type& x) const 
	{
		const_iterator witr;

		init_itr(witr);
		open_itr(witr, true);
		if (witr.move_to(x, DB_SET_RANGE)) 
			return ((self *)this)->end();
		
		return witr;
	}

	/// Find the range within which all keys equal to specified key x.
	/// \param x The target key to find.
	/// \return The range [first, last).
	/// \sa http://www.cplusplus.com/reference/stl/map/equal_range/
	pair<const_iterator, const_iterator>
	equal_range (const key_type& x) const 
	{
		pair<const_iterator,const_iterator> pr;
		const_iterator witr;
		kdt k;

		init_itr(witr);
		open_itr(witr, true);
		if (witr.move_to(x, DB_SET_RANGE)) {
			pr.first = ((self *)this)->end();
			pr.second = ((self *)this)->end();
		} else {
			pr.first = witr;
			// If no duplicate keys, move one next is sufficient.
			if (witr.pcsr_->get_current_key(k) == 0 && k == x)
				++witr;
			pr.second = witr;
		}
		

		return pr;
	}

	/// Find the key/data pair with specified key x. 
	/// \param x The target key to find.
	/// \param readonly Whether the returned iterator is readonly.
	/// \return The valid iterator sitting on the key x, or an 
	/// invalid one.
	/// \sa http://www.cplusplus.com/reference/stl/map/find/
	iterator find (const key_type& x, bool readonly = false) 
	{
		iterator witr;

		init_itr(witr);
		open_itr(witr, readonly);
		if (witr.move_to(x)) 
			return ((self *)this)->end();

		return witr;
	}

	/// Find the greatest key less than or equal to x.
	/// \param x The target key to find.
	/// \param readonly Whether the returned iterator is readonly.
	/// \return The valid iterator sitting on the key, or an 
	/// invalid one.
	/// \sa http://www.cplusplus.com/reference/stl/map/lower_bound/
	iterator lower_bound (const key_type& x, bool readonly = false) 
	{
		iterator witr;

		init_itr(witr);
		open_itr(witr, readonly);
		if (witr.move_to(x, DB_SET_RANGE)) 
			return ((self *)this)->end();
		
		return witr;
	}

	/// Find the range within which all keys equal to specified key x.
	/// \param x The target key to find.
	/// \param readonly Whether the returned iterator is readonly.
	/// \return The range [first, last).
	/// \sa http://www.cplusplus.com/reference/stl/map/equal_range/
	pair<iterator, iterator>
	equal_range (const key_type& x, bool readonly = false) 
	{
		pair<iterator,iterator> pr;
		iterator witr;
		kdt k;

		init_itr(witr);
		open_itr(witr, readonly);
		if (witr.move_to(x, DB_SET_RANGE)) {
			pr.first = ((self *)this)->end();
			pr.second = ((self *)this)->end();
		} else {
			pr.first = witr;
			// If no dup, move one next is sufficient.
			if (witr.pcsr_->get_current_key(k) == 0 && k == x)
				++witr;
			pr.second = witr;
		}
		

		return pr;
	}

	/// Count the number of key/data pairs having specified key x.
	/// \param x The key to count.
	/// \return The number of key/data pairs having x as key within the
	/// container.
	/// \sa http://www.cplusplus.com/reference/stl/map/count/
	size_type count (const key_type& x) const
	{
		int ret;
		const_iterator witr;
		try {
			this->begin_txn();
			init_itr(witr);
			open_itr(witr, true);
			ret = witr.move_to(x);
			this->commit_txn();
			if (ret != 0) 
				return 0;// No such key/data pair.
			// No duplicates, so it must be one, we don't call
			// Dbc::count because we don't have to.
			//
			else
				return 1;
		} catch (...) {
			this->abort_txn();
			throw;
		}
		
	}

	/// Find the least key greater than x.
	/// \param x The target key to find.
	/// \return The valid iterator sitting on the key, or an 
	/// invalid one.
	/// \sa http://www.cplusplus.com/reference/stl/map/upper_bound/
	const_iterator upper_bound (const key_type& x) const
	{
		const_iterator witr;

		init_itr(witr);
		open_itr(witr, true);

		if (witr.move_to(x, DB_SET_RANGE)) 
			return ((self *)this)->end();

		kdt k;

		// x exists in db, and witr.pcsr_ points to x in db.
		if (witr.pcsr_->get_current_key(k) == 0 && k == x)
			++witr;// No dup, so move one next is sufficient.

		return witr;
	}

	/// Find the least key greater than x.
	/// \param x The target key to find.
	/// \param readonly Whether the returned iterator is readonly.
	/// \return The valid iterator sitting on the key, or an 
	/// invalid one.
	/// \sa http://www.cplusplus.com/reference/stl/map/upper_bound/
	iterator upper_bound (const key_type& x, bool readonly = false)
	{
		iterator witr;

		init_itr(witr);
		open_itr(witr, readonly);

		if (witr.move_to(x, DB_SET_RANGE)) 
			return ((self *)this)->end();

		kdt k;

		// x exists in db, and witr.pcsr_ points to x in db.
		if (witr.pcsr_->get_current_key(k) == 0 && k == x)
			++witr;// No dup, so move one next is sufficient.

		return witr;
	}
	//@}
	////////////////////////////////////////////////////////////////

	// Compare function, return true if contents in m1 and m2 are 
	// identical otherwise return false. 
	// Note that we don't require the key-data pairs' order be identical
	// Put into db_map<> rather than global to utilize transactional
	// support.
	/// Map content equality comparison operator.
	/// This function does not rely on key order. For a set of keys S1 in
	/// this container and another set of keys S2 of container m2, if 
	/// set S1 contains S2 and S2 contains S1 (S1 equals to S2) and each
	/// data element of a key K in S1 from this container equals the data
	/// element of K in m2, the two db_map<> containers equal. Otherwise
	/// they are not equal.
	/// \param m2 The other container to compare against.
	/// \return Returns true if they have equal content, false otherwise.
	bool operator==(const db_map<kdt, ddt, value_type_sub>& m2) const
	{
		bool ret;
		const db_map<kdt, ddt, value_type_sub>& m1 = *this;
		COMPARE_CHECK(m2)
		verify_db_handles(m2);
		try {
			this->begin_txn();
			if (m1.size() != m2.size())
				ret = false;
			else {
				typename db_map<kdt, ddt, value_type_sub>::
				    const_iterator i1, i2;
				
				for (i1 = m1.begin(); i1 != m1.end(); ++i1) {
					if (m2.count(i1->first) == 0) {
						ret = false;
						goto exit;
					}
					i2 = m2.find(i1->first);
					if ((i2->second == i1->second) == 
					    false) {
						ret = false;
						goto exit;
					}
				} // for

				ret = true;
			}
exit:
			this->commit_txn();
			return ret;

		} catch (...) {
			this->abort_txn();
			throw;
		}
	// Now that m1 and m2 has the same number of unique elements and all
	// elements of m1 are in m2, thus there can be no element of m2 
	// that dose not belong to m1, so we won't verify each element of 
	// m2 are in m1.
	//
	}

	/// Container unequality comparison operator.
	/// \param m2 The container to compare against.
	/// \return Returns false if equal, true otherwise.
	bool operator!=(const db_map<kdt, ddt, value_type_sub>& m2) const
	{
		return !this->operator ==(m2);
	}

	
protected:
	
	virtual const char* verify_config(Db*dbp, DbEnv* envp) const
	{
		DBTYPE dbtype;
		u_int32_t oflags, sflags;
		int ret;
		const char *err = NULL;
		
		err = db_container::verify_config(dbp, envp);
		if (err)
			return err;

		BDBOP(dbp->get_type(&dbtype), ret);
		BDBOP(dbp->get_open_flags(&oflags), ret);
		BDBOP(dbp->get_flags(&sflags), ret);

		if (dbtype != DB_BTREE && dbtype != DB_HASH)
			err = 
"wrong database type, only DB_BTREE and DB_HASH allowed for db_map<> class";

		if (oflags & DB_TRUNCATE)
			err = 
"do not specify DB_TRUNCATE flag to create a db_map<> object";
		if ((sflags & DB_DUP) || (sflags & DB_DUPSORT))
			err = 
"db_map<> can not be backed by database permitting duplicate keys";
		if (sflags & DB_RECNUM)
			err = "no DB_RECNUM flag allowed in db_map<>";
		
		
		return err;

	}


	typedef ddt mapped_type;
	typedef int (*db_compare_fcn_t)(Db *db, const Dbt *dbt1, 
	    const Dbt *dbt2);
	typedef u_int32_t (*h_hash_fcn_t)
	(Db *, const void *bytes, u_int32_t length);
	typedef db_set_iterator<kdt> db_multiset_iterator_t;

	static bool compare_keys(Db *pdb, const kdt& k1, const kdt& k2) 
	{
		DBTYPE dbtype;
		int ret;
		bool bret;
		u_int32_t sz1, sz2;

		assert(pdb != NULL);
		ret = pdb->get_type(&dbtype);
		assert(ret == 0);
		db_compare_fcn_t comp = NULL;

		if (dbtype == DB_BTREE)
			BDBOP(pdb->get_bt_compare(&comp), ret);
		else // hash
			BDBOP(pdb->get_h_compare(&comp), ret);

		DataItem key1(k1, true), key2(k2, true);
		Dbt &kdbt1 = key1.get_dbt();
		Dbt &kdbt2 = key2.get_dbt();
		sz1 = kdbt1.get_size();
		sz2 = kdbt2.get_size();
		
		if (comp == NULL) {
			ret = memcmp(&k1, &k2, sz1 > sz2 ? sz2 : sz1);
			return (ret == 0) ? (sz1 < sz2) : (ret < 0);
		}
		// Return strict weak ordering.
		bret = (comp(pdb, &kdbt1, &kdbt2) < 0);
		return bret;
	}
	
	void open_itr(db_map_base_iterator<kdt, realddt, ddt>&itr, 
	    bool readonly = false) const
	{	
		u_int32_t oflags = 0;
		int ret;
		DbEnv *penv = this->get_db_handle()->get_env();

		if (!readonly && penv != NULL) {
			BDBOP((penv->get_open_flags(&oflags)) , ret);
			 if ((oflags & DB_INIT_CDB) != 0)
				((self *)this)->set_cursor_open_flags(
				    this->get_cursor_open_flags() | 
				    DB_WRITECURSOR);
		}

		itr.itr_status_ = itr.pcsr_->open((db_container*)this, 
		    this->get_cursor_open_flags());
		itr.owner_ = (db_container*)this;
	}

	void open_itr(const_reverse_iterator
	    &itr, bool readonly = false) const
	{	
		u_int32_t oflags = 0;
		int ret;
		DbEnv *penv = this->get_db_handle()->get_env();

		if (!readonly && penv != NULL) {
			BDBOP((penv->get_open_flags(&oflags)) , ret);
			 if ((oflags & DB_INIT_CDB) != 0)
				((self *)this)->set_cursor_open_flags(
				    this->get_cursor_open_flags() | 
				    DB_WRITECURSOR);
		}
		itr.itr_status_ = itr.pcsr_->open((db_container*)this, 
		    this->get_cursor_open_flags());
		itr.owner_ = (db_container*)this;
	}

	inline void init_itr(db_map_base_iterator<kdt, realddt, ddt> &
	    witr) const {
		typedef DbCursor<kdt, ddt> cursor_type;
		witr.pcsr_.set_cursor(new cursor_type());
		witr.owner_ = (db_container*)this;
	}

	// Do not use begin_txn/commit_txn in non-public(internal) methods, 
	// only wrap in public methods.
	//
	inline void copy_db(db_map<kdt, ddt, value_type_sub, iterator> &x)
	{	 

		// Make sure clear can succeed if there are cursors 
		// open in other threads.
		clear(false);
		insert(x.begin(), x.end());
		
	}

};//db_map
//@}


//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// db_multimap class template definition
//
// This class derives from db_map<>, using many of its methods,
// it also hides some functions that should not be used in
// this class, such as operator[].
//
// The underlying db must allow duplicate.
// iterator_t is default argument, see forward declaration at the
// head of this file.
// iterator_t is default argument, see forward declaration at the
// head of this file
//
/// \ingroup dbstl_containers
//@{
/// This class is the combination of std::multimap and hash_multimap. By 
/// setting database handles as DB_BTREE or DB_HASH type respectively, you
/// will be using an equivalent of std::multimap or hash_multimap respectively.
/// Database(dbp) and environment(penv) handle requirement:
/// The dbp handle must meet the following requirement:
/// 1. Database type should be DB_BTREE or DB_HASH.
/// 2. Either DB_DUP or DB_DUPSORT flag must be set. Note that so far 
/// Berkeley DB does not allow DB_DUPSORT be set and the database is storing
/// identical key/data pairs, i.e. we can't store two (1, 2), (1, 2) pairs 
/// into a database D with DB_DUPSORT flag set, but only can do so with DB_DUP
/// flag set; But we can store a (1, 2) pair and a (1, 3) pair into D with
/// DB_DUPSORT flag set. So if your data set allows DB_DUPSORT flag, you
/// should set it to gain a lot of performance promotion.
/// 3. No DB_RECNUM flag set.
/// 4. No DB_TRUNCATE specified in database open flags.
/// 5. DB_THREAD must be set if you are sharing the database handle across 
/// multiple threads directly, or indirectly by sharing the container object
/// across multiple threads.
/// \param kdt The key data type.
/// \param ddt The data data type. db_multimap stores key/data pairs.
/// \param value_type_sub Do not specify anything if ddt type is a 
/// class/struct type; Otherwise, specify ElementHolder<ddt> to it.
/// \param iterator_t Never specify anything to this type parameter. It is
/// only used internally.
/// \sa db_container db_map
template<Typename kdt, Typename ddt, Typename value_type_sub, 
    Typename iterator_t>
class _exported db_multimap : public db_map<kdt, ddt, 
    value_type_sub, iterator_t> 
{
protected:
	typedef db_multimap<kdt, ddt, value_type_sub, iterator_t> self;
	typedef db_map<kdt, ddt, value_type_sub, iterator_t> base;
public:
	typedef iterator_t iterator;
	typedef typename iterator::const_version const_iterator;
	typedef db_reverse_iterator<iterator, const_iterator> reverse_iterator;
	typedef db_reverse_iterator<const_iterator, iterator> 
	    const_reverse_iterator;
	typedef kdt key_type;
	typedef ddt data_type;
	typedef value_type_sub data_type_wrap;
	typedef pair<kdt, value_type_sub> value_type_wrap;
	typedef pair<kdt, ddt> value_type;
	typedef value_type_wrap* pointer;
	typedef value_type_wrap& reference; 
	typedef const value_type& const_reference; 
	typedef size_t size_type;
	typedef ptrdiff_t difference_type;

	////////////////////////////////////////////////////////////////
	// Begin constructors and destructor
	/// Constructor.
	/// See class detail for handle requirement.
	/// \param dbp The database handle.
	/// \param envp The database environment handle.
	/// \sa db_map::db_map(Db*, DbEnv*) db_vector::db_vector(Db*, DbEnv*)
	explicit db_multimap (Db *dbp = NULL, DbEnv* envp = NULL) : 
	    base(*(new BulkRetrievalOption(
	    BulkRetrievalOption::BulkRetrieval)))
	{
		const char *errmsg;

		this->init_members(dbp, envp);
		this->open_db_handles(dbp, envp, DB_BTREE, DB_CREATE | 
		    DB_THREAD, DB_DUP);
		// We can't call base(dbp, envp) here because it will verify 
		// failed and we can't call db_container directly, it is 
		// illegal to do so.
		if ((errmsg = verify_config(dbp, envp)) != NULL) {
			THROW(InvalidArgumentException, ("Db*", errmsg));
			
		}
		this->set_db_handle_int(dbp, envp);
		this->set_auto_commit(dbp);
	}

	/// Iteration constructor.
	/// Iterates between first and last, setting
	/// a copy of each of the sequence of elements as the content of 
	/// the container object. 
	/// This function supports auto-commit.
	/// See class detail for handle requirement.
	/// \param dbp The database handle.
	/// \param envp The database environment handle.
	/// \param first The closed boundary of the range.
	/// \param last The open boundary of the range.
	/// \sa db_map::db_map(Db*, DbEnv*, InputIterator, InputIterator)
	/// db_vector::db_vector(Db*, DbEnv*)
	//
	template <class InputIterator> 
	db_multimap (Db *dbp, DbEnv* envp, InputIterator first, 
	    InputIterator last) : base(*(new BulkRetrievalOption(
	    BulkRetrievalOption::BulkRetrieval)))
	{
		const char *errmsg;

		this->init_members(dbp, envp);
		this->open_db_handles(dbp, envp, DB_BTREE, DB_CREATE | 
		    DB_THREAD, DB_DUP);
		// Note that we can't call base(dbp, envp) here because it 
		// will verify failed; And we can't call db_container 
		// directly because it is illegal to do so.
		if ((errmsg = verify_config(dbp, envp)) != NULL) {
			THROW(InvalidArgumentException, ("Db*", errmsg));
			
		}
		this->set_db_handle_int(dbp, envp);
		this->set_auto_commit(dbp);


		this->begin_txn();
		try {
			insert(first, last);
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
	}

	/// Copy constructor.
	/// Create an database and insert all key/data pairs in x into this
	/// container. x's data members are not copied.
	/// This function supports auto-commit.
	/// \param x The other container to initialize this container.
	/// \sa db_container(const db_container&) db_map(const db_map&)
	db_multimap (const self& x) : base(*(new BulkRetrievalOption(
	    BulkRetrievalOption::BulkRetrieval)))
	{
		this->init_members(x);
		this->verify_db_handles(x);
		this->set_db_handle_int(this->clone_db_config(
		    x.get_db_handle()), x.get_db_env_handle());
		assert(this->get_db_handle() != NULL);
		
		this->begin_txn();
		try {
			copy_db((self&)x);
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
	}

	virtual ~db_multimap(){}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin functions that modify multimap content, e.g. insert, 
	// erase, assignment and swap.
	//
	/// Container content assignment operator.
	/// This function supports auto-commit.
	/// \param x The other container whose key/data pairs will be inserted
	/// into this container. Old content in this containers are discarded.
	/// \sa http://www.cplusplus.com/reference/stl/multimap/operator=/
	inline const self& operator=(const self&x)
	{
		ASSIGNMENT_PREDCOND(x)
		db_container::operator =(x);
		this->verify_db_handles(x);
		assert(this->get_db_handle() != NULL);
		this->begin_txn();
		try {
			this->copy_db((self &)x);
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
		return x;
		
	}

	/// \name Insert Functions
	/// \sa http://www.cplusplus.com/reference/stl/multimap/insert/
	//@{
	/// Range insertion. Insert a range [first, last) of key/data pairs
	/// into this container.
	/// \param first The closed boundary of the range.
	/// \param last The open boundary of the range.
	template<typename InputIterator>
	void insert (InputIterator first, InputIterator last) 
	{
		InputIterator ii;
		iterator witr;

		this->init_itr(witr);
		this->open_itr(witr);

		for (ii = first; ii != last; ++ii)
			witr.pcsr_->insert(ii->first, ii->second, 
			    DB_KEYLAST);	
	}

	// Compiler can't see the inherited version, unknown why.
	/// Range insertion. Insert a range [first, last) of key/data pairs
	/// into this container.
	/// \param first The closed boundary of the range.
	/// \param last The open boundary of the range.
	inline void insert (const_iterator& first, const_iterator& last) {
		base::insert(first, last);
	}

	// Insert x into this container, the other two versions are 
	// inherited from db_map<> class.
	// Methods returning an iterator or using an iterator as parameter 
	// can not be internally wrapped by 
	// begin/commit_txn because a cursor is inside its transaction, it 
	// must have been closed after transaction commit, and reopen is 
	// unsafe in multithreaded access.
	//
	/// Insert a single key/data pair if the key is not in the container.
	/// \param x The key/data pair to insert.
	/// \return A pair P, if insert OK, i.e. the inserted key wasn't in the
	/// container, P.first will be the iterator sitting on the inserted
	/// key/data pair, and P.second is true; otherwise P.first is an 
	/// invalid iterator and P.second is false.
	inline iterator insert (const value_type& x) 
	{
		iterator witr;

		this->init_itr(witr);
		this->open_itr(witr);
		witr.itr_status_ = witr.pcsr_->insert(x.first, x.second, 
		    DB_KEYLAST);
		witr.refresh(false);
		return witr;
	}
	//@}

	/// Swap content with another multimap container.
	/// This function supports auto-commit.
	/// \param mp The other container to swap content with.
	/// \param b_truncate See db_map::swap() for details.
	/// \sa db_vector::clear()
	void swap (db_multimap<kdt, ddt, value_type_sub>& mp, 
	    bool b_truncate = true)
	{
		Db *swapdb = NULL;
		std::string dbfname(64, '\0');

		this->verify_db_handles(mp);
		this->begin_txn();
		try {
			swapdb = this->clone_db_config(this->get_db_handle(), 
			    dbfname);
			
			db_multimap<kdt, ddt, value_type_sub> tmap(
			    swapdb, swapdb->get_env(),
			    this->begin(), this->end());
			// Clear this db_multimap<> object.
			this->clear(b_truncate);
			typename db_multimap<kdt, ddt, value_type_sub>::
			    iterator mpbitr, mpeitr;

			mpbitr = mp.begin();
			mpeitr = mp.end();
			insert(mpbitr, mpeitr);
			mp.clear(b_truncate);
			mpbitr = tmap.begin();
			mpeitr = tmap.end();
			mp.insert(mpbitr, mpeitr);
			tmap.clear();
			
			swapdb->close(0);
			if (dbfname[0] != '\0') {
				swapdb = new Db(NULL, DB_CXX_NO_EXCEPTIONS);
				swapdb->remove(dbfname.c_str(), NULL, 0);
				swapdb->close(0);
				delete swapdb;
			}
			this->commit_txn();
		} catch (...) {
			this->abort_txn();
			throw;
		}
		
	}

	// This method has identical code to db_map<>::erase(const key_type&), 
	// but we can NOT simply inherit and use 
	// that version because:
	// 1. The db_map<>::erase called equal_range which is overloaded in 
	//	db_multimap, so if we want the inherited erase to call the right
	//	version of equal_range, we have to make equal_range virtual
	// 2.  Making equal_range virtual will make the code not build--- The 
	//	default template parameter can't be replaced by real parameter, 
	//	unknow why.
	// So we have to copy the code from db_map<> to here, and keep the 
	// code consistent on each update.
	// Also, when I copy only this function, I found other erase overloaded 
	// functions also have to be copied from db_map<> to db_multimap and 
	// db_multiset, otherwise the code don't build, so I 
	// finally have to copy all versions of erase functions into db_multiset
	// and db_multimap. When updating an erase function, do update all 
	// three versions.
	/// \name Erase Functions
	/// \sa http://www.cplusplus.com/reference/stl/multimap/erase/
	//@{
	/// Erase elements by key.
	/// All key/data pairs with specified key x will be removed from 
	/// underlying database.
	/// This function supports auto-commit.
	/// \param x The key to remove from the container.
	/// \return The number of key/data pairs removed.
	size_type erase (const key_type& x) 
	{
		size_type cnt;
		iterator itr;

		this->begin_txn();
		try {
			pair<iterator, iterator> rg = equal_range(x);
			for (itr = rg.first, cnt = 0; itr != rg.second; ++itr) {
				cnt++;
				itr.pcsr_->del();
			}
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
		return cnt;
	}

	// Can not reopen external/outside iterator's cursor, pos must
	// already be in a transactional context.
	// There is identical function in db_multimap<> and db_multiset
	// for this function, we MUST keep the code consistent on update!
	// Go to db_multimap<>::erase(const key_type&) to see why.
	//
	/// Erase a key/data pair at specified position.
	/// \param pos An valid iterator of this container to erase.
	inline void erase (iterator pos) 
	{
		if (pos == this->end())
			return;
		pos.pcsr_->del();
	}

	// Can not be auto commit because first and last are already open.
	// There is identical function in db_multimap<> and db_multiset
	// for this function, we MUST keep the code consistent on update!
	// Go to db_multimap<>::erase(const key_type&) to see why.
	//
	/// Range erase. Erase all key/data pairs within the valid range 
	/// [first, last).
	/// \param first The closed boundary of the range.
	/// \param last The open boundary of the range.
	inline void erase (iterator first, iterator last) 
	{

		for (iterator i = first; i != last; ++i)
			i.pcsr_->del();
	}
	//@}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin functions that searches a key in the multimap.
	/// \name Searching Functions
	/// See of db_map's searching functions group for details about 
	/// iterator, function version and parameters.
	/// \sa db_map
	//@{
	/// Find the range within which all keys equal to specified key x.
	/// \param x The target key to find.
	/// \return The range [first, last).
	/// \sa http://www.cplusplus.com/reference/stl/multimap/equal_range/
	pair<const_iterator, const_iterator>
	equal_range (const key_type& x) const
	{
		pair<const_iterator,const_iterator> pr;
		const_iterator witr;
		kdt k;

		this->init_itr(witr);
		this->open_itr(witr, true);
		// Move witr to x if this contains x and return the itr, or if 
		// no x, position witr to the least key greater than x.
		//
		if (witr.move_to(x, DB_SET_RANGE)) {
			pr.first =  ((self *)this)->end();
			pr.second = ((self *)this)->end();
		} else {
			pr.first = witr;
			
			// No dup, so move one next is sufficient.
			if (witr.pcsr_->get_current_key(k) == 0 && k == x)
				witr.next(DB_NEXT_NODUP);
			pr.second = witr;
		}
			
		return pr;
	}

	/// Find the range within which all keys equal to specified key x.
	/// \param x The target key to find.
	/// \param readonly Whether the returned iterator is readonly.
	/// \return The range [first, last).
	/// \sa http://www.cplusplus.com/reference/stl/multimap/equal_range/
	pair<iterator,iterator>
	equal_range (const key_type& x, bool readonly = false) 
	{
		pair<iterator,iterator> pr;
		iterator witr;
		kdt k;

		this->init_itr(witr);
		this->open_itr(witr, readonly);
		// Move witr to x if this contains x and return the itr, or if 
		// no x, position witr to the least key greater than x.
		//
		if (witr.move_to(x, DB_SET_RANGE)) {
			pr.first =  ((self *)this)->end();
			pr.second = ((self *)this)->end();
		} else {
			pr.first = witr;
			
			// No dup, so move one next is sufficient.
			if (witr.pcsr_->get_current_key(k) == 0 && k == x)
				witr.next(DB_NEXT_NODUP);
			pr.second = witr;
		}
			
		return pr;
	}

	/// Find equal range and number of key/data pairs in the range.
	/// This function also returns the number of elements within the 
	/// returned range via the out parameter nelem.
	/// \param x The target key to find.
	/// \param nelem The output parameter to take back the number of 
	/// key/data pair in the returned range.
	/// \sa http://www.cplusplus.com/reference/stl/multimap/equal_range/
	pair<const_iterator, const_iterator>
	equal_range_N (const key_type& x, size_t& nelem) const
	{
		int ret;
		pair<const_iterator,const_iterator> pr;
		size_t stepped;
		const_iterator witr;
		kdt k;

		this->init_itr(witr);
		this->open_itr(witr, true);
		// Move witr to x if this contains x and return the itr, or if 
		// no x, position witr to the least key greater than x.
		//
		if (witr.move_to(x, DB_SET_RANGE)) {
			pr.first = ((self *)this)->end();
			pr.second = ((self *)this)->end();
			nelem = 0;
		} else {
			pr.first = witr;
			if (witr.pcsr_->get_current_key(k) == 0 && k == x) {
				for (stepped = 1, ret = 
				    witr.pcsr_->next(DB_NEXT_DUP); ret == 0;
				    ret = witr.pcsr_->next(DB_NEXT_DUP),
				    stepped += 1)
					;
				pr.second = ++witr;
				nelem = stepped;
			} else {
				pr.second = witr;
				nelem = 0;
			}
		}
		return pr;
	}

	/// Find equal range and number of key/data pairs in the range.
	/// This function also returns the number of elements within the 
	/// returned range via the out parameter nelem.
	/// \param x The target key to find.
	/// \param nelem The output parameter to take back the number of 
	/// key/data pair in the returned range.
	/// \param readonly Whether the returned iterator is readonly.
	/// \sa http://www.cplusplus.com/reference/stl/multimap/equal_range/
	//
	pair<iterator,iterator>
	equal_range_N (const key_type& x, size_t& nelem, 
	    bool readonly = false)
	{
		int ret;
		pair<iterator,iterator> pr;
		size_t stepped;
		iterator witr;
		kdt k;

		this->init_itr(witr);
		this->open_itr(witr, readonly);
		// Move witr to x if this contains x and return the itr, or if 
		// no x, position witr to the least key greater than x.
		//
		if (witr.move_to(x, DB_SET_RANGE)) {
			pr.first = ((self *)this)->end();
			pr.second = ((self *)this)->end();
			nelem = 0;
		} else {
			pr.first = witr;
			if (witr.pcsr_->get_current_key(k) == 0 && k == x) {
				for (stepped = 1, ret = 
				    witr.pcsr_->next(DB_NEXT_DUP); ret == 0;
				    ret = witr.pcsr_->next(DB_NEXT_DUP),
				    stepped += 1)
					;
				pr.second = ++witr;
				nelem = stepped;
			} else {
				pr.second = witr;
				nelem = 0;
			}
		}
		return pr;
	}

	/// Count the number of key/data pairs having specified key x.
	/// \param x The key to count.
	/// \return The number of key/data pairs having x as key within the
	/// container.
	/// \sa http://www.cplusplus.com/reference/stl/multimap/count/
	size_type count (const key_type& x) const
	{
		int ret;
		size_type cnt;
		iterator witr;

		try {
			this->begin_txn();
			this->init_itr(witr);
			this->open_itr(witr, true);
			ret = witr.move_to(x);
			if (ret) 
				cnt = 0;
			else
				cnt = witr.pcsr_->count();
			this->commit_txn();
		} catch (...) {
			this->abort_txn();
			throw;
		}

		return cnt;
	}

	/// Find the least key greater than x.
	/// \param x The target key to find.
	/// \return The valid iterator sitting on the key, or an 
	/// invalid one.
	/// \sa http://www.cplusplus.com/reference/stl/multimap/upper_bound/
	const_iterator upper_bound (
	    const key_type& x) const
	{
		int ret;
		const_iterator witr;

		this->init_itr(witr);
		this->open_itr(witr, true);

		// No key equal to or greater than x.
		if (witr.move_to(x, DB_SET_RANGE)) 
			return ((self *)this)->end();

		kdt k;
		// x exists in db, and witr.pcsr_ points to x in db, 
		// need to move cursor to next different key.
		//
		if (witr.pcsr_->get_current_key(k) == 0 && k == x) 
			ret = witr.next(DB_NEXT_NODUP); 

		return witr;
	}

	/// Find the least key greater than x.
	/// \param x The target key to find.
	/// \param readonly Whether the returned iterator is readonly.
	/// \return The valid iterator sitting on the key, or an 
	/// invalid one.
	/// \sa http://www.cplusplus.com/reference/stl/multimap/upper_bound/
	iterator upper_bound (const key_type& x, bool readonly = false) 
	{
		int ret;
		iterator witr;

		this->init_itr(witr);
		this->open_itr(witr, readonly);

		// No key equal to or greater than x.
		if (witr.move_to(x, DB_SET_RANGE)) 
			return ((self *)this)->end();

		kdt k;
		// x exists in db, and witr.pcsr_ points to x in db, 
		// need to move cursor to next different key.
		//
		if (witr.pcsr_->get_current_key(k) == 0 && k == x) 
			ret = witr.next(DB_NEXT_NODUP); 

		return witr;
	}
	//@}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin functions that compare container content.
	//
	// In hash_multimap this function is a global compare function, 
	// return true if contents in m1 and m2 are identical
	// otherwise return false. But we have multiple reasons to make
	// it a member of db_multimap<>:
	// 1. There need to be a temporary set to store values of a range, and
	//   db_multimap<>  is quite likely to store huge amount of data, 
	//   not suitable to store into std::set, let alone std::set is not 
	//   completely multithread-safe, thus we store them into db_set<>, 
	//   thus we need a temporary db handle, and call 
	//   db_container::clone_db_handle() function to open the db handle.
	// 2. We need the transactional support. Making this function 
	//   autocommit is good to eliminate phantom issues.
	// Note that we don't require the key-data pairs' order be identical,
	// but we assume identical records of keys are adjacent, so that 
	// iteration will go through them all one by one; Also, the records'
	// order of identical keys are unpridictable and irrelivent, so we 
	// should treat values of a equal range a set, and compare two value 
	// sets for equality when comparing a equal range of key X.
	//
	/**
	Returns whether the two containers have identical content.
	This function does not rely on key order. For a set of keys S1 in this
	container and another set of keys S2 of container m2, if set S1 
	contains S2 and S2 contains S1 (S1 equals to S2) and each set of data
	elements of any key K in S1 from this container equals the set of data
	elements of K in m2, the two db_multimap<> containers equal. Otherwise
	they are not equal. Data element set comparison does not rely on order
	either.
	\param m2 The other container to compare against.
	\return Returns true if they are equal, false otherwise.
	*/
	bool operator==(const db_multimap<kdt, ddt, value_type_sub>& m2) const
	{
		
		typedef typename self::const_iterator mm_itr_t;
		
		COMPARE_CHECK(m2)
		
		bool ret = false, retset = false;
		size_t n1, n2;
		int ret2;
		const self &m1 = *this;
		DbTxn *ptxn = NULL;
		DbEnv *penv;
		Db *pdb;
		const char *dbfilename, *dbname;
		const char *pname1, *pname2;
		string name1, name2;
		u_int32_t oflags;

		this->verify_db_handles(m2);
		pdb = this->get_db_handle();
		penv = pdb->get_env();
		try {
			this->begin_txn();
			if (m1.size() != m2.size()) {
				ret = false;
				this->commit_txn();
				return ret;
			}
			BDBOP(pdb->get_dbname(&dbfilename, &dbname), ret2);
			if (dbfilename == NULL)
				pname1 = pname2 = NULL;
			else {

				this->construct_db_file_name(name1);
				this->construct_db_file_name(name2);
				// Make name2 different from name1.
				name2.push_back('2');
				pname1 = name1.c_str();
				pname2 = name2.c_str();
			}

			Db *value_set_db = open_db(penv, 
			    pname1, DB_BTREE, DB_CREATE, 0);

			Db *value_set_db2 = open_db(penv, 
			    pname2, DB_BTREE, DB_CREATE, 0);

			db_set<ddt, value_type_sub> s1(value_set_db, penv), 
				s2(value_set_db2, penv);

			mm_itr_t i1, i11;
			pair<mm_itr_t, mm_itr_t> resrg1, resrg2;
			for (i1 = m1.begin(); 
			    i1 != m1.end();
			    i1 = resrg1.second) {
				
				resrg1 = m1.equal_range_N(i1->first, n1);
				resrg2 = m2.equal_range_N(i1->first, n2);
				if (n1 != n2) {
					ret = false;
					retset = true;
					break;
				}
					
				if (n2 == 1 && !(resrg2.first->second == 
				    resrg1.first->second)) {
					ret = false;
					retset = true;
					break;
				}
				
				for (i11 = resrg1.first; i11 != resrg1.second;
				    ++i11) 
					s1.insert(i11->second);

				for (i11 = resrg2.first; i11 != resrg2.second;
				    ++i11)
					s2.insert(i11->second);
				if (!(s1 == s2)) {
					ret = false;
					retset = true;
					break;
				}
				s1.clear();
				s2.clear();

				// Skip all equal keys in the range.
				
				
			} // for

			if (!retset) // Care: there are breaks in the for loop.
				ret = true;

			close_db(value_set_db);
			close_db(value_set_db2);

			ptxn = this->current_txn();
			BDBOP(penv->get_open_flags(&oflags), ret2);
			// The transaction handle in CDS is not a real 
			// transaction.
			if (oflags & DB_INIT_CDB)
				ptxn = NULL;
			if (name1.length() > 0)
				BDBOP2(penv->dbremove(ptxn, name1.c_str(), 
				    NULL, 0), ret2, this->abort_txn());
			if (name2.length() > 0)
				BDBOP2(penv->dbremove(ptxn, name2.c_str(), 
				    NULL, 0), ret2, this->abort_txn());

			this->commit_txn();
			return ret;

		} catch (...) {
			this->abort_txn();
			throw;
		}
	// Now that m1 and m2 has the same number of unique elements and all 
	// elements of m1 are in m2, thus there can be no element of m2 that
	// dose not belong to m1, so we won't verify each element of m2 are
	// in m1.
	//
		
	} // operator==

	/// Container unequality comparison operator.
	/// \param m2 The container to compare against.
	/// \return Returns false if equal, true otherwise.
	bool operator!=(const db_multimap<kdt, ddt, value_type_sub>& m2) const
	{
		return !this->operator==(m2);
	}
	////////////////////////////////////////////////////////////////
	
protected:
	typedef ddt mapped_type;
	typedef value_type_sub tkpair;
	typedef int (*bt_compare_fcn_t)(Db *db, const Dbt *dbt1, 
	    const Dbt *dbt2);

	friend class db_map_iterator<kdt, _DB_STL_set_value<kdt>, 
	    value_type_sub>;
	friend class db_map_iterator<kdt, ddt, value_type_sub>;

	db_multimap(BulkRetrievalOption &opt) : base(opt){}
private:
	value_type_sub operator[] (const key_type& x)
	{
		THROW(NotSupportedException, ("db_multimap<>::operator[]"));
	}

	value_type_sub operator[] (const key_type& x) const
	{
		THROW(NotSupportedException, ("db_multimap<>::operator[]"));
		
	}
	
	virtual const char* verify_config(Db*dbp, DbEnv* envp) const
	{
		DBTYPE dbtype;
		u_int32_t oflags, sflags;
		int ret;
		const char *err = NULL;

		err = db_container::verify_config(dbp, envp);
		if (err)
			return err;

		BDBOP(dbp->get_type(&dbtype), ret);
		BDBOP(dbp->get_open_flags(&oflags), ret);
		BDBOP(dbp->get_flags(&sflags), ret);

		if (dbtype != DB_BTREE && dbtype != DB_HASH)
			err = 
"wrong database type, only DB_BTREE and DB_HASH allowed for db_map<> class";
		if (oflags & DB_TRUNCATE)
			err = 
"do not specify DB_TRUNCATE flag to create a db_map<> object";

		// Can't go without no dup or dupsort flag set.
		if (!((sflags & DB_DUP) || (sflags & DB_DUPSORT)))
			err = 
"db_multimap<> can not be backed by database not permitting duplicate keys";
		
		if (sflags & DB_RECNUM)
			err = "no DB_RECNUM flag allowed in db_map<>";
		
		return err;
		
	}

	inline void copy_db(db_multimap<kdt, ddt, value_type_sub> &x)
	{	 

		// Make sure clear can succeed if there are cursors 
		// open in other threads.
		this->clear(false);
		insert(x.begin(), x.end());
		
	}

};// db_multimap<>
//@} //dbstl_containers
END_NS

#endif // !_DB_STL_DB_MAP_H_