richardstartin commented on a change in pull request #7661: URL: https://github.com/apache/pinot/pull/7661#discussion_r743438666
########## File path: pinot-segment-local/src/main/java/org/apache/pinot/segment/local/io/writer/impl/VarByteChunkSVForwardIndexWriterV4.java ########## @@ -0,0 +1,235 @@ +/** + * 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.pinot.segment.local.io.writer.impl; + +import com.google.common.base.Preconditions; +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.FileChannel; +import java.nio.charset.StandardCharsets; +import javax.annotation.concurrent.NotThreadSafe; +import org.apache.commons.io.FileUtils; +import org.apache.pinot.segment.local.io.compression.ChunkCompressorFactory; +import org.apache.pinot.segment.spi.compression.ChunkCompressionType; +import org.apache.pinot.segment.spi.compression.ChunkCompressor; +import org.apache.pinot.segment.spi.memory.CleanerUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Class to write out variable length bytes into a single column. + * + * + * Only sequential writes are supported. + */ +@NotThreadSafe +public class VarByteChunkSVForwardIndexWriterV4 implements VarByteChunkWriter { + + private static final Logger LOGGER = LoggerFactory.getLogger(VarByteChunkSVForwardIndexWriterV4.class); + + public static final int VERSION = 4; + + private static final String DATA_BUFFER_SUFFIX = ".buf"; + + private final File _dataBuffer; + private final RandomAccessFile _output; + private final FileChannel _dataChannel; + private final ByteBuffer _chunkBuffer; + private final ChunkCompressor _chunkCompressor; + + private int _docIdOffset = 0; + private int _nextDocId = 0; + private int _metadataSize = 0; + private long _chunkOffset = 0; + + public VarByteChunkSVForwardIndexWriterV4(File file, ChunkCompressionType compressionType, int chunkSize) + throws IOException { + _dataBuffer = new File(file.getName() + DATA_BUFFER_SUFFIX); + _output = new RandomAccessFile(file, "rw"); + _dataChannel = new RandomAccessFile(_dataBuffer, "rw").getChannel(); + _chunkCompressor = ChunkCompressorFactory.getCompressor(compressionType, true); + _chunkBuffer = ByteBuffer.allocateDirect(chunkSize).order(ByteOrder.LITTLE_ENDIAN); + // reserve space for numDocs + _chunkBuffer.position(Integer.BYTES); + writeHeader(_chunkCompressor.compressionType(), chunkSize); + } + + private void writeHeader(ChunkCompressionType compressionType, int targetDecompressedChunkSize) + throws IOException { + // keep metadata BE for backwards compatibility + // (e.g. the version needs to be read by a factory which assumes BE) + _output.writeInt(VERSION); + _output.writeInt(targetDecompressedChunkSize); + _output.writeInt(compressionType.getValue()); + // reserve a slot to write the data offset into + _output.writeInt(0); + _metadataSize += 4 * Integer.BYTES; + } + + @Override + public void putString(String string) { + putBytes(string.getBytes(StandardCharsets.UTF_8)); + } + + @Override + public void putBytes(byte[] bytes) { + Preconditions.checkState(_chunkOffset < 1L << 32, "exceeded 4GB of compressed chunks"); + int sizeRequired = Integer.BYTES + bytes.length; + if (_chunkBuffer.position() >= _chunkBuffer.capacity() - sizeRequired) { Review comment: I agree -- 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: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org