aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-08-12 12:28:27 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-08-12 12:28:27 +0200
commit8948d4b48acfc39462f0c75525bc6b53c1ac59b2 (patch)
treee6b4aefec31a5fb2cf6b9bf6f24e40ae9bfa2ae5
parent89797d3e3c56a954b84dc69aaea8a37a736baf04 (diff)
downloadgo-git-8948d4b48acfc39462f0c75525bc6b53c1ac59b2.tar.gz
storage/memory: implemented ReferenceStorage and Storage
-rw-r--r--storage/memory/storage.go60
-rw-r--r--storage/memory/storage_test.go144
2 files changed, 183 insertions, 21 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
+}
diff --git a/storage/memory/storage_test.go b/storage/memory/storage_test.go
index 3b6c994..275897c 100644
--- a/storage/memory/storage_test.go
+++ b/storage/memory/storage_test.go
@@ -1,6 +1,7 @@
package memory
import (
+ "io"
"testing"
. "gopkg.in/check.v1"
@@ -9,46 +10,147 @@ import (
func Test(t *testing.T) { TestingT(t) }
-type ObjectStorageSuite struct{}
+type StorageSuite struct{}
-var _ = Suite(&ObjectStorageSuite{})
+var _ = Suite(&StorageSuite{})
-func (s *ObjectStorageSuite) TestSet(c *C) {
+func (s *StorageSuite) TestStorageObjectStorage(c *C) {
+ storage := NewStorage()
+ o := storage.ObjectStorage()
+ e := storage.ObjectStorage()
+
+ c.Assert(o == e, Equals, true)
+}
+
+func (s *StorageSuite) TestStorageReferenceStorage(c *C) {
+ storage := NewStorage()
+ o := storage.ReferenceStorage()
+ e := storage.ReferenceStorage()
+
+ c.Assert(o == e, Equals, true)
+}
+
+func (s *StorageSuite) TestObjectStorageSetAndGet(c *C) {
os := NewObjectStorage()
- o := &core.MemoryObject{}
- o.SetType(core.CommitObject)
- o.SetSize(3)
+ commit := &core.MemoryObject{}
+ commit.SetType(core.CommitObject)
- writer, err := o.Writer()
+ h, err := os.Set(commit)
c.Assert(err, IsNil)
- defer func() { c.Assert(writer.Close(), IsNil) }()
+ c.Assert(h.String(), Equals, "dcf5b16e76cce7425d0beaef62d79a7d10fce1f5")
+
+ e, err := os.Get(h)
+ c.Assert(commit == e, Equals, true)
+
+ tree := &core.MemoryObject{}
+ tree.SetType(core.TreeObject)
+
+ h, err = os.Set(tree)
+ c.Assert(err, IsNil)
+ c.Assert(h.String(), Equals, "4b825dc642cb6eb9a060e54bf8d69288fbee4904")
+
+ e, err = os.Get(h)
+ c.Assert(tree == e, Equals, true)
- writer.Write([]byte("foo"))
+ blob := &core.MemoryObject{}
+ blob.SetType(core.BlobObject)
- h, err := os.Set(o)
+ h, err = os.Set(blob)
c.Assert(err, IsNil)
- c.Assert(h.String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")
+ c.Assert(h.String(), Equals, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391")
+
+ e, err = os.Get(h)
+ c.Assert(blob == e, Equals, true)
+
+ tag := &core.MemoryObject{}
+ tag.SetType(core.TagObject)
+
+ h, err = os.Set(tag)
+ c.Assert(err, IsNil)
+ c.Assert(h.String(), Equals, "d994c6bb648123a17e8f70a966857c546b2a6f94")
+
+ e, err = os.Get(h)
+ c.Assert(tag == e, Equals, true)
}
-func (s *ObjectStorageSuite) TestGet(c *C) {
+func (s *StorageSuite) TestObjectStorageIter(c *C) {
+ commit := &core.MemoryObject{}
+ commit.SetType(core.CommitObject)
+ tree := &core.MemoryObject{}
+ tree.SetType(core.TreeObject)
+ blob := &core.MemoryObject{}
+ blob.SetType(core.BlobObject)
+ tag := &core.MemoryObject{}
+ tag.SetType(core.TagObject)
+
os := NewObjectStorage()
+ os.Set(commit)
+ os.Set(tree)
+ os.Set(blob)
+ os.Set(tag)
+
+ i, err := os.Iter(core.CommitObject)
+ c.Assert(err, IsNil)
+
+ e, err := i.Next()
+ c.Assert(err, IsNil)
+ c.Assert(commit == e, Equals, true)
- o := &core.MemoryObject{}
- o.SetType(core.CommitObject)
- o.SetSize(3)
+ i, err = os.Iter(core.TreeObject)
+ c.Assert(err, IsNil)
+
+ e, err = i.Next()
+ c.Assert(err, IsNil)
+ c.Assert(tree == e, Equals, true)
- writer, err := o.Writer()
+ i, err = os.Iter(core.BlobObject)
c.Assert(err, IsNil)
- defer func() { c.Assert(writer.Close(), IsNil) }()
- writer.Write([]byte("foo"))
+ e, err = i.Next()
+ c.Assert(err, IsNil)
+ c.Assert(blob == e, Equals, true)
+
+ i, err = os.Iter(core.TagObject)
+ c.Assert(err, IsNil)
+
+ e, err = i.Next()
+ c.Assert(err, IsNil)
+ c.Assert(tag == e, Equals, true)
+
+ e, err = i.Next()
+ c.Assert(e, IsNil)
+ c.Assert(err, Equals, io.EOF)
+}
+
+func (s *StorageSuite) TestReferenceStorageSetAndGet(c *C) {
+ rs := NewReferenceStorage()
+
+ err := rs.Set(core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"))
+ c.Assert(err, IsNil)
+
+ err = rs.Set(core.NewReferenceFromStrings("bar", "482e0eada5de4039e6f216b45b3c9b683b83bfa"))
+ c.Assert(err, IsNil)
+
+ e, err := rs.Get(core.ReferenceName("foo"))
+ c.Assert(err, IsNil)
+ c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")
+}
+
+func (s *StorageSuite) TestReferenceStorageIter(c *C) {
+ rs := NewReferenceStorage()
+
+ err := rs.Set(core.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"))
+ c.Assert(err, IsNil)
- h, err := os.Set(o)
+ i, err := rs.Iter()
c.Assert(err, IsNil)
- ro, err := os.Get(h)
+ e, err := i.Next()
c.Assert(err, IsNil)
+ c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")
- c.Assert(ro, DeepEquals, o)
+ e, err = i.Next()
+ c.Assert(e, IsNil)
+ c.Assert(err, Equals, io.EOF)
}