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.
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
package chunkenc
|
2016-11-15 01:33:34 -08:00
|
|
|
|
2017-08-08 08:35:34 -07:00
|
|
|
import (
|
2019-05-06 06:47:26 -07:00
|
|
|
"fmt"
|
2020-02-06 07:58:38 -08:00
|
|
|
"math"
|
2017-08-08 08:35:34 -07:00
|
|
|
"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 {
|
2020-02-05 04:07:41 -08:00
|
|
|
// Bytes returns the underlying byte slice of the chunk.
|
2016-11-29 13:02:58 -08:00
|
|
|
Bytes() []byte
|
2020-02-05 04:07:41 -08:00
|
|
|
|
|
|
|
// Encoding returns the encoding type of the chunk.
|
2016-12-09 11:45:46 -08:00
|
|
|
Encoding() Encoding
|
2020-02-05 04:07:41 -08:00
|
|
|
|
|
|
|
// Appender returns an appender to append samples to the chunk.
|
2016-11-29 13:02:58 -08:00
|
|
|
Appender() (Appender, error)
|
2020-02-05 04:07:41 -08:00
|
|
|
|
2019-07-09 02:49:34 -07:00
|
|
|
// The iterator passed as argument is for re-use.
|
|
|
|
// Depending on implementation, the iterator can
|
|
|
|
// be re-used or a new iterator can be allocated.
|
|
|
|
Iterator(Iterator) Iterator
|
2020-02-05 04:07:41 -08:00
|
|
|
|
|
|
|
// NumSamples returns the number of samples in the chunk.
|
2017-07-12 09:31:26 -07:00
|
|
|
NumSamples() int
|
2020-02-05 04:07:41 -08:00
|
|
|
|
|
|
|
// Compact is called whenever a chunk is expected to be complete (no more
|
|
|
|
// samples appended) and the underlying implementation can eventually
|
|
|
|
// optimize the chunk.
|
|
|
|
// There's no strong guarantee that no samples will be appended once
|
|
|
|
// Compact() is called. Implementing this function is optional.
|
2020-01-23 23:44:52 -08:00
|
|
|
Compact()
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-02-26 05:11:55 -08:00
|
|
|
// Iterator iterates over the samples of a time series, in timestamp-increasing order.
|
2016-12-31 01:10:27 -08:00
|
|
|
type Iterator interface {
|
2020-02-07 08:24:17 -08:00
|
|
|
// Next advances the iterator by one.
|
|
|
|
Next() bool
|
|
|
|
// Seek advances the iterator forward to the first sample with the timestamp equal or greater than t.
|
|
|
|
// If current sample found by previous `Next` or `Seek` operation already has this property, Seek has no effect.
|
|
|
|
// Seek returns true, if such sample exists, false otherwise.
|
2020-02-06 07:58:38 -08:00
|
|
|
// Iterator is exhausted when the Seek returns false.
|
|
|
|
Seek(t int64) bool
|
|
|
|
// At returns the current timestamp/value pair.
|
2020-02-11 12:31:17 -08:00
|
|
|
// Before the iterator has advanced At behaviour is unspecified.
|
2017-01-02 04:27:52 -08:00
|
|
|
At() (int64, float64)
|
2020-02-11 08:34:09 -08:00
|
|
|
// Err returns the current error. It should be used only after iterator is
|
2020-02-14 10:48:55 -08:00
|
|
|
// exhausted, that is `Next` or `Seek` returns false.
|
2020-02-06 07:58:38 -08:00
|
|
|
Err() error
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
2017-08-08 08:35:34 -07:00
|
|
|
|
2017-10-20 00:43:52 -07:00
|
|
|
// NewNopIterator returns a new chunk iterator that does not hold any data.
|
|
|
|
func NewNopIterator() Iterator {
|
|
|
|
return nopIterator{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type nopIterator struct{}
|
|
|
|
|
2020-02-06 07:58:38 -08:00
|
|
|
func (nopIterator) Seek(int64) bool { return false }
|
|
|
|
func (nopIterator) At() (int64, float64) { return math.MinInt64, 0 }
|
2017-10-20 00:43:52 -07:00
|
|
|
func (nopIterator) Next() bool { return false }
|
|
|
|
func (nopIterator) Err() error { return nil }
|
|
|
|
|
2019-05-06 06:47:26 -07:00
|
|
|
// Pool is used to create and reuse chunk references to avoid allocations.
|
2017-08-08 08:35:34 -07:00
|
|
|
type Pool interface {
|
|
|
|
Put(Chunk) error
|
|
|
|
Get(e Encoding, b []byte) (Chunk, error)
|
|
|
|
}
|
|
|
|
|
2019-05-06 06:47:26 -07:00
|
|
|
// pool is a memory pool of chunk objects.
|
2017-08-08 08:35:34 -07:00
|
|
|
type pool struct {
|
|
|
|
xor sync.Pool
|
|
|
|
}
|
|
|
|
|
2019-05-06 06:47:26 -07:00
|
|
|
// NewPool returns a new pool.
|
2017-08-08 08:35:34 -07:00
|
|
|
func NewPool() Pool {
|
|
|
|
return &pool{
|
|
|
|
xor: sync.Pool{
|
|
|
|
New: func() interface{} {
|
2019-02-13 14:41:12 -08:00
|
|
|
return &XORChunk{b: bstream{}}
|
2017-08-08 08:35:34 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-08-09 02:10:29 -07:00
|
|
|
// 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.
|
2017-08-08 08:35:34 -07:00
|
|
|
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
|
|
|
|
}
|
2019-05-06 06:47:26 -07:00
|
|
|
|
|
|
|
// FromData returns a chunk from a byte slice of chunk data.
|
|
|
|
// This is there so that users of the library can easily create chunks from
|
|
|
|
// bytes.
|
|
|
|
func FromData(e Encoding, d []byte) (Chunk, error) {
|
|
|
|
switch e {
|
|
|
|
case EncXOR:
|
|
|
|
return &XORChunk{b: bstream{count: 0, stream: d}}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown chunk encoding: %d", e)
|
|
|
|
}
|