refactor(store.go) handle short condition first

This commit is contained in:
Xiang Li 2013-12-25 19:01:04 +08:00
parent c4179829d6
commit bfa7d54b02

View File

@ -215,30 +215,30 @@ func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint
// If both of the prevValue and prevIndex are given, we will test both of them. // If both of the prevValue and prevIndex are given, we will test both of them.
// Command will be executed, only if both of the tests are successful. // Command will be executed, only if both of the tests are successful.
if n.Compare(prevValue, prevIndex) { if !n.Compare(prevValue, prevIndex) {
// update etcd index cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex)
s.CurrentIndex++ s.Stats.Inc(CompareAndSwapFail)
return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex)
e := newEvent(CompareAndSwap, nodePath, s.CurrentIndex, n.CreatedIndex)
eNode := e.Node
eNode.PrevValue = n.Value
// if test succeed, write the value
n.Write(value, s.CurrentIndex)
n.UpdateTTL(expireTime)
eNode.Value = value
eNode.Expiration, eNode.TTL = n.ExpirationAndTTL()
s.WatcherHub.notify(e)
s.Stats.Inc(CompareAndSwapSuccess)
return e, nil
} }
cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex) // update etcd index
s.Stats.Inc(CompareAndSwapFail) s.CurrentIndex++
return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex)
e := newEvent(CompareAndSwap, nodePath, s.CurrentIndex, n.CreatedIndex)
eNode := e.Node
eNode.PrevValue = n.Value
// if test succeed, write the value
n.Write(value, s.CurrentIndex)
n.UpdateTTL(expireTime)
eNode.Value = value
eNode.Expiration, eNode.TTL = n.ExpirationAndTTL()
s.WatcherHub.notify(e)
s.Stats.Inc(CompareAndSwapSuccess)
return e, nil
} }
// Delete function deletes the node at the given path. // Delete function deletes the node at the given path.
@ -316,28 +316,28 @@ func (s *store) CompareAndDelete(nodePath string, prevValue string, prevIndex ui
// If both of the prevValue and prevIndex are given, we will test both of them. // If both of the prevValue and prevIndex are given, we will test both of them.
// Command will be executed, only if both of the tests are successful. // Command will be executed, only if both of the tests are successful.
if n.Compare(prevValue, prevIndex) { if !n.Compare(prevValue, prevIndex) {
// update etcd index cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex)
s.CurrentIndex++ s.Stats.Inc(CompareAndDeleteFail)
return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex)
e := newEvent(CompareAndDelete, nodePath, s.CurrentIndex, n.CreatedIndex)
callback := func(path string) { // notify function
// notify the watchers with deleted set true
s.WatcherHub.notifyWatchers(e, path, true)
}
// delete a key-value pair, no error should happen
n.Remove(false, false, callback)
s.WatcherHub.notify(e)
s.Stats.Inc(CompareAndDeleteSuccess)
return e, nil
} }
cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex) // update etcd index
s.Stats.Inc(CompareAndDeleteFail) s.CurrentIndex++
return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex)
e := newEvent(CompareAndDelete, nodePath, s.CurrentIndex, n.CreatedIndex)
callback := func(path string) { // notify function
// notify the watchers with deleted set true
s.WatcherHub.notifyWatchers(e, path, true)
}
// delete a key-value pair, no error should happen
n.Remove(false, false, callback)
s.WatcherHub.notify(e)
s.Stats.Inc(CompareAndDeleteSuccess)
return e, nil
} }
func (s *store) Watch(key string, recursive bool, sinceIndex uint64) (<-chan *Event, error) { func (s *store) Watch(key string, recursive bool, sinceIndex uint64) (<-chan *Event, error) {