Skip to content

Commit a280b6f

Browse files
kkondakaEC2 Default User
authored andcommitted
Add getEstimatedSize API to OutputCodec (opensearch-project#5593)
* Add getEstimatedSize API to OutputCodec Signed-off-by: Krishna Kondaka <krishkdk@amazon.com> * Fixed build failure Signed-off-by: Krishna Kondaka <krishkdk@amazon.com> * Addressed review comments Signed-off-by: Krishna Kondaka <krishkdk@amazon.com> --------- Signed-off-by: Krishna Kondaka <krishkdk@amazon.com>
1 parent 0e0e7ee commit a280b6f

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/codec/OutputCodec.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.io.IOException;
1717
import java.io.OutputStream;
18+
import java.io.ByteArrayOutputStream;
1819
import java.util.Map;
1920

2021
public interface OutputCodec {
@@ -28,7 +29,7 @@ public interface OutputCodec {
2829
* @param outputStream outputStream param for wrapping
2930
* @param event Event to auto-generate schema
3031
* @param context Extra Context used in Codec.
31-
* @throws IOException throws IOException when invalid input is received or not able to create wrapping
32+
* @throws IOException throws IOException when invalid input is received or not able to create wrapping
3233
*/
3334
void start(OutputStream outputStream, Event event, OutputCodecContext context) throws IOException;
3435

@@ -51,6 +52,21 @@ public interface OutputCodec {
5152
*/
5253
void complete(OutputStream outputStream) throws IOException;
5354

55+
/**
56+
* this method get called from {@link Sink} to estimate size of event in {@link OutputStream}
57+
*
58+
* @param event event Record event
59+
* @return long size of the serialized event
60+
* @throws IOException throws IOException when invalid input is received or not able to create wrapping
61+
*/
62+
default long getEstimatedSize(Event event, OutputCodecContext codecContext) throws IOException {
63+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
64+
start(outputStream, event, codecContext);
65+
writeEvent(event, outputStream);
66+
complete(outputStream);
67+
return outputStream.toByteArray().length;
68+
}
69+
5470
/**
5571
* used to get extension of file
5672
*

data-prepper-api/src/test/java/org/opensearch/dataprepper/model/codec/OutputCodecTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void validateAgainstCodecContext_does_not_throw_or_interact_with_outputCodecCont
4545
}
4646

4747
@Test
48-
public void testWriteMetrics() throws JsonProcessingException {
48+
public void testWriteMetrics() throws JsonProcessingException, IOException {
4949
OutputCodec outputCodec = new OutputCodec() {
5050
@Override
5151
public void start(OutputStream outputStream, Event event, final OutputCodecContext codecContext) throws IOException {
@@ -73,6 +73,7 @@ public String getExtension() {
7373
final JacksonEvent event = JacksonLog.builder().withData(json).withEventMetadata(defaultEventMetadata).build();
7474
Event tagsToEvent = outputCodec.addTagsToEvent(event, "Tag");
7575
assertNotEquals(event.toJsonString(), tagsToEvent.toJsonString());
76+
assertThat(outputCodec.getEstimatedSize(event, new OutputCodecContext()), equalTo(0L));
7677
}
7778

7879
private static Map<String, Object> generateJson() {

data-prepper-plugins/parse-json-processor/src/test/java/org/opensearch/dataprepper/plugins/codec/json/JsonOutputCodecTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Set;
2525

2626
import static org.hamcrest.CoreMatchers.equalTo;
27+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
2728
import static org.hamcrest.CoreMatchers.notNullValue;
2829
import static org.hamcrest.MatcherAssert.assertThat;
2930

@@ -147,6 +148,19 @@ void writeEvent_with_exclude_keys(final int numberOfRecords) throws IOException
147148
assertThat(index, equalTo(numberOfRecords));
148149
}
149150

151+
@Test
152+
void testGetEstimatedSize() throws Exception {
153+
int numberOfRecords = 1;
154+
JsonOutputCodec jsonOutputCodec = createObjectUnderTest();
155+
outputStream = new ByteArrayOutputStream();
156+
OutputCodecContext codecContext = new OutputCodecContext();
157+
final List<Map<String, Object>> expectedData = generateRecords(numberOfRecords);
158+
final Event event = convertToEvent(expectedData.get(0));
159+
jsonOutputCodec.start(outputStream, null, codecContext);
160+
String expectedEventString = "{\"events\":[{\"name\":\"Person0\",\"age\":0}]";
161+
assertThat(jsonOutputCodec.getEstimatedSize(event, codecContext), greaterThanOrEqualTo((long)(expectedEventString.length())));
162+
}
163+
150164
@Test
151165
void testGetExtension() {
152166
JsonOutputCodec jsonOutputCodec = createObjectUnderTest();
@@ -185,4 +199,4 @@ private Object getValue(JsonNode jsonNode) {
185199

186200
throw new RuntimeException("Test not setup correctly.");
187201
}
188-
}
202+
}

data-prepper-plugins/parse-json-processor/src/test/java/org/opensearch/dataprepper/plugins/codec/json/NewlineDelimitedOutputCodecTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,17 @@ void testGetExtension() {
8989

9090
assertThat("ndjson", equalTo(ndjsonOutputCodec.getExtension()));
9191
}
92-
}
92+
93+
@Test
94+
void testGetEstimatedSize() throws Exception {
95+
NewlineDelimitedOutputCodecTest.numberOfRecords = 1;
96+
NdjsonOutputCodec ndjsonOutputCodec = createObjectUnderTest();
97+
outputStream = new ByteArrayOutputStream();
98+
OutputCodecContext codecContext = new OutputCodecContext();
99+
ndjsonOutputCodec.start(outputStream, null, codecContext);
100+
101+
Record<Event> record = getRecord(0);
102+
String expectedEventString = "{\"name\":\"Person0\",\"age\":0}\n";
103+
assertThat(ndjsonOutputCodec.getEstimatedSize(record.getData(), codecContext), equalTo((long)expectedEventString.length()));
104+
}
105+
}

0 commit comments

Comments
 (0)