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