Merge pull request #10 from freman/master

Use constants for bits, for easier changing
This commit is contained in:
Bruce 2018-03-19 15:35:01 -05:00 committed by GitHub
commit 07b209d699
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,9 +12,11 @@ import (
) )
const ( const (
spareBits = 22
nodeBits = 10 nodeBits = 10
stepBits = 12 stepBits = spareBits - nodeBits
nodeMax = -1 ^ (-1 << nodeBits) nodeMax = -1 ^ (-1 << nodeBits)
nodeMask = nodeMax << stepBits
stepMask int64 = -1 ^ (-1 << stepBits) stepMask int64 = -1 ^ (-1 << stepBits)
timeShift uint8 = nodeBits + stepBits timeShift uint8 = nodeBits + stepBits
nodeShift uint8 = stepBits nodeShift uint8 = stepBits
@ -245,12 +247,12 @@ func (f ID) Time() int64 {
// Node returns an int64 of the snowflake ID node number // Node returns an int64 of the snowflake ID node number
func (f ID) Node() int64 { func (f ID) Node() int64 {
return int64(f) & 0x00000000003FF000 >> nodeShift return int64(f) & nodeMask >> nodeShift
} }
// Step returns an int64 of the snowflake step (or sequence) number // Step returns an int64 of the snowflake step (or sequence) number
func (f ID) Step() int64 { func (f ID) Step() int64 {
return int64(f) & 0x0000000000000FFF return int64(f) & stepMask
} }
// MarshalJSON returns a json byte array string of the snowflake ID. // MarshalJSON returns a json byte array string of the snowflake ID.