Package org.apache.bookkeeper.bookie
Class BufferedChannel
- java.lang.Object
-
- org.apache.bookkeeper.bookie.BufferedChannelBase
-
- org.apache.bookkeeper.bookie.BufferedReadChannel
-
- org.apache.bookkeeper.bookie.BufferedChannel
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
DefaultEntryLogger.BufferedLogChannel
,SlowBufferedChannel
public class BufferedChannel extends BufferedReadChannel implements java.io.Closeable
Provides a buffering layer in front of a FileChannel.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.bookkeeper.bookie.BufferedChannelBase
BufferedChannelBase.BufferedChannelClosedException
-
-
Field Summary
Fields Modifier and Type Field Description protected long
position
The absolute position of the next write operation.protected java.util.concurrent.atomic.AtomicLong
unpersistedBytes
protected long
unpersistedBytesBound
protected io.netty.buffer.ByteBuf
writeBuffer
The buffer used to write operations.protected java.util.concurrent.atomic.AtomicLong
writeBufferStartPosition
The position of the file channel's write pointer.protected int
writeCapacity
The capacity of the write buffer.-
Fields inherited from class org.apache.bookkeeper.bookie.BufferedReadChannel
cacheHitCount, invocationCount, readBuffer, readBufferStartPosition, readCapacity, sealed
-
Fields inherited from class org.apache.bookkeeper.bookie.BufferedChannelBase
fileChannel
-
-
Constructor Summary
Constructors Constructor Description BufferedChannel(io.netty.buffer.ByteBufAllocator allocator, java.nio.channels.FileChannel fc, int capacity)
BufferedChannel(io.netty.buffer.ByteBufAllocator allocator, java.nio.channels.FileChannel fc, int writeCapacity, int readCapacity, long unpersistedBytesBound)
BufferedChannel(io.netty.buffer.ByteBufAllocator allocator, java.nio.channels.FileChannel fc, int capacity, long unpersistedBytesBound)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
void
close()
void
flush()
Write any data in the buffer to the file and advance the writeBufferPosition.void
flushAndForceWrite(boolean forceMetadata)
calls both flush and forceWrite methods.void
flushAndForceWriteIfRegularFlush(boolean forceMetadata)
calls both flush and forceWrite methods if regular flush is enabled.long
forceWrite(boolean forceMetadata)
force a sync operation so that data is persisted to the disk.long
getFileChannelPosition()
Get the position of the file channel's write pointer.int
getNumOfBytesInWriteBuffer()
(package private) long
getUnpersistedBytes()
long
position()
Get the position where the next write operation will begin writing from.int
read(io.netty.buffer.ByteBuf dest, long pos, int length)
void
write(io.netty.buffer.ByteBuf src)
Write all the data in src to theFileChannel
.-
Methods inherited from class org.apache.bookkeeper.bookie.BufferedReadChannel
read, size
-
Methods inherited from class org.apache.bookkeeper.bookie.BufferedChannelBase
validateAndGetFileChannel
-
-
-
-
Field Detail
-
writeCapacity
protected final int writeCapacity
The capacity of the write buffer.
-
writeBufferStartPosition
protected java.util.concurrent.atomic.AtomicLong writeBufferStartPosition
The position of the file channel's write pointer.
-
writeBuffer
protected final io.netty.buffer.ByteBuf writeBuffer
The buffer used to write operations.
-
position
protected volatile long position
The absolute position of the next write operation.
-
unpersistedBytesBound
protected final long unpersistedBytesBound
-
unpersistedBytes
protected final java.util.concurrent.atomic.AtomicLong unpersistedBytes
-
-
Constructor Detail
-
BufferedChannel
public BufferedChannel(io.netty.buffer.ByteBufAllocator allocator, java.nio.channels.FileChannel fc, int capacity) throws java.io.IOException
- Throws:
java.io.IOException
-
BufferedChannel
public BufferedChannel(io.netty.buffer.ByteBufAllocator allocator, java.nio.channels.FileChannel fc, int capacity, long unpersistedBytesBound) throws java.io.IOException
- Throws:
java.io.IOException
-
BufferedChannel
public BufferedChannel(io.netty.buffer.ByteBufAllocator allocator, java.nio.channels.FileChannel fc, int writeCapacity, int readCapacity, long unpersistedBytesBound) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
write
public void write(io.netty.buffer.ByteBuf src) throws java.io.IOException
Write all the data in src to theFileChannel
. Note that this function can buffer or re-order writes based on the implementation. These writes will be flushed to the disk only when flush() is invoked.- Parameters:
src
- The source ByteBuffer which contains the data to be written.- Throws:
java.io.IOException
- if a write operation fails.
-
position
public long position()
Get the position where the next write operation will begin writing from.- Returns:
-
getFileChannelPosition
public long getFileChannelPosition()
Get the position of the file channel's write pointer.- Returns:
-
flushAndForceWrite
public void flushAndForceWrite(boolean forceMetadata) throws java.io.IOException
calls both flush and forceWrite methods.- Parameters:
forceMetadata
- - If true then this method is required to force changes to both the file's content and metadata to be written to storage; otherwise, it need only force content changes to be written- Throws:
java.io.IOException
-
flushAndForceWriteIfRegularFlush
public void flushAndForceWriteIfRegularFlush(boolean forceMetadata) throws java.io.IOException
calls both flush and forceWrite methods if regular flush is enabled.- Parameters:
forceMetadata
- - If true then this method is required to force changes to both the file's content and metadata to be written to storage; otherwise, it need only force content changes to be written- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
Write any data in the buffer to the file and advance the writeBufferPosition. Callers are expected to synchronize appropriately- Throws:
java.io.IOException
- if the write fails.
-
forceWrite
public long forceWrite(boolean forceMetadata) throws java.io.IOException
force a sync operation so that data is persisted to the disk.- Parameters:
forceMetadata
-- Returns:
- Throws:
java.io.IOException
-
read
public int read(io.netty.buffer.ByteBuf dest, long pos, int length) throws java.io.IOException
- Overrides:
read
in classBufferedReadChannel
- Throws:
java.io.IOException
-
clear
public void clear()
- Overrides:
clear
in classBufferedReadChannel
-
getNumOfBytesInWriteBuffer
public int getNumOfBytesInWriteBuffer()
-
getUnpersistedBytes
long getUnpersistedBytes()
-
-