Add SHAMap::iterator

This commit is contained in:
Vinnie Falco
2015-06-13 08:37:36 -07:00
committed by Nik Bougalis
parent eb709f415b
commit c53b9f3713
4 changed files with 92 additions and 14 deletions

View File

@@ -33,13 +33,11 @@ TaggedCache <uint256, AcceptedLedger> AcceptedLedger::s_cache (
AcceptedLedger::AcceptedLedger (Ledger::ref ledger) : mLedger (ledger)
{
SHAMap& txSet = *ledger->peekTransactionMap ();
for (std::shared_ptr<SHAMapItem> item = txSet.peekFirstItem (); item;
item = txSet.peekNextItem (item->getTag ()))
for (auto const& item : *ledger->peekTransactionMap())
{
SerialIter sit (item->slice());
insert (std::make_shared<AcceptedLedgerTx> (ledger, std::ref (sit)));
insert (std::make_shared<AcceptedLedgerTx>(
ledger, std::ref (sit)));
}
}

View File

@@ -1857,8 +1857,7 @@ void applyTransactions (std::shared_ptr<SHAMap> const& set,
if (set)
{
for (std::shared_ptr<SHAMapItem> item = set->peekFirstItem (); !!item;
item = set->peekNextItem (item->getTag ()))
for (auto const item : *set)
{
// If the checkLedger doesn't have the transaction
if (!checkLedger->hasTransaction (item->getTag ()))

View File

@@ -1317,16 +1317,18 @@ bool ApplicationImp::loadOldLedger (
cur = std::make_shared <Ledger> (*cur, true);
assert (!cur->isImmutable());
for (auto it = txns->peekFirstItem(); it != nullptr;
it = txns->peekNextItem(it->getTag()))
for (auto const& item : *txns)
{
Transaction::pointer txn = replayLedger->getTransaction(it->getTag());
m_journal.info << txn->getJson(0);
auto const txn =
replayLedger->getTransaction(item->getTag());
if (m_journal.info) m_journal.info <<
txn->getJson(0);
Serializer s;
txn->getSTransaction()->add(s);
if (!cur->addTransaction(it->getTag(), s))
m_journal.warning << "Unable to add transaction " << it->getTag();
getApp().getHashRouter().setFlag (it->getTag(), SF_SIGGOOD);
if (! cur->addTransaction(item->getTag(), s))
if (m_journal.warning) m_journal.warning <<
"Unable to add transaction " << item->getTag();
getApp().getHashRouter().setFlag (item->getTag(), SF_SIGGOOD);
}
// Switch to the mutable snapshot

View File

@@ -33,9 +33,11 @@
#include <ripple/nodestore/Database.h>
#include <ripple/nodestore/NodeObject.h>
#include <beast/utility/Journal.h>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/shared_lock_guard.hpp>
#include <boost/thread/shared_mutex.hpp>
#include <cassert>
#include <stack>
namespace ripple {
@@ -111,6 +113,19 @@ public:
Family& f,
beast::Journal journal);
//--------------------------------------------------------------------------
/** Iterator to a SHAMap's leaves
This is always a const iterator.
Meets the requirements of ForwardRange.
*/
class iterator;
iterator begin() const;
iterator end() const;
//--------------------------------------------------------------------------
// Returns a new map that's a snapshot of this one.
// Handles copy on write for mutable snapshots.
std::shared_ptr<SHAMap> snapShot (bool isMutable) const;
@@ -335,6 +350,70 @@ SHAMap::setUnbacked ()
backed_ = false;
}
//------------------------------------------------------------------------------
class SHAMap::iterator
: public boost::iterator_facade<
SHAMap::iterator,
std::shared_ptr<SHAMapItem const> const,
std::forward_iterator_tag>
{
private:
friend class boost::iterator_core_access;
SHAMap const* map_ = nullptr;
std::shared_ptr<
SHAMapItem const> item_;
public:
iterator() = default;
iterator (iterator const&) = default;
iterator& operator= (iterator const&) = default;
iterator (SHAMap const& map,
std::shared_ptr<SHAMapItem const> const& item)
: map_(&map)
, item_(item)
{
}
private:
void
increment()
{
item_ = map_->peekNextItem(
item_->key());
}
bool
equal (iterator const& other) const
{
assert(map_ == other.map_);
return item_ == other.item_;
}
std::shared_ptr<
SHAMapItem const> const&
dereference() const
{
return item_;
}
};
inline
SHAMap::iterator
SHAMap::begin() const
{
return iterator(*this, peekFirstItem());
}
inline
SHAMap::iterator
SHAMap::end() const
{
return iterator(*this, nullptr);
}
}
#endif