|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "yunion.io/x/pkg/errors" |
| 5 | +) |
| 6 | + |
| 7 | +type GetObjIdName[T any] func(T) string |
| 8 | +type GetDependencies[T any] func(T) []string |
| 9 | + |
| 10 | +func TopologicalSortContainers[T any](objs []T, getName GetObjIdName[T], getDependencies GetDependencies[T]) error { |
| 11 | + if len(objs) == 0 { |
| 12 | + return nil |
| 13 | + } |
| 14 | + |
| 15 | + // Build a dependency graph and an in-degree table |
| 16 | + graph := make(map[string][]string) |
| 17 | + inDegree := make(map[string]int) |
| 18 | + |
| 19 | + // init graph and inDegree |
| 20 | + for _, obj := range objs { |
| 21 | + inDegree[getName(obj)] = 0 |
| 22 | + } |
| 23 | + for _, obj := range objs { |
| 24 | + oName := getName(obj) |
| 25 | + for _, dep := range getDependencies(obj) { |
| 26 | + if _, exists := inDegree[dep]; !exists { |
| 27 | + return errors.Errorf("The dependent container %s does not exist.", dep) |
| 28 | + } |
| 29 | + graph[dep] = append(graph[dep], oName) |
| 30 | + inDegree[oName]++ |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // Topological sorting: use a queue to process nodes with an in-degree of 0 |
| 35 | + queue := []string{} |
| 36 | + for name, degree := range inDegree { |
| 37 | + if degree == 0 { |
| 38 | + queue = append(queue, name) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + sorted := []string{} |
| 43 | + for len(queue) > 0 { |
| 44 | + current := queue[0] |
| 45 | + queue = queue[1:] |
| 46 | + sorted = append(sorted, current) |
| 47 | + |
| 48 | + // Decrease the in-degree of neighboring nodes |
| 49 | + for _, neighbor := range graph[current] { |
| 50 | + inDegree[neighbor]-- |
| 51 | + if inDegree[neighbor] == 0 { |
| 52 | + queue = append(queue, neighbor) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Check whether a cycle exists |
| 58 | + if len(sorted) != len(objs) { |
| 59 | + return errors.Errorf("There is a circular dependency among the dependencies.") |
| 60 | + } |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +type DependencyTopoGraph[T any] struct { |
| 66 | + Graph map[string][]string |
| 67 | + Degree map[string]int |
| 68 | + Leafs []string // containers whose in-degree is zero |
| 69 | +} |
| 70 | + |
| 71 | +func NewDependencyTopoGraph[T any]( |
| 72 | + objs []T, |
| 73 | + getId GetObjIdName[T], |
| 74 | + getName GetObjIdName[T], |
| 75 | + getDependencies GetDependencies[T], |
| 76 | +) (*DependencyTopoGraph[T], error) { |
| 77 | + depGraph := &DependencyTopoGraph[T]{ |
| 78 | + Graph: make(map[string][]string), |
| 79 | + Degree: make(map[string]int), |
| 80 | + Leafs: make([]string, 0, len(objs)), |
| 81 | + } |
| 82 | + |
| 83 | + nameToUUID := make(map[string]string) |
| 84 | + |
| 85 | + for _, obj := range objs { |
| 86 | + uuid := getId(obj) |
| 87 | + name := getName(obj) |
| 88 | + |
| 89 | + depGraph.Degree[uuid] = 0 |
| 90 | + nameToUUID[name] = uuid |
| 91 | + } |
| 92 | + |
| 93 | + for _, obj := range objs { |
| 94 | + for _, dep := range getDependencies(obj) { |
| 95 | + depId := nameToUUID[dep] |
| 96 | + uuid := getId(obj) |
| 97 | + depGraph.Graph[depId] = append(depGraph.Graph[depId], uuid) |
| 98 | + depGraph.Degree[uuid]++ |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + for uuid, indegree := range depGraph.Degree { |
| 103 | + if indegree == 0 { |
| 104 | + depGraph.Leafs = append(depGraph.Leafs, uuid) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return depGraph, nil |
| 109 | +} |
| 110 | + |
| 111 | +type FetchObjById[T any] func(string) T |
| 112 | + |
| 113 | +func (dep *DependencyTopoGraph[T]) GetNextBatch(fetchById FetchObjById[T]) []T { |
| 114 | + if len(dep.Leafs) == 0 { |
| 115 | + return nil |
| 116 | + } |
| 117 | + |
| 118 | + objs := make([]T, 0, len(dep.Leafs)) |
| 119 | + |
| 120 | + nextLeafs := make([]string, 0) |
| 121 | + |
| 122 | + for _, uuid := range dep.Leafs { |
| 123 | + objs = append(objs, fetchById(uuid)) |
| 124 | + for _, neighbor := range dep.Graph[uuid] { |
| 125 | + dep.Degree[neighbor]-- |
| 126 | + if dep.Degree[neighbor] == 0 { |
| 127 | + nextLeafs = append(nextLeafs, neighbor) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // log.Infof("Get next batch: %s", dep.Leafs) |
| 133 | + |
| 134 | + dep.Leafs = nextLeafs |
| 135 | + |
| 136 | + return objs |
| 137 | +} |
0 commit comments