-
Notifications
You must be signed in to change notification settings - Fork 95
/
decode.rs
156 lines (142 loc) · 5.34 KB
/
decode.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
use std::collections::HashMap;
use std::io::Read;
use std::mem::transmute;
use failure::Error;
use crate::schema::Schema;
use crate::types::Value;
use crate::util::{safe_len, zag_i32, zag_i64, DecodeError};
#[inline]
fn decode_long<R: Read>(reader: &mut R) -> Result<Value, Error> {
zag_i64(reader).map(Value::Long)
}
#[inline]
fn decode_int<R: Read>(reader: &mut R) -> Result<Value, Error> {
zag_i32(reader).map(Value::Int)
}
#[inline]
fn decode_len<R: Read>(reader: &mut R) -> Result<usize, Error> {
zag_i64(reader).and_then(|len| safe_len(len as usize))
}
/// Decode a `Value` from avro format given its `Schema`.
pub fn decode<R: Read>(schema: &Schema, reader: &mut R) -> Result<Value, Error> {
match *schema {
Schema::Null => Ok(Value::Null),
Schema::Boolean => {
let mut buf = [0u8; 1];
reader.read_exact(&mut buf[..])?;
match buf[0] {
0u8 => Ok(Value::Boolean(false)),
1u8 => Ok(Value::Boolean(true)),
_ => Err(DecodeError::new("not a bool").into()),
}
},
Schema::Int => decode_int(reader),
Schema::Long => decode_long(reader),
Schema::Float => {
let mut buf = [0u8; 4];
reader.read_exact(&mut buf[..])?;
Ok(Value::Float(unsafe { transmute::<[u8; 4], f32>(buf) }))
},
Schema::Double => {
let mut buf = [0u8; 8];
reader.read_exact(&mut buf[..])?;
Ok(Value::Double(unsafe { transmute::<[u8; 8], f64>(buf) }))
},
Schema::Bytes => {
let len = decode_len(reader)?;
let mut buf = Vec::with_capacity(len);
unsafe {
buf.set_len(len);
}
reader.read_exact(&mut buf)?;
Ok(Value::Bytes(buf))
},
Schema::String => {
let len = decode_len(reader)?;
let mut buf = Vec::with_capacity(len);
unsafe {
buf.set_len(len);
}
reader.read_exact(&mut buf)?;
String::from_utf8(buf)
.map(Value::String)
.map_err(|_| DecodeError::new("not a valid utf-8 string").into())
},
Schema::Fixed { size, .. } => {
let mut buf = vec![0u8; size as usize];
reader.read_exact(&mut buf)?;
Ok(Value::Fixed(size, buf))
},
Schema::Array(ref inner) => {
let mut items = Vec::new();
loop {
let len = decode_len(reader)?;
// arrays are 0-terminated, 0i64 is also encoded as 0 in Avro
// reading a length of 0 means the end of the array
if len == 0 {
break
}
items.reserve(len as usize);
for _ in 0..len {
items.push(decode(inner, reader)?);
}
}
Ok(Value::Array(items))
},
Schema::Map(ref inner) => {
let mut items = HashMap::new();
loop {
let len = decode_len(reader)?;
// maps are 0-terminated, 0i64 is also encoded as 0 in Avro
// reading a length of 0 means the end of the map
if len == 0 {
break
}
items.reserve(len as usize);
for _ in 0..len {
if let Value::String(key) = decode(&Schema::String, reader)? {
let value = decode(inner, reader)?;
items.insert(key, value);
} else {
return Err(DecodeError::new("map key is not a string").into())
}
}
}
Ok(Value::Map(items))
},
Schema::Union(ref inner) => {
let index = zag_i64(reader)?;
let variants = inner.variants();
match variants.get(index as usize) {
Some(variant) => decode(variant, reader).map(|x| Value::Union(Box::new(x))),
None => Err(DecodeError::new("Union index out of bounds").into()),
}
},
Schema::Record { ref fields, .. } => {
// Benchmarks indicate ~10% improvement using this method.
let mut items = Vec::new();
for field in fields {
// This clone is also expensive. See if we can do away with it...
items.push((field.name.clone(), decode(&field.schema, reader)?));
}
Ok(Value::Record(items))
// fields
// .iter()
// .map(|field| decode(&field.schema, reader).map(|value| (field.name.clone(), value)))
// .collect::<Result<Vec<(String, Value)>, _>>()
// .map(|items| Value::Record(items))
},
Schema::Enum { ref symbols, .. } => {
if let Value::Int(index) = decode_int(reader)? {
if index >= 0 && (index as usize) <= symbols.len() {
let symbol = symbols[index as usize].clone();
Ok(Value::Enum(index, symbol))
} else {
Err(DecodeError::new("enum symbol index out of bounds").into())
}
} else {
Err(DecodeError::new("enum symbol not found").into())
}
},
}
}