mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
42 lines
1013 B
Go
42 lines
1013 B
Go
package store
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/coreos/etcd/log"
|
|
"github.com/coreos/go-raft"
|
|
)
|
|
|
|
func init() {
|
|
raft.RegisterCommand(&CompareAndSwapCommand{})
|
|
}
|
|
|
|
// The CompareAndSwap performs a conditional update on a key in the store.
|
|
type CompareAndSwapCommand struct {
|
|
Key string `json:"key"`
|
|
Value string `json:"value"`
|
|
ExpireTime time.Time `json:"expireTime"`
|
|
PrevValue string `json:"prevValue"`
|
|
PrevIndex uint64 `json:"prevIndex"`
|
|
}
|
|
|
|
// The name of the testAndSet command in the log
|
|
func (c *CompareAndSwapCommand) CommandName() string {
|
|
return "etcd:compareAndSwap"
|
|
}
|
|
|
|
// Set the key-value pair if the current value of the key equals to the given prevValue
|
|
func (c *CompareAndSwapCommand) Apply(server raft.Server) (interface{}, error) {
|
|
s, _ := server.StateMachine().(Store)
|
|
|
|
e, err := s.CompareAndSwap(c.Key, c.PrevValue, c.PrevIndex,
|
|
c.Value, c.ExpireTime, server.CommitIndex(), server.Term())
|
|
|
|
if err != nil {
|
|
log.Debug(err)
|
|
return nil, err
|
|
}
|
|
|
|
return e, nil
|
|
}
|