Class BitsAllocator

java.lang.Object
com.bytedesk.core.uid.impl.BitsAllocator

public class BitsAllocator extends Object
Allocate 64 bits for the UID(long)
sign (fixed 1bit) -> deltaSecond -> workerId -> sequence(within the same second)
Author:
yutianbao
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final long
    Max value for workId sequence
    private final long
     
    private final long
     
    private final int
     
    private int
    Bits for [sign-> second-> workId-> sequence]
    private final int
     
    private final int
    Shift for timestamp workerId
    static final int
    Total 64 bits
    private final int
     
    private final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    BitsAllocator(int timestampBits, int workerIdBits, int sequenceBits)
    Constructor with timestampBits, workerIdBits, sequenceBits
    The highest bit used for sign, so 63 bits for timestampBits, workerIdBits, sequenceBits
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    allocate(long deltaSeconds, long workerId, long sequence)
    Allocate bits for UID according to delta seconds workerId sequence
    Note that: The highest bit will always be 0 for sign

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TOTAL_BITS

      public static final int TOTAL_BITS
      Total 64 bits
      See Also:
    • signBits

      private int signBits
      Bits for [sign-> second-> workId-> sequence]
    • timestampBits

      private final int timestampBits
    • workerIdBits

      private final int workerIdBits
    • sequenceBits

      private final int sequenceBits
    • maxDeltaSeconds

      private final long maxDeltaSeconds
      Max value for workId sequence
    • maxWorkerId

      private final long maxWorkerId
    • maxSequence

      private final long maxSequence
    • timestampShift

      private final int timestampShift
      Shift for timestamp workerId
    • workerIdShift

      private final int workerIdShift
  • Constructor Details

    • BitsAllocator

      public BitsAllocator(int timestampBits, int workerIdBits, int sequenceBits)
      Constructor with timestampBits, workerIdBits, sequenceBits
      The highest bit used for sign, so 63 bits for timestampBits, workerIdBits, sequenceBits
  • Method Details

    • allocate

      public long allocate(long deltaSeconds, long workerId, long sequence)
      Allocate bits for UID according to delta seconds workerId sequence
      Note that: The highest bit will always be 0 for sign
      Parameters:
      deltaSeconds -
      workerId -
      sequence -
      Returns: