fix v1 handler: use create command when test against prevexistence

This commit is contained in:
Xiang Li 2013-10-16 23:57:42 -07:00
parent 7fa99b3794
commit 28722e2127
2 changed files with 19 additions and 8 deletions

View File

@ -30,12 +30,20 @@ func SetKeyHandler(w http.ResponseWriter, req *http.Request, s Server) error {
// If the "prevValue" is specified then test-and-set. Otherwise create a new key.
var c raft.Command
if prevValueArr, ok := req.Form["prevValue"]; ok && len(prevValueArr) > 0 {
c = &store.CompareAndSwapCommand{
Key: key,
Value: value,
PrevValue: prevValueArr[0],
ExpireTime: expireTime,
if prevValueArr, ok := req.Form["prevValue"]; ok {
if len(prevValueArr) > 0 { // test against previous value
c = &store.CompareAndSwapCommand{
Key: key,
Value: value,
PrevValue: prevValueArr[0],
ExpireTime: expireTime,
}
} else {
c = &store.CreateCommand{ // test against existence
Key: key,
Value: value,
ExpireTime: expireTime,
}
}
} else {

View File

@ -55,13 +55,16 @@ func (event *Event) Response() interface{} {
Expiration: event.Expiration,
}
if response.Action == Create || response.Action == Set {
response.Action = "set"
if response.Action == Set {
if response.PrevValue == "" {
response.NewKey = true
}
}
if response.Action == CompareAndSwap || response.Action == Create {
response.Action = "testAndSet"
}
return response
} else {
responses := make([]*Response, len(event.KVPairs))