mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Modify the ledger history code to use the tagged cache.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
// This class implemented a cache and a map. The cache keeps objects alive
|
||||
@@ -21,7 +22,7 @@ public:
|
||||
typedef std::pair<time_t, data_ptr> cache_entry;
|
||||
|
||||
protected:
|
||||
mutable boost::mutex mLock;
|
||||
mutable boost::recursive_mutex mLock;
|
||||
|
||||
int mTargetSize, mTargetAge;
|
||||
std::map<key_type, cache_entry> mCache; // Hold strong reference to recent objects
|
||||
@@ -43,8 +44,10 @@ public:
|
||||
|
||||
bool touch(const key_type& key);
|
||||
bool del(const key_type& key);
|
||||
boost::shared_ptr<c_Data> canonicalize(const key_type& key, boost::shared_ptr<c_Data> data);
|
||||
bool canonicalize(const key_type& key, boost::shared_ptr<c_Data>& data);
|
||||
boost::shared_ptr<c_Data> fetch(const key_type& key);
|
||||
|
||||
boost::recursive_mutex& peekMutex() { return mLock; }
|
||||
};
|
||||
|
||||
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetSize() const
|
||||
@@ -65,13 +68,13 @@ template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTar
|
||||
|
||||
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getCacheSize()
|
||||
{
|
||||
boost::mutex::scoped_lock sl(mLock);
|
||||
boost::recursive_mutex::scoped_lock sl(mLock);
|
||||
return mCache.size();
|
||||
}
|
||||
|
||||
template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::sweep()
|
||||
{
|
||||
boost::mutex::scoped_lock sl(mLock);
|
||||
boost::recursive_mutex::scoped_lock sl(mLock);
|
||||
|
||||
if(mCache.size()<mTargetSize) return;
|
||||
|
||||
@@ -110,7 +113,7 @@ template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::sweep
|
||||
|
||||
template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::touch(const key_type& key)
|
||||
{ // If present, make current in cache
|
||||
boost::mutex::scoped_lock sl(mLock);
|
||||
boost::recursive_mutex::scoped_lock sl(mLock);
|
||||
|
||||
// Is the object in the map?
|
||||
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.find(key);
|
||||
@@ -136,7 +139,7 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::touch
|
||||
|
||||
template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::del(const key_type& key)
|
||||
{ // Remove from cache, map unaffected
|
||||
boost::mutex::scoped_lock sl(mLock);
|
||||
boost::recursive_mutex::scoped_lock sl(mLock);
|
||||
|
||||
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key);
|
||||
if(cit==mCache.end()) return false;
|
||||
@@ -145,57 +148,62 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::del(c
|
||||
}
|
||||
|
||||
template<typename c_Key, typename c_Data>
|
||||
boost::shared_ptr<c_Data> TaggedCache<c_Key, c_Data>::canonicalize(const key_type& key,
|
||||
boost::shared_ptr<c_Data> data)
|
||||
bool TaggedCache<c_Key, c_Data>::canonicalize(const key_type& key, boost::shared_ptr<c_Data>& data)
|
||||
{ // Return canonical value, store if needed, refresh in cache
|
||||
boost::mutex::scoped_lock sl(mLock);
|
||||
// Return values: true=we had the data already
|
||||
boost::recursive_mutex::scoped_lock sl(mLock);
|
||||
|
||||
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.find(key);
|
||||
if(mit==mMap.end())
|
||||
{ // not in map
|
||||
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));
|
||||
mMap.insert(std::make_pair(key, data));
|
||||
return data;
|
||||
return false;
|
||||
}
|
||||
if(mit->second->expired())
|
||||
if(mit->second.expired())
|
||||
{ // in map, but expired
|
||||
mit->second=data;
|
||||
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));
|
||||
return data;
|
||||
return false;
|
||||
}
|
||||
|
||||
data=mit->second.lock();
|
||||
assert(!!data);
|
||||
|
||||
// Valid in map, is it in cache?
|
||||
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key);
|
||||
if(cit!=mCache.end())
|
||||
cit->second->first=time(NULL); // Yes, refesh
|
||||
cit->second.first=time(NULL); // Yes, refesh
|
||||
else // no, add to cache
|
||||
cit.insert(std::make_pair(key, std::make_pair(time(NULL), weak_data_ptr(mit->second))));
|
||||
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));
|
||||
|
||||
return mit->second;
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename c_Key, typename c_Data>
|
||||
boost::shared_ptr<c_Data> TaggedCache<c_Key, c_Data>::fetch(const key_type& key)
|
||||
{
|
||||
boost::mutex::scoped_lock sl(mLock);
|
||||
boost::recursive_mutex::scoped_lock sl(mLock);
|
||||
|
||||
// Is it in the map?
|
||||
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.find(key);
|
||||
if(mit==mMap.end()) return data_ptr(); // No, we're done
|
||||
if(mit->second->expired())
|
||||
if(mit->second.expired())
|
||||
{ // in map, but expired
|
||||
mMap.erase(mit);
|
||||
return data_ptr();
|
||||
}
|
||||
|
||||
boost::shared_ptr<c_Data> data=mit->second.lock();
|
||||
|
||||
// Valid in map, is it in the cache?
|
||||
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key);
|
||||
if(cit!=mCache.end())
|
||||
cit->second->first=time(NULL); // Yes, refresh
|
||||
cit->second.first=time(NULL); // Yes, refresh
|
||||
else // No, add to cache
|
||||
cit.insert(std::make_pair(key, std::make_pair(time(NULL), weak_data_ptr(mit->second))));
|
||||
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));
|
||||
|
||||
return mit->second;
|
||||
return data;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user