From 872cd6a34d88d2844940c55b0b87f8c07da5ec01 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Mon, 4 Oct 2021 18:57:21 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9demo=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E4=B8=AA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TypeScript/README.md | 11 ++++++----- TypeScript/test/test1.ts | 9 +++++++++ TypeScript/test/{test.ts => tests.ts} | 0 3 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 TypeScript/test/test1.ts rename TypeScript/test/{test.ts => tests.ts} (100%) diff --git a/TypeScript/README.md b/TypeScript/README.md index 1509999..1d12f9a 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -11,8 +11,6 @@ ```bash ts-node test/test.ts - -NODE_ENV=development REDIS_HOST=127.0.0.1 ``` @@ -20,11 +18,14 @@ NODE_ENV=development REDIS_HOST=127.0.0.1 ## 使用 ```js -import { Genid } from '../index' +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId +const Method = process.env.Method == undefined ? 1 : process.env.Method -let gen = new Genid({ WorkerId: 1 }) -let id1 = gen.NextId() +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method }) +let id1 = gen1.NextId() console.log(id1, id1.toString().length) ``` diff --git a/TypeScript/test/test1.ts b/TypeScript/test/test1.ts new file mode 100644 index 0000000..1957567 --- /dev/null +++ b/TypeScript/test/test1.ts @@ -0,0 +1,9 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +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 id1 = gen1.NextId() +console.log(id1, id1.toString().length) \ No newline at end of file diff --git a/TypeScript/test/test.ts b/TypeScript/test/tests.ts similarity index 100% rename from TypeScript/test/test.ts rename to TypeScript/test/tests.ts From 26ce408bc39971e12059b95863fb6cc290c16995 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Mon, 4 Oct 2021 18:58:33 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E4=BF=AE=E6=AD=A3demo=E4=B8=8A=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TypeScript/README.md | 2 +- TypeScript/test/{tests.ts => test2.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename TypeScript/test/{tests.ts => test2.ts} (100%) diff --git a/TypeScript/README.md b/TypeScript/README.md index 1d12f9a..00e643e 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -10,7 +10,7 @@ 执行测试代码 ```bash -ts-node test/test.ts +ts-node test/test1.ts ``` diff --git a/TypeScript/test/tests.ts b/TypeScript/test/test2.ts similarity index 100% rename from TypeScript/test/tests.ts rename to TypeScript/test/test2.ts From 21e30e55da4df47a10bd247c73ddce3228d03e24 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Mon, 25 Oct 2021 13:50:36 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=5FIsOverCost=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E8=A7=A3=E5=86=B3bigint=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TypeScript/README.md | 119 ++++++++++++++++++++++++++++++++++++++++++-- TypeScript/snowflakeIdv1.ts | 20 +++++--- TypeScript/test/test1.ts | 6 +-- TypeScript/test/test3.ts | 9 ++++ TypeScript/test/test4.ts | 9 ++++ 5 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 TypeScript/test/test3.ts create mode 100644 TypeScript/test/test4.ts diff --git a/TypeScript/README.md b/TypeScript/README.md index 00e643e..b3dfc77 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -6,6 +6,10 @@ 代码贡献者:zhupengfei(在 bubao 布宝 的JS基础上改版,感谢bubao 布宝) +js Number 类型最大数值:9007199254740992(16位), + +在JS中没有bigint类型,所以建议将ID控制在16位以内,统一使用number类型 + 执行测试代码 @@ -22,12 +26,121 @@ import { snowflakeIdv1 } from '../snowflakeIdv1' 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: WorkerId}) let id1 = gen1.NextId() console.log(id1, id1.toString().length) ``` +## 示例 + +```js +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 6 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} 长度:${id1.toString().length}`) +} + +$ ts-node test/test4.ts +0 ID:234712552579141 长度:15 +1 ID:234712552587333 长度:15 +2 ID:234712552587334 长度:15 +3 ID:234712552587335 长度:15 +4 ID:234712552587336 长度:15 +5 ID:234712552591429 长度:15 +6 ID:234712552591430 长度:15 +7 ID:234712552591431 长度:15 +8 ID:234712552591432 长度:15 +9 ID:234712552591433 长度:15 + + +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 11 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} + + +$ ts-node test/test4.ts +0 ID:7510958933018629 number 长度:16 +1 ID:7510958933280773 number 长度:16 +2 ID:7510958933280774 number 长度:16 +3 ID:7510958933280775 number 长度:16 +4 ID:7510958933411845 number 长度:16 +5 ID:7510958933411846 number 长度:16 +6 ID:7510958933542917 number 长度:16 +7 ID:7510958933542918 number 长度:16 +8 ID:7510958933542919 number 长度:16 +9 ID:7510958933673989 number 长度:16 + +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 12 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} + +$ ts-node test/test4.ts +0 ID:15021931987734533 bigint 长度:17 +1 ID:15021931987996677 bigint 长度:17 +2 ID:15021931987996678 bigint 长度:17 +3 ID:15021931987996679 bigint 长度:17 +4 ID:15021931987996680 bigint 长度:17 +5 ID:15021931988258821 bigint 长度:17 +6 ID:15021931988258822 bigint 长度:17 +7 ID:15021931988258823 bigint 长度:17 +8 ID:15021931988258824 bigint 长度:17 +9 ID:15021931988520965 bigint 长度:17 + + +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 13 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} + +$ ts-node test/test4.ts +0 ID:30043877337997317 bigint 长度:17 +1 ID:30043877338521605 bigint 长度:17 +2 ID:30043877338521606 bigint 长度:17 +3 ID:30043877339045893 bigint 长度:17 +4 ID:30043877339045894 bigint 长度:17 +5 ID:30043877339045895 bigint 长度:17 +6 ID:30043877339045896 bigint 长度:17 +7 ID:30043877339570181 bigint 长度:17 +8 ID:30043877339570182 bigint 长度:17 +9 ID:30043877339570183 bigint 长度:17 + + + + + + + + + + + + + + + + +``` + diff --git a/TypeScript/snowflakeIdv1.ts b/TypeScript/snowflakeIdv1.ts index eb1aff4..b3b5227 100644 --- a/TypeScript/snowflakeIdv1.ts +++ b/TypeScript/snowflakeIdv1.ts @@ -328,13 +328,21 @@ export class snowflakeIdv1 { * 生成ID * @returns */ - public NextId(): number { - if (this.Method == BigInt(1)) { - //雪花漂移算法 - return parseInt(this.NextOverCostId().toString()) + public NextId(): number | bigint { + if (this._IsOverCost) { + // + let id = this.NextOverCostId() + if (id >= 9007199254740992n) + return id + else + return parseInt(id.toString()) } else { - //常规雪花算法 - return parseInt(this.NextNormalId().toString()) + // + let id = this.NextNormalId() + if (id >= 9007199254740992n) + return id + else + return parseInt(id.toString()) } } } diff --git a/TypeScript/test/test1.ts b/TypeScript/test/test1.ts index 1957567..5041b27 100644 --- a/TypeScript/test/test1.ts +++ b/TypeScript/test/test1.ts @@ -2,8 +2,6 @@ import { snowflakeIdv1 } from '../snowflakeIdv1' 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: WorkerId }) let id1 = gen1.NextId() -console.log(id1, id1.toString().length) \ No newline at end of file +console.log(`ID:${id1} 长度:${id1.toString().length}`) \ No newline at end of file diff --git a/TypeScript/test/test3.ts b/TypeScript/test/test3.ts new file mode 100644 index 0000000..1957567 --- /dev/null +++ b/TypeScript/test/test3.ts @@ -0,0 +1,9 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +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 id1 = gen1.NextId() +console.log(id1, id1.toString().length) \ No newline at end of file diff --git a/TypeScript/test/test4.ts b/TypeScript/test/test4.ts new file mode 100644 index 0000000..6477960 --- /dev/null +++ b/TypeScript/test/test4.ts @@ -0,0 +1,9 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 13 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} From d8d8714eaf0a1090e590d5739ad4d039fc65c4fb Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Mon, 25 Oct 2021 15:59:29 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E4=B8=BAbigint=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaScript/test/test2.js | 9 +++++---- TypeScript/README.md | 28 ++++++++++++++++++++++++++++ TypeScript/snowflakeIdv1.ts | 14 +++----------- TypeScript/test/test4.ts | 2 +- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/JavaScript/test/test2.js b/JavaScript/test/test2.js index 1aa6048..1e7fd4b 100644 --- a/JavaScript/test/test2.js +++ b/JavaScript/test/test2.js @@ -8,10 +8,11 @@ function test1() { } } function test2() { - const genid = new GenId({ WorkerId: 1 }) - const id = genid.NextId() - console.log(typeof (id)) - console.log(id, id.toString().length) + const genid = new GenId({ WorkerId: 1, SeqBitLength: 14 }) + for (let i = 0; i < 10; i++) { + let id1 = genid.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) + } } function main() { diff --git a/TypeScript/README.md b/TypeScript/README.md index b3dfc77..aa8c1cb 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -144,3 +144,31 @@ $ ts-node test/test4.ts ``` +## 同时兼容number和bigint的写法 + +如果您觉得这个用法更好,可以手动替换对应方法 + +```js + /** + * 生成ID + * @returns + */ + public NextId(): number | bigint { + if (this._IsOverCost) { + // + let id = this.NextOverCostId() + if (id >= 9007199254740992n) + return id + else + return parseInt(id.toString()) + } else { + // + let id = this.NextNormalId() + if (id >= 9007199254740992n) + return id + else + return parseInt(id.toString()) + } + } + +``` \ No newline at end of file diff --git a/TypeScript/snowflakeIdv1.ts b/TypeScript/snowflakeIdv1.ts index b3b5227..f84a22f 100644 --- a/TypeScript/snowflakeIdv1.ts +++ b/TypeScript/snowflakeIdv1.ts @@ -328,21 +328,13 @@ export class snowflakeIdv1 { * 生成ID * @returns */ - public NextId(): number | bigint { + public NextId(): bigint { if (this._IsOverCost) { // - let id = this.NextOverCostId() - if (id >= 9007199254740992n) - return id - else - return parseInt(id.toString()) + return this.NextOverCostId() } else { // - let id = this.NextNormalId() - if (id >= 9007199254740992n) - return id - else - return parseInt(id.toString()) + return this.NextNormalId() } } } diff --git a/TypeScript/test/test4.ts b/TypeScript/test/test4.ts index 6477960..ab51f36 100644 --- a/TypeScript/test/test4.ts +++ b/TypeScript/test/test4.ts @@ -2,7 +2,7 @@ import { snowflakeIdv1 } from '../snowflakeIdv1' const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId -let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 13 }) +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 10 }) for (let i = 0; i < 10; i++) { let id1 = gen1.NextId() console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) From 7b7c69a36841f15126b349eeda8454abf9b2d329 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Tue, 26 Oct 2021 13:05:02 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TypeScript/README.md | 8 +++++++- TypeScript/test/test5.ts | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 TypeScript/test/test5.ts diff --git a/TypeScript/README.md b/TypeScript/README.md index aa8c1cb..f0e47b5 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -171,4 +171,10 @@ $ ts-node test/test4.ts } } -``` \ No newline at end of file +``` + +## 其他帮助 + +在mysql中int类型最大长度是10位数字,由于本算法默认生成的是15位,最短也是11位,所以在mysql中需要使用bigint数据类型 + + diff --git a/TypeScript/test/test5.ts b/TypeScript/test/test5.ts new file mode 100644 index 0000000..5877aca --- /dev/null +++ b/TypeScript/test/test5.ts @@ -0,0 +1,18 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 10 }) +// for (let i = 0; i < 10; i++) { +// let id1 = gen1.NextId() +// console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +// } + +console.time("Test Run") +const HSet = new Set() +for (let index = 0; index < 500000; index++) { + HSet.add(gen1.NextId()) +} +console.timeEnd("Test Run") +console.log([...HSet.values()].join("\n")) +console.log(HSet.size) \ No newline at end of file From 022dbbb093aad7e3d4049d23886010fc0af41260 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Mon, 1 Nov 2021 13:20:52 +0800 Subject: [PATCH 6/8] add ignore file --- TypeScript/.gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TypeScript/.gitignore b/TypeScript/.gitignore index d8e9788..b9276fa 100644 --- a/TypeScript/.gitignore +++ b/TypeScript/.gitignore @@ -1,3 +1,4 @@ .vscode node_modules -env.config.js \ No newline at end of file +env.config.js +FORSELF.md \ No newline at end of file From 29c69b4c64520847fb808d8805747551a1abb5f8 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Mon, 1 Nov 2021 13:31:08 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B8=89?= =?UTF-8?q?=E7=A7=8Dfunction=E5=92=8C=E6=9B=B4=E8=AF=A6=E7=BB=86=E7=9A=84?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaScript/index.js | 53 ++++++++++++++- TypeScript/README.md | 89 ++++++++++++------------- TypeScript/snowflakeIdv1.ts | 134 +++++++++++++++++++++++++------------- TypeScript/snowflakeIdv1Option.ts | 43 ++++++++++++ TypeScript/test/test1.ts | 2 +- TypeScript/test/test2.ts | 2 +- TypeScript/test/test3.ts | 2 +- TypeScript/test/test4.ts | 2 +- TypeScript/test/test5.ts | 2 +- 9 files changed, 231 insertions(+), 98 deletions(-) create mode 100644 TypeScript/snowflakeIdv1Option.ts diff --git a/JavaScript/index.js b/JavaScript/index.js index 989b01f..7dffae6 100644 --- a/JavaScript/index.js +++ b/JavaScript/index.js @@ -198,11 +198,60 @@ class Genid { return tempTimeTicker; } + + /** + * 生成ID + * @returns 始终输出number类型,超过时throw error + */ + NextNumber() { + if (this._IsOverCost) { + // + let id = this.NextOverCostId() + if (id >= 9007199254740992n) + throw Error(`${id.toString()} over max of Number 9007199254740992`) + + return parseInt(id.toString()) + } else { + // + let id = this.NextNormalId() + if (id >= 9007199254740992n) + 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 >= 9007199254740992n) + return id + else + return parseInt(id) + } else { + let id = this.NextNormalId() + if (id >= 9007199254740992n) + return id + else + return parseInt(id) + } + } + + /** + * 生成ID + * @returns 始终输出bigint类型 + */ + NextBigId() { + if (this._IsOverCost) { + // + return this.NextOverCostId() } else { - return parseInt(this.NextNormalId()); + // + return this.NextNormalId() } } } diff --git a/TypeScript/README.md b/TypeScript/README.md index f0e47b5..f5c2347 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -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 >= 9007199254740992n) - return id - else - return parseInt(id.toString()) - } else { - // - let id = this.NextNormalId() - if (id >= 9007199254740992n) - return id - else - return parseInt(id.toString()) - } - } - ``` ## 其他帮助 diff --git a/TypeScript/snowflakeIdv1.ts b/TypeScript/snowflakeIdv1.ts index f84a22f..0d4f75f 100644 --- a/TypeScript/snowflakeIdv1.ts +++ b/TypeScript/snowflakeIdv1.ts @@ -1,3 +1,5 @@ +import { snowflakeIdv1Option } from "./snowflakeIdv1Option" + /** * */ @@ -78,70 +80,67 @@ export class snowflakeIdv1 { */ private _OverCostCountInOneTerm - /** - *Creates an instance of Genid. - * @author bubao - * @param {{ - * Method: 1, // 雪花计算方法,(1-漂移算法|2-传统算法),默认 1 - * BaseTime: 1577836800000, // 基础时间(ms 单位),不能超过当前系统时间 - * WorkerId: Number, // 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1 - * WorkerIdBitLength: 6, // 机器码位长,默认值 6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过 22) - * SeqBitLength: 6, // 序列数位长,默认值 6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过 22) - * MaxSeqNumber: 5, // 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值 0,表示最大序列数取最大值(2^SeqBitLength-1]) - * MinSeqNumber: 5, // 最小序列数(含),默认值 5,取值范围 [5, MaxSeqNumber],每毫秒的前 5 个序列数对应编号 0-4 是保留位,其中 1-4 是时间回拨相应预留位,0 是手工新值预留位 - * TopOverCostCount: 2000// 最大漂移次数(含),默认 2000,推荐范围 500-10000(与计算能力有关) - * }} options - * @memberof Genid - */ - constructor(options: any) { - if (options.WorkerId === undefined) + *Creates an instance of Genid. + * @author zhupengfeivip + * @param {{ + * BaseTime: 1577836800000, // 基础时间(ms 单位),默认2020年1月1日,不能超过当前系统时间,一旦投入使用就不能再更改,更改后产生的ID可能会和以前的重复 + * WorkerId: Number, // 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1 + * WorkerIdBitLength: 6, // 机器码位长,默认值 6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过 22) + * SeqBitLength: 6, // 序列数位长,默认值 6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过 22) + * MaxSeqNumber: 5, // 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值 0,表示最大序列数取最大值(2^SeqBitLength-1]) + * MinSeqNumber: 5, // 最小序列数(含),默认值 5,取值范围 [5, MaxSeqNumber],每毫秒的前 5 个序列数对应编号 0-4 是保留位,其中 1-4 是时间回拨相应预留位,0 是手工新值预留位 + * TopOverCostCount: 2000// 最大漂移次数(含),默认 2000,推荐范围 500-10000(与计算能力有关) + * }} options + * @memberof Genid + */ + constructor(options: snowflakeIdv1Option) { + if (options.workerId === 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.BaseTime < 0) - options.BaseTime = BaseTime + if (!options.baseTime || options.baseTime < 0) + options.baseTime = BaseTime // 2.WorkerIdBitLength const WorkerIdBitLength = 6 - if (!options.WorkerIdBitLength || options.WorkerIdBitLength < 0) - options.WorkerIdBitLength = WorkerIdBitLength + if (!options.workerIdBitLength || options.workerIdBitLength < 0) + options.workerIdBitLength = WorkerIdBitLength // 4.SeqBitLength const SeqBitLength = 6 - if (!options.SeqBitLength || options.SeqBitLength < 0) - options.SeqBitLength = SeqBitLength + if (!options.seqBitLength || options.seqBitLength < 0) + options.seqBitLength = 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.MinSeqNumber < 0) - options.MinSeqNumber = MinSeqNumber + if (options.minSeqNumber == undefined || options.minSeqNumber < 0) + options.minSeqNumber = MinSeqNumber // 7.Others const topOverCostCount = 2000 - if (!options.TopOverCostCount || options.TopOverCostCount < 0) - options.TopOverCostCount = topOverCostCount + if (options.topOverCostCount == undefined || options.topOverCostCount < 0) + options.topOverCostCount = topOverCostCount - if (options.Method !== 2) - options.Method = 1 + if (options.method !== 2) + options.method = 1 else - options.Method = 2 + options.method = 2 - this.Method = BigInt(options.Method) - this.BaseTime = BigInt(options.BaseTime) - this.WorkerId = BigInt(options.WorkerId) - this.WorkerIdBitLength = BigInt(options.WorkerIdBitLength) - this.SeqBitLength = BigInt(options.SeqBitLength) - this.MaxSeqNumber = BigInt(options.MaxSeqNumber) - this.MinSeqNumber = BigInt(options.MinSeqNumber) - this.TopOverCostCount = BigInt(options.TopOverCostCount) + this.Method = BigInt(options.method) + this.BaseTime = BigInt(options.baseTime) + this.WorkerId = BigInt(options.workerId) + this.WorkerIdBitLength = BigInt(options.workerIdBitLength) + this.SeqBitLength = BigInt(options.seqBitLength) + this.MaxSeqNumber = BigInt(options.maxSeqNumber) + this.MinSeqNumber = BigInt(options.minSeqNumber) + this.TopOverCostCount = BigInt(options.topOverCostCount) 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 >= 9007199254740992n) + throw Error(`${id.toString()} over max of Number 9007199254740992`) + + return parseInt(id.toString()) + } else { + // + let id = this.NextNormalId() + if (id >= 9007199254740992n) + 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 >= 9007199254740992n) + return id + else + return parseInt(id.toString()) + } else { + // + let id = this.NextNormalId() + if (id >= 9007199254740992n) + return id + else + return parseInt(id.toString()) + } + } + + /** + * 生成ID + * @returns 始终输出bigint类型 */ - public NextId(): bigint { + public NextBigId(): bigint { if (this._IsOverCost) { // return this.NextOverCostId() diff --git a/TypeScript/snowflakeIdv1Option.ts b/TypeScript/snowflakeIdv1Option.ts new file mode 100644 index 0000000..c4a2667 --- /dev/null +++ b/TypeScript/snowflakeIdv1Option.ts @@ -0,0 +1,43 @@ +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 diff --git a/TypeScript/test/test1.ts b/TypeScript/test/test1.ts index 5041b27..4b4e322 100644 --- a/TypeScript/test/test1.ts +++ b/TypeScript/test/test1.ts @@ -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 diff --git a/TypeScript/test/test2.ts b/TypeScript/test/test2.ts index f41bd28..c3b712a 100644 --- a/TypeScript/test/test2.ts +++ b/TypeScript/test/test2.ts @@ -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 }) diff --git a/TypeScript/test/test3.ts b/TypeScript/test/test3.ts index 1957567..95f2876 100644 --- a/TypeScript/test/test3.ts +++ b/TypeScript/test/test3.ts @@ -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 diff --git a/TypeScript/test/test4.ts b/TypeScript/test/test4.ts index ab51f36..e4f88f4 100644 --- a/TypeScript/test/test4.ts +++ b/TypeScript/test/test4.ts @@ -2,7 +2,7 @@ import { snowflakeIdv1 } from '../snowflakeIdv1' const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId -let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 10 }) +let gen1 = new snowflakeIdv1({ workerId: 1, seqBitLength: 10 }) for (let i = 0; i < 10; i++) { let id1 = gen1.NextId() console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) diff --git a/TypeScript/test/test5.ts b/TypeScript/test/test5.ts index 5877aca..fb921c2 100644 --- a/TypeScript/test/test5.ts +++ b/TypeScript/test/test5.ts @@ -2,7 +2,7 @@ import { snowflakeIdv1 } from '../snowflakeIdv1' const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId -let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 10 }) +let gen1 = new snowflakeIdv1({ workerId: 1, seqBitLength: 10 }) // for (let i = 0; i < 10; i++) { // let id1 = gen1.NextId() // console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) From ab4ebd19d9759b26bfb6004611dcca81107dd651 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Mon, 1 Nov 2021 13:43:05 +0800 Subject: [PATCH 8/8] fix --- TypeScript/snowflakeIdv1.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/TypeScript/snowflakeIdv1.ts b/TypeScript/snowflakeIdv1.ts index 9eb8387..0d4f75f 100644 --- a/TypeScript/snowflakeIdv1.ts +++ b/TypeScript/snowflakeIdv1.ts @@ -372,11 +372,7 @@ export class snowflakeIdv1 { * 生成ID * @returns 始终输出bigint类型 */ -<<<<<<< HEAD public NextBigId(): bigint { -======= - public NextId(): bigint { ->>>>>>> 91459b1538b24a03174b20eba10db410d8ef5268 if (this._IsOverCost) { // return this.NextOverCostId()