create_command.go 856 B

123456789101112131415161718192021222324252627282930313233343536
  1. package command
  2. import (
  3. "github.com/coreos/etcd/log"
  4. "github.com/coreos/etcd/store"
  5. "github.com/coreos/go-raft"
  6. "time"
  7. )
  8. // Create command
  9. type CreateCommand struct {
  10. Key string `json:"key"`
  11. Value string `json:"value"`
  12. ExpireTime time.Time `json:"expireTime"`
  13. IncrementalSuffix bool `json:"incrementalSuffix"`
  14. Force bool `json:"force"`
  15. }
  16. // The name of the create command in the log
  17. func (c *CreateCommand) CommandName() string {
  18. return "etcd:create"
  19. }
  20. // Create node
  21. func (c *CreateCommand) Apply(server *raft.Server) (interface{}, error) {
  22. s, _ := server.StateMachine().(*store.Store)
  23. e, err := s.Create(c.Key, c.Value, c.IncrementalSuffix, c.Force, c.ExpireTime, server.CommitIndex(), server.Term())
  24. if err != nil {
  25. log.Debug(err)
  26. return nil, err
  27. }
  28. return e, nil
  29. }