Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yitter
雪花ID全家桶(SnowFlake IdGenerator)
提交
ab2b282d
雪
雪花ID全家桶(SnowFlake IdGenerator)
项目概览
yitter
/
雪花ID全家桶(SnowFlake IdGenerator)
9 个月 前同步成功
通知
201
Star
3
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
雪
雪花ID全家桶(SnowFlake IdGenerator)
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
ab2b282d
编写于
9月 15, 2022
作者:
yitter
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
3b1fb32e
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
43 addition
and
43 deletion
+43
-43
C#.NET/source/Yitter.IdGenTest/Program.cs
C#.NET/source/Yitter.IdGenTest/Program.cs
+21
-21
C#.NET/source/Yitter.IdGenerator/Contract/IIdGenerator.cs
C#.NET/source/Yitter.IdGenerator/Contract/IIdGenerator.cs
+1
-1
C#.NET/source/Yitter.IdGenerator/Contract/ISnowWorker.cs
C#.NET/source/Yitter.IdGenerator/Contract/ISnowWorker.cs
+1
-1
C#.NET/source/Yitter.IdGenerator/Core/SnowWorkerM1.cs
C#.NET/source/Yitter.IdGenerator/Core/SnowWorkerM1.cs
+2
-2
C#.NET/source/Yitter.IdGenerator/Core/SnowWorkerM3.cs
C#.NET/source/Yitter.IdGenerator/Core/SnowWorkerM3.cs
+2
-2
C#.NET/source/Yitter.IdGenerator/DefaultIdGenerator.cs
C#.NET/source/Yitter.IdGenerator/DefaultIdGenerator.cs
+15
-15
C#.NET/source/Yitter.IdGenerator/YitIdHelper.cs
C#.NET/source/Yitter.IdGenerator/YitIdHelper.cs
+1
-1
未找到文件。
C#.NET/source/Yitter.IdGenTest/Program.cs
浏览文件 @
ab2b282d
...
...
@@ -140,7 +140,7 @@ namespace Yitter.OrgSystem.TestA
WorkerId
=
1
,
WorkerIdBitLength
=
6
,
SeqBitLength
=
6
,
SeqBitLength
=
12
,
//TopOverCostCount = 2000,
//DataCenterIdBitLength = 0,
...
...
@@ -153,7 +153,7 @@ namespace Yitter.OrgSystem.TestA
//IdGen = new DefaultIdGenerator(options);
YitIdHelper
.
SetIdGenerator
(
options
);
genIdCount
=
50000
;
genIdCount
=
50000
00
;
while
(
true
)
{
DateTime
start
=
DateTime
.
Now
;
...
...
@@ -186,21 +186,21 @@ namespace Yitter.OrgSystem.TestA
if
(
outputLog
)
{
IdGen
.
GenIdActionAsync
=
(
arg
=>
{
if
(
arg
.
ActionType
==
1
)
{
Console
.
WriteLine
(
$">>>>
{
arg
.
WorkerId
}
:开始:
{
DateTime
.
Now
.
ToString
(
"mm:ss:fff"
)}
, 周期次序:
{
arg
.
TermIndex
}
"
);
}
else
if
(
arg
.
ActionType
==
2
)
{
Console
.
WriteLine
(
$"<<<<
{
arg
.
WorkerId
}
:结束:
{
DateTime
.
Now
.
ToString
(
"mm:ss:fff"
)}
,漂移
{
arg
.
OverCostCountInOneTerm
}
次,产生
{
arg
.
GenCountInOneTerm
}
个, 周期次序:
{
arg
.
TermIndex
}
"
);
}
if
(
arg
.
ActionType
==
8
)
{
Console
.
WriteLine
(
$"----
{
arg
.
WorkerId
}
:AA结束:
{
DateTime
.
Now
.
ToString
(
"mm:ss:fff"
)}
,时间回拨"
);
}
});
//
IdGen.GenIdActionAsync = (arg =>
//
{
//
if (arg.ActionType == 1)
//
{
//
Console.WriteLine($">>>> {arg.WorkerId}:开始:{DateTime.Now.ToString("mm:ss:fff")}, 周期次序:{arg.TermIndex}");
//
}
//
else if (arg.ActionType == 2)
//
{
//
Console.WriteLine($"<<<< {arg.WorkerId}:结束:{DateTime.Now.ToString("mm:ss:fff")},漂移 {arg.OverCostCountInOneTerm} 次,产生 {arg.GenCountInOneTerm} 个, 周期次序:{arg.TermIndex}");
//
}
//
if (arg.ActionType == 8)
//
{
//
Console.WriteLine($"---- {arg.WorkerId}:AA结束:{DateTime.Now.ToString("mm:ss:fff")},时间回拨");
//
}
//
});
}
for
(
int
i
=
1
;
i
<
workerCount
+
1
;
i
++)
...
...
@@ -233,10 +233,10 @@ namespace Yitter.OrgSystem.TestA
if
(
outputLog
)
{
idGen2
.
GenIdActionAsync
=
(
arg
=>
{
Console
.
WriteLine
(
$"
{
DateTime
.
Now
.
ToString
(
"mm:ss:fff"
)}
{
arg
.
WorkerId
}
漂移了
{
arg
.
OverCostCountInOneTerm
}
, 顺序:
{
arg
.
TermIndex
}
"
);
});
//
idGen2.GenIdActionAsync = (arg =>
//
{
//
Console.WriteLine($"{DateTime.Now.ToString("mm:ss:fff")} {arg.WorkerId} 漂移了 {arg.OverCostCountInOneTerm}, 顺序:{arg.TermIndex}");
//
});
}
testList
.
Add
(
test
);
...
...
C#.NET/source/Yitter.IdGenerator/Contract/IIdGenerator.cs
浏览文件 @
ab2b282d
...
...
@@ -16,7 +16,7 @@ namespace Yitter.IdGenerator
/// <summary>
/// 生成过程中产生的事件
/// </summary>
Action
<
OverCostActionArg
>
GenIdActionAsync
{
get
;
set
;
}
//
Action<OverCostActionArg> GenIdActionAsync { get; set; }
/// <summary>
/// 生成新的long型Id
...
...
C#.NET/source/Yitter.IdGenerator/Contract/ISnowWorker.cs
浏览文件 @
ab2b282d
...
...
@@ -15,7 +15,7 @@ namespace Yitter.IdGenerator
{
internal
interface
ISnowWorker
{
Action
<
OverCostActionArg
>
GenAction
{
get
;
set
;
}
//
Action<OverCostActionArg> GenAction { get; set; }
long
NextId
();
}
...
...
C#.NET/source/Yitter.IdGenerator/Core/SnowWorkerM1.cs
浏览文件 @
ab2b282d
...
...
@@ -327,7 +327,7 @@ namespace Yitter.IdGenerator
return
CalcId
(
_LastTimeTick
);
}
protected
virtual
long
CalcId
(
in
long
useTimeTick
)
protected
virtual
long
CalcId
(
long
useTimeTick
)
{
var
result
=
((
useTimeTick
<<
_TimestampShift
)
+
((
long
)
WorkerId
<<
SeqBitLength
)
+
...
...
@@ -337,7 +337,7 @@ namespace Yitter.IdGenerator
return
result
;
}
protected
virtual
long
CalcTurnBackId
(
in
long
useTimeTick
)
protected
virtual
long
CalcTurnBackId
(
long
useTimeTick
)
{
var
result
=
((
useTimeTick
<<
_TimestampShift
)
+
((
long
)
WorkerId
<<
SeqBitLength
)
+
_TurnBackIndex
);
...
...
C#.NET/source/Yitter.IdGenerator/Core/SnowWorkerM3.cs
浏览文件 @
ab2b282d
...
...
@@ -50,7 +50,7 @@ namespace Yitter.IdGenerator
_TimestampShift
=
(
byte
)(
DataCenterIdBitLength
+
WorkerIdBitLength
+
SeqBitLength
);
}
protected
override
long
CalcId
(
in
long
useTimeTick
)
protected
override
long
CalcId
(
long
useTimeTick
)
{
var
result
=
((
useTimeTick
<<
_TimestampShift
)
+
((
long
)
DataCenterId
<<
DataCenterIdBitLength
)
+
...
...
@@ -61,7 +61,7 @@ namespace Yitter.IdGenerator
return
result
;
}
protected
override
long
CalcTurnBackId
(
in
long
useTimeTick
)
protected
override
long
CalcTurnBackId
(
long
useTimeTick
)
{
var
result
=
((
useTimeTick
<<
_TimestampShift
)
+
((
long
)
DataCenterId
<<
DataCenterIdBitLength
)
+
...
...
C#.NET/source/Yitter.IdGenerator/DefaultIdGenerator.cs
浏览文件 @
ab2b282d
...
...
@@ -21,35 +21,35 @@ namespace Yitter.IdGenerator
{
private
ISnowWorker
_SnowWorker
{
get
;
set
;
}
public
Action
<
OverCostActionArg
>
GenIdActionAsync
{
get
=>
_SnowWorker
.
GenAction
;
set
=>
_SnowWorker
.
GenAction
=
value
;
}
//
public Action<OverCostActionArg> GenIdActionAsync
//
{
//
get => _SnowWorker.GenAction;
//
set => _SnowWorker.GenAction = value;
//
}
public
DefaultIdGenerator
(
IdGeneratorOptions
options
)
{
if
(
options
==
null
)
{
throw
new
A
pplication
Exception
(
"options error."
);
throw
new
A
rgument
Exception
(
"options error."
);
}
// 1.BaseTime
if
(
options
.
BaseTime
<
DateTime
.
Now
.
AddYears
(-
50
)
||
options
.
BaseTime
>
DateTime
.
Now
)
{
throw
new
A
pplication
Exception
(
"BaseTime error."
);
throw
new
A
rgument
Exception
(
"BaseTime error."
);
}
// 2.WorkerIdBitLength
int
maxLength
=
options
.
TimestampType
==
0
?
22
:
31
;
// (秒级时间戳时放大到31位)
if
(
options
.
WorkerIdBitLength
<=
0
)
{
throw
new
A
pplication
Exception
(
"WorkerIdBitLength error.(range:[1, 21])"
);
throw
new
A
rgument
Exception
(
"WorkerIdBitLength error.(range:[1, 21])"
);
}
if
(
options
.
DataCenterIdBitLength
+
options
.
WorkerIdBitLength
+
options
.
SeqBitLength
>
maxLength
)
{
throw
new
A
pplication
Exception
(
"error:DataCenterIdBitLength + WorkerIdBitLength + SeqBitLength <= "
+
maxLength
);
throw
new
A
rgument
Exception
(
"error:DataCenterIdBitLength + WorkerIdBitLength + SeqBitLength <= "
+
maxLength
);
}
// 3.WorkerId & DataCenterId
...
...
@@ -60,19 +60,19 @@ namespace Yitter.IdGenerator
}
if
(
options
.
WorkerId
<
0
||
options
.
WorkerId
>
maxWorkerIdNumber
)
{
throw
new
A
pplication
Exception
(
"WorkerId error. (range:[0, "
+
maxWorkerIdNumber
+
"]"
);
throw
new
A
rgument
Exception
(
"WorkerId error. (range:[0, "
+
maxWorkerIdNumber
+
"]"
);
}
var
maxDataCenterIdNumber
=
(
1
<<
options
.
DataCenterIdBitLength
)
-
1
;
if
(
options
.
DataCenterId
<
0
||
options
.
DataCenterId
>
maxDataCenterIdNumber
)
{
throw
new
A
pplication
Exception
(
"DataCenterId error. (range:[0, "
+
maxDataCenterIdNumber
+
"]"
);
throw
new
A
rgument
Exception
(
"DataCenterId error. (range:[0, "
+
maxDataCenterIdNumber
+
"]"
);
}
// 4.SeqBitLength
if
(
options
.
SeqBitLength
<
2
||
options
.
SeqBitLength
>
21
)
{
throw
new
A
pplication
Exception
(
"SeqBitLength error. (range:[2, 21])"
);
throw
new
A
rgument
Exception
(
"SeqBitLength error. (range:[2, 21])"
);
}
// 5.MaxSeqNumber
...
...
@@ -83,19 +83,19 @@ namespace Yitter.IdGenerator
}
if
(
options
.
MaxSeqNumber
<
0
||
options
.
MaxSeqNumber
>
maxSeqNumber
)
{
throw
new
A
pplication
Exception
(
"MaxSeqNumber error. (range:[1, "
+
maxSeqNumber
+
"]"
);
throw
new
A
rgument
Exception
(
"MaxSeqNumber error. (range:[1, "
+
maxSeqNumber
+
"]"
);
}
// 6.MinSeqNumber
if
(
options
.
MinSeqNumber
<
5
||
options
.
MinSeqNumber
>
maxSeqNumber
)
{
throw
new
A
pplication
Exception
(
"MinSeqNumber error. (range:[5, "
+
maxSeqNumber
+
"]"
);
throw
new
A
rgument
Exception
(
"MinSeqNumber error. (range:[5, "
+
maxSeqNumber
+
"]"
);
}
// 7.TopOverCostCount
if
(
options
.
TopOverCostCount
<
0
||
options
.
TopOverCostCount
>
10000
)
{
throw
new
A
pplication
Exception
(
"TopOverCostCount error. (range:[0, 10000]"
);
throw
new
A
rgument
Exception
(
"TopOverCostCount error. (range:[0, 10000]"
);
}
switch
(
options
.
Method
)
...
...
C#.NET/source/Yitter.IdGenerator/YitIdHelper.cs
浏览文件 @
ab2b282d
...
...
@@ -51,7 +51,7 @@ namespace Yitter.IdGenerator
// }
//}
if
(
_IdGenInstance
==
null
)
throw
new
A
pplication
Exception
(
"Please initialize Yitter.IdGeneratorOptions first."
);
if
(
_IdGenInstance
==
null
)
throw
new
A
rgument
Exception
(
"Please initialize Yitter.IdGeneratorOptions first."
);
return
_IdGenInstance
.
NewLong
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录