diff options
Diffstat (limited to 'storage/memory')
-rw-r--r-- | storage/memory/object.go | 72 | ||||
-rw-r--r-- | storage/memory/object_test.go | 75 | ||||
-rw-r--r-- | storage/memory/storage.go | 5 | ||||
-rw-r--r-- | storage/memory/storage_test.go | 8 |
4 files changed, 11 insertions, 149 deletions
diff --git a/storage/memory/object.go b/storage/memory/object.go deleted file mode 100644 index 8c35360..0000000 --- a/storage/memory/object.go +++ /dev/null @@ -1,72 +0,0 @@ -package memory - -import ( - "bytes" - "io/ioutil" - - "gopkg.in/src-d/go-git.v4/core" -) - -// Object on memory core.Object implementation -type Object struct { - t core.ObjectType - h core.Hash - cont []byte - sz int64 -} - -// NewObject creates a new object with the given type and content -func NewObject(typ core.ObjectType, size int64, cont []byte) *Object { - return &Object{ - t: typ, - h: core.ComputeHash(typ, cont), - cont: cont, - sz: int64(len(cont)), - } -} - -// Hash return the object Hash, the hash is calculated on-the-fly the first -// time is called, the subsequent calls the same Hash is returned even if the -// type or the content has changed. The Hash is only generated if the size of -// the content is exactly the Object.Size -func (o *Object) Hash() core.Hash { - if o.h == core.ZeroHash && int64(len(o.cont)) == o.sz { - o.h = core.ComputeHash(o.t, o.cont) - } - - return o.h -} - -// Type return the core.ObjectType -func (o *Object) Type() core.ObjectType { return o.t } - -// SetType sets the core.ObjectType -func (o *Object) SetType(t core.ObjectType) { o.t = t } - -// Size return the size of the object -func (o *Object) Size() int64 { return o.sz } - -// SetSize set the object size, the given size should be written afterwards -func (o *Object) SetSize(s int64) { o.sz = s } - -// Content returns the contents of the object -func (o *Object) Content() []byte { return o.cont } - -// Reader returns a core.ObjectReader used to read the object's content. -func (o *Object) Reader() (core.ObjectReader, error) { - return ioutil.NopCloser(bytes.NewBuffer(o.cont)), nil -} - -// Writer returns a core.ObjectWriter used to write the object's content. -func (o *Object) Writer() (core.ObjectWriter, error) { - return o, nil -} - -func (o *Object) Write(p []byte) (n int, err error) { - o.cont = append(o.cont, p...) - return len(p), nil -} - -// Close releases any resources consumed by the object when it is acting as a -// core.ObjectWriter. -func (o *Object) Close() error { return nil } diff --git a/storage/memory/object_test.go b/storage/memory/object_test.go deleted file mode 100644 index c252626..0000000 --- a/storage/memory/object_test.go +++ /dev/null @@ -1,75 +0,0 @@ -package memory - -import ( - "io/ioutil" - "testing" - - . "gopkg.in/check.v1" - "gopkg.in/src-d/go-git.v4/core" -) - -func Test(t *testing.T) { TestingT(t) } - -type ObjectSuite struct{} - -var _ = Suite(&ObjectSuite{}) - -func (s *ObjectSuite) TestHash(c *C) { - o := &Object{} - o.SetType(core.BlobObject) - o.SetSize(14) - - _, err := o.Write([]byte("Hello, World!\n")) - c.Assert(err, IsNil) - - c.Assert(o.Hash().String(), Equals, "8ab686eafeb1f44702738c8b0f24f2567c36da6d") - - o.SetType(core.CommitObject) - c.Assert(o.Hash().String(), Equals, "8ab686eafeb1f44702738c8b0f24f2567c36da6d") -} - -func (s *ObjectSuite) TestHashNotFilled(c *C) { - o := &Object{} - o.SetType(core.BlobObject) - o.SetSize(14) - - c.Assert(o.Hash(), Equals, core.ZeroHash) -} - -func (s *ObjectSuite) TestType(c *C) { - o := &Object{} - o.SetType(core.BlobObject) - c.Assert(o.Type(), Equals, core.BlobObject) -} - -func (s *ObjectSuite) TestSize(c *C) { - o := &Object{} - o.SetSize(42) - c.Assert(o.Size(), Equals, int64(42)) -} - -func (s *ObjectSuite) TestReader(c *C) { - o := &Object{cont: []byte("foo")} - - reader, err := o.Reader() - c.Assert(err, IsNil) - defer func() { c.Assert(reader.Close(), IsNil) }() - - b, err := ioutil.ReadAll(reader) - c.Assert(err, IsNil) - c.Assert(b, DeepEquals, []byte("foo")) -} - -func (s *ObjectSuite) TestWriter(c *C) { - o := &Object{} - - writer, err := o.Writer() - c.Assert(err, IsNil) - defer func() { c.Assert(writer.Close(), IsNil) }() - - n, err := writer.Write([]byte("foo")) - c.Assert(err, IsNil) - c.Assert(n, Equals, 3) - - c.Assert(o.cont, DeepEquals, []byte("foo")) -} diff --git a/storage/memory/storage.go b/storage/memory/storage.go index 62fd1b4..c827ce0 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -28,6 +28,11 @@ func NewObjectStorage() *ObjectStorage { } } +// NewObject creates a new MemoryObject +func (o *ObjectStorage) NewObject() core.Object { + return &core.MemoryObject{} +} + // Set stores an object, the object should be properly filled before set it. func (o *ObjectStorage) Set(obj core.Object) (core.Hash, error) { h := obj.Hash() diff --git a/storage/memory/storage_test.go b/storage/memory/storage_test.go index 200dbba..3b6c994 100644 --- a/storage/memory/storage_test.go +++ b/storage/memory/storage_test.go @@ -1,10 +1,14 @@ package memory import ( + "testing" + . "gopkg.in/check.v1" "gopkg.in/src-d/go-git.v4/core" ) +func Test(t *testing.T) { TestingT(t) } + type ObjectStorageSuite struct{} var _ = Suite(&ObjectStorageSuite{}) @@ -12,7 +16,7 @@ var _ = Suite(&ObjectStorageSuite{}) func (s *ObjectStorageSuite) TestSet(c *C) { os := NewObjectStorage() - o := &Object{} + o := &core.MemoryObject{} o.SetType(core.CommitObject) o.SetSize(3) @@ -30,7 +34,7 @@ func (s *ObjectStorageSuite) TestSet(c *C) { func (s *ObjectStorageSuite) TestGet(c *C) { os := NewObjectStorage() - o := &Object{} + o := &core.MemoryObject{} o.SetType(core.CommitObject) o.SetSize(3) |