From a4db4f377b6d0d3c91aba8bc4a2392845c2ef18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Thu, 16 Apr 2020 22:42:01 +0200 Subject: Storage: lazy loading of Stored value --- include/erebos/storage.h | 18 +++++++++++------- src/storage.h | 1 - 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/erebos/storage.h b/include/erebos/storage.h index d46c056..09dc481 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -313,7 +314,7 @@ std::optional> RecordT::Item::as() const template class Stored { - Stored(Ref ref, std::shared_ptr val): mref(ref), mval(val) {} + Stored(Ref ref, std::future && val): mref(ref), mval(std::move(val)) {} friend class Storage; public: Stored(const Stored &) = default; @@ -337,30 +338,33 @@ public: bool operator>=(const Stored & other) const { return mref.digest() >= other.mref.digest(); } - const T & operator*() const { return *mval; } - const T * operator->() const { return mval.get(); } + const T & operator*() const { return mval.get(); } + const T * operator->() const { return &mval.get(); } std::vector> previous() const; bool precedes(const Stored &) const; const Ref & ref() const { return mref; } - const std::shared_ptr & value() const { return mval; } private: Ref mref; - std::shared_ptr mval; + std::shared_future mval; }; template Stored Storage::store(const T & val) const { - return Stored(val.store(*this), std::make_shared(val)); + return Stored(val.store(*this), std::async(std::launch::deferred, [val] { + return val; + })); } template Stored Stored::load(const Ref & ref) { - return Stored(ref, std::make_shared(T::load(ref))); + return Stored(ref, std::async(std::launch::deferred, [ref] { + return T::load(ref); + })); } template diff --git a/src/storage.h b/src/storage.h index c67be22..1635d40 100644 --- a/src/storage.h +++ b/src/storage.h @@ -2,7 +2,6 @@ #include "erebos/storage.h" -#include #include #include -- cgit v1.2.3