Skip to content

Commit

Permalink
Make SerializableConfiguration cacheable (#28590)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk92 authored Sep 27, 2023
1 parent 725a2d6 commit f35a413
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.beam.sdk.io.hadoop;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
Expand All @@ -39,6 +41,8 @@ public class SerializableConfiguration implements Externalizable {

private transient Configuration conf;

private transient byte[] serializationCache;

public SerializableConfiguration() {}

public SerializableConfiguration(Configuration conf) {
Expand All @@ -49,17 +53,30 @@ public SerializableConfiguration(Configuration conf) {
}

public Configuration get() {
if (serializationCache != null) {
serializationCache = null;
}
return conf;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
if (serializationCache == null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
try (DataOutputStream dos = new DataOutputStream(baos)) {
conf.write(dos);
serializationCache = baos.toByteArray();
}
}
out.writeUTF(conf.getClass().getCanonicalName());
conf.write(out);
out.write(serializationCache);
}

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
if (serializationCache != null) {
serializationCache = null;
}
String className = in.readUTF();
try {
conf =
Expand Down

0 comments on commit f35a413

Please sign in to comment.