Clean up JSONCache

This commit is contained in:
Vinnie Falco
2013-06-12 16:08:50 -07:00
parent 41f69a9992
commit 12d1117623
12 changed files with 291 additions and 145 deletions

View File

@@ -129,11 +129,6 @@ namespace boost {
#include "utility/ripple_IntegerTypes.h" // must come first
#include "utility/ripple_Log.h" // Needed by others
#include "containers/ripple_KeyCache.h"
#include "containers/ripple_RangeSet.h"
#include "containers/ripple_SecureAllocator.h"
#include "containers/ripple_TaggedCache.h"
#include "types/ripple_BasicTypes.h"
#include "utility/ripple_ByteOrder.h"
#include "utility/ripple_DiffieHellmanUtil.h"
@@ -151,4 +146,9 @@ namespace boost {
#include "utility/ripple_HashUtilities.h" // requires UInt256
#include "types/ripple_HashMaps.h"
#include "containers/ripple_KeyCache.h"
#include "containers/ripple_RangeSet.h"
#include "containers/ripple_SecureAllocator.h"
#include "containers/ripple_TaggedCache.h"
#endif

View File

@@ -37,7 +37,7 @@
#include "src/cpp/ripple/ripple_InfoSub.h"
// Order and indentation reflect the hierarchy of dependencies
#include "src/cpp/ripple/JSONCache.h"
// VFALCO NOTE Don't add anything here!!!
#include "src/cpp/ripple/ripple_HashedObject.h"
#include "src/cpp/ripple/ripple_SHAMapItem.h"
#include "src/cpp/ripple/ripple_SHAMapNode.h"

View File

@@ -95,6 +95,8 @@ static const uint64 tenTo17m1 = tenTo17 - 1;
#include "protocol/ripple_STAmount.cpp"
#include "protocol/ripple_STAmountRound.cpp"
#include "utility/ripple_JSONCache.cpp"
// VFALCO TODO Fix this for SConstruct
#ifdef _MSC_VER
#include "ripple.pb.cc" // BROKEN because of SConstruct

View File

@@ -87,6 +87,7 @@
#include "protocol/ripple_LedgerFormat.h" // needs SOTemplate from SerializedObject
#include "protocol/ripple_TransactionFormat.h"
#include "utility/ripple_JSONCache.h"
#include "utility/ripple_UptimeTimerAdapter.h"
#endif

View File

@@ -0,0 +1,157 @@
//------------------------------------------------------------------------------
JSONCache::Key::Key (int op, uint256 const& ledger, uint160 const& object, int lastUse)
: mLedger (ledger)
, mObject (object)
, mOperation (op)
, mLastUse (lastUse)
{
mHash = static_cast <std::size_t> (mOperation);
mLedger.hash_combine (mHash);
mObject.hash_combine (mHash);
}
int JSONCache::Key::compare (Key const& other) const
{
if (mHash < other.mHash) return -1;
if (mHash > other.mHash) return 1;
if (mOperation < other.mOperation) return -1;
if (mOperation > other.mOperation) return 1;
if (mLedger < other.mLedger) return -1;
if (mLedger > other.mLedger) return 1;
if (mObject < other.mObject) return -1;
if (mObject > other.mObject) return 1;
return 0;
}
bool JSONCache::Key::operator< (Key const& rhs) const { return compare (rhs) < 0; }
bool JSONCache::Key::operator> (Key const& rhs) const { return compare (rhs) > 0; }
bool JSONCache::Key::operator<= (Key const& rhs) const { return compare (rhs) <= 0; }
bool JSONCache::Key::operator>= (Key const& rhs) const { return compare (rhs) >= 0; }
bool JSONCache::Key::operator!= (Key const& rhs) const { return compare (rhs) != 0; }
bool JSONCache::Key::operator== (Key const& rhs) const { return compare (rhs) == 0; }
void JSONCache::Key::touch (Key const& key) const
{
mLastUse = key.mLastUse;
}
bool JSONCache::Key::isExpired (int expireTimeSeconds) const
{
return mLastUse < expireTimeSeconds;
}
std::size_t JSONCache::Key::getHash () const
{
return mHash;
}
//------------------------------------------------------------------------------
JSONCache::JSONCache (int expirationTimeInSeconds)
: m_expirationTime (expirationTimeInSeconds)
, mHits (0)
, mMisses (0)
{
}
//------------------------------------------------------------------------------
float JSONCache::getHitRate ()
{
boost::recursive_mutex::scoped_lock sl (m_lock);
return (static_cast <float> (mHits) * 100.f) / (1.0f + mHits + mMisses);
}
//------------------------------------------------------------------------------
int JSONCache::getNumberOfEntries ()
{
boost::recursive_mutex::scoped_lock sl (m_lock);
return m_cache.size ();
}
//------------------------------------------------------------------------------
JSONCache::data_t JSONCache::getEntry (Kind kind, LedgerHash const& ledger, uint160 const& object)
{
JSONCache::data_t result; // default constructor indicates not found
Key key (kind, ledger, object, getUptime ());
{
boost::recursive_mutex::scoped_lock sl(m_lock);
boost::unordered_map <Key, data_t>::iterator it = m_cache.find (key);
if (it != m_cache.end ())
{
++mHits;
it->first.touch (key);
result = it->second;
}
else
{
++mMisses;
}
}
return result;
}
//------------------------------------------------------------------------------
void JSONCache::storeEntry (Kind kind, uint256 const& ledger, uint160 const& object, data_t const& data)
{
Key key (kind, ledger, object, getUptime ());
{
boost::recursive_mutex::scoped_lock sl(m_lock);
m_cache.insert (std::pair <Key, data_t> (key, data));
}
}
//------------------------------------------------------------------------------
void JSONCache::sweep ()
{
int sweepTime = getUptime ();
if (sweepTime >= m_expirationTime)
{
sweepTime -= m_expirationTime;
{
boost::recursive_mutex::scoped_lock sl(m_lock);
boost::unordered_map <Key, data_t>::iterator it = m_cache.begin();
while (it != m_cache.end ())
{
if (it->first.isExpired (sweepTime))
{
it = m_cache.erase(it);
}
else
{
++it;
}
}
}
}
}
//------------------------------------------------------------------------------
int JSONCache::getUptime () const
{
return UptimeTimer::getInstance().getElapsedSeconds();
}

View File

@@ -0,0 +1,92 @@
#ifndef RIPPLE_JSCONCACHE_H
#define RIPPLE_JSCONCACHE_H
/** A simple cache for JSON.
@note All member functions are thread-safe.
*/
class JSONCache
{
public:
class Key
{
public:
Key (int op, const uint256& ledger, const uint160& object, int lastUse);
int compare(const Key& k) const;
bool operator<(const Key &k) const;
bool operator>(const Key &k) const;
bool operator<=(const Key &k) const;
bool operator>=(const Key &k) const;
bool operator!=(const Key &k) const;
bool operator==(const Key &k) const;
void touch (Key const& key) const;
bool isExpired (int expireTime) const;
std::size_t getHash () const;
private:
uint256 mLedger;
uint160 mObject;
int mOperation;
mutable int mLastUse;
std::size_t mHash;
};
public:
typedef boost::shared_ptr <Json::Value> data_t;
public:
enum Kind
{
kindLines,
kindOffers
};
/** Construct the cache.
@param expirationTimeInSeconds The time until cached items expire, in seconds.
*/
explicit JSONCache (int expirationTimeInSeconds);
/** Return the fraction of cache hits.
*/
float getHitRate ();
/** Return the number of cached items.
*/
int getNumberOfEntries ();
/** Retrieve a cached item.
@return The item, or a default constructed container if it was not found.
*/
data_t getEntry (Kind kind, LedgerHash const& ledger, uint160 const& object);
/** Store an item in the cache.
*/
void storeEntry (Kind kind, LedgerHash const& ledger, uint160 const& object, data_t const& data);
/** Purge expired items.
This must be called periodically.
*/
void sweep ();
private:
int getUptime () const;
private:
int const m_expirationTime;
boost::unordered_map <Key, data_t> m_cache;
boost::recursive_mutex m_lock;
uint64 mHits;
uint64 mMisses;
};
inline std::size_t hash_value (JSONCache::Key const& key)
{
return key.getHash ();
}
#endif

View File

@@ -185,7 +185,6 @@
#include "src/cpp/ripple/LedgerEntrySet.h"
#include "src/cpp/ripple/TransactionEngine.h"
#include "src/cpp/ripple/ripple_CanonicalTXSet.h"
#include "src/cpp/ripple/JSONCache.h"
#include "src/cpp/ripple/ripple_LedgerHistory.h"
#include "src/cpp/ripple/LedgerMaster.h"