public final class FixedSizeDataStore extends Object
0 4 (int) : MAGIC - used to identify file type. 4 4 (int) : version - version of the file store. 8 4 (int) : sector_size - the size of each sector in the store. 12 8 (long) : delete_head - the head sector of the delete list. 20 8 (long) : sectors_used - number of sectors being used (not deleted). 28 1 (byte) : open - set to 1 when file opened, 0 when closed. 29 4 (int) : sector_start - offset where sector information starts. 33 ... 63 - reserved 64 ... 191 - reserved buffer for misc. state data. 192 ... sector_start - reserved
Each sector contains a 5 byte header. This header includes a byte that contains either USED or DELETED, and a int pointer to the next chained sector. The int pointer is used to either represent a pointer to the next sector in the chain of USED sectors, with -1 indicating the end. Or, if the sector is DELETED, it points to the next deleted sector in the chain.
Constructor and Description |
---|
FixedSizeDataStore(File data_file,
int sector_size,
boolean cache_access,
DebugLogger logger)
Constructs the data store.
|
FixedSizeDataStore(File data_file,
int sector_size,
DebugLogger logger) |
Modifier and Type | Method and Description |
---|---|
int |
addSector(byte[] buf)
Adds a new sector into the store.
|
int |
addSector(byte[] buf,
int offset,
int length)
Adds a new sector into the store.
|
int |
calculateSectorSpan(int length)
Calculates the number of sectors the given length of bytes will span.
|
boolean |
clearDeletedSectors()
Cleans up so all deleted sectors are completely removed from the store.
|
void |
close()
Closes the data store.
|
void |
copyTo(File path)
Copies the entire contents of this store to a destination directory.
|
void |
delete()
Deletes the data store from the file system.
|
void |
deleteAcross(int sector_head)
Deletes a set of sectors that have been chained together.
|
void |
deleteAllSectors()
Deletes all sectors in the entire store.
|
void |
deleteSector(int sector)
Deletes a sector from the store.
|
boolean |
exists()
Returns true if the file for this store exists.
|
void |
fix(UserTerminal terminal)
Attempts to repair this data store to a correct state.
|
int |
getLengthOfLastOutputStream()
Returns the number of bytes that were written out by the last closed
output stream returned by 'getSectorOutputStream'.
|
byte[] |
getSector(int sector)
Gets the contents of the sector at the given index.
|
byte[] |
getSector(int sector,
byte[] buf)
Gets the contents of the sector at the given index.
|
byte[] |
getSector(int sector,
byte[] buf,
int offset,
int length)
Gets the contents of the sector at the given index.
|
int[] |
getSectorAsIntArray(int sector,
int[] buf)
Gets the contents of the sector at the given index as an int[] array.
|
int[] |
getSectorChain(int sector_head)
Traverses a sector chain and returns an array of all sectors that are
part of the chain.
|
int[] |
getSectorChain(int sector_head,
int length)
Traverses a sector chain and returns an array of all sectors that are
part of the chain.
|
InputStream |
getSectorInputStream(int sector_head)
Returns an InputStream implementation that is used to read a stream of
information from the store.
|
int |
getSectorOfLastOutputStream()
Returns the first sector the OutputStream returned by
'getSectorOutputStream' wrote to.
|
OutputStream |
getSectorOutputStream()
Returns an OutputStream implementation that is used to write a stream
of information into this data store.
|
int |
getSectorSize()
Returns the number of bytes that the user may store in a sector.
|
int |
getSectorUseCount()
Returns the number of sectors in the store that are being used (as
opposed to being deleted).
|
void |
hardSynch()
Performs a hard synchronization of this store.
|
boolean |
isClosed()
Returns true if the store is closed.
|
boolean |
isReadOnly()
Returns true if the store has been opened in read only mode.
|
boolean |
isSectorDeleted(int sector)
Returns true if the sector number is flagged as deleted.
|
void |
lock()
Locks the store by some process so that we may not reclaim deleted
sectors.
|
boolean |
open(boolean read_only)
Opens the data store.
|
int |
overwriteSector(int sector,
byte[] buf)
Writes the contents of a sector into the store overwritting any
other information that may be stored there.
|
int |
overwriteSector(int sector,
byte[] buf,
int offset,
int length)
Writes the contents of a sector into the store overwritting any
other information that may be stored there.
|
int |
rawSectorCount()
Returns the total number of sectors that are currently available
(includes used and deleted sectors).
|
int |
readAcross(int sector_head,
byte[] buf,
int offset,
int length)
Reads information across a chain of sectors and fills the byte[] array
buffer.
|
void |
readReservedBuffer(byte[] info,
int offset,
int length)
Reads from the buffer reserve into the given byte array.
|
void |
repair()
Repairs the consistancy of the store.
|
String |
statusString()
Returns a string that contains diagnostic information.
|
void |
synch()
Synchronizes the memory store with the file header.
|
long |
totalSize()
Returns the size of the data store file.
|
void |
unlock()
Unlocks the store.
|
void |
wipeLastOutputStream()
Wipes the SectorOutputStream from this object.
|
int |
writeAcross(byte[] buf,
int offset,
int length)
Writes a byte[] array of data across as many sectors as it takes to store
the data.
|
void |
writeReservedBuffer(byte[] info,
int offset,
int length) |
void |
writeReservedBuffer(byte[] info,
int offset,
int length,
int res_offset)
Every data store has a 128 byte buffer that can be used to store state
information.
|
public FixedSizeDataStore(File data_file, int sector_size, boolean cache_access, DebugLogger logger)
public FixedSizeDataStore(File data_file, int sector_size, DebugLogger logger)
public long totalSize()
public void writeReservedBuffer(byte[] info, int offset, int length, int res_offset) throws IOException
IOException
public void writeReservedBuffer(byte[] info, int offset, int length) throws IOException
IOException
public void readReservedBuffer(byte[] info, int offset, int length) throws IOException
IOException
public void synch() throws IOException
IOException
public void hardSynch() throws IOException
IOException
public boolean isReadOnly()
public boolean open(boolean read_only) throws IOException
It is not possible to open a damaged store in read only mode.
read_only
- if true, then the database is opened in read only mode,
otherwise it is opened in read/write mode.IOException
public void close() throws IOException
IOException
public boolean isClosed()
public void delete()
public boolean exists() throws IOException
IOException
public int getSectorSize()
public int getSectorUseCount()
public int rawSectorCount() throws IOException
IOException
public void lock()
public void unlock()
public boolean isSectorDeleted(int sector) throws IOException
IOException
public byte[] getSector(int sector, byte[] buf, int offset, int length) throws IOException
IOException
public byte[] getSector(int sector, byte[] buf) throws IOException
IOException
public byte[] getSector(int sector) throws IOException
IOException
public int[] getSectorAsIntArray(int sector, int[] buf) throws IOException
IOException
public int readAcross(int sector_head, byte[] buf, int offset, int length) throws IOException
IOException
public int[] getSectorChain(int sector_head, int length) throws IOException
IOException
public int[] getSectorChain(int sector_head) throws IOException
IOException
public void deleteSector(int sector) throws IOException
Throws an IO error if the sector is marked as deleted.
IOException
public void deleteAcross(int sector_head) throws IOException
IOException
public void deleteAllSectors() throws IOException
IOException
public int overwriteSector(int sector, byte[] buf, int offset, int length) throws IOException
IOException
public int overwriteSector(int sector, byte[] buf) throws IOException
IOException
public int addSector(byte[] buf, int offset, int length) throws IOException
IOException
public int addSector(byte[] buf) throws IOException
IOException
public int calculateSectorSpan(int length)
public int writeAcross(byte[] buf, int offset, int length) throws IOException
IOException
public OutputStream getSectorOutputStream() throws IOException
NOTE: While an output stream returned by this method is not closed, it is unsafe to use any methods in the FixedSizeDataStore object.
IOException
public int getSectorOfLastOutputStream()
public int getLengthOfLastOutputStream()
public void wipeLastOutputStream()
public InputStream getSectorInputStream(int sector_head) throws IOException
NOTE: Using this InputStream, an end of stream identifier is never produced. When the last sector in the chain is reached, the input stream will first read padding whitespace, then it will either loop to the start of the last sector, or move to another undefined sector. You must not rely on this stream reaching an EOF.
IOException
public void copyTo(File path) throws IOException
The purpose of this method is so we can make a copy of the data in this store while the store is open and 'live'.
We assume synchronization on this object.
path
- the directory to copy this file to.IOException
public void fix(UserTerminal terminal) throws IOException
The store must have been opened before this method is called.
IOException
public boolean clearDeletedSectors() throws IOException
It is extremely important that nothing can be read/written from the file while this is happening. And certainly, we can not have any locks on this store.
Returns true if the layout of the sectors changed (so we can fix indices that point to sectors).
IOException
public void repair() throws IOException
Repair assumes we can at least get past the 'open' method. This method does not change the order of the sectors in the store. However it may change the order in which deleted sectors are reclaimed.
In a perfect world, this should never need to be called. However, it's a good idea to call this every so often because we are assured that the delete linked list and 'used_sector_count' variables will be correct when the method returns.
It is not possible to repair a store that's been opened in read only mode.
IOException
public String statusString() throws IOException
IOException
Copyright © 2015. All rights reserved.