Package me.ahoo.cosid.snowflake
Class SecondSnowflakeId
java.lang.Object
me.ahoo.cosid.snowflake.AbstractSnowflakeId
me.ahoo.cosid.snowflake.SecondSnowflakeId
- All Implemented Interfaces:
IdGenerator,SnowflakeId,Statistical,StringIdGenerator
Second-based Snowflake ID generator.
Similar to MillisecondSnowflakeId but uses seconds instead of milliseconds
as the time unit. This allows for a longer timestamp range but with lower
time precision.
Default configuration:
- Timestamp bits: 31 (about 68 years with second precision)
- Machine ID bits: 10 (1024 unique machines)
- Sequence bits: 22 (about 4 million IDs per second per machine)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault number of machine ID bits (10 bits).static final intDefault number of sequence bits (22 bits).static final longDefault sequence reset threshold (half of max sequence).static final intDefault number of timestamp bits (31 bits).Fields inherited from class me.ahoo.cosid.snowflake.AbstractSnowflakeId
epoch, lastTimestamp, machineBit, machineId, machineLeft, maxMachineId, maxSequence, maxTimestamp, sequence, sequenceBit, timestampBit, timestampLeftFields inherited from interface me.ahoo.cosid.snowflake.SnowflakeId
TOTAL_BIT -
Constructor Summary
ConstructorsConstructorDescriptionSecondSnowflakeId(int machineId) Creates a SecondSnowflakeId with default configuration.SecondSnowflakeId(int machineBit, int machineId) Creates a SecondSnowflakeId with custom machine bits.SecondSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId) Creates a SecondSnowflakeId with custom bit configuration.SecondSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold) Creates a SecondSnowflakeId with full custom configuration. -
Method Summary
Modifier and TypeMethodDescriptionprotected longGets the current time in the appropriate unit for this snowflake ID variant.Methods inherited from class me.ahoo.cosid.snowflake.AbstractSnowflakeId
generate, getEpoch, getLastTimestamp, getMachineBit, getMachineId, getMaxMachineId, getMaxSequence, getMaxTimestamp, getSequenceBit, getTimestampBit, nextTimeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface me.ahoo.cosid.IdGenerator
generateAsString, idConverterMethods inherited from interface me.ahoo.cosid.snowflake.SnowflakeId
isSafeJavascript, stat
-
Field Details
-
DEFAULT_TIMESTAMP_BIT
public static final int DEFAULT_TIMESTAMP_BITDefault number of timestamp bits (31 bits).- See Also:
-
DEFAULT_MACHINE_BIT
public static final int DEFAULT_MACHINE_BITDefault number of machine ID bits (10 bits).- See Also:
-
DEFAULT_SEQUENCE_BIT
public static final int DEFAULT_SEQUENCE_BITDefault number of sequence bits (22 bits).- See Also:
-
DEFAULT_SEQUENCE_RESET_THRESHOLD
public static final long DEFAULT_SEQUENCE_RESET_THRESHOLDDefault sequence reset threshold (half of max sequence).- See Also:
-
-
Constructor Details
-
SecondSnowflakeId
public SecondSnowflakeId(int machineId) Creates a SecondSnowflakeId with default configuration.- Parameters:
machineId- the machine ID
-
SecondSnowflakeId
public SecondSnowflakeId(int machineBit, int machineId) Creates a SecondSnowflakeId with custom machine bits.- Parameters:
machineBit- the number of bits for machine IDmachineId- the machine ID
-
SecondSnowflakeId
public SecondSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId) Creates a SecondSnowflakeId with custom bit configuration.- Parameters:
epoch- epoch timestamp in millisecondstimestampBit- number of bits for timestampmachineBit- number of bits for machine IDsequenceBit- number of bits for sequencemachineId- the machine ID
-
SecondSnowflakeId
public SecondSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold) Creates a SecondSnowflakeId with full custom configuration.- Parameters:
epoch- epoch timestamp in millisecondstimestampBit- number of bits for timestampmachineBit- number of bits for machine IDsequenceBit- number of bits for sequencemachineId- the machine IDsequenceResetThreshold- threshold for resetting sequence
-
-
Method Details
-
getCurrentTime
protected long getCurrentTime()Description copied from class:AbstractSnowflakeIdGets the current time in the appropriate unit for this snowflake ID variant.- Specified by:
getCurrentTimein classAbstractSnowflakeId- Returns:
- current time value
-