public final class TsidFactory extends Object
This class is used by TsidCreator.
You can use this class if you need to use a specific random generator
strategy. However, most people just need TsidCreator.
TSID is a 64-bit value that has 2 components:
The Random component has 2 sub-parts:
The random component layout depend on the node bits. If the node bits are 10, the counter bits are limited to 12. In this example, the maximum node value is 2^10-1 = 1023 and the maximum counter value is 2^12-1 = 4093. So the maximum TSIDs that can be generated per millisecond per node is 4096.
The time component can be incremented by 1 when the counter overflows. This is done to maintain monotonicity and generation speed. Thus, the time embedded in the TSID can be 1ms or more ahead of the system time when the application is requesting more TSIDs than expected in a millisecond.
If a system property `tsidcreator.node` or environment variable `TSIDCREATOR_NODE` is defined, it's value is utilized as node identifier. One of them should be defined to embed a machine ID in the generated TSID in order to avoid TSID collisions.
If no property or variable is defined, a random node ID is generated at initialization.
The default random generator uses SecureRandom.
| 限定符和类型 | 类和说明 |
|---|---|
static class |
TsidFactory.Builder
A nested class that builds TSID factories.
|
| 构造器和说明 |
|---|
TsidFactory()
It builds a generator with a random node identifier from 0 to 1,023.
|
TsidFactory(int node)
It builds a generator with a given node identifier from 0 to 1,023.
|
| 限定符和类型 | 方法和说明 |
|---|---|
static TsidFactory.Builder |
builder()
Returns a builder object.
|
Tsid |
create()
Returns a TSID.
|
static TsidFactory |
newInstance1024()
Returns a new factory for up to 1024 nodes and 4096 ID/ms.
|
static TsidFactory |
newInstance1024(int node)
Returns a new factory for up to 1024 nodes and 4096 ID/ms.
|
static TsidFactory |
newInstance256()
Returns a new factory for up to 256 nodes and 16384 ID/ms.
|
static TsidFactory |
newInstance256(int node)
Returns a new factory for up to 256 nodes and 16384 ID/ms.
|
static TsidFactory |
newInstance4096()
Returns a new factory for up to 4096 nodes and 1024 ID/ms.
|
static TsidFactory |
newInstance4096(int node)
Returns a new factory for up to 4096 nodes and 1024 ID/ms.
|
public TsidFactory()
If a system property `tsidcreator.node` or environment variable `TSIDCREATOR_NODE` is defined, it's value is used as node identifier.
The node identifier provided by the property or variable is embedded in the generated TSIDs in order to avoid collisions.
Instances built by this constructor can generate up to 4,096 TSIDs per millisecond per node.
public TsidFactory(int node)
Instances built by this constructor can generate up to 4,096 TSIDs per millisecond per node.
The node identifier provided by parameter is embedded in the generated TSIDs in order to avoid collisions.
node - the node identifierpublic static TsidFactory newInstance256()
TsidFactorypublic static TsidFactory newInstance256(int node)
node - the node identifierTsidFactorypublic static TsidFactory newInstance1024()
It is equivalent to new TsidFactory().
TsidFactorypublic static TsidFactory newInstance1024(int node)
It is equivalent to new TsidFactory(int).
node - the node identifierTsidFactorypublic static TsidFactory newInstance4096()
TsidFactorypublic static TsidFactory newInstance4096(int node)
node - the node identifierTsidFactorypublic static TsidFactory.Builder builder()
It is used to build a custom TsidFactory.
public Tsid create()
Copyright © 2024. All rights reserved.