Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / notary / storage / memorystore.go
1 package storage
2
3 import (
4         "crypto/sha256"
5
6         "github.com/docker/notary"
7         "github.com/docker/notary/tuf/utils"
8 )
9
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)
14         if initial == nil {
15                 initial = make(map[string][]byte)
16         } else {
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
22                 }
23         }
24         return &MemoryStore{
25                 data:       initial,
26                 consistent: consistent,
27         }
28 }
29
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
35 }
36
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) {
43         d, ok := m.data[name]
44         if ok {
45                 if size == NoSizeLimit {
46                         size = notary.MaxDownloadSize
47                 }
48                 if int64(len(d)) < size {
49                         return d, nil
50                 }
51                 return d[:size], nil
52         }
53         d, ok = m.consistent[name]
54         if ok {
55                 if int64(len(d)) < size {
56                         return d, nil
57                 }
58                 return d[:size], nil
59         }
60         return nil, ErrMetaNotFound{Resource: name}
61 }
62
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 {
66                 return d, nil
67         }
68         if d, ok := m.consistent[name]; ok {
69                 return d, nil
70         }
71         return nil, ErrMetaNotFound{Resource: name}
72 }
73
74 // Set sets the metadata value for the given name
75 func (m *MemoryStore) Set(name string, meta []byte) error {
76         m.data[name] = meta
77
78         checksum := sha256.Sum256(meta)
79         path := utils.ConsistentName(name, checksum[:])
80         m.consistent[path] = meta
81         return nil
82 }
83
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 {
88                 m.Set(role, blob)
89         }
90         return nil
91 }
92
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[:])
99                 delete(m.data, name)
100                 delete(m.consistent, path)
101         }
102         return nil
103 }
104
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)
108         return nil
109 }
110
111 // Location provides a human readable name for the storage location
112 func (m MemoryStore) Location() string {
113         return "memory"
114 }
115
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)
122         }
123         return names
124 }