forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_write.go
396 lines (366 loc) · 10.7 KB
/
node_write.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package graph
import (
"reflect"
"sort"
"github.com/gogo/protobuf/proto"
"github.com/ligato/vpp-agent/plugins/kvscheduler/internal/utils"
)
type node struct {
*nodeR
metaInSync bool
dataUpdated bool
targetsUpdated bool
sourcesUpdated bool
}
// newNode creates a new instance of node, either built from the scratch or
// extending existing nodeR.
func newNode(nodeR *nodeR) *node {
if nodeR == nil {
return &node{
nodeR: newNodeR(),
metaInSync: true,
dataUpdated: true, /* completely new node */
}
}
return &node{
nodeR: nodeR,
metaInSync: true,
}
}
// SetLabel associates given label with this node.
func (node *node) SetLabel(label string) {
node.label = label
node.dataUpdated = true
}
// SetValue associates given value with this node.
func (node *node) SetValue(value proto.Message) {
node.value = value
node.dataUpdated = true
}
// SetFlags associates given flag with this node.
func (node *node) SetFlags(flags ...Flag) {
for _, flag := range flags {
node.flags[flag.GetIndex()] = flag
}
node.dataUpdated = true
}
// DelFlags removes given flag from this node.
func (node *node) DelFlags(flagIndexes ...int) {
for _, idx := range flagIndexes {
node.flags[idx] = nil
}
node.dataUpdated = true
}
// SetMetadataMap chooses metadata map to be used to store the association
// between this node's value label and metadata.
func (node *node) SetMetadataMap(mapName string) {
if node.metadataMap == "" { // cannot be changed
node.metadataMap = mapName
node.dataUpdated = true
node.metaInSync = false
if !node.graph.wCopy {
node.syncMetadata()
}
}
}
// SetMetadata associates given value metadata with this node.
func (node *node) SetMetadata(metadata interface{}) {
node.metadata = metadata
node.dataUpdated = true
node.metaInSync = false
if !node.graph.wCopy {
node.syncMetadata()
}
}
// syncMetadata applies metadata changes into the associated mapping.
func (node *node) syncMetadata() {
if node.metaInSync {
return
}
// update metadata map
if mapping, hasMapping := node.graph.mappings[node.metadataMap]; hasMapping {
if node.metadataAdded {
if node.metadata == nil {
mapping.Delete(node.label)
node.metadataAdded = false
} else {
prevMeta, _ := mapping.GetValue(node.label)
if !reflect.DeepEqual(prevMeta, node.metadata) {
mapping.Update(node.label, node.metadata)
}
}
} else if node.metadata != nil {
mapping.Put(node.label, node.metadata)
node.metadataAdded = true
}
}
node.metaInSync = true
}
// SetTargets updates definitions of all edges pointing from this node.
func (node *node) SetTargets(targetsDef []RelationTargetDef) {
pgraph := node.graph.parent
if pgraph != nil && pgraph.methodTracker != nil {
defer pgraph.methodTracker("Node.SetTargets")()
}
sort.Slice(targetsDef, func(i, j int) bool {
_, order := targetsDef[i].Compare(targetsDef[j])
return order == -1
})
var i,j int
for i < len(targetsDef) || j < len(node.targetsDef) {
var equal bool
var order int
if i < len(targetsDef) && j < len(node.targetsDef) {
equal, order = targetsDef[i].Compare(node.targetsDef[j])
} else if i < len(targetsDef) {
equal = false
order = -1
} else {
equal = false
order = 1
}
if equal {
if targetsDef[i].WithKeySelector() {
// re-run key selector
target := &node.targets[i]
// -> remove obsolete targets
var obsolete []string
for _, key := range target.MatchingKeys.Iterate() {
if targetsDef[i].Selector.KeySelector(key) == false {
obsolete = append(obsolete, key)
}
}
for _, key := range obsolete {
target.MatchingKeys.Del(key)
targetNode := node.graph.nodes[key]
targetNode.removeFromSources(target.Relation, target.Label, node.key)
}
// -> check for new targets
node.iterEveryEdge(targetsDef[i], func(key string) {
targetNode := node.graph.nodes[key]
node.addToTargets(targetNode, target)
})
}
i++
j++
continue
}
// not equal, process the first in the order
if order == 0 {
// updated target definition
target := &node.targets[i]
target.ExpectedKey = expectedKey(targetsDef[i])
// remove previous edges
for _, key := range target.MatchingKeys.Iterate() {
targetNode := node.graph.nodes[key]
targetNode.removeFromSources(target.Relation, target.Label, node.key)
}
node.addDelEdges(node.targetsDef[j], true)
// create new edges
if targetsDef[i].Singleton() {
target.MatchingKeys = utils.NewSingletonKeySet("")
} else {
// selector
target.MatchingKeys = utils.NewSliceBasedKeySet()
}
node.addDelEdges(targetsDef[i], false)
node.iterEveryEdge(targetsDef[i], func(key string) {
targetNode := node.graph.nodes[key]
node.addToTargets(targetNode, target)
})
i++
j++
continue
}
if order == -1 {
// new target definition
node.addDelEdges(targetsDef[i], false)
node.addTargetEntry(i, targetsDef[i].Relation, targetsDef[i].Label,
targetsDef[i].Singleton())
target := &node.targets[i]
target.ExpectedKey = expectedKey(targetsDef[i])
node.iterEveryEdge(targetsDef[i], func(key string) {
targetNode := node.graph.nodes[key]
node.addToTargets(targetNode, target)
})
i++
continue
}
if order == 1 {
// obsolete target definition
target := &node.targets[i]
for _, key := range target.MatchingKeys.Iterate() {
targetNode := node.graph.nodes[key]
targetNode.removeFromSources(target.Relation, target.Label, node.key)
}
node.addDelEdges(node.targetsDef[j], true)
node.removeTargetEntry(i)
j++
continue
}
}
node.targetsDef = targetsDef
node.dataUpdated = true
// check implementation:
if len(node.targetsDef) != len(node.targets) {
panic("SetTargets: len(node.targetsDef) != len(node.targets)")
}
}
// addTargetEntry adds new target entry at the given index.
func (node *node) addTargetEntry(index int, relation, label string, singleton bool) {
node.targets = append(node.targets, Target{})
if index < len(node.targets)-1 {
copy(node.targets[index+1:], node.targets[index:])
}
node.targets[index].Relation = relation
node.targets[index].Label = label
node.targets[index].ExpectedKey = ""
node.targets[index].MatchingKeys = utils.NewSliceBasedKeySet()
if singleton {
node.targets[index].MatchingKeys = utils.NewSingletonKeySet("")
} else {
// selector
node.targets[index].MatchingKeys = utils.NewSliceBasedKeySet()
}
}
// removeTargetEntry removes target entry at the given index
func (node *node) removeTargetEntry(index int) {
if index < len(node.targets)-1 {
copy(node.targets[index:], node.targets[index+1:])
}
node.targets = node.targets[0:len(node.targets)-1]
}
func (node *node) addDelEdges(target RelationTargetDef, del bool) {
cb := node.graph.edgeLookup.addEdge
if del {
cb = node.graph.edgeLookup.delEdge
}
if target.Key != "" {
cb(edge{
targetKey: target.Key,
isPrefix: false,
sourceNode: node.key,
relation: target.Relation,
label: target.Label,
})
} else {
for _, keyPrefix := range target.Selector.KeyPrefixes {
cb(edge{
targetKey: keyPrefix,
isPrefix: true,
sourceNode: node.key,
relation: target.Relation,
label: target.Label,
})
}
if len(target.Selector.KeyPrefixes) == 0 {
cb(edge{
targetKey: "",
isPrefix: true,
sourceNode: node.key,
relation: target.Relation,
label: target.Label,
})
}
}
}
// iterEveryEdge iterates over every outgoing edge.
func (node *node) iterEveryEdge(target RelationTargetDef, cb func(targetKey string)) {
checkTarget := func(key string) {
if !target.WithKeySelector() || target.Selector.KeySelector(key) == true {
cb(key)
}
}
if target.Key != "" {
node.graph.edgeLookup.iterTargets(target.Key, false, checkTarget)
return
}
if len(target.Selector.KeyPrefixes) == 0 {
node.graph.edgeLookup.iterTargets("", true, checkTarget)
}
for _, keyPrefix := range target.Selector.KeyPrefixes {
node.graph.edgeLookup.iterTargets(keyPrefix, true, checkTarget)
}
}
// addToTargets adds node2 into the set of targets for this node.
// Sources of node2 are also updated accordingly.
func (node *node) addToTargets(node2 *node, target *Target) {
// update targets of node
updated := target.MatchingKeys.Add(node2.key)
node.targetsUpdated = updated || node.targetsUpdated
if !updated {
return
}
node.graph.unsaved.Add(node.key)
// update sources of node2
node2.addToSources(node, target)
}
// addToSources adds node2 into the set of sources for this node.
func (node *node) addToSources(node2 *node, target *Target) {
s, idx := node.sources.GetTargetForLabel(target.Relation, target.Label)
if s == nil {
node.sources = append(node.sources, Target{})
if idx < len(node.sources)-1 {
copy(node.sources[idx+1:], node.sources[idx:])
}
node.sources[idx].Relation = target.Relation
node.sources[idx].Label = target.Label
node.sources[idx].MatchingKeys = utils.NewSliceBasedKeySet()
s = &(node.sources[idx])
}
updated := s.MatchingKeys.Add(node2.key)
node.sourcesUpdated = updated || node.sourcesUpdated
if updated {
node.graph.unsaved.Add(node.key)
}
}
// removeFromTarget removes given key from the given target.
// Note: sources are not updated!
func (node *node) removeFromTarget(key, relation, label string) {
target, _ := node.targets.GetTargetForLabel(relation, label)
updated := target.MatchingKeys.Del(key)
node.targetsUpdated = updated || node.targetsUpdated
if updated {
node.graph.unsaved.Add(node.key)
}
}
// removeFromSources removes given key from the sources for the given relation.
func (node *node) removeFromSources(relation, label, key string) {
t, idx := node.sources.GetTargetForLabel(relation, label)
updated := t.MatchingKeys.Del(key)
if updated {
if t.MatchingKeys.Length() == 0 {
if idx < len(node.sources)-1 {
copy(node.sources[idx:], node.sources[idx+1:])
}
node.sources = node.sources[0:len(node.sources)-1]
}
node.sourcesUpdated = true
node.graph.unsaved.Add(node.key)
}
}
func expectedKey(target RelationTargetDef) (expKey string) {
if target.Key != "" {
return target.Key
}
for idx, prefix := range target.Selector.KeyPrefixes {
if idx > 0 {
expKey += " | "
}
expKey += prefix + "*"
}
return expKey
}