From 0bee44226cc60f2d18188dfb23f485e166073363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Wed, 30 Mar 2022 22:58:00 +0200 Subject: Identity update --- include/erebos/identity.h | 3 +++ src/identity.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++-- src/identity.h | 1 + 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/include/erebos/identity.h b/include/erebos/identity.h index 888f162..336a18a 100644 --- a/include/erebos/identity.h +++ b/include/erebos/identity.h @@ -38,6 +38,8 @@ public: bool operator!=(const Identity & other) const; std::optional ref() const; + std::vector refs() const; + std::vector updates() const; class Builder { @@ -56,6 +58,7 @@ public: static Builder create(const Storage &); Builder modify() const; + Identity update(const vector>> &) const; private: struct Priv; diff --git a/src/identity.cpp b/src/identity.cpp index 0d35122..9077db2 100644 --- a/src/identity.cpp +++ b/src/identity.cpp @@ -109,12 +109,13 @@ bool Identity::sameAs(const Identity & other) const bool Identity::operator==(const Identity & other) const { - return p->data == other.p->data; + return p->data == other.p->data && + p->updates == other.p->updates; } bool Identity::operator!=(const Identity & other) const { - return p->data != other.p->data; + return !(*this == other); } optional Identity::ref() const @@ -124,6 +125,24 @@ optional Identity::ref() const return nullopt; } +vector Identity::refs() const +{ + vector res; + res.reserve(p->data.size()); + for (const auto & idata : p->data) + res.push_back(idata.ref()); + return res; +} + +vector Identity::updates() const +{ + vector res; + res.reserve(p->updates.size()); + for (const auto & idata : p->updates) + res.push_back(idata.ref()); + return res; +} + Identity::Builder Identity::create(const Storage & st) { return Builder (new Builder::Priv { @@ -143,6 +162,37 @@ Identity::Builder Identity::modify() const }); } +Identity Identity::update(const vector>> & updates) const +{ + vector>> ndata = p->data; + vector>> ownerUpdates = p->updates; + + for (const auto & u : updates) { + vector>> tmp = p->data; + tmp.push_back(u); + + size_t tmp_size = tmp.size(); + filterAncestors(tmp); + + if (tmp.size() < tmp_size) + ndata.push_back(u); + else + ownerUpdates.push_back(u); + } + + filterAncestors(ndata); + filterAncestors(ownerUpdates); + + if (auto p = Priv::validate(ndata)) { + p->updates = move(ownerUpdates); + if (p->owner && !p->updates.empty()) + p->owner = p->owner->update(p->updates); + return Identity(move(p)); + } + + return *this; +} + Identity::Builder::Builder(Priv * p): p(p) {} @@ -263,6 +313,7 @@ shared_ptr Identity::Priv::validate(const vectorkeyMessage.value(), diff --git a/src/identity.h b/src/identity.h index e0d2803..4b61c66 100644 --- a/src/identity.h +++ b/src/identity.h @@ -28,6 +28,7 @@ struct IdentityData struct Identity::Priv { vector>> data; + vector>> updates; shared_future> name; optional owner; Stored keyMessage; -- cgit v1.2.3