summaryrefslogtreecommitdiff
path: root/stl/dbstl_set.h
blob: 1aaf9f267811f499ba5b6d34f26c2d66f647f8eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2009 Oracle.  All rights reserved.
 *
 * $Id$
 */

#ifndef _DB_STL_DB_SET_H_
#define _DB_STL_DB_SET_H_


#include "dbstl_common.h"
#include "dbstl_map.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;

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//
// _DB_STL_set_value class template definition
// This class is used for db_set as value type because it inherits from
// db_map . It dose not need a byte for DB_HASH to work, hash db can store
// duplicated keys with empty data.
//
template <Typename T>
class _DB_STL_set_value 
{
public:
	
	inline _DB_STL_set_value(const ElementRef<T>&){ }
	inline _DB_STL_set_value(const T&){}
	inline _DB_STL_set_value(){}
};

/** \ingroup dbstl_iterators
@{
\defgroup dbset_iterators Iterator classes for db_set and db_multiset.
db_set_base_iterator and db_set_iterator are the const iterator and iterator 
class for db_set and db_multiset. They have identical behaviors to 
std::set::const_iterator and std::set::iterator respectively. 

The difference between the two classes is that the db_set_base_iterator 
can only be used to read its referenced value, while db_set_iterator allows
both read and write access. If the access pattern is readonly, it is strongly
recommended that you use the const iterator because it is faster and more 
efficient.

The two classes inherit several functions from db_map_base_iterator and 
db_map_iterator respectively.
\sa db_map_base_iterator db_map_iterator

@{
*/

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//
// db_set_base_iterator class template definition
//
// db_set_base_iterator class template is the const iterator class for db_set
// and db_multiset, it can only be used to read data.
// This class need to override operator* and operator-> because the 
// db_set<>::value_type is different from db_map<>::value_type. We also has
// to copy the iterator movement operators into this class because the "self"
// type is different in db_map_base_iterator and this class.
// In db_set_base_iterator's inherited curpair_base_ pair, we store key to
// its first and second member, to work with db_map and db_multimap.
// Besides this, there is no further difference, so we can still safely make
// use of its inherited methods.
//
template <Typename kdt>
class _exported db_set_base_iterator : public 
    db_map_base_iterator<kdt, kdt, _DB_STL_set_value<kdt> >
{
protected:
	typedef db_set_base_iterator<kdt> self;
	typedef db_map_base_iterator<kdt, kdt, _DB_STL_set_value<kdt> > base;
	using db_base_iterator<kdt>::replace_current_key;
public:
	
	typedef kdt key_type;
	typedef _DB_STL_set_value<kdt> ddt;
	typedef kdt value_type;
	typedef value_type& reference;
	typedef value_type* pointer;
	typedef value_type value_type_wrap;
	typedef ptrdiff_t difference_type;
	typedef difference_type distance_type;
	// We have to use std iterator tags to match the parameter list of
	// stl internal functions, so we don't need to write our own tag 
	// classes
	//
	typedef std::bidirectional_iterator_tag iterator_category;

	////////////////////////////////////////////////////////////////
	// Begin constructors and destructor definitions.
	//
	/// \name Constructors and destructor
	/// Do not use these constructors to create iterators, but call
	/// db_set::begin() const or db_multiset::begin() const to create 
	/// valid iterators.
	//@{
	/// Destructor.
	virtual ~db_set_base_iterator()
	{

	}

	/// 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_set_base_iterator(db_container*powner, 
	    u_int32_t b_bulk_retrieval = 0, bool brmw = false, 
	    bool directdbget = true, bool b_read_only = false)
	    : base(powner, b_bulk_retrieval, brmw, directdbget, b_read_only)
	{
		this->is_set_ = true;
	}

	/// Default constructor, dose not create the cursor for now.
	db_set_base_iterator() : base()
	{
		this->is_set_ = true;
	}

	/// Copy constructor.
	/// \param s The other iterator of the same type to initialize this.
	db_set_base_iterator(const db_set_base_iterator&s) : base(s)
	{
		this->is_set_ = true;
	}

	/// Base copy constructor.
	/// \param bo Initialize from a base class iterator.
	db_set_base_iterator(const base& bo) : base(bo) 
	{
		this->is_set_ = true;
	}
	//@}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin functions that shift iterator positions.
	/// \name Iterator movement operators.
	/// These functions are identical to those of db_map_base_iterator
	/// and db_map_iterator and db_set_iterator. Actually the iterator
	/// movement functions in the four classes are the same.
	//@{
	// But we have to copy them into the four classes because the 
	// return type, namely "self" is different in each class.
	/// Post-increment. 
	/// \return This iterator after incremented.
	/// \sa db_map_base_iterator::operator++()
	inline self& operator++()
	{
		this->next();
		
		return *this;
	}

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

		this->next();
		
		return itr;
	}

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

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

	////////////////////////////////////////////////////////////////
	// Begin functions that retrieve values from iterator.
	//
	// This function returns a read only reference, you can only read
	// its referenced data, not update it.
	// curpair_base_ is always kept updated on iterator movement, but it
	// is also updated here before making use of the value it points
	// to, which is stored in curpair_base_ .
	// Even if this iterator is invalid, this call is allowed, the
	// default value of type T is returned.
	//
	/// \name Functions that retrieve values from iterator.
	//@{
	/// Dereference operator.
	/// Return the reference to the cached data element, which is an 
	/// object of type T. You can only use the return value to read 
	/// its referenced data element, can not update it.
	/// \return Current data element reference object, i.e. ElementHolder
	/// or ElementRef object.
	inline reference operator*()  
	{ 
		int ret;

		if (this->directdb_get_) {
			ret = this->pcsr_->get_current_key(
			    this->curpair_base_.first);
			dbstl_assert(ret == 0);
		// curpair_base_.second is a _DB_STL_set_value<kdt> object, 
		// not used at all. Since const iterators can't be used to 
		// write, so this is not a problem.
		}
		// Returning reference, no copy construction.
		return this->curpair_base_.first;
	}
	 
	// curpair_base_ is always kept updated on iterator movement, but it
	// is also updated here before making use of the value it points
	// to, which is stored in curpair_base_ .
	// 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 
	/// object of type T. You can only use the return value to read 
	/// its referenced data element, can not update it.
	/// \return Current data element reference object's address, i.e. 
	/// address of ElementHolder or ElementRef object.
	inline pointer operator->() const
	{
		int ret;

		if (this->directdb_get_) {
			ret = this->pcsr_->get_current_key(
			    this->curpair_base_.first);
			dbstl_assert(ret == 0);
		}

		return &(this->curpair_base_.first);
	}
	//@}
	////////////////////////////////////////////////////////////////

	/// \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
	{
		
		if (from_db && !this->directdb_get_)
			this->pcsr_->update_current_key_data_from_db(
			    DbCursorBase::SKIP_NONE);
		this->pcsr_->get_current_key(this->curpair_base_.first);
		this->curpair_base_.second = this->curpair_base_.first;
		return 0;
	}

