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,7 +215,12 @@ 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.
// Command will be executed, only if both of the tests are successful.
if n.Compare(prevValue, prevIndex) {
if !n.Compare(prevValue, prevIndex) {
cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex)
s.Stats.Inc(CompareAndSwapFail)
return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex)
}
// update etcd index
s.CurrentIndex++
@ -234,11 +239,6 @@ func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint
s.WatcherHub.notify(e)
s.Stats.Inc(CompareAndSwapSuccess)
return e, nil
}
cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex)
s.Stats.Inc(CompareAndSwapFail)
return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex)
}
// Delete function deletes the node at the given path.
@ -316,7 +316,12 @@ 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.
// Command will be executed, only if both of the tests are successful.
if n.Compare(prevValue, prevIndex) {
if !n.Compare(prevValue, prevIndex) {
cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex)
s.Stats.Inc(CompareAndDeleteFail)
return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex)
}
// update etcd index
s.CurrentIndex++
@ -333,11 +338,6 @@ func (s *store) CompareAndDelete(nodePath string, prevValue string, prevIndex ui
s.WatcherHub.notify(e)
s.Stats.Inc(CompareAndDeleteSuccess)
return e, nil
}
cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex)
s.Stats.Inc(CompareAndDeleteFail)
return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex)
}
func (s *store) Watch(key string, recursive bool, sinceIndex uint64) (<-chan *Event, error) {