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 UidGeneratorService

The unique id has 64bits (long), default allocated as blow:

  • 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

    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.

  • 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'

    Note that: The total bits must be 64 -1

  • Author:
    yutianbao, wujun
    • Field Details

      • uidProperties

        protected UidProperties uidProperties
      • bitsAllocator

        protected BitsAllocator bitsAllocator
        Bit分配器,Stable fields after spring bean initializing
      • workerId

        protected long workerId
      • sequence

        protected long sequence
        Volatile fields caused by nextId()
      • lastSecond

        protected long lastSecond
      • workerIdAssigner

        @Autowired protected WorkerIdAssigner workerIdAssigner
        Spring property
    • Constructor Details

      • DefaultUidGenerator

        public DefaultUidGenerator(UidProperties uidProperties)
    • Method Details

      • afterPropertiesSet

        public void afterPropertiesSet() throws Exception
        Specified by:
        afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
        Throws:
        Exception
      • getUID

        public long getUID() throws UidGenerateException
        Description copied from interface: UidGeneratorService
        Get a unique ID
        Specified by:
        getUID in interface UidGeneratorService
        Returns:
        UID
        Throws:
        UidGenerateException
      • parseUID

        public String parseUID(long uid)
        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 interface UidGeneratorService
        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

        public void setWorkerIdAssigner(WorkerIdAssigner workerIdAssigner)
        Setters for spring property