Package org.apache.flink.runtime.memory
Class AbstractPagedInputView
- java.lang.Object
-
- org.apache.flink.runtime.memory.AbstractPagedInputView
-
- All Implemented Interfaces:
DataInput
,org.apache.flink.core.memory.DataInputView
- Direct Known Subclasses:
AbstractChannelReaderInputView
,FileChannelInputView
,HashPartition
,RandomAccessInputView
,SeekableFileChannelInputView
public abstract class AbstractPagedInputView extends Object implements org.apache.flink.core.memory.DataInputView
The base class for all input views that are backed by multiple memory pages. This base class contains all decoding methods to read data from a page and detect page boundary crossing. The concrete sub classes must implement the methods to provide the next memory page once the boundary is crossed.
-
-
Field Summary
Fields Modifier and Type Field Description protected int
headerLength
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractPagedInputView(int headerLength)
Creates a new view that is initially not bound to a memory segment.protected
AbstractPagedInputView(org.apache.flink.core.memory.MemorySegment initialSegment, int initialLimit, int headerLength)
Creates a new view that starts with the given segment.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
advance()
Advances the view to the next memory segment.protected void
clear()
Clears the internal state of the view.protected void
doAdvance()
int
getCurrentPositionInSegment()
Gets the position from which the next byte will be read.org.apache.flink.core.memory.MemorySegment
getCurrentSegment()
Gets the memory segment that will be used to read the next bytes from.int
getCurrentSegmentLimit()
Gets the current limit in the memory segment.int
getHeaderLength()
protected abstract int
getLimitForSegment(org.apache.flink.core.memory.MemorySegment segment)
Gets the limit for reading bytes from the given memory segment.protected abstract org.apache.flink.core.memory.MemorySegment
nextSegment(org.apache.flink.core.memory.MemorySegment current)
The method by which concrete subclasses realize page crossing.int
read(byte[] b)
int
read(byte[] b, int off, int len)
boolean
readBoolean()
byte
readByte()
char
readChar()
double
readDouble()
float
readFloat()
void
readFully(byte[] b)
void
readFully(byte[] b, int off, int len)
int
readInt()
String
readLine()
long
readLong()
short
readShort()
int
readUnsignedByte()
int
readUnsignedShort()
String
readUTF()
protected void
seekInput(org.apache.flink.core.memory.MemorySegment segment, int positionInSegment, int limitInSegment)
Sets the internal state of the view such that the next bytes will be read from the given memory segment, starting at the given position.int
skipBytes(int n)
void
skipBytesToRead(int numBytes)
-
-
-
Constructor Detail
-
AbstractPagedInputView
protected AbstractPagedInputView(org.apache.flink.core.memory.MemorySegment initialSegment, int initialLimit, int headerLength)
Creates a new view that starts with the given segment. The input starts directly after the header of the given page. If the header size is zero, it starts at the beginning. The specified initial limit describes up to which position data may be read from the current segment, before the view must advance to the next segment.- Parameters:
initialSegment
- The memory segment to start reading from.initialLimit
- The position one after the last valid byte in the initial segment.headerLength
- The number of bytes to skip at the beginning of each segment for the header. This length must be the same for all memory segments.
-
AbstractPagedInputView
protected AbstractPagedInputView(int headerLength)
Creates a new view that is initially not bound to a memory segment. This constructor is typically for views that always seek first.WARNING: The view is not readable until the first call to either
advance()
, or toseekInput(MemorySegment, int, int)
.- Parameters:
headerLength
- The number of bytes to skip at the beginning of each segment for the header.
-
-
Method Detail
-
getCurrentSegment
public org.apache.flink.core.memory.MemorySegment getCurrentSegment()
Gets the memory segment that will be used to read the next bytes from. If the segment is exactly exhausted, meaning that the last byte read was the last byte available in the segment, then this segment will not serve the next bytes. The segment to serve the next bytes will be obtained through thenextSegment(MemorySegment)
method.- Returns:
- The current memory segment.
-
getCurrentPositionInSegment
public int getCurrentPositionInSegment()
Gets the position from which the next byte will be read. If that position is equal to the current limit, then the next byte will be read from next segment.- Returns:
- The position from which the next byte will be read.
- See Also:
getCurrentSegmentLimit()
-
getCurrentSegmentLimit
public int getCurrentSegmentLimit()
Gets the current limit in the memory segment. This value points to the byte one after the last valid byte in the memory segment.- Returns:
- The current limit in the memory segment.
- See Also:
getCurrentPositionInSegment()
-
nextSegment
protected abstract org.apache.flink.core.memory.MemorySegment nextSegment(org.apache.flink.core.memory.MemorySegment current) throws EOFException, IOException
The method by which concrete subclasses realize page crossing. This method is invoked when the current page is exhausted and a new page is required to continue the reading. If no further page is available, this method must throw anEOFException
.- Parameters:
current
- The current page that was read to its limit. May benull
, if this method is invoked for the first time.- Returns:
- The next page from which the reading should continue. May not be
null
. If the input is exhausted, anEOFException
must be thrown instead. - Throws:
EOFException
- Thrown, if no further segment is available.IOException
- Thrown, if the method cannot provide the next page due to an I/O related problem.
-
getLimitForSegment
protected abstract int getLimitForSegment(org.apache.flink.core.memory.MemorySegment segment)
Gets the limit for reading bytes from the given memory segment. This method must return the position of the byte after the last valid byte in the given memory segment. When the position returned by this method is reached, the view will attempt to switch to the next memory segment.- Parameters:
segment
- The segment to determine the limit for.- Returns:
- The limit for the given memory segment.
-
advance
public void advance() throws IOException
Advances the view to the next memory segment. The reading will continue after the header of the next segment. This method usesnextSegment(MemorySegment)
andgetLimitForSegment(MemorySegment)
to get the next segment and set its limit.- Throws:
IOException
- Thrown, if the next segment could not be obtained.- See Also:
nextSegment(MemorySegment)
,getLimitForSegment(MemorySegment)
-
doAdvance
protected void doAdvance() throws IOException
- Throws:
IOException
-
getHeaderLength
public int getHeaderLength()
- Returns:
- header length.
-
seekInput
protected void seekInput(org.apache.flink.core.memory.MemorySegment segment, int positionInSegment, int limitInSegment)
Sets the internal state of the view such that the next bytes will be read from the given memory segment, starting at the given position. The memory segment will provide bytes up to the given limit position.- Parameters:
segment
- The segment to read the next bytes from.positionInSegment
- The position in the segment to start reading from.limitInSegment
- The limit in the segment. When reached, the view will attempt to switch to the next segment.
-
clear
protected void clear()
Clears the internal state of the view. After this call, all read attempts will fail, until theadvance()
orseekInput(MemorySegment, int, int)
method have been invoked.
-
read
public int read(byte[] b) throws IOException
- Specified by:
read
in interfaceorg.apache.flink.core.memory.DataInputView
- Throws:
IOException
-
read
public int read(byte[] b, int off, int len) throws IOException
- Specified by:
read
in interfaceorg.apache.flink.core.memory.DataInputView
- Throws:
IOException
-
readFully
public void readFully(byte[] b) throws IOException
- Specified by:
readFully
in interfaceDataInput
- Throws:
IOException
-
readFully
public void readFully(byte[] b, int off, int len) throws IOException
- Specified by:
readFully
in interfaceDataInput
- Throws:
IOException
-
readBoolean
public boolean readBoolean() throws IOException
- Specified by:
readBoolean
in interfaceDataInput
- Throws:
IOException
-
readByte
public byte readByte() throws IOException
- Specified by:
readByte
in interfaceDataInput
- Throws:
IOException
-
readUnsignedByte
public int readUnsignedByte() throws IOException
- Specified by:
readUnsignedByte
in interfaceDataInput
- Throws:
IOException
-
readShort
public short readShort() throws IOException
- Specified by:
readShort
in interfaceDataInput
- Throws:
IOException
-
readUnsignedShort
public int readUnsignedShort() throws IOException
- Specified by:
readUnsignedShort
in interfaceDataInput
- Throws:
IOException
-
readChar
public char readChar() throws IOException
- Specified by:
readChar
in interfaceDataInput
- Throws:
IOException
-
readInt
public int readInt() throws IOException
- Specified by:
readInt
in interfaceDataInput
- Throws:
IOException
-
readLong
public long readLong() throws IOException
- Specified by:
readLong
in interfaceDataInput
- Throws:
IOException
-
readFloat
public float readFloat() throws IOException
- Specified by:
readFloat
in interfaceDataInput
- Throws:
IOException
-
readDouble
public double readDouble() throws IOException
- Specified by:
readDouble
in interfaceDataInput
- Throws:
IOException
-
readLine
public String readLine() throws IOException
- Specified by:
readLine
in interfaceDataInput
- Throws:
IOException
-
readUTF
public String readUTF() throws IOException
- Specified by:
readUTF
in interfaceDataInput
- Throws:
IOException
-
skipBytes
public int skipBytes(int n) throws IOException
- Specified by:
skipBytes
in interfaceDataInput
- Throws:
IOException
-
skipBytesToRead
public void skipBytesToRead(int numBytes) throws IOException
- Specified by:
skipBytesToRead
in interfaceorg.apache.flink.core.memory.DataInputView
- Throws:
IOException
-
-