Index inheritance history lookups
This commit is contained in:
@@ -1,6 +1,57 @@
|
||||
package engine
|
||||
|
||||
import "gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
||||
)
|
||||
|
||||
type inheritanceHistoryIndex struct {
|
||||
paths pathPrefixIndex
|
||||
}
|
||||
|
||||
func newInheritanceHistoryIndex(operations []inheritanceOperation) *inheritanceHistoryIndex {
|
||||
if len(operations) == 0 {
|
||||
return nil
|
||||
}
|
||||
index := &inheritanceHistoryIndex{}
|
||||
for operationID, operation := range operations {
|
||||
if operation.control != nil {
|
||||
index.paths.insert(operation.control.path.String(), operationID)
|
||||
continue
|
||||
}
|
||||
if operation.transfer != nil {
|
||||
index.paths.insert(operation.transfer.to.String(), operationID)
|
||||
}
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
func (index *inheritanceHistoryIndex) matchingOperations(path Path, operations []inheritanceOperation) []inheritanceOperation {
|
||||
if index == nil {
|
||||
return operations
|
||||
}
|
||||
var ids []int
|
||||
seen := make(map[int]struct{})
|
||||
collect := func(id int) {
|
||||
if _, found := seen[id]; found {
|
||||
return
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
pointer := path.String()
|
||||
index.paths.visitAncestors(pointer, collect)
|
||||
index.paths.visitDescendants(pointer, collect)
|
||||
sort.Ints(ids)
|
||||
matching := make([]inheritanceOperation, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
if id >= 0 && id < len(operations) {
|
||||
matching = append(matching, operations[id])
|
||||
}
|
||||
}
|
||||
return matching
|
||||
}
|
||||
|
||||
// Retain source history at original paths. Structural operations are held once
|
||||
// per relationship and projected onto a requested destination path only when
|
||||
|
||||
Reference in New Issue
Block a user