Skip to content

Commit

Permalink
feat: add uuidv7 functions (#1020)
Browse files Browse the repository at this point in the history
* added uuidv7

* fix format

* removed todo comments
  • Loading branch information
jaredshi0 authored Dec 26, 2024
1 parent 8d9cde4 commit ebfc441
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
39 changes: 36 additions & 3 deletions src/modules/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,41 @@ std::string uuidV6()
return ss.str();
}

std::string uuidV7()
{
RandomGenerator<std::mt19937> gen = RandomGenerator<std::mt19937>{};
auto now = std::chrono::system_clock::now();
auto since_epoch = now.time_since_epoch();

const auto timestamp =
static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(since_epoch).count());

const auto time_high = static_cast<uint32_t>((timestamp >> 16) & 0xFFFFFFFFULL);
const auto time_low = static_cast<uint32_t>(timestamp & 0xFFFFULL);

std::uniform_int_distribution<uint16_t> rand_a_seq_dis(0, 0xFFF);
uint16_t rand_a_seq = (gen(rand_a_seq_dis) & 0xFFF);
rand_a_seq |= 0x7000;

std::uniform_int_distribution<uint32_t> rand_b_seq_dis(0, 0xFFFFFFFFULL);
uint64_t rand_b_seq = static_cast<uint32_t>(gen(rand_b_seq_dis));
rand_b_seq = rand_b_seq << 32;
rand_b_seq |= static_cast<uint32_t>(gen(rand_b_seq_dis));

uint16_t rand_b_high = ((rand_b_seq >> 32) & 0x3FFFULL) | 0x8000;
uint64_t rand_b_low = (rand_b_seq & 0xFFFFFFFFFFFFULL);

std::ostringstream ss;
ss << std::hex << std::setfill('0');
ss << std::setw(8) << time_high << '-';
ss << std::setw(4) << time_low << '-';
ss << std::setw(4) << rand_a_seq << '-';
ss << std::setw(4) << rand_b_high << '-';
ss << std::setw(12) << rand_b_low;

return ss.str();
}

std::string uuid(Uuid uuid, const std::string& namespace_uuid, const std::string& name)
{
switch (uuid)
Expand All @@ -598,11 +633,9 @@ std::string uuid(Uuid uuid, const std::string& namespace_uuid, const std::string
// TODO: implement uuidV5
return uuidV5(namespace_uuid, name);
case Uuid::V6:
// TODO: implement uuidV6
return uuidV6();
case Uuid::V7:
// TODO: implement uuidV7
return uuidV4();
return uuidV7();
case Uuid::V8:
// TODO: implement uuidV8
return uuidV4();
Expand Down
35 changes: 22 additions & 13 deletions tests/modules/string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,40 +69,49 @@ TEST_F(StringTest, shouldGenerateUuid4Default)
ASSERT_EQ(generatedUuid[18], '-');
ASSERT_EQ(generatedUuid[23], '-');
}
TEST_F(StringTest, shouldGenerateUuid5) {

TEST_F(StringTest, shouldGenerateUuid5)
{
const std::string namespaceUuid = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
const std::string name = "example_name";
const auto generatedUuid = uuid(Uuid::V5,namespaceUuid, name);
const auto generatedUuid = uuid(Uuid::V5, namespaceUuid, name);

std::cerr << "Generated UUID: " << generatedUuid << std::endl;

// Ensure the UUID has the correct format
ASSERT_EQ(generatedUuid.size(), 36); // UUIDs must be 36 characters including hyphens
ASSERT_EQ(generatedUuid[8], '-'); // Hyphen at position 8
ASSERT_EQ(generatedUuid[13], '-'); // Hyphen at position 13
ASSERT_EQ(generatedUuid[18], '-'); // Hyphen at position 18
ASSERT_EQ(generatedUuid[23], '-'); // Hyphen at position 23


ASSERT_EQ(generatedUuid.size(), 36); // UUIDs must be 36 characters including hyphens
ASSERT_EQ(generatedUuid[8], '-'); // Hyphen at position 8
ASSERT_EQ(generatedUuid[13], '-'); // Hyphen at position 13
ASSERT_EQ(generatedUuid[18], '-'); // Hyphen at position 18
ASSERT_EQ(generatedUuid[23], '-'); // Hyphen at position 23
}

TEST_F(StringTest, shouldGenerateUuid6) {
TEST_F(StringTest, shouldGenerateUuid6)
{
const auto generatedUuid = uuid(Uuid::V6);
std::cerr << "Generated UUID: " << generatedUuid << std::endl;



// Ensure the UUID has the correct format
ASSERT_EQ(generatedUuid.size(), 36);
ASSERT_EQ(generatedUuid[8], '-');
ASSERT_EQ(generatedUuid[13], '-');
ASSERT_EQ(generatedUuid[18], '-');
ASSERT_EQ(generatedUuid[23], '-');
}

TEST_F(StringTest, shouldGenerateUuid7)
{
const auto generatedUuid = uuid(Uuid::V7);

// Ensure the UUID has the correct format
ASSERT_EQ(generatedUuid.size(), 36);
ASSERT_EQ(generatedUuid[8], '-');
ASSERT_EQ(generatedUuid[13], '-');
ASSERT_EQ(generatedUuid[18], '-');
ASSERT_EQ(generatedUuid[23], '-');
ASSERT_EQ(generatedUuid[14], '7');
}


TEST_F(StringTest, shouldGenerateUlidNoArguments)
{
const auto generatedUlidNoArg = ulid();
Expand Down

0 comments on commit ebfc441

Please sign in to comment.