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
use crate::prelude::*;
use crate::internal::encodings::varint::decode_suffix_varint;
use std::collections::HashMap;
#[derive(Debug)]
pub enum OneOrMany<'a, T> {
One(T),
Many(&'a [u8]),
}
impl<'a, T: BatchData + std::fmt::Debug> OneOrMany<'a, T> {
pub fn new(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mut usize, is_array_context: bool) -> Self {
if is_array_context {
let len = decode_suffix_varint(bytes, lens) as usize;
let bytes = read_bytes(bytes, len, offset);
OneOrMany::Many(bytes)
} else {
let value = T::read_one(bytes, offset);
OneOrMany::One(value)
}
}
}
#[derive(Debug)]
pub enum DynBranch<'a> {
Object { children: HashMap<&'a str, DynBranch<'a>> },
Tuple { children: Vec<DynBranch<'a>> },
Array { len: OneOrMany<'a, Array>, values: Box<DynBranch<'a>> },
Integer(OneOrMany<'a, u64>),
Nullable {opt: OneOrMany<'a, Nullable>, values: Box<DynBranch<'a>> },
Boolean(OneOrMany<'a, bool>),
Float(OneOrMany<'a, f64>),
}
fn read_next<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mut usize, is_array_context: bool) -> DynBranch<'a> {
let primitive = PrimitiveId::read(bytes, offset);
match primitive {
PrimitiveId::Object { num_fields } => {
let mut children = HashMap::with_capacity(num_fields);
for _ in 0..num_fields {
let name = Str::read_one(bytes, offset);
let child = read_next(bytes, offset, lens, is_array_context);
children.insert(name, child);
}
DynBranch::Object {children }
},
PrimitiveId::Tuple { num_fields } => {
let mut children = Vec::with_capacity(num_fields);
for _ in 0..num_fields {
let child = read_next(bytes, offset, lens, is_array_context);
children.push(child)
}
DynBranch::Tuple { children }
}
PrimitiveId::Array => {
let len = OneOrMany::new(bytes, offset, lens, is_array_context);
let values = read_next(bytes, offset, lens, true);
let values = Box::new(values);
DynBranch::Array {len, values}
},
PrimitiveId::Nullable => {
let opt = OneOrMany::new(bytes, offset, lens, is_array_context);
let values = read_next(bytes, offset, lens, is_array_context);
let values = Box::new(values);
DynBranch::Nullable {opt, values}
}
PrimitiveId::Integer => {
DynBranch::Integer(OneOrMany::new(bytes, offset, lens, is_array_context))
},
PrimitiveId::Boolean => {
DynBranch::Boolean(OneOrMany::new(bytes, offset, lens, is_array_context))
},
PrimitiveId::Float => {
DynBranch::Float(OneOrMany::new(bytes, offset, lens, is_array_context))
}
}
}
pub fn read_root(bytes: &[u8]) -> DynBranch<'_> {
let mut lens = bytes.len() - 1;
let mut offset = 0;
read_next(bytes, &mut offset, &mut lens, false)
}