2020-07-31 08:03:02 -07:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package prompb
|
|
|
|
|
2022-11-15 07:29:16 -08:00
|
|
|
import (
|
|
|
|
"sync"
|
2023-11-13 15:53:43 -08:00
|
|
|
|
|
|
|
"golang.org/x/exp/slices"
|
2022-11-15 07:29:16 -08:00
|
|
|
)
|
|
|
|
|
2020-07-31 08:03:02 -07:00
|
|
|
func (m Sample) T() int64 { return m.Timestamp }
|
|
|
|
func (m Sample) V() float64 { return m.Value }
|
2022-11-15 07:29:16 -08:00
|
|
|
|
2023-04-21 11:27:15 -07:00
|
|
|
func (h Histogram) IsFloatHistogram() bool {
|
|
|
|
_, ok := h.GetCount().(*Histogram_CountFloat)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2022-11-15 07:29:16 -08:00
|
|
|
func (r *ChunkedReadResponse) PooledMarshal(p *sync.Pool) ([]byte, error) {
|
|
|
|
size := r.Size()
|
|
|
|
data, ok := p.Get().(*[]byte)
|
|
|
|
if ok && cap(*data) >= size {
|
|
|
|
n, err := r.MarshalToSizedBuffer((*data)[:size])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return (*data)[:n], nil
|
|
|
|
}
|
|
|
|
return r.Marshal()
|
|
|
|
}
|
2023-11-09 08:00:16 -08:00
|
|
|
|
2023-11-28 06:54:32 -08:00
|
|
|
func (m *MinimizedWriteRequestStr) OptimizedMarshal(dst []byte) ([]byte, error) {
|
|
|
|
siz := m.Size()
|
|
|
|
if cap(dst) < siz {
|
|
|
|
dst = make([]byte, siz)
|
|
|
|
}
|
|
|
|
dst = dst[:siz]
|
|
|
|
|
|
|
|
n, err := m.OptimizedMarshalToSizedBuffer(dst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return (dst)[:n], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptimizedMarshalToSizedBuffer is mostly a copy of the generated MarshalToSizedBuffer,
|
|
|
|
// but calls OptimizedMarshalToSizedBuffer on the timeseries.
|
|
|
|
func (m *MinimizedWriteRequestStr) OptimizedMarshalToSizedBuffer(dAtA []byte) (int, error) {
|
|
|
|
i := len(dAtA)
|
|
|
|
_ = i
|
|
|
|
var l int
|
|
|
|
_ = l
|
|
|
|
if m.XXX_unrecognized != nil {
|
|
|
|
i -= len(m.XXX_unrecognized)
|
|
|
|
copy(dAtA[i:], m.XXX_unrecognized)
|
|
|
|
}
|
|
|
|
if len(m.Symbols) > 0 {
|
|
|
|
for iNdEx := len(m.Symbols) - 1; iNdEx >= 0; iNdEx-- {
|
|
|
|
i -= len(m.Symbols[iNdEx])
|
|
|
|
copy(dAtA[i:], m.Symbols[iNdEx])
|
|
|
|
i = encodeVarintRemote(dAtA, i, uint64(len(m.Symbols[iNdEx])))
|
|
|
|
i--
|
2023-11-29 08:17:36 -08:00
|
|
|
dAtA[i] = 0x1a
|
2023-11-28 06:54:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(m.Timeseries) > 0 {
|
|
|
|
for iNdEx := len(m.Timeseries) - 1; iNdEx >= 0; iNdEx-- {
|
|
|
|
{
|
|
|
|
size, err := m.Timeseries[iNdEx].OptimizedMarshalToSizedBuffer(dAtA[:i])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
i -= size
|
|
|
|
i = encodeVarintRemote(dAtA, i, uint64(size))
|
|
|
|
}
|
|
|
|
i--
|
|
|
|
dAtA[i] = 0xa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(dAtA) - i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptimizedMarshalToSizedBuffer is mostly a copy of the generated MarshalToSizedBuffer,
|
|
|
|
// but marshals m.LabelSymbols in place without extra allocations.
|
|
|
|
func (m *MinimizedTimeSeriesStr) OptimizedMarshalToSizedBuffer(dAtA []byte) (int, error) {
|
|
|
|
i := len(dAtA)
|
|
|
|
_ = i
|
|
|
|
var l int
|
|
|
|
_ = l
|
|
|
|
if m.XXX_unrecognized != nil {
|
|
|
|
i -= len(m.XXX_unrecognized)
|
|
|
|
copy(dAtA[i:], m.XXX_unrecognized)
|
|
|
|
}
|
|
|
|
if len(m.Histograms) > 0 {
|
|
|
|
for iNdEx := len(m.Histograms) - 1; iNdEx >= 0; iNdEx-- {
|
|
|
|
{
|
|
|
|
size, err := m.Histograms[iNdEx].MarshalToSizedBuffer(dAtA[:i])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
i -= size
|
|
|
|
i = encodeVarintTypes(dAtA, i, uint64(size))
|
|
|
|
}
|
|
|
|
i--
|
|
|
|
dAtA[i] = 0x22
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(m.Exemplars) > 0 {
|
|
|
|
for iNdEx := len(m.Exemplars) - 1; iNdEx >= 0; iNdEx-- {
|
|
|
|
{
|
|
|
|
size, err := m.Exemplars[iNdEx].MarshalToSizedBuffer(dAtA[:i])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
i -= size
|
|
|
|
i = encodeVarintTypes(dAtA, i, uint64(size))
|
|
|
|
}
|
|
|
|
i--
|
|
|
|
dAtA[i] = 0x1a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(m.Samples) > 0 {
|
|
|
|
for iNdEx := len(m.Samples) - 1; iNdEx >= 0; iNdEx-- {
|
|
|
|
{
|
|
|
|
size, err := m.Samples[iNdEx].MarshalToSizedBuffer(dAtA[:i])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
i -= size
|
|
|
|
i = encodeVarintTypes(dAtA, i, uint64(size))
|
|
|
|
}
|
|
|
|
i--
|
|
|
|
dAtA[i] = 0x12
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.LabelSymbols) > 0 {
|
|
|
|
// This is the trick: encode the varints in reverse order to make it easier
|
|
|
|
// to do it in place. Then reverse the whole thing.
|
|
|
|
var j10 int
|
|
|
|
start := i
|
|
|
|
for _, num := range m.LabelSymbols {
|
|
|
|
for num >= 1<<7 {
|
|
|
|
dAtA[i-1] = uint8(uint64(num)&0x7f | 0x80)
|
|
|
|
num >>= 7
|
|
|
|
i--
|
|
|
|
j10++
|
|
|
|
}
|
|
|
|
dAtA[i-1] = uint8(num)
|
|
|
|
i--
|
|
|
|
j10++
|
|
|
|
}
|
|
|
|
slices.Reverse(dAtA[i:start])
|
|
|
|
// --- end of trick
|
|
|
|
|
|
|
|
i = encodeVarintTypes(dAtA, i, uint64(j10))
|
|
|
|
i--
|
|
|
|
dAtA[i] = 0xa
|
|
|
|
}
|
|
|
|
return len(dAtA) - i, nil
|
|
|
|
}
|