mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #283 from lavalamp/stringSet
Move stringSet to util.StringSet
This commit is contained in:
commit
73a494c928
@ -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.
|
||||
|
40
pkg/util/set.go
Normal file
40
pkg/util/set.go
Normal file
@ -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
|
||||
}
|
37
pkg/util/set_test.go
Normal file
37
pkg/util/set_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user