clintropolis commented on code in PR #12753: URL: https://github.com/apache/druid/pull/12753#discussion_r932909483
########## processing/src/main/java/org/apache/druid/segment/data/CompressedBlockReader.java: ########## @@ -0,0 +1,248 @@ +/* + * 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.druid.segment.data; + +import com.google.common.base.Preconditions; +import org.apache.druid.collections.ResourceHolder; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.java.util.common.io.Closer; +import org.apache.druid.segment.CompressedPools; + +import java.io.Closeable; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.IntBuffer; +import java.util.function.Supplier; + +/** + * Reader for a virtual contiguous address range backed by compressed blocks of data. + * + * Format: + * | version (byte) | compression (byte) | num blocks (int) | block size (int) | end offsets | compressed data | + * + * This mechanism supports two modes of use, the first where callers may ask for a range of data from the underlying + * blocks, provided by {@link #getRange(long, int)}. The {@link ByteBuffer} provided by this method may or may not + * be valid after additional calls to {@link #getRange(long, int)} or calls to {@link #seekBlock(int)}. + * + * For fixed width values which are aligned with the block size, callers may also use the method + * {@link #getDecompressedDataBuffer()} to have direct access to the current uncompressed block, and use the methods + * {@link #loadBlock(long)} to load the correct block and translate a virtual offset into the relative offset, or + * {@link #seekBlock(int)} to change which block is currently loaded. + * + * {@link #getRange(long, int)} uses these same mechanisms internally to supply data. + */ +public final class CompressedBlockReader implements Closeable +{ + private static final ByteBuffer NULL_VALUE = ByteBuffer.wrap(new byte[0]); + public static final byte VERSION = 0x01; + + public static Supplier<CompressedBlockReader> fromByteBuffer(ByteBuffer buffer, ByteOrder byteOrder) + { + byte versionFromBuffer = buffer.get(); + + if (versionFromBuffer == VERSION) { + final CompressionStrategy compression = CompressionStrategy.forId(buffer.get()); + final int blockSize = buffer.getInt(); + assert CompressedPools.BUFFER_SIZE == blockSize; + Preconditions.checkState( + blockSize <= CompressedPools.BUFFER_SIZE, + "Maximum block size must be less than " + CompressedPools.BUFFER_SIZE + ); + final int numBlocks = buffer.getInt(); + final int offsetsSize = numBlocks * Integer.BYTES; + // buffer is at start of ending offsets + final ByteBuffer offsets = buffer.asReadOnlyBuffer().order(byteOrder); + offsets.limit(offsets.position() + offsetsSize); + final IntBuffer offsetView = offsets.slice().order(byteOrder).asIntBuffer(); + final int compressedSize = offsetView.get(numBlocks - 1); + + // move to start of compressed data + buffer.position(buffer.position() + offsetsSize); + final ByteBuffer compressedData = buffer.asReadOnlyBuffer().order(byteOrder); + compressedData.limit(compressedData.position() + compressedSize); + buffer.position(buffer.position() + compressedSize); + + final ByteBuffer compressedDataView = compressedData.slice().order(byteOrder); + return () -> new CompressedBlockReader( + compression, + numBlocks, + blockSize, + offsetView.asReadOnlyBuffer(), + compressedDataView.asReadOnlyBuffer().order(byteOrder), + byteOrder + ); + } + throw new IAE("Unknown version[%s]", versionFromBuffer); + } + + private final CompressionStrategy.Decompressor decompressor; + + private final int numBlocks; + private final int div; + private final int rem; + private final IntBuffer endOffsetsBuffer; + private final ByteBuffer compressedDataBuffer; + + private final ResourceHolder<ByteBuffer> decompressedDataBufferHolder; + private final ByteBuffer decompressedDataBuffer; + + private final ByteOrder byteOrder; + private final Closer closer; + private int currentBlockNumber = -1; + + public CompressedBlockReader( + CompressionStrategy compressionStrategy, + int numBlocks, + int blockSize, + IntBuffer endOffsetsBuffer, + ByteBuffer compressedDataBuffer, + ByteOrder byteOrder + ) + { + this.decompressor = compressionStrategy.getDecompressor(); + this.numBlocks = numBlocks; + this.div = Integer.numberOfTrailingZeros(blockSize); + this.rem = blockSize - 1; + this.endOffsetsBuffer = endOffsetsBuffer; + this.compressedDataBuffer = compressedDataBuffer; + this.closer = Closer.create(); + this.decompressedDataBufferHolder = CompressedPools.getByteBuf(byteOrder); + closer.register(decompressedDataBufferHolder); + this.decompressedDataBuffer = decompressedDataBufferHolder.get(); + this.decompressedDataBuffer.clear(); + this.byteOrder = byteOrder; + } + + /** + * Get size in bytes of virtual contiguous buffer + */ + public long getSize() Review Comment: no, this is the end position of the last uncompressed value so the uncompressed size. The `endOffsetsBuffer` contains the uncompressed end position of every value that is compressed in the blocks. Using the uncompressed start and end position of a value and knowing the size of blocks we can then locate which block(s) the value is contained in to extract it. I'll try to add some more comments and javadocs to this area so its a bit clearer what is going on -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
