Class TsFileIOWriter
- java.lang.Object
-
- org.apache.iotdb.tsfile.write.writer.TsFileIOWriter
-
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
ForceAppendTsFileWriter,RestorableTsFileIOWriter
public class TsFileIOWriter extends Object implements AutoCloseable
TsFileIOWriter is used to construct metadata and write data stored in memory to output stream.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleancanWritestatic StringCHUNK_METADATA_TEMP_FILE_SUFFIXprotected List<ChunkGroupMetadata>chunkGroupMetadataListprotected List<ChunkMetadata>chunkMetadataListprotected FilechunkMetadataTempFileprotected static TSFileConfigconfigprotected ChunkMetadatacurrentChunkMetadataprotected longcurrentChunkMetadataSizeprotected booleanenableMemoryControlprotected LinkedList<Long>endPosInCMTForDeviceprotected 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(File file)for writing a new tsfile.TsFileIOWriter(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(Map<Path,List<Long>> chunkStartTimes)Remove such ChunkMetadata that its startTime is not in chunkStartTimesvoidflush()List<ChunkGroupMetadata>getChunkGroupMetadataList()List<ChunkMetadata>getChunkMetadataListOfCurrentDeviceInMemory()This method should be called before flushing chunk group metadata list, otherwise, it will return null.StringgetCurrentChunkGroupDeviceId()Map<String,List<ChunkMetadata>>getDeviceChunkMetadataMap()Map<String,List<TimeseriesMetadata>>getDeviceTimeseriesMetadataMap()this function is for Upgrade Tool and Split Tool.FilegetFile()TsFileOutputgetIOWriterOut()this function is only for Test.longgetMaxPlanIndex()longgetMinPlanIndex()longgetPos()get the length of normal OutputStream.TsFileOutputgetTsFileOutput()booleanisWritingChunkGroup()For TsFileReWriteTool / UpgradeTool.voidmark()voidreset()voidsetFile(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(String deviceId)protected voidstartFile()voidstartFlushChunk(String measurementId, CompressionType compressionCodecName, TSDataType tsDataType, TSEncoding encodingType, Statistics<? extends 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(String measurementId, CompressionType compressionType, TSDataType tsDataType, TSEncoding encodingType, Statistics<? extends 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 File file
-
currentChunkMetadata
protected ChunkMetadata currentChunkMetadata
-
chunkMetadataList
protected List<ChunkMetadata> chunkMetadataList
-
chunkGroupMetadataList
protected List<ChunkGroupMetadata> chunkGroupMetadataList
-
maxMetadataSize
protected long maxMetadataSize
-
currentChunkMetadataSize
protected long currentChunkMetadataSize
-
chunkMetadataTempFile
protected File chunkMetadataTempFile
-
tempOutput
protected LocalTsFileOutput tempOutput
-
hasChunkMetadataInDisk
protected volatile boolean hasChunkMetadataInDisk
-
pathCount
protected int pathCount
-
enableMemoryControl
protected boolean enableMemoryControl
-
endPosInCMTForDevice
protected LinkedList<Long> endPosInCMTForDevice
-
CHUNK_METADATA_TEMP_FILE_SUFFIX
public static final String CHUNK_METADATA_TEMP_FILE_SUFFIX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TsFileIOWriter
protected TsFileIOWriter()
empty construct function.
-
TsFileIOWriter
public TsFileIOWriter(File file) throws IOException
for writing a new tsfile.- Parameters:
file- be used to output written data- Throws:
IOException- if I/O error occurs
-
TsFileIOWriter
public TsFileIOWriter(TsFileOutput output) throws IOException
for writing a new tsfile.- Parameters:
output- be used to output written data- Throws:
IOException
-
TsFileIOWriter
public TsFileIOWriter(TsFileOutput output, boolean test)
for test only
-
TsFileIOWriter
public TsFileIOWriter(File file, boolean enableMemoryControl, long maxMetadataSize) throws IOException
for write with memory control- Throws:
IOException
-
-
Method Detail
-
writeBytesToStream
public void writeBytesToStream(PublicBAOS bytes) throws 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:
IOException- if an I/O error occurs.
-
startFile
protected void startFile() throws IOException- Throws:
IOException
-
startChunkGroup
public int startChunkGroup(String deviceId) throws IOException
- Throws:
IOException
-
endChunkGroup
public void endChunkGroup() throws IOExceptionend chunk and write some log. If there is no data in the chunk group, nothing will be flushed.- Throws:
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(String measurementId, CompressionType compressionCodecName, TSDataType tsDataType, TSEncoding encodingType, Statistics<? extends Serializable> statistics, int dataSize, int numOfPages, int mask) throws IOException
start 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:
IOException- if I/O error occurs
-
writeChunk
public void writeChunk(Chunk chunk, ChunkMetadata chunkMetadata) throws IOException
Write a whole chunk in another file into this file. Providing fast merge for IoTDB.- Throws:
IOException
-
writeEmptyValueChunk
public void writeEmptyValueChunk(String measurementId, CompressionType compressionType, TSDataType tsDataType, TSEncoding encodingType, Statistics<? extends Serializable> statistics) throws IOException
Write an empty value chunk into file directly. Only used for aligned timeseries.- Throws:
IOException
-
writeChunk
public void writeChunk(Chunk chunk) throws IOException
- Throws:
IOException
-
endCurrentChunk
public void endCurrentChunk()
end chunk and write some log.
-
endFile
public void endFile() throws IOExceptionwrite TSFileMetaData to output stream and close it.- Throws:
IOException- if I/O error occurs
-
getPos
public long getPos() throws IOExceptionget the length of normal OutputStream.- Returns:
- - length of normal OutputStream
- Throws:
IOException- if I/O error occurs
-
getDeviceChunkMetadataMap
public Map<String,List<ChunkMetadata>> getDeviceChunkMetadataMap()
-
canWrite
public boolean canWrite()
-
mark
public void mark() throws IOException- Throws:
IOException
-
reset
public void reset() throws IOException- Throws:
IOException
-
close
public void close() throws IOExceptionclose the outputStream or file channel without writing FileMetadata. This is just used for Testing.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-
getFile
public File getFile()
-
setFile
public void setFile(File file)
-
filterChunks
public void filterChunks(Map<Path,List<Long>> chunkStartTimes)
Remove such ChunkMetadata that its startTime is not in chunkStartTimes
-
writePlanIndices
public void writePlanIndices() throws IOException- Throws:
IOException
-
truncate
public void truncate(long offset) throws IOException- Throws:
IOException
-
getIOWriterOut
public TsFileOutput getIOWriterOut()
this function is only for Test.- Returns:
- TsFileOutput
-
getChunkMetadataListOfCurrentDeviceInMemory
public List<ChunkMetadata> getChunkMetadataListOfCurrentDeviceInMemory()
This method should be called before flushing chunk group metadata list, otherwise, it will return null.
-
getDeviceTimeseriesMetadataMap
public Map<String,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 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:
IOException
-
sortAndFlushChunkMetadata
protected int sortAndFlushChunkMetadata() throws IOExceptionSort the chunk metadata by the lexicographical order and the start time of the chunk, then flush them to a temp file.- Throws:
IOException
-
getCurrentChunkGroupDeviceId
public String getCurrentChunkGroupDeviceId()
-
getChunkGroupMetadataList
public List<ChunkGroupMetadata> getChunkGroupMetadataList()
-
flush
public void flush() throws IOException- Throws:
IOException
-
getTsFileOutput
public TsFileOutput getTsFileOutput()
-
-