Skip to content

Commit

Permalink
Use Schema.hashCode() instead of Schema.toString().hashCode()
Browse files Browse the repository at this point in the history
Adding a unit test to make sure Schema.hashCode() works like we expect it to.
Namely, want to make sure it ignores the "doc" field
  • Loading branch information
mumrah committed Sep 12, 2011
1 parent 375221e commit a480588
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import java.util.Map;

import org.apache.avro.Schema;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.Encoder;

import voldemort.serialization.SerializationException;
import voldemort.serialization.Serializer;
Expand Down Expand Up @@ -58,7 +59,7 @@ public byte[] toBytes(T object) {
byte[] versionBytes = ByteBuffer.allocate(4).putInt(currentVersion).array();
out.write(versionBytes);
// Write the serialized Avro object as the remaining bytes
BinaryEncoder encoder = new BinaryEncoder(out);
Encoder encoder = new BinaryEncoder(out);
writer.write(object, encoder);
encoder.flush();
out.close();
Expand All @@ -84,15 +85,15 @@ public T toObject(byte[] bytes) {

// Read the bytes into T object
DatumReader<T> datumReader = readers.get(version);
BinaryDecoder decoder = decoderFactory.createBinaryDecoder(b, null);
Decoder decoder = decoderFactory.createBinaryDecoder(b, null);
return datumReader.read(null, decoder);
} catch(Exception e) {
throw new RuntimeException(e);
}
}

public static Integer getSchemaVersion(Schema schema) {
return schema.toString().hashCode();
return schema.hashCode();
}

public static Integer getSchemaVersion(String schemaAsString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ public class AvroResolvingSpecificSerializerTest extends TestCase {
String SCHEMA2NS = VoldemortTestConstants.getTestSpecificRecordSchemaWithNamespace2();
String SCHEMA = TestRecord.SCHEMA$.toString();

public void testSchemaHashCode() {
// Check various things that affect hashCode
// Base case
assertEquals(Schema.parse(SCHEMA1).hashCode(), Schema.parse(SCHEMA1).hashCode());
// Check docs
Schema s1 = Schema.parse("{\"type\":\"record\",\"name\":\"TestRecord\",\"fields\":[{\"name\":\"f1\",\"type\":\"string\"}]}");
Schema s2 = Schema.parse("{\"type\":\"record\",\"name\":\"TestRecord\",\"fields\":[{\"name\":\"f1\",\"doc\":\"field docs\",\"type\":\"string\"}]}");
assertEquals(s1.hashCode(), s2.hashCode());
// Check field order
s1 = Schema.parse("{\"type\":\"record\",\"name\":\"TestRecord\",\"fields\":[{\"name\":\"f1\",\"type\":\"string\"},{\"name\":\"f2\",\"type\":\"string\"}]}");
s2 = Schema.parse("{\"type\":\"record\",\"name\":\"TestRecord\",\"fields\":[{\"name\":\"f2\",\"type\":\"string\"},{\"name\":\"f1\",\"type\":\"string\"}]}");
assertNotSame(s1.hashCode(), s2.hashCode());
// Check namespace
assertNotSame(Schema.parse(SCHEMA1).hashCode(), Schema.parse(SCHEMA1NS).hashCode());
// Different fields
assertNotSame(Schema.parse(SCHEMA1).hashCode(), Schema.parse(SCHEMA2).hashCode());
// Field types
s1 = Schema.parse("{\"type\":\"record\",\"name\":\"TestRecord\",\"fields\":[{\"name\":\"f1\",\"type\":\"string\"}]}");
s2 = Schema.parse("{\"type\":\"record\",\"name\":\"TestRecord\",\"fields\":[{\"name\":\"f1\",\"type\":\"int\"}]}");
assertNotSame(s1.hashCode(), s2.hashCode());
}

public void testSingleInvalidSchemaInfo() {
SerializerDefinition serializerDef = new SerializerDefinition("test", "null");
try {
Expand Down

0 comments on commit a480588

Please sign in to comment.