refactor store.go add set function

This commit is contained in:
Xiang Li 2013-10-14 23:04:21 -07:00
parent 278a089908
commit fbf40fb74a
5 changed files with 19 additions and 8 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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,