### UUID/GUID Concepts and Performance Source: https://github.com/f4b6a3/uuid-creator/wiki/6.0.-References Resources discussing the performance and implementation of UUIDs and GUIDs. ```APIDOC ## GUID/UUID Performance Breakthrough ### Description Discusses performance aspects related to GUIDs and UUIDs. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ## How many ways are there to sort GUIDs? How much time do you have? ### Description Explores different methods and considerations for sorting GUIDs. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Create UUIDv7 using GUID API Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use GUID.v7() to generate a UUIDv7. ```java GUID guid = GUID.v7(); ``` -------------------------------- ### Create UUIDv3 using GUID API Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use GUID.v3() with a namespace and name to generate a UUIDv3. ```java GUID guid = GUID.v3(GUID.NAMESPACE_DNS, "www.example.com"); ``` -------------------------------- ### Create UUIDv5 using GUID API Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use GUID.v5() with a namespace and name to generate a UUIDv5. ```java GUID guid = GUID.v5(GUID.NAMESPACE_DNS, "www.example.com"); ``` -------------------------------- ### Environment Variable Examples (Bash) Source: https://github.com/f4b6a3/uuid-creator/wiki/1.1.-UUIDv1 Demonstrates various bash commands to set the UUIDCREATOR_NODE environment variable using system information like hostid, machine-id, hostname, and network interface details. ```bash # Append one of these examples to ~/.profile # Use `hostid` command export UUIDCREATOR_NODE=0x`hostid` # Use `machine-id` file export UUIDCREATOR_NODE=0x`cut -c-12 /etc/machine-id` # Use the MD5 hash of hostname export UUIDCREATOR_NODE=0x`hostname | md5sum | cut -c-12` # Use MAC returned by `ifconfig` export UUIDCREATOR_NODE=0x`ifconfig eth0 | egrep -o 'ether [0-9a-f:]+' | cut -c7- | tr -d ':'` # Use IP returned by `ifconfig` in hexadecimal export UUIDCREATOR_NODE=0x`ifconfig eth0 | egrep -o 'inet [0-9\]+' | cut -c6- | tr '.' ' ' | xargs printf '%02x'` # Use the SHA-256 hash of hostname, MAC and IP export UUIDCREATOR_NODE=0x`echo -n $(hostname) \ $(ifconfig eth0 | egrep -o 'ether [0-9a-f:]+' | cut -c7-) \ $(ifconfig eth0 | egrep -o 'inet [0-9\]+' | cut -c6-) \ | sha256sum | cut -c-12` ``` -------------------------------- ### Create UUIDv1 using GUID API Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use GUID.v1() to generate a UUIDv1. ```java GUID guid = GUID.v1(); ``` -------------------------------- ### Create UUIDv6 using GUID API Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use GUID.v6() to generate a UUIDv6. ```java GUID guid = GUID.v6(); ``` -------------------------------- ### Generate COMB GUIDs with UuidCreator Source: https://context7.com/f4b6a3/uuid-creator/llms.txt Generates COMB (Combined) GUIDs optimized for database indexing. Use prefix variants for general chronological sorting and suffix variants for MS SQL Server optimization. ```java import com.github.f4b6a3.uuid.UuidCreator; import java.time.Instant; import java.util.UUID; // Prefix COMB: timestamp at the beginning (sorts chronologically in most databases) UUID prefixComb = UuidCreator.getPrefixComb(); System.out.println(prefixComb); // e.g., 01720b5c-bf10-47cc-b631-4f262f500172 // Suffix COMB: timestamp at the end (optimized for MS SQL Server) UUID suffixComb = UuidCreator.getSuffixComb(); // Short Prefix COMB: 2-byte timestamp prefix, wraps every ~45 days UUID shortPrefix = UuidCreator.getShortPrefixComb(); // Short Suffix COMB: 2-byte timestamp suffix, wraps every ~45 days UUID shortSuffix = UuidCreator.getShortSuffixComb(); // Range queries with COMB GUIDs Instant queryTime = Instant.parse("2024-06-15T00:00:00Z"); UUID minComb = UuidCreator.getPrefixCombMin(queryTime); UUID maxComb = UuidCreator.getPrefixCombMax(queryTime); // SQL: SELECT * FROM table WHERE id BETWEEN minComb AND maxComb // Suffix COMB range queries (for MS SQL Server) UUID minSuffix = UuidCreator.getSuffixCombMin(queryTime); UUID maxSuffix = UuidCreator.getSuffixCombMax(queryTime); ``` -------------------------------- ### Create UUIDv2 using GUID API Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use GUID.v2() with a domain and identifier to generate a UUIDv2. ```java GUID guid = GUID.v2(GUID.LOCAL_DOMAIN_PERSON, 1234); ``` -------------------------------- ### Configure UUID Creator via JVM Arguments and Environment Variables Source: https://context7.com/f4b6a3/uuid-creator/llms.txt Control SecureRandom algorithms and node identifiers using JVM arguments or environment variables. Example shows setting the node identifier to use the MAC address. ```java // Configure via JVM arguments: // -Duuidcreator.securerandom="SHA1PRNG" // Use SHA1PRNG algorithm // -Duuidcreator.securerandom="DRBG" // Use DRBG algorithm (JDK9+) // -Duuidcreator.node="mac" // Use MAC address as node ID // -Duuidcreator.node="hash" // Use hash of hostname/MAC/IP // -Duuidcreator.node="random" // Use random node ID // -Duuidcreator.node="0xAABBCCDDEEFF" // Use specific node ID // Configure via environment variables: // export UUIDCREATOR_SECURERANDOM="SHA1PRNG" // export UUIDCREATOR_NODE="mac" // export UUIDCREATOR_NODE="0x$(hostid)" // export UUIDCREATOR_NODE="0x$(cut -c-12 /etc/machine-id)" import com.github.f4b6a3.uuid.UuidCreator; import java.util.UUID; // After setting the environment variable UUIDCREATOR_NODE="mac" // All time-based UUIDs will use the MAC address as node identifier UUID uuid = UuidCreator.getTimeBased(); ``` -------------------------------- ### Create UUIDv4 using GUID API Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use GUID.v4() to generate a UUIDv4. ```java GUID guid = GUID.v4(); ``` -------------------------------- ### Convert UUIDv4 to .Net Guid Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Uses DotNetGuid4Codec to convert a UUIDv4 to a .Net Guid format. ```java UUID uuid = //... UUID guid = DotNetGuid4Codec.INSTANCE.encode(uuid); ``` -------------------------------- ### Configure TimeBasedFactory with Custom Clock Sequence Functions Source: https://github.com/f4b6a3/uuid-creator/wiki/1.1.-UUIDv1 Examples of providing custom clock sequence logic to the TimeBasedFactory using either a custom class or a lambda expression. ```java // with clock sequence provided by a custom function MyClockSeqFunction function = new MyClockSeqFunction(); TimeBasedFactory factory = TimeBasedFactory.builder() .withClockSeqFunction(function) .build(); ``` ```java // with clock sequence provided by a lambda expression Random random = new Random(); TimeBasedFactory factory = TimeBasedFactory.builder() .withClockSeqFunction((timestamp) -> ClockSeqFunction.toExpectedRange(random.nextInt())) .build(); ``` -------------------------------- ### Convert UUIDv1 to .Net Guid Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Uses DotNetGuid1Codec to convert a UUIDv1 to a .Net Guid format. ```java UUID uuid = //... UUID guid = DotNetGuid1Codec.INSTANCE.encode(uuid); ``` -------------------------------- ### Configure TimeBasedFactory with Custom Time Functions Source: https://github.com/f4b6a3/uuid-creator/wiki/1.1.-UUIDv1 Examples of providing custom timestamp logic to the TimeBasedFactory using either a custom class or a lambda expression. ```java // with timestamp provided by a custom function MyTimeFunction function = new MyTimeFunction(); TimeBasedFactory factory = TimeBasedFactory.builder() .withTimeFunction(function) .build(); ``` ```java // with timestamp provided by a lambda expression TimeBasedFactory factory = TimeBasedFactory.builder() .withTimeFunction(() -> TimeFunction.toTimestamp(Instant.now())) .build(); ``` -------------------------------- ### Convert .Net Guid to UUIDv4 Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Uses DotNetGuid4Codec to convert a .Net Guid back to a UUIDv4. ```java UUID guid = //... UUID uuid = DotNetGuid4Codec.INSTANCE.encode(guid); ``` -------------------------------- ### Extract Creation Instant from Prefix COMB Source: https://github.com/f4b6a3/uuid-creator/wiki/3.0.-Library-utilities Get the creation timestamp as an Instant from a Prefix COMB (Combination of UUID and Timestamp). ```java UUID comb = /... Instant instant = CombUtil.extractPrefixInstant(comb); ``` -------------------------------- ### Convert .Net Guid to UUIDv1 Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Uses DotNetGuid1Codec to convert a .Net Guid back to a UUIDv1. ```java UUID guid = //... UUID uuid = DotNetGuid1Codec.INSTANCE.decode(guid); ``` -------------------------------- ### Get Machine Identifier String Source: https://github.com/f4b6a3/uuid-creator/wiki/3.0.-Library-utilities Generate a human-readable string identifying the current machine, including hostname, MAC address, and IP address. ```java // returns "hostname FF-FF-FF-FF-FF-FF 255.255.255.255" String string = MachineId.getMachineString(); ``` -------------------------------- ### COMB GUID Timestamp Extraction Source: https://context7.com/f4b6a3/uuid-creator/llms.txt Extract creation timestamps from COMB GUIDs (Prefix and Suffix) for querying and auditing purposes. Requires importing UuidCreator and CombUtil. ```java import com.github.f4b6a3.uuid.UuidCreator; import com.github.f4b6a3.uuid.util.CombUtil; import java.time.Instant; import java.util.UUID; // Create COMB GUIDs UUID prefixComb = UuidCreator.getPrefixComb(); UUID suffixComb = UuidCreator.getSuffixComb(); // Extract creation instant from Prefix COMB Instant prefixTime = CombUtil.extractPrefixInstant(prefixComb); System.out.println("Prefix COMB created at: " + prefixTime); // Extract creation instant from Suffix COMB Instant suffixTime = CombUtil.extractSuffixInstant(suffixComb); System.out.println("Suffix COMB created at: " + suffixTime); ``` -------------------------------- ### Extract Creation Instant from Suffix COMB Source: https://github.com/f4b6a3/uuid-creator/wiki/3.0.-Library-utilities Get the creation timestamp as an Instant from a Suffix COMB (Combination of UUID and Timestamp). ```java UUID comb = /... Instant instant = CombUtil.extractSuffixInstant(comb); ``` -------------------------------- ### DotNetGuid4Codec API Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Methods for converting between UUIDv4 and .Net Guid formats. ```APIDOC ## DotNetGuid4Codec.encode() ### Description Converts a UUIDv4 to a .Net Guid. ### Request Example UUID uuid = //... UUID guid = DotNetGuid4Codec.INSTANCE.encode(uuid); ## DotNetGuid4Codec.decode() ### Description Converts a .Net Guid to a UUIDv4. ### Request Example UUID guid = //... UUID uuid = DotNetGuid4Codec.INSTANCE.encode(guid); ``` -------------------------------- ### DotNetGuid1Codec API Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Methods for converting between UUIDv1 and .Net Guid formats. ```APIDOC ## DotNetGuid1Codec.encode() ### Description Converts a UUIDv1 to a .Net Guid. ### Request Example UUID uuid = //... UUID guid = DotNetGuid1Codec.INSTANCE.encode(uuid); ## DotNetGuid1Codec.decode() ### Description Converts a .Net Guid to a UUIDv1. ### Request Example UUID guid = //... UUID uuid = DotNetGuid1Codec.INSTANCE.decode(guid); ``` -------------------------------- ### UUID Structure and Definitions Source: https://github.com/f4b6a3/uuid-creator/wiki/6.0.-References Resources defining the structure and related concepts of UUIDs and GUIDs. ```APIDOC ## UUID structure (Microsoft) ### Description Details the structure and format of UUIDs as defined by Microsoft. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ## ThechDictionary - Universally Unique Identifier ### Description A definition and explanation of what a Universally Unique Identifier (UUID) is. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ## ThechDictionary - Globally Unique Identifier ### Description A definition and explanation of what a Globally Unique Identifier (GUID) is. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ## Namespace ### Description Information about namespaces, a concept relevant to certain types of UUIDs. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Generate JDK UUIDv7 using GUID API Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Convert a GUIDv7 to a standard JDK UUID. ```java UUID uuid = GUID.v7().toUUID(); ``` -------------------------------- ### Generate Time-Based UUID with Custom Node ID Source: https://github.com/f4b6a3/uuid-creator/wiki/1.1.-UUIDv1 This example shows how to create a time-based UUID factory with a custom node ID function, such as generating a random long. This hides the complexity of the generation method. ```java package com.example; import java.util.concurrent.ThreadLocalRandom; import com.github.f4b6a3.uuid.factory.rfc4122.TimeBasedFactory; public class UuidGenerator { private static final TimeBasedFactory FACTORY = TimeBasedFactory.builder() .withNodeIdFunction(() -> ThreadLocalRandom.current().nextLong()) .build(); public static UUID generate() { return FACTORY.create(); } ``` ```java UUID uuid = UuidGenerator.generate(); ``` -------------------------------- ### Create UUIDv4 with SecureRandom using GUID API Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Generate a UUIDv4 using a provided SecureRandom instance for cryptographic quality randomness. ```java GUID guid = GUID.v4(new SecureRandom()); ``` -------------------------------- ### Generate UUIDs with GUID Alternative API Source: https://context7.com/f4b6a3/uuid-creator/llms.txt A lightweight alternative to UuidCreator for generating various UUID versions. Supports custom SecureRandom instances and conversion to standard JDK UUIDs. ```java import com.github.f4b6a3.uuid.alt.GUID; import java.security.SecureRandom; import java.time.Instant; import java.util.UUID; // Generate UUIDs using the simple GUID API GUID v1 = GUID.v1(); // Time-based (Gregorian) GUID v4 = GUID.v4(); // Random-based GUID v6 = GUID.v6(); // Time-ordered (Gregorian) GUID v7 = GUID.v7(); // Time-ordered (Unix Epoch) // Name-based UUIDs GUID v3 = GUID.v3(GUID.NAMESPACE_DNS, "www.example.com"); // MD5 GUID v5 = GUID.v5(GUID.NAMESPACE_DNS, "www.example.com"); // SHA-1 // DCE Security UUID GUID v2 = GUID.v2(GUID.LOCAL_DOMAIN_PERSON, 1234); // With custom SecureRandom for cryptographic security SecureRandom secureRandom = new SecureRandom(); GUID secureV4 = GUID.v4(secureRandom); GUID secureV7 = GUID.v7(Instant.now(), secureRandom); // Convert GUID to JDK's UUID UUID uuid = GUID.v7().toUUID(); System.out.println(uuid); // Convert from JDK's UUID to GUID GUID guid = new GUID(UUID.randomUUID()); // Parse from string GUID parsed = new GUID("01234567-89ab-4def-a123-456789abcdef"); // Validate string boolean valid = GUID.valid("01234567-89ab-4def-a123-456789abcdef"); // true // Convert to bytes byte[] bytes = v7.toBytes(); // Special GUIDs GUID nil = GUID.NIL; // All zeros GUID max = GUID.MAX; // All ones ``` -------------------------------- ### Configure RandomBasedFactory with Random Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 Demonstrates configuring a RandomBasedFactory with a java.util.Random instance. ```java // a random-based factory with `Random` Random random = new Random(); RandomBasedFactory factory = new RandomBasedFactory(random); ``` ```java // use the custom factory UUID uuid = factory.create(); ``` -------------------------------- ### Configure SecureRandom via VM Arguments Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 Set the SecureRandom algorithm by passing system properties as VM arguments. ```bash # Append one of these examples to VM arguments # Use the the algorithm SHA1PRNG for SecureRandom -Duuidcreator.securerandom="SHA1PRNG" # Use the the algorithm DRBG for SecureRandom (JDK9+) -Duuidcreator.securerandom="DRBG" ``` -------------------------------- ### Sequence of UUID v7 Generation Source: https://github.com/f4b6a3/uuid-creator/wiki/1.7.-UUIDv7 Demonstrates the sequence of UUID v7 generation, showing how the time and monotonic random components change over time. ```text 0181be8a-e5ac-7cc0-a3e5-84ea795e8418 0181be8a-e5ac-7cc0-a3e5-84eaceb743e7 0181be8a-e5ac-7cc0-a3e5-84ebb3b9dbe7 0181be8a-e5ac-7cc0-a3e5-84eca0e14876 0181be8a-e5ac-7cc0-a3e5-84ed1fdc19c6 0181be8a-e5ac-7cc0-a3e5-84ee082176d8 0181be8a-e5ac-7cc0-a3e5-84ee1d1f63ae 0181be8a-e5ac-7cc0-a3e5-84eeafd13b2a 0181be8a-e5ad-790f-91a1-900fe2d661cc < millisecond changed 0181be8a-e5ad-790f-91a1-901051bb5f55 0181be8a-e5ad-790f-91a1-90110ca4ebf6 0181be8a-e5ad-790f-91a1-901119498fd6 0181be8a-e5ad-790f-91a1-9011903ff6aa 0181be8a-e5ad-790f-91a1-901215a742d8 0181be8a-e5ad-790f-91a1-9012407c8742 0181be8a-e5ad-790f-91a1-90127c104374 ^ look ^ look |-------------|----------------------| time monotonic random ``` -------------------------------- ### Add Gradle Dependency Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Include this implementation line in your build.gradle file to use the library in a Gradle project. ```text implementation 'com.github.f4b6a3:uuid-creator:6.1.0' ``` -------------------------------- ### Configure SecureRandom via Environment Variables Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 Set the SecureRandom algorithm by exporting environment variables in your shell configuration. ```bash # Append one of these examples to /etc/environment or ~/.profile # Use the the algorithm SHA1PRNG for SecureRandom export UUIDCREATOR_SECURERANDOM="SHA1PRNG" # Use the the algorithm DRBG for SecureRandom (JDK9+) export UUIDCREATOR_SECURERANDOM="DRBG" ``` -------------------------------- ### Create Custom Name-based Factory Source: https://github.com/f4b6a3/uuid-creator/wiki/1.5.-UUIDv5 Initializes a factory with a custom namespace to generate multiple name-based UUIDs efficiently. ```java // a random unique namespace for the application UUID namespace = UUID.fromString("b86919b5-d2b1-455e-8ef5-b752f3fa8416"); // a name-based factory that uses the unique namespace NameBasedSha1Factory factory = new NameBasedSha1Factory(namespace); // use the factory as many times as needed UUID uuid = factory.create("This is a test string"); ``` -------------------------------- ### Instantiate and Use LessBlockingFactory Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 Instantiate a LessBlockingFactory with a specified number of internal factories and then use it to create a UUID. This approach helps in scenarios where high throughput of UUID generation is required. ```java // instantiate a less-blocking factory with an array of 8 factories LessBlockingFactory factory = new LessBlockingFactory(8); // use the less-blocking factory UUID uuid = factory.create(); ``` -------------------------------- ### Configure RandomBasedFactory with SplittableRandom Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 Sets up a RandomBasedFactory to use SplittableRandom for generating random long values. ```java // use a random function that returns a long value SplittableRandom random = new SplittableRandom(); RandomBasedFactory factory = RandomBasedFactory(() -> random.nextLong()); ``` ```java // use the factory UUID uuid = factory.create(); ``` -------------------------------- ### Extract Node Identifier from UUID Source: https://github.com/f4b6a3/uuid-creator/wiki/3.0.-Library-utilities Get the node identifier, typically a MAC address, from a time-based UUID. ```java UUID uuid = /... long nodeid = UuidUtil.extractNodeIdentifier(uuid); ``` -------------------------------- ### Benchmark Results: Throughput with 4 threads on JDK 21 Source: https://github.com/f4b6a3/uuid-creator/wiki/5.0.-Benchmark This output displays the throughput benchmark results for various UUID generation methods on JDK 21. It measures operations per millisecond for different UUID versions and string conversion operations, highlighting performance differences compared to JDK 8. ```text ----------------------------------------------------------------------------- Benchmark Mode Cnt Score Error Units ----------------------------------------------------------------------------- Throughput.altGUIDFromString thrpt 5 23158.777 ± 1821.198 ops/ms Throughput.altGUIDToString thrpt 5 65180.461 ± 6017.142 ops/ms Throughput.altGUIDv1 thrpt 5 36232.100 ± 2389.162 ops/ms Throughput.altGUIDv3 thrpt 5 10586.596 ± 591.769 ops/ms Throughput.altGUIDv4 thrpt 5 179180.825 ± 10367.188 ops/ms Throughput.altGUIDv5 thrpt 5 5706.582 ± 639.636 ops/ms Throughput.altGUIDv6 thrpt 5 36542.028 ± 1225.564 ops/ms Throughput.altGUIDv7 thrpt 5 50620.045 ± 3424.101 ops/ms ----------------------------------------------------------------------------- Throughput.jdkUUIDFromString thrpt 5 74853.840 ± 5154.535 ops/ms Throughput.jdkUUIDToString thrpt 5 65793.002 ± 4664.671 ops/ms Throughput.jdkUUIDv3 thrpt 5 10953.302 ± 690.206 ops/ms Throughput.jdkUUIDv4 thrpt 5 1387.702 ± 88.053 ops/ms ----------------------------------------------------------------------------- Throughput.uuidCreatorFromString thrpt 5 36104.613 ± 2270.606 ops/ms Throughput.uuidCreatorToString thrpt 5 64112.702 ± 3592.804 ops/ms Throughput.uuidCreatorV1 thrpt 5 9952.752 ± 77.387 ops/ms Throughput.uuidCreatorV3 thrpt 5 10462.576 ± 1273.508 ops/ms Throughput.uuidCreatorV4 thrpt 5 1969.693 ± 84.694 ops/ms Throughput.uuidCreatorV5 thrpt 5 5790.544 ± 383.018 ops/ms Throughput.uuidCreatorV6 thrpt 5 9946.889 ± 24.111 ops/ms Throughput.uuidCreatorV7 thrpt 5 2937.646 ± 288.790 ops/ms ----------------------------------------------------------------------------- Total time: 00:06:49 ----------------------------------------------------------------------------- ``` -------------------------------- ### UUID v7 Factory with RandomGenerator (JDK 17+) Source: https://github.com/f4b6a3/uuid-creator/wiki/1.7.-UUIDv7 Sets up a UUID v7 factory using `RandomGenerator.getDefault()` available in JDK 17 and later. This leverages the modern random number generation API. ```java // use a random function that returns a long value RandomGenerator random = RandomGenerator.getDefault(); TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory(() -> random.nextLong()); ``` ```java // use the factory UUID uuid = factory.create(); ``` -------------------------------- ### Get Machine UUID Source: https://github.com/f4b6a3/uuid-creator/wiki/3.0.-Library-utilities Generate a UUID that uniquely identifies the current machine. This UUID is derived from a SHA-256 hash of machine data. ```java // returns 7bc3cfd7-844f-46ad-51a9-1aa22d3c427a UUID uuid = MachineId.getMachineUuid(); ``` -------------------------------- ### Benchmark Results: Throughput with 4 threads on JDK 8 Source: https://github.com/f4b6a3/uuid-creator/wiki/5.0.-Benchmark This output displays the throughput benchmark results for various UUID generation methods on JDK 8. It measures operations per millisecond for different UUID versions and string conversion operations. ```text ----------------------------------------------------------------------------- Benchmark Mode Cnt Score Error Units ----------------------------------------------------------------------------- Throughput.altGUIDFromString thrpt 5 23956.562 ± 1275.122 ops/ms Throughput.altGUIDToString thrpt 5 4856.311 ± 263.480 ops/ms Throughput.altGUIDv1 thrpt 5 37159.851 ± 2937.331 ops/ms Throughput.altGUIDv3 thrpt 5 3220.306 ± 130.254 ops/ms Throughput.altGUIDv4 thrpt 5 158275.481 ± 4692.207 ops/ms Throughput.altGUIDv5 thrpt 5 2595.631 ± 154.530 ops/ms Throughput.altGUIDv6 thrpt 5 38509.952 ± 1583.123 ops/ms Throughput.altGUIDv7 thrpt 5 45379.955 ± 2499.478 ops/ms ----------------------------------------------------------------------------- Throughput.jdkUUIDFromString thrpt 5 3195.253 ± 153.360 ops/ms Throughput.jdkUUIDToString thrpt 5 4993.072 ± 193.616 ops/ms Throughput.jdkUUIDv3 thrpt 5 3754.799 ± 93.805 ops/ms Throughput.jdkUUIDv4 thrpt 5 1211.930 ± 95.383 ops/ms ----------------------------------------------------------------------------- Throughput.uuidCreatorFromString thrpt 5 27386.782 ± 1976.176 ops/ms Throughput.uuidCreatorToString thrpt 5 33069.242 ± 1722.242 ops/ms Throughput.uuidCreatorV1 thrpt 5 9707.480 ± 157.684 ops/ms Throughput.uuidCreatorV3 thrpt 5 3770.318 ± 603.189 ops/ms Throughput.uuidCreatorV4 thrpt 5 1517.354 ± 218.455 ops/ms Throughput.uuidCreatorV5 thrpt 5 2950.117 ± 145.772 ops/ms Throughput.uuidCreatorV6 thrpt 5 9668.325 ± 367.878 ops/ms Throughput.uuidCreatorV7 thrpt 5 2173.835 ± 83.803 ops/ms ----------------------------------------------------------------------------- Total time: 00:06:50 ----------------------------------------------------------------------------- ``` -------------------------------- ### Get Machine ID Source: https://github.com/f4b6a3/uuid-creator/wiki/3.0.-Library-utilities Calculate a long identifier for the current machine. The ID is a truncated SHA-256 hash of collected machine data. ```java // returns 0x7bc3cfd7844f46ad long id = MachineId.getMachineId(); ``` -------------------------------- ### Encode UUID to NCName String Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Converts a UUID into an NCName string, which conforms to XML NCName identifier constraints. The resulting string does not start with a digit. ```java UUID uuid = UUID.fromString("01234567-89ab-4def-a123-456789abcdef"); String slug = NcnameCodec.INSTANCE.encode(uuid); ``` -------------------------------- ### Configure RandomBasedFactory with ThreadLocalRandom Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 Creates a RandomBasedFactory that uses ThreadLocalRandom to generate byte arrays for UUID creation. ```java // a random-based factory with a custom `RandomFunction` RandomBasedFactory factory = new RandomBasedFactory((int length) -> { final byte[] bytes = new byte[length]; ThreadLocalRandom.current().nextBytes(bytes); return bytes; }); ``` ```java // use the custom factory UUID uuid = factory.create(); ``` -------------------------------- ### Create UUIDv3 using UuidCreator Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use UuidCreator.getNameBasedMd5() with a namespace and name to generate a UUIDv3. ```java UUID uuid = UuidCreator.getNameBasedMd5(UuidNamespace.NAMESPACE_URL, "https://github.com/"); ``` -------------------------------- ### UUID Creation Functions (Microsoft) Source: https://github.com/f4b6a3/uuid-creator/wiki/6.0.-References Documentation for Microsoft's functions to create UUIDs. ```APIDOC ## UuidCreate Function ### Description Creates a new UUID (Universally Unique Identifier). ### Method N/A (Function Call) ### Endpoint N/A ### Parameters None ### Request Example N/A ### Response N/A ## UuidCreateSequential Function ### Description Creates a new sequential UUID (Universally Unique Identifier). ### Method N/A (Function Call) ### Endpoint N/A ### Parameters None ### Request Example N/A ### Response N/A ``` -------------------------------- ### Encode UUID to Slug String Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Converts a UUID into a Slug string, which is safe for URLs and file names. This process rearranges UUID bits to ensure the string does not start with a digit. ```java UUID uuid = UUID.fromString("01234567-89ab-4def-a123-456789abcdef"); String slug = SlugCodec.INSTANCE.encode(uuid); ``` -------------------------------- ### Create NanoID-like String with Base64UrlCodec Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 Generates a NanoID-like 22-character string using UuidCreator and Base64UrlCodec. ```java package com.example; import com.github.f4b6a3.uuid.UuidCreator; import com.github.f4b6a3.uuid.codec.base.Base64UrlCodec; public class NanoID { // NanoID and base-64-url alphabets have the same 64 characters private static final Base64UrlCodec ENCODER = Base64UrlCodec.INSTANCE; public static String next() { // return a random 22-character string return ENCODER.encode(UuidCreator.getRandomBased()); } } ``` ```java String id = NanoID.next(); ``` -------------------------------- ### Configure RandomBasedFactory with RandomGenerator (JDK 17+) Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 Configures a RandomBasedFactory using the default RandomGenerator available in JDK 17 and later. ```java // use a random function that returns a long value RandomGenerator random = RandomGenerator.getDefault(); RandomBasedFactory factory = RandomBasedFactory(() -> random.nextLong()); ``` ```java // use the factory UUID uuid = factory.create(); ``` -------------------------------- ### Create UUIDv5 using UuidCreator Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use UuidCreator.getNameBasedSha1() with a namespace and name to generate a UUIDv5. ```java UUID uuid = UuidCreator.getNameBasedSha1(UuidNamespace.NAMESPACE_URL, "https://github.com/"); ``` -------------------------------- ### Configure RandomBasedFactory with SecureRandom (DRBG) Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 Configures a RandomBasedFactory with SecureRandom using the DRBG algorithm (JDK 9+). ```java // a random-based factory with `SecureRandom` using DRBG algorithm SecureRandom random = SecureRandom.getInstance("DRBG"); RandomBasedFactory factory = new RandomBasedFactory(random); ``` ```java // use the custom factory UUID uuid = factory.create(); ``` -------------------------------- ### Generate Time-ordered UUID with Hashed System Info Node ID Source: https://github.com/f4b6a3/uuid-creator/wiki/1.6.-UUIDv6 Creates a UUIDv6 using a hash of system information, including hostname and MAC address, as the node identifier. This provides a more stable identifier than a purely random one. ```java UUID uuid = UuidCreator.getTimeOrderedWithHash(); ``` -------------------------------- ### Create UUIDv2 using UuidCreator Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use UuidCreator.getDceSecurity() with a domain and identifier to generate a UUIDv2. ```java UUID uuid = UuidCreator.getDceSecurity(UuidLocalDomain.LOCAL_DOMAIN_PERSON, 1234); ``` -------------------------------- ### Create Key Generator with Custom RandomBasedFactory Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 A key generator that encapsulates a custom RandomBasedFactory, hiding generation complexity. ```java package com.example; import java.util.Random; import com.github.f4b6a3.uuid.factory.rfc4122.RandomBasedFactory; public class KeyGenerator { private static final RandomBasedFactory FACTORY = new RandomBasedFactory(new Random()); public static String next() { return FACTORY.create().toString(); } } ``` ```java String key = KeyGenerator.next(); ``` -------------------------------- ### Add Maven Dependency Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Include this dependency in your pom.xml file to use the library in a Maven project. ```xml com.github.f4b6a3 uuid-creator 6.1.1 ``` -------------------------------- ### Create Name-based MD5 UUID with Predefined Namespace Source: https://github.com/f4b6a3/uuid-creator/wiki/1.3.-UUIDv3 Generates a name-based UUID using the MD5 hashing algorithm and a predefined namespace for URLs. Ensure the UuidCreator library is included in your project. ```java String name = "https://github.com/"; UUID uuid = UuidCreator.getNameBasedMd5(UuidNamespace.NAMESPACE_URL, name); ``` -------------------------------- ### UUID v7 Structure with Random Increments Source: https://github.com/f4b6a3/uuid-creator/wiki/1.7.-UUIDv7 Illustrates the structure of a UUID v7 with random increments, highlighting the time and monotonic random components. ```text 01720b5c-bf10-77cc-b631-4f262f500172 ^^^^ ^^^^ ^^^^^^^^^^^^ |-------------|----------------------| time monotonic random * The monotonic random component is randomized when the time changes. * The monotonic random component incremented by a positive integer `n` when the time repeats. * The positive integer `n` maximum value can be specified via factory builder. * The positive integer `n` is always randomized. ``` -------------------------------- ### Java UUID Generation Source: https://github.com/f4b6a3/uuid-creator/wiki/6.0.-References Information on UUID generation in Java, including performance considerations. ```APIDOC ## Java UUID generation – Performance impact ### Description Analyzes the performance implications of generating UUIDs within Java applications. ### Method N/A (API Usage) ### Endpoint N/A ### Parameters N/A ### Request Example ```java import java.util.UUID; // Generate a random UUID (version 4) UUID randomUUID = UUID.randomUUID(); System.out.println(randomUUID); ``` ### Response N/A ``` -------------------------------- ### Create UUIDv7 using UuidCreator Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use UuidCreator.getTimeOrderedEpoch() to generate a UUIDv7. ```java UUID uuid = UuidCreator.getTimeOrderedEpoch(); ``` -------------------------------- ### Generate Time-Ordered UUIDs (v7) Source: https://context7.com/f4b6a3/uuid-creator/llms.txt Generate Unix Epoch-based UUIDs suitable for database primary keys, offering natural chronological ordering and index performance. ```java import com.github.f4b6a3.uuid.UuidCreator; import java.time.Instant; import java.util.UUID; // Generate UUIDv7 (default - with counter and random bits) UUID uuid = UuidCreator.getTimeOrderedEpoch(); System.out.println(uuid); // e.g., 0181be8a-e592-73d4-84fe-aaca0740fa1d // Fast variant using ThreadLocalRandom (non-cryptographic) UUID fastUuid = UuidCreator.getTimeOrderedEpochFast(); // Plus-1 variant: monotonic random incremented by 1 (20x faster than UUID.randomUUID) UUID plus1Uuid = UuidCreator.getTimeOrderedEpochPlus1(); // Plus-N variant: monotonic random incremented by random number UUID plusNUuid = UuidCreator.getTimeOrderedEpochPlusN(); // Generate UUIDv7 with specific instant UUID customUuid = UuidCreator.getTimeOrderedEpoch(Instant.now().plusSeconds(3600)); // Range queries: get min/max UUIDv7 for a timestamp Instant queryTime = Instant.parse("2024-06-15T12:00:00Z"); UUID minUuid = UuidCreator.getTimeOrderedEpochMin(queryTime); UUID maxUuid = UuidCreator.getTimeOrderedEpochMax(queryTime); // Use in SQL: WHERE uuid BETWEEN minUuid AND maxUuid ``` -------------------------------- ### NPM UUID Package Source: https://github.com/f4b6a3/uuid-creator/wiki/6.0.-References Documentation for the popular 'uuid' package available on NPM. ```APIDOC ## NPM UUID Package ### Description Information about the 'uuid' package for Node.js and browser environments, used for generating UUIDs. ### Method N/A (Package Usage) ### Endpoint N/A ### Parameters N/A ### Request Example ```javascript import { v4 as uuidv4 } from 'uuid'; uuidv4(); // '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed' ``` ### Response N/A ``` -------------------------------- ### Extract Creation Instant from UUID Source: https://github.com/f4b6a3/uuid-creator/wiki/3.0.-Library-utilities Retrieve the creation timestamp as an Instant object from a time-based UUID. ```java UUID uuid = /... Instant instant = UuidUtil.extractInstant(uuid); ``` -------------------------------- ### Check UUID Types and Extract Information with UuidUtil Source: https://context7.com/f4b6a3/uuid-creator/llms.txt Use UuidUtil to determine UUID types (nil, time-based, random-based), extract creation timestamps as Instant, and retrieve the node identifier from time-based UUIDs. You can also change the version of a UUID. ```java import com.github.f4b6a3.uuid.UuidCreator; import com.github.f4b6a3.uuid.util.UuidUtil; import java.time.Instant; import java.util.UUID; // Check UUID types UUID v1 = UuidCreator.getTimeBased(); UUID v4 = UuidCreator.getRandomBased(); UUID v7 = UuidCreator.getTimeOrderedEpoch(); boolean isNil = UuidUtil.isNil(UuidCreator.getNil()); // true boolean isTimeBased = UuidUtil.isTimeBased(v1); // true (UUIDv1) boolean isRandomBased = UuidUtil.isRandomBased(v4); // true (UUIDv4) // Extract timestamp from time-based UUIDs (v1, v6, v7) Instant instant = UuidUtil.extractInstant(v1); System.out.println(instant); // Creation time of the UUID // Extract node identifier from UUIDv1 or UUIDv6 long nodeId = UuidUtil.extractNodeIdentifier(v1); System.out.printf("Node ID: %012X%n", nodeId); // Change UUID version (for testing/conversion) UUID modified = UuidUtil.setVersion(v1, 4); System.out.println(modified.version()); // 4 ``` -------------------------------- ### Base64 Codec Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Converts between UUIDs and standard Base64 strings. ```APIDOC ## Base64 Codec ### Description Converts a base-64 string to a UUID. ### Method `decode(String)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```java String string = "ASNFZ4mrTe+hI0VniavN7w"; UUID uuid = Base64Codec.INSTANCE.decode(string); ``` ### Response #### Success Response (200) - **uuid** (UUID) - The decoded UUID object. #### Response Example ```json { "uuid": "01234567-89ab-4def-a123-456789abcdef" } ``` ``` -------------------------------- ### Create Prefix COMB UUID Source: https://github.com/f4b6a3/uuid-creator/wiki/2.0.-COMB Use this method to generate a Prefix COMB UUID, which has the timestamp at the beginning of the UUID for sortability. ```java UUID uuid = UuidCreator.getPrefixComb(); ``` -------------------------------- ### Create UUIDv1 using UuidCreator Source: https://github.com/f4b6a3/uuid-creator/blob/master/README.md Use UuidCreator.getTimeBased() to generate a UUIDv1. ```java UUID uuid = UuidCreator.getTimeBased(); ``` -------------------------------- ### Storing UUID Values Source: https://github.com/f4b6a3/uuid-creator/wiki/6.0.-References Information on storing UUID values in databases like MySQL and SQL Server. ```APIDOC ## Storing UUID Values in MySQL Tables ### Description Provides guidance on how to store UUID values effectively in MySQL database tables. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ## Uniqueidentifier and Clustered Indexes ### Description Discusses the use of `uniqueidentifier` data type and its interaction with clustered indexes, likely in SQL Server. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ## How are GUIDs compared in SQL Server 2005? ### Description Details the comparison mechanisms for GUIDs within SQL Server 2005. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Base64Codec Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Converts a UUID to and from a base-64 string. ```APIDOC ## Base64Codec ### Description Converts a UUID to and from a base-64 string. ### encode() Converts a UUID to a base-64 string. ```java UUID uuid = UUID.fromString("01234567-89ab-4def-a123-456789abcdef"); String string = Base64Codec.INSTANCE.encode(uuid); ``` ``` -------------------------------- ### Set Node Identifier via System Property (Bash) Source: https://github.com/f4b6a3/uuid-creator/wiki/1.1.-UUIDv1 Configure the node identifier using a system property in bash. This method takes precedence over environment variables. ```bash # Append one of these examples to VM arguments # Use a MAC address -Duuidcreator.node="mac" # Use a hash of hostname, MAC and IP -Duuidcreator.node="hash" # Use a random number that always changes -Duuidcreator.node="random" # Use a specific number -Duuidcreator.node="0xC0DA0615BB23" ``` -------------------------------- ### UUID Proposal for ECMAScript Source: https://github.com/f4b6a3/uuid-creator/wiki/6.0.-References Information regarding the proposal for UUID support in ECMAScript (JavaScript). ```APIDOC ## UUID proposal for ECMAScript ### Description Details the proposal to standardize UUID generation and handling within ECMAScript. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ## EcmaScript UUID Proposal - Issue 15 ### Description Specific discussion related to the ECMAScript UUID proposal, focusing on issue 15. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Create Key Generator with Random UUID Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 A simple key generator that produces random UUIDs using UuidCreator.getRandomBased(). ```java package com.example; import com.github.f4b6a3.uuid.UuidCreator; public class KeyGenerator { public static String next() { return UuidCreator.getRandomBased().toString(); } } ``` ```java String key = KeyGenerator.next(); ``` -------------------------------- ### Create Quick Random-Based UUID Source: https://github.com/f4b6a3/uuid-creator/wiki/1.4.-UUIDv4 Generates a fast random-based UUID using SplittableRandom as the generator. ```java // uses SplittableRandom as random generator UUID uuid = UuidCreator.getRandomBasedFast(); ``` -------------------------------- ### Set Node Identifier via Environment Variable (Bash) Source: https://github.com/f4b6a3/uuid-creator/wiki/1.1.-UUIDv1 Configure the node identifier using an environment variable in bash. This is a common method for system-wide configuration. ```bash # Append one of these examples to /etc/environment or ~/.profile # Use a MAC address export UUIDCREATOR_NODE="mac" # Use a hash of hostname, MAC and IP export UUIDCREATOR_NODE="hash" # Use a random number that always changes export UUIDCREATOR_NODE="random" # Use a specific number export UUIDCREATOR_NODE="0xC0DA0615BB23" ``` -------------------------------- ### Base32Codec Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Converts a UUID to and from a base-32 string. ```APIDOC ## Base32Codec ### Description Converts a UUID to and from a base-32 string. ### encode() Converts a UUID to a base-32 string. ```java UUID uuid = UUID.fromString("01234567-89ab-4def-a123-456789abcdef"); String string = Base32Codec.INSTANCE.encode(uuid); ``` ### decode() Converts a base-32 string to a UUID. ```java String string = "aerukz4jvng67ijdivtytk6n54"; UUID uuid = Base32Codec.INSTANCE.decode(string); ``` ``` -------------------------------- ### Base64Url Codec Source: https://github.com/f4b6a3/uuid-creator/wiki/4.0.-Library-codecs Converts between UUIDs and Base64 URL-safe strings. ```APIDOC ## Base64Url Codec ### Description Converts a UUID to and from a base-64 URL string. #### encode() Converts a UUID to a base-64 URL string. ### Method `encode(UUID)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```java UUID uuid = UUID.fromString("01234567-89ab-4def-a123-456789abcdef"); String string = Base64UrlCodec.INSTANCE.encode(uuid); ``` ### Response #### Success Response (200) - **string** (String) - The encoded Base64 URL string. #### Response Example ```json { "string": "ASNFZ4mrTe-hI0VniavN7w" } ``` #### decode() Converts a base-64 URL string to a UUID. ### Method `decode(String)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```java String string = "ASNFZ4mrTe-hI0VniavN7w"; UUID uuid = Base64UrlCodec.INSTANCE.decode(string); ``` ### Response #### Success Response (200) - **uuid** (UUID) - The decoded UUID object. #### Response Example ```json { "uuid": "01234567-89ab-4def-a123-456789abcdef" } ``` ```