|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pinot.segment.local.io.codec; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.util.List; |
| 24 | +import org.apache.pinot.segment.local.io.codec.compression.ChunkCompressorFactory; |
| 25 | +import org.apache.pinot.segment.local.io.codec.transform.ChunkTransformFactory; |
| 26 | +import org.apache.pinot.segment.spi.codec.ChunkCodec; |
| 27 | +import org.apache.pinot.segment.spi.codec.ChunkCodecPipeline; |
| 28 | +import org.apache.pinot.segment.spi.codec.ChunkTransform; |
| 29 | +import org.apache.pinot.segment.spi.compression.ChunkDecompressor; |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * A {@link ChunkDecompressor} that reverses a codec pipeline: first decompresses using the |
| 34 | + * terminal {@link ChunkCodec.CodecKind#COMPRESSOR COMPRESSOR}, then applies all |
| 35 | + * {@link ChunkCodec.CodecKind#TRANSFORM TRANSFORM} stages in reverse order (right-to-left). |
| 36 | + * |
| 37 | + * <p>This is the read-path counterpart of {@link PipelineChunkCompressor}.</p> |
| 38 | + */ |
| 39 | +public class PipelineChunkDecompressor implements ChunkDecompressor { |
| 40 | + |
| 41 | + private final ChunkCodecPipeline _pipeline; |
| 42 | + private final ChunkTransform[] _transforms; |
| 43 | + private final ChunkDecompressor _terminalDecompressor; |
| 44 | + private final int _valueSizeInBytes; |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a pipeline decompressor. |
| 48 | + * |
| 49 | + * @param pipeline the codec pipeline |
| 50 | + * @param valueSizeInBytes size of each typed value (4 for INT, 8 for LONG); used by transforms |
| 51 | + */ |
| 52 | + public PipelineChunkDecompressor(ChunkCodecPipeline pipeline, int valueSizeInBytes) { |
| 53 | + _pipeline = pipeline; |
| 54 | + _valueSizeInBytes = valueSizeInBytes; |
| 55 | + |
| 56 | + List<ChunkCodec> transformStages = pipeline.getTransforms(); |
| 57 | + _transforms = new ChunkTransform[transformStages.size()]; |
| 58 | + for (int i = 0; i < transformStages.size(); i++) { |
| 59 | + _transforms[i] = ChunkTransformFactory.getTransform(transformStages.get(i)); |
| 60 | + } |
| 61 | + |
| 62 | + _terminalDecompressor = ChunkCompressorFactory.getDecompressor( |
| 63 | + pipeline.getChunkCompressionType()); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public int decompress(ByteBuffer compressedInput, ByteBuffer decompressedOutput) |
| 68 | + throws IOException { |
| 69 | + // Decompress using terminal decompressor. |
| 70 | + // Per Pinot convention, after this call the output buffer is flipped: position=0, limit=dataSize. |
| 71 | + int decompressedSize = _terminalDecompressor.decompress(compressedInput, decompressedOutput); |
| 72 | + |
| 73 | + if (_transforms.length > 0) { |
| 74 | + // Buffer is already in read mode (flipped). Transforms operate from position=0. |
| 75 | + int numBytes = decompressedOutput.remaining(); |
| 76 | + |
| 77 | + // Apply transforms in reverse order (right-to-left) |
| 78 | + for (int i = _transforms.length - 1; i >= 0; i--) { |
| 79 | + _transforms[i].decode(decompressedOutput, numBytes, _valueSizeInBytes); |
| 80 | + } |
| 81 | + // Buffer remains flipped: position=0, limit=numBytes — ready for the caller to read. |
| 82 | + } |
| 83 | + |
| 84 | + return decompressedSize; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public int decompressedLength(ByteBuffer compressedInput) |
| 89 | + throws IOException { |
| 90 | + return _terminalDecompressor.decompressedLength(compressedInput); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void close() |
| 95 | + throws IOException { |
| 96 | + _terminalDecompressor.close(); |
| 97 | + } |
| 98 | +} |
0 commit comments