Build item policies without repeated accumulated copies

This commit is contained in:
2026-10-24 01:20:12 +00:00
parent 1a673753b9
commit fafc28421f
3 changed files with 85 additions and 8 deletions

View File

@@ -960,7 +960,12 @@ func projectNestedCollectionPolicies(composed *composedDocument, scope acquisiti
if composed == nil {
return
}
baseOffsets := make(map[string]int)
type itemPolicyProjection struct {
destination Path
policy collectionPolicy
base int
}
projected := make(map[string]*itemPolicyProjection)
var prefixLengths map[string]int
for sourceIndex, contribution := range scope.contributions {
if prefixLengths != nil && sourceIndex > 0 {
@@ -983,17 +988,21 @@ func projectNestedCollectionPolicies(composed *composedDocument, scope acquisiti
}
itemIndex = prefixLengths[key]
}
base, initialized := baseOffsets[key]
policy, _ := composed.collectionPolicies.at(contribution.destination)
working, initialized := projected[key]
if !initialized {
base = len(policy.recordOrder)
baseOffsets[key] = base
// Read one owned copy per destination, then add each incoming
// record without recopying earlier records and their scopes.
policy, _ := composed.collectionPolicies.at(contribution.destination)
working = &itemPolicyProjection{destination: contribution.destination, policy: policy, base: len(policy.recordOrder)}
projected[key] = working
}
policy.setRecordPolicyAt(base+itemIndex, collectionRecordPolicy{
working.policy.setRecordPolicyAt(working.base+itemIndex, collectionRecordPolicy{
owner: newCollectionRecordPolicy().owner,
scope: contribution.nested.collectionPolicies,
})
composed.collectionPolicies.set(contribution.destination, policy)
}
for _, working := range projected {
composed.collectionPolicies.set(working.destination, working.policy)
}
}