2016-11-15 01:33:34 -08:00
|
|
|
package chunks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2016-12-09 11:45:46 -08:00
|
|
|
Encoding() Encoding
|
2016-11-29 13:02:58 -08:00
|
|
|
Appender() (Appender, error)
|
2016-11-15 01:33:34 -08:00
|
|
|
Iterator() Iterator
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-11-29 13:02:58 -08:00
|
|
|
case EncXOR:
|
|
|
|
return &XORChunk{
|
2016-12-20 05:21:50 -08:00
|
|
|
b: &bstream{count: 0, stream: d},
|
2016-12-11 23:11:53 -08:00
|
|
|
num: binary.BigEndian.Uint16(d),
|
2016-11-29 13:02:58 -08:00
|
|
|
}, 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 {
|
2016-11-29 13:02:58 -08:00
|
|
|
Values() (int64, float64)
|
|
|
|
Err() error
|
|
|
|
Next() bool
|
2016-11-15 01:33:34 -08:00
|
|
|
}
|