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
use crate::error::{MeiliBridgeError, Result};
use crate::pipeline::memory_monitor::MemoryMonitor;
use serde::Deserialize;
use serde_json::{Deserializer, Value};
use std::io::Read;
use tracing::{debug, warn};
/// Process JSON documents in a streaming fashion to minimize memory usage
pub struct StreamingJsonProcessor {
/// Maximum document size in bytes
max_document_size: usize,
/// Memory monitor for tracking allocations
memory_monitor: Option<MemoryMonitor>,
}
impl StreamingJsonProcessor {
pub fn new(max_document_size: usize) -> Self {
Self {
max_document_size,
memory_monitor: None,
}
}
pub fn with_memory_monitor(mut self, monitor: MemoryMonitor) -> Self {
self.memory_monitor = Some(monitor);
self
}
/// Parse a JSON document from a reader with size limits
pub fn parse_document<R: Read>(&self, mut reader: R) -> Result<Value> {
// Use a limited reader to prevent OOM
let limited_reader = reader.by_ref().take(self.max_document_size as u64);
// Parse JSON in streaming mode
let mut de = Deserializer::from_reader(limited_reader);
let value = Value::deserialize(&mut de)
.map_err(|e| MeiliBridgeError::Validation(format!("Failed to parse JSON: {}", e)))?;
// Estimate memory usage
let estimated_size = Self::estimate_value_size(&value);
debug!(
"Parsed document with estimated size: {} bytes",
estimated_size
);
// Track memory if monitor is available
if let Some(monitor) = &self.memory_monitor {
let _allocation = monitor.track_event_memory(estimated_size as u64)?;
// The allocation guard will be dropped when the value is processed
}
Ok(value)
}
/// Parse multiple documents from a reader
pub fn parse_document_stream<R: Read>(&self, reader: R) -> StreamingDocumentIterator<'_, R> {
StreamingDocumentIterator {
reader,
processor: self,
done: false,
}
}
/// Process large arrays by yielding items one at a time
pub async fn process_array_streaming<F>(
&self,
json_str: &str,
mut processor: F,
) -> Result<usize>
where
F: FnMut(Value) -> Result<()>,
{
let mut count = 0;
let mut depth = 0;
let mut in_array = false;
let mut current_item = String::new();
let chars = json_str.chars().peekable();
for ch in chars {
if !in_array {
if ch == '[' {
in_array = true;
continue;
}
} else {
match ch {
'{' => {
depth += 1;
current_item.push(ch);
}
'}' => {
depth -= 1;
current_item.push(ch);
if depth == 0 && !current_item.trim().is_empty() {
// Parse and process the item
match serde_json::from_str::<Value>(¤t_item) {
Ok(value) => {
processor(value)?;
count += 1;
}
Err(e) => {
warn!("Failed to parse array item: {}", e);
}
}
current_item.clear();
}
}
',' if depth == 0 => {
// Skip commas between items
continue;
}
']' if depth == 0 => {
// End of array
break;
}
_ => {
if depth > 0 || ch != ' ' {
current_item.push(ch);
}
}
}
}
}
Ok(count)
}
/// Estimate the memory size of a JSON value
fn estimate_value_size(value: &Value) -> usize {
match value {
Value::Null => 8,
Value::Bool(_) => 8,
Value::Number(_) => 16,
Value::String(s) => 24 + s.len(),
Value::Array(arr) => 24 + arr.iter().map(Self::estimate_value_size).sum::<usize>(),
Value::Object(obj) => {
24 + obj
.iter()
.map(|(k, v)| 24 + k.len() + Self::estimate_value_size(v))
.sum::<usize>()
}
}
}
}
/// Iterator for streaming document parsing
pub struct StreamingDocumentIterator<'a, R: Read> {
reader: R,
processor: &'a StreamingJsonProcessor,
done: bool,
}
impl<'a, R: Read> Iterator for StreamingDocumentIterator<'a, R> {
type Item = Result<Value>;
fn next(&mut self) -> Option<Self::Item> {
if self.done {
return None;
}
// Try to parse the next document
match self.processor.parse_document(&mut self.reader) {
Ok(doc) => Some(Ok(doc)),
Err(e) => {
self.done = true;
if e.to_string().contains("EOF") {
None
} else {
Some(Err(e))
}
}
}
}
}
/// Zero-copy event router using references where possible
pub struct ZeroCopyEventRouter {}
impl ZeroCopyEventRouter {
pub fn new() -> Self {
Self {}
}
}
impl Default for ZeroCopyEventRouter {
fn default() -> Self {
Self::new()
}
}
impl ZeroCopyEventRouter {
/// Route an event without unnecessary cloning
pub fn route_event<'a>(&'a mut self, event_data: &'a [u8]) -> Result<EventView<'a>> {
// Parse just enough to determine routing
let mut offset = 0;
// Skip whitespace
while offset < event_data.len() && event_data[offset].is_ascii_whitespace() {
offset += 1;
}
if offset >= event_data.len() || event_data[offset] != b'{' {
return Err(MeiliBridgeError::Validation(
"Invalid JSON object".to_string(),
));
}
// Extract table name without parsing entire document
if let Some(table_start) = find_json_string_value(event_data, b"\"table\"") {
let table = std::str::from_utf8(&event_data[table_start.0..table_start.1])
.map_err(|e| MeiliBridgeError::Validation(format!("Invalid UTF-8: {}", e)))?;
Ok(EventView {
table,
raw_data: event_data,
})
} else {
Err(MeiliBridgeError::Validation(
"Missing table field".to_string(),
))
}
}
}
/// A zero-copy view of an event
pub struct EventView<'a> {
pub table: &'a str,
pub raw_data: &'a [u8],
}
impl<'a> EventView<'a> {
/// Parse the full event only when needed
pub fn parse_full(&self) -> Result<Value> {
serde_json::from_slice(self.raw_data)
.map_err(|e| MeiliBridgeError::Validation(format!("Failed to parse event: {}", e)))
}
}
/// Find a JSON string value without full parsing
fn find_json_string_value(data: &[u8], key: &[u8]) -> Option<(usize, usize)> {
let mut i = 0;
while i < data.len() {
if data[i..].starts_with(key) {
i += key.len();
// Skip whitespace and colon
while i < data.len() && (data[i].is_ascii_whitespace() || data[i] == b':') {
i += 1;
}
// Expect opening quote
if i < data.len() && data[i] == b'"' {
i += 1;
let start = i;
// Find closing quote (handling escapes)
while i < data.len() {
if data[i] == b'\\' {
i += 2; // Skip escaped character
} else if data[i] == b'"' {
return Some((start, i));
} else {
i += 1;
}
}
}
}
i += 1;
}
None
}