Package org.apache.cassandra.io.util
Class SequentialWriter
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.cassandra.io.util.DataOutputStreamPlus
-
- org.apache.cassandra.io.util.BufferedDataOutputStreamPlus
-
- org.apache.cassandra.io.util.SequentialWriter
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.DataOutput
,java.io.Flushable
,java.lang.AutoCloseable
,DataOutputPlus
,Transactional
- Direct Known Subclasses:
ChecksummedSequentialWriter
,CompressedSequentialWriter
public class SequentialWriter extends BufferedDataOutputStreamPlus implements Transactional
Adds buffering, mark, and fsyncing to OutputStream. We always fsync on close; we may also fsync incrementally if Config.trickle_fsync is enabled.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
SequentialWriter.BufferedFileWriterMark
Class to hold a mark to the position of the fileprotected class
SequentialWriter.TransactionalProxy
-
Nested classes/interfaces inherited from interface org.apache.cassandra.utils.concurrent.Transactional
Transactional.AbstractTransactional
-
-
Field Summary
Fields Modifier and Type Field Description protected long
bufferOffset
protected java.nio.channels.FileChannel
fchannel
protected long
lastFlushOffset
protected java.util.function.LongConsumer
runPostFlush
-
Fields inherited from class org.apache.cassandra.io.util.BufferedDataOutputStreamPlus
buffer
-
Fields inherited from class org.apache.cassandra.io.util.DataOutputStreamPlus
channel
-
-
Constructor Summary
Constructors Constructor Description SequentialWriter(File file)
Create heap-based, non-compressed SequenialWriter with default buffer size(64k).SequentialWriter(File file, SequentialWriterOption option)
Create SequentialWriter for given file with specific writer option.SequentialWriter(File file, SequentialWriterOption option, boolean strictFlushing)
Create SequentialWriter for given file with specific writer option.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Throwable
abort(java.lang.Throwable accumulate)
int
bytesLeftInPage()
Returns how many bytes are left in the page.void
close()
java.lang.Throwable
commit(java.lang.Throwable accumulate)
protected long
current()
protected void
doFlush(int count)
void
finish()
protected void
flushData()
Override this method instead of overriding flush()long
getEstimatedOnDiskBytesWritten()
File
getFile()
long
getLastFlushOffset()
long
getOnDiskFilePointer()
Returns the current file pointer of the underlying on-disk file.java.lang.String
getPath()
boolean
hasPosition()
If the implementation supports providing a position, this method returnstrue
, otherwisefalse
.boolean
isOpen()
long
length()
DataPosition
mark()
int
maxBytesInPage()
Returns the number of bytes that a page can take at maximum.long
paddedPosition()
Returns the next padded position.void
padToPageBoundary()
Pad this with zeroes until the next page boundary.long
position()
Returns the current position of the underlying target like a file-pointer or the position withing a buffer.void
prepareToCommit()
void
resetAndTruncate(DataPosition mark)
Drops all buffered data that's past the limits of our new file mark + buffer capacity, or syncs and truncates the underlying file to the marked positionprotected void
resetBuffer()
void
setPostFlushListener(java.util.function.LongConsumer runPostFlush)
void
skipBytes(long numBytes)
void
sync()
Synchronize file contents with disk.protected void
syncDataOnlyInternal()
protected void
syncInternal()
void
truncate(long toSize)
protected SequentialWriter.TransactionalProxy
txnProxy()
int
writeDirectlyToChannel(java.nio.ByteBuffer buf)
-
Methods inherited from class org.apache.cassandra.io.util.BufferedDataOutputStreamPlus
allocate, flush, order, write, write, write, write, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeMostSignificantBytes, writeShort, writeUTF
-
Methods inherited from class org.apache.cassandra.io.util.DataOutputStreamPlus
newDefaultChannel, retrieveTemporaryBuffer
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.cassandra.io.util.DataOutputPlus
write, writeUnsignedVInt, writeUnsignedVInt, writeUnsignedVInt32, writeVInt, writeVInt, writeVInt32
-
-
-
-
Constructor Detail
-
SequentialWriter
public SequentialWriter(File file)
Create heap-based, non-compressed SequenialWriter with default buffer size(64k).- Parameters:
file
- File to write
-
SequentialWriter
public SequentialWriter(File file, SequentialWriterOption option)
Create SequentialWriter for given file with specific writer option.- Parameters:
file
- File to writeoption
- Writer option
-
SequentialWriter
public SequentialWriter(File file, SequentialWriterOption option, boolean strictFlushing)
Create SequentialWriter for given file with specific writer option.- Parameters:
file
-option
-strictFlushing
-
-
-
Method Detail
-
skipBytes
public void skipBytes(long numBytes) throws java.io.IOException
- Throws:
java.io.IOException
-
sync
public void sync()
Synchronize file contents with disk.
-
syncDataOnlyInternal
protected void syncDataOnlyInternal()
-
syncInternal
protected void syncInternal()
-
doFlush
protected void doFlush(int count)
- Overrides:
doFlush
in classBufferedDataOutputStreamPlus
-
setPostFlushListener
public void setPostFlushListener(java.util.function.LongConsumer runPostFlush)
-
flushData
protected void flushData()
Override this method instead of overriding flush()- Throws:
FSWriteError
- on any I/O error.
-
hasPosition
public boolean hasPosition()
Description copied from interface:DataOutputPlus
If the implementation supports providing a position, this method returnstrue
, otherwisefalse
.- Specified by:
hasPosition
in interfaceDataOutputPlus
-
position
public long position()
Description copied from interface:DataOutputPlus
Returns the current position of the underlying target like a file-pointer or the position withing a buffer. Not every implementation may support this functionality. Whether or not this functionality is supported can be checked via theDataOutputPlus.hasPosition()
.- Specified by:
position
in interfaceDataOutputPlus
-
maxBytesInPage
public int maxBytesInPage()
Description copied from interface:DataOutputPlus
Returns the number of bytes that a page can take at maximum.- Specified by:
maxBytesInPage
in interfaceDataOutputPlus
-
padToPageBoundary
public void padToPageBoundary() throws java.io.IOException
Description copied from interface:DataOutputPlus
Pad this with zeroes until the next page boundary. If the destination position is already at a page boundary, do not do anything.- Specified by:
padToPageBoundary
in interfaceDataOutputPlus
- Throws:
java.io.IOException
-
bytesLeftInPage
public int bytesLeftInPage()
Description copied from interface:DataOutputPlus
Returns how many bytes are left in the page.- Specified by:
bytesLeftInPage
in interfaceDataOutputPlus
-
paddedPosition
public long paddedPosition()
Description copied from interface:DataOutputPlus
Returns the next padded position. This is either the current position (if already padded), or the start of next page.- Specified by:
paddedPosition
in interfaceDataOutputPlus
-
getOnDiskFilePointer
public long getOnDiskFilePointer()
Returns the current file pointer of the underlying on-disk file. Note that since write works by buffering data, the value of this will increase by buffer size and not every write to the writer will modify this value. Furthermore, for compressed files, this value refers to compressed data, while the writer getFilePointer() refers to uncompressedFile- Returns:
- the current file pointer
-
getEstimatedOnDiskBytesWritten
public long getEstimatedOnDiskBytesWritten()
-
length
public long length()
-
getPath
public java.lang.String getPath()
-
getFile
public File getFile()
-
resetBuffer
protected void resetBuffer()
-
current
protected long current()
-
mark
public DataPosition mark()
-
resetAndTruncate
public void resetAndTruncate(DataPosition mark)
Drops all buffered data that's past the limits of our new file mark + buffer capacity, or syncs and truncates the underlying file to the marked position
-
getLastFlushOffset
public long getLastFlushOffset()
-
truncate
public void truncate(long toSize)
-
isOpen
public boolean isOpen()
-
prepareToCommit
public final void prepareToCommit()
- Specified by:
prepareToCommit
in interfaceTransactional
-
commit
public final java.lang.Throwable commit(java.lang.Throwable accumulate)
- Specified by:
commit
in interfaceTransactional
-
abort
public final java.lang.Throwable abort(java.lang.Throwable accumulate)
- Specified by:
abort
in interfaceTransactional
-
close
public final void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceTransactional
- Overrides:
close
in classBufferedDataOutputStreamPlus
-
writeDirectlyToChannel
public int writeDirectlyToChannel(java.nio.ByteBuffer buf) throws java.io.IOException
- Throws:
java.io.IOException
-
finish
public final void finish()
-
txnProxy
protected SequentialWriter.TransactionalProxy txnProxy()
-
-