Skip to content

Commit

Permalink
Make SerializableConfiguration cacheable
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk92 committed Sep 26, 2023
1 parent d52b077 commit b233609
Showing 1 changed file with 15 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,13 +53,23 @@ 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
Expand Down

0 comments on commit b233609

Please sign in to comment.