Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ public AlignedChunkWriterImpl(List<IMeasurementSchema> schemaList) {

public AlignedChunkWriterImpl(
List<IMeasurementSchema> schemaList, EncryptParameter encryptParam) {
this(schemaList, encryptParam, 0);
}

public AlignedChunkWriterImpl(
List<IMeasurementSchema> schemaList,
EncryptParameter encryptParam,
int valueOutInitialCapacity) {
this.encryptParam = encryptParam;
TSEncoding timeEncoding =
TSEncoding.valueOf(TSFileDescriptor.getInstance().getConfig().getTimeEncoder());
Expand All @@ -246,7 +253,8 @@ public AlignedChunkWriterImpl(
schemaList.get(i).getType(),
schemaList.get(i).getEncodingType(),
schemaList.get(i).getValueEncoder(),
this.encryptParam));
this.encryptParam,
valueOutInitialCapacity));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each aligned measurement has its own ValueChunkWriter and encoded-value buffer. Forwarding the capacity here applies the estimate to every aligned value column, not only standalone value-page construction.

}

this.valueIndex = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class ChunkWriterImpl implements IChunkWriter {

private int numOfPages;

private long pageDataSize;

/** write data into current page */
private PageWriter pageWriter;

Expand Down Expand Up @@ -346,6 +348,7 @@ private void writePageToPageBuffer() {
} else {
pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages);
}
pageDataSize += pageWriter.getLastPageDataSize();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A chunk buffer can contain multiple pages. Accumulating each encoded body here gives the explicit writer overload the exact body-byte total when the packed chunk is flushed.


