set_command.go 748 B

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