-
Notifications
You must be signed in to change notification settings - Fork 10
/
asyncresp.rs
432 lines (380 loc) · 12.9 KB
/
asyncresp.rs
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use bytes::Bytes;
use memchr::memchr;
use std::convert::From;
use std::io;
use std::str;
use crate::types::{RedisValueRef, NULL_ARRAY, NULL_BULK_STRING};
use bytes::BytesMut;
use tokio_util::codec::{Decoder, Encoder};
#[derive(Debug)]
pub enum RESPError {
UnexpectedEnd,
UnknownStartingByte,
IOError(std::io::Error),
IntParseFailure,
BadBulkStringSize(i64),
BadArraySize(i64),
}
impl From<std::io::Error> for RESPError {
fn from(e: std::io::Error) -> RESPError {
RESPError::IOError(e)
}
}
#[derive(Default)]
pub struct RespParser;
type RedisResult = Result<Option<(usize, RedisBufSplit)>, RESPError>;
enum RedisBufSplit {
String(BufSplit),
Error(BufSplit),
Array(Vec<RedisBufSplit>),
NullBulkString,
NullArray,
Int(i64),
}
impl RedisBufSplit {
fn redis_value(self, buf: &Bytes) -> RedisValueRef {
match self {
RedisBufSplit::String(bfs) => RedisValueRef::String(bfs.as_bytes(buf)),
RedisBufSplit::Error(bfs) => RedisValueRef::Error(bfs.as_bytes(buf)),
RedisBufSplit::Array(arr) => {
RedisValueRef::Array(arr.into_iter().map(|bfs| bfs.redis_value(buf)).collect())
}
RedisBufSplit::NullArray => RedisValueRef::NullArray,
RedisBufSplit::NullBulkString => RedisValueRef::NullBulkString,
RedisBufSplit::Int(i) => RedisValueRef::Int(i),
}
}
}
/// Fundamental struct for viewing byte slices
///
/// Used for zero-copy redis values.
struct BufSplit(usize, usize);
impl BufSplit {
/// Get a lifetime appropriate slice of the underlying buffer.
///
/// Constant time.
#[inline]
fn as_slice<'a>(&self, buf: &'a BytesMut) -> &'a [u8] {
&buf[self.0..self.1]
}
/// Get a Bytes object representing the appropriate slice
/// of bytes.
///
/// Constant time.
#[inline]
fn as_bytes(&self, buf: &Bytes) -> Bytes {
buf.slice(self.0..self.1)
}
}
#[inline]
fn word(buf: &BytesMut, pos: usize) -> Option<(usize, BufSplit)> {
if buf.len() <= pos {
return None;
}
memchr(b'\r', &buf[pos..]).and_then(|end| {
if end + 1 < buf.len() {
Some((pos + end + 2, BufSplit(pos, pos + end)))
} else {
None
}
})
}
fn int(buf: &BytesMut, pos: usize) -> Result<Option<(usize, i64)>, RESPError> {
match word(buf, pos) {
Some((pos, word)) => {
let s = str::from_utf8(word.as_slice(buf)).map_err(|_| RESPError::IntParseFailure)?;
let i = s.parse().map_err(|_| RESPError::IntParseFailure)?;
Ok(Some((pos, i)))
}
None => Ok(None),
}
}
fn bulk_string(buf: &BytesMut, pos: usize) -> RedisResult {
match int(buf, pos)? {
Some((pos, -1)) => Ok(Some((pos, RedisBufSplit::NullBulkString))),
Some((pos, size)) if size >= 0 => {
let total_size = pos + size as usize;
if buf.len() < total_size + 2 {
Ok(None)
} else {
let bb = RedisBufSplit::String(BufSplit(pos, total_size));
Ok(Some((total_size + 2, bb)))
}
}
Some((_pos, bad_size)) => Err(RESPError::BadBulkStringSize(bad_size)),
None => Ok(None),
}
}
fn simple_string(buf: &BytesMut, pos: usize) -> RedisResult {
Ok(word(buf, pos).map(|(pos, word)| (pos, RedisBufSplit::String(word))))
}
fn error(buf: &BytesMut, pos: usize) -> RedisResult {
Ok(word(buf, pos).map(|(pos, word)| (pos, RedisBufSplit::Error(word))))
}
fn resp_int(buf: &BytesMut, pos: usize) -> RedisResult {
Ok(int(buf, pos)?.map(|(pos, int)| (pos, RedisBufSplit::Int(int))))
}
fn array(buf: &BytesMut, pos: usize) -> RedisResult {
match int(buf, pos)? {
None => Ok(None),
Some((pos, -1)) => Ok(Some((pos, RedisBufSplit::NullArray))),
Some((pos, num_elements)) if num_elements >= 0 => {
let mut values = Vec::with_capacity(num_elements as usize);
let mut curr_pos = pos;
for _ in 0..num_elements {
match parse(buf, curr_pos)? {
Some((new_pos, value)) => {
curr_pos = new_pos;
values.push(value);
}
None => return Ok(None),
}
}
Ok(Some((curr_pos, RedisBufSplit::Array(values))))
}
Some((_pos, bad_num_elements)) => Err(RESPError::BadArraySize(bad_num_elements)),
}
}
fn parse(buf: &BytesMut, pos: usize) -> RedisResult {
if buf.is_empty() {
return Ok(None);
}
match buf[pos] {
b'+' => simple_string(buf, pos + 1),
b'-' => error(buf, pos + 1),
b'$' => bulk_string(buf, pos + 1),
b':' => resp_int(buf, pos + 1),
b'*' => array(buf, pos + 1),
_ => Err(RESPError::UnknownStartingByte),
}
}
impl Decoder for RespParser {
type Item = RedisValueRef;
type Error = RESPError;
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if buf.is_empty() {
return Ok(None);
}
match parse(buf, 0)? {
Some((pos, value)) => {
let our_data = buf.split_to(pos);
Ok(Some(value.redis_value(&our_data.freeze())))
}
None => Ok(None),
}
}
}
impl Encoder for RespParser {
type Item = RedisValueRef;
type Error = io::Error;
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> io::Result<()> {
write_redis_value(item, dst);
Ok(())
}
}
fn write_redis_value(item: RedisValueRef, dst: &mut BytesMut) {
match item {
RedisValueRef::Error(e) => {
dst.extend_from_slice(b"-");
dst.extend_from_slice(&e);
dst.extend_from_slice(b"\r\n");
}
RedisValueRef::ErrorMsg(e) => {
dst.extend_from_slice(b"-");
dst.extend_from_slice(&e);
dst.extend_from_slice(b"\r\n");
}
RedisValueRef::String(s) => {
dst.extend_from_slice(b"$");
dst.extend_from_slice(s.len().to_string().as_bytes());
dst.extend_from_slice(b"\r\n");
dst.extend_from_slice(&s);
dst.extend_from_slice(b"\r\n");
}
RedisValueRef::Array(array) => {
dst.extend_from_slice(b"*");
dst.extend_from_slice(array.len().to_string().as_bytes());
dst.extend_from_slice(b"\r\n");
for redis_value in array {
write_redis_value(redis_value, dst);
}
}
RedisValueRef::Int(i) => {
dst.extend_from_slice(b":");
dst.extend_from_slice(i.to_string().as_bytes());
dst.extend_from_slice(b"\r\n");
}
RedisValueRef::NullArray => dst.extend_from_slice(NULL_ARRAY.as_bytes()),
RedisValueRef::NullBulkString => dst.extend_from_slice(NULL_BULK_STRING.as_bytes()),
}
}
#[cfg(test)]
mod resp_parser_tests {
use crate::asyncresp::RespParser;
use crate::types::{RedisValueRef, Value};
use bytes::{Bytes, BytesMut};
use tokio_util::codec::{Decoder, Encoder};
fn generic_test(input: &'static str, output: RedisValueRef) {
let mut decoder = RespParser::default();
let result_read = decoder.decode(&mut BytesMut::from(input));
let mut encoder = RespParser::default();
let mut buf = BytesMut::new();
let result_write = encoder.encode(output.clone(), &mut buf);
assert!(
result_write.as_ref().is_ok(),
"{:?}",
result_write.unwrap_err()
);
assert_eq!(input.as_bytes(), buf.as_ref());
assert!(
result_read.as_ref().is_ok(),
"{:?}",
result_read.unwrap_err()
);
// let values = result_read.unwrap().unwrap();
// let generic_arr_test_case = vec![output.clone(), output.clone()];
// let doubled = input.to_owned() + &input.to_owned();
// assert_eq!(output, values);
// generic_test_arr(&doubled, generic_arr_test_case)
}
fn generic_test_arr(input: &str, output: Vec<RedisValueRef>) {
// TODO: Try to make this occur randomly
let first: usize = input.len() / 2;
let second = input.len() - first;
let mut first = BytesMut::from(&input[0..=first]);
let mut second = Some(BytesMut::from(&input[second..]));
let mut decoder = RespParser::default();
let mut res: Vec<RedisValueRef> = Vec::new();
loop {
match decoder.decode(&mut first) {
Ok(Some(value)) => {
res.push(value.into());
break;
}
Ok(None) => {
if let None = second {
panic!("Test expected more bytes than expected!");
}
first.extend(second.unwrap());
second = None;
}
Err(e) => panic!("Should not error, {:?}", e),
}
}
if let Some(second) = second {
first.extend(second);
}
loop {
match decoder.decode(&mut first) {
Ok(Some(value)) => {
res.push(value.into());
break;
}
Err(e) => panic!("Should not error, {:?}", e),
_ => break,
}
}
assert_eq!(output, res);
}
fn ezs() -> Value {
Bytes::from_static(b"hello")
}
// XXX: Simple String has been removed.
// #[test]
// fn test_simple_string() {
// let t = RedisValue::BulkString(ezs());
// let s = "+hello\r\n";
// generic_test(s, t);
// let t0 = RedisValue::BulkString(ezs());
// let t1 = RedisValue::BulkString("abcdefghijklmnopqrstuvwxyz".as_bytes().to_vec());
// let s = "+hello\r\n+abcdefghijklmnopqrstuvwxyz\r\n";
// generic_test_arr(s, vec![t0, t1]);
// }
#[test]
fn test_error() {
let t = RedisValueRef::Error(ezs());
let s = "-hello\r\n";
generic_test(s, t);
let t0 = RedisValueRef::Error(Bytes::from_static(b"abcdefghijklmnopqrstuvwxyz"));
let t1 = RedisValueRef::Error(ezs());
let s = "-abcdefghijklmnopqrstuvwxyz\r\n-hello\r\n";
generic_test_arr(s, vec![t0, t1]);
}
#[test]
fn test_bulk_string() {
let t = RedisValueRef::String(ezs());
let s = "$5\r\nhello\r\n";
generic_test(s, t);
let t = RedisValueRef::String(Bytes::from_static(b""));
let s = "$0\r\n\r\n";
generic_test(s, t);
}
#[test]
fn test_int() {
let t = RedisValueRef::Int(0);
let s = ":0\r\n";
generic_test(s, t);
let t = RedisValueRef::Int(123);
let s = ":123\r\n";
generic_test(s, t);
let t = RedisValueRef::Int(-123);
let s = ":-123\r\n";
generic_test(s, t);
}
#[test]
fn test_array() {
let t = RedisValueRef::Array(vec![]);
let s = "*0\r\n";
generic_test(s, t);
let inner = vec![
RedisValueRef::String(Bytes::from_static(b"foo")),
RedisValueRef::String(Bytes::from_static(b"bar")),
];
let t = RedisValueRef::Array(inner);
let s = "*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n";
generic_test(s, t);
let inner = vec![
RedisValueRef::Int(1),
RedisValueRef::Int(2),
RedisValueRef::Int(3),
];
let t = RedisValueRef::Array(inner);
let s = "*3\r\n:1\r\n:2\r\n:3\r\n";
generic_test(s, t);
let inner = vec![
RedisValueRef::Int(1),
RedisValueRef::Int(2),
RedisValueRef::Int(3),
RedisValueRef::Int(4),
RedisValueRef::String(Bytes::from_static(b"foobar")),
];
let t = RedisValueRef::Array(inner);
let s = "*5\r\n:1\r\n:2\r\n:3\r\n:4\r\n$6\r\nfoobar\r\n";
generic_test(s, t);
let inner = vec![
RedisValueRef::Array(vec![
RedisValueRef::Int(1),
RedisValueRef::Int(2),
RedisValueRef::Int(3),
]),
RedisValueRef::Array(vec![
RedisValueRef::String(Bytes::from_static(b"Foo")),
RedisValueRef::Error(Bytes::from_static(b"Bar")),
]),
];
let t = RedisValueRef::Array(inner);
let s = "*2\r\n*3\r\n:1\r\n:2\r\n:3\r\n*2\r\n$3\r\nFoo\r\n-Bar\r\n";
generic_test(s, t);
let inner = vec![
RedisValueRef::String(Bytes::from_static(b"foo")),
RedisValueRef::NullBulkString,
RedisValueRef::String(Bytes::from_static(b"bar")),
];
let t = RedisValueRef::Array(inner);
let s = "*3\r\n$3\r\nfoo\r\n$-1\r\n$3\r\nbar\r\n";
generic_test(s, t);
let t = RedisValueRef::NullArray;
let s = "*-1\r\n";
generic_test(s, t);
}
}