| 1 |
// Copyright (c) Bartłomiej Płotka @bwplotka |
| 2 |
// Licensed under the Apache License 2.0. |
| 3 |
|
| 4 |
// Copyright 2024 Google LLC |
| 5 |
// |
| 6 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
// you may not use this file except in compliance with the License. |
| 8 |
// You may obtain a copy of the License at |
| 9 |
// |
| 10 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
// |
| 12 |
// Unless required by applicable law or agreed to in writing, software |
| 13 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
// See the License for the specific language governing permissions and |
| 16 |
// limitations under the License. |
| 17 |
|
| 18 |
// Copyright 2024 Prometheus Team |
| 19 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 |
// you may not use this file except in compliance with the License. |
| 21 |
// You may obtain a copy of the License at |
| 22 |
// |
| 23 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 24 |
// |
| 25 |
// Unless required by applicable law or agreed to in writing, software |
| 26 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 27 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 |
// See the License for the specific language governing permissions and |
| 29 |
// limitations under the License. |
| 30 |
|
| 31 |
package writev2 |
| 32 |
|
| 33 |
// SymbolsTable implements table for easy symbol use. |
| 34 |
type SymbolsTable struct { |
| 35 |
strings []string |
| 36 |
symbolsMap map[string]uint32 |
| 37 |
} |
| 38 |
|
| 39 |
// NewSymbolTable returns a symbol table. |
| 40 |
func NewSymbolTable() SymbolsTable { |
| 41 |
return SymbolsTable{ |
| 42 |
// Empty string is required as a first element. |
| 43 |
symbolsMap: map[string]uint32{"": 0}, |
| 44 |
strings: []string{""}, |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// Symbolize adds (if not added before) a string to the symbols table, |
| 49 |
// while returning its reference number. |
| 50 |
func (t *SymbolsTable) Symbolize(str string) uint32 { |
| 51 |
if ref, ok := t.symbolsMap[str]; ok { |
| 52 |
return ref |
| 53 |
} |
| 54 |
ref := uint32(len(t.strings)) |
| 55 |
t.strings = append(t.strings, str) |
| 56 |
t.symbolsMap[str] = ref |
| 57 |
return ref |
| 58 |
} |
| 59 |
|
| 60 |
// SymbolizeLabels symbolize Prometheus labels. |
| 61 |
func (t *SymbolsTable) SymbolizeLabels(lbls []string, buf []uint32) []uint32 { |
| 62 |
result := buf[:0] |
| 63 |
for i := 0; i < len(lbls); i += 2 { |
| 64 |
off := t.Symbolize(lbls[i]) |
| 65 |
result = append(result, off) |
| 66 |
off = t.Symbolize(lbls[i+1]) |
| 67 |
result = append(result, off) |
| 68 |
} |
| 69 |
return result |
| 70 |
} |
| 71 |
|
| 72 |
// Symbols returns computes symbols table to put in e.g. Request.Symbols. |
| 73 |
// As per spec, order does not matter. |
| 74 |
func (t *SymbolsTable) Symbols() []string { |
| 75 |
return t.strings |
| 76 |
} |
| 77 |
|
| 78 |
// Reset clears symbols table. |
| 79 |
func (t *SymbolsTable) Reset() { |
| 80 |
// NOTE: Make sure to keep empty symbol. |
| 81 |
t.strings = t.strings[:1] |
| 82 |
for k := range t.symbolsMap { |
| 83 |
if k == "" { |
| 84 |
continue |
| 85 |
} |
| 86 |
delete(t.symbolsMap, k) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// DesymbolizeLabels decodes label references, with given symbols to labels. |
| 91 |
func DesymbolizeLabels(labelRefs []uint32, symbols, buf []string) []string { |
| 92 |
result := buf[:0] |
| 93 |
for i := 0; i < len(labelRefs); i += 2 { |
| 94 |
result = append(result, symbols[labelRefs[i]], symbols[labelRefs[i+1]]) |
| 95 |
} |
| 96 |
return result |
| 97 |
} |