From ca57bf8290ee2398a5707e168bd8fad10f8c9595 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 13 Feb 2013 05:27:31 -0800 Subject: [PATCH] Don't wipe the order book while it's being used. Fix OrderBookDB::getBooks so it can return something. Fix some cases of the find/[] anti-pattern. (You don't need to search twice.) --- src/cpp/ripple/OrderBookDB.cpp | 10 ++++++---- src/cpp/ripple/Pathfinder.cpp | 10 +++++----- src/cpp/ripple/Pathfinder.h | 6 ++++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/cpp/ripple/OrderBookDB.cpp b/src/cpp/ripple/OrderBookDB.cpp index 83c1f1ee1..d09800e6d 100644 --- a/src/cpp/ripple/OrderBookDB.cpp +++ b/src/cpp/ripple/OrderBookDB.cpp @@ -64,17 +64,19 @@ void OrderBookDB::setup(Ledger::ref ledger) // return list of all orderbooks that want IssuerID std::vector& OrderBookDB::getBooks(const uint160& issuerID) { - return mIssuerMap.find(issuerID) == mIssuerMap.end() + std::map< uint160, std::vector >::iterator it = mIssuerMap.find(issuerID); + return (it == mIssuerMap.end()) ? mEmptyVector - : mIssuerMap[issuerID]; + : it->second; } // return list of all orderbooks that want this issuerID and currencyID void OrderBookDB::getBooks(const uint160& issuerID, const uint160& currencyID, std::vector& bookRet) { - if (mIssuerMap.find(issuerID) == mIssuerMap.end()) + std::map< uint160, std::vector >::iterator it = mIssuerMap.find(issuerID); + if (it != mIssuerMap.end()) { - BOOST_FOREACH(OrderBook::ref book, mIssuerMap[issuerID]) + BOOST_FOREACH(OrderBook::ref book, it->second) { if (book->getCurrencyIn() == currencyID) bookRet.push_back(book); diff --git a/src/cpp/ripple/Pathfinder.cpp b/src/cpp/ripple/Pathfinder.cpp index b19dd866e..aec280087 100644 --- a/src/cpp/ripple/Pathfinder.cpp +++ b/src/cpp/ripple/Pathfinder.cpp @@ -139,7 +139,7 @@ Pathfinder::Pathfinder(Ledger::ref ledger, mLedger(ledger) { - theApp->getOrderBookDB().setup(mLedger); // TODO: have the orderbook update itself rather than rebuild it from scratch each time + mOrderBook.setup(mLedger); // TODO: have the orderbook update itself rather than rebuild it from scratch each time mLoadMonitor = theApp->getJobQueue().getLoadEvent(jtPATH_FIND); @@ -351,7 +351,7 @@ bool Pathfinder::findPaths(const unsigned int iMaxSteps, const unsigned int iMax else if (!speEnd.mCurrencyID) { // Cursor is for XRP, continue with qualifying books: XRP -> non-XRP - BOOST_FOREACH(OrderBook::ref book, theApp->getOrderBookDB().getXRPInBooks()) + BOOST_FOREACH(OrderBook::ref book, mOrderBook.getXRPInBooks()) { // New end is an order book with the currency and issuer. @@ -445,7 +445,7 @@ bool Pathfinder::findPaths(const unsigned int iMaxSteps, const unsigned int iMax std::vector books; - theApp->getOrderBookDB().getBooks(speEnd.mIssuerID, speEnd.mCurrencyID, books); + mOrderBook.getBooks(speEnd.mIssuerID, speEnd.mCurrencyID, books); BOOST_FOREACH(OrderBook::ref book, books) { @@ -593,7 +593,7 @@ void Pathfinder::addOptions(PathOption::pointer tail) { if (!tail->mCurrencyID) { // source XRP - BOOST_FOREACH(OrderBook::ref book, theApp->getOrderBookDB().getXRPInBooks()) + BOOST_FOREACH(OrderBook::ref book, mOrderBook.getXRPInBooks()) { PathOption::pointer pathOption(new PathOption(tail)); @@ -627,7 +627,7 @@ void Pathfinder::addOptions(PathOption::pointer tail) // every offer that wants the source currency std::vector books; - theApp->getOrderBookDB().getBooks(tail->mCurrentAccount, tail->mCurrencyID, books); + mOrderBook.getBooks(tail->mCurrentAccount, tail->mCurrencyID, books); BOOST_FOREACH(OrderBook::ref book,books) { diff --git a/src/cpp/ripple/Pathfinder.h b/src/cpp/ripple/Pathfinder.h index 69b4813bd..7ba42bb81 100644 --- a/src/cpp/ripple/Pathfinder.h +++ b/src/cpp/ripple/Pathfinder.h @@ -1,10 +1,12 @@ #ifndef __PATHFINDER__ #define __PATHFINDER__ +#include + #include "SerializedTypes.h" #include "RippleAddress.h" #include "RippleCalc.h" -#include +#include "OrderBookDB.h" #if 0 // @@ -41,7 +43,7 @@ class Pathfinder uint160 mSrcIssuerID; STAmount mSrcAmount; - //OrderBookDB mOrderBook; + OrderBookDB mOrderBook; Ledger::pointer mLedger; PathState::pointer mPsDefault; LoadEvent::pointer mLoadMonitor;