diff options
Diffstat (limited to 'cpp/src/Freeze')
-rw-r--r-- | cpp/src/Freeze/ConnectionI.cpp | 36 | ||||
-rw-r--r-- | cpp/src/Freeze/ConnectionI.h | 6 | ||||
-rw-r--r-- | cpp/src/Freeze/EvictorI.cpp | 2280 | ||||
-rw-r--r-- | cpp/src/Freeze/EvictorI.h | 16 | ||||
-rw-r--r-- | cpp/src/Freeze/EvictorIteratorI.cpp | 274 | ||||
-rw-r--r-- | cpp/src/Freeze/IndexI.cpp | 402 | ||||
-rw-r--r-- | cpp/src/Freeze/MapI.cpp | 1552 | ||||
-rw-r--r-- | cpp/src/Freeze/MapI.h | 30 | ||||
-rw-r--r-- | cpp/src/Freeze/ObjectStore.cpp | 362 | ||||
-rw-r--r-- | cpp/src/Freeze/ObjectStore.h | 2 | ||||
-rw-r--r-- | cpp/src/Freeze/SharedDb.cpp | 402 | ||||
-rw-r--r-- | cpp/src/Freeze/SharedDb.h | 34 | ||||
-rw-r--r-- | cpp/src/Freeze/SharedDbEnv.cpp | 374 | ||||
-rw-r--r-- | cpp/src/Freeze/TransactionHolder.cpp | 50 | ||||
-rw-r--r-- | cpp/src/Freeze/TransactionI.cpp | 160 | ||||
-rw-r--r-- | cpp/src/Freeze/TransactionI.h | 2 | ||||
-rw-r--r-- | cpp/src/Freeze/Util.cpp | 72 | ||||
-rw-r--r-- | cpp/src/Freeze/Util.h | 4 |
18 files changed, 3029 insertions, 3029 deletions
diff --git a/cpp/src/Freeze/ConnectionI.cpp b/cpp/src/Freeze/ConnectionI.cpp index 8b823a613ad..3f93a4eebaa 100644 --- a/cpp/src/Freeze/ConnectionI.cpp +++ b/cpp/src/Freeze/ConnectionI.cpp @@ -21,7 +21,7 @@ Freeze::ConnectionI::beginTransaction() { if(_transaction != 0) { - throw TransactionAlreadyInProgressException(__FILE__, __LINE__); + throw TransactionAlreadyInProgressException(__FILE__, __LINE__); } closeAllIterators(); _transaction = new TransactionI(this); @@ -39,21 +39,21 @@ Freeze::ConnectionI::close() { if(_transaction != 0) { - try - { - _transaction->rollback(); - } - catch(const DatabaseException&) - { - // - // Ignored - // - } + try + { + _transaction->rollback(); + } + catch(const DatabaseException&) + { + // + // Ignored + // + } } while(!_mapList.empty()) { - (*_mapList.begin())->close(); + (*_mapList.begin())->close(); } _dbEnv = 0; @@ -79,7 +79,7 @@ Freeze::ConnectionI::~ConnectionI() } Freeze::ConnectionI::ConnectionI(const CommunicatorPtr& communicator, - const string& envName, DbEnv* dbEnv) : + const string& envName, DbEnv* dbEnv) : _communicator(communicator), _dbEnv(SharedDbEnv::get(communicator, envName, dbEnv)), _envName(envName), @@ -93,9 +93,9 @@ void Freeze::ConnectionI::closeAllIterators() { for(list<MapHelperI*>::iterator p = _mapList.begin(); p != _mapList.end(); - ++p) + ++p) { - (*p)->closeAllIterators(); + (*p)->closeAllIterators(); } } @@ -113,7 +113,7 @@ Freeze::ConnectionI::unregisterMap(MapHelperI* m) Freeze::ConnectionPtr Freeze::createConnection(const CommunicatorPtr& communicator, - const string& envName) + const string& envName) { return new ConnectionI(communicator, envName, 0); @@ -121,8 +121,8 @@ Freeze::createConnection(const CommunicatorPtr& communicator, Freeze::ConnectionPtr Freeze::createConnection(const CommunicatorPtr& communicator, - const string& envName, - DbEnv& dbEnv) + const string& envName, + DbEnv& dbEnv) { return new ConnectionI(communicator, envName, &dbEnv); } diff --git a/cpp/src/Freeze/ConnectionI.h b/cpp/src/Freeze/ConnectionI.h index 7ee5a59390e..987077b1adf 100644 --- a/cpp/src/Freeze/ConnectionI.h +++ b/cpp/src/Freeze/ConnectionI.h @@ -43,7 +43,7 @@ public: virtual ~ConnectionI(); ConnectionI(const Ice::CommunicatorPtr& communicator, - const std::string& envName, DbEnv*); + const std::string& envName, DbEnv*); void closeAllIterators(); @@ -101,11 +101,11 @@ ConnectionI::dbTxn() const { if(_transaction == 0) { - return 0; + return 0; } else { - return _transaction->dbTxn(); + return _transaction->dbTxn(); } } diff --git a/cpp/src/Freeze/EvictorI.cpp b/cpp/src/Freeze/EvictorI.cpp index 75e1722c004..a3bffc1a2bc 100644 --- a/cpp/src/Freeze/EvictorI.cpp +++ b/cpp/src/Freeze/EvictorI.cpp @@ -36,23 +36,23 @@ string Freeze::EvictorI::indexPrefix = "$index:"; Freeze::EvictorPtr Freeze::createEvictor(const ObjectAdapterPtr& adapter, - const string& envName, - const string& filename, - const ServantInitializerPtr& initializer, - const vector<IndexPtr>& indices, - bool createDb) + const string& envName, + const string& filename, + const ServantInitializerPtr& initializer, + const vector<IndexPtr>& indices, + bool createDb) { return new EvictorI(adapter, envName, 0, filename, initializer, indices, createDb); } Freeze::EvictorPtr Freeze::createEvictor(const ObjectAdapterPtr& adapter, - const string& envName, - DbEnv& dbEnv, - const string& filename, - const ServantInitializerPtr& initializer, - const vector<IndexPtr>& indices, - bool createDb) + const string& envName, + DbEnv& dbEnv, + const string& filename, + const ServantInitializerPtr& initializer, + const vector<IndexPtr>& indices, + bool createDb) { return new EvictorI(adapter, envName, &dbEnv, filename, initializer, indices, createDb); } @@ -78,11 +78,11 @@ handleFatalError(const Freeze::EvictorPtr& evictor, const Ice::CommunicatorPtr& IceUtil::StaticMutex::Lock lock(fatalErrorCallbackMutex); if(fatalErrorCallback != 0) { - fatalErrorCallback(evictor, communicator); + fatalErrorCallback(evictor, communicator); } else { - ::abort(); + ::abort(); } } @@ -112,7 +112,7 @@ Freeze::DeactivateController::Guard::Guard(DeactivateController& controller) : Lock sync(controller); if(controller._deactivated || _controller._deactivating) { - throw EvictorDeactivatedException(__FILE__, __LINE__); + throw EvictorDeactivatedException(__FILE__, __LINE__); } controller._guardCount++; } @@ -123,11 +123,11 @@ Freeze::DeactivateController::Guard::~Guard() _controller._guardCount--; if(_controller._deactivating && _controller._guardCount == 0) { - // - // Notify all the threads -- although we only want to - // reach the thread doing the deactivation. - // - _controller.notifyAll(); + // + // Notify all the threads -- although we only want to + // reach the thread doing the deactivation. + // + _controller.notifyAll(); } } @@ -153,40 +153,40 @@ Freeze::DeactivateController::deactivate() if(_deactivated) { - return false; + return false; } if(_deactivating) { - // - // Wait for deactivated - // - while(!_deactivated) - { - wait(); - } - return false; + // + // Wait for deactivated + // + while(!_deactivated) + { + wait(); + } + return false; } else { - _deactivating = true; - while(_guardCount > 0) - { - if(_evictor->trace() >= 1) - { - Trace out(_evictor->communicator()->getLogger(), "Freeze.Evictor"); - out << "*** Waiting for " << _guardCount << " threads to complete before starting deactivation."; - } - - wait(); - } - - if(_evictor->trace() >= 1) - { - Trace out(_evictor->communicator()->getLogger(), "Freeze.Evictor"); - out << "Starting deactivation."; - } - return true; + _deactivating = true; + while(_guardCount > 0) + { + if(_evictor->trace() >= 1) + { + Trace out(_evictor->communicator()->getLogger(), "Freeze.Evictor"); + out << "*** Waiting for " << _guardCount << " threads to complete before starting deactivation."; + } + + wait(); + } + + if(_evictor->trace() >= 1) + { + Trace out(_evictor->communicator()->getLogger(), "Freeze.Evictor"); + out << "Starting deactivation."; + } + return true; } } @@ -195,8 +195,8 @@ Freeze::DeactivateController::deactivationComplete() { if(_evictor->trace() >= 1) { - Trace out(_evictor->communicator()->getLogger(), "Freeze.Evictor"); - out << "Deactivation complete."; + Trace out(_evictor->communicator()->getLogger(), "Freeze.Evictor"); + out << "Deactivation complete."; } Lock sync(*this); @@ -225,20 +225,20 @@ Freeze::WatchDogThread::run() while(!_done) { - if(_active) - { - if(timedWait(_timeout) == false && _active && !_done) - { - Error out(_evictor.communicator()->getLogger()); - out << "Fatal error: streaming watch dog thread timed out."; - out.flush(); - handleFatalError(&_evictor, _evictor.communicator()); - } - } - else - { - wait(); - } + if(_active) + { + if(timedWait(_timeout) == false && _active && !_done) + { + Error out(_evictor.communicator()->getLogger()); + out << "Fatal error: streaming watch dog thread timed out."; + out.flush(); + handleFatalError(&_evictor, _evictor.communicator()); + } + } + else + { + wait(); + } } } @@ -270,12 +270,12 @@ Freeze::WatchDogThread::terminate() // Freeze::EvictorI::EvictorI(const ObjectAdapterPtr& adapter, - const string& envName, - DbEnv* dbEnv, - const string& filename, - const ServantInitializerPtr& initializer, - const vector<IndexPtr>& indices, - bool createDb) : + const string& envName, + DbEnv* dbEnv, + const string& filename, + const ServantInitializerPtr& initializer, + const vector<IndexPtr>& indices, + bool createDb) : _evictorSize(10), _currentEvictorSize(0), @@ -304,10 +304,10 @@ Freeze::EvictorI::EvictorI(const ObjectAdapterPtr& adapter, // _saveSizeTrigger = _communicator->getProperties()-> - getPropertyAsIntWithDefault(propertyPrefix + ".SaveSizeTrigger", 10); + getPropertyAsIntWithDefault(propertyPrefix + ".SaveSizeTrigger", 10); Int savePeriod = _communicator->getProperties()-> - getPropertyAsIntWithDefault(propertyPrefix + ".SavePeriod", 60 * 1000); + getPropertyAsIntWithDefault(propertyPrefix + ".SavePeriod", 60 * 1000); _savePeriod = IceUtil::Time::milliSeconds(savePeriod); @@ -315,17 +315,17 @@ Freeze::EvictorI::EvictorI(const ObjectAdapterPtr& adapter, // By default, we save at most 10 * SaveSizeTrigger objects per transaction // _maxTxSize = _communicator->getProperties()-> - getPropertyAsIntWithDefault(propertyPrefix + ".MaxTxSize", 10 * _saveSizeTrigger); + getPropertyAsIntWithDefault(propertyPrefix + ".MaxTxSize", 10 * _saveSizeTrigger); if(_maxTxSize <= 0) { - _maxTxSize = 100; - } + _maxTxSize = 100; + } bool populateEmptyIndices = - (_communicator->getProperties()-> - getPropertyAsIntWithDefault(propertyPrefix + ".PopulateEmptyIndices", 0) != 0); - + (_communicator->getProperties()-> + getPropertyAsIntWithDefault(propertyPrefix + ".PopulateEmptyIndices", 0) != 0); + // // Instantiate all Dbs in 2 steps: // (1) iterate over the indices and create ObjectStore with indices @@ -342,57 +342,57 @@ Freeze::EvictorI::EvictorI(const ObjectAdapterPtr& adapter, for(vector<IndexPtr>::const_iterator i = indices.begin(); i != indices.end(); ++i) { - string facet = (*i)->facet(); + string facet = (*i)->facet(); - StoreMap::iterator q = _storeMap.find(facet); - if(q == _storeMap.end()) - { - // - // New db - // + StoreMap::iterator q = _storeMap.find(facet); + if(q == _storeMap.end()) + { + // + // New db + // - vector<IndexPtr> storeIndices; + vector<IndexPtr> storeIndices; - for(vector<IndexPtr>::const_iterator r = i; r != indices.end(); ++r) - { - if((*r)->facet() == facet) - { - storeIndices.push_back(*r); - } - } - ObjectStore* store = new ObjectStore(facet, _createDb, this, storeIndices, populateEmptyIndices); - _storeMap.insert(StoreMap::value_type(facet, store)); - } + for(vector<IndexPtr>::const_iterator r = i; r != indices.end(); ++r) + { + if((*r)->facet() == facet) + { + storeIndices.push_back(*r); + } + } + ObjectStore* store = new ObjectStore(facet, _createDb, this, storeIndices, populateEmptyIndices); + _storeMap.insert(StoreMap::value_type(facet, store)); + } } for(vector<string>::iterator p = dbs.begin(); p != dbs.end(); ++p) { - string facet = *p; - if(facet == defaultDb) - { - facet = ""; - } - - pair<StoreMap::iterator, bool> ir = - _storeMap.insert(StoreMap::value_type(facet, 0)); - - if(ir.second) - { - ir.first->second = new ObjectStore(facet, _createDb, this); - } + string facet = *p; + if(facet == defaultDb) + { + facet = ""; + } + + pair<StoreMap::iterator, bool> ir = + _storeMap.insert(StoreMap::value_type(facet, 0)); + + if(ir.second) + { + ir.first->second = new ObjectStore(facet, _createDb, this); + } } // // By default, no stream timeout // long streamTimeout = _communicator->getProperties()-> - getPropertyAsIntWithDefault(propertyPrefix+ ".StreamTimeout", 0) * 1000; + getPropertyAsIntWithDefault(propertyPrefix+ ".StreamTimeout", 0) * 1000; if(streamTimeout > 0) { - _watchDogThread = new WatchDogThread(streamTimeout, *this); - _watchDogThread->start(); + _watchDogThread = new WatchDogThread(streamTimeout, *this); + _watchDogThread->start(); } // @@ -405,13 +405,13 @@ Freeze::EvictorI::~EvictorI() { if(!_deactivateController.deactivated()) { - Warning out(_communicator->getLogger()); - out << "evictor has not been deactivated"; + Warning out(_communicator->getLogger()); + out << "evictor has not been deactivated"; - // - // Need to deactivate to save objects and join saving thread - // - deactivate(""); + // + // Need to deactivate to save objects and join saving thread + // + deactivate(""); } // @@ -419,7 +419,7 @@ Freeze::EvictorI::~EvictorI() // for(StoreMap::iterator p = _storeMap.begin(); p != _storeMap.end(); ++p) { - delete (*p).second; + delete (*p).second; } } @@ -435,7 +435,7 @@ Freeze::EvictorI::setSize(Int evictorSize) // if(evictorSize < 0) { - return; + return; } // @@ -473,35 +473,35 @@ Freeze::EvictorI::addFacet(const ObjectPtr& servant, const Identity& ident, cons for(;;) { - { - Lock sync(*this); - - StoreMap::iterator p = _storeMap.find(facet); - if(p == _storeMap.end()) - { - if(store != 0) - { - _storeMap.insert(StoreMap::value_type(facet, store)); - } - } - else - { - delete store; - store = (*p).second; - assert(store != 0); - } - } - - if(store == 0) - { - assert(facet != ""); - store = new ObjectStore(facet, _createDb, this); - // loop - } - else - { - break; // for(;;) - } + { + Lock sync(*this); + + StoreMap::iterator p = _storeMap.find(facet); + if(p == _storeMap.end()) + { + if(store != 0) + { + _storeMap.insert(StoreMap::value_type(facet, store)); + } + } + else + { + delete store; + store = (*p).second; + assert(store != 0); + } + } + + if(store == 0) + { + assert(facet != ""); + store = new ObjectStore(facet, _createDb, this); + // loop + } + else + { + break; // for(;;) + } } assert(store != 0); @@ -509,103 +509,103 @@ Freeze::EvictorI::addFacet(const ObjectPtr& servant, const Identity& ident, cons for(;;) { - // - // Create a new entry - // - - EvictorElementPtr element = new EvictorElement(*store); - element->status = EvictorElement::dead; - EvictorElementPtr oldElt = store->putIfAbsent(ident, element); + // + // Create a new entry + // + + EvictorElementPtr element = new EvictorElement(*store); + element->status = EvictorElement::dead; + EvictorElementPtr oldElt = store->putIfAbsent(ident, element); - if(oldElt != 0) - { - element = oldElt; - } - - { - Lock sync(*this); - - if(element->stale) - { - // - // Try again - // - continue; - } - fixEvictPosition(element); - - IceUtil::Mutex::Lock lock(element->mutex); - - switch(element->status) - { - case EvictorElement::clean: - case EvictorElement::created: - case EvictorElement::modified: - { - alreadyThere = true; - break; - } - case EvictorElement::destroyed: - { - element->status = EvictorElement::modified; - element->rec.servant = servant; - - // - // No need to push it on the modified queue, as a destroyed object - // is either already on the queue or about to be saved. When saved, - // it becomes dead. - // - break; - } - case EvictorElement::dead: - { - element->status = EvictorElement::created; - ObjectRecord& rec = element->rec; - - rec.servant = servant; - rec.stats.creationTime = IceUtil::Time::now().toMilliSeconds(); - rec.stats.lastSaveTime = 0; - rec.stats.avgSaveTime = 0; - - addToModifiedQueue(element); - break; - } - default: - { - assert(0); - break; - } - } - } - break; // for(;;) + if(oldElt != 0) + { + element = oldElt; + } + + { + Lock sync(*this); + + if(element->stale) + { + // + // Try again + // + continue; + } + fixEvictPosition(element); + + IceUtil::Mutex::Lock lock(element->mutex); + + switch(element->status) + { + case EvictorElement::clean: + case EvictorElement::created: + case EvictorElement::modified: + { + alreadyThere = true; + break; + } + case EvictorElement::destroyed: + { + element->status = EvictorElement::modified; + element->rec.servant = servant; + + // + // No need to push it on the modified queue, as a destroyed object + // is either already on the queue or about to be saved. When saved, + // it becomes dead. + // + break; + } + case EvictorElement::dead: + { + element->status = EvictorElement::created; + ObjectRecord& rec = element->rec; + + rec.servant = servant; + rec.stats.creationTime = IceUtil::Time::now().toMilliSeconds(); + rec.stats.lastSaveTime = 0; + rec.stats.avgSaveTime = 0; + + addToModifiedQueue(element); + break; + } + default: + { + assert(0); + break; + } + } + } + break; // for(;;) } if(alreadyThere) { - AlreadyRegisteredException ex(__FILE__, __LINE__); - ex.kindOfObject = "servant"; - ex.id = _communicator->identityToString(ident); - if(!facet.empty()) - { - ex.id += " -f " + IceUtil::escapeString(facet, ""); - } - throw ex; + AlreadyRegisteredException ex(__FILE__, __LINE__); + ex.kindOfObject = "servant"; + ex.id = _communicator->identityToString(ident); + if(!facet.empty()) + { + ex.id += " -f " + IceUtil::escapeString(facet, ""); + } + throw ex; } if(_trace >= 1) { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "added object \"" << _communicator->identityToString(ident) << "\""; - if(!facet.empty()) - { - out << " with facet \"" << facet << "\""; - } + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "added object \"" << _communicator->identityToString(ident) << "\""; + if(!facet.empty()) + { + out << " with facet \"" << facet << "\""; + } } ObjectPrx obj = _adapter->createProxy(ident); if(!facet.empty()) { - obj = obj->ice_facet(facet); + obj = obj->ice_facet(facet); } return obj; } @@ -627,108 +627,108 @@ Freeze::EvictorI::removeFacet(const Identity& ident, const string& facet) if(store != 0) { - for(;;) - { - // - // Retrieve object - // - - EvictorElementPtr element = store->pin(ident); - if(element != 0) - { - Lock sync(*this); - if(element->stale) - { - // - // Try again - // - continue; - } - - fixEvictPosition(element); - { - IceUtil::Mutex::Lock lock(element->mutex); - - switch(element->status) - { - case EvictorElement::clean: - { - servant = element->rec.servant; - element->status = EvictorElement::destroyed; - element->rec.servant = 0; - addToModifiedQueue(element); - break; - } - case EvictorElement::created: - { - servant = element->rec.servant; - element->status = EvictorElement::dead; - element->rec.servant = 0; - break; - } - case EvictorElement::modified: - { - servant = element->rec.servant; - element->status = EvictorElement::destroyed; - element->rec.servant = 0; - // - // Not necessary to push it on the modified queue, as a modified - // element is either on the queue already or about to be saved - // (at which point it becomes clean) - // - break; - } - case EvictorElement::destroyed: - case EvictorElement::dead: - { - break; - } - default: - { - assert(0); - break; - } - } - } - if(element->keepCount > 0) - { - assert(servant != 0); - - element->keepCount = 0; - // - // Add to front of evictor queue - // - // Note that save evicts dead objects - // - _evictorList.push_front(element); - _currentEvictorSize++; - element->evictPosition = _evictorList.begin(); - } - } - break; // for(;;) - } + for(;;) + { + // + // Retrieve object + // + + EvictorElementPtr element = store->pin(ident); + if(element != 0) + { + Lock sync(*this); + if(element->stale) + { + // + // Try again + // + continue; + } + + fixEvictPosition(element); + { + IceUtil::Mutex::Lock lock(element->mutex); + + switch(element->status) + { + case EvictorElement::clean: + { + servant = element->rec.servant; + element->status = EvictorElement::destroyed; + element->rec.servant = 0; + addToModifiedQueue(element); + break; + } + case EvictorElement::created: + { + servant = element->rec.servant; + element->status = EvictorElement::dead; + element->rec.servant = 0; + break; + } + case EvictorElement::modified: + { + servant = element->rec.servant; + element->status = EvictorElement::destroyed; + element->rec.servant = 0; + // + // Not necessary to push it on the modified queue, as a modified + // element is either on the queue already or about to be saved + // (at which point it becomes clean) + // + break; + } + case EvictorElement::destroyed: + case EvictorElement::dead: + { + break; + } + default: + { + assert(0); + break; + } + } + } + if(element->keepCount > 0) + { + assert(servant != 0); + + element->keepCount = 0; + // + // Add to front of evictor queue + // + // Note that save evicts dead objects + // + _evictorList.push_front(element); + _currentEvictorSize++; + element->evictPosition = _evictorList.begin(); + } + } + break; // for(;;) + } } if(servant == 0) { - NotRegisteredException ex(__FILE__, __LINE__); - ex.kindOfObject = "servant"; - ex.id = _communicator->identityToString(ident); - if(!facet.empty()) - { - ex.id += " -f " + IceUtil::escapeString(facet, ""); - } - throw ex; + NotRegisteredException ex(__FILE__, __LINE__); + ex.kindOfObject = "servant"; + ex.id = _communicator->identityToString(ident); + if(!facet.empty()) + { + ex.id += " -f " + IceUtil::escapeString(facet, ""); + } + throw ex; } if(_trace >= 1) { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "removed object \"" << _communicator->identityToString(ident) << "\""; - if(!facet.empty()) - { - out << " with facet \"" << facet << "\""; - } + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "removed object \"" << _communicator->identityToString(ident) << "\""; + if(!facet.empty()) + { + out << " with facet \"" << facet << "\""; + } } return servant; } @@ -750,77 +750,77 @@ Freeze::EvictorI::keepFacet(const Identity& ident, const string& facet) ObjectStore* store = findStore(facet); if(store == 0) { - notThere = true; + notThere = true; } else { - for(;;) - { - EvictorElementPtr element = store->pin(ident); - if(element == 0) - { - notThere = true; - break; - } - - Lock sync(*this); - - if(element->stale) - { - // - // try again - // - continue; - } - - - { - IceUtil::Mutex::Lock lockElement(element->mutex); - if(element->status == EvictorElement::destroyed || element->status == EvictorElement::dead) - { - notThere = true; - break; - } - } - - // - // Found! - // - - if(element->keepCount == 0) - { - if(element->usageCount < 0) - { - // - // New object - // - element->usageCount = 0; - } - else - { - _evictorList.erase(element->evictPosition); - _currentEvictorSize--; - } - element->keepCount = 1; - } - else - { - element->keepCount++; - } - break; - } + for(;;) + { + EvictorElementPtr element = store->pin(ident); + if(element == 0) + { + notThere = true; + break; + } + + Lock sync(*this); + + if(element->stale) + { + // + // try again + // + continue; + } + + + { + IceUtil::Mutex::Lock lockElement(element->mutex); + if(element->status == EvictorElement::destroyed || element->status == EvictorElement::dead) + { + notThere = true; + break; + } + } + + // + // Found! + // + + if(element->keepCount == 0) + { + if(element->usageCount < 0) + { + // + // New object + // + element->usageCount = 0; + } + else + { + _evictorList.erase(element->evictPosition); + _currentEvictorSize--; + } + element->keepCount = 1; + } + else + { + element->keepCount++; + } + break; + } } if(notThere) { - NotRegisteredException ex(__FILE__, __LINE__); - ex.kindOfObject = "servant"; - ex.id = _communicator->identityToString(ident); - if(!facet.empty()) - { - ex.id += " -f " + IceUtil::escapeString(facet, ""); - } - throw ex; + NotRegisteredException ex(__FILE__, __LINE__); + ex.kindOfObject = "servant"; + ex.id = _communicator->identityToString(ident); + if(!facet.empty()) + { + ex.id += " -f " + IceUtil::escapeString(facet, ""); + } + throw ex; } } @@ -837,38 +837,38 @@ Freeze::EvictorI::releaseFacet(const Identity& ident, const string& facet) DeactivateController::Guard deactivateGuard(_deactivateController); { - Lock sync(*this); - - StoreMap::iterator p = _storeMap.find(facet); - if(p != _storeMap.end()) - { - ObjectStore* store = (*p).second; - - EvictorElementPtr element = store->getIfPinned(ident); - if(element != 0) - { - assert(!element->stale); - if(element->keepCount > 0) - { - if(--element->keepCount == 0) - { - // - // Add to front of evictor queue - // - // Note that the element cannot be destroyed or dead since - // its keepCount was > 0. - // - _evictorList.push_front(element); - _currentEvictorSize++; - element->evictPosition = _evictorList.begin(); - } - // - // Success - // - return; - } - } - } + Lock sync(*this); + + StoreMap::iterator p = _storeMap.find(facet); + if(p != _storeMap.end()) + { + ObjectStore* store = (*p).second; + + EvictorElementPtr element = store->getIfPinned(ident); + if(element != 0) + { + assert(!element->stale); + if(element->keepCount > 0) + { + if(--element->keepCount == 0) + { + // + // Add to front of evictor queue + // + // Note that the element cannot be destroyed or dead since + // its keepCount was > 0. + // + _evictorList.push_front(element); + _currentEvictorSize++; + element->evictPosition = _evictorList.begin(); + } + // + // Success + // + return; + } + } + } } NotRegisteredException ex(__FILE__, __LINE__); @@ -876,7 +876,7 @@ Freeze::EvictorI::releaseFacet(const Identity& ident, const string& facet) ex.id = _communicator->identityToString(ident); if(!facet.empty()) { - ex.id += " -f " + IceUtil::escapeString(facet, ""); + ex.id += " -f " + IceUtil::escapeString(facet, ""); } throw ex; } @@ -888,14 +888,14 @@ Freeze::EvictorI::getIterator(const string& facet, Int batchSize) ObjectStore* store = 0; { - Lock sync(*this); - - StoreMap::iterator p = _storeMap.find(facet); - if(p != _storeMap.end()) - { - store = (*p).second; - saveNowNoSync(); - } + Lock sync(*this); + + StoreMap::iterator p = _storeMap.find(facet); + if(p != _storeMap.end()) + { + store = (*p).second; + saveNowNoSync(); + } } return new EvictorIteratorI(store, batchSize); } @@ -924,25 +924,25 @@ Freeze::EvictorI::hasFacetImpl(const Identity& ident, const string& facet) ObjectStore* store = 0; { - Lock sync(*this); + Lock sync(*this); - StoreMap::iterator p = _storeMap.find(facet); - if(p == _storeMap.end()) - { - return false; - } - - store = (*p).second; - - EvictorElementPtr element = store->getIfPinned(ident); - if(element != 0) - { - assert(!element->stale); - - IceUtil::Mutex::Lock lock(element->mutex); - return element->status != EvictorElement::dead && - element->status != EvictorElement::destroyed; - } + StoreMap::iterator p = _storeMap.find(facet); + if(p == _storeMap.end()) + { + return false; + } + + store = (*p).second; + + EvictorElementPtr element = store->getIfPinned(ident); + if(element != 0) + { + assert(!element->stale); + + IceUtil::Mutex::Lock lock(element->mutex); + return element->status != EvictorElement::dead && + element->status != EvictorElement::destroyed; + } } return store->dbHasObject(ident); } @@ -960,45 +960,45 @@ Freeze::EvictorI::hasAnotherFacet(const Identity& ident, const string& facet) // StoreMap storeMapCopy; { - Lock sync(*this); - storeMapCopy = _storeMap; - } - + Lock sync(*this); + storeMapCopy = _storeMap; + } + for(StoreMap::iterator p = storeMapCopy.begin(); p != storeMapCopy.end(); ++p) { - // - // Do not check again the given facet - // - if((*p).first != facet) - { - ObjectStore* store = (*p).second; - - bool inCache = false; - { - Lock sync(*this); - - EvictorElementPtr element = store->getIfPinned(ident); - if(element != 0) - { - inCache = true; - assert(!element->stale); - - IceUtil::Mutex::Lock lock(element->mutex); - if(element->status != EvictorElement::dead && - element->status != EvictorElement::destroyed) - { - return true; - } - } - } - if(!inCache) - { - if(store->dbHasObject(ident)) - { - return true; - } - } - } + // + // Do not check again the given facet + // + if((*p).first != facet) + { + ObjectStore* store = (*p).second; + + bool inCache = false; + { + Lock sync(*this); + + EvictorElementPtr element = store->getIfPinned(ident); + if(element != 0) + { + inCache = true; + assert(!element->stale); + + IceUtil::Mutex::Lock lock(element->mutex); + if(element->status != EvictorElement::dead && + element->status != EvictorElement::destroyed) + { + return true; + } + } + } + if(!inCache) + { + if(store->dbHasObject(ident)) + { + return true; + } + } + } } return false; } @@ -1018,48 +1018,48 @@ Freeze::EvictorI::locate(const Current& current, LocalObjectPtr& cookie) // if(current.operation == "ice_ping") { - if(hasFacetImpl(current.id, current.facet)) - { - if(_trace >= 3) - { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "ice_ping found \"" << _communicator->identityToString(current.id) - << "\" with facet \"" << current.facet + "\""; - } - - cookie = 0; - return _pingObject; - } - else if(hasAnotherFacet(current.id, current.facet)) - { - if(_trace >= 3) - { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "ice_ping raises FacetNotExistException for \"" << _communicator->identityToString(current.id) - << "\" with facet \"" << current.facet + "\""; - } - throw FacetNotExistException(__FILE__, __LINE__); - } - else - { - if(_trace >= 3) - { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "ice_ping will raise ObjectNotExistException for \"" - << _communicator->identityToString(current.id) << "\" with facet \"" << current.facet + "\""; - } - return 0; - } + if(hasFacetImpl(current.id, current.facet)) + { + if(_trace >= 3) + { + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "ice_ping found \"" << _communicator->identityToString(current.id) + << "\" with facet \"" << current.facet + "\""; + } + + cookie = 0; + return _pingObject; + } + else if(hasAnotherFacet(current.id, current.facet)) + { + if(_trace >= 3) + { + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "ice_ping raises FacetNotExistException for \"" << _communicator->identityToString(current.id) + << "\" with facet \"" << current.facet + "\""; + } + throw FacetNotExistException(__FILE__, __LINE__); + } + else + { + if(_trace >= 3) + { + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "ice_ping will raise ObjectNotExistException for \"" + << _communicator->identityToString(current.id) << "\" with facet \"" << current.facet + "\""; + } + return 0; + } } ObjectPtr result = locateImpl(current, cookie); if(result == 0) { - if(hasAnotherFacet(current.id, current.facet)) - { - throw FacetNotExistException(__FILE__, __LINE__); - } + if(hasAnotherFacet(current.id, current.facet)) + { + throw FacetNotExistException(__FILE__, __LINE__); + } } return result; } @@ -1074,65 +1074,65 @@ Freeze::EvictorI::locateImpl(const Current& current, LocalObjectPtr& cookie) if(store == 0) { if(_trace >= 2) - { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "locate could not find a database for facet \"" << current.facet << "\""; - } - return 0; + { + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "locate could not find a database for facet \"" << current.facet << "\""; + } + return 0; } for(;;) { - EvictorElementPtr element = store->pin(current.id); - if(element == 0) - { + EvictorElementPtr element = store->pin(current.id); + if(element == 0) + { if(_trace >= 2) - { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "locate could not find \"" << _communicator->identityToString(current.id) << "\" in database \"" - << current.facet << "\""; - } - return 0; - } - - Lock sync(*this); - - if(element->stale) - { - // - // try again - // - continue; - } - - - IceUtil::Mutex::Lock lockElement(element->mutex); - if(element->status == EvictorElement::destroyed || element->status == EvictorElement::dead) - { + { + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "locate could not find \"" << _communicator->identityToString(current.id) << "\" in database \"" + << current.facet << "\""; + } + return 0; + } + + Lock sync(*this); + + if(element->stale) + { + // + // try again + // + continue; + } + + + IceUtil::Mutex::Lock lockElement(element->mutex); + if(element->status == EvictorElement::destroyed || element->status == EvictorElement::dead) + { if(_trace >= 2) - { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "locate found \"" << _communicator->identityToString(current.id) - << "\" in the cache for database \"" << current.facet << "\" but it was dead or destroyed"; - } - return 0; - } - - // - // It's a good one! - // + { + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "locate found \"" << _communicator->identityToString(current.id) + << "\" in the cache for database \"" << current.facet << "\" but it was dead or destroyed"; + } + return 0; + } + + // + // It's a good one! + // if(_trace >= 2) - { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "locate found \"" << _communicator->identityToString(current.id) << "\" in database \"" - << current.facet << "\""; - } - - fixEvictPosition(element); - element->usageCount++; - cookie = element; - assert(element->rec.servant != 0); - return element->rec.servant; + { + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "locate found \"" << _communicator->identityToString(current.id) << "\" in database \"" + << current.facet << "\""; + } + + fixEvictPosition(element); + element->usageCount++; + cookie = element; + assert(element->rec.servant != 0); + return element->rec.servant; } } @@ -1148,51 +1148,51 @@ Freeze::EvictorI::finished(const Current& current, const ObjectPtr& servant, con if(cookie != 0) { - EvictorElementPtr element = EvictorElementPtr::dynamicCast(cookie); - assert(element); + EvictorElementPtr element = EvictorElementPtr::dynamicCast(cookie); + assert(element); - bool enqueue = false; - - if((_useNonmutating && current.mode != Nonmutating) || - (servant->ice_operationAttributes(current.operation) & 0x1) != 0) - { - IceUtil::Mutex::Lock lock(element->mutex); - - if(element->status == EvictorElement::clean) - { - // - // Assume this operation updated the object - // - element->status = EvictorElement::modified; - enqueue = true; - } - } - - Lock sync(*this); - - // - // Only elements with a usageCount == 0 can become stale and we own - // one count! - // - assert(!element->stale); - assert(element->usageCount >= 1); - - // - // Decrease the usage count of the evictor queue element. - // - element->usageCount--; - - if(enqueue) - { - addToModifiedQueue(element); - } - else if(element->usageCount == 0 && element->keepCount == 0) - { - // - // Evict as many elements as necessary. - // - evict(); - } + bool enqueue = false; + + if((_useNonmutating && current.mode != Nonmutating) || + (servant->ice_operationAttributes(current.operation) & 0x1) != 0) + { + IceUtil::Mutex::Lock lock(element->mutex); + + if(element->status == EvictorElement::clean) + { + // + // Assume this operation updated the object + // + element->status = EvictorElement::modified; + enqueue = true; + } + } + + Lock sync(*this); + + // + // Only elements with a usageCount == 0 can become stale and we own + // one count! + // + assert(!element->stale); + assert(element->usageCount >= 1); + + // + // Decrease the usage count of the evictor queue element. + // + element->usageCount--; + + if(enqueue) + { + addToModifiedQueue(element); + } + else if(element->usageCount == 0 && element->keepCount == 0) + { + // + // Evict as many elements as necessary. + // + evict(); + } } } @@ -1201,44 +1201,44 @@ Freeze::EvictorI::deactivate(const string&) { if(_deactivateController.deactivate()) { - try - { - Lock sync(*this); - - saveNowNoSync(); - - // - // Set the evictor size to zero, meaning that we will evict - // everything possible. - // - _evictorSize = 0; - evict(); - - _savingThreadDone = true; - notifyAll(); - sync.release(); - getThreadControl().join(); - - if(_watchDogThread != 0) - { - _watchDogThread->terminate(); - _watchDogThread->getThreadControl().join(); - } - - for(StoreMap::iterator p = _storeMap.begin(); p != _storeMap.end(); ++p) - { - (*p).second->close(); - } - - _dbEnv = 0; - _initializer = 0; - } - catch(...) - { - _deactivateController.deactivationComplete(); - throw; - } - _deactivateController.deactivationComplete(); + try + { + Lock sync(*this); + + saveNowNoSync(); + + // + // Set the evictor size to zero, meaning that we will evict + // everything possible. + // + _evictorSize = 0; + evict(); + + _savingThreadDone = true; + notifyAll(); + sync.release(); + getThreadControl().join(); + + if(_watchDogThread != 0) + { + _watchDogThread->terminate(); + _watchDogThread->getThreadControl().join(); + } + + for(StoreMap::iterator p = _storeMap.begin(); p != _storeMap.end(); ++p) + { + (*p).second->close(); + } + + _dbEnv = 0; + _initializer = 0; + } + catch(...) + { + _deactivateController.deactivationComplete(); + throw; + } + _deactivateController.deactivationComplete(); } } @@ -1248,7 +1248,7 @@ Freeze::EvictorI::initialize(const Identity& ident, const string& facet, const O { if(_initializer != 0) { - _initializer->initialize(_adapter, ident, facet, servant); + _initializer->initialize(_adapter, ident, facet, servant); } } @@ -1258,387 +1258,387 @@ Freeze::EvictorI::run() { try { - for(;;) - { - deque<EvictorElementPtr> allObjects; - deque<EvictorElementPtr> deadObjects; - - size_t saveNowThreadsSize = 0; - - { - Lock sync(*this); - - while(!_savingThreadDone && - (_saveNowThreads.size() == 0) && - (_saveSizeTrigger < 0 || static_cast<Int>(_modifiedQueue.size()) < _saveSizeTrigger)) - { - if(_savePeriod == IceUtil::Time::milliSeconds(0)) - { - wait(); - } - else if(timedWait(_savePeriod) == false) - { - // - // Timeout, so let's save - // - break; // while - } - } - - saveNowThreadsSize = _saveNowThreads.size(); - - if(_savingThreadDone) - { - assert(_modifiedQueue.size() == 0); - assert(saveNowThreadsSize == 0); - break; // for(;;) - } - - // - // Check first if there is something to do! - // - if(_modifiedQueue.size() == 0) - { - if(saveNowThreadsSize > 0) - { - _saveNowThreads.clear(); - notifyAll(); - } - continue; // for(;;) - } - - _modifiedQueue.swap(allObjects); - } - - const size_t size = allObjects.size(); - - deque<StreamedObject> streamedObjectQueue; - - Long streamStart = IceUtil::Time::now().toMilliSeconds(); - - // - // Stream each element - // - for(size_t i = 0; i < size; i++) - { - EvictorElementPtr& element = allObjects[i]; - - bool tryAgain; - do - { - tryAgain = false; - ObjectPtr servant = 0; - - // - // These elements can't be stale as only elements with - // usageCount == 0 can become stale, and the modifiedQueue - // (us now) owns one count. - // - - IceUtil::Mutex::Lock lockElement(element->mutex); - Byte status = element->status; - - switch(status) - { - case EvictorElement::created: - case EvictorElement::modified: - { - servant = element->rec.servant; - break; - } - case EvictorElement::destroyed: - { - size_t index = streamedObjectQueue.size(); - streamedObjectQueue.resize(index + 1); - StreamedObject& obj = streamedObjectQueue[index]; - stream(element, streamStart, obj); - - element->status = EvictorElement::dead; - deadObjects.push_back(element); - - break; - } - case EvictorElement::dead: - { - deadObjects.push_back(element); - break; - } - default: - { - // - // Nothing to do (could be a duplicate) - // - break; - } - } - if(servant == 0) - { - lockElement.release(); - } - else - { - IceUtil::AbstractMutex* mutex = dynamic_cast<IceUtil::AbstractMutex*>(servant.get()); - if(mutex != 0) - { - // - // Lock servant and then element so that user can safely lock - // servant and call various Evictor operations - // - - IceUtil::AbstractMutex::TryLock lockServant(*mutex); - if(!lockServant.acquired()) - { - lockElement.release(); - - if(_watchDogThread != 0) - { - _watchDogThread->activate(); - } - lockServant.acquire(); - if(_watchDogThread != 0) - { - _watchDogThread->deactivate(); - } - - lockElement.acquire(); - status = element->status; - } + for(;;) + { + deque<EvictorElementPtr> allObjects; + deque<EvictorElementPtr> deadObjects; + + size_t saveNowThreadsSize = 0; + + { + Lock sync(*this); + + while(!_savingThreadDone && + (_saveNowThreads.size() == 0) && + (_saveSizeTrigger < 0 || static_cast<Int>(_modifiedQueue.size()) < _saveSizeTrigger)) + { + if(_savePeriod == IceUtil::Time::milliSeconds(0)) + { + wait(); + } + else if(timedWait(_savePeriod) == false) + { + // + // Timeout, so let's save + // + break; // while + } + } + + saveNowThreadsSize = _saveNowThreads.size(); + + if(_savingThreadDone) + { + assert(_modifiedQueue.size() == 0); + assert(saveNowThreadsSize == 0); + break; // for(;;) + } + + // + // Check first if there is something to do! + // + if(_modifiedQueue.size() == 0) + { + if(saveNowThreadsSize > 0) + { + _saveNowThreads.clear(); + notifyAll(); + } + continue; // for(;;) + } + + _modifiedQueue.swap(allObjects); + } + + const size_t size = allObjects.size(); + + deque<StreamedObject> streamedObjectQueue; + + Long streamStart = IceUtil::Time::now().toMilliSeconds(); + + // + // Stream each element + // + for(size_t i = 0; i < size; i++) + { + EvictorElementPtr& element = allObjects[i]; + + bool tryAgain; + do + { + tryAgain = false; + ObjectPtr servant = 0; + + // + // These elements can't be stale as only elements with + // usageCount == 0 can become stale, and the modifiedQueue + // (us now) owns one count. + // + + IceUtil::Mutex::Lock lockElement(element->mutex); + Byte status = element->status; + + switch(status) + { + case EvictorElement::created: + case EvictorElement::modified: + { + servant = element->rec.servant; + break; + } + case EvictorElement::destroyed: + { + size_t index = streamedObjectQueue.size(); + streamedObjectQueue.resize(index + 1); + StreamedObject& obj = streamedObjectQueue[index]; + stream(element, streamStart, obj); + + element->status = EvictorElement::dead; + deadObjects.push_back(element); + + break; + } + case EvictorElement::dead: + { + deadObjects.push_back(element); + break; + } + default: + { + // + // Nothing to do (could be a duplicate) + // + break; + } + } + if(servant == 0) + { + lockElement.release(); + } + else + { + IceUtil::AbstractMutex* mutex = dynamic_cast<IceUtil::AbstractMutex*>(servant.get()); + if(mutex != 0) + { + // + // Lock servant and then element so that user can safely lock + // servant and call various Evictor operations + // + + IceUtil::AbstractMutex::TryLock lockServant(*mutex); + if(!lockServant.acquired()) + { + lockElement.release(); + + if(_watchDogThread != 0) + { + _watchDogThread->activate(); + } + lockServant.acquire(); + if(_watchDogThread != 0) + { + _watchDogThread->deactivate(); + } + + lockElement.acquire(); + status = element->status; + } - switch(status) - { - case EvictorElement::created: - case EvictorElement::modified: - { - if(servant == element->rec.servant) - { - size_t index = streamedObjectQueue.size(); - streamedObjectQueue.resize(index + 1); - StreamedObject& obj = streamedObjectQueue[index]; - stream(element, streamStart, obj); - - element->status = EvictorElement::clean; - } - else - { - tryAgain = true; - } - break; - } - case EvictorElement::destroyed: - { - lockServant.release(); - - size_t index = streamedObjectQueue.size(); - streamedObjectQueue.resize(index + 1); - StreamedObject& obj = streamedObjectQueue[index]; - stream(element, streamStart, obj); - - element->status = EvictorElement::dead; - deadObjects.push_back(element); - break; - } - case EvictorElement::dead: - { - deadObjects.push_back(element); - break; - } - default: - { - // - // Nothing to do (could be a duplicate) - // - break; - } - } - } - else - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = string(typeid(*element->rec.servant).name()) - + " does not implement IceUtil::AbstractMutex"; - throw ex; - } - } - } while(tryAgain); - } - - if(_trace >= 1) - { - Long now = IceUtil::Time::now().toMilliSeconds(); - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "streamed " << streamedObjectQueue.size() << " objects in " - << static_cast<Int>(now - streamStart) << " ms"; - } - - // - // Now let's save all these streamed objects to disk using a transaction - // - - // - // Each time we get a deadlock, we reduce the number of objects to save - // per transaction - // - size_t txSize = streamedObjectQueue.size(); - if(txSize > static_cast<size_t>(_maxTxSize)) - { - txSize = static_cast<size_t>(_maxTxSize); - } - bool tryAgain; - - do - { - tryAgain = false; - - while(streamedObjectQueue.size() > 0) - { - if(txSize > streamedObjectQueue.size()) - { - txSize = streamedObjectQueue.size(); - } - - Long saveStart = IceUtil::Time::now().toMilliSeconds(); - try - { - DbTxn* tx = 0; - _dbEnv->getEnv()->txn_begin(0, &tx, 0); - - long txnId = 0; - if(_txTrace >= 1) - { - txnId = (tx->id() & 0x7FFFFFFF) + 0x80000000L; - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "started transaction " << hex << txnId << dec << " in saving thread"; - } - - try - { - for(size_t i = 0; i < txSize; i++) - { - StreamedObject& obj = streamedObjectQueue[i]; - obj.store->save(obj.key, obj.value, obj.status, tx); - } - } - catch(...) - { - tx->abort(); - if(_txTrace >= 1) - { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "rolled back transaction " << hex << txnId << dec; - } - throw; - } - tx->commit(0); - - if(_txTrace >= 1) - { - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "committed transaction " << hex << txnId << dec; - } - - streamedObjectQueue.erase - (streamedObjectQueue.begin(), - streamedObjectQueue.begin() + txSize); - - if(_trace >= 1) - { - Long now = IceUtil::Time::now().toMilliSeconds(); - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "saved " << txSize << " objects in " - << static_cast<Int>(now - saveStart) << " ms"; - } - } - catch(const DbDeadlockException&) - { - if(_deadlockWarning) - { - Warning out(_communicator->getLogger()); - out << "Deadlock in Freeze::EvictorI::run while writing into Db \"" + _filename - + "\"; retrying ..."; - } - - tryAgain = true; - txSize = (txSize + 1)/2; - } - catch(const DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - } - } - while(tryAgain); - - { - Lock sync(*this); - - // - // Release usage count - // - for(deque<EvictorElementPtr>::iterator p = allObjects.begin(); - p != allObjects.end(); p++) - { - EvictorElementPtr& element = *p; - element->usageCount--; - } - allObjects.clear(); - - for(deque<EvictorElementPtr>::iterator q = deadObjects.begin(); - q != deadObjects.end(); q++) - { - EvictorElementPtr& element = *q; - if(!element->stale) - { - // - // Can be stale when there are duplicate elements on the - // deadObjecst queue - // - - if(!element->stale && element->usageCount == 0 && element->keepCount == 0) - { - // - // Get rid of unused dead elements - // - IceUtil::Mutex::Lock lockElement(element->mutex); - if(element->status == EvictorElement::dead) - { - evict(element); - } - } - } - } - deadObjects.clear(); - evict(); - - if(saveNowThreadsSize > 0) - { - _saveNowThreads.erase(_saveNowThreads.begin(), _saveNowThreads.begin() + saveNowThreadsSize); - notifyAll(); - } - } - } + switch(status) + { + case EvictorElement::created: + case EvictorElement::modified: + { + if(servant == element->rec.servant) + { + size_t index = streamedObjectQueue.size(); + streamedObjectQueue.resize(index + 1); + StreamedObject& obj = streamedObjectQueue[index]; + stream(element, streamStart, obj); + + element->status = EvictorElement::clean; + } + else + { + tryAgain = true; + } + break; + } + case EvictorElement::destroyed: + { + lockServant.release(); + + size_t index = streamedObjectQueue.size(); + streamedObjectQueue.resize(index + 1); + StreamedObject& obj = streamedObjectQueue[index]; + stream(element, streamStart, obj); + + element->status = EvictorElement::dead; + deadObjects.push_back(element); + break; + } + case EvictorElement::dead: + { + deadObjects.push_back(element); + break; + } + default: + { + // + // Nothing to do (could be a duplicate) + // + break; + } + } + } + else + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = string(typeid(*element->rec.servant).name()) + + " does not implement IceUtil::AbstractMutex"; + throw ex; + } + } + } while(tryAgain); + } + + if(_trace >= 1) + { + Long now = IceUtil::Time::now().toMilliSeconds(); + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "streamed " << streamedObjectQueue.size() << " objects in " + << static_cast<Int>(now - streamStart) << " ms"; + } + + // + // Now let's save all these streamed objects to disk using a transaction + // + + // + // Each time we get a deadlock, we reduce the number of objects to save + // per transaction + // + size_t txSize = streamedObjectQueue.size(); + if(txSize > static_cast<size_t>(_maxTxSize)) + { + txSize = static_cast<size_t>(_maxTxSize); + } + bool tryAgain; + + do + { + tryAgain = false; + + while(streamedObjectQueue.size() > 0) + { + if(txSize > streamedObjectQueue.size()) + { + txSize = streamedObjectQueue.size(); + } + + Long saveStart = IceUtil::Time::now().toMilliSeconds(); + try + { + DbTxn* tx = 0; + _dbEnv->getEnv()->txn_begin(0, &tx, 0); + + long txnId = 0; + if(_txTrace >= 1) + { + txnId = (tx->id() & 0x7FFFFFFF) + 0x80000000L; + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "started transaction " << hex << txnId << dec << " in saving thread"; + } + + try + { + for(size_t i = 0; i < txSize; i++) + { + StreamedObject& obj = streamedObjectQueue[i]; + obj.store->save(obj.key, obj.value, obj.status, tx); + } + } + catch(...) + { + tx->abort(); + if(_txTrace >= 1) + { + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "rolled back transaction " << hex << txnId << dec; + } + throw; + } + tx->commit(0); + + if(_txTrace >= 1) + { + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "committed transaction " << hex << txnId << dec; + } + + streamedObjectQueue.erase + (streamedObjectQueue.begin(), + streamedObjectQueue.begin() + txSize); + + if(_trace >= 1) + { + Long now = IceUtil::Time::now().toMilliSeconds(); + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "saved " << txSize << " objects in " + << static_cast<Int>(now - saveStart) << " ms"; + } + } + catch(const DbDeadlockException&) + { + if(_deadlockWarning) + { + Warning out(_communicator->getLogger()); + out << "Deadlock in Freeze::EvictorI::run while writing into Db \"" + _filename + + "\"; retrying ..."; + } + + tryAgain = true; + txSize = (txSize + 1)/2; + } + catch(const DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + } + } + while(tryAgain); + + { + Lock sync(*this); + + // + // Release usage count + // + for(deque<EvictorElementPtr>::iterator p = allObjects.begin(); + p != allObjects.end(); p++) + { + EvictorElementPtr& element = *p; + element->usageCount--; + } + allObjects.clear(); + + for(deque<EvictorElementPtr>::iterator q = deadObjects.begin(); + q != deadObjects.end(); q++) + { + EvictorElementPtr& element = *q; + if(!element->stale) + { + // + // Can be stale when there are duplicate elements on the + // deadObjecst queue + // + + if(!element->stale && element->usageCount == 0 && element->keepCount == 0) + { + // + // Get rid of unused dead elements + // + IceUtil::Mutex::Lock lockElement(element->mutex); + if(element->status == EvictorElement::dead) + { + evict(element); + } + } + } + } + deadObjects.clear(); + evict(); + + if(saveNowThreadsSize > 0) + { + _saveNowThreads.erase(_saveNowThreads.begin(), _saveNowThreads.begin() + saveNowThreadsSize); + notifyAll(); + } + } + } } catch(const IceUtil::Exception& ex) { - Error out(_communicator->getLogger()); - out << "Saving thread killed by exception: " << ex; - out.flush(); - handleFatalError(this, _communicator); + Error out(_communicator->getLogger()); + out << "Saving thread killed by exception: " << ex; + out.flush(); + handleFatalError(this, _communicator); } catch(const std::exception& ex) { - Error out(_communicator->getLogger()); - out << "Saving thread killed by std::exception: " << ex.what(); - out.flush(); - handleFatalError(this, _communicator); + Error out(_communicator->getLogger()); + out << "Saving thread killed by std::exception: " << ex.what(); + out.flush(); + handleFatalError(this, _communicator); } catch(...) { - Error out(_communicator->getLogger()); - out << "Saving thread killed by unknown exception"; - out.flush(); - handleFatalError(this, _communicator); + Error out(_communicator->getLogger()); + out << "Saving thread killed by unknown exception"; + out.flush(); + handleFatalError(this, _communicator); } } @@ -1665,7 +1665,7 @@ Freeze::EvictorI::saveNowNoSync() notifyAll(); do { - wait(); + wait(); } while(find(_saveNowThreads.begin(), _saveNowThreads.end(), myself) != _saveNowThreads.end()); } @@ -1683,50 +1683,50 @@ Freeze::EvictorI::evict() while(_currentEvictorSize > _evictorSize) { - // - // Get the last unused element from the evictor queue. - // - while(p != _evictorList.rend()) - { - if((*p)->usageCount == 0) - { - break; // Fine, servant is not in use (and not in the modifiedQueue) - } - ++p; - } - if(p == _evictorList.rend()) - { - // - // All servants are active, can't evict any further. - // - break; - } - - EvictorElementPtr& element = *p; - assert(!element->stale); - assert(element->keepCount == 0); - - if(_trace >= 2 || (_trace >= 1 && _evictorList.size() % 50 == 0)) - { - string facet = element->store.facet(); - - Trace out(_communicator->getLogger(), "Freeze.Evictor"); - out << "evicting \"" << _communicator->identityToString(element->cachePosition->first) << "\" "; - if(facet != "") - { - out << "-f \"" << facet << "\" "; - } - out << "from the queue\n" - << "number of elements in the queue: " << _currentEvictorSize; - } - - // - // Remove last unused element from the evictor queue. - // - element->stale = true; - element->store.unpin(element->cachePosition); - p = list<EvictorElementPtr>::reverse_iterator(_evictorList.erase(element->evictPosition)); - _currentEvictorSize--; + // + // Get the last unused element from the evictor queue. + // + while(p != _evictorList.rend()) + { + if((*p)->usageCount == 0) + { + break; // Fine, servant is not in use (and not in the modifiedQueue) + } + ++p; + } + if(p == _evictorList.rend()) + { + // + // All servants are active, can't evict any further. + // + break; + } + + EvictorElementPtr& element = *p; + assert(!element->stale); + assert(element->keepCount == 0); + + if(_trace >= 2 || (_trace >= 1 && _evictorList.size() % 50 == 0)) + { + string facet = element->store.facet(); + + Trace out(_communicator->getLogger(), "Freeze.Evictor"); + out << "evicting \"" << _communicator->identityToString(element->cachePosition->first) << "\" "; + if(facet != "") + { + out << "-f \"" << facet << "\" "; + } + out << "from the queue\n" + << "number of elements in the queue: " << _currentEvictorSize; + } + + // + // Remove last unused element from the evictor queue. + // + element->stale = true; + element->store.unpin(element->cachePosition); + p = list<EvictorElementPtr>::reverse_iterator(_evictorList.erase(element->evictPosition)); + _currentEvictorSize--; } } @@ -1737,20 +1737,20 @@ Freeze::EvictorI::fixEvictPosition(const EvictorElementPtr& element) if(element->keepCount == 0) { - if(element->usageCount < 0) - { - // - // New object - // - element->usageCount = 0; - _currentEvictorSize++; - } - else - { - _evictorList.erase(element->evictPosition); - } - _evictorList.push_front(element); - element->evictPosition = _evictorList.begin(); + if(element->usageCount < 0) + { + // + // New object + // + element->usageCount = 0; + _currentEvictorSize++; + } + else + { + _evictorList.erase(element->evictPosition); + } + _evictorList.push_front(element); + element->evictPosition = _evictorList.begin(); } } @@ -1775,7 +1775,7 @@ Freeze::EvictorI::addToModifiedQueue(const EvictorElementPtr& element) if(_saveSizeTrigger >= 0 && static_cast<Int>(_modifiedQueue.size()) >= _saveSizeTrigger) { - notifyAll(); + notifyAll(); } } @@ -1793,22 +1793,22 @@ Freeze::EvictorI::stream(const EvictorElementPtr& element, Long streamStart, Str if(element->status != EvictorElement::destroyed) { - // - // Update stats first - // - Statistics& stats = element->rec.stats; - Long diff = streamStart - (stats.creationTime + stats.lastSaveTime); - if(stats.lastSaveTime == 0) - { - stats.lastSaveTime = diff; - stats.avgSaveTime = diff; - } - else - { - stats.lastSaveTime = streamStart - stats.creationTime; - stats.avgSaveTime = static_cast<Long>(stats.avgSaveTime * 0.95 + diff * 0.05); - } - ObjectStore::marshal(element->rec, obj.value, _communicator); + // + // Update stats first + // + Statistics& stats = element->rec.stats; + Long diff = streamStart - (stats.creationTime + stats.lastSaveTime); + if(stats.lastSaveTime == 0) + { + stats.lastSaveTime = diff; + stats.avgSaveTime = diff; + } + else + { + stats.lastSaveTime = streamStart - stats.creationTime; + stats.avgSaveTime = static_cast<Long>(stats.avgSaveTime * 0.95 + diff * 0.05); + } + ObjectStore::marshal(element->rec, obj.value, _communicator); } } @@ -1820,11 +1820,11 @@ Freeze::EvictorI::findStore(const string& facet) const StoreMap::const_iterator p = _storeMap.find(facet); if(p == _storeMap.end()) { - return 0; + return 0; } else { - return (*p).second; + return (*p).second; } } @@ -1836,45 +1836,45 @@ Freeze::EvictorI::allDbs() const try { - Db db(_dbEnv->getEnv(), 0); - db.open(0, _filename.c_str(), 0, DB_UNKNOWN, DB_RDONLY, 0); - - Dbc* dbc = 0; - db.cursor(0, &dbc, 0); - - Dbt dbKey; - dbKey.set_flags(DB_DBT_MALLOC); - - Dbt dbValue; - dbValue.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL); - - bool more = true; - while(more) - { - more = (dbc->get(&dbKey, &dbValue, DB_NEXT) == 0); - if(more) - { - string dbName(static_cast<char*>(dbKey.get_data()), dbKey.get_size()); - - if(dbName.find(indexPrefix) != 0) - { - result.push_back(dbName); - } - free(dbKey.get_data()); - } - } - - dbc->close(); - db.close(0); + Db db(_dbEnv->getEnv(), 0); + db.open(0, _filename.c_str(), 0, DB_UNKNOWN, DB_RDONLY, 0); + + Dbc* dbc = 0; + db.cursor(0, &dbc, 0); + + Dbt dbKey; + dbKey.set_flags(DB_DBT_MALLOC); + + Dbt dbValue; + dbValue.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL); + + bool more = true; + while(more) + { + more = (dbc->get(&dbKey, &dbValue, DB_NEXT) == 0); + if(more) + { + string dbName(static_cast<char*>(dbKey.get_data()), dbKey.get_size()); + + if(dbName.find(indexPrefix) != 0) + { + result.push_back(dbName); + } + free(dbKey.get_data()); + } + } + + dbc->close(); + db.close(0); } catch(const DbException& dx) { - if(dx.get_errno() != ENOENT) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + if(dx.get_errno() != ENOENT) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } return result; diff --git a/cpp/src/Freeze/EvictorI.h b/cpp/src/Freeze/EvictorI.h index 6c85a3092df..187294cb8c6 100644 --- a/cpp/src/Freeze/EvictorI.h +++ b/cpp/src/Freeze/EvictorI.h @@ -43,11 +43,11 @@ public: class Guard { public: - Guard(DeactivateController&); - ~Guard(); + Guard(DeactivateController&); + ~Guard(); private: - DeactivateController& _controller; + DeactivateController& _controller; }; DeactivateController(EvictorI*); @@ -112,7 +112,7 @@ class EvictorI : public Evictor, public IceUtil::Monitor<IceUtil::Mutex>, publi public: EvictorI(const Ice::ObjectAdapterPtr&, const std::string&, DbEnv*, const std::string&, - const ServantInitializerPtr&, const std::vector<IndexPtr>&, bool); + const ServantInitializerPtr&, const std::vector<IndexPtr>&, bool); virtual ~EvictorI(); @@ -165,10 +165,10 @@ public: struct StreamedObject { - Key key; - Value value; - Ice::Byte status; - ObjectStore* store; + Key key; + Value value; + Ice::Byte status; + ObjectStore* store; }; diff --git a/cpp/src/Freeze/EvictorIteratorI.cpp b/cpp/src/Freeze/EvictorIteratorI.cpp index d2a1ecef2f5..68feab953aa 100644 --- a/cpp/src/Freeze/EvictorIteratorI.cpp +++ b/cpp/src/Freeze/EvictorIteratorI.cpp @@ -33,12 +33,12 @@ Freeze::EvictorIteratorI::hasNext() { if(_batchIterator != _batch.end()) { - return true; + return true; } else { - _batchIterator = nextBatch(); - return (_batchIterator != _batch.end()); + _batchIterator = nextBatch(); + return (_batchIterator != _batch.end()); } } @@ -47,11 +47,11 @@ Freeze::EvictorIteratorI::next() { if(hasNext()) { - return *_batchIterator++; + return *_batchIterator++; } else { - throw Freeze::NoSuchElementException(__FILE__, __LINE__); + throw Freeze::NoSuchElementException(__FILE__, __LINE__); } } @@ -60,13 +60,13 @@ vector<Identity>::const_iterator Freeze::EvictorIteratorI::nextBatch() { DeactivateController::Guard - deactivateGuard(_store->evictor()->deactivateController()); + deactivateGuard(_store->evictor()->deactivateController()); _batch.clear(); if(!_more) { - return _batch.end(); + return _batch.end(); } vector<EvictorElementPtr> evictorElements; @@ -78,145 +78,145 @@ Freeze::EvictorIteratorI::nextBatch() try { - for(;;) - { - _batch.clear(); - evictorElements.clear(); - - Dbt dbKey; - initializeOutDbt(_key, dbKey); - - Dbt dbValue; - dbValue.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL); - - Dbc* dbc = 0; - try - { - // - // Move to the first record - // - u_int32_t flags = DB_NEXT; - - if(_initialized) - { - // - // _key represents the next element not yet returned - // if it has been deleted, we want the one after - // - flags = DB_SET_RANGE; - - // - // Will be used as input as well - // - dbKey.set_size(static_cast<u_int32_t>(firstKey.size())); - } - - _store->db()->cursor(0, &dbc, 0); - - bool done = false; - do - { - for(;;) - { - try - { - // - // It is critical to set key size to key capacity before the - // get, as a resize that increases the size inserts 0 - // - _key.resize(_key.capacity()); - - _more = (dbc->get(&dbKey, &dbValue, flags) == 0); - if(_more) - { - _key.resize(dbKey.get_size()); - _initialized = true; - - flags = DB_NEXT; - - Ice::Identity ident; - ObjectStore::unmarshal(ident, _key, communicator); - if(_batch.size() < _batchSize) - { - _batch.push_back(ident); - } - else - { - // - // Keep the last element in _key - // - done = true; - } - } - break; - } - catch(const DbDeadlockException&) - { - throw; - } - catch(const DbException& dx) - { - handleDbException(dx, _key, dbKey, __FILE__, __LINE__); - } - } - } - while(!done && _more); - - Dbc* toClose = dbc; - dbc = 0; - toClose->close(); - break; // for (;;) - } - catch(const DbDeadlockException&) - { - if(dbc != 0) - { - try - { - dbc->close(); - } - catch(const DbDeadlockException&) - { - // - // Ignored - // - } - } - _key = firstKey; - // - // Retry - // - } - catch(...) - { - if(dbc != 0) - { - try - { - dbc->close(); - } - catch(const DbDeadlockException&) - { - // - // Ignored - // - } - } - throw; - } - } + for(;;) + { + _batch.clear(); + evictorElements.clear(); + + Dbt dbKey; + initializeOutDbt(_key, dbKey); + + Dbt dbValue; + dbValue.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL); + + Dbc* dbc = 0; + try + { + // + // Move to the first record + // + u_int32_t flags = DB_NEXT; + + if(_initialized) + { + // + // _key represents the next element not yet returned + // if it has been deleted, we want the one after + // + flags = DB_SET_RANGE; + + // + // Will be used as input as well + // + dbKey.set_size(static_cast<u_int32_t>(firstKey.size())); + } + + _store->db()->cursor(0, &dbc, 0); + + bool done = false; + do + { + for(;;) + { + try + { + // + // It is critical to set key size to key capacity before the + // get, as a resize that increases the size inserts 0 + // + _key.resize(_key.capacity()); + + _more = (dbc->get(&dbKey, &dbValue, flags) == 0); + if(_more) + { + _key.resize(dbKey.get_size()); + _initialized = true; + + flags = DB_NEXT; + + Ice::Identity ident; + ObjectStore::unmarshal(ident, _key, communicator); + if(_batch.size() < _batchSize) + { + _batch.push_back(ident); + } + else + { + // + // Keep the last element in _key + // + done = true; + } + } + break; + } + catch(const DbDeadlockException&) + { + throw; + } + catch(const DbException& dx) + { + handleDbException(dx, _key, dbKey, __FILE__, __LINE__); + } + } + } + while(!done && _more); + + Dbc* toClose = dbc; + dbc = 0; + toClose->close(); + break; // for (;;) + } + catch(const DbDeadlockException&) + { + if(dbc != 0) + { + try + { + dbc->close(); + } + catch(const DbDeadlockException&) + { + // + // Ignored + // + } + } + _key = firstKey; + // + // Retry + // + } + catch(...) + { + if(dbc != 0) + { + try + { + dbc->close(); + } + catch(const DbDeadlockException&) + { + // + // Ignored + // + } + } + throw; + } + } } catch(const DbException& dx) { - handleDbException(dx, __FILE__, __LINE__); + handleDbException(dx, __FILE__, __LINE__); } if(_batch.size() == 0) { - return _batch.end(); + return _batch.end(); } else { - return _batch.begin(); + return _batch.begin(); } } diff --git a/cpp/src/Freeze/IndexI.cpp b/cpp/src/Freeze/IndexI.cpp index 5058fb23ece..ca9c21e095a 100644 --- a/cpp/src/Freeze/IndexI.cpp +++ b/cpp/src/Freeze/IndexI.cpp @@ -37,7 +37,7 @@ vector<Identity> Freeze::IndexI::untypedFindFirst(const Key& bytes, Int firstN) const { DeactivateController::Guard - deactivateGuard(_store->evictor()->deactivateController()); + deactivateGuard(_store->evictor()->deactivateController()); Dbt dbKey; initializeInDbt(bytes, dbKey); @@ -63,111 +63,111 @@ Freeze::IndexI::untypedFindFirst(const Key& bytes, Int firstN) const try { - for(;;) - { - Dbc* dbc = 0; - identities.clear(); - - try - { - // - // Move to the first record - // - _db->cursor(0, &dbc, 0); - u_int32_t flags = DB_SET; - - bool found; - - do - { - for(;;) - { - try - { - // - // It is critical to set key size to key capacity before the - // get, as a resize that increases the size inserts 0 - // - pkey.resize(pkey.capacity()); - - found = (dbc->pget(&dbKey, &pdbKey, &dbValue, flags) == 0); - if(found) - { - pkey.resize(pdbKey.get_size()); - - Ice::Identity ident; - ObjectStore::unmarshal(ident, pkey, communicator); - identities.push_back(ident); - flags = DB_NEXT_DUP; - } - break; // for(;;) - } - catch(const DbDeadlockException&) - { - throw; - } - catch(const DbException& dx) - { - handleDbException(dx, pkey, pdbKey, __FILE__, __LINE__); - } - } - } - while((firstN <= 0 || identities.size() < static_cast<size_t>(firstN)) && found); - - Dbc* toClose = dbc; - dbc = 0; - toClose->close(); - break; // for (;;) - } - catch(const DbDeadlockException&) - { - if(dbc != 0) - { - try - { - dbc->close(); - } - catch(const DbDeadlockException&) - { - // - // Ignored - // - } - } - - if(_store->evictor()->deadlockWarning()) - { - Warning out(_store->communicator()->getLogger()); - out << "Deadlock in Freeze::IndexI::untypedFindFirst while searching \"" - << _store->evictor()->filename() + "/" + _dbName << "\"; retrying ..."; - } - - // - // Retry - // - } - catch(...) - { - if(dbc != 0) - { - try - { - dbc->close(); - } - catch(const DbDeadlockException&) - { - // - // Ignored - // - } - } - throw; - } - } + for(;;) + { + Dbc* dbc = 0; + identities.clear(); + + try + { + // + // Move to the first record + // + _db->cursor(0, &dbc, 0); + u_int32_t flags = DB_SET; + + bool found; + + do + { + for(;;) + { + try + { + // + // It is critical to set key size to key capacity before the + // get, as a resize that increases the size inserts 0 + // + pkey.resize(pkey.capacity()); + + found = (dbc->pget(&dbKey, &pdbKey, &dbValue, flags) == 0); + if(found) + { + pkey.resize(pdbKey.get_size()); + + Ice::Identity ident; + ObjectStore::unmarshal(ident, pkey, communicator); + identities.push_back(ident); + flags = DB_NEXT_DUP; + } + break; // for(;;) + } + catch(const DbDeadlockException&) + { + throw; + } + catch(const DbException& dx) + { + handleDbException(dx, pkey, pdbKey, __FILE__, __LINE__); + } + } + } + while((firstN <= 0 || identities.size() < static_cast<size_t>(firstN)) && found); + + Dbc* toClose = dbc; + dbc = 0; + toClose->close(); + break; // for (;;) + } + catch(const DbDeadlockException&) + { + if(dbc != 0) + { + try + { + dbc->close(); + } + catch(const DbDeadlockException&) + { + // + // Ignored + // + } + } + + if(_store->evictor()->deadlockWarning()) + { + Warning out(_store->communicator()->getLogger()); + out << "Deadlock in Freeze::IndexI::untypedFindFirst while searching \"" + << _store->evictor()->filename() + "/" + _dbName << "\"; retrying ..."; + } + + // + // Retry + // + } + catch(...) + { + if(dbc != 0) + { + try + { + dbc->close(); + } + catch(const DbDeadlockException&) + { + // + // Ignored + // + } + } + throw; + } + } } catch(const DbException& dx) { - handleDbException(dx, __FILE__, __LINE__); + handleDbException(dx, __FILE__, __LINE__); } return identities; @@ -183,7 +183,7 @@ Int Freeze::IndexI::untypedCount(const Key& bytes) const { DeactivateController::Guard - deactivateGuard(_store->evictor()->deactivateController()); + deactivateGuard(_store->evictor()->deactivateController()); Dbt dbKey; initializeInDbt(bytes, dbKey); @@ -203,81 +203,81 @@ Freeze::IndexI::untypedCount(const Key& bytes) const try { - for(;;) - { - Dbc* dbc = 0; - - try - { - // - // Move to the first record - // - _db->cursor(0, &dbc, 0); - bool found = (dbc->get(&dbKey, &dbValue, DB_SET) == 0); - - if(found) - { - db_recno_t count = 0; - dbc->count(&count, 0); - result = static_cast<Int>(count); - } - - Dbc* toClose = dbc; - dbc = 0; - toClose->close(); - break; // for (;;) - } - catch(const DbDeadlockException&) - { - if(dbc != 0) - { - try - { - dbc->close(); - } - catch(const DbDeadlockException&) - { - // - // Ignored - // - } - } - - if(_store->evictor()->deadlockWarning()) - { - Warning out(_store->communicator()->getLogger()); - out << "Deadlock in Freeze::IndexI::untypedCount while searching \"" - << _store->evictor()->filename() + "/" + _dbName << "\"; retrying ..."; - } - - // - // Retry - // - } - catch(...) - { - if(dbc != 0) - { - try - { - dbc->close(); - } - catch(const DbDeadlockException&) - { - // - // Ignored - // - } - } - throw; - } - } + for(;;) + { + Dbc* dbc = 0; + + try + { + // + // Move to the first record + // + _db->cursor(0, &dbc, 0); + bool found = (dbc->get(&dbKey, &dbValue, DB_SET) == 0); + + if(found) + { + db_recno_t count = 0; + dbc->count(&count, 0); + result = static_cast<Int>(count); + } + + Dbc* toClose = dbc; + dbc = 0; + toClose->close(); + break; // for (;;) + } + catch(const DbDeadlockException&) + { + if(dbc != 0) + { + try + { + dbc->close(); + } + catch(const DbDeadlockException&) + { + // + // Ignored + // + } + } + + if(_store->evictor()->deadlockWarning()) + { + Warning out(_store->communicator()->getLogger()); + out << "Deadlock in Freeze::IndexI::untypedCount while searching \"" + << _store->evictor()->filename() + "/" + _dbName << "\"; retrying ..."; + } + + // + // Retry + // + } + catch(...) + { + if(dbc != 0) + { + try + { + dbc->close(); + } + catch(const DbDeadlockException&) + { + // + // Ignored + // + } + } + throw; + } + } } catch(const DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } return result; @@ -285,7 +285,7 @@ Freeze::IndexI::untypedCount(const Key& bytes) const void Freeze::IndexI::associate(ObjectStore* store, DbTxn* txn, - bool createDb, bool populateIndex) + bool createDb, bool populateIndex) { assert(txn != 0); _store = store; @@ -298,7 +298,7 @@ Freeze::IndexI::associate(ObjectStore* store, DbTxn* txn, u_int32_t flags = 0; if(createDb) { - flags = DB_CREATE; + flags = DB_CREATE; } _dbName = EvictorI::indexPrefix + store->dbName() + "." + _index.name(); @@ -308,14 +308,14 @@ Freeze::IndexI::associate(ObjectStore* store, DbTxn* txn, flags = 0; if(populateIndex) { - flags = DB_CREATE; + flags = DB_CREATE; } store->db()->associate(txn, _db.get(), callback, flags); } int Freeze::IndexI::secondaryKeyCreate(Db* secondary, const Dbt* dbKey, - const Dbt* dbValue, Dbt* result) + const Dbt* dbValue, Dbt* result) { Ice::CommunicatorPtr communicator = _store->communicator(); @@ -327,19 +327,19 @@ Freeze::IndexI::secondaryKeyCreate(Db* secondary, const Dbt* dbKey, Key bytes; if(_index.marshalKey(rec.servant, bytes)) { - result->set_flags(DB_DBT_APPMALLOC); - void* data = malloc(bytes.size()); - memcpy(data, &bytes[0], bytes.size()); - result->set_data(data); - result->set_size(static_cast<u_int32_t>(bytes.size())); - return 0; + result->set_flags(DB_DBT_APPMALLOC); + void* data = malloc(bytes.size()); + memcpy(data, &bytes[0], bytes.size()); + result->set_data(data); + result->set_size(static_cast<u_int32_t>(bytes.size())); + return 0; } else { - // - // Don't want to index this one - // - return DB_DONOTINDEX; + // + // Don't want to index this one + // + return DB_DONOTINDEX; } } @@ -348,16 +348,16 @@ Freeze::IndexI::close() { if(_db.get() != 0) { - try - { - _db->close(0); - } - catch(const DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - _db.reset(0); + try + { + _db->close(0); + } + catch(const DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + _db.reset(0); } } diff --git a/cpp/src/Freeze/MapI.cpp b/cpp/src/Freeze/MapI.cpp index a3e756725d2..37c894dad0c 100644 --- a/cpp/src/Freeze/MapI.cpp +++ b/cpp/src/Freeze/MapI.cpp @@ -86,12 +86,12 @@ Freeze::KeyCompareBase::compareEnabled() const Freeze::MapHelper* Freeze::MapHelper::create(const Freeze::ConnectionPtr& connection, - const string& dbName, - const string& key, - const string& value, - const Freeze::KeyCompareBasePtr& keyCompare, - const std::vector<MapIndexBasePtr>& indices, - bool createDb) + const string& dbName, + const string& key, + const string& value, + const Freeze::KeyCompareBasePtr& keyCompare, + const std::vector<MapIndexBasePtr>& indices, + bool createDb) { Freeze::ConnectionIPtr connectionI = Freeze::ConnectionIPtr::dynamicCast(connection.get()); return new MapHelperI(connectionI, dbName, key, value, keyCompare, indices, createDb); @@ -112,14 +112,14 @@ Freeze::IteratorHelper::create(const MapHelper& m, bool readOnly) const MapHelperI& actualMap = dynamic_cast<const MapHelperI&>(m); auto_ptr<IteratorHelperI> r(new IteratorHelperI(actualMap, readOnly, - 0, false)); + 0, false)); if(r->next()) { - return r.release(); + return r.release(); } else { - return 0; + return 0; } } @@ -134,8 +134,8 @@ Freeze::IteratorHelper::~IteratorHelper() // Freeze::IteratorHelperI::IteratorHelperI(const MapHelperI& m, bool readOnly, - const MapIndexBasePtr& index, - bool onlyDups) : + const MapIndexBasePtr& index, + bool onlyDups) : _map(m), _dbc(0), _indexed(index != 0), @@ -144,41 +144,41 @@ Freeze::IteratorHelperI::IteratorHelperI(const MapHelperI& m, bool readOnly, { if(_map._trace >= 2) { - Trace out(_map._connection->communicator()->getLogger(), "Freeze.Map"); - out << "opening iterator on Db \"" << _map._dbName << "\""; - if(index != 0) - { - out << " with index \"" << index->name() << "\""; - } + Trace out(_map._connection->communicator()->getLogger(), "Freeze.Map"); + out << "opening iterator on Db \"" << _map._dbName << "\""; + if(index != 0) + { + out << " with index \"" << index->name() << "\""; + } } DbTxn* txn = _map._connection->dbTxn(); if(txn == 0 && !readOnly) { - // - // Need to start a transaction - // - _tx = new Tx(_map); - txn = _tx->getTxn(); + // + // Need to start a transaction + // + _tx = new Tx(_map); + txn = _tx->getTxn(); } try { - if(index != 0) - { - index->_impl->db()->cursor(txn, &_dbc, 0); - } - else - { - _map._db->cursor(txn, &_dbc, 0); - } + if(index != 0) + { + index->_impl->db()->cursor(txn, &_dbc, 0); + } + else + { + _map._db->cursor(txn, &_dbc, 0); + } } catch(const ::DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } _map._iteratorList.push_back(this); @@ -194,19 +194,19 @@ Freeze::IteratorHelperI::IteratorHelperI(const IteratorHelperI& it) : { if(_map._trace >= 2) { - Trace out(_map._connection->communicator()->getLogger(), "Freeze.Map"); - out << "duplicating iterator on Db \"" << _map._dbName << "\""; + Trace out(_map._connection->communicator()->getLogger(), "Freeze.Map"); + out << "duplicating iterator on Db \"" << _map._dbName << "\""; } try { - it._dbc->dup(&_dbc, DB_POSITION); + it._dbc->dup(&_dbc, DB_POSITION); } catch(const ::DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } _tx = it._tx; @@ -241,31 +241,31 @@ Freeze::IteratorHelperI::find(const Key& key) const { try { - if(_dbc->get(&dbKey, &dbValue, DB_SET) == 0) - { - return true; - } - else - { - return false; - } + if(_dbc->get(&dbKey, &dbValue, DB_SET) == 0) + { + return true; + } + else + { + return false; + } } catch(const ::DbDeadlockException& dx) { - if(_tx != 0) - { - _tx->dead(); - } - - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + if(_tx != 0) + { + _tx->dead(); + } + + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } catch(const ::DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } } } @@ -289,42 +289,42 @@ Freeze::IteratorHelperI::lowerBound(const Key& key) const for(;;) { - try - { - int err = _dbc->get(&dbKey, &dbValue, DB_SET_RANGE); - if(err == 0) - { - _key.resize(dbKey.get_size()); - return true; - } - else if(err == DB_NOTFOUND) - { - return false; - } - else - { - // - // Bug in Freeze - // - assert(0); - throw DatabaseException(__FILE__, __LINE__); - } - } - catch(const ::DbDeadlockException& dx) - { - if(_tx != 0) - { - _tx->dead(); - } - - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - catch(const ::DbException& dx) - { - handleDbException(dx, _key, dbKey, __FILE__, __LINE__); - } + try + { + int err = _dbc->get(&dbKey, &dbValue, DB_SET_RANGE); + if(err == 0) + { + _key.resize(dbKey.get_size()); + return true; + } + else if(err == DB_NOTFOUND) + { + return false; + } + else + { + // + // Bug in Freeze + // + assert(0); + throw DatabaseException(__FILE__, __LINE__); + } + } + catch(const ::DbDeadlockException& dx) + { + if(_tx != 0) + { + _tx->dead(); + } + + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + catch(const ::DbException& dx) + { + handleDbException(dx, _key, dbKey, __FILE__, __LINE__); + } } } @@ -333,18 +333,18 @@ Freeze::IteratorHelperI::upperBound(const Key& key) const { if(lowerBound(key)) { - if(_key == key) - { - return next(true); - } - else - { - return true; - } + if(_key == key) + { + return next(true); + } + else + { + return true; + } } else { - return false; + return false; } } @@ -364,7 +364,7 @@ Freeze::IteratorHelperI::get(const Key*& key, const Value*& value) const size_t keySize = _key.capacity(); if(keySize < 1024) { - keySize = 1024; + keySize = 1024; } _key.resize(keySize); @@ -374,7 +374,7 @@ Freeze::IteratorHelperI::get(const Key*& key, const Value*& value) const size_t valueSize = _value.capacity(); if(valueSize < 1024) { - valueSize = 1024; + valueSize = 1024; } _value.resize(valueSize); Dbt dbValue; @@ -382,60 +382,60 @@ Freeze::IteratorHelperI::get(const Key*& key, const Value*& value) const for(;;) { - try - { - int err; - - if(_indexed) - { - // - // Not interested in getting the index's key - // - Dbt iKey; - iKey.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL); - - err = _dbc->pget(&iKey, &dbKey, &dbValue, DB_CURRENT); - } - else - { - err = _dbc->get(&dbKey, &dbValue, DB_CURRENT); - } - - if(err == 0) - { - _key.resize(dbKey.get_size()); - _value.resize(dbValue.get_size()); - break; // for(;;) - } - else if(err == DB_KEYEMPTY) - { - throw InvalidPositionException(__FILE__, __LINE__); - } - else - { - // - // Bug in Freeze - // - assert(0); - throw DatabaseException(__FILE__, __LINE__); - } - } - catch(const ::DbDeadlockException& dx) - { - if(_tx != 0) - { - _tx->dead(); - } - - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - catch(const ::DbException& dx) - { - handleDbException(dx, _key, dbKey, _value, dbValue, - __FILE__, __LINE__); - } + try + { + int err; + + if(_indexed) + { + // + // Not interested in getting the index's key + // + Dbt iKey; + iKey.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL); + + err = _dbc->pget(&iKey, &dbKey, &dbValue, DB_CURRENT); + } + else + { + err = _dbc->get(&dbKey, &dbValue, DB_CURRENT); + } + + if(err == 0) + { + _key.resize(dbKey.get_size()); + _value.resize(dbValue.get_size()); + break; // for(;;) + } + else if(err == DB_KEYEMPTY) + { + throw InvalidPositionException(__FILE__, __LINE__); + } + else + { + // + // Bug in Freeze + // + assert(0); + throw DatabaseException(__FILE__, __LINE__); + } + } + catch(const ::DbDeadlockException& dx) + { + if(_tx != 0) + { + _tx->dead(); + } + + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + catch(const ::DbException& dx) + { + handleDbException(dx, _key, dbKey, _value, dbValue, + __FILE__, __LINE__); + } } } @@ -445,7 +445,7 @@ Freeze::IteratorHelperI::get() const size_t keySize = _key.capacity(); if(keySize < 1024) { - keySize = 1024; + keySize = 1024; } _key.resize(keySize); @@ -460,57 +460,57 @@ Freeze::IteratorHelperI::get() const for(;;) { - try - { - int err; - if(_indexed) - { - // - // Not interested in getting the index's key - // - Dbt iKey; - iKey.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL); - - err = _dbc->pget(&iKey, &dbKey, &dbValue, DB_CURRENT); - } - else - { - err = _dbc->get(&dbKey, &dbValue, DB_CURRENT); - } - - if(err == 0) - { - _key.resize(dbKey.get_size()); - return &_key; - } - else if(err == DB_KEYEMPTY) - { - throw InvalidPositionException(__FILE__, __LINE__); - } - else - { - // - // Bug in Freeze - // - assert(0); - throw DatabaseException(__FILE__, __LINE__); - } - } - catch(const ::DbDeadlockException& dx) - { - if(_tx != 0) - { - _tx->dead(); - } - - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - catch(const ::DbException& dx) - { - handleDbException(dx, _key, dbKey, __FILE__, __LINE__); - } + try + { + int err; + if(_indexed) + { + // + // Not interested in getting the index's key + // + Dbt iKey; + iKey.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL); + + err = _dbc->pget(&iKey, &dbKey, &dbValue, DB_CURRENT); + } + else + { + err = _dbc->get(&dbKey, &dbValue, DB_CURRENT); + } + + if(err == 0) + { + _key.resize(dbKey.get_size()); + return &_key; + } + else if(err == DB_KEYEMPTY) + { + throw InvalidPositionException(__FILE__, __LINE__); + } + else + { + // + // Bug in Freeze + // + assert(0); + throw DatabaseException(__FILE__, __LINE__); + } + } + catch(const ::DbDeadlockException& dx) + { + if(_tx != 0) + { + _tx->dead(); + } + + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + catch(const ::DbException& dx) + { + handleDbException(dx, _key, dbKey, __FILE__, __LINE__); + } } } @@ -519,9 +519,9 @@ Freeze::IteratorHelperI::set(const Value& value) { if(_indexed) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = "Cannot set an iterator retrieved through an index"; - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = "Cannot set an iterator retrieved through an index"; + throw ex; } // @@ -535,31 +535,31 @@ Freeze::IteratorHelperI::set(const Value& value) if(_tx != 0) { - _map.closeAllIteratorsExcept(_tx); + _map.closeAllIteratorsExcept(_tx); } try { - int err; - err = _dbc->put(&dbKey, &dbValue, DB_CURRENT); - assert(err == 0); + int err; + err = _dbc->put(&dbKey, &dbValue, DB_CURRENT); + assert(err == 0); } catch(const ::DbDeadlockException& dx) { - if(_tx != 0) - { - _tx->dead(); - } + if(_tx != 0) + { + _tx->dead(); + } - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } catch(const ::DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } } @@ -568,34 +568,34 @@ Freeze::IteratorHelperI::erase() { if(_tx != 0) { - _map.closeAllIteratorsExcept(_tx); + _map.closeAllIteratorsExcept(_tx); } try { - int err = _dbc->del(0); - if(err == DB_KEYEMPTY) - { - throw InvalidPositionException(__FILE__, __LINE__); - } - assert(err == 0); + int err = _dbc->del(0); + if(err == DB_KEYEMPTY) + { + throw InvalidPositionException(__FILE__, __LINE__); + } + assert(err == 0); } catch(const ::DbDeadlockException& dx) { - if(_tx != 0) - { - _tx->dead(); - } + if(_tx != 0) + { + _tx->dead(); + } - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } catch(const ::DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } } @@ -619,40 +619,40 @@ Freeze::IteratorHelperI::next(bool skipDups) const int flags = DB_NEXT; if(skipDups) { - flags = DB_NEXT_NODUP; + flags = DB_NEXT_NODUP; } else if(_indexed && _onlyDups) { - flags = DB_NEXT_DUP; + flags = DB_NEXT_DUP; } try { - if(_dbc->get(&dbKey, &dbValue, flags) == 0) - { - return true; - } - else - { - return false; - } + if(_dbc->get(&dbKey, &dbValue, flags) == 0) + { + return true; + } + else + { + return false; + } } catch(const ::DbDeadlockException& dx) { - if(_tx != 0) - { - _tx->dead(); - } + if(_tx != 0) + { + _tx->dead(); + } - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } catch(const ::DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } } @@ -661,35 +661,35 @@ Freeze::IteratorHelperI::close() { if(_dbc != 0) { - if(_map._trace >= 2) - { - Trace out(_map._connection->communicator()->getLogger(), "Freeze.Map"); - out << "closing iterator on Db \"" << _map._dbName << "\""; - } - - try - { - _dbc->close(); - } - catch(const ::DbDeadlockException& dx) - { - bool raiseException = (_tx == 0); - cleanup(); - if(raiseException) - { - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - } - catch(const ::DbException& dx) - { - cleanup(); - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - cleanup(); + if(_map._trace >= 2) + { + Trace out(_map._connection->communicator()->getLogger(), "Freeze.Map"); + out << "closing iterator on Db \"" << _map._dbName << "\""; + } + + try + { + _dbc->close(); + } + catch(const ::DbDeadlockException& dx) + { + bool raiseException = (_tx == 0); + cleanup(); + if(raiseException) + { + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + } + catch(const ::DbException& dx) + { + cleanup(); + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + cleanup(); } } @@ -713,68 +713,68 @@ Freeze::IteratorHelperI::Tx::Tx(const MapHelperI& m) : { if(_map._trace >= 2) { - Trace out(_map._connection->communicator()->getLogger(), "Freeze.Map"); - out << "starting transaction for Db \"" << _map._dbName << "\""; + Trace out(_map._connection->communicator()->getLogger(), "Freeze.Map"); + out << "starting transaction for Db \"" << _map._dbName << "\""; } try { - _map._connection->dbEnv()->getEnv()->txn_begin(0, &_txn, 0); + _map._connection->dbEnv()->getEnv()->txn_begin(0, &_txn, 0); } catch(const ::DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } } - + Freeze::IteratorHelperI::Tx::~Tx() { if(_dead) { - if(_map._trace >= 2) - { - Trace out(_map._connection->communicator()->getLogger(), ""); - out << "aborting transaction for Db \"" << _map._dbName << "\""; - } - - try - { - _txn->abort(); - } - catch(...) - { - // - // Ignore exceptions to avoid crash during stack unwinding - // - } + if(_map._trace >= 2) + { + Trace out(_map._connection->communicator()->getLogger(), ""); + out << "aborting transaction for Db \"" << _map._dbName << "\""; + } + + try + { + _txn->abort(); + } + catch(...) + { + // + // Ignore exceptions to avoid crash during stack unwinding + // + } } else { - if(_map._trace >= 2) - { - Trace out(_map._connection->communicator()->getLogger(), "Freeze.Map"); - out << "committing transaction for Db \"" << _map._dbName.c_str() << "\""; - } - - try - { - _txn->commit(0); - } - catch(const ::DbDeadlockException& dx) - { - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - catch(const ::DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + if(_map._trace >= 2) + { + Trace out(_map._connection->communicator()->getLogger(), "Freeze.Map"); + out << "committing transaction for Db \"" << _map._dbName.c_str() << "\""; + } + + try + { + _txn->commit(0); + } + catch(const ::DbDeadlockException& dx) + { + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + catch(const ::DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } } @@ -792,31 +792,31 @@ Freeze::IteratorHelperI::Tx::dead() Freeze::MapHelperI::MapHelperI(const ConnectionIPtr& connection, - const string& dbName, - const string& key, - const string& value, - const KeyCompareBasePtr& keyCompare, - const vector<MapIndexBasePtr>& indices, - bool createDb) : + const string& dbName, + const string& key, + const string& value, + const KeyCompareBasePtr& keyCompare, + const vector<MapIndexBasePtr>& indices, + bool createDb) : _connection(connection), _db(SharedDb::get(connection, dbName, key, value, keyCompare, indices, createDb)), _dbName(dbName), _trace(connection->trace()) { for(vector<MapIndexBasePtr>::const_iterator p = indices.begin(); - p != indices.end(); ++p) + p != indices.end(); ++p) { - const MapIndexBasePtr& indexBase = *p; - assert(indexBase->_impl != 0); - assert(indexBase->_communicator == _connection->communicator()); - assert(indexBase->_map == 0); - + const MapIndexBasePtr& indexBase = *p; + assert(indexBase->_impl != 0); + assert(indexBase->_communicator == _connection->communicator()); + assert(indexBase->_map == 0); + #ifndef NDEBUG - bool inserted = + bool inserted = #endif - _indices.insert(IndexMap::value_type(indexBase->name(), indexBase)).second; - assert(inserted); - indexBase->_map = this; + _indices.insert(IndexMap::value_type(indexBase->name(), indexBase)).second; + assert(inserted); + indexBase->_map = this; } _connection->registerMap(this); @@ -832,38 +832,38 @@ Freeze::MapHelperI::find(const Key& k, bool readOnly) const { for(;;) { - try - { - auto_ptr<IteratorHelperI> r(new IteratorHelperI(*this, readOnly, 0, false)); - if(r->find(k)) - { - return r.release(); - } - else - { - return 0; - } - } - catch(const DeadlockException&) - { - if(_connection->dbTxn() != 0) - { - throw; - } - else - { - if(_connection->deadlockWarning()) - { - Warning out(_connection->communicator()->getLogger()); - out << "Deadlock in Freeze::MapHelperI::find on Map \"" - << _dbName << "\"; retrying ..."; - } - - // - // Ignored, try again - // - } - } + try + { + auto_ptr<IteratorHelperI> r(new IteratorHelperI(*this, readOnly, 0, false)); + if(r->find(k)) + { + return r.release(); + } + else + { + return 0; + } + } + catch(const DeadlockException&) + { + if(_connection->dbTxn() != 0) + { + throw; + } + else + { + if(_connection->deadlockWarning()) + { + Warning out(_connection->communicator()->getLogger()); + out << "Deadlock in Freeze::MapHelperI::find on Map \"" + << _dbName << "\"; retrying ..."; + } + + // + // Ignored, try again + // + } + } } } @@ -872,38 +872,38 @@ Freeze::MapHelperI::lowerBound(const Key& k, bool readOnly) const { for(;;) { - try - { - auto_ptr<IteratorHelperI> r(new IteratorHelperI(*this, readOnly, 0, false)); - if(r->lowerBound(k)) - { - return r.release(); - } - else - { - return 0; - } - } - catch(const DeadlockException&) - { - if(_connection->dbTxn() != 0) - { - throw; - } - else - { - if(_connection->deadlockWarning()) - { - Warning out(_connection->communicator()->getLogger()); - out << "Deadlock in Freeze::MapHelperI::lowerBound on Map \"" - << _dbName << "\"; retrying ..."; - } - - // - // Ignored, try again - // - } - } + try + { + auto_ptr<IteratorHelperI> r(new IteratorHelperI(*this, readOnly, 0, false)); + if(r->lowerBound(k)) + { + return r.release(); + } + else + { + return 0; + } + } + catch(const DeadlockException&) + { + if(_connection->dbTxn() != 0) + { + throw; + } + else + { + if(_connection->deadlockWarning()) + { + Warning out(_connection->communicator()->getLogger()); + out << "Deadlock in Freeze::MapHelperI::lowerBound on Map \"" + << _dbName << "\"; retrying ..."; + } + + // + // Ignored, try again + // + } + } } } @@ -912,38 +912,38 @@ Freeze::MapHelperI::upperBound(const Key& k, bool readOnly) const { for(;;) { - try - { - auto_ptr<IteratorHelperI> r(new IteratorHelperI(*this, readOnly, 0, false)); - if(r->upperBound(k)) - { - return r.release(); - } - else - { - return 0; - } - } - catch(const DeadlockException&) - { - if(_connection->dbTxn() != 0) - { - throw; - } - else - { - if(_connection->deadlockWarning()) - { - Warning out(_connection->communicator()->getLogger()); - out << "Deadlock in Freeze::MapHelperI::upperBound on Map \"" - << _dbName << "\"; retrying ..."; - } - - // - // Ignored, try again - // - } - } + try + { + auto_ptr<IteratorHelperI> r(new IteratorHelperI(*this, readOnly, 0, false)); + if(r->upperBound(k)) + { + return r.release(); + } + else + { + return 0; + } + } + catch(const DeadlockException&) + { + if(_connection->dbTxn() != 0) + { + throw; + } + else + { + if(_connection->deadlockWarning()) + { + Warning out(_connection->communicator()->getLogger()); + out << "Deadlock in Freeze::MapHelperI::upperBound on Map \"" + << _dbName << "\"; retrying ..."; + } + + // + // Ignored, try again + // + } + } } } @@ -958,56 +958,56 @@ Freeze::MapHelperI::put(const Key& key, const Value& value) DbTxn* txn = _connection->dbTxn(); if(txn == 0) { - closeAllIterators(); + closeAllIterators(); } for(;;) { - try - { - int err = _db->put(txn, &dbKey, &dbValue, - txn != 0 ? 0 : DB_AUTO_COMMIT); - - if(err == 0) - { - break; - } - else - { - // - // Bug in Freeze - // - throw DatabaseException(__FILE__, __LINE__); - } - } - catch(const ::DbDeadlockException& dx) - { - if(txn != 0) - { - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - else - { - if(_connection->deadlockWarning()) - { - Warning out(_connection->communicator()->getLogger()); - out << "Deadlock in Freeze::MapHelperI::put on Map \"" - << _dbName << "\"; retrying ..."; - } - - // - // Ignored, try again - // - } - } - catch(const ::DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + try + { + int err = _db->put(txn, &dbKey, &dbValue, + txn != 0 ? 0 : DB_AUTO_COMMIT); + + if(err == 0) + { + break; + } + else + { + // + // Bug in Freeze + // + throw DatabaseException(__FILE__, __LINE__); + } + } + catch(const ::DbDeadlockException& dx) + { + if(txn != 0) + { + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + else + { + if(_connection->deadlockWarning()) + { + Warning out(_connection->communicator()->getLogger()); + out << "Deadlock in Freeze::MapHelperI::put on Map \"" + << _dbName << "\"; retrying ..."; + } + + // + // Ignored, try again + // + } + } + catch(const ::DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } } @@ -1020,57 +1020,57 @@ Freeze::MapHelperI::erase(const Key& key) DbTxn* txn = _connection->dbTxn(); if(txn == 0) { - closeAllIterators(); + closeAllIterators(); } for(;;) { - try - { - int err = _db->del(txn, &dbKey, txn != 0 ? 0 : DB_AUTO_COMMIT); - - if(err == 0) - { - return true; - } - else if(err == DB_NOTFOUND) - { - return false; - } - else - { - assert(0); - throw DatabaseException(__FILE__, __LINE__); - } - } - catch(const ::DbDeadlockException& dx) - { - if(txn != 0) - { - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - else - { - if(_connection->deadlockWarning()) - { - Warning out(_connection->communicator()->getLogger()); - out << "Deadlock in Freeze::MapHelperI::erase on Map \"" - << _dbName << "\"; retrying ..."; - } - - // - // Ignored, try again - // - } - } - catch(const ::DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + try + { + int err = _db->del(txn, &dbKey, txn != 0 ? 0 : DB_AUTO_COMMIT); + + if(err == 0) + { + return true; + } + else if(err == DB_NOTFOUND) + { + return false; + } + else + { + assert(0); + throw DatabaseException(__FILE__, __LINE__); + } + } + catch(const ::DbDeadlockException& dx) + { + if(txn != 0) + { + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + else + { + if(_connection->deadlockWarning()) + { + Warning out(_connection->communicator()->getLogger()); + out << "Deadlock in Freeze::MapHelperI::erase on Map \"" + << _dbName << "\"; retrying ..."; + } + + // + // Ignored, try again + // + } + } + catch(const ::DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } } @@ -1088,52 +1088,52 @@ Freeze::MapHelperI::count(const Key& key) const for(;;) { - try - { - int err = _db->get(_connection->dbTxn(), &dbKey, &dbValue, 0); - - if(err == 0) - { - return 1; - } - else if(err == DB_NOTFOUND) - { - return 0; - } - else - { - assert(0); - throw DatabaseException(__FILE__, __LINE__); - } - } - catch(const ::DbDeadlockException& dx) - { - if(_connection->dbTxn() != 0) - { - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - else - { - if(_connection->deadlockWarning()) - { - Warning out(_connection->communicator()->getLogger()); - out << "Deadlock in Freeze::MapHelperI::count on Map \"" - << _dbName << "\"; retrying ..."; - } - - // - // Ignored, try again - // - } - } - catch(const ::DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + try + { + int err = _db->get(_connection->dbTxn(), &dbKey, &dbValue, 0); + + if(err == 0) + { + return 1; + } + else if(err == DB_NOTFOUND) + { + return 0; + } + else + { + assert(0); + throw DatabaseException(__FILE__, __LINE__); + } + } + catch(const ::DbDeadlockException& dx) + { + if(_connection->dbTxn() != 0) + { + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + else + { + if(_connection->deadlockWarning()) + { + Warning out(_connection->communicator()->getLogger()); + out << "Deadlock in Freeze::MapHelperI::count on Map \"" + << _dbName << "\"; retrying ..."; + } + + // + // Ignored, try again + // + } + } + catch(const ::DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } } @@ -1143,47 +1143,47 @@ Freeze::MapHelperI::clear() DbTxn* txn = _connection->dbTxn(); if(txn == 0) { - closeAllIterators(); + closeAllIterators(); } for(;;) { - try - { - u_int32_t count; - int err; - err = _db->truncate(txn, &count, txn != 0 ? 0 : DB_AUTO_COMMIT); - assert(err == 0); - break; - } - catch(const ::DbDeadlockException& dx) - { - if(txn != 0) - { - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - else - { - if(_connection->deadlockWarning()) - { - Warning out(_connection->communicator()->getLogger()); - out << "Deadlock in Freeze::MapHelperI::clear on Map \"" - << _dbName << "\"; retrying ..."; - } - - // - // Ignored, try again - // - } - } - catch(const ::DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + try + { + u_int32_t count; + int err; + err = _db->truncate(txn, &count, txn != 0 ? 0 : DB_AUTO_COMMIT); + assert(err == 0); + break; + } + catch(const ::DbDeadlockException& dx) + { + if(txn != 0) + { + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + else + { + if(_connection->deadlockWarning()) + { + Warning out(_connection->communicator()->getLogger()); + out << "Deadlock in Freeze::MapHelperI::clear on Map \"" + << _dbName << "\"; retrying ..."; + } + + // + // Ignored, try again + // + } + } + catch(const ::DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } } @@ -1192,71 +1192,71 @@ Freeze::MapHelperI::destroy() { if(_dbName == catalogName()) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = "You cannot destroy the " + catalogName() + " database"; - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = "You cannot destroy the " + catalogName() + " database"; + throw ex; } TransactionPtr tx = _connection->currentTransaction(); bool ownTx = (tx == 0); if(ownTx) - { - tx = _connection->beginTransaction(); + { + tx = _connection->beginTransaction(); } DbTxn* txn = _connection->dbTxn(); try { - close(); + close(); - Catalog catalog(_connection, catalogName()); - catalog.erase(_dbName); - _connection->dbEnv()->getEnv()->dbremove(txn, _dbName.c_str(), 0, 0); + Catalog catalog(_connection, catalogName()); + catalog.erase(_dbName); + _connection->dbEnv()->getEnv()->dbremove(txn, _dbName.c_str(), 0, 0); - if(ownTx) - { - tx->commit(); - } + if(ownTx) + { + tx->commit(); + } } catch(const ::DbException& dx) { - if(ownTx) - { - tx = _connection->currentTransaction(); - if(tx != 0) - { - try - { - tx->rollback(); - } - catch(...) - { - } - } - } - - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + if(ownTx) + { + tx = _connection->currentTransaction(); + if(tx != 0) + { + try + { + tx->rollback(); + } + catch(...) + { + } + } + } + + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } catch(...) { - if(ownTx) - { - tx = _connection->currentTransaction(); - if(tx != 0) - { - try - { - tx->rollback(); - } - catch(...) - { - } - } - } - throw; + if(ownTx) + { + tx = _connection->currentTransaction(); + if(tx != 0) + { + try + { + tx->rollback(); + } + catch(...) + { + } + } + } + throw; } } @@ -1282,9 +1282,9 @@ Freeze::MapHelperI::size() const } catch(const ::DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } size_t num = s->bt_ndata; @@ -1298,7 +1298,7 @@ Freeze::MapHelperI::closeAllIterators() { while(!_iteratorList.empty()) { - (*_iteratorList.begin())->close(); + (*_iteratorList.begin())->close(); } } @@ -1308,9 +1308,9 @@ Freeze::MapHelperI::index(const string& name) const IndexMap::const_iterator p = _indices.find(name); if(p == _indices.end()) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = "Cannot find index \"" + name + "\""; - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = "Cannot find index \"" + name + "\""; + throw ex; } return p->second; } @@ -1320,16 +1320,16 @@ Freeze::MapHelperI::close() { if(_db != 0) { - _connection->unregisterMap(this); + _connection->unregisterMap(this); } _db = 0; for(IndexMap::iterator p = _indices.begin(); p != _indices.end(); ++p) { - MapIndexBasePtr& indexBase = p->second; + MapIndexBasePtr& indexBase = p->second; - indexBase->_impl = 0; - indexBase->_map = 0; + indexBase->_impl = 0; + indexBase->_map = 0; } _indices.clear(); } @@ -1343,15 +1343,15 @@ Freeze::MapHelperI::closeAllIteratorsExcept(const IteratorHelperI::TxPtr& tx) co while(q != _iteratorList.end()) { - if((*q)->tx().get() == tx.get()) - { - ++q; - } - else - { - (*q)->close(); - q = _iteratorList.begin(); - } + if((*q)->tx().get() == tx.get()) + { + ++q; + } + else + { + (*q)->close(); + q = _iteratorList.begin(); + } } } @@ -1385,7 +1385,7 @@ callback(Db* secondary, const Dbt* key, const Dbt* value, Dbt* result) Freeze::MapIndexI::MapIndexI(const ConnectionIPtr& connection, SharedDb& db, - DbTxn* txn, bool createDb, const MapIndexBasePtr& index) : + DbTxn* txn, bool createDb, const MapIndexBasePtr& index) : _index(index) { assert(txn != 0); @@ -1397,7 +1397,7 @@ Freeze::MapIndexI::MapIndexI(const ConnectionIPtr& connection, SharedDb& db, u_int32_t flags = 0; if(createDb) { - flags = DB_CREATE; + flags = DB_CREATE; } _dbName = db.dbName() + "." + _index->name(); @@ -1406,7 +1406,7 @@ Freeze::MapIndexI::MapIndexI(const ConnectionIPtr& connection, SharedDb& db, if(index->compareEnabled()) { - _db->set_bt_compare(&customIndexCompare); + _db->set_bt_compare(&customIndexCompare); } _db->open(txn, _dbName.c_str(), 0, DB_BTREE, flags, FREEZE_DB_MODE); @@ -1429,17 +1429,17 @@ Freeze::MapIndexI::~MapIndexI() IteratorHelper* Freeze::MapIndexI::untypedFind(const Key& k, bool ro, const MapHelperI& m, - bool onlyDups) const + bool onlyDups) const { auto_ptr<IteratorHelperI> r(new IteratorHelperI(m, ro, _index, onlyDups)); if(r->find(k)) { - return r.release(); + return r.release(); } else { - return 0; + return 0; } } @@ -1450,11 +1450,11 @@ Freeze::MapIndexI::untypedLowerBound(const Key& k, bool ro, const MapHelperI& m) if(r->lowerBound(k)) { - return r.release(); + return r.release(); } else { - return 0; + return 0; } } @@ -1465,11 +1465,11 @@ Freeze::MapIndexI::untypedUpperBound(const Key& k, bool ro, const MapHelperI& m) if(r->upperBound(k)) { - return r.release(); + return r.release(); } else { - return 0; + return 0; } } @@ -1493,81 +1493,81 @@ Freeze::MapIndexI::untypedCount(const Key& k, const ConnectionIPtr& connection) try { - for(;;) - { - Dbc* dbc = 0; - - try - { - // - // Move to the first record - // - _db->cursor(0, &dbc, 0); - bool found = (dbc->get(&dbKey, &dbValue, DB_SET) == 0); - - if(found) - { - db_recno_t count = 0; - dbc->count(&count, 0); - result = static_cast<int>(count); - } - - Dbc* toClose = dbc; - dbc = 0; - toClose->close(); - break; // for (;;) - } - catch(const DbDeadlockException&) - { - if(dbc != 0) - { - try - { - dbc->close(); - } - catch(const DbDeadlockException&) - { - // - // Ignored - // - } - } - - if(connection->deadlockWarning()) - { - Warning out(connection->communicator()->getLogger()); - out << "Deadlock in Freeze::MapIndexI::untypedCount while searching \"" - << _dbName << "\"; retrying ..."; - } - - // - // Retry - // - } - catch(...) - { - if(dbc != 0) - { - try - { - dbc->close(); - } - catch(const DbDeadlockException&) - { - // - // Ignored - // - } - } - throw; - } - } + for(;;) + { + Dbc* dbc = 0; + + try + { + // + // Move to the first record + // + _db->cursor(0, &dbc, 0); + bool found = (dbc->get(&dbKey, &dbValue, DB_SET) == 0); + + if(found) + { + db_recno_t count = 0; + dbc->count(&count, 0); + result = static_cast<int>(count); + } + + Dbc* toClose = dbc; + dbc = 0; + toClose->close(); + break; // for (;;) + } + catch(const DbDeadlockException&) + { + if(dbc != 0) + { + try + { + dbc->close(); + } + catch(const DbDeadlockException&) + { + // + // Ignored + // + } + } + + if(connection->deadlockWarning()) + { + Warning out(connection->communicator()->getLogger()); + out << "Deadlock in Freeze::MapIndexI::untypedCount while searching \"" + << _dbName << "\"; retrying ..."; + } + + // + // Retry + // + } + catch(...) + { + if(dbc != 0) + { + try + { + dbc->close(); + } + catch(const DbDeadlockException&) + { + // + // Ignored + // + } + } + throw; + } + } } catch(const DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } return result; @@ -1575,7 +1575,7 @@ Freeze::MapIndexI::untypedCount(const Key& k, const ConnectionIPtr& connection) int Freeze::MapIndexI::secondaryKeyCreate(Db* secondary, const Dbt* dbKey, - const Dbt* dbValue, Dbt* result) + const Dbt* dbValue, Dbt* result) { Byte* first = static_cast<Byte*>(dbValue->get_data()); Value value(first, first + dbValue->get_size()); diff --git a/cpp/src/Freeze/MapI.h b/cpp/src/Freeze/MapI.h index fa2e3524b3e..3623beae33d 100644 --- a/cpp/src/Freeze/MapI.h +++ b/cpp/src/Freeze/MapI.h @@ -24,7 +24,7 @@ class IteratorHelperI : public IteratorHelper public: IteratorHelperI(const MapHelperI& m, bool readOnly, - const MapIndexBasePtr& index, bool onlyDups); + const MapIndexBasePtr& index, bool onlyDups); IteratorHelperI(const IteratorHelperI&); virtual @@ -66,20 +66,20 @@ public: { public: - Tx(const MapHelperI&); - ~Tx(); + Tx(const MapHelperI&); + ~Tx(); - void dead(); + void dead(); - DbTxn* getTxn() - { - return _txn; - } + DbTxn* getTxn() + { + return _txn; + } private: - const MapHelperI& _map; - DbTxn* _txn; - bool _dead; + const MapHelperI& _map; + DbTxn* _txn; + bool _dead; }; typedef IceUtil::Handle<Tx> TxPtr; @@ -108,9 +108,9 @@ class MapHelperI : public MapHelper public: MapHelperI(const ConnectionIPtr&, const std::string&, - const std::string&, const std::string&, - const KeyCompareBasePtr&, - const std::vector<MapIndexBasePtr>&, bool); + const std::string&, const std::string&, + const KeyCompareBasePtr&, + const std::vector<MapIndexBasePtr>&, bool); virtual ~MapHelperI(); @@ -152,7 +152,7 @@ public: const ConnectionIPtr& connection() const { - return _connection; + return _connection; } diff --git a/cpp/src/Freeze/ObjectStore.cpp b/cpp/src/Freeze/ObjectStore.cpp index 6f120752afb..24ab9ba484f 100644 --- a/cpp/src/Freeze/ObjectStore.cpp +++ b/cpp/src/Freeze/ObjectStore.cpp @@ -20,9 +20,9 @@ using namespace Freeze; Freeze::ObjectStore::ObjectStore(const string& facet, - bool createDb, EvictorI* evictor, - const vector<IndexPtr>& indices, - bool populateEmptyIndices) : + bool createDb, EvictorI* evictor, + const vector<IndexPtr>& indices, + bool populateEmptyIndices) : _facet(facet), _evictor(evictor), _indices(indices), @@ -30,11 +30,11 @@ Freeze::ObjectStore::ObjectStore(const string& facet, { if(facet == "") { - _dbName = EvictorI::defaultDb; + _dbName = EvictorI::defaultDb; } else { - _dbName = facet; + _dbName = facet; } ConnectionPtr catalogConnection = createConnection(_communicator, evictor->dbEnv()->getEnvName()); @@ -43,85 +43,85 @@ Freeze::ObjectStore::ObjectStore(const string& facet, Catalog::iterator p = catalog.find(evictor->filename()); if(p != catalog.end()) { - if(p->second.evictor == false) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = evictor->filename() + " is an evictor database"; - throw ex; - } + if(p->second.evictor == false) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = evictor->filename() + " is an evictor database"; + throw ex; + } } DbEnv* dbEnv = evictor->dbEnv()->getEnv(); try { - _db.reset(new Db(dbEnv, 0)); - - TransactionPtr tx = catalogConnection->beginTransaction(); - DbTxn* txn = getTxn(tx); - - u_int32_t flags = DB_THREAD; - if(createDb) - { - flags |= DB_CREATE; - } - _db->open(txn, evictor->filename().c_str(), _dbName.c_str(), DB_BTREE, flags, FREEZE_DB_MODE); - - for(size_t i = 0; i < _indices.size(); ++i) - { - _indices[i]->_impl->associate(this, txn, createDb, populateEmptyIndices); - } - - if(p == catalog.end()) - { - CatalogData catalogData; - catalogData.evictor = true; - catalog.put(Catalog::value_type(evictor->filename(), catalogData)); - } - - tx->commit(); + _db.reset(new Db(dbEnv, 0)); + + TransactionPtr tx = catalogConnection->beginTransaction(); + DbTxn* txn = getTxn(tx); + + u_int32_t flags = DB_THREAD; + if(createDb) + { + flags |= DB_CREATE; + } + _db->open(txn, evictor->filename().c_str(), _dbName.c_str(), DB_BTREE, flags, FREEZE_DB_MODE); + + for(size_t i = 0; i < _indices.size(); ++i) + { + _indices[i]->_impl->associate(this, txn, createDb, populateEmptyIndices); + } + + if(p == catalog.end()) + { + CatalogData catalogData; + catalogData.evictor = true; + catalog.put(Catalog::value_type(evictor->filename(), catalogData)); + } + + tx->commit(); } catch(const DbException& dx) { - TransactionPtr tx = catalogConnection->currentTransaction(); - if(tx != 0) - { - try - { - tx->rollback(); - } - catch(...) - { - } - } - - if(dx.get_errno() == ENOENT) - { - NotFoundException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - else - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + TransactionPtr tx = catalogConnection->currentTransaction(); + if(tx != 0) + { + try + { + tx->rollback(); + } + catch(...) + { + } + } + + if(dx.get_errno() == ENOENT) + { + NotFoundException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + else + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } catch(...) { - TransactionPtr tx = catalogConnection->currentTransaction(); - if(tx != 0) - { - try - { - tx->rollback(); - } - catch(...) - { - } - } - throw; + TransactionPtr tx = catalogConnection->currentTransaction(); + if(tx != 0) + { + try + { + tx->rollback(); + } + catch(...) + { + } + } + throw; } } @@ -129,7 +129,7 @@ Freeze::ObjectStore::~ObjectStore() { if(_db.get() != 0) { - close(); + close(); } } @@ -138,19 +138,19 @@ Freeze::ObjectStore::close() { try { - _db->close(0); - - for(size_t i = 0; i < _indices.size(); ++i) - { - _indices[i]->_impl->close(); - } - _indices.clear(); + _db->close(0); + + for(size_t i = 0; i < _indices.size(); ++i) + { + _indices[i]->_impl->close(); + } + _indices.clear(); } catch(const DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } _db.reset(); } @@ -171,43 +171,43 @@ Freeze::ObjectStore::dbHasObject(const Identity& ident) const for(;;) { - try - { - int err = _db->get(0, &dbKey, &dbValue, 0); - - if(err == 0) - { - return true; - } - else if(err == DB_NOTFOUND) - { - return false; - } - else - { - assert(0); - throw DatabaseException(__FILE__, __LINE__); - } - } - catch(const DbDeadlockException&) - { - if(_evictor->deadlockWarning()) - { - Warning out(_communicator->getLogger()); - out << "Deadlock in Freeze::ObjectStore::dbHasObject while searching \"" - << _evictor->filename() + "/" + _dbName << "\"; retrying ..."; - } - - // - // Ignored, try again - // - } - catch(const DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + try + { + int err = _db->get(0, &dbKey, &dbValue, 0); + + if(err == 0) + { + return true; + } + else if(err == DB_NOTFOUND) + { + return false; + } + else + { + assert(0); + throw DatabaseException(__FILE__, __LINE__); + } + } + catch(const DbDeadlockException&) + { + if(_evictor->deadlockWarning()) + { + Warning out(_communicator->getLogger()); + out << "Deadlock in Freeze::ObjectStore::dbHasObject while searching \"" + << _evictor->filename() + "/" + _dbName << "\"; retrying ..."; + } + + // + // Ignored, try again + // + } + catch(const DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } } @@ -217,37 +217,37 @@ Freeze::ObjectStore::save(Key& key, Value& value, Byte status, DbTxn* tx) { switch(status) { - case EvictorElement::created: - case EvictorElement::modified: - { - Dbt dbKey; - Dbt dbValue; - initializeInDbt(key, dbKey); - initializeInDbt(value, dbValue); - u_int32_t flags = (status == EvictorElement::created) ? DB_NOOVERWRITE : 0; - int err = _db->put(tx, &dbKey, &dbValue, flags); - if(err != 0) - { - throw DatabaseException(__FILE__, __LINE__); - } - break; - } - - case EvictorElement::destroyed: - { - Dbt dbKey; - initializeInDbt(key, dbKey); - int err = _db->del(tx, &dbKey, 0); - if(err != 0) - { - throw DatabaseException(__FILE__, __LINE__); - } - break; - } - default: - { - assert(0); - } + case EvictorElement::created: + case EvictorElement::modified: + { + Dbt dbKey; + Dbt dbValue; + initializeInDbt(key, dbKey); + initializeInDbt(value, dbValue); + u_int32_t flags = (status == EvictorElement::created) ? DB_NOOVERWRITE : 0; + int err = _db->put(tx, &dbKey, &dbValue, flags); + if(err != 0) + { + throw DatabaseException(__FILE__, __LINE__); + } + break; + } + + case EvictorElement::destroyed: + { + Dbt dbKey; + initializeInDbt(key, dbKey); + int err = _db->del(tx, &dbKey, 0); + if(err != 0) + { + throw DatabaseException(__FILE__, __LINE__); + } + break; + } + default: + { + assert(0); + } } } @@ -322,36 +322,36 @@ Freeze::ObjectStore::load(const Identity& ident) for(;;) { - try - { - int rs = _db->get(0, &dbKey, &dbValue, 0); - if(rs == DB_NOTFOUND) - { - return 0; - } - else if(rs != 0) - { - assert(0); - throw DatabaseException(__FILE__, __LINE__); - } - break; // for(;;) - } - catch(const DbDeadlockException&) - { - if(_evictor->deadlockWarning()) - { - Warning out(_communicator->getLogger()); - out << "Deadlock in Freeze::ObjectStore::load while searching \"" - << _evictor->filename() + "/" + _dbName << "\"; retrying ..."; - } - // - // Ignored, try again - // - } - catch(const DbException& dx) - { - handleDbException(dx, value, dbValue, __FILE__, __LINE__); - } + try + { + int rs = _db->get(0, &dbKey, &dbValue, 0); + if(rs == DB_NOTFOUND) + { + return 0; + } + else if(rs != 0) + { + assert(0); + throw DatabaseException(__FILE__, __LINE__); + } + break; // for(;;) + } + catch(const DbDeadlockException&) + { + if(_evictor->deadlockWarning()) + { + Warning out(_communicator->getLogger()); + out << "Deadlock in Freeze::ObjectStore::load while searching \"" + << _evictor->filename() + "/" + _dbName << "\"; retrying ..."; + } + // + // Ignored, try again + // + } + catch(const DbException& dx) + { + handleDbException(dx, value, dbValue, __FILE__, __LINE__); + } } EvictorElementPtr result = new EvictorElement(*this); diff --git a/cpp/src/Freeze/ObjectStore.h b/cpp/src/Freeze/ObjectStore.h index 274a82331f7..94db579a64d 100644 --- a/cpp/src/Freeze/ObjectStore.h +++ b/cpp/src/Freeze/ObjectStore.h @@ -36,7 +36,7 @@ class ObjectStore : public Cache public: ObjectStore(const std::string&, bool, EvictorI*, - const std::vector<IndexPtr>& = std::vector<IndexPtr>(), bool = false); + const std::vector<IndexPtr>& = std::vector<IndexPtr>(), bool = false); virtual ~ObjectStore(); diff --git a/cpp/src/Freeze/SharedDb.cpp b/cpp/src/Freeze/SharedDb.cpp index 86205fa91b4..0c208c26eb1 100644 --- a/cpp/src/Freeze/SharedDb.cpp +++ b/cpp/src/Freeze/SharedDb.cpp @@ -31,15 +31,15 @@ checkTypes(const SharedDb& sharedDb, const string& key, const string& value) { if(key != sharedDb.key()) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = sharedDb.dbName() + "'s key type is " + sharedDb.key() + ", not " + key; - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = sharedDb.dbName() + "'s key type is " + sharedDb.key() + ", not " + key; + throw ex; } if(value != sharedDb.value()) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = sharedDb.dbName() + "'s value type is " + sharedDb.value() + ", not " + value; - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = sharedDb.dbName() + "'s value type is " + sharedDb.value() + ", not " + value; + throw ex; } } } @@ -68,29 +68,29 @@ Freeze::catalogName() SharedDbPtr Freeze::SharedDb::get(const ConnectionIPtr& connection, - const string& dbName, - const string& key, - const string& value, - const KeyCompareBasePtr& keyCompare, - const vector<MapIndexBasePtr>& indices, - bool createDb) + const string& dbName, + const string& key, + const string& value, + const KeyCompareBasePtr& keyCompare, + const vector<MapIndexBasePtr>& indices, + bool createDb) { if(dbName == _catalogName) { - // - // We don't want to lock the _mapMutex to retrieve the catalog - // - - SharedDbPtr result = connection->dbEnv()->getCatalog(); - checkTypes(*result, key, value); - return result; + // + // We don't want to lock the _mapMutex to retrieve the catalog + // + + SharedDbPtr result = connection->dbEnv()->getCatalog(); + checkTypes(*result, key, value); + return result; } StaticMutex::Lock lock(_mapMutex); if(sharedDbMap == 0) { - sharedDbMap = new SharedDbMap; + sharedDbMap = new SharedDbMap; } MapKey mapKey; @@ -99,20 +99,20 @@ Freeze::SharedDb::get(const ConnectionIPtr& connection, mapKey.dbName = dbName; { - SharedDbMap::iterator p = sharedDbMap->find(mapKey); - if(p != sharedDbMap->end()) - { - checkTypes(*(p->second), key, value); - p->second->connectIndices(indices); - return p->second; - } + SharedDbMap::iterator p = sharedDbMap->find(mapKey); + if(p != sharedDbMap->end()) + { + checkTypes(*(p->second), key, value); + p->second->connectIndices(indices); + return p->second; + } } // // MapKey not found, let's create and open a new Db // auto_ptr<SharedDb> result(new SharedDb(mapKey, key, value, connection, - keyCompare, indices, createDb)); + keyCompare, indices, createDb)); // // Insert it into the map @@ -131,7 +131,7 @@ Freeze::SharedDb::openCatalog(SharedDbEnv& dbEnv) if(sharedDbMap == 0) { - sharedDbMap = new SharedDbMap; + sharedDbMap = new SharedDbMap; } MapKey mapKey; @@ -145,17 +145,17 @@ Freeze::SharedDb::openCatalog(SharedDbEnv& dbEnv) // Insert it into the map // pair<SharedDbMap::iterator, bool> insertResult - = sharedDbMap->insert(SharedDbMap::value_type(mapKey, result.get())); + = sharedDbMap->insert(SharedDbMap::value_type(mapKey, result.get())); if(!insertResult.second) { - // - // That's very wrong: the catalog is associated with another env - // - assert(0); - DatabaseException ex(__FILE__, __LINE__); - ex.message = "Catalog already opened"; - throw ex; + // + // That's very wrong: the catalog is associated with another env + // + assert(0); + DatabaseException ex(__FILE__, __LINE__); + ex.message = "Catalog already opened"; + throw ex; } return result.release(); @@ -166,8 +166,8 @@ Freeze::SharedDb::~SharedDb() { if(_trace >= 1) { - Trace out(_mapKey.communicator->getLogger(), "Freeze.Map"); - out << "closing Db \"" << _mapKey.dbName << "\""; + Trace out(_mapKey.communicator->getLogger(), "Freeze.Map"); + out << "closing Db \"" << _mapKey.dbName << "\""; } cleanup(false); @@ -177,8 +177,8 @@ void Freeze::SharedDb::__incRef() { if(_trace >= 2) { - Trace out(_mapKey.communicator->getLogger(), "Freeze.Map"); - out << "incremeting reference count for Db \"" << _mapKey.dbName << "\""; + Trace out(_mapKey.communicator->getLogger(), "Freeze.Map"); + out << "incremeting reference count for Db \"" << _mapKey.dbName << "\""; } IceUtil::StaticMutex::Lock lock(_refCountMutex); @@ -189,57 +189,57 @@ void Freeze::SharedDb::__decRef() { if(_trace >= 2) { - Trace out(_mapKey.communicator->getLogger(), "Freeze.Map"); - out << "removing reference count for Db \"" << _mapKey.dbName << "\""; + Trace out(_mapKey.communicator->getLogger(), "Freeze.Map"); + out << "removing reference count for Db \"" << _mapKey.dbName << "\""; } IceUtil::StaticMutex::Lock lock(_refCountMutex); if(--_refCount == 0) { IceUtil::StaticMutex::TryLock mapLock(_mapMutex); - if(!mapLock.acquired()) - { - // - // Reacquire mutex in proper order and check again - // - lock.release(); - mapLock.acquire(); - lock.acquire(); - if(_refCount > 0) - { - return; - } - } - - // - // Remove from map - // - size_t one; - one = sharedDbMap->erase(_mapKey); - assert(one == 1); - - if(sharedDbMap->size() == 0) - { - delete sharedDbMap; - sharedDbMap = 0; - } - - // - // Keep lock to prevent somebody else to re-open this Db - // before it's closed. - // - delete this; + if(!mapLock.acquired()) + { + // + // Reacquire mutex in proper order and check again + // + lock.release(); + mapLock.acquire(); + lock.acquire(); + if(_refCount > 0) + { + return; + } + } + + // + // Remove from map + // + size_t one; + one = sharedDbMap->erase(_mapKey); + assert(one == 1); + + if(sharedDbMap->size() == 0) + { + delete sharedDbMap; + sharedDbMap = 0; + } + + // + // Keep lock to prevent somebody else to re-open this Db + // before it's closed. + // + delete this; } } Freeze::SharedDb::SharedDb(const MapKey& mapKey, - const string& key, - const string& value, - const ConnectionIPtr& connection, - const KeyCompareBasePtr& keyCompare, - const vector<MapIndexBasePtr>& indices, - bool createDb) : + const string& key, + const string& value, + const ConnectionIPtr& connection, + const KeyCompareBasePtr& keyCompare, + const vector<MapIndexBasePtr>& indices, + bool createDb) : Db(connection->dbEnv()->getEnv(), 0), _mapKey(mapKey), _refCount(0), @@ -248,128 +248,128 @@ Freeze::SharedDb::SharedDb(const MapKey& mapKey, { if(_trace >= 1) { - Trace out(_mapKey.communicator->getLogger(), "Freeze.Map"); - out << "opening Db \"" << _mapKey.dbName << "\""; + Trace out(_mapKey.communicator->getLogger(), "Freeze.Map"); + out << "opening Db \"" << _mapKey.dbName << "\""; } ConnectionPtr catalogConnection = - createConnection(_mapKey.communicator, connection->dbEnv()->getEnvName()); + createConnection(_mapKey.communicator, connection->dbEnv()->getEnvName()); Catalog catalog(catalogConnection, _catalogName); Catalog::iterator ci = catalog.find(_mapKey.dbName); if(ci != catalog.end()) { - if(ci->second.evictor) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = _mapKey.dbName + " is an evictor database"; - throw ex; - } - - _key = ci->second.key; - _value = ci->second.value; - checkTypes(*this, key, value); + if(ci->second.evictor) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = _mapKey.dbName + " is an evictor database"; + throw ex; + } + + _key = ci->second.key; + _value = ci->second.value; + checkTypes(*this, key, value); } else { - _key = key; - _value = value; + _key = key; + _value = value; } set_app_private(this); if(_keyCompare->compareEnabled()) { - set_bt_compare(&customCompare); + set_bt_compare(&customCompare); } try { - TransactionPtr tx = catalogConnection->beginTransaction(); - DbTxn* txn = getTxn(tx); - - u_int32_t flags = DB_THREAD; - if(createDb) - { - flags |= DB_CREATE; - } - open(txn, _mapKey.dbName.c_str(), 0, DB_BTREE, flags, FREEZE_DB_MODE); - - for(vector<MapIndexBasePtr>::const_iterator p = indices.begin(); - p != indices.end(); ++p) - { - const MapIndexBasePtr& indexBase = *p; - assert(indexBase->_impl == 0); - assert(indexBase->_communicator == 0); - indexBase->_communicator = connection->communicator(); - - auto_ptr<MapIndexI> indexI(new MapIndexI(connection, *this, txn, createDb, indexBase)); - + TransactionPtr tx = catalogConnection->beginTransaction(); + DbTxn* txn = getTxn(tx); + + u_int32_t flags = DB_THREAD; + if(createDb) + { + flags |= DB_CREATE; + } + open(txn, _mapKey.dbName.c_str(), 0, DB_BTREE, flags, FREEZE_DB_MODE); + + for(vector<MapIndexBasePtr>::const_iterator p = indices.begin(); + p != indices.end(); ++p) + { + const MapIndexBasePtr& indexBase = *p; + assert(indexBase->_impl == 0); + assert(indexBase->_communicator == 0); + indexBase->_communicator = connection->communicator(); + + auto_ptr<MapIndexI> indexI(new MapIndexI(connection, *this, txn, createDb, indexBase)); + #ifndef NDEBUG - bool inserted = + bool inserted = #endif - _indices.insert(IndexMap::value_type(indexBase->name(), indexI.get())).second; - assert(inserted); - - indexBase->_impl = indexI.release(); - } - - if(ci == catalog.end()) - { - CatalogData catalogData; - catalogData.evictor = false; - catalogData.key = key; - catalogData.value = value; - catalog.put(Catalog::value_type(_mapKey.dbName, catalogData)); - } - - tx->commit(); + _indices.insert(IndexMap::value_type(indexBase->name(), indexI.get())).second; + assert(inserted); + + indexBase->_impl = indexI.release(); + } + + if(ci == catalog.end()) + { + CatalogData catalogData; + catalogData.evictor = false; + catalogData.key = key; + catalogData.value = value; + catalog.put(Catalog::value_type(_mapKey.dbName, catalogData)); + } + + tx->commit(); } catch(const ::DbException& dx) { - TransactionPtr tx = catalogConnection->currentTransaction(); - if(tx != 0) - { - try - { - tx->rollback(); - } - catch(...) - { - } - } - - cleanup(true); - - if(dx.get_errno() == ENOENT) - { - NotFoundException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - else - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + TransactionPtr tx = catalogConnection->currentTransaction(); + if(tx != 0) + { + try + { + tx->rollback(); + } + catch(...) + { + } + } + + cleanup(true); + + if(dx.get_errno() == ENOENT) + { + NotFoundException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + else + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } catch(...) { - TransactionPtr tx = catalogConnection->currentTransaction(); - if(tx != 0) - { - try - { - tx->rollback(); - } - catch(...) - { - } - } - - cleanup(true); - throw; + TransactionPtr tx = catalogConnection->currentTransaction(); + if(tx != 0) + { + try + { + tx->rollback(); + } + catch(...) + { + } + } + + cleanup(true); + throw; } } @@ -385,20 +385,20 @@ Freeze::SharedDb::SharedDb(const MapKey& mapKey, DbEnv* env) : if(_trace >= 1) { - Trace out(_mapKey.communicator->getLogger(), "Freeze.Db"); - out << "opening Db \"" << _mapKey.dbName << "\""; + Trace out(_mapKey.communicator->getLogger(), "Freeze.Db"); + out << "opening Db \"" << _mapKey.dbName << "\""; } try { - u_int32_t flags = DB_THREAD | DB_CREATE | DB_AUTO_COMMIT; - open(0, _mapKey.dbName.c_str(), 0, DB_BTREE, flags, FREEZE_DB_MODE); + u_int32_t flags = DB_THREAD | DB_CREATE | DB_AUTO_COMMIT; + open(0, _mapKey.dbName.c_str(), 0, DB_BTREE, flags, FREEZE_DB_MODE); } catch(const ::DbException& dx) { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } } @@ -406,15 +406,15 @@ void Freeze::SharedDb::connectIndices(const vector<MapIndexBasePtr>& indices) const { for(vector<MapIndexBasePtr>::const_iterator p = indices.begin(); - p != indices.end(); ++p) + p != indices.end(); ++p) { - const MapIndexBasePtr& indexBase = *p; - assert(indexBase->_impl == 0); + const MapIndexBasePtr& indexBase = *p; + assert(indexBase->_impl == 0); - IndexMap::const_iterator q = _indices.find(indexBase->name()); - assert(q != _indices.end()); - indexBase->_impl = q->second; - indexBase->_communicator = _mapKey.communicator; + IndexMap::const_iterator q = _indices.find(indexBase->name()); + assert(q != _indices.end()); + indexBase->_impl = q->second; + indexBase->_communicator = _mapKey.communicator; } } @@ -423,21 +423,21 @@ Freeze::SharedDb::cleanup(bool noThrow) { try { - for(IndexMap::iterator p = _indices.begin(); p != _indices.end(); ++p) - { - delete p->second; - } - _indices.clear(); - - close(0); + for(IndexMap::iterator p = _indices.begin(); p != _indices.end(); ++p) + { + delete p->second; + } + _indices.clear(); + + close(0); } catch(const ::DbException& dx) { - if(!noThrow) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + if(!noThrow) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } } diff --git a/cpp/src/Freeze/SharedDb.h b/cpp/src/Freeze/SharedDb.h index a6ce6672c44..3576f51fa23 100644 --- a/cpp/src/Freeze/SharedDb.h +++ b/cpp/src/Freeze/SharedDb.h @@ -30,7 +30,7 @@ class MapIndexI public: MapIndexI(const ConnectionIPtr&, SharedDb&, - DbTxn*, bool, const MapIndexBasePtr&); + DbTxn*, bool, const MapIndexBasePtr&); ~MapIndexI(); @@ -45,17 +45,17 @@ public: const std::string name() const { - return _index->name(); + return _index->name(); } Db* db() const { - return _db.get(); + return _db.get(); } const MapIndexBasePtr& getKeyCompare() const { - return _index; + return _index; } private: @@ -73,9 +73,9 @@ public: using Db::get; static SharedDbPtr get(const ConnectionIPtr&, const std::string&, - const std::string&, const std::string&, - const KeyCompareBasePtr&, - const std::vector<MapIndexBasePtr>&, bool); + const std::string&, const std::string&, + const KeyCompareBasePtr&, + const std::vector<MapIndexBasePtr>&, bool); static SharedDbPtr openCatalog(SharedDbEnv&); @@ -101,7 +101,7 @@ public: virtual int get(DbTxn *txnid, Dbt *key, Dbt *data, u_int32_t flags) { - return ::Db::get(txnid, key, data, flags); + return ::Db::get(txnid, key, data, flags); } #endif @@ -111,19 +111,19 @@ private: struct MapKey { - std::string envName; - Ice::CommunicatorPtr communicator; - std::string dbName; + std::string envName; + Ice::CommunicatorPtr communicator; + std::string dbName; - inline bool - operator<(const MapKey& rhs) const; + inline bool + operator<(const MapKey& rhs) const; }; typedef std::map<MapKey, Freeze::SharedDb*> SharedDbMap; SharedDb(const MapKey&, const std::string&, const std::string&, - const ConnectionIPtr&, const KeyCompareBasePtr&, - const std::vector<MapIndexBasePtr>&, bool); + const ConnectionIPtr&, const KeyCompareBasePtr&, + const std::vector<MapIndexBasePtr>&, bool); SharedDb(const MapKey&, DbEnv*); @@ -170,8 +170,8 @@ inline bool SharedDb::MapKey::operator<(const MapKey& rhs) const { return (communicator < rhs.communicator) || - ((communicator == rhs.communicator) && (dbName < rhs.dbName)) || - ((communicator == rhs.communicator) && (dbName == rhs.dbName) && (envName < rhs.envName)); + ((communicator == rhs.communicator) && (dbName < rhs.dbName)) || + ((communicator == rhs.communicator) && (dbName == rhs.dbName) && (envName < rhs.envName)); } } diff --git a/cpp/src/Freeze/SharedDbEnv.cpp b/cpp/src/Freeze/SharedDbEnv.cpp index ea1dc64a054..3c03af446d2 100644 --- a/cpp/src/Freeze/SharedDbEnv.cpp +++ b/cpp/src/Freeze/SharedDbEnv.cpp @@ -59,7 +59,7 @@ inline bool operator<(const MapKey& lhs, const MapKey& rhs) { return (lhs.communicator < rhs.communicator) || - ((lhs.communicator == rhs.communicator) && (lhs.envName < rhs.envName)); + ((lhs.communicator == rhs.communicator) && (lhs.envName < rhs.envName)); } #if DB_VERSION_MAJOR != 4 @@ -92,13 +92,13 @@ SharedDbEnvMap* sharedDbEnvMap; Freeze::SharedDbEnvPtr Freeze::SharedDbEnv::get(const CommunicatorPtr& communicator, - const string& envName, DbEnv* env) + const string& envName, DbEnv* env) { StaticMutex::Lock lock(_mapMutex); if(sharedDbEnvMap == 0) { - sharedDbEnvMap = new SharedDbEnvMap; + sharedDbEnvMap = new SharedDbEnvMap; } MapKey key; @@ -106,11 +106,11 @@ Freeze::SharedDbEnv::get(const CommunicatorPtr& communicator, key.communicator = communicator; { - SharedDbEnvMap::iterator p = sharedDbEnvMap->find(key); - if(p != sharedDbEnvMap->end()) - { - return p->second; - } + SharedDbEnvMap::iterator p = sharedDbEnvMap->find(key); + if(p != sharedDbEnvMap->end()) + { + return p->second; + } } // @@ -132,8 +132,8 @@ Freeze::SharedDbEnv::~SharedDbEnv() { if(_trace >= 1) { - Trace out(_communicator->getLogger(), "Freeze.DbEnv"); - out << "closing database environment \"" << _envName << "\""; + Trace out(_communicator->getLogger(), "Freeze.DbEnv"); + out << "closing database environment \"" << _envName << "\""; } // @@ -146,22 +146,22 @@ Freeze::SharedDbEnv::~SharedDbEnv() // if(_thread != 0) { - _thread->terminate(); - _thread = 0; + _thread->terminate(); + _thread = 0; } if(_envHolder.get() != 0) { - try - { - _envHolder->close(0); - } - catch(const ::DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } + try + { + _envHolder->close(0); + } + catch(const ::DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } } } @@ -177,47 +177,47 @@ void Freeze::SharedDbEnv::__decRef() if(--_refCount == 0) { IceUtil::StaticMutex::TryLock mapLock(_mapMutex); - if(!mapLock.acquired()) - { - // - // Reacquire mutex in proper order and check again - // - lock.release(); - mapLock.acquire(); - lock.acquire(); - if(_refCount > 0) - { - return; - } - } - - // - // Remove from map - // + if(!mapLock.acquired()) + { + // + // Reacquire mutex in proper order and check again + // + lock.release(); + mapLock.acquire(); + lock.acquire(); + if(_refCount > 0) + { + return; + } + } + + // + // Remove from map + // MapKey key; key.envName = _envName; key.communicator = _communicator; - size_t one; - one = sharedDbEnvMap->erase(key); - assert(one == 1); - - if(sharedDbEnvMap->size() == 0) - { - delete sharedDbEnvMap; - sharedDbEnvMap = 0; - } - - // - // Keep lock to prevent somebody else to re-open this DbEnv - // before it's closed. - // - delete this; + size_t one; + one = sharedDbEnvMap->erase(key); + assert(one == 1); + + if(sharedDbEnvMap->size() == 0) + { + delete sharedDbEnvMap; + sharedDbEnvMap = 0; + } + + // + // Keep lock to prevent somebody else to re-open this DbEnv + // before it's closed. + // + delete this; } } Freeze::SharedDbEnv::SharedDbEnv(const std::string& envName, - const Ice::CommunicatorPtr& communicator, DbEnv* env) : + const Ice::CommunicatorPtr& communicator, DbEnv* env) : _env(env), _envName(envName), _communicator(communicator), @@ -229,113 +229,113 @@ Freeze::SharedDbEnv::SharedDbEnv(const std::string& envName, if(_env == 0) { - _envHolder.reset(new DbEnv(0)); - _env = _envHolder.get(); + _envHolder.reset(new DbEnv(0)); + _env = _envHolder.get(); - if(_trace >= 1) - { - Trace out(_communicator->getLogger(), "Freeze.DbEnv"); - out << "opening database environment \"" << envName << "\""; - } + if(_trace >= 1) + { + Trace out(_communicator->getLogger(), "Freeze.DbEnv"); + out << "opening database environment \"" << envName << "\""; + } - string propertyPrefix = string("Freeze.DbEnv.") + envName; - - try - { - _env->set_errpfx(reinterpret_cast<char*>(this)); + string propertyPrefix = string("Freeze.DbEnv.") + envName; + + try + { + _env->set_errpfx(reinterpret_cast<char*>(this)); - _env->set_errcall(dbErrCallback); + _env->set_errcall(dbErrCallback); #ifdef _WIN32 - // - // Berkeley DB may use a different C++ runtime - // - _env->set_alloc(::malloc, ::realloc, ::free); + // + // Berkeley DB may use a different C++ runtime + // + _env->set_alloc(::malloc, ::realloc, ::free); #endif - - // - // Deadlock detection - // - _env->set_lk_detect(DB_LOCK_YOUNGEST); - - u_int32_t flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN; - - if(properties->getPropertyAsInt(propertyPrefix + ".DbRecoverFatal") != 0) - { - flags |= DB_RECOVER_FATAL | DB_CREATE; - } - else - { - flags |= DB_RECOVER | DB_CREATE; - } - - if(properties->getPropertyAsIntWithDefault(propertyPrefix + ".DbPrivate", 1) != 0) - { - flags |= DB_PRIVATE; - } - - /* - - // - // Does not seem to work reliably in 4.1.25 - // - - time_t timeStamp = properties->getPropertyAsIntWithDefault(propertyPrefix + ".TxTimestamp", 0); - - if(timeStamp != 0) - { - try - { - set_tx_timestamp(&timeStamp); - } - catch(const ::DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - } - */ - - // - // Maybe we can deprecate this property since it can be set in the DB_CONFIG file - // - bool autoDelete = (properties->getPropertyAsIntWithDefault( - propertyPrefix + ".OldLogsAutoDelete", 1) != 0); - - if(autoDelete) - { - _env->set_flags(DB_LOG_AUTOREMOVE, 1); - } - - // - // Threading - // - flags |= DB_THREAD; - - string dbHome = properties->getPropertyWithDefault( - propertyPrefix + ".DbHome", envName); - - _env->open(dbHome.c_str(), flags, FREEZE_DB_MODE); - } - catch(const ::DbException& dx) - { - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; - } - - // - // Default checkpoint period is every 120 seconds - // - Int checkpointPeriod = properties->getPropertyAsIntWithDefault( - propertyPrefix + ".CheckpointPeriod", 120); - Int kbyte = properties->getPropertyAsIntWithDefault(propertyPrefix + ".PeriodicCheckpointMinSize", 0); - - if(checkpointPeriod > 0) - { - _thread = new CheckpointThread(*this, Time::seconds(checkpointPeriod), kbyte, _trace); - } + + // + // Deadlock detection + // + _env->set_lk_detect(DB_LOCK_YOUNGEST); + + u_int32_t flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN; + + if(properties->getPropertyAsInt(propertyPrefix + ".DbRecoverFatal") != 0) + { + flags |= DB_RECOVER_FATAL | DB_CREATE; + } + else + { + flags |= DB_RECOVER | DB_CREATE; + } + + if(properties->getPropertyAsIntWithDefault(propertyPrefix + ".DbPrivate", 1) != 0) + { + flags |= DB_PRIVATE; + } + + /* + + // + // Does not seem to work reliably in 4.1.25 + // + + time_t timeStamp = properties->getPropertyAsIntWithDefault(propertyPrefix + ".TxTimestamp", 0); + + if(timeStamp != 0) + { + try + { + set_tx_timestamp(&timeStamp); + } + catch(const ::DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + } + */ + + // + // Maybe we can deprecate this property since it can be set in the DB_CONFIG file + // + bool autoDelete = (properties->getPropertyAsIntWithDefault( + propertyPrefix + ".OldLogsAutoDelete", 1) != 0); + + if(autoDelete) + { + _env->set_flags(DB_LOG_AUTOREMOVE, 1); + } + + // + // Threading + // + flags |= DB_THREAD; + + string dbHome = properties->getPropertyWithDefault( + propertyPrefix + ".DbHome", envName); + + _env->open(dbHome.c_str(), flags, FREEZE_DB_MODE); + } + catch(const ::DbException& dx) + { + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; + } + + // + // Default checkpoint period is every 120 seconds + // + Int checkpointPeriod = properties->getPropertyAsIntWithDefault( + propertyPrefix + ".CheckpointPeriod", 120); + Int kbyte = properties->getPropertyAsIntWithDefault(propertyPrefix + ".PeriodicCheckpointMinSize", 0); + + if(checkpointPeriod > 0) + { + _thread = new CheckpointThread(*this, Time::seconds(checkpointPeriod), kbyte, _trace); + } } // @@ -361,9 +361,9 @@ void Freeze::CheckpointThread::terminate() { { - Lock sync(*this); - _done = true; - notify(); + Lock sync(*this); + _done = true; + notify(); } getThreadControl().join(); @@ -375,33 +375,33 @@ Freeze::CheckpointThread::run() { for(;;) { - { - Lock sync(*this); - while(!_done && timedWait(_checkpointPeriod)) - { - // - // Loop - // - } - if(_done) - { - return; - } - } - - try - { - if(_trace >= 2) - { - Trace out(_dbEnv.getCommunicator()->getLogger(), "Freeze.DbEnv"); - out << "checkpointing environment \"" << _dbEnv.getEnvName() << "\""; - } - _dbEnv.getEnv()->txn_checkpoint(_kbyte, 0, 0); - } - catch(const DbException& dx) - { - Warning out(_dbEnv.getCommunicator()->getLogger()); - out << "checkpoint on DbEnv \"" << _dbEnv.getEnvName() << "\" raised DbException: " << dx.what(); - } + { + Lock sync(*this); + while(!_done && timedWait(_checkpointPeriod)) + { + // + // Loop + // + } + if(_done) + { + return; + } + } + + try + { + if(_trace >= 2) + { + Trace out(_dbEnv.getCommunicator()->getLogger(), "Freeze.DbEnv"); + out << "checkpointing environment \"" << _dbEnv.getEnvName() << "\""; + } + _dbEnv.getEnv()->txn_checkpoint(_kbyte, 0, 0); + } + catch(const DbException& dx) + { + Warning out(_dbEnv.getCommunicator()->getLogger()); + out << "checkpoint on DbEnv \"" << _dbEnv.getEnvName() << "\" raised DbException: " << dx.what(); + } } } diff --git a/cpp/src/Freeze/TransactionHolder.cpp b/cpp/src/Freeze/TransactionHolder.cpp index e10c402d10e..04d87729abb 100644 --- a/cpp/src/Freeze/TransactionHolder.cpp +++ b/cpp/src/Freeze/TransactionHolder.cpp @@ -16,7 +16,7 @@ Freeze::TransactionHolder::TransactionHolder(const ConnectionPtr& connection) { if(connection->currentTransaction() == 0) { - _transaction = connection->beginTransaction(); + _transaction = connection->beginTransaction(); } } @@ -24,13 +24,13 @@ Freeze::TransactionHolder::~TransactionHolder() { try { - rollback(); + rollback(); } catch(...) { - // - // Ignored to avoid crash during stack unwinding - // + // + // Ignored to avoid crash during stack unwinding + // } } @@ -39,16 +39,16 @@ Freeze::TransactionHolder::commit() { if(_transaction != 0) { - try - { - _transaction->commit(); - _transaction = 0; - } - catch(...) - { - _transaction = 0; - throw; - } + try + { + _transaction->commit(); + _transaction = 0; + } + catch(...) + { + _transaction = 0; + throw; + } } } @@ -57,15 +57,15 @@ Freeze::TransactionHolder::rollback() { if(_transaction != 0) { - try - { - _transaction->rollback(); - _transaction = 0; - } - catch(...) - { - _transaction = 0; - throw; - } + try + { + _transaction->rollback(); + _transaction = 0; + } + catch(...) + { + _transaction = 0; + throw; + } } } diff --git a/cpp/src/Freeze/TransactionI.cpp b/cpp/src/Freeze/TransactionI.cpp index ae8c4c7e2e4..c9920c7bfc0 100644 --- a/cpp/src/Freeze/TransactionI.cpp +++ b/cpp/src/Freeze/TransactionI.cpp @@ -30,46 +30,46 @@ Freeze::TransactionI::commit() long txnId = 0; try { - _connection->closeAllIterators(); + _connection->closeAllIterators(); - if(_txTrace >= 1) - { - txnId = (_txn->id() & 0x7FFFFFFF) + 0x80000000L; - } + if(_txTrace >= 1) + { + txnId = (_txn->id() & 0x7FFFFFFF) + 0x80000000L; + } - _txn->commit(0); + _txn->commit(0); - if(_txTrace >= 1) - { - Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); - out << "committed transaction " << hex << txnId << dec; - } + if(_txTrace >= 1) + { + Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); + out << "committed transaction " << hex << txnId << dec; + } } catch(const ::DbDeadlockException& dx) { - if(_txTrace >= 1) - { - Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); - out << "failed to commit transaction " << hex << txnId << dec << ": " << dx.what(); - } - - cleanup(); - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + if(_txTrace >= 1) + { + Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); + out << "failed to commit transaction " << hex << txnId << dec << ": " << dx.what(); + } + + cleanup(); + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } catch(const ::DbException& dx) { - if(_txTrace >= 1) - { - Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); - out << "failed to commit transaction " << hex << txnId << dec << ": " << dx.what(); - } - - cleanup(); - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + if(_txTrace >= 1) + { + Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); + out << "failed to commit transaction " << hex << txnId << dec << ": " << dx.what(); + } + + cleanup(); + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } cleanup(); } @@ -82,46 +82,46 @@ Freeze::TransactionI::rollback() long txnId = 0; try { - _connection->closeAllIterators(); + _connection->closeAllIterators(); - if(_txTrace >= 1) - { - txnId = (_txn->id() & 0x7FFFFFFF) + 0x80000000L; - } + if(_txTrace >= 1) + { + txnId = (_txn->id() & 0x7FFFFFFF) + 0x80000000L; + } - _txn->abort(); + _txn->abort(); - if(_txTrace >= 1) - { - Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); - out << "rolled back transaction " << hex << txnId << dec; - } + if(_txTrace >= 1) + { + Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); + out << "rolled back transaction " << hex << txnId << dec; + } } catch(const ::DbDeadlockException& dx) { - if(_txTrace >= 1) - { - Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); - out << "failed to rollback transaction " << hex << txnId << dec << ": " << dx.what(); - } - - cleanup(); - DeadlockException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + if(_txTrace >= 1) + { + Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); + out << "failed to rollback transaction " << hex << txnId << dec << ": " << dx.what(); + } + + cleanup(); + DeadlockException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } catch(const ::DbException& dx) { - if(_txTrace >= 1) - { - Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); - out << "failed to rollback transaction " << hex << txnId << dec << ": " << dx.what(); - } - - cleanup(); - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + if(_txTrace >= 1) + { + Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); + out << "failed to rollback transaction " << hex << txnId << dec << ": " << dx.what(); + } + + cleanup(); + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } cleanup(); } @@ -133,26 +133,26 @@ Freeze::TransactionI::TransactionI(ConnectionI* connection) : { try { - _connection->dbEnv()->getEnv()->txn_begin(0, &_txn, 0); - - if(_txTrace >= 1) - { - long txnId = (_txn->id() & 0x7FFFFFFF) + 0x80000000L; - Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); - out << "started transaction " << hex << txnId << dec; - } + _connection->dbEnv()->getEnv()->txn_begin(0, &_txn, 0); + + if(_txTrace >= 1) + { + long txnId = (_txn->id() & 0x7FFFFFFF) + 0x80000000L; + Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); + out << "started transaction " << hex << txnId << dec; + } } catch(const ::DbException& dx) { - if(_txTrace >= 1) - { - Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); - out << "failed to start transaction: " << dx.what(); - } - - DatabaseException ex(__FILE__, __LINE__); - ex.message = dx.what(); - throw ex; + if(_txTrace >= 1) + { + Trace out(_connection->communicator()->getLogger(), "Freeze.Map"); + out << "failed to start transaction: " << dx.what(); + } + + DatabaseException ex(__FILE__, __LINE__); + ex.message = dx.what(); + throw ex; } } @@ -160,7 +160,7 @@ Freeze::TransactionI::~TransactionI() { if(_txn != 0) { - rollback(); + rollback(); } } diff --git a/cpp/src/Freeze/TransactionI.h b/cpp/src/Freeze/TransactionI.h index 275f8b429f4..72cfc852d16 100644 --- a/cpp/src/Freeze/TransactionI.h +++ b/cpp/src/Freeze/TransactionI.h @@ -36,7 +36,7 @@ public: DbTxn* dbTxn() const { - return _txn; + return _txn; } private: diff --git a/cpp/src/Freeze/Util.cpp b/cpp/src/Freeze/Util.cpp index c29f3845f9e..20782bfeca4 100644 --- a/cpp/src/Freeze/Util.cpp +++ b/cpp/src/Freeze/Util.cpp @@ -16,75 +16,75 @@ using namespace std; void Freeze::handleDbException(const DbException& dx, - const char* file, int line) + const char* file, int line) { throw DatabaseException(file, line, dx.what()); } void Freeze::handleDbException(const DbException& dx, - Key& key, Dbt& dbKey, - const char* file, int line) + Key& key, Dbt& dbKey, + const char* file, int line) { bool bufferSmallException = #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 2) - (dx.get_errno() == ENOMEM); + (dx.get_errno() == ENOMEM); #else (dx.get_errno() == DB_BUFFER_SMALL || dx.get_errno() == ENOMEM); -#endif - +#endif + if(bufferSmallException && (dbKey.get_size() > dbKey.get_ulen())) { - // - // Keep the old key size in case it's used as input - // - size_t oldKeySize = key.size(); + // + // Keep the old key size in case it's used as input + // + size_t oldKeySize = key.size(); - key.resize(dbKey.get_size()); - initializeOutDbt(key, dbKey); - dbKey.set_size(static_cast<u_int32_t>(oldKeySize)); + key.resize(dbKey.get_size()); + initializeOutDbt(key, dbKey); + dbKey.set_size(static_cast<u_int32_t>(oldKeySize)); } else { - handleDbException(dx, file, line); + handleDbException(dx, file, line); } } void Freeze::handleDbException(const DbException& dx, - Key& key, Dbt& dbKey, - Value& value, Dbt& dbValue, - const char* file, int line) + Key& key, Dbt& dbKey, + Value& value, Dbt& dbValue, + const char* file, int line) { bool bufferSmallException = #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 2) - (dx.get_errno() == ENOMEM); + (dx.get_errno() == ENOMEM); #else (dx.get_errno() == DB_BUFFER_SMALL || dx.get_errno() == ENOMEM); -#endif +#endif - bool resized = false; + bool resized = false; if(bufferSmallException) { - if(dbKey.get_size() > dbKey.get_ulen()) - { - size_t oldKeySize = key.size(); - key.resize(dbKey.get_size()); - initializeOutDbt(key, dbKey); - dbKey.set_size(static_cast<u_int32_t>(oldKeySize)); - resized = true; - } - - if(dbValue.get_size() > dbValue.get_ulen()) - { - value.resize(dbValue.get_size()); - initializeOutDbt(value, dbValue); - resized = true; - } + if(dbKey.get_size() > dbKey.get_ulen()) + { + size_t oldKeySize = key.size(); + key.resize(dbKey.get_size()); + initializeOutDbt(key, dbKey); + dbKey.set_size(static_cast<u_int32_t>(oldKeySize)); + resized = true; + } + + if(dbValue.get_size() > dbValue.get_ulen()) + { + value.resize(dbValue.get_size()); + initializeOutDbt(value, dbValue); + resized = true; + } } if(!resized) { - handleDbException(dx, file, line); + handleDbException(dx, file, line); } } diff --git a/cpp/src/Freeze/Util.h b/cpp/src/Freeze/Util.h index b41815f2c94..52582471517 100644 --- a/cpp/src/Freeze/Util.h +++ b/cpp/src/Freeze/Util.h @@ -61,11 +61,11 @@ handleDbException(const DbException&, const char*, int); void handleDbException(const DbException&, Key&, Dbt&, - const char*, int); + const char*, int); void handleDbException(const DbException&, Key&, Dbt&, Value&, Dbt&, - const char*, int); + const char*, int); } |