diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-05 07:59:20 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-05 07:59:20 +0200 |
commit | 65427f64599e9383ab926aed75fc02f2db32022d (patch) | |
tree | 5c81c00bc9dcfb54e0c6d0038ab2cbd8cddde389 /formats/packfile | |
parent | 440cfd96cf88ffc9d04bbd32ec8a3b1afb42144c (diff) | |
download | go-git-65427f64599e9383ab926aed75fc02f2db32022d.tar.gz |
core: ObjectStorage.Begin and TxObjectStorage
Diffstat (limited to 'formats/packfile')
-rw-r--r-- | formats/packfile/decoder.go | 22 | ||||
-rw-r--r-- | formats/packfile/decoder_test.go | 18 |
2 files changed, 24 insertions, 16 deletions
diff --git a/formats/packfile/decoder.go b/formats/packfile/decoder.go index 6d0cd8b..5b5763c 100644 --- a/formats/packfile/decoder.go +++ b/formats/packfile/decoder.go @@ -42,25 +42,33 @@ type Decoder struct { } // NewDecoder returns a new Decoder that reads from r. -func NewDecoder(r ReadRecaller) *Decoder { +func NewDecoder(r ReadRecaller, s core.ObjectStorage) *Decoder { return &Decoder{ p: NewParser(r), + s: s, } } // Decode reads a packfile and stores it in the value pointed to by s. -func (d *Decoder) Decode(s core.ObjectStorage) error { - d.s = s - +func (d *Decoder) Decode() error { count, err := d.p.ReadHeader() if err != nil { return err } - return d.readObjects(count) + tx := d.s.Begin() + if err := d.readObjects(tx, count); err != nil { + if err := tx.Rollback(); err != nil { + return nil + } + + return err + } + + return tx.Commit() } -func (d *Decoder) readObjects(count uint32) error { +func (d *Decoder) readObjects(tx core.TxObjectStorage, count uint32) error { // This code has 50-80 µs of overhead per object not counting zlib inflation. // Together with zlib inflation, it's 400-410 µs for small objects. // That's 1 sec for ~2450 objects, ~4.20 MB, or ~250 ms per MB, @@ -85,7 +93,7 @@ func (d *Decoder) readObjects(count uint32) error { return err } - _, err = d.s.Set(obj) + _, err = tx.Set(obj) if err == io.EOF { break } diff --git a/formats/packfile/decoder_test.go b/formats/packfile/decoder_test.go index fe25ee4..8c73b4e 100644 --- a/formats/packfile/decoder_test.go +++ b/formats/packfile/decoder_test.go @@ -28,10 +28,10 @@ func (s *ReaderSuite) TestReadPackfile(c *C) { data, _ := base64.StdEncoding.DecodeString(packFileWithEmptyObjects) f := bytes.NewReader(data) r := NewStream(f) - d := NewDecoder(r) - sto := memory.NewStorage() - err := d.Decode(sto.ObjectStorage()) + d := NewDecoder(r, sto.ObjectStorage()) + + err := d.Decode() c.Assert(err, IsNil) AssertObjects(c, sto, []string{ @@ -61,10 +61,10 @@ func (s *ReaderSuite) testReadPackfileGitFixture(c *C, file string, format Forma f, err := os.Open(file) c.Assert(err, IsNil) r := NewSeekable(f) - d := NewDecoder(r) - sto := memory.NewStorage() - err = d.Decode(sto.ObjectStorage()) + d := NewDecoder(r, sto.ObjectStorage()) + + err = d.Decode() c.Assert(err, IsNil) AssertObjects(c, sto, []string{ @@ -170,10 +170,10 @@ func readFromFile(c *C, file string, format Format) *memory.ObjectStorage { f, err := os.Open(file) c.Assert(err, IsNil) r := NewSeekable(f) - d := NewDecoder(r) - sto := memory.NewStorage() - err = d.Decode(sto.ObjectStorage()) + d := NewDecoder(r, sto.ObjectStorage()) + + err = d.Decode() c.Assert(err, IsNil) return sto.ObjectStorage().(*memory.ObjectStorage) |