Interface DSnowflake

  • All Superinterfaces:
    DType

    public interface DSnowflake
    extends DType
    DSnowflake is a cluster-unique ID generator based on the Twitter/X Snowflake design. Generated IDs are long primitive values and are k-ordered (roughly ordered). IDs are in the range from 0 to Long.MAX_VALUE.

    The layout of IDs is as follows:

    • timestamp (41 bits by default)
    • machine ID (10 bits by default)
    • sequence number (12 bits by default)
    The layout can be adjusted by creating DSnowflakes with custom bit lengths for the machine ID and the sequence number.

    The timestamp is stored relative to a specific epoch start which, by default, is 1 Jan, 2020 (DEFAULT_EPOCH_START).

    The default bit sizes allow for generating 4096 (2^12) sequence IDs per millisecond. If that rate is exceeded, generation will block until the next millisecond.

    The current implementation generates the default machine ID from the nanosecond timestamp on the system. Thus, DSnowflakes created on the same system, will have different machine IDs. If required, a custom machine ID can be provided.

    A builder pattern is used to create custom DSnowflake instance:

     DSnowflake flake = DSnowflake.builder()
         .withEpochStart(1_000_000)
         .withMachineId(37)
         .withSequenceBits(15)
         .build();
     
    • Method Detail

      • nextId

        long nextId()
        Generate a new ID.
        Returns:
        the next ID
      • parse

        long[] parse​(long sequence)
        Parse a sequence ID into its individual components based on the configured bit lengths. Note that the timestamp will be adjusted by the epoch start to reflect the 'correct' time.
        Parameters:
        sequence - the sequence ID to parse
        Returns:
        a long[3] array consisting of timestamp, machine ID and sequence number
      • destroy

        default void destroy()
        This is a no-op for DSnowflakes. Since nothing is distributed or stored for this type, there are no additional resources to release.
        Specified by:
        destroy in interface DType
      • builder

        static DSnowflake.Builder builder()
        Provides a Builder used to create customized DSnowflakes.
        Returns:
        a new Builder