delete_command.go 836 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package v2
  2. import (
  3. "github.com/coreos/etcd/log"
  4. "github.com/coreos/etcd/store"
  5. "github.com/coreos/etcd/third_party/github.com/goraft/raft"
  6. )
  7. func init() {
  8. raft.RegisterCommand(&DeleteCommand{})
  9. }
  10. // The DeleteCommand removes a key from the Store.
  11. type DeleteCommand struct {
  12. Key string `json:"key"`
  13. Recursive bool `json:"recursive"`
  14. Dir bool `json:"dir"`
  15. }
  16. // The name of the delete command in the log
  17. func (c *DeleteCommand) CommandName() string {
  18. return "etcd:delete"
  19. }
  20. // Delete the key
  21. func (c *DeleteCommand) Apply(context raft.Context) (interface{}, error) {
  22. s, _ := context.Server().StateMachine().(store.Store)
  23. if c.Recursive {
  24. // recursive implies dir
  25. c.Dir = true
  26. }
  27. e, err := s.Delete(c.Key, c.Dir, c.Recursive)
  28. if err != nil {
  29. log.Debug(err)
  30. return nil, err
  31. }
  32. return e, nil
  33. }