Class TsFileIOWriter
- java.lang.Object
-
- org.apache.iotdb.tsfile.write.writer.TsFileIOWriter
-
- All Implemented Interfaces:
java.lang.AutoCloseable
- Direct Known Subclasses:
ForceAppendTsFileWriter,RestorableTsFileIOWriter
public class TsFileIOWriter extends java.lang.Object implements java.lang.AutoCloseableTsFileIOWriter is used to construct metadata and write data stored in memory to output stream.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleancanWritestatic java.lang.StringCHUNK_METADATA_TEMP_FILE_SUFFIXprotected java.util.List<ChunkGroupMetadata>chunkGroupMetadataListprotected java.util.List<ChunkMetadata>chunkMetadataListprotected java.io.FilechunkMetadataTempFileprotected static TSFileConfigconfigprotected ChunkMetadatacurrentChunkMetadataprotected longcurrentChunkMetadataSizeprotected booleanenableMemoryControlprotected java.util.LinkedList<java.lang.Long>endPosInCMTForDeviceprotected java.io.Filefileprotected booleanhasChunkMetadataInDiskprotected static byte[]MAGIC_STRING_BYTESprotected longmaxMetadataSizeprotected TsFileOutputoutprotected intpathCountprotected LocalTsFileOutputtempOutputstatic byteVERSION_NUMBER_BYTE
-
Constructor Summary
Constructors Modifier Constructor Description protectedTsFileIOWriter()empty construct function.TsFileIOWriter(java.io.File file)for writing a new tsfile.TsFileIOWriter(java.io.File file, boolean enableMemoryControl, long maxMetadataSize)for write with memory controlTsFileIOWriter(TsFileOutput output)for writing a new tsfile.TsFileIOWriter(TsFileOutput output, boolean test)for test only
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanWrite()intcheckMetadataSizeAndMayFlush()Check if the size of chunk metadata in memory is greater than the given threshold.voidclose()close the outputStream or file channel without writing FileMetadata.voidendChunkGroup()end chunk and write some log.voidendCurrentChunk()end chunk and write some log.voidendFile()write TSFileMetaData to output stream and close it.voidfilterChunks(java.util.Map<Path,java.util.List<java.lang.Long>> chunkStartTimes)Remove such ChunkMetadata that its startTime is not in chunkStartTimesvoidflush()java.util.List<ChunkGroupMetadata>getChunkGroupMetadataList()java.util.List<ChunkMetadata>getChunkMetadataListOfCurrentDeviceInMemory()This method should be called before flushing chunk group metadata list, otherwise, it will return null.java.lang.StringgetCurrentChunkGroupDeviceId()java.util.Map<java.lang.String,java.util.List<ChunkMetadata>>getDeviceChunkMetadataMap()java.util.Map<java.lang.String,java.util.List<TimeseriesMetadata>>getDeviceTimeseriesMetadataMap()this function is for Upgrade Tool and Split Tool.java.io.FilegetFile()TsFileOutputgetIOWriterOut()this function is only for Test.longgetMaxPlanIndex()longgetMinPlanIndex()longgetPos()get the length of normal OutputStream.booleanisWritingChunkGroup()For TsFileReWriteTool / UpgradeTool.voidmark()voidreset()voidsetFile(java.io.File file)voidsetMaxPlanIndex(long maxPlanIndex)voidsetMinPlanIndex(long minPlanIndex)protected intsortAndFlushChunkMetadata()Sort the chunk metadata by the lexicographical order and the start time of the chunk, then flush them to a temp file.intstartChunkGroup(java.lang.String deviceId)protected voidstartFile()voidstartFlushChunk(java.lang.String measurementId, CompressionType compressionCodecName, TSDataType tsDataType, TSEncoding encodingType, Statistics<? extends java.io.Serializable> statistics, int dataSize, int numOfPages, int mask)start a ChunkMetaData.voidtruncate(long offset)voidwriteBytesToStream(PublicBAOS bytes)Writes given bytes to output stream.voidwriteChunk(Chunk chunk)voidwriteChunk(Chunk chunk, ChunkMetadata chunkMetadata)Write a whole chunk in another file into this file.voidwriteEmptyValueChunk(java.lang.String measurementId, CompressionType compressionType, TSDataType tsDataType, TSEncoding encodingType, Statistics<? extends java.io.Serializable> statistics)Write an empty value chunk into file directly.voidwritePlanIndices()
-
-
-
Field Detail
-
MAGIC_STRING_BYTES
protected static final byte[] MAGIC_STRING_BYTES
-
VERSION_NUMBER_BYTE
public static final byte VERSION_NUMBER_BYTE
-
config
protected static final TSFileConfig config
-
out
protected TsFileOutput out
-
canWrite
protected boolean canWrite
-
file
protected java.io.File file
-
currentChunkMetadata
protected ChunkMetadata currentChunkMetadata
-
chunkMetadataList
protected java.util.List<ChunkMetadata> chunkMetadataList
-
chunkGroupMetadataList
protected java.util.List<ChunkGroupMetadata> chunkGroupMetadataList
-
maxMetadataSize
protected long maxMetadataSize
-
currentChunkMetadataSize
protected long currentChunkMetadataSize
-
chunkMetadataTempFile
protected java.io.File chunkMetadataTempFile
-
tempOutput
protected LocalTsFileOutput tempOutput
-
hasChunkMetadataInDisk
protected volatile boolean hasChunkMetadataInDisk
-
pathCount
protected int pathCount
-
enableMemoryControl
protected boolean enableMemoryControl
-
endPosInCMTForDevice
protected java.util.LinkedList<java.lang.Long> endPosInCMTForDevice
-
CHUNK_METADATA_TEMP_FILE_SUFFIX
public static final java.lang.String CHUNK_METADATA_TEMP_FILE_SUFFIX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TsFileIOWriter
protected TsFileIOWriter()
empty construct function.
-
TsFileIOWriter
public TsFileIOWriter(java.io.File file) throws java.io.IOExceptionfor writing a new tsfile.- Parameters:
file- be used to output written data- Throws:
java.io.IOException- if I/O error occurs
-
TsFileIOWriter
public TsFileIOWriter(TsFileOutput output) throws java.io.IOException
for writing a new tsfile.- Parameters:
output- be used to output written data- Throws:
java.io.IOException
-
TsFileIOWriter
public TsFileIOWriter(TsFileOutput output, boolean test)
for test only
-
TsFileIOWriter
public TsFileIOWriter(java.io.File file, boolean enableMemoryControl, long maxMetadataSize) throws java.io.IOExceptionfor write with memory control- Throws:
java.io.IOException
-
-
Method Detail
-
writeBytesToStream
public void writeBytesToStream(PublicBAOS bytes) throws java.io.IOException
Writes given bytes to output stream. This method is called when total memory size exceeds the chunk group size threshold.- Parameters:
bytes- - data of several pages which has been packed- Throws:
java.io.IOException- if an I/O error occurs.
-
startFile
protected void startFile() throws java.io.IOException- Throws:
java.io.IOException
-
startChunkGroup
public int startChunkGroup(java.lang.String deviceId) throws java.io.IOException- Throws:
java.io.IOException
-
endChunkGroup
public void endChunkGroup() throws java.io.IOExceptionend chunk and write some log. If there is no data in the chunk group, nothing will be flushed.- Throws:
java.io.IOException
-
isWritingChunkGroup
public boolean isWritingChunkGroup()
For TsFileReWriteTool / UpgradeTool. Use this method to determine if needs to start a ChunkGroup.- Returns:
- isWritingChunkGroup
-
startFlushChunk
public void startFlushChunk(java.lang.String measurementId, CompressionType compressionCodecName, TSDataType tsDataType, TSEncoding encodingType, Statistics<? extends java.io.Serializable> statistics, int dataSize, int numOfPages, int mask) throws java.io.IOExceptionstart a ChunkMetaData.- Parameters:
measurementId- - measurementId of this time seriescompressionCodecName- - compression name of this time seriestsDataType- - data typestatistics- - Chunk statisticsdataSize- - the serialized size of all pagesmask- - 0x80 for time chunk, 0x40 for value chunk, 0x00 for common chunk- Throws:
java.io.IOException- if I/O error occurs
-
writeChunk
public void writeChunk(Chunk chunk, ChunkMetadata chunkMetadata) throws java.io.IOException
Write a whole chunk in another file into this file. Providing fast merge for IoTDB.- Throws:
java.io.IOException
-
writeEmptyValueChunk
public void writeEmptyValueChunk(java.lang.String measurementId, CompressionType compressionType, TSDataType tsDataType, TSEncoding encodingType, Statistics<? extends java.io.Serializable> statistics) throws java.io.IOExceptionWrite an empty value chunk into file directly. Only used for aligned timeseries.- Throws:
java.io.IOException
-
writeChunk
public void writeChunk(Chunk chunk) throws java.io.IOException
- Throws:
java.io.IOException
-
endCurrentChunk
public void endCurrentChunk()
end chunk and write some log.
-
endFile
public void endFile() throws java.io.IOExceptionwrite TSFileMetaData to output stream and close it.- Throws:
java.io.IOException- if I/O error occurs
-
getPos
public long getPos() throws java.io.IOExceptionget the length of normal OutputStream.- Returns:
- - length of normal OutputStream
- Throws:
java.io.IOException- if I/O error occurs
-
getDeviceChunkMetadataMap
public java.util.Map<java.lang.String,java.util.List<ChunkMetadata>> getDeviceChunkMetadataMap()
-
canWrite
public boolean canWrite()
-
mark
public void mark() throws java.io.IOException- Throws:
java.io.IOException
-
reset
public void reset() throws java.io.IOException- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionclose the outputStream or file channel without writing FileMetadata. This is just used for Testing.- Specified by:
closein interfacejava.lang.AutoCloseable- Throws:
java.io.IOException
-
getFile
public java.io.File getFile()
-
setFile
public void setFile(java.io.File file)
-
filterChunks
public void filterChunks(java.util.Map<Path,java.util.List<java.lang.Long>> chunkStartTimes)
Remove such ChunkMetadata that its startTime is not in chunkStartTimes
-
writePlanIndices
public void writePlanIndices() throws java.io.IOException- Throws:
java.io.IOException
-
truncate
public void truncate(long offset) throws java.io.IOException- Throws:
java.io.IOException
-
getIOWriterOut
public TsFileOutput getIOWriterOut()
this function is only for Test.- Returns:
- TsFileOutput
-
getChunkMetadataListOfCurrentDeviceInMemory
public java.util.List<ChunkMetadata> getChunkMetadataListOfCurrentDeviceInMemory()
This method should be called before flushing chunk group metadata list, otherwise, it will return null.
-
getDeviceTimeseriesMetadataMap
public java.util.Map<java.lang.String,java.util.List<TimeseriesMetadata>> getDeviceTimeseriesMetadataMap()
this function is for Upgrade Tool and Split Tool.- Returns:
- DeviceTimeseriesMetadataMap
-
getMinPlanIndex
public long getMinPlanIndex()
-
setMinPlanIndex
public void setMinPlanIndex(long minPlanIndex)
-
getMaxPlanIndex
public long getMaxPlanIndex()
-
setMaxPlanIndex
public void setMaxPlanIndex(long maxPlanIndex)
-
checkMetadataSizeAndMayFlush
public int checkMetadataSizeAndMayFlush() throws java.io.IOExceptionCheck if the size of chunk metadata in memory is greater than the given threshold. If so, the chunk metadata will be written to a temp files. Notice! If you are writing a aligned device in row, you should make sure all data of current writing device has been written before this method is called. For writing not aligned series or writing aligned series in column, you should make sure that all data of one series is written before you call this function.- Throws:
java.io.IOException
-
sortAndFlushChunkMetadata
protected int sortAndFlushChunkMetadata() throws java.io.IOExceptionSort the chunk metadata by the lexicographical order and the start time of the chunk, then flush them to a temp file.- Throws:
java.io.IOException
-
getCurrentChunkGroupDeviceId
public java.lang.String getCurrentChunkGroupDeviceId()
-
getChunkGroupMetadataList
public java.util.List<ChunkGroupMetadata> getChunkGroupMetadataList()
-
flush
public void flush() throws java.io.IOException- Throws:
java.io.IOException
-
-