2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
2014-09-16 06:47:24 -07:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-09-23 10:21:10 -07:00
|
|
|
// Package codable provides types that implement encoding.BinaryMarshaler and
|
2014-09-16 06:47:24 -07:00
|
|
|
// encoding.BinaryUnmarshaler and functions that help to encode and decode
|
|
|
|
// primitives. The Prometheus storage backend uses them to persist objects to
|
|
|
|
// files and to save objects in LevelDB.
|
|
|
|
//
|
|
|
|
// The encodings used in this package are designed in a way that objects can be
|
|
|
|
// unmarshaled from a continuous byte stream, i.e. the information when to stop
|
|
|
|
// reading is determined by the format. No separate termination information is
|
|
|
|
// needed.
|
|
|
|
//
|
|
|
|
// Strings are encoded as the length of their bytes as a varint followed by
|
|
|
|
// their bytes.
|
|
|
|
//
|
|
|
|
// Slices are encoded as their length as a varint followed by their elements.
|
|
|
|
//
|
|
|
|
// Maps are encoded as the number of mappings as a varint, followed by the
|
|
|
|
// mappings, each of which consists of the key followed by the value.
|
2014-09-23 10:21:10 -07:00
|
|
|
package codable
|
2014-09-10 09:41:52 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2014-09-16 06:47:24 -07:00
|
|
|
"fmt"
|
2014-09-10 09:41:52 -07:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
|
|
|
)
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// A byteReader is an io.ByteReader that also implements the vanilla io.Reader
|
|
|
|
// interface.
|
2014-09-10 09:41:52 -07:00
|
|
|
type byteReader interface {
|
|
|
|
io.Reader
|
|
|
|
io.ByteReader
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// bufPool is a pool for staging buffers. Using a pool allows concurrency-safe
|
|
|
|
// reuse of buffers
|
2014-09-10 09:41:52 -07:00
|
|
|
var bufPool sync.Pool
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// getBuf returns a buffer from the pool. The length of the returned slice is l.
|
2014-09-10 09:41:52 -07:00
|
|
|
func getBuf(l int) []byte {
|
|
|
|
x := bufPool.Get()
|
|
|
|
if x == nil {
|
|
|
|
return make([]byte, l)
|
|
|
|
}
|
|
|
|
buf := x.([]byte)
|
|
|
|
if cap(buf) < l {
|
|
|
|
return make([]byte, l)
|
|
|
|
}
|
|
|
|
return buf[:l]
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// putBuf returns a buffer to the pool.
|
2014-09-10 09:41:52 -07:00
|
|
|
func putBuf(buf []byte) {
|
|
|
|
bufPool.Put(buf)
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// EncodeVarint encodes an int64 as a varint and writes it to an io.Writer.
|
2014-10-24 11:27:27 -07:00
|
|
|
// It returns the number of bytes written.
|
2014-09-16 06:47:24 -07:00
|
|
|
// This is a GC-friendly implementation that takes the required staging buffer
|
|
|
|
// from a buffer pool.
|
2014-10-24 11:27:27 -07:00
|
|
|
func EncodeVarint(w io.Writer, i int64) (int, error) {
|
2014-09-10 09:41:52 -07:00
|
|
|
buf := getBuf(binary.MaxVarintLen64)
|
|
|
|
defer putBuf(buf)
|
|
|
|
|
|
|
|
bytesWritten := binary.PutVarint(buf, i)
|
|
|
|
_, err := w.Write(buf[:bytesWritten])
|
2014-10-24 11:27:27 -07:00
|
|
|
return bytesWritten, err
|
2014-09-10 09:41:52 -07:00
|
|
|
}
|
|
|
|
|
2015-05-06 07:53:12 -07:00
|
|
|
// EncodeUvarint encodes an uint64 as a varint and writes it to an io.Writer.
|
|
|
|
// It returns the number of bytes written.
|
|
|
|
// This is a GC-friendly implementation that takes the required staging buffer
|
|
|
|
// from a buffer pool.
|
|
|
|
func EncodeUvarint(w io.Writer, i uint64) (int, error) {
|
|
|
|
buf := getBuf(binary.MaxVarintLen64)
|
|
|
|
defer putBuf(buf)
|
|
|
|
|
|
|
|
bytesWritten := binary.PutUvarint(buf, i)
|
|
|
|
_, err := w.Write(buf[:bytesWritten])
|
|
|
|
return bytesWritten, err
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// EncodeUint64 writes an uint64 to an io.Writer in big-endian byte-order.
|
|
|
|
// This is a GC-friendly implementation that takes the required staging buffer
|
|
|
|
// from a buffer pool.
|
2014-09-10 09:41:52 -07:00
|
|
|
func EncodeUint64(w io.Writer, u uint64) error {
|
|
|
|
buf := getBuf(8)
|
|
|
|
defer putBuf(buf)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(buf, u)
|
|
|
|
_, err := w.Write(buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// DecodeUint64 reads an uint64 from an io.Reader in big-endian byte-order.
|
|
|
|
// This is a GC-friendly implementation that takes the required staging buffer
|
|
|
|
// from a buffer pool.
|
2014-09-10 09:41:52 -07:00
|
|
|
func DecodeUint64(r io.Reader) (uint64, error) {
|
|
|
|
buf := getBuf(8)
|
|
|
|
defer putBuf(buf)
|
|
|
|
|
|
|
|
if _, err := io.ReadFull(r, buf); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return binary.BigEndian.Uint64(buf), nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// encodeString writes the varint encoded length followed by the bytes of s to
|
|
|
|
// b.
|
2014-09-10 09:41:52 -07:00
|
|
|
func encodeString(b *bytes.Buffer, s string) error {
|
2014-10-24 11:27:27 -07:00
|
|
|
if _, err := EncodeVarint(b, int64(len(s))); err != nil {
|
2014-09-10 09:41:52 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := b.WriteString(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// decodeString decodes a string encoded by encodeString.
|
2014-09-10 09:41:52 -07:00
|
|
|
func decodeString(b byteReader) (string, error) {
|
|
|
|
length, err := binary.ReadVarint(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := getBuf(int(length))
|
|
|
|
defer putBuf(buf)
|
|
|
|
|
|
|
|
if _, err := io.ReadFull(b, buf); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(buf), nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 10:21:10 -07:00
|
|
|
// A Metric is a clientmodel.Metric that implements
|
2014-09-16 06:47:24 -07:00
|
|
|
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
type Metric clientmodel.Metric
|
2014-09-10 09:41:52 -07:00
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (m Metric) MarshalBinary() ([]byte, error) {
|
2014-09-10 09:41:52 -07:00
|
|
|
buf := &bytes.Buffer{}
|
2014-10-24 11:27:27 -07:00
|
|
|
if _, err := EncodeVarint(buf, int64(len(m))); err != nil {
|
2014-09-10 09:41:52 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for l, v := range m {
|
|
|
|
if err := encodeString(buf, string(l)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := encodeString(buf, string(v)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler. It can be used with the
|
2014-09-23 10:21:10 -07:00
|
|
|
// zero value of Metric.
|
|
|
|
func (m *Metric) UnmarshalBinary(buf []byte) error {
|
2014-09-10 09:41:52 -07:00
|
|
|
return m.UnmarshalFromReader(bytes.NewReader(buf))
|
|
|
|
}
|
|
|
|
|
2014-09-23 10:21:10 -07:00
|
|
|
// UnmarshalFromReader unmarshals a Metric from a reader that implements
|
2014-09-16 06:47:24 -07:00
|
|
|
// both, io.Reader and io.ByteReader. It can be used with the zero value of
|
2014-09-23 10:21:10 -07:00
|
|
|
// Metric.
|
|
|
|
func (m *Metric) UnmarshalFromReader(r byteReader) error {
|
2014-09-10 09:41:52 -07:00
|
|
|
numLabelPairs, err := binary.ReadVarint(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-23 10:21:10 -07:00
|
|
|
*m = make(Metric, numLabelPairs)
|
2014-09-10 09:41:52 -07:00
|
|
|
|
|
|
|
for ; numLabelPairs > 0; numLabelPairs-- {
|
|
|
|
ln, err := decodeString(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lv, err := decodeString(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
(*m)[clientmodel.LabelName(ln)] = clientmodel.LabelValue(lv)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 10:21:10 -07:00
|
|
|
// A Fingerprint is a clientmodel.Fingerprint that implements
|
2014-09-16 06:47:24 -07:00
|
|
|
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler. The implementation
|
|
|
|
// depends on clientmodel.Fingerprint to be convertible to uint64. It encodes
|
|
|
|
// the fingerprint as a big-endian uint64.
|
2014-09-23 10:21:10 -07:00
|
|
|
type Fingerprint clientmodel.Fingerprint
|
2014-09-10 09:41:52 -07:00
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (fp Fingerprint) MarshalBinary() ([]byte, error) {
|
2014-09-10 09:41:52 -07:00
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(b, uint64(fp))
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (fp *Fingerprint) UnmarshalBinary(buf []byte) error {
|
|
|
|
*fp = Fingerprint(binary.BigEndian.Uint64(buf))
|
2014-09-10 09:41:52 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 10:21:10 -07:00
|
|
|
// FingerprintSet is a map[clientmodel.Fingerprint]struct{} that
|
|
|
|
// implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler. Its
|
|
|
|
// binary form is identical to that of Fingerprints.
|
|
|
|
type FingerprintSet map[clientmodel.Fingerprint]struct{}
|
2014-09-10 09:41:52 -07:00
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (fps FingerprintSet) MarshalBinary() ([]byte, error) {
|
|
|
|
b := make([]byte, binary.MaxVarintLen64+len(fps)*8)
|
|
|
|
lenBytes := binary.PutVarint(b, int64(len(fps)))
|
|
|
|
offset := lenBytes
|
|
|
|
|
|
|
|
for fp := range fps {
|
|
|
|
binary.BigEndian.PutUint64(b[offset:], uint64(fp))
|
|
|
|
offset += 8
|
|
|
|
}
|
|
|
|
return b[:len(fps)*8+lenBytes], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
|
|
|
|
func (fps *FingerprintSet) UnmarshalBinary(buf []byte) error {
|
|
|
|
numFPs, offset := binary.Varint(buf)
|
|
|
|
if offset <= 0 {
|
|
|
|
return fmt.Errorf("could not decode length of Fingerprints, varint decoding returned %d", offset)
|
|
|
|
}
|
|
|
|
*fps = make(FingerprintSet, numFPs)
|
|
|
|
|
|
|
|
for i := 0; i < int(numFPs); i++ {
|
|
|
|
(*fps)[clientmodel.Fingerprint(binary.BigEndian.Uint64(buf[offset+i*8:]))] = struct{}{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fingerprints is a clientmodel.Fingerprints that implements
|
|
|
|
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler. Its binary form is
|
|
|
|
// identical to that of FingerprintSet.
|
|
|
|
type Fingerprints clientmodel.Fingerprints
|
|
|
|
|
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler.
|
|
|
|
func (fps Fingerprints) MarshalBinary() ([]byte, error) {
|
2014-09-16 06:47:24 -07:00
|
|
|
b := make([]byte, binary.MaxVarintLen64+len(fps)*8)
|
|
|
|
lenBytes := binary.PutVarint(b, int64(len(fps)))
|
2014-09-10 09:41:52 -07:00
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
for i, fp := range fps {
|
|
|
|
binary.BigEndian.PutUint64(b[i*8+lenBytes:], uint64(fp))
|
2014-09-10 09:41:52 -07:00
|
|
|
}
|
2014-09-16 06:47:24 -07:00
|
|
|
return b[:len(fps)*8+lenBytes], nil
|
2014-09-10 09:41:52 -07:00
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (fps *Fingerprints) UnmarshalBinary(buf []byte) error {
|
2014-09-16 06:47:24 -07:00
|
|
|
numFPs, offset := binary.Varint(buf)
|
|
|
|
if offset <= 0 {
|
2014-09-23 10:21:10 -07:00
|
|
|
return fmt.Errorf("could not decode length of Fingerprints, varint decoding returned %d", offset)
|
2014-09-10 09:41:52 -07:00
|
|
|
}
|
2014-09-23 10:21:10 -07:00
|
|
|
*fps = make(Fingerprints, numFPs)
|
2014-09-10 09:41:52 -07:00
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
for i := range *fps {
|
2014-09-10 09:41:52 -07:00
|
|
|
(*fps)[i] = clientmodel.Fingerprint(binary.BigEndian.Uint64(buf[offset+i*8:]))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 10:21:10 -07:00
|
|
|
// LabelPair is a metric.LabelPair that implements
|
2014-09-16 06:47:24 -07:00
|
|
|
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
type LabelPair metric.LabelPair
|
2014-09-10 09:41:52 -07:00
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (lp LabelPair) MarshalBinary() ([]byte, error) {
|
2014-09-10 09:41:52 -07:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if err := encodeString(buf, string(lp.Name)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := encodeString(buf, string(lp.Value)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (lp *LabelPair) UnmarshalBinary(buf []byte) error {
|
2014-09-10 09:41:52 -07:00
|
|
|
r := bytes.NewReader(buf)
|
|
|
|
n, err := decodeString(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v, err := decodeString(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lp.Name = clientmodel.LabelName(n)
|
|
|
|
lp.Value = clientmodel.LabelValue(v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 10:21:10 -07:00
|
|
|
// LabelName is a clientmodel.LabelName that implements
|
2014-09-16 06:47:24 -07:00
|
|
|
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
type LabelName clientmodel.LabelName
|
2014-09-10 09:41:52 -07:00
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (l LabelName) MarshalBinary() ([]byte, error) {
|
2014-09-10 09:41:52 -07:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if err := encodeString(buf, string(l)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (l *LabelName) UnmarshalBinary(buf []byte) error {
|
2014-09-10 09:41:52 -07:00
|
|
|
r := bytes.NewReader(buf)
|
|
|
|
n, err := decodeString(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-23 10:21:10 -07:00
|
|
|
*l = LabelName(n)
|
2014-09-10 09:41:52 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 10:21:10 -07:00
|
|
|
// LabelValueSet is a map[clientmodel.LabelValue]struct{} that implements
|
|
|
|
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler. Its binary form is
|
|
|
|
// identical to that of LabelValues.
|
|
|
|
type LabelValueSet map[clientmodel.LabelValue]struct{}
|
|
|
|
|
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler.
|
|
|
|
func (vs LabelValueSet) MarshalBinary() ([]byte, error) {
|
|
|
|
buf := &bytes.Buffer{}
|
2014-10-24 11:27:27 -07:00
|
|
|
if _, err := EncodeVarint(buf, int64(len(vs))); err != nil {
|
2014-09-23 10:21:10 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for v := range vs {
|
|
|
|
if err := encodeString(buf, string(v)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
|
|
|
|
func (vs *LabelValueSet) UnmarshalBinary(buf []byte) error {
|
|
|
|
r := bytes.NewReader(buf)
|
|
|
|
numValues, err := binary.ReadVarint(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*vs = make(LabelValueSet, numValues)
|
|
|
|
|
|
|
|
for i := int64(0); i < numValues; i++ {
|
|
|
|
v, err := decodeString(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
(*vs)[clientmodel.LabelValue(v)] = struct{}{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LabelValues is a clientmodel.LabelValues that implements
|
|
|
|
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler. Its binary form is
|
|
|
|
// identical to that of LabelValueSet.
|
|
|
|
type LabelValues clientmodel.LabelValues
|
2014-09-10 09:41:52 -07:00
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (vs LabelValues) MarshalBinary() ([]byte, error) {
|
2014-09-10 09:41:52 -07:00
|
|
|
buf := &bytes.Buffer{}
|
2014-10-24 11:27:27 -07:00
|
|
|
if _, err := EncodeVarint(buf, int64(len(vs))); err != nil {
|
2014-09-10 09:41:52 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, v := range vs {
|
|
|
|
if err := encodeString(buf, string(v)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (vs *LabelValues) UnmarshalBinary(buf []byte) error {
|
2014-09-10 09:41:52 -07:00
|
|
|
r := bytes.NewReader(buf)
|
|
|
|
numValues, err := binary.ReadVarint(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-23 10:21:10 -07:00
|
|
|
*vs = make(LabelValues, numValues)
|
2014-09-10 09:41:52 -07:00
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
for i := range *vs {
|
2014-09-10 09:41:52 -07:00
|
|
|
v, err := decodeString(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
(*vs)[i] = clientmodel.LabelValue(v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 10:21:10 -07:00
|
|
|
// TimeRange is used to define a time range and implements
|
2014-09-16 06:47:24 -07:00
|
|
|
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
type TimeRange struct {
|
2014-09-14 06:33:56 -07:00
|
|
|
First, Last clientmodel.Timestamp
|
2014-09-10 09:41:52 -07:00
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (tr TimeRange) MarshalBinary() ([]byte, error) {
|
2014-09-10 09:41:52 -07:00
|
|
|
buf := &bytes.Buffer{}
|
2014-10-24 11:27:27 -07:00
|
|
|
if _, err := EncodeVarint(buf, int64(tr.First)); err != nil {
|
2014-09-10 09:41:52 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-24 11:27:27 -07:00
|
|
|
if _, err := EncodeVarint(buf, int64(tr.Last)); err != nil {
|
2014-09-10 09:41:52 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
|
2014-09-23 10:21:10 -07:00
|
|
|
func (tr *TimeRange) UnmarshalBinary(buf []byte) error {
|
2014-09-10 09:41:52 -07:00
|
|
|
r := bytes.NewReader(buf)
|
|
|
|
first, err := binary.ReadVarint(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
last, err := binary.ReadVarint(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-14 06:33:56 -07:00
|
|
|
tr.First = clientmodel.Timestamp(first)
|
|
|
|
tr.Last = clientmodel.Timestamp(last)
|
2014-09-10 09:41:52 -07:00
|
|
|
return nil
|
|
|
|
}
|