2.3 KiB
TGK Ordering Enforcement
Status: Draft Scope: Canonical ordering rules for TGK/STORE/1 and where to enforce them in adapters and query pipelines.
1. Purpose
TGK/STORE/1 defines a canonical deterministic ordering for all edge and neighbor query results. This document records the rule and the enforcement points so implementations do not drift.
2. Canonical Ordering Rules
2.1 Edge Ordering
All adjacency and scan results MUST be ordered by:
-
For each edge
(edge_ref, body), form:order_key(edge_ref) = hash_id_bytes || digest_bytes -
hash_id_bytesisedge_ref.hash_idencoded as big-endianu16. -
digest_bytesis the rawedge_ref.digest. -
Sort edges in ascending lexicographic order of
order_key(edge_ref).
This is the normative ordering defined by TGK/STORE/1 and is equivalent to sorting by canonical ReferenceBytes.
2.2 Neighbor Ordering
If neighbors is implemented, neighbor results MUST be ordered by:
-
For each node
n, form:node_order_key(n) = hash_id_bytes || digest_bytes -
hash_id_bytesisn.hash_idencoded as big-endianu16. -
digest_bytesis the rawn.digest. -
Sort neighbors in ascending lexicographic order of
node_order_key(n).
3. Enforcement Points
Ordering MUST be enforced by TGK/STORE/1 logic, not by backend defaults.
Acceptable enforcement points:
- Adapter layer (preferred): apply ordering before returning
GraphEdgeViewlists. - Canonical result transformation layer: a shared sorting function used by all adapters.
Disallowed enforcement:
- Relying on JanusGraph traversal order.
- Relying on Elasticsearch sort order.
- Any backend-specific implicit ordering.
4. Test Requirements
Conformance tests MUST verify:
- Adjacency results are ordered per the canonical
(hash_id, digest)rule. scan_edgesresults follow the same ordering per page.neighborsresults use the canonical node ordering if the operation is exposed.- Cross-backend equivalence: the same snapshot yields identical ordered lists.
5. Implementation Notes
- Sorting uses the canonical
ReferenceBytesordering foredge_refandnodereferences. - Ordering is snapshot-relative; it must be stable for a fixed snapshot.
- If ES is used for candidate retrieval, the authoritative layer must re-apply ordering.