From 98612a7cd697f0bb61d836dc21eb9b0687d4796d Mon Sep 17 00:00:00 2001 From: Miguel Portilla Date: Mon, 21 Apr 2014 18:20:19 -0400 Subject: [PATCH] Cleanup nodestore backend classes: * Add README.md * Add missing std::move calls * Refactor visitAll in backend --- Builds/VisualStudio2013/RippleD.vcxproj | 2 +- .../VisualStudio2013/RippleD.vcxproj.filters | 6 +- src/ripple_app/ledger/InboundLedger.cpp | 4 +- src/ripple_app/ledger/Ledger.cpp | 2 +- src/ripple_app/node/SqliteFactory.cpp | 10 +-- src/ripple_app/shamap/SHAMap.cpp | 2 +- src/ripple_app/shamap/SHAMapSyncFilters.cpp | 4 +- src/ripple_core/nodestore/README.md | 25 +++++++ src/ripple_core/nodestore/api/Backend.h | 6 +- src/ripple_core/nodestore/api/Database.h | 6 +- src/ripple_core/nodestore/api/NodeObject.h | 4 +- .../nodestore/backend/HyperDBFactory.cpp | 44 +++++++----- .../nodestore/backend/LevelDBFactory.cpp | 44 +++++++----- .../nodestore/backend/MemoryFactory.cpp | 41 +++++++---- .../nodestore/backend/NullFactory.cpp | 25 ++++--- .../nodestore/backend/RocksDBFactory.cpp | 59 +++++++++------ .../nodestore/impl/BatchWriter.cpp | 15 ++-- src/ripple_core/nodestore/impl/DatabaseImp.h | 72 +++++++------------ .../nodestore/impl/DecodedBlob.cpp | 2 +- .../nodestore/impl/DummyScheduler.cpp | 6 +- .../nodestore/impl/EncodedBlob.cpp | 3 +- src/ripple_core/nodestore/impl/Manager.cpp | 35 +++++---- src/ripple_core/nodestore/impl/NodeObject.cpp | 26 +++---- src/ripple_core/nodestore/tests/TestBase.h | 4 +- 24 files changed, 263 insertions(+), 184 deletions(-) create mode 100644 src/ripple_core/nodestore/README.md diff --git a/Builds/VisualStudio2013/RippleD.vcxproj b/Builds/VisualStudio2013/RippleD.vcxproj index c5260c38ea..f766f0458d 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj +++ b/Builds/VisualStudio2013/RippleD.vcxproj @@ -3045,7 +3045,6 @@ - @@ -3249,6 +3248,7 @@ + diff --git a/Builds/VisualStudio2013/RippleD.vcxproj.filters b/Builds/VisualStudio2013/RippleD.vcxproj.filters index 5af79e0667..c83f24187e 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2013/RippleD.vcxproj.filters @@ -2474,9 +2474,6 @@ [2] Old Ripple\ripple_core\nodestore\api - - [2] Old Ripple\ripple_core\nodestore\api - [2] Old Ripple\ripple_core\nodestore\backend @@ -3473,6 +3470,9 @@ [2] Old Ripple\ripple_overlay + + [2] Old Ripple\ripple_core\nodestore + diff --git a/src/ripple_app/ledger/InboundLedger.cpp b/src/ripple_app/ledger/InboundLedger.cpp index 35be31cea6..08010fa269 100644 --- a/src/ripple_app/ledger/InboundLedger.cpp +++ b/src/ripple_app/ledger/InboundLedger.cpp @@ -131,7 +131,7 @@ bool InboundLedger::tryLocal () "Ledger base found in fetch pack"; mLedger = boost::make_shared (data, true); getApp().getNodeStore ().store (hotLEDGER, - mLedger->getLedgerSeq (), data, mHash); + mLedger->getLedgerSeq (), std::move (data), mHash); } else { @@ -790,7 +790,7 @@ bool InboundLedger::takeBase (const std::string& data) s.add32 (HashPrefix::ledgerMaster); s.addRaw (data); getApp().getNodeStore ().store (hotLEDGER, - mLedger->getLedgerSeq (), s.modData (), mHash); + mLedger->getLedgerSeq (), std::move (s.modData ()), mHash); progress (); diff --git a/src/ripple_app/ledger/Ledger.cpp b/src/ripple_app/ledger/Ledger.cpp index 511c580da5..dd5f26451d 100644 --- a/src/ripple_app/ledger/Ledger.cpp +++ b/src/ripple_app/ledger/Ledger.cpp @@ -589,7 +589,7 @@ bool Ledger::saveValidatedLedger (bool current) Serializer s (128); s.add32 (HashPrefix::ledgerMaster); addRaw (s); - getApp().getNodeStore ().store (hotLEDGER, mLedgerSeq, s.modData (), mHash); + getApp().getNodeStore ().store (hotLEDGER, mLedgerSeq, std::move (s.modData ()), mHash); } AcceptedLedger::pointer aLedger; diff --git a/src/ripple_app/node/SqliteFactory.cpp b/src/ripple_app/node/SqliteFactory.cpp index 6a3c702f79..48cebc31d1 100644 --- a/src/ripple_app/node/SqliteFactory.cpp +++ b/src/ripple_app/node/SqliteFactory.cpp @@ -95,7 +95,7 @@ public: *pObject = NodeObject::createObject ( getTypeFromString (pSt.peekString (0)), pSt.getUInt32 (1), - data, + std::move(data), hash); } else @@ -145,9 +145,9 @@ public: pStE.reset(); } - void visitAll (NodeStore::VisitCallback& callback) + void for_each (std::function f) { - // No lock needed as per the visitAll() API + // No lock needed as per the for_each() API uint256 hash; @@ -164,10 +164,10 @@ public: NodeObject::Ptr const object (NodeObject::createObject ( getTypeFromString (pSt.peekString (0)), pSt.getUInt32 (1), - data, + std::move(data), hash)); - callback.visitObject (object); + f (object); } pSt.reset (); diff --git a/src/ripple_app/shamap/SHAMap.cpp b/src/ripple_app/shamap/SHAMap.cpp index 60c5da34e6..54ab748d3b 100644 --- a/src/ripple_app/shamap/SHAMap.cpp +++ b/src/ripple_app/shamap/SHAMap.cpp @@ -1124,7 +1124,7 @@ int SHAMap::flushDirty (NodeMap& map, int maxNodes, NodeObjectType t, std::uint3 #endif - getApp().getNodeStore ().store (t, seq, s.modData (), it->second->getNodeHash ()); + getApp().getNodeStore ().store (t, seq, std::move (s.modData ()), it->second->getNodeHash ()); if (flushed++ >= maxNodes) return flushed; diff --git a/src/ripple_app/shamap/SHAMapSyncFilters.cpp b/src/ripple_app/shamap/SHAMapSyncFilters.cpp index 82e3ac91a9..904698ed70 100644 --- a/src/ripple_app/shamap/SHAMapSyncFilters.cpp +++ b/src/ripple_app/shamap/SHAMapSyncFilters.cpp @@ -90,7 +90,7 @@ void AccountStateSF::gotNode (bool fromFilter, Blob& nodeData, SHAMapTreeNode::TNType) { - getApp().getNodeStore ().store (hotACCOUNT_NODE, mLedgerSeq, nodeData, nodeHash); + getApp().getNodeStore ().store (hotACCOUNT_NODE, mLedgerSeq, std::move (nodeData), nodeHash); } bool AccountStateSF::haveNode (SHAMapNode const& id, @@ -116,7 +116,7 @@ void TransactionStateSF::gotNode (bool fromFilter, getApp().getNodeStore ().store ( (type == SHAMapTreeNode::tnTRANSACTION_NM) ? hotTRANSACTION : hotTRANSACTION_NODE, mLedgerSeq, - nodeData, + std::move (nodeData), nodeHash); } diff --git a/src/ripple_core/nodestore/README.md b/src/ripple_core/nodestore/README.md new file mode 100644 index 0000000000..6ff4526c67 --- /dev/null +++ b/src/ripple_core/nodestore/README.md @@ -0,0 +1,25 @@ + +# NodeStore + +## Introduction + +The NodeStore provides an interface that stores, in a persistent database, the collection of +NodeObject that rippled uses as its primary representation of ledger items. + +## Module directory structure + +nodestore +|-api // Public Interface +| +|-backend // Factory classes for various databases +| +|-impl // Private Implementation +| +|-test // Unit tests + +The NodeStore class is a simple object that the Ledger uses to store entries. It has a enumeration type, a hash, a ledger index and a Blob which stores arbritary data. + + +# Document WIP notes + +If the MemoryFactory backend database is used, do we loose persistance? \ No newline at end of file diff --git a/src/ripple_core/nodestore/api/Backend.h b/src/ripple_core/nodestore/api/Backend.h index 20a612e5ad..bd889093df 100644 --- a/src/ripple_core/nodestore/api/Backend.h +++ b/src/ripple_core/nodestore/api/Backend.h @@ -76,11 +76,9 @@ public: This is usually called during import. @note This routine will not be called concurrently with itself or other methods. - @see import, VisitCallback + @see import */ - virtual void visitAll (VisitCallback& callback) = 0; - // VFALCO TODO Implement - //virtual void visitAll (std::function f) = 0; + virtual void for_each (std::function f) = 0; /** Estimate the number of write operations pending. */ virtual int getWriteLoad () = 0; diff --git a/src/ripple_core/nodestore/api/Database.h b/src/ripple_core/nodestore/api/Database.h index 24f5723c7f..96631258f1 100644 --- a/src/ripple_core/nodestore/api/Database.h +++ b/src/ripple_core/nodestore/api/Database.h @@ -98,7 +98,7 @@ public: */ virtual void store (NodeObjectType type, std::uint32_t ledgerIndex, - Blob& data, + Blob&& data, uint256 const& hash) = 0; /** Visit every object in the database @@ -108,10 +108,10 @@ public: or other methods. @see import */ - virtual void visitAll (VisitCallback& callback) = 0; + virtual void for_each(std::function f) = 0; /** Import objects from another database. */ - virtual void import (Database& sourceDatabase) = 0; + virtual void import (Database& source) = 0; /** Retrieve the estimated number of pending write operations. This is used for diagnostics. diff --git a/src/ripple_core/nodestore/api/NodeObject.h b/src/ripple_core/nodestore/api/NodeObject.h index c7518bc4d8..290ff011ec 100644 --- a/src/ripple_core/nodestore/api/NodeObject.h +++ b/src/ripple_core/nodestore/api/NodeObject.h @@ -77,7 +77,7 @@ public: // This constructor is private, use createObject instead. NodeObject (NodeObjectType type, LedgerIndex ledgerIndex, - Blob& data, + Blob&& data, uint256 const& hash, PrivateAccess); @@ -94,7 +94,7 @@ public: */ static Ptr createObject (NodeObjectType type, LedgerIndex ledgerIndex, - Blob& data, + Blob&& data, uint256 const& hash); /** Retrieve the type of this object. diff --git a/src/ripple_core/nodestore/backend/HyperDBFactory.cpp b/src/ripple_core/nodestore/backend/HyperDBFactory.cpp index d8202bc74e..e7791a8340 100644 --- a/src/ripple_core/nodestore/backend/HyperDBFactory.cpp +++ b/src/ripple_core/nodestore/backend/HyperDBFactory.cpp @@ -86,14 +86,16 @@ public: { } - std::string getName() + std::string + getName() { return m_name; } //-------------------------------------------------------------------------- - Status fetch (void const* key, NodeObject::Ptr* pObject) + Status + fetch (void const* key, NodeObject::Ptr* pObject) { pObject->reset (); @@ -140,21 +142,22 @@ public: return status; } - void store (NodeObject::ref object) + void + store (NodeObject::ref object) { m_batch.store (object); } - void storeBatch (Batch const& batch) + void + storeBatch (Batch const& batch) { hyperleveldb::WriteBatch wb; EncodedBlob encoded; - // VFALCO Use range based for - BOOST_FOREACH (NodeObject::ref object, batch) + for (auto const& e : batch) { - encoded.prepare (object); + encoded.prepare (e); wb.Put ( hyperleveldb::Slice (reinterpret_cast ( @@ -168,7 +171,8 @@ public: m_db->Write (options, &wb).ok (); } - void visitAll (VisitCallback& callback) + void + for_each (std::function f) { hyperleveldb::ReadOptions const options; @@ -183,9 +187,7 @@ public: if (decoded.wasOk ()) { - NodeObject::Ptr object (decoded.createObject ()); - - callback.visitObject (object); + f (decoded.createObject ()); } else { @@ -204,14 +206,16 @@ public: } } - int getWriteLoad () + int + getWriteLoad () { return m_batch.getWriteLoad (); } //-------------------------------------------------------------------------- - void writeBatch (Batch const& batch) + void + writeBatch (Batch const& batch) { storeBatch (batch); } @@ -222,13 +226,18 @@ public: class HyperDBFactory : public NodeStore::Factory { public: - beast::String getName () const + beast::String + getName () const { return "HyperLevelDB"; } - std::unique_ptr createInstance (size_t keyBytes, - Parameters const& keyValues, Scheduler& scheduler, beast::Journal journal) + std::unique_ptr + createInstance ( + size_t keyBytes, + Parameters const& keyValues, + Scheduler& scheduler, + beast::Journal journal) { return std::make_unique ( keyBytes, keyValues, scheduler, journal); @@ -237,7 +246,8 @@ public: //------------------------------------------------------------------------------ -std::unique_ptr make_HyperDBFactory () +std::unique_ptr +make_HyperDBFactory () { return std::make_unique (); } diff --git a/src/ripple_core/nodestore/backend/LevelDBFactory.cpp b/src/ripple_core/nodestore/backend/LevelDBFactory.cpp index 31ebe9d372..edb0fc59d9 100644 --- a/src/ripple_core/nodestore/backend/LevelDBFactory.cpp +++ b/src/ripple_core/nodestore/backend/LevelDBFactory.cpp @@ -79,14 +79,16 @@ public: m_db.reset (db); } - std::string getName() + std::string + getName() { return m_name; } //-------------------------------------------------------------------------- - Status fetch (void const* key, NodeObject::Ptr* pObject) + Status + fetch (void const* key, NodeObject::Ptr* pObject) { pObject->reset (); @@ -132,20 +134,22 @@ public: return status; } - void store (NodeObject::ref object) + void + store (NodeObject::ref object) { m_batch.store (object); } - void storeBatch (Batch const& batch) + void + storeBatch (Batch const& batch) { leveldb::WriteBatch wb; EncodedBlob encoded; - BOOST_FOREACH (NodeObject::ref object, batch) + for (auto const& e : batch) { - encoded.prepare (object); + encoded.prepare (e); wb.Put ( leveldb::Slice (reinterpret_cast ( @@ -159,7 +163,8 @@ public: m_db->Write (options, &wb).ok (); } - void visitAll (VisitCallback& callback) + void + for_each (std::function f) { leveldb::ReadOptions const options; @@ -175,33 +180,35 @@ public: if (decoded.wasOk ()) { - NodeObject::Ptr object (decoded.createObject ()); - - callback.visitObject (object); + f (decoded.createObject ()); } else { // Uh oh, corrupted data! - WriteLog (lsFATAL, NodeObject) << "Corrupt NodeObject #" << uint256 (it->key ().data ()); + if (m_journal.fatal) m_journal.fatal << + "Corrupt NodeObject #" << uint256(it->key ().data ()); } } else { // VFALCO NOTE What does it mean to find an // incorrectly sized key? Corruption? - WriteLog (lsFATAL, NodeObject) << "Bad key size = " << it->key ().size (); + if (m_journal.fatal) m_journal.fatal << + "Bad key size = " << it->key().size(); } } } - int getWriteLoad () + int + getWriteLoad () { return m_batch.getWriteLoad (); } //-------------------------------------------------------------------------- - void writeBatch (Batch const& batch) + void + writeBatch (Batch const& batch) { storeBatch (batch); } @@ -230,12 +237,14 @@ public: { } - beast::String getName () const + beast::String + getName () const { return "LevelDB"; } - std::unique_ptr createInstance ( + std::unique_ptr + createInstance( size_t keyBytes, Parameters const& keyValues, Scheduler& scheduler, @@ -248,7 +257,8 @@ public: //------------------------------------------------------------------------------ -std::unique_ptr make_LevelDBFactory () +std::unique_ptr +make_LevelDBFactory () { return std::make_unique (); } diff --git a/src/ripple_core/nodestore/backend/MemoryFactory.cpp b/src/ripple_core/nodestore/backend/MemoryFactory.cpp index 1108a91100..022b9ed465 100644 --- a/src/ripple_core/nodestore/backend/MemoryFactory.cpp +++ b/src/ripple_core/nodestore/backend/MemoryFactory.cpp @@ -41,14 +41,16 @@ public: { } - std::string getName () + std::string + getName () { return "memory"; } //-------------------------------------------------------------------------- - Status fetch (void const* key, NodeObject::Ptr* pObject) + Status + fetch (void const* key, NodeObject::Ptr* pObject) { uint256 const hash (uint256::fromVoid (key)); @@ -66,7 +68,8 @@ public: return ok; } - void store (NodeObject::ref object) + void + store (NodeObject::ref object) { Map::iterator iter = m_map.find (object->getHash ()); @@ -76,19 +79,22 @@ public: } } - void storeBatch (Batch const& batch) + void + storeBatch (Batch const& batch) { - for (std::size_t i = 0; i < batch.size (); ++i) - store (batch [i]); + for (auto const& e : batch) + store (e); } - void visitAll (VisitCallback& callback) + void + for_each (std::function f) { - for (Map::const_iterator iter = m_map.begin (); iter != m_map.end (); ++iter) - callback.visitObject (iter->second); + for (auto const& e : m_map) + f (e.second); } - int getWriteLoad () + int + getWriteLoad () { return 0; } @@ -99,14 +105,18 @@ public: class MemoryFactory : public Factory { public: - beast::String getName () const + beast::String + getName () const { return "Memory"; } - std::unique_ptr createInstance ( - size_t keyBytes, Parameters const& keyValues, - Scheduler& scheduler, beast::Journal journal) + std::unique_ptr + createInstance ( + size_t keyBytes, + Parameters const& keyValues, + Scheduler& scheduler, + beast::Journal journal) { return std::make_unique ( keyBytes, keyValues, scheduler, journal); @@ -115,7 +125,8 @@ public: //------------------------------------------------------------------------------ -std::unique_ptr make_MemoryFactory () +std::unique_ptr +make_MemoryFactory () { return std::make_unique (); } diff --git a/src/ripple_core/nodestore/backend/NullFactory.cpp b/src/ripple_core/nodestore/backend/NullFactory.cpp index eb90d2642e..aaeb2b50da 100644 --- a/src/ripple_core/nodestore/backend/NullFactory.cpp +++ b/src/ripple_core/nodestore/backend/NullFactory.cpp @@ -31,29 +31,35 @@ public: { } - std::string getName() + std::string + getName() { return std::string (); } - Status fetch (void const*, NodeObject::Ptr*) + Status + fetch (void const*, NodeObject::Ptr*) { return notFound; } - void store (NodeObject::ref object) + void + store (NodeObject::ref object) { } - void storeBatch (Batch const& batch) + void + storeBatch (Batch const& batch) { } - void visitAll (VisitCallback& callback) + void + for_each (std::function f) { } - int getWriteLoad () + int + getWriteLoad () { return 0; } @@ -71,8 +77,11 @@ public: return "none"; } - std::unique_ptr createInstance ( - size_t, Parameters const&, Scheduler&, beast::Journal) + std::unique_ptr + createInstance ( + size_t, + Parameters const&, + Scheduler&, beast::Journal) { return std::make_unique (); } diff --git a/src/ripple_core/nodestore/backend/RocksDBFactory.cpp b/src/ripple_core/nodestore/backend/RocksDBFactory.cpp index 3c0ea06bac..2899500f6b 100644 --- a/src/ripple_core/nodestore/backend/RocksDBFactory.cpp +++ b/src/ripple_core/nodestore/backend/RocksDBFactory.cpp @@ -45,8 +45,10 @@ public: void (*f)(void*); void* a; }; - - static void thread_entry (void* ptr) + + static + void + thread_entry (void* ptr) { ThreadParams* const p (reinterpret_cast (ptr)); void (*f)(void*) = p->f; @@ -62,7 +64,8 @@ public: (*f)(a); } - void StartThread (void (*f)(void*), void* a) + void + StartThread (void (*f)(void*), void* a) { ThreadParams* const p (new ThreadParams (f, a)); EnvWrapper::StartThread (&RocksDBEnv::thread_entry, p); @@ -164,14 +167,16 @@ public: { } - std::string getName() + std::string + getName() { return m_name; } //-------------------------------------------------------------------------- - Status fetch (void const* key, NodeObject::Ptr* pObject) + Status + fetch (void const* key, NodeObject::Ptr* pObject) { pObject->reset (); @@ -220,20 +225,22 @@ public: return status; } - void store (NodeObject::ref object) + void + store (NodeObject::ref object) { m_batch.store (object); } - void storeBatch (Batch const& batch) + void + storeBatch (Batch const& batch) { rocksdb::WriteBatch wb; EncodedBlob encoded; - BOOST_FOREACH (NodeObject::ref object, batch) + for (auto const& e : batch) { - encoded.prepare (object); + encoded.prepare (e); wb.Put ( rocksdb::Slice (reinterpret_cast ( @@ -247,7 +254,8 @@ public: m_db->Write (options, &wb).ok (); } - void visitAll (VisitCallback& callback) + void + for_each (std::function f) { rocksdb::ReadOptions const options; @@ -263,33 +271,35 @@ public: if (decoded.wasOk ()) { - NodeObject::Ptr object (decoded.createObject ()); - - callback.visitObject (object); + f (decoded.createObject ()); } else { // Uh oh, corrupted data! - WriteLog (lsFATAL, NodeObject) << "Corrupt NodeObject #" << uint256 (it->key ().data ()); + if (m_journal.fatal) m_journal.fatal << + "Corrupt NodeObject #" << uint256 (it->key ().data ()); } } else { // VFALCO NOTE What does it mean to find an // incorrectly sized key? Corruption? - WriteLog (lsFATAL, NodeObject) << "Bad key size = " << it->key ().size (); + if (m_journal.fatal) m_journal.fatal << + "Bad key size = " << it->key ().size (); } } } - int getWriteLoad () + int + getWriteLoad () { return m_batch.getWriteLoad (); } //-------------------------------------------------------------------------- - void writeBatch (Batch const& batch) + void + writeBatch (Batch const& batch) { storeBatch (batch); } @@ -317,14 +327,18 @@ public: { } - beast::String getName () const + beast::String + getName () const { return "RocksDB"; } - std::unique_ptr createInstance ( - size_t keyBytes, Parameters const& keyValues, - Scheduler& scheduler, beast::Journal journal) + std::unique_ptr + createInstance ( + size_t keyBytes, + Parameters const& keyValues, + Scheduler& scheduler, + beast::Journal journal) { return std::make_unique ( keyBytes, keyValues, scheduler, journal, &m_env); @@ -333,7 +347,8 @@ public: //------------------------------------------------------------------------------ -std::unique_ptr make_RocksDBFactory () +std::unique_ptr +make_RocksDBFactory () { return std::make_unique (); } diff --git a/src/ripple_core/nodestore/impl/BatchWriter.cpp b/src/ripple_core/nodestore/impl/BatchWriter.cpp index 814ccd5e5d..df4e153ff9 100644 --- a/src/ripple_core/nodestore/impl/BatchWriter.cpp +++ b/src/ripple_core/nodestore/impl/BatchWriter.cpp @@ -34,7 +34,8 @@ BatchWriter::~BatchWriter () waitForWriting (); } -void BatchWriter::store (NodeObject::ref object) +void +BatchWriter::store (NodeObject::ref object) { std::lock_guard sl (mWriteMutex); @@ -48,19 +49,22 @@ void BatchWriter::store (NodeObject::ref object) } } -int BatchWriter::getWriteLoad () +int +BatchWriter::getWriteLoad () { std::lock_guard sl (mWriteMutex); return std::max (mWriteLoad, static_cast (mWriteSet.size ())); } -void BatchWriter::performScheduledTask () +void +BatchWriter::performScheduledTask () { writeBatch (); } -void BatchWriter::writeBatch () +void +BatchWriter::writeBatch () { for (;;) { @@ -90,7 +94,8 @@ void BatchWriter::writeBatch () } } -void BatchWriter::waitForWriting () +void +BatchWriter::waitForWriting () { std::unique_lock sl (mWriteMutex); diff --git a/src/ripple_core/nodestore/impl/DatabaseImp.h b/src/ripple_core/nodestore/impl/DatabaseImp.h index 8743475270..92318902f6 100644 --- a/src/ripple_core/nodestore/impl/DatabaseImp.h +++ b/src/ripple_core/nodestore/impl/DatabaseImp.h @@ -85,8 +85,8 @@ public: m_readGenCondVar.notify_all (); } - BOOST_FOREACH (std::thread& th, m_readThreads) - th.join (); + for (auto& e : m_readThreads) + e.join(); } beast::String getName () const @@ -94,7 +94,6 @@ public: return m_backend->getName (); } - //------------------------------------------------------------------------------ bool asyncFetch (uint256 const& hash, NodeObject::pointer& object) @@ -190,14 +189,15 @@ public: if (! foundInFastBackend) { - // If we have a fast back end, store it there for later. + // If we have a fast back end, store it there for later. // if (m_fastBackend != nullptr) m_fastBackend->store (obj); // Since this was a 'hard' fetch, we will log it. // - WriteLog (lsTRACE, NodeObject) << "HOS: " << hash << " fetch: in db"; + if (m_journal.trace) m_journal.trace << + "HOS: " << hash << " fetch: in db"; } } @@ -220,11 +220,13 @@ public: case dataCorrupt: // VFALCO TODO Deal with encountering corrupt data! // - WriteLog (lsFATAL, NodeObject) << "Corrupt NodeObject #" << hash; + if (m_journal.fatal) m_journal.fatal << + "Corrupt NodeObject #" << hash; break; default: - WriteLog (lsWARNING, NodeObject) << "Unknown status=" << status; + if (m_journal.warning) m_journal.warning << + "Unknown status=" << status; break; } @@ -235,10 +237,10 @@ public: void store (NodeObjectType type, std::uint32_t index, - Blob& data, + Blob&& data, uint256 const& hash) { - NodeObject::Ptr object = NodeObject::createObject (type, index, data, hash); + NodeObject::Ptr object = NodeObject::createObject(type, index, std::move(data), hash); #if RIPPLE_VERIFY_NODEOBJECT_KEYS assert (hash == Serializer::getSHA512Half (data)); @@ -326,52 +328,30 @@ public: //------------------------------------------------------------------------------ - - void visitAll (VisitCallback& callback) + void for_each (std::function f) { - m_backend->visitAll (callback); + m_backend->for_each (f); } - void import (Database& sourceDatabase) + void import (Database& source) { - class ImportVisitCallback : public VisitCallback + Batch b; + b.reserve (batchWritePreallocationSize); + + source.for_each ([&](NodeObject::Ptr object) { - public: - explicit ImportVisitCallback (Backend& backend) - : m_backend (backend) + if (b.size () >= batchWritePreallocationSize) { - m_objects.reserve (batchWritePreallocationSize); + this->m_backend->storeBatch (b); + b.clear (); + b.reserve (batchWritePreallocationSize); } - ~ImportVisitCallback () - { - if (! m_objects.empty ()) - m_backend.storeBatch (m_objects); - } + b.push_back (object); + }); - void visitObject (NodeObject::Ptr const& object) - { - if (m_objects.size () >= batchWritePreallocationSize) - { - m_backend.storeBatch (m_objects); - - m_objects.clear (); - m_objects.reserve (batchWritePreallocationSize); - } - - m_objects.push_back (object); - } - - private: - Backend& m_backend; - Batch m_objects; - }; - - //-------------------------------------------------------------------------- - - ImportVisitCallback callback (*m_backend); - - sourceDatabase.visitAll (callback); + if (! b.empty ()) + m_backend->storeBatch (b); } }; diff --git a/src/ripple_core/nodestore/impl/DecodedBlob.cpp b/src/ripple_core/nodestore/impl/DecodedBlob.cpp index 2c0afb4566..200f963fa2 100644 --- a/src/ripple_core/nodestore/impl/DecodedBlob.cpp +++ b/src/ripple_core/nodestore/impl/DecodedBlob.cpp @@ -87,7 +87,7 @@ NodeObject::Ptr DecodedBlob::createObject () memcpy (data.data (), m_objectData, m_dataBytes); object = NodeObject::createObject ( - m_objectType, m_ledgerIndex, data, uint256::fromVoid (m_key)); + m_objectType, m_ledgerIndex, std::move(data), uint256::fromVoid(m_key)); } return object; diff --git a/src/ripple_core/nodestore/impl/DummyScheduler.cpp b/src/ripple_core/nodestore/impl/DummyScheduler.cpp index f757a5b954..2da446dc25 100644 --- a/src/ripple_core/nodestore/impl/DummyScheduler.cpp +++ b/src/ripple_core/nodestore/impl/DummyScheduler.cpp @@ -28,13 +28,15 @@ DummyScheduler::~DummyScheduler () { } -void DummyScheduler::scheduleTask (Task& task) +void +DummyScheduler::scheduleTask (Task& task) { // Invoke the task synchronously. task.performScheduledTask(); } -void DummyScheduler::scheduledTasksStopped () +void +DummyScheduler::scheduledTasksStopped () { } diff --git a/src/ripple_core/nodestore/impl/EncodedBlob.cpp b/src/ripple_core/nodestore/impl/EncodedBlob.cpp index 2c064a8bd6..a35786def7 100644 --- a/src/ripple_core/nodestore/impl/EncodedBlob.cpp +++ b/src/ripple_core/nodestore/impl/EncodedBlob.cpp @@ -20,7 +20,8 @@ namespace ripple { namespace NodeStore { -void EncodedBlob::prepare (NodeObject::Ptr const& object) +void +EncodedBlob::prepare (NodeObject::Ptr const& object) { m_key = object->getHash ().begin (); diff --git a/src/ripple_core/nodestore/impl/Manager.cpp b/src/ripple_core/nodestore/impl/Manager.cpp index 4bb37e3623..9c6819e61f 100644 --- a/src/ripple_core/nodestore/impl/Manager.cpp +++ b/src/ripple_core/nodestore/impl/Manager.cpp @@ -38,12 +38,14 @@ public: { } - void add_factory (std::unique_ptr factory) + void + add_factory (std::unique_ptr factory) { m_list.emplace_back (std::move (factory)); } - void add_known_factories () + void + add_known_factories () { // This is part of the ripple_app module since it has dependencies //addFactory (make_SqliteFactory ()); @@ -62,7 +64,8 @@ public: #endif } - Factory* find (std::string const& name) const + Factory* + find (std::string const& name) const { for (List::const_iterator iter (m_list.begin ()); iter != m_list.end (); ++iter) @@ -71,7 +74,8 @@ public: return nullptr; } - static void missing_backend () + static void + missing_backend () { throw std::runtime_error ( "Your rippled.cfg is missing a [node_db] entry, " @@ -79,8 +83,11 @@ public: ); } - std::unique_ptr make_Backend (Parameters const& parameters, - Scheduler& scheduler, beast::Journal journal) + std::unique_ptr + make_Backend ( + Parameters const& parameters, + Scheduler& scheduler, + beast::Journal journal) { std::unique_ptr backend; @@ -108,10 +115,14 @@ public: return backend; } - std::unique_ptr make_Database (std::string const& name, - Scheduler& scheduler, beast::Journal journal, int readThreads, - Parameters const& backendParameters, - Parameters fastBackendParameters) + std::unique_ptr + make_Database ( + std::string const& name, + Scheduler& scheduler, + beast::Journal journal, + int readThreads, + Parameters const& backendParameters, + Parameters fastBackendParameters) { std::unique_ptr backend (make_Backend ( backendParameters, scheduler, journal)); @@ -132,8 +143,8 @@ Manager::~Manager () { } -std::unique_ptr make_Manager ( - std::vector > factories) +std::unique_ptr +make_Manager (std::vector > factories) { return std::make_unique (std::move (factories)); } diff --git a/src/ripple_core/nodestore/impl/NodeObject.cpp b/src/ripple_core/nodestore/impl/NodeObject.cpp index 359cf40db4..22896b59b1 100644 --- a/src/ripple_core/nodestore/impl/NodeObject.cpp +++ b/src/ripple_core/nodestore/impl/NodeObject.cpp @@ -26,50 +26,52 @@ SETUP_LOG (NodeObject) NodeObject::NodeObject ( NodeObjectType type, LedgerIndex ledgerIndex, - Blob& data, + Blob&& data, uint256 const& hash, PrivateAccess) : mType (type) , mHash (hash) , mLedgerIndex (ledgerIndex) { - // Take over the caller's buffer - mData.swap (data); + mData = std::move (data); } NodeObject::Ptr NodeObject::createObject ( NodeObjectType type, LedgerIndex ledgerIndex, - Blob& data, + Blob&& data, uint256 const & hash) { - // The boost::ref is important or - // else it will be passed by value! return boost::make_shared ( - type, ledgerIndex, boost::ref (data), hash, PrivateAccess ()); + type, ledgerIndex, std::move (data), hash, PrivateAccess ()); } -NodeObjectType NodeObject::getType () const +NodeObjectType +NodeObject::getType () const { return mType; } -uint256 const& NodeObject::getHash () const +uint256 const& +NodeObject::getHash () const { return mHash; } -LedgerIndex NodeObject::getIndex () const +LedgerIndex +NodeObject::getIndex () const { return mLedgerIndex; } -Blob const& NodeObject::getData () const +Blob const& +NodeObject::getData () const { return mData; } -bool NodeObject::isCloneOf (NodeObject::Ptr const& other) const +bool +NodeObject::isCloneOf (NodeObject::Ptr const& other) const { if (mType != other->mType) return false; diff --git a/src/ripple_core/nodestore/tests/TestBase.h b/src/ripple_core/nodestore/tests/TestBase.h index c72cd51734..c812bdd839 100644 --- a/src/ripple_core/nodestore/tests/TestBase.h +++ b/src/ripple_core/nodestore/tests/TestBase.h @@ -75,7 +75,7 @@ public: r.fillBitsRandomly (data.data (), payloadBytes); - return NodeObject::createObject (type, ledgerIndex, data, hash); + return NodeObject::createObject(type, ledgerIndex, std::move(data), hash); } private: @@ -163,7 +163,7 @@ public: db.store (object->getType (), object->getIndex (), - data, + std::move (data), object->getHash ()); } }