github.com/sym3tri/etcd@v0.2.1-0.20140422215517-a563d82f95d6/store/v2/compare_and_delete_command.go (about)

     1  package v2
     2  
     3  import (
     4  	"github.com/coreos/etcd/log"
     5  	"github.com/coreos/etcd/store"
     6  	"github.com/coreos/etcd/third_party/github.com/goraft/raft"
     7  )
     8  
     9  func init() {
    10  	raft.RegisterCommand(&CompareAndDeleteCommand{})
    11  }
    12  
    13  // The CompareAndDelete performs a conditional delete on a key in the store.
    14  type CompareAndDeleteCommand struct {
    15  	Key       string `json:"key"`
    16  	PrevValue string `json:"prevValue"`
    17  	PrevIndex uint64 `json:"prevIndex"`
    18  }
    19  
    20  // The name of the compareAndDelete command in the log
    21  func (c *CompareAndDeleteCommand) CommandName() string {
    22  	return "etcd:compareAndDelete"
    23  }
    24  
    25  // Set the key-value pair if the current value of the key equals to the given prevValue
    26  func (c *CompareAndDeleteCommand) Apply(server raft.Server) (interface{}, error) {
    27  	s, _ := server.StateMachine().(store.Store)
    28  
    29  	e, err := s.CompareAndDelete(c.Key, c.PrevValue, c.PrevIndex)
    30  
    31  	if err != nil {
    32  		log.Debug(err)
    33  		return nil, err
    34  	}
    35  
    36  	return e, nil
    37  }