Skip to content

Commit

Permalink
add newBinaryDecoder which re-configures a BinaryDecoder in AvroCompa…
Browse files Browse the repository at this point in the history
…tibilityHelper (#52)

Venice has a local optimization requires re-initialize a BinaryDecoder, but the method signature is different in avro 1.4 and versions after 1.4. In order to be compatible with all avro versions, we need to add this method in AvroCompatibilityHelper.

Co-authored-by: xnma <[email protected]>
  • Loading branch information
xinxinzhenhuai and xnma authored May 27, 2020
1 parent 0d79aa6 commit 10bef2f
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public interface AvroAdapter {

BinaryDecoder newBinaryDecoder(ObjectInput in);

BinaryDecoder newBinaryDecoder(byte[] bytes, int offset,
int length, BinaryDecoder reuse);

JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException;

JsonDecoder newJsonDecoder(Schema schema, InputStream in) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ public static BinaryDecoder newBinaryDecoder(InputStream in, boolean buffered, B
return ADAPTER.newBinaryDecoder(in, buffered, reuse);
}

/**
* constructs or reinitializes a {@link BinaryDecoder} with the byte array
* provided as the source of data.
* @param bytes The byte array to initialize to
* @param offset The offset to start reading from
* @param length The maximum number of bytes to read from the byte array
* @param reuse The BinaryDecoder to attempt to reinitialize.
* @return A BinaryDecoder that uses <i>bytes</i> as its source of data. If
* <i>reuse</i> is null, this will be a new instance.
*/
public static BinaryDecoder newBinaryDecoder(byte[] bytes, int offset,
int length, BinaryDecoder reuse) {
assertAvroAvailable();
return ADAPTER.newBinaryDecoder(bytes, offset, length, reuse);
}

/**
* convenience method for getting a {@link BinaryDecoder} for a given byte[]
* @param in byte array with data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.io.Avro14BinaryDecoderAccessUtil;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DecoderFactory;
Expand Down Expand Up @@ -106,6 +107,11 @@ public BinaryDecoder newBinaryDecoder(ObjectInput in) {
return newBinaryDecoder(new ObjectInputToInputStreamAdapter(in), false, null);
}

@Override
public BinaryDecoder newBinaryDecoder(byte[] bytes, int offset, int length, BinaryDecoder reuse) {
return Avro14BinaryDecoderAccessUtil.newBinaryDecoder(bytes, offset, length, reuse);
}

@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
return new JsonEncoder(schema, out);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2020 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").
* See License in the project root for license information.
*/

package org.apache.avro.io;

/**
* this class exists to allow us access to package-private classes and methods on class {@link BinaryDecoder}
*
* the difference between this method and {@link DecoderFactory#createBinaryDecoder(byte[], int, int, BinaryDecoder)}
* is that this method supports configuring custom BinaryDecoder since it does not check class type of BinaryDecoder.
*/
public class Avro14BinaryDecoderAccessUtil {
public static BinaryDecoder newBinaryDecoder(byte[] bytes, int offset,
int length, BinaryDecoder reuse) {
if (null != reuse) {
reuse.init(bytes, offset, length);
return reuse;
} else {
return new BinaryDecoder(bytes, offset, length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.stream.Collectors;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.io.Avro15BinaryDecoderAccessUtil;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DecoderFactory;
Expand Down Expand Up @@ -109,6 +110,11 @@ public BinaryDecoder newBinaryDecoder(ObjectInput in) {
return newBinaryDecoder(new ObjectInputToInputStreamAdapter(in), false, null);
}

@Override
public BinaryDecoder newBinaryDecoder(byte[] bytes, int offset, int length, BinaryDecoder reuse) {
return Avro15BinaryDecoderAccessUtil.newBinaryDecoder(bytes, offset, length, reuse);
}

@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
return EncoderFactory.get().jsonEncoder(schema, out);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2020 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").
* See License in the project root for license information.
*/

package org.apache.avro.io;

/**
* this class exists to allow us access to package-private classes and methods on class {@link BinaryDecoder}
*
* the difference between this method and {@link DecoderFactory#binaryDecoder(byte[], int, int, BinaryDecoder)}
* is that this method supports configuring custom BinaryDecoder since it does not check class type of BinaryDecoder.
*/
public class Avro15BinaryDecoderAccessUtil {
public static BinaryDecoder newBinaryDecoder(byte[] bytes, int offset,
int length, BinaryDecoder reuse) {
if (null == reuse) {
return new BinaryDecoder(bytes, offset, length);
} else {
return reuse.configure(bytes, offset, length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.stream.Collectors;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.io.Avro16BinaryDecoderAccessUtil;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DecoderFactory;
Expand Down Expand Up @@ -115,6 +116,11 @@ public BinaryDecoder newBinaryDecoder(ObjectInput in) {
return newBinaryDecoder(new ObjectInputToInputStreamAdapter(in), false, null);
}

@Override
public BinaryDecoder newBinaryDecoder(byte[] bytes, int offset, int length, BinaryDecoder reuse) {
return Avro16BinaryDecoderAccessUtil.newBinaryDecoder(bytes, offset, length, reuse);
}

@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
return EncoderFactory.get().jsonEncoder(schema, out);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2020 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").
* See License in the project root for license information.
*/

package org.apache.avro.io;

/**
* this class exists to allow us access to package-private classes and methods on class {@link BinaryDecoder}
*
* the difference between this method and {@link DecoderFactory#binaryDecoder(byte[], int, int, BinaryDecoder)}
* is that this method supports configuring custom BinaryDecoder since it does not check class type of BinaryDecoder.
*/
public class Avro16BinaryDecoderAccessUtil {
public static BinaryDecoder newBinaryDecoder(byte[] bytes, int offset,
int length, BinaryDecoder reuse) {
if (null == reuse) {
return new BinaryDecoder(bytes, offset, length);
} else {
return reuse.configure(bytes, offset, length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.avro.Schema;
import org.apache.avro.SchemaNormalization;
import org.apache.avro.generic.GenericData;
import org.apache.avro.io.Avro17BinaryDecoderAccessUtil;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DecoderFactory;
Expand Down Expand Up @@ -158,6 +159,11 @@ public BinaryDecoder newBinaryDecoder(ObjectInput in) {
return newBinaryDecoder(new ObjectInputToInputStreamAdapter(in), false, null);
}

@Override
public BinaryDecoder newBinaryDecoder(byte[] bytes, int offset, int length, BinaryDecoder reuse) {
return Avro17BinaryDecoderAccessUtil.newBinaryDecoder(bytes, offset, length, reuse);
}

@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
return EncoderFactory.get().jsonEncoder(schema, out, pretty);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2020 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").
* See License in the project root for license information.
*/

package org.apache.avro.io;

/**
* this class exists to allow us access to package-private classes and methods on class {@link BinaryDecoder}
*
* the difference between this method and {@link DecoderFactory#binaryDecoder(byte[], int, int, BinaryDecoder)}
* is that this method supports configuring custom BinaryDecoder since it does not check class type of BinaryDecoder.
*/
public class Avro17BinaryDecoderAccessUtil {
public static BinaryDecoder newBinaryDecoder(byte[] bytes, int offset,
int length, BinaryDecoder reuse) {
if (null == reuse) {
return new BinaryDecoder(bytes, offset, length);
} else {
return reuse.configure(bytes, offset, length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.avro.Schema;
import org.apache.avro.SchemaNormalization;
import org.apache.avro.generic.GenericData;
import org.apache.avro.io.Avro18BinaryDecoderAccessUtil;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DecoderFactory;
Expand Down Expand Up @@ -117,6 +118,11 @@ public BinaryDecoder newBinaryDecoder(ObjectInput in) {
return newBinaryDecoder(new ObjectInputToInputStreamAdapter(in), false, null);
}

@Override
public BinaryDecoder newBinaryDecoder(byte[] bytes, int offset, int length, BinaryDecoder reuse) {
return Avro18BinaryDecoderAccessUtil.newBinaryDecoder(bytes, offset, length, reuse);
}

@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
return EncoderFactory.get().jsonEncoder(schema, out, pretty);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2020 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").
* See License in the project root for license information.
*/

package org.apache.avro.io;

/**
* this class exists to allow us access to package-private classes and methods on class {@link BinaryDecoder}
*
* the difference between this method and {@link DecoderFactory#binaryDecoder(byte[], int, int, BinaryDecoder)}
* is that this method supports configuring custom BinaryDecoder since it does not check class type of BinaryDecoder.
*/
public class Avro18BinaryDecoderAccessUtil {
public static BinaryDecoder newBinaryDecoder(byte[] bytes, int offset,
int length, BinaryDecoder reuse) {
if (null == reuse) {
return new BinaryDecoder(bytes, offset, length);
} else {
return reuse.configure(bytes, offset, length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.avro.Schema;
import org.apache.avro.SchemaNormalization;
import org.apache.avro.generic.GenericData;
import org.apache.avro.io.Avro19BinaryDecoderAccessUtil;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DecoderFactory;
Expand Down Expand Up @@ -117,6 +118,11 @@ public BinaryDecoder newBinaryDecoder(ObjectInput in) {
return newBinaryDecoder(new ObjectInputToInputStreamAdapter(in), false, null);
}

@Override
public BinaryDecoder newBinaryDecoder(byte[] bytes, int offset, int length, BinaryDecoder reuse) {
return Avro19BinaryDecoderAccessUtil.newBinaryDecoder(bytes, offset, length, reuse);
}

@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
return EncoderFactory.get().jsonEncoder(schema, out, pretty);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2020 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").
* See License in the project root for license information.
*/

package org.apache.avro.io;

/**
* this class exists to allow us access to package-private classes and methods on class {@link BinaryDecoder}
*
* the difference between this method and {@link DecoderFactory#binaryDecoder(byte[], int, int, BinaryDecoder)}
* is that this method supports configuring custom BinaryDecoder since it does not check class type of BinaryDecoder.
*/
public class Avro19BinaryDecoderAccessUtil {
public static BinaryDecoder newBinaryDecoder(byte[] bytes, int offset,
int length, BinaryDecoder reuse) {
if (null == reuse) {
return new BinaryDecoder(bytes, offset, length);
} else {
return reuse.configure(bytes, offset, length);
}
}
}

0 comments on commit 10bef2f

Please sign in to comment.