2525import org .apache .iotdb .db .storageengine .dataregion .wal .buffer .IWALByteBufferView ;
2626import org .apache .iotdb .db .storageengine .dataregion .wal .utils .WALWriteUtils ;
2727import org .apache .iotdb .db .utils .datastructure .AlignedTVList ;
28+ import org .apache .iotdb .db .utils .datastructure .BatchEncodeInfo ;
2829import org .apache .iotdb .db .utils .datastructure .MemPointIterator ;
2930import org .apache .iotdb .db .utils .datastructure .MemPointIteratorFactory ;
3031import org .apache .iotdb .db .utils .datastructure .TVList ;
3132
3233import org .apache .tsfile .common .conf .TSFileDescriptor ;
3334import org .apache .tsfile .enums .TSDataType ;
3435import org .apache .tsfile .read .common .TimeRange ;
35- import org .apache .tsfile .read .common .block .TsBlock ;
3636import org .apache .tsfile .utils .Binary ;
3737import org .apache .tsfile .utils .BitMap ;
3838import org .apache .tsfile .utils .Pair ;
3939import org .apache .tsfile .write .UnSupportedDataTypeException ;
4040import org .apache .tsfile .write .chunk .AlignedChunkWriterImpl ;
4141import org .apache .tsfile .write .chunk .IChunkWriter ;
42- import org .apache .tsfile .write .chunk .ValueChunkWriter ;
4342import org .apache .tsfile .write .schema .IMeasurementSchema ;
4443import org .apache .tsfile .write .schema .MeasurementSchema ;
4544
@@ -65,6 +64,7 @@ public class AlignedWritableMemChunk extends AbstractWritableMemChunk {
6564 private final List <IMeasurementSchema > schemaList ;
6665 private AlignedTVList list ;
6766 private List <AlignedTVList > sortedList ;
67+ private long sortedRowCount = 0 ;
6868 private final boolean ignoreAllNullRows ;
6969
7070 private static final IoTDBConfig CONFIG = IoTDBDescriptor .getInstance ().getConfig ();
@@ -197,6 +197,7 @@ protected void handoverAlignedTvList() {
197197 list .sort ();
198198 }
199199 sortedList .add (list );
200+ this .sortedRowCount += list .rowCount ();
200201 this .list = AlignedTVList .newAlignedList (new ArrayList <>(dataTypes ));
201202 this .dataTypes = list .getTsDataTypes ();
202203 }
@@ -352,15 +353,11 @@ public long count() {
352353
353354 @ Override
354355 public long rowCount () {
355- return alignedListSize ();
356+ return sortedRowCount + list . rowCount ();
356357 }
357358
358359 public int alignedListSize () {
359- int rowCount = list .rowCount ();
360- for (AlignedTVList alignedTvList : sortedList ) {
361- rowCount += alignedTvList .rowCount ();
362- }
363- return rowCount ;
360+ return (int ) rowCount ();
364361 }
365362
366363 @ Override
@@ -638,107 +635,56 @@ private void handleEncoding(
638635 }
639636
640637 @ Override
641- public synchronized void encode (BlockingQueue <Object > ioTaskQueue ) {
638+ public synchronized void encode (
639+ BlockingQueue <Object > ioTaskQueue , BatchEncodeInfo encodeInfo , long [] times ) {
642640 if (TVLIST_SORT_THRESHOLD == 0 ) {
643641 encodeWorkingAlignedTVList (ioTaskQueue );
644642 return ;
645643 }
646644
647645 AlignedChunkWriterImpl alignedChunkWriter = new AlignedChunkWriterImpl (schemaList );
646+
648647 // create MergeSortAlignedTVListIterator.
649648 List <AlignedTVList > alignedTvLists = new ArrayList <>(sortedList );
650649 alignedTvLists .add (list );
650+ List <Integer > columnIndexList = buildColumnIndexList (schemaList );
651651 MemPointIterator timeValuePairIterator =
652- MemPointIteratorFactory .create (dataTypes , null , alignedTvLists , ignoreAllNullRows );
653-
654- int pointNumInPage = 0 ;
655- int pointNumInChunk = 0 ;
656- long [] times = new long [MAX_NUMBER_OF_POINTS_IN_PAGE ];
652+ MemPointIteratorFactory .create (
653+ dataTypes , columnIndexList , alignedTvLists , ignoreAllNullRows );
657654
658655 while (timeValuePairIterator .hasNextBatch ()) {
659- TsBlock tsBlock = timeValuePairIterator .nextBatch ();
660- if (tsBlock == null ) {
661- continue ;
656+ timeValuePairIterator .encodeBatch (alignedChunkWriter , encodeInfo , times );
657+ if (encodeInfo .pointNumInPage >= MAX_NUMBER_OF_POINTS_IN_PAGE ) {
658+ alignedChunkWriter .write (times , encodeInfo .pointNumInPage , 0 );
659+ encodeInfo .pointNumInPage = 0 ;
662660 }
663- for (int rowIndex = 0 ; rowIndex < tsBlock .getPositionCount (); rowIndex ++) {
664- long time = tsBlock .getTimeByIndex (rowIndex );
665- times [pointNumInPage ] = time ;
666661
667- for (int columnIndex = 0 ; columnIndex < dataTypes .size (); columnIndex ++) {
668- ValueChunkWriter valueChunkWriter =
669- alignedChunkWriter .getValueChunkWriterByIndex (columnIndex );
670- if (tsBlock .getColumn (columnIndex ).isNull (rowIndex )) {
671- valueChunkWriter .write (time , null , true );
672- continue ;
673- }
674- switch (schemaList .get (columnIndex ).getType ()) {
675- case BOOLEAN :
676- valueChunkWriter .write (
677- time , tsBlock .getColumn (columnIndex ).getBoolean (rowIndex ), false );
678- break ;
679- case INT32 :
680- case DATE :
681- valueChunkWriter .write (time , tsBlock .getColumn (columnIndex ).getInt (rowIndex ), false );
682- break ;
683- case INT64 :
684- case TIMESTAMP :
685- valueChunkWriter .write (time , tsBlock .getColumn (columnIndex ).getLong (rowIndex ), false );
686- break ;
687- case FLOAT :
688- valueChunkWriter .write (
689- time , tsBlock .getColumn (columnIndex ).getFloat (rowIndex ), false );
690- break ;
691- case DOUBLE :
692- valueChunkWriter .write (
693- time , tsBlock .getColumn (columnIndex ).getDouble (rowIndex ), false );
694- break ;
695- case TEXT :
696- case BLOB :
697- case STRING :
698- valueChunkWriter .write (
699- time , tsBlock .getColumn (columnIndex ).getBinary (rowIndex ), false );
700- break ;
701- default :
702- break ;
703- }
704- }
705- pointNumInPage ++;
706- pointNumInChunk ++;
707-
708- // new page
709- if (pointNumInPage == MAX_NUMBER_OF_POINTS_IN_PAGE
710- || pointNumInChunk >= maxNumberOfPointsInChunk ) {
711- alignedChunkWriter .write (times , pointNumInPage , 0 );
712- pointNumInPage = 0 ;
713- }
714-
715- // new chunk
716- if (pointNumInChunk >= maxNumberOfPointsInChunk ) {
717- alignedChunkWriter .sealCurrentPage ();
718- alignedChunkWriter .clearPageWriter ();
719- try {
720- ioTaskQueue .put (alignedChunkWriter );
721- } catch (InterruptedException e ) {
722- Thread .currentThread ().interrupt ();
723- }
724- alignedChunkWriter = new AlignedChunkWriterImpl (schemaList );
725- pointNumInChunk = 0 ;
662+ if (encodeInfo .pointNumInChunk >= maxNumberOfPointsInChunk ) {
663+ alignedChunkWriter .sealCurrentPage ();
664+ alignedChunkWriter .clearPageWriter ();
665+ try {
666+ ioTaskQueue .put (alignedChunkWriter );
667+ } catch (InterruptedException e ) {
668+ Thread .currentThread ().interrupt ();
726669 }
670+ alignedChunkWriter = new AlignedChunkWriterImpl (schemaList );
671+ encodeInfo .reset ();
727672 }
728673 }
729674
730675 // last batch of points
731- if (pointNumInChunk > 0 ) {
732- if (pointNumInPage > 0 ) {
733- alignedChunkWriter .write (times , pointNumInPage , 0 );
734- alignedChunkWriter .sealCurrentPage ();
735- alignedChunkWriter .clearPageWriter ();
676+ if (encodeInfo .pointNumInChunk > 0 ) {
677+ if (encodeInfo .pointNumInPage > 0 ) {
678+ alignedChunkWriter .write (times , encodeInfo .pointNumInPage , 0 );
736679 }
680+ alignedChunkWriter .sealCurrentPage ();
681+ alignedChunkWriter .clearPageWriter ();
737682 try {
738683 ioTaskQueue .put (alignedChunkWriter );
739684 } catch (InterruptedException e ) {
740685 Thread .currentThread ().interrupt ();
741686 }
687+ encodeInfo .reset ();
742688 }
743689 }
744690
0 commit comments