### Running TSID Benchmark Script - Bash Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/benchmark/README.md This snippet provides the shell command required to initiate the benchmark tests for the hypersistence-tsid project. It assumes the user is in the project's root directory and that the `run.sh` script exists and is executable within the `./benchmark` subdirectory. Executing this command will start the performance evaluation process defined by the script. ```Bash ./benchmark/run.sh ``` -------------------------------- ### Configuring TSID Factory with Custom Epoch in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Configures the `TSID.Factory` to use a specific `Instant` as its starting point (epoch) for time calculation. This allows generating TSIDs whose timestamps are relative to a custom historical or future event. ```java // use a CUSTOM epoch that starts from the fall of the Berlin Wall Instant customEpoch = Instant.parse("1989-11-09T00:00:00Z"); TSID.Factory factory = TSID.Factory.builder().withCustomEpoch(customEpoch).build(); // use the factory TSID tsid = factory.generate(); ``` -------------------------------- ### Configuring TSID Factory for Discord Snowflake Style in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Configures a `TSID.Factory` to emulate the structure and epoch of Discord Snowflakes. It calculates the node ID from worker and process IDs and sets a specific custom epoch corresponding to Discord's start time. ```java // Discord Snowflakes have 5 bits for worker ID and 5 bits for process ID int worker = 1; // max: 2^5-1 = 31 int process = 1; // max: 2^5-1 = 31 int node = (worker << 5 | process); // max: 2^10-1 = 1023 // Discord Epoch starts in the first millisecond of 2015 Instant customEpoch = Instant.parse("2015-01-01T00:00:00.000Z"); // a factory that returns TSIDs similar to Discord Snowflakes TSID.Factory factory = TSID.Factory.builder() .withCustomEpoch(customEpoch) .withNode(node) .build(); // use the factory TSID tsid = factory.generate(); ``` -------------------------------- ### Getting TSID Creation Instant in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Extracts and returns the Instant object representing the precise timestamp when the TSID was generated. TSIDs are time-sorted, embedding the creation time within their value. ```java Instant instant = tsid.getInstant(); // 2020-04-15T22:31:02.458Z ``` -------------------------------- ### Using Custom TSID Key Generator in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Demonstrates how to use the static `next()` method of the custom `KeyGenerator` class to obtain a new TSID string. This shows a simple pattern for abstracting TSID generation. ```java String key = KeyGenerator.next(); ``` -------------------------------- ### Creating a Quick TSID in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Generates a new TSID instance using the default, fast generation method provided by the TSID library. This is the simplest way to create a standard TSID. ```java TSID tsid = TSID.fast(); ``` -------------------------------- ### Defining Custom TSID Key Generator Class in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Defines a simple public class named `KeyGenerator` within the `com.example` package. It provides a static method `next()` that internally uses the default `TSID.Factory` to generate a new TSID and returns its canonical string representation. ```java package com.example; public class KeyGenerator { public static String next() { return TSID.Factory.TSID.Factory.getTsid().toString(); } } ``` -------------------------------- ### Configuring TSID Factory with Node Bits and Node in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Uses the `TSID.Factory.builder()` to configure the factory with a specific number of bits allocated for the node ID and a corresponding node value within that bit range. This allows fine-grained control over TSID structure. ```java // setup a factory for up to 64 nodes and 65536 ID/ms. TSID.Factory factory = TSID.Factory.builder() .withNodeBits(6) // max: 20 .withNode(63) // max: 2^nodeBits .build(); // use the factory TSID tsid = factory.generate(); ``` -------------------------------- ### Configuring TSID Factory with java.util.Random in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Configures the `TSID.Factory` to use a provided instance of `java.util.Random` for generating the random component of the TSID. This can be useful for specific seeding requirements or performance tuning. ```java // use a `java.util.Random` instance for fast generation TSID.Factory factory = TSID.Factory.builder().withRandom(new Random()).build(); // use the factory TSID tsid = factory.generate(); ``` -------------------------------- ### Adding Maven Dependency for Hypersistence TSID Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md This XML snippet provides the necessary configuration for adding the hypersistence-tsid library as a dependency in a Maven project's pom.xml file. Include this within the section of your project file to use the library's TSID generation capabilities. Requires a Maven build environment. ```xml io.hypersistence hypersistence-tsid 2.1.4 ``` -------------------------------- ### Formatting TSID with Prefix in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Formats the TSID object into a string according to a specified pattern. The pattern "%S" is a placeholder for the canonical TSID string, allowing custom prefixes or suffixes. ```java String string = tsid.format("K%S"); // K0AWE5HZP3SKTK ``` -------------------------------- ### Generating a TSID as String in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md This snippet illustrates how to generate a new TSID and convert it into its 13-character base32 string representation. This format is URL-safe, case-insensitive, and suitable for environments where 64-bit integers are not standard or convenient, such as JavaScript. Requires the io.hypersistence.tsid library dependency. ```java String string = TSID.Factory.getTsid().toString(); ``` -------------------------------- ### Configuring TSID Factory for Twitter Snowflake Style in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Configures a `TSID.Factory` to emulate the structure and epoch of Twitter Snowflakes. It calculates the node ID from datacenter and worker IDs and sets a specific custom epoch and a random function that forces the counter to zero on millisecond change. ```java // Twitter Snowflakes have 5 bits for datacenter ID and 5 bits for worker ID int datacenter = 1; // max: 2^5-1 = 31 int worker = 1; // max: 2^5-1 = 31 int node = (datacenter << 5 | worker); // max: 2^10-1 = 1023 // Twitter Epoch is fixed in 1288834974657 (2010-11-04T01:42:54.657Z) Instant customEpoch = Instant.ofEpochMilli(1288834974657L); // a function that returns an array with ZEROS, making the factory // to RESET the counter to ZERO when the millisecond changes IntFunction randomFunction = (x) -> new byte[x]; // a factory that returns TSIDs similar to Twitter Snowflakes TSID.Factory factory = TSID.Factory.builder() .withRandomFunction(randomFunction) .withCustomEpoch(customEpoch) .withNode(node) .build(); // use the factory TSID tsid = factory.generate(); ``` -------------------------------- ### Configuring TSID Factory with RandomGenerator (JDK 17+) in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Configures the `TSID.Factory` to use a random function derived from a `java.util.random.RandomGenerator` instance, suitable for JDK 17+. It allows integrating newer random number generator implementations. ```java // use a random function that returns an int value RandomGenerator random = RandomGenerator.getDefault(); TSID.Factory factory = TSID.Factory.builder() .withRandomFunction(() -> random.nextInt()) .build(); // use the factory TSID tsid = factory.generate(); ``` -------------------------------- ### Configuring TSID Factory with Fixed Node in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Creates a `TSID.Factory` instance explicitly configured with a predefined integer value for the node identifier. The factory is then used to generate new TSID objects with this fixed node ID. ```java int node = 256; // max: 2^10 TSID.Factory factory = new TSID.Factory(node); // use the factory TSID tsid = factory.generate(); ``` -------------------------------- ### Configuring TSID Factory with ThreadLocalRandom in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Configures the `TSID.Factory` to use a random function based on `java.util.concurrent.ThreadLocalRandom`. This is generally recommended for concurrent environments to avoid contention. ```java // use a random function that returns an int value TSID.Factory factory = TSID.Factory.builder() .withRandomFunction(() -> ThreadLocalRandom.current().nextInt()) .build(); // use the factory TSID tsid = factory.generate(); ``` -------------------------------- ### Converting TSID to String in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md This snippet explicitly shows converting a generated TSID to its base32 string format. This method encodes the internal 64-bit value into a 13-character string using Crockford's base 32 encoding. Requires the io.hypersistence.tsid library dependency. ```java String tsid = TSID.Factory.getTsid().toString(); ``` -------------------------------- ### Generating a TSID Object in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md This snippet demonstrates the basic way to generate a new Time-Sorted Unique Identifier (TSID) object using the static factory method provided by the TSID.Factory class. The generated TSID object can then be used or converted to other formats like long or string. Requires the io.hypersistence.tsid library dependency. ```java TSID tsid = TSID.Factory.getTsid(); ``` -------------------------------- ### Creating a TSID from Canonical String in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Parses a 13-character canonical string representation of a TSID and reconstructs the corresponding TSID object. Useful for deserializing or validating TSIDs from storage or external systems. ```java TSID tsid = TSID.from("0123456789ABC"); ``` -------------------------------- ### Converting TSID to Long in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md This snippet explicitly shows retrieving the internal long value of a generated TSID. This is the same operation as generating and converting, emphasizing that the long value represents the core TSID. Requires the io.hypersistence.tsid library dependency. ```java long tsid = TSID.Factory.getTsid().toLong(); ``` -------------------------------- ### Converting TSID to Lowercase String in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Converts a TSID object into its canonical 13-character string representation, ensuring all alphabetic characters are in lowercase. This is a standard way to represent the TSID value. ```java String string = tsid.toLowerCase(); // 0123456789abc ``` -------------------------------- ### Generating a TSID as Long in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md This snippet shows how to generate a new TSID and immediately convert it to its internal long representation. This long value can be stored efficiently and preserves the time-sorted property, useful for database primary keys. Requires the io.hypersistence.tsid library dependency. ```java long number = TSID.Factory.getTsid().toLong(); ``` -------------------------------- ### Encoding TSID to Base-62 String in Java Source: https://github.com/vladmihalcea/hypersistence-tsid/blob/master/README.md Encodes the internal numeric value of the TSID into a shorter string representation using base-62 encoding. The length of the encoded string depends on the value and the base used. ```java String string = tsid.encode(62); // 0T5jFDIkmmy ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.