Browse Source

refactor store.go add set function

Xiang Li 12 years ago
parent
commit
fbf40fb74a
5 changed files with 19 additions and 8 deletions
  1. 1 1
      server/registry.go
  2. 1 1
      store/create_command.go
  3. 2 1
      store/event.go
  4. 1 1
      store/set_command.go
  5. 14 4
      store/store.go

+ 1 - 1
server/registry.go

@@ -45,7 +45,7 @@ func (r *Registry) Register(name string, peerVersion string, peerURL string, url
 	// Write data to store.
 	// Write data to store.
 	key := path.Join(RegistryKey, name)
 	key := path.Join(RegistryKey, name)
 	value := fmt.Sprintf("raft=%s&etcd=%s&raftVersion=%s", peerURL, url, peerVersion)
 	value := fmt.Sprintf("raft=%s&etcd=%s&raftVersion=%s", peerURL, url, peerVersion)
-	_, err := r.store.Create(key, value, false, false, store.Permanent, commitIndex, term)
+	_, err := r.store.Create(key, value, false, store.Permanent, commitIndex, term)
 	log.Debugf("Register: %s (%v)", name, err)
 	log.Debugf("Register: %s (%v)", name, err)
 	return err
 	return err
 }
 }

+ 1 - 1
store/create_command.go

@@ -27,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, false, c.ExpireTime, server.CommitIndex(), server.Term())
+	e, err := s.Create(c.Key, c.Value, c.IncrementalSuffix, c.ExpireTime, server.CommitIndex(), server.Term())
 
 
 	if err != nil {
 	if err != nil {
 		log.Debug(err)
 		log.Debug(err)

+ 2 - 1
store/event.go

@@ -7,6 +7,7 @@ import (
 const (
 const (
 	Get            = "get"
 	Get            = "get"
 	Create         = "create"
 	Create         = "create"
+	Set            = "set"
 	Update         = "update"
 	Update         = "update"
 	Delete         = "delete"
 	Delete         = "delete"
 	CompareAndSwap = "compareAndSwap"
 	CompareAndSwap = "compareAndSwap"
@@ -54,7 +55,7 @@ func (event *Event) Response() interface{} {
 			Expiration: event.Expiration,
 			Expiration: event.Expiration,
 		}
 		}
 
 
-		if response.Action == Create || response.Action == Update {
+		if response.Action == Create || response.Action == Set {
 			response.Action = "set"
 			response.Action = "set"
 			if response.PrevValue == "" {
 			if response.PrevValue == "" {
 				response.NewKey = true
 				response.NewKey = true

+ 1 - 1
store/set_command.go

@@ -27,7 +27,7 @@ func (c *SetCommand) Apply(server raft.Server) (interface{}, error) {
 	s, _ := server.StateMachine().(Store)
 	s, _ := server.StateMachine().(Store)
 
 
 	// create a new node or replace the old node.
 	// 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())
+	e, err := s.Set(c.Key, c.Value, c.ExpireTime, server.CommitIndex(), server.Term())
 
 
 	if err != nil {
 	if err != nil {
 		log.Debug(err)
 		log.Debug(err)

+ 14 - 4
store/store.go

@@ -15,8 +15,9 @@ import (
 
 
 type Store interface {
 type Store interface {
 	Get(nodePath string, recursive, sorted bool, index uint64, term uint64) (*Event, error)
 	Get(nodePath string, recursive, sorted bool, index uint64, term uint64) (*Event, error)
-	Create(nodePath string, value string, incrementalSuffix bool, force bool,
-		expireTime time.Time, index uint64, term uint64) (*Event, error)
+	Set(nodePath string, value string, expireTime time.Time, index uint64, term uint64) (*Event, error)
+	Create(nodePath string, value string, incrementalSuffix bool, expireTime time.Time,
+		index uint64, term uint64) (*Event, error)
 	CompareAndSwap(nodePath string, prevValue string, prevIndex uint64,
 	CompareAndSwap(nodePath string, prevValue string, prevIndex uint64,
 		value string, expireTime time.Time, index uint64, term uint64) (*Event, error)
 		value string, expireTime time.Time, index uint64, term uint64) (*Event, error)
 	Delete(nodePath string, recursive bool, index uint64, term uint64) (*Event, error)
 	Delete(nodePath string, recursive bool, index uint64, term uint64) (*Event, error)
@@ -106,13 +107,22 @@ 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.
 // 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 the node has already existed, create will fail.
 // If any node on the path is a file, 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, force bool,
+func (s *store) Create(nodePath string, value string, incrementalSuffix bool,
 	expireTime time.Time, index uint64, term uint64) (*Event, error) {
 	expireTime time.Time, index uint64, term uint64) (*Event, error) {
 	nodePath = path.Clean(path.Join("/", nodePath))
 	nodePath = path.Clean(path.Join("/", nodePath))
 
 
 	s.worldLock.Lock()
 	s.worldLock.Lock()
 	defer s.worldLock.Unlock()
 	defer s.worldLock.Unlock()
-	return s.internalCreate(nodePath, value, incrementalSuffix, force, expireTime, index, term, Create)
+	return s.internalCreate(nodePath, value, incrementalSuffix, false, expireTime, index, term, Create)
+}
+
+// Set function creates or replace the Node at nodePath.
+func (s *store) Set(nodePath string, value string, 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, false, true, expireTime, index, term, Set)
 }
 }
 
 
 func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint64,
 func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint64,