xxid

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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) FromShort

func (g *Generator) FromShort(short int64) (ID, error)

FromShort restore a short int64 id to a full unique ID.

func (*Generator) New

func (g *Generator) New() ID

New generates a globally unique ID.

func (*Generator) NewWithTime

func (g *Generator) NewWithTime(t time.Time) ID

NewWithTime generates a globally unique ID with the given time.

func (*Generator) UseFlag

func (g *Generator) UseFlag(flag uint8) *Generator

func (*Generator) UseIP

func (g *Generator) UseIP(ip net.IP) *Generator

func (*Generator) UsePort

func (g *Generator) UsePort(port uint16) *Generator

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 FromBytes

func FromBytes(b []byte) (ID, error)

FromBytes convert the byte array representation of `ID` back to `ID`.

func FromShort

func FromShort(short int64) (ID, error)

FromShort restore a short int64 id back to a full `ID`.

func FromString

func FromString(id string) (ID, error)

FromString reads an ID from its string representation.

func FromUUID

func FromUUID(uuid string) (ID, error)

FromUUID reads an ID from its UUID string representation.

func New

func New() ID

New generates a globally unique id.

func NewWithTime

func NewWithTime(t time.Time) ID

NewWithTime generates a globally unique id with the given time.

func NilID

func NilID() ID

NilID returns a zero value for `ID`.

func (ID) Addr

func (id ID) Addr() string

Addr returns the v4 ip:port format of ip and port info of the id.

func (ID) Bytes

func (id ID) Bytes() []byte

Bytes returns the byte array representation of `ID`.

func (ID) Compare

func (id ID) Compare(other ID) int

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

func (id ID) Counter() int32

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

func (id ID) Flag() uint8

Flag returns the user provided flag value. If flag is not set explicitly, it will return 0.

func (ID) IsNil

func (id ID) IsNil() bool

IsNil Returns true if this is a "nil" ID.

func (ID) Machine

func (id ID) Machine() []byte

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

func (id ID) MachineID() uint32

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

func (id ID) MachineIP() net.IP

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

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json Marshaler interface.

func (ID) MarshalText

func (id ID) MarshalText() ([]byte, error)

MarshalText implements encoding/text TextMarshaler interface.

func (ID) Pid

func (id ID) Pid() uint16

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

func (id ID) Port() uint16

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) Scan

func (id *ID) Scan(value interface{}) (err error)

Scan implements the sql.Scanner interface.

func (ID) Short

func (id ID) Short() int64

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) String

func (id ID) String() string

String returns a base62 hex lowercased with no padding representation of the id.

func (ID) Time

func (id ID) Time() time.Time

Time returns the timestamp part of the id. It's a runtime error to call this method with an invalid id.

func (ID) UUID

func (id ID) UUID() string

UUID returns a UUID form representation of the id.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(b []byte) error

UnmarshalJSON implements encoding/json Unmarshaler interface.

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(text []byte) error

UnmarshalText implements encoding/text TextUnmarshaler interface.

func (ID) Value

func (id ID) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL