Class MillisecondSnowflakeId
- All Implemented Interfaces:
IdGenerator
,SnowflakeId
,Statistical
,StringIdGenerator
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The default number of bits used for the machine ID portion (10 bits).static final int
The default number of bits used for the sequence portion (12 bits).static final long
The default sequence reset threshold (half of the maximum sequence value).static final int
The default number of bits used for the timestamp portion (41 bits).Fields inherited from class me.ahoo.cosid.snowflake.AbstractSnowflakeId
epoch, lastTimestamp, machineBit, machineId, machineLeft, maxMachineId, maxSequence, maxTimestamp, sequence, sequenceBit, timestampBit, timestampLeft
Fields inherited from interface me.ahoo.cosid.snowflake.SnowflakeId
TOTAL_BIT
-
Constructor Summary
ConstructorsConstructorDescriptionMillisecondSnowflakeId
(int machineId) Create a new MillisecondSnowflakeId with default configuration.MillisecondSnowflakeId
(int machineBit, int machineId) Create a new MillisecondSnowflakeId with custom machine bit configuration.MillisecondSnowflakeId
(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId) Create a new MillisecondSnowflakeId with custom bit configuration.MillisecondSnowflakeId
(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold) Create a new MillisecondSnowflakeId with full custom configuration. -
Method Summary
Modifier and TypeMethodDescriptionprotected long
Get the current time in milliseconds.Methods inherited from class me.ahoo.cosid.snowflake.AbstractSnowflakeId
generate, getEpoch, getLastTimestamp, getMachineBit, getMachineId, getMaxMachineId, getMaxSequence, getMaxTimestamp, getSequenceBit, getTimestampBit, nextTime
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface me.ahoo.cosid.IdGenerator
generateAsString, idConverter
Methods inherited from interface me.ahoo.cosid.snowflake.SnowflakeId
isSafeJavascript, stat
-
Field Details
-
DEFAULT_TIMESTAMP_BIT
public static final int DEFAULT_TIMESTAMP_BITThe 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_BITThe 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_BITThe 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_THRESHOLDThe 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 IDmachineId
- 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 basetimestampBit
- The number of bits to use for timestampmachineBit
- The number of bits to use for machine IDsequenceBit
- The number of bits to use for sequencemachineId
- 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 basetimestampBit
- The number of bits to use for timestampmachineBit
- The number of bits to use for machine IDsequenceBit
- The number of bits to use for sequencemachineId
- The machine ID for this generatorsequenceResetThreshold
- 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 classAbstractSnowflakeId
- Returns:
- The current time in milliseconds since the Unix epoch
-