diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-12 12:28:27 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-12 12:28:27 +0200 |
commit | 8948d4b48acfc39462f0c75525bc6b53c1ac59b2 (patch) | |
tree | e6b4aefec31a5fb2cf6b9bf6f24e40ae9bfa2ae5 /storage/memory/storage.go | |
parent | 89797d3e3c56a954b84dc69aaea8a37a736baf04 (diff) | |
download | go-git-8948d4b48acfc39462f0c75525bc6b53c1ac59b2.tar.gz |
storage/memory: implemented ReferenceStorage and Storage
Diffstat (limited to 'storage/memory/storage.go')
-rw-r--r-- | storage/memory/storage.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/storage/memory/storage.go b/storage/memory/storage.go index c827ce0..f38ec16 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -8,6 +8,31 @@ import ( var ErrUnsupportedObjectType = fmt.Errorf("unsupported object type") +type Storage struct { + o *ObjectStorage + r *ReferenceStorage +} + +func NewStorage() *Storage { + return &Storage{} +} + +func (s *Storage) ObjectStorage() core.ObjectStorage { + if s.o == nil { + s.o = NewObjectStorage() + } + + return s.o +} + +func (s *Storage) ReferenceStorage() core.ReferenceStorage { + if s.r == nil { + s.r = NewReferenceStorage() + } + + return s.r +} + // ObjectStorage is the implementation of core.ObjectStorage for memory.Object type ObjectStorage struct { Objects map[core.Hash]core.Object @@ -77,6 +102,7 @@ func (o *ObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error) { case core.TagObject: series = flattenObjectMap(o.Tags) } + return core.NewObjectSliceIter(series), nil } @@ -87,3 +113,37 @@ func flattenObjectMap(m map[core.Hash]core.Object) []core.Object { } return objects } + +type ReferenceStorage map[core.ReferenceName]*core.Reference + +func NewReferenceStorage() *ReferenceStorage { + s := make(ReferenceStorage, 0) + + return &s +} + +func (r ReferenceStorage) Set(ref *core.Reference) error { + if ref != nil { + r[ref.Name()] = ref + } + + return nil +} + +func (r ReferenceStorage) Get(n core.ReferenceName) (*core.Reference, error) { + ref, ok := r[n] + if !ok { + return nil, core.ErrReferenceNotFound + } + + return ref, nil +} + +func (r ReferenceStorage) Iter() (core.ReferenceIter, error) { + var refs []*core.Reference + for _, ref := range r { + refs = append(refs, ref) + } + + return core.NewReferenceSliceIter(refs), nil +} |