id

package
v0.3.679 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2019 License: Apache-2.0 Imports: 5 Imported by: 28

Documentation

Index

Constants

View Source
const EncodedSize = 26

EncodedSize is the length of a text encoded Id.

View Source
const Encoding = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"

Encoding is the base 32 encoding alphabet used in Id strings.

Variables

This section is empty.

Functions

func EncodeDescending

func EncodeDescending(src string) string

EncodeDescending returns a lexicographically sortable descending encoding of a given id, e.g. 000 -> ZZZ, which allows reversing the sort order when stored contiguously since ids are lexicographically sortable. The returned string will be of len(src), and assumes src is from the base32 crockford alphabet, otherwise using 0xFF.

func SetMachineId

func SetMachineId(ID uint64)

SetMachineId may only be called by one thread before any id generation is done. It must be set if multiple machines are generating ids in order to avoid collisions. Only the least significant 48 bits are used.

func SetMachineIdHost

func SetMachineIdHost(addr net.IP, port uint16)

SetMachineIdHost is a convenience wrapper to hide bit twiddling of calling SetMachineId, it has the same constraints as SetMachineId with an addition that net.IP must be a ipv4 address.

Types

type Id

type Id [16]byte

func New

func New() Id

New will generate a new Id for use. New is safe to be called from concurrent threads. SetMachineId should be called once before any calls to New are made. 2^32 calls to New per millisecond will be unique, provided machine id is seeded correctly across machines.

binary format: [ [ 48 bits time ] [ 48 bits machineID ] [ 32 bits counter ] ]

Ids are sortable within (not between, thanks to clocks) each machine, with a modified base32 encoding exposed for convenience in API usage.

func NewWithTime

func NewWithTime(t time.Time) Id

NewWithTime returns an id that uses the milliseconds from the given time. New is identical to NewWithTime(time.Now())

func (Id) MarshalBinary

func (id Id) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface by returning the Id as a byte slice.

func (Id) MarshalBinaryTo

func (id Id) MarshalBinaryTo(dst []byte) error

MarshalBinaryTo writes the binary encoding of the Id to the given buffer. ErrBufferSize is returned when the len(dst) != 16.

func (Id) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface by returning the string encoded Id.

func (Id) MarshalTextTo

func (id Id) MarshalTextTo(dst []byte) error

MarshalTextTo writes the Id as a string to the given buffer. an error is returned when the len(dst) != 26.

func (Id) String

func (id Id) String() string

String returns a lexicographically sortable string encoded Id (26 characters, non-standard base 32) e.g. 01AN4Z07BY79KA1307SR9X4MV3 Format: ttttttttttmmmmmmmmmmeeeeee where t is time, m is machine id and c is a counter

func (*Id) UnmarshalBinary

func (id *Id) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface by copying the passed data and converting it to an Id. ErrDataSize is returned if the data length is different from Id length.

func (*Id) UnmarshalText

func (id *Id) UnmarshalText(v []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface by parsing the data as string encoded Id.

an error is returned if the len(v) is different from an encoded Id's length. Invalid encodings produce undefined Ids.

Jump to

Keyboard shortcuts

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