mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
feat POST-create unique node under given path
This commit is contained in:
@@ -22,7 +22,7 @@ func PostHandler(w http.ResponseWriter, req *http.Request, s Server) error {
|
||||
Key: key,
|
||||
Value: value,
|
||||
ExpireTime: expireTime,
|
||||
IncrementalSuffix: (req.FormValue("incremental") == "true"),
|
||||
Unique: true,
|
||||
}
|
||||
|
||||
return s.Dispatch(c, w, req)
|
||||
|
||||
@@ -15,7 +15,7 @@ type CreateCommand struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
ExpireTime time.Time `json:"expireTime"`
|
||||
IncrementalSuffix bool `json:"incrementalSuffix"`
|
||||
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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user