From 3d1e8a92d3a4d403cea39ffd0640a94f5430eb20 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Fri, 27 Jun 2014 17:04:11 -0700 Subject: [PATCH] Move stringSet to util.StringSet --- pkg/registry/minion_registry.go | 29 ++++++------------------ pkg/util/set.go | 40 +++++++++++++++++++++++++++++++++ pkg/util/set_test.go | 37 ++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 pkg/util/set.go create mode 100644 pkg/util/set_test.go diff --git a/pkg/registry/minion_registry.go b/pkg/registry/minion_registry.go index dbe7fc5c994..62aa403d7c9 100644 --- a/pkg/registry/minion_registry.go +++ b/pkg/registry/minion_registry.go @@ -24,6 +24,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" + "github.com/GoogleCloudPlatform/kubernetes/pkg/util" ) var ErrDoesNotExist = fmt.Errorf("The requested resource does not exist.") @@ -39,32 +40,16 @@ type MinionRegistry interface { // Initialize a minion registry with a list of minions. func MakeMinionRegistry(minions []string) MinionRegistry { m := &minionList{ - minions: stringSet{}, + minions: util.StringSet{}, } for _, minion := range minions { - m.minions.insert(minion) + m.minions.Insert(minion) } return m } -type empty struct{} -type stringSet map[string]empty - -func (s stringSet) insert(item string) { - s[item] = empty{} -} - -func (s stringSet) delete(item string) { - delete(s, item) -} - -func (s stringSet) has(item string) bool { - _, contained := s[item] - return contained -} - type minionList struct { - minions stringSet + minions util.StringSet lock sync.Mutex } @@ -82,21 +67,21 @@ func (m *minionList) List() (currentMinions []string, err error) { func (m *minionList) Insert(newMinion string) error { m.lock.Lock() defer m.lock.Unlock() - m.minions.insert(newMinion) + m.minions.Insert(newMinion) return nil } func (m *minionList) Delete(minion string) error { m.lock.Lock() defer m.lock.Unlock() - m.minions.delete(minion) + m.minions.Delete(minion) return nil } func (m *minionList) Contains(minion string) (bool, error) { m.lock.Lock() defer m.lock.Unlock() - return m.minions.has(minion), nil + return m.minions.Has(minion), nil } // MinionRegistryStorage implements the RESTStorage interface, backed by a MinionRegistry. diff --git a/pkg/util/set.go b/pkg/util/set.go new file mode 100644 index 00000000000..2b0db6002c5 --- /dev/null +++ b/pkg/util/set.go @@ -0,0 +1,40 @@ +/* +Copyright 2014 Google Inc. All rights reserved. + +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 util + +type empty struct{} + +// A set of strings, implemented via map[string]struct{} for minimal memory consumption. +type StringSet map[string]empty + +// Insert adds items to the set. +func (s StringSet) Insert(items ...string) { + for _, item := range items { + s[item] = empty{} + } +} + +// Delete removes item from the set. +func (s StringSet) Delete(item string) { + delete(s, item) +} + +// Has returns true iff item is contained in the set. +func (s StringSet) Has(item string) bool { + _, contained := s[item] + return contained +} diff --git a/pkg/util/set_test.go b/pkg/util/set_test.go new file mode 100644 index 00000000000..cc4323ac802 --- /dev/null +++ b/pkg/util/set_test.go @@ -0,0 +1,37 @@ +/* +Copyright 2014 Google Inc. All rights reserved. + +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 util + +import ( + "testing" +) + +func TestStringSet(t *testing.T) { + s := StringSet{} + s.Insert("a", "b") + s.Insert("c") + if s.Has("d") { + t.Errorf("Unexpected contents: %#v", s) + } + if !s.Has("a") { + t.Errorf("Missing contents: %#v", s) + } + s.Delete("a") + if s.Has("a") { + t.Errorf("Unexpected contents: %#v", s) + } +}