Fix order book scaning for path finding.

This commit is contained in:
Arthur Britto
2013-02-02 19:08:26 -08:00
parent 3f4a06df9f
commit c9b954dd7c
2 changed files with 42 additions and 24 deletions

View File

@@ -1,28 +1,42 @@
#include "OrderBookDB.h"
#include "Log.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include "OrderBookDB.h"
#include "Log.h"
SETUP_LOG();
// TODO: this would be way faster if we could just look under the order dirs // TODO: this would be way faster if we could just look under the order dirs
OrderBookDB::OrderBookDB(Ledger::pointer ledger) OrderBookDB::OrderBookDB(Ledger::pointer ledger)
{ {
// walk through the entire ledger looking for orderbook entries // walk through the entire ledger looking for orderbook entries
uint256 currentIndex=ledger->getFirstLedgerIndex(); uint256 currentIndex = ledger->getFirstLedgerIndex();
while(currentIndex.isNonZero())
cLog(lsDEBUG) << "OrderBookDB>";
while (currentIndex.isNonZero())
{ {
SLE::pointer entry=ledger->getSLE(currentIndex); SLE::pointer entry=ledger->getSLE(currentIndex);
OrderBook::pointer book=OrderBook::newOrderBook(entry); OrderBook::pointer book = OrderBook::newOrderBook(entry);
if(book) if (book)
{ {
if( mKnownMap.find(book->getBookBase()) != mKnownMap.end() ) cLog(lsDEBUG) << "OrderBookDB: found book";
{
mKnownMap[book->getBookBase()]=true;
if(!book->getCurrencyIn()) if (mKnownMap.find(book->getBookBase()) == mKnownMap.end())
{ // XRP {
mKnownMap[book->getBookBase()] = true;
cLog(lsDEBUG) << "OrderBookDB: unknown book in: "
<< STAmount::createHumanCurrency(book->getCurrencyIn())
<< " -> "
<< STAmount::createHumanCurrency(book->getCurrencyOut());
if (!book->getCurrencyIn())
{
// XRP
mXRPOrders.push_back(book); mXRPOrders.push_back(book);
}else }
else
{ {
mIssuerMap[book->getIssuerIn()].push_back(book); mIssuerMap[book->getIssuerIn()].push_back(book);
} }
@@ -31,26 +45,29 @@ OrderBookDB::OrderBookDB(Ledger::pointer ledger)
currentIndex=ledger->getNextLedgerIndex(currentIndex); currentIndex=ledger->getNextLedgerIndex(currentIndex);
} }
cLog(lsDEBUG) << "OrderBookDB<";
} }
// return list of all orderbooks that want IssuerID // return list of all orderbooks that want IssuerID
std::vector<OrderBook::pointer>& OrderBookDB::getBooks(const uint160& issuerID) std::vector<OrderBook::pointer>& OrderBookDB::getBooks(const uint160& issuerID)
{ {
if( mIssuerMap.find(issuerID) == mIssuerMap.end() ) return mEmptyVector; return mIssuerMap.find(issuerID) == mIssuerMap.end()
else return( mIssuerMap[issuerID]); ? mEmptyVector
: mIssuerMap[issuerID];
} }
// return list of all orderbooks that want this issuerID and currencyID // return list of all orderbooks that want this issuerID and currencyID
void OrderBookDB::getBooks(const uint160& issuerID, const uint160& currencyID, std::vector<OrderBook::pointer>& bookRet) void OrderBookDB::getBooks(const uint160& issuerID, const uint160& currencyID, std::vector<OrderBook::pointer>& bookRet)
{ {
if( mIssuerMap.find(issuerID) == mIssuerMap.end() ) if (mIssuerMap.find(issuerID) == mIssuerMap.end())
{ {
BOOST_FOREACH(OrderBook::ref book, mIssuerMap[issuerID]) BOOST_FOREACH(OrderBook::ref book, mIssuerMap[issuerID])
{ {
if(book->getCurrencyIn()==currencyID) if (book->getCurrencyIn() == currencyID)
{
bookRet.push_back(book); bookRet.push_back(book);
}
} }
} }
} }
// vim:ts=4

View File

@@ -1,10 +1,10 @@
#include "Ledger.h" #include "Ledger.h"
#include "OrderBook.h" #include "OrderBook.h"
/* //
we can eventually make this cached and just update it as transactions come in. // XXX Eventually make this cached and just update it as transactions come in.
But for now it is probably faster to just generate it each time // But, for now it is probably faster to just generate it each time.
*/ //
class OrderBookDB class OrderBookDB
{ {
@@ -19,14 +19,15 @@ public:
// return list of all orderbooks that want XRP // return list of all orderbooks that want XRP
std::vector<OrderBook::pointer>& getXRPInBooks(){ return mXRPOrders; } std::vector<OrderBook::pointer>& getXRPInBooks(){ return mXRPOrders; }
// return list of all orderbooks that want IssuerID // return list of all orderbooks that want IssuerID
std::vector<OrderBook::pointer>& getBooks(const uint160& issuerID); std::vector<OrderBook::pointer>& getBooks(const uint160& issuerID);
// return list of all orderbooks that want this issuerID and currencyID // return list of all orderbooks that want this issuerID and currencyID
void getBooks(const uint160& issuerID, const uint160& currencyID, std::vector<OrderBook::pointer>& bookRet); void getBooks(const uint160& issuerID, const uint160& currencyID, std::vector<OrderBook::pointer>& bookRet);
// returns the best rate we can find // returns the best rate we can find
float getPrice(uint160& currencyIn,uint160& currencyOut); float getPrice(uint160& currencyIn,uint160& currencyOut);
}; };
// vim:ts=4 // vim:ts=4