Add series file reader

This commit is contained in:
Fabian Reinartz 2016-12-11 15:50:00 +01:00
parent 5e02e28f9c
commit 81b4d570ad
2 changed files with 63 additions and 1 deletions

62
reader.go Normal file
View File

@ -0,0 +1,62 @@
package tsdb
import (
"encoding/binary"
"fmt"
"github.com/fabxc/tsdb/chunks"
)
// SeriesReader provides reading access of serialized time series data.
type SeriesReader interface {
// Chunk returns the series data chunk at the given offset.
Chunk(offset uint32) (chunks.Chunk, error)
}
// seriesReader implements a SeriesReader for a serialized byte stream
// of series data.
type seriesReader struct {
// The underlying byte slice holding the encoded series data.
b []byte
}
func newSeriesReader(b []byte) (*seriesReader, error) {
// Verify magic number.
if m := binary.BigEndian.Uint32(b[:4]); m != MagicSeries {
return nil, fmt.Errorf("invalid magic number %x", m)
}
return &seriesReader{b: b}, nil
}
func (s *seriesReader) Chunk(offset uint32) (chunks.Chunk, error) {
b := s.b[offset:]
l, n := binary.Uvarint(b)
if n < 0 {
return nil, fmt.Errorf("reading chunk length failed")
}
b = b[n:]
enc := chunks.Encoding(b[0])
c, err := chunks.FromData(enc, b[1:1+l])
if err != nil {
return nil, err
}
return c, nil
}
// IndexReader provides reading access of serialized index data.
type IndexReader interface {
//
// Close releases resources associated with the reader.
Close()
}
type indexReader struct {
// The underlying byte slice holding the encoded series data.
b []byte
// Cached hashmaps of sections for label values
labelOffsets map[string]uint32
}

View File

@ -277,7 +277,7 @@ func (w *indexWriter) writeSymbols() error {
sort.Strings(symbols)
buf := [binary.MaxVarintLen32]byte{}
b := append(make([]byte, 4096), flagStd)
b := append(make([]byte, 0, 4096), flagStd)
for _, s := range symbols {
w.symbols[s] = uint32(w.n) + uint32(len(b))