diff --git a/parquet-column/src/main/java/org/apache/parquet/column/ColumnWriteStore.java b/parquet-column/src/main/java/org/apache/parquet/column/ColumnWriteStore.java index 739c00f70e..bb9dfeaa55 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/ColumnWriteStore.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/ColumnWriteStore.java @@ -58,4 +58,10 @@ public interface ColumnWriteStore { * @return a formated string representing memory usage per column */ abstract public String memUsageString(); + + /** + * Close the related output stream and release any resources + */ + abstract public void close(); + } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/ColumnWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/ColumnWriter.java index 7605c5085e..c824504065 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/ColumnWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/ColumnWriter.java @@ -83,5 +83,16 @@ public interface ColumnWriter { */ void writeNull(int repetitionLevel, int definitionLevel); + /** + * Close the underlying store. This should be called when there are no + * more data to be written. + */ + void close(); + + /** + * used to decide when to write a page or row group + * @return the number of bytes of memory used to buffer the current data + */ + long getBufferedSizeInMemory(); } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/ParquetProperties.java b/parquet-column/src/main/java/org/apache/parquet/column/ParquetProperties.java index df44c4be4e..f8567a84e5 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/ParquetProperties.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/ParquetProperties.java @@ -18,6 +18,10 @@ */ package org.apache.parquet.column; +import org.apache.parquet.Preconditions; +import org.apache.parquet.bytes.ByteBufferAllocator; +import org.apache.parquet.bytes.HeapByteBufferAllocator; + import static org.apache.parquet.bytes.BytesUtils.getWidthFromMaxInt; import static org.apache.parquet.column.Encoding.PLAIN; import static org.apache.parquet.column.Encoding.PLAIN_DICTIONARY; @@ -74,19 +78,27 @@ public static WriterVersion fromString(String name) { private final int dictionaryPageSizeThreshold; private final WriterVersion writerVersion; private final boolean enableDictionary; + private final ByteBufferAllocator allocator; public ParquetProperties(int dictPageSize, WriterVersion writerVersion, boolean enableDict) { + this(dictPageSize, writerVersion, enableDict, new HeapByteBufferAllocator()); + } + + public ParquetProperties(int dictPageSize, WriterVersion writerVersion, boolean enableDict, ByteBufferAllocator allocator) { this.dictionaryPageSizeThreshold = dictPageSize; this.writerVersion = writerVersion; this.enableDictionary = enableDict; + Preconditions.checkNotNull(allocator, "ByteBufferAllocator"); + this.allocator = allocator; } - public static ValuesWriter getColumnDescriptorValuesWriter(int maxLevel, int initialSizePerCol, int pageSize) { + public ValuesWriter getColumnDescriptorValuesWriter(int maxLevel, int initialSizePerCol, int pageSize) { if (maxLevel == 0) { return new DevNullValuesWriter(); } else { return new RunLengthBitPackingHybridValuesWriter( - getWidthFromMaxInt(maxLevel), initialSizePerCol, pageSize); + getWidthFromMaxInt(maxLevel), initialSizePerCol, pageSize, this.allocator + ); } } @@ -95,15 +107,15 @@ private ValuesWriter plainWriter(ColumnDescriptor path, int initialSizePerCol, i case BOOLEAN: return new BooleanPlainValuesWriter(); case INT96: - return new FixedLenByteArrayPlainValuesWriter(12, initialSizePerCol, pageSize); + return new FixedLenByteArrayPlainValuesWriter(12, initialSizePerCol, pageSize, this.allocator); case FIXED_LEN_BYTE_ARRAY: - return new FixedLenByteArrayPlainValuesWriter(path.getTypeLength(), initialSizePerCol, pageSize); + return new FixedLenByteArrayPlainValuesWriter(path.getTypeLength(), initialSizePerCol, pageSize, this.allocator); case BINARY: case INT32: case INT64: case DOUBLE: case FLOAT: - return new PlainValuesWriter(initialSizePerCol, pageSize); + return new PlainValuesWriter(initialSizePerCol, pageSize, this.allocator); default: throw new IllegalArgumentException("Unknown type " + path.getType()); } @@ -128,19 +140,19 @@ private DictionaryValuesWriter dictionaryWriter(ColumnDescriptor path, int initi case BOOLEAN: throw new IllegalArgumentException("no dictionary encoding for BOOLEAN"); case BINARY: - return new PlainBinaryDictionaryValuesWriter(dictionaryPageSizeThreshold, encodingForDataPage, encodingForDictionaryPage); + return new PlainBinaryDictionaryValuesWriter(dictionaryPageSizeThreshold, encodingForDataPage, encodingForDictionaryPage, this.allocator); case INT32: - return new PlainIntegerDictionaryValuesWriter(dictionaryPageSizeThreshold, encodingForDataPage, encodingForDictionaryPage); + return new PlainIntegerDictionaryValuesWriter(dictionaryPageSizeThreshold, encodingForDataPage, encodingForDictionaryPage, this.allocator); case INT64: - return new PlainLongDictionaryValuesWriter(dictionaryPageSizeThreshold, encodingForDataPage, encodingForDictionaryPage); + return new PlainLongDictionaryValuesWriter(dictionaryPageSizeThreshold, encodingForDataPage, encodingForDictionaryPage, this.allocator); case INT96: - return new PlainFixedLenArrayDictionaryValuesWriter(dictionaryPageSizeThreshold, 12, encodingForDataPage, encodingForDictionaryPage); + return new PlainFixedLenArrayDictionaryValuesWriter(dictionaryPageSizeThreshold, 12, encodingForDataPage, encodingForDictionaryPage, this.allocator); case DOUBLE: - return new PlainDoubleDictionaryValuesWriter(dictionaryPageSizeThreshold, encodingForDataPage, encodingForDictionaryPage); + return new PlainDoubleDictionaryValuesWriter(dictionaryPageSizeThreshold, encodingForDataPage, encodingForDictionaryPage, this.allocator); case FLOAT: - return new PlainFloatDictionaryValuesWriter(dictionaryPageSizeThreshold, encodingForDataPage, encodingForDictionaryPage); + return new PlainFloatDictionaryValuesWriter(dictionaryPageSizeThreshold, encodingForDataPage, encodingForDictionaryPage, this.allocator); case FIXED_LEN_BYTE_ARRAY: - return new PlainFixedLenArrayDictionaryValuesWriter(dictionaryPageSizeThreshold, path.getTypeLength(), encodingForDataPage, encodingForDictionaryPage); + return new PlainFixedLenArrayDictionaryValuesWriter(dictionaryPageSizeThreshold, path.getTypeLength(), encodingForDataPage, encodingForDictionaryPage, this.allocator); default: throw new IllegalArgumentException("Unknown type " + path.getType()); } @@ -153,12 +165,12 @@ private ValuesWriter writerToFallbackTo(ColumnDescriptor path, int initialSizePe case PARQUET_2_0: switch (path.getType()) { case BOOLEAN: - return new RunLengthBitPackingHybridValuesWriter(1, initialSizePerCol, pageSize); + return new RunLengthBitPackingHybridValuesWriter(1, initialSizePerCol, pageSize, this.allocator); case BINARY: case FIXED_LEN_BYTE_ARRAY: - return new DeltaByteArrayWriter(initialSizePerCol, pageSize); + return new DeltaByteArrayWriter(initialSizePerCol, pageSize,this.allocator); case INT32: - return new DeltaBinaryPackingValuesWriter(initialSizePerCol, pageSize); + return new DeltaBinaryPackingValuesWriter(initialSizePerCol, pageSize, this.allocator); case INT96: case INT64: case DOUBLE: @@ -218,23 +230,28 @@ public boolean isEnableDictionary() { return enableDictionary; } + public ByteBufferAllocator getAllocator() { + return allocator; + } + public ColumnWriteStore newColumnWriteStore( MessageType schema, PageWriteStore pageStore, - int pageSize) { + int pageSize, + ByteBufferAllocator allocator) { switch (writerVersion) { case PARQUET_1_0: return new ColumnWriteStoreV1( pageStore, pageSize, dictionaryPageSizeThreshold, - enableDictionary, writerVersion); + enableDictionary, writerVersion, allocator); case PARQUET_2_0: return new ColumnWriteStoreV2( schema, pageStore, pageSize, - new ParquetProperties(dictionaryPageSizeThreshold, writerVersion, enableDictionary)); + new ParquetProperties(dictionaryPageSizeThreshold, writerVersion, enableDictionary, allocator)); default: throw new IllegalArgumentException("unknown version " + writerVersion); } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnReaderImpl.java b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnReaderImpl.java index 09b1bdfe0c..5ba7da1e79 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnReaderImpl.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnReaderImpl.java @@ -27,6 +27,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.CorruptDeltaByteArrays; import org.apache.parquet.Log; @@ -548,7 +549,7 @@ public Void visit(DataPageV2 dataPageV2) { }); } - private void initDataReader(Encoding dataEncoding, byte[] bytes, int offset, int valueCount) { + private void initDataReader(Encoding dataEncoding, ByteBuffer bytes, int offset, int valueCount) { ValuesReader previousReader = this.dataColumn; this.currentEncoding = dataEncoding; @@ -588,8 +589,8 @@ private void readPageV1(DataPageV1 page) { this.repetitionLevelColumn = new ValuesReaderIntIterator(rlReader); this.definitionLevelColumn = new ValuesReaderIntIterator(dlReader); try { - byte[] bytes = page.getBytes().toByteArray(); - if (DEBUG) LOG.debug("page size " + bytes.length + " bytes and " + pageValueCount + " records"); + ByteBuffer bytes = page.getBytes().toByteBuffer(); + if (DEBUG) LOG.debug("page size " + bytes.remaining() + " bytes and " + pageValueCount + " records"); if (DEBUG) LOG.debug("reading repetition levels at 0"); rlReader.initFromPage(pageValueCount, bytes, 0); int next = rlReader.getNextOffset(); @@ -608,7 +609,7 @@ private void readPageV2(DataPageV2 page) { this.definitionLevelColumn = newRLEIterator(path.getMaxDefinitionLevel(), page.getDefinitionLevels()); try { if (DEBUG) LOG.debug("page data size " + page.getData().size() + " bytes and " + pageValueCount + " records"); - initDataReader(page.getDataEncoding(), page.getData().toByteArray(), 0, page.getValueCount()); + initDataReader(page.getDataEncoding(), page.getData().toByteBuffer(), 0, page.getValueCount()); } catch (IOException e) { throw new ParquetDecodingException("could not read page " + page + " in col " + path, e); } @@ -622,7 +623,7 @@ private IntIterator newRLEIterator(int maxLevel, BytesInput bytes) { return new RLEIntIterator( new RunLengthBitPackingHybridDecoder( BytesUtils.getWidthFromMaxInt(maxLevel), - new ByteArrayInputStream(bytes.toByteArray()))); + bytes.toInputStream())); } catch (IOException e) { throw new ParquetDecodingException("could not read levels in page for col " + path, e); } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriteStoreV1.java b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriteStoreV1.java index a72b6f7f08..277c468045 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriteStoreV1.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriteStoreV1.java @@ -25,6 +25,7 @@ import java.util.Set; import java.util.TreeMap; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.column.ColumnDescriptor; import org.apache.parquet.column.ColumnWriteStore; import org.apache.parquet.column.ColumnWriter; @@ -40,14 +41,16 @@ public class ColumnWriteStoreV1 implements ColumnWriteStore { private final int dictionaryPageSizeThreshold; private final boolean enableDictionary; private final WriterVersion writerVersion; + private final ByteBufferAllocator allocator; - public ColumnWriteStoreV1(PageWriteStore pageWriteStore, int pageSizeThreshold, int dictionaryPageSizeThreshold, boolean enableDictionary, WriterVersion writerVersion) { + public ColumnWriteStoreV1(PageWriteStore pageWriteStore, int pageSizeThreshold, int dictionaryPageSizeThreshold, boolean enableDictionary, WriterVersion writerVersion, ByteBufferAllocator allocator) { super(); this.pageWriteStore = pageWriteStore; this.pageSizeThreshold = pageSizeThreshold; this.dictionaryPageSizeThreshold = dictionaryPageSizeThreshold; this.enableDictionary = enableDictionary; this.writerVersion = writerVersion; + this.allocator = allocator; } public ColumnWriter getColumnWriter(ColumnDescriptor path) { @@ -65,7 +68,7 @@ public Set getColumnDescriptors() { private ColumnWriterV1 newMemColumn(ColumnDescriptor path) { PageWriter pageWriter = pageWriteStore.getPageWriter(path); - return new ColumnWriterV1(path, pageWriter, pageSizeThreshold, dictionaryPageSizeThreshold, enableDictionary, writerVersion); + return new ColumnWriterV1(path, pageWriter, pageSizeThreshold, dictionaryPageSizeThreshold, enableDictionary, writerVersion, allocator); } @Override @@ -132,4 +135,11 @@ public void endRecord() { // V1 does not take record boundaries into account } + public void close() { + Collection values = columns.values(); + for (ColumnWriterV1 memColumn : values) { + memColumn.close(); + } + } + } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriteStoreV2.java b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriteStoreV2.java index fc17a222da..4126004109 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriteStoreV2.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriteStoreV2.java @@ -29,6 +29,7 @@ import java.util.Set; import java.util.TreeMap; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.column.ColumnDescriptor; import org.apache.parquet.column.ColumnWriteStore; import org.apache.parquet.column.ColumnWriter; @@ -50,6 +51,7 @@ public class ColumnWriteStoreV2 implements ColumnWriteStore { private long rowCount; private long rowCountForNextSizeCheck = MINIMUM_RECORD_COUNT_FOR_CHECK; private final long thresholdTolerance; + private final ByteBufferAllocator allocator; private int pageSizeThreshold; @@ -61,6 +63,7 @@ public ColumnWriteStoreV2( super(); this.pageSizeThreshold = pageSizeThreshold; this.thresholdTolerance = (long)(pageSizeThreshold * THRESHOLD_TOLERANCE_RATIO); + this.allocator = parquetProps.getAllocator(); Map mcolumns = new TreeMap(); for (ColumnDescriptor path : schema.getColumns()) { PageWriter pageWriter = pageWriteStore.getPageWriter(path); @@ -127,6 +130,14 @@ public String memUsageString() { return b.toString(); } + @Override + public void close() { + flush(); // calling flush() here to keep it consistent with the behavior before merging with master + for (ColumnWriterV2 memColumn : columns.values()) { + memColumn.close(); + } + } + @Override public void endRecord() { ++ rowCount; diff --git a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriterV1.java b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriterV1.java index f4079c761c..f010df8594 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriterV1.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriterV1.java @@ -23,6 +23,7 @@ import java.io.IOException; import org.apache.parquet.Log; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.bytes.CapacityByteArrayOutputStream; import org.apache.parquet.column.ColumnDescriptor; import org.apache.parquet.column.ColumnWriter; @@ -66,7 +67,8 @@ public ColumnWriterV1( int pageSizeThreshold, int dictionaryPageSizeThreshold, boolean enableDictionary, - WriterVersion writerVersion) { + WriterVersion writerVersion, + ByteBufferAllocator allocator) { this.path = path; this.pageWriter = pageWriter; this.pageSizeThreshold = pageSizeThreshold; @@ -74,10 +76,10 @@ public ColumnWriterV1( this.valueCountForNextSizeCheck = INITIAL_COUNT_FOR_SIZE_CHECK; resetStatistics(); - ParquetProperties parquetProps = new ParquetProperties(dictionaryPageSizeThreshold, writerVersion, enableDictionary); + ParquetProperties parquetProps = new ParquetProperties(dictionaryPageSizeThreshold, writerVersion, enableDictionary, allocator); - this.repetitionLevelColumn = ParquetProperties.getColumnDescriptorValuesWriter(path.getMaxRepetitionLevel(), MIN_SLAB_SIZE, pageSizeThreshold); - this.definitionLevelColumn = ParquetProperties.getColumnDescriptorValuesWriter(path.getMaxDefinitionLevel(), MIN_SLAB_SIZE, pageSizeThreshold); + this.repetitionLevelColumn = parquetProps.getColumnDescriptorValuesWriter(path.getMaxRepetitionLevel(), MIN_SLAB_SIZE, pageSizeThreshold); + this.definitionLevelColumn = parquetProps.getColumnDescriptorValuesWriter(path.getMaxDefinitionLevel(), MIN_SLAB_SIZE, pageSizeThreshold); int initialSlabSize = CapacityByteArrayOutputStream.initialSlabSizeHeuristic(MIN_SLAB_SIZE, pageSizeThreshold, 10); this.dataColumn = parquetProps.getValuesWriter(path, initialSlabSize, pageSizeThreshold); @@ -239,7 +241,7 @@ public void flush() { if (valueCount > 0) { writePage(); } - final DictionaryPage dictionaryPage = dataColumn.createDictionaryPage(); + final DictionaryPage dictionaryPage = dataColumn.toDictPageAndClose(); if (dictionaryPage != null) { if (DEBUG) LOG.debug("write dictionary"); try { @@ -251,6 +253,16 @@ public void flush() { } } + @Override + public void close() { + flush(); + // Close the Values writers. + repetitionLevelColumn.close(); + definitionLevelColumn.close(); + dataColumn.close(); + } + + @Override public long getBufferedSizeInMemory() { return repetitionLevelColumn.getBufferedSize() + definitionLevelColumn.getBufferedSize() diff --git a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriterV2.java b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriterV2.java index 5e936a2f09..8249b72077 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriterV2.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/impl/ColumnWriterV2.java @@ -25,6 +25,7 @@ import org.apache.parquet.Ints; import org.apache.parquet.Log; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.CapacityByteArrayOutputStream; import org.apache.parquet.column.ColumnDescriptor; @@ -69,8 +70,10 @@ public ColumnWriterV2( this.pageWriter = pageWriter; resetStatistics(); - this.repetitionLevelColumn = new RunLengthBitPackingHybridEncoder(getWidthFromMaxInt(path.getMaxRepetitionLevel()), MIN_SLAB_SIZE, pageSize); - this.definitionLevelColumn = new RunLengthBitPackingHybridEncoder(getWidthFromMaxInt(path.getMaxDefinitionLevel()), MIN_SLAB_SIZE, pageSize); + this.repetitionLevelColumn = new RunLengthBitPackingHybridEncoder( + getWidthFromMaxInt(path.getMaxRepetitionLevel()), MIN_SLAB_SIZE, pageSize, parquetProps.getAllocator()); + this.definitionLevelColumn = new RunLengthBitPackingHybridEncoder( + getWidthFromMaxInt(path.getMaxDefinitionLevel()), MIN_SLAB_SIZE, pageSize, parquetProps.getAllocator()); int initialSlabSize = CapacityByteArrayOutputStream.initialSlabSizeHeuristic(MIN_SLAB_SIZE, pageSize, 10); this.dataColumn = parquetProps.getValuesWriter(path, initialSlabSize, pageSize); @@ -113,6 +116,22 @@ public void writeNull(int repetitionLevel, int definitionLevel) { ++ valueCount; } + @Override + public void close() { + // Close the Values writers. + repetitionLevelColumn.close(); + definitionLevelColumn.close(); + dataColumn.close(); + } + + @Override + public long getBufferedSizeInMemory() { + return repetitionLevelColumn.getBufferedSize() + + definitionLevelColumn.getBufferedSize() + + dataColumn.getBufferedSize() + + pageWriter.getMemSize(); + } + /** * writes the current value * @param value @@ -208,7 +227,7 @@ public void write(long value, int repetitionLevel, int definitionLevel) { * Is called right after writePage */ public void finalizeColumnChunk() { - final DictionaryPage dictionaryPage = dataColumn.createDictionaryPage(); + final DictionaryPage dictionaryPage = dataColumn.toDictPageAndClose(); if (dictionaryPage != null) { if (DEBUG) LOG.debug("write dictionary"); try { @@ -252,7 +271,7 @@ public long allocatedSize() { } /** - * @param prefix a prefix to format lines + * @param indent a prefix to format lines * @return a formatted string showing how memory is used */ public String memUsageString(String indent) { diff --git a/parquet-column/src/main/java/org/apache/parquet/column/page/PageWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/page/PageWriter.java index 4ad7d9f2fb..a1d86474e2 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/page/PageWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/page/PageWriter.java @@ -20,6 +20,7 @@ import java.io.IOException; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.column.Encoding; import org.apache.parquet.column.statistics.Statistics; @@ -54,7 +55,6 @@ public interface PageWriter { * @param dataEncoding the encoding for the data * @param data the data encoded with dataEncoding * @param statistics optional stats for this page - * @param metadata optional free form key values * @throws IOException */ void writePageV2( diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/ValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/ValuesReader.java index a3d8920ce4..03aa2f8128 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/ValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/ValuesReader.java @@ -20,13 +20,14 @@ import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.io.ParquetDecodingException; import org.apache.parquet.io.api.Binary; /** * Base class to implement an encoding for a given column type. * - * A ValuesReader is provided with a page (byte-array) and is responsible + * A ValuesReader is provided with a page (byte-buffer) and is responsible * for deserializing the primitive values stored in that page. * * Given that pages are homogeneous (store only a single type), typical subclasses @@ -58,8 +59,20 @@ public abstract class ValuesReader { * * @throws IOException */ - public abstract void initFromPage(int valueCount, byte[] page, int offset) throws IOException; - + public abstract void initFromPage(int valueCount, ByteBuffer page, int offset) throws IOException; + + /** + * Same functionality as method of the same name that takes a ByteBuffer instead of a byte[]. + * + * This method is only provided for backward compatibility and will be removed in a future release. + * Please update any code using it as soon as possible. + * @see #initFromPage(int, ByteBuffer, int) + */ + @Deprecated + public void initFromPage(int valueCount, byte[] page, int offset) throws IOException { + this.initFromPage(valueCount, ByteBuffer.wrap(page), offset); + } + /** * Called to return offset of the next section * @return offset of the next section diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/ValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/ValuesWriter.java index c8f31b9510..e5cf8e9441 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/ValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/ValuesWriter.java @@ -56,9 +56,20 @@ public abstract class ValuesWriter { public abstract void reset(); /** + * Called to close the values writer. Any output stream is closed and can no longer be used. + * All resources are released. + */ + public void close() { + } + + /** + * Returns the dictionary generated by this writer if one was created. + * As part of this operation the dictionary is closed and will not have + * any new values written into it. + * * @return the dictionary page or null if not dictionary based */ - public DictionaryPage createDictionaryPage() { + public DictionaryPage toDictPageAndClose() { return null; } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/BitPackingValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/BitPackingValuesReader.java index f71326354e..f540c392b5 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/BitPackingValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/BitPackingValuesReader.java @@ -21,9 +21,10 @@ import static org.apache.parquet.bytes.BytesUtils.getWidthFromMaxInt; import static org.apache.parquet.column.values.bitpacking.BitPacking.createBitPackingReader; -import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.ByteBuffer; +import org.apache.parquet.bytes.ByteBufferInputStream; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesUtils; import org.apache.parquet.column.values.ValuesReader; @@ -39,7 +40,7 @@ public class BitPackingValuesReader extends ValuesReader { private static final Log LOG = Log.getLog(BitPackingValuesReader.class); - private ByteArrayInputStream in; + private ByteBufferInputStream in; private BitPackingReader bitPackingReader; private final int bitsPerValue; private int nextOffset; @@ -66,18 +67,18 @@ public int readInteger() { /** * {@inheritDoc} - * @see org.apache.parquet.column.values.ValuesReader#initFromPage(long, byte[], int) + * @see org.apache.parquet.column.values.ValuesReader#initFromPage(int, ByteBuffer, int) */ @Override - public void initFromPage(int valueCount, byte[] in, int offset) throws IOException { + public void initFromPage(int valueCount, ByteBuffer in, int offset) throws IOException { int effectiveBitLength = valueCount * bitsPerValue; int length = BytesUtils.paddedByteCountFromBits(effectiveBitLength); if (Log.DEBUG) LOG.debug("reading " + length + " bytes for " + valueCount + " values of size " + bitsPerValue + " bits." ); - this.in = new ByteArrayInputStream(in, offset, length); + this.in = new ByteBufferInputStream(in, offset, length); this.bitPackingReader = createBitPackingReader(bitsPerValue, this.in, valueCount); this.nextOffset = offset + length; } - + @Override public int getNextOffset() { return nextOffset; diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/BitPackingValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/BitPackingValuesWriter.java index 24436ef884..08751a0f96 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/BitPackingValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/BitPackingValuesWriter.java @@ -24,6 +24,7 @@ import java.io.IOException; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.CapacityByteArrayOutputStream; import org.apache.parquet.column.Encoding; @@ -47,9 +48,9 @@ public class BitPackingValuesWriter extends ValuesWriter { * @param bound the maximum value stored by this column * @param pageSize */ - public BitPackingValuesWriter(int bound, int initialCapacity, int pageSize) { + public BitPackingValuesWriter(int bound, int initialCapacity, int pageSize, ByteBufferAllocator allocator) { this.bitsPerValue = getWidthFromMaxInt(bound); - this.out = new CapacityByteArrayOutputStream(initialCapacity, pageSize); + this.out = new CapacityByteArrayOutputStream(initialCapacity, pageSize, allocator); init(); } @@ -103,6 +104,11 @@ public void reset() { init(); } + @Override + public void close() { + out.close(); + } + /** * {@inheritDoc} * @see org.apache.parquet.column.values.ValuesWriter#getAllocatedSize() diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/ByteBitPackingValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/ByteBitPackingValuesReader.java index c0ab7e047b..f4c8c8efc5 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/ByteBitPackingValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/ByteBitPackingValuesReader.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.util.Arrays; +import java.nio.ByteBuffer; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesUtils; @@ -34,7 +35,7 @@ public class ByteBitPackingValuesReader extends ValuesReader { private final BytePacker packer; private final int[] decoded = new int[VALUES_AT_A_TIME]; private int decodedPosition = VALUES_AT_A_TIME - 1; - private byte[] encoded; + private ByteBuffer encoded; private int encodedPos; private int nextOffset; @@ -47,8 +48,13 @@ public ByteBitPackingValuesReader(int bound, Packer packer) { public int readInteger() { ++ decodedPosition; if (decodedPosition == decoded.length) { - if (encodedPos + bitWidth > encoded.length) { - packer.unpack8Values(Arrays.copyOfRange(encoded, encodedPos, encodedPos + bitWidth), 0, decoded, 0); + encoded.position(encodedPos); + if (encodedPos + bitWidth > encoded.limit()) { + // unpack8Values needs at least bitWidth bytes to read from, + // We have to fill in 0 byte at the end of encoded bytes. + byte[] tempEncode = new byte[bitWidth]; + encoded.get(tempEncode, 0, encoded.limit() - encodedPos); + packer.unpack8Values(tempEncode, 0, decoded, 0); } else { packer.unpack8Values(encoded, encodedPos, decoded, 0); } @@ -59,7 +65,7 @@ public int readInteger() { } @Override - public void initFromPage(int valueCount, byte[] page, int offset) + public void initFromPage(int valueCount, ByteBuffer page, int offset) throws IOException { int effectiveBitLength = valueCount * bitWidth; int length = BytesUtils.paddedByteCountFromBits(effectiveBitLength); // ceil diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/ByteBitPackingValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/ByteBitPackingValuesWriter.java index d0240bbdde..a33e22ca99 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/ByteBitPackingValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/bitpacking/ByteBitPackingValuesWriter.java @@ -22,6 +22,7 @@ import java.io.IOException; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.BytesUtils; import org.apache.parquet.column.Encoding; diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BitReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BitReader.java index 865eea2b60..caea5b51c6 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BitReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BitReader.java @@ -19,13 +19,14 @@ package org.apache.parquet.column.values.boundedint; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.io.ParquetDecodingException; class BitReader { private int currentByte = 0; private int currentPosition = 8; - private byte[] buf; + private ByteBuffer buf; private int currentBufferPosition = 0; private static final int[] byteGetValueMask = new int[8]; private static final int[] readMask = new int[32]; @@ -50,7 +51,7 @@ class BitReader { * The array is not copied, so must not be mutated during the course of * reading. */ - public void prepare(byte[] buf, int offset, int length) { + public void prepare(ByteBuffer buf, int offset, int length) { this.buf = buf; this.endBufferPosistion = offset + length; currentByte = 0; @@ -87,7 +88,7 @@ public int readNBitInteger(int bitsPerValue) { private int getNextByte() { if (currentBufferPosition < endBufferPosistion) { - return buf[currentBufferPosition++] & 0xFF; + return buf.get(currentBufferPosition++) & 0xFF; } return 0; } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BitWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BitWriter.java index 1d1d9d12bc..9489714b31 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BitWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BitWriter.java @@ -18,6 +18,7 @@ */ package org.apache.parquet.column.values.boundedint; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.CapacityByteArrayOutputStream; @@ -41,8 +42,8 @@ class BitWriter { } } - public BitWriter(int initialCapacity, int pageSize) { - this.baos = new CapacityByteArrayOutputStream(initialCapacity, pageSize); + public BitWriter(int initialCapacity, int pageSize, ByteBufferAllocator allocator) { + this.baos = new CapacityByteArrayOutputStream(initialCapacity, pageSize, allocator); } public void writeBit(boolean bit) { @@ -156,4 +157,11 @@ public int getCapacity() { public String memUsageString(String prefix) { return baos.memUsageString(prefix); } + + public void close() { + currentByte = 0; + currentBytePosition = 0; + finished = false; + baos.close(); + } } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesFactory.java b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesFactory.java index 3ace6e2a3d..bbbf8dae88 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesFactory.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesFactory.java @@ -18,6 +18,7 @@ */ package org.apache.parquet.column.values.boundedint; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.column.values.ValuesWriter; @@ -26,7 +27,7 @@ public static ValuesReader getBoundedReader(int bound) { return bound == 0 ? new ZeroIntegerValuesReader() : new BoundedIntValuesReader(bound); } - public static ValuesWriter getBoundedWriter(int bound, int initialCapacity, int pageSize) { - return bound == 0 ? new DevNullValuesWriter() : new BoundedIntValuesWriter(bound, initialCapacity, pageSize); + public static ValuesWriter getBoundedWriter(int bound, int initialCapacity, int pageSize, ByteBufferAllocator allocator) { + return bound == 0 ? new DevNullValuesWriter() : new BoundedIntValuesWriter(bound, initialCapacity, pageSize, allocator); } } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesReader.java index dda443167d..c322125e7e 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesReader.java @@ -21,6 +21,7 @@ import static org.apache.parquet.Log.DEBUG; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesUtils; @@ -70,8 +71,8 @@ public int readInteger() { // bytes would have to be serialized). This is the flip-side // to BoundedIntColumnWriter.writeData(BytesOutput) @Override - public void initFromPage(int valueCount, byte[] in, int offset) throws IOException { - if (DEBUG) LOG.debug("reading size at "+ offset + ": " + in[offset] + " " + in[offset + 1] + " " + in[offset + 2] + " " + in[offset + 3] + " "); + public void initFromPage(int valueCount, ByteBuffer in, int offset) throws IOException { + if (DEBUG) LOG.debug("reading size at "+ offset + ": " + in.get(offset) + " " + in.get(offset + 1) + " " + in.get(offset + 2) + " " + in.get(offset + 3) + " "); int totalBytes = BytesUtils.readIntLittleEndian(in, offset); if (DEBUG) LOG.debug("will read "+ totalBytes + " bytes"); currentValueCt = 0; diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesWriter.java index 0acaaf7dea..a90a6e5fcf 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/BoundedIntValuesWriter.java @@ -18,6 +18,7 @@ */ package org.apache.parquet.column.values.boundedint; +import org.apache.parquet.bytes.ByteBufferAllocator; import static org.apache.parquet.bytes.BytesInput.concat; import static org.apache.parquet.column.Encoding.RLE; import org.apache.parquet.Log; @@ -59,11 +60,11 @@ class BoundedIntValuesWriter extends ValuesWriter { } } - public BoundedIntValuesWriter(int bound, int initialCapacity, int pageSize) { + public BoundedIntValuesWriter(int bound, int initialCapacity, int pageSize, ByteBufferAllocator allocator) { if (bound == 0) { throw new ParquetEncodingException("Value bound cannot be 0. Use DevNullColumnWriter instead."); } - this.bitWriter = new BitWriter(initialCapacity, pageSize); + this.bitWriter = new BitWriter(initialCapacity, pageSize, allocator); bitsPerValue = (int)Math.ceil(Math.log(bound + 1)/Math.log(2)); shouldRepeatThreshold = (bitsPerValue + 9)/(1 + bitsPerValue); if (Log.DEBUG) LOG.debug("init column with bit width of " + bitsPerValue + " and repeat threshold of " + shouldRepeatThreshold); @@ -100,6 +101,11 @@ public void reset() { bitWriter.reset(); } + @Override + public void close() { + bitWriter.close(); + } + @Override public void writeInteger(int val) { if (currentValue == val) { diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/ZeroIntegerValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/ZeroIntegerValuesReader.java index 92015962f6..8c78c388a7 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/ZeroIntegerValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/boundedint/ZeroIntegerValuesReader.java @@ -19,6 +19,7 @@ package org.apache.parquet.column.values.boundedint; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.column.values.ValuesReader; @@ -36,7 +37,7 @@ public int readInteger() { } @Override - public void initFromPage(int valueCount, byte[] in, int offset) throws IOException { + public void initFromPage(int valueCount, ByteBuffer in, int offset) throws IOException { this.nextOffset = offset; } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesReader.java index c1678ae626..3f92deb72f 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesReader.java @@ -19,14 +19,15 @@ package org.apache.parquet.column.values.delta; +import org.apache.parquet.bytes.ByteBufferInputStream; import org.apache.parquet.bytes.BytesUtils; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.column.values.bitpacking.BytePacker; import org.apache.parquet.column.values.bitpacking.Packer; import org.apache.parquet.io.ParquetDecodingException; -import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.ByteBuffer; /** * Read values written by {@link DeltaBinaryPackingValuesWriter} @@ -40,7 +41,7 @@ public class DeltaBinaryPackingValuesReader extends ValuesReader { */ private int valuesRead; private int minDeltaInCurrentBlock; - private byte[] page; + private ByteBuffer page; /** * stores the decoded values including the first value which is written to the header */ @@ -50,7 +51,7 @@ public class DeltaBinaryPackingValuesReader extends ValuesReader { * when data is not aligned to mini block, which means padding 0s are in the buffer */ private int valuesBuffered; - private ByteArrayInputStream in; + private ByteBufferInputStream in; private int nextOffset; private DeltaBinaryPackingConfig config; private int[] bitWidths; @@ -64,8 +65,8 @@ public class DeltaBinaryPackingValuesReader extends ValuesReader { * @throws IOException */ @Override - public void initFromPage(int valueCount, byte[] page, int offset) throws IOException { - in = new ByteArrayInputStream(page, offset, page.length - offset); + public void initFromPage(int valueCount, ByteBuffer page, int offset) throws IOException { + in = new ByteBufferInputStream(page, offset, page.limit() - offset); this.config = DeltaBinaryPackingConfig.readConfig(in); this.page = page; this.totalValueCount = BytesUtils.readUnsignedVarInt(in); @@ -78,7 +79,7 @@ public void initFromPage(int valueCount, byte[] page, int offset) throws IOExcep while (valuesBuffered < totalValueCount) { //values Buffered could be more than totalValueCount, since we flush on a mini block basis loadNewBlockToBuffer(); } - this.nextOffset = page.length - in.available(); + this.nextOffset = page.limit() - in.available(); } @Override @@ -151,7 +152,7 @@ private void unpackMiniBlock(BytePacker packer) { private void unpack8Values(BytePacker packer) { //calculate the pos because the packer api uses array not stream - int pos = page.length - in.available(); + int pos = page.limit() - in.available(); packer.unpack8Values(page, pos, valuesBuffer, valuesBuffered); this.valuesBuffered += 8; //sync the pos in stream diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesWriter.java index 1b3692f686..421182f73e 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesWriter.java @@ -18,6 +18,7 @@ */ package org.apache.parquet.column.values.delta; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.BytesUtils; import org.apache.parquet.bytes.CapacityByteArrayOutputStream; @@ -111,16 +112,16 @@ public class DeltaBinaryPackingValuesWriter extends ValuesWriter { */ private int minDeltaInCurrentBlock = Integer.MAX_VALUE; - public DeltaBinaryPackingValuesWriter(int slabSize, int pageSize) { - this(DEFAULT_NUM_BLOCK_VALUES, DEFAULT_NUM_MINIBLOCKS, slabSize, pageSize); + public DeltaBinaryPackingValuesWriter(int slabSize, int pageSize, ByteBufferAllocator allocator) { + this(DEFAULT_NUM_BLOCK_VALUES, DEFAULT_NUM_MINIBLOCKS, slabSize, pageSize, allocator); } - public DeltaBinaryPackingValuesWriter(int blockSizeInValues, int miniBlockNum, int slabSize, int pageSize) { + public DeltaBinaryPackingValuesWriter(int blockSizeInValues, int miniBlockNum, int slabSize, int pageSize, ByteBufferAllocator allocator) { this.config = new DeltaBinaryPackingConfig(blockSizeInValues, miniBlockNum); bitWidths = new int[config.miniBlockNumInABlock]; deltaBlockBuffer = new int[blockSizeInValues]; miniBlockByteBuffer = new byte[config.miniBlockSizeInValues * MAX_BITWIDTH]; - baos = new CapacityByteArrayOutputStream(slabSize, pageSize); + baos = new CapacityByteArrayOutputStream(slabSize, pageSize, allocator); } @Override @@ -257,6 +258,14 @@ public void reset() { this.minDeltaInCurrentBlock = Integer.MAX_VALUE; } + @Override + public void close() { + this.totalValueCount = 0; + this.baos.close(); + this.deltaValuesToFlush = 0; + this.minDeltaInCurrentBlock = Integer.MAX_VALUE; + } + @Override public long getAllocatedSize() { return baos.getCapacity(); diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesReader.java index fb9bdc50b0..41f221d85a 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesReader.java @@ -21,6 +21,7 @@ import static org.apache.parquet.Log.DEBUG; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.Log; import org.apache.parquet.column.values.ValuesReader; @@ -28,7 +29,7 @@ import org.apache.parquet.io.api.Binary; /** - * Reads binary data written by {@link DeltaLengthByteArrayValueWriter} + * Reads binary data written by {@link DeltaLengthByteArrayValuesWriter} * * @author Aniket Mokashi * @@ -37,7 +38,7 @@ public class DeltaLengthByteArrayValuesReader extends ValuesReader { private static final Log LOG = Log.getLog(DeltaLengthByteArrayValuesReader.class); private ValuesReader lengthReader; - private byte[] in; + private ByteBuffer in; private int offset; public DeltaLengthByteArrayValuesReader() { @@ -45,9 +46,9 @@ public DeltaLengthByteArrayValuesReader() { } @Override - public void initFromPage(int valueCount, byte[] in, int offset) + public void initFromPage(int valueCount, ByteBuffer in, int offset) throws IOException { - if (DEBUG) LOG.debug("init from page at offset "+ offset + " for length " + (in.length - offset)); + if (DEBUG) LOG.debug("init from page at offset "+ offset + " for length " + (in.limit() - offset)); lengthReader.initFromPage(valueCount, in, offset); offset = lengthReader.getNextOffset(); this.in = in; @@ -59,7 +60,7 @@ public Binary readBytes() { int length = lengthReader.readInteger(); int start = offset; offset = start + length; - return Binary.fromConstantByteArray(in, start, length); + return Binary.fromConstantByteBuffer(in, start, length); } @Override diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesWriter.java index 3f686cc199..2d6b21389c 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/deltalengthbytearray/DeltaLengthByteArrayValuesWriter.java @@ -20,6 +20,7 @@ import java.io.IOException; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.CapacityByteArrayOutputStream; @@ -48,13 +49,13 @@ public class DeltaLengthByteArrayValuesWriter extends ValuesWriter { private CapacityByteArrayOutputStream arrayOut; private LittleEndianDataOutputStream out; - public DeltaLengthByteArrayValuesWriter(int initialSize, int pageSize) { - arrayOut = new CapacityByteArrayOutputStream(initialSize, pageSize); + public DeltaLengthByteArrayValuesWriter(int initialSize, int pageSize, ByteBufferAllocator allocator) { + arrayOut = new CapacityByteArrayOutputStream(initialSize, pageSize, allocator); out = new LittleEndianDataOutputStream(arrayOut); lengthWriter = new DeltaBinaryPackingValuesWriter( DeltaBinaryPackingValuesWriter.DEFAULT_NUM_BLOCK_VALUES, DeltaBinaryPackingValuesWriter.DEFAULT_NUM_MINIBLOCKS, - initialSize, pageSize); + initialSize, pageSize, allocator); } @Override @@ -94,6 +95,12 @@ public void reset() { arrayOut.reset(); } + @Override + public void close() { + lengthWriter.close(); + arrayOut.close(); + } + @Override public long getAllocatedSize() { return lengthWriter.getAllocatedSize() + arrayOut.getCapacity(); @@ -104,3 +111,4 @@ public String memUsageString(String prefix) { return arrayOut.memUsageString(lengthWriter.memUsageString(prefix) + " DELTA_LENGTH_BYTE_ARRAY"); } } + diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayReader.java index 87ec08e2e8..c2cfd6d311 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayReader.java @@ -19,6 +19,7 @@ package org.apache.parquet.column.values.deltastrings; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.column.values.RequiresPreviousReader; import org.apache.parquet.column.values.ValuesReader; @@ -45,13 +46,13 @@ public DeltaByteArrayReader() { } @Override - public void initFromPage(int valueCount, byte[] page, int offset) + public void initFromPage(int valueCount, ByteBuffer page, int offset) throws IOException { prefixLengthReader.initFromPage(valueCount, page, offset); int next = prefixLengthReader.getNextOffset(); suffixReader.initFromPage(valueCount, page, next); } - + @Override public void skip() { prefixLengthReader.skip(); diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayWriter.java index 54234db7b0..1604ddbaac 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/deltastrings/DeltaByteArrayWriter.java @@ -18,6 +18,7 @@ */ package org.apache.parquet.column.values.deltastrings; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.column.Encoding; import org.apache.parquet.column.values.ValuesWriter; @@ -41,9 +42,9 @@ public class DeltaByteArrayWriter extends ValuesWriter{ private ValuesWriter suffixWriter; private byte[] previous; - public DeltaByteArrayWriter(int initialCapacity, int pageSize) { - this.prefixLengthWriter = new DeltaBinaryPackingValuesWriter(128, 4, initialCapacity, pageSize); - this.suffixWriter = new DeltaLengthByteArrayValuesWriter(initialCapacity, pageSize); + public DeltaByteArrayWriter(int initialCapacity, int pageSize, ByteBufferAllocator allocator) { + this.prefixLengthWriter = new DeltaBinaryPackingValuesWriter(128, 4, initialCapacity, pageSize, allocator); + this.suffixWriter = new DeltaLengthByteArrayValuesWriter(initialCapacity, pageSize, allocator); this.previous = new byte[0]; } @@ -69,6 +70,12 @@ public void reset() { previous = new byte[0]; } + @Override + public void close() { + prefixLengthWriter.close(); + suffixWriter.close(); + } + @Override public long getAllocatedSize() { return prefixLengthWriter.getAllocatedSize() + suffixWriter.getAllocatedSize(); @@ -85,6 +92,7 @@ public void writeBytes(Binary v) { int i = 0; byte[] vb = v.getBytes(); int length = previous.length < vb.length ? previous.length : vb.length; + // find the number of matching prefix bytes between this value and the previous one for(i = 0; (i < length) && (previous[i] == vb[i]); i++); prefixLengthWriter.writeInteger(i); suffixWriter.writeBytes(v.slice(i, vb.length - i)); diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesReader.java index 972c87e251..e421da99ff 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesReader.java @@ -20,9 +20,10 @@ import static org.apache.parquet.Log.DEBUG; -import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.ByteBuffer; +import org.apache.parquet.bytes.ByteBufferInputStream; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesUtils; import org.apache.parquet.column.Dictionary; @@ -40,7 +41,7 @@ public class DictionaryValuesReader extends ValuesReader { private static final Log LOG = Log.getLog(DictionaryValuesReader.class); - private ByteArrayInputStream in; + private ByteBufferInputStream in; private Dictionary dictionary; @@ -51,12 +52,12 @@ public DictionaryValuesReader(Dictionary dictionary) { } @Override - public void initFromPage(int valueCount, byte[] page, int offset) + public void initFromPage(int valueCount, ByteBuffer page, int offset) throws IOException { - this.in = new ByteArrayInputStream(page, offset, page.length - offset); - if (page.length - offset > 0) { + this.in = new ByteBufferInputStream(page, offset, page.limit() - offset); + if (page.limit() - offset > 0) { if (DEBUG) - LOG.debug("init from page at offset " + offset + " for length " + (page.length - offset)); + LOG.debug("init from page at offset " + offset + " for length " + (page.limit() - offset)); int bitWidth = BytesUtils.readIntLittleEndianOnOneByte(in); if (DEBUG) LOG.debug("bit width " + bitWidth); decoder = new RunLengthBitPackingHybridDecoder(bitWidth, in); diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesWriter.java index eb9fdd959c..86edd79b13 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/DictionaryValuesWriter.java @@ -36,9 +36,11 @@ import it.unimi.dsi.fastutil.objects.ObjectIterator; import java.io.IOException; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.BytesUtils; @@ -92,17 +94,28 @@ public abstract class DictionaryValuesWriter extends ValuesWriter implements Req /* dictionary encoded values */ protected IntList encodedValues = new IntList(); + /** indicates if this is the first page being processed */ + protected boolean firstPage = true; + + protected ByteBufferAllocator allocator; + /* Track the list of writers used so they can be appropriately closed when necessary + (currently used for off-heap memory which is not garbage collected) */ + private List encoders = new ArrayList(); + /** * @param maxDictionaryByteSize */ - protected DictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage) { + protected DictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage, ByteBufferAllocator allocator) { + this.allocator = allocator; this.maxDictionaryByteSize = maxDictionaryByteSize; this.encodingForDataPage = encodingForDataPage; this.encodingForDictionaryPage = encodingForDictionaryPage; } - protected DictionaryPage dictPage(ValuesWriter dictionaryEncoder) { - return new DictionaryPage(dictionaryEncoder.getBytes(), lastUsedDictionarySize, encodingForDictionaryPage); + protected DictionaryPage dictPage(ValuesWriter dictPageWriter) { + DictionaryPage ret = new DictionaryPage(dictPageWriter.getBytes(), lastUsedDictionarySize, encodingForDictionaryPage); + dictPageWriter.close(); + return ret; } @Override @@ -147,12 +160,12 @@ public BytesInput getBytes() { int maxDicId = getDictionarySize() - 1; if (DEBUG) LOG.debug("max dic id " + maxDicId); int bitWidth = BytesUtils.getWidthFromMaxInt(maxDicId); - int initialSlabSize = CapacityByteArrayOutputStream.initialSlabSizeHeuristic(MIN_INITIAL_SLAB_SIZE, maxDictionaryByteSize, 10); RunLengthBitPackingHybridEncoder encoder = - new RunLengthBitPackingHybridEncoder(bitWidth, initialSlabSize, maxDictionaryByteSize); + new RunLengthBitPackingHybridEncoder(bitWidth, initialSlabSize, maxDictionaryByteSize, this.allocator); + encoders.add(encoder); IntIterator iterator = encodedValues.iterator(); try { while (iterator.hasNext()) { @@ -179,9 +192,19 @@ public Encoding getEncoding() { @Override public void reset() { + close(); encodedValues = new IntList(); } + @Override + public void close() { + encodedValues = null; + for (RunLengthBitPackingHybridEncoder encoder : encoders) { + encoder.close(); + } + encoders.clear(); + } + @Override public void resetDictionary() { lastUsedDictionaryByteSize = 0; @@ -225,8 +248,8 @@ public static class PlainBinaryDictionaryValuesWriter extends DictionaryValuesWr /** * @param maxDictionaryByteSize */ - public PlainBinaryDictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage) { - super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage); + public PlainBinaryDictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage, ByteBufferAllocator allocator) { + super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage, allocator); binaryDictionaryContent.defaultReturnValue(-1); } @@ -243,10 +266,10 @@ public void writeBytes(Binary v) { } @Override - public DictionaryPage createDictionaryPage() { + public DictionaryPage toDictPageAndClose() { if (lastUsedDictionarySize > 0) { // return a dictionary only if we actually used it - PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize); + PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize, allocator); Iterator binaryIterator = binaryDictionaryContent.keySet().iterator(); // write only the part of the dict that we used for (int i = 0; i < lastUsedDictionarySize; i++) { @@ -294,10 +317,9 @@ public static class PlainFixedLenArrayDictionaryValuesWriter extends PlainBinary /** * @param maxDictionaryByteSize - * @param initialSize */ - public PlainFixedLenArrayDictionaryValuesWriter(int maxDictionaryByteSize, int length, Encoding encodingForDataPage, Encoding encodingForDictionaryPage) { - super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage); + public PlainFixedLenArrayDictionaryValuesWriter(int maxDictionaryByteSize, int length, Encoding encodingForDataPage, Encoding encodingForDictionaryPage, ByteBufferAllocator allocator) { + super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage, allocator); this.length = length; } @@ -313,10 +335,10 @@ public void writeBytes(Binary value) { } @Override - public DictionaryPage createDictionaryPage() { + public DictionaryPage toDictPageAndClose() { if (lastUsedDictionarySize > 0) { // return a dictionary only if we actually used it - FixedLenByteArrayPlainValuesWriter dictionaryEncoder = new FixedLenByteArrayPlainValuesWriter(length, lastUsedDictionaryByteSize, maxDictionaryByteSize); + FixedLenByteArrayPlainValuesWriter dictionaryEncoder = new FixedLenByteArrayPlainValuesWriter(length, lastUsedDictionaryByteSize, maxDictionaryByteSize, allocator); Iterator binaryIterator = binaryDictionaryContent.keySet().iterator(); // write only the part of the dict that we used for (int i = 0; i < lastUsedDictionarySize; i++) { @@ -339,10 +361,9 @@ public static class PlainLongDictionaryValuesWriter extends DictionaryValuesWrit /** * @param maxDictionaryByteSize - * @param initialSize */ - public PlainLongDictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage) { - super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage); + public PlainLongDictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage, ByteBufferAllocator allocator) { + super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage, allocator); longDictionaryContent.defaultReturnValue(-1); } @@ -358,10 +379,10 @@ public void writeLong(long v) { } @Override - public DictionaryPage createDictionaryPage() { + public DictionaryPage toDictPageAndClose() { if (lastUsedDictionarySize > 0) { // return a dictionary only if we actually used it - PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize); + PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize, allocator); LongIterator longIterator = longDictionaryContent.keySet().iterator(); // write only the part of the dict that we used for (int i = 0; i < lastUsedDictionarySize; i++) { @@ -411,10 +432,9 @@ public static class PlainDoubleDictionaryValuesWriter extends DictionaryValuesWr /** * @param maxDictionaryByteSize - * @param initialSize */ - public PlainDoubleDictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage) { - super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage); + public PlainDoubleDictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage, ByteBufferAllocator allocator) { + super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage, allocator); doubleDictionaryContent.defaultReturnValue(-1); } @@ -430,10 +450,10 @@ public void writeDouble(double v) { } @Override - public DictionaryPage createDictionaryPage() { + public DictionaryPage toDictPageAndClose() { if (lastUsedDictionarySize > 0) { // return a dictionary only if we actually used it - PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize); + PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize, allocator); DoubleIterator doubleIterator = doubleDictionaryContent.keySet().iterator(); // write only the part of the dict that we used for (int i = 0; i < lastUsedDictionarySize; i++) { @@ -483,10 +503,9 @@ public static class PlainIntegerDictionaryValuesWriter extends DictionaryValuesW /** * @param maxDictionaryByteSize - * @param initialSize */ - public PlainIntegerDictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage) { - super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage); + public PlainIntegerDictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage, ByteBufferAllocator allocator) { + super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage, allocator); intDictionaryContent.defaultReturnValue(-1); } @@ -502,10 +521,10 @@ public void writeInteger(int v) { } @Override - public DictionaryPage createDictionaryPage() { + public DictionaryPage toDictPageAndClose() { if (lastUsedDictionarySize > 0) { // return a dictionary only if we actually used it - PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize); + PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize, allocator); it.unimi.dsi.fastutil.ints.IntIterator intIterator = intDictionaryContent.keySet().iterator(); // write only the part of the dict that we used for (int i = 0; i < lastUsedDictionarySize; i++) { @@ -555,10 +574,9 @@ public static class PlainFloatDictionaryValuesWriter extends DictionaryValuesWri /** * @param maxDictionaryByteSize - * @param initialSize */ - public PlainFloatDictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage) { - super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage); + public PlainFloatDictionaryValuesWriter(int maxDictionaryByteSize, Encoding encodingForDataPage, Encoding encodingForDictionaryPage, ByteBufferAllocator allocator) { + super(maxDictionaryByteSize, encodingForDataPage, encodingForDictionaryPage, allocator); floatDictionaryContent.defaultReturnValue(-1); } @@ -574,10 +592,10 @@ public void writeFloat(float v) { } @Override - public DictionaryPage createDictionaryPage() { + public DictionaryPage toDictPageAndClose() { if (lastUsedDictionarySize > 0) { // return a dictionary only if we actually used it - PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize); + PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize, allocator); FloatIterator floatIterator = floatDictionaryContent.keySet().iterator(); // write only the part of the dict that we used for (int i = 0; i < lastUsedDictionarySize; i++) { diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/PlainValuesDictionary.java b/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/PlainValuesDictionary.java index e67131027c..0fa6cc66f2 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/PlainValuesDictionary.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/dictionary/PlainValuesDictionary.java @@ -23,6 +23,7 @@ import static org.apache.parquet.column.Encoding.PLAIN; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.Preconditions; import org.apache.parquet.column.Dictionary; @@ -86,9 +87,10 @@ public PlainBinaryDictionary(DictionaryPage dictionaryPage) throws IOException { */ public PlainBinaryDictionary(DictionaryPage dictionaryPage, Integer length) throws IOException { super(dictionaryPage); - final byte[] dictionaryBytes = dictionaryPage.getBytes().toByteArray(); + final ByteBuffer dictionaryBytes = dictionaryPage.getBytes().toByteBuffer(); binaryDictionaryContent = new Binary[dictionaryPage.getDictionarySize()]; - int offset = 0; + // dictionary values are stored in order: size (4 bytes LE) followed by {size} bytes + int offset = dictionaryBytes.position(); if (length == null) { // dictionary values are stored in order: size (4 bytes LE) followed by {size} bytes for (int i = 0; i < binaryDictionaryContent.length; i++) { @@ -96,7 +98,7 @@ public PlainBinaryDictionary(DictionaryPage dictionaryPage, Integer length) thro // read the length offset += 4; // wrap the content in a binary - binaryDictionaryContent[i] = Binary.fromConstantByteArray(dictionaryBytes, offset, len); + binaryDictionaryContent[i] = Binary.fromConstantByteBuffer(dictionaryBytes, offset, len); // increment to the next value offset += len; } @@ -106,7 +108,7 @@ public PlainBinaryDictionary(DictionaryPage dictionaryPage, Integer length) thro "Invalid byte array length: " + length); for (int i = 0; i < binaryDictionaryContent.length; i++) { // wrap the content in a Binary - binaryDictionaryContent[i] = Binary.fromConstantByteArray( + binaryDictionaryContent[i] = Binary.fromConstantByteBuffer( dictionaryBytes, offset, length); // increment to the next value offset += length; @@ -148,10 +150,10 @@ public static class PlainLongDictionary extends PlainValuesDictionary { */ public PlainLongDictionary(DictionaryPage dictionaryPage) throws IOException { super(dictionaryPage); - final byte[] dictionaryBytes = dictionaryPage.getBytes().toByteArray(); + final ByteBuffer dictionaryByteBuf = dictionaryPage.getBytes().toByteBuffer(); longDictionaryContent = new long[dictionaryPage.getDictionarySize()]; LongPlainValuesReader longReader = new LongPlainValuesReader(); - longReader.initFromPage(dictionaryPage.getDictionarySize(), dictionaryBytes, 0); + longReader.initFromPage(dictionaryPage.getDictionarySize(), dictionaryByteBuf, dictionaryByteBuf.position()); for (int i = 0; i < longDictionaryContent.length; i++) { longDictionaryContent[i] = longReader.readLong(); } @@ -191,10 +193,10 @@ public static class PlainDoubleDictionary extends PlainValuesDictionary { */ public PlainDoubleDictionary(DictionaryPage dictionaryPage) throws IOException { super(dictionaryPage); - final byte[] dictionaryBytes = dictionaryPage.getBytes().toByteArray(); + final ByteBuffer dictionaryByteBuf = dictionaryPage.getBytes().toByteBuffer(); doubleDictionaryContent = new double[dictionaryPage.getDictionarySize()]; DoublePlainValuesReader doubleReader = new DoublePlainValuesReader(); - doubleReader.initFromPage(dictionaryPage.getDictionarySize(), dictionaryBytes, 0); + doubleReader.initFromPage(dictionaryPage.getDictionarySize(), dictionaryByteBuf, 0); for (int i = 0; i < doubleDictionaryContent.length; i++) { doubleDictionaryContent[i] = doubleReader.readDouble(); } @@ -234,10 +236,10 @@ public static class PlainIntegerDictionary extends PlainValuesDictionary { */ public PlainIntegerDictionary(DictionaryPage dictionaryPage) throws IOException { super(dictionaryPage); - final byte[] dictionaryBytes = dictionaryPage.getBytes().toByteArray(); + final ByteBuffer dictionaryByteBuf = dictionaryPage.getBytes().toByteBuffer(); intDictionaryContent = new int[dictionaryPage.getDictionarySize()]; IntegerPlainValuesReader intReader = new IntegerPlainValuesReader(); - intReader.initFromPage(dictionaryPage.getDictionarySize(), dictionaryBytes, 0); + intReader.initFromPage(dictionaryPage.getDictionarySize(), dictionaryByteBuf, 0); for (int i = 0; i < intDictionaryContent.length; i++) { intDictionaryContent[i] = intReader.readInteger(); } @@ -277,10 +279,10 @@ public static class PlainFloatDictionary extends PlainValuesDictionary { */ public PlainFloatDictionary(DictionaryPage dictionaryPage) throws IOException { super(dictionaryPage); - final byte[] dictionaryBytes = dictionaryPage.getBytes().toByteArray(); + final ByteBuffer dictionaryByteBuf = dictionaryPage.getBytes().toByteBuffer(); floatDictionaryContent = new float[dictionaryPage.getDictionarySize()]; FloatPlainValuesReader floatReader = new FloatPlainValuesReader(); - floatReader.initFromPage(dictionaryPage.getDictionarySize(), dictionaryBytes, 0); + floatReader.initFromPage(dictionaryPage.getDictionarySize(), dictionaryByteBuf, dictionaryByteBuf.position()); for (int i = 0; i < floatDictionaryContent.length; i++) { floatDictionaryContent[i] = floatReader.readFloat(); } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/fallback/FallbackValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/fallback/FallbackValuesWriter.java index f66c7c9d89..19fed7d823 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/fallback/FallbackValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/fallback/FallbackValuesWriter.java @@ -97,11 +97,17 @@ public void reset() { currentWriter.reset(); } - public DictionaryPage createDictionaryPage() { + @Override + public void close() { + initialWriter.close(); + fallBackWriter.close(); + } + + public DictionaryPage toDictPageAndClose() { if (initialUsedAndHadDictionary) { - return initialWriter.createDictionaryPage(); + return initialWriter.toDictPageAndClose(); } else { - return currentWriter.createDictionaryPage(); + return currentWriter.toDictPageAndClose(); } } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BinaryPlainValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BinaryPlainValuesReader.java index 4346e02331..26f5e29796 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BinaryPlainValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BinaryPlainValuesReader.java @@ -21,6 +21,7 @@ import static org.apache.parquet.Log.DEBUG; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesUtils; @@ -30,7 +31,7 @@ public class BinaryPlainValuesReader extends ValuesReader { private static final Log LOG = Log.getLog(BinaryPlainValuesReader.class); - private byte[] in; + private ByteBuffer in; private int offset; @Override @@ -39,7 +40,7 @@ public Binary readBytes() { int length = BytesUtils.readIntLittleEndian(in, offset); int start = offset + 4; offset = start + length; - return Binary.fromConstantByteArray(in, start, length); + return Binary.fromConstantByteBuffer(in, start, length); } catch (IOException e) { throw new ParquetDecodingException("could not read bytes at offset " + offset, e); } catch (RuntimeException e) { @@ -60,11 +61,10 @@ public void skip() { } @Override - public void initFromPage(int valueCount, byte[] in, int offset) + public void initFromPage(int valueCount, ByteBuffer in, int offset) throws IOException { - if (DEBUG) LOG.debug("init from page at offset "+ offset + " for length " + (in.length - offset)); + if (DEBUG) LOG.debug("init from page at offset "+ offset + " for length " + (in.limit() - offset)); this.in = in; this.offset = offset; } - } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BooleanPlainValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BooleanPlainValuesReader.java index 31e711fef8..c3304907e2 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BooleanPlainValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BooleanPlainValuesReader.java @@ -22,6 +22,7 @@ import static org.apache.parquet.column.values.bitpacking.Packer.LITTLE_ENDIAN; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.Log; import org.apache.parquet.column.values.ValuesReader; @@ -62,8 +63,8 @@ public void skip() { * @see org.apache.parquet.column.values.ValuesReader#initFromPage(byte[], int) */ @Override - public void initFromPage(int valueCount, byte[] in, int offset) throws IOException { - if (DEBUG) LOG.debug("init from page at offset "+ offset + " for length " + (in.length - offset)); + public void initFromPage(int valueCount, ByteBuffer in, int offset) throws IOException { + if (DEBUG) LOG.debug("init from page at offset "+ offset + " for length " + (in.limit() - offset)); this.in.initFromPage(valueCount, in, offset); } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BooleanPlainValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BooleanPlainValuesWriter.java index 78920f08c5..c3e88ea908 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BooleanPlainValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/BooleanPlainValuesWriter.java @@ -18,6 +18,7 @@ */ package org.apache.parquet.column.values.plain; + import static org.apache.parquet.column.Encoding.PLAIN; import static org.apache.parquet.column.values.bitpacking.Packer.LITTLE_ENDIAN; import org.apache.parquet.bytes.BytesInput; @@ -60,6 +61,11 @@ public void reset() { bitPackingWriter.reset(); } + @Override + public void close() { + bitPackingWriter.close(); + } + @Override public long getAllocatedSize() { return bitPackingWriter.getAllocatedSize(); diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/FixedLenByteArrayPlainValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/FixedLenByteArrayPlainValuesReader.java index 098a48652e..8496e7e4c9 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/FixedLenByteArrayPlainValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/FixedLenByteArrayPlainValuesReader.java @@ -19,6 +19,7 @@ package org.apache.parquet.column.values.plain; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.Log; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.io.ParquetDecodingException; @@ -33,7 +34,7 @@ */ public class FixedLenByteArrayPlainValuesReader extends ValuesReader { private static final Log LOG = Log.getLog(FixedLenByteArrayPlainValuesReader.class); - private byte[] in; + private ByteBuffer in; private int offset; private int length; @@ -46,7 +47,7 @@ public Binary readBytes() { try { int start = offset; offset = start + length; - return Binary.fromConstantByteArray(in, start, length); + return Binary.fromConstantByteBuffer(in, start, length); } catch (RuntimeException e) { throw new ParquetDecodingException("could not read bytes at offset " + offset, e); } @@ -58,9 +59,9 @@ public void skip() { } @Override - public void initFromPage(int valueCount, byte[] in, int offset) + public void initFromPage(int valueCount, ByteBuffer in, int offset) throws IOException { - if (DEBUG) LOG.debug("init from page at offset "+ offset + " for length " + (in.length - offset)); + if (DEBUG) LOG.debug("init from page at offset "+ offset + " for length " + (in.limit() - offset)); this.in = in; this.offset = offset; } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/FixedLenByteArrayPlainValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/FixedLenByteArrayPlainValuesWriter.java index 986ae0b64f..6ab2dea4b1 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/FixedLenByteArrayPlainValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/FixedLenByteArrayPlainValuesWriter.java @@ -20,6 +20,7 @@ import java.io.IOException; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.CapacityByteArrayOutputStream; @@ -40,10 +41,13 @@ public class FixedLenByteArrayPlainValuesWriter extends ValuesWriter { private CapacityByteArrayOutputStream arrayOut; private LittleEndianDataOutputStream out; private int length; + private ByteBufferAllocator allocator; + - public FixedLenByteArrayPlainValuesWriter(int length, int initialSize, int pageSize) { + public FixedLenByteArrayPlainValuesWriter(int length, int initialSize, int pageSize, ByteBufferAllocator allocator) { this.length = length; - this.arrayOut = new CapacityByteArrayOutputStream(initialSize, pageSize); + this.allocator = allocator; + this.arrayOut = new CapacityByteArrayOutputStream(initialSize, pageSize, this.allocator); this.out = new LittleEndianDataOutputStream(arrayOut); } @@ -81,6 +85,11 @@ public void reset() { arrayOut.reset(); } + @Override + public void close() { + arrayOut.close(); + } + @Override public long getAllocatedSize() { return arrayOut.getCapacity(); diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesReader.java index bd938eeb93..c8fb303f12 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesReader.java @@ -20,9 +20,10 @@ import static org.apache.parquet.Log.DEBUG; -import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.ByteBuffer; +import org.apache.parquet.bytes.ByteBufferInputStream; import org.apache.parquet.Log; import org.apache.parquet.bytes.LittleEndianDataInputStream; import org.apache.parquet.column.values.ValuesReader; @@ -41,12 +42,16 @@ abstract public class PlainValuesReader extends ValuesReader { /** * {@inheritDoc} - * @see org.apache.parquet.column.values.ValuesReader#initFromPage(byte[], int) + * @see org.apache.parquet.column.values.ValuesReader#initFromPage(int, ByteBuffer, int) */ @Override - public void initFromPage(int valueCount, byte[] in, int offset) throws IOException { - if (DEBUG) LOG.debug("init from page at offset "+ offset + " for length " + (in.length - offset)); - this.in = new LittleEndianDataInputStream(new ByteArrayInputStream(in, offset, in.length - offset)); + public void initFromPage(int valueCount, ByteBuffer in, int offset) throws IOException { + if (DEBUG) LOG.debug("init from page at offset "+ offset + " for length " + (in.limit() - offset)); + this.in = new LittleEndianDataInputStream(toInputStream(in, offset)); + } + + private ByteBufferInputStream toInputStream(ByteBuffer in, int offset) { + return new ByteBufferInputStream(in.duplicate(), offset, in.limit() - offset); } public static class DoublePlainValuesReader extends PlainValuesReader { diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesWriter.java index f33bd811b7..add5495a39 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesWriter.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.nio.charset.Charset; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.CapacityByteArrayOutputStream; @@ -44,8 +45,8 @@ public class PlainValuesWriter extends ValuesWriter { private CapacityByteArrayOutputStream arrayOut; private LittleEndianDataOutputStream out; - public PlainValuesWriter(int initialSize, int pageSize) { - arrayOut = new CapacityByteArrayOutputStream(initialSize, pageSize); + public PlainValuesWriter(int initialSize, int pageSize, ByteBufferAllocator allocator) { + arrayOut = new CapacityByteArrayOutputStream(initialSize, pageSize, allocator); out = new LittleEndianDataOutputStream(arrayOut); } @@ -125,6 +126,12 @@ public void reset() { arrayOut.reset(); } + @Override + public void close() { + arrayOut.close(); + out.close(); + } + @Override public long getAllocatedSize() { return arrayOut.getCapacity(); diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridDecoder.java b/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridDecoder.java index 38eb354b52..1280e8d989 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridDecoder.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridDecoder.java @@ -20,10 +20,12 @@ import static org.apache.parquet.Log.DEBUG; -import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import org.apache.parquet.bytes.ByteBufferInputStream; import org.apache.parquet.Log; import org.apache.parquet.Preconditions; import org.apache.parquet.bytes.BytesUtils; @@ -43,14 +45,14 @@ private static enum MODE { RLE, PACKED } private final int bitWidth; private final BytePacker packer; - private final ByteArrayInputStream in; + private final InputStream in; private MODE mode; private int currentCount; private int currentValue; private int[] currentBuffer; - public RunLengthBitPackingHybridDecoder(int bitWidth, ByteArrayInputStream in) { + public RunLengthBitPackingHybridDecoder(int bitWidth, InputStream in) { if (DEBUG) LOG.debug("decoding bitWidth " + bitWidth); Preconditions.checkArgument(bitWidth >= 0 && bitWidth <= 32, "bitWidth must be >= 0 and <= 32"); diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridEncoder.java b/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridEncoder.java index 9d37574084..001d3f695e 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridEncoder.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridEncoder.java @@ -20,6 +20,7 @@ import java.io.IOException; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.Log; import org.apache.parquet.Preconditions; import org.apache.parquet.bytes.BytesInput; @@ -116,7 +117,7 @@ public class RunLengthBitPackingHybridEncoder { private boolean toBytesCalled; - public RunLengthBitPackingHybridEncoder(int bitWidth, int initialCapacity, int pageSize) { + public RunLengthBitPackingHybridEncoder(int bitWidth, int initialCapacity, int pageSize, ByteBufferAllocator allocator) { if (DEBUG) { LOG.debug(String.format("Encoding: RunLengthBitPackingHybridEncoder with " + "bithWidth: %d initialCapacity %d", bitWidth, initialCapacity)); @@ -125,7 +126,7 @@ public RunLengthBitPackingHybridEncoder(int bitWidth, int initialCapacity, int p Preconditions.checkArgument(bitWidth >= 0 && bitWidth <= 32, "bitWidth must be >= 0 and <= 32"); this.bitWidth = bitWidth; - this.baos = new CapacityByteArrayOutputStream(initialCapacity, pageSize); + this.baos = new CapacityByteArrayOutputStream(initialCapacity, pageSize, allocator); this.packBuffer = new byte[bitWidth]; this.bufferedValues = new int[8]; this.packer = Packer.LITTLE_ENDIAN.newBytePacker(bitWidth); @@ -281,6 +282,11 @@ public void reset() { reset(true); } + public void close() { + reset(false); + baos.close(); + } + public long getBufferedSize() { return baos.size(); } diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridValuesReader.java b/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridValuesReader.java index bd4e11d09a..4ccf2b86a3 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridValuesReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridValuesReader.java @@ -18,9 +18,10 @@ */ package org.apache.parquet.column.values.rle; -import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.ByteBuffer; +import org.apache.parquet.bytes.ByteBufferInputStream; import org.apache.parquet.bytes.BytesUtils; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.io.ParquetDecodingException; @@ -41,8 +42,8 @@ public RunLengthBitPackingHybridValuesReader(int bitWidth) { } @Override - public void initFromPage(int valueCountL, byte[] page, int offset) throws IOException { - ByteArrayInputStream in = new ByteArrayInputStream(page, offset, page.length - offset); + public void initFromPage(int valueCountL, ByteBuffer page, int offset) throws IOException { + ByteBufferInputStream in = new ByteBufferInputStream(page, offset, page.limit() - offset); int length = BytesUtils.readIntLittleEndian(in); decoder = new RunLengthBitPackingHybridDecoder(bitWidth, in); diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridValuesWriter.java index bccfd348ed..14ef1611e1 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/rle/RunLengthBitPackingHybridValuesWriter.java @@ -20,6 +20,7 @@ import java.io.IOException; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.Ints; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.column.Encoding; @@ -32,8 +33,8 @@ public class RunLengthBitPackingHybridValuesWriter extends ValuesWriter { private final RunLengthBitPackingHybridEncoder encoder; - public RunLengthBitPackingHybridValuesWriter(int bitWidth, int initialCapacity, int pageSize) { - this.encoder = new RunLengthBitPackingHybridEncoder(bitWidth, initialCapacity, pageSize); + public RunLengthBitPackingHybridValuesWriter(int bitWidth, int initialCapacity, int pageSize, ByteBufferAllocator allocator) { + this.encoder = new RunLengthBitPackingHybridEncoder(bitWidth, initialCapacity, pageSize, allocator); } @Override @@ -81,6 +82,11 @@ public void reset() { encoder.reset(); } + @Override + public void close() { + encoder.close(); + } + @Override public String memUsageString(String prefix) { return String.format("%s RunLengthBitPackingHybrid %d bytes", prefix, getAllocatedSize()); diff --git a/parquet-column/src/main/java/org/apache/parquet/io/api/Binary.java b/parquet-column/src/main/java/org/apache/parquet/io/api/Binary.java index f88d7408b9..ff833ecbd3 100644 --- a/parquet-column/src/main/java/org/apache/parquet/io/api/Binary.java +++ b/parquet-column/src/main/java/org/apache/parquet/io/api/Binary.java @@ -62,12 +62,16 @@ private Binary() { } abstract boolean equals(byte[] bytes, int offset, int length); + abstract boolean equals(ByteBuffer bytes, int offset, int length); + abstract boolean equals(Binary other); abstract public int compareTo(Binary other); abstract int compareTo(byte[] bytes, int offset, int length); + abstract int compareTo(ByteBuffer bytes, int offset, int length); + abstract public ByteBuffer toByteBuffer(); @Override @@ -173,6 +177,11 @@ boolean equals(byte[] other, int otherOffset, int otherLength) { return Binary.equals(value, offset, length, other, otherOffset, otherLength); } + @Override + boolean equals(ByteBuffer bytes, int otherOffset, int otherLength) { + return Binary.equals(value, offset, length, bytes, otherOffset, otherLength); + } + @Override public int compareTo(Binary other) { return other.compareTo(value, offset, length); @@ -183,6 +192,11 @@ int compareTo(byte[] other, int otherOffset, int otherLength) { return Binary.compareTwoByteArrays(value, offset, length, other, otherOffset, otherLength); } + @Override + int compareTo(ByteBuffer bytes, int otherOffset, int otherLength) { + return Binary.compareByteArrayToByteBuffer(value, offset, length, bytes, otherOffset, otherLength); + } + @Override public ByteBuffer toByteBuffer() { return ByteBuffer.wrap(value, offset, length); @@ -291,6 +305,11 @@ boolean equals(byte[] other, int otherOffset, int otherLength) { return Binary.equals(value, 0, value.length, other, otherOffset, otherLength); } + @Override + boolean equals(ByteBuffer bytes, int otherOffset, int otherLength) { + return Binary.equals(value, 0, value.length, bytes, otherOffset, otherLength); + } + @Override public int compareTo(Binary other) { return other.compareTo(value, 0, value.length); @@ -301,6 +320,11 @@ int compareTo(byte[] other, int otherOffset, int otherLength) { return Binary.compareTwoByteArrays(value, 0, value.length, other, otherOffset, otherLength); } + @Override + int compareTo(ByteBuffer bytes, int otherOffset, int otherLength) { + return Binary.compareByteArrayToByteBuffer(value, 0, value.length, bytes, otherOffset, otherLength); + } + @Override public ByteBuffer toByteBuffer() { return ByteBuffer.wrap(value); @@ -330,36 +354,58 @@ public static Binary fromByteArray(final byte[] value) { } private static class ByteBufferBackedBinary extends Binary { - private transient ByteBuffer value; - private transient byte[] cachedBytes; + private ByteBuffer value; + private byte[] cachedBytes; + private final int offset; + private final int length; - public ByteBufferBackedBinary(ByteBuffer value, boolean isBackingBytesReused) { + public ByteBufferBackedBinary(ByteBuffer value, int offset, int length, boolean isBackingBytesReused) { this.value = value; + this.offset = offset; + this.length = length; this.isBackingBytesReused = isBackingBytesReused; } @Override public String toStringUsingUTF8() { - return UTF8.decode(value).toString(); + int limit = value.limit(); + value.limit(offset+length); + int position = value.position(); + value.position(offset); + // no corresponding interface to read a subset of a buffer, would have to slice it + // which creates another ByteBuffer object or do what is done here to adjust the + // limit/offset and set them back after + String ret = UTF8.decode(value).toString(); + value.limit(limit); + value.position(position); + return ret; } @Override public int length() { - return value.remaining(); + return length; } @Override public void writeTo(OutputStream out) throws IOException { - // TODO: should not have to materialize those bytes - out.write(getBytesUnsafe()); + if (value.hasArray()) { + out.write(value.array(), value.arrayOffset() + offset, length); + } else { + out.write(getBytesUnsafe(), 0, length); + } } @Override public byte[] getBytes() { - byte[] bytes = new byte[value.remaining()]; + byte[] bytes = new byte[length]; - value.mark(); - value.get(bytes).reset(); + int limit = value.limit(); + value.limit(offset + length); + int position = value.position(); + value.position(offset); + value.get(bytes); + value.limit(limit); + value.position(position); if (!isBackingBytesReused) { // backing buffer might change cachedBytes = bytes; } @@ -375,60 +421,68 @@ public byte[] getBytesUnsafe() { public Binary slice(int start, int length) { return Binary.fromConstantByteArray(getBytesUnsafe(), start, length); } - @Override public int hashCode() { if (value.hasArray()) { - return Binary.hashCode(value.array(), value.arrayOffset() + value.position(), - value.arrayOffset() + value.remaining()); + return Binary.hashCode(value.array(), value.arrayOffset() + offset, length); + } else { + return Binary.hashCode(value, offset, length); } - byte[] bytes = getBytesUnsafe(); - return Binary.hashCode(bytes, 0, bytes.length); } @Override boolean equals(Binary other) { if (value.hasArray()) { - return other.equals(value.array(), value.arrayOffset() + value.position(), - value.arrayOffset() + value.remaining()); + return other.equals(value.array(), value.arrayOffset() + offset, length); + } else { + return other.equals(value, offset, length); } - byte[] bytes = getBytesUnsafe(); - return other.equals(bytes, 0, bytes.length); } @Override boolean equals(byte[] other, int otherOffset, int otherLength) { if (value.hasArray()) { - return Binary.equals(value.array(), value.arrayOffset() + value.position(), - value.arrayOffset() + value.remaining(), other, otherOffset, otherLength); + return Binary.equals(value.array(), value.arrayOffset() + offset, length, other, otherOffset, otherLength); + } else { + return Binary.equals(other, otherOffset, otherLength, value, offset, length); } - byte[] bytes = getBytesUnsafe(); - return Binary.equals(bytes, 0, bytes.length, other, otherOffset, otherLength); + } + + @Override + boolean equals(ByteBuffer otherBytes, int otherOffset, int otherLength) { + return Binary.equals(value, 0, length, otherBytes, otherOffset, otherLength); } @Override public int compareTo(Binary other) { if (value.hasArray()) { - return other.compareTo(value.array(), value.arrayOffset() + value.position(), - value.arrayOffset() + value.remaining()); + return other.compareTo(value.array(), value.arrayOffset() + offset, length); + } else { + return other.compareTo(value, offset, length); } - byte[] bytes = getBytesUnsafe(); - return other.compareTo(bytes, 0, bytes.length); } @Override int compareTo(byte[] other, int otherOffset, int otherLength) { if (value.hasArray()) { - return Binary.compareTwoByteArrays(value.array(), value.arrayOffset() + value.position(), - value.arrayOffset() + value.remaining(), other, otherOffset, otherLength); + return Binary.compareTwoByteArrays(value.array(), value.arrayOffset() + offset, length, + other, otherOffset, otherLength); + } { + return Binary.compareByteBufferToByteArray(value, offset, length, other, otherOffset, otherLength); } - byte[] bytes = getBytesUnsafe(); - return Binary.compareTwoByteArrays(bytes, 0, bytes.length, other, otherOffset, otherLength); + } + + @Override + int compareTo(ByteBuffer bytes, int otherOffset, int otherLength) { + return Binary.compareTwoByteBuffers(value, offset, length, bytes, otherOffset, otherLength); } @Override public ByteBuffer toByteBuffer() { - return value; + ByteBuffer ret = value.slice(); + ret.position(offset); + ret.limit(offset + length); + return ret; } @Override @@ -456,12 +510,20 @@ private void readObjectNoData() throws ObjectStreamException { } + public static Binary fromReusedByteBuffer(final ByteBuffer value, int offset, int length) { + return new ByteBufferBackedBinary(value, offset, length, true); + } + + public static Binary fromConstantByteBuffer(final ByteBuffer value, int offset, int length) { + return new ByteBufferBackedBinary(value, offset, length, false); + } + public static Binary fromReusedByteBuffer(final ByteBuffer value) { - return new ByteBufferBackedBinary(value, true); + return new ByteBufferBackedBinary(value, value.position(), value.remaining(), true); } public static Binary fromConstantByteBuffer(final ByteBuffer value) { - return new ByteBufferBackedBinary(value, false); + return new ByteBufferBackedBinary(value, value.position(), value.remaining(), false); } @Deprecated @@ -492,6 +554,39 @@ private static final int hashCode(byte[] array, int offset, int length) { return result; } + private static final int hashCode(ByteBuffer buf, int offset, int length) { + int result = 1; + for (int i = offset; i < offset + length; i++) { + byte b = buf.get(i); + result = 31 * result + b; + } + return result; + } + + private static final boolean equals(ByteBuffer buf1, int offset1, int length1, ByteBuffer buf2, int offset2, int length2) { + if (buf1 == null && buf2 == null) return true; + if (buf1 == null || buf2 == null) return false; + if (length1 != length2) return false; + for (int i = 0; i < length1; i++) { + if (buf1.get(i + offset1) != buf2.get(i + offset2)) { + return false; + } + } + return true; + } + + private static final boolean equals(byte[] array1, int offset1, int length1, ByteBuffer buf, int offset2, int length2) { + if (array1 == null && buf == null) return true; + if (array1 == null || buf == null) return false; + if (length1 != length2) return false; + for (int i = 0; i < length1; i++) { + if (array1[i + offset1] != buf.get(i + offset2)) { + return false; + } + } + return true; + } + /** * @see {@link Arrays#equals(byte[], byte[])} * @param array1 @@ -515,6 +610,47 @@ private static final boolean equals(byte[] array1, int offset1, int length1, byt return true; } + private static final int compareByteBufferToByteArray(ByteBuffer buf, int offset1, int length1, + byte[] array, int offset2, int length2) { + return -1 * Binary.compareByteArrayToByteBuffer(array, offset1, length1, buf, offset2, length2); + } + + private static final int compareByteArrayToByteBuffer(byte[] array1, int offset1, int length1, + ByteBuffer buf, int offset2, int length2) { + if (array1 == null && buf == null) return 0; + int min_length = (length1 < length2) ? length1 : length2; + for (int i = 0; i < min_length; i++) { + if (array1[i + offset1] < buf.get(i + offset2)) { + return 1; + } + if (array1[i + offset1] > buf.get(i + offset2)) { + return -1; + } + } + // check remainder + if (length1 == length2) { return 0; } + else if (length1 < length2) { return 1;} + else { return -1; } + } + + private static final int compareTwoByteBuffers(ByteBuffer buf1, int offset1, int length1, + ByteBuffer buf2, int offset2, int length2) { + if (buf1 == null && buf2 == null) return 0; + int min_length = (length1 < length2) ? length1 : length2; + for (int i = 0; i < min_length; i++) { + if (buf1.get(i + offset1) < buf2.get(i + offset2)) { + return 1; + } + if (buf1.get(i + offset1) > buf2.get(i + offset2)) { + return -1; + } + } + // check remainder + if (length1 == length2) { return 0; } + else if (length1 < length2) { return 1;} + else { return -1; } + } + private static final int compareTwoByteArrays(byte[] array1, int offset1, int length1, byte[] array2, int offset2, int length2) { if (array1 == null && array2 == null) return 0; diff --git a/parquet-column/src/main/java/org/apache/parquet/schema/PrimitiveType.java b/parquet-column/src/main/java/org/apache/parquet/schema/PrimitiveType.java index 7988f4aa4f..5c6e460cb7 100644 --- a/parquet-column/src/main/java/org/apache/parquet/schema/PrimitiveType.java +++ b/parquet-column/src/main/java/org/apache/parquet/schema/PrimitiveType.java @@ -333,10 +333,9 @@ public PrimitiveType(Repetition repetition, PrimitiveTypeName primitive, * @param decimalMeta (optional) metadata about the decimal type * @param id the id of the field */ - PrimitiveType( - Repetition repetition, PrimitiveTypeName primitive, - int length, String name, OriginalType originalType, - DecimalMetadata decimalMeta, ID id) { + public PrimitiveType(Repetition repetition, PrimitiveTypeName primitive, + int length, String name, OriginalType originalType, + DecimalMetadata decimalMeta, ID id) { super(name, repetition, originalType, id); this.primitive = primitive; this.length = length; diff --git a/parquet-column/src/test/java/org/apache/parquet/column/impl/TestColumnReaderImpl.java b/parquet-column/src/test/java/org/apache/parquet/column/impl/TestColumnReaderImpl.java index a1820e654e..6792361a64 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/impl/TestColumnReaderImpl.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/impl/TestColumnReaderImpl.java @@ -25,6 +25,7 @@ import org.apache.parquet.Version; import org.apache.parquet.VersionParser; +import org.apache.parquet.bytes.HeapByteBufferAllocator; import org.junit.Test; import org.apache.parquet.column.ColumnDescriptor; diff --git a/parquet-column/src/test/java/org/apache/parquet/column/impl/TestCorruptDeltaByteArrays.java b/parquet-column/src/test/java/org/apache/parquet/column/impl/TestCorruptDeltaByteArrays.java index 0327948c8d..9bb2759a10 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/impl/TestCorruptDeltaByteArrays.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/impl/TestCorruptDeltaByteArrays.java @@ -36,8 +36,10 @@ import org.apache.parquet.schema.PrimitiveType; import org.junit.Assert; import org.junit.Test; +import org.apache.parquet.bytes.HeapByteBufferAllocator; import java.lang.reflect.Field; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; @@ -74,16 +76,20 @@ public void testEncodingRequiresSequentailRead() { assertFalse(CorruptDeltaByteArrays.requiresSequentialReads(fixed, Encoding.DELTA_BYTE_ARRAY)); } + private DeltaByteArrayWriter getDeltaByteArrayWriter() { + return new DeltaByteArrayWriter(10, 100, new HeapByteBufferAllocator()); + } + @Test public void testReassemblyWithCorruptPage() throws Exception { - DeltaByteArrayWriter writer = new DeltaByteArrayWriter(10, 100); + DeltaByteArrayWriter writer = getDeltaByteArrayWriter(); String lastValue = null; for (int i = 0; i < 10; i += 1) { lastValue = str(i); writer.writeBytes(Binary.fromString(lastValue)); } - byte[] firstPageBytes = writer.getBytes().toByteArray(); + ByteBuffer firstPageBytes = writer.getBytes().toByteBuffer(); writer.reset(); // sets previous to new byte[0] corruptWriter(writer, lastValue); @@ -91,7 +97,7 @@ public void testReassemblyWithCorruptPage() throws Exception { for (int i = 10; i < 20; i += 1) { writer.writeBytes(Binary.fromString(str(i))); } - byte[] corruptPageBytes = writer.getBytes().toByteArray(); + ByteBuffer corruptPageBytes = writer.getBytes().toByteBuffer(); DeltaByteArrayReader firstPageReader = new DeltaByteArrayReader(); firstPageReader.initFromPage(10, firstPageBytes, 0); @@ -119,19 +125,19 @@ public void testReassemblyWithCorruptPage() throws Exception { @Test public void testReassemblyWithoutCorruption() throws Exception { - DeltaByteArrayWriter writer = new DeltaByteArrayWriter(10, 100); + DeltaByteArrayWriter writer = getDeltaByteArrayWriter(); for (int i = 0; i < 10; i += 1) { writer.writeBytes(Binary.fromString(str(i))); } - byte[] firstPageBytes = writer.getBytes().toByteArray(); + ByteBuffer firstPageBytes = writer.getBytes().toByteBuffer(); writer.reset(); // sets previous to new byte[0] for (int i = 10; i < 20; i += 1) { writer.writeBytes(Binary.fromString(str(i))); } - byte[] secondPageBytes = writer.getBytes().toByteArray(); + ByteBuffer secondPageBytes = writer.getBytes().toByteBuffer(); DeltaByteArrayReader firstPageReader = new DeltaByteArrayReader(); firstPageReader.initFromPage(10, firstPageBytes, 0); @@ -150,19 +156,19 @@ public void testReassemblyWithoutCorruption() throws Exception { @Test public void testOldReassemblyWithoutCorruption() throws Exception { - DeltaByteArrayWriter writer = new DeltaByteArrayWriter(10, 100); + DeltaByteArrayWriter writer = getDeltaByteArrayWriter(); for (int i = 0; i < 10; i += 1) { writer.writeBytes(Binary.fromString(str(i))); } - byte[] firstPageBytes = writer.getBytes().toByteArray(); + ByteBuffer firstPageBytes = writer.getBytes().toByteBuffer(); writer.reset(); // sets previous to new byte[0] for (int i = 10; i < 20; i += 1) { writer.writeBytes(Binary.fromString(str(i))); } - byte[] secondPageBytes = writer.getBytes().toByteArray(); + ByteBuffer secondPageBytes = writer.getBytes().toByteBuffer(); DeltaByteArrayReader firstPageReader = new DeltaByteArrayReader(); firstPageReader.initFromPage(10, firstPageBytes, 0); @@ -185,15 +191,16 @@ public void testColumnReaderImplWithCorruptPage() throws Exception { MemPageStore pages = new MemPageStore(0); PageWriter memWriter = pages.getPageWriter(column); + ParquetProperties parquetProps = new ParquetProperties(0, ParquetProperties.WriterVersion.PARQUET_1_0, false, new HeapByteBufferAllocator()); + // get generic repetition and definition level bytes to use for pages - ValuesWriter rdValues = ParquetProperties - .getColumnDescriptorValuesWriter(0, 10, 100); + ValuesWriter rdValues = parquetProps.getColumnDescriptorValuesWriter(0, 10, 100); for (int i = 0; i < 10; i += 1) { rdValues.writeInteger(0); } // use a byte array backed BytesInput because it is reused BytesInput rd = BytesInput.from(rdValues.getBytes().toByteArray()); - DeltaByteArrayWriter writer = new DeltaByteArrayWriter(10, 100); + DeltaByteArrayWriter writer = getDeltaByteArrayWriter(); String lastValue = null; List values = new ArrayList(); for (int i = 0; i < 10; i += 1) { diff --git a/parquet-column/src/test/java/org/apache/parquet/column/mem/TestMemColumn.java b/parquet-column/src/test/java/org/apache/parquet/column/mem/TestMemColumn.java index 135123f687..044fe2af0f 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/mem/TestMemColumn.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/mem/TestMemColumn.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals; +import org.apache.parquet.bytes.HeapByteBufferAllocator; import org.junit.Test; import org.apache.parquet.Log; @@ -160,6 +161,6 @@ public void testMemColumnSeveralPagesRepeated() throws Exception { } private ColumnWriteStoreV1 newColumnWriteStoreImpl(MemPageStore memPageStore) { - return new ColumnWriteStoreV1(memPageStore, 2048, 2048, false, WriterVersion.PARQUET_1_0); + return new ColumnWriteStoreV1(memPageStore, 2048, 2048, false, WriterVersion.PARQUET_1_0, new HeapByteBufferAllocator()); } } diff --git a/parquet-column/src/test/java/org/apache/parquet/column/page/mem/MemPageWriter.java b/parquet-column/src/test/java/org/apache/parquet/column/page/mem/MemPageWriter.java index d5bfe22a8d..ddab636319 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/page/mem/MemPageWriter.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/page/mem/MemPageWriter.java @@ -109,5 +109,4 @@ public String memUsageString(String prefix) { return String.format("%s %,d bytes", prefix, memSize); } - } diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/Utils.java b/parquet-column/src/test/java/org/apache/parquet/column/values/Utils.java index c9a62b406b..8caad2ba5c 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/Utils.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/Utils.java @@ -19,6 +19,7 @@ package org.apache.parquet.column.values; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.Random; import org.apache.parquet.io.api.Binary; @@ -61,7 +62,7 @@ public static void writeData(ValuesWriter writer, String[] strings) public static Binary[] readData(ValuesReader reader, byte[] data, int offset, int length) throws IOException { Binary[] bins = new Binary[length]; - reader.initFromPage(length, data, 0); + reader.initFromPage(length, ByteBuffer.wrap(data), 0); for(int i=0; i < length; i++) { bins[i] = reader.readBytes(); } @@ -76,7 +77,7 @@ public static Binary[] readData(ValuesReader reader, byte[] data, int length) public static int[] readInts(ValuesReader reader, byte[] data, int offset, int length) throws IOException { int[] ints = new int[length]; - reader.initFromPage(length, data, offset); + reader.initFromPage(length, ByteBuffer.wrap(data), offset); for(int i=0; i < length; i++) { ints[i] = reader.readInteger(); } diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/bitpacking/BitPackingPerfTest.java b/parquet-column/src/test/java/org/apache/parquet/column/values/bitpacking/BitPackingPerfTest.java index e74e78775f..2733b7269b 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/bitpacking/BitPackingPerfTest.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/bitpacking/BitPackingPerfTest.java @@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.ByteBuffer; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.column.values.bitpacking.BitPacking.BitPackingWriter; @@ -87,7 +88,7 @@ private static long readNTimes(byte[] bytes, int[] result, ValuesReader r) System.out.print(" no gc <"); for (int k = 0; k < N; k++) { long t2 = System.nanoTime(); - r.initFromPage(result.length, bytes, 0); + r.initFromPage(result.length, ByteBuffer.wrap(bytes), 0); for (int i = 0; i < result.length; i++) { result[i] = r.readInteger(); } diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/bitpacking/TestBitPackingColumn.java b/parquet-column/src/test/java/org/apache/parquet/column/values/bitpacking/TestBitPackingColumn.java index 2f311ec121..aef259c509 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/bitpacking/TestBitPackingColumn.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/bitpacking/TestBitPackingColumn.java @@ -23,9 +23,11 @@ import static org.apache.parquet.column.values.bitpacking.Packer.BIG_ENDIAN; import java.io.IOException; +import java.nio.ByteBuffer; import org.junit.Test; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.Log; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.column.values.ValuesWriter; @@ -172,7 +174,7 @@ private void validateEncodeDecode(int bitLength, int[] vals, String expected) th LOG.debug("bytes: " + TestBitPacking.toString(bytes)); assertEquals(type.toString(), expected, TestBitPacking.toString(bytes)); ValuesReader r = type.getReader(bound); - r.initFromPage(vals.length, bytes, 0); + r.initFromPage(vals.length, ByteBuffer.wrap(bytes), 0); int[] result = new int[vals.length]; for (int i = 0; i < result.length; i++) { result[i] = r.readInteger(); @@ -188,7 +190,7 @@ public ValuesReader getReader(final int bound) { return new BitPackingValuesReader(bound); } public ValuesWriter getWriter(final int bound) { - return new BitPackingValuesWriter(bound, 32*1024, 64*1024); + return new BitPackingValuesWriter(bound, 32*1024, 64*1024, new DirectByteBufferAllocator()); } } , diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/boundedint/TestBoundedColumns.java b/parquet-column/src/test/java/org/apache/parquet/column/values/boundedint/TestBoundedColumns.java index ba979b75d1..d1e43d283c 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/boundedint/TestBoundedColumns.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/boundedint/TestBoundedColumns.java @@ -23,11 +23,14 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Random; import org.junit.Test; +import org.apache.parquet.bytes.DirectByteBufferAllocator; + public class TestBoundedColumns { private final Random r = new Random(42L); @@ -54,7 +57,7 @@ public void testWriterNoRepeat() throws IOException { } private void compareOutput(int bound, int[] ints, String[] result) throws IOException { - BoundedIntValuesWriter bicw = new BoundedIntValuesWriter(bound, 64*1024, 64*1024); + BoundedIntValuesWriter bicw = new BoundedIntValuesWriter(bound, 64*1024, 64*1024, new DirectByteBufferAllocator()); for (int i : ints) { bicw.writeInteger(i); } @@ -63,7 +66,7 @@ private void compareOutput(int bound, int[] ints, String[] result) throws IOExce byte[] byteArray = bicw.getBytes().toByteArray(); assertEquals(concat(result), toBinaryString(byteArray, 4)); BoundedIntValuesReader bicr = new BoundedIntValuesReader(bound); - bicr.initFromPage(1, byteArray, 0); + bicr.initFromPage(1, ByteBuffer.wrap(byteArray), 0); String expected = ""; String got = ""; for (int i : ints) { @@ -123,7 +126,7 @@ public void testSerDe() throws Exception { ByteArrayOutputStream tmp = new ByteArrayOutputStream(); int[] stream = new int[totalValuesInStream]; - BoundedIntValuesWriter bicw = new BoundedIntValuesWriter(bound, 64 * 1024, 64*1024); + BoundedIntValuesWriter bicw = new BoundedIntValuesWriter(bound, 64 * 1024, 64*1024, new DirectByteBufferAllocator()); int idx = 0; for (int stripeNum = 0; stripeNum < valuesPerStripe.length; stripeNum++) { int next = 0; @@ -155,7 +158,7 @@ public void testSerDe() throws Exception { idx = 0; int offset = 0; for (int stripeNum = 0; stripeNum < valuesPerStripe.length; stripeNum++) { - bicr.initFromPage(1, input, offset); + bicr.initFromPage(1, ByteBuffer.wrap(input), offset); offset = bicr.getNextOffset(); for (int i = 0; i < valuesPerStripe[stripeNum]; i++) { int number = stream[idx++]; diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesWriterTest.java b/parquet-column/src/test/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesWriterTest.java index d428fbf8fa..6308e4788b 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesWriterTest.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/delta/DeltaBinaryPackingValuesWriterTest.java @@ -22,11 +22,13 @@ import static org.junit.Assert.assertTrue; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.Random; import org.junit.Before; import org.junit.Test; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.column.values.ValuesWriter; import org.apache.parquet.io.ParquetDecodingException; @@ -42,13 +44,13 @@ public class DeltaBinaryPackingValuesWriterTest { public void setUp() { blockSize = 128; miniBlockNum = 4; - writer = new DeltaBinaryPackingValuesWriter(blockSize, miniBlockNum, 100, 200); + writer = new DeltaBinaryPackingValuesWriter(blockSize, miniBlockNum, 100, 200, new DirectByteBufferAllocator()); random = new Random(); } @Test(expected = IllegalArgumentException.class) public void miniBlockSizeShouldBeMultipleOf8() { - new DeltaBinaryPackingValuesWriter(1281, 4, 100, 100); + new DeltaBinaryPackingValuesWriter(1281, 4, 100, 100, new DirectByteBufferAllocator()); } /* When data size is multiple of Block*/ @@ -154,7 +156,7 @@ public void shouldReturnCorrectOffsetAfterInitialization() throws IOException { System.arraycopy(valueContent, 0, pageContent, contentOffsetInPage, valueContent.length); //offset should be correct - reader.initFromPage(100, pageContent, contentOffsetInPage); + reader.initFromPage(100, ByteBuffer.wrap(pageContent), contentOffsetInPage); int offset= reader.getNextOffset(); assertEquals(valueContent.length + contentOffsetInPage, offset); @@ -187,7 +189,7 @@ public void shouldSkip() throws IOException { } writeData(data); reader = new DeltaBinaryPackingValuesReader(); - reader.initFromPage(100, writer.getBytes().toByteArray(), 0); + reader.initFromPage(100, writer.getBytes().toByteBuffer(), 0); for (int i = 0; i < data.length; i++) { if (i % 3 == 0) { reader.skip(); @@ -243,7 +245,7 @@ private void shouldReadAndWrite(int[] data, int length) throws IOException { + blockFlushed * miniBlockNum //bitWidth of mini blocks + (5.0 * blockFlushed);//min delta for each block assertTrue(estimatedSize >= page.length); - reader.initFromPage(100, page, 0); + reader.initFromPage(100, ByteBuffer.wrap(page), 0); for (int i = 0; i < length; i++) { assertEquals(data[i], reader.readInteger()); diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/BenchmarkIntegerOutputSize.java b/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/BenchmarkIntegerOutputSize.java index dc69fcc9c9..40f6bfc017 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/BenchmarkIntegerOutputSize.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/BenchmarkIntegerOutputSize.java @@ -19,6 +19,7 @@ package org.apache.parquet.column.values.delta.benchmark; import org.junit.Test; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesWriter; import org.apache.parquet.column.values.rle.RunLengthBitPackingHybridValuesWriter; import java.util.Random; @@ -77,8 +78,8 @@ public int getIntValue() { } public void testRandomIntegers(IntFunc func,int bitWidth) { - DeltaBinaryPackingValuesWriter delta=new DeltaBinaryPackingValuesWriter(blockSize,miniBlockNum, 100, 20000); - RunLengthBitPackingHybridValuesWriter rle= new RunLengthBitPackingHybridValuesWriter(bitWidth, 100, 20000); + DeltaBinaryPackingValuesWriter delta=new DeltaBinaryPackingValuesWriter(blockSize,miniBlockNum, 100, 20000, new DirectByteBufferAllocator()); + RunLengthBitPackingHybridValuesWriter rle= new RunLengthBitPackingHybridValuesWriter(bitWidth, 100, 20000, new DirectByteBufferAllocator()); for (int i = 0; i < dataSize; i++) { int v = func.getIntValue(); delta.writeInteger(v); diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/BenchmarkReadingRandomIntegers.java b/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/BenchmarkReadingRandomIntegers.java index 24b007f329..4ad5dadb18 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/BenchmarkReadingRandomIntegers.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/BenchmarkReadingRandomIntegers.java @@ -25,6 +25,7 @@ import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.column.values.ValuesWriter; import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesReader; @@ -33,6 +34,7 @@ import org.apache.parquet.column.values.rle.RunLengthBitPackingHybridValuesWriter; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.Random; @AxisRange(min = 0, max = 1) @@ -54,8 +56,8 @@ public static void prepare() throws IOException { data[i] = random.nextInt(100) - 200; } - ValuesWriter delta = new DeltaBinaryPackingValuesWriter(blockSize, miniBlockNum, 100, 20000); - ValuesWriter rle = new RunLengthBitPackingHybridValuesWriter(32, 100, 20000); + ValuesWriter delta = new DeltaBinaryPackingValuesWriter(blockSize, miniBlockNum, 100, 20000, new DirectByteBufferAllocator()); + ValuesWriter rle = new RunLengthBitPackingHybridValuesWriter(32, 100, 20000, new DirectByteBufferAllocator()); for (int i = 0; i < data.length; i++) { delta.writeInteger(data[i]); @@ -86,7 +88,7 @@ public void readingRLE() throws IOException { } private void readData(ValuesReader reader, byte[] deltaBytes) throws IOException { - reader.initFromPage(data.length, deltaBytes, 0); + reader.initFromPage(data.length, ByteBuffer.wrap(deltaBytes), 0); for (int i = 0; i < data.length; i++) { reader.readInteger(); } diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/RandomWritingBenchmarkTest.java b/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/RandomWritingBenchmarkTest.java index 50c97cf81c..80e65337af 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/RandomWritingBenchmarkTest.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/RandomWritingBenchmarkTest.java @@ -25,6 +25,7 @@ import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.column.values.ValuesWriter; import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesWriter; import org.apache.parquet.column.values.rle.RunLengthBitPackingHybridValuesWriter; @@ -50,21 +51,21 @@ public static void prepare() { @BenchmarkOptions(benchmarkRounds = 10, warmupRounds = 2) @Test public void writeDeltaPackingTest(){ - DeltaBinaryPackingValuesWriter writer = new DeltaBinaryPackingValuesWriter(blockSize, miniBlockNum, 100, 20000); + DeltaBinaryPackingValuesWriter writer = new DeltaBinaryPackingValuesWriter(blockSize, miniBlockNum, 100, 20000, new DirectByteBufferAllocator()); runWriteTest(writer); } @BenchmarkOptions(benchmarkRounds = 10, warmupRounds = 2) @Test public void writeRLETest(){ - ValuesWriter writer = new RunLengthBitPackingHybridValuesWriter(32, 100, 20000); + ValuesWriter writer = new RunLengthBitPackingHybridValuesWriter(32, 100, 20000, new DirectByteBufferAllocator()); runWriteTest(writer); } @BenchmarkOptions(benchmarkRounds = 10, warmupRounds = 2) @Test public void writeDeltaPackingTest2(){ - DeltaBinaryPackingValuesWriter writer = new DeltaBinaryPackingValuesWriter(blockSize, miniBlockNum, 100, 20000); + DeltaBinaryPackingValuesWriter writer = new DeltaBinaryPackingValuesWriter(blockSize, miniBlockNum, 100, 20000, new DirectByteBufferAllocator()); runWriteTest(writer); } } diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/SmallRangeWritingBenchmarkTest.java b/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/SmallRangeWritingBenchmarkTest.java index 3141fd728d..0dc7cb0d09 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/SmallRangeWritingBenchmarkTest.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/delta/benchmark/SmallRangeWritingBenchmarkTest.java @@ -23,6 +23,7 @@ import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart; import org.junit.BeforeClass; import org.junit.Test; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.column.values.ValuesWriter; import org.apache.parquet.column.values.rle.RunLengthBitPackingHybridValuesWriter; import java.util.Random; @@ -42,7 +43,7 @@ public static void prepare() { @BenchmarkOptions(benchmarkRounds = 10, warmupRounds = 2) @Test public void writeRLEWithSmallBitWidthTest(){ - ValuesWriter writer = new RunLengthBitPackingHybridValuesWriter(2, 100, 20000); + ValuesWriter writer = new RunLengthBitPackingHybridValuesWriter(2, 100, 20000, new DirectByteBufferAllocator()); runWriteTest(writer); } } diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/TestDeltaLengthByteArray.java b/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/TestDeltaLengthByteArray.java index aaae0645ce..d7ebee502d 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/TestDeltaLengthByteArray.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/TestDeltaLengthByteArray.java @@ -23,6 +23,7 @@ import org.junit.Test; import org.junit.Assert; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.column.values.Utils; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesReader; @@ -32,11 +33,15 @@ public class TestDeltaLengthByteArray { String[] values = { "parquet", "hadoop", "mapreduce"}; + private DeltaLengthByteArrayValuesWriter getDeltaLengthByteArrayValuesWriter() { + return new DeltaLengthByteArrayValuesWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); + } + @Test public void testSerialization () throws IOException { - DeltaLengthByteArrayValuesWriter writer = new DeltaLengthByteArrayValuesWriter(64 * 1024, 64 * 1024); + DeltaLengthByteArrayValuesWriter writer = getDeltaLengthByteArrayValuesWriter(); DeltaLengthByteArrayValuesReader reader = new DeltaLengthByteArrayValuesReader(); - + Utils.writeData(writer, values); Binary[] bin = Utils.readData(reader, writer.getBytes().toByteArray(), values.length); @@ -47,7 +52,7 @@ public void testSerialization () throws IOException { @Test public void testRandomStrings() throws IOException { - DeltaLengthByteArrayValuesWriter writer = new DeltaLengthByteArrayValuesWriter(64 * 1024, 64 * 1024); + DeltaLengthByteArrayValuesWriter writer = getDeltaLengthByteArrayValuesWriter(); DeltaLengthByteArrayValuesReader reader = new DeltaLengthByteArrayValuesReader(); String[] values = Utils.getRandomStringSamples(1000, 32); @@ -61,7 +66,7 @@ public void testRandomStrings() throws IOException { @Test public void testLengths() throws IOException { - DeltaLengthByteArrayValuesWriter writer = new DeltaLengthByteArrayValuesWriter(64 * 1024, 64 * 1024); + DeltaLengthByteArrayValuesWriter writer = getDeltaLengthByteArrayValuesWriter(); ValuesReader reader = new DeltaBinaryPackingValuesReader(); Utils.writeData(writer, values); diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/benchmark/BenchmarkDeltaLengthByteArray.java b/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/benchmark/BenchmarkDeltaLengthByteArray.java index f5f9d768ee..69c5e15c6e 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/benchmark/BenchmarkDeltaLengthByteArray.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/deltalengthbytearray/benchmark/BenchmarkDeltaLengthByteArray.java @@ -23,6 +23,7 @@ import org.junit.Rule; import org.junit.Test; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.column.values.Utils; import org.apache.parquet.column.values.deltalengthbytearray.DeltaLengthByteArrayValuesReader; import org.apache.parquet.column.values.deltalengthbytearray.DeltaLengthByteArrayValuesWriter; @@ -47,7 +48,7 @@ public class BenchmarkDeltaLengthByteArray { @BenchmarkOptions(benchmarkRounds = 20, warmupRounds = 4) @Test public void benchmarkRandomStringsWithPlainValuesWriter() throws IOException { - PlainValuesWriter writer = new PlainValuesWriter(64 * 1024, 64 * 1024); + PlainValuesWriter writer = new PlainValuesWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); BinaryPlainValuesReader reader = new BinaryPlainValuesReader(); Utils.writeData(writer, values); @@ -59,7 +60,7 @@ public void benchmarkRandomStringsWithPlainValuesWriter() throws IOException { @BenchmarkOptions(benchmarkRounds = 20, warmupRounds = 4) @Test public void benchmarkRandomStringsWithDeltaLengthByteArrayValuesWriter() throws IOException { - DeltaLengthByteArrayValuesWriter writer = new DeltaLengthByteArrayValuesWriter(64 * 1024, 64 * 1024); + DeltaLengthByteArrayValuesWriter writer = new DeltaLengthByteArrayValuesWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); DeltaLengthByteArrayValuesReader reader = new DeltaLengthByteArrayValuesReader(); Utils.writeData(writer, values); diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/TestDeltaByteArray.java b/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/TestDeltaByteArray.java index b62ef8447f..770f4dcc5b 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/TestDeltaByteArray.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/TestDeltaByteArray.java @@ -23,6 +23,7 @@ import org.junit.Test; import org.junit.Assert; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.column.values.Utils; import org.apache.parquet.column.values.ValuesReader; import org.apache.parquet.column.values.delta.DeltaBinaryPackingValuesReader; @@ -35,7 +36,7 @@ public class TestDeltaByteArray { @Test public void testSerialization () throws Exception { - DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024); + DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); DeltaByteArrayReader reader = new DeltaByteArrayReader(); assertReadWrite(writer, reader, values); @@ -43,14 +44,14 @@ public void testSerialization () throws Exception { @Test public void testRandomStrings() throws Exception { - DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024); + DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); DeltaByteArrayReader reader = new DeltaByteArrayReader(); assertReadWrite(writer, reader, randvalues); } @Test public void testLengths() throws IOException { - DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024); + DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); ValuesReader reader = new DeltaBinaryPackingValuesReader(); Utils.writeData(writer, values); @@ -82,7 +83,7 @@ private void assertReadWrite(DeltaByteArrayWriter writer, DeltaByteArrayReader r @Test public void testWriterReset() throws Exception { - DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024); + DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); assertReadWrite(writer, new DeltaByteArrayReader(), values); diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/benchmark/BenchmarkDeltaByteArray.java b/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/benchmark/BenchmarkDeltaByteArray.java index c61ef30a4f..eac4bd27e6 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/benchmark/BenchmarkDeltaByteArray.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/deltastrings/benchmark/BenchmarkDeltaByteArray.java @@ -24,6 +24,7 @@ import org.junit.Rule; import org.junit.Test; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.column.values.Utils; import org.apache.parquet.column.values.deltastrings.DeltaByteArrayReader; import org.apache.parquet.column.values.deltastrings.DeltaByteArrayWriter; @@ -54,7 +55,7 @@ public class BenchmarkDeltaByteArray { @BenchmarkOptions(benchmarkRounds = 20, warmupRounds = 4) @Test public void benchmarkRandomStringsWithPlainValuesWriter() throws IOException { - PlainValuesWriter writer = new PlainValuesWriter(64 * 1024, 64 * 1024); + PlainValuesWriter writer = new PlainValuesWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); BinaryPlainValuesReader reader = new BinaryPlainValuesReader(); Utils.writeData(writer, values); @@ -66,7 +67,7 @@ public void benchmarkRandomStringsWithPlainValuesWriter() throws IOException { @BenchmarkOptions(benchmarkRounds = 20, warmupRounds = 4) @Test public void benchmarkRandomStringsWithDeltaLengthByteArrayValuesWriter() throws IOException { - DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024); + DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); DeltaByteArrayReader reader = new DeltaByteArrayReader(); Utils.writeData(writer, values); @@ -78,7 +79,7 @@ public void benchmarkRandomStringsWithDeltaLengthByteArrayValuesWriter() throws @BenchmarkOptions(benchmarkRounds = 20, warmupRounds = 4) @Test public void benchmarkSortedStringsWithPlainValuesWriter() throws IOException { - PlainValuesWriter writer = new PlainValuesWriter(64 * 1024, 64 * 1024); + PlainValuesWriter writer = new PlainValuesWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); BinaryPlainValuesReader reader = new BinaryPlainValuesReader(); Utils.writeData(writer, sortedVals); @@ -90,7 +91,7 @@ public void benchmarkSortedStringsWithPlainValuesWriter() throws IOException { @BenchmarkOptions(benchmarkRounds = 20, warmupRounds = 4) @Test public void benchmarkSortedStringsWithDeltaLengthByteArrayValuesWriter() throws IOException { - DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024); + DeltaByteArrayWriter writer = new DeltaByteArrayWriter(64 * 1024, 64 * 1024, new DirectByteBufferAllocator()); DeltaByteArrayReader reader = new DeltaByteArrayReader(); Utils.writeData(writer, sortedVals); diff --git a/parquet-column/src/test/java/org/apache/parquet/column/values/dictionary/TestDictionary.java b/parquet-column/src/test/java/org/apache/parquet/column/values/dictionary/TestDictionary.java index 020868edbb..ada1c93cfb 100644 --- a/parquet-column/src/test/java/org/apache/parquet/column/values/dictionary/TestDictionary.java +++ b/parquet-column/src/test/java/org/apache/parquet/column/values/dictionary/TestDictionary.java @@ -28,10 +28,12 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; import org.junit.Assert; import org.junit.Test; +import org.apache.parquet.bytes.DirectByteBufferAllocator; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.column.ColumnDescriptor; import org.apache.parquet.column.Dictionary; @@ -54,27 +56,27 @@ public class TestDictionary { private FallbackValuesWriter plainFallBack(I dvw, int initialSize) { - return FallbackValuesWriter.of(dvw, new PlainValuesWriter(initialSize, initialSize * 5)); + return FallbackValuesWriter.of(dvw, new PlainValuesWriter(initialSize, initialSize * 5, new DirectByteBufferAllocator())); } private FallbackValuesWriter newPlainBinaryDictionaryValuesWriter(int maxDictionaryByteSize, int initialSize) { - return plainFallBack(new PlainBinaryDictionaryValuesWriter(maxDictionaryByteSize, PLAIN_DICTIONARY, PLAIN_DICTIONARY), initialSize); + return plainFallBack(new PlainBinaryDictionaryValuesWriter(maxDictionaryByteSize, PLAIN_DICTIONARY, PLAIN_DICTIONARY, new DirectByteBufferAllocator()), initialSize); } private FallbackValuesWriter newPlainLongDictionaryValuesWriter(int maxDictionaryByteSize, int initialSize) { - return plainFallBack(new PlainLongDictionaryValuesWriter(maxDictionaryByteSize, PLAIN_DICTIONARY, PLAIN_DICTIONARY), initialSize); + return plainFallBack(new PlainLongDictionaryValuesWriter(maxDictionaryByteSize, PLAIN_DICTIONARY, PLAIN_DICTIONARY, new DirectByteBufferAllocator()), initialSize); } private FallbackValuesWriter newPlainIntegerDictionaryValuesWriter(int maxDictionaryByteSize, int initialSize) { - return plainFallBack(new PlainIntegerDictionaryValuesWriter(maxDictionaryByteSize, PLAIN_DICTIONARY, PLAIN_DICTIONARY), initialSize); + return plainFallBack(new PlainIntegerDictionaryValuesWriter(maxDictionaryByteSize, PLAIN_DICTIONARY, PLAIN_DICTIONARY, new DirectByteBufferAllocator()), initialSize); } private FallbackValuesWriter newPlainDoubleDictionaryValuesWriter(int maxDictionaryByteSize, int initialSize) { - return plainFallBack(new PlainDoubleDictionaryValuesWriter(maxDictionaryByteSize, PLAIN_DICTIONARY, PLAIN_DICTIONARY), initialSize); + return plainFallBack(new PlainDoubleDictionaryValuesWriter(maxDictionaryByteSize, PLAIN_DICTIONARY, PLAIN_DICTIONARY, new DirectByteBufferAllocator()), initialSize); } private FallbackValuesWriter newPlainFloatDictionaryValuesWriter(int maxDictionaryByteSize, int initialSize) { - return plainFallBack(new PlainFloatDictionaryValuesWriter(maxDictionaryByteSize, PLAIN_DICTIONARY, PLAIN_DICTIONARY), initialSize); + return plainFallBack(new PlainFloatDictionaryValuesWriter(maxDictionaryByteSize, PLAIN_DICTIONARY, PLAIN_DICTIONARY, new DirectByteBufferAllocator()), initialSize); } @Test @@ -116,7 +118,7 @@ public void testBinaryDictionaryFallBack() throws IOException { //Fallbacked to Plain encoding, therefore use PlainValuesReader to read it back ValuesReader reader = new BinaryPlainValuesReader(); - reader.initFromPage(100, cw.getBytes().toByteArray(), 0); + reader.initFromPage(100, cw.getBytes().toByteBuffer(), 0); for (long i = 0; i < 100; i++) { assertEquals(Binary.fromString("str" + i), reader.readBytes()); @@ -202,13 +204,13 @@ public void testLongDictionary() throws IOException { DictionaryValuesReader cr = initDicReader(cw, PrimitiveTypeName.INT64); - cr.initFromPage(COUNT, bytes1.toByteArray(), 0); + cr.initFromPage(COUNT, bytes1.toByteBuffer(), 0); for (long i = 0; i < COUNT; i++) { long back = cr.readLong(); assertEquals(i % 50, back); } - cr.initFromPage(COUNT2, bytes2.toByteArray(), 0); + cr.initFromPage(COUNT2, bytes2.toByteBuffer(), 0); for (long i = COUNT2; i > 0; i--) { long back = cr.readLong(); assertEquals(i % 50, back); @@ -226,7 +228,7 @@ private void roundTripLong(FallbackValuesWriter 0; i--) { double back = cr.readDouble(); assertEquals(i % 50, back, 0.0); @@ -297,7 +299,7 @@ private void roundTripDouble(FallbackValuesWriter 0; i--) { int back = cr.readInteger(); assertEquals(i % 50, back); @@ -368,7 +370,7 @@ private void roundTripInt(FallbackValuesWriter 0; i--) { float back = cr.readFloat(); assertEquals(i % 50, back, 0.0f); @@ -439,7 +441,7 @@ private void roundTripFloat(FallbackValuesWriter + * 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 org.apache.parquet; + +/** + * Runtime exception indicating that a stream failed to be closed properly. + * + * Used to wrap up the checked IOException usually thrown from IO operations, + * these are generally not recoverable so it does not make sense to pollute the + * codebase declaring that they can be thrown whenever resources are being + * closed out. + */ +public class OutputStreamCloseException extends ParquetRuntimeException { + + private static final long serialVersionUID = 1L; + + public OutputStreamCloseException() { + } + + public OutputStreamCloseException(String message, Throwable cause) { + super(message, cause); + } + + public OutputStreamCloseException(String message) { + super(message); + } + + public OutputStreamCloseException(Throwable cause) { + super(cause); + } +} diff --git a/parquet-common/src/main/java/org/apache/parquet/ParquetRuntimeException.java b/parquet-common/src/main/java/org/apache/parquet/ParquetRuntimeException.java index f67b15aa5e..d0f13a8f12 100644 --- a/parquet-common/src/main/java/org/apache/parquet/ParquetRuntimeException.java +++ b/parquet-common/src/main/java/org/apache/parquet/ParquetRuntimeException.java @@ -18,6 +18,9 @@ */ package org.apache.parquet; +import java.io.Closeable; +import java.io.IOException; + /** * The parent class for all runtime exceptions * @@ -42,5 +45,4 @@ public ParquetRuntimeException(String message) { public ParquetRuntimeException(Throwable cause) { super(cause); } - } diff --git a/parquet-common/src/main/java/org/apache/parquet/bytes/ByteBufferAllocator.java b/parquet-common/src/main/java/org/apache/parquet/bytes/ByteBufferAllocator.java new file mode 100644 index 0000000000..ee36b74094 --- /dev/null +++ b/parquet-common/src/main/java/org/apache/parquet/bytes/ByteBufferAllocator.java @@ -0,0 +1,38 @@ +/* + * 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 + * + * 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 org.apache.parquet.bytes; + +import java.nio.ByteBuffer; + +public interface ByteBufferAllocator { + ByteBuffer allocate(int size); + + /** + * For RefCounted implementations using direct memory, the release method + * needs to be called to free references to the allocated memory. + */ + void release(ByteBuffer b); + + /** + * Indicates if this allocator will produce ByteBuffers backed by direct memory. + * + * @return true if direct memory backed buffers will be created by this allocator, else false + */ + boolean isDirect(); +} diff --git a/parquet-common/src/main/java/org/apache/parquet/bytes/ByteBufferInputStream.java b/parquet-common/src/main/java/org/apache/parquet/bytes/ByteBufferInputStream.java new file mode 100644 index 0000000000..5b3b8533b0 --- /dev/null +++ b/parquet-common/src/main/java/org/apache/parquet/bytes/ByteBufferInputStream.java @@ -0,0 +1,82 @@ +/* + * 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 + * + * 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 org.apache.parquet.bytes; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; + +/** + * This ByteBufferInputStream does not consume the ByteBuffer being passed in, + * but will create a slice of the current buffer. + */ +public class ByteBufferInputStream extends InputStream { + + protected ByteBuffer byteBuf; + protected int initPos; + protected int count; + public ByteBufferInputStream(ByteBuffer buffer) { + this(buffer, buffer.position(), buffer.remaining()); + } + + public ByteBufferInputStream(ByteBuffer buffer, int offset, int count) { + ByteBuffer temp = buffer.duplicate(); + temp.position(offset); + byteBuf = temp.slice(); + byteBuf.limit(count); + this.initPos = offset; + this.count = count; + } + + public ByteBuffer toByteBuffer() { + return byteBuf.slice(); + } + + @Override + public int read() throws IOException { + if (!byteBuf.hasRemaining()) { + return -1; + } + //Workaround for unsigned byte + return byteBuf.get() & 0xFF; + } + + @Override + public int read(byte[] bytes, int offset, int length) throws IOException { + int count = Math.min(byteBuf.remaining(), length); + if (count == 0) return -1; + byteBuf.get(bytes, offset, count); + return count; + } + + @Override + public long skip(long n) { + if (n > byteBuf.remaining()) + n = byteBuf.remaining(); + int pos = byteBuf.position(); + byteBuf.position((int)(pos + n)); + return n; + } + + + @Override + public int available() { + return byteBuf.remaining(); + } +} diff --git a/parquet-common/src/main/java/org/apache/parquet/bytes/BytesUtils.java b/parquet-common/src/main/java/org/apache/parquet/bytes/BytesUtils.java index d96a1e52a1..d40721a8f4 100644 --- a/parquet-common/src/main/java/org/apache/parquet/bytes/BytesUtils.java +++ b/parquet-common/src/main/java/org/apache/parquet/bytes/BytesUtils.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.ByteBuffer; import java.nio.charset.Charset; import org.apache.parquet.Log; @@ -46,6 +47,21 @@ public static int getWidthFromMaxInt(int bound) { return 32 - Integer.numberOfLeadingZeros(bound); } + /** + * reads an int in little endian at the given position + * @param in + * @param offset + * @return + * @throws IOException + */ + public static int readIntLittleEndian(ByteBuffer in, int offset) throws IOException { + int ch4 = in.get(offset) & 0xff; + int ch3 = in.get(offset + 1) & 0xff; + int ch2 = in.get(offset + 2) & 0xff; + int ch1 = in.get(offset + 3) & 0xff; + return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); + } + /** * reads an int in little endian at the given position * @param in @@ -205,6 +221,14 @@ public static void writeUnsignedVarInt(int value, OutputStream out) throws IOExc out.write(value & 0x7F); } + public static void writeUnsignedVarInt(int value, ByteBuffer dest) throws IOException { + while ((value & 0xFFFFFF80) != 0L) { + dest.putInt((value & 0x7F) | 0x80); + value >>>= 7; + } + dest.putInt(value & 0x7F); + } + public static void writeZigZagVarInt(int intValue, OutputStream out) throws IOException{ writeUnsignedVarInt((intValue << 1) ^ (intValue >> 31), out); } diff --git a/parquet-common/src/main/java/org/apache/parquet/bytes/DirectByteBufferAllocator.java b/parquet-common/src/main/java/org/apache/parquet/bytes/DirectByteBufferAllocator.java new file mode 100644 index 0000000000..9fe45381ee --- /dev/null +++ b/parquet-common/src/main/java/org/apache/parquet/bytes/DirectByteBufferAllocator.java @@ -0,0 +1,43 @@ +/* + * 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 + * + * 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 org.apache.parquet.bytes; + +import java.nio.ByteBuffer; + +public class DirectByteBufferAllocator implements ByteBufferAllocator{ + public static final DirectByteBufferAllocator getInstance(){return new DirectByteBufferAllocator();} + public DirectByteBufferAllocator() { + super(); + } + + public ByteBuffer allocate(final int size) { + return ByteBuffer.allocateDirect(size); + } + + @Override + public void release(ByteBuffer b) { + // The ByteBuffer.allocateDirect + return; + } + + @Override + public boolean isDirect() { + return true; + } +} diff --git a/parquet-common/src/main/java/org/apache/parquet/bytes/HeapByteBufferAllocator.java b/parquet-common/src/main/java/org/apache/parquet/bytes/HeapByteBufferAllocator.java new file mode 100644 index 0000000000..c5f475d2f0 --- /dev/null +++ b/parquet-common/src/main/java/org/apache/parquet/bytes/HeapByteBufferAllocator.java @@ -0,0 +1,44 @@ +/* + * 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 + * + * 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 org.apache.parquet.bytes; + +import java.nio.ByteBuffer; + +public class HeapByteBufferAllocator implements ByteBufferAllocator{ + + public static final HeapByteBufferAllocator getInstance(){ return new HeapByteBufferAllocator();} + + public HeapByteBufferAllocator() { + super(); + } + + public ByteBuffer allocate(final int size) { + return ByteBuffer.allocate(size); + } + + public void release(ByteBuffer b) { + return; + } + + @Override + public boolean isDirect() { + return false; + } + +} diff --git a/parquet-encoding/src/main/java/org/apache/parquet/bytes/BytesInput.java b/parquet-encoding/src/main/java/org/apache/parquet/bytes/BytesInput.java index ac334ae5a8..40190ee3bf 100644 --- a/parquet-encoding/src/main/java/org/apache/parquet/bytes/BytesInput.java +++ b/parquet-encoding/src/main/java/org/apache/parquet/bytes/BytesInput.java @@ -25,6 +25,9 @@ import java.io.OutputStream; import java.util.Arrays; import java.util.List; +import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.WritableByteChannel; import org.apache.parquet.Log; @@ -71,6 +74,15 @@ public static BytesInput concat(List inputs) { public static BytesInput from(InputStream in, int bytes) { return new StreamBytesInput(in, bytes); } + + /** + * @param buffer + * @param length number of bytes to read + * @return a BytesInput that will read the given bytes from the ByteBuffer + */ + public static BytesInput from(ByteBuffer buffer, int offset, int length) { + return new ByteBufferBytesInput(buffer, offset, length); + } /** * @@ -121,7 +133,7 @@ public static BytesInput from(CapacityByteArrayOutputStream arrayOut) { } /** - * @param arrayOut + * @param baos - stream to wrap into a BytesInput * @return a BytesInput that will write the content of the buffer */ public static BytesInput from(ByteArrayOutputStream baos) { @@ -164,6 +176,24 @@ public byte[] toByteArray() throws IOException { return baos.getBuf(); } + /** + * + * @return a new ByteBuffer materializing the contents of this input + * @throws IOException + */ + public ByteBuffer toByteBuffer() throws IOException { + return ByteBuffer.wrap(toByteArray()); + } + + /** + * + * @return a new InputStream materializing the contents of this input + * @throws IOException + */ + public InputStream toInputStream() throws IOException { + return new ByteBufferInputStream(toByteBuffer()); + } + /** * * @return the size in bytes that would be written @@ -258,6 +288,10 @@ public void writeAllTo(OutputStream out) throws IOException { BytesUtils.writeIntLittleEndian(out, intValue); } + public ByteBuffer toByteBuffer() throws IOException { + return ByteBuffer.allocate(4).putInt(0, intValue); + } + @Override public long size() { return 4; @@ -278,6 +312,12 @@ public void writeAllTo(OutputStream out) throws IOException { BytesUtils.writeUnsignedVarInt(intValue, out); } + public ByteBuffer toByteBuffer() throws IOException { + ByteBuffer ret = ByteBuffer.allocate((int) size()); + BytesUtils.writeUnsignedVarInt(intValue, ret); + return ret; + } + @Override public long size() { int s = 5 - ((Integer.numberOfLeadingZeros(intValue) + 3) / 7); @@ -296,6 +336,10 @@ public long size() { return 0; } + public ByteBuffer toByteBuffer() throws IOException { + return ByteBuffer.allocate(0); + } + } private static class CapacityBAOSBytesInput extends BytesInput { @@ -355,11 +399,49 @@ public void writeAllTo(OutputStream out) throws IOException { out.write(in, offset, length); } + public ByteBuffer toByteBuffer() throws IOException { + return ByteBuffer.wrap(in, offset, length); + } + @Override public long size() { return length; } } + + private static class ByteBufferBytesInput extends BytesInput { + + private final ByteBuffer byteBuf; + private final int length; + private final int offset; + private ByteBufferBytesInput(ByteBuffer byteBuf, int offset, int length) { + this.byteBuf = byteBuf; + this.offset = offset; + this.length = length; + } + + @Override + public void writeAllTo(OutputStream out) throws IOException { + final WritableByteChannel outputChannel = Channels.newChannel(out); + byteBuf.position(offset); + ByteBuffer tempBuf = byteBuf.slice(); + tempBuf.limit(length); + outputChannel.write(tempBuf); + } + + @Override + public ByteBuffer toByteBuffer() throws IOException { + byteBuf.position(offset); + ByteBuffer buf = byteBuf.slice(); + buf.limit(length); + return buf; + } + + @Override + public long size() { + return length; + } + } } diff --git a/parquet-encoding/src/main/java/org/apache/parquet/bytes/CapacityByteArrayOutputStream.java b/parquet-encoding/src/main/java/org/apache/parquet/bytes/CapacityByteArrayOutputStream.java index 1670f9ca9f..61555653d6 100644 --- a/parquet-encoding/src/main/java/org/apache/parquet/bytes/CapacityByteArrayOutputStream.java +++ b/parquet-encoding/src/main/java/org/apache/parquet/bytes/CapacityByteArrayOutputStream.java @@ -21,16 +21,17 @@ import static java.lang.Math.max; import static java.lang.Math.pow; import static java.lang.String.format; -import static java.lang.System.arraycopy; import static org.apache.parquet.Preconditions.checkArgument; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; import org.apache.parquet.Log; +import org.apache.parquet.OutputStreamCloseException; /** * Similar to a {@link ByteArrayOutputStream}, but uses a different strategy for growing that does not involve copying. @@ -54,16 +55,17 @@ */ public class CapacityByteArrayOutputStream extends OutputStream { private static final Log LOG = Log.getLog(CapacityByteArrayOutputStream.class); - private static final byte[] EMPTY_SLAB = new byte[0]; + private static final ByteBuffer EMPTY_SLAB = ByteBuffer.wrap(new byte[0]); private int initialSlabSize; private final int maxCapacityHint; - private final List slabs = new ArrayList(); + private final List slabs = new ArrayList(); - private byte[] currentSlab; + private ByteBuffer currentSlab; private int currentSlabIndex; private int bytesAllocated = 0; private int bytesUsed = 0; + private ByteBufferAllocator allocator; /** * Return an initial slab size such that a CapacityByteArrayOutputStream constructed with it @@ -86,38 +88,64 @@ public static int initialSlabSizeHeuristic(int minSlabSize, int targetCapacity, return max(minSlabSize, ((int) (targetCapacity / pow(2, targetNumSlabs)))); } + public static CapacityByteArrayOutputStream withTargetNumSlabs( + int minSlabSize, int maxCapacityHint, int targetNumSlabs) { + return withTargetNumSlabs(minSlabSize, maxCapacityHint, targetNumSlabs, new HeapByteBufferAllocator()); + } + /** * Construct a CapacityByteArrayOutputStream configured such that its initial slab size is * determined by {@link #initialSlabSizeHeuristic}, with targetCapacity == maxCapacityHint */ public static CapacityByteArrayOutputStream withTargetNumSlabs( - int minSlabSize, int maxCapacityHint, int targetNumSlabs) { + int minSlabSize, int maxCapacityHint, int targetNumSlabs, ByteBufferAllocator allocator) { return new CapacityByteArrayOutputStream( initialSlabSizeHeuristic(minSlabSize, maxCapacityHint, targetNumSlabs), - maxCapacityHint); + maxCapacityHint, allocator); } /** * Defaults maxCapacityHint to 1MB * @param initialSlabSize - * @deprecated use {@link CapacityByteArrayOutputStream#CapacityByteArrayOutputStream(int, int)} + * @deprecated use {@link CapacityByteArrayOutputStream#CapacityByteArrayOutputStream(int, int, ByteBufferAllocator)} */ @Deprecated public CapacityByteArrayOutputStream(int initialSlabSize) { - this(initialSlabSize, 1024 * 1024); + this(initialSlabSize, 1024 * 1024, new HeapByteBufferAllocator()); + } + + /** + * Defaults maxCapacityHint to 1MB + * @param initialSlabSize + * @deprecated use {@link CapacityByteArrayOutputStream#CapacityByteArrayOutputStream(int, int, ByteBufferAllocator)} + */ + @Deprecated + public CapacityByteArrayOutputStream(int initialSlabSize, ByteBufferAllocator allocator) { + this(initialSlabSize, 1024 * 1024, allocator); } /** * @param initialSlabSize the size to make the first slab * @param maxCapacityHint a hint (not guarantee) of the max amount of data written to this stream + * @deprecated use {@link CapacityByteArrayOutputStream#CapacityByteArrayOutputStream(int, int, ByteBufferAllocator)} */ + @Deprecated public CapacityByteArrayOutputStream(int initialSlabSize, int maxCapacityHint) { + this(initialSlabSize, maxCapacityHint, new HeapByteBufferAllocator()); + } + + /** + * @param initialSlabSize the size to make the first slab + * @param maxCapacityHint a hint (not guarantee) of the max amount of data written to this stream + */ + public CapacityByteArrayOutputStream(int initialSlabSize, int maxCapacityHint, ByteBufferAllocator allocator) { checkArgument(initialSlabSize > 0, "initialSlabSize must be > 0"); checkArgument(maxCapacityHint > 0, "maxCapacityHint must be > 0"); checkArgument(maxCapacityHint >= initialSlabSize, String.format("maxCapacityHint can't be less than initialSlabSize %d %d", initialSlabSize, maxCapacityHint)); this.initialSlabSize = initialSlabSize; this.maxCapacityHint = maxCapacityHint; + this.allocator = allocator; reset(); } @@ -145,7 +173,7 @@ private void addSlab(int minimumSize) { if (Log.DEBUG) LOG.debug(format("used %d slabs, adding new slab of size %d", slabs.size(), nextSlabSize)); - this.currentSlab = new byte[nextSlabSize]; + this.currentSlab = allocator.allocate(nextSlabSize); this.slabs.add(currentSlab); this.bytesAllocated += nextSlabSize; this.currentSlabIndex = 0; @@ -153,11 +181,12 @@ private void addSlab(int minimumSize) { @Override public void write(int b) { - if (currentSlabIndex == currentSlab.length) { + if (!currentSlab.hasRemaining()) { addSlab(1); } - currentSlab[currentSlabIndex] = (byte) b; + currentSlab.put(currentSlabIndex, (byte) b); currentSlabIndex += 1; + currentSlab.position(currentSlabIndex); bytesUsed += 1; } @@ -168,18 +197,34 @@ public void write(byte b[], int off, int len) { throw new IndexOutOfBoundsException( String.format("Given byte array of size %d, with requested length(%d) and offset(%d)", b.length, len, off)); } - if (currentSlabIndex + len >= currentSlab.length) { - final int length1 = currentSlab.length - currentSlabIndex; - arraycopy(b, off, currentSlab, currentSlabIndex, length1); + if (len >= currentSlab.remaining()) { + final int length1 = currentSlab.remaining(); + currentSlab.put(b, off, length1); + bytesUsed += length1; + currentSlabIndex += length1; final int length2 = len - length1; addSlab(length2); - arraycopy(b, off + length1, currentSlab, currentSlabIndex, length2); + currentSlab.put(b, off + length1, length2); currentSlabIndex = length2; + bytesUsed += length2; } else { - arraycopy(b, off, currentSlab, currentSlabIndex, len); + currentSlab.put(b, off, len); currentSlabIndex += len; + bytesUsed += len; + } + } + + private void writeToOutput(OutputStream out, ByteBuffer buf, int len) throws IOException { + if (buf.hasArray()) { + out.write(buf.array(), buf.arrayOffset(), len); + } else { + // The OutputStream interface only takes a byte[], unfortunately this means that a ByteBuffer + // not backed by a byte array must be copied to fulfil this interface + byte[] copy = new byte[len]; + buf.flip(); + buf.get(copy); + out.write(copy); } - bytesUsed += len; } /** @@ -191,10 +236,9 @@ public void write(byte b[], int off, int len) { */ public void writeTo(OutputStream out) throws IOException { for (int i = 0; i < slabs.size() - 1; i++) { - final byte[] slab = slabs.get(i); - out.write(slab); + writeToOutput(out, slabs.get(i), slabs.get(i).position()); } - out.write(currentSlab, 0, currentSlabIndex); + writeToOutput(out, currentSlab, currentSlabIndex); } /** @@ -222,6 +266,9 @@ public void reset() { // 7 = 2^3 - 1 so that doubling the initial size 3 times will get to the same size this.initialSlabSize = max(bytesUsed / 7, initialSlabSize); if (Log.DEBUG) LOG.debug(String.format("initial slab of size %d", initialSlabSize)); + for (ByteBuffer slab : slabs) { + allocator.release(slab); + } this.slabs.clear(); this.bytesAllocated = 0; this.bytesUsed = 0; @@ -249,13 +296,13 @@ public void setByte(long index, byte value) { long seen = 0; for (int i = 0; i < slabs.size(); i++) { - byte[] slab = slabs.get(i); - if (index < seen + slab.length) { + ByteBuffer slab = slabs.get(i); + if (index < seen + slab.limit()) { // ok found index - slab[(int)(index-seen)] = value; + slab.put((int)(index-seen), value); break; } - seen += slab.length; + seen += slab.limit(); } } @@ -273,4 +320,16 @@ public String memUsageString(String prefix) { int getSlabCount() { return slabs.size(); } + + @Override + public void close() { + for (ByteBuffer slab : slabs) { + allocator.release(slab); + } + try { + super.close(); + }catch(IOException e){ + throw new OutputStreamCloseException(e); + } + } } diff --git a/parquet-encoding/src/main/java/org/apache/parquet/bytes/LittleEndianDataOutputStream.java b/parquet-encoding/src/main/java/org/apache/parquet/bytes/LittleEndianDataOutputStream.java index da4e92f94f..9d4a8a9ec1 100644 --- a/parquet-encoding/src/main/java/org/apache/parquet/bytes/LittleEndianDataOutputStream.java +++ b/parquet-encoding/src/main/java/org/apache/parquet/bytes/LittleEndianDataOutputStream.java @@ -18,6 +18,9 @@ */ package org.apache.parquet.bytes; +import org.apache.parquet.IOExceptionUtils; +import org.apache.parquet.ParquetRuntimeException; + import java.io.IOException; import java.io.OutputStream; @@ -210,4 +213,8 @@ public final void writeDouble(double v) throws IOException { writeLong(Double.doubleToLongBits(v)); } + public void close() { + IOExceptionUtils.closeQuietly(out); + } + } diff --git a/parquet-encoding/src/main/java/org/apache/parquet/column/values/bitpacking/BytePacker.java b/parquet-encoding/src/main/java/org/apache/parquet/column/values/bitpacking/BytePacker.java index b9a37adc20..675576cd6c 100644 --- a/parquet-encoding/src/main/java/org/apache/parquet/column/values/bitpacking/BytePacker.java +++ b/parquet-encoding/src/main/java/org/apache/parquet/column/values/bitpacking/BytePacker.java @@ -18,6 +18,8 @@ */ package org.apache.parquet.column.values.bitpacking; +import java.nio.ByteBuffer; + /** * Packs and unpacks into bytes * @@ -71,7 +73,15 @@ public final int getBitWidth() { * @param output the output values * @param outPos where to write to in output */ - public abstract void unpack8Values(final byte[] input, final int inPos, final int[] output, final int outPos); + public abstract void unpack8Values(final ByteBuffer input, final int inPos, final int[] output, final int outPos); + + /** + * Compatibility API + */ + @Deprecated + public void unpack8Values(final byte[] input, final int inPos, final int[] output, final int outPos) { + unpack8Values(ByteBuffer.wrap(input), inPos, output, outPos); + } /** * unpack bitWidth * 4 bytes from input at inPos into 32 values in output at outPos. @@ -81,6 +91,13 @@ public final int getBitWidth() { * @param output the output values * @param outPos where to write to in output */ - public abstract void unpack32Values(byte[] input, int inPos, int[] output, int outPos); + public abstract void unpack32Values(ByteBuffer input, int inPos, int[] output, int outPos); + /** + * Compatibility API + */ + @Deprecated + public void unpack32Values(byte[] input, int inPos, int[] output, int outPos) { + unpack32Values(ByteBuffer.wrap(input), inPos, output, outPos); + } } diff --git a/parquet-encoding/src/test/java/org/apache/parquet/bytes/TestBytesInput.java b/parquet-encoding/src/test/java/org/apache/parquet/bytes/TestBytesInput.java new file mode 100644 index 0000000000..1cb0304cc4 --- /dev/null +++ b/parquet-encoding/src/test/java/org/apache/parquet/bytes/TestBytesInput.java @@ -0,0 +1,42 @@ +/* + * 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 + * + * 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 org.apache.parquet.bytes; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; + +import static org.junit.Assert.assertEquals; + +public class TestBytesInput { + + @Test + public void testWriteInt() throws Throwable { + int[] testVals = { + Integer.MIN_VALUE, + Integer.MAX_VALUE, + 0, 100, 1000, 0xdaedbeef}; + for (Integer testVal : testVals) { + BytesInput varInt = BytesInput.fromUnsignedVarInt(testVal); + byte[] rno = varInt.toByteArray(); + int i = BytesUtils.readUnsignedVarInt(new ByteArrayInputStream(rno)); + assertEquals((int) testVal, i); + } + } +} diff --git a/parquet-encoding/src/test/java/org/apache/parquet/bytes/TestCapacityByteArrayOutputStream.java b/parquet-encoding/src/test/java/org/apache/parquet/bytes/TestCapacityByteArrayOutputStream.java index b80fe40542..89db1981f4 100644 --- a/parquet-encoding/src/test/java/org/apache/parquet/bytes/TestCapacityByteArrayOutputStream.java +++ b/parquet-encoding/src/test/java/org/apache/parquet/bytes/TestCapacityByteArrayOutputStream.java @@ -63,7 +63,7 @@ public void testWriteArrayAndInt() throws Throwable { } protected CapacityByteArrayOutputStream newCapacityBAOS(int initialSize) { - return new CapacityByteArrayOutputStream(10, 1000000); + return new CapacityByteArrayOutputStream(initialSize, 1000000, new HeapByteBufferAllocator()); } @Test @@ -129,12 +129,12 @@ public void testWriteArrayManySlabs() throws Throwable { assertEquals(i % (v * 3), byteArray[i]); } // verifying we have not created 500 * 23 / 10 slabs - assertTrue("slab count: " + capacityByteArrayOutputStream.getSlabCount(),capacityByteArrayOutputStream.getSlabCount() <= 20); + assertTrue("slab count: " + capacityByteArrayOutputStream.getSlabCount(), capacityByteArrayOutputStream.getSlabCount() <= 20); capacityByteArrayOutputStream.reset(); writeArraysOf3(capacityByteArrayOutputStream, v); validate(capacityByteArrayOutputStream, v * 3); // verifying we use less slabs now - assertTrue("slab count: " + capacityByteArrayOutputStream.getSlabCount(),capacityByteArrayOutputStream.getSlabCount() <= 2); + assertTrue("slab count: " + capacityByteArrayOutputStream.getSlabCount(), capacityByteArrayOutputStream.getSlabCount() <= 2); } @Test diff --git a/parquet-encoding/src/test/java/org/apache/parquet/column/values/bitpacking/TestByteBitPacking.java b/parquet-encoding/src/test/java/org/apache/parquet/column/values/bitpacking/TestByteBitPacking.java index a5ce37e66b..8df5f39bab 100644 --- a/parquet-encoding/src/test/java/org/apache/parquet/column/values/bitpacking/TestByteBitPacking.java +++ b/parquet-encoding/src/test/java/org/apache/parquet/column/values/bitpacking/TestByteBitPacking.java @@ -21,6 +21,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.ByteBuffer; import org.junit.Assert; import org.junit.Test; @@ -50,7 +51,7 @@ private void packUnpack(BytePacker packer, int[] values, int[] unpacked) { byte[] packed = new byte[packer.getBitWidth() * 4]; packer.pack32Values(values, 0, packed, 0); LOG.debug("packed: " + TestBitPacking.toString(packed)); - packer.unpack32Values(packed, 0, unpacked, 0); + packer.unpack32Values(ByteBuffer.wrap(packed), 0, unpacked, 0); } private int[] generateValues(int bitWidth) { @@ -140,7 +141,7 @@ public void testPackUnPackAgainstLemire() throws IOException { LOG.debug("Gener. out: " + TestBitPacking.toString(packedGenerated)); Assert.assertEquals(pack.name() + " width " + i, TestBitPacking.toString(packedByLemireAsBytes), TestBitPacking.toString(packedGenerated)); - bytePacker.unpack32Values(packedByLemireAsBytes, 0, unpacked, 0); + bytePacker.unpack32Values(ByteBuffer.wrap(packedByLemireAsBytes), 0, unpacked, 0); LOG.debug("Output: " + TestBitPacking.toString(unpacked)); Assert.assertArrayEquals("width " + i, values, unpacked); diff --git a/parquet-encoding/src/test/java/org/apache/parquet/column/values/bitpacking/TestLemireBitPacking.java b/parquet-encoding/src/test/java/org/apache/parquet/column/values/bitpacking/TestLemireBitPacking.java index e0c97e041e..2c5fa58358 100644 --- a/parquet-encoding/src/test/java/org/apache/parquet/column/values/bitpacking/TestLemireBitPacking.java +++ b/parquet-encoding/src/test/java/org/apache/parquet/column/values/bitpacking/TestLemireBitPacking.java @@ -21,6 +21,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.ByteBuffer; import org.junit.Assert; import org.junit.Test; @@ -64,7 +65,7 @@ private void packUnpack(IntPacker packer, int[] values, int[] unpacked) { private void packUnpack(BytePacker packer, int[] values, int[] unpacked) { byte[] packed = new byte[packer.getBitWidth() * 4]; packer.pack32Values(values, 0, packed, 0); - packer.unpack32Values(packed, 0, unpacked, 0); + packer.unpack32Values(ByteBuffer.wrap(packed), 0, unpacked, 0); } private int[] generateValues(int bitWidth) { diff --git a/parquet-generator/src/main/java/org/apache/parquet/encoding/bitpacking/ByteBasedBitPackingGenerator.java b/parquet-generator/src/main/java/org/apache/parquet/encoding/bitpacking/ByteBasedBitPackingGenerator.java index 9a7c5623ef..3d182e2929 100644 --- a/parquet-generator/src/main/java/org/apache/parquet/encoding/bitpacking/ByteBasedBitPackingGenerator.java +++ b/parquet-generator/src/main/java/org/apache/parquet/encoding/bitpacking/ByteBasedBitPackingGenerator.java @@ -50,6 +50,7 @@ private static void generateScheme(String className, boolean msbFirst, String ba } FileWriter fw = new FileWriter(file); fw.append("package org.apache.parquet.column.values.bitpacking;\n"); + fw.append("import java.nio.ByteBuffer;\n"); fw.append("\n"); fw.append("/**\n"); if (msbFirst) { @@ -97,8 +98,10 @@ private static void generateClass(FileWriter fw, int bitWidth, boolean msbFirst) generatePack(fw, bitWidth, 4, msbFirst); // Unpacking - generateUnpack(fw, bitWidth, 1, msbFirst); - generateUnpack(fw, bitWidth, 4, msbFirst); + generateUnpack(fw, bitWidth, 1, msbFirst, true); + generateUnpack(fw, bitWidth, 1, msbFirst, false); + generateUnpack(fw, bitWidth, 4, msbFirst, true); + generateUnpack(fw, bitWidth, 4, msbFirst, false); fw.append(" }\n"); } @@ -203,9 +206,15 @@ private static void generatePack(FileWriter fw, int bitWidth, int batch, boolean fw.append(" }\n"); } - private static void generateUnpack(FileWriter fw, int bitWidth, int batch, boolean msbFirst) + private static void generateUnpack(FileWriter fw, int bitWidth, int batch, boolean msbFirst, boolean useByteArray) throws IOException { - fw.append(" public final void unpack" + (batch * 8) + "Values(final byte[] in, final int inPos, final int[] out, final int outPos) {\n"); + final String bufferDataType; + if (useByteArray) { + bufferDataType = "byte[]"; + } else { + bufferDataType = "ByteBuffer"; + } + fw.append(" public final void unpack" + (batch * 8) + "Values(final " + bufferDataType + " in, final int inPos, final int[] out, final int outPos) {\n"); if (bitWidth > 0) { int mask = genMask(bitWidth); for (int valueIndex = 0; valueIndex < (batch * 8); ++valueIndex) { @@ -228,7 +237,14 @@ private static void generateUnpack(FileWriter fw, int bitWidth, int batch, boole } else if (shift > 0){ shiftString = "<< " + shift; } - fw.append(" (((((int)in[" + align(byteIndex, 2) + " + inPos]) & 255) " + shiftString + ") & " + mask + ")"); + final String byteAccess; + if (useByteArray) { + byteAccess = "in[" + align(byteIndex, 2) + " + inPos]"; + } else { + // use ByteBuffer#get(index) method + byteAccess = "in.get(" + align(byteIndex, 2) + " + inPos)"; + } + fw.append(" (((((int)" + byteAccess + ") & 255) " + shiftString + ") & " + mask + ")"); } fw.append(";\n"); } diff --git a/parquet-hadoop/pom.xml b/parquet-hadoop/pom.xml index a7f9d2c424..2f2e932f72 100644 --- a/parquet-hadoop/pom.xml +++ b/parquet-hadoop/pom.xml @@ -92,6 +92,11 @@ 1.9.5 test + + commons-pool + commons-pool + 1.5.4 + diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/format/converter/ParquetMetadataConverter.java b/parquet-hadoop/src/main/java/org/apache/parquet/format/converter/ParquetMetadataConverter.java index fdeb2ba246..6821bbfff4 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/format/converter/ParquetMetadataConverter.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/format/converter/ParquetMetadataConverter.java @@ -36,6 +36,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import org.apache.hadoop.fs.FSDataInputStream; import org.apache.parquet.CorruptStatistics; import org.apache.parquet.Log; import org.apache.parquet.hadoop.metadata.ColumnPath; diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/CodecFactory.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/CodecFactory.java index 684095024b..8bf882f362 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/CodecFactory.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/CodecFactory.java @@ -1,4 +1,4 @@ -/* +/* * 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 @@ -6,9 +6,9 @@ * 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 - * + * * 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 @@ -18,14 +18,14 @@ */ package org.apache.parquet.hadoop; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.Collections; import java.util.HashMap; import java.util.Map; - import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io.compress.CodecPool; import org.apache.hadoop.io.compress.CompressionCodec; @@ -34,18 +34,64 @@ import org.apache.hadoop.io.compress.Decompressor; import org.apache.hadoop.util.ReflectionUtils; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.hadoop.metadata.CompressionCodecName; -class CodecFactory { +public class CodecFactory { + + protected static final Map CODEC_BY_NAME = Collections + .synchronizedMap(new HashMap()); + + private final Map compressors = new HashMap(); + private final Map decompressors = new HashMap(); - public class BytesDecompressor { + protected final Configuration configuration; + protected final int pageSize; + + /** + * Create a new codec factory. + * + * @param configuration used to pass compression codec configuration information + * @param pageSize the expected page size, does not set a hard limit, currently just + * used to set the initial size of the output stream used when + * compressing a buffer. If this factory is only used to construct + * decompressors this parameter has no impact on the function of the factory + */ + public CodecFactory(Configuration configuration, int pageSize) { + this.configuration = configuration; + this.pageSize = pageSize; + } + + /** + * Create a codec factory that will provide compressors and decompressors + * that will work natively with ByteBuffers backed by direct memory. + * + * @param config configuration options for different compression codecs + * @param allocator an allocator for creating result buffers during compression + * and decompression, must provide buffers backed by Direct + * memory and return true for the isDirect() method + * on the ByteBufferAllocator interface + * @param pageSize the default page size. This does not set a hard limit on the + * size of buffers that can be compressed, but performance may + * be improved by setting it close to the expected size of buffers + * (in the case of parquet, pages) that will be compressed. This + * setting is unused in the case of decompressing data, as parquet + * always records the uncompressed size of a buffer. If this + * CodecFactory is only going to be used for decompressors, this + * parameter will not impact the function of the factory. + */ + public static CodecFactory createDirectCodecFactory(Configuration config, ByteBufferAllocator allocator, int pageSize) { + return new DirectCodecFactory(config, allocator, pageSize); + } + + class HeapBytesDecompressor extends BytesDecompressor { private final CompressionCodec codec; private final Decompressor decompressor; - public BytesDecompressor(CompressionCodec codec) { - this.codec = codec; + HeapBytesDecompressor(CompressionCodecName codecName) { + this.codec = getCodec(codecName); if (codec != null) { decompressor = CodecPool.getDecompressor(codec); } else { @@ -53,11 +99,12 @@ public BytesDecompressor(CompressionCodec codec) { } } + @Override public BytesInput decompress(BytesInput bytes, int uncompressedSize) throws IOException { final BytesInput decompressed; if (codec != null) { decompressor.reset(); - InputStream is = codec.createInputStream(new ByteArrayInputStream(bytes.toByteArray()), decompressor); + InputStream is = codec.createInputStream(bytes.toInputStream(), decompressor); decompressed = BytesInput.from(is, uncompressedSize); } else { decompressed = bytes; @@ -65,7 +112,13 @@ public BytesInput decompress(BytesInput bytes, int uncompressedSize) throws IOEx return decompressed; } - private void release() { + @Override + public void decompress(ByteBuffer input, int compressedSize, ByteBuffer output, int uncompressedSize) throws IOException { + ByteBuffer decompressed = decompress(BytesInput.from(input, 0, input.remaining()), uncompressedSize).toByteBuffer(); + output.put(decompressed); + } + + protected void release() { if (decompressor != null) { CodecPool.returnDecompressor(decompressor); } @@ -78,16 +131,16 @@ private void release() { * @author Julien Le Dem * */ - public static class BytesCompressor { + class HeapBytesCompressor extends BytesCompressor { private final CompressionCodec codec; private final Compressor compressor; private final ByteArrayOutputStream compressedOutBuffer; private final CompressionCodecName codecName; - public BytesCompressor(CompressionCodecName codecName, CompressionCodec codec, int pageSize) { + HeapBytesCompressor(CompressionCodecName codecName) { this.codecName = codecName; - this.codec = codec; + this.codec = getCodec(codecName); if (codec != null) { this.compressor = CodecPool.getCompressor(codec); this.compressedOutBuffer = new ByteArrayOutputStream(pageSize); @@ -97,6 +150,7 @@ public BytesCompressor(CompressionCodecName codecName, CompressionCodec codec, i } } + @Override public BytesInput compress(BytesInput bytes) throws IOException { final BytesInput compressedBytes; if (codec == null) { @@ -116,7 +170,8 @@ public BytesInput compress(BytesInput bytes) throws IOException { return compressedBytes; } - private void release() { + @Override + protected void release() { if (compressor != null) { CodecPool.returnCompressor(compressor); } @@ -128,60 +183,58 @@ public CompressionCodecName getCodecName() { } - private final Map compressors = new HashMap(); - private final Map decompressors = new HashMap(); - private final Map codecByName = new HashMap(); - private final Configuration configuration; + public BytesCompressor getCompressor(CompressionCodecName codecName) { + BytesCompressor comp = compressors.get(codecName); + if (comp == null) { + comp = createCompressor(codecName); + compressors.put(codecName, comp); + } + return comp; + } - public CodecFactory(Configuration configuration) { - this.configuration = configuration; + public BytesDecompressor getDecompressor(CompressionCodecName codecName) { + BytesDecompressor decomp = decompressors.get(codecName); + if (decomp == null) { + decomp = createDecompressor(codecName); + decompressors.put(codecName, decomp); + } + return decomp; + } + + protected BytesCompressor createCompressor(CompressionCodecName codecName) { + return new HeapBytesCompressor(codecName); + } + + protected BytesDecompressor createDecompressor(CompressionCodecName codecName) { + return new HeapBytesDecompressor(codecName); } /** * - * @param codecName the requested codec + * @param codecName + * the requested codec * @return the corresponding hadoop codec. null if UNCOMPRESSED */ - private CompressionCodec getCodec(CompressionCodecName codecName) { + protected CompressionCodec getCodec(CompressionCodecName codecName) { String codecClassName = codecName.getHadoopCompressionCodecClassName(); if (codecClassName == null) { return null; } - CompressionCodec codec = codecByName.get(codecClassName); + CompressionCodec codec = CODEC_BY_NAME.get(codecClassName); if (codec != null) { return codec; } try { Class codecClass = Class.forName(codecClassName); - codec = (CompressionCodec)ReflectionUtils.newInstance(codecClass, configuration); - codecByName.put(codecClassName, codec); + codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, configuration); + CODEC_BY_NAME.put(codecClassName, codec); return codec; } catch (ClassNotFoundException e) { throw new BadConfigurationException("Class " + codecClassName + " was not found", e); } } - public BytesCompressor getCompressor(CompressionCodecName codecName, int pageSize) { - BytesCompressor comp = compressors.get(codecName); - if (comp == null) { - CompressionCodec codec = getCodec(codecName); - comp = new BytesCompressor(codecName, codec, pageSize); - compressors.put(codecName, comp); - } - return comp; - } - - public BytesDecompressor getDecompressor(CompressionCodecName codecName) { - BytesDecompressor decomp = decompressors.get(codecName); - if (decomp == null) { - CompressionCodec codec = getCodec(codecName); - decomp = new BytesDecompressor(codec); - decompressors.put(codecName, decomp); - } - return decomp; - } - public void release() { for (BytesCompressor compressor : compressors.values()) { compressor.release(); @@ -192,4 +245,16 @@ public void release() { } decompressors.clear(); } + + public static abstract class BytesCompressor { + public abstract BytesInput compress(BytesInput bytes) throws IOException; + public abstract CompressionCodecName getCodecName(); + protected abstract void release(); + } + + public static abstract class BytesDecompressor { + public abstract BytesInput decompress(BytesInput bytes, int uncompressedSize) throws IOException; + public abstract void decompress(ByteBuffer input, int compressedSize, ByteBuffer output, int uncompressedSize) throws IOException; + protected abstract void release(); + } } diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java index b6934c2fb6..af067474f8 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java @@ -1,4 +1,4 @@ -/* +/* * 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 @@ -6,9 +6,9 @@ * 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 - * + * * 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 diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageWriteStore.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageWriteStore.java index 0a0b316065..2eab54a1a0 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageWriteStore.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageWriteStore.java @@ -42,6 +42,7 @@ import org.apache.parquet.hadoop.CodecFactory.BytesCompressor; import org.apache.parquet.io.ParquetEncodingException; import org.apache.parquet.schema.MessageType; +import org.apache.parquet.bytes.ByteBufferAllocator; class ColumnChunkPageWriteStore implements PageWriteStore { private static final Log LOG = Log.getLog(ColumnChunkPageWriteStore.class); @@ -65,10 +66,14 @@ private static final class ColumnChunkPageWriter implements PageWriter { private Set encodings = new HashSet(); private Statistics totalStatistics; + private final ByteBufferAllocator allocator; - private ColumnChunkPageWriter(ColumnDescriptor path, BytesCompressor compressor, int pageSize) { + private ColumnChunkPageWriter(ColumnDescriptor path, + BytesCompressor compressor, + ByteBufferAllocator allocator) { this.path = path; this.compressor = compressor; + this.allocator = allocator; this.buf = new ConcatenatingByteArrayCollector(); this.totalStatistics = getStatsBasedOnType(this.path.getType()); } @@ -84,14 +89,14 @@ public void writePage(BytesInput bytes, if (uncompressedSize > Integer.MAX_VALUE) { throw new ParquetEncodingException( "Cannot write page larger than Integer.MAX_VALUE bytes: " + - uncompressedSize); + uncompressedSize); } BytesInput compressedBytes = compressor.compress(bytes); long compressedSize = compressedBytes.size(); if (compressedSize > Integer.MAX_VALUE) { throw new ParquetEncodingException( "Cannot write compressed page larger than Integer.MAX_VALUE bytes: " - + compressedSize); + + compressedSize); } tempOutputStream.reset(); parquetMetadataConverter.writeDataPageHeader( @@ -151,10 +156,10 @@ public void writePageV2( // we only allocate one buffer to copy into instead of multiple. buf.collect( BytesInput.concat( - BytesInput.from(tempOutputStream), - repetitionLevels, - definitionLevels, - compressedData) + BytesInput.from(tempOutputStream), + repetitionLevels, + definitionLevels, + compressedData) ); encodings.add(dataEncoding); } @@ -163,7 +168,7 @@ private int toIntWithCheck(long size) { if (size > Integer.MAX_VALUE) { throw new ParquetEncodingException( "Cannot write page larger than " + Integer.MAX_VALUE + " bytes: " + - size); + size); } return (int)size; } @@ -186,10 +191,10 @@ public void writeToFileWriter(ParquetFileWriter writer) throws IOException { String.format( "written %,dB for %s: %,d values, %,dB raw, %,dB comp, %d pages, encodings: %s", buf.size(), path, totalValueCount, uncompressedLength, compressedLength, pageCount, encodings) - + (dictionaryPage != null ? String.format( - ", dic { %,d entries, %,dB raw, %,dB comp}", - dictionaryPage.getDictionarySize(), dictionaryPage.getUncompressedSize(), dictionaryPage.getDictionarySize()) - : "")); + + (dictionaryPage != null ? String.format( + ", dic { %,d entries, %,dB raw, %,dB comp}", + dictionaryPage.getDictionarySize(), dictionaryPage.getUncompressedSize(), dictionaryPage.getDictionarySize()) + : "")); } encodings.clear(); pageCount = 0; @@ -215,15 +220,16 @@ public void writeDictionaryPage(DictionaryPage dictionaryPage) throws IOExceptio public String memUsageString(String prefix) { return buf.memUsageString(prefix + " ColumnChunkPageWriter"); } + } private final Map writers = new HashMap(); private final MessageType schema; - public ColumnChunkPageWriteStore(BytesCompressor compressor, MessageType schema, int pageSize) { + public ColumnChunkPageWriteStore(BytesCompressor compressor, MessageType schema, ByteBufferAllocator allocator) { this.schema = schema; for (ColumnDescriptor path : schema.getColumns()) { - writers.put(path, new ColumnChunkPageWriter(path, compressor, pageSize)); + writers.put(path, new ColumnChunkPageWriter(path, compressor, allocator)); } } diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/DirectCodecFactory.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/DirectCodecFactory.java new file mode 100644 index 0000000000..bb711daa29 --- /dev/null +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/DirectCodecFactory.java @@ -0,0 +1,522 @@ +/** + * 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 + * + * 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 org.apache.parquet.hadoop; + + + +import java.lang.reflect.Method; +import java.lang.reflect.InvocationTargetException; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.pool.BasePoolableObjectFactory; +import org.apache.commons.pool.impl.GenericObjectPool; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.io.compress.CompressionCodec; +import org.apache.hadoop.io.compress.Compressor; +import org.apache.hadoop.io.compress.Decompressor; +import org.xerial.snappy.Snappy; + +import org.apache.parquet.bytes.ByteBufferAllocator; +import org.apache.parquet.bytes.BytesInput; +import org.apache.parquet.hadoop.metadata.CompressionCodecName; +import org.apache.parquet.Log; +import org.apache.parquet.ParquetRuntimeException; +import org.apache.parquet.Preconditions; + +/** + * Factory to produce compressors and decompressors that operate on java + * direct memory, without requiring a copy into heap memory (where possible). + */ +class DirectCodecFactory extends CodecFactory implements AutoCloseable { + private static final Log LOG = Log.getLog(DirectCodecFactory.class); + + private final ByteBufferAllocator allocator; + + // Any of these can be null depending on the version of hadoop on the classpath + private static final Class DIRECT_DECOMPRESSION_CODEC_CLASS; + private static final Method DECOMPRESS_METHOD; + private static final Method CREATE_DIRECT_DECOMPRESSOR_METHOD; + + static { + Class tempClass = null; + Method tempCreateMethod = null; + Method tempDecompressMethod = null; + try { + tempClass = Class.forName("org.apache.hadoop.io.compress.DirectDecompressionCodec"); + tempCreateMethod = tempClass.getMethod("createDirectDecompressor"); + tempDecompressMethod = tempClass.getMethod("decompress", ByteBuffer.class, ByteBuffer.class); + } catch (ClassNotFoundException e) { + // do nothing, the class will just be assigned null + } catch (NoSuchMethodException e) { + // do nothing, the method will just be assigned null + } + DIRECT_DECOMPRESSION_CODEC_CLASS = tempClass; + CREATE_DIRECT_DECOMPRESSOR_METHOD = tempCreateMethod; + DECOMPRESS_METHOD = tempDecompressMethod; + } + + /** + * See docs on CodecFactory#createDirectCodecFactory which is how this class is + * exposed publicly and is just a pass-through factory method for this constructor + * to hide the rest of this class from public access. + */ + DirectCodecFactory(Configuration config, ByteBufferAllocator allocator, int pageSize) { + super(config, pageSize); + Preconditions.checkNotNull(allocator, "allocator"); + Preconditions.checkState(allocator.isDirect(), + "A %s requires a direct buffer allocator be provided.", + getClass().getSimpleName()); + this.allocator = allocator; + } + + private ByteBuffer ensure(ByteBuffer buffer, int size) { + if (buffer == null) { + buffer = allocator.allocate(size); + } else if (buffer.capacity() >= size) { + buffer.clear(); + } else { + release(buffer); + buffer = allocator.allocate(size); + } + return buffer; + } + + ByteBuffer release(ByteBuffer buffer) { + if (buffer != null) { + allocator.release(buffer); + } + return null; + } + + @Override + protected BytesCompressor createCompressor(final CompressionCodecName codecName) { + + CompressionCodec codec = getCodec(codecName); + if (codec == null) { + return new NoopCompressor(); + } else if (codecName == CompressionCodecName.SNAPPY) { + // avoid using the default Snappy codec since it allocates direct buffers at awkward spots. + return new SnappyCompressor(); + } else { + // todo: create class similar to the SnappyCompressor for zlib and exclude it as + // snappy is above since it also generates allocateDirect calls. + return new HeapBytesCompressor(codecName); + } + } + + @Override + protected BytesDecompressor createDecompressor(final CompressionCodecName codecName) { + CompressionCodec codec = getCodec(codecName); + if (codec == null) { + return new NoopDecompressor(); + } else if (codecName == CompressionCodecName.SNAPPY ) { + return new SnappyDecompressor(); + } else if (DirectCodecPool.INSTANCE.codec(codec).supportsDirectDecompression()) { + return new FullDirectDecompressor(codecName); + } else { + return new IndirectDecompressor(codec); + } + } + + public void close() { + release(); + } + + /** + * Wrapper around legacy hadoop compressors that do not implement a direct memory + * based version of the decompression algorithm. + */ + public class IndirectDecompressor extends BytesDecompressor { + private final Decompressor decompressor; + + public IndirectDecompressor(CompressionCodec codec) { + this.decompressor = DirectCodecPool.INSTANCE.codec(codec).borrowDecompressor(); + } + + @Override + public BytesInput decompress(BytesInput bytes, int uncompressedSize) throws IOException { + decompressor.reset(); + byte[] inputBytes = bytes.toByteArray(); + decompressor.setInput(inputBytes, 0, inputBytes.length); + byte[] output = new byte[uncompressedSize]; + decompressor.decompress(output, 0, uncompressedSize); + return BytesInput.from(output); + } + + @Override + public void decompress(ByteBuffer input, int compressedSize, ByteBuffer output, int uncompressedSize) + throws IOException { + + decompressor.reset(); + byte[] inputBytes = new byte[compressedSize]; + input.position(0); + input.get(inputBytes); + decompressor.setInput(inputBytes, 0, inputBytes.length); + byte[] outputBytes = new byte[uncompressedSize]; + decompressor.decompress(outputBytes, 0, uncompressedSize); + output.clear(); + output.put(outputBytes); + } + + @Override + protected void release() { + DirectCodecPool.INSTANCE.returnDecompressor(decompressor); + } + } + + /** + * Wrapper around new Hadoop compressors that implement a direct memory + * based version of a particular decompression algorithm. To maintain + * compatibility with Hadoop 1.x these classes that implement + * {@link org.apache.hadoop.io.compress.DirectDecompressionCodec} + * are currently retrieved and have their decompression method invoked + * with reflection. + */ + public class FullDirectDecompressor extends BytesDecompressor { + private final Object decompressor; + private HeapBytesDecompressor extraDecompressor; + public FullDirectDecompressor(CompressionCodecName codecName){ + CompressionCodec codec = getCodec(codecName); + this.decompressor = DirectCodecPool.INSTANCE.codec(codec).borrowDirectDecompressor(); + this.extraDecompressor = new HeapBytesDecompressor(codecName); + } + + @Override + public BytesInput decompress(BytesInput compressedBytes, int uncompressedSize) throws IOException { + return extraDecompressor.decompress(compressedBytes, uncompressedSize); + } + + @Override + public void decompress(ByteBuffer input, int compressedSize, ByteBuffer output, int uncompressedSize) + throws IOException { + output.clear(); + try { + DECOMPRESS_METHOD.invoke(decompressor, (ByteBuffer) input.limit(compressedSize), (ByteBuffer) output.limit(uncompressedSize)); + } catch (IllegalAccessException e) { + throw new DirectCodecPool.ParquetCompressionCodecException(e); + } catch (InvocationTargetException e) { + throw new DirectCodecPool.ParquetCompressionCodecException(e); + } + output.position(uncompressedSize); + } + + @Override + protected void release() { + DirectCodecPool.INSTANCE.returnDirectDecompressor(decompressor); + extraDecompressor.release(); + } + + } + + public class NoopDecompressor extends BytesDecompressor { + + @Override + public void decompress(ByteBuffer input, int compressedSize, ByteBuffer output, int uncompressedSize) + throws IOException { + Preconditions.checkArgument(compressedSize == uncompressedSize, + "Non-compressed data did not have matching compressed and uncompressed sizes."); + output.clear(); + output.put((ByteBuffer) input.duplicate().position(0).limit(compressedSize)); + } + + @Override + public BytesInput decompress(BytesInput bytes, int uncompressedSize) throws IOException { + return bytes; + } + + @Override + protected void release() {} + + } + + public class SnappyDecompressor extends BytesDecompressor { + + private HeapBytesDecompressor extraDecompressor; + public SnappyDecompressor() { + this.extraDecompressor = new HeapBytesDecompressor(CompressionCodecName.SNAPPY); + } + + @Override + public BytesInput decompress(BytesInput bytes, int uncompressedSize) throws IOException { + return extraDecompressor.decompress(bytes, uncompressedSize); + } + + @Override + public void decompress(ByteBuffer src, int compressedSize, ByteBuffer dst, int uncompressedSize) throws IOException { + dst.clear(); + int size = Snappy.uncompress(src, dst); + dst.limit(size); + } + + @Override + protected void release() {} + } + + public class SnappyCompressor extends BytesCompressor { + + // TODO - this outgoing buffer might be better off not being shared, this seems to + // only work because of an extra copy currently happening where this interface is + // be consumed + private ByteBuffer incoming; + private ByteBuffer outgoing; + + /** + * Compress a given buffer of bytes + * @param bytes + * @return + * @throws IOException + */ + @Override + public BytesInput compress(BytesInput bytes) throws IOException { + int maxOutputSize = Snappy.maxCompressedLength((int) bytes.size()); + ByteBuffer bufferIn = bytes.toByteBuffer(); + outgoing = ensure(outgoing, maxOutputSize); + final int size; + if (bufferIn.isDirect()) { + size = Snappy.compress(bufferIn, outgoing); + } else { + // Snappy library requires buffers be direct + this.incoming = ensure(this.incoming, (int) bytes.size()); + this.incoming.put(bufferIn); + this.incoming.flip(); + size = Snappy.compress(this.incoming, outgoing); + } + + return BytesInput.from(outgoing, 0, (int) size); + } + + @Override + public CompressionCodecName getCodecName() { + return CompressionCodecName.SNAPPY; + } + + @Override + protected void release() { + outgoing = DirectCodecFactory.this.release(outgoing); + incoming = DirectCodecFactory.this.release(incoming); + } + + } + + public static class NoopCompressor extends BytesCompressor { + + public NoopCompressor() {} + + @Override + public BytesInput compress(BytesInput bytes) throws IOException { + return bytes; + } + + @Override + public CompressionCodecName getCodecName() { + return CompressionCodecName.UNCOMPRESSED; + } + + @Override + protected void release() {} + } + + static class DirectCodecPool { + + public static final DirectCodecPool INSTANCE = new DirectCodecPool(); + + private final Map codecs = + Collections.synchronizedMap(new HashMap()); + private final Map, GenericObjectPool> directDePools = Collections + .synchronizedMap(new HashMap, GenericObjectPool>()); + private final Map, GenericObjectPool> dePools = Collections + .synchronizedMap(new HashMap, GenericObjectPool>()); + private final Map, GenericObjectPool> cPools = Collections + .synchronizedMap(new HashMap, GenericObjectPool>()); + + private DirectCodecPool() {} + + public class CodecPool { + private final GenericObjectPool compressorPool; + private final GenericObjectPool decompressorPool; + private final GenericObjectPool directDecompressorPool; + private final boolean supportDirectDecompressor; + private static final String BYTE_BUF_IMPL_NOT_FOUND_MSG = + "Unable to find ByteBuffer based %s for codec %s, will use a byte array based implementation instead."; + + private CodecPool(final CompressionCodec codec){ + try { + boolean supportDirectDecompressor = codec.getClass() == DIRECT_DECOMPRESSION_CODEC_CLASS; + compressorPool = new GenericObjectPool(new BasePoolableObjectFactory() { + public Object makeObject() throws Exception { + return codec.createCompressor(); + } + }, Integer.MAX_VALUE); + + Object com = compressorPool.borrowObject(); + if (com != null) { + cPools.put(com.getClass(), compressorPool); + compressorPool.returnObject(com); + } else { + if (Log.DEBUG) { + LOG.debug(String.format(BYTE_BUF_IMPL_NOT_FOUND_MSG, "compressor", codec.getClass().getName())); + } + } + + decompressorPool = new GenericObjectPool(new BasePoolableObjectFactory() { + public Object makeObject() throws Exception { + return codec.createDecompressor(); + } + }, Integer.MAX_VALUE); + + Object decom = decompressorPool.borrowObject(); + if (decom != null) { + dePools.put(decom.getClass(), decompressorPool); + decompressorPool.returnObject(decom); + } else { + if (Log.DEBUG) { + LOG.debug(String.format(BYTE_BUF_IMPL_NOT_FOUND_MSG, "decompressor" + codec.getClass().getName())); + } + } + + if (supportDirectDecompressor) { + directDecompressorPool = new GenericObjectPool( + new BasePoolableObjectFactory() { + public Object makeObject() throws Exception { + return CREATE_DIRECT_DECOMPRESSOR_METHOD.invoke(DIRECT_DECOMPRESSION_CODEC_CLASS); + } + }, Integer.MAX_VALUE); + + Object ddecom = directDecompressorPool.borrowObject(); + if (ddecom != null) { + directDePools.put(ddecom.getClass(), directDecompressorPool); + directDecompressorPool.returnObject(ddecom); + + } else { + supportDirectDecompressor = false; + if (Log.DEBUG) { + LOG.debug(String.format(BYTE_BUF_IMPL_NOT_FOUND_MSG, "compressor" + codec.getClass().getName())); + } + } + + } else { + directDecompressorPool = null; + } + + this.supportDirectDecompressor = supportDirectDecompressor; + } catch (Exception e) { + throw new ParquetCompressionCodecException("Error creating compression codec pool.", e); + } + } + + public Object borrowDirectDecompressor(){ + Preconditions.checkArgument(supportDirectDecompressor, "Tried to get a direct Decompressor from a non-direct codec."); + try { + return directDecompressorPool.borrowObject(); + } catch (Exception e) { + throw new ParquetCompressionCodecException(e); + } + } + + public boolean supportsDirectDecompression() { + return supportDirectDecompressor; + } + + public Decompressor borrowDecompressor(){ + return borrow(decompressorPool); + } + + public Compressor borrowCompressor(){ + return borrow(compressorPool); + } + } + + public CodecPool codec(CompressionCodec codec){ + CodecPool pools = codecs.get(codec); + if(pools == null){ + synchronized(this){ + pools = codecs.get(codec); + if(pools == null){ + pools = new CodecPool(codec); + codecs.put(codec, pools); + } + } + } + return pools; + } + + private void returnToPool(Object obj, Map, GenericObjectPool> pools) { + try { + GenericObjectPool pool = pools.get(obj.getClass()); + if (pool == null) { + throw new IllegalStateException("Received unexpected compressor or decompressor, " + + "cannot be returned to any available pool: " + obj.getClass().getSimpleName()); + } + pool.returnObject(obj); + } catch (Exception e) { + throw new ParquetCompressionCodecException(e); + } + } + + /** + * Borrow an object from a pool. + * + * @param pool - the pull to borrow from, must not be null + * @return - an object from the pool + */ + @SuppressWarnings("unchecked") + public T borrow(GenericObjectPool pool) { + try { + return (T) pool.borrowObject(); + } catch (Exception e) { + throw new ParquetCompressionCodecException(e); + } + + } + + public void returnCompressor(Compressor compressor) { + returnToPool(compressor, cPools); + } + + public void returnDecompressor(Decompressor decompressor) { + returnToPool(decompressor, dePools); + } + + public void returnDirectDecompressor(Object decompressor) { + returnToPool(decompressor, directDePools); + } + + public static class ParquetCompressionCodecException extends ParquetRuntimeException { + + public ParquetCompressionCodecException() { + super(); + } + + public ParquetCompressionCodecException(String message, Throwable cause) { + super(message, cause); + } + + public ParquetCompressionCodecException(String message) { + super(message); + } + + public ParquetCompressionCodecException(Throwable cause) { + + } + } + } +} diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/InternalParquetRecordWriter.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/InternalParquetRecordWriter.java index ab9cb3ea7b..87b23a2c0d 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/InternalParquetRecordWriter.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/InternalParquetRecordWriter.java @@ -1,4 +1,4 @@ -/* +/* * 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 @@ -6,9 +6,9 @@ * 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 - * + * * 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 @@ -28,6 +28,7 @@ import java.util.HashMap; import java.util.Map; +import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.Log; import org.apache.parquet.column.ColumnWriteStore; import org.apache.parquet.column.ParquetProperties; @@ -86,7 +87,8 @@ public InternalParquetRecordWriter( int dictionaryPageSize, boolean enableDictionary, boolean validating, - WriterVersion writerVersion) { + WriterVersion writerVersion, + ByteBufferAllocator allocator) { this.parquetFileWriter = parquetFileWriter; this.writeSupport = checkNotNull(writeSupport, "writeSupport"); this.schema = schema; @@ -97,16 +99,17 @@ public InternalParquetRecordWriter( this.pageSize = pageSize; this.compressor = compressor; this.validating = validating; - this.parquetProperties = new ParquetProperties(dictionaryPageSize, writerVersion, enableDictionary); + this.parquetProperties = new ParquetProperties(dictionaryPageSize, writerVersion, enableDictionary, allocator); initStore(); } private void initStore() { - pageStore = new ColumnChunkPageWriteStore(compressor, schema, pageSize); + pageStore = new ColumnChunkPageWriteStore(compressor, schema, parquetProperties.getAllocator()); columnStore = parquetProperties.newColumnWriteStore( schema, pageStore, - pageSize); + pageSize, + parquetProperties.getAllocator()); MessageColumnIO columnIO = new ColumnIOFactory(validating).getColumnIO(schema); this.recordConsumer = columnIO.getRecordWriter(columnStore); writeSupport.prepareForWrite(recordConsumer); @@ -150,7 +153,9 @@ private void checkBlockSizeReached() throws IOException { max(MINIMUM_RECORD_COUNT_FOR_CHECK, (recordCount + (long)(nextRowGroupSize / ((float)recordSize))) / 2), // will check halfway recordCount + MAXIMUM_RECORD_COUNT_FOR_CHECK // will not look more than max records ahead ); - if (DEBUG) LOG.debug(format("Checked mem at %,d will check again at: %,d ", recordCount, recordCountForNextMemCheck)); + if (DEBUG) { + LOG.debug(format("Checked mem at %,d will check again at: %,d ", recordCount, recordCountForNextMemCheck)); + } } } } diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileReader.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileReader.java index ea7a6723fa..336a25e839 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileReader.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileReader.java @@ -1,4 +1,4 @@ -/* +/* * 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 @@ -6,9 +6,9 @@ * 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 - * + * * 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 @@ -27,10 +27,10 @@ import static org.apache.parquet.hadoop.ParquetFileWriter.PARQUET_COMMON_METADATA_FILE; import static org.apache.parquet.hadoop.ParquetFileWriter.PARQUET_METADATA_FILE; -import java.io.ByteArrayInputStream; import java.io.Closeable; import java.io.IOException; import java.io.SequenceInputStream; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -53,6 +53,11 @@ import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; +import org.apache.parquet.bytes.ByteBufferAllocator; +import org.apache.parquet.bytes.ByteBufferInputStream; +import org.apache.parquet.bytes.HeapByteBufferAllocator; +import org.apache.parquet.hadoop.util.CompatibilityUtil; + import org.apache.parquet.Log; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.column.ColumnDescriptor; @@ -185,7 +190,9 @@ public Map call() throws Exception { if (toRead.size() > 0) { // read the footers of the files that did not have a summary file - if (Log.INFO) LOG.info("reading another " + toRead.size() + " footers"); + if (Log.INFO) { + LOG.info("reading another " + toRead.size() + " footers"); + } result.addAll(readAllFootersInParallel(configuration, toRead, skipRowGroups)); } @@ -288,7 +295,7 @@ public static List