1
0
Fork 0
forked from mirrors/tar-split

Merge pull request #12 from LK4D4/multi_for_tee

tar/storage: Replace TeeReader with MultiWriter
This commit is contained in:
Vincent Batts 2015-08-13 15:12:22 -04:00
commit 9b9df04f1f
2 changed files with 28 additions and 6 deletions

View file

@ -59,15 +59,15 @@ func (bfgp bufferFileGetPutter) Get(name string) (io.ReadCloser, error) {
} }
func (bfgp *bufferFileGetPutter) Put(name string, r io.Reader) (int64, []byte, error) { func (bfgp *bufferFileGetPutter) Put(name string, r io.Reader) (int64, []byte, error) {
c := crc64.New(CRCTable) crc := crc64.New(CRCTable)
tRdr := io.TeeReader(r, c) buf := bytes.NewBuffer(nil)
b := bytes.NewBuffer([]byte{}) cw := io.MultiWriter(crc, buf)
i, err := io.Copy(b, tRdr) i, err := io.Copy(cw, r)
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
} }
bfgp.files[name] = b.Bytes() bfgp.files[name] = buf.Bytes()
return i, c.Sum(nil), nil return i, crc.Sum(nil), nil
} }
type readCloserWrapper struct { type readCloserWrapper struct {

View file

@ -2,7 +2,9 @@ package storage
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"strings"
"testing" "testing"
) )
@ -39,6 +41,7 @@ func TestGetter(t *testing.T) {
} }
} }
} }
func TestPutter(t *testing.T) { func TestPutter(t *testing.T) {
fp := NewDiscardFilePutter() fp := NewDiscardFilePutter()
// map[filename]map[body]crc64sum // map[filename]map[body]crc64sum
@ -60,3 +63,22 @@ func TestPutter(t *testing.T) {
} }
} }
} }
func BenchmarkPutter(b *testing.B) {
files := []string{
strings.Repeat("foo", 1000),
strings.Repeat("bar", 1000),
strings.Repeat("baz", 1000),
strings.Repeat("fooz", 1000),
strings.Repeat("vbatts", 1000),
strings.Repeat("systemd", 1000),
}
for i := 0; i < b.N; i++ {
fgp := NewBufferFileGetPutter()
for n, body := range files {
if _, _, err := fgp.Put(fmt.Sprintf("%d", n), bytes.NewBufferString(body)); err != nil {
b.Fatal(err)
}
}
}
}