update to contact4

This commit is contained in:
Green Sky 2025-03-06 16:20:30 +01:00
parent 491c30988e
commit c284e0779e
No known key found for this signature in database
GPG Key ID: DBE05085D874AB4A
2 changed files with 19 additions and 22 deletions

View File

@ -1,5 +1,7 @@
#include "./message_serializer.hpp"
#include <solanaceae/contact/contact_store_impl.hpp>
#include <solanaceae/message3/components.hpp>
#include <solanaceae/contact/components.hpp>
#include <solanaceae/object_store/meta_components.hpp>
@ -8,18 +10,9 @@
#include <iostream>
static Contact3 findContactByID(Contact3Registry& cr, const std::vector<uint8_t>& id) {
// TODO: id lookup table, this is very inefficent
for (const auto& [c_it, id_it] : cr.view<Contact::Components::ID>().each()) {
if (id == id_it.data) {
return c_it;
}
}
nlohmann::json MessageSerializerNJ::serlContactByID(Contact4 c) const {
const auto& cr = cs.registry();
return entt::null;
}
nlohmann::json MessageSerializerNJ::serlContactByID(Contact3 c) const {
if (!cr.valid(c)) {
// while this is invalid registry state, it is valid serialization
std::cerr << "MSC warning: encountered invalid contact\n";
@ -35,7 +28,7 @@ nlohmann::json MessageSerializerNJ::serlContactByID(Contact3 c) const {
return nlohmann::json::binary(cr.get<Contact::Components::ID>(c).data);
}
Contact3 MessageSerializerNJ::deserlContactByID(const nlohmann::json& j) {
Contact4 MessageSerializerNJ::deserlContactByID(const nlohmann::json& j) {
std::vector<uint8_t> id;
if (j.is_binary()) {
id = j.get_binary();
@ -43,11 +36,13 @@ Contact3 MessageSerializerNJ::deserlContactByID(const nlohmann::json& j) {
j.at("bytes").get_to(id);
}
Contact3 other_c = findContactByID(cr, id);
if (!cr.valid(other_c)) {
auto other_c = cs.getOneContactByID(ByteSpan{id});
if (!other_c) {
auto& cr = cs.registry();
// create sparse contact with id only
other_c = cr.create();
other_c = {cr, cr.create()};
cr.emplace_or_replace<Contact::Components::ID>(other_c, id);
cs.throwEventConstruct(other_c);
}
return other_c;
@ -86,7 +81,7 @@ ObjectHandle MessageSerializerNJ::deserlFileObjByID(const nlohmann::json& j) {
template<>
bool MessageSerializerNJ::component_get_json<Message::Components::ContactFrom>(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j) {
const Contact3 c = h.get<Message::Components::ContactFrom>().c;
const Contact4 c = h.get<Message::Components::ContactFrom>().c;
j = msc.serlContactByID(c);
return true;
@ -108,7 +103,7 @@ bool MessageSerializerNJ::component_emplace_or_replace_json<Message::Components:
template<>
bool MessageSerializerNJ::component_get_json<Message::Components::ContactTo>(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j) {
const Contact3 c = h.get<Message::Components::ContactTo>().c;
const Contact4 c = h.get<Message::Components::ContactTo>().c;
j = msc.serlContactByID(c);
return true;
@ -137,8 +132,10 @@ bool MessageSerializerNJ::component_get_json<Message::Components::ReceivedBy>(Me
return true;
}
const auto& cr = msc.cs.registry();
for (const auto& [c, v] : comp.ts) {
if (!msc.cr.valid(c) || !msc.cr.all_of<Contact::Components::ID>(c)) {
if (!cr.valid(c) || !cr.all_of<Contact::Components::ID>(c)) {
// while this is invalid registry state, it is valid serialization
// we just skip this contact entirely and drop the time
std::cerr << "MSC warning: encountered invalid contact / contact without ID\n";

View File

@ -12,9 +12,9 @@ struct MessageSerializerNJ {
using Registry = Message3Registry;
using Handle = Message3Handle;
static constexpr const char* version {"2"};
static constexpr const char* version {"3"};
Contact3Registry& cr;
ContactStore4I& cs;
ObjectStore2& os;
// nlohmann
@ -76,8 +76,8 @@ struct MessageSerializerNJ {
// helper
nlohmann::json serlContactByID(Contact3 c) const;
Contact3 deserlContactByID(const nlohmann::json& j);
nlohmann::json serlContactByID(Contact4 c) const;
Contact4 deserlContactByID(const nlohmann::json& j);
nlohmann::json serlFileObjByID(ObjectHandle o) const;
ObjectHandle deserlFileObjByID(const nlohmann::json& j);
};