Update package tsdb/record for new labels.Labels type

Implement decoding via labels.ScratchBuilder, which we retain and re-use
to reduce memory allocations.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2022-06-28 16:02:08 +01:00
parent 543c318ec2
commit ce2cfad0cb

View file

@ -17,7 +17,6 @@ package record
import ( import (
"math" "math"
"sort"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -182,7 +181,9 @@ type RefMmapMarker struct {
// Decoder decodes series, sample, metadata and tombstone records. // Decoder decodes series, sample, metadata and tombstone records.
// The zero value is ready to use. // The zero value is ready to use.
type Decoder struct{} type Decoder struct {
builder labels.ScratchBuilder
}
// Type returns the type of the record. // Type returns the type of the record.
// Returns RecordUnknown if no valid record type is found. // Returns RecordUnknown if no valid record type is found.
@ -267,14 +268,15 @@ func (d *Decoder) Metadata(rec []byte, metadata []RefMetadata) ([]RefMetadata, e
// DecodeLabels decodes one set of labels from buf. // DecodeLabels decodes one set of labels from buf.
func (d *Decoder) DecodeLabels(dec *encoding.Decbuf) labels.Labels { func (d *Decoder) DecodeLabels(dec *encoding.Decbuf) labels.Labels {
lset := make(labels.Labels, dec.Uvarint()) // TODO: reconsider if this function could be pushed down into labels.Labels to be more efficient.
d.builder.Reset()
for i := range lset { nLabels := dec.Uvarint()
lset[i].Name = dec.UvarintStr() for i := 0; i < nLabels; i++ {
lset[i].Value = dec.UvarintStr() lName := dec.UvarintStr()
lValue := dec.UvarintStr()
d.builder.Add(lName, lValue)
} }
sort.Sort(lset) return d.builder.Labels()
return lset
} }
// Samples appends samples in rec to the given slice. // Samples appends samples in rec to the given slice.
@ -525,12 +527,13 @@ func (e *Encoder) Metadata(metadata []RefMetadata, b []byte) []byte {
// EncodeLabels encodes the contents of labels into buf. // EncodeLabels encodes the contents of labels into buf.
func EncodeLabels(buf *encoding.Encbuf, lbls labels.Labels) { func EncodeLabels(buf *encoding.Encbuf, lbls labels.Labels) {
buf.PutUvarint(len(lbls)) // TODO: reconsider if this function could be pushed down into labels.Labels to be more efficient.
buf.PutUvarint(lbls.Len())
for _, l := range lbls { lbls.Range(func(l labels.Label) {
buf.PutUvarintStr(l.Name) buf.PutUvarintStr(l.Name)
buf.PutUvarintStr(l.Value) buf.PutUvarintStr(l.Value)
} })
} }
// Samples appends the encoded samples to b and returns the resulting slice. // Samples appends the encoded samples to b and returns the resulting slice.