prometheus/chunks/chunk.go

118 lines
2.4 KiB
Go
Raw Normal View History

2017-04-10 11:59:45 -07:00
// 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.
2016-11-15 01:33:34 -08:00
package chunks
2017-08-08 08:35:34 -07:00
import (
"fmt"
"sync"
"github.com/pkg/errors"
)
2016-11-15 01:33:34 -08:00
2017-04-28 06:41:42 -07:00
// Encoding is the identifier for a chunk encoding.
2016-11-15 01:33:34 -08:00
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 {
Bytes() []byte
2016-12-09 11:45:46 -08:00
Encoding() Encoding
Appender() (Appender, error)
2016-11-15 01:33:34 -08:00
Iterator() Iterator
NumSamples() int
2016-11-15 01:33:34 -08:00
}
2016-12-09 11:45:46 -08:00
// FromData returns a chunk from a byte slice of chunk data.
func FromData(e Encoding, d []byte) (Chunk, error) {
2016-11-15 01:33:34 -08:00
switch e {
case EncXOR:
return &XORChunk{b: &bstream{count: 0, stream: d}}, nil
2016-11-15 01:33:34 -08:00
}
return nil, fmt.Errorf("unknown chunk encoding: %d", e)
}
// Appender adds sample pairs to a chunk.
type Appender interface {
2016-12-31 01:10:27 -08:00
Append(int64, float64)
2016-11-15 01:33:34 -08:00
}
2016-12-31 01:10:27 -08:00
// Iterator is a simple iterator that can only get the next value.
type Iterator interface {
2017-01-02 04:27:52 -08:00
At() (int64, float64)
Err() error
Next() bool
2016-11-15 01:33:34 -08:00
}
2017-08-08 08:35:34 -07:00
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)
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
}