github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/store/v2/set_command.go (about)

     1  package v2
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/coreos/etcd/log"
     7  	"github.com/coreos/etcd/store"
     8  	"github.com/coreos/etcd/third_party/github.com/goraft/raft"
     9  )
    10  
    11  func init() {
    12  	raft.RegisterCommand(&SetCommand{})
    13  }
    14  
    15  // Create command
    16  type SetCommand struct {
    17  	Key        string    `json:"key"`
    18  	Value      string    `json:"value"`
    19  	ExpireTime time.Time `json:"expireTime"`
    20  	Dir        bool      `json:"dir"`
    21  }
    22  
    23  // The name of the create command in the log
    24  func (c *SetCommand) CommandName() string {
    25  	return "etcd:set"
    26  }
    27  
    28  // Create node
    29  func (c *SetCommand) Apply(context raft.Context) (interface{}, error) {
    30  	s, _ := context.Server().StateMachine().(store.Store)
    31  
    32  	// create a new node or replace the old node.
    33  	e, err := s.Set(c.Key, c.Dir, c.Value, c.ExpireTime)
    34  
    35  	if err != nil {
    36  		log.Debug(err)
    37  		return nil, err
    38  	}
    39  
    40  	return e, nil
    41  }