Skip to content

Latest commit

 

History

History
229 lines (129 loc) · 5.05 KB

sequence.md

File metadata and controls

229 lines (129 loc) · 5.05 KB

sequence

import "github.com/andy2046/gopie/pkg/sequence"

Package sequence implements Iceflake sequence generator interface.

Iceflake is the interface for snowflake similar sequence generator.

Iceflake algorithm:

+-------+--------------------+----------+
| sign  | delta milliseconds | sequence |
+-------+--------------------+----------+
| 1 bit | 63-n bits          | n bits   |

sequence (n bits)
The last custom n bits, represents sequence within the one millisecond.

delta milliseconds (63-n bits)
The next 63-n bits, represents delta milliseconds since a custom epoch.

iceflake.go memflake.go memsequence.go sequencer.go

type Iceflake interface {
    Sequencer
    // StartTime defines the time since which
    // the Iceflake time is defined as the elapsed time.
    StartTime() time.Time
    // BitLenSequence defines the bit length of sequence number,
    // and the bit length of time is 63 - BitLenSequence().
    BitLenSequence() uint8
}

Iceflake is the interface for snowflake similar sequence generator.

type MemFlake struct {
    sync.Mutex
    // contains filtered or unexported fields
}

MemFlake is an implementation of in-memory Iceflake.

func NewMemFlake(startTime time.Time, bitLenSequence uint8, machineID uint64) *MemFlake

NewMemFlake creates a MemFlake.

func (*MemFlake) BitLenSequence

func (m *MemFlake) BitLenSequence() uint8

BitLenSequence ...

func (*MemFlake) MachineID

func (m *MemFlake) MachineID() uint64

MachineID ...

func (*MemFlake) Next

func (m *MemFlake) Next() (uint64, error)

Next ...

func (*MemFlake) NextN

func (m *MemFlake) NextN(n int) (uint64, error)

NextN ...

func (*MemFlake) StartTime

func (m *MemFlake) StartTime() time.Time

StartTime ...

type MemSeq struct {
    sync.Mutex
    // contains filtered or unexported fields
}

MemSeq is an implementation of in-memory Sequencer.

func NewMemSeq(machineID uint64) *MemSeq

NewMemSeq creates a MemSeq.

func (*MemSeq) MachineID

func (m *MemSeq) MachineID() uint64

MachineID ...

func (*MemSeq) Next

func (m *MemSeq) Next() (uint64, error)

Next ...

func (*MemSeq) NextN

func (m *MemSeq) NextN(n int) (uint64, error)

NextN ...

type Sequencer interface {
    // Next returns the next sequence.
    Next() (uint64, error)
    // NextN reserves the next `n` sequences and returns the first one,
    // `n` should not be less than 1.
    NextN(n int) (uint64, error)
    // MachineID returns the unique ID of the instance.
    MachineID() uint64
}

Sequencer is the interface for sequence generator.


Generated by godoc2md