rid

package module
v0.0.0-...-66830c4 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2018 License: MIT Imports: 14 Imported by: 1

README

Unique ID Generator

Use 6 byte for submission time (UTC). You can use for SQL.

! Limit values to 2310 year.

Identifier structure:
  • 1-byte hardware address CRC4 ID

  • 2-byte process id

  • 4-byte value representing the seconds since the 2018-01-01 epoch

  • 3-byte counter (random, or manual)

  • 2-byte nanoseconds (the first 2 bytes of the four byte value)

Install

go get github.com/itrabbit/rid

Usage

fmt.Println(rid.New())
// -> t9eti00dzt0e0hkibo9g

fmt.Println(rid.New().String())
// -> t9ew400dzt1db4okkw80

fmt.Println(rid.New().NumeralString())
// -> 218093205000013254195242051055202176

Custom source:

src := rid.NewSource()
// src.Seed(0) <- Set start counter by custom value

id := src.NewID()

Get embedded info:

id := rid.New()

id.Mid()    // Hardware Address CRC ID
id.Pid()    // Process Pid
id.Time()   // With an accuracy of up to 6 bytes (from 8)

id.Counter() 

Copyright (c) 2018 IT Rabbit.

Original encode/decode algorithm: Copyright (c) 2015 Olivier Poitrey rs@dailymotion.com. The same MIT license applies.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidID = errors.New("invalid ID")
)

Functions

func Mid

func Mid() uint8

func Pid

func Pid() uint16

Types

type ID

type ID [rawLength]byte

1-byte hardware address CRC4 ID 2-byte process id 4-byte value representing the seconds since the Unix epoch 3-byte counter 2-byte nanoseconds (the first 2 bytes of the four byte value)

func FromString

func FromString(id string) (ID, error)

func New

func New() ID

func (ID) Counter

func (id ID) Counter() uint32

func (ID) IsNil

func (id ID) IsNil() bool

func (ID) MarshalText

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

func (ID) Mid

func (id ID) Mid() uint8

func (ID) NumeralString

func (id ID) NumeralString() string

func (ID) Pid

func (id ID) Pid() uint16

func (*ID) Scan

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

func (ID) SqlType

func (ID) SqlType(dialect string, size int, settings map[string]string) string

func (ID) String

func (id ID) String() string

func (ID) Time

func (id ID) Time() time.Time

func (*ID) UnmarshalText

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

func (ID) Value

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

type Source

type Source struct {
	// contains filtered or unexported fields
}

func GlobalSource

func GlobalSource() *Source

func NewSource

func NewSource() *Source

func (Source) Counter

func (s Source) Counter() uint32

func (*Source) NewID

func (s *Source) NewID() ID

func (*Source) Seed

func (s *Source) Seed(pos uint32)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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