forked from variflight/feeyo-redisproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisRequestDecoderV2.java
More file actions
242 lines (190 loc) · 5.82 KB
/
RedisRequestDecoderV2.java
File metadata and controls
242 lines (190 loc) · 5.82 KB
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
package com.feeyo.redis.engine.codec;
/**
----------------------Redis 协议-----------------------
*<参数数量> CR LF
$<参数1的字节数> CR LF
<参数1的数据> CR LF
...
$<参数N的字节数> CR LF
<参数N的数据> CR LF
--------------------------------------------------------
*/
public class RedisRequestDecoderV2 {
private RedisRequest request = null;
private byte[] _buffer;
private int _offset;
public void reset() {
_offset = 0;
_buffer = null;
}
public RedisRequest next() throws RedisRequestUnknowException {
return null;
}
public RedisRequest decode(byte[] buffer) throws RedisRequestUnknowException {
append( buffer );
try {
// 至少3字节 :\r\n
if (bytesRemaining() < 3) {
throw new RedisRequestUnknowException("Invalid request");
}
// * 协议
byte b = _buffer[ _offset ];
if ( b == '*') {
// skip first byte *
_offset++;
request = new RedisRequest();
// read arguments count
int numArgs = readInt();
byte[][] args = new byte[ numArgs ][];
request.setArgs(args);
// read arguments
for(int i = 0; i < numArgs; i++ ) {
byte b1 = _buffer[ _offset ];
if (b1 == '$') {
_offset++;
int length = readInt();
args[i] = new byte[ length ];
for(int j = 0; j < length; j++) {
args[i][j] = _buffer[ _offset + j ];
}
// skip read length
_offset += length;
// skip \r\n
_offset++;
_offset++;
} else {
throw new RedisRequestUnknowException("Invalid request");
//TODO: 异常待处理,抛弃非REDIS包
}
}
// INLINE 协议
} else {
int start = _offset;
int end = _offset;
if (end + 1 >= _buffer.length) {
throw new IndexOutOfBoundsException("Not enough data.");
}
while (_buffer[end] != '\r' && _buffer[end + 1] != '\n') {
end++;
if (end + 1 == _buffer.length) {
throw new IndexOutOfBoundsException("didn't see LF");
}
}
int length = end - start;
byte[][] args = new byte[1][];
args[0] = new byte[ length ];
for(int j = 0; j < length; j++) {
args[0][j] = _buffer[ _offset + j ];
}
request = new RedisRequest();
request.setInline( true );
request.setArgs(args);
// skip read length
_offset += length;
// skip \r\n
_offset++;
_offset++;
}
// 处理粘包
byte[] theBuffer = _buffer;
if ( theBuffer.length > _offset ) {
byte[] newBuffer = new byte[ theBuffer.length - _offset ];
System.arraycopy(theBuffer, _offset, newBuffer, 0, newBuffer.length);
_buffer = newBuffer;
theBuffer = null;
_offset = 0;
} else {
_offset = 0;
_buffer = null;
}
} catch (IndexOutOfBoundsException e1) {
this._offset = 0;
return null;
}
return request;
}
private int readInt() throws IndexOutOfBoundsException, RedisRequestUnknowException {
long v = 0;
boolean isNeg = false;
// check offset
if ( _offset >= _buffer.length ) {
throw new IndexOutOfBoundsException("Not enough data.");
}
byte b = _buffer[ _offset ];
if ( b == '-' ) {
isNeg = true;
_offset++;
if ( _offset >= _buffer.length ) {
throw new IndexOutOfBoundsException("Not enough data.");
}
b = _buffer[ _offset ];
}
while ( b != '\r' ) {
int value = b - '0';
if (value >= 0 && value < 10) {
v *= 10;
v += value;
} else {
throw new RedisRequestUnknowException("Invalid character in integer");
}
_offset++;
if ( _offset >= _buffer.length ) {
throw new IndexOutOfBoundsException("Not enough data.");
}
b = _buffer[ _offset ];
}
// skip \r\n
_offset++;
_offset++;
v = (isNeg ? -v : v);
return (int)v;
}
private void append(byte[] newBuffer) {
if (newBuffer == null) {
return;
}
if (_buffer == null) {
_buffer = newBuffer;
return;
}
_buffer = margeByteArray(_buffer, newBuffer);
_offset = 0;
}
private byte[] margeByteArray(byte[] a, byte[] b) {
byte[] result = new byte[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
/*
for (int i = 0; i < result.length; ++i) {
result[i] = i < a.length ? a[i] : b[i - a.length];
}
*/
return result;
}
private int bytesRemaining() {
return (_buffer.length - _offset) < 0 ? 0 : (_buffer.length - _offset);
}
public static void main(String args[]) throws Exception {
RedisRequestDecoderV2 decoder = new RedisRequestDecoderV2();
long t = System.currentTimeMillis();
for(int j = 0; j < 10000000; j++) {
try {
byte[] buff = "*2\r\n$3\r\nGET\r\n$2\r\naa\r\n".getBytes();
//buff = "*2\r\n$4\r\nKEYS\r\n$1\r\\*\r\n".getBytes();
//buff = "PING\r\n".getBytes();
//buff = "QUIT\r\n".getBytes();
//buff = "*1\r\n$4\r\nPING\r\n".getBytes();
long t1 = System.currentTimeMillis();
RedisRequest req = decoder.decode( buff );
long t2 = System.currentTimeMillis();
int diff = (int)(t2-t1);
if ( diff > 1) {
System.out.println(" decode diff=" + diff + ", req=" + req.toString() );
}
} catch (RedisRequestUnknowException e) {
e.printStackTrace();
}
}
System.out.println(System.currentTimeMillis() - t);
}
}