6 "github.com/docker/notary"
7 "github.com/docker/notary/tuf/utils"
10 // NewMemoryStore returns a MetadataStore that operates entirely in memory.
11 // Very useful for testing
12 func NewMemoryStore(initial map[string][]byte) *MemoryStore {
13 var consistent = make(map[string][]byte)
15 initial = make(map[string][]byte)
17 // add all seed meta to consistent
18 for name, data := range initial {
19 checksum := sha256.Sum256(data)
20 path := utils.ConsistentName(name, checksum[:])
21 consistent[path] = data
26 consistent: consistent,
30 // MemoryStore implements a mock RemoteStore entirely in memory.
31 // For testing purposes only.
32 type MemoryStore struct {
33 data map[string][]byte
34 consistent map[string][]byte
37 // GetSized returns up to size bytes of data references by name.
38 // If size is "NoSizeLimit", this corresponds to "infinite," but we cut off at a
39 // predefined threshold "notary.MaxDownloadSize", as we will always know the
40 // size for everything but a timestamp and sometimes a root,
41 // neither of which should be exceptionally large
42 func (m MemoryStore) GetSized(name string, size int64) ([]byte, error) {
45 if size == NoSizeLimit {
46 size = notary.MaxDownloadSize
48 if int64(len(d)) < size {
53 d, ok = m.consistent[name]
55 if int64(len(d)) < size {
60 return nil, ErrMetaNotFound{Resource: name}
63 // Get returns the data associated with name
64 func (m MemoryStore) Get(name string) ([]byte, error) {
65 if d, ok := m.data[name]; ok {
68 if d, ok := m.consistent[name]; ok {
71 return nil, ErrMetaNotFound{Resource: name}
74 // Set sets the metadata value for the given name
75 func (m *MemoryStore) Set(name string, meta []byte) error {
78 checksum := sha256.Sum256(meta)
79 path := utils.ConsistentName(name, checksum[:])
80 m.consistent[path] = meta
84 // SetMulti sets multiple pieces of metadata for multiple names
85 // in a single operation.
86 func (m *MemoryStore) SetMulti(metas map[string][]byte) error {
87 for role, blob := range metas {
93 // Remove removes the metadata for a single role - if the metadata doesn't
94 // exist, no error is returned
95 func (m *MemoryStore) Remove(name string) error {
96 if meta, ok := m.data[name]; ok {
97 checksum := sha256.Sum256(meta)
98 path := utils.ConsistentName(name, checksum[:])
100 delete(m.consistent, path)
105 // RemoveAll clears the existing memory store by setting this store as new empty one
106 func (m *MemoryStore) RemoveAll() error {
107 *m = *NewMemoryStore(nil)
111 // Location provides a human readable name for the storage location
112 func (m MemoryStore) Location() string {
116 // ListFiles returns a list of all files. The names returned should be
117 // usable with Get directly, with no modification.
118 func (m *MemoryStore) ListFiles() []string {
119 names := make([]string, 0, len(m.data))
120 for n := range m.data {
121 names = append(names, n)