1
0
mirror of synced 2025-12-10 07:08:19 +08:00

auto commit

This commit is contained in:
yitter
2021-12-08 23:36:18 +08:00
parent a434e07c5d
commit 1d043bf0fe
4 changed files with 84 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
import time
import traceback
from IdGeneratorOptions import IdGeneratorOptions
from SnowFlake import SnowFlake
from SnowFlakeM1 import SnowFlakeM1
class DefaultIdGenerator(object):
def SetIdGernerator(self, options) :
if options.BaseTime < 100000 :
raise ValueError ("BaseTime error.")
self.SnowFlake= SnowFlakeM1(options)
def NextId(self):
return self.SnowFlake.NextId()
if __name__ == '__main__':
try:
options = IdGeneratorOptions(23)
options.BaseTime = 1231111111
idgen = DefaultIdGenerator()
idgen.SetIdGernerator(options)
print (idgen.NextId())
print (options.__dict__)
except ValueError as e:
print(e)

View File

@@ -0,0 +1,30 @@
import time
class IdGeneratorOptions(object):
def __init__(self, workerId = 0, workerIdBitLength = 6, seqBitLength = 6):
# 雪花计算方法,1-漂移算法|2-传统算法默认1。目前只实现了1。
self.Method = 1
# 基础时间ms单位不能超过当前系统时间
self.BaseTime = 1288834974657
# 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1
self.WorkerId = workerId
# 机器码位长默认值6取值范围 [1, 15](要求:序列数位长+机器码位长不超过22
self.WorkerIdBitLength = workerIdBitLength
# 序列数位长默认值6取值范围 [3, 21](要求:序列数位长+机器码位长不超过22
self.SeqBitLength = seqBitLength
# 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1]默认值0表示最大序列数取最大值2^SeqBitLength-1]
self.MaxSeqNumber = 0
# 最小序列数默认值5取值范围 [5, MaxSeqNumber]每毫秒的前5个序列数对应编号0-4是保留位其中1-4是时间回拨相应预留位0是手工新值预留位
self.MinSeqNumber = 5
# 最大漂移次数默认2000推荐范围500-10000与计算能力有关
self.TopOverCostCount = 2000

11
Python/soure/SnowFlake.py Normal file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/python
# coding=UTF-8
# 组件编号生成器
class SnowFlake(object):
def __init__(self, options):
self.Options = options
def NextId(self):
return 0

View File

@@ -0,0 +1,12 @@
#!/usr/bin/python
# coding=UTF-8
from SnowFlake import SnowFlake
# 组件编号生成器
class SnowFlakeM1(SnowFlake):
def __init__(self, options):
self.Options = options
def NextId(self):
return self.Options.WorkerId