2016-11-15 01:33:34 -08:00
|
|
|
package chunks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Encoding is the identifier for a chunk encoding
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrChunkFull is returned if the remaining size of a chunk cannot
|
|
|
|
// fit the appended data.
|
|
|
|
ErrChunkFull = errors.New("chunk full")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Chunk holds a sequence of sample pairs that can be iterated over and appended to.
|
|
|
|
type Chunk interface {
|
2016-11-29 13:02:58 -08:00
|
|
|
Bytes() []byte
|
|
|
|
Appender() (Appender, error)
|
2016-11-15 01:33:34 -08:00
|
|
|
Iterator() Iterator
|
|
|
|
}
|
|
|
|
|
2016-11-29 13:02:58 -08:00
|
|
|
// FromBytes returns a chunk from a byte slice of chunk data.
|
|
|
|
func FromBytes(d []byte) (Chunk, error) {
|
|
|
|
if len(d) < 1 {
|
2016-11-15 01:33:34 -08:00
|
|
|
return nil, fmt.Errorf("no data")
|
|
|
|
}
|
|
|
|
e := Encoding(d[0])
|
|
|
|
|
|
|
|
switch e {
|
2016-11-29 13:02:58 -08:00
|
|
|
case EncXOR:
|
|
|
|
return &XORChunk{
|
|
|
|
b: &bstream{count: 8},
|
|
|
|
num: binary.LittleEndian.Uint16(d[1:3]),
|
|
|
|
}, nil
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown chunk encoding: %d", e)
|
|
|
|
}
|
|
|
|
|
2016-11-29 13:02:58 -08:00
|
|
|
// Iterator provides iterating access over sample pairs in chunks.
|
2016-11-15 01:33:34 -08:00
|
|
|
type Iterator interface {
|
2016-11-29 13:02:58 -08:00
|
|
|
StreamingIterator
|
2016-11-15 01:33:34 -08:00
|
|
|
|
2016-11-29 13:02:58 -08:00
|
|
|
// Seek(t int64) bool
|
|
|
|
// SeekBefore(t int64) bool
|
|
|
|
// Next() bool
|
|
|
|
// Values() (int64, float64)
|
|
|
|
// Err() error
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Appender adds sample pairs to a chunk.
|
|
|
|
type Appender interface {
|
2016-11-29 13:02:58 -08:00
|
|
|
Append(int64, float64) error
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
|
|
|
|
2016-11-29 13:02:58 -08:00
|
|
|
// StreamingIterator is a simple iterator that can only get the next value.
|
|
|
|
type StreamingIterator interface {
|
|
|
|
Values() (int64, float64)
|
|
|
|
Err() error
|
|
|
|
Next() bool
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|
|
|
|
|
2016-11-29 13:02:58 -08:00
|
|
|
// fancyIterator wraps a StreamingIterator and implements a regular
|
|
|
|
// Iterator with it.
|
|
|
|
type fancyIterator struct {
|
|
|
|
StreamingIterator
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|