Commit 3ca23163 authored by Clayton Coleman's avatar Clayton Coleman

Add a GetByKey method to Store

Without the ability to retrieve underlying items by key (instead of the object passed to KeyFunc) it is impossible to build wrappers around cache.Store that want to make decisions about keys, because they would need to reconstruct the object passed to Get. This allows retrieval by key, and makes sure Get(obj) uses it.
parent e335e2d3
......@@ -86,13 +86,18 @@ func (f *FIFO) List() []interface{} {
// Get returns the requested item, or sets exists=false.
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 {
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()
defer f.lock.RUnlock()
item, exists = f.items[id]
item, exists = f.items[key]
return item, exists, nil
}
......
......@@ -37,6 +37,7 @@ type Store interface {
Delete(obj interface{}) error
List() []interface{}
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
// 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.
func (c *cache) Add(obj interface{}) error {
id, err := c.keyFunc(obj)
key, err := c.keyFunc(obj)
if err != nil {
return fmt.Errorf("couldn't create key for object: %v", err)
}
c.lock.Lock()
defer c.lock.Unlock()
c.items[id] = obj
c.items[key] = obj
return nil
}
// Update sets an item in the cache to its updated state.
func (c *cache) Update(obj interface{}) error {
id, err := c.keyFunc(obj)
key, err := c.keyFunc(obj)
if err != nil {
return fmt.Errorf("couldn't create key for object: %v", err)
}
c.lock.Lock()
defer c.lock.Unlock()
c.items[id] = obj
c.items[key] = obj
return nil
}
// Delete removes an item from the cache.
func (c *cache) Delete(obj interface{}) error {
id, err := c.keyFunc(obj)
key, err := c.keyFunc(obj)
if err != nil {
return fmt.Errorf("couldn't create key for object: %v", err)
}
c.lock.Lock()
defer c.lock.Unlock()
delete(c.items, id)
delete(c.items, key)
return nil
}
......@@ -117,13 +118,19 @@ func (c *cache) List() []interface{} {
// Get returns the requested item, or sets exists=false.
// 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) {
id, _ := c.keyFunc(obj)
key, _ := c.keyFunc(obj)
if err != nil {
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()
defer c.lock.RUnlock()
item, exists = c.items[id]
item, exists = c.items[key]
return item, exists, nil
}
......
......@@ -69,6 +69,9 @@ func (u *UndeltaStore) List() []interface{} {
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 {
return err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment