diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-12 13:27:14 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-12 13:27:14 +0200 |
commit | 5a9ed6e35d7a807de80cfea04e1bd31ebc718563 (patch) | |
tree | 631be8af09453ff5039396fd91f6a2460cab2ce5 /storage/memory/storage.go | |
parent | e1f7b169aaa99d07fe8ad8e71a0cba2322e7d35f (diff) | |
download | go-git-5a9ed6e35d7a807de80cfea04e1bd31ebc718563.tar.gz |
core: Storage, now returns errors
Diffstat (limited to 'storage/memory/storage.go')
-rw-r--r-- | storage/memory/storage.go | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/storage/memory/storage.go b/storage/memory/storage.go index f38ec16..9c97523 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -8,29 +8,44 @@ import ( var ErrUnsupportedObjectType = fmt.Errorf("unsupported object type") +// Storage in memory storage system type Storage struct { o *ObjectStorage r *ReferenceStorage } +// NewStorage returns a new Storage func NewStorage() *Storage { return &Storage{} } -func (s *Storage) ObjectStorage() core.ObjectStorage { - if s.o == nil { - s.o = NewObjectStorage() +// ObjectStorage returns the ObjectStorage if not exists creates a new one +func (s *Storage) ObjectStorage() (core.ObjectStorage, error) { + if s.o != nil { + return s.o, nil } - return s.o + s.o = &ObjectStorage{ + Objects: make(map[core.Hash]core.Object, 0), + Commits: make(map[core.Hash]core.Object, 0), + Trees: make(map[core.Hash]core.Object, 0), + Blobs: make(map[core.Hash]core.Object, 0), + Tags: make(map[core.Hash]core.Object, 0), + } + + return s.o, nil } -func (s *Storage) ReferenceStorage() core.ReferenceStorage { - if s.r == nil { - s.r = NewReferenceStorage() +// ReferenceStorage returns the ReferenceStorage if not exists creates a new one +func (s *Storage) ReferenceStorage() (core.ReferenceStorage, error) { + if s.r != nil { + return s.r, nil } - return s.r + r := make(ReferenceStorage, 0) + s.r = &r + + return s.r, nil } // ObjectStorage is the implementation of core.ObjectStorage for memory.Object @@ -116,12 +131,7 @@ func flattenObjectMap(m map[core.Hash]core.Object) []core.Object { type ReferenceStorage map[core.ReferenceName]*core.Reference -func NewReferenceStorage() *ReferenceStorage { - s := make(ReferenceStorage, 0) - - return &s -} - +// Set stores a reference. func (r ReferenceStorage) Set(ref *core.Reference) error { if ref != nil { r[ref.Name()] = ref @@ -130,6 +140,7 @@ func (r ReferenceStorage) Set(ref *core.Reference) error { return nil } +// Get returns a stored reference with the given name func (r ReferenceStorage) Get(n core.ReferenceName) (*core.Reference, error) { ref, ok := r[n] if !ok { @@ -139,6 +150,7 @@ func (r ReferenceStorage) Get(n core.ReferenceName) (*core.Reference, error) { return ref, nil } +// Iter returns a core.ReferenceIter func (r ReferenceStorage) Iter() (core.ReferenceIter, error) { var refs []*core.Reference for _, ref := range r { |