protected:

	// Declare friend classes to access protected/private members,
	// while expose to outside only methods in stl specifications.
	//
	friend class db_set<kdt>;
	
	friend class db_map<kdt, _DB_STL_set_value<kdt>, 
	    ElementHolder<kdt>, self>;
	friend class db_map<kdt, _DB_STL_set_value<kdt>, 
	    ElementRef<kdt>, self>;

	typedef pair<kdt, value_type> curpair_type;

}; // db_set_base_iterator


/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//
// db_set_iterator class template definition
//
// db_set_iterator class template is the iterator class for db_set and 
// db_multiset, it can be used to update its referenced key/data pair.
// This class need to override operator* and operator-> because the 
// db_set<>::value_type is different from db_map<>::value_type. besides
// this, there is no further difference, so we can still safely make
// use of db_set inherited methods.
//
template <Typename kdt, Typename value_type_sub>
class _exported db_set_iterator : 
    public db_map_iterator<kdt, _DB_STL_set_value<kdt>, value_type_sub>
{
protected:
	typedef db_set_iterator<kdt, value_type_sub> self;
	typedef db_map_iterator<kdt, _DB_STL_set_value<kdt>, 
	    value_type_sub> base;
	
public:
	
	typedef kdt key_type;
	typedef _DB_STL_set_value<kdt> ddt;
	typedef kdt value_type;
	typedef value_type_sub value_type_wrap;
	typedef value_type_sub& reference;
	typedef value_type_sub* pointer;
	typedef ptrdiff_t difference_type;
	typedef difference_type distance_type;
	// We have to use std iterator tags to match the parameter list of
	// stl internal functions, so we don't need to write our own tag 
	// classes
	//
	typedef std::bidirectional_iterator_tag iterator_category;
	
	////////////////////////////////////////////////////////////////
	// Begin constructors and destructor definitions.
	/// \name Constructors and destructor
	/// Do not use these constructors to create iterators, but call
	/// db_set::begin() or db_multiset::begin() to create valid ones.
	//@{
	/// Destructor.
	virtual ~db_set_iterator()
	{

	}

	/// 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_set_iterator(db_container*powner, 
	    u_int32_t b_bulk_retrieval = 0, bool brmw = false, 
	    bool directdbget = true, bool b_read_only = false)
	    : base(powner, b_bulk_retrieval, brmw, directdbget, b_read_only)
	{
		this->is_set_ = true;
	}

	/// Default constructor, dose not create the cursor for now.
	db_set_iterator() : base()
	{
		this->is_set_ = true;
	}

	/// Copy constructor.
	/// \param s The other iterator of the same type to initialize this.
	db_set_iterator(const db_set_iterator&s) : base(s)
	{
		this->is_set_ = true;
	}

	/// Base copy constructor.
	/// \param bo Initialize from a base class iterator.
	db_set_iterator(const base& bo) : base(bo) 
	{
		this->is_set_ = true;
	}

	/// Sibling copy constructor.
	/// Note that this class does not derive from db_set_base_iterator
	/// but from db_map_iterator.
	/// \param bs Initialize from a base class iterator.
	db_set_iterator(const db_set_base_iterator<kdt>&bs) : base(bs)
	{
		this->is_set_ = true;
	}
	//@}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin functions that shift iterator positions.
	/// \name Iterator movement operators.
	//@{
	/// Pre-increment.
	/// Identical to those of db_map_iterator.
	/// \return This iterator after incremented.
	/// \sa db_map_iterator::operator++()
	inline self& operator++()
	{
		this->next();
		
		return *this;
	}

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

		this->next();
		
		return itr;
	}

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

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

	////////////////////////////////////////////////////////////////
	// Begin functions that retrieve values from iterator.
	//
	// This functions returns a read-write reference to its data element.
	// Even if this iterator is invalid, this call is allowed, the
	// default value of type T is returned.
	/// \name Functions that retrieve values from iterator.
	//@{
	/// Dereference operator.
	/// Return the reference to the cached data element, which is an 
	/// ElementRef<T> object if T is a class type or an 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*()  
	{ 

		if (this->directdb_get_) 
			refresh(true);
		// returning reference, no copy construction
		return this->curpair_.second;
	}
	 
	// curpair_ is always kept updated on iterator movement, but it
	// is also updated here before making use of the value it points
	// to, which is stored in curpair_ .
	// 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 
	/// ElementRef<T> object if T is a class type or an 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_) 
			refresh(true);

		return &(this->curpair_.second);
	}
	//@}
	////////////////////////////////////////////////////////////////

	/// \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
	{
		
		if (from_db && !this->directdb_get_)
			this->pcsr_->update_current_key_data_from_db(
			    DbCursorBase::SKIP_NONE);
		this->pcsr_->get_current_key(this->curpair_.first);
		this->curpair_.second._DB_STL_CopyData(this->curpair_.first);
		this->set_curpair_base(this->curpair_.first, this->curpair_.first);
		return 0;
	}

