@@ -15,16 +15,15 @@ package querier
1515
1616import (
1717 "fmt"
18- "sort"
1918 "time"
2019
2120 "github.com/prometheus/common/model"
2221 "github.com/prometheus/prometheus/storage/local"
23- prom_chunk "github.com/prometheus/prometheus/storage/local/chunk"
2422 "github.com/prometheus/prometheus/storage/metric"
2523 "golang.org/x/net/context"
2624
2725 "github.com/weaveworks/cortex/chunk"
26+ "github.com/weaveworks/cortex/util"
2827)
2928
3029// A Querier allows querying all samples in a given time range that match a set
@@ -48,68 +47,7 @@ func (q *ChunkQuerier) Query(ctx context.Context, from, to model.Time, matchers
4847 return nil , err
4948 }
5049
51- // Group chunks by series, sort and dedupe samples.
52- sampleStreams := map [model.Fingerprint ]* model.SampleStream {}
53-
54- for _ , c := range chunks {
55- fp := c .Metric .Fingerprint ()
56- ss , ok := sampleStreams [fp ]
57- if ! ok {
58- ss = & model.SampleStream {
59- Metric : c .Metric ,
60- }
61- sampleStreams [fp ] = ss
62- }
63-
64- samples , err := decodeChunk (c .Data )
65- if err != nil {
66- return nil , err
67- }
68-
69- ss .Values = append (ss .Values , samples ... )
70- }
71-
72- for _ , ss := range sampleStreams {
73- sort .Sort (timeSortableSamplePairs (ss .Values ))
74- // TODO: should we also dedupe samples here or leave that to the upper layers?
75- }
76-
77- matrix := make (model.Matrix , 0 , len (sampleStreams ))
78- for _ , ss := range sampleStreams {
79- matrix = append (matrix , ss )
80- }
81-
82- return matrix , nil
83- }
84-
85- func decodeChunk (buf []byte ) ([]model.SamplePair , error ) {
86- lc , err := prom_chunk .NewForEncoding (prom_chunk .DoubleDelta )
87- if err != nil {
88- return nil , err
89- }
90- lc .UnmarshalFromBuf (buf )
91- it := lc .NewIterator ()
92- // TODO(juliusv): Pre-allocate this with the right length again once we
93- // add a method upstream to get the number of samples in a chunk.
94- var samples []model.SamplePair
95- for it .Scan () {
96- samples = append (samples , it .Value ())
97- }
98- return samples , nil
99- }
100-
101- type timeSortableSamplePairs []model.SamplePair
102-
103- func (ts timeSortableSamplePairs ) Len () int {
104- return len (ts )
105- }
106-
107- func (ts timeSortableSamplePairs ) Less (i , j int ) bool {
108- return ts [i ].Timestamp < ts [j ].Timestamp
109- }
110-
111- func (ts timeSortableSamplePairs ) Swap (i , j int ) {
112- ts [i ], ts [j ] = ts [j ], ts [i ]
50+ return chunk .ChunksToMatrix (chunks )
11351}
11452
11553// LabelValuesForLabelName returns all of the label values that are associated with a given label name.
@@ -145,18 +83,11 @@ func (qm MergeQuerier) QueryRange(ctx context.Context, from, to model.Time, matc
14583 }
14684 } else {
14785 ssIt := it .(sampleStreamIterator )
148- ssIt .ss .Values = append (ssIt .ss .Values , ss .Values ... )
86+ ssIt .ss .Values = util . MergeSamples (ssIt .ss .Values , ss .Values )
14987 }
15088 }
15189 }
15290
153- // Sort and dedupe samples.
154- for _ , it := range fpToIt {
155- sortable := timeSortableSamplePairs (it .(sampleStreamIterator ).ss .Values )
156- sort .Sort (sortable )
157- // TODO: Dedupe samples. Not strictly necessary.
158- }
159-
16091 iterators := make ([]local.SeriesIterator , 0 , len (fpToIt ))
16192 for _ , it := range fpToIt {
16293 iterators = append (iterators , it )
0 commit comments