// update statistics of this chunk
numOfPages++;
Expand All @@ -372,6 +375,7 @@ public void writeToFileWriter(

// reinit this chunk writer
pageBuffer.reset();
pageDataSize = 0;
numOfPages = 0;
sizeWithoutStatistic = 0;
firstPageStatistics = null;
Expand Down Expand Up @@ -493,6 +497,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header)
}
numOfPages++;
// write page content to temp PBAOS
pageDataSize += data.remaining();
try (WritableByteChannel channel = Channels.newChannel(pageBuffer)) {
channel.write(data);
} catch (IOException e) {
Expand Down Expand Up @@ -535,7 +540,7 @@ private void writeAllPagesOfChunkToTsFile(
long dataOffset = writer.getPos();

// write all pages of this column
writer.writeBytesToStream(pageBuffer);
writer.writeBytesToStream(pageBuffer, pageDataSize);

int dataSize = (int) (writer.getPos() - dataOffset);
if (dataSize != pageBuffer.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class TimeChunkWriter {

private int numOfPages;

private long pageDataSize;

/** write data into current page */
private TimePageWriter pageWriter;

Expand Down Expand Up @@ -208,6 +210,7 @@ public void writePageToPageBuffer() {
} else {
pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages);
}
pageDataSize += pageWriter.getLastPageDataSize();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parallel accumulator covers the time-column pages of aligned flushes. It keeps time body bytes in the ratio while their page headers remain excluded.


// update statistics of this chunk
numOfPages++;
Expand Down Expand Up @@ -261,6 +264,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header)
}
numOfPages++;
// write page content to temp PBAOS
pageDataSize += data.remaining();
try (WritableByteChannel channel = Channels.newChannel(pageBuffer)) {
channel.write(data);
} catch (IOException e) {
Expand All @@ -274,6 +278,7 @@ public void writeToFileWriter(TsFileIOWriter tsfileWriter) throws IOException {

// reinit this chunk writer
pageBuffer.reset();
pageDataSize = 0;
numOfPages = 0;
sizeWithoutStatistic = 0;
firstPageStatistics = null;
Expand Down Expand Up @@ -352,7 +357,7 @@ public void writeAllPagesOfChunkToTsFile(TsFileIOWriter writer) throws IOExcepti
long dataOffset = writer.getPos();

// write all pages of this column
writer.writeBytesToStream(pageBuffer);
writer.writeBytesToStream(pageBuffer, pageDataSize);

int dataSize = (int) (writer.getPos() - dataOffset);
if (dataSize != pageBuffer.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class ValueChunkWriter {

private int numOfPages;

private long pageDataSize;

/** write data into current page */
private ValuePageWriter pageWriter;

Expand Down Expand Up @@ -96,25 +98,14 @@ public ValueChunkWriter(
TSDataType dataType,
TSEncoding encodingType,
Encoder valueEncoder) {
this.measurementId = measurementId;
this.encodingType = encodingType;
this.dataType = dataType;
this.compressionType = compressionType;
this.encryptParam = EncryptUtils.getEncryptParameter();
this.chunkOrdinal = nextChunkOrdinal();
this.pageBuffer = new PublicBAOS();
this.pageSizeThreshold = TSFileDescriptor.getInstance().getConfig().getPageSizeInByte();
this.maxNumberOfPointsInPage =
TSFileDescriptor.getInstance().getConfig().getMaxNumberOfPointsInPage();
this.valueCountInOnePageForNextCheck = MINIMUM_RECORD_COUNT_FOR_CHECK;

// init statistics for this chunk and page
this.statistics = Statistics.getStatsByType(dataType);

this.pageWriter =
new ValuePageWriter(
valueEncoder, ICompressor.getCompressor(compressionType), dataType, this.encryptParam);
this.pageWriter.setChunkOrdinal(chunkOrdinal);
this(
measurementId,
compressionType,
dataType,
encodingType,
valueEncoder,
EncryptUtils.getEncryptParameter(),
0);
}

public ValueChunkWriter(
Expand All @@ -124,6 +115,17 @@ public ValueChunkWriter(
TSEncoding encodingType,
Encoder valueEncoder,
EncryptParameter encryptParam) {
this(measurementId, compressionType, dataType, encodingType, valueEncoder, encryptParam, 0);
}

public ValueChunkWriter(
String measurementId,
CompressionType compressionType,
TSDataType dataType,
TSEncoding encodingType,
Encoder valueEncoder,
EncryptParameter encryptParam,
int valueOutInitialCapacity) {
this.measurementId = measurementId;
this.encodingType = encodingType;
this.dataType = dataType;
Expand All @@ -140,8 +142,18 @@ public ValueChunkWriter(
this.statistics = Statistics.getStatsByType(dataType);

this.pageWriter =
new ValuePageWriter(
valueEncoder, ICompressor.getCompressor(compressionType), dataType, this.encryptParam);
valueOutInitialCapacity > 0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A positive flush estimate is supplied to the capacity-aware ValuePageWriter constructor. Zero selects the default allocation path when no historical estimate is available.

? new ValuePageWriter(
valueEncoder,
ICompressor.getCompressor(compressionType),
dataType,
this.encryptParam,
valueOutInitialCapacity)
: new ValuePageWriter(
valueEncoder,
ICompressor.getCompressor(compressionType),
dataType,
this.encryptParam);
this.pageWriter.setChunkOrdinal(chunkOrdinal);
}

Expand Down Expand Up @@ -240,6 +252,7 @@ public void writePageToPageBuffer() {
} else {
pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages);
}
pageDataSize += pageWriter.getLastPageDataSize();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This accumulator covers value-column pages and is reset with the chunk buffer after emission. Together with the time-column counter it represents aligned flush page bodies.


// update statistics of this chunk
numOfPages++;
Expand Down Expand Up @@ -307,6 +320,7 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header)
}
numOfPages++;
// write page content to temp PBAOS
pageDataSize += data.remaining();
try (WritableByteChannel channel = Channels.newChannel(pageBuffer)) {
channel.write(data);
} catch (IOException e) {
Expand All @@ -326,6 +340,7 @@ public void writeToFileWriter(

// reinit this chunk writer
pageBuffer.reset();
pageDataSize = 0;
numOfPages = 0;
sizeWithoutStatistic = 0;
firstPageStatistics = null;
Expand Down Expand Up @@ -472,7 +487,7 @@ public void writeAllPagesOfChunkToTsFile(
long dataOffset = writer.getPos();

// write all pages of this column
writer.writeBytesToStream(pageBuffer);
writer.writeBytesToStream(pageBuffer, pageDataSize);

int dataSize = (int) (writer.getPos() - dataOffset);
if (dataSize != pageBuffer.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class PageWriter {
*/
private Statistics<? extends Serializable> statistics;

private int lastPageDataSize;

public PageWriter() {
this((Encoder) null, null);
}
Expand Down Expand Up @@ -233,6 +235,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first)
/** write the page header and data into the PageWriter's output stream. */
public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, int pageIndex)
throws IOException {
lastPageDataSize = 0;
if (statistics.getCount() == 0) {
return 0;
}
Expand All @@ -243,6 +246,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first,
PageBodyEncoder.encode(
pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal);
int pageBodySize = pageBody.remaining();
lastPageDataSize = pageBodySize;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PageBodyEncoder has already produced the bytes appended after the page header. Recording this length excludes page-header varints and statistics from non-aligned page-data accounting.


// write the page header to IOWriter
int sizeWithoutStatistic = 0;
Expand All @@ -264,6 +268,10 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first,
return sizeWithoutStatistic;
}

public int getLastPageDataSize() {
return lastPageDataSize;
}

/**
* calculate max possible memory size it occupies, including time outputStream and value
* outputStream, because size outputStream is never used until flushing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class TimePageWriter {
*/
private TimeStatistics statistics;

private int lastPageDataSize;

public TimePageWriter(Encoder timeEncoder, ICompressor compressor) {
this.timeOut = new PublicBAOS();
this.timeEncoder = timeEncoder;
Expand Down Expand Up @@ -116,6 +118,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first)
/** write the page header and data into the PageWriter's output stream. */
public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, int pageIndex)
throws IOException {
lastPageDataSize = 0;
if (statistics.getCount() == 0) {
return 0;
}
Expand All @@ -126,6 +129,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first,
PageBodyEncoder.encode(
pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal);
int pageBodySize = pageBody.remaining();
lastPageDataSize = pageBodySize;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aligned time pages also contribute compressed page-body bytes to the flush ratio. Capturing the encoded body length here keeps their page headers out of the count.


// write the page header to IOWriter
int sizeWithoutStatistic = 0;
Expand All @@ -149,6 +153,10 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first,
return sizeWithoutStatistic;
}

public int getLastPageDataSize() {
return lastPageDataSize;
}

/**
* calculate max possible memory size it occupies, including time outputStream and value
* outputStream, because size outputStream is never used until flushing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,57 @@ public class ValuePageWriter {

private int size;

private int lastPageDataSize;

private final PublicBAOS bitmapOut;

private static final int MASK = 1 << 7;

private static final byte[] NULL_BITMAP_BATCH = new byte[1024];

public ValuePageWriter(Encoder valueEncoder, ICompressor compressor, TSDataType dataType) {
this.valueOut = new PublicBAOS();
this.bitmap = 0;
this.size = 0;
this.bitmapOut = new PublicBAOS();
this.valueEncoder = valueEncoder;
this.statistics = Statistics.getStatsByType(dataType);
this.compressor = compressor;
this.encryptParam = EncryptUtils.getEncryptParameter();
this(valueEncoder, compressor, dataType, EncryptUtils.getEncryptParameter());
}

public ValuePageWriter(
Encoder valueEncoder,
ICompressor compressor,
TSDataType dataType,
EncryptParameter encryptParam) {
this.valueOut = new PublicBAOS();
this(valueEncoder, compressor, dataType, encryptParam, new PublicBAOS());
}

/** Creates a page writer with an initial capacity in bytes for the encoded values buffer. */
public ValuePageWriter(
Encoder valueEncoder,
ICompressor compressor,
TSDataType dataType,
int valueOutInitialCapacity) {
this(
valueEncoder,
compressor,
dataType,
EncryptUtils.getEncryptParameter(),
valueOutInitialCapacity);
}

/** Creates a page writer with explicit encryption and an initial encoded values capacity. */
public ValuePageWriter(
Encoder valueEncoder,
ICompressor compressor,
TSDataType dataType,
EncryptParameter encryptParam,
int valueOutInitialCapacity) {
this(valueEncoder, compressor, dataType, encryptParam, new PublicBAOS(valueOutInitialCapacity));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating PublicBAOS with this capacity sizes the encoded-value buffer before its first encoder write, allowing flush estimates to avoid repeated growth in this stream. The encryption-aware overload reaches the same allocation point while using its supplied EncryptParameter.

@jt2594838 jt2594838 Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Routing constructor variants through the shared initializer keeps statistics, compressor, and chunk-ordinal setup consistent. The capacity-aware overload supplies a pre-sized PublicBAOS through that same initialization path.

}

private ValuePageWriter(
Encoder valueEncoder,
ICompressor compressor,
TSDataType dataType,
EncryptParameter encryptParam,
PublicBAOS valueOut) {
this.valueOut = valueOut;
this.bitmap = 0;
this.size = 0;
this.bitmapOut = new PublicBAOS();
Expand Down Expand Up @@ -407,6 +435,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first)
/** write the page header and data into the PageWriter's output stream. */
public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, int pageIndex)
throws IOException {
lastPageDataSize = 0;
if (size == 0) {
return 0;
} else if (statistics.getCount() == 0) {
Expand All @@ -420,6 +449,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first,
PageBodyEncoder.encode(
pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal);
int pageBodySize = pageBody.remaining();
lastPageDataSize = pageBodySize;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This records the compressed or encrypted page body after PageBodyEncoder. The page header and statistics are serialized separately, so this length is the data-only portion used by flush accounting.


// write the page header to IOWriter
int sizeWithoutStatistic = 0;
Expand All @@ -440,6 +470,10 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first,
return sizeWithoutStatistic;
}

public int getLastPageDataSize() {
return lastPageDataSize;
}

/**
* calculate max possible memory size it occupies, including time outputStream and value
* outputStream, because size outputStream is never used until flushing.
Expand Down
Loading
Loading