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 --- src/identity.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'src/identity.cpp') 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(), -- cgit v1.2.3