jankotek/JDBM3 — pull request #94 by joval (2012-09-04 14:03:38+00:00)
Title: Serialization optimization for UUID keys.

Hi Jan,

This is a change from a user who follows my clone of JDBM. I think it looks like a good change; you might want to consider merging it yourself.

Here is the diff ignoring whitespace changes:

diff --git a/src/main/java/org/apache/jdbm/Serialization.java b/src/main/java/org/apache/jdbm/Serialization.java
index b8fccce..0e1d56a 100644
--- a/src/main/java/org/apache/jdbm/Serialization.java
+++ b/src/main/java/org/apache/jdbm/Serialization.java
@@ -263,6 +263,10 @@ public class Serialization extends SerialClassInfo implements Serializer {
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);
@@ -407,6 +411,12 @@ public class Serialization extends SerialClassInfo implements Serializer {

}

+ 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 @@ public class Serialization extends SerialClassInfo implements Serializer {
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 class Serialization extends SerialClassInfo implements Serializer {
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 @@ public class Serialization extends SerialClassInfo implements Serializer {
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)
diff --git a/src/main/java/org/apache/jdbm/SerializationHeader.java b/src/main/java/org/apache/jdbm/SerializationHeader.java
index e3ec45c..8e86836 100644
--- a/src/main/java/org/apache/jdbm/SerializationHeader.java
+++ b/src/main/java/org/apache/jdbm/SerializationHeader.java
@@ -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;
diff --git a/src/test/java/org/apache/jdbm/SerializationTest.java b/src/test/java/org/apache/jdbm/SerializationTest.java
index c61f743..57a58e7 100644
--- a/src/test/java/org/apache/jdbm/SerializationTest.java
+++ b/src/test/java/org/apache/jdbm/SerializationTest.java
@@ -399,6 +399,18 @@ public class SerializationTest extends TestCase {
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)));