mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-29 07:25:51 +00:00
Add memory backend for node store
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
#if RIPPLE_HYPERLEVELDB_AVAILABLE
|
||||
|
||||
/** Factory to produce HyperLevelDB backends for the NodeStore.
|
||||
|
||||
@see NodeStore
|
||||
*/
|
||||
class HyperLevelDBBackendFactory : public NodeStore::BackendFactory
|
||||
{
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#define RIPPLE_KEYVABACKENDFACTORY_H_INCLUDED
|
||||
|
||||
/** Factory to produce KeyvaDB backends for the NodeStore.
|
||||
|
||||
@see NodeStore
|
||||
*/
|
||||
class KeyvaDBBackendFactory : public NodeStore::BackendFactory
|
||||
{
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#define RIPPLE_LEVELDBBACKENDFACTORY_H_INCLUDED
|
||||
|
||||
/** Factory to produce LevelDB backends for the NodeStore.
|
||||
|
||||
@see NodeStore
|
||||
*/
|
||||
class LevelDBBackendFactory : public NodeStore::BackendFactory
|
||||
{
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
/** Factory to produce a backend using MDB.
|
||||
|
||||
@note MDB is not currently available for Win32
|
||||
|
||||
@see NodeStore
|
||||
*/
|
||||
class MdbBackendFactory : public NodeStore::BackendFactory
|
||||
{
|
||||
|
||||
113
modules/ripple_app/node/ripple_MemoryBackendFactory.cpp
Normal file
113
modules/ripple_app/node/ripple_MemoryBackendFactory.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
class MemoryBackendFactory::Backend : public NodeStore::Backend
|
||||
{
|
||||
private:
|
||||
typedef std::map <uint256 const, NodeObject::Ptr> Map;
|
||||
|
||||
public:
|
||||
Backend (size_t keyBytes, StringPairArray const& keyValues)
|
||||
: m_keyBytes (keyBytes)
|
||||
{
|
||||
}
|
||||
|
||||
~Backend ()
|
||||
{
|
||||
}
|
||||
|
||||
std::string getName ()
|
||||
{
|
||||
return "memory";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
Status fetch (void const* key, NodeObject::Ptr* pObject)
|
||||
{
|
||||
uint256 const hash (key);
|
||||
|
||||
Map::iterator iter = m_map.find (hash);
|
||||
|
||||
if (iter != m_map.end ())
|
||||
{
|
||||
*pObject = iter->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
pObject->reset ();
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
void store (NodeObject::ref object)
|
||||
{
|
||||
Map::iterator iter = m_map.find (object->getHash ());
|
||||
|
||||
if (iter == m_map.end ())
|
||||
{
|
||||
m_map.insert (std::make_pair (object->getHash (), object));
|
||||
}
|
||||
}
|
||||
|
||||
void storeBatch (NodeStore::Batch const& batch)
|
||||
{
|
||||
for (int i = 0; i < batch.size (); ++i)
|
||||
store (batch [i]);
|
||||
}
|
||||
|
||||
void visitAll (VisitCallback& callback)
|
||||
{
|
||||
for (Map::const_iterator iter = m_map.begin (); iter != m_map.end (); ++iter)
|
||||
callback.visitObject (iter->second);
|
||||
}
|
||||
|
||||
int getWriteLoad ()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
private:
|
||||
size_t const m_keyBytes;
|
||||
|
||||
Map m_map;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
MemoryBackendFactory::MemoryBackendFactory ()
|
||||
{
|
||||
}
|
||||
|
||||
MemoryBackendFactory::~MemoryBackendFactory ()
|
||||
{
|
||||
}
|
||||
|
||||
MemoryBackendFactory& MemoryBackendFactory::getInstance ()
|
||||
{
|
||||
static MemoryBackendFactory instance;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
String MemoryBackendFactory::getName () const
|
||||
{
|
||||
return "Memory";
|
||||
}
|
||||
|
||||
NodeStore::Backend* MemoryBackendFactory::createInstance (
|
||||
size_t keyBytes,
|
||||
StringPairArray const& keyValues,
|
||||
NodeStore::Scheduler& scheduler)
|
||||
{
|
||||
return new MemoryBackendFactory::Backend (keyBytes, keyValues);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
32
modules/ripple_app/node/ripple_MemoryBackendFactory.h
Normal file
32
modules/ripple_app/node/ripple_MemoryBackendFactory.h
Normal file
@@ -0,0 +1,32 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_MEMORYBACKENDFACTORY_H_INCLUDED
|
||||
#define RIPPLE_MEMORYBACKENDFACTORY_H_INCLUDED
|
||||
|
||||
/** Factory to produce a RAM based backend for the NodeStore.
|
||||
|
||||
@see NodeStore
|
||||
*/
|
||||
class MemoryBackendFactory : public NodeStore::BackendFactory
|
||||
{
|
||||
private:
|
||||
class Backend;
|
||||
|
||||
MemoryBackendFactory ();
|
||||
~MemoryBackendFactory ();
|
||||
|
||||
public:
|
||||
static MemoryBackendFactory& getInstance ();
|
||||
|
||||
String getName () const;
|
||||
|
||||
NodeStore::Backend* createInstance (size_t keyBytes,
|
||||
StringPairArray const& keyValues,
|
||||
NodeStore::Scheduler& scheduler);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1043,7 +1043,10 @@ public:
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void testNodeStore (String type, bool const useEphemeralDatabase, int64 const seedValue)
|
||||
void testNodeStore (String type,
|
||||
bool const useEphemeralDatabase,
|
||||
bool const testPersistence,
|
||||
int64 const seedValue)
|
||||
{
|
||||
String s;
|
||||
s << String ("NodeStore backend '") + type + "'";
|
||||
@@ -1092,33 +1095,36 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
if (testPersistence)
|
||||
{
|
||||
// Re-open the database without the ephemeral DB
|
||||
ScopedPointer <NodeStore> db (NodeStore::New (nodeParams));
|
||||
{
|
||||
// Re-open the database without the ephemeral DB
|
||||
ScopedPointer <NodeStore> db (NodeStore::New (nodeParams));
|
||||
|
||||
// Read it back in
|
||||
NodeStore::Batch copy;
|
||||
fetchCopyOfBatch (*db, ©, batch);
|
||||
// Read it back in
|
||||
NodeStore::Batch copy;
|
||||
fetchCopyOfBatch (*db, ©, batch);
|
||||
|
||||
// Canonicalize the source and destination batches
|
||||
std::sort (batch.begin (), batch.end (), NodeObject::LessThan ());
|
||||
std::sort (copy.begin (), copy.end (), NodeObject::LessThan ());
|
||||
expect (areBatchesEqual (batch, copy), "Should be equal");
|
||||
}
|
||||
// Canonicalize the source and destination batches
|
||||
std::sort (batch.begin (), batch.end (), NodeObject::LessThan ());
|
||||
std::sort (copy.begin (), copy.end (), NodeObject::LessThan ());
|
||||
expect (areBatchesEqual (batch, copy), "Should be equal");
|
||||
}
|
||||
|
||||
if (useEphemeralDatabase)
|
||||
{
|
||||
// Verify the ephemeral db
|
||||
ScopedPointer <NodeStore> db (NodeStore::New (tempParams, StringPairArray ()));
|
||||
if (useEphemeralDatabase)
|
||||
{
|
||||
// Verify the ephemeral db
|
||||
ScopedPointer <NodeStore> db (NodeStore::New (tempParams, StringPairArray ()));
|
||||
|
||||
// Read it back in
|
||||
NodeStore::Batch copy;
|
||||
fetchCopyOfBatch (*db, ©, batch);
|
||||
// Read it back in
|
||||
NodeStore::Batch copy;
|
||||
fetchCopyOfBatch (*db, ©, batch);
|
||||
|
||||
// Canonicalize the source and destination batches
|
||||
std::sort (batch.begin (), batch.end (), NodeObject::LessThan ());
|
||||
std::sort (copy.begin (), copy.end (), NodeObject::LessThan ());
|
||||
expect (areBatchesEqual (batch, copy), "Should be equal");
|
||||
// Canonicalize the source and destination batches
|
||||
std::sort (batch.begin (), batch.end (), NodeObject::LessThan ());
|
||||
std::sort (copy.begin (), copy.end (), NodeObject::LessThan ());
|
||||
expect (areBatchesEqual (batch, copy), "Should be equal");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1126,18 +1132,18 @@ public:
|
||||
|
||||
void runBackendTests (bool useEphemeralDatabase, int64 const seedValue)
|
||||
{
|
||||
testNodeStore ("keyvadb", useEphemeralDatabase, seedValue);
|
||||
testNodeStore ("keyvadb", useEphemeralDatabase, true, seedValue);
|
||||
|
||||
testNodeStore ("leveldb", useEphemeralDatabase, seedValue);
|
||||
testNodeStore ("leveldb", useEphemeralDatabase, true, seedValue);
|
||||
|
||||
testNodeStore ("sqlite", useEphemeralDatabase, seedValue);
|
||||
testNodeStore ("sqlite", useEphemeralDatabase, true, seedValue);
|
||||
|
||||
#if RIPPLE_HYPERLEVELDB_AVAILABLE
|
||||
testNodeStore ("hyperleveldb", useEphemeralDatabase, seedValue);
|
||||
testNodeStore ("hyperleveldb", useEphemeralDatabase, true, seedValue);
|
||||
#endif
|
||||
|
||||
#if RIPPLE_MDB_AVAILABLE
|
||||
testNodeStore ("mdb", useEphemeralDatabase, seedValue);
|
||||
testNodeStore ("mdb", useEphemeralDatabase, true, seedValue);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1166,6 +1172,8 @@ public:
|
||||
{
|
||||
int64 const seedValue = 50;
|
||||
|
||||
testNodeStore ("memory", false, false, seedValue);
|
||||
|
||||
runBackendTests (false, seedValue);
|
||||
|
||||
runBackendTests (true, seedValue);
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
/** Factory to produce a null backend.
|
||||
|
||||
This is for standalone / testing mode.
|
||||
|
||||
@see NodeStore
|
||||
*/
|
||||
class NullBackendFactory : public NodeStore::BackendFactory
|
||||
{
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#define RIPPLE_SQLITEBACKENDFACTORY_H_INCLUDED
|
||||
|
||||
/** Factory to produce SQLite backends for the NodeStore.
|
||||
|
||||
@see NodeStore
|
||||
*/
|
||||
class SqliteBackendFactory : public NodeStore::BackendFactory
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user