Add ReadView::sles

This commit is contained in:
Vinnie Falco
2015-07-14 11:56:09 -07:00
parent 110bbf3956
commit 84161b86c7
17 changed files with 273 additions and 80 deletions

View File

@@ -2353,6 +2353,10 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\ledger\impl\ReadView.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\ledger\impl\TxMeta.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>

View File

@@ -3078,6 +3078,9 @@
<ClCompile Include="..\..\src\ripple\ledger\impl\RawStateTable.cpp">
<Filter>ripple\ledger\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\ledger\impl\ReadView.cpp">
<Filter>ripple\ledger\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\ledger\impl\TxMeta.cpp">
<Filter>ripple\ledger\impl</Filter>
</ClCompile>

View File

@@ -61,6 +61,59 @@ create_genesis_t const create_genesis {};
//------------------------------------------------------------------------------
class Ledger::sles_iter_impl
: public sles_type::iter_base
{
private:
ReadView const* view_;
SHAMap::const_iterator iter_;
public:
sles_iter_impl() = delete;
sles_iter_impl& operator= (sles_iter_impl const&) = delete;
sles_iter_impl (sles_iter_impl const&) = default;
sles_iter_impl (SHAMap::const_iterator iter,
ReadView const& view)
: view_ (&view)
, iter_ (iter)
{
}
std::unique_ptr<base_type>
copy() const override
{
return std::make_unique<
sles_iter_impl>(*this);
}
bool
equal (base_type const& impl) const override
{
auto const& other = dynamic_cast<
sles_iter_impl const&>(impl);
return iter_ == other.iter_;
}
void
increment() override
{
++iter_;
}
sles_type::value_type
dereference() const override
{
auto const item = *iter_;
SerialIter sit(item.slice());
return std::make_shared<SLE const>(
sit, item.key());
}
};
//------------------------------------------------------------------------------
class Ledger::txs_iter_impl
: public txs_type::iter_base
{
@@ -870,6 +923,24 @@ Ledger::read (Keylet const& k) const
//------------------------------------------------------------------------------
auto
Ledger::slesBegin() const ->
std::unique_ptr<sles_type::iter_base>
{
return std::make_unique<
sles_iter_impl>(
stateMap_->begin(), *this);
}
auto
Ledger::slesEnd() const ->
std::unique_ptr<sles_type::iter_base>
{
return std::make_unique<
sles_iter_impl>(
stateMap_->end(), *this);
}
auto
Ledger::txsBegin() const ->
std::unique_ptr<txs_type::iter_base>

View File

@@ -152,6 +152,12 @@ public:
std::shared_ptr<SLE const>
read (Keylet const& k) const override;
std::unique_ptr<sles_type::iter_base>
slesBegin() const override;
std::unique_ptr<sles_type::iter_base>
slesEnd() const override;
std::unique_ptr<txs_type::iter_base>
txsBegin() const override;
@@ -363,6 +369,7 @@ public:
getHashesByIndex (std::uint32_t minSeq, std::uint32_t maxSeq);
private:
class sles_iter_impl;
class txs_iter_impl;
void saveValidatedLedgerAsync(Job&, bool current)

View File

@@ -131,20 +131,20 @@ void fillJsonState(Object& json, LedgerFill const& fill)
auto expanded = isExpanded(fill);
auto binary = isBinary(fill);
forEachSLE(ledger, [&] (SLE const& sle) {
for(auto const& sle : ledger.sles)
{
count.yield();
if (binary)
{
auto&& obj = appendObject(array);
obj[jss::hash] = to_string(sle.key());
obj[jss::tx_blob] = serializeHex(sle);
obj[jss::hash] = to_string(sle->key());
obj[jss::tx_blob] = serializeHex(*sle);
}
else if (expanded)
array.append(sle.getJson(0));
array.append(sle->getJson(0));
else
array.append(to_string(sle.key()));
return true;
});
array.append(to_string(sle->key()));
}
}
template <class Object>

View File

