Class DBFileReader
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
DBFileWriter
This class provides the basic abilty to read a DBFile as a single
sequential file, obscuring the fact that it is actually broken into pages.
As the file-pointer moves through the file, the Storage Manager is used to
load individual pages into the buffer manager.
It is certainly possible that a value being read might span two adjacent data pages. In these cases, the access will be a little slower, as the operation must access partial data from the first page, and then access the remainder of the data from the next page.
There is a subclass of this reader, the DBFileWriter, which allows
data to be read and written sequentially to a DBFile.
- Design Note:
- This class always has the current
DBPagepinned, and it will unpin the current page when it moves into the next page. This means that when the reader is closed, it may still have a page that is pinned. Therefore, the class implementsAutoCloseableso that users can callclose()on a reader to unpin the last page, or they can use this type with the "try-with-resources" Java syntax.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DBFileThe database file being read by this reader.protected DBPageThe last page used for reading the database file.protected booleanA flag controlling whether the file being read should be extended as it is read.private static org.apache.logging.log4j.LoggerA logging object for reporting anything interesting that happens.protected intThe page-size of the database file being read from.protected intThe current position in the file where reads will occur from.protected StorageManagerA reference to the storage manager, which we will use to read pages needed for the various operations.protected byte[]This temporary buffer is used to read primitive values that overlap the boundaries between two pages. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidvoidclose()If the file-reader currently holds aDBPage, this method will unpin the page.intReturns the current location in the page where the next operation will start from.protected intComputes and returns the page-number of the page that thepositionvalue currently falls within.protected intComputes and returns the offset within the current page that thepositionvalue currently falls at.voidmovePosition(int n) Move the current position by n bytes.voidread(byte[] b) Read a sequence of bytes into the provided byte-array.voidread(byte[] b, int off, int len) Read a sequence of bytes into the provided byte-array, starting with the specified offset, and reading the specified number of bytes.booleanReads and returns a Boolean value from the current position.bytereadByte()Reads and returns a signed byte from the current position.charreadChar()Reads and returns a two-byte char value from the current position.doublefloatintreadInt()Reads and returns a four-byte integer value from the current position.longreadLong()Reads and returns an eight-byte long integer value from the current position.shortReads and returns a signed short from the current position.intReads and returns an unsigned byte from the current position.longReads and returns a four-byte unsigned integer value from the current position.intReads and returns an unsigned short from the current position.This method reads and returns a variable-length string whose maximum length is 255 bytes.This method reads and returns a variable-length string whose maximum length is 65535 bytes.voidsetPosition(int position) Sets the location in the page where the next operation will start from.
-
Field Details
-
logger
private static org.apache.logging.log4j.Logger loggerA logging object for reporting anything interesting that happens. -
storageManager
A reference to the storage manager, which we will use to read pages needed for the various operations. -
dbFile
The database file being read by this reader. -
extendFile
protected boolean extendFileA flag controlling whether the file being read should be extended as it is read. The reader is expected to not extend the file, but theDBFileWriter, a subclass of this class, sets this flag to true. -
pageSize
protected int pageSizeThe page-size of the database file being read from. -
dbPage
The last page used for reading the database file. -
position
protected int positionThe current position in the file where reads will occur from. -
tmpBuf
protected byte[] tmpBufThis temporary buffer is used to read primitive values that overlap the boundaries between two pages.
-
-
Constructor Details
-
DBFileReader
-
-
Method Details
-
close
public void close()If the file-reader currently holds aDBPage, this method will unpin the page. ThedbPagevalue is also set tonullso that if someone tries to use the reader later, the page will be re-loaded (and therefore also re-pinned).- Specified by:
closein interfaceAutoCloseable
-
getDBFile
-
getPosition
public int getPosition()Returns the current location in the page where the next operation will start from.- Returns:
- the current location in the page
-
setPosition
public void setPosition(int position) Sets the location in the page where the next operation will start from.- Parameters:
position- the new location in the page
-
movePosition
public void movePosition(int n) Move the current position by n bytes. A negative value of n will move the position backward.- Parameters:
n- the delta to apply to the current position
-
getPositionPageNo
protected int getPositionPageNo()Computes and returns the page-number of the page that thepositionvalue currently falls within.- Returns:
- the page-number of the page that the
positionvalue currently falls within
-
getPositionPageOffset
protected int getPositionPageOffset()Computes and returns the offset within the current page that thepositionvalue currently falls at.- Returns:
- the offset within the current page that the
positionvalue currently falls at
-
checkDBPage
protected void checkDBPage() -
read
public void read(byte[] b, int off, int len) Read a sequence of bytes into the provided byte-array, starting with the specified offset, and reading the specified number of bytes.- Parameters:
b- the byte-array to read bytes intooff- the offset to read the bytes into the arraylen- the number of bytes to read into the array
-
read
public void read(byte[] b) Read a sequence of bytes into the provided byte-array. The entire array is filled from start to end.- Parameters:
b- the byte-array to read bytes into
-
readBoolean
public boolean readBoolean()Reads and returns a Boolean value from the current position. A zero value is interpreted as false, and a nonzero value is interpreted as true. -
readByte
public byte readByte()Reads and returns a signed byte from the current position. -
readUnsignedByte
public int readUnsignedByte()Reads and returns an unsigned byte from the current position. The value is returned as an int whose value will be between 0 and 255, inclusive. -
readUnsignedShort
public int readUnsignedShort()Reads and returns an unsigned short from the current position. The value is returned as an int whose value will be between 0 and 65535, inclusive. -
readShort
public short readShort()Reads and returns a signed short from the current position. -
readChar
public char readChar()Reads and returns a two-byte char value from the current position. -
readUnsignedInt
public long readUnsignedInt()Reads and returns a four-byte unsigned integer value from the current position. -
readInt
public int readInt()Reads and returns a four-byte integer value from the current position. -
readLong
public long readLong()Reads and returns an eight-byte long integer value from the current position. -
readFloat
public float readFloat() -
readDouble
public double readDouble() -
readVarString255
This method reads and returns a variable-length string whose maximum length is 255 bytes. The string is expected to be in US-ASCII encoding, so multibyte characters are not supported.The string's data format is expected to be a single unsigned byte b specifying the string's length, followed by b more bytes consisting of the string value itself.
-
readVarString65535
This method reads and returns a variable-length string whose maximum length is 65535 bytes. The string is expected to be in US-ASCII encoding, so multibyte characters are not supported.The string's data format is expected to be a single unsigned short (two bytes) s specifying the string's length, followed by s more bytes consisting of the string value itself.
-