private static boolean isMostRecentVersion(byte version) { return SchemaVersionUtils.decodeAvroSchemaVersion(version) == RowSchemaInfo.VER_CURRENT; }
@Override public Schema findSchema(final int version) { final Schema[] container = getSchemaContainer(); checkArgument(version >= 0 && version < container.length); return container[version]; } }
/** * Deserialize the provided serialized row buffer to a new {@link Row} instance * * @param serializedRow byte buffer containing serialized Row * @return new Row instance from serializedRow */ public static Row deserialize(byte[] serializedRow) { checkNotNull(serializedRow); checkArgument(serializedRow.length > 0); SchemaVersionUtils.processSchemaVersion(serializedRow[0], RowSchemaInfo.VER_CURRENT); return new Row(Util.deserializeAvroObject(serializedRow, reader)); }
@Test(expected = IllegalArgumentException.class) public void testFindSchemaNegativeSchemaVersion() { tableSchemaInfo.findSchema(-1); }
@Test(expected = IllegalArgumentException.class) public void testFindSchemaNegativeSchemaVersion() { rowSchemaInfo.findSchema(-1); }
@Before public void setupTestCases() { tableSchemaInfo = new TableSchemaInfo(); }
@Before public void setupTestCases() { rowSchemaInfo = new RowSchemaInfo(); }
@Test(expected = IllegalArgumentException.class) public void testFindSchemaInvalidSchemaVersion() { final BaseSchemaInfo baseInfo = Mockito.mock(BaseSchemaInfo.class, Mockito.CALLS_REAL_METHODS); baseInfo.findSchema(0); } }
/** * Processes the provided version byte to ensure that it represents a supported * schema version for a serialized container type. * * @param versionByte The byte representation of the encoded version * @param supportedVersion The currently supported schema version, non-negative * @return True if and only if the processed schema version matches the supported schema version * @throws UnknownSchemaVersionException Thrown if the processed schema version is not supported */ public static boolean processSchemaVersion(final byte versionByte, final int supportedVersion) { checkArgument(supportedVersion >= 0, "The supported schema version is invalid"); final int writerSchemaVersion = decodeAvroSchemaVersion(versionByte); if( writerSchemaVersion != supportedVersion ) { logger.warn(format("Cannot process unknown schema version %d, expected: %d ", writerSchemaVersion, supportedVersion)); throw new UnknownSchemaVersionException(writerSchemaVersion, supportedVersion); } return true; }
/** * Deserialize a byte array into a table schema. * * @param serializedTableSchema Byte array of the table schema [Not null] * @return An table schema for the byte array. */ public static TableSchema deserialize(byte[] serializedTableSchema) { checkNotNull(serializedTableSchema); checkArgument(serializedTableSchema.length > 0); SchemaVersionUtils.processSchemaVersion(serializedTableSchema[0], TableSchemaInfo.VER_CURRENT); return new TableSchema(Util.deserializeAvroObject(serializedTableSchema, reader)); }
@Test(expected = IllegalArgumentException.class) public void testFindSchemaExceedAvailableVersions() { tableSchemaInfo.findSchema(Integer.MAX_VALUE); }
@Test(expected = IllegalArgumentException.class) public void testFindSchemaExceedAvailableVersions() { rowSchemaInfo.findSchema(Integer.MAX_VALUE); }
@Test(expected = IllegalArgumentException.class) public void testDecodeAvroSchemaVersionOddEncodedValue() { SchemaVersionUtils.decodeAvroSchemaVersion((byte)0x01); } }
@Test(expected = UnknownSchemaVersionException.class) public void testProcessSchemaVersionUnsupportedVersion() { SchemaVersionUtils.processSchemaVersion((byte)0x02, 0); }
@Test public void testFindSchemaValidSchemaVersion() { assertNotNull(tableSchemaInfo.findSchema(0)); } }
@Test public void testFindSchemaValidSchemaVersion() { assertNotNull(rowSchemaInfo.findSchema(0)); } }
@Test(expected = IllegalArgumentException.class) public void testDecodeAvroSchemaVersionExceedMinimumValue() { SchemaVersionUtils.decodeAvroSchemaVersion((byte)-5); }
@Test(expected = IllegalArgumentException.class) public void testProcessSchemaVersionNegativeSupportedVersion() { SchemaVersionUtils.processSchemaVersion((byte)0x00, -1); }
@Test(expected = IllegalArgumentException.class) public void testDecodeAvroSchemaVersionExceedMaximumValue() { SchemaVersionUtils.decodeAvroSchemaVersion((byte)0x7F); }
@Test public void testProcessSchemaVersionSupportedVersion() { assertTrue(SchemaVersionUtils.processSchemaVersion((byte)0x7E, 63)); }