Skip to main content

Crate heeranjid

Crate heeranjid 

Source
Expand description

Core HeeRanjID types.

HeeRanjID is designed to let a project start on a single Postgres node with a compact 8-byte integer primary key, and migrate to distributed writers later without rewriting a single ID or schema. HeerId — a 64-bit time-ordered integer whose layout already carries a node_id field — is the primary type you reach for on day one. Going from one writer to many later is a config change (allocate more node_id values, bind each service’s session); existing IDs stay valid.

RanjId is the natural extension when HeerId’s capacity isn’t enough (more than 511 nodes, more than 8,191 IDs per node per millisecond, or sub-millisecond timestamp precision). It’s a UUIDv8-compatible 128-bit identifier stored as uuid, and conversion from HeerId is lossless.

HeerIdDesc and RanjIdDesc are reverse-chronologically-sorted siblings: their raw-bit ordering matches a DESC scan, so newest-first ORDER BY id is served directly by the primary key index without a secondary descending index or a reverse scan. See the asc-to-desc migration playbook at docs/migrations/asc-to-desc.md for the workflow that flips an existing column under live writes.

§Sentinel values

Each of the four ID types exposes a pub const ZERO for use as a placeholder before persistence (e.g., when constructing a row in-memory before INSERT ... RETURNING id assigns the real ID):

let id = HeerId::ZERO;
assert!(id.is_zero());

The sentinel is the wire-zero bit pattern. For HeerId / HeerIdDesc this is provably outside the image of generate_id() because production timestamps are always non-zero. For RanjId / RanjIdDesc this is the RFC 4122 §4.1.7 nil UUID, which is structurally invalid as UUIDv8 — see each type’s documentation for the sentinel-only contract.

For desc-encoded variants, the wire-zero bit pattern does not correspond to logical-zero components; see each type’s ZERO documentation for the decoded component values.

Modules§

mssql_schema
MSSQL schema helpers for the v0.3.0 descending-sort variants.
reverse_order
Bulk cross-direction conversion for HeerIdHeerIdDesc and RanjIdRanjIdDesc. O(n) time, O(1) extra memory — the input Vec’s allocation is reused for the output. See spec §4.6.
schema_shared
Vendor-neutral primitives shared between postgres_schema and mssql_schema.

Structs§

ConversionConflict
HeerId
64-bit time-ordered identifier with ascending raw-bit order.
HeerIdDesc
64-bit time-ordered identifier with descending raw-bit order; the reverse-chronologically-sorted sibling of HeerId.
HeerIdParts
RanjId
128-bit UUIDv8 time-ordered identifier with ascending raw-bit order.
RanjIdDesc
128-bit UUIDv8 time-ordered identifier with descending raw-bit order; the reverse-chronologically-sorted sibling of RanjId.
RanjIdParts

Enums§

ConflictKind
ConversionError
Error
RanjPrecision

Constants§

HEER_NODE_ID_BITS
HEER_SEQUENCE_BITS
HEER_TIMESTAMP_BITS
RANJ_NODE_ID_BITS
RANJ_PRECISION_BITS
RANJ_SEQUENCE_BITS
RANJ_TIMESTAMP_BITS

Functions§

generation_precision
Return the configured generation precision.
try_generation_precision
Parse RANJID_PRECISION without panicking.