Skip to content

Commit 10c3561

Browse files
committed
update signature serialization interface
1 parent aa31e16 commit 10c3561

File tree

2 files changed

+13
-34
lines changed

2 files changed

+13
-34
lines changed

minhash.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lshensemble
33
import (
44
"encoding/binary"
55
"hash/fnv"
6+
"io"
67
"math/rand"
78

89
minwise "github.com/dgryski/go-minhash"
@@ -56,41 +57,27 @@ func (m *Minhash) Signature() Signature {
5657
return m.mw.Signature()
5758
}
5859

59-
// Serialize the siganture into the byte buffer.
60-
func (sig Signature) Write(buffer []byte) {
61-
offset := 0
60+
// Serialize the siganture into the writer.
61+
func (sig Signature) Write(w io.Writer) error {
6262
for i := range sig {
63-
binary.BigEndian.PutUint64(buffer[offset:], sig[i])
64-
offset += HashValueSize
63+
if err := binary.Write(w, binary.BigEndian, sig[i]); err != nil {
64+
return err
65+
}
6566
}
67+
return nil
6668
}
6769

68-
// Deserialize the signature from the byte buffer.
69-
func (sig Signature) Read(buffer []byte) {
70-
offset := 0
70+
// Deserialize the signature from the reader.
71+
func (sig Signature) Read(r io.Reader) error {
7172
for i := range sig {
72-
sig[i] = binary.BigEndian.Uint64(buffer[offset:])
73-
offset += HashValueSize
73+
if err := binary.Read(r, binary.BigEndian, &(sig[i])); err != nil {
74+
return err
75+
}
7476
}
77+
return nil
7578
}
7679

7780
// Compute the serialized length in terms of number of bytes.
7881
func (sig Signature) ByteLen() int {
7982
return len(sig) * HashValueSize
8083
}
81-
82-
func SerializeSignature(sig Signature) []byte {
83-
buffer := make([]byte, sig.ByteLen())
84-
sig.Write(buffer)
85-
return buffer
86-
}
87-
88-
func DeserializeSignature(buffer []byte) Signature {
89-
if len(buffer)%HashValueSize != 0 {
90-
panic("Incorrect length of buffer")
91-
}
92-
size := len(buffer) / HashValueSize
93-
sig := make(Signature, size)
94-
sig.Read(buffer)
95-
return sig
96-
}

minhash_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ import (
99
func TestMinhash(t *testing.T) {
1010
m := NewMinhash(1, 256)
1111
m.Push([]byte("Test some input"))
12-
sig := m.Signature()
13-
buf := SerializeSignature(sig)
14-
sig2 := DeserializeSignature(buf)
15-
for i, v := range sig {
16-
if v != sig2[i] {
17-
t.Fail()
18-
}
19-
}
2012
}
2113

2214
func data(size int) [][]byte {

0 commit comments

Comments
 (0)