Merge pull request #5270 from lavalamp/fix7

Controller framework
This commit is contained in:
Derek Carr
2015-04-07 16:58:09 -04:00
8 changed files with 427 additions and 21 deletions

View File

@@ -37,6 +37,7 @@ type Store interface {
Update(obj interface{}) error
Delete(obj interface{}) error
List() []interface{}
ListKeys() []string
Get(obj interface{}) (item interface{}, exists bool, err error)
GetByKey(key string) (item interface{}, exists bool, err error)
@@ -196,6 +197,18 @@ func (c *cache) List() []interface{} {
return list
}
// ListKeys returns a list of all the keys of the objects currently
// in the cache.
func (c *cache) ListKeys() []string {
c.lock.RLock()
defer c.lock.RUnlock()
list := make([]string, 0, len(c.items))
for key := range c.items {
list = append(list, key)
}
return list
}
// 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 *cache) Index(indexName string, obj interface{}) ([]interface{}, error) {

View File

@@ -24,8 +24,8 @@ package cache
// in one call to PushFunc, but sometimes PushFunc may be called twice with the same values.
// PushFunc should be thread safe.
type UndeltaStore struct {
ActualStore Store
PushFunc func([]interface{})
Store
PushFunc func([]interface{})
}
// Assert that it implements the Store interface.
@@ -43,47 +43,41 @@ var _ Store = &UndeltaStore{}
// 5 Store.List() -> [a,b]
func (u *UndeltaStore) Add(obj interface{}) error {
if err := u.ActualStore.Add(obj); err != nil {
if err := u.Store.Add(obj); err != nil {
return err
}
u.PushFunc(u.ActualStore.List())
u.PushFunc(u.Store.List())
return nil
}
func (u *UndeltaStore) Update(obj interface{}) error {
if err := u.ActualStore.Update(obj); err != nil {
if err := u.Store.Update(obj); err != nil {
return err
}
u.PushFunc(u.ActualStore.List())
u.PushFunc(u.Store.List())
return nil
}
func (u *UndeltaStore) Delete(obj interface{}) error {
if err := u.ActualStore.Delete(obj); err != nil {
if err := u.Store.Delete(obj); err != nil {
return err
}
u.PushFunc(u.ActualStore.List())
u.PushFunc(u.Store.List())
return nil
}
func (u *UndeltaStore) List() []interface{} {
return u.ActualStore.List()
}
func (u *UndeltaStore) Get(obj interface{}) (item interface{}, exists bool, err error) {
return u.ActualStore.Get(obj)
}
func (u *UndeltaStore) GetByKey(key string) (item interface{}, exists bool, err error) {
return u.ActualStore.GetByKey(key)
}
func (u *UndeltaStore) Replace(list []interface{}) error {
if err := u.ActualStore.Replace(list); err != nil {
if err := u.Store.Replace(list); err != nil {
return err
}
u.PushFunc(u.ActualStore.List())
u.PushFunc(u.Store.List())
return nil
}
// NewUndeltaStore returns an UndeltaStore implemented with a Store.
func NewUndeltaStore(pushFunc func([]interface{}), keyFunc KeyFunc) *UndeltaStore {
return &UndeltaStore{
ActualStore: NewStore(keyFunc),
PushFunc: pushFunc,
Store: NewStore(keyFunc),
PushFunc: pushFunc,
}
}