From 2059c09154870d704d26d10f0462db1e9b4ea676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 4 Jan 2020 21:07:33 +0100 Subject: Partial and memory-backed storage --- include/erebos/storage.h | 163 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 115 insertions(+), 48 deletions(-) (limited to 'include/erebos') diff --git a/include/erebos/storage.h b/include/erebos/storage.h index edb0aca..51fa3a4 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -12,39 +12,75 @@ namespace erebos { class Storage; +class PartialStorage; class Digest; class Ref; -class Object; +class PartialRef; + +template class RecordT; +typedef RecordT Record; +typedef RecordT PartialRecord; +template class ObjectT; +typedef ObjectT Object; +typedef ObjectT PartialObject; +class Blob; + template class Stored; -class Storage +class PartialStorage { public: - Storage(const Storage &) = default; - Storage & operator=(const Storage &) = delete; + typedef erebos::PartialRef Ref; + + PartialStorage(const PartialStorage &) = default; + PartialStorage & operator=(const PartialStorage &) = default; + virtual ~PartialStorage() = default; + + bool operator==(const PartialStorage &) const; + bool operator!=(const PartialStorage &) const; - static std::optional open(std::filesystem::path path); + PartialRef ref(const Digest &) const; - bool operator==(const Storage &) const; - bool operator!=(const Storage &) const; + std::optional loadObject(const Digest &) const; + PartialRef storeObject(const PartialObject &) const; + PartialRef storeObject(const PartialRecord &) const; + PartialRef storeObject(const Blob &) const; + +protected: + friend class Storage; + friend erebos::Ref; + friend erebos::PartialRef; + struct Priv; + const std::shared_ptr p; + PartialStorage(const std::shared_ptr p): p(p) {} +}; + +class Storage : public PartialStorage +{ +public: + typedef erebos::Ref Ref; + + Storage(const std::filesystem::path &); + Storage(const Storage &) = default; + Storage & operator=(const Storage &) = default; + + Storage deriveEphemeralStorage() const; + PartialStorage derivePartialStorage() const; std::optional ref(const Digest &) const; std::optional loadObject(const Digest &) const; Ref storeObject(const Object &) const; - Ref storeObject(const class Record &) const; - Ref storeObject(const class Blob &) const; + Ref storeObject(const Record &) const; + Ref storeObject(const Blob &) const; template Stored store(const T &) const; void storeKey(Ref pubref, const std::vector &) const; std::optional> loadKey(Ref pubref) const; -private: - friend class Ref; - struct Priv; - const std::shared_ptr p; - Storage(const std::shared_ptr p): p(p) {} +protected: + Storage(const std::shared_ptr p): PartialStorage(p) {} }; class Digest @@ -72,28 +108,49 @@ private: std::array value; }; -class Ref +class PartialRef { public: - Ref(const Ref &) = default; - Ref & operator=(const Ref &) = delete; + PartialRef(const PartialRef &) = default; + PartialRef & operator=(const PartialRef &) = default; - static std::optional create(Storage, const Digest &); + static PartialRef create(PartialStorage, const Digest &); const Digest & digest() const; - const Object & operator*() const; - const Object * operator->() const; - const Storage & storage() const; + operator bool() const; + const PartialObject operator*() const; + std::unique_ptr operator->() const; -private: + const PartialStorage & storage() const; + +protected: friend class Storage; struct Priv; const std::shared_ptr p; - Ref(const std::shared_ptr p): p(p) {} + PartialRef(const std::shared_ptr p): p(p) {} +}; + +class Ref : public PartialRef +{ +public: + Ref(const Ref &) = default; + Ref & operator=(const Ref &) = default; + + static std::optional create(Storage, const Digest &); + + constexpr operator bool() const { return true; } + const Object operator*() const; + std::unique_ptr operator->() const; + + const Storage & storage() const; + +protected: + Ref(const std::shared_ptr p): PartialRef(p) {} }; -class Record +template +class RecordT { public: class Item { @@ -103,7 +160,7 @@ public: int, std::string, std::vector, - Ref> Variant; + typename S::Ref> Variant; Item(const std::string & name): Item(name, std::monostate()) {} @@ -121,7 +178,7 @@ public: std::optional asInteger() const; std::optional asText() const; std::optional> asBinary() const; - std::optional asRef() const; + std::optional asRef() const; template std::optional> as() const; @@ -130,12 +187,12 @@ public: }; private: - Record(const std::shared_ptr> & ptr): + RecordT(const std::shared_ptr> & ptr): ptr(ptr) {} public: - Record(const std::vector &); - Record(std::vector &&); + RecordT(const std::vector &); + RecordT(std::vector &&); std::vector encode() const; const std::vector & items() const; @@ -144,15 +201,18 @@ public: std::vector items(const std::string & name) const; private: - friend class Object; + friend ObjectT; std::vector encodeInner() const; - static Record decode(Storage, + static std::optional> decode(const S &, std::vector::const_iterator, std::vector::const_iterator); const std::shared_ptr> ptr; }; +extern template class RecordT; +extern template class RecordT; + class Blob { public: @@ -162,9 +222,10 @@ public: std::vector encode() const; private: - friend class Object; + friend Object; + friend PartialObject; std::vector encodeInner() const; - static Blob decode(Storage, + static Blob decode( std::vector::const_iterator, std::vector::const_iterator); @@ -173,41 +234,47 @@ private: const std::shared_ptr> ptr; }; -class Object +template +class ObjectT { public: typedef std::variant< - Record, + RecordT, Blob> Variants; - Object(const Object &) = default; - Object(Variants content): content(content) {} - Object & operator=(const Object &) = delete; + ObjectT(const ObjectT &) = default; + ObjectT(Variants content): content(content) {} + ObjectT & operator=(const ObjectT &) = default; - static std::optional::const_iterator>> - decodePrefix(Storage, std::vector::const_iterator, + static std::optional, std::vector::const_iterator>> + decodePrefix(const S &, + std::vector::const_iterator, std::vector::const_iterator); - static std::optional decode(Storage, const std::vector &); - static std::optional decode(Storage, + static std::optional> decode(const S &, const std::vector &); + static std::optional> decode(const S &, std::vector::const_iterator, std::vector::const_iterator); - static std::vector decodeMany(Storage, const std::vector &); + static std::vector> decodeMany(const S &, const std::vector &); std::vector encode() const; - static std::optional load(const Ref &); + static std::optional> load(const typename S::Ref &); - std::optional asRecord() const; + std::optional> asRecord() const; std::optional asBlob() const; private: - friend class Record; - friend class Blob; + friend RecordT; + friend Blob; Variants content; }; +extern template class ObjectT; +extern template class ObjectT; + +template template -std::optional> Record::Item::as() const +std::optional> RecordT::Item::as() const { if (auto ref = asRef()) return Stored::load(ref.value()); -- cgit v1.2.3