DBZ-7859: reduce enum array allocation

This commit is contained in:
sullis 2024-05-09 11:57:09 -07:00 committed by Jiri Pechanec
parent 5f795fb497
commit 369d95aa0c
4 changed files with 26 additions and 5 deletions

View File

@ -454,6 +454,7 @@ Saulius Valatka
Sayed Mohammad Hossein Torabi Sayed Mohammad Hossein Torabi
Scofield Xu Scofield Xu
Sara Fonseca Sara Fonseca
Sean C. Sullivan
Sean Rooney Sean Rooney
Sean Wu Sean Wu
Sebastiaan Knijnenburg Sebastiaan Knijnenburg

View File

@ -8,7 +8,10 @@
import java.time.Instant; import java.time.Instant;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.kafka.connect.data.Field; import org.apache.kafka.connect.data.Field;
import org.apache.kafka.connect.data.Schema; import org.apache.kafka.connect.data.Schema;
@ -32,6 +35,7 @@ public final class Envelope {
* The constants for the values for the {@link FieldName#OPERATION operation} field in the message envelope. * The constants for the values for the {@link FieldName#OPERATION operation} field in the message envelope.
*/ */
public enum Operation { public enum Operation {
/** /**
* The operation that read the current state of a record, most typically during snapshots. * The operation that read the current state of a record, most typically during snapshots.
*/ */
@ -57,6 +61,11 @@ public enum Operation {
*/ */
MESSAGE("m"); MESSAGE("m");
// Enum .values() returns a new array upon each invocation
// Reference: https://www.gamlor.info/wordpress/2017/08/javas-enum-values-hidden-allocations/
private static final Map<String, Operation> CODE_LOOKUP = Stream.of(Operation.values()).collect(
Collectors.toMap(Operation::code, op -> op));
private final String code; private final String code;
Operation(String code) { Operation(String code) {
@ -64,13 +73,11 @@ public enum Operation {
} }
public static Operation forCode(String code) { public static Operation forCode(String code) {
for (Operation op : Operation.values()) { if (code == null) {
if (op.code().equalsIgnoreCase(code)) {
return op;
}
}
return null; return null;
} }
return CODE_LOOKUP.get(code.toLowerCase());
}
public String code() { public String code() {
return code; return code;

View File

@ -57,6 +57,18 @@ public void shouldBuildWithCustomTransactionSchema() {
assertRequiredField(env, Envelope.FieldName.TRANSACTION, SchemaBuilder.STRING_SCHEMA); assertRequiredField(env, Envelope.FieldName.TRANSACTION, SchemaBuilder.STRING_SCHEMA);
} }
@Test
public void envelopeOperationLookupByCode() {
assertThat(Envelope.Operation.forCode(null)).isNull();
assertThat(Envelope.Operation.forCode("")).isNull();
assertThat(Envelope.Operation.forCode("bogus")).isNull();
for (Envelope.Operation operation : Envelope.Operation.values()) {
assertThat(Envelope.Operation.forCode(operation.code().toLowerCase())).isEqualTo(operation);
assertThat(Envelope.Operation.forCode(operation.code().toUpperCase())).isEqualTo(operation);
assertThat(Envelope.Operation.forCode(operation.name())).isNull();
}
}
protected void assertRequiredField(Envelope env, String fieldName, Schema expectedSchema) { protected void assertRequiredField(Envelope env, String fieldName, Schema expectedSchema) {
assertField(env.schema().field(fieldName), fieldName, expectedSchema, false); assertField(env.schema().field(fieldName), fieldName, expectedSchema, false);
} }

View File

@ -265,3 +265,4 @@ zeldanerd24,Kevin Rothenberger
PradeepNain,Pradeep Nain PradeepNain,Pradeep Nain
DLT1412,Duc Le Tu DLT1412,Duc Le Tu
gaurav7261,Gaurav Miglani gaurav7261,Gaurav Miglani
sullis,Sean C. Sullivan