2020-03-24 13:15:47 -07:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// This file holds boilerplate adapters for generic MergeSeriesSet and MergeQuerier functions, so we can have one optimized
|
|
|
|
// solution that works for both ChunkSeriesSet as well as SeriesSet.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
2020-07-31 08:03:02 -07:00
|
|
|
import (
|
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
)
|
2020-03-24 13:15:47 -07:00
|
|
|
|
|
|
|
type genericQuerier interface {
|
2020-06-24 06:41:52 -07:00
|
|
|
LabelQuerier
|
2020-06-09 09:57:31 -07:00
|
|
|
Select(bool, *SelectHints, ...*labels.Matcher) genericSeriesSet
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type genericSeriesSet interface {
|
|
|
|
Next() bool
|
|
|
|
At() Labels
|
|
|
|
Err() error
|
2020-06-09 09:57:31 -07:00
|
|
|
Warnings() Warnings
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type genericSeriesMergeFunc func(...Labels) Labels
|
|
|
|
|
|
|
|
type genericSeriesSetAdapter struct {
|
|
|
|
SeriesSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *genericSeriesSetAdapter) At() Labels {
|
|
|
|
return a.SeriesSet.At()
|
|
|
|
}
|
|
|
|
|
|
|
|
type genericChunkSeriesSetAdapter struct {
|
|
|
|
ChunkSeriesSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *genericChunkSeriesSetAdapter) At() Labels {
|
|
|
|
return a.ChunkSeriesSet.At()
|
|
|
|
}
|
|
|
|
|
|
|
|
type genericQuerierAdapter struct {
|
2020-06-24 06:41:52 -07:00
|
|
|
LabelQuerier
|
2020-03-24 13:15:47 -07:00
|
|
|
|
|
|
|
// One-of. If both are set, Querier will be used.
|
|
|
|
q Querier
|
|
|
|
cq ChunkQuerier
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:57:31 -07:00
|
|
|
func (q *genericQuerierAdapter) Select(sortSeries bool, hints *SelectHints, matchers ...*labels.Matcher) genericSeriesSet {
|
2020-03-24 13:15:47 -07:00
|
|
|
if q.q != nil {
|
2020-06-09 09:57:31 -07:00
|
|
|
return &genericSeriesSetAdapter{q.q.Select(sortSeries, hints, matchers...)}
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
2020-06-09 09:57:31 -07:00
|
|
|
return &genericChunkSeriesSetAdapter{q.cq.Select(sortSeries, hints, matchers...)}
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func newGenericQuerierFrom(q Querier) genericQuerier {
|
2020-06-24 06:41:52 -07:00
|
|
|
return &genericQuerierAdapter{LabelQuerier: q, q: q}
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func newGenericQuerierFromChunk(cq ChunkQuerier) genericQuerier {
|
2020-06-24 06:41:52 -07:00
|
|
|
return &genericQuerierAdapter{LabelQuerier: cq, cq: cq}
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type querierAdapter struct {
|
|
|
|
genericQuerier
|
|
|
|
}
|
|
|
|
|
|
|
|
type seriesSetAdapter struct {
|
|
|
|
genericSeriesSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *seriesSetAdapter) At() Series {
|
|
|
|
return a.genericSeriesSet.At().(Series)
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:57:31 -07:00
|
|
|
func (q *querierAdapter) Select(sortSeries bool, hints *SelectHints, matchers ...*labels.Matcher) SeriesSet {
|
|
|
|
return &seriesSetAdapter{q.genericQuerier.Select(sortSeries, hints, matchers...)}
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type chunkQuerierAdapter struct {
|
|
|
|
genericQuerier
|
|
|
|
}
|
|
|
|
|
|
|
|
type chunkSeriesSetAdapter struct {
|
|
|
|
genericSeriesSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *chunkSeriesSetAdapter) At() ChunkSeries {
|
|
|
|
return a.genericSeriesSet.At().(ChunkSeries)
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:57:31 -07:00
|
|
|
func (q *chunkQuerierAdapter) Select(sortSeries bool, hints *SelectHints, matchers ...*labels.Matcher) ChunkSeriesSet {
|
|
|
|
return &chunkSeriesSetAdapter{q.genericQuerier.Select(sortSeries, hints, matchers...)}
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type seriesMergerAdapter struct {
|
|
|
|
VerticalSeriesMergeFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *seriesMergerAdapter) Merge(s ...Labels) Labels {
|
2020-06-08 11:55:53 -07:00
|
|
|
buf := make([]Series, 0, len(s))
|
2020-03-24 13:15:47 -07:00
|
|
|
for _, ser := range s {
|
2020-06-08 11:55:53 -07:00
|
|
|
buf = append(buf, ser.(Series))
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
2020-06-08 11:55:53 -07:00
|
|
|
return a.VerticalSeriesMergeFunc(buf...)
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type chunkSeriesMergerAdapter struct {
|
2020-06-24 06:41:52 -07:00
|
|
|
VerticalChunkSeriesMergeFunc
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *chunkSeriesMergerAdapter) Merge(s ...Labels) Labels {
|
2020-06-08 11:55:53 -07:00
|
|
|
buf := make([]ChunkSeries, 0, len(s))
|
2020-03-24 13:15:47 -07:00
|
|
|
for _, ser := range s {
|
2020-06-08 11:55:53 -07:00
|
|
|
buf = append(buf, ser.(ChunkSeries))
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
2020-06-24 06:41:52 -07:00
|
|
|
return a.VerticalChunkSeriesMergeFunc(buf...)
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
2020-06-09 09:57:31 -07:00
|
|
|
|
|
|
|
type noopGenericSeriesSet struct{}
|
|
|
|
|
|
|
|
func (noopGenericSeriesSet) Next() bool { return false }
|
|
|
|
|
|
|
|
func (noopGenericSeriesSet) At() Labels { return nil }
|
|
|
|
|
|
|
|
func (noopGenericSeriesSet) Err() error { return nil }
|
|
|
|
|
|
|
|
func (noopGenericSeriesSet) Warnings() Warnings { return nil }
|