| 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_MAP_H_ |
| 10 |
#define _DB_STL_DB_MAP_H_ |
| 11 |
|
| 12 |
#include <string> |
| 13 |
|
| 14 |
#include "dbstl_common.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 |
using std::string; |
| 26 |
// Forward declarations, DO NOT delete the default argument values |
| 27 |
// because class templates defintions need them. No need for _export here. |
| 28 |
// |
| 29 |
template <Typename T> |
| 30 |
class _DB_STL_set_value; |
| 31 |
|
| 32 |
template <Typename kdt, Typename ddt, Typename value_type_sub = |
| 33 |
ElementRef<ddt> > |
| 34 |
class db_map_iterator; |
| 35 |
|
| 36 |
template <Typename kdt, Typename ddt, Typename value_type_sub = |
| 37 |
ElementRef<ddt>, Typename iterator_t = |
| 38 |
db_map_iterator<kdt, ddt, value_type_sub> > |
| 39 |
class db_map; |
| 40 |
|
| 41 |
template<Typename kdt, Typename ddt, Typename value_type_sub = |
| 42 |
ElementRef<ddt>, Typename iterator_t = |
| 43 |
db_map_iterator<kdt, ddt, value_type_sub> > |
| 44 |
class db_multimap; |
| 45 |
|
| 46 |
template <Typename kdt> |
| 47 |
class db_set_base_iterator; |
| 48 |
|
| 49 |
template <Typename kdt, Typename value_type_sub = ElementRef<kdt> > |
| 50 |
class db_set_iterator; |
| 51 |
|
| 52 |
template <Typename kdt, Typename value_type_sub = ElementRef<kdt> > |
| 53 |
class db_set; |
| 54 |
|
| 55 |
template <Typename kdt, Typename value_type_sub = ElementRef<kdt> > |
| 56 |
class db_multiset; |
| 57 |
|
| 58 |
#if NO_MEMBER_FUNCTION_PARTIAL_SPECIALIZATION |
| 59 |
template <Typename kdt, Typename datadt, Typename value_type_sub> |
| 60 |
void assign_second0(pair<kdt, value_type_sub>& v, const datadt& d) ; |
| 61 |
#endif |
| 62 |
|
| 63 |
/** \ingroup dbstl_iterators |
| 64 |
@{ |
| 65 |
\defgroup db_map_iterators Iterator classes for db_map and db_multimap. |
| 66 |
db_map has two iterator class templates -- db_map_base_iterator and |
| 67 |
db_map_iterator. They are the const iterator class and iterator class for |
| 68 |
db_map and db_multimap. db_map_iterator inherits from db_map_base_iterator. |
| 69 |
|
| 70 |
The two classes have identical behaviors to std::map::const_iterator and |
| 71 |
std::map::iterator respectively. Note that the common public member function |
| 72 |
behaviors are described in the db_base_iterator section. |
| 73 |
|
| 74 |
The differences between the two classes are that the db_map_base_iterator can |
| 75 |
only be used to read its referenced value, while db_map_iterator allows both |
| 76 |
read and write access. If your access pattern is readonly, it is strongly |
| 77 |
recommended that you use the const iterator because it is faster and more |
| 78 |
efficient. |
| 79 |
@{ |
| 80 |
*/ |
| 81 |
////////////////////////////////////////////////////////////////////// |
| 82 |
// db_map_base_iterator class definition |
| 83 |
// |
| 84 |
// This class is a const iterator class for db_map and db_multimap, it can |
| 85 |
// be used only to read data under the iterator, can't be used to write. |
| 86 |
// |
| 87 |
// Iterator const-ness implementation: |
| 88 |
// |
| 89 |
// const iterators can not update key/data pairs, other than this, |
| 90 |
// they can do anything else like non-const iterators, so we define |
| 91 |
// db_map_base_iterator to be the const iterator which can only be used |
| 92 |
// to read its underlying key/data pair, but not updating them; We |
| 93 |
// derive the db_map_iterator from the base iterator to be the |
| 94 |
// read-write iterator. We also maintain a "readonly" property in all |
| 95 |
// iterators so that users can specify a db_map_iterator to be |
| 96 |
// read only. db_map_base_iterator is more efficient to read data then |
| 97 |
// db_map_iterator, so read only accesses are strongly recommended to be |
| 98 |
// done using a const iterator. |
| 99 |
// |
| 100 |
template <Typename kdt, Typename ddt, Typename csrddt = ddt> |
| 101 |
class _exported db_map_base_iterator : public |
| 102 |
db_base_iterator<ddt> |
| 103 |
{ |
| 104 |
protected: |
| 105 |
typedef db_map_base_iterator<kdt, ddt, csrddt> self; |
| 106 |
typedef db_base_iterator<ddt> base; |
| 107 |
using base::replace_current_key; |
| 108 |
public: |
| 109 |
typedef kdt key_type; |
| 110 |
typedef ddt data_type; |
| 111 |
typedef pair<kdt, ddt> value_type; |
| 112 |
// Not used in this class, but required to satisfy |
| 113 |
// db_reverse_iterator type extraction. |
| 114 |
typedef ptrdiff_t difference_type; |
| 115 |
typedef difference_type distance_type; |
| 116 |
typedef value_type& reference; |
| 117 |
typedef value_type* pointer; |
| 118 |
typedef value_type value_type_wrap; |
| 119 |
// We have to use standard iterator tags to match the parameter |
| 120 |
// list of stl internal functions, we can't use our own tag |
| 121 |
// classes, so we don't write tag classes in dbstl. |
| 122 |
// |
| 123 |
typedef std::bidirectional_iterator_tag iterator_category; |
| 124 |
|
| 125 |
//////////////////////////////////////////////////////////////////// |
| 126 |
// |
| 127 |
// Begin public constructors and destructor. |
| 128 |
/// @name Constructors and destructor |
| 129 |
/// Do not create iterators directly using these constructors, but |
| 130 |
/// call db_map::begin or db_multimap_begin to get instances of |
| 131 |
/// this class. |
| 132 |
/// \sa db_map::begin() db_multimap::begin() |
| 133 |
//@{ |
| 134 |
/// Copy constructor. |
| 135 |
/// \param vi The other iterator of the same type to initialize this. |
| 136 |
db_map_base_iterator(const self& vi) |
| 137 |
: db_base_iterator<ddt>(vi) |
| 138 |
{ |
| 139 |
// Lazy-dup another cursor, cursor to iterator mapping |
| 140 |
// is 1 to 1. |
| 141 |
pcsr_ = vi.pcsr_; |
| 142 |
curpair_base_.first = vi.curpair_base_.first; |
| 143 |
curpair_base_.second = vi.curpair_base_.second; |
| 144 |
} |
| 145 |
|
| 146 |
/// Base copy constructor. |
| 147 |
/// \param vi Initialize from a base class iterator. |
| 148 |
db_map_base_iterator(const base& vi) : base(vi), |
| 149 |
pcsr_(new cursor_type(vi.get_bulk_retrieval(), |
| 150 |
vi.is_rmw(), vi.is_directdb_get())) |
| 151 |
{ |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
/// Constructor. |
| 156 |
/// \param powner The container which creates this iterator. |
| 157 |
/// \param b_bulk_retrieval The bulk read buffer size. 0 means |
| 158 |
/// bulk read disabled. |
| 159 |
/// \param rmw Whether set DB_RMW flag in underlying cursor. |
| 160 |
/// \param directdbget Whether do direct database get rather than |
| 161 |
/// using key/data values cached in the iterator whenever read. |
| 162 |
/// \param readonly Whether open a read only cursor. Only effective |
| 163 |
/// when using Berkeley DB Concurrent Data Store. |
| 164 |
explicit db_map_base_iterator(db_container*powner, |
| 165 |
u_int32_t b_bulk_retrieval = 0, bool rmw = false, |
| 166 |
bool directdbget = true, bool readonly = false) |
| 167 |
: db_base_iterator<ddt>( |
| 168 |
powner, directdbget, readonly, b_bulk_retrieval, rmw), |
| 169 |
pcsr_(new cursor_type(b_bulk_retrieval, rmw, directdbget)) |
| 170 |
{ |
| 171 |
} |
| 172 |
|
| 173 |
/// Default constructor, dose not create the cursor for now. |
| 174 |
db_map_base_iterator() |
| 175 |
{ |
| 176 |
} |
| 177 |
|
| 178 |
// Use virtual because ElementRef<> uses a db_base_iterator* pointer |
| 179 |
// to refer to the iterator, and also use "dead_" flag to avoid |
| 180 |
// multiple calls to the same destructor by ~ElementRef<>(). |
| 181 |
/// Destructor. |
| 182 |
virtual ~db_map_base_iterator() |
| 183 |
{ |
| 184 |
this->dead_ = true; |
| 185 |
if (pcsr_) |
| 186 |
pcsr_->close(); |
| 187 |
} |
| 188 |
//@} |
| 189 |
|
| 190 |
//////////////////////////////////////////////////////////////////// |
| 191 |
|
| 192 |
//////////////////////////////////////////////////////////////////// |
| 193 |
// Begin functions that shift iterator position. |
| 194 |
// |
| 195 |
// Do not throw exceptions here because it is likely and normal |
| 196 |
// to iterate to the "end iterator". |
| 197 |
// |
| 198 |
/// @name Iterator increment movement functions. |
| 199 |
/// The two functions moves the iterator one element backward, so that |
| 200 |
/// the element it sits on has a bigger key. The btree/hash key |
| 201 |
/// comparison routine determines which key is greater. |
| 202 |
/// Use ++iter rather than iter++ where possible to avoid two useless |
| 203 |
/// iterator copy constructions. |
| 204 |
//@{ |
| 205 |
/// Pre-increment. |
| 206 |
/// \return This iterator after incremented. |
| 207 |
inline self& operator++() |
| 208 |
{ |
| 209 |
next(); |
| 210 |
|
| 211 |
return *this; |
| 212 |
} |
| 213 |
|
| 214 |
/// Post-increment. |
| 215 |
/// \return Another iterator having the old value of this iterator. |
| 216 |
inline self operator++(int) |
| 217 |
{ |
| 218 |
self itr = *this; |
| 219 |
|
| 220 |
next(); |
| 221 |
|
| 222 |
return itr; |
| 223 |
} |
| 224 |
//@} |
| 225 |
|
| 226 |
/// @name Iterator decrement movement functions. |
| 227 |
/// The two functions moves the iterator one element forward, so that |
| 228 |
/// the element it sits on has a smaller key. The btree/hash key |
| 229 |
/// comparison routine determines which key is greater. |
| 230 |
/// Use --iter rather than iter-- where possible to avoid two useless |
| 231 |
/// iterator copy constructions. |
| 232 |
//@{ |
| 233 |
/// Pre-decrement. |
| 234 |
/// \return This iterator after decremented. |
| 235 |
inline self& operator--() |
| 236 |
{ |
| 237 |
prev(); |
| 238 |
|
| 239 |
return *this; |
| 240 |
} |
| 241 |
|
| 242 |
/// Post-decrement. |
| 243 |
/// \return Another iterator having the old value of this iterator. |
| 244 |
self operator--(int) |
| 245 |
{ |
| 246 |
self itr = *this; |
| 247 |
prev(); |
| 248 |
|
| 249 |
return itr; |
| 250 |
} |
| 251 |
//@} |
| 252 |
|
| 253 |
/// Assignment operator. This iterator will point to the same key/data |
| 254 |
/// pair as itr, and have the same configurations as itr. |
| 255 |
/// \param itr The right value of assignment. |
| 256 |
/// \return The reference of itr. |
| 257 |
/// \sa db_base_iterator::operator=(const self&) |
| 258 |
// We will duplicate the Dbc cursor here. |
| 259 |
inline const self& operator=(const self&itr) |
| 260 |
{ |
| 261 |
ASSIGNMENT_PREDCOND(itr) |
| 262 |
base::operator=(itr); |
| 263 |
|
| 264 |
curpair_base_.first = itr.curpair_base_.first; |
| 265 |
curpair_base_.second = itr.curpair_base_.second; |
| 266 |
if (pcsr_) |
| 267 |
pcsr_->close(); |
| 268 |
pcsr_ = itr.pcsr_; |
| 269 |
|
| 270 |
return itr; |
| 271 |
} |
| 272 |
|
| 273 |
//////////////////////////////////////////////////////////////////// |
| 274 |
|
| 275 |
//////////////////////////////////////////////////////////////////// |
| 276 |
// Begin iterator comparison functions. |
| 277 |
// |
| 278 |
/// \name Compare operators. |
| 279 |
/// Only equal comparison is supported. |
| 280 |
//@{ |
| 281 |
/// Compare two iterators. |
| 282 |
/// Two iterators compare equal when they are both invalid or |
| 283 |
/// both valid and underlying cursor compare equal(i.e. sitting on the |
| 284 |
/// same key/data pair). |
| 285 |
// |
| 286 |
// Note that the iterator itr or this iterator may be an invalid |
| 287 |
// one, i.e. its this->itr_status_ is INVALID_ITERATOR_POSITION. |
| 288 |
// We do not distinguish between end and rend iterators although |
| 289 |
// we are able to do so, because they are never compared together. |
| 290 |
/// Equal comparison operator. |
| 291 |
/// \param itr The iterator to compare against. |
| 292 |
/// \return Returns true if equal, false otherwise. |
| 293 |
inline bool operator==(const self&itr) const |
| 294 |
{ |
| 295 |
COMPARE_CHECK(itr) |
| 296 |
if (((itr.itr_status_ == this->itr_status_) && |
| 297 |
(this->itr_status_ == INVALID_ITERATOR_POSITION)) || |
| 298 |
((itr.itr_status_ == this->itr_status_) && |
| 299 |
(pcsr_->compare((itr.pcsr_.base_ptr())) == 0))) |
| 300 |
return true; |
| 301 |
return false; |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
/// Unequal comparison operator. |
| 306 |
/// \param itr The iterator to compare against. |
| 307 |
/// \return Returns false if equal, true otherwise. |
| 308 |
/// \sa bool operator==(const self&itr) const |
| 309 |
inline bool operator!=(const self&itr) const |
| 310 |
{ |
| 311 |
return !(*this == itr) ; |
| 312 |
} |
| 313 |
//@} |
| 314 |
//////////////////////////////////////////////////////////////////// |
| 315 |
|
| 316 |
//////////////////////////////////////////////////////////////////// |
| 317 |
// Begin functions that retrieve values from the iterator. |
| 318 |
// |
| 319 |
// curpair_base_ is always kept updated on iterator movement, but if |
| 320 |
// directdb_get_ is true, curpair_base_ is also updated here before |
| 321 |
// making use of the value it references. |
| 322 |
// Even if this iterator is invalid, this call is allowed, the |
| 323 |
// default value of type T is returned. |
| 324 |
// |
| 325 |
// Note that the returned reference can only be used to read data, |
| 326 |
// can't be used to update data. |
| 327 |
/// \name Functions that retrieve values from the iterator. |
| 328 |
//@{ |
| 329 |
/// Dereference operator. |
| 330 |
/// Return the reference to the cached data element, which is an |
| 331 |
/// pair<Key_type, T>. You can only read its referenced data via |
| 332 |
/// this iterator but can not update it. |
| 333 |
/// \return Current data element reference object, i.e. ElementHolder |
| 334 |
/// or ElementRef object. |
| 335 |
inline reference operator*() const |
| 336 |
{ |
| 337 |
|
| 338 |
if (this->directdb_get_) { |
| 339 |
csrddt d; |
| 340 |
pcsr_->get_current_key_data(curpair_base_.first, d); |
| 341 |
assign_second0(curpair_base_, d); |
| 342 |
} |
| 343 |
// Returning reference, no copy construction. |
| 344 |
return curpair_base_; |
| 345 |
} |
| 346 |
|
| 347 |
// curpair_base_ is always kept updated on iterator movement, but if |
| 348 |
// directdb_get_ is true, curpair_base_ is also updated here before |
| 349 |
// making use of the value it references. |
| 350 |
// Even if this iterator is invalid, this call is allowed, the |
| 351 |
// default value of type T is returned. |
| 352 |
// |
| 353 |
// Note that the returned reference can only be used to read data, |
| 354 |
// can't be used to update data. |
| 355 |
/// Arrow operator. |
| 356 |
/// Return the pointer to the cached data element, which is an |
| 357 |
/// pair<Key_type, T>. You can only read its referenced data via |
| 358 |
/// this iterator but can not update it. |
| 359 |
/// \return Current data element reference object's address, i.e. |
| 360 |
/// address of ElementHolder or ElementRef object. |
| 361 |
inline pointer operator->() const |
| 362 |
{ |
| 363 |
|
| 364 |
if (this->directdb_get_) { |
| 365 |
csrddt d; |
| 366 |
pcsr_->get_current_key_data(curpair_base_.first, d); |
| 367 |
assign_second0(curpair_base_, d); |
| 368 |
} |
| 369 |
|
| 370 |
return &curpair_base_; |
| 371 |
} |
| 372 |
//@} |
| 373 |
//////////////////////////////////////////////////////////////////// |
| 374 |
|
| 375 |
//////////////////////////////////////////////////////////////////// |
| 376 |
// Begin dbstl specific functions. |
| 377 |
// |
| 378 |
// Refresh the underlying cursor's current data and this object's |
| 379 |
// curpair_base_. It need to be called only if directdb_get is |
| 380 |
// disabled, and other iterators updated |
| 381 |
// the key/data pair this iterator points to and we are about to use |
| 382 |
// this iterator to access that key/data pair. |
| 383 |
// If direct db get is enabled, this method never needs to be called. |
| 384 |
/// @name dbstl specific functions |
| 385 |
//@{ |
| 386 |
/// \brief Refresh iterator cached value. |
| 387 |
/// \param from_db If not doing direct database get and this parameter |
| 388 |
/// is true, we will retrieve data directly from db. |
| 389 |
/// \sa db_base_iterator::refresh(bool) |
| 390 |
virtual int refresh(bool from_db = true) const |
| 391 |
{ |
| 392 |
csrddt d; |
| 393 |
|
| 394 |
if (from_db && !this->directdb_get_) |
| 395 |
pcsr_->update_current_key_data_from_db( |
| 396 |
DbCursorBase::SKIP_NONE); |
| 397 |
pcsr_->get_current_key_data(curpair_base_.first, d); |
| 398 |
assign_second0(curpair_base_, d); |
| 399 |
|
| 400 |
return 0; |
| 401 |
} |
| 402 |
|
| 403 |
// By calling this function, users can choose to close the underlying |
| 404 |
// cursor before iterator destruction to get better performance |
| 405 |
// potentially. |
| 406 |
/// \brief Close underlying Berkeley DB cursor of this iterator. |
| 407 |
/// \sa db_base_iterator::close_cursor() const |
| 408 |
inline void close_cursor() const |
| 409 |
{ |
| 410 |
if (pcsr_) |
| 411 |
pcsr_->close(); |
| 412 |
} |
| 413 |
|
| 414 |
/// Iterator movement function. |
| 415 |
/// Move this iterator to the specified key k, by default moves |
| 416 |
/// exactly to k, and update cached data element, you can |
| 417 |
/// also specify DB_SET_RANGE, to move to the biggest key smaller |
| 418 |
/// than k. The btree/hash key comparison routine determines which |
| 419 |
/// key is bigger. When the iterator is on a multiple container, |
| 420 |
/// move_to will move itself to the first key/data pair of the |
| 421 |
/// identical keys. |
| 422 |
/// \param k The target key value to move to. |
| 423 |
/// \param flag Flags available: DB_SET(default) or DB_SET_RANGE. |
| 424 |
/// DB_SET will move this iterator exactly at k; DB_SET_RANGE moves |
| 425 |
/// this iterator to k or the smallest key greater than k. If fail |
| 426 |
/// to find such a key, this iterator will become invalid. |
| 427 |
/// \return 0 if succeed; non-0 otherwise, and this iterator becomes |
| 428 |
/// invalid. Call db_strerror with the return value to get the error |
| 429 |
/// message. |
| 430 |
inline int move_to(const kdt& k, int flag = DB_SET) const |
| 431 |
{ |
| 432 |
int ret; |
| 433 |
// Use tmpk2 to avoid k being modified. |
| 434 |
kdt tmpk2 = k; |
| 435 |
|
| 436 |
this->itr_status_ = (ret = pcsr_->move_to(tmpk2, flag)); |
| 437 |
if (ret != 0) { |
| 438 |
this->inval_pos_type_ = base::IPT_UNSET; |
| 439 |
return ret; |
| 440 |
} |
| 441 |
|
| 442 |
refresh(); |
| 443 |
|
| 444 |
return ret; |
| 445 |
} |
| 446 |
|
| 447 |
/// Modify bulk buffer size. |
| 448 |
/// Bulk read is enabled when creating an |
| 449 |
/// iterator, so users later can only modify the bulk buffer size |
| 450 |
/// to another value, but can't enable/disable bulk read while an |
| 451 |
/// iterator is already alive. |
| 452 |
/// \param sz The new size of the bulk read buffer of this iterator. |
| 453 |
/// \return Returns true if succeeded, false otherwise. |
| 454 |
/// \sa db_base_iterator::set_bulk_buffer(u_int32_t ) |
| 455 |
bool set_bulk_buffer(u_int32_t sz) |
| 456 |
{ |
| 457 |
bool ret = this->pcsr_->set_bulk_buffer(sz); |
| 458 |
if (ret) |
| 459 |
this->bulk_retrieval_ = |
| 460 |
this->pcsr_->get_bulk_bufsize(); |
| 461 |
return ret; |
| 462 |
} |
| 463 |
|
| 464 |
/// \brief Get bulk retrieval buffer size in bytes. |
| 465 |
/// \return Return current bulk buffer size or 0 if bulk retrieval is |
| 466 |
/// not enabled. |
| 467 |
/// \sa db_base_iterator::get_bulk_bufsize() |
| 468 |
u_int32_t get_bulk_bufsize() |
| 469 |
{ |
| 470 |
this->bulk_retrieval_ = pcsr_->get_bulk_bufsize(); |
| 471 |
return this->bulk_retrieval_; |
| 472 |
} |
| 473 |
//@} |
| 474 |
//////////////////////////////////////////////////////////////////// |
| 475 |
|
| 476 |
protected: |
| 477 |
|
| 478 |
// The cursor_type is used to directly return the pair object, |
| 479 |
// rather than a reference to it |
| 480 |
typedef DbCursor<kdt, csrddt> cursor_type; |
| 481 |
|
| 482 |
friend class db_map_iterator<kdt, ddt, ElementRef<ddt> >; |
| 483 |
friend class db_map_iterator<kdt, ddt, ElementHolder<ddt> >; |
| 484 |
// Use friend classes to hide internal members from users. |
| 485 |
friend class db_map<kdt, ddt, ElementHolder<ddt> >; |
| 486 |
friend class db_map<kdt, ddt, ElementHolder<kdt>, |
| 487 |
db_set_iterator<kdt, ElementHolder<kdt> > >; |
| 488 |
friend class db_map<kdt, _DB_STL_set_value<kdt>, ElementHolder<kdt>, |
| 489 |
db_set_iterator<kdt, ElementHolder<kdt> > >; |
| 490 |
|
| 491 |
friend class db_set<kdt, ElementHolder<kdt> >; |
| 492 |
friend class db_set_iterator<kdt, ElementHolder<kdt> >; |
| 493 |
friend class db_multiset<kdt, ElementHolder<kdt> >; |
| 494 |
friend class db_multimap<kdt, ddt, ElementHolder<ddt> >; |
| 495 |
friend class db_multimap<kdt, _DB_STL_set_value<kdt>, |
| 496 |
ElementHolder<kdt>, db_set_iterator<kdt, ElementHolder<kdt> > >; |
| 497 |
|
| 498 |
friend class db_map<kdt, ddt, ElementRef<ddt> >; |
| 499 |
friend class db_map<kdt, _DB_STL_set_value<kdt>, ElementRef<kdt>, |
| 500 |
db_set_iterator<kdt, ElementRef<kdt> > >; |
| 501 |
|
| 502 |
friend class db_set<kdt, ElementRef<kdt> >; |
| 503 |
friend class db_set_iterator<kdt, ElementRef<kdt> >; |
| 504 |
friend class db_multiset<kdt, ElementRef<kdt> >; |
| 505 |
friend class db_multimap<kdt, ddt, ElementRef<ddt> >; |
| 506 |
friend class db_multimap<kdt, _DB_STL_set_value<kdt>, ElementRef<kdt>, |
| 507 |
db_set_iterator<kdt, ElementRef<kdt> > >; |
| 508 |
|
| 509 |
|
| 510 |
//////////////////////////////////////////////////////////////////// |
| 511 |
// Begin db_map_base_iterator data members. |
| 512 |
// |
| 513 |
// Cursor of this iterator, note that each db_map_base_iterator has a |
| 514 |
// unique DbCursor, not shared with any other iterator, and when copy |
| 515 |
// constructing or assigning, the cursor is duplicated |
| 516 |
// when it is actually used to access db. |
| 517 |
// |
| 518 |
mutable LazyDupCursor<DbCursor<kdt, csrddt> > pcsr_; |
| 519 |
|
| 520 |
// In order for std::map style iterator to work, we need a pair |
| 521 |
// here to store the key-value pair this iterator currently points |
| 522 |
// to in the db_map. |
| 523 |
// |
| 524 |
// curpair_base_ is always kept updated on every cursor/iterator |
| 525 |
// movement and initialized to point to the first key-value pair when |
| 526 |
// db_map<>::begin() is called. |
| 527 |
// |
| 528 |
mutable value_type curpair_base_; |
| 529 |
//////////////////////////////////////////////////////////////////// |
| 530 |
|
| 531 |
//////////////////////////////////////////////////////////////////// |
| 532 |
// Begin internal helper functions. |
| 533 |
// |
| 534 |
// Open the iterator and its cursor. |
| 535 |
// |
| 536 |
void open() const |
| 537 |
{ |
| 538 |
u_int32_t oflags = 0, coflags = 0; |
| 539 |
int ret; |
| 540 |
Db *pdb = this->owner_->get_db_handle(); |
| 541 |
DbEnv *penv = pdb->get_env(); |
| 542 |
|
| 543 |
coflags = this->owner_->get_cursor_open_flags(); |
| 544 |
assert(this->owner_ != NULL); |
| 545 |
if (!this->read_only_ && penv != NULL) { |
| 546 |
BDBOP((penv->get_open_flags(&oflags)), ret); |
| 547 |
if ((oflags & DB_INIT_CDB) != 0) |
| 548 |
this->owner_->set_cursor_open_flags(coflags |= |
| 549 |
DB_WRITECURSOR); |
| 550 |
} |
| 551 |
if (!pcsr_) |
| 552 |
pcsr_.set_cursor(new DbCursor<kdt, csrddt>( |
| 553 |
this->bulk_retrieval_, |
| 554 |
this->rmw_csr_, this->directdb_get_)); |
| 555 |
this->itr_status_ = pcsr_->open((db_container*)this->owner_, |
| 556 |
coflags); |
| 557 |
|
| 558 |
} |
| 559 |
|
| 560 |
// Move this iterator as well as the underlying Dbc* cursor to |
| 561 |
// first element and update cur_pair_. |
| 562 |
// |
| 563 |
int first() const |
| 564 |
{ |
| 565 |
|
| 566 |
assert(this->owner_ != NULL); |
| 567 |
this->itr_status_ = pcsr_->first(); |
| 568 |
if (this->itr_status_ == 0) |
| 569 |
refresh(); |
| 570 |
else |
| 571 |
this->inval_pos_type_ = base::IPT_UNSET; |
| 572 |
|
| 573 |
return this->itr_status_; |
| 574 |
|
| 575 |
} |
| 576 |
|
| 577 |
// Move this iterator as well as the underlying Dbc* cursor |
| 578 |
// to last effective(valid) element and update cur_pair_. |
| 579 |
// |
| 580 |
int last() const |
| 581 |
{ |
| 582 |
|
| 583 |
assert(this->owner_ != NULL); |
| 584 |
this->itr_status_ = pcsr_->last(); |
| 585 |
if (this->itr_status_ == 0) |
| 586 |
refresh(); |
| 587 |
else |
| 588 |
this->inval_pos_type_ = base::IPT_UNSET; |
| 589 |
|
| 590 |
return this->itr_status_; |
| 591 |
} |
| 592 |
|
| 593 |
// Move this iterator as well as the underlying Dbc* cursor |
| 594 |
// to next element, then update its position flags and cur_pair_. |
| 595 |
// |
| 596 |
int next(int flags = DB_NEXT) const |
| 597 |
{ |
| 598 |
|
| 599 |
assert(this->owner_ != NULL); |
| 600 |
|
| 601 |
if (this->itr_status_ == INVALID_ITERATOR_POSITION) { |
| 602 |
if (this->inval_pos_type_ == base::IPT_BEFORE_FIRST) { |
| 603 |
// This rend itr must have an non-NULL owner. |
| 604 |
open(); |
| 605 |
// rend itr can go back to first element. |
| 606 |
this->itr_status_ = first(); |
| 607 |
} else if (this->inval_pos_type_ == base::IPT_UNSET) { |
| 608 |
THROW0(InvalidIteratorException); |
| 609 |
} |
| 610 |
// Else, return itr_status_ in last line. |
| 611 |
} else { |
| 612 |
|
| 613 |
this->itr_status_ = pcsr_->next(flags); |
| 614 |
if (this->itr_status_ == 0) |
| 615 |
refresh(); |
| 616 |
else |
| 617 |
this->inval_pos_type_ = base::IPT_AFTER_LAST; |
| 618 |
|
| 619 |
} |
| 620 |
|
| 621 |
return this->itr_status_; |
| 622 |
} |
| 623 |
|
| 624 |
// Move this iterator as well as the underlying Dbc* cursor |
| 625 |
// to previous element. |
| 626 |
// |
| 627 |
int prev(int flags = DB_PREV) const |
| 628 |
{ |
| 629 |
|
| 630 |
assert(this->owner_ != NULL); |
| 631 |
if (this->itr_status_ == INVALID_ITERATOR_POSITION) { |
| 632 |
if (this->inval_pos_type_ == base::IPT_AFTER_LAST) { |
| 633 |
// This rend itr must have an non-NULL owner. |
| 634 |
open(); |
| 635 |
// end itr can go back to last element. |
| 636 |
this->itr_status_ = last(); |
| 637 |
} else if (this->inval_pos_type_ == base::IPT_UNSET) { |
| 638 |
THROW0(InvalidIteratorException); |
| 639 |
} |
| 640 |
// Else, return itr stat in last line. |
| 641 |
} else { |
| 642 |
this->itr_status_ = pcsr_->prev(flags); |
| 643 |
if (this->itr_status_ == 0) |
| 644 |
refresh(); |
| 645 |
else |
| 646 |
this->inval_pos_type_ = base::IPT_BEFORE_FIRST; |
| 647 |
|
| 648 |
} |
| 649 |
|
| 650 |
return this->itr_status_; |
| 651 |
} |
| 652 |
|
| 653 |
void set_curpair_base(const kdt& k, const csrddt &d) const |
| 654 |
{ |
| 655 |
curpair_base_.first = k; |
| 656 |
assign_second0(curpair_base_, d); |
| 657 |
} |
| 658 |
|
| 659 |
//////////////////////////////////////////////////////////////////// |
| 660 |
|
| 661 |
protected: // Do not remove this line, otherwise assign_second0 may be public. |
| 662 |
#ifndef DOXYGEN_CANNOT_SEE_THIS |
| 663 |
#if NO_MEMBER_FUNCTION_PARTIAL_SPECIALIZATION |
| 664 |
};// end of db_map_base_iterator<> |
| 665 |
template <Typename kdt, Typename datadt, Typename ddt> |
| 666 |
void assign_second0(pair<kdt, ddt>& v, const datadt& d) |
| 667 |
{ |
| 668 |
v.second = d; |
| 669 |
} |
| 670 |
|
| 671 |
template<Typename kdt, Typename ddt> |
| 672 |
void assign_second0(pair<kdt, ddt> &v, |
| 673 |
const _DB_STL_set_value<kdt>& |
| 674 |
/* d unused, use v.first to assign v.second */) |
| 675 |
{ |
| 676 |
v.second = v.first; |
| 677 |
} |
| 678 |
#else |
| 679 |
|
| 680 |
template <Typename datadt> |
| 681 |
inline void assign_second0(value_type& v, const datadt& d) const |
| 682 |
{ |
| 683 |
v.second = d; |
| 684 |
} |
| 685 |
|
| 686 |
template<> |
| 687 |
inline void |
| 688 |
assign_second0(value_type &v, const _DB_STL_set_value<kdt>& |
| 689 |
/* d unused, use v.first to assign v.second */) const |
| 690 |
{ |
| 691 |
v.second = v.first; |
| 692 |
} |
| 693 |
|
| 694 |
};// end of db_map_base_iterator<> |
| 695 |
|
| 696 |
#endif |
| 697 |
|
| 698 |
#else |
| 699 |
}; |
| 700 |
#endif // DOXYGEN_CANNOT_SEE_THIS |
| 701 |
//@} // db_map_iterators |
| 702 |
//@} // dbstl_iterators |
| 703 |
|
| 704 |
////////////////////////////////////////////////////////////////////////// |
| 705 |
////////////////////////////////////////////////////////////////////////// |
| 706 |
// |
| 707 |
// db_map_iterator class template definition |
| 708 |
// |
| 709 |
// db_map_iterator is the iterator class template for db_map and |
| 710 |
// db_multimap, it is also the base class for db_set_iterator. It can be |
| 711 |
// used to both read and write the database. |
| 712 |
// |
| 713 |
// Template parameters info: |
| 714 |
// kdt is "key data type", ddt is "data data type", value_type_sub is |
| 715 |
// either ElementRef<ddt> (by default, in this case ElementRef inherits |
| 716 |
// from ddt, so ddt must not be a primitive type) or ElementHolder<ddt>, |
| 717 |
// in this case ElementHolder has a data member of type ddt, so suitable for |
| 718 |
// primitive types, but don't apply it to classes otherwise you can't access |
| 719 |
// members like this : *iterator.member = value. |
| 720 |
// |
| 721 |
/// \ingroup dbstl_iterators |
| 722 |
//@{ |
| 723 |
/// \ingroup db_map_iterators |
| 724 |
//@{ |
| 725 |
template <Typename kdt, Typename ddt, Typename value_type_sub> |
| 726 |
class _exported db_map_iterator : public |
| 727 |
db_map_base_iterator<kdt, typename value_type_sub::content_type, ddt> |
| 728 |
{ |
| 729 |
protected: |
| 730 |
typedef db_map_iterator<kdt, ddt, value_type_sub> self; |
| 731 |
typedef typename value_type_sub::content_type realddt; |
| 732 |
using db_base_iterator<typename value_type_sub::content_type>:: |
| 733 |
replace_current_key; |
| 734 |
public: |
| 735 |
typedef kdt key_type; |
| 736 |
typedef ddt data_type; |
| 737 |
typedef pair<kdt, ddt> value_type; |
| 738 |
typedef pair<kdt, value_type_sub> value_type_wrap; |
| 739 |
// Not used in this class, but required to satisfy |
| 740 |
// db_reverse_iterator type extraction. |
| 741 |
typedef ptrdiff_t difference_type; |
| 742 |
typedef difference_type distance_type; |
| 743 |
typedef value_type_wrap& reference; |
| 744 |
typedef value_type_wrap* pointer; |
| 745 |
|
| 746 |
// We have to use standard iterator tags to match the parameter |
| 747 |
// list of stl internal functions, we can't use our own tag |
| 748 |
// classes, so we don't write tag classes in dbstl. |
| 749 |
// |
| 750 |
typedef std::bidirectional_iterator_tag iterator_category; |
| 751 |
|
| 752 |
// Refresh the underlying cursor's current data and this object's |
| 753 |
// curpair_. It need to be called only if other iterators updated the |
| 754 |
// key/data pair this iterator points to and we are about to use |
| 755 |
// this iterator to access that key/data pair. If direct db get is |
| 756 |
// enabled, this method never needs to be called. |
| 757 |
/// \brief Refresh iterator cached value. |
| 758 |
/// \param from_db If not doing direct database get and this parameter |
| 759 |
/// is true, we will retrieve data directly from db. |
| 760 |
/// \sa db_base_iterator::refresh(bool ) |
| 761 |
virtual int refresh(bool from_db = true) const |
| 762 |
{ |
| 763 |
kdt k; |
| 764 |
ddt d; |
| 765 |
|
| 766 |
if (from_db && !this->directdb_get_) |
| 767 |
this->pcsr_->update_current_key_data_from_db( |
| 768 |
DbCursorBase::SKIP_NONE); |
| 769 |
this->pcsr_->get_current_key_data(k, d); |
| 770 |
curpair_.first = k; |
| 771 |
assign_second(curpair_, d); |
| 772 |
this->set_curpair_base(k, d); |
| 773 |
|
| 774 |
return 0; |
| 775 |
} |
| 776 |
|
| 777 |
//////////////////////////////////////////////////////////////// |
| 778 |
// Begin constructors and destructor definitions. |
| 779 |
/// \name Constructors and destructor |
| 780 |
/// Do not create iterators directly using these constructors, but |
| 781 |
/// call db_map::begin or db_multimap_begin to get instances of |
| 782 |
/// this class. |
| 783 |
/// \sa db_map::begin() db_multimap::begin() |
| 784 |
//@{ |
| 785 |
/// Copy constructor. |
| 786 |
/// \param vi The other iterator of the same type to initialize this. |
| 787 |
db_map_iterator(const db_map_iterator<kdt, ddt, value_type_sub>& vi) |
| 788 |
: db_map_base_iterator<kdt, realddt, ddt>(vi) |
| 789 |
{ |
| 790 |
// Lazy-dup another cursor, cursor to iterator mapping |
| 791 |
// is 1 to 1. |
| 792 |
curpair_.first = vi.curpair_.first; |
| 793 |
curpair_.second._DB_STL_CopyData(vi.curpair_.second); |
| 794 |
curpair_.second._DB_STL_SetIterator(this); |
| 795 |
} |
| 796 |
|
| 797 |
/// Base copy constructor. |
| 798 |
/// \param vi Initialize from a base class iterator. |
| 799 |
db_map_iterator(const db_map_base_iterator<kdt, realddt, ddt>& vi) : |
| 800 |
db_map_base_iterator<kdt, realddt, ddt>(vi) |
| 801 |
|
| 802 |
{ |
| 803 |
curpair_.second._DB_STL_SetIterator(this); |
| 804 |
curpair_.first = vi->first; |
| 805 |
curpair_.second._DB_STL_CopyData(vi->second); |
| 806 |
} |
| 807 |
|
| 808 |
/// Constructor. |
| 809 |
/// \param powner The container which creates this iterator. |
| 810 |
/// \param b_bulk_retrieval The bulk read buffer size. 0 means |
| 811 |
/// bulk read disabled. |
| 812 |
/// \param brmw Whether set DB_RMW flag in underlying cursor. |
| 813 |
/// \param directdbget Whether do direct database get rather than |
| 814 |
/// using key/data values cached in the iterator whenever read. |
| 815 |
/// \param b_read_only Whether open a read only cursor. Only effective |
| 816 |
/// when using Berkeley DB Concurrent Data Store. |
| 817 |
explicit db_map_iterator(db_container*powner, |
| 818 |
u_int32_t b_bulk_retrieval = 0, bool brmw = false, |
| 819 |
bool directdbget = true, bool b_read_only = false) |
| 820 |
: db_map_base_iterator<kdt, realddt, ddt> |
| 821 |
(powner, b_bulk_retrieval, brmw, directdbget, b_read_only) |
| 822 |
{ |
| 823 |
curpair_.second._DB_STL_SetIterator(this); |
| 824 |
} |
| 825 |
|
| 826 |
/// Default constructor, dose not create the cursor for now. |
| 827 |
db_map_iterator() : db_map_base_iterator<kdt, realddt, ddt>() |
| 828 |
{ |
| 829 |
curpair_.second._DB_STL_SetIterator(this); |
| 830 |
} |
| 831 |
|
| 832 |
// Use virtual because ElementRef<> uses a db_base_iterator* pointer |
| 833 |
// to refer to the iterator, and also use "dead_" flag to avoid |
| 834 |
// multiple call to the same destructor by ~ElementRef<>(). |
| 835 |
/// Destructor. |
| 836 |
virtual ~db_map_iterator() |
| 837 |
{ |
| 838 |
// Required here though set in base destructor too. |
| 839 |
this->dead_ = true; |
| 840 |
} |
| 841 |
//@} |
| 842 |
//////////////////////////////////////////////////////////////// |
| 843 |
|
| 844 |
//////////////////////////////////////////////////////////////////// |
| 845 |
// Begin functions that shift iterator position. |
| 846 |
// |
| 847 |
// Do not throw exceptions here because it is likely and normal |
| 848 |
// to iterate to the "end iterator". |
| 849 |
/// \name Iterator movement operators. |
| 850 |
//@{ |
| 851 |
/// Pre-increment \sa db_map_base_iterator::operator++() |
| 852 |
/// \return This iterator after incremented. |
| 853 |
inline self& operator++() |
| 854 |
{ |
| 855 |
this->next(); |
| 856 |
|
| 857 |
return *this; |
| 858 |
} |
| 859 |
|
| 860 |
/// Post-increment \sa db_map_base_iterator::operator++(int) |
| 861 |
/// \return Another iterator having the old value of this iterator. |
| 862 |
inline self operator++(int) |
| 863 |
{ |
| 864 |
self itr = *this; |
| 865 |
|
| 866 |
this->next(); |
| 867 |
|
| 868 |
return itr; |
| 869 |
} |
| 870 |
|
| 871 |
/// Pre-decrement \sa db_map_base_iterator::operator--() |
| 872 |
/// \return This iterator after decremented. |
| 873 |
inline self& operator--() |
| 874 |
{ |
| 875 |
this->prev(); |
| 876 |
|
| 877 |
return *this; |
| 878 |
} |
| 879 |
|
| 880 |
/// Post-decrement \sa db_map_base_iterator::operator--(int) |
| 881 |
/// \return Another iterator having the old value of this iterator. |
| 882 |
self operator--(int) |
| 883 |
{ |
| 884 |
self itr = *this; |
| 885 |
this->prev(); |
| 886 |
|
| 887 |
return itr; |
| 888 |
} |
| 889 |
//@} |
| 890 |
// Assignment operator, we will duplicate the Dbc cursor here. |
| 891 |
/// Assignment operator. This iterator will point to the same key/data |
| 892 |
/// pair as itr, and have the same configurations as itr. |
| 893 |
/// \param itr The right value of assignment. |
| 894 |
/// \return The reference of itr. |
| 895 |
/// \sa db_base_iterator::operator=(const self&) |
| 896 |
inline const self& operator=(const self&itr) |
| 897 |
{ |
| 898 |
ASSIGNMENT_PREDCOND(itr) |
| 899 |
base::operator=(itr); |
| 900 |
|
| 901 |
curpair_.first = itr.curpair_.first; |
| 902 |
|
| 903 |
// Only copy data from itr.curpair_ into curpair_, |
| 904 |
// don't store into db. Note that we can not assign |
| 905 |
// itr.curpair_ to curpair_ simply by curpair_ = itr.curpair_, |
| 906 |
// otherwise, ElementRef<>::operator= is called, which will |
| 907 |
// update the data element referenced by this iterator. |
| 908 |
// |
| 909 |
curpair_.second._DB_STL_CopyData(itr.curpair_.second); |
| 910 |
|
| 911 |
return itr; |
| 912 |
} |
| 913 |
//////////////////////////////////////////////////////////////////// |
| 914 |
|
| 915 |
//////////////////////////////////////////////////////////////////// |
| 916 |
// Begin functions that retrieve values from the iterator. |
| 917 |
// |
| 918 |
// curpair_base_ is always kept updated on iterator movement, but if |
| 919 |
// directdb_get_ is true, curpair_base_ is also updated here before |
| 920 |
// making use of the value it references. |
| 921 |
// Even if this iterator is invalid, this call is allowed, the |
| 922 |
// default value of type T is returned. |
| 923 |
// |
| 924 |
/// \name Functions that retrieve values from the iterator. |
| 925 |
//@{ |
| 926 |
/// Dereference operator. |
| 927 |
/// Return the reference to the cached data element, which is an |
| 928 |
/// pair<Key_type, ElementRef<T> > object if T is a class type or an |
| 929 |
/// pair<Key_type, ElementHolder<T> > object if T is a C++ primitive |
| 930 |
/// data type. |
| 931 |
/// \return Current data element reference object, i.e. ElementHolder |
| 932 |
/// or ElementRef object. |
| 933 |
inline reference operator*() const |
| 934 |
{ |
| 935 |
|
| 936 |
if (this->directdb_get_) { |
| 937 |
ddt d; |
| 938 |
this->pcsr_->get_current_key_data(curpair_.first, d); |
| 939 |
assign_second(curpair_, d); |
| 940 |
} |
| 941 |
|
| 942 |
return curpair_;// returning reference, no copy construction |
| 943 |
} |
| 944 |
|
| 945 |
// curpair_base_ is always kept updated on iterator movement, but if |
| 946 |
// directdb_get_ is true, curpair_base_ is also updated here before |
| 947 |
// making use of the value it references. |
| 948 |
// Even if this iterator is invalid, this call is allowed, the |
| 949 |
// default value of type T is returned. |
| 950 |
/// Arrow operator. |
| 951 |
/// Return the pointer to the cached data element, which is an |
| 952 |
/// pair<Key_type, ElementRef<T> > object if T is a class type or an |
| 953 |
/// pair<Key_type, ElementHolder<T> > object if T is a C++ primitive |
| 954 |
/// data type. |
| 955 |
/// \return Current data element reference object's address, i.e. |
| 956 |
/// address of ElementHolder or ElementRef object. |
| 957 |
inline pointer operator->() const |
| 958 |
{ |
| 959 |
|
| 960 |
if (this->directdb_get_) { |
| 961 |
ddt d; |
| 962 |
this->pcsr_->get_current_key_data(curpair_.first, d); |
| 963 |
assign_second(curpair_, d); |
| 964 |
} |
| 965 |
return &curpair_; |
| 966 |
} |
| 967 |
//@} |
| 968 |
//////////////////////////////////////////////////////////////////// |
| 969 |
|
| 970 |
//@} // db_map_iterators |
| 971 |
//@} // dbstl_iterators |
| 972 |
protected: |
| 973 |
// The cursor_type is used to directly return the pair object, |
| 974 |
// rather than a reference to it. |
| 975 |
typedef DbCursor<kdt, ddt> cursor_type; |
| 976 |
typedef db_map_base_iterator<kdt, realddt, ddt> base; |
| 977 |
typedef db_map_base_iterator<kdt, realddt> const_version; |
| 978 |
|
| 979 |
// Use friend classes to hide internal members from users. |
| 980 |
friend class db_map<kdt, ddt, value_type_sub>; |
| 981 |
friend class db_map<kdt, ddt, value_type_sub, |
| 982 |
db_set_iterator<kdt, value_type_sub> >; |
| 983 |
friend class db_set<kdt, value_type_sub>; |
| 984 |
friend class db_set_iterator<kdt, value_type_sub>; |
| 985 |
friend class db_multiset<kdt, value_type_sub>; |
| 986 |
friend class db_multimap<kdt, ddt, value_type_sub>; |
| 987 |
friend class db_multimap<kdt, _DB_STL_set_value<kdt>, value_type_sub, |
| 988 |
db_set_iterator<kdt, value_type_sub > >; |
| 989 |
|
| 990 |
//////////////////////////////////////////////////////////////// |
| 991 |
// Begin db_map_iterator data members. |
| 992 |
// |
| 993 |
// In order for std::map style iterator to work, we need a pair |
| 994 |
// here to store the key-value pair this iterator currently points |
| 995 |
// to in the db_map. |
| 996 |
// |
| 997 |
// curpair_ is always kept updated on every cursor/iterator movement |
| 998 |
// and initialized to point to the first key-value pair when |
| 999 |
// db_map<>::begin() is called. |
| 1000 |
// |
| 1001 |
mutable value_type_wrap curpair_; |
| 1002 |
//////////////////////////////////////////////////////////////// |
| 1003 |
|
| 1004 |
//////////////////////////////////////////////////////////////// |
| 1005 |
// Begin internal helper functions. |
| 1006 |
// |
| 1007 |
// Called by ElementRef<> object when this iterator belongs to the |
| 1008 |
// object---The only situation is in db_container::operator[] which |
| 1009 |
// has to return an ElementRef/Holder object A, and its iterator has |
| 1010 |
// to survive until A is destructed. |
| 1011 |
virtual void delete_me() const |
| 1012 |
{ |
| 1013 |
if (!this->dead_) |
| 1014 |
delete this; |
| 1015 |
} |
| 1016 |
|
| 1017 |
// Duplicate this iterator. |
| 1018 |
virtual self* dup_itr() const |
| 1019 |
{ |
| 1020 |
self *itr = new self(*this); |
| 1021 |
// The curpair_ of itr does not delete itr, the independent |
| 1022 |
// one does. |
| 1023 |
//itr->curpair_.second._DB_STL_SetDelItr(); |
| 1024 |
return itr; |
| 1025 |
} |
| 1026 |
|
| 1027 |
// Replace the current key/data pair's data pointed to by this |
| 1028 |
// iterator's underlying Dbc* cursor with the parameter d. |
| 1029 |
// |
| 1030 |
virtual int replace_current( |
| 1031 |
const typename value_type_sub::content_type& d) |
| 1032 |
{ |
| 1033 |
int ret; |
| 1034 |
|
| 1035 |
if (this->read_only_) { |
| 1036 |
THROW(InvalidFunctionCall, ( |
| 1037 |
"db_map_iterator<>::replace_current can't be called via a read only iterator")); |
| 1038 |
} |
| 1039 |
ret = this->pcsr_->replace(d); |
| 1040 |
|
| 1041 |
return ret; |
| 1042 |
} |
| 1043 |
|
| 1044 |
// Used by set iterator to store another different key : |
| 1045 |
// remove the previous one then insert the new one. |
| 1046 |
// It has to be defined in this class because db_set_iterator |
| 1047 |
// inherits from db_map_iterator but we have no polymorphism when |
| 1048 |
// using stl because the object are always used rather the |
| 1049 |
// pointer/reference. |
| 1050 |
// |
| 1051 |
virtual int replace_current_key(const kdt& k) |
| 1052 |
{ |
| 1053 |
int ret; |
| 1054 |
|
| 1055 |
if (this->read_only_) { |
| 1056 |
THROW(InvalidFunctionCall, ( |
| 1057 |
"db_map_iterator<>::replace_current_key can't be called via a read only iterator")); |
| 1058 |
} |
| 1059 |
ret = this->pcsr_->replace_key(k); |
| 1060 |
|
| 1061 |
return ret; |
| 1062 |
} |
| 1063 |
|
| 1064 |
//////////////////////////////////////////////////////////////// |
| 1065 |
|
| 1066 |
|
| 1067 |
protected: // Do not remove this line, otherwise assign_second may be public. |
| 1068 |
#ifndef DOXYGEN_CANNOT_SEE_THIS |
| 1069 |
#if NO_MEMBER_FUNCTION_PARTIAL_SPECIALIZATION |
| 1070 |
};// end of db_map_iterator<> |
| 1071 |
|
| 1072 |
template <Typename kdt, Typename datadt, Typename value_type_sub> |
| 1073 |
void assign_second(pair<kdt, value_type_sub>& v, const datadt& d) |
| 1074 |
{ |
| 1075 |
v.second._DB_STL_CopyData(d); |
| 1076 |
} |
| 1077 |
|
| 1078 |
template<Typename kdt, Typename value_type_sub> |
| 1079 |
void assign_second(pair<kdt, value_type_sub> &v, |
| 1080 |
const _DB_STL_set_value<kdt>& |
| 1081 |
/* d unused, use v.first to assign v.second */) |
| 1082 |
{ |
| 1083 |
v.second._DB_STL_CopyData(v.first); |
| 1084 |
} |
| 1085 |
#else |
| 1086 |
|
| 1087 |
template <Typename datadt> |
| 1088 |
inline void assign_second(value_type_wrap& v, const datadt& d) const |
| 1089 |
{ |
| 1090 |
v.second._DB_STL_CopyData(d); |
| 1091 |
} |
| 1092 |
|
| 1093 |
template<> |
| 1094 |
inline void |
| 1095 |
assign_second(value_type_wrap &v, const _DB_STL_set_value<kdt>& |
| 1096 |
/* d unused, use v.first to assign v.second */) const |
| 1097 |
{ |
| 1098 |
v.second._DB_STL_CopyData(v.first); |
| 1099 |
} |
| 1100 |
|
| 1101 |
};// end of db_map_iterator<> |
| 1102 |
|
| 1103 |
#endif |
| 1104 |
#else |
| 1105 |
}; |
| 1106 |
#endif // DOXYGEN_CANNOT_SEE_THIS |
| 1107 |
u_int32_t hash_default(Db *dbp, const void *key, u_int32_t len); |
| 1108 |
|
| 1109 |
////////////////////////////////////////////////////////////////////////// |
| 1110 |
///////////////////////////////////////////////////////////////////////// |
| 1111 |
// |
| 1112 |
// db_map container class definition |
| 1113 |
/// \ingroup dbstl_containers |
| 1114 |
//@{ |
| 1115 |
/// db_map has identical methods to std::map and the semantics for each |
| 1116 |
/// method is identical to its std::map counterpart, except that it stores data |
| 1117 |
/// into underlying Berkeley DB btree or hash database. Passing a database |
| 1118 |
/// handle of btree or hash type creates a db_map equivalent to std::map and |
| 1119 |
/// std::hashmap respectively. |
| 1120 |
/// Database(dbp) and environment(penv) handle requirement(applies to all |
| 1121 |
/// constructors in this class template): |
| 1122 |
/// 0. The dbp is opened inside the penv environment. Either one of the two |
| 1123 |
/// handles can be NULL. If dbp is NULL, an anonymous database is created |
| 1124 |
/// by dbstl. |
| 1125 |
/// 1. Database type of dbp should be DB_BTREE or DB_HASH. |
| 1126 |
/// 2. No DB_DUP or DB_DUPSORT flag set in dbp. |
| 1127 |
/// 3. No DB_RECNUM flag set in dbp. |
| 1128 |
/// 4. No DB_TRUNCATE specified in dbp's database open flags. |
| 1129 |
/// 5. DB_THREAD must be set if you are sharing the dbp across |
| 1130 |
/// multiple threads directly, or indirectly by sharing the container object |
| 1131 |
/// across multiple threads. |
| 1132 |
/// \param kdt The key data type. |
| 1133 |
/// \param ddt The data data type. db_map stores key/data pairs. |
| 1134 |
/// \param value_type_sub Do not specify anything if ddt type is a |
| 1135 |
/// class/struct type; Otherwise, specify ElementHolder<ddt> to it. |
| 1136 |
/// \param iterator_t Never specify anything to this type parameter. It is |
| 1137 |
/// only used internally. |
| 1138 |
/// \sa db_container db_container(Db*, DbEnv*) db_container(const db_container&) |
| 1139 |
template <Typename kdt, Typename ddt, Typename value_type_sub, |
| 1140 |
Typename iterator_t> |
| 1141 |
class _exported db_map : public db_container |
| 1142 |
{ |
| 1143 |
|
| 1144 |
public: |
| 1145 |
// iterator_t is default argument, see forward declaration at the |
| 1146 |
// head of this file |
| 1147 |
typedef iterator_t iterator; |
| 1148 |
typedef typename iterator::const_version const_iterator; |
| 1149 |
typedef db_reverse_iterator<iterator, const_iterator> reverse_iterator; |
| 1150 |
typedef db_reverse_iterator<const_iterator, iterator> |
| 1151 |
const_reverse_iterator; |
| 1152 |
typedef kdt key_type; |
| 1153 |
typedef ddt data_type; |
| 1154 |
typedef value_type_sub data_type_wrap; |
| 1155 |
typedef pair<kdt, ddt> value_type; |
| 1156 |
typedef pair<kdt, value_type_sub> value_type_wrap; |
| 1157 |
typedef const value_type const_value_type; |
| 1158 |
typedef ptrdiff_t difference_type; |
| 1159 |
typedef size_t size_type; |
| 1160 |
// The following three types are not used in db_map, but we define |
| 1161 |
// them to conform to stl specifications. |
| 1162 |
typedef value_type_wrap& reference; |
| 1163 |
typedef const value_type& const_reference; |
| 1164 |
typedef value_type_wrap* pointer; |
| 1165 |
protected: |
| 1166 |
typedef db_map<kdt, ddt, value_type_sub, iterator> self; |
| 1167 |
typedef typename value_type_sub::content_type realddt; |
| 1168 |
|
| 1169 |
// This constructor is for db_multimap's constructors to call, |
| 1170 |
// because other constructors of this class will verify db handles |
| 1171 |
// and create one if needed. We need a special one that don't do |
| 1172 |
// anything. The BulkRetrievalOption is randomly picked, no special |
| 1173 |
// implications at all. |
| 1174 |
db_map(BulkRetrievalOption& arg){ delete &arg; } |
| 1175 |
public: |
| 1176 |
//////////////////////////////////////////////////////////////// |
| 1177 |
// Begin inner class definitions. |
| 1178 |
// |
| 1179 |
// key_compare class definition, it is defined as an inner class, |
| 1180 |
// using underlying btree/hash db's compare function |
| 1181 |
// |
| 1182 |
class key_compare |
| 1183 |
{ |
| 1184 |
private: |
| 1185 |
Db*pdb; |
| 1186 |
public: |
| 1187 |
key_compare(Db*pdb1) |
| 1188 |
{ |
| 1189 |
pdb = pdb1; |
| 1190 |
} |
| 1191 |
bool operator()(const kdt& k1, const kdt& k2) const |
| 1192 |
{ |
| 1193 |
return compare_keys(pdb, k1, k2); |
| 1194 |
} |
| 1195 |
|
| 1196 |
}; // key_compare class definition |
| 1197 |
|
| 1198 |
// value_compare class definition, it is defined as an inner class, |
| 1199 |
// using key_compare class to do comparison. |
| 1200 |
// |
| 1201 |
// The difference between key_compare and value_compare is the |
| 1202 |
// parameter its operator() function accepts, see the function |
| 1203 |
// signature. |
| 1204 |
// |
| 1205 |
class value_compare |
| 1206 |
{ |
| 1207 |
key_compare kc; |
| 1208 |
public: |
| 1209 |
value_compare(Db*pdb) : kc(pdb) |
| 1210 |
{ |
| 1211 |
|
| 1212 |
} |
| 1213 |
|
| 1214 |
bool operator()(const value_type& v1, |
| 1215 |
const value_type& v2) const |
| 1216 |
{ |
| 1217 |
|
| 1218 |
return kc(v1.first, v2.first); |
| 1219 |
} |
| 1220 |
|
| 1221 |
}; // value_compare class definition |
| 1222 |
|
| 1223 |
class hasher |
| 1224 |
{ |
| 1225 |
private: |
| 1226 |
Db*pdb; |
| 1227 |
public: |
| 1228 |
hasher(Db*db){pdb = db;} |
| 1229 |
size_t operator()(const kdt&k) const |
| 1230 |
{ |
| 1231 |
DBTYPE dbtype; |
| 1232 |
int ret; |
| 1233 |
|
| 1234 |
assert(pdb != NULL); |
| 1235 |
ret = pdb->get_type(&dbtype); |
| 1236 |
assert(ret == 0); |
| 1237 |
if (dbtype != DB_HASH) { |
| 1238 |
THROW(InvalidFunctionCall, ( |
| 1239 |
"db_map<>::hasher")); |
| 1240 |
} |
| 1241 |
h_hash_fcn_t hash = NULL; |
| 1242 |
BDBOP(pdb->get_h_hash(&hash), ret); |
| 1243 |
if (hash == NULL) |
| 1244 |
hash = hash_default; |
| 1245 |
return hash(pdb, &k, sizeof(k)); |
| 1246 |
} |
| 1247 |
}; // hasher |
| 1248 |
|
| 1249 |
class key_equal |
| 1250 |
{ |
| 1251 |
private: |
| 1252 |
Db*pdb; |
| 1253 |
public: |
| 1254 |
key_equal(Db*db){pdb = db;} |
| 1255 |
bool operator()(const kdt& kk1, const kdt&kk2) const |
| 1256 |
{ |
| 1257 |
DBTYPE dbtype; |
| 1258 |
kdt k1 = kk1, k2 = kk2; |
| 1259 |
int ret; |
| 1260 |
|
| 1261 |
dbstl_assert(pdb != NULL); |
| 1262 |
ret = pdb->get_type(&dbtype); |
| 1263 |
dbstl_assert(ret == 0); |
| 1264 |
if (dbtype != DB_HASH) { |
| 1265 |
THROW(InvalidFunctionCall, ( |
| 1266 |
"db_map<>::key_equal")); |
| 1267 |
} |
| 1268 |
|
| 1269 |
db_compare_fcn_t comp = NULL; |
| 1270 |
BDBOP(pdb->get_h_compare(&comp), ret); |
| 1271 |
if (comp == NULL) |
| 1272 |
return memcmp(&kk1, &kk2, sizeof(kdt)) == 0; |
| 1273 |
Dbt kd1(&k1, sizeof(k1)), kd2(&k2, sizeof(k2)); |
| 1274 |
|
| 1275 |
return comp(pdb, &kd1, &kd2) == 0; |
| 1276 |
|
| 1277 |
|
| 1278 |
} |
| 1279 |
|
| 1280 |
};// key_equal |
| 1281 |
//////////////////////////////////////////////////////////////// |
| 1282 |
|
| 1283 |
/// Function to get key compare functor. |
| 1284 |
/// Used when this container is a hash_map, hash_multimap, |
| 1285 |
/// hash_set or hash_multiset equivalent. |
| 1286 |
/// \return key_equal type of compare functor. |
| 1287 |
/// \sa http://www.sgi.com/tech/stl/hash_map.html |
| 1288 |
inline key_equal key_eq() const |
| 1289 |
{ |
| 1290 |
key_equal ke(this->get_db_handle()); |
| 1291 |
return ke; |
| 1292 |
} |
| 1293 |
|
| 1294 |
/// Function to get hash key generating functor. |
| 1295 |
/// Used when this container is a hash_map, hash_multimap, |
| 1296 |
/// hash_set or hash_multiset equivalent. |
| 1297 |
/// \return The hash key generating functor. |
| 1298 |
/// \sa http://www.sgi.com/tech/stl/hash_map.html |
| 1299 |
inline hasher hash_funct() const |
| 1300 |
{ |
| 1301 |
hasher h(this->get_db_handle()); |
| 1302 |
return h; |
| 1303 |
|
| 1304 |
} |
| 1305 |
|
| 1306 |
/// Function to get value compare functor. Used when this container |
| 1307 |
/// is a std::map, std::multimap, std::set or std::multiset equivalent. |
| 1308 |
/// \return The value compare functor. |
| 1309 |
/// \sa http://www.cplusplus.com/reference/stl/map/value_comp/ |
| 1310 |
inline value_compare value_comp() const |
| 1311 |
{ |
| 1312 |
value_compare vc(this->get_db_handle()); |
| 1313 |
return vc; |
| 1314 |
} |
| 1315 |
|
| 1316 |
/// Function to get key compare functor. Used when this container |
| 1317 |
/// is a std::map, std::multimap, std::set or std::multiset equivalent. |
| 1318 |
/// \return The key compare functor. |
| 1319 |
/// \sa http://www.cplusplus.com/reference/stl/map/key_comp/ |
| 1320 |
inline key_compare key_comp() const |
| 1321 |
{ |
| 1322 |
key_compare kc(this->get_db_handle()); |
| 1323 |
return kc; |
| 1324 |
} |
| 1325 |
|
| 1326 |
//////////////////////////////////////////////////////////////// |
| 1327 |
// Begin constructors and destructor definitions. |
| 1328 |
/// \name Constructors and destructor |
| 1329 |
//@{ |
| 1330 |
// We don't need the equal compare or allocator here, user need to |
| 1331 |
// call Db::set_bt_compare or Db::set_h_compare to set comparison |
| 1332 |
// function. |
| 1333 |
/// Create a std::map/hash_map equivalent associative container. |
| 1334 |
/// See the handle requirement in class details to pass correct |
| 1335 |
/// database/environment handles. |
| 1336 |
/// \param dbp The database handle. |
| 1337 |
/// \param envp The database environment handle. |
| 1338 |
/// \sa db_container(Db*, DbEnv*) |
| 1339 |
explicit db_map(Db *dbp = NULL, DbEnv* envp = NULL) : |
| 1340 |
db_container(dbp, envp) |
| 1341 |
{ |
| 1342 |
const char *errmsg; |
| 1343 |
|
| 1344 |
this->open_db_handles(dbp, envp, DB_BTREE, |
| 1345 |
DB_CREATE | DB_THREAD, 0); |
| 1346 |
|
| 1347 |
if ((errmsg = verify_config(dbp, envp)) != NULL) { |
| 1348 |
THROW(InvalidArgumentException, ("Db*", errmsg)); |
| 1349 |
} |
| 1350 |
this->set_db_handle_int(dbp, envp); |
| 1351 |
} |
| 1352 |
|
| 1353 |
/// Iteration constructor. Iterates between first and last, |
| 1354 |
/// setting a copy of each of the sequence of elements as the |
| 1355 |
/// content of the container object. |
| 1356 |
/// Create a std::map/hash_map equivalent associative container. |
| 1357 |
/// Insert a range of elements into the database. The range is |
| 1358 |
/// [first, last), which contains elements that can |
| 1359 |
/// be converted to type ddt automatically. |
| 1360 |
/// See the handle requirement in class details to pass correct |
| 1361 |
/// database/environment handles. |
| 1362 |
/// This function supports auto-commit. |
| 1363 |
/// \param dbp The database handle. |
| 1364 |
/// \param envp The database environment handle. |
| 1365 |
/// \param first The closed boundary of the range. |
| 1366 |
/// \param last The open boundary of the range. |
| 1367 |
/// \sa db_container(Db*, DbEnv*) |
| 1368 |
template <class InputIterator> |
| 1369 |
db_map(Db *dbp, DbEnv* envp, InputIterator first, |
| 1370 |
InputIterator last) : db_container(dbp, envp) |
| 1371 |
{ |
| 1372 |
const char *errmsg; |
| 1373 |
|
| 1374 |
this->open_db_handles(dbp, envp, DB_BTREE, |
| 1375 |
DB_CREATE | DB_THREAD, 0); |
| 1376 |
if ((errmsg = verify_config(dbp, envp)) != NULL) { |
| 1377 |
THROW(InvalidArgumentException, ("Db*", errmsg)); |
| 1378 |
} |
| 1379 |
this->set_db_handle_int(dbp, envp); |
| 1380 |
|
| 1381 |
this->begin_txn(); |
| 1382 |
try { |
| 1383 |
insert(first, last); |
| 1384 |
} catch (...) { |
| 1385 |
this->abort_txn(); |
| 1386 |
throw; |
| 1387 |
} |
| 1388 |
this->commit_txn(); |
| 1389 |
} |
| 1390 |
|
| 1391 |
// Copy constructor. The object is initialized to have the same |
| 1392 |
// contents as the x map object, do not copy properties because |
| 1393 |
// if we copy things like pdb, we are storing to the same db, so we |
| 1394 |
// create a new database, use it as the backing db, and store data |
| 1395 |
// into it. |
| 1396 |
/// Copy constructor. |
| 1397 |
/// Create an database and insert all key/data pairs in x into this |
| 1398 |
/// container. x's data members are not copied. |
| 1399 |
/// This function supports auto-commit. |
| 1400 |
/// \param x The other container to initialize this container. |
| 1401 |
/// \sa db_container(const db_container&) |
| 1402 |
db_map(const db_map<kdt, ddt, value_type_sub, iterator>& x) : |
| 1403 |
db_container(x) |
| 1404 |
{ |
| 1405 |
verify_db_handles(x); |
| 1406 |
this->set_db_handle_int(this->clone_db_config( |
| 1407 |
x.get_db_handle()), x.get_db_env_handle()); |
| 1408 |
assert(this->get_db_handle() != NULL); |
| 1409 |
|
| 1410 |
this->begin_txn(); |
| 1411 |
try { |
| 1412 |
copy_db((db_map<kdt, ddt, value_type_sub, iterator>&)x); |
| 1413 |
} catch (...) { |
| 1414 |
this->abort_txn(); |
| 1415 |
throw; |
| 1416 |
} |
| 1417 |
this->commit_txn(); |
| 1418 |
} |
| 1419 |
|
| 1420 |
virtual ~db_map(){} |
| 1421 |
//@} |
| 1422 |
//////////////////////////////////////////////////////////////// |
| 1423 |
|
| 1424 |
//////////////////////////////////////////////////////////////// |
| 1425 |
// Begin insert function definitions. |
| 1426 |
/// Container content assignment operator. |
| 1427 |
/// This function supports auto-commit. |
| 1428 |
/// \param x The other container whose key/data pairs will be inserted |
| 1429 |
/// into this container. Old content in this containers are discarded. |
| 1430 |
/// \sa http://www.cplusplus.com/reference/stl/map/operator=/ |
| 1431 |
inline const self& operator=(const self& x) |
| 1432 |
{ |
| 1433 |
ASSIGNMENT_PREDCOND(x) |
| 1434 |
db_container::operator =(x); |
| 1435 |
verify_db_handles(x); |
| 1436 |
assert(this->get_db_handle() != NULL); |
| 1437 |
this->begin_txn(); |
| 1438 |
try { |
| 1439 |
copy_db((self &)x); |
| 1440 |
} catch (...) { |
| 1441 |
this->abort_txn(); |
| 1442 |
throw; |
| 1443 |
} |
| 1444 |
this->commit_txn(); |
| 1445 |
return x; |
| 1446 |
} |
| 1447 |
|
| 1448 |
/// \name Insert Functions |
| 1449 |
/// They have similiar usage as their C++ STL equivalents. |
| 1450 |
/// Note that when secondary index is enabled, each |
| 1451 |
/// db_container can create a db_multimap secondary container, |
| 1452 |
/// but the insert function is not functional for secondary containers. |
| 1453 |
/// \sa http://www.cplusplus.com/reference/stl/map/insert/ |
| 1454 |
//@{ |
| 1455 |
// |
| 1456 |
// Insert functions. Note that stl requires if the entry with x.key |
| 1457 |
// already exists, insert should not overwrite that entry and the |
| 1458 |
// insert should fail; but bdb Dbc::cursor(DB_KEYLAST) will replace |
| 1459 |
// existing data with new one, so we will first find whether we |
| 1460 |
// have this data, if have, return false; |
| 1461 |
// |
| 1462 |
// Can not internally use begin/commit_txn to wrap this call because |
| 1463 |
// it returns an iterator, which is closed after commit_txn(), and |
| 1464 |
// reopening it is wrong in multithreaded access. |
| 1465 |
/// Insert a single key/data pair if the key is not in the container. |
| 1466 |
/// \param x The key/data pair to insert. |
| 1467 |
/// \return A pair P, if insert OK, i.e. the inserted key wasn't in the |
| 1468 |
/// container, P.first will be the iterator sitting on the inserted |
| 1469 |
/// key/data pair, and P.second is true; otherwise P.first is an |
| 1470 |
/// invalid iterator and P.second is false. |
| 1471 |
pair<iterator,bool> insert (const value_type& x ) |
| 1472 |
{ |
| 1473 |
pair<iterator,bool> ib; |
| 1474 |
iterator witr; |
| 1475 |
|
| 1476 |
init_itr(witr); |
| 1477 |
open_itr(witr); |
| 1478 |
|
| 1479 |
if (witr.move_to(x.first) == 0) {// has it |
| 1480 |
ib.first = witr; |
| 1481 |
ib.second = false; |
| 1482 |
// Cursor movements are not logged, no need to |
| 1483 |
// use transaction here. |
| 1484 |
return ib; |
| 1485 |
} |
| 1486 |
|
| 1487 |
witr.itr_status_ = witr.pcsr_->insert(x.first, x.second, |
| 1488 |
DB_KEYLAST); |
| 1489 |
assert(witr.itr_status_ == 0); |
| 1490 |
witr.refresh(false); |
| 1491 |
ib.first = witr; |
| 1492 |
ib.second = true; |
| 1493 |
|
| 1494 |
return ib; |
| 1495 |
} |
| 1496 |
|
| 1497 |
/// Insert with hint position. We ignore the hint position because |
| 1498 |
/// Berkeley DB knows better where to insert. |
| 1499 |
/// \param position The hint position. |
| 1500 |
/// \param x The key/data pair to insert. |
| 1501 |
/// \return The iterator sitting on the inserted key/data pair, or an |
| 1502 |
/// invalid iterator if the key was already in the container. |
| 1503 |
inline iterator insert (iterator position, const value_type& x ) |
| 1504 |
{ |
| 1505 |
pair<iterator,bool> ib = insert(x); |
| 1506 |
return ib.first; |
| 1507 |
} |
| 1508 |
|
| 1509 |
// Member function template overload. |
| 1510 |
/// Range insertion. Insert a range [first, last) of key/data pairs |
| 1511 |
/// into this container. |
| 1512 |
/// \param first The closed boundary of the range. |
| 1513 |
/// \param last The open boundary of the range. |
| 1514 |
void insert (const db_map_base_iterator<kdt, realddt, ddt>& first, |
| 1515 |
const db_map_base_iterator<kdt, realddt, ddt>& last) |
| 1516 |
{ |
| 1517 |
db_map_base_iterator<kdt, realddt, ddt> ii; |
| 1518 |
iterator witr; |
| 1519 |
|
| 1520 |
init_itr(witr); |
| 1521 |
open_itr(witr); |
| 1522 |
|
| 1523 |
for (ii = first; ii != last; ++ii) |
| 1524 |
witr.pcsr_->insert(ii->first, ii->second, |
| 1525 |
DB_KEYLAST); |
| 1526 |
} |
| 1527 |
|
| 1528 |
/// Range insertion. Insert a range [first, last) of key/data pairs |
| 1529 |
/// into this container. |
| 1530 |
/// \param first The closed boundary of the range. |
| 1531 |
/// \param last The open boundary of the range. |
| 1532 |
template<typename InputIterator> |
| 1533 |
void insert (InputIterator first, InputIterator last) |
| 1534 |
{ |
| 1535 |
InputIterator ii; |
| 1536 |
iterator witr; |
| 1537 |
|
| 1538 |
init_itr(witr); |
| 1539 |
open_itr(witr); |
| 1540 |
|
| 1541 |
for (ii = first; ii != last; ++ii) |
| 1542 |
witr.pcsr_->insert(ii->first, ii->second, |
| 1543 |
DB_KEYLAST); |
| 1544 |
} |
| 1545 |
//@} |
| 1546 |
//////////////////////////////////////////////////////////////// |
| 1547 |
|
| 1548 |
//////////////////////////////////////////////////////////////// |
| 1549 |
// Begin functions that create iterators. |
| 1550 |
/// \name Iterator Functions |
| 1551 |
/// The parameters in begin functions of this group have identical |
| 1552 |
/// meaning to thoes in db_vector::begin, refer to those functions |
| 1553 |
/// for details. |
| 1554 |
/// \sa db_vector::begin() |
| 1555 |
//@{ |
| 1556 |
/// Begin a read-write or readonly iterator which sits on the first |
| 1557 |
/// key/data pair of the database. |
| 1558 |
/// \param rmw Same as that of |
| 1559 |
/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1560 |
/// \param bulkretrieval Same as that of |
| 1561 |
/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1562 |
/// \param directdb_get Same as that of |
| 1563 |
/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1564 |
/// \param readonly Same as that of |
| 1565 |
/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1566 |
/// \return The created iterator. |
| 1567 |
/// \sa db_vector::begin(ReadModifyWriteOption, bool, |
| 1568 |
/// BulkRetrievalOption, bool) |
| 1569 |
// |
| 1570 |
iterator begin(ReadModifyWriteOption rmw = |
| 1571 |
ReadModifyWriteOption::no_read_modify_write(), |
| 1572 |
bool readonly = false, BulkRetrievalOption bulkretrieval = |
| 1573 |
BulkRetrievalOption::no_bulk_retrieval(), |
| 1574 |
bool directdb_get = true) |
| 1575 |
{ |
| 1576 |
bool b_rmw; |
| 1577 |
u_int32_t bulk_retrieval = 0; |
| 1578 |
|
| 1579 |
b_rmw = (rmw == ReadModifyWriteOption::read_modify_write()); |
| 1580 |
// Read only cursor don't need acquire write lock. |
| 1581 |
if (readonly && b_rmw) |
| 1582 |
b_rmw = false; |
| 1583 |
if (readonly && bulkretrieval == BulkRetrievalOption:: |
| 1584 |
BulkRetrieval) |
| 1585 |
bulk_retrieval = bulkretrieval.bulk_buf_size(); |
| 1586 |
|
| 1587 |
iterator itr(dynamic_cast<db_container*>(this), |
| 1588 |
bulk_retrieval, b_rmw, directdb_get, readonly); |
| 1589 |
|
| 1590 |
open_itr(itr, readonly); |
| 1591 |
itr.first(); |
| 1592 |
return itr; |
| 1593 |
} |
| 1594 |
|
| 1595 |
/// Begin a read-only iterator. |
| 1596 |
/// \param bulkretrieval Same as that of |
| 1597 |
/// begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1598 |
/// \param directdb_get Same as that of |
| 1599 |
/// begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1600 |
/// \return The created const iterator. |
| 1601 |
/// \sa db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, |
| 1602 |
/// bool); |
| 1603 |
const_iterator begin(BulkRetrievalOption bulkretrieval = |
| 1604 |
BulkRetrievalOption::no_bulk_retrieval(), |
| 1605 |
bool directdb_get = true) const |
| 1606 |
{ |
| 1607 |
u_int32_t b_bulk_retrieval = (bulkretrieval == |
| 1608 |
BulkRetrievalOption::BulkRetrieval ? |
| 1609 |
bulkretrieval.bulk_buf_size() : 0); |
| 1610 |
|
| 1611 |
const_iterator itr((db_container*)this, |
| 1612 |
b_bulk_retrieval, false, directdb_get, true); |
| 1613 |
|
| 1614 |
open_itr(itr, true); |
| 1615 |
itr.first(); |
| 1616 |
return itr; |
| 1617 |
} |
| 1618 |
|
| 1619 |
/// \brief Create an open boundary iterator. |
| 1620 |
/// \return Returns an invalid iterator denoting the position after |
| 1621 |
/// the last valid element of the container. |
| 1622 |
/// \sa db_vector::end() |
| 1623 |
inline iterator end() |
| 1624 |
{ |
| 1625 |
iterator itr; |
| 1626 |
|
| 1627 |
// end() is at an invalid position. We don't know what key it |
| 1628 |
// refers, so itr_status_ and inval_pos_type are the only |
| 1629 |
// data members to identify an iterator's position. |
| 1630 |
// |
| 1631 |
itr.itr_status_ = INVALID_ITERATOR_POSITION; |
| 1632 |
itr.inval_pos_type_ = iterator::IPT_AFTER_LAST; |
| 1633 |
itr.owner_ = (db_container*)this; |
| 1634 |
return itr; |
| 1635 |
} |
| 1636 |
|
| 1637 |
/// \brief Create an open boundary iterator. |
| 1638 |
/// \return Returns an invalid const iterator denoting the position |
| 1639 |
/// after the last valid element of the container. |
| 1640 |
/// \sa db_vector::end() const |
| 1641 |
inline const_iterator end() const |
| 1642 |
{ |
| 1643 |
const_iterator itr; |
| 1644 |
|
| 1645 |
// end() is at an invalid position. We don't know what key it |
| 1646 |
// refers, so itr_status_ and inval_pos_type are the only |
| 1647 |
// data members to identify an iterator's position. |
| 1648 |
// |
| 1649 |
itr.itr_status_ = INVALID_ITERATOR_POSITION; |
| 1650 |
itr.inval_pos_type_ = iterator::IPT_AFTER_LAST; |
| 1651 |
itr.owner_ = (db_container*)this; |
| 1652 |
return itr; |
| 1653 |
} |
| 1654 |
|
| 1655 |
/// Begin a read-write or readonly reverse iterator which sits on the |
| 1656 |
/// first key/data pair of the database. |
| 1657 |
/// \param rmw Same as that of |
| 1658 |
/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1659 |
/// \param bulkretrieval Same as that of |
| 1660 |
/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1661 |
/// \param directdb_get Same as that of |
| 1662 |
/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1663 |
/// \param read_only Same as that of |
| 1664 |
/// db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1665 |
/// \return The created iterator. |
| 1666 |
/// \sa db_vector::begin(ReadModifyWriteOption, bool, |
| 1667 |
/// BulkRetrievalOption, bool) |
| 1668 |
/// \sa db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, |
| 1669 |
/// bool); |
| 1670 |
reverse_iterator rbegin(ReadModifyWriteOption rmw = |
| 1671 |
ReadModifyWriteOption::no_read_modify_write(), |
| 1672 |
bool read_only = false, BulkRetrievalOption bulkretrieval = |
| 1673 |
BulkRetrievalOption::no_bulk_retrieval(), |
| 1674 |
bool directdb_get = true) |
| 1675 |
{ |
| 1676 |
u_int32_t bulk_retrieval = 0; |
| 1677 |
|
| 1678 |
iterator itr = end(); |
| 1679 |
itr.rmw_csr_ = (rmw == ( |
| 1680 |
ReadModifyWriteOption::read_modify_write())) && !read_only; |
| 1681 |
itr.directdb_get_ = directdb_get; |
| 1682 |
itr.read_only_ = read_only; |
| 1683 |
if (read_only && bulkretrieval == BulkRetrievalOption:: |
| 1684 |
BulkRetrieval) |
| 1685 |
bulk_retrieval = bulkretrieval.bulk_buf_size(); |
| 1686 |
itr.bulk_retrieval_ = bulk_retrieval; |
| 1687 |
reverse_iterator ritr(itr); |
| 1688 |
|
| 1689 |
return ritr; |
| 1690 |
} |
| 1691 |
|
| 1692 |
/// Begin a read-only reverse iterator. |
| 1693 |
/// \param bulkretrieval Same as that of |
| 1694 |
/// begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1695 |
/// \param directdb_get Same as that of |
| 1696 |
/// begin(ReadModifyWrite, bool, BulkRetrievalOption, bool); |
| 1697 |
/// \return The created const iterator. |
| 1698 |
/// \sa db_vector::begin(ReadModifyWrite, bool, BulkRetrievalOption, |
| 1699 |
/// bool); |
| 1700 |
const_reverse_iterator rbegin(BulkRetrievalOption bulkretrieval = |
| 1701 |
BulkRetrievalOption::no_bulk_retrieval(), |
| 1702 |
bool directdb_get = true) const |
| 1703 |
{ |
| 1704 |
const_iterator itr = end(); |
| 1705 |
itr.bulk_retrieval_ = (bulkretrieval == |
| 1706 |
BulkRetrievalOption::BulkRetrieval ? |
| 1707 |
bulkretrieval.bulk_buf_size() : 0); |
| 1708 |
itr.directdb_get_ = directdb_get; |
| 1709 |
itr.read_only_ = true; |
| 1710 |
const_reverse_iterator ritr(itr); |
| 1711 |
|
| 1712 |
return ritr; |
| 1713 |
} |
| 1714 |
|
| 1715 |
/// \brief Create an open boundary iterator. |
| 1716 |
/// \return Returns an invalid iterator denoting the position |
| 1717 |
/// before the first valid element of the container. |
| 1718 |
/// \sa db_vector::rend() |
| 1719 |
inline reverse_iterator rend() |
| 1720 |
{ |
| 1721 |
reverse_iterator ritr; |
| 1722 |
ritr.inval_pos_type_ = iterator::IPT_BEFORE_FIRST; |
| 1723 |
return ritr; |
| 1724 |
} |
| 1725 |
|
| 1726 |
/// \brief Create an open boundary iterator. |
| 1727 |
/// \return Returns an invalid const iterator denoting the position |
| 1728 |
/// before the first valid element of the container. |
| 1729 |
/// \sa db_vector::rend() const |
| 1730 |
inline const_reverse_iterator rend() const |
| 1731 |
{ |
| 1732 |
const_reverse_iterator ritr; |
| 1733 |
ritr.inval_pos_type_ = iterator::IPT_BEFORE_FIRST; |
| 1734 |
return ritr; |
| 1735 |
} |
| 1736 |
//@} // iterator functions |
| 1737 |
//////////////////////////////////////////////////////////////// |
| 1738 |
|
| 1739 |
//////////////////////////////////////////////////////////////// |
| 1740 |
// |
| 1741 |
// Begin functions that return container metadata. |
| 1742 |
/// \name Metadata Functions |
| 1743 |
/// These functions return metadata about the container. |
| 1744 |
//@{ |
| 1745 |
/// Get container category. |
| 1746 |
/// Determines whether this container object is a std::map<> |
| 1747 |
/// equivalent(when returns false) or that of hash_map<> |
| 1748 |
/// class(when returns true). This method is not in stl, but it |
| 1749 |
/// may be called by users because some operations are not supported |
| 1750 |
/// by both type(map/hash_map) of containers, you need to call this |
| 1751 |
/// function to distinguish the two types. dbstl will not stop you |
| 1752 |
/// from calling the wrong methods of this class. |
| 1753 |
/// \return Returns true if this container is a hash container based |
| 1754 |
/// on a Berkeley DB hash database; returns false if it is based on a |
| 1755 |
/// Berkeley DB btree database. |
| 1756 |
// |
| 1757 |
inline bool is_hash() const |
| 1758 |
{ |
| 1759 |
DBTYPE dbtype = DB_UNKNOWN; |
| 1760 |
int ret; |
| 1761 |
|
| 1762 |
assert(this->get_db_handle() != NULL); |
| 1763 |
ret = this->get_db_handle()->get_type(&dbtype); |
| 1764 |
assert(ret == 0); |
| 1765 |
return dbtype == DB_HASH; |
| 1766 |
} |
| 1767 |
|
| 1768 |
/// Only for std::hash_map, return number of hash bucket in use. |
| 1769 |
/// This function supports auto-commit. |
| 1770 |
/// \return The number of hash buckets of the database. |
| 1771 |
size_type bucket_count() const |
| 1772 |
{ |
| 1773 |
DBTYPE dbtype; |
| 1774 |
u_int32_t flags; |
| 1775 |
void *sp; |
| 1776 |
size_type sz; |
| 1777 |
int ret; |
| 1778 |
DbTxn*txn; |
| 1779 |
|
| 1780 |
assert(this->get_db_handle() != NULL); |
| 1781 |
ret = this->get_db_handle()->get_type(&dbtype); |
| 1782 |
assert(ret == 0); |
| 1783 |
if (dbtype != DB_HASH) { |
| 1784 |
THROW(InvalidFunctionCall, ("db_map<>::bucket_count")); |
| 1785 |
|
| 1786 |
} |
| 1787 |
flags = DB_FAST_STAT; |
| 1788 |
|
| 1789 |
// Here we use current_txn(), so we will get a valid |
| 1790 |
// transaction handle if we are using explicit transactions; |
| 1791 |
// and NULL if we are using autocommit, in which case bdb |
| 1792 |
// internal auto commit will be enabled automatically. |
| 1793 |
// |
| 1794 |
txn = ResourceManager::instance()-> |
| 1795 |
current_txn(this->get_db_handle()->get_env()); |
| 1796 |
BDBOP(this->get_db_handle()->stat(txn, &sp, flags), ret); |
| 1797 |
|
| 1798 |
sz = (size_type)(((DB_HASH_STAT*)sp)->hash_buckets); |
| 1799 |
free(sp); |
| 1800 |
return sz; |
| 1801 |
} |
| 1802 |
|
| 1803 |
/// Get container size. |
| 1804 |
// Return size of the map, can control whether compute |
| 1805 |
// accurately(slower if db is huge) or not. |
| 1806 |
/// This function supports auto-commit. |
| 1807 |
/// \return Return the number of key/data pairs in the container. |
| 1808 |
/// \param accurate This function uses database's statistics to get |
| 1809 |
/// the number of key/data pairs. The statistics mechanism will either |
| 1810 |
/// scan the whole database to find the accurate number or use the |
| 1811 |
/// number of last accurate scanning, and thus much faster. If there |
| 1812 |
/// are millions of key/data pairs, the scanning can take some while, |
| 1813 |
/// so in that case you may want to set the "accurate" parameter to |
| 1814 |
/// false. |
| 1815 |
size_type size(bool accurate = true) const |
| 1816 |
{ |
| 1817 |
u_int32_t flags; |
| 1818 |
void *sp; |
| 1819 |
DBTYPE dbtype; |
| 1820 |
size_t sz; |
| 1821 |
int ret; |
| 1822 |
DbTxn*txn; |
| 1823 |
|
| 1824 |
flags = accurate ? 0 : DB_FAST_STAT; |
| 1825 |
BDBOP(this->get_db_handle()->get_type(&dbtype), ret); |
| 1826 |
|
| 1827 |
// Here we use current_txn(), so we will get a valid |
| 1828 |
// transaction handle if we are using explicit transactions; |
| 1829 |
// and NULL if we are using autocommit, in which case bdb |
| 1830 |
// internal auto commit will be enabled automatically. |
| 1831 |
// |
| 1832 |
txn = ResourceManager::instance()-> |
| 1833 |
current_txn(this->get_db_handle()->get_env()); |
| 1834 |
BDBOP(this->get_db_handle()->stat(txn, &sp, flags), ret); |
| 1835 |
|
| 1836 |
assert((dbtype == DB_BTREE) || (dbtype == DB_HASH)); |
| 1837 |
// dbtype is BTREE OR HASH, no others. |
| 1838 |
sz = dbtype == DB_BTREE ? ((DB_BTREE_STAT*)sp)-> |
| 1839 |
bt_ndata : ((DB_HASH_STAT*)sp)->hash_ndata; |
| 1840 |
free(sp); |
| 1841 |
return sz; |
| 1842 |
} |
| 1843 |
|
| 1844 |
/// Get max size. |
| 1845 |
/// The returned size is not the actual limit of database. See the |
| 1846 |
/// Berkeley DB limits to get real max size. |
| 1847 |
/// \return A meaningless huge number. |
| 1848 |
/// \sa db_vector::max_size() |
| 1849 |
inline size_type max_size() const |
| 1850 |
{ |
| 1851 |
return SIZE_T_MAX; |
| 1852 |
} |
| 1853 |
|
| 1854 |
/// Returns whether this container is empty. |
| 1855 |
/// This function supports auto-commit. |
| 1856 |
/// \return True if empty, false otherwise. |
| 1857 |
bool empty() const |
| 1858 |
{ |
| 1859 |
// If we fail to move to the first record, the db is |
| 1860 |
// supposed to be empty. |
| 1861 |
const_iterator witr; |
| 1862 |
bool ret; |
| 1863 |
|
| 1864 |
try { |
| 1865 |
this->begin_txn(); |
| 1866 |
init_itr(witr); |
| 1867 |
open_itr(witr, true); |
| 1868 |
ret = witr.first() != 0; |
| 1869 |
this->commit_txn(); |
| 1870 |
return ret; |
| 1871 |
} catch (...) { |
| 1872 |
this->abort_txn(); |
| 1873 |
throw; |
| 1874 |
} |
| 1875 |
} |
| 1876 |
//@} |
| 1877 |
//////////////////////////////////////////////////////////////// |
| 1878 |
|
| 1879 |
//////////////////////////////////////////////////////////////// |
| 1880 |
// Begin element accessors. |
| 1881 |
// |
| 1882 |
// Don't use transaction wrapper(begin/commit_txn) here because |
| 1883 |
// even insert(the only logged operation) is only part of the |
| 1884 |
// whole expression---the use case is dmmap[key] = value; |
| 1885 |
// So insert and another put call should |
| 1886 |
// be atomic, so there must be an outside transaction. |
| 1887 |
// |
| 1888 |
// As stated in STL specification, this method can't have "const" |
| 1889 |
// modifier because it is likely to insert a new record. |
| 1890 |
/// Retrieve data element by key. |
| 1891 |
/// This function returns an reference to the underlying data element |
| 1892 |
/// of the specified key x. The returned object can be used to read or |
| 1893 |
/// write the data element of the key/data pair. |
| 1894 |
/// Do use a data_type_wrap of db_map or value_type::second_type(they |
| 1895 |
/// are the same) type of variable to hold the return value of this |
| 1896 |
/// function. |
| 1897 |
/// \param x The target key to get value from. |
| 1898 |
/// \return Data element reference. |
| 1899 |
// |
| 1900 |
data_type_wrap operator[] (const key_type& x) |
| 1901 |
{ |
| 1902 |
iterator witr, *pitr; |
| 1903 |
int ret; |
| 1904 |
|
| 1905 |
init_itr(witr); |
| 1906 |
open_itr(witr, false); |
| 1907 |
|
| 1908 |
if (witr.move_to(x) != 0) { |
| 1909 |
ddt d;//default value |
| 1910 |
DbstlInitializeDefault<ddt> initdef(d); |
| 1911 |
// Insert (x, d) as place holder. |
| 1912 |
witr.pcsr_->insert(x, d, DB_KEYLAST); |
| 1913 |
// Should be OK this time. |
| 1914 |
ret = witr.move_to(x); |
| 1915 |
assert(ret == 0); |
| 1916 |
// Return the reference to the data item of x. |
| 1917 |
} |
| 1918 |
|
| 1919 |
//witr->curpair_.second._DB_STL_SetDelItr(); |
| 1920 |
pitr = new iterator(witr); |
| 1921 |
data_type_wrap ref(pitr->curpair_.second); |
| 1922 |
ref._DB_STL_SetDelItr(); |
| 1923 |
return ref; |
| 1924 |
} |
| 1925 |
|
| 1926 |
// Only returns a right-value, no left value for assignment, so |
| 1927 |
// directly return the value rather than the ElementRef/ElementHolder |
| 1928 |
// wrapper. Must use a const reference to this container to call this |
| 1929 |
// const function. |
| 1930 |
// |
| 1931 |
/// Retrieve data element by key. |
| 1932 |
/// This function returns the value of the underlying data element of |
| 1933 |
/// specified key x. You can only read the element, but unable to |
| 1934 |
/// update the element via the return value of this function. And you |
| 1935 |
/// need to use the container's const reference to call this method. |
| 1936 |
/// \param x The target key to get value from. |
| 1937 |
/// \return Data element, read only, can't be used to modify it. |
| 1938 |
const ddt operator[] (const key_type& x) const |
| 1939 |
{ |
| 1940 |
iterator witr; |
| 1941 |
|
| 1942 |
init_itr(witr); |
| 1943 |
open_itr(witr); |
| 1944 |
|
| 1945 |
// x is supposed to be in this map. |
| 1946 |
if (witr.move_to(x) != 0) { |
| 1947 |
THROW0(NoSuchKeyException); |
| 1948 |
|
| 1949 |
} |
| 1950 |
return witr.curpair_.second._DB_STL_value(); |
| 1951 |
} |
| 1952 |
//////////////////////////////////////////////////////////////// |
| 1953 |
|
| 1954 |
//////////////////////////////////////////////////////////////// |
| 1955 |
// Begin functions that erase elements from the container. |
| 1956 |
// |
| 1957 |
// Can not reopen external/outside iterator's cursor, pos must |
| 1958 |
// already be in a transactional context. |
| 1959 |
// There is identical function in db_multimap<> and db_multiset |
| 1960 |
// for this function, we MUST keep the code consistent on update! |
| 1961 |
// Go to db_multimap<>::erase(const key_type&) to see why. |
| 1962 |
/// \name Erase Functions |
| 1963 |
/// \sa http://www.cplusplus.com/reference/stl/map/erase/ |
| 1964 |
//@{ |
| 1965 |
/// Erase a key/data pair at specified position. |
| 1966 |
/// \param pos An valid iterator of this container to erase. |
| 1967 |
inline void erase (iterator pos) |
| 1968 |
{ |
| 1969 |
if (pos == end()) |
| 1970 |
return; |
| 1971 |
pos.pcsr_->del(); |
| 1972 |
} |
| 1973 |
|
| 1974 |
/// Erase elements by key. |
| 1975 |
/// All key/data pairs with specified key x will be removed from |
| 1976 |
/// underlying database. |
| 1977 |
/// This function supports auto-commit. |
| 1978 |
/// \param x The key to remove from the container. |
| 1979 |
/// \return The number of key/data pairs removed. |
| 1980 |
// There is identical function in db_multimap<> and db_multiset |
| 1981 |
// for this function, we MUST keep the code consistent on update! |
| 1982 |
// Go to db_multimap<>::erase(const key_type&) to see why. |
| 1983 |
// |
| 1984 |
size_type erase (const key_type& x) |
| 1985 |
{ |
| 1986 |
size_type cnt; |
| 1987 |
iterator itr; |
| 1988 |
|
| 1989 |
this->begin_txn(); |
| 1990 |
try { |
| 1991 |
pair<iterator, iterator> rg = equal_range(x); |
| 1992 |
for (itr = rg.first, cnt = 0; itr != rg.second; ++itr) { |
| 1993 |
cnt++; |
| 1994 |
itr.pcsr_->del(); |
| 1995 |
} |
| 1996 |
} catch (...) { |
| 1997 |
this->abort_txn(); |
| 1998 |
throw; |
| 1999 |
} |
| 2000 |
this->commit_txn(); |
| 2001 |
return cnt; |
| 2002 |
} |
| 2003 |
|
| 2004 |
// Can not be auto commit because first and last are already open. |
| 2005 |
// There is identical function in db_multimap<> and db_multiset |
| 2006 |
// for this function, we MUST keep the code consistent on update! |
| 2007 |
// Go to db_multimap<>::erase(const key_type&) to see why. |
| 2008 |
/// Range erase. Erase all key/data pairs within the valid range |
| 2009 |
/// [first, last). |
| 2010 |
/// \param first The closed boundary of the range. |
| 2011 |
/// \param last The open boundary of the range. |
| 2012 |
inline void erase (iterator first, iterator last) |
| 2013 |
{ |
| 2014 |
iterator i; |
| 2015 |
|
| 2016 |
for (i = first; i != last; ++i) |
| 2017 |
i.pcsr_->del(); |
| 2018 |
} |
| 2019 |
//@} |
| 2020 |
|
| 2021 |
/// Swap content with container mp. |
| 2022 |
/// This function supports auto-commit. |
| 2023 |
/// \param mp The container to swap content with. |
| 2024 |
/// \param b_truncate: See db_vector::swap() for details. |
| 2025 |
/// \sa http://www.cplusplus.com/reference/stl/map/swap/ |
| 2026 |
/// db_vector::clear() |
| 2027 |
void swap (db_map<kdt, ddt, value_type_sub>& mp, bool b_truncate = true) |
| 2028 |
{ |
| 2029 |
Db *swapdb = NULL; |
| 2030 |
std::string dbfname(64, '\0'); |
| 2031 |
|
| 2032 |
verify_db_handles(mp); |
| 2033 |
this->begin_txn(); |
| 2034 |
try { |
| 2035 |
swapdb = this->clone_db_config(this->get_db_handle(), |
| 2036 |
dbfname); |
| 2037 |
db_map<kdt, ddt, value_type_sub> tmap(swapdb, |
| 2038 |
swapdb->get_env(), begin(), end()); |
| 2039 |
clear(b_truncate);// Clear this db_map<> object. |
| 2040 |
typename db_map<kdt, ddt, value_type_sub>:: |
| 2041 |
iterator itr1, itr2; |
| 2042 |
itr1 = mp.begin(); |
| 2043 |
itr2 = mp.end(); |
| 2044 |
insert(itr1, itr2); |
| 2045 |
mp.clear(b_truncate); |
| 2046 |
itr1 = tmap.begin(); |
| 2047 |
itr2 = tmap.end(); |
| 2048 |
mp.insert(itr1, itr2); |
| 2049 |
tmap.clear(); |
| 2050 |
|
| 2051 |
swapdb->close(0); |
| 2052 |
if (dbfname[0] != '\0') { |
| 2053 |
swapdb = new Db(NULL, DB_CXX_NO_EXCEPTIONS); |
| 2054 |
swapdb->remove(dbfname.c_str(), NULL, 0); |
| 2055 |
swapdb->close(0); |
| 2056 |
delete swapdb; |
| 2057 |
} |
| 2058 |
} catch (...) { |
| 2059 |
this->abort_txn(); |
| 2060 |
throw; |
| 2061 |
} |
| 2062 |
this->commit_txn(); |
| 2063 |
} |
| 2064 |
|
| 2065 |
/// Clear contents in this container. |
| 2066 |
/// This function supports auto-commit. |
| 2067 |
/// \param b_truncate See db_vector::clear(bool) for details. |
| 2068 |
/// \sa db_vector::clear(bool) |
| 2069 |
void clear (bool b_truncate = true) |
| 2070 |
{ |
| 2071 |
int ret; |
| 2072 |
u_int32_t flag; |
| 2073 |
DbEnv *penv = this->get_db_handle()->get_env(); |
| 2074 |
|
| 2075 |
if (b_truncate) { |
| 2076 |
ResourceManager::instance()->close_db_cursors( |
| 2077 |
this->get_db_handle()); |
| 2078 |
BDBOP2(this->get_db_handle()->truncate( |
| 2079 |
ResourceManager::instance()->current_txn(penv), |
| 2080 |
NULL, 0), ret, this->abort_txn()); |
| 2081 |
} else { |
| 2082 |
ReadModifyWriteOption brmw( |
| 2083 |
ReadModifyWriteOption::no_read_modify_write()); |
| 2084 |
|
| 2085 |
BDBOP(penv->get_open_flags(&flag), ret); |
| 2086 |
|
| 2087 |
// DB_RMW flag requires locking subsystem started. |
| 2088 |
if ((flag & DB_INIT_LOCK) || (flag & DB_INIT_CDB) || |
| 2089 |
(flag & DB_INIT_TXN)) |
| 2090 |
brmw = |
| 2091 |
ReadModifyWriteOption::read_modify_write(); |
| 2092 |
try { |
| 2093 |
// In if branch, truncate is capable of |
| 2094 |
// autocommit internally. |
| 2095 |
this->begin_txn(); |
| 2096 |
erase(begin(brmw, false), end()); |
| 2097 |
this->commit_txn(); |
| 2098 |
} catch (...) { |
| 2099 |
this->abort_txn(); |
| 2100 |
throw; |
| 2101 |
} |
| 2102 |
} |
| 2103 |
} |
| 2104 |
//////////////////////////////////////////////////////////////// |
| 2105 |
|
| 2106 |
//////////////////////////////////////////////////////////////// |
| 2107 |
// Begin functions that searches a key in the map. |
| 2108 |
/// \name Searching Functions |
| 2109 |
/// The following functions are returning iterators, and they by |
| 2110 |
/// default return read-write iterators. If you intend to use the |
| 2111 |
/// returned iterator only to read, you should call the const version |
| 2112 |
/// of each function using a const reference to this container. |
| 2113 |
/// Using const iterators can potentially promote concurrency a lot. |
| 2114 |
/// You can also set the readonly parameter to each non-const version |
| 2115 |
/// of the functions to true if you don't use the returned iterator |
| 2116 |
/// to write, which also promotes concurrency and overall performance. |
| 2117 |
//@{ |
| 2118 |
/// Find the key/data pair with specified key x. |
| 2119 |
/// \param x The target key to find. |
| 2120 |
/// \return The valid const iterator sitting on the key x, or an |
| 2121 |
/// invalid one. |
| 2122 |
/// \sa http://www.cplusplus.com/reference/stl/map/find/ |
| 2123 |
const_iterator find (const key_type& x) const |
| 2124 |
{ |
| 2125 |
const_iterator witr; |
| 2126 |
|
| 2127 |
init_itr(witr); |
| 2128 |
open_itr(witr, true); |
| 2129 |
if (witr.move_to(x)) |
| 2130 |
return ((self *)this)->end(); |
| 2131 |
|
| 2132 |
return witr; |
| 2133 |
} |
| 2134 |
|
| 2135 |
/// Find the greatest key less than or equal to x. |
| 2136 |
/// \param x The target key to find. |
| 2137 |
/// \return The valid const iterator sitting on the key, or an |
| 2138 |
/// invalid one. |
| 2139 |
/// \sa http://www.cplusplus.com/reference/stl/map/lower_bound/ |
| 2140 |
const_iterator lower_bound (const key_type& x) const |
| 2141 |
{ |
| 2142 |
const_iterator witr; |
| 2143 |
|
| 2144 |
init_itr(witr); |
| 2145 |
open_itr(witr, true); |
| 2146 |
if (witr.move_to(x, DB_SET_RANGE)) |
| 2147 |
return ((self *)this)->end(); |
| 2148 |
|
| 2149 |
return witr; |
| 2150 |
} |
| 2151 |
|
| 2152 |
/// Find the range within which all keys equal to specified key x. |
| 2153 |
/// \param x The target key to find. |
| 2154 |
/// \return The range [first, last). |
| 2155 |
/// \sa http://www.cplusplus.com/reference/stl/map/equal_range/ |
| 2156 |
pair<const_iterator, const_iterator> |
| 2157 |
equal_range (const key_type& x) const |
| 2158 |
{ |
| 2159 |
pair<const_iterator,const_iterator> pr; |
| 2160 |
const_iterator witr; |
| 2161 |
kdt k; |
| 2162 |
|
| 2163 |
init_itr(witr); |
| 2164 |
open_itr(witr, true); |
| 2165 |
if (witr.move_to(x, DB_SET_RANGE)) { |
| 2166 |
pr.first = ((self *)this)->end(); |
| 2167 |
pr.second = ((self *)this)->end(); |
| 2168 |
} else { |
| 2169 |
pr.first = witr; |
| 2170 |
// If no duplicate keys, move one next is sufficient. |
| 2171 |
if (witr.pcsr_->get_current_key(k) == 0 && k == x) |
| 2172 |
++witr; |
| 2173 |
pr.second = witr; |
| 2174 |
} |
| 2175 |
|
| 2176 |
|
| 2177 |
return pr; |
| 2178 |
} |
| 2179 |
|
| 2180 |
/// Find the key/data pair with specified key x. |
| 2181 |
/// \param x The target key to find. |
| 2182 |
/// \param readonly Whether the returned iterator is readonly. |
| 2183 |
/// \return The valid iterator sitting on the key x, or an |
| 2184 |
/// invalid one. |
| 2185 |
/// \sa http://www.cplusplus.com/reference/stl/map/find/ |
| 2186 |
iterator find (const key_type& x, bool readonly = false) |
| 2187 |
{ |
| 2188 |
iterator witr; |
| 2189 |
|
| 2190 |
init_itr(witr); |
| 2191 |
open_itr(witr, readonly); |
| 2192 |
if (witr.move_to(x)) |
| 2193 |
return ((self *)this)->end(); |
| 2194 |
|
| 2195 |
return witr; |
| 2196 |
} |
| 2197 |
|
| 2198 |
/// Find the greatest key less than or equal to x. |
| 2199 |
/// \param x The target key to find. |
| 2200 |
/// \param readonly Whether the returned iterator is readonly. |
| 2201 |
/// \return The valid iterator sitting on the key, or an |
| 2202 |
/// invalid one. |
| 2203 |
/// \sa http://www.cplusplus.com/reference/stl/map/lower_bound/ |
| 2204 |
iterator lower_bound (const key_type& x, bool readonly = false) |
| 2205 |
{ |
| 2206 |
iterator witr; |
| 2207 |
|
| 2208 |
init_itr(witr); |
| 2209 |
open_itr(witr, readonly); |
| 2210 |
if (witr.move_to(x, DB_SET_RANGE)) |
| 2211 |
return ((self *)this)->end(); |
| 2212 |
|
| 2213 |
return witr; |
| 2214 |
} |
| 2215 |
|
| 2216 |
/// Find the range within which all keys equal to specified key x. |
| 2217 |
/// \param x The target key to find. |
| 2218 |
/// \param readonly Whether the returned iterator is readonly. |
| 2219 |
/// \return The range [first, last). |
| 2220 |
/// \sa http://www.cplusplus.com/reference/stl/map/equal_range/ |
| 2221 |
pair<iterator, iterator> |
| 2222 |
equal_range (const key_type& x, bool readonly = false) |
| 2223 |
{ |
| 2224 |
pair<iterator,iterator> pr; |
| 2225 |
iterator witr; |
| 2226 |
kdt k; |
| 2227 |
|
| 2228 |
init_itr(witr); |
| 2229 |
open_itr(witr, readonly); |
| 2230 |
if (witr.move_to(x, DB_SET_RANGE)) { |
| 2231 |
pr.first = ((self *)this)->end(); |
| 2232 |
pr.second = ((self *)this)->end(); |
| 2233 |
} else { |
| 2234 |
pr.first = witr; |
| 2235 |
// If no dup, move one next is sufficient. |
| 2236 |
if (witr.pcsr_->get_current_key(k) == 0 && k == x) |
| 2237 |
++witr; |
| 2238 |
pr.second = witr; |
| 2239 |
} |
| 2240 |
|
| 2241 |
|
| 2242 |
return pr; |
| 2243 |
} |
| 2244 |
|
| 2245 |
/// Count the number of key/data pairs having specified key x. |
| 2246 |
/// \param x The key to count. |
| 2247 |
/// \return The number of key/data pairs having x as key within the |
| 2248 |
/// container. |
| 2249 |
/// \sa http://www.cplusplus.com/reference/stl/map/count/ |
| 2250 |
size_type count (const key_type& x) const |
| 2251 |
{ |
| 2252 |
int ret; |
| 2253 |
const_iterator witr; |
| 2254 |
try { |
| 2255 |
this->begin_txn(); |
| 2256 |
init_itr(witr); |
| 2257 |
open_itr(witr, true); |
| 2258 |
ret = witr.move_to(x); |
| 2259 |
this->commit_txn(); |
| 2260 |
if (ret != 0) |
| 2261 |
return 0;// No such key/data pair. |
| 2262 |
// No duplicates, so it must be one, we don't call |
| 2263 |
// Dbc::count because we don't have to. |
| 2264 |
// |
| 2265 |
else |
| 2266 |
return 1; |
| 2267 |
} catch (...) { |
| 2268 |
this->abort_txn(); |
| 2269 |
throw; |
| 2270 |
} |
| 2271 |
|
| 2272 |
} |
| 2273 |
|
| 2274 |
/// Find the least key greater than x. |
| 2275 |
/// \param x The target key to find. |
| 2276 |
/// \return The valid iterator sitting on the key, or an |
| 2277 |
/// invalid one. |
| 2278 |
/// \sa http://www.cplusplus.com/reference/stl/map/upper_bound/ |
| 2279 |
const_iterator upper_bound (const key_type& x) const |
| 2280 |
{ |
| 2281 |
const_iterator witr; |
| 2282 |
|
| 2283 |
init_itr(witr); |
| 2284 |
open_itr(witr, true); |
| 2285 |
|
| 2286 |
if (witr.move_to(x, DB_SET_RANGE)) |
| 2287 |
return ((self *)this)->end(); |
| 2288 |
|
| 2289 |
kdt k; |
| 2290 |
|
| 2291 |
// x exists in db, and witr.pcsr_ points to x in db. |
| 2292 |
if (witr.pcsr_->get_current_key(k) == 0 && k == x) |
| 2293 |
++witr;// No dup, so move one next is sufficient. |
| 2294 |
|
| 2295 |
return witr; |
| 2296 |
} |
| 2297 |
|
| 2298 |
/// Find the least key greater than x. |
| 2299 |
/// \param x The target key to find. |
| 2300 |
/// \param readonly Whether the returned iterator is readonly. |
| 2301 |
/// \return The valid iterator sitting on the key, or an |
| 2302 |
/// invalid one. |
| 2303 |
/// \sa http://www.cplusplus.com/reference/stl/map/upper_bound/ |
| 2304 |
iterator upper_bound (const key_type& x, bool readonly = false) |
| 2305 |
{ |
| 2306 |
iterator witr; |
| 2307 |
|
| 2308 |
init_itr(witr); |
| 2309 |
open_itr(witr, readonly); |
| 2310 |
|
| 2311 |
if (witr.move_to(x, DB_SET_RANGE)) |
| 2312 |
return ((self *)this)->end(); |
| 2313 |
|
| 2314 |
kdt k; |
| 2315 |
|
| 2316 |
// x exists in db, and witr.pcsr_ points to x in db. |
| 2317 |
if (witr.pcsr_->get_current_key(k) == 0 && k == x) |
| 2318 |
++witr;// No dup, so move one next is sufficient. |
| 2319 |
|
| 2320 |
return witr; |
| 2321 |
} |
| 2322 |
//@} |
| 2323 |
//////////////////////////////////////////////////////////////// |
| 2324 |
|
| 2325 |
// Compare function, return true if contents in m1 and m2 are |
| 2326 |
// identical otherwise return false. |
| 2327 |
// Note that we don't require the key-data pairs' order be identical |
| 2328 |
// Put into db_map<> rather than global to utilize transactional |
| 2329 |
// support. |
| 2330 |
/// Map content equality comparison operator. |
| 2331 |
/// This function does not rely on key order. For a set of keys S1 in |
| 2332 |
/// this container and another set of keys S2 of container m2, if |
| 2333 |
/// set S1 contains S2 and S2 contains S1 (S1 equals to S2) and each |
| 2334 |
/// data element of a key K in S1 from this container equals the data |
| 2335 |
/// element of K in m2, the two db_map<> containers equal. Otherwise |
| 2336 |
/// they are not equal. |
| 2337 |
/// \param m2 The other container to compare against. |
| 2338 |
/// \return Returns true if they have equal content, false otherwise. |
| 2339 |
bool operator==(const db_map<kdt, ddt, value_type_sub>& m2) const |
| 2340 |
{ |
| 2341 |
bool ret; |
| 2342 |
const db_map<kdt, ddt, value_type_sub>& m1 = *this; |
| 2343 |
COMPARE_CHECK(m2) |
| 2344 |
verify_db_handles(m2); |
| 2345 |
try { |
| 2346 |
this->begin_txn(); |
| 2347 |
if (m1.size() != m2.size()) |
| 2348 |
ret = false; |
| 2349 |
else { |
| 2350 |
typename db_map<kdt, ddt, value_type_sub>:: |
| 2351 |
const_iterator i1, i2; |
| 2352 |
|
| 2353 |
for (i1 = m1.begin(); i1 != m1.end(); ++i1) { |
| 2354 |
if (m2.count(i1->first) == 0) { |
| 2355 |
ret = false; |
| 2356 |
goto exit; |
| 2357 |
} |
| 2358 |
i2 = m2.find(i1->first); |
| 2359 |
if ((i2->second == i1->second) == |
| 2360 |
false) { |
| 2361 |
ret = false; |
| 2362 |
goto exit; |
| 2363 |
} |
| 2364 |
} // for |
| 2365 |
|
| 2366 |
ret = true; |
| 2367 |
} |
| 2368 |
exit: |
| 2369 |
this->commit_txn(); |
| 2370 |
return ret; |
| 2371 |
|
| 2372 |
} catch (...) { |
| 2373 |
this->abort_txn(); |
| 2374 |
throw; |
| 2375 |
} |
| 2376 |
// Now that m1 and m2 has the same number of unique elements and all |
| 2377 |
// elements of m1 are in m2, thus there can be no element of m2 |
| 2378 |
// that dose not belong to m1, so we won't verify each element of |
| 2379 |
// m2 are in m1. |
| 2380 |
// |
| 2381 |
} |
| 2382 |
|
| 2383 |
/// Container unequality comparison operator. |
| 2384 |
/// \param m2 The container to compare against. |
| 2385 |
/// \return Returns false if equal, true otherwise. |
| 2386 |
bool operator!=(const db_map<kdt, ddt, value_type_sub>& m2) const |
| 2387 |
{ |
| 2388 |
return !this->operator ==(m2); |
| 2389 |
} |
| 2390 |
|
| 2391 |
|
| 2392 |
protected: |
| 2393 |
|
| 2394 |
virtual const char* verify_config(Db*dbp, DbEnv* envp) const |
| 2395 |
{ |
| 2396 |
DBTYPE dbtype; |
| 2397 |
u_int32_t oflags, sflags; |
| 2398 |
int ret; |
| 2399 |
const char *err = NULL; |
| 2400 |
|
| 2401 |
err = db_container::verify_config(dbp, envp); |
| 2402 |
if (err) |
| 2403 |
return err; |
| 2404 |
|
| 2405 |
BDBOP(dbp->get_type(&dbtype), ret); |
| 2406 |
BDBOP(dbp->get_open_flags(&oflags), ret); |
| 2407 |
BDBOP(dbp->get_flags(&sflags), ret); |
| 2408 |
|
| 2409 |
if (dbtype != DB_BTREE && dbtype != DB_HASH) |
| 2410 |
err = |
| 2411 |
"wrong database type, only DB_BTREE and DB_HASH allowed for db_map<> class"; |
| 2412 |
|
| 2413 |
if (oflags & DB_TRUNCATE) |
| 2414 |
err = |
| 2415 |
"do not specify DB_TRUNCATE flag to create a db_map<> object"; |
| 2416 |
if ((sflags & DB_DUP) || (sflags & DB_DUPSORT)) |
| 2417 |
err = |
| 2418 |
"db_map<> can not be backed by database permitting duplicate keys"; |
| 2419 |
if (sflags & DB_RECNUM) |
| 2420 |
err = "no DB_RECNUM flag allowed in db_map<>"; |
| 2421 |
|
| 2422 |
|
| 2423 |
return err; |
| 2424 |
|
| 2425 |
} |
| 2426 |
|
| 2427 |
|
| 2428 |
typedef ddt mapped_type; |
| 2429 |
typedef int (*db_compare_fcn_t)(Db *db, const Dbt *dbt1, |
| 2430 |
const Dbt *dbt2); |
| 2431 |
typedef u_int32_t (*h_hash_fcn_t) |
| 2432 |
(Db *, const void *bytes, u_int32_t length); |
| 2433 |
typedef db_set_iterator<kdt> db_multiset_iterator_t; |
| 2434 |
|
| 2435 |
static bool compare_keys(Db *pdb, const kdt& k1, const kdt& k2) |
| 2436 |
{ |
| 2437 |
DBTYPE dbtype; |
| 2438 |
int ret; |
| 2439 |
bool bret; |
| 2440 |
u_int32_t sz1, sz2; |
| 2441 |
|
| 2442 |
assert(pdb != NULL); |
| 2443 |
ret = pdb->get_type(&dbtype); |
| 2444 |
assert(ret == 0); |
| 2445 |
db_compare_fcn_t comp = NULL; |
| 2446 |
|
| 2447 |
if (dbtype == DB_BTREE) |
| 2448 |
BDBOP(pdb->get_bt_compare(&comp), ret); |
| 2449 |
else // hash |
| 2450 |
BDBOP(pdb->get_h_compare(&comp), ret); |
| 2451 |
|
| 2452 |
DataItem key1(k1, true), key2(k2, true); |
| 2453 |
Dbt &kdbt1 = key1.get_dbt(); |
| 2454 |
Dbt &kdbt2 = key2.get_dbt(); |
| 2455 |
sz1 = kdbt1.get_size(); |
| 2456 |
sz2 = kdbt2.get_size(); |
| 2457 |
|
| 2458 |
if (comp == NULL) { |
| 2459 |
ret = memcmp(&k1, &k2, sz1 > sz2 ? sz2 : sz1); |
| 2460 |
return (ret == 0) ? (sz1 < sz2) : (ret < 0); |
| 2461 |
} |
| 2462 |
// Return strict weak ordering. |
| 2463 |
bret = (comp(pdb, &kdbt1, &kdbt2) < 0); |
| 2464 |
return bret; |
| 2465 |
} |
| 2466 |
|
| 2467 |
void open_itr(db_map_base_iterator<kdt, realddt, ddt>&itr, |
| 2468 |
bool readonly = false) const |
| 2469 |
{ |
| 2470 |
u_int32_t oflags = 0; |
| 2471 |
int ret; |
| 2472 |
DbEnv *penv = this->get_db_handle()->get_env(); |
| 2473 |
|
| 2474 |
if (!readonly && penv != NULL) { |
| 2475 |
BDBOP((penv->get_open_flags(&oflags)) , ret); |
| 2476 |
if ((oflags & DB_INIT_CDB) != 0) |
| 2477 |
((self *)this)->set_cursor_open_flags( |
| 2478 |
this->get_cursor_open_flags() | |
| 2479 |
DB_WRITECURSOR); |
| 2480 |
} |
| 2481 |
|
| 2482 |
itr.itr_status_ = itr.pcsr_->open((db_container*)this, |
| 2483 |
this->get_cursor_open_flags()); |
| 2484 |
itr.owner_ = (db_container*)this; |
| 2485 |
} |
| 2486 |
|
| 2487 |
void open_itr(const_reverse_iterator |
| 2488 |
&itr, bool readonly = false) const |
| 2489 |
{ |
| 2490 |
u_int32_t oflags = 0; |
| 2491 |
int ret; |
| 2492 |
DbEnv *penv = this->get_db_handle()->get_env(); |
| 2493 |
|
| 2494 |
if (!readonly && penv != NULL) { |
| 2495 |
BDBOP((penv->get_open_flags(&oflags)) , ret); |
| 2496 |
if ((oflags & DB_INIT_CDB) != 0) |
| 2497 |
((self *)this)->set_cursor_open_flags( |
| 2498 |
this->get_cursor_open_flags() | |
| 2499 |
DB_WRITECURSOR); |
| 2500 |
} |
| 2501 |
itr.itr_status_ = itr.pcsr_->open((db_container*)this, |
| 2502 |
this->get_cursor_open_flags()); |
| 2503 |
itr.owner_ = (db_container*)this; |
| 2504 |
} |
| 2505 |
|
| 2506 |
inline void init_itr(db_map_base_iterator<kdt, realddt, ddt> & |
| 2507 |
witr) const { |
| 2508 |
typedef DbCursor<kdt, ddt> cursor_type; |
| 2509 |
witr.pcsr_.set_cursor(new cursor_type()); |
| 2510 |
witr.owner_ = (db_container*)this; |
| 2511 |
} |
| 2512 |
|
| 2513 |
// Do not use begin_txn/commit_txn in non-public(internal) methods, |
| 2514 |
// only wrap in public methods. |
| 2515 |
// |
| 2516 |
inline void copy_db(db_map<kdt, ddt, value_type_sub, iterator> &x) |
| 2517 |
{ |
| 2518 |
|
| 2519 |
// Make sure clear can succeed if there are cursors |
| 2520 |
// open in other threads. |
| 2521 |
clear(false); |
| 2522 |
insert(x.begin(), x.end()); |
| 2523 |
|
| 2524 |
} |
| 2525 |
|
| 2526 |
};//db_map |
| 2527 |
//@} |
| 2528 |
|
| 2529 |
|
| 2530 |
////////////////////////////////////////////////////////////////////////// |
| 2531 |
////////////////////////////////////////////////////////////////////////// |
| 2532 |
// |
| 2533 |
// db_multimap class template definition |
| 2534 |
// |
| 2535 |
// This class derives from db_map<>, using many of its methods, |
| 2536 |
// it also hides some functions that should not be used in |
| 2537 |
// this class, such as operator[]. |
| 2538 |
// |
| 2539 |
// The underlying db must allow duplicate. |
| 2540 |
// iterator_t is default argument, see forward declaration at the |
| 2541 |
// head of this file. |
| 2542 |
// iterator_t is default argument, see forward declaration at the |
| 2543 |
// head of this file |
| 2544 |
// |
| 2545 |
/// \ingroup dbstl_containers |
| 2546 |
//@{ |
| 2547 |
/// This class is the combination of std::multimap and hash_multimap. By |
| 2548 |
/// setting database handles as DB_BTREE or DB_HASH type respectively, you |
| 2549 |
/// will be using an equivalent of std::multimap or hash_multimap respectively. |
| 2550 |
/// Database(dbp) and environment(penv) handle requirement: |
| 2551 |
/// The dbp handle must meet the following requirement: |
| 2552 |
/// 1. Database type should be DB_BTREE or DB_HASH. |
| 2553 |
/// 2. Either DB_DUP or DB_DUPSORT flag must be set. Note that so far |
| 2554 |
/// Berkeley DB does not allow DB_DUPSORT be set and the database is storing |
| 2555 |
/// identical key/data pairs, i.e. we can't store two (1, 2), (1, 2) pairs |
| 2556 |
/// into a database D with DB_DUPSORT flag set, but only can do so with DB_DUP |
| 2557 |
/// flag set; But we can store a (1, 2) pair and a (1, 3) pair into D with |
| 2558 |
/// DB_DUPSORT flag set. So if your data set allows DB_DUPSORT flag, you |
| 2559 |
/// should set it to gain a lot of performance promotion. |
| 2560 |
/// 3. No DB_RECNUM flag set. |
| 2561 |
/// 4. No DB_TRUNCATE specified in database open flags. |
| 2562 |
/// 5. DB_THREAD must be set if you are sharing the database handle across |
| 2563 |
/// multiple threads directly, or indirectly by sharing the container object |
| 2564 |
/// across multiple threads. |
| 2565 |
/// \param kdt The key data type. |
| 2566 |
/// \param ddt The data data type. db_multimap stores key/data pairs. |
| 2567 |
/// \param value_type_sub Do not specify anything if ddt type is a |
| 2568 |
/// class/struct type; Otherwise, specify ElementHolder<ddt> to it. |
| 2569 |
/// \param iterator_t Never specify anything to this type parameter. It is |
| 2570 |
/// only used internally. |
| 2571 |
/// \sa db_container db_map |
| 2572 |
template<Typename kdt, Typename ddt, Typename value_type_sub, |
| 2573 |
Typename iterator_t> |
| 2574 |
class _exported db_multimap : public db_map<kdt, ddt, |
| 2575 |
value_type_sub, iterator_t> |
| 2576 |
{ |
| 2577 |
protected: |
| 2578 |
typedef db_multimap<kdt, ddt, value_type_sub, iterator_t> self; |
| 2579 |
typedef db_map<kdt, ddt, value_type_sub, iterator_t> base; |
| 2580 |
public: |
| 2581 |
typedef iterator_t iterator; |
| 2582 |
typedef typename iterator::const_version const_iterator; |
| 2583 |
typedef db_reverse_iterator<iterator, const_iterator> reverse_iterator; |
| 2584 |
typedef db_reverse_iterator<const_iterator, iterator> |
| 2585 |
const_reverse_iterator; |
| 2586 |
typedef kdt key_type; |
| 2587 |
typedef ddt data_type; |
| 2588 |
typedef value_type_sub data_type_wrap; |
| 2589 |
typedef pair<kdt, value_type_sub> value_type_wrap; |
| 2590 |
typedef pair<kdt, ddt> value_type; |
| 2591 |
typedef value_type_wrap* pointer; |
| 2592 |
typedef value_type_wrap& reference; |
| 2593 |
typedef const value_type& const_reference; |
| 2594 |
typedef size_t size_type; |
| 2595 |
typedef ptrdiff_t difference_type; |
| 2596 |
|
| 2597 |
//////////////////////////////////////////////////////////////// |
| 2598 |
// Begin constructors and destructor |
| 2599 |
/// Constructor. |
| 2600 |
/// See class detail for handle requirement. |
| 2601 |
/// \param dbp The database handle. |
| 2602 |
/// \param envp The database environment handle. |
| 2603 |
/// \sa db_map::db_map(Db*, DbEnv*) db_vector::db_vector(Db*, DbEnv*) |
| 2604 |
explicit db_multimap (Db *dbp = NULL, DbEnv* envp = NULL) : |
| 2605 |
base(*(new BulkRetrievalOption( |
| 2606 |
BulkRetrievalOption::BulkRetrieval))) |
| 2607 |
{ |
| 2608 |
const char *errmsg; |
| 2609 |
|
| 2610 |
this->init_members(dbp, envp); |
| 2611 |
this->open_db_handles(dbp, envp, DB_BTREE, DB_CREATE | |
| 2612 |
DB_THREAD, DB_DUP); |
| 2613 |
// We can't call base(dbp, envp) here because it will verify |
| 2614 |
// failed and we can't call db_container directly, it is |
| 2615 |
// illegal to do so. |
| 2616 |
if ((errmsg = verify_config(dbp, envp)) != NULL) { |
| 2617 |
THROW(InvalidArgumentException, ("Db*", errmsg)); |
| 2618 |
|
| 2619 |
} |
| 2620 |
this->set_db_handle_int(dbp, envp); |
| 2621 |
this->set_auto_commit(dbp); |
| 2622 |
} |
| 2623 |
|
| 2624 |
/// Iteration constructor. |
| 2625 |
/// Iterates between first and last, setting |
| 2626 |
/// a copy of each of the sequence of elements as the content of |
| 2627 |
/// the container object. |
| 2628 |
/// This function supports auto-commit. |
| 2629 |
/// See class detail for handle requirement. |
| 2630 |
/// \param dbp The database handle. |
| 2631 |
/// \param envp The database environment handle. |
| 2632 |
/// \param first The closed boundary of the range. |
| 2633 |
/// \param last The open boundary of the range. |
| 2634 |
/// \sa db_map::db_map(Db*, DbEnv*, InputIterator, InputIterator) |
| 2635 |
/// db_vector::db_vector(Db*, DbEnv*) |
| 2636 |
// |
| 2637 |
template <class InputIterator> |
| 2638 |
db_multimap (Db *dbp, DbEnv* envp, InputIterator first, |
| 2639 |
InputIterator last) : base(*(new BulkRetrievalOption( |
| 2640 |
BulkRetrievalOption::BulkRetrieval))) |
| 2641 |
{ |
| 2642 |
const char *errmsg; |
| 2643 |
|
| 2644 |
this->init_members(dbp, envp); |
| 2645 |
this->open_db_handles(dbp, envp, DB_BTREE, DB_CREATE | |
| 2646 |
DB_THREAD, DB_DUP); |
| 2647 |
// Note that we can't call base(dbp, envp) here because it |
| 2648 |
// will verify failed; And we can't call db_container |
| 2649 |
// directly because it is illegal to do so. |
| 2650 |
if ((errmsg = verify_config(dbp, envp)) != NULL) { |
| 2651 |
THROW(InvalidArgumentException, ("Db*", errmsg)); |
| 2652 |
|
| 2653 |
} |
| 2654 |
this->set_db_handle_int(dbp, envp); |
| 2655 |
this->set_auto_commit(dbp); |
| 2656 |
|
| 2657 |
|
| 2658 |
this->begin_txn(); |
| 2659 |
try { |
| 2660 |
insert(first, last); |
| 2661 |
} catch (...) { |
| 2662 |
this->abort_txn(); |
| 2663 |
throw; |
| 2664 |
} |
| 2665 |
this->commit_txn(); |
| 2666 |
} |
| 2667 |
|
| 2668 |
/// Copy constructor. |
| 2669 |
/// Create an database and insert all key/data pairs in x into this |
| 2670 |
/// container. x's data members are not copied. |
| 2671 |
/// This function supports auto-commit. |
| 2672 |
/// \param x The other container to initialize this container. |
| 2673 |
/// \sa db_container(const db_container&) db_map(const db_map&) |
| 2674 |
db_multimap (const self& x) : base(*(new BulkRetrievalOption( |
| 2675 |
BulkRetrievalOption::BulkRetrieval))) |
| 2676 |
{ |
| 2677 |
this->init_members(x); |
| 2678 |
this->verify_db_handles(x); |
| 2679 |
this->set_db_handle_int(this->clone_db_config( |
| 2680 |
x.get_db_handle()), x.get_db_env_handle()); |
| 2681 |
assert(this->get_db_handle() != NULL); |
| 2682 |
|
| 2683 |
this->begin_txn(); |
| 2684 |
try { |
| 2685 |
copy_db((self&)x); |
| 2686 |
} catch (...) { |
| 2687 |
this->abort_txn(); |
| 2688 |
throw; |
| 2689 |
} |
| 2690 |
this->commit_txn(); |
| 2691 |
} |
| 2692 |
|
| 2693 |
virtual ~db_multimap(){} |
| 2694 |
//////////////////////////////////////////////////////////////// |
| 2695 |
|
| 2696 |
//////////////////////////////////////////////////////////////// |
| 2697 |
// Begin functions that modify multimap content, e.g. insert, |
| 2698 |
// erase, assignment and swap. |
| 2699 |
// |
| 2700 |
/// Container content assignment operator. |
| 2701 |
/// This function supports auto-commit. |
| 2702 |
/// \param x The other container whose key/data pairs will be inserted |
| 2703 |
/// into this container. Old content in this containers are discarded. |
| 2704 |
/// \sa http://www.cplusplus.com/reference/stl/multimap/operator=/ |
| 2705 |
inline const self& operator=(const self&x) |
| 2706 |
{ |
| 2707 |
ASSIGNMENT_PREDCOND(x) |
| 2708 |
db_container::operator =(x); |
| 2709 |
this->verify_db_handles(x); |
| 2710 |
assert(this->get_db_handle() != NULL); |
| 2711 |
this->begin_txn(); |
| 2712 |
try { |
| 2713 |
this->copy_db((self &)x); |
| 2714 |
} catch (...) { |
| 2715 |
this->abort_txn(); |
| 2716 |
throw; |
| 2717 |
} |
| 2718 |
this->commit_txn(); |
| 2719 |
return x; |
| 2720 |
|
| 2721 |
} |
| 2722 |
|
| 2723 |
/// \name Insert Functions |
| 2724 |
/// \sa http://www.cplusplus.com/reference/stl/multimap/insert/ |
| 2725 |
//@{ |
| 2726 |
/// Range insertion. Insert a range [first, last) of key/data pairs |
| 2727 |
/// into this container. |
| 2728 |
/// \param first The closed boundary of the range. |
| 2729 |
/// \param last The open boundary of the range. |
| 2730 |
template<typename InputIterator> |
| 2731 |
void insert (InputIterator first, InputIterator last) |
| 2732 |
{ |
| 2733 |
InputIterator ii; |
| 2734 |
iterator witr; |
| 2735 |
|
| 2736 |
this->init_itr(witr); |
| 2737 |
this->open_itr(witr); |
| 2738 |
|
| 2739 |
for (ii = first; ii != last; ++ii) |
| 2740 |
witr.pcsr_->insert(ii->first, ii->second, |
| 2741 |
DB_KEYLAST); |
| 2742 |
} |
| 2743 |
|
| 2744 |
// Compiler can't see the inherited version, unknown why. |
| 2745 |
/// Range insertion. Insert a range [first, last) of key/data pairs |
| 2746 |
/// into this container. |
| 2747 |
/// \param first The closed boundary of the range. |
| 2748 |
/// \param last The open boundary of the range. |
| 2749 |
inline void insert (const_iterator& first, const_iterator& last) { |
| 2750 |
base::insert(first, last); |
| 2751 |
} |
| 2752 |
|
| 2753 |
// Insert x into this container, the other two versions are |
| 2754 |
// inherited from db_map<> class. |
| 2755 |
// Methods returning an iterator or using an iterator as parameter |
| 2756 |
// can not be internally wrapped by |
| 2757 |
// begin/commit_txn because a cursor is inside its transaction, it |
| 2758 |
// must have been closed after transaction commit, and reopen is |
| 2759 |
// unsafe in multithreaded access. |
| 2760 |
// |
| 2761 |
/// Insert a single key/data pair if the key is not in the container. |
| 2762 |
/// \param x The key/data pair to insert. |
| 2763 |
/// \return A pair P, if insert OK, i.e. the inserted key wasn't in the |
| 2764 |
/// container, P.first will be the iterator sitting on the inserted |
| 2765 |
/// key/data pair, and P.second is true; otherwise P.first is an |
| 2766 |
/// invalid iterator and P.second is false. |
| 2767 |
inline iterator insert (const value_type& x) |
| 2768 |
{ |
| 2769 |
iterator witr; |
| 2770 |
|
| 2771 |
this->init_itr(witr); |
| 2772 |
this->open_itr(witr); |
| 2773 |
witr.itr_status_ = witr.pcsr_->insert(x.first, x.second, |
| 2774 |
DB_KEYLAST); |
| 2775 |
witr.refresh(false); |
| 2776 |
return witr; |
| 2777 |
} |
| 2778 |
//@} |
| 2779 |
|
| 2780 |
/// Swap content with another multimap container. |
| 2781 |
/// This function supports auto-commit. |
| 2782 |
/// \param mp The other container to swap content with. |
| 2783 |
/// \param b_truncate See db_map::swap() for details. |
| 2784 |
/// \sa db_vector::clear() |
| 2785 |
void swap (db_multimap<kdt, ddt, value_type_sub>& mp, |
| 2786 |
bool b_truncate = true) |
| 2787 |
{ |
| 2788 |
Db *swapdb = NULL; |
| 2789 |
std::string dbfname(64, '\0'); |
| 2790 |
|
| 2791 |
this->verify_db_handles(mp); |
| 2792 |
this->begin_txn(); |
| 2793 |
try { |
| 2794 |
swapdb = this->clone_db_config(this->get_db_handle(), |
| 2795 |
dbfname); |
| 2796 |
|
| 2797 |
db_multimap<kdt, ddt, value_type_sub> tmap( |
| 2798 |
swapdb, swapdb->get_env(), |
| 2799 |
this->begin(), this->end()); |
| 2800 |
// Clear this db_multimap<> object. |
| 2801 |
this->clear(b_truncate); |
| 2802 |
typename db_multimap<kdt, ddt, value_type_sub>:: |
| 2803 |
iterator mpbitr, mpeitr; |
| 2804 |
|
| 2805 |
mpbitr = mp.begin(); |
| 2806 |
mpeitr = mp.end(); |
| 2807 |
insert(mpbitr, mpeitr); |
| 2808 |
mp.clear(b_truncate); |
| 2809 |
mpbitr = tmap.begin(); |
| 2810 |
mpeitr = tmap.end(); |
| 2811 |
mp.insert(mpbitr, mpeitr); |
| 2812 |
tmap.clear(); |
| 2813 |
|
| 2814 |
swapdb->close(0); |
| 2815 |
if (dbfname[0] != '\0') { |
| 2816 |
swapdb = new Db(NULL, DB_CXX_NO_EXCEPTIONS); |
| 2817 |
swapdb->remove(dbfname.c_str(), NULL, 0); |
| 2818 |
swapdb->close(0); |
| 2819 |
delete swapdb; |
| 2820 |
} |
| 2821 |
this->commit_txn(); |
| 2822 |
} catch (...) { |
| 2823 |
this->abort_txn(); |
| 2824 |
throw; |
| 2825 |
} |
| 2826 |
|
| 2827 |
} |
| 2828 |
|
| 2829 |
// This method has identical code to db_map<>::erase(const key_type&), |
| 2830 |
// but we can NOT simply inherit and use |
| 2831 |
// that version because: |
| 2832 |
// 1. The db_map<>::erase called equal_range which is overloaded in |
| 2833 |
// db_multimap, so if we want the inherited erase to call the right |
| 2834 |
// version of equal_range, we have to make equal_range virtual |
| 2835 |
// 2. Making equal_range virtual will make the code not build--- The |
| 2836 |
// default template parameter can't be replaced by real parameter, |
| 2837 |
// unknow why. |
| 2838 |
// So we have to copy the code from db_map<> to here, and keep the |
| 2839 |
// code consistent on each update. |
| 2840 |
// Also, when I copy only this function, I found other erase overloaded |
| 2841 |
// functions also have to be copied from db_map<> to db_multimap and |
| 2842 |
// db_multiset, otherwise the code don't build, so I |
| 2843 |
// finally have to copy all versions of erase functions into db_multiset |
| 2844 |
// and db_multimap. When updating an erase function, do update all |
| 2845 |
// three versions. |
| 2846 |
/// \name Erase Functions |
| 2847 |
/// \sa http://www.cplusplus.com/reference/stl/multimap/erase/ |
| 2848 |
//@{ |
| 2849 |
/// Erase elements by key. |
| 2850 |
/// All key/data pairs with specified key x will be removed from |
| 2851 |
/// underlying database. |
| 2852 |
/// This function supports auto-commit. |
| 2853 |
/// \param x The key to remove from the container. |
| 2854 |
/// \return The number of key/data pairs removed. |
| 2855 |
size_type erase (const key_type& x) |
| 2856 |
{ |
| 2857 |
size_type cnt; |
| 2858 |
iterator itr; |
| 2859 |
|
| 2860 |
this->begin_txn(); |
| 2861 |
try { |
| 2862 |
pair<iterator, iterator> rg = equal_range(x); |
| 2863 |
for (itr = rg.first, cnt = 0; itr != rg.second; ++itr) { |
| 2864 |
cnt++; |
| 2865 |
itr.pcsr_->del(); |
| 2866 |
} |
| 2867 |
} catch (...) { |
| 2868 |
this->abort_txn(); |
| 2869 |
throw; |
| 2870 |
} |
| 2871 |
this->commit_txn(); |
| 2872 |
return cnt; |
| 2873 |
} |
| 2874 |
|
| 2875 |
// Can not reopen external/outside iterator's cursor, pos must |
| 2876 |
// already be in a transactional context. |
| 2877 |
// There is identical function in db_multimap<> and db_multiset |
| 2878 |
// for this function, we MUST keep the code consistent on update! |
| 2879 |
// Go to db_multimap<>::erase(const key_type&) to see why. |
| 2880 |
// |
| 2881 |
/// Erase a key/data pair at specified position. |
| 2882 |
/// \param pos An valid iterator of this container to erase. |
| 2883 |
inline void erase (iterator pos) |
| 2884 |
{ |
| 2885 |
if (pos == this->end()) |
| 2886 |
return; |
| 2887 |
pos.pcsr_->del(); |
| 2888 |
} |
| 2889 |
|
| 2890 |
// Can not be auto commit because first and last are already open. |
| 2891 |
// There is identical function in db_multimap<> and db_multiset |
| 2892 |
// for this function, we MUST keep the code consistent on update! |
| 2893 |
// Go to db_multimap<>::erase(const key_type&) to see why. |
| 2894 |
// |
| 2895 |
/// Range erase. Erase all key/data pairs within the valid range |
| 2896 |
/// [first, last). |
| 2897 |
/// \param first The closed boundary of the range. |
| 2898 |
/// \param last The open boundary of the range. |
| 2899 |
inline void erase (iterator first, iterator last) |
| 2900 |
{ |
| 2901 |
|
| 2902 |
for (iterator i = first; i != last; ++i) |
| 2903 |
i.pcsr_->del(); |
| 2904 |
} |
| 2905 |
//@} |
| 2906 |
//////////////////////////////////////////////////////////////// |
| 2907 |
|
| 2908 |
//////////////////////////////////////////////////////////////// |
| 2909 |
// Begin functions that searches a key in the multimap. |
| 2910 |
/// \name Searching Functions |
| 2911 |
/// See of db_map's searching functions group for details about |
| 2912 |
/// iterator, function version and parameters. |
| 2913 |
/// \sa db_map |
| 2914 |
//@{ |
| 2915 |
/// Find the range within which all keys equal to specified key x. |
| 2916 |
/// \param x The target key to find. |
| 2917 |
/// \return The range [first, last). |
| 2918 |
/// \sa http://www.cplusplus.com/reference/stl/multimap/equal_range/ |
| 2919 |
pair<const_iterator, const_iterator> |
| 2920 |
equal_range (const key_type& x) const |
| 2921 |
{ |
| 2922 |
pair<const_iterator,const_iterator> pr; |
| 2923 |
const_iterator witr; |
| 2924 |
kdt k; |
| 2925 |
|
| 2926 |
this->init_itr(witr); |
| 2927 |
this->open_itr(witr, true); |
| 2928 |
// Move witr to x if this contains x and return the itr, or if |
| 2929 |
// no x, position witr to the least key greater than x. |
| 2930 |
// |
| 2931 |
if (witr.move_to(x, DB_SET_RANGE)) { |
| 2932 |
pr.first = ((self *)this)->end(); |
| 2933 |
pr.second = ((self *)this)->end(); |
| 2934 |
} else { |
| 2935 |
pr.first = witr; |
| 2936 |
|
| 2937 |
// No dup, so move one next is sufficient. |
| 2938 |
if (witr.pcsr_->get_current_key(k) == 0 && k == x) |
| 2939 |
witr.next(DB_NEXT_NODUP); |
| 2940 |
pr.second = witr; |
| 2941 |
} |
| 2942 |
|
| 2943 |
return pr; |
| 2944 |
} |
| 2945 |
|
| 2946 |
/// Find the range within which all keys equal to specified key x. |
| 2947 |
/// \param x The target key to find. |
| 2948 |
/// \param readonly Whether the returned iterator is readonly. |
| 2949 |
/// \return The range [first, last). |
| 2950 |
/// \sa http://www.cplusplus.com/reference/stl/multimap/equal_range/ |
| 2951 |
pair<iterator,iterator> |
| 2952 |
equal_range (const key_type& x, bool readonly = false) |
| 2953 |
{ |
| 2954 |
pair<iterator,iterator> pr; |
| 2955 |
iterator witr; |
| 2956 |
kdt k; |
| 2957 |
|
| 2958 |
this->init_itr(witr); |
| 2959 |
this->open_itr(witr, readonly); |
| 2960 |
// Move witr to x if this contains x and return the itr, or if |
| 2961 |
// no x, position witr to the least key greater than x. |
| 2962 |
// |
| 2963 |
if (witr.move_to(x, DB_SET_RANGE)) { |
| 2964 |
pr.first = ((self *)this)->end(); |
| 2965 |
pr.second = ((self *)this)->end(); |
| 2966 |
} else { |
| 2967 |
pr.first = witr; |
| 2968 |
|
| 2969 |
// No dup, so move one next is sufficient. |
| 2970 |
if (witr.pcsr_->get_current_key(k) == 0 && k == x) |
| 2971 |
witr.next(DB_NEXT_NODUP); |
| 2972 |
pr.second = witr; |
| 2973 |
} |
| 2974 |
|
| 2975 |
return pr; |
| 2976 |
} |
| 2977 |
|
| 2978 |
/// Find equal range and number of key/data pairs in the range. |
| 2979 |
/// This function also returns the number of elements within the |
| 2980 |
/// returned range via the out parameter nelem. |
| 2981 |
/// \param x The target key to find. |
| 2982 |
/// \param nelem The output parameter to take back the number of |
| 2983 |
/// key/data pair in the returned range. |
| 2984 |
/// \sa http://www.cplusplus.com/reference/stl/multimap/equal_range/ |
| 2985 |
pair<const_iterator, const_iterator> |
| 2986 |
equal_range_N (const key_type& x, size_t& nelem) const |
| 2987 |
{ |
| 2988 |
int ret; |
| 2989 |
pair<const_iterator,const_iterator> pr; |
| 2990 |
size_t stepped; |
| 2991 |
const_iterator witr; |
| 2992 |
kdt k; |
| 2993 |
|
| 2994 |
this->init_itr(witr); |
| 2995 |
this->open_itr(witr, true); |
| 2996 |
// Move witr to x if this contains x and return the itr, or if |
| 2997 |
// no x, position witr to the least key greater than x. |
| 2998 |
// |
| 2999 |
if (witr.move_to(x, DB_SET_RANGE)) { |
| 3000 |
pr.first = ((self *)this)->end(); |
| 3001 |
pr.second = ((self *)this)->end(); |
| 3002 |
nelem = 0; |
| 3003 |
} else { |
| 3004 |
pr.first = witr; |
| 3005 |
if (witr.pcsr_->get_current_key(k) == 0 && k == x) { |
| 3006 |
for (stepped = 1, ret = |
| 3007 |
witr.pcsr_->next(DB_NEXT_DUP); ret == 0; |
| 3008 |
ret = witr.pcsr_->next(DB_NEXT_DUP), |
| 3009 |
stepped += 1) |
| 3010 |
; |
| 3011 |
pr.second = ++witr; |
| 3012 |
nelem = stepped; |
| 3013 |
} else { |
| 3014 |
pr.second = witr; |
| 3015 |
nelem = 0; |
| 3016 |
} |
| 3017 |
} |
| 3018 |
return pr; |
| 3019 |
} |
| 3020 |
|
| 3021 |
/// Find equal range and number of key/data pairs in the range. |
| 3022 |
/// This function also returns the number of elements within the |
| 3023 |
/// returned range via the out parameter nelem. |
| 3024 |
/// \param x The target key to find. |
| 3025 |
/// \param nelem The output parameter to take back the number of |
| 3026 |
/// key/data pair in the returned range. |
| 3027 |
/// \param readonly Whether the returned iterator is readonly. |
| 3028 |
/// \sa http://www.cplusplus.com/reference/stl/multimap/equal_range/ |
| 3029 |
// |
| 3030 |
pair<iterator,iterator> |
| 3031 |
equal_range_N (const key_type& x, size_t& nelem, |
| 3032 |
bool readonly = false) |
| 3033 |
{ |
| 3034 |
int ret; |
| 3035 |
pair<iterator,iterator> pr; |
| 3036 |
size_t stepped; |
| 3037 |
iterator witr; |
| 3038 |
kdt k; |
| 3039 |
|
| 3040 |
this->init_itr(witr); |
| 3041 |
this->open_itr(witr, readonly); |
| 3042 |
// Move witr to x if this contains x and return the itr, or if |
| 3043 |
// no x, position witr to the least key greater than x. |
| 3044 |
// |
| 3045 |
if (witr.move_to(x, DB_SET_RANGE)) { |
| 3046 |
pr.first = ((self *)this)->end(); |
| 3047 |
pr.second = ((self *)this)->end(); |
| 3048 |
nelem = 0; |
| 3049 |
} else { |
| 3050 |
pr.first = witr; |
| 3051 |
if (witr.pcsr_->get_current_key(k) == 0 && k == x) { |
| 3052 |
for (stepped = 1, ret = |
| 3053 |
witr.pcsr_->next(DB_NEXT_DUP); ret == 0; |
| 3054 |
ret = witr.pcsr_->next(DB_NEXT_DUP), |
| 3055 |
stepped += 1) |
| 3056 |
; |
| 3057 |
pr.second = ++witr; |
| 3058 |
nelem = stepped; |
| 3059 |
} else { |
| 3060 |
pr.second = witr; |
| 3061 |
nelem = 0; |
| 3062 |
} |
| 3063 |
} |
| 3064 |
return pr; |
| 3065 |
} |
| 3066 |
|
| 3067 |
/// Count the number of key/data pairs having specified key x. |
| 3068 |
/// \param x The key to count. |
| 3069 |
/// \return The number of key/data pairs having x as key within the |
| 3070 |
/// container. |
| 3071 |
/// \sa http://www.cplusplus.com/reference/stl/multimap/count/ |
| 3072 |
size_type count (const key_type& x) const |
| 3073 |
{ |
| 3074 |
int ret; |
| 3075 |
size_type cnt; |
| 3076 |
iterator witr; |
| 3077 |
|
| 3078 |
try { |
| 3079 |
this->begin_txn(); |
| 3080 |
this->init_itr(witr); |
| 3081 |
this->open_itr(witr, true); |
| 3082 |
ret = witr.move_to(x); |
| 3083 |
if (ret) |
| 3084 |
cnt = 0; |
| 3085 |
else |
| 3086 |
cnt = witr.pcsr_->count(); |
| 3087 |
this->commit_txn(); |
| 3088 |
} catch (...) { |
| 3089 |
this->abort_txn(); |
| 3090 |
throw; |
| 3091 |
} |
| 3092 |
|
| 3093 |
return cnt; |
| 3094 |
} |
| 3095 |
|
| 3096 |
/// Find the least key greater than x. |
| 3097 |
/// \param x The target key to find. |
| 3098 |
/// \return The valid iterator sitting on the key, or an |
| 3099 |
/// invalid one. |
| 3100 |
/// \sa http://www.cplusplus.com/reference/stl/multimap/upper_bound/ |
| 3101 |
const_iterator upper_bound ( |
| 3102 |
const key_type& x) const |
| 3103 |
{ |
| 3104 |
int ret; |
| 3105 |
const_iterator witr; |
| 3106 |
|
| 3107 |
this->init_itr(witr); |
| 3108 |
this->open_itr(witr, true); |
| 3109 |
|
| 3110 |
// No key equal to or greater than x. |
| 3111 |
if (witr.move_to(x, DB_SET_RANGE)) |
| 3112 |
return ((self *)this)->end(); |
| 3113 |
|
| 3114 |
kdt k; |
| 3115 |
// x exists in db, and witr.pcsr_ points to x in db, |
| 3116 |
// need to move cursor to next different key. |
| 3117 |
// |
| 3118 |
if (witr.pcsr_->get_current_key(k) == 0 && k == x) |
| 3119 |
ret = witr.next(DB_NEXT_NODUP); |
| 3120 |
|
| 3121 |
return witr; |
| 3122 |
} |
| 3123 |
|
| 3124 |
/// Find the least key greater than x. |
| 3125 |
/// \param x The target key to find. |
| 3126 |
/// \param readonly Whether the returned iterator is readonly. |
| 3127 |
/// \return The valid iterator sitting on the key, or an |
| 3128 |
/// invalid one. |
| 3129 |
/// \sa http://www.cplusplus.com/reference/stl/multimap/upper_bound/ |
| 3130 |
iterator upper_bound (const key_type& x, bool readonly = false) |
| 3131 |
{ |
| 3132 |
int ret; |
| 3133 |
iterator witr; |
| 3134 |
|
| 3135 |
this->init_itr(witr); |
| 3136 |
this->open_itr(witr, readonly); |
| 3137 |
|
| 3138 |
// No key equal to or greater than x. |
| 3139 |
if (witr.move_to(x, DB_SET_RANGE)) |
| 3140 |
return ((self *)this)->end(); |
| 3141 |
|
| 3142 |
kdt k; |
| 3143 |
// x exists in db, and witr.pcsr_ points to x in db, |
| 3144 |
// need to move cursor to next different key. |
| 3145 |
// |
| 3146 |
if (witr.pcsr_->get_current_key(k) == 0 && k == x) |
| 3147 |
ret = witr.next(DB_NEXT_NODUP); |
| 3148 |
|
| 3149 |
return witr; |
| 3150 |
} |
| 3151 |
//@} |
| 3152 |
//////////////////////////////////////////////////////////////// |
| 3153 |
|
| 3154 |
//////////////////////////////////////////////////////////////// |
| 3155 |
// Begin functions that compare container content. |
| 3156 |
// |
| 3157 |
// In hash_multimap this function is a global compare function, |
| 3158 |
// return true if contents in m1 and m2 are identical |
| 3159 |
// otherwise return false. But we have multiple reasons to make |
| 3160 |
// it a member of db_multimap<>: |
| 3161 |
// 1. There need to be a temporary set to store values of a range, and |
| 3162 |
// db_multimap<> is quite likely to store huge amount of data, |
| 3163 |
// not suitable to store into std::set, let alone std::set is not |
| 3164 |
// completely multithread-safe, thus we store them into db_set<>, |
| 3165 |
// thus we need a temporary db handle, and call |
| 3166 |
// db_container::clone_db_handle() function to open the db handle. |
| 3167 |
// 2. We need the transactional support. Making this function |
| 3168 |
// autocommit is good to eliminate phantom issues. |
| 3169 |
// Note that we don't require the key-data pairs' order be identical, |
| 3170 |
// but we assume identical records of keys are adjacent, so that |
| 3171 |
// iteration will go through them all one by one; Also, the records' |
| 3172 |
// order of identical keys are unpridictable and irrelivent, so we |
| 3173 |
// should treat values of a equal range a set, and compare two value |
| 3174 |
// sets for equality when comparing a equal range of key X. |
| 3175 |
// |
| 3176 |
/** |
| 3177 |
Returns whether the two containers have identical content. |
| 3178 |
This function does not rely on key order. For a set of keys S1 in this |
| 3179 |
container and another set of keys S2 of container m2, if set S1 |
| 3180 |
contains S2 and S2 contains S1 (S1 equals to S2) and each set of data |
| 3181 |
elements of any key K in S1 from this container equals the set of data |
| 3182 |
elements of K in m2, the two db_multimap<> containers equal. Otherwise |
| 3183 |
they are not equal. Data element set comparison does not rely on order |
| 3184 |
either. |
| 3185 |
\param m2 The other container to compare against. |
| 3186 |
\return Returns true if they are equal, false otherwise. |
| 3187 |
*/ |
| 3188 |
bool operator==(const db_multimap<kdt, ddt, value_type_sub>& m2) const |
| 3189 |
{ |
| 3190 |
|
| 3191 |
typedef typename self::const_iterator mm_itr_t; |
| 3192 |
|
| 3193 |
COMPARE_CHECK(m2) |
| 3194 |
|
| 3195 |
bool ret = false, retset = false; |
| 3196 |
size_t n1, n2; |
| 3197 |
int ret2; |
| 3198 |
const self &m1 = *this; |
| 3199 |
DbTxn *ptxn = NULL; |
| 3200 |
DbEnv *penv; |
| 3201 |
Db *pdb; |
| 3202 |
const char *dbfilename, *dbname; |
| 3203 |
const char *pname1, *pname2; |
| 3204 |
string name1, name2; |
| 3205 |
u_int32_t oflags; |
| 3206 |
|
| 3207 |
this->verify_db_handles(m2); |
| 3208 |
pdb = this->get_db_handle(); |
| 3209 |
penv = pdb->get_env(); |
| 3210 |
try { |
| 3211 |
this->begin_txn(); |
| 3212 |
if (m1.size() != m2.size()) { |
| 3213 |
ret = false; |
| 3214 |
this->commit_txn(); |
| 3215 |
return ret; |
| 3216 |
} |
| 3217 |
BDBOP(pdb->get_dbname(&dbfilename, &dbname), ret2); |
| 3218 |
if (dbfilename == NULL) |
| 3219 |
pname1 = pname2 = NULL; |
| 3220 |
else { |
| 3221 |
|
| 3222 |
this->construct_db_file_name(name1); |
| 3223 |
this->construct_db_file_name(name2); |
| 3224 |
// Make name2 different from name1. |
| 3225 |
name2.push_back('2'); |
| 3226 |
pname1 = name1.c_str(); |
| 3227 |
pname2 = name2.c_str(); |
| 3228 |
} |
| 3229 |
|
| 3230 |
Db *value_set_db = open_db(penv, |
| 3231 |
pname1, DB_BTREE, DB_CREATE, 0); |
| 3232 |
|
| 3233 |
Db *value_set_db2 = open_db(penv, |
| 3234 |
pname2, DB_BTREE, DB_CREATE, 0); |
| 3235 |
|
| 3236 |
db_set<ddt, value_type_sub> s1(value_set_db, penv), |
| 3237 |
s2(value_set_db2, penv); |
| 3238 |
|
| 3239 |
mm_itr_t i1, i11; |
| 3240 |
pair<mm_itr_t, mm_itr_t> resrg1, resrg2; |
| 3241 |
for (i1 = m1.begin(); |
| 3242 |
i1 != m1.end(); |
| 3243 |
i1 = resrg1.second) { |
| 3244 |
|
| 3245 |
resrg1 = m1.equal_range_N(i1->first, n1); |
| 3246 |
resrg2 = m2.equal_range_N(i1->first, n2); |
| 3247 |
if (n1 != n2) { |
| 3248 |
ret = false; |
| 3249 |
retset = true; |
| 3250 |
break; |
| 3251 |
} |
| 3252 |
|
| 3253 |
if (n2 == 1 && !(resrg2.first->second == |
| 3254 |
resrg1.first->second)) { |
| 3255 |
ret = false; |
| 3256 |
retset = true; |
| 3257 |
break; |
| 3258 |
} |
| 3259 |
|
| 3260 |
for (i11 = resrg1.first; i11 != resrg1.second; |
| 3261 |
++i11) |
| 3262 |
s1.insert(i11->second); |
| 3263 |
|
| 3264 |
for (i11 = resrg2.first; i11 != resrg2.second; |
| 3265 |
++i11) |
| 3266 |
s2.insert(i11->second); |
| 3267 |
if (!(s1 == s2)) { |
| 3268 |
ret = false; |
| 3269 |
retset = true; |
| 3270 |
break; |
| 3271 |
} |
| 3272 |
s1.clear(); |
| 3273 |
s2.clear(); |
| 3274 |
|
| 3275 |
// Skip all equal keys in the range. |
| 3276 |
|
| 3277 |
|
| 3278 |
} // for |
| 3279 |
|
| 3280 |
if (!retset) // Care: there are breaks in the for loop. |
| 3281 |
ret = true; |
| 3282 |
|
| 3283 |
close_db(value_set_db); |
| 3284 |
close_db(value_set_db2); |
| 3285 |
|
| 3286 |
ptxn = this->current_txn(); |
| 3287 |
BDBOP(penv->get_open_flags(&oflags), ret2); |
| 3288 |
// The transaction handle in CDS is not a real |
| 3289 |
// transaction. |
| 3290 |
if (oflags & DB_INIT_CDB) |
| 3291 |
ptxn = NULL; |
| 3292 |
if (name1.length() > 0) |
| 3293 |
BDBOP2(penv->dbremove(ptxn, name1.c_str(), |
| 3294 |
NULL, 0), ret2, this->abort_txn()); |
| 3295 |
if (name2.length() > 0) |
| 3296 |
BDBOP2(penv->dbremove(ptxn, name2.c_str(), |
| 3297 |
NULL, 0), ret2, this->abort_txn()); |
| 3298 |
|
| 3299 |
this->commit_txn(); |
| 3300 |
return ret; |
| 3301 |
|
| 3302 |
} catch (...) { |
| 3303 |
this->abort_txn(); |
| 3304 |
throw; |
| 3305 |
} |
| 3306 |
// Now that m1 and m2 has the same number of unique elements and all |
| 3307 |
// elements of m1 are in m2, thus there can be no element of m2 that |
| 3308 |
// dose not belong to m1, so we won't verify each element of m2 are |
| 3309 |
// in m1. |
| 3310 |
// |
| 3311 |
|
| 3312 |
} // operator== |
| 3313 |
|
| 3314 |
/// Container unequality comparison operator. |
| 3315 |
/// \param m2 The container to compare against. |
| 3316 |
/// \return Returns false if equal, true otherwise. |
| 3317 |
bool operator!=(const db_multimap<kdt, ddt, value_type_sub>& m2) const |
| 3318 |
{ |
| 3319 |
return !this->operator==(m2); |
| 3320 |
} |
| 3321 |
//////////////////////////////////////////////////////////////// |
| 3322 |
|
| 3323 |
protected: |
| 3324 |
typedef ddt mapped_type; |
| 3325 |
typedef value_type_sub tkpair; |
| 3326 |
typedef int (*bt_compare_fcn_t)(Db *db, const Dbt *dbt1, |
| 3327 |
const Dbt *dbt2); |
| 3328 |
|
| 3329 |
friend class db_map_iterator<kdt, _DB_STL_set_value<kdt>, |
| 3330 |
value_type_sub>; |
| 3331 |
friend class db_map_iterator<kdt, ddt, value_type_sub>; |
| 3332 |
|
| 3333 |
db_multimap(BulkRetrievalOption &opt) : base(opt){} |
| 3334 |
private: |
| 3335 |
value_type_sub operator[] (const key_type& x) |
| 3336 |
{ |
| 3337 |
THROW(NotSupportedException, ("db_multimap<>::operator[]")); |
| 3338 |
} |
| 3339 |
|
| 3340 |
value_type_sub operator[] (const key_type& x) const |
| 3341 |
{ |
| 3342 |
THROW(NotSupportedException, ("db_multimap<>::operator[]")); |
| 3343 |
|
| 3344 |
} |
| 3345 |
|
| 3346 |
virtual const char* verify_config(Db*dbp, DbEnv* envp) const |
| 3347 |
{ |
| 3348 |
DBTYPE dbtype; |
| 3349 |
u_int32_t oflags, sflags; |
| 3350 |
int ret; |
| 3351 |
const char *err = NULL; |
| 3352 |
|
| 3353 |
err = db_container::verify_config(dbp, envp); |
| 3354 |
if (err) |
| 3355 |
return err; |
| 3356 |
|
| 3357 |
BDBOP(dbp->get_type(&dbtype), ret); |
| 3358 |
BDBOP(dbp->get_open_flags(&oflags), ret); |
| 3359 |
BDBOP(dbp->get_flags(&sflags), ret); |
| 3360 |
|
| 3361 |
if (dbtype != DB_BTREE && dbtype != DB_HASH) |
| 3362 |
err = |
| 3363 |
"wrong database type, only DB_BTREE and DB_HASH allowed for db_map<> class"; |
| 3364 |
if (oflags & DB_TRUNCATE) |
| 3365 |
err = |
| 3366 |
"do not specify DB_TRUNCATE flag to create a db_map<> object"; |
| 3367 |
|
| 3368 |
// Can't go without no dup or dupsort flag set. |
| 3369 |
if (!((sflags & DB_DUP) || (sflags & DB_DUPSORT))) |
| 3370 |
err = |
| 3371 |
"db_multimap<> can not be backed by database not permitting duplicate keys"; |
| 3372 |
|
| 3373 |
if (sflags & DB_RECNUM) |
| 3374 |
err = "no DB_RECNUM flag allowed in db_map<>"; |
| 3375 |
|
| 3376 |
return err; |
| 3377 |
|
| 3378 |
} |
| 3379 |
|
| 3380 |
inline void copy_db(db_multimap<kdt, ddt, value_type_sub> &x) |
| 3381 |
{ |
| 3382 |
|
| 3383 |
// Make sure clear can succeed if there are cursors |
| 3384 |
// open in other threads. |
| 3385 |
this->clear(false); |
| 3386 |
insert(x.begin(), x.end()); |
| 3387 |
|
| 3388 |
} |
| 3389 |
|
| 3390 |
};// db_multimap<> |
| 3391 |
//@} //dbstl_containers |
| 3392 |
END_NS |
| 3393 |
|
| 3394 |
#endif // !_DB_STL_DB_MAP_H_ |