Tanzu GemFire Distributed Types Java API Reference
Interface DSnowflake
-
- All Superinterfaces:
DType
public interface DSnowflake extends DType
DSnowflake is a cluster-unique ID generator based on the Twitter/X Snowflake design. Generated IDs are long primitive values and are k-ordered (roughly ordered). IDs are in the range from 0 to Long.MAX_VALUE.The layout of IDs is as follows:
- timestamp (41 bits by default)
- machine ID (10 bits by default)
- sequence number (12 bits by default)
The timestamp is stored relative to a specific epoch start which, by default, is 1 Jan, 2020 (
DEFAULT_EPOCH_START).The default bit sizes allow for generating 4096 (2^12) sequence IDs per millisecond. If that rate is exceeded, generation will block until the next millisecond.
The current implementation generates the default machine ID from the nanosecond timestamp on the system. Thus, DSnowflakes created on the same system, will have different machine IDs. If required, a custom machine ID can be provided.
A builder pattern is used to create custom DSnowflake instance:
DSnowflake flake = DSnowflake.builder() .withEpochStart(1_000_000) .withMachineId(37) .withSequenceBits(15) .build();
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceDSnowflake.BuilderBuilder used to create custom DSnowflakes.
-
Field Summary
Fields Modifier and Type Field Description static longDEFAULT_EPOCH_STARTThe default epoch start - 1 Jan, 2020static intDEFAULT_MACHINE_BITSstatic intDEFAULT_SEQUENCE_BITS
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static DSnowflake.Builderbuilder()Provides a Builder used to create customized DSnowflakes.default voiddestroy()This is a no-op for DSnowflakes.longnextId()Generate a new ID.long[]parse(long sequence)Parse a sequence ID into its individual components based on the configured bit lengths.
-
-
-
Field Detail
-
DEFAULT_MACHINE_BITS
static final int DEFAULT_MACHINE_BITS
- See Also:
- Constant Field Values
-
DEFAULT_SEQUENCE_BITS
static final int DEFAULT_SEQUENCE_BITS
- See Also:
- Constant Field Values
-
DEFAULT_EPOCH_START
static final long DEFAULT_EPOCH_START
The default epoch start - 1 Jan, 2020- See Also:
- Constant Field Values
-
-
Method Detail
-
nextId
long nextId()
Generate a new ID.- Returns:
- the next ID
-
parse
long[] parse(long sequence)
Parse a sequence ID into its individual components based on the configured bit lengths. Note that the timestamp will be adjusted by the epoch start to reflect the 'correct' time.- Parameters:
sequence- the sequence ID to parse- Returns:
- a long[3] array consisting of timestamp, machine ID and sequence number
-
destroy
default void destroy()
This is a no-op for DSnowflakes. Since nothing is distributed or stored for this type, there are no additional resources to release.
-
builder
static DSnowflake.Builder builder()
Provides a Builder used to create customized DSnowflakes.- Returns:
- a new Builder
-
-