MCPcopy
hub / github.com/grafana/dskit / Set

Method Set

tenant/metadata.go:167–192  ·  view source on GitHub ↗

Set a key value pair. It is O(n) over the size of Metadata and allocates.

(key string, val string)

Source from the content-addressed store, hash-verified

165//
166// It is O(n) over the size of Metadata and allocates.
167func (m *Metadata) Set(key string, val string) {
168 var source strings.Builder
169 inserted := false
170 for prevKey, prevVal := range m.Iter() {
171 if inserted {
172 writeMetadataKV(&source, prevKey, prevVal)
173 continue
174 }
175
176 switch strings.Compare(prevKey, key) {
177 case -1: // prevKey < key
178 writeMetadataKV(&source, prevKey, prevVal)
179 case 0: // prevKey == key, key replaces prevKey
180 writeMetadataKV(&source, key, val)
181 inserted = true
182 case 1: // prevKey > key, insert key before it
183 writeMetadataKV(&source, key, val)
184 writeMetadataKV(&source, prevKey, prevVal)
185 inserted = true
186 }
187 }
188 if !inserted {
189 writeMetadataKV(&source, key, val)
190 }
191 m.source = source.String()
192}
193
194func writeMetadataKV(into *strings.Builder, key, val string) {
195 _ = into.WriteByte(metadataSeparator)

Callers 4

WithMethod · 0.95
TestMetadata_HasFunction · 0.95
TestMetadata_GetFunction · 0.95
TestMetadata_SetFunction · 0.95

Calls 3

IterMethod · 0.95
writeMetadataKVFunction · 0.85
StringMethod · 0.65

Tested by 3

TestMetadata_HasFunction · 0.76
TestMetadata_GetFunction · 0.76
TestMetadata_SetFunction · 0.76