-
Notifications
You must be signed in to change notification settings - Fork 1
/
rabin.go
45 lines (34 loc) · 790 Bytes
/
rabin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package chunk
import (
"hash/fnv"
"io"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/chunker"
)
var IpfsRabinPoly = chunker.Pol(17437180132763653)
type Rabin struct {
r *chunker.Chunker
reader io.Reader
}
func NewRabin(r io.Reader, avgBlkSize uint64) *Rabin {
min := avgBlkSize / 3
max := avgBlkSize + (avgBlkSize / 2)
return NewRabinMinMax(r, min, avgBlkSize, max)
}
func NewRabinMinMax(r io.Reader, min, avg, max uint64) *Rabin {
h := fnv.New32a()
ch := chunker.New(r, IpfsRabinPoly, h, avg, min, max)
return &Rabin{
r: ch,
reader: r,
}
}
func (r *Rabin) NextBytes() ([]byte, error) {
ch, err := r.r.Next()
if err != nil {
return nil, err
}
return ch.Data, nil
}
func (r *Rabin) Reader() io.Reader {
return r.reader
}