This commit is contained in:
wangkang
2017-09-06 10:45:56 +08:00
parent 19fb3c476a
commit b3b6c28815
3 changed files with 9 additions and 1 deletions
@@ -115,6 +115,7 @@ public class IdWorker {
public Id reverse(long id) {
Id reverseId = new Id();
reverseId.setSequence((id) & ~(-1L << sequenceBits));
reverseId.setDwId((id >> (workerIdShift)) & ~(-1L << (datacenterIdBits + workerIdBits)));
reverseId.setWorkerId((id >> workerIdShift) & ~(-1L << workerIdBits));
reverseId.setDatacenterId((id >> datacenterIdShift) & ~(-1L << datacenterIdBits));
reverseId.setTimestamp((id >> timestampLeftShift) + twepoch);
@@ -29,6 +29,12 @@ public class Id implements Serializable {
@ApiModelProperty(value = "数据中心ID( 0 - 31 )", required = true, dataType = "long")
private long datacenterId;
/**
* 数据中心ID | 机器ID ( 0 - 1023 )
*/
@ApiModelProperty(value = "数据中心ID | 机器ID ( 0 - 4095 )", required = true, dataType = "long")
private long dwId;
/**
* 时间戳
*/
@@ -15,7 +15,7 @@ public class IdWorkerTest {
public void id() {
Map<Long, Long> map = new HashMap<>();
int maxCount = 1;
IdWorker idWorker = new IdWorker(10, 20);
IdWorker idWorker = new IdWorker(1, 1);
for (int i = 0; i < maxCount; i++) {
long id = idWorker.nextId();
log.info("--------------------------------------------------------------");
@@ -30,6 +30,7 @@ public class IdWorkerTest {
log.info(Long.toBinaryString((id >> 17)) + " ---> id ,左移17位,定位到datacenterId");
log.info(Long.toBinaryString((id >> 22)) + " ---> id ,左移22位,定位到timestamp");
log.info(Long.parseLong(Long.toBinaryString((id) & ~(-1L << 12L)), 2) + " ---> & 运算 , 解析sequence");
log.info(Long.parseLong(Long.toBinaryString((id >> 12) & ~(-1L << 10L)), 2) + " ---> & 运算 , 解析dwId");
log.info(Long.parseLong(Long.toBinaryString((id >> 12) & ~(-1L << 5L)), 2) + " ---> & 运算 , 解析workerId");
log.info(Long.parseLong(Long.toBinaryString((id >> 17) & ~(-1L << 5L)), 2) + " ---> & 运算 , 解析datacenterId");
log.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date(Long.parseLong(Long.toBinaryString((id >> 22) + 1288834974657l), 2))) + " ---> & 运算 , 解析timestamp");