aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-04-07 00:15:18 +0200
committerGitHub <noreply@github.com>2017-04-07 00:15:18 +0200
commit48dec88a3710da94df200f13793c85382da8c807 (patch)
treecfef63cf65c6624e6eb2c1616acb96a228f0a079
parentc34356d70ec011b8fe3a18bd60ce8234541358f7 (diff)
parent6ed3f8cbb0119887d000db77335c67b59cada59b (diff)
downloadgo-git-48dec88a3710da94df200f13793c85382da8c807.tar.gz
Merge pull request #333 from taralx/master
Lazily load object index.
-rw-r--r--storage/filesystem/object.go28
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