-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbbhash_iter.go
More file actions
47 lines (41 loc) · 1.05 KB
/
bbhash_iter.go
File metadata and controls
47 lines (41 loc) · 1.05 KB
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
package bbhash
import (
"crypto/sha256"
"encoding/binary"
"io"
"iter"
"github.com/relab/bbhash/internal/fast"
)
// Find the chunks from slow memory
// Chunks returns smaller chunks of data given a reader with some data already being read and with the buffer size.
func ReadChunks(readerInfo io.Reader, bufSz int) iter.Seq[[]byte] {
return func(yield func([]byte) bool) {
buffer := make([]byte, bufSz)
for {
// Create buffer and read the input into it for a certain range of bytes
n, err := readerInfo.Read(buffer)
if err != nil {
return
}
if !yield(buffer[:n]) {
return
}
}
}
}
// Keys returns the hashes of the chunks using the provided hash function
func Keys(hashFunc func([]byte) uint64, chunks iter.Seq[[]byte]) []uint64 {
var keys []uint64
for c := range chunks {
keys = append(keys, hashFunc(c))
}
return keys
}
var SHA256HashFunc = func(buf []byte) uint64 {
h := sha256.New()
h.Write(buf)
return binary.LittleEndian.Uint64(h.Sum(nil))
}
var FastHashFunc = func(buf []byte) uint64 {
return fast.Hash64(123, buf)
}