compare_and_delete_command.go 938 B

12345678910111213141516171819202122232425262728293031323334353637
  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(&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. }
  16. // The name of the compareAndDelete command in the log
  17. func (c *CompareAndDeleteCommand) CommandName() string {
  18. return "etcd:compareAndDelete"
  19. }
  20. // Set the key-value pair if the current value of the key equals to the given prevValue
  21. func (c *CompareAndDeleteCommand) Apply(server raft.Server) (interface{}, error) {
  22. s, _ := server.StateMachine().(store.Store)
  23. e, err := s.CompareAndDelete(c.Key, c.PrevValue, c.PrevIndex)
  24. if err != nil {
  25. log.Debug(err)
  26. return nil, err
  27. }
  28. return e, nil
  29. }