create_command.go 873 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. IncrementalSuffix bool `json:"incrementalSuffix"`
  16. Force bool `json:"force"`
  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)
  25. e, err := s.Create(c.Key, c.Value, c.IncrementalSuffix, c.Force, c.ExpireTime, server.CommitIndex(), server.Term())
  26. if err != nil {
  27. log.Debug(err)
  28. return nil, err
  29. }
  30. return e, nil
  31. }