mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 21:54:10 -08:00
7d7b9eacff
On a 32 bit architecture the size of int is 32 bits. Thus converting from int64, uint64 can overflow it and flip the sign. Try for yourself in playground: package main import "fmt" func main() { x := int64(0x1F0000001) y := int64(1) z := int32(x - y) // numerically this is 0x1F0000000 fmt.Printf("%v\n", z) } Prints -268435456 as if x was smaller. Followup to #12650 Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
// Copyright 2019 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.
|
|
|
|
package index
|
|
|
|
import (
|
|
"math"
|
|
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
// Stat holds values for a single cardinality statistic.
|
|
type Stat struct {
|
|
Name string
|
|
Count uint64
|
|
}
|
|
|
|
type maxHeap struct {
|
|
maxLength int
|
|
minValue uint64
|
|
minIndex int
|
|
Items []Stat
|
|
}
|
|
|
|
func (m *maxHeap) init(length int) {
|
|
m.maxLength = length
|
|
m.minValue = math.MaxUint64
|
|
m.Items = make([]Stat, 0, length)
|
|
}
|
|
|
|
func (m *maxHeap) push(item Stat) {
|
|
if len(m.Items) < m.maxLength {
|
|
if item.Count < m.minValue {
|
|
m.minValue = item.Count
|
|
m.minIndex = len(m.Items)
|
|
}
|
|
m.Items = append(m.Items, item)
|
|
return
|
|
}
|
|
if item.Count < m.minValue {
|
|
return
|
|
}
|
|
|
|
m.Items[m.minIndex] = item
|
|
m.minValue = item.Count
|
|
|
|
for i, stat := range m.Items {
|
|
if stat.Count < m.minValue {
|
|
m.minValue = stat.Count
|
|
m.minIndex = i
|
|
}
|
|
}
|
|
}
|
|
|
|
func (m *maxHeap) get() []Stat {
|
|
slices.SortFunc(m.Items, func(a, b Stat) int {
|
|
switch {
|
|
case b.Count < a.Count:
|
|
return -1
|
|
case b.Count > a.Count:
|
|
return 1
|
|
default:
|
|
return 0
|
|
}
|
|
})
|
|
return m.Items
|
|
}
|