public class VariableSizeDataStore extends Object
This file format is not intended to be a fully fledged file system. For example, we can not easily change the size of a data entry. To change the record, we must delete it then add a new.
This system uses two files. One file is the index, and the second file stores the data. The index file contains sectors as follows:
4 (int) : index - The sector index of the data in the data file. 4 (int) : length - Length of the data that was stored. 4 (int) : status - 32 status bits.
This employs a simple compression scheme when writing out data that would span more than one sector. It tries compressing the field. If the field can be compressed into less sectors than if left uncompressed, then the compressed field is put into the data store.
Constructor and Description |
---|
VariableSizeDataStore(File name,
DebugLogger logger) |
VariableSizeDataStore(File name,
int sector_size,
DebugLogger logger)
Constructs the variable size store.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the data store.
|
int |
completeRecordStreamWrite()
Updates the record allocation table with the data in the output stream
returned by 'getRecordOutputStream'.
|
void |
copyTo(File path)
Copies this data store to the given path.
|
void |
delete()
Deletes the store from the file system.
|
static boolean |
delete(File path,
String name)
Convenience for deleting a VariableSizeDataStore store.
|
int |
delete(int record)
Deletes the data at the given index position.
|
boolean |
exists()
Returns true if the data store exists.
|
static boolean |
exists(File path,
String name)
Convenience for checking if a given data store exists or not.
|
void |
fix(UserTerminal terminal)
Attempts to fix a corrupt VariableSizeDataStore object.
|
InputStream |
getRecordInputStream(int record)
Returns an InputStream that is used to read a record in this store with
the given index.
|
OutputStream |
getRecordOutputStream()
Returns an OutputStream object that can be used to write data into the
store.
|
void |
hardSynch()
Hard synchronizes the data from memory into the hard copy on disk.
|
boolean |
isCompressed(int record)
Returns true if the given record is compressed.
|
boolean |
isReadOnly()
Returns true if the store was openned in read only mode.
|
void |
lock()
Locks the store so that not deleted elements may be overwritten.
|
boolean |
locked()
Returns true if we are locked.
|
boolean |
open(boolean read_only)
Opens the data store.
|
int |
rawRecordCount()
Returns the total number of records that are in the store (including
deleted records.
|
int |
read(int record,
byte[] buf,
int offset,
int length)
Reads a variable length byte[] array from the given index position.
|
byte[] |
readRecord(int record)
Reads in a complete record and puts it into the returned byte[] array.
|
int |
readRecordType(int record_index)
Reads the 32-bit type_key int for the given record.
|
void |
readReservedBuffer(byte[] info,
int offset,
int length)
Reads reserved information from the variable data store.
|
String |
readString(int record) |
boolean |
recordDeleted(int record_index)
Returns true if the given record is marked as deleted or not.
|
int |
recordSectorCount(int record)
Returns the number of sectors the given record takes up in the data
store.
|
int |
recordSize(int record)
Returns the size of the given record number (compressed size if
applicable).
|
static boolean |
rename(File path_source,
String name_source,
File path_dest,
String name_dest)
Convenience for renaming a VariableSizeDataStore store to another name.
|
int |
sectorSize()
Returns the size (in bytes) of the sectors used to store information in
the data file.
|
void |
synch()
Synchronizes all the data in memory with the hard copy on disk.
|
long |
totalStoreSize()
Returns the size of the data file that keeps all the data in this
store.
|
void |
unlock()
Unlocks the store so that deleted elements can be reclaimed again.
|
int |
usedRecordCount()
Returns the number of records that are being used.
|
int |
write(byte[] buf,
int offset,
int length)
Writes a variable length byte[] array to the first available index.
|
int |
writeRecordType(int record_index,
int type_key)
Updates the 32-bit type_key int of a record.
|
void |
writeReservedBuffer(byte[] info,
int offset,
int length) |
void |
writeReservedBuffer(byte[] info,
int offset,
int length,
int res_offset)
Writes reserved information to the variable data store.
|
int |
writeString(String str) |
public VariableSizeDataStore(File name, int sector_size, DebugLogger logger)
public VariableSizeDataStore(File name, DebugLogger logger)
public void synch() throws IOException
IOException
public void hardSynch() throws IOException
IOException
public boolean locked()
public void lock()
public void unlock()
public boolean recordDeleted(int record_index) throws IOException
IOException
public int usedRecordCount()
public int rawRecordCount() throws IOException
IOException
public boolean exists() 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 void delete()
public void fix(UserTerminal terminal) throws IOException
The store should be open before this method is called.
IOException
public void copyTo(File path) throws IOException
IOException
public int writeRecordType(int record_index, int type_key) throws IOException
IOException
public int readRecordType(int record_index) throws IOException
IOException
public int write(byte[] buf, int offset, int length) throws IOException
IOException
public int read(int record, byte[] buf, int offset, int length) throws IOException
IOException
public byte[] readRecord(int record) throws IOException
IOException
public int delete(int record) throws IOException
IOException
public OutputStream getRecordOutputStream() throws IOException
NOTE: Only one open stream may be active at a time. While this stream is open this VariableSizeDataStore object may not be used in any other way.
IOException
public int completeRecordStreamWrite() throws IOException
After this method is called it is safe again to use this VariableSizeDataStore object.
IOException
public InputStream getRecordInputStream(int record) throws IOException
NOTE: This can not handle compressed records.
NOTE: This does not detect the end of stream (reading past the end of the record will return undefined data).
IOException
public int sectorSize() throws IOException
IOException
public int recordSize(int record) throws IOException
IOException
public boolean isCompressed(int record) throws IOException
IOException
public int recordSectorCount(int record) throws IOException
IOException
public long totalStoreSize()
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 static boolean exists(File path, String name) throws IOException
IOException
public static boolean delete(File path, String name) throws IOException
IOException
public static boolean rename(File path_source, String name_source, File path_dest, String name_dest) throws IOException
IOException
public int writeString(String str) throws IOException
IOException
public String readString(int record) throws IOException
IOException
Copyright © 2015. All rights reserved.