diff --git a/src/fragment_store/fragment_store.cpp b/src/fragment_store/fragment_store.cpp index fad12464..a13431bd 100644 --- a/src/fragment_store/fragment_store.cpp +++ b/src/fragment_store/fragment_store.cpp @@ -27,6 +27,21 @@ static const char* metaFileTypeSuffix(MetaFileType mft) { } FragmentStore::FragmentStore(void) { + { // random namespace + const auto num0 = _rng(); + const auto num1 = _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[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; + + } registerSerializers(); } @@ -40,6 +55,30 @@ entt::basic_handle> FragmentStore::fragmentHand return {_reg, fid}; } +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); + + const auto num0 = _rng(); + const auto num1 = _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; + + return new_uid; +} + +std::vector FragmentStore::generateNewUID(void) { + return generateNewUID(_session_uuid_namespace); +} + FragmentID FragmentStore::newFragmentMemoryOwned( const std::vector& id, size_t initial_size @@ -126,6 +165,12 @@ FragmentID FragmentStore::newFragmentFile( return new_frag; } +FragmentID FragmentStore::newFragmentFile( + std::string_view store_path, + MetaFileType mft +) { + return newFragmentFile(store_path, mft, generateNewUID()); +} FragmentID FragmentStore::getFragmentByID( const std::vector& id diff --git a/src/fragment_store/fragment_store.hpp b/src/fragment_store/fragment_store.hpp index 58b3aef3..90cff43b 100644 --- a/src/fragment_store/fragment_store.hpp +++ b/src/fragment_store/fragment_store.hpp @@ -1,7 +1,6 @@ #pragma once #include "./fragment_store_i.hpp" -#include "entt/entity/fwd.hpp" #include #include @@ -14,6 +13,7 @@ #include #include #include +#include enum class Encryption : uint8_t { NONE = 0x00, @@ -99,6 +99,7 @@ struct SerializerCallbacks { struct FragmentStore : public FragmentStoreI { entt::basic_registry _reg; + std::minstd_rand _rng{std::random_device{}()}; std::array _session_uuid_namespace; std::string _default_store_path; @@ -116,6 +117,9 @@ struct FragmentStore : public FragmentStoreI { // TODO: make the frags ref counted + std::vector generateNewUID(std::array& uuid_namespace); + std::vector generateNewUID(void); + // ========== new fragment ========== // memory backed owned diff --git a/src/fragment_store/test_fragstore.cpp b/src/fragment_store/test_fragstore.cpp index 898b3973..9c021650 100644 --- a/src/fragment_store/test_fragstore.cpp +++ b/src/fragment_store/test_fragstore.cpp @@ -36,7 +36,9 @@ int main(void) { const auto frag0 = fs.newFragmentFile("", MetaFileType::TEXT_JSON, {0xff, 0xf1, 0xf2, 0xf0, 0xff, 0xff, 0xff, 0xf9}); - const auto frag1 = fs.newFragmentFile("", MetaFileType::BINARY_MSGPACK, {0xff, 0xff, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xf9}); + const auto frag1 = fs.newFragmentFile("", MetaFileType::BINARY_MSGPACK); + + const auto frag2 = fs.newFragmentFile("", MetaFileType::BINARY_MSGPACK); { auto frag0h = fs.fragmentHandle(frag0); @@ -62,7 +64,6 @@ int main(void) { frag1h.emplace_or_replace(); frag1h.emplace_or_replace(); - //frag1h.emplace_or_replace(MetaFileType::BINARY_MSGPACK); std::function fn_cb = [read = 0ul](uint8_t* request_buffer, uint64_t buffer_size) mutable -> uint64_t { static constexpr std::string_view text = "This is some random data";