2015-05-08 09:26:35 -04:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2021-05-27 20:15:56 -04:00
|
|
|
"github.com/docker/libkv/store"
|
2015-05-08 09:26:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-05-21 22:25:52 -04:00
|
|
|
// ErrNotImplemented exported
|
|
|
|
ErrNotImplemented = errors.New("Functionality not implemented")
|
2015-05-08 09:26:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// MockData exported
|
|
|
|
type MockData struct {
|
|
|
|
Data []byte
|
|
|
|
Index uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// MockStore exported
|
|
|
|
type MockStore struct {
|
|
|
|
db map[string]*MockData
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMockStore creates a Map backed Datastore that is useful for mocking
|
|
|
|
func NewMockStore() *MockStore {
|
|
|
|
db := make(map[string]*MockData)
|
|
|
|
return &MockStore{db}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value at "key", returns the last modified index
|
|
|
|
// to use in conjunction to CAS calls
|
2015-05-29 23:42:23 -04:00
|
|
|
func (s *MockStore) Get(key string) (*store.KVPair, error) {
|
2015-05-08 09:26:35 -04:00
|
|
|
mData := s.db[key]
|
|
|
|
if mData == nil {
|
2015-05-29 23:42:23 -04:00
|
|
|
return nil, nil
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
return &store.KVPair{Value: mData.Data, LastIndex: mData.Index}, nil
|
2015-05-08 09:26:35 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put a value at "key"
|
2015-05-29 23:42:23 -04:00
|
|
|
func (s *MockStore) Put(key string, value []byte, options *store.WriteOptions) error {
|
2015-05-08 09:26:35 -04:00
|
|
|
mData := s.db[key]
|
|
|
|
if mData == nil {
|
|
|
|
mData = &MockData{value, 0}
|
|
|
|
}
|
|
|
|
mData.Index = mData.Index + 1
|
|
|
|
s.db[key] = mData
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a value at "key"
|
|
|
|
func (s *MockStore) Delete(key string) error {
|
|
|
|
delete(s.db, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists checks that the key exists inside the store
|
|
|
|
func (s *MockStore) Exists(key string) (bool, error) {
|
|
|
|
_, ok := s.db[key]
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
2015-05-29 23:42:23 -04:00
|
|
|
// List gets a range of values at "directory"
|
|
|
|
func (s *MockStore) List(prefix string) ([]*store.KVPair, error) {
|
2017-05-21 22:25:52 -04:00
|
|
|
return nil, ErrNotImplemented
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 23:42:23 -04:00
|
|
|
// DeleteTree deletes a range of values at "directory"
|
|
|
|
func (s *MockStore) DeleteTree(prefix string) error {
|
2015-06-05 16:31:12 -04:00
|
|
|
delete(s.db, prefix)
|
|
|
|
return nil
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Watch a single key for modifications
|
2015-05-29 23:42:23 -04:00
|
|
|
func (s *MockStore) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) {
|
2017-05-21 22:25:52 -04:00
|
|
|
return nil, ErrNotImplemented
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 23:42:23 -04:00
|
|
|
// WatchTree triggers a watch on a range of values at "directory"
|
|
|
|
func (s *MockStore) WatchTree(prefix string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) {
|
2017-05-21 22:25:52 -04:00
|
|
|
return nil, ErrNotImplemented
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 23:42:23 -04:00
|
|
|
// NewLock exposed
|
|
|
|
func (s *MockStore) NewLock(key string, options *store.LockOptions) (store.Locker, error) {
|
2017-05-21 22:25:52 -04:00
|
|
|
return nil, ErrNotImplemented
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AtomicPut put a value at "key" if the key has not been
|
|
|
|
// modified in the meantime, throws an error if this is the case
|
2015-05-29 23:42:23 -04:00
|
|
|
func (s *MockStore) AtomicPut(key string, newValue []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) {
|
2015-05-08 09:26:35 -04:00
|
|
|
mData := s.db[key]
|
2015-06-18 18:13:38 -04:00
|
|
|
|
|
|
|
if previous == nil {
|
|
|
|
if mData != nil {
|
|
|
|
return false, nil, types.BadRequestErrorf("atomic put failed because key exists")
|
|
|
|
} // Else OK.
|
|
|
|
} else {
|
|
|
|
if mData == nil {
|
|
|
|
return false, nil, types.BadRequestErrorf("atomic put failed because key exists")
|
|
|
|
}
|
|
|
|
if mData != nil && mData.Index != previous.LastIndex {
|
|
|
|
return false, nil, types.BadRequestErrorf("atomic put failed due to mismatched Index")
|
|
|
|
} // Else OK.
|
2015-05-29 23:42:23 -04:00
|
|
|
}
|
|
|
|
err := s.Put(key, newValue, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
return true, &store.KVPair{Key: key, Value: newValue, LastIndex: s.db[key].Index}, nil
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AtomicDelete deletes a value at "key" if the key has not
|
|
|
|
// been modified in the meantime, throws an error if this is the case
|
2015-05-29 23:42:23 -04:00
|
|
|
func (s *MockStore) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
|
2015-05-08 09:26:35 -04:00
|
|
|
mData := s.db[key]
|
2015-05-29 23:42:23 -04:00
|
|
|
if mData != nil && mData.Index != previous.LastIndex {
|
2015-06-05 16:31:12 -04:00
|
|
|
return false, types.BadRequestErrorf("atomic delete failed due to mismatched Index")
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
return true, s.Delete(key)
|
|
|
|
}
|
2015-05-26 06:53:42 -04:00
|
|
|
|
|
|
|
// Close closes the client connection
|
|
|
|
func (s *MockStore) Close() {
|
|
|
|
}
|