Documentation ¶
Index ¶
- Variables
- type ID
- type IdByDefault
- type Manager
- type ManagerByDefault
- func (m *ManagerByDefault) New(args map[string]int64) ID
- func (m *ManagerByDefault) NewBytes(args map[string]int64) ([]byte, error)
- func (m *ManagerByDefault) NewString(args map[string]int64) (string, error)
- func (m *ManagerByDefault) NewToInt64(args map[string]int64) (int64, error)
- func (m *ManagerByDefault) ParseBytes(b []byte) (ID, error)
- func (m *ManagerByDefault) ParseInt64(i64 int64) (ID, error)
- func (m *ManagerByDefault) ParseString(s string) (ID, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ArgsOrder defines the order in which data is stored. // If the keys of ArgsBits are not in ArgsOrder, these keys will not be used. ArgsOrder = []string{"unused", "machine"} // ArgsBits defines number of bits of snowflake id occupied by key. // It is recommended to set aside 40 bits for Overtime. // Please note that the default Sequence takes up 12 bits. ArgsBits = map[string]uint8{"unused": 1, "machine": 10} // ArgsOffsetBits defines offset of each Args, Automatically calculate when new manager is created. ArgsOffsetBits = map[string]uint8{} // ArgsMax defines max of each Args, Automatically calculate when new manager is created. ArgsMax = map[string]int64{} )
var ( // Epoch defines a start time and recommended to be set as the project on-line time. Epoch int64 = 1288834974657 // OvertimeBits defines the number of bits occupied by Overtime. Automatic initialization. OvertimeBits uint8 = 64 // SequenceBits defines the number of bits occupied by Sequence. SequenceBits uint8 = 12 // OvertimeOffsetBits defines the offset of Overtime. Automatic initialization. OvertimeOffsetBits uint8 = SequenceBits // SequenceOffsetBits defines the offset of Sequence. Automatic initialization. SequenceOffsetBits uint8 // OvertimeMax defines the maximum value of Overtime. Automatic initialization. OvertimeMax int64 = -1 ^ (-1 << OvertimeMax) // SequenceMax defines the maximum value of Sequence. Automatic initialization. SequenceMax int64 = -1 ^ (-1 << SequenceBits) )
Functions ¶
This section is empty.
Types ¶
type ID ¶
type ID interface { // convert id to int64 type ToInt64() (int64, error) // convert id to []byte type ToBytes() ([]byte, error) // convert id to string type ToString() (string, error) // Calculates id create time CreateTime() int64 }
ID is the raw interface of snowflake id. The following functions represent actions that can be used.
type IdByDefault ¶
snowflake id Args is the content stored in snowflake id. If the key of args does not exist, the default value is 0. Overtime = now - Epoch (in ms) Sequence is the serial number in the same microsecond.
func (*IdByDefault) CreateTime ¶
func (id *IdByDefault) CreateTime() int64
Calculates id create time Please do not modify Epoch at will, otherwise CreateTime will be confused.
func (*IdByDefault) ToBytes ¶
func (id *IdByDefault) ToBytes() ([]byte, error)
convert id to []byte type
func (*IdByDefault) ToInt64 ¶
func (id *IdByDefault) ToInt64() (int64, error)
convert id to int64 type
func (*IdByDefault) ToString ¶
func (id *IdByDefault) ToString() (string, error)
convert id to string type
type Manager ¶
type Manager interface { // New return a Snowflake ID interface. // This ID records key data. New(map[string]int64) ID // NewToInt64 return int64 data that can be used to represent an ID, and return possible errors. NewToInt64(map[string]int64) (int64, error) // ParseInt64 return parsed int64 data, and return possible errors. ParseInt64(int64) (ID, error) // NewBytes return []byte data that can be used to represent an ID, and return possible errors. NewBytes(map[string]int64) ([]byte, error) // ParseInt64 return parsed []byte data, and return possible errors. ParseBytes([]byte) (ID, error) // NewString return string data that can be used to represent an ID, and return possible errors. NewString(map[string]int64) (string, error) // ParseInt64 return parsed string data, and return possible errors. ParseString(string) (ID, error) }
Manager is the raw interface used to create snowflake id. The following functions represent the operations that can be used. To be honest, I don't know why I don't call it generators.
func NewDefaultManager ¶
ManagerByDefault returns a Manager object and an error caused by some abnormal data All the generation and analysis are operated by the manager.
func NewManager ¶
NewManager retrun NewDefaultManager object. snowflagid is mainly generated/parsed by manager.
type ManagerByDefault ¶
id manager Mut dirty data lock. LastUseTime records the last time the manager was used. Sequence records the id serial number that was last generated using the manager.
func (*ManagerByDefault) New ¶
func (m *ManagerByDefault) New(args map[string]int64) ID
Create a new snowflag id Args is snowflake id Args !!!In fact, this part is unsafe because the maximum value is not checked for performance reasons. One solution is to move the args part to the manager.
func (*ManagerByDefault) NewBytes ¶
func (m *ManagerByDefault) NewBytes(args map[string]int64) ([]byte, error)
Create a new snowflag id and convert it to []byte type
func (*ManagerByDefault) NewString ¶
func (m *ManagerByDefault) NewString(args map[string]int64) (string, error)
Create a new snowflag id and convert it to string type
func (*ManagerByDefault) NewToInt64 ¶
func (m *ManagerByDefault) NewToInt64(args map[string]int64) (int64, error)
Create a new snowflag id and convert it to int64 type
func (*ManagerByDefault) ParseBytes ¶
func (m *ManagerByDefault) ParseBytes(b []byte) (ID, error)
Parsing snowflake id from []byte type data
func (*ManagerByDefault) ParseInt64 ¶
func (m *ManagerByDefault) ParseInt64(i64 int64) (ID, error)
Parsing snowflake id from int64 type data
func (*ManagerByDefault) ParseString ¶
func (m *ManagerByDefault) ParseString(s string) (ID, error)
Parsing snowflake id from string type data