diff --git a/CHANGELOG.md b/CHANGELOG.md index 50276edb9..87b642019 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## master / unreleased - [BUGFIX] Calling `Close` more than once on a querier returns an error instead of a panic. + - [ENHANCEMENT] Add (again) FromData function to easily create a chunk from bytes. ## 0.7.1 diff --git a/chunkenc/chunk.go b/chunkenc/chunk.go index 6c1b8b78d..dc566606d 100644 --- a/chunkenc/chunk.go +++ b/chunkenc/chunk.go @@ -14,6 +14,7 @@ package chunkenc import ( + "fmt" "sync" "github.com/pkg/errors" @@ -70,16 +71,18 @@ func (nopIterator) At() (int64, float64) { return 0, 0 } func (nopIterator) Next() bool { return false } func (nopIterator) Err() error { return nil } +// Pool is used to create and reuse chunk references to avoid allocations. type Pool interface { Put(Chunk) error Get(e Encoding, b []byte) (Chunk, error) } -// Pool is a memory pool of chunk objects. +// pool is a memory pool of chunk objects. type pool struct { xor sync.Pool } +// NewPool returns a new pool. func NewPool() Pool { return &pool{ xor: sync.Pool{ @@ -119,3 +122,14 @@ func (p *pool) Put(c Chunk) error { } return nil } + +// 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) +}