-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgraphmap.go
62 lines (51 loc) · 1.48 KB
/
graphmap.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package eventlogger
import (
"fmt"
"sync"
)
// TODO: remove this if Go ever introduces sync.Map with generics
// graphMap implements a type-safe synchronized map[PipelineID]*linkedNode
type graphMap struct {
m sync.Map
}
// registeredPipeline represents both linked nodes and the registration policy
// for the pipeline.
type registeredPipeline struct {
rootNode *linkedNode
registrationPolicy RegistrationPolicy
}
// Range calls sync.Map.Range
func (g *graphMap) Range(f func(key PipelineID, value *registeredPipeline) bool) {
g.m.Range(func(key, value interface{}) bool {
return f(key.(PipelineID), value.(*registeredPipeline))
})
}
// Store calls sync.Map.Store
func (g *graphMap) Store(id PipelineID, root *registeredPipeline) {
g.m.Store(id, root)
}
// Delete calls sync.Map.Delete
func (g *graphMap) Delete(id PipelineID) {
g.m.Delete(id)
}
// Nodes returns all the nodes referenced by the specified Pipeline
func (g *graphMap) Nodes(id PipelineID) ([]NodeID, error) {
v, ok := g.m.Load(id)
if !ok {
return nil, fmt.Errorf("unable to load root node from underlying data store")
}
pr, ok := v.(*registeredPipeline)
if !ok {
return nil, fmt.Errorf("unable to retrieve pipeline registration (linked nodes and policy) from underlying data store")
}
nodes := pr.rootNode.flatten()
result := make([]NodeID, len(nodes))
i := 0
for k := range nodes {
result[i] = k
i++
}
return result, nil
}