sne

package
v0.0.0-...-dd0d1ad Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2016 License: MIT Imports: 5 Imported by: 0

README

sne

sne: snowflake like IDs with millisecond time resolution, 11 bit sequence, 56 bit key, and 20 bit secondary key.

The timestamp is from a custom epoch: 1/1/2016 00:00:00.

The sequence number is 11 bits and starts at a random number and increases with each snowflake and rolls over. It should not be relied on for anything.

A 20-bit secondary key, SID, element is provided.

The key, or ID, is 7 bytes and no assumptions are made about its contents or layout. A snowflake generator is used for a single ID and SID combination.

128 bit

128 bit snowflakes have the following layout:

bits    
 0-40     Timestamp, in milliseconds
41-51     Sequence number
52-71     SID: secondary ID
72-127    ID

Documentation

Overview

Package sne generates snowflake like 128 bit ids that uses milliseconds for the time element. A sne Flake has a 41 bit time field, a 11 bit sequence, a 56 bit ID, and a 20 bit secondary id. The time field holds milliseconds since 1/1/2016 00:00:00. The sequence starts at a random position and increments with each Snowflake. If there is the possibility of needing more than 2,048,000 Flakes per second for a given ID and secondary ID combination this package should not be used.

Data layout:

  0-40    time, in milliseconds: 69 years
 41-51    sequence; for collision prevention
 52-71    secondary id
72-127    primary ID

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Flake

type Flake [16]byte

Flake is the type for a 128 bit snowflake.

func (*Flake) ID

func (f *Flake) ID() []byte

ID returns the Flake's ID as a []byte. A snowflake's ID is the last 8 bytes. No assumptios are made about either the contents of those bytes or their layout, that is left up to the user.

func (*Flake) SID

func (f *Flake) SID() int32

SID returns the Flake's secondary ID. The secondary ID is 20 bits.

func (*Flake) Time

func (f *Flake) Time() int64

Time returns the Flake's timestamp as an int64. The timestamp has microsecond resolution.

type Generator

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

Generator creates snowflakes for a given id

func New

func New(id []byte, id2 int32) Generator

New returns an initialized generator. If the passed byte slice's length is greater than 7 bytes, the first 7 bytes will be used for the generator's id. If the passed byte slice's length is less than 7 bytes, the id will be left- padded with 0x00. The id2 parameter is the secondary id: only the right- most 20 bits are used. The generator's sequence is initialized with a random int within [0, 2^11).

func (*Generator) ID

func (g *Generator) ID() []byte

ID returns the ID bytes associated with this generator.

func (*Generator) SID

func (g *Generator) SID() int32

SID returns the secondary ID associated with this generator.

func (*Generator) Snowflake

func (g *Generator) Snowflake() Flake

Snowflake generates an Flake from the current time and next sequence value.

Jump to

Keyboard shortcuts

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