aboutsummaryrefslogtreecommitdiffstats
path: root/storage
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-08-11 18:07:29 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-08-11 18:07:29 +0200
commit1f64d789038594098ea2c9cf796391f101d0bea5 (patch)
tree50fb530fc2e48560e70489dc81758f54822dcf50 /storage
parentc1e277a7ca75ff84741d75ad45e29a2ff3e633e3 (diff)
downloadgo-git-1f64d789038594098ea2c9cf796391f101d0bea5.tar.gz
core: new MemoryObject, move from memory.Object, packfile.Parser, base on new ObjectStorage interface
Diffstat (limited to 'storage')
-rw-r--r--storage/memory/object.go72
-rw-r--r--storage/memory/object_test.go75
-rw-r--r--storage/memory/storage.go5
-rw-r--r--storage/memory/storage_test.go8
-rw-r--r--storage/seekable/internal/index/index.go4
-rw-r--r--storage/seekable/storage.go7
-rw-r--r--storage/seekable/storage_test.go2
7 files changed, 20 insertions, 153 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)
diff --git a/storage/seekable/internal/index/index.go b/storage/seekable/internal/index/index.go
index 4282f3e..737aca6 100644
--- a/storage/seekable/internal/index/index.go
+++ b/storage/seekable/internal/index/index.go
@@ -51,8 +51,8 @@ func NewFromPackfile(rs io.ReadSeeker) (Index, error) {
return nil, err
}
- obj, err := p.ReadObject()
- if err != nil {
+ obj := &core.MemoryObject{}
+ if err := p.FillObject(obj); err != nil {
return nil, err
}
diff --git a/storage/seekable/storage.go b/storage/seekable/storage.go
index 9cc37ba..e056c54 100644
--- a/storage/seekable/storage.go
+++ b/storage/seekable/storage.go
@@ -90,6 +90,10 @@ func buildIndexFromIdxfile(fs fs.FS, path string) (index.Index, error) {
return index.NewFromIdx(f)
}
+func (s *ObjectStorage) NewObject() core.Object {
+ return &core.MemoryObject{}
+}
+
// Set adds a new object to the storage. As this functionality is not
// yet supported, this method always returns a "not implemented yet"
// error an zero hash.
@@ -131,7 +135,8 @@ func (s *ObjectStorage) Get(h core.Hash) (core.Object, error) {
r.HashToOffset = map[core.Hash]int64(s.index)
p := packfile.NewParser(r)
- return p.ReadObject()
+ obj := s.NewObject()
+ return obj, p.FillObject(obj)
}
// Iter returns an iterator for all the objects in the packfile with the
diff --git a/storage/seekable/storage_test.go b/storage/seekable/storage_test.go
index 2002d2b..bd12ed1 100644
--- a/storage/seekable/storage_test.go
+++ b/storage/seekable/storage_test.go
@@ -321,6 +321,6 @@ func (s *FsSuite) TestSet(c *C) {
sto, err := seekable.New(fs, gitPath)
c.Assert(err, IsNil)
- _, err = sto.Set(&memory.Object{})
+ _, err = sto.Set(&core.MemoryObject{})
c.Assert(err, ErrorMatches, "not implemented yet")
}