-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependency_network.go
162 lines (127 loc) · 3 KB
/
dependency_network.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package anser
import (
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/deciduosity/anser/model"
"github.com/cdr/grip"
"github.com/tychoish/tarjan"
)
func newDependencyNetwork() model.DependencyNetworker {
return &dependencyNetwork{
network: make(map[string]map[string]struct{}),
group: make(map[string]map[string]struct{}),
}
}
type dependencyNetwork struct {
network map[string]map[string]struct{}
group map[string]map[string]struct{}
mu sync.RWMutex
}
func (n *dependencyNetwork) Add(name string, deps []string) {
n.mu.Lock()
defer n.mu.Unlock()
depSet, ok := n.network[name]
if !ok {
depSet = make(map[string]struct{})
n.network[name] = depSet
}
for _, d := range deps {
depSet[d] = struct{}{}
}
}
func (n *dependencyNetwork) Resolve(name string) []string {
out := []string{}
n.mu.RLock()
defer n.mu.RUnlock()
edges, ok := n.network[name]
if !ok {
return out
}
for e := range edges {
out = append(out, e)
}
return out
}
func (n *dependencyNetwork) All() []string {
out := []string{}
n.mu.RLock()
defer n.mu.RUnlock()
for name := range n.network {
out = append(out, name)
}
return out
}
func (n *dependencyNetwork) Network() map[string][]string {
n.mu.RLock()
defer n.mu.RUnlock()
return n.getNetworkUnsafe()
}
// this is implemented separately from network so we can use it in
// validation and have a sane locking strategy.
func (n *dependencyNetwork) getNetworkUnsafe() map[string][]string {
out := make(map[string][]string)
for node, edges := range n.network {
deps := []string{}
for e := range edges {
deps = append(deps, e)
}
out[node] = deps
}
return out
}
func (n *dependencyNetwork) Validate() error {
dependencies := make(map[string]struct{})
catcher := grip.NewCatcher()
n.mu.RLock()
defer n.mu.RUnlock()
graph := n.getNetworkUnsafe()
for _, edges := range graph {
for _, id := range edges {
dependencies[id] = struct{}{}
}
}
for id := range dependencies {
if _, ok := n.network[id]; !ok {
catcher.Add(fmt.Errorf("dependency %s is not defined", id))
}
}
for _, group := range tarjan.Connections(graph) {
if len(group) > 1 {
catcher.Add(fmt.Errorf("cycle detected between nodes: [%s]",
strings.Join(group, ", ")))
}
}
return catcher.Resolve()
}
func (n *dependencyNetwork) AddGroup(name string, group []string) {
n.mu.Lock()
defer n.mu.Unlock()
groupSet, ok := n.group[name]
if !ok {
groupSet = make(map[string]struct{})
n.group[name] = groupSet
}
for _, g := range group {
groupSet[g] = struct{}{}
}
}
func (n *dependencyNetwork) GetGroup(name string) []string {
out := []string{}
n.mu.RLock()
defer n.mu.RUnlock()
group, ok := n.group[name]
if !ok {
return out
}
for g := range group {
out = append(out, g)
}
return out
}
//////////////////////////////////////////
//
// Output Formats
func (n *dependencyNetwork) MarshalJSON() ([]byte, error) { return json.Marshal(n.Network()) }
func (n *dependencyNetwork) String() string { return fmt.Sprintf("%v", n.Network()) }