diff options
author | Santiago M. Mola <santi@mola.io> | 2016-08-29 22:47:13 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-29 22:47:13 +0200 |
commit | e4246138cb9ffb819c052ba17a9fbdf915427291 (patch) | |
tree | bd938368afe0ffd7c9e1df16256e39e17d8184b5 /storage/filesystem | |
parent | dd4af03ad368cc50dd08912010f5b667bd7569cd (diff) | |
download | go-git-e4246138cb9ffb819c052ba17a9fbdf915427291.tar.gz |
storage: Add object type hint parameter to ObjectStorage.Get. (#69)
Some storage backends can optimize object lookup if they get
the object type that is expected. So we the signature of the Get
method is now Get(Hash, ObjectType).
Added generic tests for storage backends.
Diffstat (limited to 'storage/filesystem')
-rw-r--r-- | storage/filesystem/object.go | 15 | ||||
-rw-r--r-- | storage/filesystem/object_test.go | 10 |
2 files changed, 17 insertions, 8 deletions
diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go index f3a1dda..6024ae0 100644 --- a/storage/filesystem/object.go +++ b/storage/filesystem/object.go @@ -38,7 +38,7 @@ func (s *ObjectStorage) Set(core.Object) (core.Hash, error) { // Get returns the object with the given hash, by searching for it in // the packfile. -func (s *ObjectStorage) Get(h core.Hash) (core.Object, error) { +func (s *ObjectStorage) Get(h core.Hash, t core.ObjectType) (core.Object, error) { offset, err := s.index.Get(h) if err != nil { return nil, err @@ -71,7 +71,14 @@ func (s *ObjectStorage) Get(h core.Hash) (core.Object, error) { p := packfile.NewParser(r) obj := s.NewObject() - return obj, p.FillObject(obj) + err = p.FillObject(obj) + if err != nil { + return nil, err + } + if core.AnyObject != t && obj.Type() != t { + return nil, core.ErrObjectNotFound + } + return obj, nil } // Iter returns an iterator for all the objects in the packfile with the @@ -80,11 +87,11 @@ func (s *ObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error) { var objects []core.Object for hash := range s.index { - object, err := s.Get(hash) + object, err := s.Get(hash, core.AnyObject) if err != nil { return nil, err } - if object.Type() == t { + if t == core.AnyObject || object.Type() == t { objects = append(objects, object) } } diff --git a/storage/filesystem/object_test.go b/storage/filesystem/object_test.go index 692a69b..956fdeb 100644 --- a/storage/filesystem/object_test.go +++ b/storage/filesystem/object_test.go @@ -64,9 +64,11 @@ func (s *FsSuite) TearDownSuite(c *C) { func (s *FsSuite) TestHashNotFound(c *C) { sto := s.newObjectStorage(c, "binary-relations") - - _, err := sto.Get(core.ZeroHash) - c.Assert(err, Equals, core.ErrObjectNotFound) + types := []core.ObjectType{core.AnyObject, core.TagObject, core.CommitObject, core.BlobObject, core.TreeObject} + for t := range types { + _, err := sto.Get(core.ZeroHash, core.ObjectType(t)) + c.Assert(err, Equals, core.ErrObjectNotFound) + } } func (s *FsSuite) newObjectStorage(c *C, fixtureName string) core.ObjectStorage { @@ -156,7 +158,7 @@ func equalsStorages(a, b core.ObjectStorage) (bool, string, error) { break } - bo, err := b.Get(ao.Hash()) + bo, err := b.Get(ao.Hash(), core.AnyObject) if err != nil { return false, "", fmt.Errorf("getting object with hash %s: %s", ao.Hash(), err) |