Documentation
¶
Index ¶
- Constants
- Variables
- func Compare(a, b KSID) int
- func IsSorted(ids []KSID) bool
- func SetRand(r io.Reader)
- func Sort(ids []KSID)
- type CompressedSet
- type CompressedSetIter
- type Config
- type ConfigFn
- type KSID
- func (i KSID) Append(b []byte) []byte
- func (i KSID) Bytes() []byte
- func (i KSID) Get() interface{}
- func (i KSID) IsNil() bool
- func (i KSID) MarshalBinary() ([]byte, error)
- func (i KSID) MarshalText() ([]byte, error)
- func (i KSID) Next() KSID
- func (i KSID) Payload() []byte
- func (i KSID) Prev() KSID
- func (i *KSID) Scan(src interface{}) error
- func (i *KSID) Set(s string) error
- func (i KSID) String() string
- func (i KSID) Time() time.Time
- func (i KSID) Timestamp() uint32
- func (i *KSID) UnmarshalBinary(b []byte) error
- func (i *KSID) UnmarshalText(b []byte) error
- func (i KSID) Value() (driver.Value, error)
- type Sequence
Constants ¶
const ( // EpochTimestamp KSID's epoch starts more recently so that the 32-bit number space gives a // significantly higher useful lifetime of around 136 years from March 2017. // This number (14e8) was picked to be easy to remember. EpochTimestamp int64 = 1400000000 // TimestampSize is a uint32 TimestampSize = 4 // PayloadSize is 16-bytes PayloadSize = 16 // Size KSUIDs are 20 bytes when binary encoded Size = TimestampSize + PayloadSize )
Variables ¶
var FastRander = newRBG()
FastRander is an io.Reader that uses math/rand and is optimized for generating 16 bytes KSID payloads. It is intended to be used as a performance improvements for programs that have no need for cryptographically secure KSUIDs and are generating a lot of them.
Functions ¶
Types ¶
type CompressedSet ¶
type CompressedSet []byte
CompressedSet is an immutable data type which stores a set of KSUIDs.
func AppendCompressed ¶
func AppendCompressed(set []byte, ids ...KSID) CompressedSet
AppendCompressed uses the given byte slice as pre-allocated storage space to build a KSID set.
Note that the set uses a compression technique to store the KSUIDs, so the resulting length is not 20 x len(ids). The rule of thumb here is for the given byte slice to reserve the amount of memory that the application would be OK to waste.
func Compress ¶
func Compress(ids ...KSID) CompressedSet
Compress creates and returns a compressed set of KSUIDs from the list given as arguments.
func (CompressedSet) GoString ¶
func (set CompressedSet) GoString() string
String satisfies the fmt.GoStringer interface, returns a Go representation of the set.
func (CompressedSet) Iter ¶
func (set CompressedSet) Iter() CompressedSetIter
Iter returns an iterator that produces all KSUIDs in the set.
func (CompressedSet) String ¶
func (set CompressedSet) String() string
String satisfies the fmt.Stringer interface, returns a human-readable string representation of the set.
type CompressedSetIter ¶
type CompressedSetIter struct { // KSUID is modified by calls to the Next method to hold the KSUID loaded // by the iterator. KSUID KSID // contains filtered or unexported fields }
CompressedSetIter is an iterator type returned by Set.Iter to produce the list of KSUIDs stored in a set.
Here's is how the iterator type is commonly used:
for it := set.Iter(); it.Next(); { id := it.KSID // ... }
CompressedSetIter values are not safe to use concurrently from multiple goroutines.
func (*CompressedSetIter) Next ¶
func (it *CompressedSetIter) Next() bool
Next moves the iterator forward, returning true if there a KSID was found, or false if the iterator as reached the end of the set it was created from.
type KSID ¶
KSID are 20 bytes:
00-03 byte: uint32 be UTC timestamp with custom epoch 04-19 byte: random "payload"
func (KSID) Append ¶
Append appends the string representation of i to b, returning a slice to a potentially larger memory area.
func (KSID) Get ¶
func (i KSID) Get() interface{}
Get satisfies the flag.Getter interface, making it possible to use KSUIDs as part of the command line options of a program.
func (KSID) MarshalBinary ¶
func (KSID) MarshalText ¶
func (*KSID) Scan ¶
Scan implements the sql.Scanner interface. It supports converting from string, []byte, or nil into a KSID value. Attempting to convert from another type will return an error.
func (*KSID) Set ¶
Set satisfies the flag.Value interface, making it possible to use KSUIDs as part of the command line options of a program.
func (KSID) Timestamp ¶
Timestamp The timestamp portion of the ID as a bare integer which is uncorrected for KSID's special epoch.
func (*KSID) UnmarshalBinary ¶
func (*KSID) UnmarshalText ¶
type Sequence ¶
type Sequence struct { // The seed is used as base for the KSID generator, all generated KSUIDs // share the same leading 18 bytes of the seed. Seed KSID // contains filtered or unexported fields }
Sequence is a KSID generator which produces a sequence of ordered KSUIDs from a seed.
Up to 65536 KSUIDs can be generated by for a single seed.
A typical usage of a Sequence looks like this:
seq := ksid.Sequence{ Seed: ksid.New(), } id, err := seq.Next()
Sequence values are not safe to use concurrently from multiple goroutines.