compare_and_delete_command.go 953 B

1234567891011121314151617181920212223242526272829303132333435363738
  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(&CompareAndDeleteCommand{})
  9. }
  10. // The CompareAndDelete performs a conditional delete on a key in the store.
  11. type CompareAndDeleteCommand struct {
  12. Key string `json:"key"`
  13. PrevValue string `json:"prevValue"`
  14. PrevIndex uint64 `json:"prevIndex"`
  15. Recursive bool `json:"recursive"`
  16. }
  17. // The name of the compareAndDelete command in the log
  18. func (c *CompareAndDeleteCommand) CommandName() string {
  19. return "etcd:compareAndDelete"
  20. }
  21. // Set the key-value pair if the current value of the key equals to the given prevValue
  22. func (c *CompareAndDeleteCommand) Apply(server raft.Server) (interface{}, error) {
  23. s, _ := server.StateMachine().(store.Store)
  24. e, err := s.CompareAndDelete(c.Key, c.Recursive, c.PrevValue, c.PrevIndex)
  25. if err != nil {
  26. log.Debug(err)
  27. return nil, err
  28. }
  29. return e, nil
  30. }