From baa683b48443fd1fc4012aa3ef3370a4a6a37687 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Tue, 15 Oct 2013 22:21:55 -0700 Subject: [PATCH] feat POST-create unique node under given path --- server/v2/post_handler.go | 8 ++++---- store/create_command.go | 10 +++++----- store/store.go | 10 +++++----- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/server/v2/post_handler.go b/server/v2/post_handler.go index dda146e5e..b55eddba4 100644 --- a/server/v2/post_handler.go +++ b/server/v2/post_handler.go @@ -19,10 +19,10 @@ func PostHandler(w http.ResponseWriter, req *http.Request, s Server) error { } c := &store.CreateCommand{ - Key: key, - Value: value, - ExpireTime: expireTime, - IncrementalSuffix: (req.FormValue("incremental") == "true"), + Key: key, + Value: value, + ExpireTime: expireTime, + Unique: true, } return s.Dispatch(c, w, req) diff --git a/store/create_command.go b/store/create_command.go index b9f1aced5..6a2487cf0 100644 --- a/store/create_command.go +++ b/store/create_command.go @@ -12,10 +12,10 @@ func init() { // Create command type CreateCommand struct { - Key string `json:"key"` - Value string `json:"value"` - ExpireTime time.Time `json:"expireTime"` - IncrementalSuffix bool `json:"incrementalSuffix"` + Key string `json:"key"` + Value string `json:"value"` + ExpireTime time.Time `json:"expireTime"` + Unique bool `json:"unique"` } // The name of the create command in the log @@ -27,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.ExpireTime, server.CommitIndex(), server.Term()) + e, err := s.Create(c.Key, c.Value, c.Unique, c.ExpireTime, server.CommitIndex(), server.Term()) if err != nil { log.Debug(err) diff --git a/store/store.go b/store/store.go index 8cecfb178..73d56bee5 100644 --- a/store/store.go +++ b/store/store.go @@ -107,13 +107,13 @@ func (s *store) Get(nodePath string, recursive, sorted bool, index uint64, term // Create function creates the Node at nodePath. Create will help to create intermediate directories with no ttl. // If the node has already existed, create will fail. // If any node on the path is a file, create will fail. -func (s *store) Create(nodePath string, value string, incrementalSuffix bool, +func (s *store) Create(nodePath string, value string, unique bool, expireTime time.Time, index uint64, term uint64) (*Event, error) { nodePath = path.Clean(path.Join("/", nodePath)) s.worldLock.Lock() defer s.worldLock.Unlock() - return s.internalCreate(nodePath, value, incrementalSuffix, false, expireTime, index, term, Create) + return s.internalCreate(nodePath, value, unique, false, expireTime, index, term, Create) } // Set function creates or replace the Node at nodePath. @@ -302,13 +302,13 @@ func (s *store) update(nodePath string, newValue string, expireTime time.Time, i return e, nil } -func (s *store) internalCreate(nodePath string, value string, incrementalSuffix bool, replace bool, +func (s *store) internalCreate(nodePath string, value string, unique bool, replace bool, expireTime time.Time, index uint64, term uint64, action string) (*Event, error) { s.Index, s.Term = index, term - if incrementalSuffix { // append unique incremental suffix to the node path - nodePath += "_" + strconv.FormatUint(index, 10) + if unique { // append unique item under the node path + nodePath += "/" + strconv.FormatUint(index, 10) } nodePath = path.Clean(path.Join("/", nodePath))