Browse Source

feat add set command

Xiang Li 12 years ago
parent
commit
53a9bd0618
5 changed files with 42 additions and 8 deletions
  1. 1 2
      server/server.go
  2. 1 2
      server/v1/set_key_handler.go
  3. 1 2
      server/v2/put_handler.go
  4. 1 2
      store/create_command.go
  5. 38 0
      store/set_command.go

+ 1 - 2
server/server.go

@@ -254,11 +254,10 @@ func (s *Server) SpeedTestHandler(w http.ResponseWriter, req *http.Request) erro
 	for i := 0; i < count; i++ {
 		go func() {
 			for j := 0; j < 10; j++ {
-				c := &store.CreateCommand{
+				c := &store.SetCommand{
 					Key:        "foo",
 					Value:      "bar",
 					ExpireTime: time.Unix(0, 0),
-					Force:      true,
 				}
 				s.peerServer.RaftServer().Do(c)
 			}

+ 1 - 2
server/v1/set_key_handler.go

@@ -39,11 +39,10 @@ func SetKeyHandler(w http.ResponseWriter, req *http.Request, s Server) error {
 		}
 
 	} else {
-		c = &store.CreateCommand{
+		c = &store.SetCommand{
 			Key:        key,
 			Value:      value,
 			ExpireTime: expireTime,
-			Force:      true,
 		}
 	}
 

+ 1 - 2
server/v2/put_handler.go

@@ -35,11 +35,10 @@ func PutHandler(w http.ResponseWriter, req *http.Request, s Server) error {
 
 	// Set command: create a new node or replace the old one.
 	if !valueOk && !indexOk && !existOk {
-		c = &store.CreateCommand{
+		c = &store.SetCommand{
 			Key:        key,
 			Value:      value,
 			ExpireTime: expireTime,
-			Force:      true,
 		}
 		return s.Dispatch(c, w, req)
 	}

+ 1 - 2
store/create_command.go

@@ -16,7 +16,6 @@ type CreateCommand struct {
 	Value             string    `json:"value"`
 	ExpireTime        time.Time `json:"expireTime"`
 	IncrementalSuffix bool      `json:"incrementalSuffix"`
-	Force             bool      `json:"force"`
 }
 
 // The name of the create command in the log
@@ -28,7 +27,7 @@ func (c *CreateCommand) CommandName() string {
 func (c *CreateCommand) Apply(server raft.Server) (interface{}, error) {
 	s, _ := server.StateMachine().(Store)
 
-	e, err := s.Create(c.Key, c.Value, c.IncrementalSuffix, c.Force, c.ExpireTime, server.CommitIndex(), server.Term())
+	e, err := s.Create(c.Key, c.Value, c.IncrementalSuffix, false, c.ExpireTime, server.CommitIndex(), server.Term())
 
 	if err != nil {
 		log.Debug(err)

+ 38 - 0
store/set_command.go

@@ -0,0 +1,38 @@
+package store
+
+import (
+	"github.com/coreos/etcd/log"
+	"github.com/coreos/go-raft"
+	"time"
+)
+
+func init() {
+	raft.RegisterCommand(&CreateCommand{})
+}
+
+// Create command
+type SetCommand struct {
+	Key        string    `json:"key"`
+	Value      string    `json:"value"`
+	ExpireTime time.Time `json:"expireTime"`
+}
+
+// The name of the create command in the log
+func (c *SetCommand) CommandName() string {
+	return "etcd:set"
+}
+
+// Create node
+func (c *SetCommand) Apply(server raft.Server) (interface{}, error) {
+	s, _ := server.StateMachine().(Store)
+
+	// create a new node or replace the old node.
+	e, err := s.Create(c.Key, c.Value, false, true, c.ExpireTime, server.CommitIndex(), server.Term())
+
+	if err != nil {
+		log.Debug(err)
+		return nil, err
+	}
+
+	return e, nil
+}