Snowflake ids (Effect's Snowflake): 64-bit ids that sort by the
millisecond they were made, timestamp - EPOCH_MILLIS in the top bits,
then a 10-bit machine id and a 12-bit sequence.
Values#
| Attribute | Type | Description |
|---|---|---|
Snowflake | type[Snowflake] | |
SnowflakeFromBigInt | BrandSchema[Snowflake, int, Never, Never] | Snowflakes as integers. |
SnowflakeFromString | BrandSchema[Snowflake, str, Never, Never] | Snowflakes encoded as their decimal digits. |
EPOCH_MILLIS | Literal[1735689600000] = 1735689600000 | 2025-01-01T00:00:00Z, the zero of a snowflake's timestamp. |
layer_generator | layer_.Layer[Generator, Never] | A snowflake |
Classes#
Functions#
make#
make(*, machine_id, sequence, timestamp) -> SnowflakeThe snowflake made at timestamp (Unix milliseconds); the machine
id is taken mod 1024 and the sequence mod 4096.
| Parameter | Type | Description |
|---|---|---|
machine_id | MachineId | |
sequence | int | |
timestamp | int |
Returns Snowflake
def make(*, machine_id: MachineId, sequence: int, timestamp: int) -> Snowflake:
"""The snowflake made at ``timestamp`` (Unix milliseconds); the machine
id is taken mod 1024 and the sequence mod 4096."""
return Snowflake((timestamp - EPOCH_MILLIS) << 22 | (machine_id % 1024) << 12 | sequence % 4096)timestamp#
timestamp(snowflake) -> intThe Unix milliseconds snowflake was made at.
| Parameter | Type | Description |
|---|---|---|
snowflake | Snowflake |
Returns int
def timestamp(snowflake: Snowflake) -> int:
"""The Unix milliseconds ``snowflake`` was made at."""
return (snowflake >> 22) + EPOCH_MILLISdate_time#
date_time(snowflake) -> datetime| Parameter | Type | Description |
|---|---|---|
snowflake | Snowflake |
Returns datetime
def date_time(snowflake: Snowflake) -> datetime:
return from_epoch_millis(timestamp(snowflake))machine_id#
machine_id(snowflake) -> MachineId| Parameter | Type | Description |
|---|---|---|
snowflake | Snowflake |
Returns MachineId
def machine_id(snowflake: Snowflake) -> MachineId:
return MachineId((snowflake >> 12) % 1024)sequence#
sequence(snowflake) -> int| Parameter | Type | Description |
|---|---|---|
snowflake | Snowflake |
Returns int
def sequence(snowflake: Snowflake) -> int:
return snowflake % 4096to_parts#
to_parts(snowflake) -> Parts| Parameter | Type | Description |
|---|---|---|
snowflake | Snowflake |
Returns Parts
def to_parts(snowflake: Snowflake) -> Parts:
return Parts(timestamp(snowflake), machine_id(snowflake), sequence(snowflake))make_generator#
make_generator() -> Effect[SnowflakeGenerator]A generator on the current Clock, with a random machine id
until set_machine_id.
Returns Effect[SnowflakeGenerator]
def make_generator() -> Effect[SnowflakeGenerator]:
"""A generator on the current ``Clock``, with a random machine id
until ``set_machine_id``."""
return Clock().map(lambda clock: SnowflakeGenerator(clock, MachineId(random.randrange(1024))))