Implement label value queries in all layers.

This commit is contained in:
Fabian Reinartz 2016-12-19 12:26:25 +01:00
parent aabb21f4b9
commit 282d9ae6e2
3 changed files with 69 additions and 10 deletions

View File

@ -84,7 +84,7 @@ func (h *HeadBlock) Series(ref uint32, mint, maxt int64) (Series, error) {
if !intervalOverlap(cd.firsTimestamp, cd.lastTimestamp, mint, maxt) { if !intervalOverlap(cd.firsTimestamp, cd.lastTimestamp, mint, maxt) {
return nil, nil return nil, nil
} }
s := &series{ s := &chunkSeries{
labels: cd.lset, labels: cd.lset,
chunks: []ChunkMeta{ chunks: []ChunkMeta{
{MinTime: h.stats.MinTime, Ref: 0}, {MinTime: h.stats.MinTime, Ref: 0},

View File

@ -88,8 +88,49 @@ func (q *querier) Select(ms ...Matcher) SeriesSet {
return r return r
} }
func (q *querier) LabelValues(string) ([]string, error) { func (q *querier) LabelValues(n string) ([]string, error) {
return nil, nil // TODO(fabxc): return returned merged result.
res, err := q.shards[0].LabelValues(n)
if err != nil {
return nil, err
}
for _, sq := range q.shards[1:] {
pr, err := sq.LabelValues(n)
if err != nil {
return nil, err
}
// Merge new values into deduplicated result.
res = mergeStrings(res, pr)
}
return res, nil
}
func mergeStrings(a, b []string) []string {
maxl := len(a)
if len(b) > len(a) {
maxl = len(b)
}
res := make([]string, 0, maxl*10/9)
for len(a) > 0 && len(b) > 0 {
d := strings.Compare(a[0], b[0])
if d == 0 {
res = append(res, a[0])
a, b = a[1:], b[1:]
} else if d < 0 {
res = append(res, a[0])
a = a[1:]
} else if d > 0 {
res = append(res, b[0])
b = b[1:]
}
}
// Append all remaining elements.
res = append(res, a...)
res = append(res, b...)
return res
} }
func (q *querier) LabelValuesFor(string, Label) ([]string, error) { func (q *querier) LabelValuesFor(string, Label) ([]string, error) {
@ -121,8 +162,21 @@ func (s *Shard) Querier(mint, maxt int64) Querier {
return sq return sq
} }
func (q *shardQuerier) LabelValues(string) ([]string, error) { func (q *shardQuerier) LabelValues(n string) ([]string, error) {
return nil, nil // TODO(fabxc): return returned merged result.
res, err := q.blocks[0].LabelValues(n)
if err != nil {
return nil, err
}
for _, bq := range q.blocks[1:] {
pr, err := bq.LabelValues(n)
if err != nil {
return nil, err
}
// Merge new values into deduplicated result.
res = mergeStrings(res, pr)
}
return res, nil
} }
func (q *shardQuerier) LabelValuesFor(string, Label) ([]string, error) { func (q *shardQuerier) LabelValuesFor(string, Label) ([]string, error) {
@ -216,7 +270,7 @@ func (q *blockQuerier) LabelValues(name string) ([]string, error) {
} }
res = append(res, vals[0]) res = append(res, vals[0])
} }
return nil, nil return res, nil
} }
func (q *blockQuerier) LabelValuesFor(string, Label) ([]string, error) { func (q *blockQuerier) LabelValuesFor(string, Label) ([]string, error) {
@ -410,18 +464,22 @@ func (s *blockSeriesSet) Next() bool {
func (s *blockSeriesSet) Series() Series { return s.cur } func (s *blockSeriesSet) Series() Series { return s.cur }
func (s *blockSeriesSet) Err() error { return s.err } func (s *blockSeriesSet) Err() error { return s.err }
type series struct { // chunkSeries is a series that is backed by a sequence of chunks holding
// time series data.
type chunkSeries struct {
labels Labels labels Labels
chunks []ChunkMeta // in-order chunk refs chunks []ChunkMeta // in-order chunk refs
// chunk is a function that retrieves chunks based on a reference
// number contained in the chunk meta information.
chunk func(ref uint32) (chunks.Chunk, error) chunk func(ref uint32) (chunks.Chunk, error)
} }
func (s *series) Labels() Labels { func (s *chunkSeries) Labels() Labels {
return s.labels return s.labels
} }
func (s *series) Iterator() SeriesIterator { func (s *chunkSeries) Iterator() SeriesIterator {
var cs []chunks.Chunk var cs []chunks.Chunk
var mints []int64 var mints []int64
@ -454,6 +512,7 @@ type SeriesIterator interface {
} }
// chainedSeries implements a series for a list of time-sorted series. // chainedSeries implements a series for a list of time-sorted series.
// They all must have the same labels.
type chainedSeries struct { type chainedSeries struct {
series []Series series []Series
} }

View File

@ -327,7 +327,7 @@ func (r *indexReader) Series(ref uint32, mint, maxt int64) (Series, error) {
return nil, nil return nil, nil
} }
return &series{ return &chunkSeries{
labels: labels, labels: labels,
chunks: chunks, chunks: chunks,
chunk: r.series.Chunk, chunk: r.series.Chunk,