set_command.go 749 B

1234567891011121314151617181920212223242526272829303132333435363738
  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(&SetCommand{})
  9. }
  10. // Create command
  11. type SetCommand struct {
  12. Key string `json:"key"`
  13. Value string `json:"value"`
  14. ExpireTime time.Time `json:"expireTime"`
  15. }
  16. // The name of the create command in the log
  17. func (c *SetCommand) CommandName() string {
  18. return "etcd:set"
  19. }
  20. // Create node
  21. func (c *SetCommand) Apply(server raft.Server) (interface{}, error) {
  22. s, _ := server.StateMachine().(Store)
  23. // create a new node or replace the old node.
  24. e, err := s.Set(c.Key, 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. }