olaf

package module
v0.0.0-...-10203e4 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2019 License: MIT Imports: 6 Imported by: 0

README

Since 10-Apr-2019: Olaf has been merged as a sub-module of btnguyen2k/consu

Olaf

Go Report Card cover.run GoDoc Mentioned in Awesome Go

Golang implementation of Twitter Snowflake.

Getting Started

Install Package
go get github.com/btnguyen2k/olaf
Usage
package main

import (
    "fmt"
    "github.com/btnguyen2k/olaf"
)

func main() {
    // use default epoch
    o := olaf.NewOlaf(1981)

    //use custom epoch (note: epoch is in milliseconds)
    //o := olaf.NewOlafWithEpoch(103, 1546543604123)

    id64 := o.Id64()
    id64Hex := o.Id64Hex()
    id64Ascii := o.Id64Ascii()
    fmt.Println("ID 64-bit (int)   : ", id64, " / Timestamp: ", o.ExtractTime64(id64))
    fmt.Println("ID 64-bit (hex)   : ", id64Hex, " / Timestamp: ", o.ExtractTime64Hex(id64Hex))
    fmt.Println("ID 64-bit (ascii) : ", id64Ascii, " / Timestamp: ", o.ExtractTime64Ascii(id64Ascii))

    id128 := o.Id128()
    id128Hex := o.Id128Hex()
    id128Ascii := o.Id128Ascii()
    fmt.Println("ID 128-bit (int)  : ", id128.String(), " / Timestamp: ", o.ExtractTime128(id128))
    fmt.Println("ID 128-bit (hex)  : ", id128Hex, " / Timestamp: ", o.ExtractTime128Hex(id128Hex))
    fmt.Println("ID 128-bit (ascii): ", id128Ascii, " / Timestamp: ", o.ExtractTime128Ascii(id128Ascii))
}

Document - GoDoc

See GoDoc.

History

Current version: 0.1.0.

2019-01-03 - v0.1.0

First release:

  • Generate 64-bit and 128-bit IDs:
    • Support formats: integer, hex-string (base 16) and ascii-string (base 36).
    • Custom epoch (64-bit ID only).
  • Extract the time metadata from generated ID.

Documentation

Overview

Package olaf provides methods to generate unique ID using Twitter Snowflake algorithm. #author Thanh Nguyen <btnguyen2k@gmail.com> #version 0.1.0

Index

Constants

View Source
const Epoch int64 = 1546300800000

Epoch is set to 2019-01-01 00:00:00 UTC. You may customize this to set a different epoch for your application.

Variables

This section is empty.

Functions

func UnixMilliseconds

func UnixMilliseconds() int64

UnixMilliseconds returns current UNIX timestamp in milliseconds.

func WaitTillNextMillisec

func WaitTillNextMillisec(currentMillisec int64) int64

WaitTillNextMillisec waits till clock moves to the next millisecond. Returns the "next" millisecond.

Types

type Olaf

type Olaf struct {
	NodeID int64 // original node-id

	Epoch      int64 // Twitter snowflake's epoch
	SequenceId int64 // Twitter snowflake's sequence-id
	Timestamp  int64 // last 'touch' UNIX timestamp in milliseconds
	// contains filtered or unexported fields
}

Olaf wraps configurations for Twitter Snowflake IDs.

func NewOlaf

func NewOlaf(nodeId int64) *Olaf

NewOlaf creates a new Olaf with default epoch.

func NewOlafWithEpoch

func NewOlafWithEpoch(nodeId int64, epoch int64) *Olaf

NewOlafWithEpoch creates a new Olaf with custom epoch.

func (*Olaf) ExtractTime128

func (o *Olaf) ExtractTime128(id128 *big.Int) time.Time

ExtractTime128 extracts time metadata from a 128-bit id.

func (*Olaf) ExtractTime128Ascii

func (o *Olaf) ExtractTime128Ascii(id128Ascii string) time.Time

ExtractTime128Ascii extracts time metadata from a 128-bit id in ascii (base 36) format.

func (*Olaf) ExtractTime128Hex

func (o *Olaf) ExtractTime128Hex(id128Hex string) time.Time

ExtractTime128Hex extracts time metadata from a 128-bit id in hex (base 16) format.

func (*Olaf) ExtractTime64

func (o *Olaf) ExtractTime64(id64 uint64) time.Time

ExtractTime64 extracts time metadata from a 64-bit id.

func (*Olaf) ExtractTime64Ascii

func (o *Olaf) ExtractTime64Ascii(id64Ascii string) time.Time

ExtractTime64Ascii extracts time metadata from a 64-bit id in ascii (base 36) format.

func (*Olaf) ExtractTime64Hex

func (o *Olaf) ExtractTime64Hex(id64Hex string) time.Time

ExtractTime64Hex extracts time metadata from a 64-bit id in hex (base 16) format.

func (*Olaf) Id128

func (o *Olaf) Id128() *big.Int

Id128 generates a 128-bit id.

func (*Olaf) Id128Ascii

func (o *Olaf) Id128Ascii() string

Id128Ascii generates a 128-bit id as an ascii (base 36) string.

func (*Olaf) Id128Hex

func (o *Olaf) Id128Hex() string

Id128Hex generates a 128-bit id as a hex (base 16) string.

func (*Olaf) Id64

func (o *Olaf) Id64() uint64

Id64 generates a 64-bit id.

func (*Olaf) Id64Ascii

func (o *Olaf) Id64Ascii() string

Id64Ascii generates a 64-bit id as an ascii string (base 36).

func (*Olaf) Id64Hex

func (o *Olaf) Id64Hex() string

Id64Hex generates a 64-bit id as a hex (base 16) string.

Jump to

Keyboard shortcuts

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