This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
binarymarshaler.go
87 lines (73 loc) · 1.71 KB
/
binarymarshaler.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Package bloomfilter is face-meltingly fast, thread-safe,
// marshalable, unionable, probability- and
// optimal-size-calculating Bloom filter in go
//
// https://github.com/steakknife/bloomfilter
//
// Copyright © 2014, 2015, 2018 Barry Allard
//
// MIT license
//
package bloomfilter
import (
"bytes"
"crypto/sha512"
"encoding/binary"
)
// conforms to encoding.BinaryMarshaler
// marshalled binary layout (Little Endian):
//
// k 1 uint64
// n 1 uint64
// m 1 uint64
// keys [k]uint64
// bits [(m+63)/64]uint64
// hash sha384 (384 bits == 48 bytes)
//
// size = (3 + k + (m+63)/64) * 8 bytes
//
func (f *Filter) marshal() (buf *bytes.Buffer,
hash [sha512.Size384]byte,
err error,
) {
f.lock.RLock()
defer f.lock.RUnlock()
debug("write bf k=%d n=%d m=%d\n", f.K(), f.n, f.m)
buf = new(bytes.Buffer)
err = binary.Write(buf, binary.LittleEndian, f.K())
if err != nil {
return nil, hash, err
}
err = binary.Write(buf, binary.LittleEndian, f.n)
if err != nil {
return nil, hash, err
}
err = binary.Write(buf, binary.LittleEndian, f.m)
if err != nil {
return nil, hash, err
}
err = binary.Write(buf, binary.LittleEndian, f.keys)
if err != nil {
return nil, hash, err
}
err = binary.Write(buf, binary.LittleEndian, f.bits)
if err != nil {
return nil, hash, err
}
hash = sha512.Sum384(buf.Bytes())
err = binary.Write(buf, binary.LittleEndian, hash)
return buf, hash, err
}
// MarshalBinary converts a Filter into []bytes
func (f *Filter) MarshalBinary() (data []byte, err error) {
buf, hash, err := f.marshal()
if err != nil {
return nil, err
}
debug(
"bloomfilter.MarshalBinary: Successfully wrote %d byte(s), sha384 %v",
buf.Len(), hash,
)
data = buf.Bytes()
return data, nil
}