Debian Codegraph @

storage/memory/storage.go

raw
1 // Package memory is a storage backend base on memory
2 package memory
3
4 import (
5 "fmt"
6 "time"
7
8 "github.com/go-git/go-git/v5/config"
9 "github.com/go-git/go-git/v5/plumbing"
10 "github.com/go-git/go-git/v5/plumbing/format/index"
11 "github.com/go-git/go-git/v5/plumbing/storer"
12 "github.com/go-git/go-git/v5/storage"
13 )
14
15 var ErrUnsupportedObjectType = fmt.Errorf("unsupported object type")
16
17 // Storage is an implementation of git.Storer that stores data on memory, being
18 // ephemeral. The use of this storage should be done in controlled environments,
19 // since the representation in memory of some repository can fill the machine
20 // memory. in the other hand this storage has the best performance.
21 type Storage struct {
22 ConfigStorage
23 ObjectStorage
24 ShallowStorage
25 IndexStorage
26 ReferenceStorage
27 ModuleStorage
28 }
29
30 // NewStorage returns a new Storage base on memory
31 func NewStorage() *Storage {
32 return &Storage{
33 ReferenceStorage: make(ReferenceStorage),
34 ConfigStorage: ConfigStorage{},
35 ShallowStorage: ShallowStorage{},
36 ObjectStorage: ObjectStorage{
37 Objects: make(map[plumbing.Hash]plumbing.EncodedObject),
38 Commits: make(map[plumbing.Hash]plumbing.EncodedObject),
39 Trees: make(map[plumbing.Hash]plumbing.EncodedObject),
40 Blobs: make(map[plumbing.Hash]plumbing.EncodedObject),
41 Tags: make(map[plumbing.Hash]plumbing.EncodedObject),
42 },
43 ModuleStorage: make(ModuleStorage),
44 }
45 }
46
47 type ConfigStorage struct {
48 config *config.Config
49 }
50
51 func (c *ConfigStorage) SetConfig(cfg *config.Config) error {
52 if err := cfg.Validate(); err != nil {
53 return err
54 }
55
56 c.config = cfg
57 return nil
58 }
59
60 func (c *ConfigStorage) Config() (*config.Config, error) {
61 if c.config == nil {
62 c.config = config.NewConfig()
63 }
64
65 return c.config, nil
66 }
67
68 type IndexStorage struct {
69 index *index.Index
70 }
71
72 // SetIndex stores the given index.
73 // Note: this method sets idx.ModTime to simulate filesystem storage behavior.
74 func (c *IndexStorage) SetIndex(idx *index.Index) error {
75 // Set ModTime to enable racy git detection in the metadata optimization.
76 idx.ModTime = time.Now()
77 c.index = idx
78 return nil
79 }
80
81 func (c *IndexStorage) Index() (*index.Index, error) {
82 if c.index == nil {
83 c.index = &index.Index{Version: 2}
84 }
85
86 return c.index, nil
87 }
88
89 type ObjectStorage struct {
90 Objects map[plumbing.Hash]plumbing.EncodedObject
91 Commits map[plumbing.Hash]plumbing.EncodedObject
92 Trees map[plumbing.Hash]plumbing.EncodedObject
93 Blobs map[plumbing.Hash]plumbing.EncodedObject
94 Tags map[plumbing.Hash]plumbing.EncodedObject
95 }
96
97 func (o *ObjectStorage) NewEncodedObject() plumbing.EncodedObject {
98 return &plumbing.MemoryObject{}
99 }
100
101 func (o *ObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbing.Hash, error) {
102 h := obj.Hash()
103 o.Objects[h] = obj
104
105 switch obj.Type() {
106 case plumbing.CommitObject:
107 o.Commits[h] = o.Objects[h]
108 case plumbing.TreeObject:
109 o.Trees[h] = o.Objects[h]
110 case plumbing.BlobObject:
111 o.Blobs[h] = o.Objects[h]
112 case plumbing.TagObject:
113 o.Tags[h] = o.Objects[h]
114 default:
115 return h, ErrUnsupportedObjectType
116 }
117
118 return h, nil
119 }
120
121 func (o *ObjectStorage) HasEncodedObject(h plumbing.Hash) (err error) {
122 if _, ok := o.Objects[h]; !ok {
123 return plumbing.ErrObjectNotFound
124 }
125 return nil
126 }
127
128 func (o *ObjectStorage) EncodedObjectSize(h plumbing.Hash) (
129 size int64, err error) {
130 obj, ok := o.Objects[h]
131 if !ok {
132 return 0, plumbing.ErrObjectNotFound
133 }
134
135 return obj.Size(), nil
136 }
137
138 func (o *ObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) {
139 obj, ok := o.Objects[h]
140 if !ok || (plumbing.AnyObject != t && obj.Type() != t) {
141 return nil, plumbing.ErrObjectNotFound
142 }
143
144 return obj, nil
145 }
146
147 func (o *ObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (storer.EncodedObjectIter, error) {
148 var series []plumbing.EncodedObject
149 switch t {
150 case plumbing.AnyObject:
151 series = flattenObjectMap(o.Objects)
152 case plumbing.CommitObject:
153 series = flattenObjectMap(o.Commits)
154 case plumbing.TreeObject:
155 series = flattenObjectMap(o.Trees)
156 case plumbing.BlobObject:
157 series = flattenObjectMap(o.Blobs)
158 case plumbing.TagObject:
159 series = flattenObjectMap(o.Tags)
160 }
161
162 return storer.NewEncodedObjectSliceIter(series), nil
163 }
164
165 func flattenObjectMap(m map[plumbing.Hash]plumbing.EncodedObject) []plumbing.EncodedObject {
166 objects := make([]plumbing.EncodedObject, 0, len(m))
167 for _, obj := range m {
168 objects = append(objects, obj)
169 }
170 return objects
171 }
172
173 func (o *ObjectStorage) Begin() storer.Transaction {
174 return &TxObjectStorage{
175 Storage: o,
176 Objects: make(map[plumbing.Hash]plumbing.EncodedObject),
177 }
178 }
179
180 func (o *ObjectStorage) ForEachObjectHash(fun func(plumbing.Hash) error) error {
181 for h := range o.Objects {
182 err := fun(h)
183 if err != nil {
184 if err == storer.ErrStop {
185 return nil
186 }
187 return err
188 }
189 }
190 return nil
191 }
192
193 func (o *ObjectStorage) ObjectPacks() ([]plumbing.Hash, error) {
194 return nil, nil
195 }
196 func (o *ObjectStorage) DeleteOldObjectPackAndIndex(plumbing.Hash, time.Time) error {
197 return nil
198 }
199
200 var errNotSupported = fmt.Errorf("not supported")
201
202 func (o *ObjectStorage) LooseObjectTime(hash plumbing.Hash) (time.Time, error) {
203 return time.Time{}, errNotSupported
204 }
205 func (o *ObjectStorage) DeleteLooseObject(plumbing.Hash) error {
206 return errNotSupported
207 }
208
209 func (o *ObjectStorage) AddAlternate(remote string) error {
210 return errNotSupported
211 }
212
213 type TxObjectStorage struct {
214 Storage *ObjectStorage
215 Objects map[plumbing.Hash]plumbing.EncodedObject
216 }
217
218 func (tx *TxObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbing.Hash, error) {
219 h := obj.Hash()
220 tx.Objects[h] = obj
221
222 return h, nil
223 }
224
225 func (tx *TxObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) {
226 obj, ok := tx.Objects[h]
227 if !ok || (plumbing.AnyObject != t && obj.Type() != t) {
228 return nil, plumbing.ErrObjectNotFound
229 }
230
231 return obj, nil
232 }
233
234 func (tx *TxObjectStorage) Commit() error {
235 for h, obj := range tx.Objects {
236 delete(tx.Objects, h)
237 if _, err := tx.Storage.SetEncodedObject(obj); err != nil {
238 return err
239 }
240 }
241
242 return nil
243 }
244
245 func (tx *TxObjectStorage) Rollback() error {
246 tx.Objects = make(map[plumbing.Hash]plumbing.EncodedObject)
247 return nil
248 }
249
250 type ReferenceStorage map[plumbing.ReferenceName]*plumbing.Reference
251
252 func (r ReferenceStorage) SetReference(ref *plumbing.Reference) error {
253 if ref != nil {
254 r[ref.Name()] = ref
255 }
256
257 return nil
258 }
259
260 func (r ReferenceStorage) CheckAndSetReference(ref, old *plumbing.Reference) error {
261 if ref == nil {
262 return nil
263 }
264
265 if old != nil {
266 tmp := r[ref.Name()]
267 if tmp != nil && tmp.Hash() != old.Hash() {
268 return storage.ErrReferenceHasChanged
269 }
270 }
271 r[ref.Name()] = ref
272 return nil
273 }
274
275 func (r ReferenceStorage) Reference(n plumbing.ReferenceName) (*plumbing.Reference, error) {
276 ref, ok := r[n]
277 if !ok {
278 return nil, plumbing.ErrReferenceNotFound
279 }
280
281 return ref, nil
282 }
283
284 func (r ReferenceStorage) IterReferences() (storer.ReferenceIter, error) {
285 var refs []*plumbing.Reference
286 for _, ref := range r {
287 refs = append(refs, ref)
288 }
289
290 return storer.NewReferenceSliceIter(refs), nil
291 }
292
293 func (r ReferenceStorage) CountLooseRefs() (int, error) {
294 return len(r), nil
295 }
296
297 func (r ReferenceStorage) PackRefs() error {
298 return nil
299 }
300
301 func (r ReferenceStorage) RemoveReference(n plumbing.ReferenceName) error {
302 delete(r, n)
303 return nil
304 }
305
306 type ShallowStorage []plumbing.Hash
307
308 func (s *ShallowStorage) SetShallow(commits []plumbing.Hash) error {
309 *s = commits
310 return nil
311 }
312
313 func (s ShallowStorage) Shallow() ([]plumbing.Hash, error) {
314 return s, nil
315 }
316
317 type ModuleStorage map[string]*Storage
318
319 func (s ModuleStorage) Module(name string) (storage.Storer, error) {
320 if m, ok := s[name]; ok {
321 return m, nil
322 }
323
324 m := NewStorage()
325 s[name] = m
326
327 return m, nil
328 }