-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Java: Add blocking direct binary encoder * Optimize * Comments and more tests * Comments and more tests * Fix rat check
- Loading branch information
Showing
10 changed files
with
937 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
lang/java/avro/src/main/java/org/apache/avro/io/BlockingDirectBinaryEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.avro.io; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* An {@link Encoder} for Avro's binary encoding that does not buffer output. | ||
* <p/> | ||
* This encoder does not buffer writes in contrast to | ||
* {@link BufferedBinaryEncoder}. However, it is lighter-weight and useful when: | ||
* The buffering in BufferedBinaryEncoder is not desired because you buffer a | ||
* different level or the Encoder is very short-lived. | ||
* </p> | ||
* The BlockingDirectBinaryEncoder will encode the number of bytes of the Map | ||
* and Array blocks. This will allow to postpone the decoding, or skip over it | ||
* at all. | ||
* <p/> | ||
* To construct, use | ||
* {@link EncoderFactory#blockingDirectBinaryEncoder(OutputStream, BinaryEncoder)} | ||
* <p/> | ||
* {@link BlockingDirectBinaryEncoder} instances returned by this method are not | ||
* thread-safe | ||
* | ||
* @see BinaryEncoder | ||
* @see EncoderFactory | ||
* @see Encoder | ||
* @see Decoder | ||
*/ | ||
public class BlockingDirectBinaryEncoder extends DirectBinaryEncoder { | ||
private final BufferOutputStream buffer; | ||
|
||
private OutputStream originalStream; | ||
|
||
private boolean inBlock = false; | ||
|
||
private long blockItemCount; | ||
|
||
/** | ||
* Create a writer that sends its output to the underlying stream | ||
* <code>out</code>. | ||
* | ||
* @param out The Outputstream to write to | ||
*/ | ||
public BlockingDirectBinaryEncoder(OutputStream out) { | ||
super(out); | ||
this.buffer = new BufferOutputStream(); | ||
} | ||
|
||
private void startBlock() { | ||
if (inBlock) { | ||
throw new RuntimeException("Nested Maps/Arrays are not supported by the BlockingDirectBinaryEncoder"); | ||
} | ||
originalStream = out; | ||
buffer.reset(); | ||
out = buffer; | ||
inBlock = true; | ||
} | ||
|
||
private void endBlock() { | ||
if (!inBlock) { | ||
throw new RuntimeException("Called endBlock, while not buffering a block"); | ||
} | ||
out = originalStream; | ||
if (blockItemCount > 0) { | ||
try { | ||
// Make it negative, so the reader knows that the number of bytes is coming | ||
writeLong(-blockItemCount); | ||
writeLong(buffer.size()); | ||
writeFixed(buffer.toBufferWithoutCopy()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
inBlock = false; | ||
buffer.reset(); | ||
} | ||
|
||
@Override | ||
public void setItemCount(long itemCount) throws IOException { | ||
blockItemCount = itemCount; | ||
} | ||
|
||
@Override | ||
public void writeArrayStart() throws IOException { | ||
startBlock(); | ||
} | ||
|
||
@Override | ||
public void writeArrayEnd() throws IOException { | ||
endBlock(); | ||
// Writes another zero to indicate that this is the last block | ||
super.writeArrayEnd(); | ||
} | ||
|
||
@Override | ||
public void writeMapStart() throws IOException { | ||
startBlock(); | ||
} | ||
|
||
@Override | ||
public void writeMapEnd() throws IOException { | ||
endBlock(); | ||
// Writes another zero to indicate that this is the last block | ||
super.writeMapEnd(); | ||
} | ||
|
||
private static class BufferOutputStream extends ByteArrayOutputStream { | ||
BufferOutputStream() { | ||
} | ||
|
||
ByteBuffer toBufferWithoutCopy() { | ||
return ByteBuffer.wrap(buf, 0, count); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
lang/java/avro/src/test/java/org/apache/avro/io/TestBlockingDirectBinaryEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.avro.io; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.SchemaNormalization; | ||
import org.apache.avro.generic.GenericDatumReader; | ||
import org.apache.avro.message.BinaryMessageDecoder; | ||
import org.apache.avro.specific.TestRecordWithMapsAndArrays; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class TestBlockingDirectBinaryEncoder { | ||
|
||
@Test | ||
void blockingDirectBinaryEncoder() throws IOException, NoSuchAlgorithmException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
BinaryEncoder encoder = EncoderFactory.get().blockingDirectBinaryEncoder(baos, null); | ||
|
||
// This is needed because there is no BlockingDirectBinaryEncoder | ||
// BinaryMessageWriter | ||
// available out of the box | ||
encoder.writeFixed(new byte[] { (byte) 0xC3, (byte) 0x01 }); | ||
encoder.writeFixed(SchemaNormalization.parsingFingerprint("CRC-64-AVRO", TestRecordWithMapsAndArrays.SCHEMA$)); | ||
|
||
int len = 5; | ||
|
||
encoder.writeArrayStart(); | ||
encoder.setItemCount(len); | ||
for (int i = 0; i < len; i++) { | ||
encoder.startItem(); | ||
encoder.writeString(Integer.toString(i)); | ||
} | ||
encoder.writeArrayEnd(); | ||
|
||
encoder.writeMapStart(); | ||
encoder.setItemCount(len); | ||
for (long i = 0; i < len; i++) { | ||
encoder.startItem(); | ||
encoder.writeString(Long.toString(i)); | ||
encoder.writeLong(i); | ||
} | ||
encoder.writeMapEnd(); | ||
encoder.flush(); | ||
|
||
BinaryMessageDecoder<TestRecordWithMapsAndArrays> decoder = TestRecordWithMapsAndArrays.getDecoder(); | ||
TestRecordWithMapsAndArrays r = decoder.decode(baos.toByteArray()); | ||
|
||
assertThat(r.getArr(), is(Arrays.asList("0", "1", "2", "3", "4"))); | ||
Map<String, Long> map = r.getMap(); | ||
assertThat(map.size(), is(5)); | ||
for (long i = 0; i < len; i++) { | ||
assertThat(map.get(Long.toString(i)), is(i)); | ||
} | ||
} | ||
|
||
@Test | ||
void testSkippingUsingBlocks() throws IOException, NoSuchAlgorithmException { | ||
// Create an empty schema for read, so we skip over all the fields | ||
Schema emptySchema = new Schema.Parser().parse( | ||
"{\"type\":\"record\",\"name\":\"TestRecordWithMapsAndArrays\",\"namespace\":\"org.apache.avro.specific\",\"fields\":[]}"); | ||
|
||
GenericDatumReader<?> in = new GenericDatumReader<>(TestRecordWithMapsAndArrays.SCHEMA$, emptySchema); | ||
Decoder mockDecoder = mock(BinaryDecoder.class); | ||
|
||
for (long i = 0; i < 1; i++) { | ||
in.read(null, mockDecoder); | ||
} | ||
|
||
verify(mockDecoder, times(1)).skipMap(); | ||
verify(mockDecoder, times(1)).skipArray(); | ||
verify(mockDecoder, times(0)).readString(); | ||
verify(mockDecoder, times(0)).readLong(); | ||
} | ||
} |
Oops, something went wrong.