idgen

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2021 License: MIT Imports: 10 Imported by: 0

README

idgen4go

A fast, snowflake-alike ID generator for go.

Usage

Get

go get github.com/thiinbit/idgen4go

Use

import idg "github.com/thiinbit/idgen4go"

// Generate a new ID
id, _ := idg.Next() // 10361665786741541

// Get the ID generation time (int64 timestamp of millis)
idTimestamp := idg.ExtractTimestamp(id) // 1580307213634

fmt.Println(id, idTimestamp)

Speed

It takes about 1.560047465s to generate 4000000 (4 million) IDs. (use MacBookPro13,2018)
The test results are as follows:

Elapsed:  1.560343318s   
Elapsed:  1.546589782s   
Elapsed:  1.56980953s   
Elapsed:  1.575468796s   
Elapsed:  1.527984553s   
Elapsed:  1.56299164s   
Elapsed:  1.544387404s   
Elapsed:  1.56981767s   
Elapsed:  1.576140736s   
Elapsed:  1.566941228s   

The generated ID example:

11054270870716420   
11054270870716421   
11054270870716424   
11054270870716425   
11054270870716428   
11054270870716429   
11054270870716432   
11054270870716433   
11054270870716436   
11054270870716437   
11054270870716440   
11054270870716441   
11054270870716444   
11054270870716445   
11054270870716448   
11054270870716449   
11054270870716452   
11054270870716453   
11054270870716456   
11054270870716457   
11054270870716460   
11054270870716461   
11054270870716464   
11054270870716465   
11054270870716468   
11054270870716469   
11054270870716472   
11054270870716473   
11054270870716476   
11054270870716477   
11054270870716480   
11054270870716481   
11054270870716484   
11054270870716485   
11054270870716488   
11054270870716489   
11054270870716492   
11054270870716493   
11054270870716496   
11054270870716497   
11054270870716500   
11054270870716501   
11054270870716504   
11054270870716505   
11054270870716508   
11054270870716509   
11054270870716512   
11054270870716513   
11054270870716516   
11054270870716517   
11054270870716520   
11054270870716521   
11054270870716524   
11054270874910721   
11054270874910720   
11054270874910725   
11054270874910724   
11054270874910729   
11054270874910728   
11054270874910733   
11054270874910732   
11054270874910737   
11054270874910736   
11054270874910741   
11054270874910740   
11054270874910745   
11054270874910744   
11054270874910749   
11054270874910748   
11054270874910753   
11054270874910752   
11054270874910757   
11054270874910756   
11054270874910761   
11054270874910760   
11054270874910765   
11054270874910764   
11054270874910769   
11054270874910768   
11054270874910773   
11054270874910772   
11054270874910777   
11054270874910776   
11054270874910781   
11054270874910780   
11054270874910785   
11054270874910784   
11054270874910789   
11054270874910788   
11054270874910793   
11054270874910792   
11054270874910797   
11054270874910796   
11054270874910801   
11054270874910800   
11054270874910805   
11054270874910804   
11054270874910809   
11054270874910808   
11054270874910813   

Documentation

Index

Constants

View Source
const (
	Decimal = 10
)

Variables

This section is empty.

Functions

func DBName

func DBName(dbPrefix string, idx int) string

func DBTBIndex

func DBTBIndex(id int64, dbSize int, tbSize int) (int, int)

func DBTBName

func DBTBName(id int64, dbPrefix string, tbPrefix string, dbSize int, tbSize int) (string, string)

func DropSeqDB

func DropSeqDB()

func Exec

func Exec(runSql string, args ...interface{}) (re sql.Result, err error)

func ExtractMachine

func ExtractMachine(seq int64) int

ExtractMachineID from ID

func ExtractTimestamp

func ExtractTimestamp(seq int64) int64

ExtractTimestamp from ID

func InitSeqDB

func InitSeqDB()

func Mod

func Mod(id int64, m int64) (int64, error)

Mod return the id's timestamp mod m (m in the range of hashMask)

func ModUUID

func ModUUID(uuid string, m int64) (int64, error)

ModUUID return uuid mod m

func Next

func Next() (int64, error)

Next ID number

func NextSeq

func NextSeq(id int64) (int64, error)

NextSeqBySection return next sequence of the id

func Query

func Query(runSql string, args ...interface{}) (rows *sql.Rows, err error)

func QueryRow

func QueryRow(runSql string, args ...interface{}) (row *sql.Row, err error)

func SetMachineID

func SetMachineID(mID int)

SetMachineID (only need set once on startup)

func TBName

func TBName(dbName string, tbPrefix string, tbIdx int) string

TBName return dbName.tableName

func UUID

func UUID(id int64) string

UUID return a UUID with the same mod result as id

Types

This section is empty.

Jump to

Keyboard shortcuts

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