Write label value indices

This commit is contained in:
Fabian Reinartz 2016-12-09 21:40:38 +01:00
parent 1e0edf367b
commit 8cbc95c316
2 changed files with 35 additions and 5 deletions

9
db.go
View file

@ -291,6 +291,15 @@ func (s *SeriesShard) persist() error {
if err := iw.WriteStats(nil); err != nil {
return err
}
for n, v := range head.index.values {
s := make([]string, 0, len(v))
for x := range v {
s = append(s, x)
}
iw.WriteLabelIndex([]string{n}, s)
}
sz := fmt.Sprintf("%fMiB", float64(sw.Size()+iw.Size())/1024/1024)

View file

@ -2,6 +2,7 @@ package tsdb
import (
"encoding/binary"
"fmt"
"hash/crc32"
"io"
"os"
@ -175,14 +176,16 @@ type indexWriter struct {
series []Labels
offsets [][]ChunkOffset
symbols map[string]uint32
symbols map[string]uint32 // symbol offsets
labelIndexes map[string]uint32 // label index offsets
}
func newIndexWriter(w io.Writer) *indexWriter {
return &indexWriter{
w: w,
n: 0,
symbols: make(map[string]uint32),
w: w,
n: 0,
symbols: make(map[string]uint32),
labelIndexes: make(map[string]uint32),
}
}
@ -251,7 +254,25 @@ func (w *indexWriter) writeSymbols() error {
}
func (w *indexWriter) WriteLabelIndex(names []string, values []string) error {
return nil
if len(names) != 1 {
return fmt.Errorf("not supported")
}
sort.Strings(values)
h := crc32.NewIEEE()
wr := io.MultiWriter(h, w.w)
w.labelIndexes[names[0]] = uint32(w.n)
for _, v := range values {
o := w.symbols[v]
if err := w.write(wr, ((*[4]byte)(unsafe.Pointer(&o)))[:]); err != nil {
return err
}
}
return w.write(w.w, h.Sum(nil))
}
func (w *indexWriter) WriteSeries(ref uint32, ls ...Labels) error {