Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yitter
雪花ID全家桶(SnowFlake IdGenerator)
提交
29c69b4c
雪
雪花ID全家桶(SnowFlake IdGenerator)
项目概览
yitter
/
雪花ID全家桶(SnowFlake IdGenerator)
大约 1 年 前同步成功
通知
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,发现更多精彩内容 >>
提交
29c69b4c
编写于
11月 01, 2021
作者:
当代码农
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加了三种function和更详细的说明
上级
022dbbb0
变更
9
显示空白变更内容
内联
并排
Showing
9 changed file
with
231 addition
and
98 deletion
+231
-98
JavaScript/index.js
JavaScript/index.js
+51
-2
TypeScript/README.md
TypeScript/README.md
+43
-46
TypeScript/snowflakeIdv1.ts
TypeScript/snowflakeIdv1.ts
+89
-45
TypeScript/snowflakeIdv1Option.ts
TypeScript/snowflakeIdv1Option.ts
+43
-0
TypeScript/test/test1.ts
TypeScript/test/test1.ts
+1
-1
TypeScript/test/test2.ts
TypeScript/test/test2.ts
+1
-1
TypeScript/test/test3.ts
TypeScript/test/test3.ts
+1
-1
TypeScript/test/test4.ts
TypeScript/test/test4.ts
+1
-1
TypeScript/test/test5.ts
TypeScript/test/test5.ts
+1
-1
未找到文件。
JavaScript/index.js
浏览文件 @
29c69b4c
...
...
@@ -198,11 +198,60 @@ class Genid {
return
tempTimeTicker
;
}
/**
* 生成ID
* @returns 始终输出number类型,超过时throw error
*/
NextNumber
()
{
if
(
this
.
_IsOverCost
)
{
//
let
id
=
this
.
NextOverCostId
()
if
(
id
>=
9007199254740992
n
)
throw
Error
(
`
${
id
.
toString
()}
over max of Number 9007199254740992`
)
return
parseInt
(
id
.
toString
())
}
else
{
//
let
id
=
this
.
NextNormalId
()
if
(
id
>=
9007199254740992
n
)
throw
Error
(
`
${
id
.
toString
()}
over max of Number 9007199254740992`
)
return
parseInt
(
id
.
toString
())
}
}
/**
* 生成ID
* @returns 根据输出数值判断,小于number最大值时输出number类型,大于时输出bigint
*/
NextId
()
{
if
(
this
.
_IsOverCost
)
{
return
parseInt
(
this
.
NextOverCostId
());
let
id
=
this
.
NextOverCostId
()
if
(
id
>=
9007199254740992
n
)
return
id
else
return
parseInt
(
id
)
}
else
{
let
id
=
this
.
NextNormalId
()
if
(
id
>=
9007199254740992
n
)
return
id
else
return
parseInt
(
id
)
}
}
/**
* 生成ID
* @returns 始终输出bigint类型
*/
NextBigId
()
{
if
(
this
.
_IsOverCost
)
{
//
return
this
.
NextOverCostId
()
}
else
{
return
parseInt
(
this
.
NextNormalId
());
//
return
this
.
NextNormalId
()
}
}
}
...
...
TypeScript/README.md
浏览文件 @
29c69b4c
...
...
@@ -11,14 +11,55 @@ js Number 类型最大数值:9007199254740992(16位),
在JS中没有bigint类型,所以建议将ID控制在16位以内,统一使用number类型
执行测试代码
## 使用
### 1.文件引用
```
js
import
{
snowflakeIdv1
}
from
'
../snowflakeIdv1
'
const
WorkerId
=
process
.
env
.
WorkerId
==
undefined
?
1
:
process
.
env
.
WorkerId
let
gen1
=
new
snowflakeIdv1
({
WorkerId
:
WorkerId
})
let
id1
=
gen1
.
NextId
()
console
.
log
(
id1
,
id1
.
toString
().
length
)
```
### 2.npm库安装
```
sh
npm i simple-flakeid
```
```
js
const
snowId
=
require
(
'
simple-flakeid
'
)
let
gen1
=
new
snowId
.
SnowflakeIdv1
({
workerId
:
1
})
for
(
let
i
=
0
;
i
<
10
;
i
++
)
{
let
id1
=
gen1
.
NextId
()
console
.
log
(
`
${
i
}
ID:
${
id1
}
${
typeof
id1
}
length:
${
id1
.
toString
().
length
}
`
)
}
```
## function
### function NextId()
根据输出数值判断,小于number最大值时输出number类型,大于时输出bigint
### function NextNumber()
始终输出number类型,超过时throw error
### function NextBigId()
始终输出bigint类型
## 测试
```
bash
ts-node
test
/test1.ts
```
## 使用
```
js
...
...
@@ -127,50 +168,6 @@ $ ts-node test/test4.ts
8
ID
:
30043877339570182
bigint
长度
:
17
9
ID
:
30043877339570183
bigint
长度
:
17
```
## 同时兼容number和bigint的写法
如果您觉得这个用法更好,可以手动替换对应方法
```
js
/**
* 生成ID
* @returns
*/
public
NextId
():
number
|
bigint
{
if
(
this
.
_IsOverCost
)
{
//
let
id
=
this
.
NextOverCostId
()
if
(
id
>=
9007199254740992
n
)
return
id
else
return
parseInt
(
id
.
toString
())
}
else
{
//
let
id
=
this
.
NextNormalId
()
if
(
id
>=
9007199254740992
n
)
return
id
else
return
parseInt
(
id
.
toString
())
}
}
```
## 其他帮助
...
...
TypeScript/snowflakeIdv1.ts
浏览文件 @
29c69b4c
import
{
snowflakeIdv1Option
}
from
"
./snowflakeIdv1Option
"
/**
*
*/
...
...
@@ -78,13 +80,11 @@ export class snowflakeIdv1 {
*/
private
_OverCostCountInOneTerm
/**
*Creates an instance of Genid.
* @author bubao
* @author zhupengfeivip
* @param {{
* Method: 1, // 雪花计算方法,(1-漂移算法|2-传统算法),默认 1
* BaseTime: 1577836800000, // 基础时间(ms 单位),不能超过当前系统时间
* BaseTime: 1577836800000, // 基础时间(ms 单位),默认2020年1月1日,不能超过当前系统时间,一旦投入使用就不能再更改,更改后产生的ID可能会和以前的重复
* WorkerId: Number, // 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1
* WorkerIdBitLength: 6, // 机器码位长,默认值 6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过 22)
* SeqBitLength: 6, // 序列数位长,默认值 6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过 22)
...
...
@@ -94,54 +94,53 @@ export class snowflakeIdv1 {
* }} options
* @memberof Genid
*/
constructor
(
options
:
any
)
{
if
(
options
.
W
orkerId
===
undefined
)
constructor
(
options
:
snowflakeIdv1Option
)
{
if
(
options
.
w
orkerId
===
undefined
)
throw
new
Error
(
"
lost WorkerId
"
)
// 1.BaseTime 2020年1月1日
// 1.BaseTime 2020年1月1日
Wed, 01 Jan 2020 00:00:00 GMT 0时区的2020年1月1日
const
BaseTime
=
1577836800000
if
(
!
options
.
BaseTime
||
options
.
B
aseTime
<
0
)
options
.
B
aseTime
=
BaseTime
if
(
!
options
.
baseTime
||
options
.
b
aseTime
<
0
)
options
.
b
aseTime
=
BaseTime
// 2.WorkerIdBitLength
const
WorkerIdBitLength
=
6
if
(
!
options
.
WorkerIdBitLength
||
options
.
W
orkerIdBitLength
<
0
)
options
.
W
orkerIdBitLength
=
WorkerIdBitLength
if
(
!
options
.
workerIdBitLength
||
options
.
w
orkerIdBitLength
<
0
)
options
.
w
orkerIdBitLength
=
WorkerIdBitLength
// 4.SeqBitLength
const
SeqBitLength
=
6
if
(
!
options
.
SeqBitLength
||
options
.
S
eqBitLength
<
0
)
options
.
S
eqBitLength
=
SeqBitLength
if
(
!
options
.
seqBitLength
||
options
.
s
eqBitLength
<
0
)
options
.
s
eqBitLength
=
SeqBitLength
// 5.MaxSeqNumber
const
MaxSeqNumber
=
(
1
<<
SeqBitLength
)
-
1
if
(
options
.
MaxSeqNumber
<=
0
||
options
.
MaxSeqNumber
===
undefined
)
{
options
.
MaxSeqNumber
=
MaxSeqNumber
}
if
(
options
.
maxSeqNumber
==
undefined
||
options
.
maxSeqNumber
<=
0
)
options
.
maxSeqNumber
=
(
1
<<
SeqBitLength
)
-
1
// 6.MinSeqNumber
const
MinSeqNumber
=
5
if
(
!
options
.
MinSeqNumber
||
options
.
M
inSeqNumber
<
0
)
options
.
M
inSeqNumber
=
MinSeqNumber
if
(
options
.
minSeqNumber
==
undefined
||
options
.
m
inSeqNumber
<
0
)
options
.
m
inSeqNumber
=
MinSeqNumber
// 7.Others
const
topOverCostCount
=
2000
if
(
!
options
.
TopOverCostCount
||
options
.
T
opOverCostCount
<
0
)
options
.
T
opOverCostCount
=
topOverCostCount
if
(
options
.
topOverCostCount
==
undefined
||
options
.
t
opOverCostCount
<
0
)
options
.
t
opOverCostCount
=
topOverCostCount
if
(
options
.
M
ethod
!==
2
)
options
.
M
ethod
=
1
if
(
options
.
m
ethod
!==
2
)
options
.
m
ethod
=
1
else
options
.
M
ethod
=
2
options
.
m
ethod
=
2
this
.
Method
=
BigInt
(
options
.
M
ethod
)
this
.
BaseTime
=
BigInt
(
options
.
B
aseTime
)
this
.
WorkerId
=
BigInt
(
options
.
W
orkerId
)
this
.
WorkerIdBitLength
=
BigInt
(
options
.
W
orkerIdBitLength
)
this
.
SeqBitLength
=
BigInt
(
options
.
S
eqBitLength
)
this
.
MaxSeqNumber
=
BigInt
(
options
.
M
axSeqNumber
)
this
.
MinSeqNumber
=
BigInt
(
options
.
M
inSeqNumber
)
this
.
TopOverCostCount
=
BigInt
(
options
.
T
opOverCostCount
)
this
.
Method
=
BigInt
(
options
.
m
ethod
)
this
.
BaseTime
=
BigInt
(
options
.
b
aseTime
)
this
.
WorkerId
=
BigInt
(
options
.
w
orkerId
)
this
.
WorkerIdBitLength
=
BigInt
(
options
.
w
orkerIdBitLength
)
this
.
SeqBitLength
=
BigInt
(
options
.
s
eqBitLength
)
this
.
MaxSeqNumber
=
BigInt
(
options
.
m
axSeqNumber
)
this
.
MinSeqNumber
=
BigInt
(
options
.
m
inSeqNumber
)
this
.
TopOverCostCount
=
BigInt
(
options
.
t
opOverCostCount
)
const
timestampShift
=
this
.
WorkerIdBitLength
+
this
.
SeqBitLength
const
currentSeqNumber
=
this
.
MinSeqNumber
...
...
@@ -156,6 +155,7 @@ export class snowflakeIdv1 {
this
.
_OverCostCountInOneTerm
=
0
}
/**
* 当前序列号超过最大范围,开始透支使用序号号的通知事件,,本项暂未实现
* @returns
...
...
@@ -326,9 +326,53 @@ export class snowflakeIdv1 {
/**
* 生成ID
* @returns
* @returns 始终输出number类型,超过时throw error
*/
public
NextNumber
():
number
{
if
(
this
.
_IsOverCost
)
{
//
let
id
=
this
.
NextOverCostId
()
if
(
id
>=
9007199254740992
n
)
throw
Error
(
`
${
id
.
toString
()}
over max of Number 9007199254740992`
)
return
parseInt
(
id
.
toString
())
}
else
{
//
let
id
=
this
.
NextNormalId
()
if
(
id
>=
9007199254740992
n
)
throw
Error
(
`
${
id
.
toString
()}
over max of Number 9007199254740992`
)
return
parseInt
(
id
.
toString
())
}
}
/**
* 生成ID
* @returns 根据输出数值判断,小于number最大值时输出number类型,大于时输出bigint
*/
public
NextId
():
number
|
bigint
{
if
(
this
.
_IsOverCost
)
{
//
let
id
=
this
.
NextOverCostId
()
if
(
id
>=
9007199254740992
n
)
return
id
else
return
parseInt
(
id
.
toString
())
}
else
{
//
let
id
=
this
.
NextNormalId
()
if
(
id
>=
9007199254740992
n
)
return
id
else
return
parseInt
(
id
.
toString
())
}
}
/**
* 生成ID
* @returns 始终输出bigint类型
*/
public
NextId
():
bigint
{
public
Next
Big
Id
():
bigint
{
if
(
this
.
_IsOverCost
)
{
//
return
this
.
NextOverCostId
()
...
...
TypeScript/snowflakeIdv1Option.ts
0 → 100644
浏览文件 @
29c69b4c
export
class
snowflakeIdv1Option
{
/**
* 雪花计算方法,(1-漂移算法|2-传统算法),默认 1
*/
method
?:
number
=
1
/**
* 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1
*/
workerId
:
number
=
1
/**
* 机器码位长,默认值 6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过 22)
*/
workerIdBitLength
?:
number
=
6
/**
* 基础时间(ms 单位),不能超过当前系统时间, 默认2020年1月1日
*/
baseTime
?:
number
=
1577836800000
/**
* 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值 0,表示最大序列数取最大值(2^SeqBitLength-1])
*/
maxSeqNumber
?:
number
=
undefined
/**
* 最小序列数(含),默认值 5,取值范围 [5, MaxSeqNumber],每毫秒的前 5 个序列数对应编号 0-4 是保留位,其中 1-4 是时间回拨相应预留位,0 是手工新值预留位
*/
minSeqNumber
?:
number
=
5
/**
* 序列数位长,默认值 6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过 22)
*/
seqBitLength
?:
number
=
6
/**
* 最大漂移次数(含),默认 2000,推荐范围 500-10000(与计算能力有关)
*/
topOverCostCount
?:
number
=
2000
}
\ No newline at end of file
TypeScript/test/test1.ts
浏览文件 @
29c69b4c
...
...
@@ -2,6 +2,6 @@ import { snowflakeIdv1 } from '../snowflakeIdv1'
const
WorkerId
=
process
.
env
.
WorkerId
==
undefined
?
1
:
process
.
env
.
WorkerId
let
gen1
=
new
snowflakeIdv1
({
WorkerId
:
WorkerId
})
let
gen1
=
new
snowflakeIdv1
({
workerId
:
1
})
let
id1
=
gen1
.
NextId
()
console
.
log
(
`ID:
${
id1
}
长度:
${
id1
.
toString
().
length
}
`
)
\ No newline at end of file
TypeScript/test/test2.ts
浏览文件 @
29c69b4c
...
...
@@ -8,7 +8,7 @@ const Method = process.env.Method == undefined ? 1 : process.env.Method
console
.
log
(
"
WorkerId:
"
+
WorkerId
)
console
.
log
(
"
Method:
"
+
Method
)
console
.
log
(
"
--------------------
"
)
let
gen1
=
new
snowflakeIdv1
({
WorkerId
:
WorkerId
,
Method
:
Method
})
let
gen1
=
new
snowflakeIdv1
({
workerId
:
1
})
...
...
TypeScript/test/test3.ts
浏览文件 @
29c69b4c
...
...
@@ -4,6 +4,6 @@ const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
const
Method
=
process
.
env
.
Method
==
undefined
?
1
:
process
.
env
.
Method
let
gen1
=
new
snowflakeIdv1
({
WorkerId
:
WorkerId
,
Method
:
Method
})
let
gen1
=
new
snowflakeIdv1
({
workerId
:
1
})
let
id1
=
gen1
.
NextId
()
console
.
log
(
id1
,
id1
.
toString
().
length
)
\ No newline at end of file
TypeScript/test/test4.ts
浏览文件 @
29c69b4c
...
...
@@ -2,7 +2,7 @@ import { snowflakeIdv1 } from '../snowflakeIdv1'
const
WorkerId
=
process
.
env
.
WorkerId
==
undefined
?
1
:
process
.
env
.
WorkerId
let
gen1
=
new
snowflakeIdv1
({
WorkerId
:
WorkerId
,
S
eqBitLength
:
10
})
let
gen1
=
new
snowflakeIdv1
({
workerId
:
1
,
s
eqBitLength
:
10
})
for
(
let
i
=
0
;
i
<
10
;
i
++
)
{
let
id1
=
gen1
.
NextId
()
console
.
log
(
`
${
i
}
ID:
${
id1
}
${
typeof
id1
}
长度:
${
id1
.
toString
().
length
}
`
)
...
...
TypeScript/test/test5.ts
浏览文件 @
29c69b4c
...
...
@@ -2,7 +2,7 @@ import { snowflakeIdv1 } from '../snowflakeIdv1'
const
WorkerId
=
process
.
env
.
WorkerId
==
undefined
?
1
:
process
.
env
.
WorkerId
let
gen1
=
new
snowflakeIdv1
({
WorkerId
:
WorkerId
,
S
eqBitLength
:
10
})
let
gen1
=
new
snowflakeIdv1
({
workerId
:
1
,
s
eqBitLength
:
10
})
// for (let i = 0; i < 10; i++) {
// let id1 = gen1.NextId()
// console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录