forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
46 lines (38 loc) · 1.04 KB
/
hash.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
package pfs
import (
"hash/adler32"
"path"
"github.com/pachyderm/pachyderm/src/client/pfs"
)
type Hasher struct {
FileModulus uint64
BlockModulus uint64
}
func NewHasher(fileModulus uint64, blockModulus uint64) *Hasher {
return &Hasher{
FileModulus: fileModulus,
BlockModulus: blockModulus,
}
}
func (s *Hasher) HashFile(file *pfs.File) uint64 {
return uint64(adler32.Checksum([]byte(path.Clean(file.Path)))) % s.FileModulus
}
func (s *Hasher) HashBlock(block *pfs.Block) uint64 {
return uint64(adler32.Checksum([]byte(block.Hash))) % s.BlockModulus
}
func FileInShard(shard *pfs.Shard, file *pfs.File) bool {
if shard == nil {
// this lets us default to no filtering
return true
}
sharder := &Hasher{FileModulus: shard.FileModulus}
return sharder.HashFile(file) == shard.FileNumber
}
func BlockInShard(shard *pfs.Shard, block *pfs.Block) bool {
if shard == nil {
// this lets us default to no filtering
return true
}
sharder := &Hasher{BlockModulus: shard.BlockModulus}
return sharder.HashBlock(block) == shard.BlockNumber
}