mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
2.9 KiB
132 lines
2.9 KiB
8 years ago
|
// Copyright 2017 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.
|
||
|
|
||
7 years ago
|
package chunkenc
|
||
8 years ago
|
|
||
7 years ago
|
import (
|
||
|
"fmt"
|
||
|
"sync"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
8 years ago
|
|
||
8 years ago
|
// Encoding is the identifier for a chunk encoding.
|
||
8 years ago
|
type Encoding uint8
|
||
|
|
||
|
func (e Encoding) String() string {
|
||
|
switch e {
|
||
|
case EncNone:
|
||
|
return "none"
|
||
|
case EncXOR:
|
||
|
return "XOR"
|
||
|
}
|
||
|
return "<unknown>"
|
||
|
}
|
||
|
|
||
|
// The different available chunk encodings.
|
||
|
const (
|
||
|
EncNone Encoding = iota
|
||
|
EncXOR
|
||
|
)
|
||
|
|
||
|
// Chunk holds a sequence of sample pairs that can be iterated over and appended to.
|
||
|
type Chunk interface {
|
||
8 years ago
|
Bytes() []byte
|
||
8 years ago
|
Encoding() Encoding
|
||
8 years ago
|
Appender() (Appender, error)
|
||
8 years ago
|
Iterator() Iterator
|
||
7 years ago
|
NumSamples() int
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// FromData returns a chunk from a byte slice of chunk data.
|
||
|
func FromData(e Encoding, d []byte) (Chunk, error) {
|
||
8 years ago
|
switch e {
|
||
8 years ago
|
case EncXOR:
|
||
7 years ago
|
return &XORChunk{b: &bstream{count: 0, stream: d}}, nil
|
||
8 years ago
|
}
|
||
|
return nil, fmt.Errorf("unknown chunk encoding: %d", e)
|
||
|
}
|
||
|
|
||
|
// Appender adds sample pairs to a chunk.
|
||
|
type Appender interface {
|
||
8 years ago
|
Append(int64, float64)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// Iterator is a simple iterator that can only get the next value.
|
||
|
type Iterator interface {
|
||
8 years ago
|
At() (int64, float64)
|
||
8 years ago
|
Err() error
|
||
|
Next() bool
|
||
8 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
// NewNopIterator returns a new chunk iterator that does not hold any data.
|
||
|
func NewNopIterator() Iterator {
|
||
|
return nopIterator{}
|
||
|
}
|
||
|
|
||
|
type nopIterator struct{}
|
||
|
|
||
|
func (nopIterator) At() (int64, float64) { return 0, 0 }
|
||
|
func (nopIterator) Next() bool { return false }
|
||
|
func (nopIterator) Err() error { return nil }
|
||
|
|
||
7 years ago
|
type Pool interface {
|
||
|
Put(Chunk) error
|
||
|
Get(e Encoding, b []byte) (Chunk, error)
|
||
|
}
|
||
|
|
||
|
// Pool is a memory pool of chunk objects.
|
||
|
type pool struct {
|
||
|
xor sync.Pool
|
||
|
}
|
||
|
|
||
|
func NewPool() Pool {
|
||
|
return &pool{
|
||
|
xor: sync.Pool{
|
||
|
New: func() interface{} {
|
||
|
return &XORChunk{b: &bstream{}}
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (p *pool) Get(e Encoding, b []byte) (Chunk, error) {
|
||
|
switch e {
|
||
|
case EncXOR:
|
||
|
c := p.xor.Get().(*XORChunk)
|
||
|
c.b.stream = b
|
||
|
c.b.count = 0
|
||
|
return c, nil
|
||
|
}
|
||
|
return nil, errors.Errorf("invalid encoding %q", e)
|
||
|
}
|
||
|
|
||
|
func (p *pool) Put(c Chunk) error {
|
||
|
switch c.Encoding() {
|
||
|
case EncXOR:
|
||
|
xc, ok := c.(*XORChunk)
|
||
7 years ago
|
// This may happen often with wrapped chunks. Nothing we can really do about
|
||
|
// it but returning an error would cause a lot of allocations again. Thus,
|
||
|
// we just skip it.
|
||
7 years ago
|
if !ok {
|
||
|
return nil
|
||
|
}
|
||
|
xc.b.stream = nil
|
||
|
xc.b.count = 0
|
||
|
p.xor.Put(c)
|
||
|
default:
|
||
|
return errors.Errorf("invalid encoding %q", c.Encoding())
|
||
|
}
|
||
|
return nil
|
||
|
}
|