/
whiteboard.go
298 lines (249 loc) · 6.4 KB
/
whiteboard.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package shared
import (
"sort"
"strings"
"sync"
"sync/atomic"
"k8s.io/utils/ptr"
)
const (
// Separator is used to on translating keys to/from flat maps
Separator = "/"
// deleted is a special value to mark a resource id as deleted
deleted = "<deleted>"
)
// FlatMap is a semantic name for string map used for importing and exporting
type FlatMap map[string]string
// Whiteboard is a hierarchical key/value store for the internal state.
// It is safe to be used concurrently.
// Additionally, it can cache objects for use in subsequent flow task.
type Whiteboard interface {
IsEmpty() bool
// GetChild returns a sub-whiteboard for the given key. If no child exists with this name, it is created automatically.
// Each child has its own lock.
GetChild(key string) Whiteboard
// HasChild returns true if there is a non-empty child for that key
HasChild(key string) bool
// GetChildrenKeys returns all children keys
GetChildrenKeys() []string
// Get returns a valid value or nil (never "" or special deleted value)
Get(key string) *string
Set(key, id string)
Delete(key string)
SetPtr(key string, id *string)
IsAlreadyDeleted(key string) bool
SetAsDeleted(key string)
// Keys returns all stored keys, even for deleted ones
Keys() []string
// AsMap returns a map with all valid key/values
AsMap() map[string]string
GetObject(key string) any
SetObject(key string, obj any)
DeleteObject(key string)
HasObject(key string) bool
ObjectKeys() []string
// ImportFromFlatMap reconstructs the hierarchical structure from a flat map containing path-like keys
ImportFromFlatMap(data FlatMap)
// ExportAsFlatMap exports the hierarchical structure to a flat map with path-like keys. Objects are ignored.
ExportAsFlatMap() FlatMap
// CurrentGeneration returns current generation, which increments with any change
CurrentGeneration() int64
}
type whiteboard struct {
sync.Mutex
children map[string]*whiteboard
data map[string]string
objects map[string]any
generation *atomic.Int64
}
var _ Whiteboard = &whiteboard{}
// NewWhiteboard creates an empty whiteboard.
func NewWhiteboard() Whiteboard {
return newWhiteboard(&atomic.Int64{})
}
func newWhiteboard(generation *atomic.Int64) *whiteboard {
return &whiteboard{
children: map[string]*whiteboard{},
data: map[string]string{},
objects: map[string]any{},
generation: generation,
}
}
func (w *whiteboard) CurrentGeneration() int64 {
return w.generation.Load()
}
func (w *whiteboard) IsEmpty() bool {
w.Lock()
defer w.Unlock()
if len(w.data) != 0 || len(w.objects) != 0 {
return false
}
for _, child := range w.children {
if !child.IsEmpty() {
return false
}
}
return true
}
func (w *whiteboard) GetChild(key string) Whiteboard {
return w.getChild(key)
}
func (w *whiteboard) getChild(key string) *whiteboard {
w.Lock()
defer w.Unlock()
child := w.children[key]
if child == nil {
child = newWhiteboard(w.generation)
w.children[key] = child
}
return child
}
func (w *whiteboard) HasChild(key string) bool {
w.Lock()
defer w.Unlock()
child := w.children[key]
return child != nil && !child.IsEmpty()
}
func (w *whiteboard) GetChildrenKeys() []string {
w.Lock()
defer w.Unlock()
return sortedKeys(w.children)
}
func (w *whiteboard) GetObject(key string) any {
w.Lock()
defer w.Unlock()
return w.objects[key]
}
func (w *whiteboard) SetObject(key string, obj any) {
w.Lock()
defer w.Unlock()
w.objects[key] = obj
}
func (w *whiteboard) Keys() []string {
w.Lock()
defer w.Unlock()
return sortedKeys(w.data)
}
func (w *whiteboard) AsMap() map[string]string {
w.Lock()
defer w.Unlock()
m := map[string]string{}
for key, value := range w.data {
if IsValidValue(value) {
m[key] = value
}
}
return m
}
func (w *whiteboard) Get(key string) *string {
w.Lock()
defer w.Unlock()
id := w.data[key]
if !IsValidValue(id) {
return nil
}
return &id
}
func (w *whiteboard) Set(key, id string) {
w.Lock()
defer w.Unlock()
oldId := w.data[key]
if id != "" {
w.data[key] = id
} else {
delete(w.data, key)
}
if oldId != id {
w.modified()
}
}
func (w *whiteboard) SetPtr(key string, id *string) {
w.Set(key, ptr.Deref(id, ""))
}
func (w *whiteboard) Delete(key string) {
w.Lock()
defer w.Unlock()
if _, ok := w.data[key]; ok {
delete(w.data, key)
w.modified()
}
}
func (w *whiteboard) IsAlreadyDeleted(key string) bool {
w.Lock()
defer w.Unlock()
return w.data[key] == deleted
}
func (w *whiteboard) SetAsDeleted(key string) {
w.Set(key, deleted)
}
func (w *whiteboard) ImportFromFlatMap(data FlatMap) {
if data == nil {
return
}
for key, value := range data {
parts := strings.Split(key, Separator)
level := w
for i := 0; i < len(parts)-1; i++ {
level = level.getChild(parts[i])
}
level.Set(parts[len(parts)-1], value)
}
}
func (w *whiteboard) ExportAsFlatMap() FlatMap {
data := map[string]string{}
w.copyMap(data, "")
fillDataFromChildren(data, "", w)
return data
}
func (w *whiteboard) copyMap(data map[string]string, prefix string) {
w.Lock()
defer w.Unlock()
for k, v := range w.data {
data[prefix+k] = v
}
}
func fillDataFromChildren(data map[string]string, parentPrefix string, parent *whiteboard) {
for _, childKey := range parent.GetChildrenKeys() {
child := parent.getChild(childKey)
childPrefix := parentPrefix + childKey + Separator
child.copyMap(data, childPrefix)
fillDataFromChildren(data, childPrefix, child)
}
}
func (w *whiteboard) modified() {
w.generation.Add(1)
}
func sortedKeys[V any](m map[string]V) []string {
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
// IsValidValue returns true if an exported value is valid, i.e. not empty and not special value for deleted.
func IsValidValue(value string) bool {
return value != "" && value != deleted
}
// HasObject returns true if the object with the given key exists.
func (w *whiteboard) HasObject(key string) bool {
w.Lock()
defer w.Unlock()
_, ok := w.objects[key]
return ok
}
// DeleteObject deletes the object with the given key.
func (w *whiteboard) DeleteObject(key string) {
w.Lock()
defer w.Unlock()
delete(w.objects, key)
}
// ObjectKeys returns a slice containing the keys of the Whiteboard in ascending order.
func (w *whiteboard) ObjectKeys() []string {
w.Lock()
defer w.Unlock()
return sortedKeys(w.objects)
}