shuwenwei commented on code in PR #14766: URL: https://github.com/apache/iotdb/pull/14766#discussion_r1929576045
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/tools/utils/TsFileRewriteOverPrecisedI64Scan.java: ########## @@ -0,0 +1,218 @@ +/* + * 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.iotdb.db.tools.utils; + +import org.apache.tsfile.encoding.encoder.TSEncodingBuilder; +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.file.header.ChunkHeader; +import org.apache.tsfile.file.header.PageHeader; +import org.apache.tsfile.file.metadata.statistics.Statistics; +import org.apache.tsfile.read.common.BatchData; +import org.apache.tsfile.read.common.Chunk; +import org.apache.tsfile.read.reader.IPageReader; +import org.apache.tsfile.read.reader.chunk.AlignedChunkReader; +import org.apache.tsfile.read.reader.chunk.ChunkReader; +import org.apache.tsfile.utils.Pair; +import org.apache.tsfile.utils.TsPrimitiveType; +import org.apache.tsfile.write.chunk.ChunkWriterImpl; +import org.apache.tsfile.write.chunk.ValueChunkWriter; +import org.apache.tsfile.write.schema.MeasurementSchema; +import org.apache.tsfile.write.writer.TsFileIOWriter; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Collections; +import java.util.List; + +public class TsFileRewriteOverPrecisedI64Scan extends TsFileSequenceScan { + + private final File target; + private TsFileIOWriter writer; + private Chunk currTimeChunk; + + public TsFileRewriteOverPrecisedI64Scan(File target) throws IOException { + this.target = target; + } + + public static void main(String[] args) throws IOException { + File sourceFile = new File(args[0]); + File targetFile = new File(args[1]); + TsFileRewriteOverPrecisedI64Scan scan = new TsFileRewriteOverPrecisedI64Scan(targetFile); + scan.scanTsFile(sourceFile); + + long sourceLength = sourceFile.length(); + long targetLength = targetFile.length(); + System.out.printf( + "Before rewrite %d, after rewrite %d, ratio %f%n", + sourceLength, targetLength, sourceLength * 1.0 / targetLength); + } + + @Override + protected boolean onFileOpen(File file) throws IOException { + boolean shouldScan = super.onFileOpen(file); + if (shouldScan) { + writer = new TsFileIOWriter(target); + } + return shouldScan; + } + + @Override + protected void onFileEnd() throws IOException { + writer.endChunkGroup(); + writer.endFile(); + } + + @Override + protected void onChunkGroup() throws IOException { + if (currDeviceID != null) { + writer.endChunkGroup(); + } + super.onChunkGroup(); + writer.startChunkGroup(currDeviceID); + } + + @Override + protected void onChunk(PageVisitor pageVisitor) throws IOException { + reader.position(reader.position() - 1); + Chunk chunk = reader.readMemChunk(reader.position()); + chunk = + new Chunk( + chunk.getHeader(), + chunk.getData(), + Collections.emptyList(), + Statistics.getStatsByType(chunk.getHeader().getDataType())); + currMeasurementID = chunk.getHeader().getMeasurementID(); + currTimeseriesID = new Pair<>(currDeviceID, currMeasurementID); + if (!currDeviceAligned) { + onNonAlignedChunk(chunk); + } else { + onAlignedChunk(chunk); + } + System.out.println("Processed a chunk of " + currDeviceID + "." + currMeasurementID); + reader.position( + reader.position() + + chunk.getHeader().getSerializedSize() + + chunk.getHeader().getDataSize()); + } + + private void onNonAlignedChunk(Chunk chunk) throws IOException { + if (chunk.getHeader().getDataType() != TSDataType.INT64) { + writer.writeChunk(chunk); + } else { + if (!rewriteInt64ChunkNonAligned(chunk)) { + writer.writeChunk(chunk); + } + } + } + + private void onAlignedChunk(Chunk chunk) throws IOException { + if (isTimeChunk || chunk.getHeader().getDataType() != TSDataType.INT64) { + writer.writeChunk(chunk); + if (isTimeChunk) { + currTimeChunk = chunk; + } + } else { + if (!rewriteInt64ChunkAligned(chunk)) { + writer.writeChunk(chunk); + } + } + } + + private boolean rewriteInt64ChunkNonAligned(Chunk chunk) throws IOException { + ChunkReader chunkReader = new ChunkReader(chunk); + ChunkHeader header = chunk.getHeader(); + List<IPageReader> pageReaders = chunkReader.loadPageReaderList(); + ChunkWriterImpl chunkWriter = + new ChunkWriterImpl( + new MeasurementSchema( + header.getMeasurementID(), + TSDataType.INT32, + header.getEncodingType(), + header.getCompressionType())); + + for (IPageReader pageReader : pageReaders) { + BatchData batchData = pageReader.getAllSatisfiedPageData(); + while (batchData.hasCurrent()) { + int intVal = (int) batchData.getLong(); + if (intVal != batchData.getLong()) { + // the chunk is not over-precised + return false; + } + chunkWriter.write(batchData.currentTime(), (int) batchData.getLong()); + } + chunkWriter.sealCurrentPage(); + } + chunkWriter.writeToFileWriter(writer); + return true; + } + + private boolean rewriteInt64ChunkAligned(Chunk chunk) throws IOException { + AlignedChunkReader chunkReader = + new AlignedChunkReader(currTimeChunk, Collections.singletonList(chunk)); Review Comment: Suppose there are (time, s1, s2, s3,) and we want to rewrite chunks of s1. At this time, a TimeValuePair is (1, null, 1, 1). When only (time, s1) is provided to AlignedChunkReader, this row will not be returned because all value is null, and the new chunk written by ValueChunkWriter may not be able to align the value with the original time chunk. -- 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]
