From b62b66a24cefb13c682f9669932b32e1e83cae5c Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 29 Sep 2016 20:55:16 +0200 Subject: [PATCH] replace std::vector for std::set to represent _hashes_to_remove, so as to avoid duplicates (suggested by Jo) --- libretroshare/src/turtle/p3turtle.cc | 29 ++++++++++++---------------- libretroshare/src/turtle/p3turtle.h | 2 +- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/libretroshare/src/turtle/p3turtle.cc b/libretroshare/src/turtle/p3turtle.cc index 25d1972a5..2044b52ac 100644 --- a/libretroshare/src/turtle/p3turtle.cc +++ b/libretroshare/src/turtle/p3turtle.cc @@ -430,21 +430,21 @@ void p3turtle::autoWash() { RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/ - for(unsigned int i=0;i<_hashes_to_remove.size();++i) + for(std::set::const_iterator hit(_hashes_to_remove.begin());hit!=_hashes_to_remove.end();++hit) { - std::map::iterator it(_incoming_file_hashes.find(_hashes_to_remove[i])) ; + std::map::iterator it(_incoming_file_hashes.find(*hit)) ; if(it == _incoming_file_hashes.end()) { #ifdef P3TURTLE_DEBUG - std::cerr << "p3turtle: asked to stop monitoring file hash " << _hashes_to_remove[i] << ", but this hash is actually not handled by the turtle router." << std::endl ; + std::cerr << "p3turtle: asked to stop monitoring file hash " << *hit << ", but this hash is actually not handled by the turtle router." << std::endl ; #endif continue ; } // copy the list of tunnels to remove. #ifdef P3TURTLE_DEBUG - std::cerr << "p3turtle: stopping monitoring for file hash " << _hashes_to_remove[i] << ", and closing " << it->second.tunnels.size() << " tunnels (" ; + std::cerr << "p3turtle: stopping monitoring for file hash " << *hit << ", and closing " << it->second.tunnels.size() << " tunnels (" ; #endif std::vector tunnels_to_remove ; @@ -463,11 +463,8 @@ void p3turtle::autoWash() _incoming_file_hashes.erase(it) ; } - if(!_hashes_to_remove.empty()) - { - IndicateConfigChanged() ; // initiates saving of handled hashes. - _hashes_to_remove.clear() ; - } + + _hashes_to_remove.clear() ; } // look for tunnels and stored temporary info that have not been used for a while. @@ -657,7 +654,7 @@ void p3turtle::stopMonitoringTunnels(const RsFileHash& hash) std::cerr << "p3turtle: Marking hash " << hash << " to be removed during autowash." << std::endl ; #endif // We don't do the deletion in this process, because it can cause a race with tunnel management. - _hashes_to_remove.push_back(hash) ; + _hashes_to_remove.insert(hash) ; } // -----------------------------------------------------------------------------------// @@ -1855,15 +1852,13 @@ void p3turtle::monitorTunnels(const RsFileHash& hash,RsTurtleClientService *clie // First, check if the hash is tagged for removal (there's a delay) - for(uint32_t i=0;i<_hashes_to_remove.size();++i) - if(_hashes_to_remove[i] == hash) - { - _hashes_to_remove[i] = _hashes_to_remove.back() ; - _hashes_to_remove.pop_back() ; + if(_hashes_to_remove.find(hash) != _hashes_to_remove.end()) + { + _hashes_to_remove.erase(hash) ; #ifdef P3TURTLE_DEBUG - std::cerr << "p3turtle: File hash " << hash << " Was scheduled for removal. Canceling the removal." << std::endl ; + std::cerr << "p3turtle: File hash " << hash << " Was scheduled for removal. Canceling the removal." << std::endl ; #endif - } + } // Then, check if the hash is already there // diff --git a/libretroshare/src/turtle/p3turtle.h b/libretroshare/src/turtle/p3turtle.h index bfda7663e..ceb137e7f 100644 --- a/libretroshare/src/turtle/p3turtle.h +++ b/libretroshare/src/turtle/p3turtle.h @@ -409,7 +409,7 @@ class p3turtle: public p3Service, public RsTurtle, public p3Config std::map _virtual_peers ; /// Hashes marked to be deleted. - std::vector _hashes_to_remove ; + std::set _hashes_to_remove ; /// List of client services that have regitered. std::list _registered_services ;