From fbf40fb74a1c700fe786bc251eef1cc3d89738d5 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 14 Oct 2013 23:04:21 -0700 Subject: [PATCH] refactor store.go add set function --- server/registry.go | 2 +- store/create_command.go | 2 +- store/event.go | 3 ++- store/set_command.go | 2 +- store/store.go | 18 ++++++++++++++---- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/server/registry.go b/server/registry.go index 23ef9ddbb..fa63b5027 100644 --- a/server/registry.go +++ b/server/registry.go @@ -45,7 +45,7 @@ func (r *Registry) Register(name string, peerVersion string, peerURL string, url // Write data to store. key := path.Join(RegistryKey, name) 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) return err } diff --git a/store/create_command.go b/store/create_command.go index c1f57910a..b9f1aced5 100644 --- a/store/create_command.go +++ b/store/create_command.go @@ -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, 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 { log.Debug(err) diff --git a/store/event.go b/store/event.go index f9ae0938b..14ba1e529 100644 --- a/store/event.go +++ b/store/event.go @@ -7,6 +7,7 @@ import ( const ( Get = "get" Create = "create" + Set = "set" Update = "update" Delete = "delete" CompareAndSwap = "compareAndSwap" @@ -54,7 +55,7 @@ func (event *Event) Response() interface{} { Expiration: event.Expiration, } - if response.Action == Create || response.Action == Update { + if response.Action == Create || response.Action == Set { response.Action = "set" if response.PrevValue == "" { response.NewKey = true diff --git a/store/set_command.go b/store/set_command.go index 6c2bd6885..55635cd99 100644 --- a/store/set_command.go +++ b/store/set_command.go @@ -27,7 +27,7 @@ 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()) + e, err := s.Set(c.Key, c.Value, c.ExpireTime, server.CommitIndex(), server.Term()) if err != nil { log.Debug(err) diff --git a/store/store.go b/store/store.go index 6e0fe4d68..35dfd32c5 100644 --- a/store/store.go +++ b/store/store.go @@ -15,8 +15,9 @@ import ( type Store interface { 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, value string, expireTime time.Time, 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. // 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, force bool, +func (s *store) Create(nodePath string, value string, incrementalSuffix 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, 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,