Documentation ¶
Index ¶
- Variables
- func Sort(ids []ID)
- func SortByMachine(ids []ID)
- type Generator
- type ID
- func (id ID) Addr() string
- func (id ID) Bytes() []byte
- func (id ID) Compare(other ID) int
- func (id ID) Counter() int32
- func (id ID) Flag() uint8
- func (id ID) IsNil() bool
- func (id ID) Machine() []byte
- func (id ID) MachineID() uint32
- func (id ID) MachineIP() net.IP
- func (id ID) MarshalJSON() ([]byte, error)
- func (id ID) MarshalText() ([]byte, error)
- func (id ID) Pid() uint16
- func (id ID) Port() uint16
- func (id *ID) Scan(value interface{}) (err error)
- func (id ID) Short() int64
- func (id ID) String() string
- func (id ID) Time() time.Time
- func (id ID) UUID() string
- func (id *ID) UnmarshalJSON(b []byte) error
- func (id *ID) UnmarshalText(text []byte) error
- func (id ID) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidID is returned when trying to unmarshal an invalid id. ErrInvalidID = errors.New("xxid: invalid ID") )
Functions ¶
func Sort ¶
func Sort(ids []ID)
Sort sorts an array of IDs in-place. It works by wrapping `[]ID` and use `sort.Sort`.
func SortByMachine ¶
func SortByMachine(ids []ID)
SortByMachine sorts an array of IDs in-place using MachineID and Pid lexicographically. It works by wrapping `[]ID` and use `sort.Sort`.
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
func NewGenerator ¶
func NewGenerator() *Generator
NewGenerator makes a new generator initialized with same machineID and pid as the package's default generator, and new random flag and counter, this is useful to specify IP, port and flag instead of the default machineID, process id and random flag. For general purpose without provided IP, port or flag, please just use the package's default functions `New` and `NewWithTime`.
func (*Generator) NewWithTime ¶
NewWithTime generates a globally unique ID with the given time.
type ID ¶
type ID [rawLen]byte
ID represents a unique request id, consists of - 4 bytes seconds since epoch 15e8 - 1 byte random or user specified flag - 4 bytes machine id or user specified ip address - 2 bytes pid or user specified port number - 4 bytes counter (low 31 bits)
func FromString ¶
FromString reads an ID from its string representation.
func NewWithTime ¶
NewWithTime generates a globally unique id with the given time.
func (ID) Compare ¶
Compare returns an integer comparing two IDs. It behaves just like `bytes.Compare`. The result will be 0 if two IDs are identical, -1 if current id is less than the other one, and 1 if current id is greater than the other.
func (ID) Counter ¶
Counter returns the incrementing value part of the id. It's a runtime error to call this method with an invalid id.
func (ID) Flag ¶
Flag returns the user provided flag value. If flag is not set explicitly, it will return 0.
func (ID) Machine ¶
Machine returns the 4-byte machine id part of the id. It's a runtime error to call this method with an invalid id.
func (ID) MachineID ¶
MachineID returns the uint32 representation of the machine id part. It's a runtime error to call this method with an invalid id.
func (ID) MachineIP ¶
MachineIP returns the IP representation of the machine id part when used with IP/PORT mode. It's a runtime error to call this method with an invalid id.
func (ID) MarshalJSON ¶
MarshalJSON implements encoding/json Marshaler interface.
func (ID) MarshalText ¶
MarshalText implements encoding/text TextMarshaler interface.
func (ID) Pid ¶
Pid returns the process id part of the id. It's a runtime error to call this method with an invalid id.
func (ID) Port ¶
Port returns the port part of the id when used with IP/PORT mode. It's a runtime error to call this method with an invalid id.
func (ID) Short ¶
Short returns the 63 bits int64 representation of the ID consisting of timestamp and counter, the first bit is always 0, then the 32 bits timestamp, and the 31 bits counter.
func (ID) Time ¶
Time returns the timestamp part of the id. It's a runtime error to call this method with an invalid id.
func (*ID) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json Unmarshaler interface.
func (*ID) UnmarshalText ¶
UnmarshalText implements encoding/text TextUnmarshaler interface.