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