snowflake

package
v0.0.0-...-47cc4bd Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: AGPL-3.0, AGPL-3.0-or-later Imports: 3 Imported by: 0

README

Snowflake ID generator

This is a Go implementation of Twitter Snowflake.

The most useful aspect of these IDs is they are roughly sortable and when generated at roughly the same time, should have values in close proximity to each other.

IDs

Each id will be a 64-bit number represented, structured as follows:

6  6         5         4         3         2         1
3210987654321098765432109876543210987654321098765432109876543210

ttttttttttttttttttttttttttttttttttttttttttmmmmmmmmmmssssssssssss

where

  • s (sequence) is a 12-bit integer that increments if called multiple times for the same millisecond
  • m (machine id) is a 10-bit integer representing the server id
  • t (time) is a 42-bit integer representing the current timestamp in milliseconds the number of milliseconds to have elapsed since 1491696000000 or 2017-04-09T00:00:00Z
String Encoding

The 64-bit unsigned integer is base-63 encoded using the following URL-safe characters, which are ordered according to their ASCII value.

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~

A binary sort of a list of encoded values will be correctly ordered according to the numerical representation.

Documentation

Overview

Package snowflake is copied directly from influxdb/pkg/snowflake to generate a universally unique uint64.

pkg/snowflake cannot be imported because there is no way to import github.com/influxdata/influxdb/influxql@1.1.5 for this package nor github.com/influxdata/influxql@3684fd4f35857877a7a9659e1dfea8bce026d08c for other chronograf specific logic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

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

Generator defines a snowflake generator structure.

func New

func New(machineID int) *Generator

New creates a new generator.

func (*Generator) AppendNext

func (g *Generator) AppendNext(s *[11]byte)

AppendNext appends the byte slice of a unique uint64 to s.

func (*Generator) MachineID

func (g *Generator) MachineID() int

MachineID returns the machine's ID.

func (*Generator) Next

func (g *Generator) Next() uint64

Next generates and returns a universally unique uint64.

func (*Generator) NextString

func (g *Generator) NextString() string

NextString generates and returns a universally unique uint64 as a string.

Jump to

Keyboard shortcuts

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