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
use std::{collections::BTreeMap, fmt::Display};
use std::fmt;
use std::io::Cursor;
use fluvio_protocol::record::Offset;
use fluvio_protocol::{Encoder, Decoder, record::Record};
#[derive(Debug, Default, Clone, Encoder, Decoder)]
pub struct SmartModuleExtraParams {
inner: BTreeMap<String, String>,
}
impl From<BTreeMap<String, String>> for SmartModuleExtraParams {
fn from(inner: BTreeMap<String, String>) -> SmartModuleExtraParams {
SmartModuleExtraParams { inner }
}
}
impl SmartModuleExtraParams {
pub fn get(&self, key: &str) -> Option<&String> {
self.inner.get(key)
}
pub fn insert(&mut self, key: String, value: String) {
self.inner.insert(key, value);
}
}
#[derive(Debug, Default, Clone, Encoder, Decoder)]
pub struct SmartModuleInput {
base_offset: Offset,
raw_bytes: Vec<u8>,
#[deprecated]
params: SmartModuleExtraParams,
#[fluvio(min_version = 16)]
join_record: Vec<u8>,
}
impl SmartModuleInput {
pub fn new(raw_bytes: Vec<u8>, base_offset: Offset) -> Self {
Self {
base_offset,
raw_bytes,
..Default::default()
}
}
pub fn base_offset(&self) -> Offset {
self.base_offset
}
pub fn set_base_offset(&mut self, base_offset: Offset) {
self.base_offset = base_offset;
}
pub fn raw_bytes(&self) -> &[u8] {
&self.raw_bytes
}
pub fn into_raw_bytes(self) -> Vec<u8> {
self.raw_bytes
}
pub fn parts(self) -> (Vec<u8>, Vec<u8>) {
(self.raw_bytes, self.join_record)
}
}
impl TryFrom<Vec<Record>> for SmartModuleInput {
type Error = std::io::Error;
fn try_from(records: Vec<Record>) -> Result<Self, Self::Error> {
let mut raw_bytes = Vec::new();
records.encode(&mut raw_bytes, 0)?;
Ok(SmartModuleInput {
raw_bytes,
..Default::default()
})
}
}
impl TryInto<Vec<Record>> for SmartModuleInput {
type Error = std::io::Error;
fn try_into(mut self) -> Result<Vec<Record>, Self::Error> {
Decoder::decode_from(&mut Cursor::new(&mut self.raw_bytes), 0)
}
}
impl Display for SmartModuleInput {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"SmartModuleInput {{ base_offset: {:?}, record_data: {:?}, join_data: {:#?} }}",
self.base_offset,
self.raw_bytes.len(),
self.join_record.len()
)
}
}
#[derive(Debug, Default, Clone, Encoder, Decoder)]
pub struct SmartModuleAggregateInput {
pub base: SmartModuleInput,
pub accumulator: Vec<u8>,
}
#[derive(Debug, Default, Clone, Encoder, Decoder)]
pub struct SmartModuleInitInput {
pub params: SmartModuleExtraParams,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_record_to_sm_input_and_back() {
let records = vec![
Record::new("apple"),
Record::new("fruit"),
Record::new("banana"),
];
let sm_input: SmartModuleInput = records
.try_into()
.expect("records to input conversion failed");
let records_decoded: Vec<Record> = sm_input
.try_into()
.expect("input to records conversion failed");
assert_eq!(records_decoded[0].value.as_ref(), b"apple");
assert_eq!(records_decoded[1].value.as_ref(), b"fruit");
assert_eq!(records_decoded[2].value.as_ref(), b"banana");
}
}