parent
651ee16aef
commit
ecec124fdb
@ -0,0 +1,55 @@
|
||||
package gosnowflake
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
workerBits uint8 = 10
|
||||
numberBits uint8 = 12
|
||||
workerMax int64 = -1 ^ (-1 << workerBits)
|
||||
numberMax int64 = -1 ^ (-1 << numberBits)
|
||||
timeShift uint8 = workerBits + numberBits
|
||||
workerShift uint8 = numberBits
|
||||
startTime int64 = 1525705533000 // 如果在程序跑了一段时间修改了epoch这个值 可能会导致生成相同的ID
|
||||
)
|
||||
|
||||
type Worker struct {
|
||||
mu sync.Mutex
|
||||
timestamp int64
|
||||
workerId int64
|
||||
number int64
|
||||
}
|
||||
|
||||
func NewWorker(workerId int64) (*Worker, error) {
|
||||
if workerId < 0 || workerId > workerMax {
|
||||
return nil, errors.New("worker ID excess of quantity")
|
||||
}
|
||||
// 生成一个新节点
|
||||
return &Worker{
|
||||
timestamp: 0,
|
||||
workerId: workerId,
|
||||
number: 0,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (w *Worker) GetId() int64 {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
now := time.Now().UnixNano() / 1e6
|
||||
if w.timestamp == now {
|
||||
w.number++
|
||||
if w.number > numberMax {
|
||||
for now <= w.timestamp {
|
||||
now = time.Now().UnixNano() / 1e6
|
||||
}
|
||||
}
|
||||
} else {
|
||||
w.number = 0
|
||||
w.timestamp = now
|
||||
}
|
||||
ID := (now-startTime)<<timeShift | (w.workerId << workerShift) | (w.number)
|
||||
return ID
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
package only
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go.dtapp.net/library/utils/gorandom"
|
||||
"go.dtapp.net/library/utils/gotime"
|
||||
)
|
||||
|
||||
// GenerateId 生成18一编号
|
||||
func GenerateId(customId string) string {
|
||||
one, err := generateIdOne(customId)
|
||||
if err == nil {
|
||||
return one
|
||||
}
|
||||
two, err := generateIdTwo(customId)
|
||||
if err == nil {
|
||||
return two
|
||||
}
|
||||
three, err := generateIdThree(customId)
|
||||
if err == nil {
|
||||
return three
|
||||
}
|
||||
four, err := generateIdFour(customId)
|
||||
return four
|
||||
}
|
||||
|
||||
// 生成18位时间[年月日时分]唯一编号
|
||||
func generateIdOne(customId string) (string, error) {
|
||||
|
||||
var (
|
||||
randomLength = 4 // 随机数据长度
|
||||
customIdLength = len(customId) // 自定义长度
|
||||
)
|
||||
|
||||
const (
|
||||
dataLength = 18 // 默认数据长度
|
||||
dateFormat = "200601021504" // 时间格式
|
||||
dateFormatLength = len(dateFormat) // 时间格式长度
|
||||
)
|
||||
|
||||
// 重新计算随机数据长度
|
||||
randomLength = dataLength - (dateFormatLength + customIdLength)
|
||||
|
||||
if dateFormatLength+customIdLength > dataLength {
|
||||
return "", errors.New("超出长度")
|
||||
} else if dateFormatLength+customIdLength == dataLength {
|
||||
return "", errors.New("无法使用时间方法")
|
||||
} else if randomLength < 3 {
|
||||
return "", errors.New("自定义的数据过长")
|
||||
}
|
||||
return fmt.Sprintf("%v%s%s", customId, gotime.Current().SetFormat(dateFormat), gorandom.Numeric(randomLength)), nil
|
||||
}
|
||||
|
||||
// 生成18位时间[年月日时]唯一编号
|
||||
func generateIdTwo(customId string) (string, error) {
|
||||
|
||||
var (
|
||||
randomLength = 4 // 随机数据长度
|
||||
customIdLength = len(customId) // 自定义长度
|
||||
)
|
||||
|
||||
const (
|
||||
dataLength = 18 // 默认数据长度
|
||||
dateFormat = "2006010215" // 时间格式
|
||||
dateFormatLength = len(dateFormat) // 时间格式长度
|
||||
)
|
||||
|
||||
// 重新计算随机数据长度
|
||||
randomLength = dataLength - (dateFormatLength + customIdLength)
|
||||
|
||||
if dateFormatLength+customIdLength > dataLength {
|
||||
return "", errors.New("超出长度")
|
||||
} else if dateFormatLength+customIdLength == dataLength {
|
||||
return "", errors.New("无法使用时间方法")
|
||||
} else if randomLength < 3 {
|
||||
return "", errors.New("自定义的数据过长")
|
||||
}
|
||||
return fmt.Sprintf("%v%s%s", customId, gotime.Current().SetFormat(dateFormat), gorandom.Numeric(randomLength)), nil
|
||||
}
|
||||
|
||||
// 生成18位时间[年月日]唯一编号
|
||||
func generateIdThree(customId string) (string, error) {
|
||||
|
||||
var (
|
||||
randomLength = 4 // 随机数据长度
|
||||
customIdLength = len(customId) // 自定义长度
|
||||
)
|
||||
|
||||
const (
|
||||
dataLength = 18 // 默认数据长度
|
||||
dateFormat = "20060102" // 时间格式
|
||||
dateFormatLength = len(dateFormat) // 时间格式长度
|
||||
)
|
||||
|
||||
// 重新计算随机数据长度
|
||||
randomLength = dataLength - (dateFormatLength + customIdLength)
|
||||
|
||||
if dateFormatLength+customIdLength > dataLength {
|
||||
return "", errors.New("超出长度")
|
||||
} else if dateFormatLength+customIdLength == dataLength {
|
||||
return "", errors.New("无法使用时间方法")
|
||||
} else if randomLength < 3 {
|
||||
return "", errors.New("自定义的数据过长")
|
||||
}
|
||||
return fmt.Sprintf("%v%s%s", customId, gotime.Current().SetFormat(dateFormat), gorandom.Numeric(randomLength)), nil
|
||||
}
|
||||
|
||||
// 生成18位随机唯一编号
|
||||
func generateIdFour(customId string) (string, error) {
|
||||
|
||||
var (
|
||||
randomLength = 4 // 随机数据长度
|
||||
customIdLength = len(customId) // 自定义长度
|
||||
)
|
||||
|
||||
const (
|
||||
dataLength = 18 // 默认数据长度
|
||||
)
|
||||
|
||||
// 重新计算随机数据长度
|
||||
randomLength = dataLength - customIdLength
|
||||
|
||||
if customIdLength >= dataLength {
|
||||
return "", errors.New("超出长度")
|
||||
} else if randomLength < 3 {
|
||||
return "", errors.New("自定义的数据过长")
|
||||
}
|
||||
return fmt.Sprintf("%v%s", customId, gorandom.Numeric(randomLength)), nil
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package only
|
||||
|
||||
type Only struct {
|
||||
}
|
||||
|
||||
func NewOnly() *Only {
|
||||
return &Only{}
|
||||
}
|
Loading…
Reference in new issue