Debian Codegraph @

lang/cxx/stl/dbstl_set.h

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 * $Id$
7 */
8
9 #ifndef _DB_STL_DB_SET_H_
10 #define _DB_STL_DB_SET_H_
11
12
13 #include "dbstl_common.h"
14 #include "dbstl_map.h"
15 #include "dbstl_dbc.h"
16 #include "dbstl_container.h"
17 #include "dbstl_resource_manager.h"
18 #include "dbstl_element_ref.h"
19 #include "dbstl_base_iterator.h"
20
21 START_NS(dbstl)
22
23 using std::pair;
24 using std::make_pair;
25
26 /////////////////////////////////////////////////////////////////////////
27 /////////////////////////////////////////////////////////////////////////
28 //
29 // _DB_STL_set_value class template definition
30 // This class is used for db_set as value type because it inherits from
31 // db_map . It dose not need a byte for DB_HASH to work, hash db can store
32 // duplicated keys with empty data.
33 //
34 template <Typename T>
35 class _DB_STL_set_value
36 {
37 public:
38
39 inline _DB_STL_set_value(const ElementRef<T>&){ }
40 inline _DB_STL_set_value(const T&){}
41 inline _DB_STL_set_value(){}
42 };
43
44 /** \ingroup dbstl_iterators
45 @{
46 \defgroup dbset_iterators Iterator classes for db_set and db_multiset.
47 db_set_base_iterator and db_set_iterator are the const iterator and iterator
48 class for db_set and db_multiset. They have identical behaviors to
49 std::set::const_iterator and std::set::iterator respectively.
50
51 The difference between the two classes is that the db_set_base_iterator
52 can only be used to read its referenced value, while db_set_iterator allows
53 both read and write access. If the access pattern is readonly, it is strongly
54 recommended that you use the const iterator because it is faster and more
55 efficient.
56
57 The two classes inherit several functions from db_map_base_iterator and
58 db_map_iterator respectively.
59 \sa db_map_base_iterator db_map_iterator
60
61 @{
62 */
63
64 /////////////////////////////////////////////////////////////////////////
65 /////////////////////////////////////////////////////////////////////////
66 //
67 // db_set_base_iterator class template definition
68 //
69 // db_set_base_iterator class template is the const iterator class for db_set
70 // and db_multiset, it can only be used to read data.
71 // This class need to override operator* and operator-> because the
72 // db_set<>::value_type is different from db_map<>::value_type. We also has
73 // to copy the iterator movement operators into this class because the "self"
74 // type is different in db_map_base_iterator and this class.
75 // In db_set_base_iterator's inherited curpair_base_ pair, we store key to
76 // its first and second member, to work with db_map and db_multimap.
77 // Besides this, there is no further difference, so we can still safely make
78 // use of its inherited methods.
79 //
80 template <Typename kdt>
81 class _exported db_set_base_iterator : public
82 db_map_base_iterator<kdt, kdt, _DB_STL_set_value<kdt> >
83 {
84 protected:
85 typedef db_set_base_iterator<kdt> self;
86 typedef db_map_base_iterator<kdt, kdt, _DB_STL_set_value<kdt> > base;
87 using db_base_iterator<kdt>::replace_current_key;
88 public:
89
90 typedef kdt key_type;
91 typedef _DB_STL_set_value<kdt> ddt;
92 typedef kdt value_type;
93 typedef value_type& reference;
94 typedef value_type* pointer;
95 typedef value_type value_type_wrap;
96 typedef ptrdiff_t difference_type;
97 typedef difference_type distance_type;
98 // We have to use std iterator tags to match the parameter list of
99 // stl internal functions, so we don't need to write our own tag
100 // classes
101 //
102 typedef std::bidirectional_iterator_tag iterator_category;
103
104 ////////////////////////////////////////////////////////////////
105 // Begin constructors and destructor definitions.
106 //
107 /// \name Constructors and destructor
108 /// Do not use these constructors to create iterators, but call
109 /// db_set::begin() const or db_multiset::begin() const to create
110 /// valid iterators.
111 //@{
112 /// Destructor.
113 virtual ~db_set_base_iterator()
114 {
115
116 }
117
118 /// Constructor.
119 /// \param powner The container which creates this iterator.
120 /// \param b_bulk_retrieval The bulk read buffer size. 0 means
121 /// bulk read disabled.
122 /// \param brmw Whether set DB_RMW flag in underlying cursor.
123 /// \param directdbget Whether do direct database get rather than
124 /// using key/data values cached in the iterator whenever read.
125 /// \param b_read_only Whether open a read only cursor. Only effective
126 /// when using Berkeley DB Concurrent Data Store.
127 explicit db_set_base_iterator(db_container*powner,
128 u_int32_t b_bulk_retrieval = 0, bool brmw = false,
129 bool directdbget = true, bool b_read_only = false)
130 : base(powner, b_bulk_retrieval, brmw, directdbget, b_read_only)
131 {
132 this->is_set_ = true;
133 }
134
135 /// Default constructor, dose not create the cursor for now.
136 db_set_base_iterator() : base()
137 {
138 this->is_set_ = true;
139 }
140
141 /// Copy constructor.
142 /// \param s The other iterator of the same type to initialize this.
143 db_set_base_iterator(const db_set_base_iterator&s) : base(s)
144 {
145 this->is_set_ = true;
146 }
147
148 /// Base copy constructor.
149 /// \param bo Initialize from a base class iterator.
150 db_set_base_iterator(const base& bo) : base(bo)
151 {
152 this->is_set_ = true;
153 }
154 //@}
155 ////////////////////////////////////////////////////////////////
156
157 ////////////////////////////////////////////////////////////////
158 // Begin functions that shift iterator positions.
159 /// \name Iterator movement operators.
160 /// These functions are identical to those of db_map_base_iterator
161 /// and db_map_iterator and db_set_iterator. Actually the iterator
162 /// movement functions in the four classes are the same.
163 //@{
164 // But we have to copy them into the four classes because the
165 // return type, namely "self" is different in each class.
166 /// Post-increment.
167 /// \return This iterator after incremented.
168 /// \sa db_map_base_iterator::operator++()
169 inline self& operator++()
170 {
171 this->next();
172
173 return *this;
174 }
175
176 /// Pre-increment.
177 /// \return Another iterator having the old value of this iterator.
178 /// \sa db_map_base_iterator::operator++(int)
179 inline self operator++(int)
180 {
181 self itr = *this;
182
183 this->next();
184
185 return itr;
186 }
187
188 /// Post-decrement.
189 /// \return This iterator after decremented.
190 /// \sa db_map_base_iterator::operator--()
191 inline self& operator--()
192 {
193 this->prev();
194
195 return *this;
196 }
197
198 /// Pre-decrement.
199 /// \return Another iterator having the old value of this iterator.
200 /// \sa db_map_base_iterator::operator--(int)
201 self operator--(int)
202 {
203 self itr = *this;
204 this->prev();
205
206 return itr;
207 }
208 //@}
209 ////////////////////////////////////////////////////////////////
210
211 ////////////////////////////////////////////////////////////////
212 // Begin functions that retrieve values from iterator.
213 //
214 // This function returns a read only reference, you can only read
215 // its referenced data, not update it.
216 // curpair_base_ is always kept updated on iterator movement, but it
217 // is also updated here before making use of the value it points
218 // to, which is stored in curpair_base_ .
219 // Even if this iterator is invalid, this call is allowed, the
220 // default value of type T is returned.
221 //
222 /// \name Functions that retrieve values from iterator.
223 //@{
224 /// Dereference operator.
225 /// Return the reference to the cached data element, which is an
226 /// object of type T. You can only use the return value to read
227 /// its referenced data element, can not update it.
228 /// \return Current data element reference object, i.e. ElementHolder
229 /// or ElementRef object.
230 inline reference operator*()
231 {
232 int ret;
233
234 if (this->directdb_get_) {
235 ret = this->pcsr_->get_current_key(
236 this->curpair_base_.first);
237 dbstl_assert(ret == 0);
238 // curpair_base_.second is a _DB_STL_set_value<kdt> object,
239 // not used at all. Since const iterators can't be used to
240 // write, so this is not a problem.
241 }
242 // Returning reference, no copy construction.
243 return this->curpair_base_.first;
244 }
245
246 // curpair_base_ is always kept updated on iterator movement, but it
247 // is also updated here before making use of the value it points
248 // to, which is stored in curpair_base_ .
249 // Even if this iterator is invalid, this call is allowed, the
250 // default value of type T is returned.
251 /// Arrow operator.
252 /// Return the pointer to the cached data element, which is an
253 /// object of type T. You can only use the return value to read
254 /// its referenced data element, can not update it.
255 /// \return Current data element reference object's address, i.e.
256 /// address of ElementHolder or ElementRef object.
257 inline pointer operator->() const
258 {
259 int ret;
260
261 if (this->directdb_get_) {
262 ret = this->pcsr_->get_current_key(
263 this->curpair_base_.first);
264 dbstl_assert(ret == 0);
265 }
266
267 return &(this->curpair_base_.first);
268 }
269 //@}
270 ////////////////////////////////////////////////////////////////
271
272 /// \brief Refresh iterator cached value.
273 /// \param from_db If not doing direct database get and this parameter
274 /// is true, we will retrieve data directly from db.
275 /// \sa db_base_iterator::refresh(bool)
276 virtual int refresh(bool from_db = true) const
277 {
278
279 if (from_db && !this->directdb_get_)
280 this->pcsr_->update_current_key_data_from_db(
281 DbCursorBase::SKIP_NONE);
282 this->pcsr_->get_current_key(this->curpair_base_.first);
283 this->curpair_base_.second = this->curpair_base_.first;
284 return 0;
285 }
286
287 protected:
288
289 // Declare friend classes to access protected/private members,
290 // while expose to outside only methods in stl specifications.
291 //
292 friend class db_set<kdt>;
293
294 friend class db_map<kdt, _DB_STL_set_value<kdt>,
295 ElementHolder<kdt>, self>;
296 friend class db_map<kdt, _DB_STL_set_value<kdt>,
297 ElementRef<kdt>, self>;
298
299 typedef pair<kdt, value_type> curpair_type;
300
301 }; // db_set_base_iterator
302
303
304 /////////////////////////////////////////////////////////////////////////
305 /////////////////////////////////////////////////////////////////////////
306 //
307 // db_set_iterator class template definition
308 //
309 // db_set_iterator class template is the iterator class for db_set and
310 // db_multiset, it can be used to update its referenced key/data pair.
311 // This class need to override operator* and operator-> because the
312 // db_set<>::value_type is different from db_map<>::value_type. besides
313 // this, there is no further difference, so we can still safely make
314 // use of db_set inherited methods.
315 //
316 template <Typename kdt, Typename value_type_sub>
317 class _exported db_set_iterator :
318 public db_map_iterator<kdt, _DB_STL_set_value<kdt>, value_type_sub>
319 {
320 protected:
321 typedef db_set_iterator<kdt, value_type_sub> self;
322 typedef db_map_iterator<kdt, _DB_STL_set_value<kdt>,
323 value_type_sub> base;
324
325 public:
326
327 typedef kdt key_type;
328 typedef _DB_STL_set_value<kdt> ddt;
329 typedef kdt value_type;
330 typedef value_type_sub value_type_wrap;
331 typedef value_type_sub& reference;
332 typedef value_type_sub* pointer;
333 typedef ptrdiff_t difference_type;
334 typedef difference_type distance_type;
335 // We have to use std iterator tags to match the parameter list of
336 // stl internal functions, so we don't need to write our own tag
337 // classes
338 //
339 typedef std::bidirectional_iterator_tag iterator_category;
340
341 ////////////////////////////////////////////////////////////////
342 // Begin constructors and destructor definitions.
343 /// \name Constructors and destructor
344 /// Do not use these constructors to create iterators, but call
345 /// db_set::begin() or db_multiset::begin() to create valid ones.
346 //@{
347 /// Destructor.
348 virtual ~db_set_iterator()
349 {
350
351 }
352
353 /// Constructor.
354 /// \param powner The container which creates this iterator.
355 /// \param b_bulk_retrieval The bulk read buffer size. 0 means
356 /// bulk read disabled.
357 /// \param brmw Whether set DB_RMW flag in underlying cursor.
358 /// \param directdbget Whether do direct database get rather than
359 /// using key/data values cached in the iterator whenever read.
360 /// \param b_read_only Whether open a read only cursor. Only effective
361 /// when using Berkeley DB Concurrent Data Store.
362 explicit db_set_iterator(db_container*powner,
363 u_int32_t b_bulk_retrieval = 0, bool brmw = false,
364 bool directdbget = true, bool b_read_only = false)
365 : base(powner, b_bulk_retrieval, brmw, directdbget, b_read_only)
366 {
367 this->is_set_ = true;
368 }
369
370 /// Default constructor, dose not create the cursor for now.
371 db_set_iterator() : base()
372 {
373 this->is_set_ = true;
374 }
375
376 /// Copy constructor.
377 /// \param s The other iterator of the same type to initialize this.
378 db_set_iterator(const db_set_iterator&s) : base(s)
379 {
380 this->is_set_ = true;
381 }
382
383 /// Base copy constructor.
384 /// \param bo Initialize from a base class iterator.
385 db_set_iterator(const base& bo) : base(bo)
386 {
387 this->is_set_ = true;
388 }
389
390 /// Sibling copy constructor.
391 /// Note that this class does not derive from db_set_base_iterator
392 /// but from db_map_iterator.
393 /// \param bs Initialize from a base class iterator.
394 db_set_iterator(const db_set_base_iterator<kdt>&bs) : base(bs)
395 {
396 this->is_set_ = true;
397 }
398 //@}
399 ////////////////////////////////////////////////////////////////
400
401 ////////////////////////////////////////////////////////////////
402 // Begin functions that shift iterator positions.
403 /// \name Iterator movement operators.
404 //@{
405 /// Pre-increment.
406 /// Identical to those of db_map_iterator.
407 /// \return This iterator after incremented.
408 /// \sa db_map_iterator::operator++()
409 inline self& operator++()
410 {
411 this->next();
412
413 return *this;
414 }
415
416 /// Post-increment.
417 /// \return Another iterator having the old value of this iterator.
418 /// \sa db_map_iterator::operator++(int)
419 inline self operator++(int)
420 {
421 self itr = *this;
422
423 this->next();
424
425 return itr;
426 }
427
428 /// Pre-decrement.
429 /// \return This iterator after decremented.
430 /// \sa db_map_iterator::operator--()
431 inline self& operator--()
432 {
433 this->prev();
434
435 return *this;
436 }
437
438 /// Post-decrement
439 /// \return Another iterator having the old value of this iterator.
440 /// \sa db_map_iterator::operator--(int)
441 self operator--(int)
442 {
443 self itr = *this;
444 this->prev();
445
446 return itr;
447 }
448 //@}
449 ////////////////////////////////////////////////////////////////
450
451 ////////////////////////////////////////////////////////////////
452 // Begin functions that retrieve values from iterator.
453 //
454 // This functions returns a read-write reference to its data element.
455 // Even if this iterator is invalid, this call is allowed, the
456 // default value of type T is returned.
457 /// \name Functions that retrieve values from iterator.
458 //@{
459 /// Dereference operator.
460 /// Return the reference to the cached data element, which is an
461 /// ElementRef<T> object if T is a class type or an ElementHolder<T>
462 /// object if T is a C++ primitive data type.
463 /// \return Current data element reference object, i.e. ElementHolder
464 /// or ElementRef object.
465 inline reference operator*()
466 {
467
468 if (this->directdb_get_)
469 refresh(true);
470 // returning reference, no copy construction
471 return this->curpair_.second;
472 }
473
474 // curpair_ is always kept updated on iterator movement, but it
475 // is also updated here before making use of the value it points
476 // to, which is stored in curpair_ .
477 // Even if this iterator is invalid, this call is allowed, the
478 // default value of type T is returned.
479 /// Arrow operator.
480 /// Return the pointer to the cached data element, which is an
481 /// ElementRef<T> object if T is a class type or an ElementHolder<T>
482 /// object if T is a C++ primitive data type.
483 /// \return Current data element reference object's address, i.e.
484 /// address of ElementHolder or ElementRef object.
485 inline pointer operator->() const
486 {
487
488 if (this->directdb_get_)
489 refresh(true);
490
491 return &(this->curpair_.second);
492 }
493 //@}
494 ////////////////////////////////////////////////////////////////
495
496 /// \brief Refresh iterator cached value.
497 /// \param from_db If not doing direct database get and this parameter
498 /// is true, we will retrieve data directly from db.
499 /// \sa db_base_iterator::refresh(bool)
500 virtual int refresh(bool from_db = true) const
501 {
502
503 if (from_db && !this->directdb_get_)
504 this->pcsr_->update_current_key_data_from_db(
505 DbCursorBase::SKIP_NONE);
506 this->pcsr_->get_current_key(this->curpair_.first);
507 this->curpair_.second._DB_STL_CopyData(this->curpair_.first);
508 this->set_curpair_base(this->curpair_.first, this->curpair_.first);
509 return 0;
510 }
511
512 protected:
513 typedef pair<kdt, value_type_sub> curpair_type;
514 typedef db_set_base_iterator<kdt> const_version;
515 // Declare friend classes to access protected/private members,
516 // while expose to outside only methods in stl specifications.
517 //
518 friend class db_set<kdt, ElementHolder<kdt> >;
519 friend class db_set<kdt, ElementRef<kdt> >;
520 friend class db_multiset<kdt, ElementHolder<kdt> >;
521 friend class db_multiset<kdt, ElementRef<kdt> >;
522 friend class db_map<kdt, _DB_STL_set_value<kdt>,
523 ElementHolder<kdt>, self>;
524 friend class db_multimap<kdt, _DB_STL_set_value<kdt>,
525 ElementHolder<kdt>, self>;
526 friend class db_map<kdt, _DB_STL_set_value<kdt>,
527 ElementRef<kdt>, self>;
528 friend class db_multimap<kdt, _DB_STL_set_value<kdt>,
529 ElementRef<kdt>, self>;
530
531 virtual void delete_me() const
532 {
533 delete this;
534 }
535
536 }; // db_set_iterator
537 //@} // dbset_iterators
538 //@} // dbstl_iterators
539
540 /////////////////////////////////////////////////////////////////////////
541 /////////////////////////////////////////////////////////////////////////
542 //
543 // db_set class template definition
544 //
545 // db_set<> inherits from db_map<>, storing nothing data, because
546 // we only need the key. It uses db_set_iterator<>/db_set_base_iterator
547 // as its iterator and const iterator respectively.
548 // The only difference between db_set<> and db_map<> classes
549 // is the value_type, so we redefine the insert function by directly copying
550 // the db_map<>::insert, in order to make use of the newly defined
551 // value_type of db_set. If typedef could be dynamically bound, we won't
552 // have to make this duplicated effort.
553 /// \ingroup dbstl_containers
554 /// This class is the combination of std::set and hash_set. By setting
555 /// database handles of DB_BTREE or DB_HASH type, you will be using the
556 /// equivalent of std::set or hash_set. This container stores the key in the
557 /// key element of a key/data pair in the underlying database,
558 /// but doesn't store anything in the data element.
559 /// Database and environment handle requirement:
560 /// The same as that of db_map.
561 /// \param kdt The key data type.
562 /// \param value_type_sub If kdt is a class/struct type, do not specify
563 /// anything in this parameter; Otherwise specify ElementHolder<kdt>.
564 /// \sa db_map db_container
565 //
566 template <Typename kdt, Typename value_type_sub>
567 class _exported db_set : public db_map<kdt, _DB_STL_set_value<kdt>,
568 value_type_sub, db_set_iterator<kdt, value_type_sub> >
569 {
570 protected:
571 typedef db_set<kdt, value_type_sub> self;
572 public:
573 typedef db_set_iterator<kdt, value_type_sub> iterator;
574 typedef typename iterator::const_version const_iterator;
575 typedef db_reverse_iterator<iterator, const_iterator> reverse_iterator;
576 typedef db_reverse_iterator<const_iterator, iterator>
577 const_reverse_iterator;
578 typedef kdt key_type;
579 typedef ptrdiff_t difference_type;
580 // the ElementRef should store key value because key is also data,
581 // and *itr is key and data value
582 //
583 typedef kdt value_type;
584 typedef value_type_sub value_type_wrap;
585 typedef value_type_sub* pointer;
586 typedef value_type_sub& reference;
587 typedef value_type& const_reference;
588 typedef size_t size_type;
589
590 ////////////////////////////////////////////////////////////////
591 // Begin constructors and destructor.
592 /// \name Constructors and destructor
593 //@{
594 /// Create a std::set/hash_set equivalent associative container.
595 /// See the handle requirement in class details to pass correct
596 /// database/environment handles.
597 /// \param dbp The database handle.
598 /// \param envp The database environment handle.
599 /// \sa db_map(Db*, DbEnv*) db_container(Db*, DbEnv*)
600 explicit db_set (Db *dbp = NULL, DbEnv* envp = NULL) : base(dbp, envp){
601 // There are some special handling in db_map_iterator<>
602 // if owner is set.
603 this->set_is_set(true);
604 }
605
606 /// Iteration constructor. Iterates between first and last,
607 /// copying each of the elements in the range into
608 /// this container.
609 /// Create a std::set/hash_set equivalent associative container.
610 /// Insert a range of elements into the database. The range is
611 /// [first, last), which contains elements that can
612 /// be converted to type ddt automatically.
613 /// This function supports auto-commit.
614 /// See the handle requirement in class details to pass correct
615 /// database/environment handles.
616 /// \param dbp The database handle.
617 /// \param envp The database environment handle.
618 /// \param first The closed boundary of the range.
619 /// \param last The open boundary of the range.
620 /// \sa db_map(Db*, DbEnv*, InputIterator, InputIterator)
621 template <class InputIterator>
622 db_set (Db *dbp, DbEnv* envp, InputIterator first,
623 InputIterator last) : base(*(new BulkRetrievalOption(
624 BulkRetrievalOption::BulkRetrieval)))
625 {
626
627 const char *errmsg;
628
629 this->init_members(dbp, envp);
630 this->open_db_handles(dbp, envp, DB_BTREE,
631 DB_CREATE | DB_THREAD, 0);
632 if ((errmsg = this->verify_config(dbp, envp)) != NULL) {
633 THROW(InvalidArgumentException, ("Db*", errmsg));
634 }
635 this->set_db_handle_int(dbp, envp);
636 this->set_auto_commit(dbp);
637 this->begin_txn();
638 try {
639 insert(first, last);
640 } catch (...) {
641 this->abort_txn();
642 throw;
643 }
644 this->commit_txn();
645 // There are some special handling in db_map_iterator<>
646 // if owner is set.
647 this->set_is_set(true);
648
649 }
650
651 /// Copy constructor.
652 /// Create a database and insert all key/data pairs in x into this
653 /// container. x's data members are not copied.
654 /// This function supports auto-commit.
655 /// \param x The source container to initialize this container.
656 /// \sa db_map(const db_map&) db_container(const db_container&)
657 db_set(const self& x) : base(*(new BulkRetrievalOption(
658 BulkRetrievalOption::BulkRetrieval)))
659 {
660 this->init_members(x);
661 this->verify_db_handles(x);
662 this->set_db_handle_int(this->clone_db_config(
663 x.get_db_handle()), x.get_db_env_handle());
664 assert(this->get_db_handle() != NULL);
665
666 this->begin_txn();
667 try {
668 copy_db((self&)x);
669 } catch (...) {
670 this->abort_txn();
671 throw;
672 }
673 this->commit_txn();
674 // There are some special handling in db_map_iterator<>
675 // if owner is set.
676 this->set_is_set(true);
677 }
678
679 virtual ~db_set(){}
680 //@}
681 ////////////////////////////////////////////////////////////////
682
683 /// Container content assignment operator.
684 /// This function supports auto-commit.
685 /// \param x The source container whose key/data pairs will be
686 /// inserted into the target container. Old content in the target
687 /// container is discarded.
688 /// \return The container x's reference.
689 /// \sa http://www.cplusplus.com/reference/stl/set/operator=/
690 const self& operator=(const self& x)
691 {
692 ASSIGNMENT_PREDCOND(x)
693 db_container::operator =(x);
694 this->verify_db_handles(x);
695 assert(this->get_db_handle() != NULL);
696 this->begin_txn();
697 try {
698 this->copy_db((self &)x);
699 } catch (...) {
700 this->abort_txn();
701 throw;
702 }
703 this->commit_txn();
704 return x;
705 }
706
707 ////////////////////////////////////////////////////////////////
708 // Begin key/value compare classes/functions.
709 //
710 // key_compare class definition, it is defined as an inner class,
711 // using underlying btree/hash db's compare function. It is the same
712 // as that of db_map<>, but because we have to redefine value_compare
713 // in this class, gcc forces us to also define key_compare again.
714 class key_compare
715 {
716 private:
717 Db*pdb;
718 public:
719 key_compare(Db*pdb1)
720 {
721 pdb = pdb1;
722 }
723 bool operator()(const kdt& k1, const kdt& k2) const
724 {
725 return db_set::compare_keys(pdb, k1, k2);
726 }
727
728 }; // key_compare class definition
729
730 class value_compare
731 {
732 key_compare kc;
733 public:
734 value_compare(Db*pdb) : kc(pdb)
735 {
736
737 }
738
739 bool operator()(const value_type& v1,
740 const value_type& v2) const
741 {
742
743 return kc(v1, v2);
744 }
745
746 }; // value_compare class definition
747
748 /// Get value comparison functor.
749 /// \return The value comparison functor.
750 /// \sa http://www.cplusplus.com/reference/stl/set/value_comp/
751 inline value_compare value_comp() const
752 {
753 return value_compare(this->get_db_handle());
754 }
755 ////////////////////////////////////////////////////////////////
756
757 ////////////////////////////////////////////////////////////////
758 // Begin insert functions and swap function.
759 //
760 // Note that when secondary index is enabled, each db_container
761 // can create a db_multimap secondary container, but the insert
762 // function is not functional.
763 //
764 // Insert functions. Note that stl requires if the entry with x.key
765 // already exists, insert should not overwrite that entry and the
766 // insert should fail; but bdb Dbc::cursor(DB_KEYLAST) will replace
767 // existing data with new one, so we will first find whether we
768 // have this data, if have, return false;
769 /// \name Insert Functions
770 /// \sa http://www.cplusplus.com/reference/stl/set/insert/
771 //@{
772 /// Insert a single key/data pair if the key is not in the container.
773 /// \param x The key/data pair to insert.
774 /// \return A pair P, if insert OK, i.e. the inserted key wasn't in the
775 /// container, P.first will be the iterator positioned on the inserted
776 /// key/data pair, and P.second is true; otherwise P.first is an invalid
777 /// iterator equal to that returned by end() and P.second is false.
778 pair<iterator,bool> insert (const value_type& x )
779 {
780 pair<iterator,bool> ib;
781
782 _DB_STL_set_value<kdt> sv;
783 iterator witr;
784
785 this->init_itr(witr);
786 this->open_itr(witr);
787
788 if (witr.move_to(x) == 0) {// has it
789 ib.first = witr;
790 ib.second = false;
791
792 return ib;
793 }
794 witr.itr_status_ = witr.pcsr_->insert(x, sv, DB_KEYLAST);
795 witr.refresh(false);
796 ib.first = witr;
797 ib.second = true;
798
799 return ib;
800 }
801
802 // NOT_AUTOCOMMIT_TAG
803 // Note that if first and last are on same db as this container,
804 // then insert may deadlock if there is no common transaction context
805 // for first and witr (when they are on the same page).
806 // The insert function in base class can not be directly used,
807 // got compile errors.
808 /// Range insertion. Insert a range [first, last) of key/data pairs
809 /// into this container.
810 /// \param first The closed boundary of the range.
811 /// \param last The open boundary of the range.
812 inline void insert (const_iterator& first, const_iterator& last)
813 {
814 const_iterator ii;
815 _DB_STL_set_value<kdt> v;
816 iterator witr;
817
818 this->init_itr(witr);
819 this->open_itr(witr);
820
821 for (ii = first; ii != last; ++ii)
822 witr.pcsr_->insert(*ii, v, DB_KEYLAST);
823 }
824
825 /// Range insertion. Insert a range [first, last) of key/data pairs
826 /// into this container.
827 /// \param first The closed boundary of the range.
828 /// \param last The open boundary of the range.
829 void insert (iterator& first, iterator& last)
830 {
831 iterator ii, witr;
832 _DB_STL_set_value<kdt> d;
833
834 this->init_itr(witr);
835 this->open_itr(witr);
836
837 for (ii = first; ii != last; ++ii)
838 witr.pcsr_->insert(*ii, d, DB_KEYLAST);
839
840 }
841
842 // Ignore the position parameter because bdb knows better
843 // where to insert.
844 /// Insert with hint position. We ignore the hint position because
845 /// Berkeley DB knows better where to insert.
846 /// \param position The hint position.
847 /// \param x The key/data pair to insert.
848 /// \return The iterator positioned on the inserted key/data pair,
849 /// or an invalid iterator if the key was already in the container.
850 inline iterator insert ( iterator position, const value_type& x )
851 {
852 pair<iterator,bool> ib = insert(x);
853 return ib.first;
854 }
855
856 /// Range insertion. Insert a range [first, last) of key/data pairs
857 /// into this container.
858 /// \param first The closed boundary of the range.
859 /// \param last The open boundary of the range.
860 template <class InputIterator>
861 void insert (InputIterator first, InputIterator last)
862 {
863 InputIterator ii;
864 _DB_STL_set_value<kdt> v;
865 iterator witr;
866
867 this->init_itr(witr);
868 this->open_itr(witr);
869
870 for (ii = first; ii != last; ++ii)
871 witr.pcsr_->insert(*ii, v, DB_KEYLAST);
872
873 }
874 //@}
875
876 /// Swap content with another container.
877 /// This function supports auto-commit.
878 /// \param mp The container to swap content with.
879 /// \param b_truncate See db_vector::swap's b_truncate parameter
880 /// for details.
881 /// \sa db_map::swap() db_vector::clear()
882 void swap (db_set<kdt, value_type_sub>& mp, bool b_truncate = true)
883 {
884 Db *swapdb = NULL;
885 std::string dbfname(64, '\0');
886
887 this->verify_db_handles(mp);
888 this->begin_txn();
889 try {
890 swapdb = this->clone_db_config(this->get_db_handle(),
891 dbfname);
892
893 db_set<kdt, value_type_sub> tmap(swapdb,
894 swapdb->get_env(), this->begin(), this->end());
895 typename db_set<kdt, value_type_sub>::
896 iterator itr1, itr2;
897
898 this->clear(b_truncate);// Clear this db_map<> object.
899 itr1 = mp.begin();
900 itr2 = mp.end();
901 this->insert(itr1, itr2);
902 mp.clear(b_truncate);
903 itr1 = tmap.begin();
904 itr2 = tmap.end();
905 mp.insert(itr1, itr2);
906 // tmap has no opened cursor, so simply truncate.
907 tmap.clear();
908
909 swapdb->close(0);
910 if (dbfname[0] != '\0') {
911 swapdb = new Db(NULL, DB_CXX_NO_EXCEPTIONS);
912 swapdb->remove(dbfname.c_str(), NULL, 0);
913 swapdb->close(0);
914 delete swapdb;
915 }
916 this->commit_txn();
917 } catch (...) {
918 this->abort_txn();
919 throw;
920 }
921
922 }
923 ////////////////////////////////////////////////////////////////
924
925 ////////////////////////////////////////////////////////////////
926 // Begin container comparison functions.
927 //
928 // Return true if contents in m1 and m2 are identical otherwise
929 // return false.
930 //
931 // Note that we don't require the keys' order be identical, we are
932 // doing mathmatical set comparisons.
933 /// Set content equality comparison operator.
934 /// Return if the two containers have identical content. This function
935 /// does not rely on key order.
936 /// Two sets A and B are equal if and only if A contains B and B
937 /// contains A.
938 /// \param m2 The container to compare against.
939 /// \return Returns true if the two containers are equal,
940 /// false otherwise.
941 //
942 bool operator==(const db_set<kdt, value_type_sub>& m2) const
943 {
944 bool ret;
945
946 COMPARE_CHECK(m2)
947 this->verify_db_handles(m2);
948 const db_set<kdt, value_type_sub>& m1 = *this;
949
950 try {
951 this->begin_txn();
952 if (m1.size() != m2.size())
953 ret = false;
954 else {
955 typename db_set<kdt, value_type_sub>::
956 const_iterator i1;
957
958 for (i1 = m1.begin(); i1 != m1.end(); ++i1) {
959 if (m2.count(*i1) == 0) {
960 ret = false;
961 goto exit;
962 }
963
964 } // for
965 ret = true;
966 } // else
967 exit:
968 this->commit_txn();
969 // Now that m1 and m2 has the same number of unique elements and
970 // all elements of m1 are in m2, thus there can be no element of m2
971 // that dose not belong to m1, so we won't verify each element of
972 // m2 are in m1.
973 return ret;
974 } catch (...) {
975 this->abort_txn();
976 throw;
977 }
978 }// operator==
979
980 /// Inequality comparison operator.
981 bool operator!=(const db_set<kdt, value_type_sub>& m2) const
982 {
983 return !this->operator==(m2);
984 }
985 ////////////////////////////////////////////////////////////////
986
987 protected:
988 typedef int (*db_compare_fcn_t)(Db *db, const Dbt *dbt1,
989 const Dbt *dbt2);
990
991
992 typedef db_map<kdt, _DB_STL_set_value<kdt>, value_type_sub,
993 db_set_iterator<kdt, value_type_sub> > base;
994 private:
995
996 value_type_sub operator[] (const key_type& x)
997 {
998 THROW(NotSupportedException, ("db_set<>::operator[]"));
999
1000 }
1001
1002 value_type_sub operator[] (const key_type& x) const
1003 {
1004 THROW(NotSupportedException, ("db_set<>::operator[]"));
1005
1006 }
1007
1008 inline void copy_db(db_set<kdt, value_type_sub> &x)
1009 {
1010
1011 // Make sure clear can succeed if there are cursors
1012 // open in other threads.
1013 this->clear(false);
1014 insert(x.begin(), x.end());
1015
1016 }
1017
1018 }; // db_set<>
1019
1020
1021 /////////////////////////////////////////////////////////////////////////
1022 /////////////////////////////////////////////////////////////////////////
1023 //
1024 // db_multiset class template definition
1025 //
1026 // db_multiset<> inherits from db_multimap, storing nothing as data, because
1027 // we only need the key. It uses db_set_iterator<> and db_set_base_iterator<>
1028 // as its iterator and const iterator respectively, so that
1029 // we can safely make use of the inherited methods. The only difference
1030 // is the value_type, so we redefine the insert functions by directly copying
1031 // the db_map<>::insert versions, in order to make use of the newly defined
1032 // value_type of db_multiset. If typedef could be dynamically bound, we won't
1033 // have to make this duplicated effort.
1034 /// \ingroup dbstl_containers
1035 /// This class is the combination of std::multiset and hash_multiset. By
1036 /// setting database handles of DB_BTREE or DB_HASH type respectively, you
1037 /// will be using the equivalent of std::multiset or hash_multiset respectively.
1038 /// This container stores the key in the key element of a key/data pair in
1039 /// the underlying database, but doesn't store anything in the data element.
1040 /// Database and environment handle requirement:
1041 /// The requirement to these handles is the same as that to db_multimap.
1042 /// \param kdt The key data type.
1043 /// \param value_type_sub If kdt is a class/struct type, do not specify
1044 /// anything in this parameter; Otherwise specify ElementHolder<kdt>.
1045 /// \sa db_multimap db_map db_container db_set
1046 //
1047 template <Typename kdt, Typename value_type_sub>
1048 class _exported db_multiset : public db_multimap<kdt, _DB_STL_set_value<kdt>,
1049 value_type_sub, db_set_iterator<kdt, value_type_sub> >
1050 {
1051 protected:
1052 typedef db_multiset<kdt, value_type_sub> self;
1053 public:
1054 typedef db_set_iterator<kdt, value_type_sub> iterator;
1055 typedef typename iterator::const_version const_iterator;
1056 typedef db_reverse_iterator<iterator, const_iterator> reverse_iterator;
1057 typedef db_reverse_iterator<const_iterator, iterator>
1058 const_reverse_iterator;
1059 typedef kdt key_type;
1060 typedef ptrdiff_t difference_type;
1061 // The ElementRef should store key value because key is also data,
1062 // and *itr is key and data value.
1063 //
1064 typedef kdt value_type;
1065 typedef value_type_sub value_type_wrap;
1066 typedef value_type_sub& reference;
1067 typedef const value_type& const_reference;
1068 typedef value_type_sub* pointer;
1069 typedef size_t size_type;
1070
1071 public:
1072 ////////////////////////////////////////////////////////////////
1073 // Begin constructors and destructor.
1074 /// \name Constructors and destructor
1075 //@{
1076 /// Create a std::multiset/hash_multiset equivalent associative
1077 /// container.
1078 /// See the handle requirement in class details to pass correct
1079 /// database/environment handles.
1080 /// \param dbp The database handle.
1081 /// \param envp The database environment handle.
1082 /// \sa db_multimap(Db*, DbEnv*)
1083 explicit db_multiset (Db *dbp = NULL, DbEnv* envp = NULL) :
1084 base(dbp, envp) {
1085 // There are special handling in db_map_iterator<> if owner
1086 // is a set.
1087 this->set_is_set(true);
1088 }
1089
1090 /// Iteration constructor. Iterates between first and last,
1091 /// copying each of the elements in the range into
1092 /// this container.
1093 /// Create a std::multi/hash_multiset equivalent associative container.
1094 /// Insert a range of elements into the database. The range is
1095 /// [first, last), which contains elements that can
1096 /// be converted to type ddt automatically.
1097 /// This function supports auto-commit.
1098 /// See the handle requirement in class details to pass correct
1099 /// database/environment handles.
1100 /// \param dbp The database handle.
1101 /// \param envp The database environment handle.
1102 /// \param first The closed boundary of the range.
1103 /// \param last The open boundary of the range.
1104 /// \sa db_multimap(Db*, DbEnv*, InputIterator, InputIterator)
1105 template <class InputIterator>
1106 db_multiset (Db *dbp, DbEnv* envp, InputIterator first,
1107 InputIterator last) : base(*(new BulkRetrievalOption(
1108 BulkRetrievalOption::BulkRetrieval)))
1109 {
1110
1111 const char *errmsg;
1112
1113 this->init_members(dbp, envp);
1114 this->open_db_handles(dbp, envp, DB_BTREE, DB_CREATE |
1115 DB_THREAD, DB_DUP);
1116 // Note that we can't call base(dbp, envp) here because it
1117 // will verify failed; And we can't call db_container
1118 // directly because it is illegal to do so.
1119 if ((errmsg = verify_config(dbp, envp)) != NULL) {
1120 THROW(InvalidArgumentException, ("Db*", errmsg));
1121
1122 }
1123 this->set_db_handle_int(dbp, envp);
1124 this->set_auto_commit(dbp);
1125
1126 this->begin_txn();
1127 try {
1128 insert(first, last);
1129 } catch (...) {
1130 this->abort_txn();
1131 throw;
1132 }
1133 this->commit_txn();
1134
1135 // There are special handling in db_map_iterator<> if owner
1136 // is a set.
1137 this->set_is_set(true);
1138
1139 }
1140
1141 /// Copy constructor.
1142 /// Create a database and insert all key/data pairs in x into this
1143 /// container. x's data members are not copied.
1144 /// This function supports auto-commit.
1145 /// \param x The source container to initialize this container.
1146 /// \sa db_multimap(const db_multimap&) db_container(const db_container&)
1147 db_multiset(const self& x) : base(*(new BulkRetrievalOption(
1148 BulkRetrievalOption::BulkRetrieval)))
1149 {
1150 this->init_members(x);
1151 this->verify_db_handles(x);
1152 this->set_db_handle_int(this->clone_db_config(
1153 x.get_db_handle()), x.get_db_env_handle());
1154 assert(this->get_db_handle() != NULL);
1155
1156 this->begin_txn();
1157 try {
1158 copy_db((self&)x);
1159 } catch (...) {
1160 this->abort_txn();
1161 throw;
1162 }
1163 this->commit_txn();
1164
1165 // There are special handling in db_map_iterator<> if owner
1166 // is a set.
1167 this->set_is_set(true);
1168 }
1169
1170 virtual ~db_multiset(){}
1171 //@}
1172 ////////////////////////////////////////////////////////////////
1173
1174 ////////////////////////////////////////////////////////////////
1175 // Begin functions that modify the container's content, i.e. insert,
1176 // erase, assignment and swap functions.
1177 //
1178 /// Container content assignment operator.
1179 /// This function supports auto-commit.
1180 /// \param x The source container whose key/data pairs will be
1181 /// inserted into the target container. Old content in the target
1182 /// container is discarded.
1183 /// \return The container x's reference.
1184 /// \sa http://www.cplusplus.com/reference/stl/multiset/operator=/
1185 inline const self& operator=(const self& x)
1186 {
1187 ASSIGNMENT_PREDCOND(x)
1188 db_container::operator =(x);
1189 this->verify_db_handles(x);
1190 assert(this->get_db_handle() != NULL);
1191 this->begin_txn();
1192 try {
1193 this->copy_db((self &)x);
1194 } catch (...) {
1195 this->abort_txn();
1196 throw;
1197 }
1198 this->commit_txn();
1199 return x;
1200 }
1201
1202 // Note that when secondary index is enabled, each db_container
1203 // can create a db_multimap secondary container, but the insert
1204 // function is not functional.
1205 /// \name Insert Functions
1206 /// \sa http://www.cplusplus.com/reference/stl/multiset/insert/
1207 //@{
1208 /// Insert a single key if the key is not in the container.
1209 /// \param x The key to insert.
1210 /// \return An iterator positioned on the newly inserted key. If the
1211 /// key x already exists, an invalid iterator equal to that returned
1212 /// by end() function is returned.
1213 inline iterator insert(const value_type& x )
1214 {
1215 pair<iterator,bool> ib;
1216 _DB_STL_set_value<kdt> sv;
1217 iterator witr;
1218
1219 this->init_itr(witr);
1220 this->open_itr(witr);
1221
1222 witr.itr_status_ = witr.pcsr_->insert(x, sv, DB_KEYLAST);
1223 witr.refresh(false);
1224 return witr;
1225 }
1226
1227 // Ignore the position parameter because bdb knows better
1228 // where to insert.
1229 /// Insert a single key with hint if the key is not in the container.
1230 /// The hint position is ignored because Berkeley DB controls where
1231 /// to insert the key.
1232 /// \param x The key to insert.
1233 /// \param position The hint insert position, ignored.
1234 /// \return An iterator positioned on the newly inserted key. If the
1235 /// key x already exists, an invalid iterator equal to that returned
1236 /// by end() function is returned.
1237 inline iterator insert ( iterator position, const value_type& x )
1238 {
1239 return insert(x);
1240 }
1241
1242 /// Range insertion. Insert a range [first, last) of key/data pairs
1243 /// into this container.
1244 /// \param first The closed boundary of the range.
1245 /// \param last The open boundary of the range.
1246 template <class InputIterator>
1247 void insert (InputIterator first, InputIterator last)
1248 {
1249 InputIterator ii;
1250 _DB_STL_set_value<kdt> v;
1251
1252 iterator witr;
1253
1254 this->init_itr(witr);
1255 this->open_itr(witr);
1256 for (ii = first; ii != last; ++ii)
1257 witr.pcsr_->insert(*ii, v, DB_KEYLAST);
1258
1259 }
1260
1261 // Member function template overload.
1262 // This function is a specialization for the member template version
1263 // dedicated to db_set<>.
1264 //
1265 /// Range insertion. Insert a range [first, last) of key/data pairs
1266 /// into this container.
1267 /// \param first The closed boundary of the range.
1268 /// \param last The open boundary of the range.
1269 void insert (db_set_iterator<kdt, value_type_sub>& first,
1270 db_set_iterator<kdt, value_type_sub>& last)
1271 {
1272 db_set_iterator<kdt, value_type_sub> ii;
1273 iterator witr;
1274 _DB_STL_set_value<kdt> d;
1275
1276 this->init_itr(witr);
1277 this->open_itr(witr);
1278
1279 for (ii = first; ii != last; ++ii)
1280 witr.pcsr_->insert(*ii, d, DB_KEYLAST);
1281
1282
1283 }
1284
1285 // Member function template overload.
1286 // This function is a specialization for the member template version
1287 // dedicated to db_set<>.
1288 //
1289 /// Range insertion. Insert a range [first, last) of key/data pairs
1290 /// into this container.
1291 /// \param first The closed boundary of the range.
1292 /// \param last The open boundary of the range.
1293 void insert (db_set_base_iterator<kdt>& first,
1294 db_set_base_iterator<kdt>& last)
1295 {
1296 db_set_base_iterator<kdt> ii;
1297 iterator witr;
1298 _DB_STL_set_value<kdt> d;
1299
1300 this->init_itr(witr);
1301 this->open_itr(witr);
1302
1303 for (ii = first; ii != last; ++ii)
1304 witr.pcsr_->insert(*ii, d, DB_KEYLAST);
1305 }
1306 //@}
1307
1308 // There is identical function in db_multimap<> and db_multiset
1309 // for this function, we MUST keep the code consistent on update!
1310 // Go to db_multimap<>::erase(const key_type&) to see why.
1311 /// \name Erase Functions
1312 /// \sa http://www.cplusplus.com/reference/stl/multiset/erase/
1313 //@{
1314 /// Erase elements by key.
1315 /// All key/data pairs with specified key x will be removed from
1316 /// the underlying database.
1317 /// This function supports auto-commit.
1318 /// \param x The key to remove from the container.
1319 /// \return The number of key/data pairs removed.
1320 size_type erase (const key_type& x)
1321 {
1322 size_type cnt;
1323 iterator itr;
1324
1325 this->begin_txn();
1326 try {
1327 pair<iterator, iterator> rg = this->equal_range(x);
1328 for (itr = rg.first, cnt = 0;
1329 itr != rg.second; ++itr) {
1330 cnt++;
1331 itr.pcsr_->del();
1332 }
1333 } catch (...) {
1334 this->abort_txn();
1335 throw;
1336 }
1337 this->commit_txn();
1338 return cnt;
1339 }
1340
1341 // Can not reopen external/outside iterator's cursor, pos must
1342 // already have been in a transactional context.
1343 // There is identical function in db_multimap<> and db_multiset
1344 // for this function, we MUST keep the code consistent on update!
1345 // Go to db_multimap<>::erase(const key_type&) to see why.
1346 //
1347 /// Erase a key/data pair at specified position.
1348 /// \param pos A valid iterator of this container to erase.
1349 inline void erase (iterator pos)
1350 {
1351 if (pos == this->end())
1352 return;
1353 pos.pcsr_->del();
1354 }
1355
1356 // Can not be auto commit because first and last are already open.
1357 // There is identical function in db_multimap<> and db_multiset
1358 // for this function, we MUST keep the code consistent on update!
1359 // Go to db_multimap<>::erase(const key_type&) to see why.
1360 //
1361 /// Range erase. Erase all key/data pairs within the valid range
1362 /// [first, last).
1363 /// \param first The closed boundary of the range.
1364 /// \param last The open boundary of the range.
1365 inline void erase (iterator first, iterator last)
1366 {
1367 iterator i;
1368
1369 for (i = first; i != last; ++i)
1370 i.pcsr_->del();
1371 }
1372 //@}
1373
1374 /// Swap content with another container.
1375 /// This function supports auto-commit.
1376 /// \param mp The container to swap content with.
1377 /// \param b_truncate See db_multimap::swap() for details.
1378 /// \sa db_map::swap() db_vector::clear()
1379 void swap (db_multiset<kdt, value_type_sub>& mp, bool b_truncate = true)
1380 {
1381 Db *swapdb = NULL;
1382 std::string dbfname(64, '\0');
1383
1384 this->verify_db_handles(mp);
1385 this->begin_txn();
1386 try {
1387 swapdb = this->clone_db_config(this->get_db_handle(),
1388 dbfname);
1389
1390 db_multiset<kdt, value_type_sub> tmap(swapdb,
1391 swapdb->get_env(), this->begin(), this->end());
1392 this->clear(b_truncate);// Clear this db_map<> object.
1393 typename db_multiset<kdt, value_type_sub>::
1394 iterator itr1, itr2;
1395 itr1 = mp.begin();
1396 itr2 = mp.end();
1397 this->insert(itr1, itr2);
1398 mp.clear(b_truncate);
1399 itr1 = tmap.begin();
1400 itr2 = tmap.end();
1401 mp.insert(itr1, itr2);
1402
1403 tmap.clear();
1404
1405 swapdb->close(0);
1406 if (dbfname[0] != '\0') {
1407 swapdb = new Db(NULL, DB_CXX_NO_EXCEPTIONS);
1408 swapdb->remove(dbfname.c_str(), NULL, 0);
1409 swapdb->close(0);
1410 delete swapdb;
1411 }
1412 this->commit_txn();
1413
1414 } catch (...) {
1415 swapdb->close(0);
1416 this->abort_txn();
1417 throw;
1418 }
1419
1420 }
1421 ////////////////////////////////////////////////////////////////
1422
1423 ////////////////////////////////////////////////////////////////
1424 // Begin container comparison functions.
1425 //
1426 // Compare two db_multiset containers for equality, containers
1427 // containing identical set of keys are considered equal, keys'
1428 // order are not presumed or relied upon by this comparison.
1429 /// Container content equality compare operator.
1430 /// This function does not rely on key order.
1431 /// Two sets A and B are equal if and only if
1432 /// for each and every key K having n occurrences in A, K has n
1433 /// occurrences in B, and for each and every key K` having N
1434 /// occurrences in B, K` has n occurrences in A.
1435 /// \param m2 The container to compare against.
1436 /// \return Returns true if the two containers are equal,
1437 /// false otherwise.
1438 bool operator==(const self& m2) const
1439 {
1440 COMPARE_CHECK(m2)
1441 this->verify_db_handles(m2);
1442
1443 const db_multiset<kdt, value_type_sub> &m1 = *this;
1444 const_iterator i1, i11;
1445 pair<const_iterator, const_iterator> resrg1, resrg2;
1446 size_t n1, n2;
1447 bool ret = false;
1448
1449 try {
1450 this->begin_txn();
1451 if (m1.size() != m2.size())
1452 ret = false;
1453 else {
1454 for (i1 = m1.begin(); i1 != m1.end(); ) {
1455 resrg1 = m1.equal_range_N(*i1, n1);
1456 resrg2 = m2.equal_range_N(*i1, n2);
1457 if (n1 != n2) {
1458 ret = false;
1459 goto exit;
1460 }
1461
1462 // If both is 1, resrg2 may contain no i1->first.
1463 if (n2 == 1 && !(*(resrg2.first) ==
1464 *(resrg1.first))) {
1465 ret = false;
1466 goto exit;
1467 }
1468 // m1 and m2 contains identical set of i1->first key,
1469 // so move on, skip all equal keys in the range.
1470 //
1471 i1 = resrg1.second;
1472
1473 } // for
1474 ret = true;
1475
1476 }// else
1477 exit:
1478 this->commit_txn();
1479 return ret;
1480 } catch (...) {
1481 this->abort_txn();
1482 throw;
1483 }
1484
1485 // Now that m1 and m2 has the same number of unique elements and all
1486 // elements of m1 are in m2, thus there can be no element of m2 that
1487 // dose not belong to m1, so we won't verify each element of m2 are
1488 // in m1.
1489
1490 } // operator==
1491
1492 /// Inequality comparison operator.
1493 bool operator!=(const self& m2) const
1494 {
1495 return !this->operator==(m2);
1496 }
1497 ////////////////////////////////////////////////////////////////
1498
1499 protected:
1500
1501 typedef int (*db_compare_fcn_t)(Db *db, const Dbt *dbt1,
1502 const Dbt *dbt2);
1503 typedef db_multimap<kdt, _DB_STL_set_value<kdt>,
1504 value_type_sub, db_set_iterator<kdt, value_type_sub> > base;
1505
1506 // Declare base our friend, we can't use 'friend class base' to do
1507 // this declaration on gcc.
1508 friend class db_multimap<kdt, _DB_STL_set_value<kdt>,
1509 value_type_sub, db_set_iterator<kdt, value_type_sub> >;
1510 // Declare iterator our friend, we can't use 'friend class iterator'
1511 // to do this declaration on gcc.
1512 friend class db_set_iterator<kdt, value_type_sub>;
1513 friend class db_map_iterator<kdt, _DB_STL_set_value<kdt>,
1514 value_type_sub>;
1515 friend class db_map_iterator<kdt, _DB_STL_set_value<kdt> >;
1516
1517 private:
1518
1519 inline void copy_db(db_multiset<kdt, value_type_sub> &x)
1520 {
1521
1522 // Make sure clear can succeed if there are cursors
1523 // open in other threads.
1524 this->clear(false);
1525 insert(x.begin(), x.end());
1526
1527 }
1528
1529 // Prevent user calling the inherited version.
1530 value_type operator[] (const key_type& x)
1531 {
1532 THROW(NotSupportedException, ("db_multiset<>::operator[]"));
1533
1534 }
1535
1536 value_type operator[] (const key_type& x) const
1537 {
1538 THROW(NotSupportedException, ("db_multiset<>::operator[]"));
1539
1540 }
1541
1542 virtual const char* verify_config(Db*dbp, DbEnv* envp) const
1543 {
1544 DBTYPE dbtype;
1545 u_int32_t oflags, sflags;
1546 int ret;
1547 const char *err = NULL;
1548
1549 err = db_container::verify_config(dbp, envp);
1550 if (err)
1551 return err;
1552
1553 BDBOP(dbp->get_type(&dbtype), ret);
1554 BDBOP(dbp->get_open_flags(&oflags), ret);
1555 BDBOP(dbp->get_flags(&sflags), ret);
1556
1557 if (dbtype != DB_BTREE && dbtype != DB_HASH)
1558 err =
1559 "wrong database type, only DB_BTREE and DB_HASH allowed for db_map<> class";
1560 if (oflags & DB_TRUNCATE)
1561 err =
1562 "do not specify DB_TRUNCATE flag to create a db_map<> object";
1563
1564 // Must set DB_DUP and NOT DB_DUPSORT.
1565 if (!(sflags & DB_DUP) || (sflags & DB_DUPSORT))
1566 err =
1567 "db_multiset<> requires a database with DB_DUP set and without DB_DUPSORT set.";
1568
1569 if (sflags & DB_RECNUM)
1570 err = "no DB_RECNUM flag allowed in db_map<>";
1571
1572 return err;
1573
1574 }
1575
1576 }; // db_multiset<>
1577
1578
1579 END_NS
1580
1581 #endif// !_DB_STL_DB_SET_H_