Make sure we remove old object from index before adding new

pull/6/head
derekwaynecarr 2015-02-09 16:34:44 -05:00
parent 1cf69bdefc
commit 53f1efa484
1 changed files with 14 additions and 7 deletions

View File

@ -78,22 +78,28 @@ func (c *cache) Add(obj interface{}) error {
if err != nil {
return fmt.Errorf("couldn't create key for object: %v", err)
}
// keep a pointer to whatever could have been there previously
c.lock.Lock()
defer c.lock.Unlock()
oldObject := c.items[key]
c.items[key] = obj
c.updateIndices(obj)
c.updateIndices(oldObject, obj)
return nil
}
// updateIndices modifies the objects location in the managed indexes
// updateIndices modifies the objects location in the managed indexes, if this is an update, you must provide an oldObj
// updateIndices must be called from a function that already has a lock on the cache
func (c *cache) updateIndices(obj interface{}) error {
key, err := c.keyFunc(obj)
func (c *cache) updateIndices(oldObj interface{}, newObj interface{}) error {
// if we got an old object, we need to remove it before we add it again
if oldObj != nil {
c.deleteFromIndices(oldObj)
}
key, err := c.keyFunc(newObj)
if err != nil {
return err
}
for name, indexFunc := range c.indexers {
indexValue, err := indexFunc(obj)
indexValue, err := indexFunc(newObj)
if err != nil {
return err
}
@ -143,8 +149,9 @@ func (c *cache) Update(obj interface{}) error {
}
c.lock.Lock()
defer c.lock.Unlock()
oldObject := c.items[key]
c.items[key] = obj
c.updateIndices(obj)
c.updateIndices(oldObject, obj)
return nil
}
@ -236,7 +243,7 @@ func (c *cache) Replace(list []interface{}) error {
// rebuild any index
c.indices = Indices{}
for _, item := range c.items {
c.updateIndices(item)
c.updateIndices(nil, item)
}
return nil