Skip to content

Commit a383e95

Browse files
committed
feat(algo): added bulkGenerate method for Snowflake algorithms
1 parent ceb0d41 commit a383e95

21 files changed

+411
-51
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes Logs
22

3+
## v2.1.0
4+
5+
- feat(algo): added `bulkGenerate` for both Snowflake and Snowflake-SI algorithms.
6+
37
## v2.0.3
48

59
- fix(algo): reverted the update of `v2.0.2` that removed the reset of sequence number to 0.

docs/en-US/api/Snowflake/classes/SnowflakeGenerator.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Class: SnowflakeGenerator
44

5-
Defined in: [Snowflake.ts:134](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L134)
5+
Defined in: [Snowflake.ts:162](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L162)
66

77
The class for generating Snowflake IDs.
88

@@ -12,7 +12,7 @@ The class for generating Snowflake IDs.
1212

1313
> **new SnowflakeGenerator**(`opts`): `SnowflakeGenerator`
1414
15-
Defined in: [Snowflake.ts:186](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L186)
15+
Defined in: [Snowflake.ts:214](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L214)
1616

1717
#### Parameters
1818

@@ -30,7 +30,7 @@ Defined in: [Snowflake.ts:186](https://github.com/litert/uuid.js/blob/master/src
3030

3131
> `readonly` **epoch**: `number`
3232
33-
Defined in: [Snowflake.ts:176](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L176)
33+
Defined in: [Snowflake.ts:204](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L204)
3434

3535
The epoch of the generator.
3636

@@ -48,7 +48,7 @@ The value is recommended to be the time when the service/application goes online
4848

4949
> `readonly` **machineId**: `number`
5050
51-
Defined in: [Snowflake.ts:167](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L167)
51+
Defined in: [Snowflake.ts:195](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L195)
5252

5353
The identifier of the machine that generates the UUID.
5454

@@ -65,7 +65,7 @@ so that the generated IDs will not conflict with each other.
6565

6666
> `readonly` `static` **MAX\_MACHINE\_ID**: `1023` = `1023`
6767
68-
Defined in: [Snowflake.ts:157](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L157)
68+
Defined in: [Snowflake.ts:185](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L185)
6969

7070
The maximum machine ID that can be used by the generator.
7171

@@ -75,17 +75,50 @@ The maximum machine ID that can be used by the generator.
7575

7676
> `readonly` `static` **MIN\_MACHINE\_ID**: `0` = `0`
7777
78-
Defined in: [Snowflake.ts:151](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L151)
78+
Defined in: [Snowflake.ts:179](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L179)
7979

8080
The minimum machine ID that can be used by the generator.
8181

8282
## Methods
8383

84+
### bulkGenerate()
85+
86+
> **bulkGenerate**(`qty`, `opts`): `Promise`\<`bigint`[]\>
87+
88+
Defined in: [Snowflake.ts:286](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L286)
89+
90+
Generate a bulk of Snowflake IDs.
91+
92+
This method helps generate batch of Snowflake IDs, handling the time
93+
reversed and sequence overflow scenarios automatically.
94+
95+
#### Parameters
96+
97+
##### qty
98+
99+
`number`
100+
101+
The quantity of IDs to generate.
102+
103+
##### opts
104+
105+
[`IBulkGenerationOptions`](../interfaces/IBulkGenerationOptions.md) = `DEFAULT_BULK_OPTIONS`
106+
107+
The options for bulk generation.
108+
109+
#### Returns
110+
111+
`Promise`\<`bigint`[]\>
112+
113+
A promise that resolves to an array of Snowflake IDs.
114+
115+
***
116+
84117
### generate()
85118

86119
> **generate**(): `bigint`
87120
88-
Defined in: [Snowflake.ts:256](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L256)
121+
Defined in: [Snowflake.ts:334](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L334)
89122

90123
Generate the next Snowflake ID, based on the current time and the next sequence number.
91124

@@ -113,7 +146,7 @@ If the current time is before the epoch.
113146

114147
> **generateBy**(`timestamp`, `sequence`): `bigint`
115148
116-
Defined in: [Snowflake.ts:323](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L323)
149+
Defined in: [Snowflake.ts:401](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L401)
117150

118151
Generate a Snowflake ID by specifying the timestamp and sequence number.
119152

docs/en-US/api/Snowflake/enumerations/ESnowflakeSequenceStrategy.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Enumeration: ESnowflakeSequenceStrategy
44

5-
Defined in: [Snowflake.ts:55](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L55)
5+
Defined in: [Snowflake.ts:83](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L83)
66

77
How should the generator handle the sequence number when the time changes.
88

@@ -12,7 +12,7 @@ How should the generator handle the sequence number when the time changes.
1212

1313
> **KEEP\_CURRENT**: `1`
1414
15-
Defined in: [Snowflake.ts:72](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L72)
15+
Defined in: [Snowflake.ts:100](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L100)
1616

1717
Keep the sequence number unchanged when the time changes.
1818

@@ -26,7 +26,7 @@ Keep the sequence number unchanged when the time changes.
2626

2727
> **RESET**: `0`
2828
29-
Defined in: [Snowflake.ts:63](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L63)
29+
Defined in: [Snowflake.ts:91](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L91)
3030

3131
Reset the sequence number to 0 when the time changes.
3232

docs/en-US/api/Snowflake/enumerations/ESnowflakeTimeReversedStrategy.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Enumeration: ESnowflakeTimeReversedStrategy
44

5-
Defined in: [Snowflake.ts:21](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L21)
5+
Defined in: [Snowflake.ts:49](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L49)
66

77
The strategy for handling time reversal events in Snowflake ID generation.
88

@@ -12,7 +12,7 @@ The strategy for handling time reversal events in Snowflake ID generation.
1212

1313
> **THROW\_ERROR**: `0`
1414
15-
Defined in: [Snowflake.ts:30](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L30)
15+
Defined in: [Snowflake.ts:58](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L58)
1616

1717
Throw an error to stop the generator from generating IDs.
1818

@@ -26,7 +26,7 @@ Throw an error to stop the generator from generating IDs.
2626

2727
> **USE\_PREVIOUS\_TIME**: `2`
2828
29-
Defined in: [Snowflake.ts:49](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L49)
29+
Defined in: [Snowflake.ts:77](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L77)
3030

3131
Use the previous time as the current time to generate IDs.
3232

@@ -41,7 +41,7 @@ Use the previous time as the current time to generate IDs.
4141

4242
> **USE\_REVERSED\_TIME**: `1`
4343
44-
Defined in: [Snowflake.ts:39](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L39)
44+
Defined in: [Snowflake.ts:67](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L67)
4545

4646
Use the reversed time as the current time to generate IDs.
4747

docs/en-US/api/Snowflake/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
## Interfaces
1515

16+
- [IBulkGenerationOptions](interfaces/IBulkGenerationOptions.md)
1617
- [ISnowflakeOptions](interfaces/ISnowflakeOptions.md)
1718

1819
## Type Aliases
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[Documents for @litert/uuid](../../index.md) / [Snowflake](../index.md) / IBulkGenerationOptions
2+
3+
# Interface: IBulkGenerationOptions
4+
5+
Defined in: [Snowflake.ts:22](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L22)
6+
7+
The options for bulk generation of snowflake IDs.
8+
9+
## Properties
10+
11+
### maxRetries?
12+
13+
> `optional` **maxRetries**: `number`
14+
15+
Defined in: [Snowflake.ts:31](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L31)
16+
17+
The maximum number of sequential retries for generating IDs in bulk.
18+
19+
If the failures exceeded this limit, the original error will be thrown.
20+
21+
#### Default
22+
23+
```ts
24+
3
25+
```
26+
27+
***
28+
29+
### retryDelayMs?
30+
31+
> `optional` **retryDelayMs**: `number`
32+
33+
Defined in: [Snowflake.ts:38](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L38)
34+
35+
The delay between each retry attempt in milliseconds.
36+
37+
#### Default
38+
39+
```ts
40+
1
41+
```

docs/en-US/api/Snowflake/interfaces/ISnowflakeOptions.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Interface: ISnowflakeOptions
44

5-
Defined in: [Snowflake.ts:83](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L83)
5+
Defined in: [Snowflake.ts:111](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L111)
66

77
The options for the standard Snowflake ID generator.
88

@@ -16,7 +16,7 @@ The options for the standard Snowflake ID generator.
1616

1717
> `readonly` `optional` **epoch**: `number`
1818
19-
Defined in: [Snowflake.ts:176](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L176)
19+
Defined in: [Snowflake.ts:204](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L204)
2020

2121
The epoch of the generator.
2222

@@ -38,7 +38,7 @@ The value is recommended to be the time when the service/application goes online
3838

3939
> `readonly` **machineId**: `number`
4040
41-
Defined in: [Snowflake.ts:167](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L167)
41+
Defined in: [Snowflake.ts:195](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L195)
4242

4343
The identifier of the machine that generates the UUID.
4444

@@ -59,7 +59,7 @@ so that the generated IDs will not conflict with each other.
5959

6060
> `optional` **onTimeChanged**: [`ESnowflakeSequenceStrategy`](../enumerations/ESnowflakeSequenceStrategy.md) \| [`ISnowflakeUpdateSequenceOnTimeChanged`](../type-aliases/ISnowflakeUpdateSequenceOnTimeChanged.md)\<`bigint`\>
6161
62-
Defined in: [Snowflake.ts:101](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L101)
62+
Defined in: [Snowflake.ts:129](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L129)
6363

6464
The strategy for handling the sequence number when the time changes.
6565

@@ -78,7 +78,7 @@ ESnowflakeSequenceStrategy.RESET
7878

7979
> `optional` **onTimeReversed**: [`ESnowflakeTimeReversedStrategy`](../enumerations/ESnowflakeTimeReversedStrategy.md)
8080
81-
Defined in: [Snowflake.ts:91](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L91)
81+
Defined in: [Snowflake.ts:119](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L119)
8282

8383
The strategy for handling time reversal events.
8484

@@ -94,7 +94,7 @@ ESnowflakeTimeReversedStrategy.THROW_ERROR
9494

9595
> `optional` **sequenceResetThreshold**: `number`
9696
97-
Defined in: [Snowflake.ts:111](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L111)
97+
Defined in: [Snowflake.ts:139](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L139)
9898

9999
When the sequence strategy is set to `ESnowflakeSequenceStrategy.RESET`,
100100
this option specifies the threshold for resetting the sequence number.

docs/en-US/api/Snowflake/type-aliases/ISnowflakeUpdateSequenceOnTimeChanged.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
> **ISnowflakeUpdateSequenceOnTimeChanged**\<`T`\> = (`current`) => `T`
66
7-
Defined in: [Snowflake.ts:78](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L78)
7+
Defined in: [Snowflake.ts:106](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L106)
88

99
The strategy for handling time changes in Snowflake ID generation.
1010

docs/en-US/api/Snowflake/variables/SNOWFLAKE_DEFAULT_TIME_CHANGED_STRATEGY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
> `const` **SNOWFLAKE\_DEFAULT\_TIME\_CHANGED\_STRATEGY**: [`RESET`](../enumerations/ESnowflakeSequenceStrategy.md#reset) = `ESnowflakeSequenceStrategy.RESET`
66
7-
Defined in: [Snowflake.ts:124](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L124)
7+
Defined in: [Snowflake.ts:152](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L152)
88

99
The default strategy for the time changed event,
1010
which reset the sequence number to 0.

docs/en-US/api/Snowflake/variables/SNOWFLAKE_DEFAULT_TIME_REVERSAL_STRATEGY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
> `const` **SNOWFLAKE\_DEFAULT\_TIME\_REVERSAL\_STRATEGY**: [`THROW_ERROR`](../enumerations/ESnowflakeTimeReversedStrategy.md#throw_error) = `ESnowflakeTimeReversedStrategy.THROW_ERROR`
66
7-
Defined in: [Snowflake.ts:118](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L118)
7+
Defined in: [Snowflake.ts:146](https://github.com/litert/uuid.js/blob/master/src/lib/Snowflake.ts#L146)
88

99
The default strategy for the time reversal event,
1010
which throws an `E_TIME_REVERSED` error.

0 commit comments

Comments
 (0)