From ef232f43590c1f6d8da72c40235cdbe724ead8f7 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 7 Jun 2013 13:44:15 -0700 Subject: [PATCH] Split up and refactor HashedObject --- modules/ripple_main/ripple_main.cpp | 15 ++- newcoin.vcxproj | 22 ++-- newcoin.vcxproj.filters | 21 +++- src/cpp/ripple/Application.cpp | 22 ---- src/cpp/ripple/Application.h | 17 +-- src/cpp/ripple/HashedObject.h | 114 ------------------ src/cpp/ripple/HttpsClient.cpp | 44 +++---- src/cpp/ripple/HttpsClient.h | 97 +++++++-------- src/cpp/ripple/Ledger.h | 14 +-- src/cpp/ripple/OrderBook.h | 23 ++-- src/cpp/ripple/OrderBookDB.h | 30 +++-- src/cpp/ripple/SHAMap.cpp | 5 - src/cpp/ripple/SHAMap.h | 15 +-- src/cpp/ripple/SHAMapDiff.cpp | 4 - src/cpp/ripple/SHAMapNodes.cpp | 14 --- src/cpp/ripple/ripple_HashedObject.cpp | 14 +++ src/cpp/ripple/ripple_HashedObject.h | 105 ++++++++++++++++ ...bject.cpp => ripple_HashedObjectStore.cpp} | 52 ++++---- src/cpp/ripple/ripple_HashedObjectStore.h | 76 ++++++++++++ src/cpp/ripple/ripple_Peer.h | 11 -- src/cpp/ripple/ripple_UptimeTimerAdapter.h | 14 +++ 21 files changed, 385 insertions(+), 344 deletions(-) delete mode 100644 src/cpp/ripple/HashedObject.h create mode 100644 src/cpp/ripple/ripple_HashedObject.cpp create mode 100644 src/cpp/ripple/ripple_HashedObject.h rename src/cpp/ripple/{HashedObject.cpp => ripple_HashedObjectStore.cpp} (91%) create mode 100644 src/cpp/ripple/ripple_HashedObjectStore.h create mode 100644 src/cpp/ripple/ripple_UptimeTimerAdapter.h diff --git a/modules/ripple_main/ripple_main.cpp b/modules/ripple_main/ripple_main.cpp index cdba2c6a0f..bc9b95cd36 100644 --- a/modules/ripple_main/ripple_main.cpp +++ b/modules/ripple_main/ripple_main.cpp @@ -67,10 +67,10 @@ //------------------------------------------------------------------------------ // VFALCO: TODO, prepare a unity header for LevelDB -#ifdef USE_LEVELDB #include "leveldb/cache.h" #include "leveldb/filter_policy.h" -#endif +#include "leveldb/db.h" +#include "leveldb/write_batch.h" //------------------------------------------------------------------------------ @@ -93,6 +93,7 @@ #include "src/cpp/ripple/Ledger.h" #include "src/cpp/ripple/SerializedValidation.h" #include "src/cpp/database/SqliteDatabase.h" +#include "src/cpp/ripple/LoadManager.h" //------------------------------------------------------------------------------ // @@ -105,6 +106,9 @@ #include "src/cpp/ripple/ripple_ProofOfWork.h" #include "src/cpp/ripple/ripple_Job.h" #include "src/cpp/ripple/ripple_JobQueue.h" +#include "src/cpp/ripple/ripple_UptimeTimerAdapter.h" +#include "src/cpp/ripple/ripple_HashedObject.h" +#include "src/cpp/ripple/ripple_HashedObjectStore.h" #include "src/cpp/ripple/ripple_IFeatures.h" #include "src/cpp/ripple/ripple_IFeeVote.h" @@ -139,7 +143,6 @@ #include "src/cpp/ripple/ChangeTransactor.h" #include "src/cpp/ripple/HTTPRequest.h" #include "src/cpp/ripple/HashPrefixes.h" -#include "src/cpp/ripple/HashedObject.h" #include "src/cpp/ripple/HttpsClient.h" #include "src/cpp/ripple/Ledger.h" #include "src/cpp/ripple/LedgerAcquire.h" @@ -149,7 +152,6 @@ #include "src/cpp/ripple/LedgerMaster.h" #include "src/cpp/ripple/LedgerProposal.h" #include "src/cpp/ripple/LedgerTiming.h" -#include "src/cpp/ripple/LoadManager.h" #include "src/cpp/ripple/NetworkOPs.h" #include "src/cpp/ripple/NicknameState.h" #include "src/cpp/ripple/Offer.h" @@ -224,7 +226,6 @@ static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength) #include "src/cpp/ripple/ChangeTransactor.cpp" // no log #include "src/cpp/ripple/Contract.cpp" // no log #include "src/cpp/ripple/DBInit.cpp" -#include "src/cpp/ripple/HashedObject.cpp" #include "src/cpp/ripple/HTTPRequest.cpp" #include "src/cpp/ripple/HttpsClient.cpp" #include "src/cpp/ripple/Interpreter.cpp" // no log @@ -288,10 +289,12 @@ static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength) // Refactored sources -#include "src/cpp/ripple/ripple_Config.cpp" // no log +#include "src/cpp/ripple/ripple_Config.cpp" #include "src/cpp/ripple/ripple_DatabaseCon.cpp" #include "src/cpp/ripple/ripple_Features.cpp" #include "src/cpp/ripple/ripple_FeeVote.cpp" +#include "src/cpp/ripple/ripple_HashedObject.cpp" +#include "src/cpp/ripple/ripple_HashedObjectStore.cpp" #include "src/cpp/ripple/ripple_HashRouter.cpp" #include "src/cpp/ripple/ripple_Job.cpp" #include "src/cpp/ripple/ripple_JobQueue.cpp" diff --git a/newcoin.vcxproj b/newcoin.vcxproj index 84ad582e9f..190b15b75e 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -758,6 +758,18 @@ true true + + true + true + true + true + + + true + true + true + true + true true @@ -776,12 +788,6 @@ true true - - true - true - true - true - true true @@ -1645,9 +1651,10 @@ + + - @@ -1692,6 +1699,7 @@ + diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index a521911fbd..bfb5b952e4 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -462,9 +462,6 @@ 1. Modules\ripple_main\_unfactored\contracts - - 1. Modules\ripple_main\_unfactored\types - 1. Modules\ripple_main\_unfactored\types @@ -813,6 +810,12 @@ 1. Modules\ripple_main\refactored + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + @@ -1130,9 +1133,6 @@ 1. Modules\ripple_main\_unfactored\contracts - - 1. Modules\ripple_main\_unfactored\types - 1. Modules\ripple_main\_unfactored\types @@ -1511,6 +1511,15 @@ 1. Modules\ripple_main\refactored + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + + + 1. Modules\ripple_main\refactored + diff --git a/src/cpp/ripple/Application.cpp b/src/cpp/ripple/Application.cpp index 04eaeb41f8..213f15ed79 100644 --- a/src/cpp/ripple/Application.cpp +++ b/src/cpp/ripple/Application.cpp @@ -1,23 +1,4 @@ -/* -#include "Application.h" - -#include "leveldb/cache.h" -#include "leveldb/filter_policy.h" - -#include "AcceptedLedger.h" -#include "PeerDoor.h" -#include "RPCDoor.h" - -#include "../database/SqliteDatabase.h" - -#include - -#include -#include -#include -*/ - // VFALCO: TODO Replace these with beast "unsigned long long" generators #define SYSTEM_CURRENCY_GIFT 1000ull #define SYSTEM_CURRENCY_USERS 100000000ull @@ -58,10 +39,7 @@ Application::Application () , mNetNodeDB (NULL) , mPathFindDB (NULL) , mHashNodeDB (NULL) - // VFALCO: TODO eliminate USE_LEVELDB macro -#ifdef USE_LEVELDB , mHashNodeLDB (NULL) -#endif , mPeerDoor (NULL) , mRPCDoor (NULL) , mWSPublicDoor (NULL) diff --git a/src/cpp/ripple/Application.h b/src/cpp/ripple/Application.h index 5ab15c2b3f..0337c0327b 100644 --- a/src/cpp/ripple/Application.h +++ b/src/cpp/ripple/Application.h @@ -33,8 +33,9 @@ class IPeers; class RPCDoor; class PeerDoor; -typedef TaggedCache< uint256, std::vector, UptimeTimerAdapter> NodeCache; -typedef TaggedCache< uint256, SLE, UptimeTimerAdapter> SLECache; + +typedef TaggedCache , UptimeTimerAdapter> NodeCache; +typedef TaggedCache SLECache; class Application { @@ -92,7 +93,11 @@ public: void sweep(); private: - boost::asio::io_service mIOService; + void updateTables (bool); + void startNewLedger (); + bool loadOldLedger (const std::string&); + + boost::asio::io_service mIOService; boost::asio::io_service mAuxService; boost::asio::io_service::work mIOWork; boost::asio::io_service::work mAuxWork; @@ -112,7 +117,7 @@ private: LoadManager mLoadMgr; TXQueue mTxnQueue; OrderBookDB mOrderBookDB; - + // VFALCO: Clean stuff beast::ScopedPointer mFeatures; beast::ScopedPointer mFeeVote; @@ -140,10 +145,6 @@ private: boost::recursive_mutex mPeerMapLock; volatile bool mShutdown; - - void updateTables(bool); - void startNewLedger(); - bool loadOldLedger(const std::string&); }; extern Application* theApp; diff --git a/src/cpp/ripple/HashedObject.h b/src/cpp/ripple/HashedObject.h deleted file mode 100644 index 31e31c3c16..0000000000 --- a/src/cpp/ripple/HashedObject.h +++ /dev/null @@ -1,114 +0,0 @@ -#ifndef __HASHEDOBJECT__ -#define __HASHEDOBJECT__ - -#include - -#include -#include - -// VFALCO: TODO, Move this to someplace sensible!! -// Adapter to furnish uptime information to KeyCache via UptimeTimer singleton -struct UptimeTimerAdapter -{ - inline static int getElapsedSeconds () - { - return UptimeTimer::getInstance().getElapsedSeconds (); - } -}; - - - -DEFINE_INSTANCE(HashedObject); - -class Job; - -enum HashedObjectType -{ - hotUNKNOWN = 0, - hotLEDGER = 1, - hotTRANSACTION = 2, - hotACCOUNT_NODE = 3, - hotTRANSACTION_NODE = 4 -}; - -class HashedObject : private IS_INSTANCE(HashedObject) -{ -public: - typedef boost::shared_ptr pointer; - - HashedObjectType mType; - uint256 mHash; - uint32 mLedgerIndex; - std::vector mData; - - HashedObject(HashedObjectType type, uint32 index, const std::vector& data, const uint256& hash) : - mType(type), mHash(hash), mLedgerIndex(index), mData(data) { ; } - - HashedObject(HashedObjectType type, uint32 index, const unsigned char *data, int dlen, const uint256& hash) : - mType(type), mHash(hash), mLedgerIndex(index), mData(data, data + dlen) { ; } - - const std::vector& getData() const { return mData; } - const uint256& getHash() const { return mHash; } - HashedObjectType getType() const { return mType; } - uint32 getIndex() const { return mLedgerIndex; } -}; - -class HashedObjectStore -{ -public: - HashedObjectStore(int cacheSize, int cacheAge); - - bool isLevelDB() { return mLevelDB; } - - float getCacheHitRate() { return mCache.getHitRate(); } - - bool store(HashedObjectType type, uint32 index, const std::vector& data, - const uint256& hash) - { - if (mLevelDB) - return storeLevelDB(type, index, data, hash); - return storeSQLite(type, index, data, hash); - } - - HashedObject::pointer retrieve(const uint256& hash) - { - if (mLevelDB) - return retrieveLevelDB(hash); - return retrieveSQLite(hash); - } - - bool storeSQLite(HashedObjectType type, uint32 index, const std::vector& data, - const uint256& hash); - HashedObject::pointer retrieveSQLite(const uint256& hash); - void bulkWriteSQLite(Job&); - - bool storeLevelDB(HashedObjectType type, uint32 index, const std::vector& data, - const uint256& hash); - HashedObject::pointer retrieveLevelDB(const uint256& hash); - void bulkWriteLevelDB(Job&); - - - void waitWrite(); - void tune(int size, int age); - void sweep() { mCache.sweep(); mNegativeCache.sweep(); } - int getWriteLoad(); - - int import(const std::string& fileName); - -private: - TaggedCache mCache; - KeyCache mNegativeCache; - - boost::mutex mWriteMutex; - boost::condition_variable mWriteCondition; - int mWriteGeneration; - int mWriteLoad; - - std::vector< boost::shared_ptr > mWriteSet; - bool mWritePending; - bool mLevelDB; - bool mEphemeralDB; -}; - -#endif -// vim:ts=4 diff --git a/src/cpp/ripple/HttpsClient.cpp b/src/cpp/ripple/HttpsClient.cpp index d772f076a3..86b56adff9 100644 --- a/src/cpp/ripple/HttpsClient.cpp +++ b/src/cpp/ripple/HttpsClient.cpp @@ -2,35 +2,20 @@ // Fetch a web page via https. // -#include "HttpsClient.h" - -#include -#include -#include -#include -#include - -#define CLIENT_MAX_HEADER (32*1024) - SETUP_LOG (HttpsClient) -using namespace boost::system; -using namespace boost::asio; - -HttpsClient::HttpsClient( - boost::asio::io_service& io_service, - const unsigned short port, - std::size_t responseMax - ) : - mSocket(io_service, theConfig.SSL_CONTEXT), - mResolver(io_service), - mHeader(CLIENT_MAX_HEADER), - mPort(port), - mResponseMax(responseMax), - mDeadline(io_service) +HttpsClient::HttpsClient (boost::asio::io_service& io_service, + const unsigned short port, + std::size_t responseMax) + : mSocket (io_service, theConfig.SSL_CONTEXT) + , mResolver (io_service) + , mHeader (maxClientHeaderBytes) + , mPort (port) + , mResponseMax (responseMax) + , mDeadline (io_service) { if (!theConfig.SSL_VERIFY) - mSocket.SSLSocket().set_verify_mode(boost::asio::ssl::verify_none); + mSocket.SSLSocket().set_verify_mode (boost::asio::ssl::verify_none); } void HttpsClient::makeGet(const std::string& strPath, boost::asio::streambuf& sb, const std::string& strHost) @@ -82,8 +67,11 @@ void HttpsClient::httpsNext() { WriteLog (lsTRACE, HttpsClient) << "Fetch: " << mDeqSites[0]; - boost::shared_ptr query(new boost::asio::ip::tcp::resolver::query(mDeqSites[0], boost::lexical_cast(mPort), - ip::resolver_query_base::numeric_service)); + boost::shared_ptr query ( + new boost::asio::ip::tcp::resolver::query ( + mDeqSites[0], + boost::lexical_cast (mPort), + boost::asio::ip::resolver_query_base::numeric_service)); mQuery = query; mDeadline.expires_from_now(mTimeout, mShutdown); @@ -477,7 +465,7 @@ void HttpsClient::sendSMS(boost::asio::io_service& io_service, const std::string if (iPort < 0) iPort = bSSL ? 443 : 80; - boost::shared_ptr client(new HttpsClient(io_service, iPort, CLIENT_MAX_HEADER)); + boost::shared_ptr client(new HttpsClient(io_service, iPort, maxClientHeaderBytes)); client->httpsGet(bSSL, deqSites, strURI, boost::posix_time::seconds(SMS_TIMEOUT), BIND_TYPE(&responseSMS, P_1, P_2, P_3)); diff --git a/src/cpp/ripple/HttpsClient.h b/src/cpp/ripple/HttpsClient.h index 41c22c2988..1ecd743cb3 100644 --- a/src/cpp/ripple/HttpsClient.h +++ b/src/cpp/ripple/HttpsClient.h @@ -18,54 +18,7 @@ class HttpsClient : public boost::enable_shared_from_this { -private: - typedef boost::shared_ptr pointer; - - bool mSSL; - AutoSocket mSocket; - boost::asio::ip::tcp::resolver mResolver; - boost::shared_ptr mQuery; - boost::asio::streambuf mRequest; - boost::asio::streambuf mHeader; - boost::asio::streambuf mResponse; - std::string mBody; - const unsigned short mPort; - int mResponseMax; - int mStatus; - FUNCTION_TYPE mBuild; - FUNCTION_TYPE mComplete; - - boost::asio::deadline_timer mDeadline; - - // If not success, we are shutting down. - boost::system::error_code mShutdown; - - std::deque mDeqSites; - boost::posix_time::time_duration mTimeout; - - void handleDeadline(const boost::system::error_code& ecResult); - - void handleResolve(const boost::system::error_code& ecResult, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); - - void handleConnect(const boost::system::error_code& ecResult); - - void handleRequest(const boost::system::error_code& ecResult); - - void handleWrite(const boost::system::error_code& ecResult, std::size_t bytes_transferred); - - void handleHeader(const boost::system::error_code& ecResult, std::size_t bytes_transferred); - - void handleData(const boost::system::error_code& ecResult, std::size_t bytes_transferred); - - void handleShutdown(const boost::system::error_code& ecResult); - - void httpsNext(); - - void invokeComplete(const boost::system::error_code& ecResult, int iStatus = 0, const std::string& strData = ""); - - void makeGet(const std::string& strPath, boost::asio::streambuf& sb, const std::string& strHost); public: - HttpsClient( boost::asio::io_service& io_service, const unsigned short port, @@ -117,6 +70,56 @@ public: FUNCTION_TYPE complete); static void sendSMS(boost::asio::io_service& io_service, const std::string& strText); + +private: + static const int maxClientHeaderBytes = 32 * 1024; + + typedef boost::shared_ptr pointer; + + bool mSSL; + AutoSocket mSocket; + boost::asio::ip::tcp::resolver mResolver; + boost::shared_ptr mQuery; + boost::asio::streambuf mRequest; + boost::asio::streambuf mHeader; + boost::asio::streambuf mResponse; + std::string mBody; + const unsigned short mPort; + int mResponseMax; + int mStatus; + FUNCTION_TYPE mBuild; + FUNCTION_TYPE mComplete; + + boost::asio::deadline_timer mDeadline; + + // If not success, we are shutting down. + boost::system::error_code mShutdown; + + std::deque mDeqSites; + boost::posix_time::time_duration mTimeout; + + void handleDeadline(const boost::system::error_code& ecResult); + + void handleResolve(const boost::system::error_code& ecResult, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); + + void handleConnect(const boost::system::error_code& ecResult); + + void handleRequest(const boost::system::error_code& ecResult); + + void handleWrite(const boost::system::error_code& ecResult, std::size_t bytes_transferred); + + void handleHeader(const boost::system::error_code& ecResult, std::size_t bytes_transferred); + + void handleData(const boost::system::error_code& ecResult, std::size_t bytes_transferred); + + void handleShutdown(const boost::system::error_code& ecResult); + + void httpsNext(); + + void invokeComplete(const boost::system::error_code& ecResult, int iStatus = 0, const std::string& strData = ""); + + void makeGet(const std::string& strPath, boost::asio::streambuf& sb, const std::string& strHost); }; + #endif // vim:ts=4 diff --git a/src/cpp/ripple/Ledger.h b/src/cpp/ripple/Ledger.h index a97763a528..df7e5adc47 100644 --- a/src/cpp/ripple/Ledger.h +++ b/src/cpp/ripple/Ledger.h @@ -1,13 +1,9 @@ -#ifndef __LEDGER__ -#define __LEDGER__ +#ifndef RIPPLE_LEDGER_H +#define RIPPLE_LEDGER_H -#include -#include +// VFALCO: TODO Get this include out of here! +#include "ripple_HashedObject.h" -#include -#include -#include -#include #include "Transaction.h" #include "TransactionMeta.h" @@ -15,6 +11,8 @@ #include "NicknameState.h" #include "SHAMap.h" +class Job; + enum LedgerStateParms { lepNONE = 0, // no special flags diff --git a/src/cpp/ripple/OrderBook.h b/src/cpp/ripple/OrderBook.h index f4bce4a18c..543dafc910 100644 --- a/src/cpp/ripple/OrderBook.h +++ b/src/cpp/ripple/OrderBook.h @@ -2,26 +2,14 @@ #ifndef ORDERBOOK_H #define ORDERBOOK_H - #include "SerializedLedger.h" #include "NetworkOPs.h" -#include /* Encapsulates the SLE for an orderbook */ class OrderBook { - uint256 mBookBase; - - uint160 mCurrencyIn; - uint160 mCurrencyOut; - uint160 mIssuerIn; - uint160 mIssuerOut; - - //SerializedLedgerEntry::pointer mLedgerEntry; - OrderBook(SerializedLedgerEntry::ref ledgerEntry); // For accounts in a ledger - public: typedef boost::shared_ptr pointer; typedef const boost::shared_ptr& ref; @@ -42,6 +30,17 @@ public: // looks through the best offers to see how much it would cost to take the given amount STAmount& getTakePrice(STAmount& takeAmount); + +private: + uint256 mBookBase; + + uint160 mCurrencyIn; + uint160 mCurrencyOut; + uint160 mIssuerIn; + uint160 mIssuerOut; + + //SerializedLedgerEntry::pointer mLedgerEntry; + OrderBook(SerializedLedgerEntry::ref ledgerEntry); // For accounts in a ledger }; #endif diff --git a/src/cpp/ripple/OrderBookDB.h b/src/cpp/ripple/OrderBookDB.h index f7dbd0fa84..43706e01a8 100644 --- a/src/cpp/ripple/OrderBookDB.h +++ b/src/cpp/ripple/OrderBookDB.h @@ -25,28 +25,20 @@ typedef std::pair currencyIssuer_ct; // C++ defect 106 class BookListeners { - boost::unordered_map mListeners; - boost::recursive_mutex mLock; - public: typedef boost::shared_ptr pointer; void addSubscriber(InfoSub::ref sub); void removeSubscriber(uint64 sub); void publish(Json::Value& jvObj); + +private: + boost::unordered_map mListeners; + boost::recursive_mutex mLock; }; class OrderBookDB { - boost::unordered_map< currencyIssuer_t, std::vector > mSourceMap; // by ci/ii - boost::unordered_map< currencyIssuer_t, std::vector > mDestMap; // by co/io - - // issuerPays, issuerGets, currencyPays, currencyGets - std::map > > > mListeners; - - uint32 mSeq; - boost::recursive_mutex mLock; - public: OrderBookDB(); void setup(Ledger::ref ledger); @@ -60,11 +52,23 @@ public: BookListeners::pointer getBookListeners(const uint160& currencyPays, const uint160& currencyGets, const uint160& issuerPays, const uint160& issuerGets); - BookListeners::pointer makeBookListeners(const uint160& currencyPays, const uint160& currencyGets, + + BookListeners::pointer makeBookListeners(const uint160& currencyPays, const uint160& currencyGets, const uint160& issuerPays, const uint160& issuerGets); // see if this txn effects any orderbook void processTxn(Ledger::ref ledger, const ALTransaction& alTx, Json::Value& jvObj); + +private: + boost::unordered_map< currencyIssuer_t, std::vector > mSourceMap; // by ci/ii + boost::unordered_map< currencyIssuer_t, std::vector > mDestMap; // by co/io + + // issuerPays, issuerGets, currencyPays, currencyGets + std::map > > > mListeners; + + uint32 mSeq; + boost::recursive_mutex mLock; + }; #endif diff --git a/src/cpp/ripple/SHAMap.cpp b/src/cpp/ripple/SHAMap.cpp index a0b02a2302..c7d517fca7 100644 --- a/src/cpp/ripple/SHAMap.cpp +++ b/src/cpp/ripple/SHAMap.cpp @@ -1,6 +1,4 @@ -#include "SHAMap.h" - #include #include @@ -9,9 +7,6 @@ #include #include -#include "SHAMap.h" -#include "Application.h" - #ifndef STATE_MAP_BUCKETS #define STATE_MAP_BUCKETS 1024 #endif diff --git a/src/cpp/ripple/SHAMap.h b/src/cpp/ripple/SHAMap.h index f936da75e6..677e41a7c9 100644 --- a/src/cpp/ripple/SHAMap.h +++ b/src/cpp/ripple/SHAMap.h @@ -1,15 +1,8 @@ -#ifndef __SHAMAP__ -#define __SHAMAP__ +#ifndef RIPPLE_SHAMAP_H +#define RIPPLE_SHAMAP_H -#include -#include -#include - -#include -#include -#include - -#include "HashedObject.h" +// VFALCO: TODO Get this include out of here! +#include "ripple_UptimeTimerAdapter.h" DEFINE_INSTANCE(SHAMap); DEFINE_INSTANCE(SHAMapItem); diff --git a/src/cpp/ripple/SHAMapDiff.cpp b/src/cpp/ripple/SHAMapDiff.cpp index c621233d0e..1fc636527e 100644 --- a/src/cpp/ripple/SHAMapDiff.cpp +++ b/src/cpp/ripple/SHAMapDiff.cpp @@ -1,7 +1,3 @@ -#include "SHAMap.h" - -#include - // This code is used to compare another node's transaction tree // to our own. It returns a map containing all items that are different // between two SHA maps. It is optimized not to descend down tree diff --git a/src/cpp/ripple/SHAMapNodes.cpp b/src/cpp/ripple/SHAMapNodes.cpp index 2efd7ea3cf..bc96721d70 100644 --- a/src/cpp/ripple/SHAMapNodes.cpp +++ b/src/cpp/ripple/SHAMapNodes.cpp @@ -1,18 +1,4 @@ -#include "SHAMap.h" - -#include -#include - -#include -#include -#include -#include - -#include - -#include "HashPrefixes.h" - SETUP_LOG (SHAMapNode) std::string SHAMapNode::getString() const diff --git a/src/cpp/ripple/ripple_HashedObject.cpp b/src/cpp/ripple/ripple_HashedObject.cpp new file mode 100644 index 0000000000..b33f1f8ae8 --- /dev/null +++ b/src/cpp/ripple/ripple_HashedObject.cpp @@ -0,0 +1,14 @@ + +/* +#include "HashedObject.h" +#include "leveldb/db.h" +#include "leveldb/write_batch.h" +#include "../database/SqliteDatabase.h" +#include "Application.h" +*/ + +SETUP_LOG (HashedObject) + +DECLARE_INSTANCE(HashedObject); + +// vim:ts=4 diff --git a/src/cpp/ripple/ripple_HashedObject.h b/src/cpp/ripple/ripple_HashedObject.h new file mode 100644 index 0000000000..c746324ed8 --- /dev/null +++ b/src/cpp/ripple/ripple_HashedObject.h @@ -0,0 +1,105 @@ +#ifndef RIPPLE_HASHEDOBJECT_H +#define RIPPLE_HASHEDOBJECT_H + +/** The types of hashed objects. +*/ +enum HashedObjectType +{ + hotUNKNOWN = 0, + hotLEDGER = 1, + hotTRANSACTION = 2, + hotACCOUNT_NODE = 3, + hotTRANSACTION_NODE = 4 +}; + +DEFINE_INSTANCE (HashedObject); + +/** A blob of data with associated metadata, referenced by hash. + + The metadata includes the following: + + - Type of the blob + - The ledger index in which it appears + - The SHA 256 hash + + @note No checking is performed to make sure the hash matches the data. + @see SHAMap +*/ +// VFALCO: TODO, consider making the instance a private member of SHAMap +// since its the primary user. +// +class HashedObject : private IS_INSTANCE (HashedObject) +{ +public: + typedef boost::shared_ptr pointer; + + /** Create from a vector of data. + + @note A copy of the data is created. + */ + HashedObject (HashedObjectType type, + uint32 ledgerIndex, + std::vector const& binaryDataToCopy, + uint256 const& hash) + : mType (type) + , mHash (hash) + , mLedgerIndex (ledgerIndex) + , mData (binaryDataToCopy) + { + } + + /** Create from an area of memory. + + @note A copy of the data is created. + */ + HashedObject (HashedObjectType type, + uint32 ledgerIndex, + void const* bufferToCopy, + int bytesInBuffer, + uint256 const& hash) + : mType (type) + , mHash (hash) + , mLedgerIndex (ledgerIndex) + , mData (static_cast (bufferToCopy), + static_cast (bufferToCopy) + bytesInBuffer) + { + } + + /** Retrieve the type of this object. + */ + HashedObjectType getType () const + { + return mType; + } + + /** Retrieve the hash metadata. + */ + uint256 const& getHash() const + { + return mHash; + } + + /** Retrieve the ledger index in which this object appears. + */ + // VFALCO: TODO rename to getLedgerIndex or getLedgerId + uint32 getIndex () const + { + return mLedgerIndex; + } + + /** Retrieve the binary data. + */ + std::vector const& getData() const + { + return mData; + } + +private: + HashedObjectType const mType; + uint256 const mHash; + uint32 const mLedgerIndex; + std::vector const mData; +}; + +#endif +// vim:ts=4 diff --git a/src/cpp/ripple/HashedObject.cpp b/src/cpp/ripple/ripple_HashedObjectStore.cpp similarity index 91% rename from src/cpp/ripple/HashedObject.cpp rename to src/cpp/ripple/ripple_HashedObjectStore.cpp index f8044cf6ba..c2240a4746 100644 --- a/src/cpp/ripple/HashedObject.cpp +++ b/src/cpp/ripple/ripple_HashedObjectStore.cpp @@ -1,18 +1,11 @@ -#include "HashedObject.h" +/* +#include "HashedObject.h" #include "leveldb/db.h" #include "leveldb/write_batch.h" - -#include -#include - #include "../database/SqliteDatabase.h" - #include "Application.h" - -SETUP_LOG (HashedObject) - -DECLARE_INSTANCE(HashedObject); +*/ HashedObjectStore::HashedObjectStore(int cacheSize, int cacheAge) : mCache("HashedObjectStore", cacheSize, cacheAge), mNegativeCache("HashedObjectNegativeCache", 0, 120), @@ -53,8 +46,9 @@ int HashedObjectStore::getWriteLoad() return std::max(mWriteLoad, static_cast(mWriteSet.size())); } -static HashedObject::pointer LLRetrieve(const uint256& hash, leveldb::DB* db) -{ // low-level retrieve +// low-level retrieve +HashedObject::pointer HashedObjectStore::LLRetrieve(const uint256& hash, leveldb::DB* db) +{ std::string sData; leveldb::Status st = db->Get(leveldb::ReadOptions(), @@ -73,19 +67,20 @@ static HashedObject::pointer LLRetrieve(const uint256& hash, leveldb::DB* db) bufPtr + 9, sData.size() - 9, hash); } -static void LLWrite(boost::shared_ptr ptr, leveldb::DB* db) -{ // low-level write single +// low-level write single +void HashedObjectStore::LLWrite(boost::shared_ptr ptr, leveldb::DB* db) +{ HashedObject& obj = *ptr; - std::vector rawData(9 + obj.mData.size()); + std::vector rawData(9 + obj.getData ().size()); unsigned char* bufPtr = &rawData.front(); - *reinterpret_cast(bufPtr + 0) = ntohl(obj.mLedgerIndex); - *reinterpret_cast(bufPtr + 4) = ntohl(obj.mLedgerIndex); - *(bufPtr + 8) = static_cast(obj.mType); - memcpy(bufPtr + 9, &obj.mData.front(), obj.mData.size()); + *reinterpret_cast(bufPtr + 0) = ntohl(obj.getIndex ()); + *reinterpret_cast(bufPtr + 4) = ntohl(obj.getIndex ()); + *(bufPtr + 8) = static_cast(obj.getType ()); + memcpy(bufPtr + 9, &obj.getData ().front(), obj.getData ().size()); leveldb::Status st = db->Put(leveldb::WriteOptions(), - leveldb::Slice(reinterpret_cast(obj.mHash.begin()), obj.mHash.size()), + leveldb::Slice(reinterpret_cast(obj.getHash ().begin()), obj.getHash ().size()), leveldb::Slice(reinterpret_cast(bufPtr), rawData.size())); if (!st.ok()) { @@ -94,22 +89,23 @@ static void LLWrite(boost::shared_ptr ptr, leveldb::DB* db) } } -static void LLWrite(const std::vector< boost::shared_ptr >& set, leveldb::DB* db) -{ // low-level write set +// low-level write set +void HashedObjectStore::LLWrite(const std::vector< boost::shared_ptr >& set, leveldb::DB* db) +{ leveldb::WriteBatch batch; BOOST_FOREACH(const boost::shared_ptr& it, set) { const HashedObject& obj = *it; - std::vector rawData(9 + obj.mData.size()); + std::vector rawData(9 + obj.getData ().size()); unsigned char* bufPtr = &rawData.front(); - *reinterpret_cast(bufPtr + 0) = ntohl(obj.mLedgerIndex); - *reinterpret_cast(bufPtr + 4) = ntohl(obj.mLedgerIndex); - *(bufPtr + 8) = static_cast(obj.mType); - memcpy(bufPtr + 9, &obj.mData.front(), obj.mData.size()); + *reinterpret_cast(bufPtr + 0) = ntohl(obj.getIndex ()); + *reinterpret_cast(bufPtr + 4) = ntohl(obj.getIndex ()); + *(bufPtr + 8) = static_cast(obj.getType ()); + memcpy(bufPtr + 9, &obj.getData ().front(), obj.getData ().size()); - batch.Put(leveldb::Slice(reinterpret_cast(obj.mHash.begin()), obj.mHash.size()), + batch.Put(leveldb::Slice(reinterpret_cast(obj.getHash ().begin()), obj.getHash ().size()), leveldb::Slice(reinterpret_cast(bufPtr), rawData.size())); } diff --git a/src/cpp/ripple/ripple_HashedObjectStore.h b/src/cpp/ripple/ripple_HashedObjectStore.h new file mode 100644 index 0000000000..1123b1034c --- /dev/null +++ b/src/cpp/ripple/ripple_HashedObjectStore.h @@ -0,0 +1,76 @@ +#ifndef RIPPLE_HASHEDOBJECTSTORE_H +#define RIPPLE_HASHEDOBJECTSTORE_H + +/** Persistency layer for hashed objects. +*/ +class HashedObjectStore +{ +public: + HashedObjectStore (int cacheSize, int cacheAge); + + bool isLevelDB() + { + return mLevelDB; + } + + float getCacheHitRate () + { + return mCache.getHitRate(); + } + + bool store (HashedObjectType type, uint32 index, const std::vector& data, + const uint256& hash) + { + if (mLevelDB) + return storeLevelDB(type, index, data, hash); + + return storeSQLite(type, index, data, hash); + } + + HashedObject::pointer retrieve(const uint256& hash) + { + if (mLevelDB) + return retrieveLevelDB(hash); + return retrieveSQLite(hash); + } + + bool storeSQLite(HashedObjectType type, uint32 index, const std::vector& data, + const uint256& hash); + HashedObject::pointer retrieveSQLite(const uint256& hash); + void bulkWriteSQLite(Job&); + + bool storeLevelDB(HashedObjectType type, uint32 index, const std::vector& data, + const uint256& hash); + HashedObject::pointer retrieveLevelDB(const uint256& hash); + void bulkWriteLevelDB(Job&); + + + void waitWrite(); + void tune(int size, int age); + void sweep() { mCache.sweep(); mNegativeCache.sweep(); } + int getWriteLoad(); + + int import(const std::string& fileName); + +private: + static HashedObject::pointer LLRetrieve(const uint256& hash, leveldb::DB* db); + static void LLWrite(boost::shared_ptr ptr, leveldb::DB* db); + static void LLWrite(const std::vector< boost::shared_ptr >& set, leveldb::DB* db); + +private: + TaggedCache mCache; + KeyCache mNegativeCache; + + boost::mutex mWriteMutex; + boost::condition_variable mWriteCondition; + int mWriteGeneration; + int mWriteLoad; + + std::vector< boost::shared_ptr > mWriteSet; + bool mWritePending; + bool mLevelDB; + bool mEphemeralDB; +}; + +#endif +// vim:ts=4 diff --git a/src/cpp/ripple/ripple_Peer.h b/src/cpp/ripple/ripple_Peer.h index f5146c7b54..ff908bc5ef 100644 --- a/src/cpp/ripple/ripple_Peer.h +++ b/src/cpp/ripple/ripple_Peer.h @@ -1,17 +1,6 @@ #ifndef __PEER__ #define __PEER__ -#include -#include -#include -#include -#include -#include - -#include "Ledger.h" -#include "Transaction.h" -#include "LoadManager.h" - typedef std::pair ipPort; class Peer : public boost::enable_shared_from_this diff --git a/src/cpp/ripple/ripple_UptimeTimerAdapter.h b/src/cpp/ripple/ripple_UptimeTimerAdapter.h new file mode 100644 index 0000000000..465f4e76f8 --- /dev/null +++ b/src/cpp/ripple/ripple_UptimeTimerAdapter.h @@ -0,0 +1,14 @@ +#ifndef RIPPLE_UPTIMETIMERADAPTER_H +#define RIPPLE_UPTIMETIMERADAPTER_H + +/** Adapter providing uptime measurements for template classes. +*/ +struct UptimeTimerAdapter +{ + inline static int getElapsedSeconds () + { + return UptimeTimer::getInstance().getElapsedSeconds (); + } +}; + +#endif