From 67c6f9adb0c8762267a309b0f0fedb406313f369 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 19 Feb 2024 15:11:32 +0100 Subject: [PATCH] make empty contacts from ids on message load --- src/fragment_store/message_serializer.cpp | 45 +++++++++++++++-------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/fragment_store/message_serializer.cpp b/src/fragment_store/message_serializer.cpp index 6bb7df08..35823e64 100644 --- a/src/fragment_store/message_serializer.cpp +++ b/src/fragment_store/message_serializer.cpp @@ -7,6 +7,17 @@ #include +static Contact3 findContactByID(Contact3Registry& cr, const std::vector& id) { + // TODO: id lookup table, this is very inefficent + for (const auto& [c_it, id_it] : cr.view().each()) { + if (id == id_it.data) { + return c_it; + } + } + + return entt::null; +} + template<> bool MessageSerializerCallbacks::component_get_json(MessageSerializerCallbacks& msc, const Handle h, nlohmann::json& j) { const Contact3 c = h.get().c; @@ -38,16 +49,17 @@ bool MessageSerializerCallbacks::component_emplace_or_replace_json id = j.is_binary()?j:j["bytes"]; - // TODO: id lookup table, this is very inefficent - for (const auto& [c_it, id_it] : msc.cr.view().each()) { - if (id == id_it.data) { - h.emplace_or_replace(c_it); - return true; - } + Contact3 other_c = findContactByID(msc.cr, id); + if (!msc.cr.valid(other_c)) { + // create sparse contact with id only + other_c = msc.cr.create(); + msc.cr.emplace_or_replace(other_c, id); } - // TODO: should we really return false if the contact is unknown?? - return false; + h.emplace_or_replace(other_c); + + // TODO: should we return false if the contact is unknown?? + return true; } template<> @@ -81,14 +93,15 @@ bool MessageSerializerCallbacks::component_emplace_or_replace_json id = j.is_binary()?j:j["bytes"]; - // TODO: id lookup table, this is very inefficent - for (const auto& [c_it, id_it] : msc.cr.view().each()) { - if (id == id_it.data) { - h.emplace_or_replace(c_it); - return true; - } + Contact3 other_c = findContactByID(msc.cr, id); + if (!msc.cr.valid(other_c)) { + // create sparse contact with id only + other_c = msc.cr.create(); + msc.cr.emplace_or_replace(other_c, id); } - // TODO: should we really return false if the contact is unknown?? - return false; + h.emplace_or_replace(other_c); + + // TODO: should we return false if the contact is unknown?? + return true; }