Permalink
Browse files

Added Serialization optimization for UUID

  • Loading branch information...
1 parent f5fdf17 commit 5c6ee3002b274f91832fd1aace55e4d95766ba4d @jankotek committed Sep 4, 2012
@@ -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);
@@ -312,14 +316,14 @@ else if (val == 1)
} else {
out.write(ARRAY_OBJECT);
LongPacker.packInt(out, b.length);
-
+
// Write class id for components
Class<?> componentType = obj.getClass().getComponentType();
registerClass(componentType);
//write class header
int classId = getClassId(componentType);
LongPacker.packInt(out, classId);
-
+
for (Object o : b)
serialize(out, o, objectStack);
@@ -407,6 +411,12 @@ 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);
@@ -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;
@@ -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;
@@ -1068,6 +1080,11 @@ private Class deserializeClass(DataInput is) throws IOException, ClassNotFoundEx
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();
if (size < 0)
@@ -1089,7 +1106,7 @@ private Class deserializeClass(DataInput is) throws IOException, ClassNotFoundEx
int classId = LongPacker.unpackInt(is);
Class clazz = classId2class.get(classId);
if (clazz == null) clazz = Object.class;
-
+
Object[] s = (Object[])Array.newInstance(clazz, size);
objectStack.add(s);
for (int i = 0; i < size; i++)
@@ -1310,4 +1327,4 @@ int identityIndexOf(Object obj) {
}
-}
+}
@@ -120,6 +120,7 @@
final static int CLASS = 126;
final static int DATE = 127;
+ final static int UUID = 128;
static final int JDBMLINKEDLIST = 159;
@@ -138,4 +139,4 @@
static final int JAVA_SERIALIZATION = 172;
-}
+}
@@ -330,24 +330,24 @@ public void testPackedLongCollection() throws ClassNotFoundException, IOExceptio
}
public void testNegativeLongsArray() throws ClassNotFoundException, IOException {
- long[] l = new long[] { -12 };
- Object deserialize = ser.deserialize(ser.serialize(l));
- assertTrue(Arrays.equals(l, (long[]) deserialize));
- }
+ long[] l = new long[] { -12 };
+ Object deserialize = ser.deserialize(ser.serialize(l));
+ assertTrue(Arrays.equals(l, (long[]) deserialize));
+ }
public void testNegativeIntArray() throws ClassNotFoundException, IOException {
- int[] l = new int[] { -12 };
- Object deserialize = ser.deserialize(ser.serialize(l));
- assertTrue(Arrays.equals(l, (int[]) deserialize));
- }
+ int[] l = new int[] { -12 };
+ Object deserialize = ser.deserialize(ser.serialize(l));
+ assertTrue(Arrays.equals(l, (int[]) deserialize));
+ }
public void testNegativeShortArray() throws ClassNotFoundException, IOException {
- short[] l = new short[] { -12 };
- Object deserialize = ser.deserialize(ser.serialize(l));
+ short[] l = new short[] { -12 };
+ Object deserialize = ser.deserialize(ser.serialize(l));
assertTrue(Arrays.equals(l, (short[]) deserialize));
- }
+ }
public void testBooleanArray() throws ClassNotFoundException, IOException {
boolean[] l = new boolean[] { true,false };
@@ -401,6 +401,18 @@ public void testBigInteger() throws IOException, ClassNotFoundException {
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{
assertEquals(Locale.FRANCE, ser.deserialize(ser.serialize(Locale.FRANCE)));
@@ -469,4 +481,4 @@ public void testObjectArrayArray() throws IOException, ClassNotFoundException {
assertEquals(asList(arr[i]), asList(arr2[i]));
}
-}
+}

0 comments on commit 5c6ee30

Please sign in to comment.