1
0
mirror of https://github.com/restic/restic.git synced 2024-07-03 08:50:53 +02:00

chunker: Allow resetting polynomial

This commit is contained in:
Alexander Neumann 2015-04-05 22:52:01 +02:00
parent 25e3ac40ee
commit c969a42e8d
2 changed files with 17 additions and 12 deletions

View File

@ -82,22 +82,25 @@ type Chunker struct {
// with bufsize and pass all data to hash along the way. // with bufsize and pass all data to hash along the way.
func New(rd io.Reader, p Pol, bufsize int, hash hash.Hash) (*Chunker, error) { func New(rd io.Reader, p Pol, bufsize int, hash hash.Hash) (*Chunker, error) {
c := &Chunker{ c := &Chunker{
pol: p, buf: make([]byte, bufsize),
pol_shift: uint(p.Deg() - 8), h: hash,
buf: make([]byte, bufsize),
h: hash,
} }
if err := c.fill_tables(); err != nil { if err := c.Reset(rd, p); err != nil {
return nil, err return nil, err
} }
c.Reset(rd)
return c, nil return c, nil
} }
// Reset restarts a chunker so that it can be reused with a different reader as // Reset restarts a chunker so that it can be reused with a different
// the source. // polynomial and reader.
func (c *Chunker) Reset(rd io.Reader) { func (c *Chunker) Reset(rd io.Reader, p Pol) error {
c.pol = p
c.pol_shift = uint(p.Deg() - 8)
if err := c.fill_tables(); err != nil {
return err
}
c.rd = rd c.rd = rd
for i := 0; i < WindowSize; i++ { for i := 0; i < WindowSize; i++ {
@ -117,6 +120,8 @@ func (c *Chunker) Reset(rd io.Reader) {
// do not start a new chunk unless at least MinSize bytes have been read // do not start a new chunk unless at least MinSize bytes have been read
c.pre = MinSize - WindowSize c.pre = MinSize - WindowSize
return nil
} }
// Calculate out_table and mod_table for optimization. Must be called only // Calculate out_table and mod_table for optimization. Must be called only
@ -279,7 +284,7 @@ func (c *Chunker) Next() (*Chunk, error) {
// reset chunker, but keep position // reset chunker, but keep position
pos := c.pos pos := c.pos
c.Reset(c.rd) c.Reset(c.rd, c.pol)
c.pos = pos c.pos = pos
c.start = pos c.start = pos
c.pre = MinSize - WindowSize c.pre = MinSize - WindowSize

View File

@ -257,7 +257,7 @@ func TestChunkerReuse(t *testing.T) {
buf := get_random(23, 32*1024*1024) buf := get_random(23, 32*1024*1024)
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
ch.Reset(bytes.NewReader(buf)) ch.Reset(bytes.NewReader(buf), testPol)
test_with_data(t, ch, chunks1) test_with_data(t, ch, chunks1)
} }
} }
@ -300,7 +300,7 @@ func benchmarkChunker(b *testing.B, hash hash.Hash) {
chunks = 0 chunks = 0
rd.Seek(0, 0) rd.Seek(0, 0)
ch.Reset(rd) ch.Reset(rd, testPol)
for { for {
_, err := ch.Next() _, err := ch.Next()