feat POST-create unique node under given path
parent
01bbad31c7
commit
baa683b484
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue