ORE Studio 0.0.4
Loading...
Searching...
No Matches
Public Member Functions | List of all members
uuid_v7_generator Class Reference

A generator for UUID version 7 (v7) based on RFC 9562. More...

#include <uuid_v7_generator.hpp>

Collaboration diagram for uuid_v7_generator:
Collaboration graph

Public Member Functions

 uuid_v7_generator ()
 Constructs a new UUID v7 generator.
 
boost::uuids::uuid operator() ()
 Generates a new UUID v7.
 

Detailed Description

A generator for UUID version 7 (v7) based on RFC 9562.

This class creates UUIDs that are time-sortable and contain high-entropy random data. A UUID v7 is composed of a 48-bit Unix timestamp (in milliseconds), a 4-bit version field, and 74 bits of random data.

The combination of a timestamp and random data makes v7 UUIDs excellent for use as primary keys in distributed databases, as they are sortable by generation time and have a very low probability of collision.

This implementation is designed to be efficient, creating and seeding a random number engine only once upon construction.

Note
C++23 does not include a standard UUID library, and Boost.UUID does not provide a v7 generator. This class fills that gap.
See also
RFC 9562 - UUID Version 7

Constructor & Destructor Documentation

◆ uuid_v7_generator()

Constructs a new UUID v7 generator.

Initializes the internal Mersenne Twister random number engine with a high-entropy seed obtained from std::random_device. This ensures that the random components of the generated UUIDs are unpredictable.

Member Function Documentation

◆ operator()()

boost::uuids::uuid operator() ( )

Generates a new UUID v7.

This function performs the core generation logic:

  1. Retrieves the current time as a 48-bit Unix timestamp in milliseconds.
  2. Generates a base UUID filled with cryptographically strong random bytes.
  3. Overwrites the timestamp, version, and variant fields within the random base to conform to the v7 specification.

The resulting UUID is globally unique, time-sortable, and suitable for a wide range of applications.

Returns
A boost::uuids::uuid object conforming to the v7 specification.
Note
The 48-bit timestamp will overflow around the year 10889, which is generally not a concern for most systems.