protected:
	typedef pair<kdt, value_type_sub> curpair_type;
	typedef db_set_base_iterator<kdt> const_version;
	// Declare friend classes to access protected/private members,
	// while expose to outside only methods in stl specifications.
	//
	friend class db_set<kdt, ElementHolder<kdt> >;
	friend class db_set<kdt, ElementRef<kdt> >;
	friend class db_multiset<kdt, ElementHolder<kdt> >;
	friend class db_multiset<kdt, ElementRef<kdt> >;
	friend class db_map<kdt, _DB_STL_set_value<kdt>, 
	    ElementHolder<kdt>, self>;
	friend class db_multimap<kdt, _DB_STL_set_value<kdt>, 
	    ElementHolder<kdt>, self>;
	friend class db_map<kdt, _DB_STL_set_value<kdt>, 
	    ElementRef<kdt>, self>;
	friend class db_multimap<kdt, _DB_STL_set_value<kdt>, 
	    ElementRef<kdt>, self>;
	
	virtual void delete_me() const
	{
		delete this;
	}

}; // db_set_iterator
//@} // dbset_iterators
//@} // dbstl_iterators

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//
// db_set class template definition
//
// db_set<> inherits from db_map<>, storing nothing data, because
// we only need the key. It uses db_set_iterator<>/db_set_base_iterator
// as its iterator and const iterator respectively.
// The only difference between db_set<> and db_map<> classes
// is the value_type, so we redefine the insert function by directly copying
// the db_map<>::insert, in order to make use of the newly defined 
// value_type of db_set. If typedef could be dynamically bound, we won't
// have to make this duplicated effort.
/// \ingroup dbstl_containers
/// This class is the combination of std::set and hash_set. By setting 
/// database handles of DB_BTREE or DB_HASH type, you will be using the 
/// equivalent of std::set or hash_set. This container stores the key in the
/// key element of a key/data pair in the underlying database, 
/// but doesn't store anything in the data element.
/// Database and environment handle requirement:
/// The same as that of db_map.
/// \param kdt The key data type.
/// \param value_type_sub If kdt is a class/struct type, do not specify 
/// anything in this parameter; Otherwise specify ElementHolder<kdt>.
/// \sa db_map db_container
//
template <Typename kdt, Typename value_type_sub>
class _exported db_set : public db_map<kdt, _DB_STL_set_value<kdt>, 
    value_type_sub, db_set_iterator<kdt, value_type_sub> > 
{
protected:
	typedef db_set<kdt, value_type_sub> self;
public:
	typedef db_set_iterator<kdt, value_type_sub> 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 ptrdiff_t difference_type;
	// the ElementRef should store key value because key is also data, 
	// and *itr is key and data value
	//
	typedef kdt value_type; 
	typedef value_type_sub value_type_wrap;
	typedef value_type_sub* pointer;
	typedef value_type_sub& reference;
	typedef value_type& const_reference;
	typedef size_t size_type;

	////////////////////////////////////////////////////////////////
	// Begin constructors and destructor.
	/// \name Constructors and destructor
	//@{
	/// Create a std::set/hash_set 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_map(Db*, DbEnv*) db_container(Db*, DbEnv*)
	explicit db_set (Db *dbp = NULL, DbEnv* envp = NULL) : base(dbp, envp){
		// There are some special handling in db_map_iterator<> 
		// if owner is set.
		this->set_is_set(true);
	}

	/// Iteration constructor. Iterates between first and last, 
	/// copying each of the elements in the range into 
	/// this container. 
	/// Create a std::set/hash_set 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.
	/// This function supports auto-commit.
	/// See the handle requirement in class details to pass correct
	/// database/environment handles.
	/// \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*, DbEnv*, InputIterator, InputIterator)
	template <class InputIterator> 
	db_set (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, 0);
		if ((errmsg = this->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();
		// There are some special handling in db_map_iterator<> 
		// if owner is set.
		this->set_is_set(true);

	}

	/// Copy constructor.
	/// Create a 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 source container to initialize this container.
	/// \sa db_map(const db_map&) db_container(const db_container&)
	db_set(const self& x) : base(*(new BulkRetrievalOption(
	    BulkRetrievalOption::BulkRetrieval))) 
	{
		this->init_members(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((self&)x);
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
		// There are some special handling in db_map_iterator<> 
		// if owner is set.
		this->set_is_set(true);
	}

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

	/// Container content assignment operator.
	/// This function supports auto-commit.
	/// \param x The source container whose key/data pairs will be 
	/// inserted into the target container. Old content in the target
	/// container is discarded.
	/// \return The container x's reference.
	/// \sa http://www.cplusplus.com/reference/stl/set/operator=/
	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 {
			this->copy_db((self &)x);
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
		return x;
	}
	
	////////////////////////////////////////////////////////////////
	// Begin key/value compare classes/functions.
	//
	// key_compare class definition, it is defined as an inner class,
	// using underlying btree/hash db's compare function. It is the same
	// as that of db_map<>, but because we have to redefine value_compare
	// in this class, gcc forces us to also define key_compare again.
	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
	
	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, v2);
		}

	}; // value_compare class definition

	/// Get value comparison functor.
	/// \return The value comparison functor.
	/// \sa http://www.cplusplus.com/reference/stl/set/value_comp/
	inline value_compare value_comp() const
	{
		return value_compare(this->get_db_handle());
	}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin insert functions and swap function.
	//
	// Note that when secondary index is enabled, each db_container
	// can create a db_multimap secondary container, but the insert 
	// function is not functional.
	//
	// 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;
	/// \name Insert Functions
	/// \sa http://www.cplusplus.com/reference/stl/set/insert/
	//@{
	/// 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 positioned on the inserted
	/// key/data pair, and P.second is true; otherwise P.first is an invalid 
	/// iterator equal to that returned by end() and P.second is false.
	pair<iterator,bool> insert (const value_type& x ) 
	{
		pair<iterator,bool> ib;

		_DB_STL_set_value<kdt> sv;
		iterator witr;

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

		if (witr.move_to(x) == 0) {// has it
			ib.first = witr;
			ib.second = false;

			return ib;
		}
		witr.itr_status_ = witr.pcsr_->insert(x, sv, DB_KEYLAST); 
		witr.refresh(false);
		ib.first = witr;
		ib.second = true;

		return ib;
	}

	// NOT_AUTOCOMMIT_TAG  
	// Note that if first and last are on same db as this container, 
	// then insert may deadlock if there is no common transaction context 
	// for first and witr (when they are on the same page).
	// The insert function in base class can not be directly used, 
	// got compile errors.
	/// 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) 
	{
		const_iterator ii;
		_DB_STL_set_value<kdt> v;
		iterator witr;

		this->init_itr(witr);
		
		this->open_itr(witr);
	
		for (ii = first; ii != last; ++ii) 
			witr.pcsr_->insert(*ii, v, 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.
	void insert (iterator& first, iterator& last) 
	{
		iterator ii, witr;
		_DB_STL_set_value<kdt> d;

		init_itr(witr);
		open_itr(witr);
	
		for (ii = first; ii != last; ++ii)
			witr.pcsr_->insert(*ii, d, DB_KEYLAST);
		
	}

	// Ignore the position parameter because bdb knows better
	// where to insert.
	/// 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 positioned 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;
	}

	/// 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 <class InputIterator>
	void insert (InputIterator first, InputIterator last) 
	{
		InputIterator ii;
		_DB_STL_set_value<kdt> v;
		iterator witr;

		this->init_itr(witr);
	
		this->open_itr(witr);
	
		for (ii = first; ii != last; ++ii) 
			witr.pcsr_->insert(*ii, v, DB_KEYLAST);

	}
	//@}

	/// Swap content with another container.
	/// This function supports auto-commit.
	/// \param mp The container to swap content with.
	/// \param b_truncate See db_vector::swap's b_truncate parameter
	/// for details.
	/// \sa db_map::swap() db_vector::clear()
	void swap (db_set<kdt, 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_set<kdt, value_type_sub> tmap(swapdb, 
			    swapdb->get_env(), this->begin(), this->end());
			typename db_set<kdt, value_type_sub>::
			    iterator itr1, itr2;

			this->clear(b_truncate);// Clear this db_map<> object.
			itr1 = mp.begin();
			itr2 = mp.end();
			this->insert(itr1, itr2);
			mp.clear(b_truncate);
			itr1 = tmap.begin();
			itr2 = tmap.end();
			mp.insert(itr1, itr2);
			// tmap has no opened cursor, so simply truncate.
			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;
		}	
		
	}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin container comparison functions.
	//
	// Return true if contents in m1 and m2 are identical otherwise 
	// return false. 
	// 
	// Note that we don't require the keys' order be identical, we are
	// doing mathmatical set comparisons.
	/// Set content equality comparison operator.
	/// Return if the two containers have identical content. This function
	/// does not rely on key order.
	/// Two sets A and B are equal if and only if A contains B and B 
	/// contains A.
	/// \param m2 The container to compare against.
	/// \return Returns true if the two containers are equal, 
	/// false otherwise.
	//
	bool operator==(const db_set<kdt, value_type_sub>& m2) const
	{
		bool ret;
		
		COMPARE_CHECK(m2)
		verify_db_handles(m2);
		const db_set<kdt, value_type_sub>& m1 = *this;

		try {
			this->begin_txn();
			if (m1.size() != m2.size())
				ret = false;
			else {
				typename db_set<kdt, value_type_sub>::
				    const_iterator i1;

				for (i1 = m1.begin(); i1 != m1.end(); ++i1) {
					if (m2.count(*i1) == 0) {
						ret = false;
						goto exit;
					}
						
				} // for
				ret = true;
			} // else
exit:
			this->commit_txn();
	// 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.
			return ret;
		} catch (...) {
			this->abort_txn();
			throw;
		}
	}// operator==

	/// Inequality comparison operator.
	bool operator!=(const db_set<kdt, value_type_sub>& m2) const
	{
		return !this->operator==(m2);
	}
	////////////////////////////////////////////////////////////////
	
