-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcompression_snappy.go
103 lines (82 loc) · 2.46 KB
/
compression_snappy.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package freezer
import (
"io"
"os"
"github.com/golang/snappy"
"github.com/uw-labs/straw"
)
var _ straw.StreamStore = &snappyStreamStore{}
// snappyStreamStore is a straw.StreamStore wrapper that implements transparent snappy compression. Everything is supported, except for calling Size() on the os.FileInfo returned from Stat or Lstat. Calling Size() like this will panic, but freezer does not need that functionality anyway.
type snappyStreamStore struct {
store straw.StreamStore
}
func (fs *snappyStreamStore) Lstat(name string) (os.FileInfo, error) {
fi, err := fs.store.Lstat(name)
if err != nil {
return nil, err
}
return &noSizeFileInfo{fi}, nil
}
func (fs *snappyStreamStore) Stat(name string) (os.FileInfo, error) {
fi, err := fs.store.Stat(name)
if err != nil {
return nil, err
}
return &noSizeFileInfo{fi}, nil
}
func (fs *snappyStreamStore) OpenReadCloser(name string) (straw.StrawReader, error) {
rc, err := fs.store.OpenReadCloser(name)
if err != nil {
return nil, err
}
return &snappyReadCloser{snappy.NewReader(rc), rc}, nil
}
func (fs *snappyStreamStore) Mkdir(name string, mode os.FileMode) error {
return fs.store.Mkdir(name, mode)
}
func (fs *snappyStreamStore) Remove(name string) error {
return fs.store.Remove(name)
}
func (fs *snappyStreamStore) CreateWriteCloser(name string) (straw.StrawWriter, error) {
wc, err := fs.store.CreateWriteCloser(name)
if err != nil {
return nil, err
}
return &snappyWriteCloser{snappy.NewBufferedWriter(wc), wc}, nil
}
func (fs *snappyStreamStore) Readdir(name string) ([]os.FileInfo, error) {
return fs.store.Readdir(name)
}
func (fs *snappyStreamStore) Close() error {
return fs.store.Close()
}
type snappyReadCloser struct {
sr *snappy.Reader
inner io.Closer
}
func (src *snappyReadCloser) Read(buf []byte) (int, error) {
return src.sr.Read(buf)
}
func (src *snappyReadCloser) Close() error {
return src.inner.Close()
}
func (src *snappyReadCloser) Seek(int64, int) (int64, error) {
panic("freezer: Seek not supported in snappy read closer")
}
func (src *snappyReadCloser) ReadAt([]byte, int64) (int, error) {
panic("freezer: ReadAt not supported in snappy read closer")
}
type snappyWriteCloser struct {
swc io.WriteCloser
inner io.Closer
}
func (src *snappyWriteCloser) Write(buf []byte) (int, error) {
return src.swc.Write(buf)
}
func (src *snappyWriteCloser) Close() error {
if err := src.swc.Close(); err != nil {
_ = src.inner.Close()
return err
}
return src.inner.Close()
}