Index结构

在这里Index存储接口的具体实现是cache。

1
2
3
4
5
6
7
8
type cache struct {
	// cacheStorage bears the burden of thread safety for the cache
	cacheStorage ThreadSafeStore
	// keyFunc is used to make the key for objects stored in and retrieved from items, and
	// should be deterministic.
	//这个keyFunc是后面用来获取对应的obj对象的,默认是namespace/name
	keyFunc KeyFunc
}

Index接口

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
type Indexer interface {
	Store
	// Index returns the stored objects whose set of indexed values
	// intersects the set of indexed values of the given object, for
	// the named index
	Index(indexName string, obj interface{}) ([]interface{}, error)
	// IndexKeys returns the storage keys of the stored objects whose
	// set of indexed values for the named index includes the given
	// indexed value
	IndexKeys(indexName, indexedValue string) ([]string, error)
	// ListIndexFuncValues returns all the indexed values of the given index
	ListIndexFuncValues(indexName string) []string
	// ByIndex returns the stored objects whose set of indexed values
	// for the named index includes the given indexed value
	ByIndex(indexName, indexedValue string) ([]interface{}, error)
	// GetIndexer return the indexers
	GetIndexers() Indexers

	// AddIndexers adds more indexers to this store.  If you call this after you already have data
	// in the store, the results are undefined.
	AddIndexers(newIndexers Indexers) error
}

cache中的cacheStorage则是通过threadSafeMap实现,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
type threadSafeMap struct {
	lock  sync.RWMutex
	items map[string]interface{}

	// indexers maps a name to an IndexFunc
	indexers Indexers
	// indices maps a name to an Index
	indices Indices
}

type Index map[string]sets.String

// Indexers maps a name to a IndexFunc
type Indexers map[string]IndexFunc

// Indices maps a name to an Index
type Indices map[string]Index

默认的Indexers是cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}。其中key是namespace,value是获取object对象namespace的函数。 此时通过一个对象更新操作分析这些结构的关系。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//先遍历indexers中的索引函数,默认是namespace
for name, indexFunc := range c.indexers {
//由indexFunc获取需要操作的对象namespace
		indexValues, err := indexFunc(newObj)
		if err != nil {
			panic(fmt.Errorf("unable to calculate an index entry for key %q on index %q: %v", key, name, err))
		}
//从indices中获取对应indexFunc的所有索引,默认是namespace,如果不存在就创建。
		index := c.indices[name]
		if index == nil {
			index = Index{}
			c.indices[name] = index
		}
//通过namespace索引获取到Indexers结构,这个结构里存放的是每个namespace的所有key,这里的key默认都是namespace/name结构,如果不存在则把这个key添加到Indexers对应的namespace中。
		for _, indexValue := range indexValues {
			set := index[indexValue]
			if set == nil {
				set = sets.String{}
				index[indexValue] = set
			}
			set.Insert(key)
		}
	}

indexers中存的是获取索引的函数,map[name]IndexFunc形式。 Indices是存的obj对应的索引。形式是map[string]Index,这个map的key和indexers中的是一一对应。obj根据IndexFunc计算出来的索引去Index中查存储在items中的key,默认是namespace/objname的形式,如果没有namespace则前面是空。然后再拿key去items中查对应的值。