idg

package module
v0.0.0-...-291854a Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2017 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

idg (ID generator)

idg is a simple ID generation helper. Each ID is indexed and assigned a timestamp upon creation.

Usage

package main

import (
	"fmt"
	"time"

	"github.com/PathDNA/idg"
)

func main() {
	var (
		idx uint64
		t   time.Time
		err error
	)

	// Initialize id generator
	idg := idg.New(0)
	// Get next ID
	id := idg.Next()
	// Get string representation of ID
	str := id.String()
	// Get index of ID
	if idx, err = id.Index(); err != nil {
		panic("Error getting index: " + err.Error())
	}
	// Get time of ID
	if t, err = id.Time(); err != nil {
		panic("Error getting time: " + err.Error())
	}
	// Display ID information
	fmt.Printf("ID\nString: %s\nIndex: %d\nTime: %v\n", str, idx, t)
}

Benchmarks

## idg
BenchmarkGenerationIDG-16             20000000    96.4 ns/op    0 B/op    0 allocs/op
BenchmarkGenerationParallelIDG-16     30000000    36.6 ns/op    0 B/op    0 allocs/op

## missionMeteora/uuid
BenchmarkGenerationUUID-16            10000000    120 ns/op     0 B/op    0 allocs/op
BenchmarkGenerationParallelUUID-16    5000000     291 ns/op     0 B/op    0 allocs/op

Documentation

Index

Constants

View Source
const ErrEmptyID = errors.Error("cannot perform action on nil ID")

ErrEmptyID is returned when an action is performed on a nil instance of ID

View Source
const (
	// ErrInvalidLength is returned when an ID is not 16 bytes in length
	ErrInvalidLength = errors.Error("invalid length")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ID

type ID [16]byte

ID represents an id

func Parse

func Parse(in string) (id ID, err error)

Parse will parse a string id

func (*ID) Bytes

func (id *ID) Bytes() (out []byte)

Bytes will return the byteslice representation Note: This function is unsafe and can change the underlying array Please.. read only!

func (*ID) Index

func (id *ID) Index() (idx uint64, err error)

Index will return the index of an ID

func (*ID) IsEmpty

func (id *ID) IsEmpty() (empty bool)

IsEmpty will return if an ID is empty

func (*ID) MarshalJSON

func (id *ID) MarshalJSON() (out []byte, err error)

MarshalJSON is a JSON encoding helper func

func (*ID) String

func (id *ID) String() (out string)

String will return a string representation Note: This is referenced as a non-pointer so it can be called directly from a struct utilizing the non-pointer value of ID

func (*ID) Time

func (id *ID) Time() (t time.Time, err error)

Time will return the time.Time of an ID

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(in []byte) (err error)

UnmarshalJSON is a JSON decoding helper func

type ID32

type ID32 [8]byte

ID32 represents a 32-bit id

func (*ID32) Bytes

func (id *ID32) Bytes() (out []byte)

Bytes will return the byteslice representation Note: This function is unsafe and can change the underlying array Please.. read only!

func (*ID32) Index

func (id *ID32) Index() (idx uint32, err error)

Index will return the index of an ID

func (*ID32) IsEmpty

func (id *ID32) IsEmpty() (empty bool)

IsEmpty will return if an ID is empty

func (*ID32) MarshalJSON

func (id *ID32) MarshalJSON() (out []byte, err error)

MarshalJSON is a JSON encoding helper func

func (*ID32) String

func (id *ID32) String() (out string)

String will return a string representation Note: This is referenced as a non-pointer so it can be called directly from a struct utilizing the non-pointer value of ID

func (*ID32) Time

func (id *ID32) Time() (t time.Time, err error)

Time will return the time.Time of an ID

func (*ID32) UnmarshalJSON

func (id *ID32) UnmarshalJSON(in []byte) (err error)

UnmarshalJSON is a JSON decoding helper func

type IDG

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

IDG is an non-persistent atomic ID generator

func New

func New(idx uint64) (idg IDG)

New will return a new ID generator

func (*IDG) Next

func (i *IDG) Next() (id ID)

Next will return the next id

func (*IDG) Next32

func (i *IDG) Next32() (id ID32)

Next32 will return the next 32-bit id

type PIDG

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

PIDG is an non-persistent atomic ID generator

func NewPersistent

func NewPersistent(key, dir string) (pidg *PIDG, err error)

NewPersistent will return a new ID generator

func (*PIDG) Close

func (p *PIDG) Close() (err error)

Close will close the internal file

func (*PIDG) Next

func (p *PIDG) Next() (id ID, err error)

Next will return the next id

func (*PIDG) Next32

func (p *PIDG) Next32() (id ID32, err error)

Next32 will return the next 32-bit id

type TIDG

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

TIDG is a persistent turtleDB-based ID generator

func NewTIDG

func NewTIDG(key string, fm turtleDB.FuncsMap) (t TIDG)

NewTIDG will return a new turtleDB-backed ID generator

func (*TIDG) Next

func (t *TIDG) Next(txn turtleDB.Txn) (id ID, err error)

Next will return the next id

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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