github.com/macb/etcd@v0.3.1-0.20140227003422-a60481c6b1a0/store/v2/compare_and_swap_command.go (about) 1 package v2 2 3 import ( 4 "time" 5 6 "github.com/coreos/etcd/log" 7 "github.com/coreos/etcd/store" 8 "github.com/coreos/etcd/third_party/github.com/coreos/raft" 9 ) 10 11 func init() { 12 raft.RegisterCommand(&CompareAndSwapCommand{}) 13 } 14 15 // The CompareAndSwap performs a conditional update on a key in the store. 16 type CompareAndSwapCommand struct { 17 Key string `json:"key"` 18 Value string `json:"value"` 19 ExpireTime time.Time `json:"expireTime"` 20 PrevValue string `json:"prevValue"` 21 PrevIndex uint64 `json:"prevIndex"` 22 } 23 24 // The name of the testAndSet command in the log 25 func (c *CompareAndSwapCommand) CommandName() string { 26 return "etcd:compareAndSwap" 27 } 28 29 // Set the key-value pair if the current value of the key equals to the given prevValue 30 func (c *CompareAndSwapCommand) Apply(context raft.Context) (interface{}, error) { 31 s, _ := context.Server().StateMachine().(store.Store) 32 33 e, err := s.CompareAndSwap(c.Key, c.PrevValue, c.PrevIndex, c.Value, c.ExpireTime) 34 35 if err != nil { 36 log.Debug(err) 37 return nil, err 38 } 39 40 return e, nil 41 }