forked from vaquita/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.go
283 lines (227 loc) · 7.19 KB
/
compress.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
The MIT License (MIT)
Copyright (c) 2015 Nirbhay Choubey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package mysql
import (
"bytes"
"compress/zlib"
"io"
)
type compressRW struct {
c *Conn
cbuff buffer // buffer to hold compressed packet
ubuff buffer // buffer to hold uncompressed packet(s)
seqno uint8 // packet sequence number
}
func (rw *compressRW) init(c *Conn) {
rw.c = c
rw.cbuff.New(_INITIAL_PACKET_BUFFER_SIZE)
rw.ubuff.New(_INITIAL_PACKET_BUFFER_SIZE)
}
// read reads a compressed protocol packet from network (when required),
// uncompresses and caches it so that the uncompressed content of requested
// size can be copied to given buffer.
func (rw *compressRW) read(b []byte, length int) (int, error) {
var (
n, unread int
err error
)
// unread bytes in the buffer
unread = rw.ubuff.Len() - rw.ubuff.Tell()
// read a compressed packet if the local buffer (ubuff) is either
// empty, fully read or does not have enough requested unread bytes
if length > unread {
if err = rw.readCompressedPacket(unread); err != nil {
return 0, err
}
}
// fill the supplied buffer with contents from locally cached
// uncompressed packet buffer of specified length.
n = copy(b, rw.ubuff.Read(length))
return n, nil
}
// readCompressedPacket reads a compressed protocol packet from network into
// the local compressed packet buffer (cbuff), uncompresses it and caches it
// into the local cache for uncompressed packets (ubuff)
func (rw *compressRW) readCompressedPacket(unread int) error {
var (
payloadLength, origPayloadLength int
cbuff, old []byte
err error
)
// save unread bytes from the buffer
if unread > 0 {
old = make([]byte, unread)
copy(old, rw.ubuff.Read(unread))
}
// read the compressed packet header into the compressed packet
// buffer (cbuff) and parse it
if cbuff, err = rw.cbuff.Reset(7); err != nil {
return err
}
if _, err = rw.c.netRead(cbuff[0:7]); err != nil {
return myError(ErrRead, err)
}
// packet payload length
payloadLength = int(getUint24(cbuff[0:3]))
// check for out-of-order packets
if rw.seqno != cbuff[3] {
return myError(ErrNetPacketsOutOfOrder)
}
// length of payload before compression
origPayloadLength = int(getUint24(cbuff[4:7]))
// increment the packet sequence number
rw.seqno++
// error out if the packet is too big
if payloadLength+7 > int(rw.c.p.maxPacketSize) {
return myError(ErrNetPacketTooLarge)
}
// read compressed protocol packet payload from the network into
// the compressed packet buffer (note: the header gets overwritten)
if cbuff, err = rw.cbuff.Reset(payloadLength); err != nil {
return err
}
if _, err = rw.c.netRead(cbuff[0:payloadLength]); err != nil {
return myError(ErrRead, err)
}
// at this point we have the packet payload stored into the compressed
// packet buffer (cbuff), uncompress (if needed) and store it into the
// uncompressed packet buffer (ubuff).
if origPayloadLength != 0 { // its a compressed payload
var (
src io.ReadCloser
)
if _, err = rw.ubuff.Reset(origPayloadLength + unread); err != nil {
return err
}
// reload the unread bytes from old buffer
if unread > 0 {
rw.ubuff.Write(old)
}
if src, err = zlib.NewReader(bytes.NewReader(cbuff[0:payloadLength])); err != nil {
return myError(ErrCompression, err)
} else if _, err = io.Copy(&rw.ubuff, src); err != nil {
return myError(ErrCompression, err)
}
} else { // its an uncompressed payload, simply copy it
if _, err = rw.ubuff.Reset(payloadLength + unread); err != nil {
return err
}
// reload the unread bytes from old buffer
if unread > 0 {
rw.ubuff.Write(old)
}
rw.ubuff.Write(cbuff[0:payloadLength])
}
// reset for reading
rw.ubuff.Seek(0)
return nil
}
// write creates a compressed protocol packet with the specified payload and
// writes it to the network.
func (rw *compressRW) write(b []byte) (int, error) {
var (
cbuff []byte
n int
err error
)
// TODO: add a property for compression threshold
if len(b) > 50 { // compress the payload
if cbuff, err = rw.createCompPacket(b); err != nil {
return 0, err
}
} else { // no need to compress the payload
if cbuff, err = rw.createRegPacket(b); err != nil {
return 0, err
}
}
// increment the packet sequence number
rw.seqno++
if n, err = rw.c.netWrite(cbuff); err != nil {
return n, myError(ErrWrite, err)
}
return n, nil
}
// createCompPacket generates a compressed protocol packet after
// compressing the given payload.
func (rw *compressRW) createCompPacket(b []byte) ([]byte, error) {
var (
w *zlib.Writer
z bytes.Buffer
cbuff []byte
err error
payloadLength int
off int
)
// TODO: add a property for compression level
if w, err = zlib.NewWriterLevel(&z, zlib.DefaultCompression); err != nil {
goto E
}
if _, err = w.Write(b); err != nil {
goto E
}
if err = w.Close(); err != nil {
goto E
}
payloadLength = z.Len()
if cbuff, err = rw.cbuff.Reset(7 + payloadLength); err != nil {
return nil, err
}
// compressed header
// - size of compressed payload
putUint24(cbuff[0:3], uint32(payloadLength))
// - packet sequence number
cbuff[3] = rw.seqno
// - size of payload before it was compressed
putUint24(cbuff[4:7], uint32(len(b)))
off += 7
// copy the compressed payload
off += copy(cbuff[7:], z.Bytes())
return cbuff[0:off], nil
E:
return nil, myError(ErrCompression, err)
}
// createRegPacket generates a non-compressed protocol packet from the specified
// payload.
func (rw *compressRW) createRegPacket(b []byte) ([]byte, error) {
var (
cbuff []byte
off, payloadLength int
err error
)
payloadLength = len(b)
if cbuff, err = rw.cbuff.Reset(7 + payloadLength); err != nil {
return nil, err
}
// compressed header
// - size of compressed payload
putUint24(cbuff[0:3], uint32(payloadLength))
// - packet sequence number
cbuff[3] = rw.seqno
// - = 0, because the payload is not being compressed
putUint24(cbuff[4:7], uint32(0))
off += 7
// store the payload (as is)
off += copy(cbuff[7:], b)
return cbuff[0:off], nil
}
// reset resets the packet sequence number.
func (rw *compressRW) reset() {
rw.seqno = 0
}