etcdserver: introduce Set

This commit is contained in:
Blake Mizerany 2014-08-27 11:54:29 -07:00 committed by Yicheng Qin
parent e79e9c4853
commit 78d7b38a17
3 changed files with 31 additions and 14 deletions

View File

@ -35,7 +35,7 @@ type Request struct {
Dir bool `protobuf:"varint,5,req,name=dir" json:"dir"` Dir bool `protobuf:"varint,5,req,name=dir" json:"dir"`
PrevValue string `protobuf:"bytes,6,req,name=prevValue" json:"prevValue"` PrevValue string `protobuf:"bytes,6,req,name=prevValue" json:"prevValue"`
PrevIndex uint64 `protobuf:"varint,7,req,name=prevIndex" json:"prevIndex"` PrevIndex uint64 `protobuf:"varint,7,req,name=prevIndex" json:"prevIndex"`
PrevExists bool `protobuf:"varint,8,req,name=prevExists" json:"prevExists"` PrevExists *bool `protobuf:"varint,8,req,name=prevExists" json:"prevExists,omitempty"`
Expiration int64 `protobuf:"varint,9,req,name=expiration" json:"expiration"` Expiration int64 `protobuf:"varint,9,req,name=expiration" json:"expiration"`
Wait bool `protobuf:"varint,10,req,name=wait" json:"wait"` Wait bool `protobuf:"varint,10,req,name=wait" json:"wait"`
Since uint64 `protobuf:"varint,11,req,name=since" json:"since"` Since uint64 `protobuf:"varint,11,req,name=since" json:"since"`
@ -220,7 +220,8 @@ func (m *Request) Unmarshal(data []byte) error {
break break
} }
} }
m.PrevExists = bool(v != 0) b := bool(v != 0)
m.PrevExists = &b
case 9: case 9:
if wireType != 0 { if wireType != 0 {
return code_google_com_p_gogoprotobuf_proto.ErrWrongType return code_google_com_p_gogoprotobuf_proto.ErrWrongType
@ -339,7 +340,9 @@ func (m *Request) Size() (n int) {
l = len(m.PrevValue) l = len(m.PrevValue)
n += 1 + l + sovRequest(uint64(l)) n += 1 + l + sovRequest(uint64(l))
n += 1 + sovRequest(uint64(m.PrevIndex)) n += 1 + sovRequest(uint64(m.PrevIndex))
n += 2 if m.PrevExists != nil {
n += 2
}
n += 1 + sovRequest(uint64(m.Expiration)) n += 1 + sovRequest(uint64(m.Expiration))
n += 2 n += 2
n += 1 + sovRequest(uint64(m.Since)) n += 1 + sovRequest(uint64(m.Since))
@ -409,14 +412,16 @@ func (m *Request) MarshalTo(data []byte) (n int, err error) {
data[i] = 0x38 data[i] = 0x38
i++ i++
i = encodeVarintRequest(data, i, uint64(m.PrevIndex)) i = encodeVarintRequest(data, i, uint64(m.PrevIndex))
data[i] = 0x40 if m.PrevExists != nil {
i++ data[i] = 0x40
if m.PrevExists { i++
data[i] = 1 if *m.PrevExists {
} else { data[i] = 1
data[i] = 0 } else {
data[i] = 0
}
i++
} }
i++
data[i] = 0x48 data[i] = 0x48
i++ i++
i = encodeVarintRequest(data, i, uint64(m.Expiration)) i = encodeVarintRequest(data, i, uint64(m.Expiration))

View File

@ -15,7 +15,7 @@ message Request {
required bool dir = 5 [(gogoproto.nullable) = false]; required bool dir = 5 [(gogoproto.nullable) = false];
required string prevValue = 6 [(gogoproto.nullable) = false]; required string prevValue = 6 [(gogoproto.nullable) = false];
required uint64 prevIndex = 7 [(gogoproto.nullable) = false]; required uint64 prevIndex = 7 [(gogoproto.nullable) = false];
required bool prevExists = 8 [(gogoproto.nullable) = false]; required bool prevExists = 8 [(gogoproto.nullable) = true];
required int64 expiration = 9 [(gogoproto.nullable) = false]; required int64 expiration = 9 [(gogoproto.nullable) = false];
required bool wait = 10 [(gogoproto.nullable) = false]; required bool wait = 10 [(gogoproto.nullable) = false];
required uint64 since = 11 [(gogoproto.nullable) = false]; required uint64 since = 11 [(gogoproto.nullable) = false];

View File

@ -119,13 +119,18 @@ func (s *Server) apply(ctx context.Context, e raft.Entry) (*store.Event, error)
case "POST": case "POST":
return s.st.Create(r.Path, r.Dir, r.Val, true, expr) return s.st.Create(r.Path, r.Dir, r.Val, true, expr)
case "PUT": case "PUT":
exists, set := getBool(r.PrevExists)
switch { switch {
case r.PrevExists: case set:
return s.st.Update(r.Path, r.Val, expr) if exists {
return s.st.Update(r.Path, r.Val, expr)
} else {
return s.st.Create(r.Path, r.Dir, r.Val, false, expr)
}
case r.PrevIndex > 0 || r.PrevValue != "": case r.PrevIndex > 0 || r.PrevValue != "":
return s.st.CompareAndSwap(r.Path, r.PrevValue, r.PrevIndex, r.Val, expr) return s.st.CompareAndSwap(r.Path, r.PrevValue, r.PrevIndex, r.Val, expr)
default: default:
return s.st.Create(r.Path, r.Dir, r.Val, false, expr) return s.st.Set(r.Path, r.Dir, r.Val, expr)
} }
case "DELETE": case "DELETE":
switch { switch {
@ -138,3 +143,10 @@ func (s *Server) apply(ctx context.Context, e raft.Entry) (*store.Event, error)
return nil, ErrUnknownMethod return nil, ErrUnknownMethod
} }
} }
func getBool(v *bool) (vv bool, set bool) {
if v == nil {
return false, false
}
return *v, true
}