From a2ff5d27c540d3b88a4e44bcd99c61cb4ad2aaff Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 26 Sep 2016 22:06:30 +0200 Subject: [PATCH] simplified p3FileList code a little bit: removed mFriendIndexTab and made locked_getFriendIndex() create the remote directories on demand --- libretroshare/src/file_sharing/p3filelists.cc | 97 ++++++++----------- libretroshare/src/file_sharing/p3filelists.h | 1 - 2 files changed, 41 insertions(+), 57 deletions(-) diff --git a/libretroshare/src/file_sharing/p3filelists.cc b/libretroshare/src/file_sharing/p3filelists.cc index c0f29bf7c..46f7dfde9 100644 --- a/libretroshare/src/file_sharing/p3filelists.cc +++ b/libretroshare/src/file_sharing/p3filelists.cc @@ -67,7 +67,7 @@ p3FileDatabase::p3FileDatabase(p3ServiceControl *mpeers) throw std::runtime_error("Cannot create base directory to store/access file sharing files.") ; // loads existing indexes for friends. Some might be already present here. - // + // mRemoteDirectories.clear() ; // we should load them! mOwnId = mpeers->getOwnId() ; @@ -76,7 +76,7 @@ p3FileDatabase::p3FileDatabase(p3ServiceControl *mpeers) mLocalDirWatcher = new LocalDirectoryUpdater(mHashCache,mLocalSharedDirs) ; - mUpdateFlags = P3FILELISTS_UPDATE_FLAG_NOTHING_CHANGED ; + mUpdateFlags = P3FILELISTS_UPDATE_FLAG_NOTHING_CHANGED ; mLastRemoteDirSweepTS = 0 ; mLastCleanupTime = 0 ; @@ -154,16 +154,16 @@ RsServiceInfo p3FileDatabase::getServiceInfo() } int p3FileDatabase::tick() { - // tick the input/output list of update items and process them - // - tickRecv() ; - tickSend() ; + // tick the input/output list of update items and process them + // + tickRecv() ; + tickSend() ; time_t now = time(NULL) ; // cleanup - // - remove/delete shared file lists for people who are not friend anymore - // - + // - remove/delete shared file lists for people who are not friend anymore + // - if(mLastCleanupTime + 5 < now) { @@ -176,7 +176,7 @@ int p3FileDatabase::tick() if(last_print_time + 20 < now) { RS_STACK_MUTEX(mFLSMtx) ; - + #ifdef DEBUG_FILE_HIERARCHY mLocalSharedDirs->print(); #endif @@ -439,10 +439,18 @@ void p3FileDatabase::cleanup() friend_set.erase(mRemoteDirectories[i]->peerId()); mFriendIndexMap.erase(mRemoteDirectories[i]->peerId()); - mFriendIndexTab[i].clear(); delete mRemoteDirectories[i]; mRemoteDirectories[i] = NULL ; + + // now, in order to avoid empty seats, just move the last one here, and update indexes + + mRemoteDirectories[i] = mRemoteDirectories.back(); + mRemoteDirectories.pop_back(); + + mFriendIndexMap[mRemoteDirectories[i]->peerId()] = i; + + mUpdateFlags |= P3FILELISTS_UPDATE_FLAG_REMOTE_DIRS_CHANGED ; } // look through the remaining list of friends, which are the ones for which no remoteDirectoryStorage class has been allocated. @@ -452,21 +460,6 @@ void p3FileDatabase::cleanup() // Check if a remote directory exists for that friend, possibly creating the index. uint32_t friend_index = locked_getFriendIndex(*it) ; - - if(mRemoteDirectories.size() > friend_index && mRemoteDirectories[friend_index] != NULL) - continue ; - -#ifdef DEBUG_P3FILELISTS - P3FILELISTS_DEBUG() << " adding missing remote dir entry for friend " << *it << ", with index " << friend_index << std::endl; -#endif - - if(mRemoteDirectories.size() <= friend_index) - mRemoteDirectories.resize(friend_index+1,NULL) ; - - mRemoteDirectories[friend_index] = new RemoteDirectoryStorage(*it,makeRemoteFileName(*it)); - - mUpdateFlags |= P3FILELISTS_UPDATE_FLAG_REMOTE_DIRS_CHANGED ; - mUpdateFlags |= P3FILELISTS_UPDATE_FLAG_REMOTE_MAP_CHANGED ; } // cancel existing requests for which the peer is offline @@ -511,8 +504,8 @@ uint32_t p3FileDatabase::locked_getFriendIndex(const RsPeerId& pid) { // allocate a new index for that friend, and tell that we should save. uint32_t found = 0 ; - for(uint32_t i=0;i= (1 << NB_FRIEND_INDEX_BITS) ) + if(mRemoteDirectories.size() >= (1 << NB_FRIEND_INDEX_BITS) ) { std::cerr << "(EE) FriendIndexTab is full. This is weird. Do you really have more than " << (1<second) - mRemoteDirectories.resize(it->second,NULL) ; + { + mRemoteDirectories.resize(it->second+1,NULL) ; + mRemoteDirectories[it->second] = new RemoteDirectoryStorage(pid,makeRemoteFileName(pid)); + + mUpdateFlags |= P3FILELISTS_UPDATE_FLAG_REMOTE_DIRS_CHANGED ; + mUpdateFlags |= P3FILELISTS_UPDATE_FLAG_REMOTE_MAP_CHANGED ; + +#ifdef DEBUG_P3FILELISTS + P3FILELISTS_DEBUG() << " adding missing remote dir entry for friend " << *it << ", with index " << friend_index << std::endl; +#endif + } return it->second; } } -const RsPeerId& p3FileDatabase::locked_getFriendFromIndex(uint32_t indx) const -{ - static const RsPeerId null_id ; - - if(indx >= mFriendIndexTab.size()) - return null_id ; - - if(mFriendIndexTab[indx].isNull()) - { - std::cerr << "(EE) null friend id requested from index " << indx << ": this is a bug, most likely" << std::endl; - return null_id ; - } - - return mFriendIndexTab[indx]; -} bool p3FileDatabase::convertPointerToEntryIndex(const void *p, EntryIndex& e, uint32_t& friend_index) { // trust me, I can do this ;-) @@ -1352,12 +1343,6 @@ void p3FileDatabase::handleDirSyncResponse(RsFileListsSyncResponseItem *sitem) // make sure we have a remote directory for that friend. - if(mRemoteDirectories.size() <= fi) - mRemoteDirectories.resize(fi+1,NULL) ; - - if(mRemoteDirectories[fi] == NULL) - mRemoteDirectories[fi] = new RemoteDirectoryStorage(item->PeerId(),makeRemoteFileName(item->PeerId())); - if(!mRemoteDirectories[fi]->getIndexFromDirHash(item->entry_hash,entry_index)) { std::cerr << std::endl; diff --git a/libretroshare/src/file_sharing/p3filelists.h b/libretroshare/src/file_sharing/p3filelists.h index 67834bfa7..3b155a109 100644 --- a/libretroshare/src/file_sharing/p3filelists.h +++ b/libretroshare/src/file_sharing/p3filelists.h @@ -196,7 +196,6 @@ class p3FileDatabase: public p3Service, public p3Config, public ftSearch //, pub static bool convertEntryIndexToPointer(const EntryIndex &e, uint32_t friend_index, void *& p); static bool convertPointerToEntryIndex(const void *p, EntryIndex& e, uint32_t& friend_index) ; uint32_t locked_getFriendIndex(const RsPeerId& pid); - const RsPeerId& locked_getFriendFromIndex(uint32_t indx) const; void handleDirSyncRequest (RsFileListsSyncRequestItem *) ; void handleDirSyncResponse (RsFileListsSyncResponseItem *) ;