diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-04-07 00:15:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-07 00:15:18 +0200 |
commit | 48dec88a3710da94df200f13793c85382da8c807 (patch) | |
tree | cfef63cf65c6624e6eb2c1616acb96a228f0a079 /storage/filesystem | |
parent | c34356d70ec011b8fe3a18bd60ce8234541358f7 (diff) | |
parent | 6ed3f8cbb0119887d000db77335c67b59cada59b (diff) | |
download | go-git-48dec88a3710da94df200f13793c85382da8c807.tar.gz |
Merge pull request #333 from taralx/master
Lazily load object index.
Diffstat (limited to 'storage/filesystem')
-rw-r--r-- | storage/filesystem/object.go | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go index 1518256..782b99b 100644 --- a/storage/filesystem/object.go +++ b/storage/filesystem/object.go @@ -22,14 +22,18 @@ type ObjectStorage struct { func newObjectStorage(dir *dotgit.DotGit) (ObjectStorage, error) { s := ObjectStorage{ - dir: dir, - index: make(map[plumbing.Hash]idx, 0), + dir: dir, } - return s, s.loadIdxFiles() + return s, nil } -func (s *ObjectStorage) loadIdxFiles() error { +func (s *ObjectStorage) requireIndex() error { + if s.index != nil { + return nil + } + + s.index = make(map[plumbing.Hash]idx, 0) packs, err := s.dir.ObjectPacks() if err != nil { return err @@ -49,6 +53,7 @@ func (s *ObjectStorage) loadIdxFile(h plumbing.Hash) error { if err != nil { return err } + defer idxfile.Close() s.index[h] = make(idx) return s.index[h].Decode(idxfile) @@ -59,6 +64,10 @@ func (s *ObjectStorage) NewEncodedObject() plumbing.EncodedObject { } func (s *ObjectStorage) PackfileWriter() (io.WriteCloser, error) { + if err := s.requireIndex(); err != nil { + return nil, err + } + w, err := s.dir.NewObjectPack() if err != nil { return nil, err @@ -163,6 +172,10 @@ func (s *ObjectStorage) getFromUnpacked(h plumbing.Hash) (obj plumbing.EncodedOb // Get returns the object with the given hash, by searching for it in // the packfile. func (s *ObjectStorage) getFromPackfile(h plumbing.Hash) (plumbing.EncodedObject, error) { + if err := s.requireIndex(); err != nil { + return nil, err + } + pack, offset := s.findObjectInPackfile(h) if offset == -1 { return nil, plumbing.ErrObjectNotFound @@ -219,8 +232,11 @@ func (s *ObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (storer.Encode return storer.NewMultiEncodedObjectIter(iters), nil } -func (s *ObjectStorage) buildPackfileIters( - t plumbing.ObjectType, seen map[plumbing.Hash]bool) ([]storer.EncodedObjectIter, error) { +func (s *ObjectStorage) buildPackfileIters(t plumbing.ObjectType, seen map[plumbing.Hash]bool) ([]storer.EncodedObjectIter, error) { + if err := s.requireIndex(); err != nil { + return nil, err + } + packs, err := s.dir.ObjectPacks() if err != nil { return nil, err |