delete_command.go 762 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package v2
  2. import (
  3. "github.com/coreos/etcd/log"
  4. "github.com/coreos/etcd/store"
  5. "github.com/coreos/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(server raft.Server) (interface{}, error) {
  22. s, _ := server.StateMachine().(store.Store)
  23. if c.Recursive {
  24. c.Dir = true
  25. }
  26. e, err := s.Delete(c.Key, c.Dir, c.Recursive)
  27. if err != nil {
  28. log.Debug(err)
  29. return nil, err
  30. }
  31. return e, nil
  32. }