Remove generic set type

This commit is contained in:
Fabian Reinartz 2015-05-28 21:51:44 +02:00
parent dbc0d30e3e
commit 02717e6fde
3 changed files with 3 additions and 184 deletions

View file

@ -24,7 +24,6 @@ import (
"github.com/prometheus/prometheus/pkg/strutil" "github.com/prometheus/prometheus/pkg/strutil"
"github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/utility"
) )
const ( const (
@ -149,10 +148,10 @@ func (rule *AlertingRule) Eval(timestamp clientmodel.Timestamp, engine *promql.E
// Create pending alerts for any new vector elements in the alert expression // Create pending alerts for any new vector elements in the alert expression
// or update the expression value for existing elements. // or update the expression value for existing elements.
resultFingerprints := utility.Set{} resultFPs := map[clientmodel.Fingerprint]struct{}{}
for _, sample := range exprResult { for _, sample := range exprResult {
fp := sample.Metric.Metric.Fingerprint() fp := sample.Metric.Metric.Fingerprint()
resultFingerprints.Add(fp) resultFPs[fp] = struct{}{}
if alert, ok := rule.activeAlerts[fp]; !ok { if alert, ok := rule.activeAlerts[fp]; !ok {
labels := clientmodel.LabelSet{} labels := clientmodel.LabelSet{}
@ -177,7 +176,7 @@ func (rule *AlertingRule) Eval(timestamp clientmodel.Timestamp, engine *promql.E
// Check if any pending alerts should be removed or fire now. Write out alert timeseries. // Check if any pending alerts should be removed or fire now. Write out alert timeseries.
for fp, activeAlert := range rule.activeAlerts { for fp, activeAlert := range rule.activeAlerts {
if !resultFingerprints.Has(fp) { if _, ok := resultFPs[fp]; !ok {
vector = append(vector, activeAlert.sample(timestamp, 0)) vector = append(vector, activeAlert.sample(timestamp, 0))
delete(rule.activeAlerts, fp) delete(rule.activeAlerts, fp)
continue continue

View file

@ -1,58 +0,0 @@
// Copyright 2013 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 utility
// Set is a type which models a set.
type Set map[interface{}]struct{}
// Add adds an item to the set.
func (s Set) Add(v interface{}) {
s[v] = struct{}{}
}
// Remove removes an item from the set.
func (s Set) Remove(v interface{}) {
delete(s, v)
}
// Elements returns a slice containing all elements in the set.
func (s Set) Elements() []interface{} {
result := make([]interface{}, 0, len(s))
for k := range s {
result = append(result, k)
}
return result
}
// Has returns true if an element is contained in the set.
func (s Set) Has(v interface{}) bool {
_, p := s[v]
return p
}
// Intersection returns a new set with items that exist in both sets.
func (s Set) Intersection(o Set) Set {
result := Set{}
for k := range s {
if o.Has(k) {
result[k] = struct{}{}
}
}
return result
}

View file

@ -1,122 +0,0 @@
// Copyright 2013 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 utility
import (
"testing"
"testing/quick"
)
func TestSetEqualMemberships(t *testing.T) {
f := func(x int) bool {
first := make(Set)
second := make(Set)
first.Add(x)
second.Add(x)
intersection := first.Intersection(second)
members := intersection.Elements()
return members != nil && len(members) == 1 && members[0] == x
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestSetInequalMemberships(t *testing.T) {
f := func(x int) bool {
first := make(Set)
second := make(Set)
first.Add(x)
intersection := first.Intersection(second)
members := intersection.Elements()
return members != nil && len(members) == 0
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestSetAsymmetricMemberships(t *testing.T) {
f := func(x int) bool {
first := make(Set)
second := make(Set)
first.Add(x)
second.Add(x)
first.Add(x + 1)
second.Add(x + 1)
second.Add(x + 2)
first.Add(x + 2)
first.Add(x + 3)
second.Add(x + 4)
intersection := first.Intersection(second)
members := intersection.Elements()
return members != nil && len(members) == 3
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestSetRemoval(t *testing.T) {
f := func(x int) bool {
first := make(Set)
first.Add(x)
first.Remove(x)
members := first.Elements()
return members != nil && len(members) == 0
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestSetAdditionAndRemoval(t *testing.T) {
f := func(x int) bool {
first := make(Set)
second := make(Set)
first.Add(x)
second.Add(x)
first.Add(x + 1)
first.Remove(x + 1)
intersection := first.Intersection(second)
members := intersection.Elements()
return members != nil && len(members) == 1 && members[0] == x
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}