diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea5ef87..c26a40f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,6 +4,9 @@ add_library(fragment_store ./fragment_store/file2_zstd.hpp ./fragment_store/file2_zstd.cpp + ./fragment_store/uuid_generator.hpp + ./fragment_store/uuid_generator.cpp + ./fragment_store/fragment_store_i.hpp ./fragment_store/fragment_store_i.cpp ./fragment_store/types.hpp diff --git a/src/fragment_store/fragment_store.cpp b/src/fragment_store/fragment_store.cpp index 17830d8..450a016 100644 --- a/src/fragment_store/fragment_store.cpp +++ b/src/fragment_store/fragment_store.cpp @@ -32,75 +32,17 @@ static const char* metaFileTypeSuffix(MetaFileType mft) { } 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+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+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 -) : _session_uuid_namespace(std::move(session_uuid_namespace)) { +) : _session_uuid_gen(std::move(session_uuid_namespace)) { registerSerializers(); } -std::vector FragmentStore::generateNewUID(std::array& uuid_namespace) { - std::vector new_uid(uuid_namespace.cbegin(), uuid_namespace.cend()); - 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+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; -} - std::vector FragmentStore::generateNewUID(void) { - return generateNewUID(_session_uuid_namespace); + return _session_uuid_gen(); } FragmentID FragmentStore::newFragmentMemoryOwned( diff --git a/src/fragment_store/fragment_store.hpp b/src/fragment_store/fragment_store.hpp index 0e52e51..26c5204 100644 --- a/src/fragment_store/fragment_store.hpp +++ b/src/fragment_store/fragment_store.hpp @@ -7,6 +7,8 @@ #include "./serializer.hpp" +#include "./uuid_generator.hpp" + #include #include @@ -18,8 +20,7 @@ #include struct FragmentStore : public FragmentStoreI { - std::minstd_rand _rng{std::random_device{}()}; - std::array _session_uuid_namespace; + UUIDGenerator_128_128 _session_uuid_gen; std::string _default_store_path; @@ -34,7 +35,6 @@ struct FragmentStore : public FragmentStoreI { // TODO: make the frags ref counted // TODO: check for exising - std::vector generateNewUID(std::array& uuid_namespace); std::vector generateNewUID(void); // ========== new fragment ========== diff --git a/src/fragment_store/uuid_generator.cpp b/src/fragment_store/uuid_generator.cpp new file mode 100644 index 0000000..cc07985 --- /dev/null +++ b/src/fragment_store/uuid_generator.cpp @@ -0,0 +1,68 @@ +#include "./uuid_generator.hpp" + +UUIDGenerator_128_128::UUIDGenerator_128_128(void) { + { // random namespace + const auto num0 = _rng(); + const auto num1 = _rng(); + const auto num2 = _rng(); + const auto num3 = _rng(); + + _uuid_namespace[0+0] = (num0 >> 0) & 0xff; + _uuid_namespace[0+1] = (num0 >> 8) & 0xff; + _uuid_namespace[0+2] = (num0 >> 16) & 0xff; + _uuid_namespace[0+3] = (num0 >> 24) & 0xff; + + _uuid_namespace[4+0] = (num1 >> 0) & 0xff; + _uuid_namespace[4+1] = (num1 >> 8) & 0xff; + _uuid_namespace[4+2] = (num1 >> 16) & 0xff; + _uuid_namespace[4+3] = (num1 >> 24) & 0xff; + + _uuid_namespace[8+0] = (num2 >> 0) & 0xff; + _uuid_namespace[8+1] = (num2 >> 8) & 0xff; + _uuid_namespace[8+2] = (num2 >> 16) & 0xff; + _uuid_namespace[8+3] = (num2 >> 24) & 0xff; + + _uuid_namespace[12+0] = (num3 >> 0) & 0xff; + _uuid_namespace[12+1] = (num3 >> 8) & 0xff; + _uuid_namespace[12+2] = (num3 >> 16) & 0xff; + _uuid_namespace[12+3] = (num3 >> 24) & 0xff; + } +} + +UUIDGenerator_128_128::UUIDGenerator_128_128(const std::array& uuid_namespace) : + _uuid_namespace(uuid_namespace) +{ +} + +std::vector UUIDGenerator_128_128::operator()(void) { + std::vector new_uid(_uuid_namespace.cbegin(), _uuid_namespace.cend()); + 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+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; +} + diff --git a/src/fragment_store/uuid_generator.hpp b/src/fragment_store/uuid_generator.hpp new file mode 100644 index 0000000..755e3be --- /dev/null +++ b/src/fragment_store/uuid_generator.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include +#include + +struct UUIDGeneratorI { + virtual std::vector operator()(void) = 0; +}; + +struct UUIDGenerator_128_128 final : public UUIDGeneratorI { + private: + std::array _uuid_namespace; + std::minstd_rand _rng{std::random_device{}()}; + + public: + UUIDGenerator_128_128(void); // default randomly initializes namespace + UUIDGenerator_128_128(const std::array& uuid_namespace); + + std::vector operator()(void) override; +}; +