diff --git a/libretroshare/src/gxs/rsdataservice.cc b/libretroshare/src/gxs/rsdataservice.cc index 8bf848a38..b09e62c99 100644 --- a/libretroshare/src/gxs/rsdataservice.cc +++ b/libretroshare/src/gxs/rsdataservice.cc @@ -507,8 +507,8 @@ RsGxsGrpMetaData* RsDataService::locked_getGrpMeta(RetroCursor &c, int colOffset else grpMeta = new RsGxsGrpMetaData(); - // if(!grpMeta->mGroupId.isNull()) // the grpMeta is already initialized because it comes from the cache - // return grpMeta; + if(!grpMeta->mGroupId.isNull()) // the grpMeta is already initialized because it comes from the cache + return grpMeta; grpMeta->mGroupId = RsGxsGroupId(tempId); c.getString(mColGrpMeta_NxsIdentity + colOffset, tempId); @@ -664,13 +664,13 @@ RsGxsMsgMetaData* RsDataService::locked_getMsgMeta(RetroCursor &c, int colOffset RsGxsMsgMetaData* msgMeta = nullptr; -// if(use_cache) -// msgMeta = mMsgMetaDataCache[group_id].getOrCreateMeta(msg_id); -// else + if(use_cache) + msgMeta = mMsgMetaDataCache[group_id].getOrCreateMeta(msg_id); + else msgMeta = new RsGxsMsgMetaData(); - //if(!msgMeta->mGroupId.isNull()) // we cannot do that because the cursor needs to advance. Is there a method to skip some data in the db? - // return msgMeta; + if(!msgMeta->mGroupId.isNull()) // we cannot do that because the cursor needs to advance. Is there a method to skip some data in the db? + return msgMeta; msgMeta->mGroupId = group_id; msgMeta->mMsgId = msg_id; @@ -838,6 +838,7 @@ int RsDataService::storeMessage(const std::list& msg) // This is needed so that mLastPost is correctly updated in the group meta when it is re-loaded. mGrpMetaDataCache.clear(msgMetaPtr->mGroupId); + mMsgMetaDataCache[msgMetaPtr->mGroupId].updateMeta(msgMetaPtr->mMsgId,*msgMetaPtr); } // finish transaction @@ -1240,7 +1241,7 @@ void RsDataService::locked_retrieveMessages(RetroCursor *c, std::vectorfirst; + const std::set& msgIdV = mit->second; // if vector empty then request all messages - const std::set& msgIdV = mit->second; - std::vector metaSet; - if(msgIdV.empty()){ - RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, mMsgMetaColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", ""); + t_MetaDataCache& cache(mMsgMetaDataCache[grpId]); - if (c) - { - locked_retrieveMsgMeta(c, metaSet); + if(msgIdV.empty()) + { + if(cache.isCacheUpToDate()) + cache.getFullMetaList(msgMeta[grpId]); + else + { + RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, mMsgMetaColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", ""); + + if (c) + { + locked_retrieveMsgMetaList(c, msgMeta[grpId]); #ifdef RS_DATA_SERVICE_DEBUG_CACHE - std::cerr << mDbName << ": Retrieving (all) Msg metadata grpId=" << grpId << ", " << std::dec << metaSet.size() << " messages" << std::endl; + std::cerr << mDbName << ": Retrieving (all) Msg metadata grpId=" << grpId << ", " << std::dec << metaSet.size() << " messages" << std::endl; #endif - } - }else{ - - // request each grp - std::set::const_iterator sit = msgIdV.begin(); - - for(; sit!=msgIdV.end(); ++sit){ - const RsGxsMessageId& msgId = *sit; - RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, mMsgMetaColumns, KEY_GRP_ID+ "='" + grpId.toStdString() - + "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", ""); - - if (c) - { - locked_retrieveMsgMeta(c, metaSet); -#ifdef RS_DATA_SERVICE_DEBUG_CACHE - std::cerr << mDbName << ": Retrieving Msg metadata grpId=" << grpId << ", " << std::dec << metaSet.size() << " messages" << std::endl; -#endif - } - } + cache.setCacheUpToDate(true); + } + delete c; + } } + else + { + // request each msg meta + auto& metaSet(msgMeta[grpId]); -#ifdef RS_DATA_SERVICE_DEBUG_TIME - resultCount += metaSet.size(); + for(auto sit(msgIdV.begin()); sit!=msgIdV.end(); ++sit) + { + const RsGxsMessageId& msgId = *sit; + + RsGxsMsgMetaData *meta = cache.getMeta(msgId); + + if(meta) + metaSet.push_back(meta); + else + { + RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, mMsgMetaColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", ""); + + c->moveToFirst(); + RsGxsMsgMetaData* meta = locked_getMsgMeta(*c, 0,true); + + if(meta) + metaSet.push_back(meta); + + delete c; + +#ifdef RS_DATA_SERVICE_DEBUG_CACHE + std::cerr << mDbName << ": Retrieving Msg metadata grpId=" << grpId << ", " << std::dec << metaSet.size() << " messages" << std::endl; #endif - - msgMeta[grpId] = metaSet; + } + } + } } #ifdef RS_DATA_SERVICE_DEBUG_TIME @@ -1305,22 +1320,43 @@ int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaRes return 1; } -void RsDataService::locked_retrieveMsgMeta(RetroCursor *c, std::vector &msgMeta) +void RsDataService::locked_retrieveGrpMetaList(RetroCursor *c, std::map& grpMeta) { + if(!c) + { + RsErr() << __PRETTY_FUNCTION__ << ": attempt to retrieve Group Meta data from the DB with null cursor!" << std::endl; + return; + } - if(c) - { - bool valid = c->moveToFirst(); - while(valid){ - RsGxsMsgMetaData* m = locked_getMsgMeta(*c, 0,true); + bool valid = c->moveToFirst(); - if(m != NULL) - msgMeta.push_back(m); + while(valid) + { + RsGxsGrpMetaData* m = locked_getGrpMeta(*c, 0,true); - valid = c->moveToNext(); - } - delete c; - } + if(m) + grpMeta[m->mGroupId] = m; + + valid = c->moveToNext(); + } +} +void RsDataService::locked_retrieveMsgMetaList(RetroCursor *c, std::vector& msgMeta) +{ + if(!c) + { + RsErr() << __PRETTY_FUNCTION__ << ": attempt to retrieve Msg Meta data from the DB with null cursor!" << std::endl; + return; + } + + bool valid = c->moveToFirst(); + while(valid){ + const RsGxsMsgMetaData* m = locked_getMsgMeta(*c, 0,true); + + if(m != NULL) + msgMeta.push_back(m); + + valid = c->moveToNext(); + } } int RsDataService::retrieveGxsGrpMetaData(RsGxsGrpMetaTemporaryMap& grp) @@ -1346,7 +1382,7 @@ int RsDataService::retrieveGxsGrpMetaData(RsGxsGrpMetaTemporaryMap& grp) std::cerr << (void*)this << ": RsDataService::retrieveGxsGrpMetaData() retrieving all from cache!" << std::endl; #endif - grp = mGrpMetaDataCache.getFullMetaMap() ; + mGrpMetaDataCache.getFullMetaList(grp) ; } else { @@ -1357,87 +1393,99 @@ int RsDataService::retrieveGxsGrpMetaData(RsGxsGrpMetaTemporaryMap& grp) RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, mGrpMetaColumns, "", ""); - if(c) + if(c) { - bool valid = c->moveToFirst(); + locked_retrieveGrpMetaList(c,grp); - while(valid) - { - RsGxsGrpMetaData* g = locked_getGrpMeta(*c, 0,true); - - if(g) - { - grp[g->mGroupId] = g; -#ifdef RS_DATA_SERVICE_DEBUG_CACHE - std::cerr << (void *)this << " " << mDbName << ": Retrieving (all) Grp metadata grpId=" << g->mGroupId << std::endl; -#endif - } - valid = c->moveToNext(); - -#ifdef RS_DATA_SERVICE_DEBUG_TIME - ++resultCount; -#endif - } - delete c; + mGrpMetaDataCache.setCacheUpToDate(true); } + delete c; - mGrpMetaDataCache.setCacheUpToDate(true); +// if(c) +// { +// bool valid = c->moveToFirst(); +// +// while(valid) +// { +// RsGxsGrpMetaData* g = locked_getGrpMeta(*c, 0,true); +// +// if(g) +// { +// grp[g->mGroupId] = g; +//#ifdef RS_DATA_SERVICE_DEBUG_CACHE +// std::cerr << (void *)this << " " << mDbName << ": Retrieving (all) Grp metadata grpId=" << g->mGroupId << std::endl; +//#endif +// } +// valid = c->moveToNext(); +// +//#ifdef RS_DATA_SERVICE_DEBUG_TIME +// ++resultCount; +//#endif +// } +// delete c; +// } } } else - { - std::map::iterator mit = grp.begin(); + { + for(auto mit(grp.begin()); mit != grp.end(); ++mit) + { + RsGxsGrpMetaData *meta = mGrpMetaDataCache.getMeta(mit->first) ; - for(; mit != grp.end(); ++mit) - { - RsGxsGrpMetaData *meta = mGrpMetaDataCache.getMeta(mit->first) ; - - if(meta) - grp[mit->first] = meta; - else - { + if(meta) + grp[mit->first] = meta; + else + { #ifdef RS_DATA_SERVICE_DEBUG_CACHE - std::cerr << mDbName << ": Retrieving Grp metadata grpId=" << mit->first ; + std::cerr << mDbName << ": 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(); + c->moveToFirst(); + RsGxsGrpMetaData* meta = locked_getGrpMeta(*c, 0,true); + if(meta) + grp[mit->first] = meta; + + delete c; + + // 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; + //#endif + // while(valid) + // { + // RsGxsGrpMetaData* g = locked_getGrpMeta(*c, 0,true); + // + // if(g) + // { + // grp[g->mGroupId] = g; + //#ifdef RS_DATA_SERVICE_DEBUG_CACHE + // std::cerr << ". Got it. Updating cache." << std::endl; + //#endif + // } + // valid = c->moveToNext(); + // + //#ifdef RS_DATA_SERVICE_DEBUG_TIME + // ++resultCount; + //#endif + // } + // delete c; + // } #ifdef RS_DATA_SERVICE_DEBUG_CACHE - if(!valid) - std::cerr << " Empty query! GrpId " << grpId << " is not in database" << std::endl; + else + std::cerr << ". not found!" << std::endl; #endif - while(valid) - { - RsGxsGrpMetaData* g = locked_getGrpMeta(*c, 0,true); + } + } - if(g) - { - grp[g->mGroupId] = g; -#ifdef RS_DATA_SERVICE_DEBUG_CACHE - std::cerr << ". Got it. Updating cache." << std::endl; -#endif - } - valid = c->moveToNext(); - -#ifdef RS_DATA_SERVICE_DEBUG_TIME - ++resultCount; -#endif - } - delete c; - } -#ifdef RS_DATA_SERVICE_DEBUG_CACHE - else - std::cerr << ". not found!" << std::endl; -#endif - } - } - - } + } #ifdef RS_DATA_SERVICE_DEBUG_TIME std::cerr << "RsDataService::retrieveGxsGrpMetaData() " << mDbName << ", Requests: " << requestedGroups << ", Results: " << resultCount << ", Time: " << timer.duration() << std::endl; @@ -1474,14 +1522,14 @@ int RsDataService::resetDataStore() return 1; } -int RsDataService::updateGroupMetaData(GrpLocMetaData &meta) +int RsDataService::updateGroupMetaData(const GrpLocMetaData& meta) { #ifdef RS_DATA_SERVICE_DEBUG_CACHE std::cerr << (void*)this << ": Updating Grp Meta data: grpId = " << meta.grpId << std::endl; #endif RsStackMutex stack(mDbMutex); - RsGxsGroupId& grpId = meta.grpId; + const RsGxsGroupId& grpId = meta.grpId; #ifdef RS_DATA_SERVICE_DEBUG_CACHE std::cerr << (void*)this << ": erasing old entry from cache." << std::endl; @@ -1492,17 +1540,19 @@ int RsDataService::updateGroupMetaData(GrpLocMetaData &meta) return mDb->sqlUpdate(GRP_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", meta.val) ? 1 : 0; } -int RsDataService::updateMessageMetaData(MsgLocMetaData &metaData) +int RsDataService::updateMessageMetaData(const MsgLocMetaData& metaData) { #ifdef RS_DATA_SERVICE_DEBUG_CACHE std::cerr << (void*)this << ": Updating Msg Meta data: grpId = " << metaData.msgId.first << " msgId = " << metaData.msgId.second << std::endl; #endif RsStackMutex stack(mDbMutex); - RsGxsGroupId& grpId = metaData.msgId.first; - RsGxsMessageId& msgId = metaData.msgId.second; - return mDb->sqlUpdate(MSG_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() - + "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", metaData.val) ? 1 : 0; + const RsGxsGroupId& grpId = metaData.msgId.first; + const RsGxsMessageId& msgId = metaData.msgId.second; + + mMsgMetaDataCache[grpId].clear(msgId); + + return mDb->sqlUpdate(MSG_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", metaData.val) ? 1 : 0; } int RsDataService::removeMsgs(const GxsMsgReq& msgIds) @@ -1627,13 +1677,13 @@ bool RsDataService::locked_removeMessageEntries(const GxsMsgReq& msgIds) { const RsGxsGroupId& grpId = mit->first; const std::set& msgsV = mit->second; - std::set::const_iterator vit = msgsV.begin(); + auto& cache(mMsgMetaDataCache[grpId]); - for(; vit != msgsV.end(); ++vit) + for(auto& msgId:msgsV) { - const RsGxsMessageId& msgId = *vit; - mDb->sqlDelete(MSG_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() - + "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", ""); + mDb->sqlDelete(MSG_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", ""); + + cache.clear(msgId); } } diff --git a/libretroshare/src/gxs/rsdataservice.h b/libretroshare/src/gxs/rsdataservice.h index 5babb59ce..35234be9e 100644 --- a/libretroshare/src/gxs/rsdataservice.h +++ b/libretroshare/src/gxs/rsdataservice.h @@ -42,7 +42,9 @@ public: bool isCacheUpToDate() const { return mCache_ContainsAllMetas ; } void setCacheUpToDate(bool b) { mCache_ContainsAllMetas = b; } - const std::map getFullMetaMap() const { return mMetas ; } + + void getFullMetaList(std::map& mp) const { mp = mMetas ; } + void getFullMetaList(std::vector& mp) const { for(auto& m:mMetas) mp.push_back(m.second) ; } MetaDataClass *getMeta(const ID& id) { @@ -128,7 +130,7 @@ private: std::map mMetas; std::list > mOldCachedItems ; // dead list, where items get deleted after being unused for a while. This is due to not using smart ptrs. - static const uint32_t CACHE_ENTRY_GRACE_PERIOD = 600 ; // 10 minutes + static const uint32_t CACHE_ENTRY_GRACE_PERIOD = 600 ; // Unused items are deleted 10 minutes after last usage. bool mCache_ContainsAllMetas ; }; @@ -245,13 +247,13 @@ public: * @param metaData The meta data item to update * @return error code */ - int updateMessageMetaData(MsgLocMetaData& metaData); + int updateMessageMetaData(const MsgLocMetaData& metaData); /*! * @param metaData The meta data item to update * @return error code */ - int updateGroupMetaData(GrpLocMetaData& meta); + int updateGroupMetaData(const GrpLocMetaData &meta); /*! * Completely clear out data stored in @@ -292,9 +294,16 @@ private: /*! * Retrieves all the msg meta results from a cursor * @param c cursor to result set - * @param metaSet message metadata retrieved from cursor are stored here + * @param msgMeta message metadata retrieved from cursor are stored here */ - void locked_retrieveMsgMeta(RetroCursor* c, std::vector& msgMeta); + void locked_retrieveMsgMetaList(RetroCursor* c, std::vector& msgMeta); + + /*! + * Retrieves all the grp meta results from a cursor + * @param c cursor to result set + * @param grpMeta group metadata retrieved from cursor are stored here + */ + void locked_retrieveGrpMetaList(RetroCursor *c, std::map& grpMeta); /*! * extracts a msg meta item from a cursor at its diff --git a/libretroshare/src/gxs/rsgds.h b/libretroshare/src/gxs/rsgds.h index 04c2b17e7..fb985b9f7 100644 --- a/libretroshare/src/gxs/rsgds.h +++ b/libretroshare/src/gxs/rsgds.h @@ -239,12 +239,12 @@ public: /*! * @param metaData */ - virtual int updateMessageMetaData(MsgLocMetaData& metaData) = 0; + virtual int updateMessageMetaData(const MsgLocMetaData& metaData) = 0; /*! * @param metaData */ - virtual int updateGroupMetaData(GrpLocMetaData& meta) = 0; + virtual int updateGroupMetaData(const GrpLocMetaData& meta) = 0; virtual int updateGroupKeys(const RsGxsGroupId& grpId,const RsTlvSecurityKeySet& keys,uint32_t subscribed_flags) = 0 ; diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 8debf99f4..bfc1bbd23 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -1274,18 +1274,18 @@ bool RsGenExchange::getMsgMeta(const uint32_t &token, for(; mit != result.end(); ++mit) { - std::vector& metaV = mit->second; + std::vector& metaV = mit->second; std::vector& msgInfoV = msgInfo[mit->first]; - std::vector::iterator vit = metaV.begin(); + std::vector::iterator vit = metaV.begin(); RsMsgMetaData meta; for(; vit != metaV.end(); ++vit) { - RsGxsMsgMetaData& m = *(*vit); + const RsGxsMsgMetaData& m = *(*vit); meta = m; msgInfoV.push_back(meta); - delete *vit; + //delete *vit; } metaV.clear(); } @@ -1302,18 +1302,18 @@ bool RsGenExchange::getMsgRelatedMeta(const uint32_t &token, GxsMsgRelatedMetaMa for(; mit != result.end(); ++mit) { - std::vector& metaV = mit->second; + std::vector& metaV = mit->second; std::vector& msgInfoV = msgMeta[mit->first]; - std::vector::iterator vit = metaV.begin(); + std::vector::iterator vit = metaV.begin(); RsMsgMetaData meta; for(; vit != metaV.end(); ++vit) { - RsGxsMsgMetaData& m = *(*vit); + const RsGxsMsgMetaData& m = *(*vit); meta = m; msgInfoV.push_back(meta); - delete *vit; + //delete *vit; } metaV.clear(); } @@ -2016,15 +2016,15 @@ void RsGenExchange::processMsgMetaChanges() if(mit != result.end()) { - std::vector& msgMetaV = mit->second; + std::vector& msgMetaV = mit->second; if(!msgMetaV.empty()) { - RsGxsMsgMetaData* meta = *(msgMetaV.begin()); + const RsGxsMsgMetaData* meta = *(msgMetaV.begin()); value = (meta->mMsgStatus & ~mask) | (mask & value); changed = (static_cast(meta->mMsgStatus) != value); m.val.put(RsGeneralDataService::MSG_META_STATUS, value); - delete meta; + //delete meta; ok = true; } } diff --git a/libretroshare/src/gxs/rsgxs.h b/libretroshare/src/gxs/rsgxs.h index 3f25b087b..fd7f693f9 100644 --- a/libretroshare/src/gxs/rsgxs.h +++ b/libretroshare/src/gxs/rsgxs.h @@ -29,8 +29,8 @@ /* data types used throughout Gxs from netservice to genexchange */ -typedef std::map > GxsMsgMetaResult; -typedef std::map > MsgRelatedMetaResult; +typedef std::map > GxsMsgMetaResult; +typedef std::map > MsgRelatedMetaResult; // Default values that are used throughout GXS code diff --git a/libretroshare/src/gxs/rsgxsdata.cc b/libretroshare/src/gxs/rsgxsdata.cc index 0ec61de8e..c3c9b9f08 100644 --- a/libretroshare/src/gxs/rsgxsdata.cc +++ b/libretroshare/src/gxs/rsgxsdata.cc @@ -209,7 +209,7 @@ RsGxsMsgMetaData::~RsGxsMsgMetaData(){ return; } -uint32_t RsGxsMsgMetaData::serial_size() +uint32_t RsGxsMsgMetaData::serial_size() const { uint32_t s = 8; // header size diff --git a/libretroshare/src/gxs/rsgxsdata.h b/libretroshare/src/gxs/rsgxsdata.h index 282f063fb..6ef5b0c28 100644 --- a/libretroshare/src/gxs/rsgxsdata.h +++ b/libretroshare/src/gxs/rsgxsdata.h @@ -94,7 +94,7 @@ public: ~RsGxsMsgMetaData(); bool deserialise(void *data, uint32_t *size); bool serialise(void* data, uint32_t *size); - uint32_t serial_size(); + uint32_t serial_size() const; void clear(); void operator =(const RsMsgMetaData& rMeta); diff --git a/libretroshare/src/gxs/rsgxsdataaccess.cc b/libretroshare/src/gxs/rsgxsdataaccess.cc index 6104b0b56..a695672f9 100644 --- a/libretroshare/src/gxs/rsgxsdataaccess.cc +++ b/libretroshare/src/gxs/rsgxsdataaccess.cc @@ -1028,7 +1028,7 @@ bool RsGxsDataAccess::getMsgMetaDataList( const GxsMsgReq& msgIds, const RsTokRe //auto& filter( metaFilter[grpId] ); // does the initialization of metaFilter[grpId] and avoids further O(log(n)) calls - std::vector& metaV = meta_it->second; + std::vector& metaV = meta_it->second; if (onlyLatestMsgs) // if we only consider latest messages, we need to first filter out messages with "children" { @@ -1062,7 +1062,7 @@ bool RsGxsDataAccess::getMsgMetaDataList( const GxsMsgReq& msgIds, const RsTokRe for(uint32_t i=0;imParentId.isNull()) { - delete msgMeta; + //delete msgMeta; metaV[i] = nullptr; continue; } if (onlyOrigMsgs && !msgMeta->mOrigMsgId.isNull() && msgMeta->mMsgId != msgMeta->mOrigMsgId) { - delete msgMeta; + //delete msgMeta; metaV[i] = nullptr; continue; } @@ -1187,7 +1187,7 @@ bool RsGxsDataAccess::getMsgIdList( const GxsMsgReq& msgIds, const RsTokReqOptio } // delete meta data - cleanseMsgMetaMap(result); + //cleanseMsgMetaMap(result); return true; } @@ -1296,9 +1296,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) return true; } - std::vector::iterator vit_msgIds = req->mMsgIds.begin(); - - for(; vit_msgIds != req->mMsgIds.end(); ++vit_msgIds) + for(auto vit_msgIds(req->mMsgIds.begin()); vit_msgIds != req->mMsgIds.end(); ++vit_msgIds) { MsgMetaFilter filterMap; @@ -1310,8 +1308,8 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) GxsMsgReq msgIds; msgIds.insert(std::make_pair(grpMsgIdPair.first, std::set())); mDataStore->retrieveGxsMsgMetaData(msgIds, result); - std::vector& metaV = result[grpMsgIdPair.first]; - std::vector::iterator vit_meta; + std::vector& metaV = result[grpMsgIdPair.first]; + std::vector::iterator vit_meta; // msg id to relate to const RsGxsMessageId& msgId = grpMsgIdPair.second; @@ -1319,10 +1317,11 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) std::set outMsgIds; - RsGxsMsgMetaData* origMeta = nullptr; + const RsGxsMsgMetaData* origMeta = nullptr; + for(vit_meta = metaV.begin(); vit_meta != metaV.end(); ++vit_meta) { - RsGxsMsgMetaData* meta = *vit_meta; + const RsGxsMsgMetaData* meta = *vit_meta; if(msgId == meta->mMsgId) { @@ -1337,12 +1336,11 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) RsDbg() << "RsGxsDataAccess::getMsgRelatedInfo(): Cannot find meta of msgId (to relate to)!" << std::endl; #endif - cleanseMsgMetaMap(result); return false; } const RsGxsMessageId& origMsgId = origMeta->mOrigMsgId; - std::map& metaMap = filterMap[grpId]; + std::map& metaMap = filterMap[grpId]; if (onlyLatestMsgs) { @@ -1354,7 +1352,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) for(vit_meta = metaV.begin(); vit_meta != metaV.end(); ++vit_meta) { - RsGxsMsgMetaData* meta = *vit_meta; + const RsGxsMsgMetaData* meta = *vit_meta; // skip msgs that aren't children. if (onlyChildMsgs) @@ -1422,11 +1420,11 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) /* first guess is potentially better than Orig (can't be worse!) */ rstime_t latestTs = 0; RsGxsMessageId latestMsgId; - RsGxsMsgMetaData* latestMeta=nullptr; + const RsGxsMsgMetaData* latestMeta=nullptr; for(vit_meta = metaV.begin(); vit_meta != metaV.end(); ++vit_meta) { - RsGxsMsgMetaData* meta = *vit_meta; + const RsGxsMsgMetaData* meta = *vit_meta; if (meta->mOrigMsgId == origMsgId) { @@ -1446,7 +1444,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) { for(vit_meta = metaV.begin(); vit_meta != metaV.end(); ++vit_meta) { - RsGxsMsgMetaData* meta = *vit_meta; + const RsGxsMsgMetaData* meta = *vit_meta; if (meta->mOrigMsgId == origMsgId) { @@ -1482,8 +1480,6 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) outMsgIds.clear(); filteredOutMsgIds.clear(); - - cleanseMsgMetaMap(result); } return true; } @@ -1496,7 +1492,7 @@ bool RsGxsDataAccess::getGroupStatistic(GroupStatisticRequest *req) GxsMsgMetaResult metaResult; mDataStore->retrieveGxsMsgMetaData(metaReq, metaResult); - const std::vector& msgMetaV = metaResult[req->mGrpId]; + const std::vector& msgMetaV = metaResult[req->mGrpId]; req->mGroupStatistic.mGrpId = req->mGrpId; req->mGroupStatistic.mNumMsgs = msgMetaV.size(); @@ -1514,7 +1510,7 @@ bool RsGxsDataAccess::getGroupStatistic(GroupStatisticRequest *req) for(uint32_t i = 0; i < msgMetaV.size(); ++i) { - RsGxsMsgMetaData* m = msgMetaV[i]; + const RsGxsMsgMetaData* m = msgMetaV[i]; req->mGroupStatistic.mTotalSizeOfMsgs += m->mMsgSize + m->serial_size(); if(obsolete_msgs.find(m->mMsgId) != obsolete_msgs.end()) // skip obsolete messages. @@ -1540,7 +1536,7 @@ bool RsGxsDataAccess::getGroupStatistic(GroupStatisticRequest *req) } } - cleanseMsgMetaMap(metaResult); + //cleanseMsgMetaMap(metaResult); return true; } @@ -1595,21 +1591,19 @@ bool RsGxsDataAccess::getMsgIdList(MsgIdReq* req) mDataStore->retrieveGxsMsgMetaData(req->mMsgIds, result); - GxsMsgMetaResult::iterator mit = result.begin(), mit_end = result.end(); for(; mit != mit_end; ++mit) { const RsGxsGroupId grpId = mit->first; - std::vector& metaV = mit->second; - std::vector::iterator vit = metaV.begin(), + std::vector& metaV = mit->second; + std::vector::iterator vit = metaV.begin(), vit_end = metaV.end(); for(; vit != vit_end; ++vit) { - RsGxsMsgMetaData* meta = *vit; + const RsGxsMsgMetaData* meta = *vit; req->mMsgIdResult[grpId].insert(meta->mMsgId); - delete meta; // discard meta data mem } } @@ -1622,24 +1616,24 @@ bool RsGxsDataAccess::getMsgIdList(MsgIdReq* req) return true; } -void RsGxsDataAccess::cleanseMsgMetaMap(GxsMsgMetaResult& result) -{ - GxsMsgMetaResult::iterator mit = result.begin(); - - for(; mit !=result.end(); ++mit) - { - - std::vector& msgMetaV = mit->second; - std::vector::iterator vit = msgMetaV.begin(); - for(; vit != msgMetaV.end(); ++vit) - { - delete *vit; - } - } - - result.clear(); - return; -} +// void RsGxsDataAccess::cleanseMsgMetaMap(GxsMsgMetaResult& result) +// { +// GxsMsgMetaResult::iterator mit = result.begin(); +// +// for(; mit !=result.end(); ++mit) +// { +// +// std::vector& msgMetaV = mit->second; +// std::vector::iterator vit = msgMetaV.begin(); +// for(; vit != msgMetaV.end(); ++vit) +// { +// delete *vit; +// } +// } +// +// result.clear(); +// return; +// } void RsGxsDataAccess::filterMsgIdList( GxsMsgIdResult& resultsMap, const RsTokReqOptions& opts, const MsgMetaFilter& msgMetas ) const { @@ -1659,11 +1653,11 @@ void RsGxsDataAccess::filterMsgIdList( GxsMsgIdResult& resultsMap, const RsTokRe for( std::set::iterator msgIdIt = msgsIdSet.begin(); msgIdIt != msgsIdSet.end(); ) { const RsGxsMessageId& msgId(*msgIdIt); - const std::map& msgsMetaMap = + const std::map& msgsMetaMap = cit->second; bool keep = false; - std::map::const_iterator msgsMetaMapIt; + std::map::const_iterator msgsMetaMapIt; if( (msgsMetaMapIt = msgsMetaMap.find(msgId)) != msgsMetaMap.end() ) { diff --git a/libretroshare/src/gxs/rsgxsdataaccess.h b/libretroshare/src/gxs/rsgxsdataaccess.h index 21cb89b24..727ebe605 100644 --- a/libretroshare/src/gxs/rsgxsdataaccess.h +++ b/libretroshare/src/gxs/rsgxsdataaccess.h @@ -28,7 +28,7 @@ #include "rsgds.h" -typedef std::map< RsGxsGroupId, std::map > MsgMetaFilter; +typedef std::map< RsGxsGroupId, std::map > MsgMetaFilter; typedef std::map< RsGxsGroupId, RsGxsGrpMetaData* > GrpMetaFilter; bool operator<(const std::pair& p1,const std::pair& p2); @@ -328,11 +328,11 @@ private: */ void tokenList(std::list &tokens); - /*! - * Convenience function to delete the ids - * @param filter the meta filter to clean - */ - void cleanseMsgMetaMap(GxsMsgMetaResult& result); + // /*! + // * Convenience function to delete the ids + // * @param filter the meta filter to clean + // */ + // void cleanseMsgMetaMap(GxsMsgMetaResult& result); public: diff --git a/libretroshare/src/gxs/rsgxsnetservice.cc b/libretroshare/src/gxs/rsgxsnetservice.cc index 3259a060c..484ad466b 100644 --- a/libretroshare/src/gxs/rsgxsnetservice.cc +++ b/libretroshare/src/gxs/rsgxsnetservice.cc @@ -953,7 +953,7 @@ void RsGxsNetService::handleRecvSyncGrpStatistics(RsNxsSyncGrpStatsItem *grs) #endif mDataStore->retrieveGxsMsgMetaData(reqIds, result); - const std::vector& vec(result[grs->grpId]) ; + const std::vector& vec(result[grs->grpId]) ; if(vec.empty()) // that means we don't have any, or there isn't any, but since the default is always 0, no need to send. return ; @@ -970,12 +970,9 @@ void RsGxsNetService::handleRecvSyncGrpStatistics(RsNxsSyncGrpStatsItem *grs) // be used to discard groups that are not used. for(uint32_t i=0;ilast_post_TS < vec[i]->mPublishTs) grs_resp->last_post_TS = vec[i]->mPublishTs; - delete vec[i] ; - } #ifdef NXS_NET_DEBUG_6 GXSNETDEBUG_PG(grs->PeerId(),grs->grpId) << " sending back statistics item with " << vec.size() << " elements." << std::endl; #endif @@ -2953,21 +2950,19 @@ void RsGxsNetService::locked_genReqMsgTransaction(NxsTransaction* tr) reqIds[grpId] = std::set(); GxsMsgMetaResult result; mDataStore->retrieveGxsMsgMetaData(reqIds, result); - std::vector &msgMetaV = result[grpId]; + std::vector &msgMetaV = result[grpId]; #ifdef NXS_NET_DEBUG_1 GXSNETDEBUG_PG(item->PeerId(),grpId) << " retrieving grp message list..." << std::endl; GXSNETDEBUG_PG(item->PeerId(),grpId) << " grp locally contains " << msgMetaV.size() << " messsages." << std::endl; #endif - std::vector::const_iterator vit = msgMetaV.begin(); + std::vector::const_iterator vit = msgMetaV.begin(); std::set msgIdSet; // put ids in set for each searching for(; vit != msgMetaV.end(); ++vit) - { msgIdSet.insert((*vit)->mMsgId); - delete(*vit); - } + msgMetaV.clear(); #ifdef NXS_NET_DEBUG_1 @@ -4367,7 +4362,7 @@ void RsGxsNetService::handleRecvSyncMessage(RsNxsSyncMsgReqItem *item,bool item_ GxsMsgMetaResult metaResult; mDataStore->retrieveGxsMsgMetaData(req, metaResult); - std::vector& msgMetas = metaResult[item->grpId]; + std::vector& msgMetas = metaResult[item->grpId]; #ifdef NXS_NET_DEBUG_0 GXSNETDEBUG_PG(item->PeerId(),item->grpId) << " retrieving message meta data." << std::endl; @@ -4395,9 +4390,9 @@ void RsGxsNetService::handleRecvSyncMessage(RsNxsSyncMsgReqItem *item,bool item_ if(canSendMsgIds(msgMetas, *grpMeta, peer, should_encrypt_to_this_circle_id)) { - for(std::vector::iterator vit = msgMetas.begin();vit != msgMetas.end(); ++vit) + for(auto vit = msgMetas.begin();vit != msgMetas.end(); ++vit) { - RsGxsMsgMetaData* m = *vit; + const RsGxsMsgMetaData* m = *vit; // Check reputation @@ -4497,8 +4492,8 @@ void RsGxsNetService::handleRecvSyncMessage(RsNxsSyncMsgReqItem *item,bool item_ // release meta resource - for(std::vector::iterator vit = msgMetas.begin(); vit != msgMetas.end(); ++vit) - delete *vit; + // for(std::vector::iterator vit = msgMetas.begin(); vit != msgMetas.end(); ++vit) + // delete *vit; } void RsGxsNetService::locked_pushMsgRespFromList(std::list& itemL, const RsPeerId& sslId, const RsGxsGroupId& grp_id,const uint32_t& transN) @@ -4542,7 +4537,7 @@ void RsGxsNetService::locked_pushMsgRespFromList(std::list& itemL, c } } -bool RsGxsNetService::canSendMsgIds(std::vector& msgMetas, const RsGxsGrpMetaData& grpMeta, const RsPeerId& sslId,RsGxsCircleId& should_encrypt_id) +bool RsGxsNetService::canSendMsgIds(std::vector& msgMetas, const RsGxsGrpMetaData& grpMeta, const RsPeerId& sslId,RsGxsCircleId& should_encrypt_id) { #ifdef NXS_NET_DEBUG_4 GXSNETDEBUG_PG(sslId,grpMeta.mGroupId) << "RsGxsNetService::canSendMsgIds() CIRCLE VETTING" << std::endl; diff --git a/libretroshare/src/gxs/rsgxsnetservice.h b/libretroshare/src/gxs/rsgxsnetservice.h index 39fd2982e..e4056bb06 100644 --- a/libretroshare/src/gxs/rsgxsnetservice.h +++ b/libretroshare/src/gxs/rsgxsnetservice.h @@ -395,7 +395,7 @@ private: * @return false, if you cannot send to this peer, true otherwise */ 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); + bool canSendMsgIds(std::vector& msgMetas, const RsGxsGrpMetaData&, const RsPeerId& sslId, RsGxsCircleId &should_encrypt_id); /*! * \brief checkPermissionsForFriendGroup diff --git a/libretroshare/src/gxs/rsgxsutil.cc b/libretroshare/src/gxs/rsgxsutil.cc index 2396618a8..951476403 100644 --- a/libretroshare/src/gxs/rsgxsutil.cc +++ b/libretroshare/src/gxs/rsgxsutil.cc @@ -85,7 +85,7 @@ bool RsGxsMessageCleanUp::clean() for(; mit != result.end(); ++mit) { - std::vector& metaV = mit->second; + std::vector& metaV = mit->second; // First, make a map of which message have a child message. This allows to only delete messages that dont have child messages. // A more accurate way to go would be to compute the time of the oldest message and possibly delete all the branch, but in the @@ -99,7 +99,7 @@ bool RsGxsMessageCleanUp::clean() for( uint32_t i=0;imMsgId)!=messages_with_kids.end()); @@ -132,7 +132,7 @@ bool RsGxsMessageCleanUp::clean() std::cerr << std::endl; #endif - delete meta; + //delete meta; } }