-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompression.go
63 lines (51 loc) · 1.26 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package extsort
import (
"compress/gzip"
"io"
"github.com/klauspost/compress/snappy"
)
// Compression codec.
type Compression uint8
// Supported compression types.
const (
CompressionNone Compression = iota
CompressionGzip
CompressionSnappy
)
func (c Compression) norm() Compression {
if c < CompressionNone || c > CompressionSnappy {
return CompressionNone
}
return c
}
func (c Compression) newReader(r io.Reader) (io.ReadCloser, error) {
switch c {
case CompressionGzip:
return gzip.NewReader(r)
case CompressionSnappy:
r := snappy.NewReader(r)
return readerNoopCloser{Reader: r}, nil
}
return readerNoopCloser{Reader: r}, nil
}
func (c Compression) newWriter(w io.Writer) compressedWriter {
switch c {
case CompressionGzip:
wr, _ := gzip.NewWriterLevel(w, gzip.BestSpeed)
return wr
case CompressionSnappy:
wr := snappy.NewBufferedWriter(w)
return wr
}
return &writerNoopCloser{Writer: w}
}
type compressedWriter interface {
io.Writer
Reset(w io.Writer)
Close() error
}
type readerNoopCloser struct{ io.Reader }
func (readerNoopCloser) Close() error { return nil }
type writerNoopCloser struct{ io.Writer }
func (w *writerNoopCloser) Reset(wr io.Writer) { w.Writer = wr }
func (*writerNoopCloser) Close() error { return nil }