/
compression.go
43 lines (36 loc) · 1.17 KB
/
compression.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
package reqresp
import (
"fmt"
"github.com/golang/snappy"
"io"
)
type Compression interface {
// Wraps a reader to decompress data as reads happen.
Decompress(r io.Reader) io.Reader
// Wraps a writer to compress data as writes happen.
Compress(w io.WriteCloser) io.WriteCloser
// Returns an error when the input size is too large to encode.
MaxEncodedLen(msgLen uint64) (uint64, error)
// The name of the compression that is suffixed to the actual encoding. E.g. "snappy", w.r.t. "ssz_snappy".
Name() string
}
type SnappyCompression struct{}
func (c SnappyCompression) Decompress(reader io.Reader) io.Reader {
return snappy.NewReader(reader)
}
func (c SnappyCompression) Compress(w io.WriteCloser) io.WriteCloser {
return snappy.NewBufferedWriter(w)
}
func (c SnappyCompression) MaxEncodedLen(msgLen uint64) (uint64, error) {
if msgLen&(1<<63) != 0 {
return 0, fmt.Errorf("message length %d is too large to compress with snappy", msgLen)
}
m := snappy.MaxEncodedLen(int(msgLen))
if m < 0 {
return 0, fmt.Errorf("message length %d is too large to compress with snappy", msgLen)
}
return uint64(m), nil
}
func (c SnappyCompression) Name() string {
return "snappy"
}