Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 9bb8a7d

Browse files
committed
filesystem: ObjectStorage, keep packfiles open if KeepDescriptors is enabled
1 parent aa6f288 commit 9bb8a7d

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

storage/filesystem/object.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ type ObjectStorage struct {
2424
// loaded loose objects
2525
objectCache cache.Object
2626

27-
dir *dotgit.DotGit
28-
index map[plumbing.Hash]idxfile.Index
27+
dir *dotgit.DotGit
28+
index map[plumbing.Hash]idxfile.Index
29+
packfiles map[plumbing.Hash]*packfile.Packfile
2930
}
3031

3132
// NewObjectStorage creates a new ObjectStorage with the given .git directory and cache.
@@ -215,14 +216,7 @@ func (s *ObjectStorage) encodedObjectSizeFromPackfile(h plumbing.Hash) (
215216
return 0, err
216217
}
217218

218-
var p *packfile.Packfile
219-
if s.objectCache != nil {
220-
p = packfile.NewPackfileWithCache(idx, s.dir.Fs(), f, s.objectCache)
221-
} else {
222-
p = packfile.NewPackfile(idx, s.dir.Fs(), f)
223-
}
224-
225-
return p.GetSizeByOffset(offset)
219+
return s.getPackfile(f, idx, pack).GetSizeByOffset(offset)
226220
}
227221

228222
// EncodedObjectSize returns the plaintext size of the given object,
@@ -372,16 +366,36 @@ func (s *ObjectStorage) getFromPackfile(h plumbing.Hash, canBeDelta bool) (
372366

373367
idx := s.index[pack]
374368
if canBeDelta {
375-
return s.decodeDeltaObjectAt(f, idx, offset, hash)
369+
return s.decodeDeltaObjectAt(f, idx, offset, hash, pack)
376370
}
377371

378-
return s.decodeObjectAt(f, idx, offset)
372+
return s.decodeObjectAt(f, idx, offset, hash, pack)
373+
}
374+
375+
func (s *ObjectStorage) getPackfile(f billy.File, idx idxfile.Index, pack plumbing.Hash) *packfile.Packfile {
376+
var p *packfile.Packfile
377+
378+
if p, ok := s.packfiles[pack]; !ok {
379+
if s.objectCache != nil {
380+
p = packfile.NewPackfileWithCache(idx, s.dir.Fs(), f, s.objectCache)
381+
} else {
382+
p = packfile.NewPackfile(idx, s.dir.Fs(), f)
383+
}
384+
385+
if s.options.KeepDescriptors {
386+
s.packfiles[pack] = p
387+
}
388+
}
389+
390+
return p
379391
}
380392

381393
func (s *ObjectStorage) decodeObjectAt(
382394
f billy.File,
383395
idx idxfile.Index,
384396
offset int64,
397+
hash plumbing.Hash,
398+
pack plumbing.Hash,
385399
) (plumbing.EncodedObject, error) {
386400
hash, err := idx.FindHash(offset)
387401
if err == nil {
@@ -410,6 +424,7 @@ func (s *ObjectStorage) decodeDeltaObjectAt(
410424
idx idxfile.Index,
411425
offset int64,
412426
hash plumbing.Hash,
427+
pack plumbing.Hash,
413428
) (plumbing.EncodedObject, error) {
414429
if _, err := f.Seek(0, io.SeekStart); err != nil {
415430
return nil, err
@@ -434,7 +449,7 @@ func (s *ObjectStorage) decodeDeltaObjectAt(
434449
return nil, err
435450
}
436451
default:
437-
return s.decodeObjectAt(f, idx, offset)
452+
return s.decodeObjectAt(f, idx, offset, hash, pack)
438453
}
439454

440455
obj := &plumbing.MemoryObject{}
@@ -515,6 +530,11 @@ func (s *ObjectStorage) buildPackfileIters(
515530

516531
// Close closes all opened files.
517532
func (s *ObjectStorage) Close() error {
533+
for _, p := range s.packfiles {
534+
p.Close()
535+
}
536+
s.packfiles = make(map[plumbing.Hash]*packfile.Packfile)
537+
518538
return s.dir.Close()
519539
}
520540

0 commit comments

Comments
 (0)