From c64eb817d5e5cbaec10dea1342e1ec95721e886b Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Tue, 25 Jul 2017 10:08:36 +0200 Subject: packfile: create packfile.Index and reuse it There was an internal type (i.e. storage/filesystem.idx) to use as in-memory index for packfiles. This was not convenient to reuse in the packfile. This commit creates a new representation (format/packfile.Index) that can be converted to and from idxfile.Idxfile. A packfile.Index now contains the functionality that was scattered on storage/filesystem.idx and packfile.Decoder's internals. storage/filesystem now reuses packfile.Index instances and this also results in higher cache hit ratios when resolving deltas. --- plumbing/format/idxfile/decoder.go | 2 +- plumbing/format/idxfile/decoder_test.go | 11 ++++------- plumbing/format/idxfile/encoder.go | 2 +- plumbing/format/idxfile/encoder_test.go | 3 ++- plumbing/format/idxfile/idxfile.go | 6 +++++- 5 files changed, 13 insertions(+), 11 deletions(-) (limited to 'plumbing/format/idxfile') diff --git a/plumbing/format/idxfile/decoder.go b/plumbing/format/idxfile/decoder.go index fea5f0b..4243f76 100644 --- a/plumbing/format/idxfile/decoder.go +++ b/plumbing/format/idxfile/decoder.go @@ -104,7 +104,7 @@ func readObjectNames(idx *Idxfile, r io.Reader) error { return err } - idx.Entries = append(idx.Entries, Entry{Hash: ref}) + idx.Entries = append(idx.Entries, &Entry{Hash: ref}) } return nil diff --git a/plumbing/format/idxfile/decoder_test.go b/plumbing/format/idxfile/decoder_test.go index 609f4e3..991232d 100644 --- a/plumbing/format/idxfile/decoder_test.go +++ b/plumbing/format/idxfile/decoder_test.go @@ -1,4 +1,4 @@ -package idxfile +package idxfile_test import ( "bytes" @@ -6,6 +6,7 @@ import ( "testing" "github.com/src-d/go-git-fixtures" + . "gopkg.in/src-d/go-git.v4/plumbing/format/idxfile" "gopkg.in/src-d/go-git.v4/plumbing/format/packfile" "gopkg.in/src-d/go-git.v4/storage/memory" @@ -48,12 +49,8 @@ func (s *IdxfileSuite) TestDecodeCRCs(c *C) { _, err = pd.Decode() c.Assert(err, IsNil) - i := &Idxfile{Version: VersionSupported} - - offsets := pd.Offsets() - for h, crc := range pd.CRCs() { - i.Add(h, uint64(offsets[h]), crc) - } + i := pd.Index().ToIdxFile() + i.Version = VersionSupported buf := bytes.NewBuffer(nil) e := NewEncoder(buf) diff --git a/plumbing/format/idxfile/encoder.go b/plumbing/format/idxfile/encoder.go index 71e1b3f..d8f4d94 100644 --- a/plumbing/format/idxfile/encoder.go +++ b/plumbing/format/idxfile/encoder.go @@ -124,7 +124,7 @@ func (e *Encoder) encodeChecksums(idx *Idxfile) (int, error) { } // EntryList implements sort.Interface allowing sorting in increasing order. -type EntryList []Entry +type EntryList []*Entry func (p EntryList) Len() int { return len(p) } func (p EntryList) Less(i, j int) bool { return p[i].Hash.String() < p[j].Hash.String() } diff --git a/plumbing/format/idxfile/encoder_test.go b/plumbing/format/idxfile/encoder_test.go index 1fc4e9c..d566b0d 100644 --- a/plumbing/format/idxfile/encoder_test.go +++ b/plumbing/format/idxfile/encoder_test.go @@ -1,4 +1,4 @@ -package idxfile +package idxfile_test import ( "bytes" @@ -6,6 +6,7 @@ import ( "github.com/src-d/go-git-fixtures" "gopkg.in/src-d/go-git.v4/plumbing" + . "gopkg.in/src-d/go-git.v4/plumbing/format/idxfile" . "gopkg.in/check.v1" ) diff --git a/plumbing/format/idxfile/idxfile.go b/plumbing/format/idxfile/idxfile.go index 5a718f3..b9bb1c2 100644 --- a/plumbing/format/idxfile/idxfile.go +++ b/plumbing/format/idxfile/idxfile.go @@ -21,6 +21,10 @@ type Idxfile struct { IdxChecksum [20]byte } +func NewIdxfile() *Idxfile { + return &Idxfile{} +} + // Entry is the in memory representation of an object entry in the idx file. type Entry struct { Hash plumbing.Hash @@ -30,7 +34,7 @@ type Entry struct { // Add adds a new Entry with the given values to the Idxfile. func (idx *Idxfile) Add(h plumbing.Hash, offset uint64, crc32 uint32) { - idx.Entries = append(idx.Entries, Entry{ + idx.Entries = append(idx.Entries, &Entry{ Hash: h, Offset: offset, CRC32: crc32, -- cgit