-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastcdc.go
60 lines (51 loc) · 1.14 KB
/
fastcdc.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package fastcdc
//go:generate go run ./cmd/gear
import (
_ "embed"
"encoding/binary"
)
var (
//go:embed gear
gear []byte
// gearN is the normal gear table
gearN [256]uint64
// gearL is the left shifted gear table
gearL [256]uint64
)
func init() {
// pre-compute gear tables
for i := 0; i < 256; i++ {
gearN[i] = binary.LittleEndian.Uint64(gear[i*8:])
gearL[i] = gearN[i] << 1
}
}
// Boundary returns the next chunk boundary for the given bytes.
func Boundary(src []byte, options *Options) int {
size := min(len(src), options.maxSize)
if size <= options.minSize {
return size
}
hash := uint64(0)
norm := min(options.avgSize, size)
for i := options.minSize; i < (norm / 2); i++ {
hash = (hash << 2) + gearL[src[i*2]]
if (hash & (options.maskS << 1)) == 0 {
return i * 2
}
hash = hash + gearN[src[i*2+1]]
if (hash & options.maskS) == 0 {
return (i * 2) + 1
}
}
for i := (norm / 2); i < (size / 2); i++ {
hash = (hash << 2) + gearL[src[i*2]]
if (hash & (options.maskL << 1)) == 0 {
return i * 2
}
hash = hash + gearN[src[i*2+1]]
if (hash & options.maskL) == 0 {
return (i * 2) + 1
}
}
return size
}