Skip to content
Closed
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
Next Next commit
fix serialization of UTF8String across different JVM
  • Loading branch information
Davies Liu committed Aug 14, 2015
commit ebeb6977d6731388477bbf010b14d6321fe9f7bc
25 changes: 20 additions & 5 deletions unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
package org.apache.spark.unsafe.types;

import javax.annotation.Nonnull;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Map;
Expand All @@ -41,9 +40,9 @@
public final class UTF8String implements Comparable<UTF8String>, Serializable {

@Nonnull
private final Object base;
private final long offset;
private final int numBytes;
private Object base;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to still having them as final?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if you can use Externalizable instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Externalizable still need them to be non-final. Should we use UNSAFE here? Not sure about the performance difference about final and non-final.

private long offset;
private int numBytes;

public Object getBaseObject() { return base; }
public long getBaseOffset() { return offset; }
Expand Down Expand Up @@ -978,4 +977,20 @@ public UTF8String soundex() {
}
return UTF8String.fromBytes(sx);
}

private static final long serialVersionUID = 42L;

private void writeObject(ObjectOutputStream out) throws IOException {
byte[] bytes = getBytes();
out.writeInt(bytes.length);
out.write(bytes);
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
offset = BYTE_ARRAY_OFFSET;
numBytes = in.readInt();
base = new byte[numBytes];
in.readFully((byte[]) base);
}

}