-
Notifications
You must be signed in to change notification settings - Fork 11
/
kv.go
216 lines (170 loc) · 3.61 KB
/
kv.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package kv implements core key-value type as a base for labels and annotations.
package kv
import (
"maps"
"sort"
"gopkg.in/yaml.v3"
"github.com/cosi-project/runtime/pkg/resource/kvutils"
)
// KV is a set free-form of key-value pairs.
//
// Order of keys is not guaranteed.
//
// KV support copy-on-write semantics, so metadata copies share common labels as long as possible.
type KV struct {
m map[string]string
}
// Delete the key.
//
// Deleting the key copies the map, so metadata copies share common storage as long as possible.
func (kv *KV) Delete(key string) {
if _, ok := kv.m[key]; !ok {
// no change
return
}
kvCopy := make(map[string]string, len(kv.m))
for k, v := range kv.m {
if k == key {
continue
}
kvCopy[k] = v
}
kv.m = kvCopy
}
// Set the key value.
//
// Setting the value copies the map, so metadata copies share common storage as long as possible.
func (kv *KV) Set(key, value string) {
if kv.m == nil {
kv.m = map[string]string{}
} else {
v, ok := kv.m[key]
if ok && v == value {
// no change
return
}
kv.m = maps.Clone(kv.m)
}
kv.m[key] = value
}
// Get the value.
func (kv *KV) Get(key string) (string, bool) {
value, ok := kv.m[key]
return value, ok
}
// Raw returns the raw map.
//
// Raw map should not be modified outside of the call.
func (kv *KV) Raw() map[string]string {
return kv.m
}
// Equal checks kv for equality.
func (kv KV) Equal(other KV) bool {
// shortcut for common case of having no keys
if kv.m == nil && other.m == nil {
return true
}
if len(kv.m) != len(other.m) {
return false
}
for k, v := range kv.m {
if v != other.m[k] {
return false
}
}
return true
}
// Empty if there are no pairs.
func (kv KV) Empty() bool {
return len(kv.m) == 0
}
// Len returns the number of keys.
func (kv KV) Len() int {
return len(kv.m)
}
// Keys returns a sorted list of keys.
func (kv KV) Keys() []string {
if kv.Empty() {
return nil
}
keys := make([]string, 0, len(kv.m))
for k := range kv.m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
// ToYAML returns a set of YAML nodes.
func (kv KV) ToYAML(label string) []*yaml.Node {
if kv.Empty() {
return nil
}
nodes := []*yaml.Node{
{
Kind: yaml.ScalarNode,
Value: label,
},
{
Kind: yaml.MappingNode,
Content: make([]*yaml.Node, 0, kv.Len()),
},
}
keys := kv.Keys()
for _, k := range keys {
v, _ := kv.Get(k)
nodes[1].Content = append(nodes[1].Content, &yaml.Node{
Kind: yaml.ScalarNode,
Value: k,
}, &yaml.Node{
Kind: yaml.ScalarNode,
Value: v,
})
}
return nodes
}
// Do executes a function with a temporary copy of the map. It copies the map back only if the function modifies it.
func (kv *KV) Do(ts func(temp kvutils.TempKV)) {
temp := &tempKV{m: kv.m}
ts(temp)
if temp.dirty {
kv.m = temp.m
}
}
type tempKV struct {
m map[string]string
dirty bool
}
func (tmp *tempKV) Delete(key string) {
if _, ok := tmp.m[key]; !ok {
// no change
return
}
if !tmp.dirty {
tmp.m = maps.Clone(tmp.m)
tmp.dirty = true
}
delete(tmp.m, key)
}
func (tmp *tempKV) Set(key, value string) {
v, ok := tmp.m[key]
if ok && v == value {
// no change
return
}
if !tmp.dirty {
if tmp.m == nil {
tmp.m = map[string]string{}
} else {
tmp.m = maps.Clone(tmp.m)
}
tmp.dirty = true
}
tmp.m[key] = value
}
func (tmp *tempKV) Get(key string) (string, bool) {
value, ok := tmp.m[key]
return value, ok
}