Repository navigation
Description
Summary
A unique [Index] promises "one entity per key" with no archetype qualifier. It is enforced per leaf archetype. Two entities in different archetypes of the same tree can hold the same key, and a point lookup then returns both.
Design: claude/design/Indexing/index-scope-and-uniqueness.md (lands with #629)
Reproduction
Two archetypes sharing one component with a unique [Index], same key value:
A) same archetype, duplicate key -> rejected: UniqueConstraintViolationException
B) two archetypes, duplicate key -> ACCEPTED
C) query Key == 777 -> 2 entities returned
The published contract (unique-secondary-index.md) promises UniqueConstraintViolationException on any duplicate. Of seven database systems surveyed, every one either enforces the guarantee or documents that it does not have it. Typhon is the only one that promises and silently does not deliver.
How it got here
Before #629 there were two index homes and therefore two uniqueness scopes:
| Storage | Pre-#629 index home | Unique scope enforced |
|---|---|---|
| Flat | ComponentTable.IndexedFieldInfo.Index — one ComponentTable per component name |
every archetype using the component |
| Cluster | ArchetypeClusterState.IndexSlots / IndexSegment — already per archetype |
that archetype only |
Removing the flat home made the per-archetype scope universal. For a clustered component the gap predates #629; for a flat-stored one #629 narrowed a guarantee that was being kept.
The scope we want
Unique across the declaring archetype's subtree — this is a decision, narrower than the old flat behaviour and wider than today's. Queries are polymorphic over a subtree (ArchetypeRegistry.CollectSubtree), so the subtree is the only scope the schema can already state.
The scope is derived, never annotated: a child archetype copies its parent's component slots and appends only its own, so a component is declared exactly once per inheritance chain and the set of declaring archetypes is always an antichain. Either there is one declarer — and its subtree is an unambiguous scope — or there are several incomparable ones and no archetype tree contains them all. If more than one archetype declares the component, a unique index on it is rejected at registration.
Why probing sibling trees is not the fix
It is O(K) descents per insert and racy — the check and the insert are not atomic, so two concurrent inserts of the same key into different archetypes both pass. No surveyed system does it; see §5.2 of the design for the quotes. Uniqueness needs a single structure.
Plan
- 1. Registration-time validation — reject a unique
[Index]on a component declared by 2+ archetypes. Pure schema check, no storage, ships alone. - 2. The subtree hash — segment layout, WAL participation, crash recovery, rebuild-from-data, checkpoint interaction. This is where the work is; the algorithm is trivial, a new persisted structure is not.
- 3. Enforcement on insert/update via a per-bucket latch on
(key, entity). - 4. Open-time validation that fails loudly, naming the key and both entities, when an existing database already holds duplicates the constraint now forbids.
- 5. Route equality lookups on unique fields to the hash.
- 9.
IndexDataOraclehash = trees = data, in tests andRunStorageIntegrityCheck(). - 10. Lazy cleanup — archetype teardown writes tombstones, a background sweep reclaims.
Steps 6-8 of the design (the parkable cursor for streaming ordered queries) are independent and tracked separately.
Open questions
- Where does the subtree hash live, and who owns its lifetime? Written by every descendant, owned by the declaring archetype — neither
ComponentTablenorArchetypeClusterState. The largest unknown. - Is the registration-time rejection too strict against real schemas?
- Should the scope be narrowable below the declaring archetype via an annotation? Not proposed — it reintroduces the declaration/enforcement gap — but it is the obvious extension request.
Migration hazard
Existing databases may already contain duplicates the new constraint forbids — they were legal until now. Opening such a database must fail loudly naming the offending key and both entities, not throw during a later unrelated write.
What does not change
No on-disk change to any existing index. No leaf value re-encoding, no AllowMultiple change, no query-path rewrite. [Index] keeps its meaning, now correctly enforced. The change is additive.
Activity
#629 Retire the per-ComponentTable secondary index (PR #680)
#629 #678 Close items 12-21 of the index-ownership consolidation revi…
nockawa commentedon Nov 11, 2026
Interim: #1655 refuses a unique [Index] declared on an archetype with descendants until this lands.