forked from colinmarc/sequencefile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnappy.go
166 lines (137 loc) · 3.83 KB
/
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package sequencefile
import (
"bytes"
"encoding/binary"
"errors"
"io"
"github.com/golang/snappy"
)
// snappyFrameReader is a decompressor that implements the hadoop framing format
// for snappy. The format consists of:
// - A big-endian uint32 with the total uncompressed size of the data
// - A number of blocks, each of which has:
// - A big-endian uint32 with the compressed size of the data
// - An actual snappy chunk
type snappyFrameReader struct {
r io.Reader
remaining int
compressed bytes.Buffer
uncompressed bytes.Buffer
currentBlock *bytes.Reader
}
func newSnappyFrameReader(r io.Reader) (*snappyFrameReader, error) {
s := &snappyFrameReader{r: r}
err := s.Reset(r)
return s, err
}
func (s *snappyFrameReader) Read(b []byte) (int, error) {
// If anything is left over from a previous partial read, return that.
if s.currentBlock != nil && s.currentBlock.Len() > 0 {
return s.currentBlock.Read(b)
} else {
s.currentBlock = nil
}
sizeBytes := make([]byte, 4)
_, err := io.ReadFull(s.r, sizeBytes)
if err != nil {
return 0, err
}
compressedLength := int(binary.BigEndian.Uint32(sizeBytes))
if compressedLength == 0 {
if s.remaining != 0 {
return 0, errors.New("sequencefile: snappy: partial block")
}
return 0, io.EOF
}
s.compressed.Reset()
_, err = io.CopyN(&s.compressed, s.r, int64(compressedLength))
if err != nil {
return 0, err
}
compressed := s.compressed.Bytes()
uncompressedLength, err := snappy.DecodedLen(compressed)
if err != nil {
return 0, err
}
s.remaining -= uncompressedLength
if s.remaining < 0 {
return 0, errors.New("sequencefile: snappy: partial block")
}
// If the amount asked for is greater than the uncompressed size, we can read
// off the uncompressed data directly. Otherwise, we have to spill into a
// buffer.
if len(b) >= uncompressedLength {
return s.decodeBlock(b[:uncompressedLength], compressed)
} else {
s.uncompressed.Reset()
s.uncompressed.Grow(uncompressedLength)
uncompressed := s.uncompressed.Bytes()[:uncompressedLength]
_, err := s.decodeBlock(uncompressed, compressed)
if err != nil {
return 0, err
}
s.currentBlock = bytes.NewReader(uncompressed)
return s.currentBlock.Read(b)
}
}
func (s *snappyFrameReader) decodeBlock(b, compressed []byte) (int, error) {
uncompressed, err := snappy.Decode(b, compressed)
if err != nil {
return 0, err
}
// If we're doing this correctly, b and uncompressed should be the same, since
// snappy uses the passed-in slice if it's big enough.
if len(uncompressed) != len(b) {
panic("sequencefile: snappy: input buffer was sized incorrectly")
}
return len(b), nil
}
// Reset prepares the snappyFrameReader to read a new stream.
func (s *snappyFrameReader) Reset(r io.Reader) error {
s.r = r
s.uncompressed.Reset()
s.compressed.Reset()
b := make([]byte, 4)
_, err := io.ReadFull(s.r, b)
if err != nil {
return err
}
s.remaining = int(binary.BigEndian.Uint32(b))
if s.remaining < 0 {
panic("sequencefile: snappy: stream size overflows int32")
}
return nil
}
// Close is a noop; it only exists to satisfy the decompressor interface.
func (s *snappyFrameReader) Close() error {
return nil
}
const snappyDefaultChunkSize = 1 << 16
type snappyCompressor struct {
chunkSize int
}
func (c snappyCompressor) compress(src []byte) ([]byte, error) {
var buf bytes.Buffer
var bs [4]byte
binary.BigEndian.PutUint32(bs[:], uint32(len(src)))
if _, err := buf.Write(bs[:]); err != nil {
return nil, err
}
var chunk, dst []byte
for len(src) > 0 {
l := c.chunkSize
if l > len(src) {
l = len(src)
}
chunk, src = src[:l], src[l:]
dst = snappy.Encode(dst, chunk)
binary.BigEndian.PutUint32(bs[:], uint32(len(dst)))
if _, err := buf.Write(bs[:]); err != nil {
return nil, err
}
if _, err := buf.Write(dst); err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}