Class MillisecondSnowflakeId

java.lang.Object
me.ahoo.cosid.snowflake.AbstractSnowflakeId
me.ahoo.cosid.snowflake.MillisecondSnowflakeId
All Implemented Interfaces:
IdGenerator, SnowflakeId, Statistical, StringIdGenerator

public class MillisecondSnowflakeId extends AbstractSnowflakeId
Millisecond-precision Snowflake ID generator.

This implementation of the Snowflake algorithm uses millisecond-precision timestamps as the time component of generated IDs. It provides a good balance between time resolution and longevity:

  • 41 bits for timestamp (millisecond precision)
  • 10 bits for machine ID (1024 unique machines)
  • 12 bits for sequence (4096 IDs per millisecond per machine)

With millisecond precision and 41 bits, this generator can produce unique IDs for approximately 69 years from the epoch. The default epoch is set to December 24, 2019, which extends the usable time range into the 2080s.

This class extends AbstractSnowflakeId and provides the concrete implementation for retrieving the current time in milliseconds.

  • Field Details

    • DEFAULT_TIMESTAMP_BIT

      public static final int DEFAULT_TIMESTAMP_BIT
      The default number of bits used for the timestamp portion (41 bits).

      With 41 bits, timestamps can represent approximately 69 years of millisecond-precision time, providing a good balance between longevity and resolution.

      See Also:
    • DEFAULT_MACHINE_BIT

      public static final int DEFAULT_MACHINE_BIT
      The default number of bits used for the machine ID portion (10 bits).

      With 10 bits, up to 1024 unique machines can participate in ID generation, which is sufficient for most distributed systems.

      See Also:
    • DEFAULT_SEQUENCE_BIT

      public static final int DEFAULT_SEQUENCE_BIT
      The default number of bits used for the sequence portion (12 bits).

      With 12 bits, up to 4096 unique IDs can be generated per time unit (per millisecond) per machine, providing high throughput within a single time unit.

      See Also:
    • DEFAULT_SEQUENCE_RESET_THRESHOLD

      public static final long DEFAULT_SEQUENCE_RESET_THRESHOLD
      The default sequence reset threshold (half of the maximum sequence value).

      This threshold is used to determine when to reset the sequence counter to avoid reaching the maximum sequence value too quickly.

      See Also:
  • Constructor Details

    • MillisecondSnowflakeId

      public MillisecondSnowflakeId(int machineId)
      Create a new MillisecondSnowflakeId with default configuration.

      This constructor creates a generator with the default bit configuration and the CosId epoch, intended for use with the specified machine ID.

      Parameters:
      machineId - The machine ID for this generator (0-1023)
    • MillisecondSnowflakeId

      public MillisecondSnowflakeId(int machineBit, int machineId)
      Create a new MillisecondSnowflakeId with custom machine bit configuration.

      This constructor allows customization of the machine bit size while using default values for other parameters.

      Parameters:
      machineBit - The number of bits to use for machine ID
      machineId - The machine ID for this generator
    • MillisecondSnowflakeId

      public MillisecondSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId)
      Create a new MillisecondSnowflakeId with custom bit configuration.

      This constructor allows customization of the bit sizes while using the default sequence reset threshold calculation.

      Parameters:
      epoch - The epoch timestamp to use as the base
      timestampBit - The number of bits to use for timestamp
      machineBit - The number of bits to use for machine ID
      sequenceBit - The number of bits to use for sequence
      machineId - The machine ID for this generator
    • MillisecondSnowflakeId

      public MillisecondSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold)
      Create a new MillisecondSnowflakeId with full custom configuration.

      This constructor allows complete control over all parameters of the Snowflake ID generator.

      Parameters:
      epoch - The epoch timestamp to use as the base
      timestampBit - The number of bits to use for timestamp
      machineBit - The number of bits to use for machine ID
      sequenceBit - The number of bits to use for sequence
      machineId - The machine ID for this generator
      sequenceResetThreshold - The threshold for sequence reset
  • Method Details

    • getCurrentTime

      protected long getCurrentTime()
      Get the current time in milliseconds.

      This method provides the time source for the timestamp portion of generated IDs. It uses System.currentTimeMillis() to retrieve the current time with millisecond precision.

      Specified by:
      getCurrentTime in class AbstractSnowflakeId
      Returns:
      The current time in milliseconds since the Unix epoch