test_and_set_command.go 971 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package command
  2. import (
  3. "time"
  4. "github.com/coreos/etcd/log"
  5. "github.com/coreos/etcd/store"
  6. "github.com/coreos/go-raft"
  7. )
  8. // The TestAndSetCommand performs a conditional update on a key in the store.
  9. type TestAndSetCommand struct {
  10. Key string `json:"key"`
  11. Value string `json:"value"`
  12. ExpireTime time.Time `json:"expireTime"`
  13. PrevValue string `json: prevValue`
  14. PrevIndex uint64 `json: prevIndex`
  15. }
  16. // The name of the testAndSet command in the log
  17. func (c *TestAndSetCommand) CommandName() string {
  18. return "etcd:testAndSet"
  19. }
  20. // Set the key-value pair if the current value of the key equals to the given prevValue
  21. func (c *TestAndSetCommand) Apply(server *raft.Server) (interface{}, error) {
  22. s, _ := server.StateMachine().(*store.Store)
  23. e, err := s.TestAndSet(c.Key, c.PrevValue, c.PrevIndex,
  24. c.Value, c.ExpireTime, server.CommitIndex(), server.Term())
  25. if err != nil {
  26. log.Debug(err)
  27. return nil, err
  28. }
  29. return e, nil
  30. }