Skip to main content
HexxlaDB uses Morton (Z-order) encoding to pack hexagonal coordinates into compact keys that preserve spatial locality in the B+ tree. This makes ring walks and neighborhood scans efficient prefix operations.

Coordinate packing scheme

To support efficient ring scans and spatial locality:
  1. Convert axial (q, r) to cube coordinates (q, r, s) where s = -q - r
  2. Zigzag-encode each signed coordinate to unsigned values
  3. Compute a Morton (Z-order) code by interleaving the bits of the encoded values
The resulting PackedCoord (typically 128-bit or wider) is used as the primary key.

PackedCoord

Key properties:
  • Spatially close coordinates have nearby keys
  • Ring walks become efficient prefix scans
  • Scales with ring area, not database size
  • High-order bits support future super-hex region identifiers for sharding

Packing and unpacking

Morton ordering

Morton ordering maps spatially close points to nearby keys with high probability. This is expected to benefit neighborhood operations like ring walks and context assembly. Why Morton encoding?
  • Preserves spatial locality in the B+ tree
  • Enables efficient prefix scans for ring enumeration
  • Natural support for hierarchical clustering (super-hex regions)
  • Deterministic and reversible

Key family encoding

Cell keys

Example: cell/0x1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p

Facet keys

Example: facet/0x1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p/1

Edge keys

Example: edge/0x1a2b.../0x2c3d.../see-also

Seam keys

Primary:
Example: seam/01H5X9Y8Z7W6V5U4T3S2R1Q0P9O8N7M Secondary (by cells):
Uses canonical ordering (lower PackedCoord first) so each pair appears once.

Embedding keys

Stores a fixed-dimension float32 vector (little-endian).

HNSW keys

Secondary index keys

Tag index:
Length-prefixed UTF-8 tag + packed coord. Source index:
Length-prefixed source ID + packed coord. Time index:
Week bucket from validity window. Seam time index:
Seam source index:

MVCC version suffixes

When MVCC is enabled (format v2), version suffixes are appended:
This allows multiple committed versions to coexist, with visibility scoped by commit_seq.

Timeline keys

Map wall-clock as_of to snapshots:
Required for ViewAtTime on format v2.

Key ordering

Keys are ordered lexicographically within each family. This enables:
  • Efficient range scans for ring walks
  • Prefix scans for neighborhood queries
  • Ordered iteration for secondary indexes

Zigzag encoding

Signed coordinates are zigzag-encoded before Morton interleaving:
  • Maps signed integers to unsigned integers
  • Preserves ordering: smaller signed values map to smaller unsigned values
  • Enables bit interleaving without sign bit complications

See also