diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2019-12-21 21:42:20 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2019-12-21 21:42:20 +0100 |
commit | f94443c63dfd63300e5bd29889935fd1f451175e (patch) | |
tree | 7722cfdac0ed76ff7f1fd525abb2ab15e0b0c998 /include | |
parent | d084c069be38b6f3ad74912ca629403d9fdaec58 (diff) |
Identity storage and modification
Diffstat (limited to 'include')
-rw-r--r-- | include/erebos/identity.h | 19 | ||||
-rw-r--r-- | include/erebos/storage.h | 39 |
2 files changed, 56 insertions, 2 deletions
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<std::string> name() const; std::optional<Identity> owner() const; + std::optional<Ref> 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<Priv> 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<Storage> open(std::filesystem::path path); + + bool operator==(const Storage &) const; + bool operator!=(const Storage &) const; + std::optional<Ref> ref(const Digest &) const; - std::optional<Object> load(const Digest &) const; - Ref store(const Object &) const; + + std::optional<Object> loadObject(const Digest &) const; + Ref storeObject(const Object &) const; + Ref storeObject(const class Record &) const; + Ref storeObject(const class Blob &) const; + + template<typename T> Stored<T> store(const T &) const; + + void storeKey(Ref pubref, const std::vector<uint8_t> &) const; + std::optional<std::vector<uint8_t>> 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<typename T> + Item(const std::string & name, const Stored<T> & value): + Item(name, value.ref) {} + Item(const Item &) = default; Item & operator=(const Item &) = delete; @@ -118,6 +136,7 @@ private: public: Record(const std::vector<Item> &); + Record(std::vector<Item> &&); std::vector<uint8_t> encode() const; const std::vector<Item> & items() const; @@ -192,8 +211,10 @@ template<typename T> class Stored { Stored(Ref ref, std::shared_ptr<T> val): ref(ref), val(val) {} + friend class Storage; public: static std::optional<Stored<T>> load(const Ref &); + Ref store(const Storage &) const; bool operator==(const Stored<T> & other) const { return ref.digest() == other.ref.digest(); } @@ -219,6 +240,12 @@ public: }; template<typename T> +Stored<T> Storage::store(const T & val) const +{ + return Stored(val.store(*this), std::make_shared<T>(val)); +} + +template<typename T> std::optional<Stored<T>> Stored<T>::load(const Ref & ref) { if (auto val = T::load(ref)) @@ -227,6 +254,14 @@ std::optional<Stored<T>> Stored<T>::load(const Ref & ref) } template<typename T> +Ref Stored<T>::store(const Storage & st) const +{ + if (st == ref.storage()) + return ref; + return st.storeObject(*ref); +} + +template<typename T> std::vector<Stored<T>> Stored<T>::previous() const { auto rec = ref->asRecord(); |