public class BitsAllocator extends Object
为UID分配64位(long)
sign (fixed 1bit) -> deltaSecond -> workerId -> sequence(within the same second)
| 限定符和类型 | 字段和说明 |
|---|---|
static int |
TOTAL_BITS
Total 64 bits
|
| 构造器和说明 |
|---|
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 |
| 限定符和类型 | 方法和说明 |
|---|---|
long |
allocate(long deltaSeconds,
long workerId,
long sequence)
这里就是把不同的字段放到相应的位上
id的总体结构是:
sign (fixed 1bit) -> deltaSecond -> workerId -> sequence(within the same second)
deltaSecond 左移(workerIdBits + sequenceBits)位,workerId左移sequenceBits位,此时就完成了字节的分配
Allocate bits for UID according to delta seconds & workerId & sequence
Note that: The highest bit will always be 0 for sign |
long |
getMaxDeltaSeconds() |
long |
getMaxSequence() |
long |
getMaxWorkerId() |
int |
getSequenceBits() |
int |
getSignBits()
Getters
|
int |
getTimestampBits() |
int |
getTimestampShift() |
int |
getWorkerIdBits() |
int |
getWorkerIdShift() |
String |
toString() |
public static final int TOTAL_BITS
public BitsAllocator(int timestampBits,
int workerIdBits,
int sequenceBits)
63 bits for timestampBits, workerIdBits, sequenceBitspublic 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
deltaSeconds - workerId - sequence - public int getSignBits()
public int getTimestampBits()
public int getWorkerIdBits()
public int getSequenceBits()
public long getMaxDeltaSeconds()
public long getMaxWorkerId()
public long getMaxSequence()
public int getTimestampShift()
public int getWorkerIdShift()
Copyright © 2023. All rights reserved.