diff --git a/libretroshare/src/gxs/rsdataservice.cc b/libretroshare/src/gxs/rsdataservice.cc index 535aa3035..5add7dabc 100644 --- a/libretroshare/src/gxs/rsdataservice.cc +++ b/libretroshare/src/gxs/rsdataservice.cc @@ -486,7 +486,7 @@ bool RsDataService::finishReleaseUpdate(int release, bool result) return result; } -RsGxsGrpMetaData* RsDataService::locked_getGrpMeta(RetroCursor &c, int colOffset) +RsGxsGrpMetaData* RsDataService::locked_getNewGrpMeta(RetroCursor &c, int colOffset) { #ifdef RS_DATA_SERVICE_DEBUG std::cerr << "RsDataService::locked_getGrpMeta()"; @@ -556,9 +556,10 @@ RsGxsGrpMetaData* RsDataService::locked_getGrpMeta(RetroCursor &c, int colOffset if(ok) return grpMeta; else + { delete grpMeta; - - return NULL; + return NULL; + } } RsNxsGrp* RsDataService::locked_getGroup(RetroCursor &c) @@ -894,8 +895,14 @@ int RsDataService::storeGroup(const std::list& grp) void RsDataService::locked_clearGrpMetaCache(const RsGxsGroupId& gid) { - mGrpMetaDataCache.erase(gid) ; // cleans existing cache entry - mGrpMetaDataCache_ContainsAllDatabase = false; + std::map::iterator it = mGrpMetaDataCache.find(gid) ; + + if(it != mGrpMetaDataCache.end()) + { + delete it->second ; + mGrpMetaDataCache.erase(it) ; + mGrpMetaDataCache_ContainsAllDatabase = false; + } } int RsDataService::updateGroup(const std::list &grp) @@ -1097,7 +1104,7 @@ void RsDataService::locked_retrieveGroups(RetroCursor* c, std::vector if(g) { if (metaOffset) { - g->metaData = locked_getGrpMeta(*c, metaOffset); + g->metaData = locked_getNewGrpMeta(*c, metaOffset); } grps.push_back(g); } @@ -1274,7 +1281,7 @@ void RsDataService::locked_retrieveMsgMeta(RetroCursor *c, std::vector& grp) +int RsDataService::retrieveGxsGrpMetaData(RsGxsGrpMetaTemporaryMap& grp) { #ifdef RS_DATA_SERVICE_DEBUG std::cerr << "RsDataService::retrieveGxsGrpMetaData()"; @@ -1297,100 +1304,110 @@ int RsDataService::retrieveGxsGrpMetaData(std::map::const_iterator it(mGrpMetaDataCache.begin());it!=mGrpMetaDataCache.end();++it) - grp[it->first] = new RsGxsGrpMetaData(it->second); + grp = mGrpMetaDataCache ; + //for(std::map::const_iterator it(mGrpMetaDataCache.begin());it!=mGrpMetaDataCache.end();++it) + // grp[it->first] = new RsGxsGrpMetaData(it->second); } else - { + { #ifdef RS_DATA_SERVICE_DEBUG - std::cerr << "RsDataService::retrieveGxsGrpMetaData() retrieving all" << std::endl; + std::cerr << "RsDataService::retrieveGxsGrpMetaData() retrieving all" << std::endl; #endif + // clear the cache - RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, mGrpMetaColumns, "", ""); + for(auto it(mGrpMetaDataCache.begin());it!=mGrpMetaDataCache.end();++it) + delete it->second ; - if(c) - { - bool valid = c->moveToFirst(); + mGrpMetaDataCache.clear(); - while(valid) - { - RsGxsGrpMetaData* g = locked_getGrpMeta(*c, 0); - if(g) - { - grp[g->mGroupId] = g; - mGrpMetaDataCache[g->mGroupId] = *g ; + RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, mGrpMetaColumns, "", ""); + + if(c) + { + bool valid = c->moveToFirst(); + + while(valid) + { + RsGxsGrpMetaData* g = locked_getNewGrpMeta(*c, 0); + + if(g) + { + grp[g->mGroupId] = g; + mGrpMetaDataCache[g->mGroupId] = g ; #ifdef RS_DATA_SERVICE_DEBUG_CACHE - std::cerr << (void *)this << ": Retrieving (all) Grp metadata grpId=" << g->mGroupId << std::endl; + std::cerr << (void *)this << ": Retrieving (all) Grp metadata grpId=" << g->mGroupId << std::endl; #endif - } - valid = c->moveToNext(); + } + valid = c->moveToNext(); #ifdef RS_DATA_SERVICE_DEBUG_TIME - ++resultCount; + ++resultCount; #endif - } - delete c; - } + } + delete c; + } + + mGrpMetaDataCache_ContainsAllDatabase = true ; + } - mGrpMetaDataCache_ContainsAllDatabase = true ; } - - }else + else { - std::map::iterator mit = grp.begin(); + std::map::iterator mit = grp.begin(); for(; mit != grp.end(); ++mit) { - std::map::const_iterator itt = mGrpMetaDataCache.find(mit->first) ; + std::map::const_iterator itt = mGrpMetaDataCache.find(mit->first) ; + if(itt != mGrpMetaDataCache.end()) { #ifdef RS_DATA_SERVICE_DEBUG_CACHE std::cerr << "Retrieving Grp metadata grpId=" << mit->first << " from cache!" << std::endl; #endif - grp[mit->first] = new RsGxsGrpMetaData(itt->second) ; + grp[mit->first] = itt->second ; } else - { + { #ifdef RS_DATA_SERVICE_DEBUG_CACHE - std::cerr << "Retrieving Grp metadata grpId=" << mit->first ; + std::cerr << "Retrieving Grp metadata grpId=" << mit->first ; #endif - const RsGxsGroupId& grpId = mit->first; - RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, mGrpMetaColumns, "grpId='" + grpId.toStdString() + "'", ""); + const RsGxsGroupId& grpId = mit->first; + RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, mGrpMetaColumns, "grpId='" + grpId.toStdString() + "'", ""); - if(c) - { - bool valid = c->moveToFirst(); + if(c) + { + bool valid = c->moveToFirst(); #ifdef RS_DATA_SERVICE_DEBUG_CACHE - if(!valid) - std::cerr << " Empty query! GrpId " << grpId << " is not in database" << std::endl; + if(!valid) + std::cerr << " Empty query! GrpId " << grpId << " is not in database" << std::endl; #endif - while(valid) - { - RsGxsGrpMetaData* g = locked_getGrpMeta(*c, 0); + while(valid) + { + RsGxsGrpMetaData* g = locked_getNewGrpMeta(*c, 0); - if(g) - { - grp[g->mGroupId] = g; - mGrpMetaDataCache[g->mGroupId] = *g ; + if(g) + { + grp[g->mGroupId] = g; + mGrpMetaDataCache[g->mGroupId] = g ; #ifdef RS_DATA_SERVICE_DEBUG_CACHE - std::cerr << ". Got it. Updating cache." << std::endl; + std::cerr << ". Got it. Updating cache." << std::endl; #endif - } - valid = c->moveToNext(); + } + valid = c->moveToNext(); #ifdef RS_DATA_SERVICE_DEBUG_TIME - ++resultCount; + ++resultCount; #endif - } - delete c; - } + } + delete c; + } #ifdef RS_DATA_SERVICE_DEBUG_CACHE - else - std::cerr << ". not found!" << std::endl; + else + std::cerr << ". not found!" << std::endl; #endif - } + } } } @@ -1607,7 +1624,7 @@ bool RsDataService::locked_removeGroupEntries(const std::vector& g mDb->sqlDelete(GRP_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", ""); // also remove the group meta from cache. - mGrpMetaDataCache.erase(*vit) ; + locked_clearGrpMetaCache(*vit) ; } ret &= mDb->commitTransaction(); diff --git a/libretroshare/src/gxs/rsdataservice.h b/libretroshare/src/gxs/rsdataservice.h index 1e33747d6..ed5bbe9f0 100644 --- a/libretroshare/src/gxs/rsdataservice.h +++ b/libretroshare/src/gxs/rsdataservice.h @@ -71,7 +71,7 @@ public: * @param cache whether to store retrieval in mem for faster later retrieval * @return error code */ - int retrieveGxsGrpMetaData(std::map& grp); + int retrieveGxsGrpMetaData(RsGxsGrpMetaTemporaryMap& grp); /*! * Retrieves meta data of all groups stored (most current versions only) @@ -208,7 +208,7 @@ private: * extracts a grp meta item from a cursor at its * current position */ - RsGxsGrpMetaData* locked_getGrpMeta(RetroCursor& c, int colOffset); + RsGxsGrpMetaData* locked_getNewGrpMeta(RetroCursor& c, int colOffset); /*! * extracts a msg item from a cursor at its @@ -347,7 +347,7 @@ private: void locked_clearGrpMetaCache(const RsGxsGroupId& gid); - std::map mGrpMetaDataCache ; + std::map mGrpMetaDataCache ; bool mGrpMetaDataCache_ContainsAllDatabase ; }; diff --git a/libretroshare/src/gxs/rsgds.h b/libretroshare/src/gxs/rsgds.h index 33ed386c4..8af9a53a2 100644 --- a/libretroshare/src/gxs/rsgds.h +++ b/libretroshare/src/gxs/rsgds.h @@ -36,9 +36,9 @@ #include "rsitems/rsnxsitems.h" #include "gxs/rsgxsdata.h" #include "rsgxs.h" +#include "rsgxsutil.h" #include "util/contentvalue.h" - class RsGxsSearchModule { public: @@ -61,6 +61,8 @@ public: ContentValue val; }; +typedef std::map RsGxsGrpMetaTemporaryMap; + /*! * This allows modification of local * meta data items of a group @@ -168,7 +170,7 @@ public: * , if grpId is failed to be retrieved it will be erased from map * @return error code */ - virtual int retrieveGxsGrpMetaData(std::map& grp) = 0; + virtual int retrieveGxsGrpMetaData(RsGxsGrpMetaTemporaryMap& grp) = 0; /*! * Retrieves meta data of all groups stored (most current versions only) diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 0c32bab92..97fa36f5b 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -533,7 +533,7 @@ int RsGenExchange::createGroupSignatures(RsTlvKeySignatureSet& signSet, RsTlvBin } int RsGenExchange::createMsgSignatures(RsTlvKeySignatureSet& signSet, RsTlvBinaryData& msgData, - const RsGxsMsgMetaData& msgMeta, RsGxsGrpMetaData& grpMeta) + const RsGxsMsgMetaData& msgMeta, const RsGxsGrpMetaData& grpMeta) { bool needPublishSign = false, needIdentitySign = false; uint32_t grpFlag = grpMeta.mGroupFlags; @@ -714,8 +714,7 @@ int RsGenExchange::createMessage(RsNxsMsg* msg) #ifdef GEN_EXCH_DEBUG std::cerr << "RsGenExchange::createMessage() " << std::endl; #endif - std::map metaMap; - + RsGxsGrpMetaTemporaryMap metaMap ; metaMap.insert(std::make_pair(id, (RsGxsGrpMetaData*)(NULL))); mDataStore->retrieveGxsGrpMetaData(metaMap); @@ -730,7 +729,7 @@ int RsGenExchange::createMessage(RsNxsMsg* msg) else { // get publish key - RsGxsGrpMetaData* grpMeta = metaMap[id]; + const RsGxsGrpMetaData* grpMeta = metaMap[id]; uint32_t metaDataLen = meta.serial_size(); uint32_t allMsgDataLen = metaDataLen + msg->msg.bin_len; @@ -763,8 +762,6 @@ int RsGenExchange::createMessage(RsNxsMsg* msg) delete[] metaData; delete[] allMsgData; - - delete grpMeta; } if(ret_val == SIGN_FAIL) @@ -1196,14 +1193,14 @@ bool RsGenExchange::getMsgRelatedList(const uint32_t &token, MsgRelatedIdResult bool RsGenExchange::getGroupMeta(const uint32_t &token, std::list &groupInfo) { - std::list metaL; + std::list metaL; bool ok = mDataAccess->getGroupSummary(token, metaL); RsGroupMetaData m; - for( std::list::iterator lit = metaL.begin(); lit != metaL.end(); ++lit) + for( auto lit = metaL.begin(); lit != metaL.end(); ++lit) { - RsGxsGrpMetaData& gMeta = *(*lit); + const RsGxsGrpMetaData& gMeta = *(*lit); m = gMeta; RsGroupNetworkStats sts ; @@ -1223,7 +1220,6 @@ bool RsGenExchange::getGroupMeta(const uint32_t &token, std::list grpMetaMap; - std::map::iterator mit; + RsGxsGrpMetaTemporaryMap grpMetaMap; + std::map::iterator mit; grpMetaMap.insert(std::make_pair(grpId, (RsGxsGrpMetaData*)(NULL))); mDataStore->retrieveGxsGrpMetaData(grpMetaMap); if((mit = grpMetaMap.find(grpId)) != grpMetaMap.end()) { - grpMeta = mit->second; + const RsGxsGrpMetaData *grpMeta = mit->second; if (!grpMeta) { #ifdef GEN_EXCH_DEBUG @@ -2112,12 +2108,9 @@ bool RsGenExchange::processGrpMask(const RsGxsGroupId& grpId, ContentValue &grpC { key = RsGeneralDataService::GRP_META_SUBSCRIBE_FLAG; currValue = grpMeta->mSubscribeFlags; - }else - { - if(grpMeta) - delete grpMeta; - return !(grpCv.empty()); } + else + return !(grpCv.empty()); ok &= grpCv.getAsInt32(key+GXS_MASK, mask); @@ -2129,9 +2122,6 @@ bool RsGenExchange::processGrpMask(const RsGxsGroupId& grpId, ContentValue &grpC grpCv.put(key, value); - if(grpMeta) - delete grpMeta; - return ok; } @@ -2350,7 +2340,7 @@ void RsGenExchange::processGroupUpdatePublish() // get keys for group update publish // first build meta request map for groups to be updated - std::map grpMeta; + RsGxsGrpMetaTemporaryMap grpMeta; std::vector::iterator vit = mGroupUpdatePublish.begin(); for(; vit != mGroupUpdatePublish.end(); ++vit) @@ -2360,8 +2350,8 @@ void RsGenExchange::processGroupUpdatePublish() grpMeta.insert(std::make_pair(groupId, (RsGxsGrpMetaData*)(NULL))); } - if(grpMeta.empty()) - return; + if(grpMeta.empty()) + return; mDataStore->retrieveGxsGrpMetaData(grpMeta); @@ -2371,20 +2361,18 @@ void RsGenExchange::processGroupUpdatePublish() { GroupUpdatePublish& gup = *vit; const RsGxsGroupId& groupId = gup.grpItem->meta.mGroupId; - std::map::iterator mit = grpMeta.find(groupId); + std::map::iterator mit = grpMeta.find(groupId); - RsGxsGrpMetaData* meta = NULL; + const RsGxsGrpMetaData* meta = NULL; if(mit == grpMeta.end() || mit->second == NULL) { std::cerr << "Error! could not find meta of old group to update!" << std::endl; mDataAccess->updatePublicRequestStatus(gup.mToken, RsTokenService::GXS_REQUEST_V2_STATUS_FAILED); delete gup.grpItem; continue; - }else - { - meta = mit->second; } - + else + meta = mit->second; //gup.grpItem->meta = *meta; GxsGrpPendingSign ggps(gup.grpItem, gup.mToken); @@ -2402,14 +2390,13 @@ void RsGenExchange::processGroupUpdatePublish() ggps.mToken = gup.mToken; mGrpsToPublish.push_back(ggps); } - else + else { std::cerr << "(EE) publish group fails because RS cannot find the private publish and author keys" << std::endl; delete gup.grpItem; mDataAccess->updatePublicRequestStatus(gup.mToken, RsTokenService::GXS_REQUEST_V2_STATUS_FAILED); } - delete meta; } mGroupUpdatePublish.clear(); @@ -2814,23 +2801,20 @@ bool RsGenExchange::getGroupKeys(const RsGxsGroupId &grpId, RsTlvSecurityKeySet RS_STACK_MUTEX(mGenMtx) ; - std::map grpMeta; + RsGxsGrpMetaTemporaryMap grpMeta; grpMeta[grpId] = NULL; mDataStore->retrieveGxsGrpMetaData(grpMeta); if(grpMeta.empty()) return false; - RsGxsGrpMetaData* meta = grpMeta[grpId]; + const RsGxsGrpMetaData* meta = grpMeta[grpId]; if(meta == NULL) return false; keySet = meta->keys; - GxsSecurity::createPublicKeysFromPrivateKeys(keySet) ; - - for(std::map::iterator it=grpMeta.begin();it!=grpMeta.end();++it) - delete it->second ; + GxsSecurity::createPublicKeysFromPrivateKeys(keySet) ; return true; } @@ -2944,7 +2928,7 @@ void RsGenExchange::processRecvdMessages() #ifdef GEN_EXCH_DEBUG std::cerr << " deserialised info: grp id=" << meta->mGroupId << ", msg id=" << meta->mMsgId ; #endif - std::map::iterator mit = grpMetas.find(msg->grpId); + std::map::iterator mit = grpMetas.find(msg->grpId); #ifdef GEN_EXCH_DEBUG std::cerr << " msg info : grp id=" << msg->grpId << ", msg id=" << msg->msgId << std::endl; @@ -2958,11 +2942,12 @@ void RsGenExchange::processRecvdMessages() continue ; } - RsGxsGrpMetaData *grpMeta = mit->second; + const RsGxsGrpMetaData *grpMeta = mit->second; + RsTlvSecurityKeySet keys = grpMeta->keys ; - GxsSecurity::createPublicKeysFromPrivateKeys(grpMeta->keys); // make sure we have the public keys that correspond to the private ones, as it happens. Most of the time this call does nothing. + GxsSecurity::createPublicKeysFromPrivateKeys(keys); // make sure we have the public keys that correspond to the private ones, as it happens. Most of the time this call does nothing. - int validateReturn = validateMsg(msg, grpMeta->mGroupFlags, grpMeta->mSignFlags, grpMeta->keys); + int validateReturn = validateMsg(msg, grpMeta->mGroupFlags, grpMeta->mSignFlags, keys); #ifdef GEN_EXCH_DEBUG std::cerr << " grpMeta.mSignFlags: " << std::hex << grpMeta->mSignFlags << std::dec << std::endl; @@ -3176,7 +3161,7 @@ void RsGenExchange::processRecvdGroups() void RsGenExchange::performUpdateValidation() { - RS_STACK_MUTEX(mGenMtx) ; + RS_STACK_MUTEX(mGenMtx) ; if(mGroupUpdates.empty()) return; @@ -3185,7 +3170,7 @@ void RsGenExchange::performUpdateValidation() std::cerr << "RsGenExchange::performUpdateValidation() " << std::endl; #endif - std::map grpMetas; + RsGxsGrpMetaTemporaryMap grpMetas; std::vector::iterator vit = mGroupUpdates.begin(); for(; vit != mGroupUpdates.end(); ++vit) @@ -3200,8 +3185,7 @@ void RsGenExchange::performUpdateValidation() for(; vit != mGroupUpdates.end(); ++vit) { GroupUpdate& gu = *vit; - std::map::iterator mit = - grpMetas.find(gu.newGrp->grpId); + std::map::iterator mit = grpMetas.find(gu.newGrp->grpId); gu.oldGrpMeta = mit->second; gu.validUpdate = updateValid(*(gu.oldGrpMeta), *(gu.newGrp)); } @@ -3223,51 +3207,50 @@ void RsGenExchange::performUpdateValidation() if(gu.newGrp->metaData->mCircleType == GXS_CIRCLE_TYPE_YOUR_FRIENDS_ONLY) gu.newGrp->metaData->mOriginator = gu.newGrp->PeerId(); - // Keep subscriptionflag to what it was. This avoids clearing off the flag when updates to group meta information - // is received. - - gu.newGrp->metaData->mSubscribeFlags = gu.oldGrpMeta->mSubscribeFlags ; - + // Keep subscriptionflag to what it was. This avoids clearing off the flag when updates to group meta information + // is received. + + gu.newGrp->metaData->mSubscribeFlags = gu.oldGrpMeta->mSubscribeFlags ; + grps.push_back(gu.newGrp); } else { delete gu.newGrp; - gu.newGrp = NULL ; + gu.newGrp = NULL ; } - delete gu.oldGrpMeta; - gu.oldGrpMeta = NULL ; + gu.oldGrpMeta = NULL ; } - // notify the client - - RsGxsGroupChange* c = new RsGxsGroupChange(RsGxsNotify::TYPE_RECEIVE, true); - - for(uint32_t i=0;imGrpIdList.push_back(mGroupUpdates[i].newGrp->grpId) ; + // notify the client + + RsGxsGroupChange* c = new RsGxsGroupChange(RsGxsNotify::TYPE_RECEIVE, true); + + for(uint32_t i=0;imGrpIdList.push_back(mGroupUpdates[i].newGrp->grpId) ; #ifdef GEN_EXCH_DEBUG - std::cerr << " " << mGroupUpdates[i].newGrp->grpId << std::endl; + std::cerr << " " << mGroupUpdates[i].newGrp->grpId << std::endl; #endif - } - - mNotifications.push_back(c); - - // Warning: updateGroup will destroy the objects in grps. Dont use it afterwards! - + } + + mNotifications.push_back(c); + + // Warning: updateGroup will destroy the objects in grps. Dont use it afterwards! + mDataStore->updateGroup(grps); - + #ifdef GEN_EXCH_DEBUG - std::cerr << " adding the following grp ids to notification: " << std::endl; + std::cerr << " adding the following grp ids to notification: " << std::endl; #endif - - // cleanup - + + // cleanup + mGroupUpdates.clear(); } -bool RsGenExchange::updateValid(RsGxsGrpMetaData& oldGrpMeta, RsNxsGrp& newGrp) const +bool RsGenExchange::updateValid(const RsGxsGrpMetaData& oldGrpMeta, RsNxsGrp& newGrp) const { std::map& signSet = newGrp.metaData->signSet.keySignSet; std::map::iterator mit = signSet.find(INDEX_AUTHEN_ADMIN); @@ -3283,10 +3266,11 @@ bool RsGenExchange::updateValid(RsGxsGrpMetaData& oldGrpMeta, RsNxsGrp& newGrp) } RsTlvKeySignature adminSign = mit->second; + RsTlvSecurityKeySet old_keys = oldGrpMeta.keys ; - GxsSecurity::createPublicKeysFromPrivateKeys(oldGrpMeta.keys); // make sure we have the public keys that correspond to the private ones, as it happens. Most of the time this call does nothing. + GxsSecurity::createPublicKeysFromPrivateKeys(old_keys); // make sure we have the public keys that correspond to the private ones, as it happens. Most of the time this call does nothing. - std::map& keys = oldGrpMeta.keys.public_keys; + std::map& keys = old_keys.public_keys; std::map::iterator keyMit = keys.find(RsGxsId(oldGrpMeta.mGroupId)); if(keyMit == keys.end()) diff --git a/libretroshare/src/gxs/rsgenexchange.h b/libretroshare/src/gxs/rsgenexchange.h index febfc5ff7..94a455824 100644 --- a/libretroshare/src/gxs/rsgenexchange.h +++ b/libretroshare/src/gxs/rsgenexchange.h @@ -223,7 +223,7 @@ public: * @param groupInfo * @return false if could not redeem token */ - bool getGroupMeta(const uint32_t &token, std::list &groupInfo); + bool getGroupMeta(const uint32_t &token, std::list& groupInfo); /*! * retrieves message meta data associated to a request token @@ -765,7 +765,7 @@ private: * SIGN_FAIL_TRY_LATER for Id sign key not avail (but requested), try later */ int createMsgSignatures(RsTlvKeySignatureSet& signSet, RsTlvBinaryData& msgData, - const RsGxsMsgMetaData& msgMeta, RsGxsGrpMetaData& grpMeta); + const RsGxsMsgMetaData& msgMeta, const RsGxsGrpMetaData& grpMeta); /*! * convenience function to create sign for groups @@ -835,7 +835,7 @@ private: * @param newGrp the new group that updates the old group (must have meta data member initialised) * @return */ - bool updateValid(RsGxsGrpMetaData& oldGrp, RsNxsGrp& newGrp) const; + bool updateValid(const RsGxsGrpMetaData& oldGrp, RsNxsGrp& newGrp) const; /*! * convenience function for checking private publish and admin keys are present diff --git a/libretroshare/src/gxs/rsgxsdata.cc b/libretroshare/src/gxs/rsgxsdata.cc index de1ce4024..48dfa9917 100644 --- a/libretroshare/src/gxs/rsgxsdata.cc +++ b/libretroshare/src/gxs/rsgxsdata.cc @@ -33,7 +33,7 @@ RsGxsGrpMetaData::RsGxsGrpMetaData() clear(); } -uint32_t RsGxsGrpMetaData::serial_size(uint32_t api_version) +uint32_t RsGxsGrpMetaData::serial_size(uint32_t api_version) const { uint32_t s = 8; // header size diff --git a/libretroshare/src/gxs/rsgxsdata.h b/libretroshare/src/gxs/rsgxsdata.h index 76924167c..3cead4a11 100644 --- a/libretroshare/src/gxs/rsgxsdata.h +++ b/libretroshare/src/gxs/rsgxsdata.h @@ -51,7 +51,7 @@ public: RsGxsGrpMetaData(); bool deserialise(void *data, uint32_t &pktsize); bool serialise(void* data, uint32_t &pktsize, uint32_t api_version); - uint32_t serial_size(uint32_t api_version); + uint32_t serial_size(uint32_t api_version) const; void clear(); void operator =(const RsGroupMetaData& rMeta); diff --git a/libretroshare/src/gxs/rsgxsdataaccess.cc b/libretroshare/src/gxs/rsgxsdataaccess.cc index e010ffaf9..5378662d0 100644 --- a/libretroshare/src/gxs/rsgxsdataaccess.cc +++ b/libretroshare/src/gxs/rsgxsdataaccess.cc @@ -25,6 +25,7 @@ #include +#include "rsgxsutil.h" #include "rsgxsdataaccess.h" #include "retroshare/rsgxsflags.h" @@ -377,7 +378,7 @@ bool RsGxsDataAccess::clearRequest(const uint32_t& token) return true; } -bool RsGxsDataAccess::getGroupSummary(const uint32_t& token, std::list& groupInfo) +bool RsGxsDataAccess::getGroupSummary(const uint32_t& token, std::list& groupInfo) { RS_STACK_MUTEX(mDataMutex); @@ -1000,23 +1001,22 @@ bool RsGxsDataAccess::getGroupData(GroupDataReq* req) bool RsGxsDataAccess::getGroupSummary(GroupMetaReq* req) { - std::map grpMeta; + RsGxsGrpMetaTemporaryMap grpMeta; + std::list grpIdsOut; - std::list grpIdsOut; + getGroupList(req->mGroupIds, req->Options, grpIdsOut); - getGroupList(req->mGroupIds, req->Options, grpIdsOut); + if(grpIdsOut.empty()) + return true; - if(grpIdsOut.empty()) - return true; + std::list::const_iterator lit = grpIdsOut.begin(); - std::list::const_iterator lit = grpIdsOut.begin(); - - for(; lit != grpIdsOut.end(); ++lit) + for(; lit != grpIdsOut.end(); ++lit) grpMeta[*lit] = NULL; mDataStore->retrieveGxsGrpMetaData(grpMeta); - std::map::iterator mit = grpMeta.begin(); + std::map::iterator mit = grpMeta.begin(); for(; mit != grpMeta.end(); ++mit) req->mGroupMetaData.push_back(mit->second); @@ -1033,29 +1033,18 @@ bool RsGxsDataAccess::getGroupList(GroupIdReq* req) bool RsGxsDataAccess::getGroupList(const std::list& grpIdsIn, const RsTokReqOptions& opts, std::list& grpIdsOut) { - std::map grpMeta; + RsGxsGrpMetaTemporaryMap grpMeta; - std::list::const_iterator lit = grpIdsIn.begin(); - - for(; lit != grpIdsIn.end(); ++lit) - grpMeta[*lit] = NULL; + for(auto lit = grpIdsIn.begin(); lit != grpIdsIn.end(); ++lit) + grpMeta[*lit] = NULL; mDataStore->retrieveGxsGrpMetaData(grpMeta); - std::map::iterator mit = grpMeta.begin(); - - for(; mit != grpMeta.end(); ++mit) - { - grpIdsOut.push_back(mit->first); - } + for(auto mit = grpMeta.begin() ; mit != grpMeta.end(); ++mit) + grpIdsOut.push_back(mit->first); filterGrpList(grpIdsOut, opts, grpMeta); - for(mit = grpMeta.begin(); mit != grpMeta.end(); ++mit) - { - delete mit->second; // so wasteful!! - } - return true; } @@ -1622,12 +1611,10 @@ bool RsGxsDataAccess::getGroupStatistic(GroupStatisticRequest *req) // potentially very expensive! bool RsGxsDataAccess::getServiceStatistic(ServiceStatisticRequest *req) { - std::map grpMeta; + RsGxsGrpMetaTemporaryMap grpMeta ; mDataStore->retrieveGxsGrpMetaData(grpMeta); - std::map::iterator mit = grpMeta.begin(); - req->mServiceStatistic.mNumGrps = grpMeta.size(); req->mServiceStatistic.mNumMsgs = 0; req->mServiceStatistic.mSizeOfGrps = 0; @@ -1638,9 +1625,9 @@ bool RsGxsDataAccess::getServiceStatistic(ServiceStatisticRequest *req) req->mServiceStatistic.mNumChildMsgsNew = 0; req->mServiceStatistic.mNumChildMsgsUnread = 0; - for(; mit != grpMeta.end(); ++mit) + for(auto mit = grpMeta.begin(); mit != grpMeta.end(); ++mit) { - RsGxsGrpMetaData* m = mit->second; + const RsGxsGrpMetaData* m = mit->second; req->mServiceStatistic.mSizeOfGrps += m->mGrpSize + m->serial_size(RS_GXS_GRP_META_DATA_CURRENT_API_VERSION); if (IS_GROUP_SUBSCRIBED(m->mSubscribeFlags)) @@ -1658,8 +1645,6 @@ bool RsGxsDataAccess::getServiceStatistic(ServiceStatisticRequest *req) req->mServiceStatistic.mNumChildMsgsNew += gr.mGroupStatistic.mNumChildMsgsNew; req->mServiceStatistic.mNumChildMsgsUnread += gr.mGroupStatistic.mNumChildMsgsUnread; } - - delete m; } req->mServiceStatistic.mSizeStore = req->mServiceStatistic.mSizeOfGrps + req->mServiceStatistic.mSizeOfMsgs; diff --git a/libretroshare/src/gxs/rsgxsdataaccess.h b/libretroshare/src/gxs/rsgxsdataaccess.h index ef132dc0e..3c0401d29 100644 --- a/libretroshare/src/gxs/rsgxsdataaccess.h +++ b/libretroshare/src/gxs/rsgxsdataaccess.h @@ -32,7 +32,7 @@ typedef std::map< RsGxsGroupId, std::map > MsgMetaFilter; -typedef std::map< RsGxsGroupId, RsGxsGrpMetaData* > GrpMetaFilter; +typedef std::map< RsGxsGroupId, const RsGxsGrpMetaData* > GrpMetaFilter; class RsGxsDataAccess : public RsTokenService { @@ -209,7 +209,7 @@ public: * @param token request token to be redeemed * @param groupInfo */ - bool getGroupSummary(const uint32_t &token, std::list &groupInfo); + bool getGroupSummary(const uint32_t &token, std::list& groupInfo); /*! * diff --git a/libretroshare/src/gxs/rsgxsnetservice.cc b/libretroshare/src/gxs/rsgxsnetservice.cc index 88c7701d2..7e9aa3f5b 100644 --- a/libretroshare/src/gxs/rsgxsnetservice.cc +++ b/libretroshare/src/gxs/rsgxsnetservice.cc @@ -559,7 +559,7 @@ void RsGxsNetService::syncWithPeers() for(RsGxsGrpMetaTemporaryMap::iterator mit = grpMeta.begin(); mit != grpMeta.end(); ++mit) { - RsGxsGrpMetaData* meta = mit->second; + const RsGxsGrpMetaData* meta = mit->second; // This was commented out because we want to know how many messages are available for unsubscribed groups. @@ -689,7 +689,7 @@ void RsGxsNetService::syncGrpStatistics() time_t now = time(NULL) ; - for(std::map::const_iterator it(grpMeta.begin());it!=grpMeta.end();++it) + for(auto it(grpMeta.begin());it!=grpMeta.end();++it) { const RsGxsGrpConfig& rec = locked_getGrpConfig(it->first) ; @@ -755,7 +755,7 @@ void RsGxsNetService::handleRecvSyncGrpStatistics(RsNxsSyncGrpStatsItem *grs) mDataStore->retrieveGxsGrpMetaData(grpMetas); - RsGxsGrpMetaData* grpMeta = grpMetas[grs->grpId]; + const RsGxsGrpMetaData* grpMeta = grpMetas[grs->grpId]; if(grpMeta == NULL) { @@ -1951,7 +1951,7 @@ void RsGxsNetService::updateServerSyncTS() // finally, update timestamps. bool change = false; - for(std::map::const_iterator mit = gxsMap.begin();mit != gxsMap.end(); ++mit) + for(auto mit = gxsMap.begin();mit != gxsMap.end(); ++mit) { // Check if the group is subscribed and restricted to a circle. If the circle has changed, update the // global TS to reflect that change to clients who may be able to see/subscribe to that particular group. @@ -2722,7 +2722,7 @@ void RsGxsNetService::locked_genReqMsgTransaction(NxsTransaction* tr) grpMetaMap[grpId] = NULL; mDataStore->retrieveGxsGrpMetaData(grpMetaMap); - RsGxsGrpMetaData* grpMeta = grpMetaMap[grpId]; + const RsGxsGrpMetaData* grpMeta = grpMetaMap[grpId]; if(grpMeta == NULL) // this should not happen, but just in case... { @@ -3028,7 +3028,7 @@ void RsGxsNetService::locked_genReqGrpTransaction(NxsTransaction* tr) RsNxsSyncGrpItem*& grpSyncItem = *llit; const RsGxsGroupId& grpId = grpSyncItem->grpId; - std::map::const_iterator metaIter = grpMetaMap.find(grpId); + std::map::const_iterator metaIter = grpMetaMap.find(grpId); bool haveItem = false; bool latestVersion = false; @@ -3825,9 +3825,9 @@ void RsGxsNetService::handleRecvSyncGroup(RsNxsSyncGrpReqItem *item) GXSNETDEBUG_P_(peer) << " Group list beings being sent: " << std::endl; #endif - for(std::map::iterator mit = grp.begin(); mit != grp.end(); ++mit) + for(auto mit = grp.begin(); mit != grp.end(); ++mit) { - RsGxsGrpMetaData* grpMeta = mit->second; + const RsGxsGrpMetaData* grpMeta = mit->second; // Only send info about subscribed groups. @@ -3899,7 +3899,7 @@ void RsGxsNetService::handleRecvSyncGroup(RsNxsSyncGrpReqItem *item) -bool RsGxsNetService::canSendGrpId(const RsPeerId& sslId, RsGxsGrpMetaData& grpMeta, std::vector& /* toVet */, bool& should_encrypt) +bool RsGxsNetService::canSendGrpId(const RsPeerId& sslId, const RsGxsGrpMetaData& grpMeta, std::vector& /* toVet */, bool& should_encrypt) { #ifdef NXS_NET_DEBUG_4 GXSNETDEBUG_PG(sslId,grpMeta.mGroupId) << "RsGxsNetService::canSendGrpId()"<< std::endl; @@ -4092,7 +4092,7 @@ void RsGxsNetService::handleRecvSyncMessage(RsNxsSyncMsgReqItem *item,bool item_ grpMetas[item->grpId] = NULL; mDataStore->retrieveGxsGrpMetaData(grpMetas); - RsGxsGrpMetaData* grpMeta = grpMetas[item->grpId]; + const RsGxsGrpMetaData* grpMeta = grpMetas[item->grpId]; if(grpMeta == NULL) { @@ -4633,7 +4633,7 @@ void RsGxsNetService::sharePublishKeysPending() // Find the publish keys in the retrieved info - RsGxsGrpMetaData *grpMeta = grpMetaMap[mit->first] ; + const RsGxsGrpMetaData *grpMeta = grpMetaMap[mit->first] ; if(grpMeta == NULL) { @@ -4718,7 +4718,7 @@ void RsGxsNetService::handleRecvPublishKeys(RsNxsGroupPublishKeyItem *item) // update the publish keys in this group meta info - RsGxsGrpMetaData *grpMeta = grpMetaMap[item->grpId] ; + const RsGxsGrpMetaData *grpMeta = grpMetaMap[item->grpId] ; if (!grpMeta) { std::cerr << "(EE) RsGxsNetService::handleRecvPublishKeys() grpMeta not found." << std::endl; return ; @@ -4760,9 +4760,10 @@ void RsGxsNetService::handleRecvPublishKeys(RsNxsGroupPublishKeyItem *item) // Store/update the info. - grpMeta->keys.private_keys[item->private_key.keyId] = item->private_key ; + RsTlvSecurityKeySet keys = grpMeta->keys ; + keys.private_keys[item->private_key.keyId] = item->private_key ; - bool ret = mDataStore->updateGroupKeys(item->grpId,grpMeta->keys, grpMeta->mSubscribeFlags | GXS_SERV::GROUP_SUBSCRIBE_PUBLISH) ; + bool ret = mDataStore->updateGroupKeys(item->grpId,keys, grpMeta->mSubscribeFlags | GXS_SERV::GROUP_SUBSCRIBE_PUBLISH) ; if(ret) { diff --git a/libretroshare/src/gxs/rsgxsnetservice.h b/libretroshare/src/gxs/rsgxsnetservice.h index 29c28e96e..00ba07953 100644 --- a/libretroshare/src/gxs/rsgxsnetservice.h +++ b/libretroshare/src/gxs/rsgxsnetservice.h @@ -370,7 +370,7 @@ private: * @param toVet groupid/peer to vet are stored here if their circle id is not cached * @return false, if you cannot send to this peer, true otherwise */ - bool canSendGrpId(const RsPeerId& sslId, RsGxsGrpMetaData& grpMeta, std::vector& toVet, bool &should_encrypt); + bool canSendGrpId(const RsPeerId& sslId, const RsGxsGrpMetaData& grpMeta, std::vector& toVet, bool &should_encrypt); bool canSendMsgIds(std::vector& msgMetas, const RsGxsGrpMetaData&, const RsPeerId& sslId, RsGxsCircleId &should_encrypt_id); /*! diff --git a/libretroshare/src/gxs/rsgxsrequesttypes.h b/libretroshare/src/gxs/rsgxsrequesttypes.h index 8009f294b..5c70c9d52 100644 --- a/libretroshare/src/gxs/rsgxsrequesttypes.h +++ b/libretroshare/src/gxs/rsgxsrequesttypes.h @@ -52,7 +52,7 @@ public: public: std::list mGroupIds; - std::list mGroupMetaData; + std::list mGroupMetaData; }; class GroupIdReq : public GxsRequest diff --git a/libretroshare/src/gxs/rsgxsutil.cc b/libretroshare/src/gxs/rsgxsutil.cc index 1790964c8..abf02aef2 100644 --- a/libretroshare/src/gxs/rsgxsutil.cc +++ b/libretroshare/src/gxs/rsgxsutil.cc @@ -42,13 +42,10 @@ static const uint32_t MAX_GXS_IDS_REQUESTS_NET = 10 ; // max number of reques RsGxsMessageCleanUp::RsGxsMessageCleanUp(RsGeneralDataService* const dataService, RsGenExchange *genex, uint32_t chunkSize) : mDs(dataService), mGenExchangeClient(genex), CHUNK_SIZE(chunkSize) { - - std::map grpMeta; + RsGxsGrpMetaTemporaryMap grpMeta; mDs->retrieveGxsGrpMetaData(grpMeta); - std::map::iterator cit = grpMeta.begin(); - - for(;cit != grpMeta.end(); ++cit) + for(auto cit=grpMeta.begin();cit != grpMeta.end(); ++cit) mGrpMeta.push_back(cit->second); } @@ -64,7 +61,7 @@ bool RsGxsMessageCleanUp::clean() #endif while(!mGrpMeta.empty()) { - RsGxsGrpMetaData* grpMeta = mGrpMeta.back(); + const RsGxsGrpMetaData* grpMeta = mGrpMeta.back(); const RsGxsGroupId& grpId = grpMeta->mGroupId; mGrpMeta.pop_back(); GxsMsgReq req; @@ -137,8 +134,6 @@ bool RsGxsMessageCleanUp::clean() mDs->removeMsgs(req); - delete grpMeta; - i++; if(i > CHUNK_SIZE) break; } diff --git a/libretroshare/src/gxs/rsgxsutil.h b/libretroshare/src/gxs/rsgxsutil.h index a83d768ba..b44f04750 100644 --- a/libretroshare/src/gxs/rsgxsutil.h +++ b/libretroshare/src/gxs/rsgxsutil.h @@ -32,6 +32,7 @@ class RsGixs ; class RsGenExchange ; +class RsGeneralDataService ; // temporary holds a map of pointers to class T, and destroys all pointers on delete. @@ -104,7 +105,7 @@ public: } }; -typedef t_RsGxsGenericDataTemporaryMap RsGxsGrpMetaTemporaryMap; +typedef std::map RsGxsGrpMetaTemporaryMap; typedef t_RsGxsGenericDataTemporaryMap RsNxsGrpDataTemporaryMap; typedef t_RsGxsGenericDataTemporaryMapVector RsGxsMsgMetaTemporaryMap ; @@ -178,7 +179,7 @@ private: RsGeneralDataService* const mDs; RsGenExchange *mGenExchangeClient; uint32_t CHUNK_SIZE; - std::vector mGrpMeta; + std::vector mGrpMeta; }; /*! @@ -226,7 +227,7 @@ class GroupUpdate public: GroupUpdate() : oldGrpMeta(NULL), newGrp(NULL), validUpdate(false) {} - RsGxsGrpMetaData* oldGrpMeta; + const RsGxsGrpMetaData* oldGrpMeta; RsNxsGrp* newGrp; bool validUpdate; }; diff --git a/tests/unittests/libretroshare/gxs/data_service/rsdataservice_test.cc b/tests/unittests/libretroshare/gxs/data_service/rsdataservice_test.cc index 7f4e4b49c..d5da291a4 100644 --- a/tests/unittests/libretroshare/gxs/data_service/rsdataservice_test.cc +++ b/tests/unittests/libretroshare/gxs/data_service/rsdataservice_test.cc @@ -98,7 +98,7 @@ void test_groupStoreAndRetrieve(){ } RsGxsGrpMetaData *l_Meta = (*mit)->metaData, - *r_Meta = grpMetaR[grpId]; + *r_Meta = const_cast(grpMetaR[grpId]); // assign signSet and mGrpSize // to right as these values are not stored in db diff --git a/tests/unittests/libretroshare/gxs/gen_exchange/genexchangetester.cc b/tests/unittests/libretroshare/gxs/gen_exchange/genexchangetester.cc index 6fd3bfda2..6fb4cae2a 100644 --- a/tests/unittests/libretroshare/gxs/gen_exchange/genexchangetester.cc +++ b/tests/unittests/libretroshare/gxs/gen_exchange/genexchangetester.cc @@ -334,8 +334,8 @@ void GenExchangeTest::init(RsGroupMetaData& grpMetaData) const randString(SHORT_STR, grpMetaData.mServiceString); - grpMetaData.mGroupFlags = randNum() & (0x00000007);//See GXS_SERV::FLAG_PRIVACY_XXX values /libretroshare/src/retroshare/rsgxsflags.h:21 - if (grpMetaData.mGroupFlags == 0) grpMetaData.mGroupFlags = GXS_SERV::FLAG_PRIVACY_PRIVATE; + grpMetaData.mGroupFlags = GXS_SERV::FLAG_PRIVACY_PUBLIC; //randNum() & (0x00000007);//See GXS_SERV::FLAG_PRIVACY_XXX values /libretroshare/src/retroshare/rsgxsflags.h:21 + grpMetaData.mLastPost = randNum(); grpMetaData.mGroupStatus = randNum(); grpMetaData.mVisibleMsgCount = randNum();