create_command.go 768 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(&CreateCommand{})
  9. }
  10. // Create command
  11. type CreateCommand struct {
  12. Key string `json:"key"`
  13. Value string `json:"value"`
  14. ExpireTime time.Time `json:"expireTime"`
  15. Unique bool `json:"unique"`
  16. }
  17. // The name of the create command in the log
  18. func (c *CreateCommand) CommandName() string {
  19. return "etcd:create"
  20. }
  21. // Create node
  22. func (c *CreateCommand) Apply(server raft.Server) (interface{}, error) {
  23. s, _ := server.StateMachine().(Store)
  24. e, err := s.Create(c.Key, c.Value, c.Unique, c.ExpireTime, server.CommitIndex(), server.Term())
  25. if err != nil {
  26. log.Debug(err)
  27. return nil, err
  28. }
  29. return e, nil
  30. }