Documentation ¶
Index ¶
- Constants
- Variables
- func IsZero(rid RLID) bool
- func Now() uint64
- func Time(ms uint64) time.Time
- func Timestamp(t time.Time) uint64
- type LockedSequence
- type RLID
- func (id RLID) Bytes() []byte
- func (id RLID) Compare(other RLID) int
- func (id *RLID) Decode(src []byte, strict bool) error
- func (id RLID) Encode(dst []byte) error
- func (id RLID) MarshalBinary() ([]byte, error)
- func (id RLID) MarshalText() ([]byte, error)
- func (id *RLID) Scan(src interface{}) error
- func (id RLID) Sequence() uint32
- func (id *RLID) SetSequence(seq uint32) error
- func (id *RLID) SetTime(ms uint64) error
- func (id RLID) String() string
- func (id RLID) Time() uint64
- func (id *RLID) UnmarshalBinary(data []byte) error
- func (id *RLID) UnmarshalText(data []byte) error
- func (id RLID) Value() (driver.Value, error)
- type Sequence
Constants ¶
const ( // EncodedSize is the length of a text encoded RLID. EncodedSize = 16 // Encoding is the base 32 encoding alphabet used in ULID strings. Encoding = "0123456789abcdefghjkmnpqrstvwxyz" )
const MaxTime uint64 = 281474976710655
The maximum time in milliseconds that can be represented in an RLID.
Variables ¶
var ( // Returned when constructing an RLID with a timestamp that is larger than maxTime ErrOverTime = errors.New("timestamp is too far in the future to encode") // Returned when marshaling RLIDs to a buffer that is the incorrect size. ErrBufferSize = errors.New("bad buffer size: cannot encode the id") // Returned when unmarshaling RLIDs from a string that is the incorrect length. ErrDataSize = errors.New("bad data size: cannot decode the id from string") // Returned when unmarshaling RLIDs in strict mode if the string contains bad chars ErrInvalidCharacters = errors.New("invalid characters: cannot decode the id from string") // Returned when using the sql.Scanner interface if the type is incorrect. ErrScanValue = errors.New("source value must be either string or byte slice") )
var Null = RLID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
Null RLID pre-allocated for easier checking.
Functions ¶
func Now ¶
func Now() uint64
Now is a convenience function to return the current time in Unix epoch time in milliseconds. Unix epoch time is the number of milliseconds since January 1, 1970 at midnight UTC. Note that Unix epoch time will always be in the UTC timezone.
Types ¶
type LockedSequence ¶ added in v0.8.0
LockedSequence generates thread-safe totally ordered RLIDs.
func (*LockedSequence) Next ¶ added in v0.8.0
func (s *LockedSequence) Next() RLID
type RLID ¶
type RLID [10]byte
An RLID is a 10 byte totally ordered unique lexicographically sortable identifier. The components are encoded in 10 octets where each component is encoded with the MSB first (network byte order). The first 48 bits encode a timestamp at millisecond granularity and the next 32 bits encode a montonically increasing sequence number that allows for ~4.294e12 unique ids to be generated per second with timestamps that will last until around 10,000 years after 1970.
RLIDs are string encoded using Crockford's base32 (5 bits per character) meaning that each RLID is 16 characters long in string format.
func Make ¶
Make returns an RLID with the current timestamp and the given sequence number. Make will panic if the current timestamp is too far in the future for an RLID.
func MustParseStrict ¶
MustParseStrict parses anRLID from an encoded string in strict mode and panics on error
func Parse ¶
Parse an RLID from an encoded string. ErrDataSize is returned if the length of the string is different from the expected encoded length of RLIDs. Invalid encodings produce undefined RLIDs. For a version that validates the RLID, use ParseStrict.
func ParseStrict ¶
Parse an RLID from an encoded string in strict mode. ErrDataSize is returned if the length of the string is different from the expected encoded length of the RLID. ErrInvalidCharacters is returned if the encoding is not valid or would produce an undefined RLID.
func (RLID) Compare ¶
Compare returns an integer comparing id and other lexicographically. The result will be 0 if id==other, -1 if id < other, and +1 if id > other.
func (*RLID) Decode ¶
Decode the RLID from a string represented as a UTF encoded byte array. If strict is true then the decoder will validate that the string contains only valid base32 characters, but this is slightly slower if the input is known to be valid.
func (RLID) Encode ¶
Encode the RLID as a string to the given buffer. ErrBufferSize is returned if the dst is not equal to the encoded size.
func (RLID) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (RLID) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*RLID) Scan ¶
Scan implements the sql.Scanner interface. Supports scanning either a string or a byte slice.
func (*RLID) SetSequence ¶
SetSequence sets the sequence component of the RLID to the given integer.
func (*RLID) SetTime ¶
SetTime sets the time component of the RLID to the given Unix epoch time in milliseconds.
func (RLID) String ¶
String returns a lexicographically sortable string encoded RLID. 16 characters, non-standard base32 encoded
func (RLID) Time ¶
Time returns the Unix epoch time in milliseconds that is encoded in the ID. Use the top level Time function to convert into a time.Time.
func (*RLID) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (*RLID) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.