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