Index source operations during history construction
This commit is contained in:
@@ -53,6 +53,7 @@ func (builder *historyBuilder) observeSourceEvidence(root *document.Node, origin
|
||||
if len(applications) == 0 && len(records) == 0 {
|
||||
return
|
||||
}
|
||||
operations := newSourceOperationIndex(builder.model)
|
||||
applicationsBySource := make(map[string]historyPayloadRef)
|
||||
applicationsByLayer := make(map[Layer][]historyPayloadRef)
|
||||
for index := range applications {
|
||||
@@ -88,7 +89,7 @@ func (builder *historyBuilder) observeSourceEvidence(root *document.Node, origin
|
||||
owned.hasApplicationRef = true
|
||||
}
|
||||
for displacedIndex, displaced := range record.displaced {
|
||||
if sourceOriginRepresentedByOperation(builder.model, displaced.path, displaced.origin) {
|
||||
if operations.contains(builder.model, displaced.path, displaced.origin) {
|
||||
continue
|
||||
}
|
||||
definitionPayload := payload
|
||||
@@ -110,12 +111,13 @@ func (builder *historyBuilder) observeSourceEvidence(root *document.Node, origin
|
||||
})
|
||||
}
|
||||
for _, introduced := range record.introduced {
|
||||
builder.addObservation(historyObservation{
|
||||
id := builder.addObservation(historyObservation{
|
||||
path: introduced.path, phase: historyPhaseSources, payload: payload,
|
||||
reportedPath: record.change.Path, role: historyRoleOperation,
|
||||
afterPresent: true, sensitive: introduced.sensitive,
|
||||
attributionRef: attribution,
|
||||
})
|
||||
operations.add(builder.model, id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,7 +126,7 @@ func (builder *historyBuilder) observeSourceEvidence(root *document.Node, origin
|
||||
}
|
||||
walkDocument(root, Path{}, func(path Path, node *document.Node) {
|
||||
origin, found := origins[path.String()]
|
||||
if !found || origin.Layer == "" || sourceOriginRepresentedByOperation(builder.model, path, origin) {
|
||||
if !found || origin.Layer == "" || operations.contains(builder.model, path, origin) {
|
||||
return
|
||||
}
|
||||
payload := builder.addSourceSummary(path, origin)
|
||||
@@ -191,22 +193,72 @@ func sourceApplicationOrigin(application historyApplication, node *document.Node
|
||||
return origin
|
||||
}
|
||||
|
||||
func sourceOriginRepresentedByOperation(model *historyModel, path Path, origin Origin) bool {
|
||||
for _, observationID := range model.observationsByPointer[path.String()] {
|
||||
observation := model.observations[observationID]
|
||||
if observation.phase != historyPhaseSources || observation.role != historyRoleOperation {
|
||||
// sourceOperationIndex is local to source evidence construction. It indexes only
|
||||
// completed after-operation facts, narrowing attribution checks by source ID.
|
||||
// General observation lists also contain all contributions at a path and grow
|
||||
// with every replacement; scanning those lists per replacement is quadratic.
|
||||
type sourceOperationIndex struct {
|
||||
byPath map[string][]int
|
||||
bySource map[sourceOperationSource][]int
|
||||
}
|
||||
|
||||
type sourceOperationSource struct {
|
||||
pointer string
|
||||
sourceID string
|
||||
}
|
||||
|
||||
func newSourceOperationIndex(model *historyModel) *sourceOperationIndex {
|
||||
index := &sourceOperationIndex{
|
||||
byPath: make(map[string][]int),
|
||||
bySource: make(map[sourceOperationSource][]int),
|
||||
}
|
||||
for id := range model.observations {
|
||||
index.add(model, id)
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
func (index *sourceOperationIndex) add(model *historyModel, id int) {
|
||||
if id < 0 {
|
||||
return
|
||||
}
|
||||
observation := model.observations[id]
|
||||
if observation.phase != historyPhaseSources || observation.role != historyRoleOperation || !observation.afterPresent {
|
||||
return
|
||||
}
|
||||
attribution, ok := model.resolveAttribution(observation.attributionRef)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
pointer := observation.path.String()
|
||||
index.byPath[pointer] = append(index.byPath[pointer], id)
|
||||
seen := make(map[string]struct{}, len(attribution.origin.Locations))
|
||||
for _, location := range attribution.origin.Locations {
|
||||
if _, found := seen[location.SourceID]; found {
|
||||
continue
|
||||
}
|
||||
if !observation.afterPresent || !sameConcreteHistoryPath(path, observation.path) {
|
||||
seen[location.SourceID] = struct{}{}
|
||||
key := sourceOperationSource{pointer: pointer, sourceID: location.SourceID}
|
||||
index.bySource[key] = append(index.bySource[key], id)
|
||||
}
|
||||
}
|
||||
|
||||
func (index *sourceOperationIndex) contains(model *historyModel, path Path, origin Origin) bool {
|
||||
pointer := path.String()
|
||||
candidates := index.byPath[pointer]
|
||||
if len(origin.Locations) != 0 {
|
||||
// Every representing origin must include this source, for both exact and
|
||||
// ancestor replacements. The full predicate still checks all locations,
|
||||
// contributor traces, layers, operations and concrete path shapes.
|
||||
candidates = index.bySource[sourceOperationSource{pointer: pointer, sourceID: origin.Locations[0].SourceID}]
|
||||
}
|
||||
for _, id := range candidates {
|
||||
observation := model.observations[id]
|
||||
if !sameConcreteHistoryPath(path, observation.path) {
|
||||
continue
|
||||
}
|
||||
operationOrigin, ok := model.resolveAttribution(observation.attributionRef)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if originRepresentedByChange(path, origin, Change{
|
||||
Path: observation.reportedPath, Origin: operationOrigin.origin,
|
||||
}) {
|
||||
attribution, _ := model.resolveAttribution(observation.attributionRef)
|
||||
if originRepresentedByChange(path, origin, Change{Path: observation.reportedPath, Origin: attribution.origin}) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user