Interface DSnowflakeId

All Superinterfaces:
DType

public interface DSnowflakeId extends DType
DSnowflakeId 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 DSnowflakeIds 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, DSnowflakeIds 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 DSnowflakeId instance:

 DSnowflakeId flake = DSnowflakeId.builder()
     .withEpochStart(1_000_000)
     .withMachineId(37)
     .withSequenceBits(15)
     .build();
 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Builder used to create custom DSnowflakeIds.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    The default epoch start - 1 Jan, 2020
    static final int
     
    static final int
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Provides a Builder used to create customized DSnowflakeIds.
    default void
    This is a no-op for DSnowflakeIds.
    long
    Generate a new ID.
    long[]
    parse(long sequence)
    Parse a sequence ID into its individual components based on the configured bit lengths.

    Methods inherited from interface com.vmware.gemfire.distributedtypes.DType

    getName
  • Field Details

    • DEFAULT_MACHINE_BITS

      static final int DEFAULT_MACHINE_BITS
      See Also:
    • DEFAULT_SEQUENCE_BITS

      static final int DEFAULT_SEQUENCE_BITS
      See Also:
    • DEFAULT_EPOCH_START

      static final long DEFAULT_EPOCH_START
      The default epoch start - 1 Jan, 2020
      See Also:
  • Method Details

    • 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 DSnowflakeIds. Since nothing is distributed or stored for this type, there are no additional resources to release.
      Specified by:
      destroy in interface DType
    • builder

      static DSnowflakeId.Builder builder()
      Provides a Builder used to create customized DSnowflakeIds.
      Returns:
      a new Builder