From 110bbf3956a81f37dac37b67b4d2a5edfbe8b6b9 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 15 Jul 2015 14:58:08 -0700 Subject: [PATCH] Add CachedLedger: This type alias provide cache-wrapping for Ledger objects. Through the CachedLedger interface, access to the underlying Ledger is permitted to allow for cases where the implementation must perform Ledger specific activities. For example, building a fetch pack from the contained SHAMap objects. The CachingReadView is refactored: * Renamed to CachedView * Templated on Base, the base type * base() returns a shared_ptr to the wrapped object * Constructor requires a shared_ptr --- Builds/VisualStudio2013/RippleD.vcxproj | 4 +- .../VisualStudio2013/RippleD.vcxproj.filters | 4 +- src/ripple/app/ledger/Ledger.h | 4 + src/ripple/app/ledger/OpenLedger.h | 2 +- src/ripple/app/ledger/impl/OpenLedger.cpp | 12 +- .../{CachingReadView.h => CachedView.h} | 109 ++++++++++-------- .../{CachingReadView.cpp => CachedView.cpp} | 20 +--- src/ripple/unity/ledger.cpp | 2 +- 8 files changed, 85 insertions(+), 72 deletions(-) rename src/ripple/ledger/{CachingReadView.h => CachedView.h} (61%) rename src/ripple/ledger/impl/{CachingReadView.cpp => CachedView.cpp} (82%) diff --git a/Builds/VisualStudio2013/RippleD.vcxproj b/Builds/VisualStudio2013/RippleD.vcxproj index b9cf2c0ab7..e3decf3b5b 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj +++ b/Builds/VisualStudio2013/RippleD.vcxproj @@ -2303,7 +2303,7 @@ - + @@ -2333,7 +2333,7 @@ True True - + True True diff --git a/Builds/VisualStudio2013/RippleD.vcxproj.filters b/Builds/VisualStudio2013/RippleD.vcxproj.filters index f102266f88..23fdcf303c 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2013/RippleD.vcxproj.filters @@ -3030,7 +3030,7 @@ ripple\ledger - + ripple\ledger @@ -3063,7 +3063,7 @@ ripple\ledger\impl - + ripple\ledger\impl diff --git a/src/ripple/app/ledger/Ledger.h b/src/ripple/app/ledger/Ledger.h index 130468e069..2c053415a9 100644 --- a/src/ripple/app/ledger/Ledger.h +++ b/src/ripple/app/ledger/Ledger.h @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -412,6 +413,9 @@ private: std::uint32_t mutable mReserveIncrement = 0; }; +/** A ledger wrapped in a CachedView. */ +using CachedLedger = CachedView; + //------------------------------------------------------------------------------ // // API diff --git a/src/ripple/app/ledger/OpenLedger.h b/src/ripple/app/ledger/OpenLedger.h index 14629266e1..5989a26331 100644 --- a/src/ripple/app/ledger/OpenLedger.h +++ b/src/ripple/app/ledger/OpenLedger.h @@ -68,7 +68,7 @@ public: @param ledger A closed ledger */ explicit - OpenLedger (std::shared_ptr< + OpenLedger(std::shared_ptr< Ledger const> const& ledger, Config const& config, CachedSLEs& cache, beast::Journal journal); diff --git a/src/ripple/app/ledger/impl/OpenLedger.cpp b/src/ripple/app/ledger/impl/OpenLedger.cpp index d7f87c63bd..56dc2bf899 100644 --- a/src/ripple/app/ledger/impl/OpenLedger.cpp +++ b/src/ripple/app/ledger/impl/OpenLedger.cpp @@ -20,13 +20,13 @@ #include #include #include -#include +#include #include namespace ripple { -OpenLedger::OpenLedger( - std::shared_ptr const& ledger, +OpenLedger::OpenLedger(std::shared_ptr< + Ledger const> const& ledger, Config const& config, CachedSLEs& cache, beast::Journal journal) : j_ (journal) @@ -65,7 +65,7 @@ OpenLedger::modify (std::function< } void -OpenLedger::accept (std::shared_ptr< +OpenLedger::accept(std::shared_ptr< Ledger const> const& ledger, OrderedTxs const& locals, bool retriesFirst, OrderedTxs& retries, ApplyFlags flags, @@ -113,12 +113,12 @@ OpenLedger::accept (std::shared_ptr< //------------------------------------------------------------------------------ std::shared_ptr -OpenLedger::create (std::shared_ptr< +OpenLedger::create(std::shared_ptr< Ledger const> const& ledger) { return std::make_shared( open_ledger, std::make_shared< - CachingReadView const>(ledger, + CachedLedger const>(ledger, cache_)); } diff --git a/src/ripple/ledger/CachingReadView.h b/src/ripple/ledger/CachedView.h similarity index 61% rename from src/ripple/ledger/CachingReadView.h rename to src/ripple/ledger/CachedView.h index b450a2fdb5..27d4b69a23 100644 --- a/src/ripple/ledger/CachingReadView.h +++ b/src/ripple/ledger/CachedView.h @@ -17,8 +17,8 @@ */ //============================================================================== -#ifndef RIPPLE_LEDGER_CACHINGREADVIEW_H_INCLUDED -#define RIPPLE_LEDGER_CACHINGREADVIEW_H_INCLUDED +#ifndef RIPPLE_LEDGER_CACHEDVIEW_H_INCLUDED +#define RIPPLE_LEDGER_CACHEDVIEW_H_INCLUDED #include #include @@ -26,41 +26,39 @@ #include #include #include +#include namespace ripple { -//------------------------------------------------------------------------------ +namespace detail { -/** ReadView that caches by key and hash. */ -class CachingReadView - : public ReadView +class CachedViewImpl + : public DigestAwareReadView { private: + DigestAwareReadView const& base_; CachedSLEs& cache_; std::mutex mutable mutex_; - DigestAwareReadView const& base_; - std::shared_ptr hold_; std::unordered_map, hardened_hash<>> mutable map_; public: - CachingReadView() = delete; - CachingReadView (CachingReadView const&) = delete; - CachingReadView& operator= (CachingReadView const&) = delete; + CachedViewImpl() = delete; + CachedViewImpl (CachedViewImpl const&) = delete; + CachedViewImpl& operator= (CachedViewImpl const&) = delete; - CachingReadView( - DigestAwareReadView const* base, - CachedSLEs& cache, - std::shared_ptr hold); - - CachingReadView (std::shared_ptr< - DigestAwareReadView const> const& base, + CachedViewImpl (DigestAwareReadView const* base, CachedSLEs& cache) - : CachingReadView (&*base, cache, base) + : base_ (*base) + , cache_ (cache) { } + // + // ReadView + // + bool exists (Keylet const& k) const override; @@ -110,39 +108,58 @@ public: return base_.txRead(key); } + // + // DigestAwareReadView + // + + boost::optional + digest (key_type const& key) const override + { + return base_.digest(key); + } + }; -//------------------------------------------------------------------------------ +} // detail -/** Wrap a DigestAwareReadView with a cache. +/** Wraps a DigestAwareReadView to provide caching. - Effects: - - Returns ownership of a base ReadView that is - wrapped in a thread-safe cache. - - The returned ReadView gains a reference to - the base. - - Postconditions: - - The base object will not be destroyed before - the returned view is destroyed. - - The caller is responsible for ensuring that the - `cache` object lifetime extends to the lifetime of - the returned object. + @tparam Base A subclass of DigestAwareReadView */ -inline -std::shared_ptr -makeCached (std::shared_ptr< - DigestAwareReadView const> const& base, - CachedSLEs& cache) +template +class CachedView + : public detail::CachedViewImpl { - return std::make_shared< - CachingReadView const>( - &*base, cache, base); -} +private: + static_assert(std::is_base_of< + DigestAwareReadView, Base>::value, ""); + + std::shared_ptr sp_; + +public: + using base_type = Base; + + CachedView() = delete; + CachedView (CachedView const&) = delete; + CachedView& operator= (CachedView const&) = delete; + + CachedView (std::shared_ptr< + Base const> const& base, CachedSLEs& cache) + : CachedViewImpl (base.get(), cache) + , sp_ (base) + { + } + + /** Returns the base type. + + @note This breaks encapsulation and bypasses the cache. + */ + std::shared_ptr const& + base() const + { + return sp_; + } +}; } // ripple diff --git a/src/ripple/ledger/impl/CachingReadView.cpp b/src/ripple/ledger/impl/CachedView.cpp similarity index 82% rename from src/ripple/ledger/impl/CachingReadView.cpp rename to src/ripple/ledger/impl/CachedView.cpp index e4aba573dd..e5007cd4c5 100644 --- a/src/ripple/ledger/impl/CachingReadView.cpp +++ b/src/ripple/ledger/impl/CachedView.cpp @@ -18,30 +18,21 @@ //============================================================================== #include -#include +#include #include #include namespace ripple { - -CachingReadView::CachingReadView( - DigestAwareReadView const* base, - CachedSLEs& cache, - std::shared_ptr hold) - : cache_ (cache) - , base_ (*base) - , hold_ (hold) -{ -} +namespace detail { bool -CachingReadView::exists (Keylet const& k) const +CachedViewImpl::exists (Keylet const& k) const { return read(k) != nullptr; } std::shared_ptr -CachingReadView::read (Keylet const& k) const +CachedViewImpl::read (Keylet const& k) const { { std::lock_guard< @@ -70,9 +61,10 @@ CachingReadView::read (Keylet const& k) const return sle; } if (! k.check(*iter->second)) - LogicError("CachingReadView::read: wrong type"); + LogicError("CachedView::read: wrong type"); return iter->second; } +} // detail } // ripple diff --git a/src/ripple/unity/ledger.cpp b/src/ripple/unity/ledger.cpp index 29a7b59b78..fe25d9271c 100644 --- a/src/ripple/unity/ledger.cpp +++ b/src/ripple/unity/ledger.cpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include #include #include