Merge pull request #4016 from smarterclayton/add_getbykey_to_store

Add a GetByKey method to Store
This commit is contained in:
Alex Robinson 2015-02-02 10:51:57 -08:00
commit 59ee1d901a
3 changed files with 25 additions and 10 deletions

View File

@ -86,13 +86,18 @@ func (f *FIFO) List() []interface{} {
// Get returns the requested item, or sets exists=false. // Get returns the requested item, or sets exists=false.
func (f *FIFO) Get(obj interface{}) (item interface{}, exists bool, err error) { func (f *FIFO) Get(obj interface{}) (item interface{}, exists bool, err error) {
id, err := f.keyFunc(obj) key, err := f.keyFunc(obj)
if err != nil { if err != nil {
return nil, false, fmt.Errorf("couldn't create key for object: %v", err) return nil, false, fmt.Errorf("couldn't create key for object: %v", err)
} }
return f.GetByKey(key)
}
// GetByKey returns the requested item, or sets exists=false.
func (f *FIFO) GetByKey(key string) (item interface{}, exists bool, err error) {
f.lock.RLock() f.lock.RLock()
defer f.lock.RUnlock() defer f.lock.RUnlock()
item, exists = f.items[id] item, exists = f.items[key]
return item, exists, nil return item, exists, nil
} }

View File

@ -37,6 +37,7 @@ type Store interface {
Delete(obj interface{}) error Delete(obj interface{}) error
List() []interface{} List() []interface{}
Get(obj interface{}) (item interface{}, exists bool, err error) Get(obj interface{}) (item interface{}, exists bool, err error)
GetByKey(key string) (item interface{}, exists bool, err error)
// Replace will delete the contents of the store, using instead the // Replace will delete the contents of the store, using instead the
// given list. Store takes ownership of the list, you should not reference // given list. Store takes ownership of the list, you should not reference
@ -68,37 +69,37 @@ type cache struct {
// Add inserts an item into the cache. // Add inserts an item into the cache.
func (c *cache) Add(obj interface{}) error { func (c *cache) Add(obj interface{}) error {
id, err := c.keyFunc(obj) key, err := c.keyFunc(obj)
if err != nil { if err != nil {
return fmt.Errorf("couldn't create key for object: %v", err) return fmt.Errorf("couldn't create key for object: %v", err)
} }
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
c.items[id] = obj c.items[key] = obj
return nil return nil
} }
// Update sets an item in the cache to its updated state. // Update sets an item in the cache to its updated state.
func (c *cache) Update(obj interface{}) error { func (c *cache) Update(obj interface{}) error {
id, err := c.keyFunc(obj) key, err := c.keyFunc(obj)
if err != nil { if err != nil {
return fmt.Errorf("couldn't create key for object: %v", err) return fmt.Errorf("couldn't create key for object: %v", err)
} }
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
c.items[id] = obj c.items[key] = obj
return nil return nil
} }
// Delete removes an item from the cache. // Delete removes an item from the cache.
func (c *cache) Delete(obj interface{}) error { func (c *cache) Delete(obj interface{}) error {
id, err := c.keyFunc(obj) key, err := c.keyFunc(obj)
if err != nil { if err != nil {
return fmt.Errorf("couldn't create key for object: %v", err) return fmt.Errorf("couldn't create key for object: %v", err)
} }
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
delete(c.items, id) delete(c.items, key)
return nil return nil
} }
@ -117,13 +118,19 @@ func (c *cache) List() []interface{} {
// Get returns the requested item, or sets exists=false. // Get returns the requested item, or sets exists=false.
// Get is completely threadsafe as long as you treat all items as immutable. // Get is completely threadsafe as long as you treat all items as immutable.
func (c *cache) Get(obj interface{}) (item interface{}, exists bool, err error) { func (c *cache) Get(obj interface{}) (item interface{}, exists bool, err error) {
id, _ := c.keyFunc(obj) key, _ := c.keyFunc(obj)
if err != nil { if err != nil {
return nil, false, fmt.Errorf("couldn't create key for object: %v", err) return nil, false, fmt.Errorf("couldn't create key for object: %v", err)
} }
return c.GetByKey(key)
}
// GetByKey returns the request item, or exists=false.
// GetByKey is completely threadsafe as long as you treat all items as immutable.
func (c *cache) GetByKey(key string) (item interface{}, exists bool, err error) {
c.lock.RLock() c.lock.RLock()
defer c.lock.RUnlock() defer c.lock.RUnlock()
item, exists = c.items[id] item, exists = c.items[key]
return item, exists, nil return item, exists, nil
} }

View File

@ -69,6 +69,9 @@ func (u *UndeltaStore) List() []interface{} {
func (u *UndeltaStore) Get(obj interface{}) (item interface{}, exists bool, err error) { func (u *UndeltaStore) Get(obj interface{}) (item interface{}, exists bool, err error) {
return u.ActualStore.Get(obj) 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 { func (u *UndeltaStore) Replace(list []interface{}) error {
if err := u.ActualStore.Replace(list); err != nil { if err := u.ActualStore.Replace(list); err != nil {
return err return err