Skip to content
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

Index, IndexFunc, Indexers, Indices #26

Open
zhouhaibing089 opened this issue Aug 13, 2017 · 0 comments
Open

Index, IndexFunc, Indexers, Indices #26

zhouhaibing089 opened this issue Aug 13, 2017 · 0 comments

Comments

@zhouhaibing089
Copy link
Owner

zhouhaibing089 commented Aug 13, 2017

Source Code: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/client-go/tools/cache/index.go.

IndexFunc: takes an object, and produces a list of index values.
Index: a mapping from index value to key set.
Indexers: a mapping from index name to IndexFunc.
Indices: a mapping from index name to Index

// Index returns a list of items that match on the index function
// Index is thread-safe so long as you treat all items as immutable
func (c *threadSafeMap) Index(indexName string, obj interface{}) ([]interface{}, error) {
	c.lock.RLock()
	defer c.lock.RUnlock()

	indexFunc := c.indexers[indexName]
	if indexFunc == nil {
		return nil, fmt.Errorf("Index with name %s does not exist", indexName)
	}

	indexKeys, err := indexFunc(obj)
	if err != nil {
		return nil, err
	}
	index := c.indices[indexName]

	// need to de-dupe the return list.  Since multiple keys are allowed, this can happen.
	returnKeySet := sets.String{}
	for _, indexKey := range indexKeys {
		set := index[indexKey]
		for _, key := range set.UnsortedList() {
			returnKeySet.Insert(key)
		}
	}

	list := make([]interface{}, 0, returnKeySet.Len())
	for absoluteKey := range returnKeySet {
		list = append(list, c.items[absoluteKey])
	}
	return list, nil
}
  1. 先通过indexName获取indexFunc.
  2. 对obj做indexFunc拿到[]string的index values.
  3. 再通过indexName获取index.
  4. 对每个index value, 从index中取出key set.
  5. 合并所有的key set.
  6. 从storage中根据key set取出所有的object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant