diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6540420..f793b77 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.9 FATAL_ERROR) add_library(fragment_store ./fragment_store/fragment_store_i.hpp + ./fragment_store/fragment_store_i.cpp ./fragment_store/types.hpp ./fragment_store/meta_components.hpp ./fragment_store/meta_components_id.inl diff --git a/src/fragment_store/fragment_store.cpp b/src/fragment_store/fragment_store.cpp index c0aa02c..3c0a4cc 100644 --- a/src/fragment_store/fragment_store.cpp +++ b/src/fragment_store/fragment_store.cpp @@ -33,23 +33,34 @@ FragmentStore::FragmentStore(void) { { // random namespace const auto num0 = _rng(); const auto num1 = _rng(); + const auto num2 = _rng(); + const auto num3 = _rng(); - _session_uuid_namespace[0] = (num0 >> 0) & 0xff; - _session_uuid_namespace[1] = (num0 >> 8) & 0xff; - _session_uuid_namespace[2] = (num0 >> 16) & 0xff; - _session_uuid_namespace[3] = (num0 >> 24) & 0xff; + _session_uuid_namespace[0+0] = (num0 >> 0) & 0xff; + _session_uuid_namespace[0+1] = (num0 >> 8) & 0xff; + _session_uuid_namespace[0+2] = (num0 >> 16) & 0xff; + _session_uuid_namespace[0+3] = (num0 >> 24) & 0xff; - _session_uuid_namespace[4] = (num1 >> 0) & 0xff; - _session_uuid_namespace[5] = (num1 >> 8) & 0xff; - _session_uuid_namespace[6] = (num1 >> 16) & 0xff; - _session_uuid_namespace[7] = (num1 >> 24) & 0xff; + _session_uuid_namespace[4+0] = (num1 >> 0) & 0xff; + _session_uuid_namespace[4+1] = (num1 >> 8) & 0xff; + _session_uuid_namespace[4+2] = (num1 >> 16) & 0xff; + _session_uuid_namespace[4+3] = (num1 >> 24) & 0xff; + _session_uuid_namespace[8+0] = (num2 >> 0) & 0xff; + _session_uuid_namespace[8+1] = (num2 >> 8) & 0xff; + _session_uuid_namespace[8+2] = (num2 >> 16) & 0xff; + _session_uuid_namespace[8+3] = (num2 >> 24) & 0xff; + + _session_uuid_namespace[12+0] = (num3 >> 0) & 0xff; + _session_uuid_namespace[12+1] = (num3 >> 8) & 0xff; + _session_uuid_namespace[12+2] = (num3 >> 16) & 0xff; + _session_uuid_namespace[12+3] = (num3 >> 24) & 0xff; } registerSerializers(); } FragmentStore::FragmentStore( - std::array session_uuid_namespace + std::array session_uuid_namespace ) : _session_uuid_namespace(std::move(session_uuid_namespace)) { registerSerializers(); } @@ -58,22 +69,34 @@ FragmentHandle FragmentStore::fragmentHandle(FragmentID fid) { return {_reg, fid}; } -std::vector FragmentStore::generateNewUID(std::array& uuid_namespace) { +std::vector FragmentStore::generateNewUID(std::array& uuid_namespace) { std::vector new_uid(uuid_namespace.cbegin(), uuid_namespace.cend()); - new_uid.resize(new_uid.size() + 8); + new_uid.resize(new_uid.size() + 16); const auto num0 = _rng(); const auto num1 = _rng(); + const auto num2 = _rng(); + const auto num3 = _rng(); new_uid[uuid_namespace.size()+0] = (num0 >> 0) & 0xff; new_uid[uuid_namespace.size()+1] = (num0 >> 8) & 0xff; new_uid[uuid_namespace.size()+2] = (num0 >> 16) & 0xff; new_uid[uuid_namespace.size()+3] = (num0 >> 24) & 0xff; - new_uid[uuid_namespace.size()+4] = (num1 >> 0) & 0xff; - new_uid[uuid_namespace.size()+5] = (num1 >> 8) & 0xff; - new_uid[uuid_namespace.size()+6] = (num1 >> 16) & 0xff; - new_uid[uuid_namespace.size()+7] = (num1 >> 24) & 0xff; + new_uid[uuid_namespace.size()+4+0] = (num1 >> 0) & 0xff; + new_uid[uuid_namespace.size()+4+1] = (num1 >> 8) & 0xff; + new_uid[uuid_namespace.size()+4+2] = (num1 >> 16) & 0xff; + new_uid[uuid_namespace.size()+4+3] = (num1 >> 24) & 0xff; + + new_uid[uuid_namespace.size()+8+0] = (num2 >> 0) & 0xff; + new_uid[uuid_namespace.size()+8+1] = (num2 >> 8) & 0xff; + new_uid[uuid_namespace.size()+8+2] = (num2 >> 16) & 0xff; + new_uid[uuid_namespace.size()+8+3] = (num2 >> 24) & 0xff; + + new_uid[uuid_namespace.size()+12+0] = (num3 >> 0) & 0xff; + new_uid[uuid_namespace.size()+12+1] = (num3 >> 8) & 0xff; + new_uid[uuid_namespace.size()+12+2] = (num3 >> 16) & 0xff; + new_uid[uuid_namespace.size()+12+3] = (num3 >> 24) & 0xff; return new_uid; } @@ -114,6 +137,8 @@ FragmentID FragmentStore::newFragmentMemoryOwned( // TODO: memory comp _reg.emplace>>(new_frag) = std::move(new_data); + throwEventConstruct(new_frag); + return new_frag; } @@ -159,6 +184,8 @@ FragmentID FragmentStore::newFragmentFile( _reg.emplace(new_frag, mft); + throwEventConstruct(new_frag); + // meta needs to be synced to file std::function empty_data_cb = [](const uint8_t*, uint64_t) -> uint64_t { return 0; }; if (!syncToStorage(new_frag, empty_data_cb)) { @@ -476,6 +503,7 @@ size_t FragmentStore::scanStoragePath(std::string_view path) { if (it.meta_ext == ".meta.msgpack") { // uh // read binary header + assert(false); } else if (it.meta_ext == ".meta.json") { std::ifstream file(it.frag_path.generic_u8string() + it.meta_ext); if (!file.is_open()) { @@ -506,6 +534,8 @@ size_t FragmentStore::scanStoragePath(std::string_view path) { std::cerr << "FS warning: missing deserializer for meta key '" << k << "'\n"; } } + // throw new frag event here + throwEventConstruct(fh); count++; } else { assert(false); @@ -553,8 +583,6 @@ static bool deserl_json_data_comp_type(FragmentHandle fh, const nlohmann::json& return true; } -// TODO: dserl comp type - void FragmentStore::registerSerializers(void) { _sc.registerSerializerJson(serl_json_data_enc_type); _sc.registerDeSerializerJson(deserl_json_data_enc_type); diff --git a/src/fragment_store/fragment_store.hpp b/src/fragment_store/fragment_store.hpp index 101a933..0aec782 100644 --- a/src/fragment_store/fragment_store.hpp +++ b/src/fragment_store/fragment_store.hpp @@ -18,10 +18,8 @@ #include struct FragmentStore : public FragmentStoreI { - FragmentRegistry _reg; - std::minstd_rand _rng{std::random_device{}()}; - std::array _session_uuid_namespace; + std::array _session_uuid_namespace; std::string _default_store_path; @@ -31,7 +29,7 @@ struct FragmentStore : public FragmentStoreI { SerializerCallbacks _sc; FragmentStore(void); - FragmentStore(std::array session_uuid_namespace); + FragmentStore(std::array session_uuid_namespace); // HACK: get access to the reg FragmentHandle fragmentHandle(FragmentID fid); @@ -39,7 +37,7 @@ struct FragmentStore : public FragmentStoreI { // TODO: make the frags ref counted // TODO: check for exising - std::vector generateNewUID(std::array& uuid_namespace); + std::vector generateNewUID(std::array& uuid_namespace); std::vector generateNewUID(void); // ========== new fragment ========== diff --git a/src/fragment_store/fragment_store_i.cpp b/src/fragment_store/fragment_store_i.cpp new file mode 100644 index 0000000..75a9e44 --- /dev/null +++ b/src/fragment_store/fragment_store_i.cpp @@ -0,0 +1,23 @@ +#include "./fragment_store_i.hpp" + +#include + +void FragmentStoreI::throwEventConstruct(const FragmentID fid) { + std::cout << "FSI debug: event construct " << entt::to_integral(fid) << "\n"; + dispatch( + FragmentStore_Event::fragment_construct, + Fragment::Events::FragmentConstruct{ + FragmentHandle{_reg, fid} + } + ); +} + +void FragmentStoreI::throwEventUpdate(const FragmentID fid) { + std::cout << "FSI debug: event updated " << entt::to_integral(fid) << "\n"; + dispatch( + FragmentStore_Event::fragment_updated, + Fragment::Events::FragmentUpdated{ + FragmentHandle{_reg, fid} + } + ); +} diff --git a/src/fragment_store/fragment_store_i.hpp b/src/fragment_store/fragment_store_i.hpp index be3f9c1..8dffdf3 100644 --- a/src/fragment_store/fragment_store_i.hpp +++ b/src/fragment_store/fragment_store_i.hpp @@ -1,6 +1,9 @@ #pragma once +#include + #include +#include #include @@ -9,7 +12,52 @@ enum class FragmentID : uint32_t {}; using FragmentRegistry = entt::basic_registry; using FragmentHandle = entt::basic_handle; -struct FragmentStoreI { - virtual ~FragmentStoreI(void) {} +namespace Fragment::Events { + + struct FragmentConstruct { + const FragmentHandle e; + }; + struct FragmentUpdated { + const FragmentHandle e; + }; + //struct MessageDestory { + //const Message3Handle e; + //}; + +} // Fragment::Events + +enum class FragmentStore_Event : uint32_t { + fragment_construct, + fragment_updated, + //message_destroy, + + MAX +}; + +struct FragmentStoreEventI { + using enumType = FragmentStore_Event; + + virtual ~FragmentStoreEventI(void) {} + + virtual bool onEvent(const Fragment::Events::FragmentConstruct&) { return false; } + virtual bool onEvent(const Fragment::Events::FragmentUpdated&) { return false; } + //virtual bool onEvent(const Fragment::Events::MessageDestory&) { return false; } + + // mm3 + // send text + // send file path +}; +using FragmentStoreEventProviderI = EventProviderI; + +struct FragmentStoreI : public FragmentStoreEventProviderI { + static constexpr const char* version {"1"}; + + FragmentRegistry _reg; + + virtual ~FragmentStoreI(void) {} + + void throwEventConstruct(const FragmentID fid); + void throwEventUpdate(const FragmentID fid); + //void throwEventDestroy(); }; diff --git a/src/fragment_store/message_fragment_store.cpp b/src/fragment_store/message_fragment_store.cpp index cecc02e..fffa084 100644 --- a/src/fragment_store/message_fragment_store.cpp +++ b/src/fragment_store/message_fragment_store.cpp @@ -185,6 +185,10 @@ void MessageFragmentStore::handleMessage(const Message3Handle& m) { fuid_open.emplace_back(Message::Components::OpenFragments::OpenFrag{msg_ts, msg_ts, fragment_uid}); std::cout << "MFS: created new fragment " << bin2hex(fragment_uid) << "\n"; + + _fs_ignore_event = true; + _fs.throwEventConstruct(fh); + _fs_ignore_event = false; } // if this is still empty, something is very wrong and we exit here diff --git a/src/fragment_store/message_fragment_store.hpp b/src/fragment_store/message_fragment_store.hpp index 559f477..8093948 100644 --- a/src/fragment_store/message_fragment_store.hpp +++ b/src/fragment_store/message_fragment_store.hpp @@ -47,6 +47,7 @@ class MessageFragmentStore : public RegistryMessageModelEventI { Contact3Registry& _cr; RegistryMessageModel& _rmm; FragmentStore& _fs; + bool _fs_ignore_event {false}; // for message components only MessageSerializerCallbacks _sc;