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
#![allow(deprecated)]
use std::io::Read;
use std::io;
use std::fmt::{Debug, self};
use flate2::{
Compression,
bufread::{GzEncoder, GzDecoder},
};
use fluvio_protocol::{Encoder, Decoder};
use fluvio_smartmodule::dataplane::smartmodule::SmartModuleExtraParams;
#[derive(Debug, Default, Clone, Encoder, Decoder)]
#[deprecated(
since = "0.10.0",
note = "will be removed in the next version. Use SmartModuleInvocation instead "
)]
pub struct LegacySmartModulePayload {
pub wasm: SmartModuleWasmCompressed,
pub kind: SmartModuleKind,
pub params: SmartModuleExtraParams,
}
#[derive(Debug, Default, Clone, Encoder, Decoder)]
pub struct SmartModuleInvocation {
pub wasm: SmartModuleInvocationWasm,
pub kind: SmartModuleKind,
pub params: SmartModuleExtraParams,
}
#[derive(Clone, Encoder, Decoder)]
pub enum SmartModuleInvocationWasm {
Predefined(String),
AdHoc(Vec<u8>),
}
impl SmartModuleInvocationWasm {
pub fn adhoc_from_bytes(bytes: &[u8]) -> io::Result<Self> {
Ok(Self::AdHoc(zip(bytes)?))
}
pub fn into_raw(self) -> io::Result<Vec<u8>> {
match self {
Self::AdHoc(gzipped) => Ok(unzip(gzipped.as_ref())?),
_ => Err(io::Error::new(
io::ErrorKind::InvalidData,
"unable to represent as raw data",
)),
}
}
}
impl Default for SmartModuleInvocationWasm {
fn default() -> Self {
Self::AdHoc(Vec::new())
}
}
impl Debug for SmartModuleInvocationWasm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Predefined(module) => write!(f, "Predefined{}", module),
Self::AdHoc(bytes) => f
.debug_tuple("Adhoc")
.field(&format!("{} bytes", bytes.len()))
.finish(),
}
}
}
#[derive(Debug, Clone, Encoder, Decoder, Default)]
pub enum SmartModuleKind {
#[default]
Filter,
Map,
#[fluvio(min_version = ARRAY_MAP_WASM_API)]
ArrayMap,
Aggregate {
accumulator: Vec<u8>,
},
#[fluvio(min_version = ARRAY_MAP_WASM_API)]
FilterMap,
#[fluvio(min_version = SMART_MODULE_API)]
Join(String),
#[fluvio(min_version = SMART_MODULE_API)]
JoinStream {
topic: String,
derivedstream: String,
},
#[fluvio(min_version = GENERIC_SMARTMODULE_API)]
Generic(SmartModuleContextData),
}
impl std::fmt::Display for SmartModuleKind {
fn fmt(&self, out: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
let name = match self {
SmartModuleKind::Filter => "filter",
SmartModuleKind::Map => "map",
SmartModuleKind::ArrayMap => "array_map",
SmartModuleKind::Aggregate { .. } => "aggregate",
SmartModuleKind::FilterMap => "filter_map",
SmartModuleKind::Join(..) => "join",
SmartModuleKind::JoinStream { .. } => "join_stream",
SmartModuleKind::Generic(..) => "smartmodule",
};
out.write_str(name)
}
}
#[derive(Debug, Clone, Encoder, Decoder, Default)]
pub enum SmartModuleContextData {
#[default]
None,
Aggregate {
accumulator: Vec<u8>,
},
Join(String),
JoinStream {
topic: String,
derivedstream: String,
},
}
#[deprecated(
since = "0.10.0",
note = "will be removed in the next version. Use SmartModuleInvocationWasm instead"
)]
#[derive(Clone, Encoder, Decoder, Debug)]
pub enum SmartModuleWasmCompressed {
Raw(Vec<u8>),
#[fluvio(min_version = 14)]
Gzip(Vec<u8>),
}
impl Default for SmartModuleWasmCompressed {
fn default() -> Self {
Self::Raw(Default::default())
}
}
fn zip(raw: &[u8]) -> io::Result<Vec<u8>> {
let mut encoder = GzEncoder::new(raw, Compression::default());
let mut buffer = Vec::with_capacity(raw.len());
encoder.read_to_end(&mut buffer)?;
Ok(buffer)
}
fn unzip(compressed: &[u8]) -> io::Result<Vec<u8>> {
let mut decoder = GzDecoder::new(compressed);
let mut buffer = Vec::with_capacity(compressed.len());
decoder.read_to_end(&mut buffer)?;
Ok(buffer)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encode_smartmodulekind() {
let mut dest = Vec::new();
let value: SmartModuleKind = SmartModuleKind::Filter;
value.encode(&mut dest, 0).expect("should encode");
assert_eq!(dest.len(), 1);
assert_eq!(dest[0], 0x00);
}
#[test]
fn test_decode_smartmodulekind() {
let bytes = vec![0x01];
let mut value: SmartModuleKind = Default::default();
value
.decode(&mut io::Cursor::new(bytes), 0)
.expect("should decode");
assert!(matches!(value, SmartModuleKind::Map));
}
#[test]
fn test_gzip_smartmoduleinvocationwasm() {
let bytes = vec![0xde, 0xad, 0xbe, 0xef];
let value: SmartModuleInvocationWasm =
SmartModuleInvocationWasm::adhoc_from_bytes(&bytes).expect("should encode");
if let SmartModuleInvocationWasm::AdHoc(compressed_bytes) = value {
let decompressed_bytes = unzip(&compressed_bytes).expect("should decompress");
assert_eq!(decompressed_bytes, bytes);
} else {
panic!("not adhoc")
}
}
}