Skip to content

Commit

Permalink
Added Optimized Serialization for UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
cshingle committed Sep 3, 2012
1 parent cd825e2 commit 2a4fc6b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/main/java/org/apache/jdbm/Serialization.java
Expand Up @@ -263,6 +263,10 @@ else if (val == 1)
out.write(DATE);
out.writeLong(((Date) obj).getTime());
return;
} else if (clazz == UUID.class) {
out.write(UUID);
serializeUUID(out,(UUID) obj);
return;
} else if (clazz == BTree.class) {
out.write(BTREE);
((BTree) obj).writeExternal(out);
Expand Down Expand Up @@ -406,7 +410,13 @@ static void serializeString(DataOutput out, String obj) throws IOException {
}

}


private void serializeUUID(DataOutput out, UUID uuid) throws IOException
{
out.writeLong(uuid.getMostSignificantBits());
out.writeLong(uuid.getLeastSignificantBits());
}

private void serializeMap(int header, DataOutput out, Object obj, FastArrayList objectStack) throws IOException {
Map l = (Map) obj;
out.write(header);
Expand All @@ -432,7 +442,6 @@ private void serializeByteArrayInt(DataOutput out, byte[] b) throws IOException
out.write(b);
}


private void writeLongArray(DataOutput da, long[] obj) throws IOException {
long max = Long.MIN_VALUE;
long min = Long.MAX_VALUE;
Expand Down Expand Up @@ -824,6 +833,9 @@ public Object deserialize(DataInput is, FastArrayList objectStack) throws IOExce
case DATE:
ret = new Date(is.readLong());
break;
case UUID:
ret = deserializeUUID(is);
break;
case ARRAY_INT_B_255:
ret = deserializeArrayIntB255(is);
break;
Expand Down Expand Up @@ -1067,6 +1079,11 @@ private long[] deserializeArrayLongPack(DataInput is) throws IOException {
}
return ret;
}

private UUID deserializeUUID(DataInput is) throws IOException
{
return new UUID(is.readLong(), is.readLong());
}

private int[] deserializeArrayIntB255(DataInput is) throws IOException {
int size = is.readUnsignedByte();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/apache/jdbm/SerializationHeader.java
Expand Up @@ -120,6 +120,7 @@ final class SerializationHeader {

final static int CLASS = 126;
final static int DATE = 127;
final static int UUID = 128;


static final int JDBMLINKEDLIST = 159;
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/apache/jdbm/SerializationTest.java
Expand Up @@ -398,6 +398,18 @@ public void testBigInteger() throws IOException, ClassNotFoundException {
d = new BigInteger("-535345345345344456567889889895165654423236");
assertEquals(d, ser.deserialize(ser.serialize(d)));
}

public void testUUID() throws IOException, ClassNotFoundException {
//try a bunch of UUIDs.
for(int i = 0; i < 1000;i++)
{
UUID uuid = UUID.randomUUID();
SimpleEntry a = new SimpleEntry(uuid, "11");
byte[] buf = ser.serialize(a);
SimpleEntry b = (SimpleEntry) ser.deserialize(buf);
assertEquals(b, a);
}
}


public void testLocale() throws Exception{
Expand Down

0 comments on commit 2a4fc6b

Please sign in to comment.