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; }