@@ -84,6 +84,18 @@ public:
return base_.succ(key, last);
}
std::unique_ptr<sles_type::iter_base>
slesBegin() const override
{
return base_.slesBegin();
}
std::unique_ptr<sles_type::iter_base>
slesEnd() const override
{
return base_.slesEnd();
}
std::unique_ptr<txs_type::iter_base>
txsBegin() const override
{

View File

@@ -164,6 +164,12 @@ public:
std::shared_ptr<SLE const>
read (Keylet const& k) const override;
std::unique_ptr<sles_type::iter_base>
slesBegin() const override;
std::unique_ptr<sles_type::iter_base>
slesEnd() const override;
std::unique_ptr<txs_type::iter_base>
txsBegin() const override;

View File

@@ -136,11 +136,22 @@ public:
using mapped_type =
std::shared_ptr<SLE const>;
using sles_type = detail::ReadViewFwdRange<
std::shared_ptr<SLE const>>;
struct sles_type : detail::ReadViewFwdRange<
std::shared_ptr<SLE const>>
{
explicit sles_type (ReadView const& view);
iterator begin() const;
iterator const& end() const;
};
using txs_type =
detail::ReadViewFwdRange<tx_type>;
struct txs_type
: detail::ReadViewFwdRange<tx_type>
{
explicit txs_type (ReadView const& view);
bool empty() const;
iterator begin() const;
iterator const& end() const;
};
virtual ~ReadView() = default;
@@ -148,17 +159,20 @@ public:
ReadView& operator= (ReadView const& other) = delete;
ReadView()
: txs(*this)
: sles(*this)
, txs(*this)
{
}
ReadView (ReadView const&)
: txs(*this)
: sles(*this)
, txs(*this)
{
}
ReadView (ReadView&& other)
: txs(*this)
: sles(*this)
, txs(*this)
{
}
@@ -254,6 +268,16 @@ public:
return amount;
}
// used by the implementation
virtual
std::unique_ptr<sles_type::iter_base>
slesBegin() const = 0;
// used by the implementation
virtual
std::unique_ptr<sles_type::iter_base>
slesEnd() const = 0;
// used by the implementation
virtual
std::unique_ptr<txs_type::iter_base>
@@ -289,8 +313,14 @@ public:
// Memberspaces
//
// The range of ledger entries
//sles_type sles;
/** Iterable range of ledger state items.
Visiting each state entry in the ledger can be
quite expensive. This will only visit entries in
the base ledger. Entries in open views, apply
views, or sandboxes will not be visited.
*/
sles_type sles;
// The range of transactions
txs_type txs;

View File

@@ -169,18 +169,6 @@ getCandidateLedger (LedgerIndex requested)
return (requested + 255) & (~255);
}
/** Run a functor on each SLE in a ReadView starting from the key start,
as long as the functor returns true.
*/
template <class Functor>
void forEachSLE(ReadView const& view, Functor func, uint256 const& start = {})
{
for (auto k = view.succ(start); k; k = view.succ(*k))
if (auto sle = view.read(keylet::unchecked(*k)))
if (! func(*sle))
break;
}
//------------------------------------------------------------------------------
//
// Modifiers

View File

@@ -73,6 +73,12 @@ public:
std::shared_ptr<SLE const>
read (Keylet const& k) const override;
std::unique_ptr<sles_type::iter_base>
slesBegin() const override;
std::unique_ptr<sles_type::iter_base>
slesEnd() const override;
std::unique_ptr<txs_type::iter_base>
txsBegin() const override;

View File

@@ -139,18 +139,6 @@ public:
ReadViewFwdRange (ReadViewFwdRange const&) = default;
ReadViewFwdRange& operator= (ReadViewFwdRange const&) = default;
/** Returns `true` if the range is empty. */
bool
empty() const;
/** Return iterator to the beginning of the range. */
iterator
begin() const;
/** Return iterator to one past the end of the range. */
iterator const&
end() const;
// VFALCO Otherwise causes errors on clang
//private:
// friend class ReadView;
@@ -161,7 +149,7 @@ public:
{
}
private:
protected:
ReadView const* view_;
boost::optional<iterator> mutable end_;
};

View File

