diff options
-rw-r--r-- | storage/memory/storage.go | 40 | ||||
-rw-r--r-- | storage/memory/storage_test.go | 33 |
2 files changed, 49 insertions, 24 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 { diff --git a/storage/memory/storage_test.go b/storage/memory/storage_test.go index 275897c..8e9a524 100644 --- a/storage/memory/storage_test.go +++ b/storage/memory/storage_test.go @@ -16,22 +16,28 @@ var _ = Suite(&StorageSuite{}) func (s *StorageSuite) TestStorageObjectStorage(c *C) { storage := NewStorage() - o := storage.ObjectStorage() - e := storage.ObjectStorage() + o, err := storage.ObjectStorage() + c.Assert(err, IsNil) + e, err := storage.ObjectStorage() + c.Assert(err, IsNil) c.Assert(o == e, Equals, true) } func (s *StorageSuite) TestStorageReferenceStorage(c *C) { storage := NewStorage() - o := storage.ReferenceStorage() - e := storage.ReferenceStorage() + o, err := storage.ReferenceStorage() + c.Assert(err, IsNil) + e, err := storage.ReferenceStorage() + c.Assert(err, IsNil) c.Assert(o == e, Equals, true) } func (s *StorageSuite) TestObjectStorageSetAndGet(c *C) { - os := NewObjectStorage() + storage := NewStorage() + os, err := storage.ObjectStorage() + c.Assert(err, IsNil) commit := &core.MemoryObject{} commit.SetType(core.CommitObject) @@ -84,7 +90,10 @@ func (s *StorageSuite) TestObjectStorageIter(c *C) { tag := &core.MemoryObject{} tag.SetType(core.TagObject) - os := NewObjectStorage() + storage := NewStorage() + os, err := storage.ObjectStorage() + c.Assert(err, IsNil) + os.Set(commit) os.Set(tree) os.Set(blob) @@ -124,9 +133,11 @@ func (s *StorageSuite) TestObjectStorageIter(c *C) { } func (s *StorageSuite) TestReferenceStorageSetAndGet(c *C) { - rs := NewReferenceStorage() + storage := NewStorage() + rs, err := storage.ReferenceStorage() + c.Assert(err, IsNil) - err := rs.Set(core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")) + err = rs.Set(core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")) c.Assert(err, IsNil) err = rs.Set(core.NewReferenceFromStrings("bar", "482e0eada5de4039e6f216b45b3c9b683b83bfa")) @@ -138,9 +149,11 @@ func (s *StorageSuite) TestReferenceStorageSetAndGet(c *C) { } func (s *StorageSuite) TestReferenceStorageIter(c *C) { - rs := NewReferenceStorage() + storage := NewStorage() + rs, err := storage.ReferenceStorage() + c.Assert(err, IsNil) - err := rs.Set(core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")) + err = rs.Set(core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")) c.Assert(err, IsNil) i, err := rs.Iter() |