com.github.drinkjava2.jdialects.id
Class SnowflakeCreator

java.lang.Object
  extended by com.github.drinkjava2.jdialects.id.SnowflakeCreator

public class SnowflakeCreator
extends Object

java edition of Twitter Snowflake, a network service for generating unique ID numbers at high scale with some simple guarantees. https://github.com/twitter/snowflake Usage example: long id= new SnowflakeCreator(5L,5L, 18, 31).nextId();
P1 is datacenterIdBits, 1~9 bits
P2 is workerIdBits, 1~9 bits
P3 is real datacenterId, 0 to 511
P4 is real workerId, 0 to 511 Should: P1 + P2 = 10 (2^10=1024) Should: P3 + P4 < 1024

Author:
downgoon, Yong Z.

Constructor Summary
SnowflakeCreator(long datacenterIdBits, long workerIdBits, long datacenterId, long workerId)
           
 
Method Summary
 String formatId(long id)
          extract and display time stamp, datacenterId, workerId and sequence number information from the given id in humanization format
 long getEpoch()
           
 long getWaitCount()
           
 long nextId()
          generate an unique and incrementing id
 long[] parseId(long id)
          extract time stamp, datacenterId, workerId and sequence number information from the given id
protected  long timestampGen()
          get current time stamp
 String toString()
          show settings of Snowflake
protected  long waitNextMillis(long currTimestamp)
          running loop blocking until next millisecond
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SnowflakeCreator

public SnowflakeCreator(long datacenterIdBits,
                        long workerIdBits,
                        long datacenterId,
                        long workerId)
Parameters:
datacenterId - data center number the process running on, value range: [0,31]
workerId - machine or process number, value range: [0,31]
Method Detail

nextId

public long nextId()
generate an unique and incrementing id

Returns:
id

getWaitCount

public long getWaitCount()
Returns:
the amount of calling waitNextMillis(long) method

waitNextMillis

protected long waitNextMillis(long currTimestamp)
running loop blocking until next millisecond

Parameters:
currTimestamp - current time stamp
Returns:
current time stamp in millisecond

timestampGen

protected long timestampGen()
get current time stamp

Returns:
current time stamp in millisecond

toString

public String toString()
show settings of Snowflake

Overrides:
toString in class Object

getEpoch

public long getEpoch()

parseId

public long[] parseId(long id)
extract time stamp, datacenterId, workerId and sequence number information from the given id

Parameters:
id - a snowflake id generated by this object
Returns:
an array containing time stamp, datacenterId, workerId and sequence number

formatId

public String formatId(long id)
extract and display time stamp, datacenterId, workerId and sequence number information from the given id in humanization format

Parameters:
id - snowflake id in Long format
Returns:
snowflake id in String format


Copyright © 2018. All rights reserved.