Author: gdusbabek Date: Tue Aug 3 17:20:11 2010 New Revision: 981976 URL: http://svn.apache.org/viewvc?rev=981976&view=rev Log: specify schema used to read serialized migrations. patch by stuhood, reviewed by gdusbabek. CASSANDRA-1351
Modified: cassandra/trunk/src/java/org/apache/cassandra/db/DefsTable.java cassandra/trunk/src/java/org/apache/cassandra/db/migration/Migration.java cassandra/trunk/src/java/org/apache/cassandra/io/SerDeUtils.java cassandra/trunk/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java Modified: cassandra/trunk/src/java/org/apache/cassandra/db/DefsTable.java URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/DefsTable.java?rev=981976&r1=981975&r2=981976&view=diff ============================================================================== --- cassandra/trunk/src/java/org/apache/cassandra/db/DefsTable.java (original) +++ cassandra/trunk/src/java/org/apache/cassandra/db/DefsTable.java Tue Aug 3 17:20:11 2010 @@ -106,7 +106,7 @@ public class DefsTable { if (Arrays.equals(column.name(), DEFINITION_SCHEMA_COLUMN_NAME)) continue; - org.apache.cassandra.config.avro.KsDef ks = SerDeUtils.<org.apache.cassandra.config.avro.KsDef>deserialize(schema, column.value()); + org.apache.cassandra.config.avro.KsDef ks = SerDeUtils.deserialize(schema, column.value(), new org.apache.cassandra.config.avro.KsDef()); keyspaces.add(KSMetaData.inflate(ks)); } return keyspaces; Modified: cassandra/trunk/src/java/org/apache/cassandra/db/migration/Migration.java URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/migration/Migration.java?rev=981976&r1=981975&r2=981976&view=diff ============================================================================== --- cassandra/trunk/src/java/org/apache/cassandra/db/migration/Migration.java (original) +++ cassandra/trunk/src/java/org/apache/cassandra/db/migration/Migration.java Tue Aug 3 17:20:11 2010 @@ -279,7 +279,7 @@ public abstract class Migration public static Migration deserialize(byte[] bytes) throws IOException { // deserialize - org.apache.cassandra.db.migration.avro.Migration mi = SerDeUtils.deserializeWithSchema(bytes); + org.apache.cassandra.db.migration.avro.Migration mi = SerDeUtils.deserializeWithSchema(bytes, new org.apache.cassandra.db.migration.avro.Migration()); // create an instance of the migration subclass Migration migration; Modified: cassandra/trunk/src/java/org/apache/cassandra/io/SerDeUtils.java URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/io/SerDeUtils.java?rev=981976&r1=981975&r2=981976&view=diff ============================================================================== --- cassandra/trunk/src/java/org/apache/cassandra/io/SerDeUtils.java (original) +++ cassandra/trunk/src/java/org/apache/cassandra/io/SerDeUtils.java Tue Aug 3 17:20:11 2010 @@ -48,14 +48,17 @@ public final class SerDeUtils /** * Deserializes a single object based on the given Schema. - * @param schema writer's schema + * @param writer writer's schema * @param bytes Array to deserialize from + * @param ob An empty object to deserialize into (must not be null). * @throws IOException */ - public static <T extends SpecificRecord> T deserialize(Schema schema, byte[] bytes) throws IOException + public static <T extends SpecificRecord> T deserialize(Schema writer, byte[] bytes, T ob) throws IOException { BinaryDecoder dec = DIRECT_DECODERS.createBinaryDecoder(bytes, null); - return new SpecificDatumReader<T>(schema).read(null, dec); + SpecificDatumReader<T> reader = new SpecificDatumReader<T>(writer); + reader.setExpected(ob.getSchema()); + return reader.read(ob, dec); } /** @@ -74,14 +77,17 @@ public final class SerDeUtils /** * Deserializes a single object as stored along with its Schema by serialize(T). NB: See warnings on serialize(T). + * @param ob An empty object to deserialize into (must not be null). * @param bytes Array to deserialize from * @throws IOException */ - public static <T extends SpecificRecord> T deserializeWithSchema(byte[] bytes) throws IOException + public static <T extends SpecificRecord> T deserializeWithSchema(byte[] bytes, T ob) throws IOException { BinaryDecoder dec = DIRECT_DECODERS.createBinaryDecoder(bytes, null); - Schema schema = Schema.parse(dec.readString(new Utf8()).toString()); - return new SpecificDatumReader<T>(schema).read(null, dec); + Schema writer = Schema.parse(dec.readString(new Utf8()).toString()); + SpecificDatumReader<T> reader = new SpecificDatumReader<T>(writer); + reader.setExpected(ob.getSchema()); + return new SpecificDatumReader<T>(writer).read(ob, dec); } /** Modified: cassandra/trunk/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java?rev=981976&r1=981975&r2=981976&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/config/DatabaseDescriptorTest.java Tue Aug 3 17:20:11 2010 @@ -22,6 +22,7 @@ import static org.junit.Assert.assertNot import org.apache.avro.specific.SpecificRecord; +import org.apache.cassandra.config.avro.KsDef; import org.apache.cassandra.CleanupHelper; import org.apache.cassandra.db.migration.AddKeyspace; import org.apache.cassandra.locator.RackUnawareStrategy; @@ -35,9 +36,11 @@ import java.util.UUID; public class DatabaseDescriptorTest { - protected <D extends SpecificRecord> D serDe(D record) throws IOException + protected <D extends SpecificRecord> D serDe(D record, D newInstance) throws IOException { - D actual = SerDeUtils.<D>deserialize(record.getSchema(), SerDeUtils.serialize(record)); + D actual = SerDeUtils.deserialize(record.getSchema(), + SerDeUtils.serialize(record), + newInstance); assert actual.equals(record) : actual + " != " + record; return actual; } @@ -56,7 +59,7 @@ public class DatabaseDescriptorTest { for (CFMetaData cfm : DatabaseDescriptor.getTableMetaData(table).values()) { - CFMetaData cfmDupe = CFMetaData.inflate(serDe(cfm.deflate())); + CFMetaData cfmDupe = CFMetaData.inflate(serDe(cfm.deflate(), new org.apache.cassandra.config.avro.CfDef())); assert cfmDupe != null; assert cfmDupe.equals(cfm); } @@ -68,7 +71,7 @@ public class DatabaseDescriptorTest { for (KSMetaData ksm : DatabaseDescriptor.tables.values()) { - KSMetaData ksmDupe = KSMetaData.inflate(serDe(ksm.deflate())); + KSMetaData ksmDupe = KSMetaData.inflate(serDe(ksm.deflate(), new org.apache.cassandra.config.avro.KsDef())); assert ksmDupe != null; assert ksmDupe.equals(ksm); }