SliceBatchStreamReader.java
/*
* Licensed 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
*
* http://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 com.facebook.presto.orc.reader;
import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.type.CharType;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.common.type.VarbinaryType;
import com.facebook.presto.common.type.VarcharType;
import com.facebook.presto.orc.OrcAggregatedMemoryContext;
import com.facebook.presto.orc.OrcCorruptionException;
import com.facebook.presto.orc.StreamDescriptor;
import com.facebook.presto.orc.Stripe;
import com.facebook.presto.orc.metadata.ColumnEncoding.ColumnEncodingKind;
import com.facebook.presto.orc.stream.InputStreamSources;
import com.google.common.io.Closer;
import io.airlift.slice.Slice;
import org.openjdk.jol.info.ClassLayout;
import java.io.IOException;
import java.io.UncheckedIOException;
import static com.facebook.presto.common.type.Chars.byteCountWithoutTrailingSpace;
import static com.facebook.presto.common.type.Chars.isCharType;
import static com.facebook.presto.common.type.VarbinaryType.isVarbinaryType;
import static com.facebook.presto.common.type.Varchars.byteCount;
import static com.facebook.presto.common.type.Varchars.isVarcharType;
import static com.facebook.presto.orc.metadata.ColumnEncoding.ColumnEncodingKind.DICTIONARY;
import static com.facebook.presto.orc.metadata.ColumnEncoding.ColumnEncodingKind.DICTIONARY_V2;
import static com.facebook.presto.orc.metadata.ColumnEncoding.ColumnEncodingKind.DIRECT;
import static com.facebook.presto.orc.metadata.ColumnEncoding.ColumnEncodingKind.DIRECT_V2;
import static com.facebook.presto.orc.metadata.ColumnEncoding.ColumnEncodingKind.DWRF_DIRECT;
import static com.facebook.presto.orc.reader.ReaderUtils.verifyStreamType;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;
public class SliceBatchStreamReader
implements BatchStreamReader
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(SliceBatchStreamReader.class).instanceSize();
private final StreamDescriptor streamDescriptor;
private final SliceDirectBatchStreamReader directReader;
private final SliceDictionaryBatchStreamReader dictionaryReader;
private BatchStreamReader currentReader;
private final boolean resetAllReaders;
public SliceBatchStreamReader(Type type, StreamDescriptor streamDescriptor, OrcAggregatedMemoryContext systemMemoryContext, long maxSliceSize, boolean resetAllReaders)
throws OrcCorruptionException
{
requireNonNull(type, "type is null");
verifyStreamType(streamDescriptor, type, t -> t instanceof VarcharType || t instanceof CharType || t instanceof VarbinaryType);
this.streamDescriptor = requireNonNull(streamDescriptor, "stream is null");
this.directReader = new SliceDirectBatchStreamReader(streamDescriptor, getMaxCodePointCount(type), isCharType(type), maxSliceSize);
this.dictionaryReader = new SliceDictionaryBatchStreamReader(streamDescriptor, getMaxCodePointCount(type), isCharType(type), systemMemoryContext.newOrcLocalMemoryContext(SliceBatchStreamReader.class.getSimpleName()));
this.resetAllReaders = resetAllReaders;
}
@Override
public Block readBlock()
throws IOException
{
return currentReader.readBlock();
}
@Override
public void prepareNextRead(int batchSize)
{
currentReader.prepareNextRead(batchSize);
}
@Override
public void startStripe(Stripe stripe)
throws IOException
{
ColumnEncodingKind columnEncodingKind = stripe.getColumnEncodings().get(streamDescriptor.getStreamId())
.getColumnEncoding(streamDescriptor.getSequence())
.getColumnEncodingKind();
if (columnEncodingKind == DIRECT || columnEncodingKind == DIRECT_V2 || columnEncodingKind == DWRF_DIRECT) {
currentReader = directReader;
if (dictionaryReader != null && resetAllReaders) {
dictionaryReader.startStripe(stripe);
System.setProperty("RESET_SLICE_BATCH_READER", "RESET_SLICE_BATCH_READER");
}
}
else if (columnEncodingKind == DICTIONARY || columnEncodingKind == DICTIONARY_V2) {
currentReader = dictionaryReader;
if (directReader != null && resetAllReaders) {
directReader.startStripe(stripe);
System.setProperty("RESET_SLICE_BATCH_READER", "RESET_SLICE_BATCH_READER");
}
}
else {
throw new IllegalArgumentException("Unsupported encoding " + columnEncodingKind);
}
currentReader.startStripe(stripe);
}
@Override
public void startRowGroup(InputStreamSources dataStreamSources)
throws IOException
{
currentReader.startRowGroup(dataStreamSources);
}
@Override
public String toString()
{
return toStringHelper(this)
.addValue(streamDescriptor)
.toString();
}
public static int getMaxCodePointCount(Type type)
{
if (isVarcharType(type)) {
VarcharType varcharType = (VarcharType) type;
return varcharType.isUnbounded() ? -1 : varcharType.getLengthSafe();
}
if (isCharType(type)) {
return ((CharType) type).getLength();
}
if (isVarbinaryType(type)) {
return -1;
}
throw new IllegalArgumentException("Unsupported encoding " + type.getDisplayName());
}
public static int computeTruncatedLength(Slice slice, int offset, int length, int maxCodePointCount, boolean isCharType)
{
if (isCharType) {
// truncate the characters and then remove the trailing white spaces
return byteCountWithoutTrailingSpace(slice, offset, length, maxCodePointCount);
}
if (maxCodePointCount >= 0 && length > maxCodePointCount) {
return byteCount(slice, offset, length, maxCodePointCount);
}
return length;
}
@Override
public void close()
{
try (Closer closer = Closer.create()) {
closer.register(directReader::close);
closer.register(dictionaryReader::close);
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public long getRetainedSizeInBytes()
{
return INSTANCE_SIZE + directReader.getRetainedSizeInBytes() + dictionaryReader.getRetainedSizeInBytes();
}
}