1
0
mirror of https://github.com/docker/docs.git synced 2026-10-31 10:34:26 +03:00

Merge pull request #849 from abronan/cas_return_last_index

Return last index on CAS operation
This commit is contained in:
Victor Vieux
2015-05-26 15:57:20 -07:00
5 changed files with 26 additions and 13 deletions

View File

@@ -342,14 +342,27 @@ func (l *consulLock) Unlock() error {
// AtomicPut put a value at "key" if the key has not been
// modified in the meantime, throws an error if this is the case
func (s *Consul) AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, error) {
func (s *Consul) AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, *KVPair, error) {
lock, err := s.NewLock(key, nil)
if err != nil {
return false, nil, err
}
lock.Lock()
defer lock.Unlock()
p := &api.KVPair{Key: s.normalize(key), Value: value, ModifyIndex: previous.LastIndex}
if work, _, err := s.client.KV().CAS(p, nil); err != nil {
return false, err
return false, nil, err
} else if !work {
return false, ErrKeyModified
return false, nil, ErrKeyModified
}
return true, nil
pair, err := s.Get(key)
if err != nil {
return false, nil, err
}
return true, pair, nil
}
// AtomicDelete deletes a value at "key" if the key has not

View File

@@ -252,12 +252,12 @@ func (s *Etcd) WatchTree(prefix string, stopCh <-chan struct{}) (<-chan []*KVPai
// AtomicPut put a value at "key" if the key has not been
// modified in the meantime, throws an error if this is the case
func (s *Etcd) AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, error) {
_, err := s.client.CompareAndSwap(normalize(key), string(value), 0, "", previous.LastIndex)
func (s *Etcd) AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, *KVPair, error) {
meta, err := s.client.CompareAndSwap(normalize(key), string(value), 0, "", previous.LastIndex)
if err != nil {
return false, err
return false, nil, err
}
return true, nil
return true, &KVPair{Key: key, Value: value, LastIndex: meta.Node.ModifiedIndex}, nil
}
// AtomicDelete deletes a value at "key" if the key has not

View File

@@ -75,9 +75,9 @@ func (s *Mock) DeleteTree(prefix string) error {
}
// AtomicPut mock
func (s *Mock) AtomicPut(key string, value []byte, previous *KVPair, opts *WriteOptions) (bool, error) {
func (s *Mock) AtomicPut(key string, value []byte, previous *KVPair, opts *WriteOptions) (bool, *KVPair, error) {
args := s.Mock.Called(key, value, previous, opts)
return args.Bool(0), args.Error(1)
return args.Bool(0), args.Get(1).(*KVPair), args.Error(2)
}
// AtomicDelete mock

View File

@@ -89,7 +89,7 @@ type Store interface {
DeleteTree(prefix string) error
// Atomic operation on a single value
AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, error)
AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, *KVPair, error)
// Atomic delete of a single value
AtomicDelete(key string, previous *KVPair) (bool, error)

View File

@@ -214,9 +214,9 @@ func (s *Zookeeper) DeleteTree(prefix string) error {
// AtomicPut put a value at "key" if the key has not been
// modified in the meantime, throws an error if this is the case
func (s *Zookeeper) AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, error) {
func (s *Zookeeper) AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, *KVPair, error) {
// Use index of Set method to implement CAS
return false, ErrNotImplemented
return false, nil, ErrNotImplemented
}
// AtomicDelete deletes a value at "key" if the key has not