From 7aa7649e980ff4b335b41eaea34a9a11820c3e2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Mon, 8 Aug 2022 22:25:46 +0200 Subject: Generation number of stored objects with caching --- include/erebos/storage.h | 53 +++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/include/erebos/storage.h b/include/erebos/storage.h index 24a59df..e894fee 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -41,6 +41,8 @@ using std::bind; using std::call_once; using std::make_unique; using std::move; +using std::string; +using std::vector; class PartialStorage { @@ -68,6 +70,10 @@ protected: struct Priv; const std::shared_ptr p; PartialStorage(const std::shared_ptr & p): p(p) {} + +public: + // For test usage + const Priv & priv() const { return *p; } }; class Storage : public PartialStorage @@ -200,6 +206,12 @@ public: const Storage & storage() const; + vector previous() const; + class Generation generation() const; + +private: + class Generation generationLocked() const; + protected: Ref(const std::shared_ptr p): PartialRef(p) {} }; @@ -349,6 +361,19 @@ std::optional> RecordT::Item::as() const return std::nullopt; } +class Generation +{ +public: + Generation(); + static Generation next(const vector &); + + explicit operator string() const; + +private: + Generation(size_t); + size_t gen; +}; + template class Stored { @@ -382,6 +407,8 @@ public: const T & operator*() const { init(); return *p->val; } const T * operator->() const { init(); return p->val.get(); } + Generation generation() const { return p->ref.generation(); } + std::vector> previous() const; bool precedes(const Stored &) const; @@ -448,27 +475,11 @@ Ref Stored::store(const Storage & st) const template std::vector> Stored::previous() const { - auto rec = p->ref->asRecord(); - if (!rec) - return {}; - - auto sdata = rec->item("SDATA").asRef(); - if (sdata) { - auto drec = sdata.value()->asRecord(); - if (!drec) - return {}; - - std::vector> res; - for (const Record::Item & i : drec->items("SPREV")) - if (auto x = i.as()) - res.push_back(*x); - return res; - } - - std::vector> res; - for (const Record::Item & i : rec->items("PREV")) - if (auto x = i.as()) - res.push_back(*x); + auto refs = p->ref.previous(); + vector> res; + res.reserve(refs.size()); + for (const auto & r : refs) + res.push_back(Stored::load(r)); return res; } -- cgit v1.2.3