From f94443c63dfd63300e5bd29889935fd1f451175e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 21 Dec 2019 21:42:20 +0100 Subject: Identity storage and modification --- include/erebos/identity.h | 19 +++++++++++++++++++ include/erebos/storage.h | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) (limited to 'include/erebos') diff --git a/include/erebos/identity.h b/include/erebos/identity.h index 7b66d82..f57a12e 100644 --- a/include/erebos/identity.h +++ b/include/erebos/identity.h @@ -12,6 +12,25 @@ public: std::optional name() const; std::optional owner() const; + std::optional ref() const; + + class Builder + { + public: + Identity commit() const; + + void name(const std::string &); + void owner(const Identity &); + + private: + friend class Identity; + struct Priv; + const std::shared_ptr p; + Builder(Priv * p): p(p) {} + }; + + static Builder create(const Storage &); + Builder modify() const; private: struct Priv; diff --git a/include/erebos/storage.h b/include/erebos/storage.h index ea0e894..3777572 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -23,9 +23,21 @@ public: Storage & operator=(const Storage &) = delete; static std::optional open(std::filesystem::path path); + + bool operator==(const Storage &) const; + bool operator!=(const Storage &) const; + std::optional ref(const Digest &) const; - std::optional load(const Digest &) const; - Ref store(const Object &) const; + + std::optional loadObject(const Digest &) const; + Ref storeObject(const Object &) const; + Ref storeObject(const class Record &) const; + Ref storeObject(const class 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; @@ -71,6 +83,8 @@ public: const Object & operator*() const; const Object * operator->() const; + const Storage & storage() const; + private: friend class Storage; struct Priv; @@ -94,6 +108,10 @@ public: Item(name, std::monostate()) {} Item(const std::string & name, Variant value): name(name), value(value) {} + template + Item(const std::string & name, const Stored & value): + Item(name, value.ref) {} + Item(const Item &) = default; Item & operator=(const Item &) = delete; @@ -118,6 +136,7 @@ private: public: Record(const std::vector &); + Record(std::vector &&); std::vector encode() const; const std::vector & items() const; @@ -192,8 +211,10 @@ template class Stored { Stored(Ref ref, std::shared_ptr val): ref(ref), val(val) {} + friend class Storage; public: static std::optional> load(const Ref &); + Ref store(const Storage &) const; bool operator==(const Stored & other) const { return ref.digest() == other.ref.digest(); } @@ -218,6 +239,12 @@ public: const std::shared_ptr val; }; +template +Stored Storage::store(const T & val) const +{ + return Stored(val.store(*this), std::make_shared(val)); +} + template std::optional> Stored::load(const Ref & ref) { @@ -226,6 +253,14 @@ std::optional> Stored::load(const Ref & ref) return std::nullopt; } +template +Ref Stored::store(const Storage & st) const +{ + if (st == ref.storage()) + return ref; + return st.storeObject(*ref); +} + template std::vector> Stored::previous() const { -- cgit v1.2.3