mirror of
https://github.com/restic/restic.git
synced 2026-10-12 13:07:14 +00:00
chunker: Allow resetting polynomial
This commit is contained in:
1 parent
25e3ac40ee
commit
c969a42e8d
2 files changed
+17
-12
No files matched your search
+15
-10
@@ -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
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
Reference in new issue
Block a user