@@ -27,7 +27,8 @@ template<class ValueType>
ReadViewFwdRange<ValueType>::iterator::iterator(
iterator const& other)
: view_ (other.view_)
, impl_ (other.impl_->copy())
, impl_ (other.impl_ ?
other.impl_->copy() : nullptr)
, cache_ (other.cache_)
{
}
@@ -59,7 +60,8 @@ ReadViewFwdRange<ValueType>::iterator::operator=(
if (this == &other)
return *this;
view_ = other.view_;
impl_ = other.impl_->copy();
impl_ = other.impl_ ?
other.impl_->copy() : nullptr;
cache_ = other.cache_;
return *this;
}
@@ -133,33 +135,6 @@ ReadViewFwdRange<ValueType>::iterator::operator++(int) ->
return prev;
}
//------------------------------------------------------------------------------
template<class ValueType>
bool
ReadViewFwdRange<ValueType>::empty() const
{
return begin() == end();
}
template<class ValueType>
auto
ReadViewFwdRange<ValueType>::begin() const ->
iterator
{
return iterator(view_, view_->txsBegin());
}
template<class ValueType>
auto
ReadViewFwdRange<ValueType>::end() const ->
iterator const&
{
if (! end_)
end_.emplace(view_, view_->txsEnd());
return *end_;
}
} // detail
} // ripple

View File

@@ -66,6 +66,20 @@ ApplyViewBase::read (Keylet const& k) const
return items_.read(*base_, k);
}
auto
ApplyViewBase::slesBegin() const ->
std::unique_ptr<sles_type::iter_base>
{
return base_->slesBegin();
}
auto
ApplyViewBase::slesEnd() const ->
std::unique_ptr<sles_type::iter_base>
{
return base_->slesEnd();
}
auto
ApplyViewBase::txsBegin() const ->
std::unique_ptr<txs_type::iter_base>

View File

@@ -158,6 +158,20 @@ OpenView::read (Keylet const& k) const
return items_.read(*base_, k);
}
auto
OpenView::slesBegin() const ->
std::unique_ptr<sles_type::iter_base>
{
return base_->slesBegin();
}
auto
OpenView::slesEnd() const ->
std::unique_ptr<sles_type::iter_base>
{
return base_->slesEnd();
}
auto
OpenView::txsBegin() const ->
std::unique_ptr<txs_type::iter_base>

View File

@@ -0,0 +1,75 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/ledger/ReadView.h>
namespace ripple {
ReadView::sles_type::sles_type(
ReadView const& view)
: ReadViewFwdRange(view)
{
}
auto
ReadView::sles_type::begin() const ->
iterator
{
return iterator(view_, view_->slesBegin());
}
auto
ReadView::sles_type::end() const ->
iterator const&
{
if (! end_)
end_ = iterator(view_, view_->slesEnd());
return *end_;
}
ReadView::txs_type::txs_type(
ReadView const& view)
: ReadViewFwdRange(view)
{
}
bool
ReadView::txs_type::empty() const
{
return begin() == end();
}
auto
ReadView::txs_type::begin() const ->
iterator
{
return iterator(view_, view_->txsBegin());
}
auto
ReadView::txs_type::end() const ->
iterator const&
{
if (! end_)
end_ = iterator(view_, view_->txsEnd());
return *end_;
}
} // ripple

View File

@@ -80,28 +80,27 @@ Json::Value doLedgerData (RPC::Context& context)
Json::Value& nodes = (jvResult[jss::state] = Json::arrayValue);
forEachSLE(*lpLedger, [&] (SLE const& sle) {
for(auto const& sle : lpLedger->sles)
{
if (limit-- <= 0)
{
auto marker = sle.key();
auto marker = sle->key();
jvResult[jss::marker] = to_string (--marker);
return false;
break;
}
if (isBinary)
{
Json::Value& entry = nodes.append (Json::objectValue);
entry[jss::data] = serializeHex(sle);
entry[jss::index] = to_string (sle.key());
entry[jss::data] = serializeHex(*sle);
entry[jss::index] = to_string (sle->key());
}
else
{
Json::Value& entry = nodes.append (sle.getJson (0));
entry[jss::index] = to_string (sle.key());
Json::Value& entry = nodes.append (sle->getJson (0));
entry[jss::index] = to_string (sle->key());
}
return true;
});
}
return jvResult;
}

View File

@@ -28,6 +28,7 @@
#include <ripple/ledger/impl/OpenView.cpp>
#include <ripple/ledger/impl/PaymentSandbox.cpp>
#include <ripple/ledger/impl/RawStateTable.cpp>
#include <ripple/ledger/impl/ReadView.cpp>
#include <ripple/ledger/impl/TxMeta.cpp>
#include <ripple/ledger/impl/View.cpp>