From bea5f2250fd16ecf2c03ab4722473f2e6496e945 Mon Sep 17 00:00:00 2001 From: chrisparker126 Date: Sun, 17 Jul 2011 22:25:41 +0000 Subject: [PATCH] covered situation in which cache entries are invalidated when messages expire git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-cacheopt@4459 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/services/p3distrib.cc | 62 +++++++++++++++++++++++-- libretroshare/src/services/p3distrib.h | 12 +++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/libretroshare/src/services/p3distrib.cc b/libretroshare/src/services/p3distrib.cc index 622c4e285..4f2f1e7fb 100644 --- a/libretroshare/src/services/p3distrib.cc +++ b/libretroshare/src/services/p3distrib.cc @@ -476,6 +476,54 @@ void p3GroupDistrib::updateCacheDocument() return; } +struct by_cacheid +{ + bool operator()(pugi::xml_node node) const + { + bool cachIdEqual = true; + cachIdEqual &= (node.child_value("subId") == subId); + cachIdEqual &= (node.child_value("pId") == peerId); + return cachIdEqual; + } + + std::string peerId; + std::string subId; +}; + +void p3GroupDistrib::locked_removeCacheTableEntry(const pCacheId& pCid) +{ + + // search through cache document for all entries with this cache id + + pugi::xml_node_iterator nit = mCacheDoc.begin(); + by_cacheid bCid; + bCid.peerId = pCid.first; + + char subIdBuffer[6]; + std::string subId; + sprintf(subIdBuffer, "%d", pCid.second); + subId = subIdBuffer; + bCid.subId = subId; + + // for each grp, remove message nodes that match pCid + for(; nit != mCacheDoc.end(); nit++) + { + pugi::xml_node msgNode = nit->child("messages"); + + if(msgNode) + { + while(pugi::xml_node cNode= msgNode.find_child(bCid)) + { + msgNode.remove_child(cNode); + } + } + } + + locked_buildCacheTable(); + + return; +} + void p3GroupDistrib::locked_updateCacheTableGrp(const std::vector& grpNodes, bool historical) { @@ -553,6 +601,7 @@ bool p3GroupDistrib::locked_historyCached(const std::string& grpId, bool& cached std::map::iterator cit; if(mCacheTable.end() != (cit = mCacheTable.find(grpId))) { + cached = cit->second.cached; return true; } @@ -561,6 +610,8 @@ bool p3GroupDistrib::locked_historyCached(const std::string& grpId, bool& cached return false; } + + bool p3GroupDistrib::locked_historyCached(const pCacheId& cId) { @@ -588,6 +639,9 @@ bool p3GroupDistrib::locked_buildCacheTable(){ return false; } + // clear cache table + mCacheTable.clear(); + pugi::xml_node_iterator grpIt = mCacheDoc.begin(), msgIt; pugi::xml_node messages_node; std::map > msgCacheMap; @@ -769,8 +823,7 @@ void p3GroupDistrib::locked_getHistoryCacheData(const std::string& grpId, std::l } else locked_getStoredCache(cDataTemp); - - cDataSet.push_back(cDataTemp); + cDataSet.push_back(cDataTemp); } } else @@ -1869,6 +1922,8 @@ void p3GroupDistrib::locked_publishPendingMsgs() newCache.cid.type = CacheSource::getCacheType(); newCache.cid.subid = locked_determineCacheSubId(); + // remove old cache entry using this pid + locked_removeCacheTableEntry(pCacheId(newCache.pid, newCache.cid.subid)); /* create filename */ std::string path = CacheSource::getCacheDir(); @@ -1916,9 +1971,6 @@ void p3GroupDistrib::locked_publishPendingMsgs() { ok &= false; } - - - } /* Extract File Information from pqistore */ diff --git a/libretroshare/src/services/p3distrib.h b/libretroshare/src/services/p3distrib.h index cdc84808f..2bbd22afe 100644 --- a/libretroshare/src/services/p3distrib.h +++ b/libretroshare/src/services/p3distrib.h @@ -413,6 +413,15 @@ class p3GroupDistrib: public CacheSource, public CacheStore, public p3Config, pu */ bool locked_loadHistoryCacheFile(); + /*! + * this removes the given cache id and associated msgs nodes from + * all grp nodes + * cache table is updated to reflect document + * this costly, and is here to be called once a year has been reached on + * @param pCid the cache id to remove from cache document + */ + void locked_removeCacheTableEntry(const pCacheId& pCid); + private: /* these lists are filled by the overloaded fns... then cleared by the thread */ @@ -667,6 +676,9 @@ class p3GroupDistrib: public CacheSource, public CacheStore, public p3Config, pu void locked_publishPendingMsgs(); /*! + * This function is key to determining how long caches permeate + * a distributed network, after mStorePeriod has elapsed for a message + * it is over written since its cache subid is used for the cache file name * @return cache sub id */ uint16_t locked_determineCacheSubId();