|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.Diagnostics;
|
7 | 7 |
|
8 |
| -namespace PSRule.Rules.Azure.Data.Template |
| 8 | +namespace PSRule.Rules.Azure.Data.Template; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// A graph that tracks resource dependencies in scope of a deployment. |
| 12 | +/// </summary> |
| 13 | +internal sealed class ResourceDependencyGraph |
9 | 14 | {
|
| 15 | + private readonly Dictionary<string, Node> _ById = new(StringComparer.OrdinalIgnoreCase); |
| 16 | + private readonly Dictionary<string, Node> _ByName = new(StringComparer.OrdinalIgnoreCase); |
| 17 | + private readonly Dictionary<string, Node> _BySymbolicName = new(StringComparer.OrdinalIgnoreCase); |
| 18 | + private readonly Dictionary<string, List<Node>> _ByCopyName = new(StringComparer.OrdinalIgnoreCase); |
| 19 | + |
| 20 | + [DebuggerDisplay("{Resource.Id}")] |
| 21 | + private sealed class Node(IResourceValue resource, string[] dependencies) |
| 22 | + { |
| 23 | + internal readonly IResourceValue Resource = resource; |
| 24 | + internal readonly string[] Dependencies = dependencies; |
| 25 | + } |
| 26 | + |
10 | 27 | /// <summary>
|
11 |
| - /// A graph that tracks resource dependencies in scope of a deployment. |
| 28 | + /// Sort the provided resources based on dependency graph. |
12 | 29 | /// </summary>
|
13 |
| - internal sealed class ResourceDependencyGraph |
| 30 | + /// <param name="resources">The resources to sort.</param> |
| 31 | + /// <returns>An ordered set of resources.</returns> |
| 32 | + internal IResourceValue[] Sort(IResourceValue[] resources) |
14 | 33 | {
|
15 |
| - private readonly Dictionary<string, Node> _ById = new(StringComparer.OrdinalIgnoreCase); |
16 |
| - private readonly Dictionary<string, Node> _ByName = new(StringComparer.OrdinalIgnoreCase); |
17 |
| - private readonly Dictionary<string, Node> _BySymbolicName = new(StringComparer.OrdinalIgnoreCase); |
18 |
| - private readonly Dictionary<string, List<Node>> _ByCopyName = new(StringComparer.OrdinalIgnoreCase); |
| 34 | + if (resources == null || resources.Length <= 1) |
| 35 | + return resources; |
19 | 36 |
|
20 |
| - [DebuggerDisplay("{Resource.Id}")] |
21 |
| - private sealed class Node |
22 |
| - { |
23 |
| - internal readonly IResourceValue Resource; |
24 |
| - internal readonly string[] Dependencies; |
| 37 | + var stack = new List<IResourceValue>(resources.Length); |
| 38 | + var visited = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
25 | 39 |
|
26 |
| - public Node(IResourceValue resource, string[] dependencies) |
| 40 | + foreach (var resource in resources) |
| 41 | + { |
| 42 | + if (TryGet(resource.Id, out var item)) |
27 | 43 | {
|
28 |
| - Resource = resource; |
29 |
| - Dependencies = dependencies; |
| 44 | + Visit(item, visited, stack); |
30 | 45 | }
|
31 |
| - } |
32 |
| - |
33 |
| - /// <summary> |
34 |
| - /// Sort the provided resources based on dependency graph. |
35 |
| - /// </summary> |
36 |
| - /// <param name="resources">The resources to sort.</param> |
37 |
| - /// <returns>An ordered set of resources.</returns> |
38 |
| - internal IResourceValue[] Sort(IResourceValue[] resources) |
39 |
| - { |
40 |
| - if (resources == null || resources.Length <= 1) |
41 |
| - return resources; |
42 |
| - |
43 |
| - var stack = new List<IResourceValue>(resources.Length); |
44 |
| - var visited = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
45 |
| - |
46 |
| - foreach (var resource in resources) |
| 46 | + else |
47 | 47 | {
|
48 |
| - if (TryGet(resource.Id, out var item)) |
49 |
| - { |
50 |
| - Visit(item, visited, stack); |
51 |
| - } |
52 |
| - else |
53 |
| - { |
54 |
| - stack.Add(resource); |
55 |
| - visited.Add(resource.Id); |
56 |
| - } |
| 48 | + stack.Add(resource); |
| 49 | + visited.Add(resource.Id); |
57 | 50 | }
|
58 |
| - return stack.ToArray(); |
59 | 51 | }
|
| 52 | + return [.. stack]; |
| 53 | + } |
60 | 54 |
|
61 |
| - /// <summary> |
62 |
| - /// Add a resource to the graph. |
63 |
| - /// </summary> |
64 |
| - /// <param name="resource">The resource node to add to the graph.</param> |
65 |
| - /// <param name="dependencies">Any dependencies for the node.</param> |
66 |
| - internal void Track(IResourceValue resource, string[] dependencies) |
67 |
| - { |
68 |
| - if (resource == null) |
69 |
| - return; |
| 55 | + /// <summary> |
| 56 | + /// Add a resource to the graph. |
| 57 | + /// </summary> |
| 58 | + /// <param name="resource">The resource node to add to the graph.</param> |
| 59 | + /// <param name="dependencies">Any dependencies for the node.</param> |
| 60 | + internal void Track(IResourceValue resource, string[] dependencies) |
| 61 | + { |
| 62 | + if (resource == null) |
| 63 | + return; |
70 | 64 |
|
71 |
| - var item = new Node(resource, dependencies); |
72 |
| - _ById[resource.Id] = item; |
73 |
| - _ByName[resource.Name] = item; |
| 65 | + var item = new Node(resource, dependencies); |
| 66 | + _ById[resource.Id] = item; |
| 67 | + _ByName[resource.Name] = item; |
74 | 68 |
|
75 |
| - if (!string.IsNullOrEmpty(resource.SymbolicName)) |
76 |
| - _BySymbolicName[resource.SymbolicName] = item; |
| 69 | + if (!string.IsNullOrEmpty(resource.SymbolicName)) |
| 70 | + _BySymbolicName[resource.SymbolicName] = item; |
77 | 71 |
|
78 |
| - if (resource.Copy != null && !string.IsNullOrEmpty(resource.Copy.Name)) |
| 72 | + if (resource.Copy != null && !string.IsNullOrEmpty(resource.Copy.Name)) |
| 73 | + { |
| 74 | + if (!_ByCopyName.TryGetValue(resource.Copy.Name, out var copyItems)) |
79 | 75 | {
|
80 |
| - if (!_ByCopyName.TryGetValue(resource.Copy.Name, out var copyItems)) |
81 |
| - { |
82 |
| - copyItems = new List<Node>(); |
83 |
| - _ByCopyName[resource.Copy.Name] = copyItems; |
84 |
| - } |
85 |
| - copyItems.Add(item); |
| 76 | + copyItems = []; |
| 77 | + _ByCopyName[resource.Copy.Name] = copyItems; |
86 | 78 | }
|
| 79 | + copyItems.Add(item); |
87 | 80 | }
|
| 81 | + } |
88 | 82 |
|
89 |
| - private bool TryGet(string key, out Node item) |
90 |
| - { |
91 |
| - return _ById.TryGetValue(key, out item) || |
92 |
| - _ByName.TryGetValue(key, out item) || |
93 |
| - _BySymbolicName.TryGetValue(key, out item); |
94 |
| - } |
| 83 | + private bool TryGet(string key, out Node item) |
| 84 | + { |
| 85 | + return _ById.TryGetValue(key, out item) || |
| 86 | + _ByName.TryGetValue(key, out item) || |
| 87 | + _BySymbolicName.TryGetValue(key, out item); |
| 88 | + } |
95 | 89 |
|
96 |
| - /// <summary> |
97 |
| - /// Traverse a node and dependencies. |
98 |
| - /// </summary> |
99 |
| - private void Visit(Node source, HashSet<string> visited, List<IResourceValue> stack) |
100 |
| - { |
101 |
| - if (visited.Contains(source.Resource.Id)) |
102 |
| - return; |
| 90 | + /// <summary> |
| 91 | + /// Traverse a node and dependencies. |
| 92 | + /// </summary> |
| 93 | + private void Visit(Node source, HashSet<string> visited, List<IResourceValue> stack) |
| 94 | + { |
| 95 | + if (visited.Contains(source.Resource.Id)) |
| 96 | + return; |
103 | 97 |
|
104 |
| - visited.Add(source.Resource.Id); |
105 |
| - for (var i = 0; source.Dependencies != null && i < source.Dependencies.Length; i++) |
| 98 | + visited.Add(source.Resource.Id); |
| 99 | + for (var i = 0; source.Dependencies != null && i < source.Dependencies.Length; i++) |
| 100 | + { |
| 101 | + if (_ByCopyName.TryGetValue(source.Dependencies[i], out var countItems)) |
106 | 102 | {
|
107 |
| - if (TryGet(source.Dependencies[i], out var item)) |
| 103 | + foreach (var countItem in countItems) |
108 | 104 | {
|
109 |
| - Visit(item, visited, stack); |
110 |
| - } |
111 |
| - else if (_ByCopyName.TryGetValue(source.Dependencies[i], out var countItems)) |
112 |
| - { |
113 |
| - foreach (var countItem in countItems) |
114 |
| - Visit(countItem, visited, stack); |
| 105 | + Visit(countItem, visited, stack); |
115 | 106 | }
|
116 | 107 | }
|
117 |
| - stack.Add(source.Resource); |
| 108 | + else if (TryGet(source.Dependencies[i], out var item)) |
| 109 | + { |
| 110 | + Visit(item, visited, stack); |
| 111 | + } |
118 | 112 | }
|
| 113 | + stack.Add(source.Resource); |
119 | 114 | }
|
120 | 115 | }
|
0 commit comments