update_command.go 720 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package store
  2. import (
  3. "github.com/coreos/etcd/log"
  4. "github.com/coreos/go-raft"
  5. "time"
  6. )
  7. func init() {
  8. raft.RegisterCommand(&UpdateCommand{})
  9. }
  10. // Update command
  11. type UpdateCommand struct {
  12. Key string `json:"key"`
  13. Value string `json:"value"`
  14. ExpireTime time.Time `json:"expireTime"`
  15. }
  16. // The name of the update command in the log
  17. func (c *UpdateCommand) CommandName() string {
  18. return "etcd:update"
  19. }
  20. // Create node
  21. func (c *UpdateCommand) Apply(server raft.Server) (interface{}, error) {
  22. s, _ := server.StateMachine().(Store)
  23. e, err := s.Update(c.Key, c.Value, c.ExpireTime, server.CommitIndex(), server.Term())
  24. if err != nil {
  25. log.Debug(err)
  26. return nil, err
  27. }
  28. return e, nil
  29. }