Add FromData function (again)! (#600)

Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com>
This commit is contained in:
Goutham Veeramachaneni 2019-05-06 19:17:26 +05:30 committed by GitHub
parent 4da65c46c9
commit 2ae028114c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -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

View File

@ -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)
}