-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
148 lines (126 loc) · 2.73 KB
/
node.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
package goraff
import (
"sync"
"github.com/google/uuid"
)
// Node state represents a key value store for an individual node
type Node struct {
id string
name string
state map[string][][]byte
done bool
notifier ChangeNotifier
subGraphs []*ReadableGraph
mut sync.Mutex
triggeredBy []*ReadableNode
}
func (n *Node) AddSubGraph(s *Graph) {
n.mut.Lock()
defer n.mut.Unlock()
s.Notifier = n.notifier
r := NewReadableGraph(s)
n.subGraphs = append(n.subGraphs, r)
}
func (n *Node) MarkDone() {
n.done = true
}
func (n *Node) Add(key string, value []byte) {
n.mut.Lock()
if n.state == nil {
n.state = make(map[string][][]byte)
}
n.state[key] = append(n.state[key], value)
n.mut.Unlock()
if n.notifier != nil {
n.notifier.Notify(GraphChangeNotification{NodeID: n.name})
}
}
func (n *Node) AddStr(key, value string) {
n.Add(key, []byte(value))
}
func (n *Node) AddStrs(key string, value []string) {
for _, v := range value {
n.AddStr(key, v)
}
}
func (n *Node) Set(key string, value []byte) {
n.mut.Lock()
if n.state == nil {
n.state = make(map[string][][]byte)
}
n.state[key] = [][]byte{value}
n.mut.Unlock()
if n.notifier != nil {
n.notifier.Notify(GraphChangeNotification{NodeID: n.name})
}
}
func (n *Node) SetStr(key, value string) {
n.Set(key, []byte(value))
}
func (n *Node) Get() *ReadableNode {
return &ReadableNode{node: n}
}
// ReadableNode is a read only view of a node state
type ReadableNode struct {
node *Node
}
func (n *ReadableNode) Keys() []string {
keys := []string{}
for k := range n.node.state {
keys = append(keys, k)
}
return keys
}
func (n *ReadableNode) SubGraph() []*ReadableGraph {
if n.node.subGraphs == nil {
return nil
}
return n.node.subGraphs
}
func (s *ReadableNode) First(key string) []byte {
if s.node.state == nil {
return []byte{}
}
s.node.mut.Lock()
defer s.node.mut.Unlock()
if s.node.state[key] == nil {
return []byte{}
}
return s.node.state[key][0]
}
func (s *ReadableNode) FirstStr(key string) string {
return string(s.First(key))
}
func (s *ReadableNode) All(key string) [][]byte {
if s.node.state == nil {
return [][]byte{}
}
s.node.mut.Lock()
defer s.node.mut.Unlock()
if s.node.state[key] == nil {
return [][]byte{}
}
return s.node.state[key]
}
func (s *ReadableNode) AllStr(key string) []string {
vals := []string{}
for _, v := range s.All(key) {
vals = append(vals, string(v))
}
return vals
}
func (s *ReadableNode) ID() string {
if s.node.id == "" {
s.node.id = uuid.NewString()
}
return s.node.id
}
func (s *ReadableNode) Name() string {
return s.node.name
}
func (s *ReadableNode) Done() bool {
return s.node.done
}
func (n *ReadableNode) TriggeredBy() []*ReadableNode {
return n.node.triggeredBy
}