forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
merger.go
42 lines (38 loc) · 1.06 KB
/
merger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package util
import "github.com/prometheus/common/model"
// MergeSampleSets merges and dedupes two sets of already sorted sample pairs.
func MergeSampleSets(a, b []model.SamplePair) []model.SamplePair {
result := make([]model.SamplePair, 0, len(a)+len(b))
i, j := 0, 0
for i < len(a) && j < len(b) {
if a[i].Timestamp < b[j].Timestamp {
result = append(result, a[i])
i++
} else if a[i].Timestamp > b[j].Timestamp {
result = append(result, b[j])
j++
} else {
result = append(result, a[i])
i++
j++
}
}
// Add the rest of a or b. One of them is empty now.
result = append(result, a[i:]...)
result = append(result, b[j:]...)
return result
}
// MergeNSampleSets merges and dedupes n sets of already sorted sample pairs.
func MergeNSampleSets(sampleSets ...[]model.SamplePair) []model.SamplePair {
l := len(sampleSets)
switch l {
case 0:
return []model.SamplePair{}
case 1:
return sampleSets[0]
}
n := l / 2
left := MergeNSampleSets(sampleSets[:n]...)
right := MergeNSampleSets(sampleSets[n:]...)
return MergeSampleSets(left, right)
}