Refactor some NodeStore, TaggedCache

This commit is contained in:
Vinnie Falco
2013-07-15 19:42:01 -07:00
parent ff73676294
commit 3906c7e161
3 changed files with 268 additions and 154 deletions

View File

@@ -12,32 +12,41 @@
class NodeStore : LeakChecked <NodeStore>
{
public:
enum
{
/** This is the largest number of key/value pairs we
will write during a bulk write.
*/
// VFALCO TODO Make this a tunable parameter in the key value pairs
bulkWriteBatchSize = 128
};
/** Interface to inform callers of cetain activities.
*/
class Hooks
{
virtual void on
};
/** Back end used for the store.
*/
class Backend
{
public:
// VFALCO TODO Move the function definition to the .cpp
Backend ()
: mWriteGeneration(0)
, mWriteLoad(0)
, mWritePending(false)
{
mWriteSet.reserve(128);
}
Backend ();
virtual ~Backend () { }
virtual std::string getDataBaseName() = 0;
// Store/retrieve a single object
// These functions must be thread safe
/** Store a single object.
*/
// VFALCO TODO Why should the Backend know or care about NodeObject?
// It should just deal with a fixed key and raw data.
//
virtual bool store (NodeObject::ref);
virtual NodeObject::pointer retrieve (uint256 const &hash) = 0;
// Store a group of objects
// This function will only be called from a single thread
virtual bool bulkStore (const std::vector< NodeObject::pointer >&) = 0;
/** Retrieve an individual object.
*/
virtual NodeObject::pointer retrieve (uint256 const &hash) = 0;
// Visit every object in the database
// This function will only be called during an import operation
@@ -46,19 +55,38 @@ public:
//
virtual void visitAll (FUNCTION_TYPE <void (NodeObject::pointer)>) = 0;
private:
friend class NodeStore;
// VFALCO TODO Put this bulk writing logic into a separate class.
virtual void bulkWrite (Job &);
virtual void waitWrite ();
virtual int getWriteLoad ();
// NOTE Why are these virtual?
void bulkWrite (Job &);
void waitWrite ();
int getWriteLoad ();
private:
virtual std::string getDataBaseName() = 0;
// Store a group of objects
// This function will only be called from a single thread
// VFALCO NOTE It looks like NodeStore throws this into the job queue?
virtual bool bulkStore (const std::vector< NodeObject::pointer >&) = 0;
protected:
// VFALCO TODO Put this bulk writing logic into a separate class.
boost::mutex mWriteMutex;
boost::condition_variable mWriteCondition;
int mWriteGeneration;
int mWriteLoad;
bool mWritePending;
std::vector <boost::shared_ptr<NodeObject> > mWriteSet;
boost::mutex mWriteMutex;
boost::condition_variable mWriteCondition;
int mWriteGeneration;
int mWriteLoad;
bool mWritePending;
std::vector <boost::shared_ptr <NodeObject> > mWriteSet;
};
public:
// Helper functions for the backend
class BackendHelper
{
public:
};
public:
@@ -90,6 +118,7 @@ public:
*/
// VFALCO NOTE Is cacheSize in bytes? objects? KB?
// Is cacheAge in minutes? seconds?
// These should be in the parameters.
//
NodeStore (String backendParameters,
String fastBackendParameters,
@@ -103,18 +132,32 @@ public:
*/
static void addBackendFactory (BackendFactory& factory);
// VFALCO TODO Document this.
float getCacheHitRate ();
// VFALCO TODO Document this.
bool store (NodeObjectType type, uint32 index, Blob const& data,
uint256 const& hash);
// VFALCO TODO Document this.
NodeObject::pointer retrieve (uint256 const& hash);
// VFALCO TODO Document this.
void waitWrite ();
// VFALCO TODO Document this.
// TODO Document the parameter meanings.
void tune (int size, int age);
// VFALCO TODO Document this.
void sweep ();
// VFALCO TODO Document this.
// What are the units of the return value?
int getWriteLoad ();
// VFALCO TODO Document this.
// NOTE What's the return value?
int import (String sourceBackendParameters);
private:
@@ -125,11 +168,15 @@ private:
static Array <BackendFactory*> s_factories;
private:
// Persistent key/value storage.
ScopedPointer <Backend> m_backend;
// Larger key/value storage, but not necessarily persistent.
ScopedPointer <Backend> m_fastBackend;
TaggedCache<uint256, NodeObject, UptimeTimerAdapter> mCache;
KeyCache <uint256, UptimeTimerAdapter> mNegativeCache;
// VFALCO NOTE What are these things for? We need comments.
TaggedCache <uint256, NodeObject, UptimeTimerAdapter> m_cache;
KeyCache <uint256, UptimeTimerAdapter> m_negativeCache;
};
#endif