mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2026-01-08 16:21:50 +00:00
Update vendor
This commit is contained in:
99
vendor/k8s.io/kube-openapi/pkg/cached/cache.go
generated
vendored
99
vendor/k8s.io/kube-openapi/pkg/cached/cache.go
generated
vendored
@@ -110,14 +110,6 @@ type Data[T any] interface {
|
||||
Get() Result[T]
|
||||
}
|
||||
|
||||
// T is the source type, V is the destination type.
|
||||
type merger[K comparable, T, V any] struct {
|
||||
mergeFn func(map[K]Result[T]) Result[V]
|
||||
caches map[K]Data[T]
|
||||
cacheResults map[K]Result[T]
|
||||
result Result[V]
|
||||
}
|
||||
|
||||
// NewMerger creates a new merge cache, a cache that merges the result
|
||||
// of other caches. The function only gets called if any of the
|
||||
// dependency has changed.
|
||||
@@ -135,27 +127,71 @@ type merger[K comparable, T, V any] struct {
|
||||
// function will remerge all the dependencies together everytime. Since
|
||||
// the list of dependencies is constant, there is no way to save some
|
||||
// partial merge information either.
|
||||
//
|
||||
// Also note that Golang map iteration is not stable. If the mergeFn
|
||||
// depends on the order iteration to be stable, it will need to
|
||||
// implement its own sorting or iteration order.
|
||||
func NewMerger[K comparable, T, V any](mergeFn func(results map[K]Result[T]) Result[V], caches map[K]Data[T]) Data[V] {
|
||||
return &merger[K, T, V]{
|
||||
listCaches := make([]Data[T], 0, len(caches))
|
||||
// maps from index to key
|
||||
indexes := make(map[int]K, len(caches))
|
||||
i := 0
|
||||
for k := range caches {
|
||||
listCaches = append(listCaches, caches[k])
|
||||
indexes[i] = k
|
||||
i++
|
||||
}
|
||||
|
||||
return NewListMerger(func(results []Result[T]) Result[V] {
|
||||
if len(results) != len(indexes) {
|
||||
panic(fmt.Errorf("invalid result length %d, expected %d", len(results), len(indexes)))
|
||||
}
|
||||
m := make(map[K]Result[T], len(results))
|
||||
for i := range results {
|
||||
m[indexes[i]] = results[i]
|
||||
}
|
||||
return mergeFn(m)
|
||||
}, listCaches)
|
||||
}
|
||||
|
||||
type listMerger[T, V any] struct {
|
||||
mergeFn func([]Result[T]) Result[V]
|
||||
caches []Data[T]
|
||||
cacheResults []Result[T]
|
||||
result Result[V]
|
||||
}
|
||||
|
||||
// NewListMerger creates a new merge cache that merges the results of
|
||||
// other caches in list form. The function only gets called if any of
|
||||
// the dependency has changed.
|
||||
//
|
||||
// The benefit of ListMerger over the basic Merger is that caches are
|
||||
// stored in an ordered list so the order of the cache will be
|
||||
// preserved in the order of the results passed to the mergeFn.
|
||||
//
|
||||
// If any of the dependency returned an error before, or any of the
|
||||
// dependency returned an error this time, or if the mergeFn failed
|
||||
// before, then the function is reran.
|
||||
//
|
||||
// Note that this assumes there is no "partial" merge, the merge
|
||||
// function will remerge all the dependencies together everytime. Since
|
||||
// the list of dependencies is constant, there is no way to save some
|
||||
// partial merge information either.
|
||||
func NewListMerger[T, V any](mergeFn func(results []Result[T]) Result[V], caches []Data[T]) Data[V] {
|
||||
return &listMerger[T, V]{
|
||||
mergeFn: mergeFn,
|
||||
caches: caches,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *merger[K, T, V]) prepareResults() map[K]Result[T] {
|
||||
cacheResults := make(map[K]Result[T], len(c.caches))
|
||||
for key, cache := range c.caches {
|
||||
cacheResults[key] = cache.Get()
|
||||
func (c *listMerger[T, V]) prepareResults() []Result[T] {
|
||||
cacheResults := make([]Result[T], 0, len(c.caches))
|
||||
for _, cache := range c.caches {
|
||||
cacheResults = append(cacheResults, cache.Get())
|
||||
}
|
||||
return cacheResults
|
||||
}
|
||||
|
||||
// Rerun if:
|
||||
// - The last run resulted in an error
|
||||
// - Any of the dependency previously returned an error
|
||||
// - Any of the dependency just returned an error
|
||||
// - Any of the dependency's etag changed
|
||||
func (c *merger[K, T, V]) needsRunning(results map[K]Result[T]) bool {
|
||||
func (c *listMerger[T, V]) needsRunning(results []Result[T]) bool {
|
||||
if c.cacheResults == nil {
|
||||
return true
|
||||
}
|
||||
@@ -165,12 +201,8 @@ func (c *merger[K, T, V]) needsRunning(results map[K]Result[T]) bool {
|
||||
if len(results) != len(c.cacheResults) {
|
||||
panic(fmt.Errorf("invalid number of results: %v (expected %v)", len(results), len(c.cacheResults)))
|
||||
}
|
||||
for key, oldResult := range c.cacheResults {
|
||||
newResult, ok := results[key]
|
||||
if !ok {
|
||||
panic(fmt.Errorf("unknown cache entry: %v", key))
|
||||
}
|
||||
|
||||
for i, oldResult := range c.cacheResults {
|
||||
newResult := results[i]
|
||||
if newResult.Etag != oldResult.Etag || newResult.Err != nil || oldResult.Err != nil {
|
||||
return true
|
||||
}
|
||||
@@ -178,7 +210,7 @@ func (c *merger[K, T, V]) needsRunning(results map[K]Result[T]) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *merger[K, T, V]) Get() Result[V] {
|
||||
func (c *listMerger[T, V]) Get() Result[V] {
|
||||
cacheResults := c.prepareResults()
|
||||
if c.needsRunning(cacheResults) {
|
||||
c.cacheResults = cacheResults
|
||||
@@ -187,8 +219,6 @@ func (c *merger[K, T, V]) Get() Result[V] {
|
||||
return c.result
|
||||
}
|
||||
|
||||
type transformerCacheKeyType struct{}
|
||||
|
||||
// NewTransformer creates a new cache that transforms the result of
|
||||
// another cache. The transformFn will only be called if the source
|
||||
// cache has updated the output, otherwise, the cached result will be
|
||||
@@ -198,15 +228,12 @@ type transformerCacheKeyType struct{}
|
||||
// this time, or if the transformerFn failed before, the function is
|
||||
// reran.
|
||||
func NewTransformer[T, V any](transformerFn func(Result[T]) Result[V], source Data[T]) Data[V] {
|
||||
return NewMerger(func(caches map[transformerCacheKeyType]Result[T]) Result[V] {
|
||||
cache, ok := caches[transformerCacheKeyType{}]
|
||||
if len(caches) != 1 || !ok {
|
||||
return NewListMerger(func(caches []Result[T]) Result[V] {
|
||||
if len(caches) != 1 {
|
||||
panic(fmt.Errorf("invalid cache for transformer cache: %v", caches))
|
||||
}
|
||||
return transformerFn(cache)
|
||||
}, map[transformerCacheKeyType]Data[T]{
|
||||
{}: source,
|
||||
})
|
||||
return transformerFn(caches[0])
|
||||
}, []Data[T]{source})
|
||||
}
|
||||
|
||||
// NewSource creates a new cache that generates some data. This
|
||||
|
||||
Reference in New Issue
Block a user