protected:
	typedef int (*db_compare_fcn_t)(Db *db, const Dbt *dbt1, 
	    const Dbt *dbt2);
	
	
	typedef db_map<kdt, _DB_STL_set_value<kdt>, value_type_sub,
	    db_set_iterator<kdt, value_type_sub> > base;
private:

	value_type_sub operator[] (const key_type& x)
	{
		THROW(NotSupportedException, ("db_set<>::operator[]"));
		
	}

	value_type_sub operator[] (const key_type& x) const
	{
		THROW(NotSupportedException, ("db_set<>::operator[]"));
		
	}

	inline void copy_db(db_set<kdt, 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_set<>


/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//
// db_multiset class template definition
//
// db_multiset<> inherits from db_multimap, storing nothing as data, because
// we only need the key. It uses db_set_iterator<> and db_set_base_iterator<>
// as its iterator and const iterator respectively, so that
// we can safely make use of the inherited methods. The only difference
// is the value_type, so we redefine the insert functions by directly copying
// the db_map<>::insert versions, in order to make use of the newly defined 
// value_type of db_multiset. If typedef could be dynamically bound, we won't
// have to make this duplicated effort.
/// \ingroup dbstl_containers
/// This class is the combination of std::multiset and hash_multiset. By 
/// setting database handles of DB_BTREE or DB_HASH type respectively, you 
/// will be using the equivalent of std::multiset or hash_multiset respectively.
/// This container stores the key in the key element of a key/data pair in 
/// the underlying database, but doesn't store anything in the data element.
/// Database and environment handle requirement:
/// The requirement to these handles is the same as that to db_multimap.
/// \param kdt The key data type.
/// \param value_type_sub If kdt is a class/struct type, do not specify 
/// anything in this parameter; Otherwise specify ElementHolder<kdt>.
/// \sa db_multimap db_map db_container db_set
//
template <Typename kdt, Typename value_type_sub>
class _exported db_multiset : public db_multimap<kdt, _DB_STL_set_value<kdt>, 
    value_type_sub, db_set_iterator<kdt, value_type_sub> >
{
protected:
	typedef db_multiset<kdt, value_type_sub> self;
public:
	typedef db_set_iterator<kdt, value_type_sub> 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 ptrdiff_t difference_type;
	// The ElementRef should store key value because key is also data, 
	// and *itr is key and data value.
	//
	typedef kdt value_type; 
	typedef value_type_sub value_type_wrap;
	typedef value_type_sub& reference;
	typedef const value_type& const_reference;
	typedef value_type_sub* pointer;
	typedef size_t size_type;

public:
	////////////////////////////////////////////////////////////////
	// Begin constructors and destructor.
	/// \name Constructors and destructor
	//@{
	/// Create a std::multiset/hash_multiset 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_multimap(Db*, DbEnv*)
	explicit db_multiset (Db *dbp = NULL, DbEnv* envp = NULL) : 
	    base(dbp, envp) {
		// There are special handling in db_map_iterator<> if owner 
		// is a set.
		this->set_is_set(true);
	}

	/// Iteration constructor. Iterates between first and last, 
	/// copying each of the elements in the range into 
	/// this container. 
	/// Create a std::multi/hash_multiset 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.
	/// This function supports auto-commit.
	/// See the handle requirement in class details to pass correct
	/// database/environment handles.
	/// \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_multimap(Db*, DbEnv*, InputIterator, InputIterator)
	template <class InputIterator> 
	db_multiset (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();

		// There are special handling in db_map_iterator<> if owner 
		// is a set.
		this->set_is_set(true);

	}

	/// Copy constructor.
	/// Create a 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 source container to initialize this container.
	/// \sa db_multimap(const db_multimap&) db_container(const db_container&)
	db_multiset(const self& x) : base(*(new BulkRetrievalOption(
	    BulkRetrievalOption::BulkRetrieval))) 
	{
		this->init_members(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((self&)x);
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();

		// There are special handling in db_map_iterator<> if owner 
		// is a set.
		this->set_is_set(true);
	}

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

	////////////////////////////////////////////////////////////////
	// Begin functions that modify the container's content, i.e. insert,
	// erase, assignment and swap functions.
	//
	/// Container content assignment operator.
	/// This function supports auto-commit.
	/// \param x The source container whose key/data pairs will be 
	/// inserted into the target container. Old content in the target 
	/// container is discarded.
	/// \return The container x's reference.
	/// \sa http://www.cplusplus.com/reference/stl/multiset/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 {
			this->copy_db((self &)x);
		} catch (...) {
			this->abort_txn();
			throw;
		}
		this->commit_txn();
		return x;
	}

	// Note that when secondary index is enabled, each db_container
	// can create a db_multimap secondary container, but the insert 
	// function is not functional.
	/// \name Insert Functions
	/// \sa http://www.cplusplus.com/reference/stl/multiset/insert/
	//@{
	/// Insert a single key if the key is not in the container.
	/// \param x The key to insert.
	/// \return An iterator positioned on the newly inserted key. If the
	/// key x already exists, an invalid iterator equal to that returned 
	/// by end() function is returned.
	inline iterator insert(const value_type& x ) 
	{
		pair<iterator,bool> ib;
		_DB_STL_set_value<kdt> sv;
		iterator witr;

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

		witr.itr_status_ = witr.pcsr_->insert(x, sv, DB_KEYLAST);
		witr.refresh(false);
		return witr;
	}

	// Ignore the position parameter because bdb knows better
	// where to insert.
	/// Insert a single key with hint if the key is not in the container.
	/// The hint position is ignored because Berkeley DB controls where
	/// to insert the key.
	/// \param x The key to insert.
	/// \param position The hint insert position, ignored.
	/// \return An iterator positioned on the newly inserted key. If the
	/// key x already exists, an invalid iterator equal to that returned 
	/// by end() function is returned.
	inline iterator insert ( iterator position, const value_type& x ) 
	{
		return insert(x);
	}
	
	/// 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 <class InputIterator>
	void insert (InputIterator first, InputIterator last) 
	{
		InputIterator ii;
		_DB_STL_set_value<kdt> v;

		iterator witr;

		this->init_itr(witr);	
		this->open_itr(witr);
		for (ii = first; ii != last; ++ii) 
			witr.pcsr_->insert(*ii, v, DB_KEYLAST);

	}

	// Member function template overload.
	// This function is a specialization for the member template version
	// dedicated to db_set<>.
	//
	/// 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 (db_set_iterator<kdt, value_type_sub>& first, 
	    db_set_iterator<kdt, value_type_sub>& last) 
	{
		db_set_iterator<kdt, value_type_sub> ii;
		iterator witr;
		_DB_STL_set_value<kdt> d;

		init_itr(witr);
		open_itr(witr);
	
		for (ii = first; ii != last; ++ii)
			witr.pcsr_->insert(*ii, d, DB_KEYLAST);
		

	}

	// Member function template overload.
	// This function is a specialization for the member template version
	// dedicated to db_set<>.
	//
	/// 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 (db_set_base_iterator<kdt>& first, 
	    db_set_base_iterator<kdt>& last) 
	{
		db_set_base_iterator<kdt> ii;
		iterator witr;
		_DB_STL_set_value<kdt> d;

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

	// 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/multiset/erase/
	//@{
	/// Erase elements by key.
	/// All key/data pairs with specified key x will be removed from 
	/// the 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 have been 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 A 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) 
	{
		iterator i;

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

	/// Swap content with another container.
	/// This function supports auto-commit.
	/// \param mp The container to swap content with.
	/// \param b_truncate See db_multimap::swap() for details.
	/// \sa db_map::swap() db_vector::clear()
	void swap (db_multiset<kdt, 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_multiset<kdt, value_type_sub> tmap(swapdb, 
			    swapdb->get_env(), this->begin(), this->end());
			this->clear(b_truncate);// Clear this db_map<> object.
			typename db_multiset<kdt, value_type_sub>::
			    iterator itr1, itr2;
			itr1 = mp.begin();
			itr2 = mp.end();
			this->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;
			}
			this->commit_txn();
			
		} catch (...) {
			swapdb->close(0);
			this->abort_txn();
			throw;
		}	
		
	}
	////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////
	// Begin container comparison functions.
	//
	// Compare two db_multiset containers for equality, containers
	// containing identical set of keys are considered equal, keys'
	// order are not presumed or relied upon by this comparison.
	/// Container content equality compare operator.
	/// This function does not rely on key order.
	/// Two sets A and B are equal if and only if
	/// for each and every key K having n occurrences in A, K has n 
	/// occurrences in B, and for each and every key K` having N
	/// occurrences in B, K` has n occurrences in A.
	/// \param m2 The container to compare against.
	/// \return Returns true if the two containers are equal, 
	/// false otherwise.
	bool operator==(const self& m2) const
	{
		COMPARE_CHECK(m2)
		verify_db_handles(m2);

		const db_multiset<kdt, value_type_sub> &m1 = *this;
		const_iterator i1, i11;
		pair<const_iterator, const_iterator> resrg1, resrg2;
		size_t n1, n2;
		bool ret = false;

		try {
			this->begin_txn();
			if (m1.size() != m2.size())
				ret = false;
			else {
				for (i1 = m1.begin(); i1 != m1.end(); ) {
					resrg1 = m1.equal_range_N(*i1, n1);
					resrg2 = m2.equal_range_N(*i1, n2);
					if (n1 != n2) {
						ret = false;
						goto exit;
					}
						
			// If both is 1, resrg2 may contain no i1->first.
					if (n2 == 1 && !(*(resrg2.first) == 
					    *(resrg1.first))) {
						ret = false;
						goto exit;
					}
			// m1 and m2 contains identical set of i1->first key,
			// so move on, skip all equal keys in the range.
			//
					i1 = resrg1.second; 
						
				} // for
				ret = true;

			}// else
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.

	} // operator==

	/// Inequality comparison operator.	
	bool operator!=(const self& m2) const
	{
		return !this->operator==(m2);
	}
	////////////////////////////////////////////////////////////////

protected:
	
	typedef int (*db_compare_fcn_t)(Db *db, const Dbt *dbt1, 
	    const Dbt *dbt2);
	typedef db_multimap<kdt, _DB_STL_set_value<kdt>, 
	    value_type_sub, db_set_iterator<kdt, value_type_sub> > base;

	// Declare base our friend, we can't use 'friend class base' to do
	// this declaration on gcc.
	friend class db_multimap<kdt, _DB_STL_set_value<kdt>, 
	    value_type_sub, db_set_iterator<kdt, value_type_sub> >; 
	// Declare iterator our friend, we can't use 'friend class iterator'
	// to do this declaration on gcc.
	friend class db_set_iterator<kdt, value_type_sub>; 
	friend class db_map_iterator<kdt, _DB_STL_set_value<kdt>, 
	    value_type_sub>;
	friend class db_map_iterator<kdt, _DB_STL_set_value<kdt> >;

private:

	inline void copy_db(db_multiset<kdt, 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());
		
	}

	// Prevent user calling the inherited version.
	value_type operator[] (const key_type& x)
	{
		THROW(NotSupportedException, ("db_multiset<>::operator[]"));
		
	}

	value_type operator[] (const key_type& x) const
	{
		THROW(NotSupportedException, ("db_multiset<>::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";

		// Must set DB_DUP and NOT DB_DUPSORT.
		if (!(sflags & DB_DUP) || (sflags & DB_DUPSORT))
			err = 
"db_multiset<> requires a database with DB_DUP set and without DB_DUPSORT set.";
		
		if (sflags & DB_RECNUM)
			err = "no DB_RECNUM flag allowed in db_map<>";
		
		return err;
		
	}

}; // db_multiset<>


END_NS

#endif// !_DB_STL_DB_SET_H_