-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable look-up by secondary index in cache #4125
Merged
smarterclayton
merged 3 commits into
kubernetes:master
from
derekwaynecarr:store_by_namespace
Feb 12, 2015
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
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 cache | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta" | ||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" | ||
) | ||
|
||
// Indexer is a storage interface that lets you list objects using multiple indexing functions | ||
type Indexer interface { | ||
Store | ||
// Retrieve list of objects that match on the named indexing function | ||
Index(indexName string, obj interface{}) ([]interface{}, error) | ||
} | ||
|
||
// IndexFunc knows how to provide an indexed value for an object. | ||
type IndexFunc func(obj interface{}) (string, error) | ||
|
||
// MetaNamespaceIndexFunc is a default index function that indexes based on an object's namespace | ||
func MetaNamespaceIndexFunc(obj interface{}) (string, error) { | ||
meta, err := meta.Accessor(obj) | ||
if err != nil { | ||
return "", fmt.Errorf("object has no meta: %v", err) | ||
} | ||
return meta.Namespace(), nil | ||
} | ||
|
||
// Index maps the indexed value to a set of keys in the store that match on that value | ||
type Index map[string]util.StringSet | ||
|
||
// Indexers maps a name to a IndexFunc | ||
type Indexers map[string]IndexFunc | ||
|
||
// Indices maps a name to an Index | ||
type Indices map[string]Index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,10 +86,53 @@ func doTestStore(t *testing.T, store Store) { | |
} | ||
} | ||
|
||
// Test public interface | ||
func doTestIndex(t *testing.T, indexer Indexer) { | ||
mkObj := func(id string, val string) testStoreObject { | ||
return testStoreObject{id: id, val: val} | ||
} | ||
|
||
// Test Index | ||
expected := map[string]util.StringSet{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: inline declaration? |
||
expected["b"] = util.NewStringSet("a", "c") | ||
expected["f"] = util.NewStringSet("e") | ||
expected["h"] = util.NewStringSet("g") | ||
indexer.Add(mkObj("a", "b")) | ||
indexer.Add(mkObj("c", "b")) | ||
indexer.Add(mkObj("e", "f")) | ||
indexer.Add(mkObj("g", "h")) | ||
{ | ||
for k, v := range expected { | ||
found := util.StringSet{} | ||
indexResults, err := indexer.Index("by_val", mkObj("", k)) | ||
if err != nil { | ||
t.Errorf("Unexpected error %v", err) | ||
} | ||
for _, item := range indexResults { | ||
found.Insert(item.(testStoreObject).id) | ||
} | ||
items := v.List() | ||
if !found.HasAll(items...) { | ||
t.Errorf("missing items, index %s, expected %v but found %v", k, items, found.List()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func testStoreKeyFunc(obj interface{}) (string, error) { | ||
return obj.(testStoreObject).id, nil | ||
} | ||
|
||
func testStoreIndexFunc(obj interface{}) (string, error) { | ||
return obj.(testStoreObject).val, nil | ||
} | ||
|
||
func testStoreIndexers() Indexers { | ||
indexers := Indexers{} | ||
indexers["by_val"] = testStoreIndexFunc | ||
return indexers | ||
} | ||
|
||
type testStoreObject struct { | ||
id string | ||
val string | ||
|
@@ -107,3 +150,7 @@ func TestUndeltaStore(t *testing.T) { | |
nop := func([]interface{}) {} | ||
doTestStore(t, NewUndeltaStore(nop, testStoreKeyFunc)) | ||
} | ||
|
||
func TestIndex(t *testing.T) { | ||
doTestIndex(t, NewIndexer(testStoreKeyFunc, testStoreIndexers())) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you build the index before the lock and then swap it in to limit contention? (Similar to above)