Package com.bytedesk.core.uid.impl
Class DefaultUidGenerator
java.lang.Object
com.bytedesk.core.uid.impl.DefaultUidGenerator
- All Implemented Interfaces:
UidGeneratorService
,org.springframework.beans.factory.InitializingBean
- Direct Known Subclasses:
CachedUidGenerator
public class DefaultUidGenerator
extends Object
implements UidGeneratorService, org.springframework.beans.factory.InitializingBean
Represents an implementation of sign: The highest bit is 0
delta seconds: The next 28 bits, represents delta seconds since a customer epoch(2016-05-20 00:00:00.000).
Supports about 8.7 years until to 2024-11-20 21:24:16
worker id: The next 22 bits, represents the worker's id which assigns based on database, max id is about 420W
sequence: The next 13 bits, represents a sequence within the same second, max for 8192/s
timeBits: default as 28
workerBits: default as 22
seqBits: default as 13
epochStr: Epoch date string format 'yyyy-MM-dd'. Default as '2016-05-20'
UidGeneratorService
The unique id has 64bits (long), default allocated as blow:
The parseUID(long)
is a tool method to parse the bits
+------+----------------------+----------------+-----------+
| sign | delta seconds | worker node id | sequence |
+------+----------------------+----------------+-----------+
1bit 28bits 22bits 13bits
You can also specified the bits by Spring property setting.
Note that: The total bits must be 64 -1
- Author:
- yutianbao, wujun
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BitsAllocator
Bit分配器,Stable fields after spring bean initializingprotected long
protected long
Volatile fields caused by nextId()protected UidProperties
protected long
protected WorkerIdAssigner
Spring property -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
private long
Get current secondprivate long
getNextSecond
(long lastTimestamp) Get next secondlong
getUID()
Get a unique IDprotected long
nextId()
Get UIDparseUID
(long uid) Parse the UID into elements which are used to generate the UID.void
setWorkerIdAssigner
(WorkerIdAssigner workerIdAssigner) Setters for spring property
-
Field Details
-
uidProperties
-
bitsAllocator
Bit分配器,Stable fields after spring bean initializing -
workerId
protected long workerId -
sequence
protected long sequenceVolatile fields caused by nextId() -
lastSecond
protected long lastSecond -
workerIdAssigner
Spring property
-
-
Constructor Details
-
DefaultUidGenerator
-
-
Method Details
-
afterPropertiesSet
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
Exception
-
getUID
Description copied from interface:UidGeneratorService
Get a unique ID- Specified by:
getUID
in interfaceUidGeneratorService
- Returns:
- UID
- Throws:
UidGenerateException
-
parseUID
Description copied from interface:UidGeneratorService
Parse the UID into elements which are used to generate the UID.
Such as timestamp and workerId and sequence...- Specified by:
parseUID
in interfaceUidGeneratorService
- Returns:
- Parsed info
-
nextId
protected long nextId()Get UID- Returns:
- UID
- Throws:
UidGenerateException
- in the case: Clock moved backwards; Exceeds the max timestamp
-
getNextSecond
private long getNextSecond(long lastTimestamp) Get next second -
getCurrentSecond
private long getCurrentSecond()Get current second -
setWorkerIdAssigner
Setters for spring property
-