2121
2222import org .apache .iotdb .common .rpc .thrift .TTimePartitionSlot ;
2323import org .apache .iotdb .commons .utils .TimePartitionUtils ;
24+ import org .apache .iotdb .db .queryengine .plan .planner .plan .node .load .LoadTsFilePieceNode ;
2425
2526import org .apache .tsfile .enums .TSDataType ;
2627import org .apache .tsfile .exception .write .PageException ;
@@ -66,16 +67,15 @@ public class AlignedChunkData implements ChunkData {
6667 protected final IDeviceID device ;
6768 protected List <ChunkHeader > chunkHeaderList ;
6869
69- protected final PublicBAOS byteStream ;
70- protected final DataOutputStream stream ;
70+ protected PublicBAOS byteStream ;
71+ protected DataOutputStream stream ;
7172 protected List <long []> timeBatch ;
7273 protected long dataSize ;
7374 protected boolean needDecodeChunk ;
7475 protected List <Integer > pageNumbers ;
7576 protected Queue <Integer > satisfiedLengthQueue ;
7677
77- private AlignedChunkWriterImpl chunkWriter ;
78- protected List <Chunk > chunkList ;
78+ protected ByteBuffer chunkData ;
7979
8080 public AlignedChunkData (
8181 @ Nonnull final IDeviceID device ,
@@ -143,14 +143,8 @@ public boolean isAligned() {
143143 }
144144
145145 @ Override
146- public void writeToFileWriter (final TsFileIOWriter writer ) throws IOException {
147- if (chunkList != null ) {
148- for (final Chunk chunk : chunkList ) {
149- writer .writeChunk (chunk );
150- }
151- } else {
152- chunkWriter .writeToFileWriter (writer );
153- }
146+ public void writeToFileWriter (final TsFileIOWriter writer ) throws IOException , PageException {
147+ writeTsFileData (writer );
154148 }
155149
156150 public void addValueChunk (final ChunkHeader chunkHeader ) {
@@ -167,6 +161,7 @@ public void serialize(final DataOutputStream stream) throws IOException {
167161 ReadWriteIOUtils .write (getType ().ordinal (), stream );
168162 ReadWriteIOUtils .write (isAligned (), stream );
169163 serializeAttr (stream );
164+ ReadWriteIOUtils .write (byteStream .size (), stream );
170165 byteStream .writeTo (stream );
171166 close ();
172167 }
@@ -291,26 +286,41 @@ public void writeDecodeValuePage(
291286 }
292287 }
293288
294- protected void deserializeTsFileData (final InputStream stream ) throws IOException , PageException {
289+ protected void writeTsFileData (TsFileIOWriter writer ) throws IOException , PageException {
290+ final InputStream stream = new LoadTsFilePieceNode .ByteBufferInputStream (chunkData );
295291 if (needDecodeChunk ) {
296- buildChunkWriter (stream );
292+ writeChunkToWriter (stream , writer );
293+ } else {
294+ writeEntireChunkToWriter (stream , writer );
295+ }
296+ }
297+
298+ protected void deserializeTsFileDataByte (final InputStream stream ) throws IOException {
299+ final int size = ReadWriteIOUtils .readInt (stream );
300+ if (stream instanceof LoadTsFilePieceNode .ByteBufferInputStream ) {
301+ this .chunkData = ((LoadTsFilePieceNode .ByteBufferInputStream ) stream ).read (size );
297302 } else {
298- deserializeEntireChunk (stream );
303+ byte [] data = new byte [size ];
304+ if (size != stream .read (data )) {
305+ throw new IOException ("TsFileData byte array read error, size mismatch." );
306+ }
307+ this .chunkData = ByteBuffer .wrap (data );
299308 }
300309 }
301310
302- private void deserializeEntireChunk (final InputStream stream ) throws IOException {
303- chunkList = new ArrayList <>();
311+ private void writeEntireChunkToWriter (final InputStream stream , final TsFileIOWriter writer )
312+ throws IOException {
304313 for (final ChunkHeader chunkHeader : chunkHeaderList ) {
305314 final ByteBuffer chunkData =
306315 ByteBuffer .wrap (ReadWriteIOUtils .readBytesWithSelfDescriptionLength (stream ));
307316 final Statistics <? extends Serializable > statistics =
308317 Statistics .deserialize (stream , chunkHeader .getDataType ());
309- chunkList . add (new Chunk (chunkHeader , chunkData , null , statistics ));
318+ writer . writeChunk (new Chunk (chunkHeader , chunkData , null , statistics ));
310319 }
311320 }
312321
313- protected void buildChunkWriter (final InputStream stream ) throws IOException , PageException {
322+ protected void writeChunkToWriter (final InputStream stream , final TsFileIOWriter writer )
323+ throws IOException , PageException {
314324 final List <IMeasurementSchema > measurementSchemaList = new ArrayList <>();
315325 IMeasurementSchema timeSchema = null ;
316326 for (final ChunkHeader chunkHeader : chunkHeaderList ) {
@@ -330,17 +340,20 @@ protected void buildChunkWriter(final InputStream stream) throws IOException, Pa
330340 chunkHeader .getEncodingType (),
331341 chunkHeader .getCompressionType ()));
332342 }
333- chunkWriter = new AlignedChunkWriterImpl (timeSchema , measurementSchemaList );
343+ AlignedChunkWriterImpl chunkWriter =
344+ new AlignedChunkWriterImpl (timeSchema , measurementSchemaList );
334345 timeBatch = new ArrayList <>();
335346 final int chunkHeaderSize = chunkHeaderList .size ();
336347 for (int i = 0 ; i < chunkHeaderSize ; i ++) {
337- buildChunk (stream , chunkHeaderList .get (i ), pageNumbers .get (i ), i - 1 , i == 0 );
348+ buildChunk (chunkWriter , stream , chunkHeaderList .get (i ), pageNumbers .get (i ), i - 1 , i == 0 );
338349 }
339350 timeBatch = null ;
351+ chunkWriter .writeToFileWriter (writer );
340352 }
341353
342354 @ SuppressWarnings ({"squid:S6541" , "squid:S3776" })
343355 private void buildChunk (
356+ final AlignedChunkWriterImpl chunkWriter ,
344357 final InputStream stream ,
345358 final ChunkHeader chunkHeader ,
346359 final int pageNumber ,
@@ -463,7 +476,7 @@ public static AlignedChunkData deserialize(final InputStream stream)
463476 chunkData .needDecodeChunk = needDecodeChunk ;
464477 chunkData .chunkHeaderList = chunkHeaderList ;
465478 chunkData .pageNumbers = pageNumbers ;
466- chunkData .deserializeTsFileData (stream );
479+ chunkData .deserializeTsFileDataByte (stream );
467480 chunkData .dataSize = dataSize ;
468481 chunkData .close ();
469482 return chunkData ;
0 commit comments