Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: tidy encode_input function #18300

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions src/sinks/util/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io;

use bytes::BytesMut;
use codecs::encoding::Framer;
use itertools::{Itertools, Position};
use tokio_util::codec::Encoder as _;
use vector_common::request_metadata::GroupedCountByteSize;
use vector_core::{config::telemetry, EstimatedJsonEncodedSizeOf};
Expand All @@ -24,7 +25,7 @@ pub trait Encoder<T> {
impl Encoder<Vec<Event>> for (Transformer, crate::codecs::Encoder<Framer>) {
fn encode_input(
&self,
mut events: Vec<Event>,
events: Vec<Event>,
writer: &mut dyn io::Write,
) -> io::Result<(usize, GroupedCountByteSize)> {
let mut encoder = self.1.clone();
Expand All @@ -36,37 +37,31 @@ impl Encoder<Vec<Event>> for (Transformer, crate::codecs::Encoder<Framer>) {

let mut byte_size = telemetry().create_request_count_byte_size();

if let Some(last) = events.pop() {
for mut event in events {
self.0.transform(&mut event);

// Ensure the json size is calculated after any fields have been removed
// by the transformer.
byte_size.add_event(&event, event.estimated_json_encoded_size_of());

let mut bytes = BytesMut::new();
encoder
.encode(event, &mut bytes)
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
write_all(writer, n_events_pending, &bytes)?;
bytes_written += bytes.len();
n_events_pending -= 1;
}
let mut event = last;
for (position, mut event) in events.into_iter().with_position() {
self.0.transform(&mut event);

// Ensure the json size is calculated after any fields have been removed
// by the transformer.
byte_size.add_event(&event, event.estimated_json_encoded_size_of());

let mut bytes = BytesMut::new();
encoder
.serialize(event, &mut bytes)
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
match position {
Position::Last | Position::Only => {
encoder
.serialize(event, &mut bytes)
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
}
_ => {
encoder
.encode(event, &mut bytes)
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
}
}
write_all(writer, n_events_pending, &bytes)?;
bytes_written += bytes.len();
n_events_pending -= 1;
}

let batch_suffix = encoder.batch_suffix();
assert!(n_events_pending == 0);
write_all(writer, 0, batch_suffix)?;
Expand Down