random ids

This commit is contained in:
Green Sky 2024-02-13 01:19:12 +01:00
parent 267f8dffc1
commit 98ab974515
No known key found for this signature in database
3 changed files with 53 additions and 3 deletions

View File

@ -27,6 +27,21 @@ static const char* metaFileTypeSuffix(MetaFileType mft) {
} }
FragmentStore::FragmentStore(void) { 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(); registerSerializers();
} }
@ -40,6 +55,30 @@ entt::basic_handle<entt::basic_registry<FragmentID>> FragmentStore::fragmentHand
return {_reg, fid}; return {_reg, fid};
} }
std::vector<uint8_t> FragmentStore::generateNewUID(std::array<uint8_t, 8>& uuid_namespace) {
std::vector<uint8_t> 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<uint8_t> FragmentStore::generateNewUID(void) {
return generateNewUID(_session_uuid_namespace);
}
FragmentID FragmentStore::newFragmentMemoryOwned( FragmentID FragmentStore::newFragmentMemoryOwned(
const std::vector<uint8_t>& id, const std::vector<uint8_t>& id,
size_t initial_size size_t initial_size
@ -126,6 +165,12 @@ FragmentID FragmentStore::newFragmentFile(
return new_frag; return new_frag;
} }
FragmentID FragmentStore::newFragmentFile(
std::string_view store_path,
MetaFileType mft
) {
return newFragmentFile(store_path, mft, generateNewUID());
}
FragmentID FragmentStore::getFragmentByID( FragmentID FragmentStore::getFragmentByID(
const std::vector<uint8_t>& id const std::vector<uint8_t>& id

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "./fragment_store_i.hpp" #include "./fragment_store_i.hpp"
#include "entt/entity/fwd.hpp"
#include <entt/core/fwd.hpp> #include <entt/core/fwd.hpp>
#include <entt/core/type_info.hpp> #include <entt/core/type_info.hpp>
@ -14,6 +13,7 @@
#include <vector> #include <vector>
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <random>
enum class Encryption : uint8_t { enum class Encryption : uint8_t {
NONE = 0x00, NONE = 0x00,
@ -99,6 +99,7 @@ struct SerializerCallbacks {
struct FragmentStore : public FragmentStoreI { struct FragmentStore : public FragmentStoreI {
entt::basic_registry<FragmentID> _reg; entt::basic_registry<FragmentID> _reg;
std::minstd_rand _rng{std::random_device{}()};
std::array<uint8_t, 8> _session_uuid_namespace; std::array<uint8_t, 8> _session_uuid_namespace;
std::string _default_store_path; std::string _default_store_path;
@ -116,6 +117,9 @@ struct FragmentStore : public FragmentStoreI {
// TODO: make the frags ref counted // TODO: make the frags ref counted
std::vector<uint8_t> generateNewUID(std::array<uint8_t, 8>& uuid_namespace);
std::vector<uint8_t> generateNewUID(void);
// ========== new fragment ========== // ========== new fragment ==========
// memory backed owned // memory backed owned

View File

@ -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 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); auto frag0h = fs.fragmentHandle(frag0);
@ -62,7 +64,6 @@ int main(void) {
frag1h.emplace_or_replace<Components::DataCompressionType>(); frag1h.emplace_or_replace<Components::DataCompressionType>();
frag1h.emplace_or_replace<Components::DataEncryptionType>(); frag1h.emplace_or_replace<Components::DataEncryptionType>();
//frag1h.emplace_or_replace<Components::Ephemeral::MetaFileType>(MetaFileType::BINARY_MSGPACK);
std::function<FragmentStore::write_to_storage_fetch_data_cb> fn_cb = [read = 0ul](uint8_t* request_buffer, uint64_t buffer_size) mutable -> uint64_t { std::function<FragmentStore::write_to_storage_fetch_data_cb> 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"; static constexpr std::string_view text = "This is some random data";