undelta_store.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cache
  14. // UndeltaStore listens to incremental updates and sends complete state on every change.
  15. // It implements the Store interface so that it can receive a stream of mirrored objects
  16. // from Reflector. Whenever it receives any complete (Store.Replace) or incremental change
  17. // (Store.Add, Store.Update, Store.Delete), it sends the complete state by calling PushFunc.
  18. // It is thread-safe. It guarantees that every change (Add, Update, Replace, Delete) results
  19. // in one call to PushFunc, but sometimes PushFunc may be called twice with the same values.
  20. // PushFunc should be thread safe.
  21. type UndeltaStore struct {
  22. Store
  23. PushFunc func([]interface{})
  24. }
  25. // Assert that it implements the Store interface.
  26. var _ Store = &UndeltaStore{}
  27. // Note about thread safety. The Store implementation (cache.cache) uses a lock for all methods.
  28. // In the functions below, the lock gets released and reacquired betweend the {Add,Delete,etc}
  29. // and the List. So, the following can happen, resulting in two identical calls to PushFunc.
  30. // time thread 1 thread 2
  31. // 0 UndeltaStore.Add(a)
  32. // 1 UndeltaStore.Add(b)
  33. // 2 Store.Add(a)
  34. // 3 Store.Add(b)
  35. // 4 Store.List() -> [a,b]
  36. // 5 Store.List() -> [a,b]
  37. func (u *UndeltaStore) Add(obj interface{}) error {
  38. if err := u.Store.Add(obj); err != nil {
  39. return err
  40. }
  41. u.PushFunc(u.Store.List())
  42. return nil
  43. }
  44. func (u *UndeltaStore) Update(obj interface{}) error {
  45. if err := u.Store.Update(obj); err != nil {
  46. return err
  47. }
  48. u.PushFunc(u.Store.List())
  49. return nil
  50. }
  51. func (u *UndeltaStore) Delete(obj interface{}) error {
  52. if err := u.Store.Delete(obj); err != nil {
  53. return err
  54. }
  55. u.PushFunc(u.Store.List())
  56. return nil
  57. }
  58. func (u *UndeltaStore) Replace(list []interface{}, resourceVersion string) error {
  59. if err := u.Store.Replace(list, resourceVersion); err != nil {
  60. return err
  61. }
  62. u.PushFunc(u.Store.List())
  63. return nil
  64. }
  65. // NewUndeltaStore returns an UndeltaStore implemented with a Store.
  66. func NewUndeltaStore(pushFunc func([]interface{}), keyFunc KeyFunc) *UndeltaStore {
  67. return &UndeltaStore{
  68. Store: NewStore(keyFunc),
  69. PushFunc: pushFunc,
  70. }
  71. }