feat add set command

This commit is contained in:
Xiang Li
2013-10-14 22:44:17 -07:00
parent 2aeb25e80c
commit 53a9bd0618
5 changed files with 42 additions and 8 deletions

View File

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

View File

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

View File

@@ -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. // Set command: create a new node or replace the old one.
if !valueOk && !indexOk && !existOk { if !valueOk && !indexOk && !existOk {
c = &store.CreateCommand{ c = &store.SetCommand{
Key: key, Key: key,
Value: value, Value: value,
ExpireTime: expireTime, ExpireTime: expireTime,
Force: true,
} }
return s.Dispatch(c, w, req) return s.Dispatch(c, w, req)
} }

View File

@@ -16,7 +16,6 @@ type CreateCommand struct {
Value string `json:"value"` Value string `json:"value"`
ExpireTime time.Time `json:"expireTime"` ExpireTime time.Time `json:"expireTime"`
IncrementalSuffix bool `json:"incrementalSuffix"` IncrementalSuffix bool `json:"incrementalSuffix"`
Force bool `json:"force"`
} }
// The name of the create command in the log // 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) { func (c *CreateCommand) Apply(server raft.Server) (interface{}, error) {
s, _ := server.StateMachine().(Store) 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 { if err != nil {
log.Debug(err) log.Debug(err)

38
store/set_command.go Normal file
View File

@@ -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
}