Skip to content

Commit a95f518

Browse files
committed
Fixed the position of MID of UUID in Snowflake-SI-vA.
1 parent 52c9ef4 commit a95f518

File tree

5 files changed

+46
-26
lines changed

5 files changed

+46
-26
lines changed

CHANGES.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
# Changes Logs
22

3-
## v0.2.2
3+
## v0.1.1
44

5-
- Fixed: Error when creating text logger lacking `$trace` definition.
6-
7-
## v0.2.1
8-
9-
- Fixed: Only one level was operated in global operation.
10-
11-
## v0.2.0
12-
13-
- A full new simpler logger.
5+
- Fixed the position of MID of UUID in Snowflake-SI-vA.

docs/en-US/Snowflake-SI-vA.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ brought into being.
99
The Snowflake-SI-vA algorithm uses 53-bits integer as the output of UUID
1010
generation, its bits-mapping shows like following:
1111

12-
| H | 52 ~ 10 | 9 ~ 0 | L |
13-
|:-:|:-------------------------------------------:|:----------:|:-:|
14-
| | 1111111111111111111111111111111111111111111 | 1111111111 | |
15-
| | UIN | MID | |
12+
| H | 52 ~ 43 | 42 ~ 0 | L |
13+
|:-:|:----------:|:-------------------------------------------:|:-:|
14+
| | 1111111111 | 1111111111111111111111111111111111111111111 | |
15+
| | MID | UIN | |
1616

17-
As above graph shows, the high 43 bits are the UIN(UUID Index Number). It's the
17+
As above graph shows, the low 43 bits are the UIN(UUID Index Number). It's the
1818
sum of the cursor of an incremental sequence and the current Unix timestamp in
1919
milliseconds. It means the genrated UUID is still an incremental sequence.
2020

@@ -23,7 +23,7 @@ milliseconds. It means the genrated UUID is still an incremental sequence.
2323
> incremental sequence and timestamp, there is 2-bits as the extra reserved
2424
> zone.
2525
26-
The low 10-bits is the ID of machine, for distributed services. So that it
26+
The high 10-bits is the ID of machine, for distributed services. So that it
2727
will not generate duplicated UUID between multi-instances. Different to the
2828
Snowflake-SI, Snowflake-SI-vA use a fixed 10-bits for MID, and then there could
2929
always be 1024 instances at most.

docs/zh-CN/Snowflake-SI-vA.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ Snowflake-SI-vA 算法是 Snowflake-SI 算法的变种。Snowflake-SI 算法使
77
Snowflake-SI-vA 算法为了确保生成的 UUID 的精度,于是使用了 53 位整数作为 UUID 的生成
88
结果,其位分部如下:
99

10-
| H | 52 ~ 10 | 9 ~ 0 | L |
11-
|:-:|:-------------------------------------------:|:----------:|:-:|
12-
| | 1111111111111111111111111111111111111111111 | 1111111111 | |
13-
| | UIN | MID | |
10+
| H | 52 ~ 43 | 42 ~ 0 | L |
11+
|:-:|:----------:|:-------------------------------------------:|:-:|
12+
| | 1111111111 | 1111111111111111111111111111111111111111111 | |
13+
| | MID | UIN | |
1414

15-
如上表, 43 位是 UUID 序号(UIN,UUID Index Number),它是一个递增序列当前指针和
15+
如上表, 43 位是 UUID 序号(UIN,UUID Index Number),它是一个递增序列当前指针和
1616
当前 UNIX 毫秒时间戳的加法和。因此生成的 UUID 仍然是按时间递增的序列。
1717

1818
> 到 2038 年, Unix 毫秒时间戳总共用了 41 个二进制位,但是考虑到递增序列的加和影响,
1919
> 因此额外增加了两位作为额外保留区(Extra Reserved Zone),防止溢出。
2020
21-
10 位是机器序号(Machine ID),用于确保在分布式服务中,多个实例同时工作,也不会
21+
10 位是机器序号(Machine ID),用于确保在分布式服务中,多个实例同时工作,也不会
2222
生成重复的 UUID。此处固定为 10 位,因此最多允许 1024 个实例同时工作。
2323

2424
## 特点

src/algorithms/snowflake-si-va.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export namespace SnowflakeSIvA {
2727
*/
2828
const MID_LENGTH = 10;
2929

30+
/**
31+
* UUID Index Number
32+
*/
33+
const UIN_LENGTH = 43;
34+
3035
export function calculateCursor(lastUUID: number): number {
3136

3237
const result = Math.floor(
@@ -41,6 +46,30 @@ export namespace SnowflakeSIvA {
4146
return 0;
4247
}
4348

49+
export interface UUIDRange {
50+
51+
max: number;
52+
53+
min: number;
54+
}
55+
56+
export function getUUIDRangeByMID(mid: number): UUIDRange {
57+
58+
const MAX_MID = Math.pow(2, MID_LENGTH);
59+
60+
if (mid >= MAX_MID || mid < 0) {
61+
62+
throw new RangeError(`mid must be between 0 and ${MAX_MID - 1}.`);
63+
}
64+
65+
const BASE = Math.pow(2, UIN_LENGTH);
66+
67+
return {
68+
max: mid * BASE + BASE - 1,
69+
min: mid * BASE
70+
};
71+
}
72+
4473
export function createGenerateor(opts: {
4574

4675
/**
@@ -68,13 +97,11 @@ export namespace SnowflakeSIvA {
6897

6998
let ret: any;
7099

71-
const MID_FIELD = opts.mid;
72-
73-
const UIN_BASE = Math.pow(2, MID_LENGTH);
100+
const MID_FIELD = opts.mid * Math.pow(2, UIN_LENGTH);
74101

75102
ret = function(): number {
76103

77-
return MID_FIELD + (Date.now() + cursor++) * UIN_BASE;
104+
return MID_FIELD + Date.now() + cursor++;
78105
};
79106

80107
Object.defineProperty(ret, "MACHINE_ID", {

src/tests/snowflake-si-va.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const nextUUID = libuuid.SnowflakeSIvA.createGenerateor({
2727
"cursor": libuuid.SnowflakeSIvA.calculateCursor(1562244321456127)
2828
});
2929

30+
console.log(libuuid.SnowflakeSIvA.getUUIDRangeByMID(10));
3031
console.log(nextUUID());
3132
console.log(nextUUID());
3233
console.log(nextUUID.MACHINE_ID);

0 commit comments

Comments
 (0)