Factor upTime() out of KeyCache, fix warnings

This commit is contained in:
Vinnie Falco
2013-05-25 12:08:17 -07:00
parent 55c39b7131
commit e227637e34
15 changed files with 57 additions and 48 deletions

View File

@@ -128,6 +128,7 @@ int SqliteDatabase::getNumRowsAffected()
}
// VFALCO: TODO, This must be fixed!!! return value needs to be int64
//
int SqliteDatabase::getLastInsertID()
{
return(sqlite3_last_insert_rowid(mConnection));
@@ -192,12 +193,12 @@ int32 SqliteDatabase::getInt(int colIndex)
float SqliteDatabase::getFloat(int colIndex)
{
return(sqlite3_column_double(mCurrentStmt, colIndex));
return(static_cast <float> (sqlite3_column_double(mCurrentStmt, colIndex)));
}
bool SqliteDatabase::getBool(int colIndex)
{
return(sqlite3_column_int(mCurrentStmt, colIndex));
return(sqlite3_column_int(mCurrentStmt, colIndex) ? true : false);
}
int SqliteDatabase::getBinary(int colIndex,unsigned char* buf,int maxSize)

View File

@@ -9,9 +9,21 @@
#include "uint256.h"
#include "ScopedLock.h"
#include "TaggedCache.h"
#include "KeyCache.h"
#include "InstanceCounter.h"
// VFALCO: TODO, Move this to someplace sensible!!
// Adapter to furnish uptime information to KeyCache via UptimeTimer singleton
struct KeyCacheUptimeTimer
{
inline static int getElapsedSeconds ()
{
return UptimeTimer::getInstance().getElapsedSeconds ();
}
};
DEFINE_INSTANCE(HashedObject);
class Job;
@@ -51,7 +63,7 @@ class HashedObjectStore
{
protected:
TaggedCache<uint256, HashedObject> mCache;
KeyCache<uint256> mNegativeCache;
KeyCache <uint256, KeyCacheUptimeTimer> mNegativeCache;
boost::mutex mWriteMutex;
boost::condition_variable mWriteCondition;

View File

@@ -1,29 +0,0 @@
#ifndef INTEGERTYPES_H
#define INTEGERTYPES_H
// VFALCO: TODO, determine if Borland C is supported
#if defined (_MSC_VER) /*|| defined(__BORLANDC__)*/
typedef __int64 int64;
typedef unsigned __int64 uint64;
typedef unsigned int uint32;
typedef unsigned short int uint16;
typedef int int32;
#else
typedef long long int64;
typedef unsigned long long uint64;
typedef unsigned int uint32;
typedef unsigned short int uint16;
typedef int int32;
#endif
// VFALCO: TODO, make sure minimum VS version is 9, 10, or 11
// If commenting this out creates a problem, contact me!
/*
#if defined(_MSC_VER) && _MSC_VER < 1300
#define for if (false) ; else for
#endif
*/
#endif

View File

@@ -1,131 +0,0 @@
#ifndef KEY_CACHE__H
#define KEY_CACHE__H
#include <string>
#include <boost/unordered_map.hpp>
#include <boost/thread/mutex.hpp>
extern int upTime();
template <typename c_Key> class KeyCache
{ // Maintains a cache of keys with no associated data
public:
typedef c_Key key_type;
typedef boost::unordered_map<key_type, int> map_type;
typedef typename map_type::iterator map_iterator;
protected:
const std::string mName;
boost::mutex mNCLock;
map_type mCache;
unsigned int mTargetSize, mTargetAge;
public:
KeyCache(const std::string& name, int size = 0, int age = 120) : mName(name), mTargetSize(size), mTargetAge(age)
{
assert((size >= 0) && (age > 2));
}
void getSize()
{
boost::mutex::scoped_lock sl(mNCLock);
return mCache.size();
}
void getTargetSize()
{
boost::mutex::scoped_lock sl(mNCLock);
return mTargetSize;
}
void getTargetAge()
{
boost::mutex::scoped_lock sl(mNCLock);
return mTargetAge;
}
void setTargets(int size, int age)
{
boost::mutex::scoped_lock sl(mNCLock);
mTargetSize = size;
mTargetAge = age;
assert((mTargetSize >= 0) && (mTargetAge > 2));
}
const std::string& getName()
{
return mName;
}
bool isPresent(const key_type& key, bool refresh = true)
{ // Check if an entry is cached, refresh it if so
boost::mutex::scoped_lock sl(mNCLock);
map_iterator it = mCache.find(key);
if (it == mCache.end())
return false;
if (refresh)
it->second = upTime();
return true;
}
bool del(const key_type& key)
{ // Remove an entry from the cache, return false if not-present
boost::mutex::scoped_lock sl(mNCLock);
map_iterator it = mCache.find(key);
if (it == mCache.end())
return false;
mCache.erase(it);
return true;
}
bool add(const key_type& key)
{ // Add an entry to the cache, return true if it is new
boost::mutex::scoped_lock sl(mNCLock);
map_iterator it = mCache.find(key);
if (it != mCache.end())
{
it->second = upTime();
return false;
}
mCache.insert(std::make_pair(key, upTime()));
return true;
}
void sweep()
{ // Remove stale entries from the cache
int now = upTime();
boost::mutex::scoped_lock sl(mNCLock);
int target;
if ((mTargetSize == 0) || (mCache.size() <= mTargetSize))
target = now - mTargetAge;
else
{
target = now - (mTargetAge * mTargetSize / mCache.size());
if (target > (now - 2))
target = now - 2;
}
map_iterator it = mCache.begin();
while (it != mCache.end())
{
if (it->second > now)
{
it->second = now;
++it;
}
else if (it->second < target)
it = mCache.erase(it);
else
++it;
}
}
};
#endif

View File

@@ -146,7 +146,7 @@ class LedgerAcquireMaster
protected:
boost::mutex mLock;
std::map<uint256, LedgerAcquire::pointer> mLedgers;
KeyCache<uint256> mRecentFailures;
KeyCache<uint256, KeyCacheUptimeTimer> mRecentFailures;
public:
LedgerAcquireMaster() : mRecentFailures("LedgerAcquireRecentFailures", 0, LEDGER_REACQUIRE_INTERVAL) { ; }

View File

@@ -1644,7 +1644,7 @@ void Peer::recvGetLedger(ripple::TMGetLedger& packet, ScopedLock& MasterLockHold
else
WriteLog (lsWARNING, Peer) << "getNodeFat returns false";
}
catch (std::exception& e)
catch (std::exception&)
{
std::string info;
if (packet.itype() == ripple::liTS_CANDIDATE)

View File

@@ -778,7 +778,7 @@ void SHAMap::fetchRoot(const uint256& hash, SHAMapSyncFilter* filter)
{
root = fetchNodeExternal(SHAMapNode(), hash);
}
catch (SHAMapMissingNode& mn)
catch (SHAMapMissingNode&)
{
std::vector<unsigned char> nodeData;
if (!filter || !filter->haveNode(SHAMapNode(), hash, nodeData))

View File

@@ -356,7 +356,7 @@ protected:
SHAMapType mType;
static KeyCache<uint256> fullBelowCache;
static KeyCache <uint256, KeyCacheUptimeTimer> fullBelowCache;
void dirtyUp(std::stack<SHAMapTreeNode::pointer>& stack, const uint256& target, uint256 prevHash);
std::stack<SHAMapTreeNode::pointer> getStack(const uint256& id, bool include_nonmatching_leaf, bool partialOk);

View File

@@ -13,7 +13,7 @@
static const uint256 uZero;
KeyCache<uint256> SHAMap::fullBelowCache("fullBelowCache", 65536, 240);
KeyCache <uint256, KeyCacheUptimeTimer> SHAMap::fullBelowCache("fullBelowCache", 65536, 240);
void SHAMap::getMissingNodes(std::vector<SHAMapNode>& nodeIDs, std::vector<uint256>& hashes, int max,
SHAMapSyncFilter* filter)