jhplot.io
Class BaseRecordsFile

java.lang.Object
  extended by jhplot.io.BaseRecordsFile
Direct Known Subclasses:
RecordsFile

public abstract class BaseRecordsFile
extends Object


Field Summary
protected static long DATA_START_HEADER_LOCATION
           
protected  long dataStartPtr
           
protected static int FILE_HEADERS_REGION_LENGTH
           
protected static int INDEX_ENTRY_LENGTH
           
protected static int MAX_KEY_LENGTH
           
protected static long NUM_RECORDS_HEADER_LOCATION
           
protected static int RECORD_HEADER_LENGTH
           
 
Constructor Summary
protected BaseRecordsFile(String dbPath, int initialSize)
          Creates a new database file, initializing the appropriate headers.
protected BaseRecordsFile(String dbPath, String accessFlags)
          Opens an existing database file and initializes the dataStartPtr.
 
Method Summary
protected  void addEntryToIndex(String key, RecordHeader newRecord, int currentNumRecords)
          Appends an entry to end of index.
protected abstract  RecordHeader allocateRecord(String key, int dataLength)
          Locates space for a new record of dataLength size and initializes a RecordHeader.
 void close()
          Closes the file.
protected  void deleteEntryFromIndex(String key, RecordHeader header, int currentNumRecords)
          Removes the record from the index.
 void deleteRecord(String key)
          Deletes a record.
abstract  Enumeration enumerateKeys()
          Returns an Enumeration of the keys of all records in the database.
protected  long getFileLength()
           
abstract  int getNumRecords()
          Returns the number or records in the database.
protected abstract  RecordHeader getRecordAt(long targetFp)
          Returns the record to which the target file pointer belongs - meaning the specified location in the file is part of the record data of the RecordHeader which is returned.
protected  long indexPositionToKeyFp(int pos)
          Returns a file pointer in the index pointing to the first byte in the key located at the given index position.
 void insertRecord(RecordWriter rw)
          Adds the given record to the database.
protected  void insureIndexSpace(int requiredNumRecords)
           
protected abstract  RecordHeader keyToRecordHeader(String key)
          Maps a key to a record header.
protected  long readDataStartHeader()
          Reads the data start pointer header from the file.
protected  int readNumRecordsHeader()
          Reads the number of records header from the file.
 RecordReader readRecord(String key)
          Reads a record.
protected  byte[] readRecordData(RecordHeader header)
          Reads the record data for the given record header.
protected  byte[] readRecordData(String key)
          Reads the data for the record with the given key.
abstract  boolean recordExists(String key)
          Checks there is a record with the given key.
protected  void setFileLength(long l)
           
 void updateRecord(RecordWriter rw)
          Updates an existing record.
protected  void writeDataStartPtrHeader(long dataStartPtr)
          Writes the data start pointer header to the file.
protected  void writeNumRecordsHeader(int numRecords)
          Writes the number of records header to the file.
protected  void writeRecordData(RecordHeader header, byte[] data)
          Updates the contents of the given record.
protected  void writeRecordData(RecordHeader header, RecordWriter rw)
          Updates the contents of the given record.
protected  void writeRecordHeaderToIndex(RecordHeader header)
          Writes the ith record header to the index.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dataStartPtr

protected long dataStartPtr

FILE_HEADERS_REGION_LENGTH

protected static final int FILE_HEADERS_REGION_LENGTH
See Also:
Constant Field Values

RECORD_HEADER_LENGTH

protected static final int RECORD_HEADER_LENGTH
See Also:
Constant Field Values

MAX_KEY_LENGTH

protected static final int MAX_KEY_LENGTH
See Also:
Constant Field Values

INDEX_ENTRY_LENGTH

protected static final int INDEX_ENTRY_LENGTH
See Also:
Constant Field Values

NUM_RECORDS_HEADER_LOCATION

protected static final long NUM_RECORDS_HEADER_LOCATION
See Also:
Constant Field Values

DATA_START_HEADER_LOCATION

protected static final long DATA_START_HEADER_LOCATION
See Also:
Constant Field Values
Constructor Detail

BaseRecordsFile

protected BaseRecordsFile(String dbPath,
                          int initialSize)
                   throws IOException,
                          RecordsFileException
Creates a new database file, initializing the appropriate headers. Enough space is allocated in the index for the specified initial size.

Throws:
IOException
RecordsFileException

BaseRecordsFile

protected BaseRecordsFile(String dbPath,
                          String accessFlags)
                   throws IOException,
                          RecordsFileException
Opens an existing database file and initializes the dataStartPtr. The accessFlags parameter can be "r" or "rw" -- as defined in RandomAccessFile.

Throws:
IOException
RecordsFileException
Method Detail

enumerateKeys

public abstract Enumeration enumerateKeys()
Returns an Enumeration of the keys of all records in the database.


getNumRecords

public abstract int getNumRecords()
Returns the number or records in the database.


recordExists

public abstract boolean recordExists(String key)
Checks there is a record with the given key.


keyToRecordHeader

protected abstract RecordHeader keyToRecordHeader(String key)
                                           throws RecordsFileException
Maps a key to a record header.

Throws:
RecordsFileException

allocateRecord

protected abstract RecordHeader allocateRecord(String key,
                                               int dataLength)
                                        throws RecordsFileException,
                                               IOException
Locates space for a new record of dataLength size and initializes a RecordHeader.

Throws:
RecordsFileException
IOException

getRecordAt

protected abstract RecordHeader getRecordAt(long targetFp)
                                     throws RecordsFileException
Returns the record to which the target file pointer belongs - meaning the specified location in the file is part of the record data of the RecordHeader which is returned. Returns null if the location is not part of a record. (O(n) mem accesses)

Throws:
RecordsFileException

getFileLength

protected long getFileLength()
                      throws IOException
Throws:
IOException

setFileLength

protected void setFileLength(long l)
                      throws IOException
Throws:
IOException

readNumRecordsHeader

protected int readNumRecordsHeader()
                            throws IOException
Reads the number of records header from the file.

Throws:
IOException

writeNumRecordsHeader

protected void writeNumRecordsHeader(int numRecords)
                              throws IOException
Writes the number of records header to the file.

Throws:
IOException

readDataStartHeader

protected long readDataStartHeader()
                            throws IOException
Reads the data start pointer header from the file.

Throws:
IOException

writeDataStartPtrHeader

protected void writeDataStartPtrHeader(long dataStartPtr)
                                throws IOException
Writes the data start pointer header to the file.

Throws:
IOException

indexPositionToKeyFp

protected long indexPositionToKeyFp(int pos)
Returns a file pointer in the index pointing to the first byte in the key located at the given index position.


writeRecordHeaderToIndex

protected void writeRecordHeaderToIndex(RecordHeader header)
                                 throws IOException
Writes the ith record header to the index.

Throws:
IOException

addEntryToIndex

protected void addEntryToIndex(String key,
                               RecordHeader newRecord,
                               int currentNumRecords)
                        throws IOException,
                               RecordsFileException
Appends an entry to end of index. Assumes that insureIndexSpace() has already been called.

Throws:
IOException
RecordsFileException

deleteEntryFromIndex

protected void deleteEntryFromIndex(String key,
                                    RecordHeader header,
                                    int currentNumRecords)
                             throws IOException,
                                    RecordsFileException
Removes the record from the index. Replaces the target with the entry at the end of the index.

Throws:
IOException
RecordsFileException

insertRecord

public void insertRecord(RecordWriter rw)
                  throws RecordsFileException,
                         IOException
Adds the given record to the database.

Throws:
RecordsFileException
IOException

updateRecord

public void updateRecord(RecordWriter rw)
                  throws RecordsFileException,
                         IOException
Updates an existing record. If the new contents do not fit in the original record, then the update is handled by deleting the old record and adding the new.

Throws:
RecordsFileException
IOException

readRecord

public RecordReader readRecord(String key)
                        throws RecordsFileException,
                               IOException
Reads a record.

Throws:
RecordsFileException
IOException

readRecordData

protected byte[] readRecordData(String key)
                         throws IOException,
                                RecordsFileException
Reads the data for the record with the given key.

Throws:
IOException
RecordsFileException

readRecordData

protected byte[] readRecordData(RecordHeader header)
                         throws IOException
Reads the record data for the given record header.

Throws:
IOException

writeRecordData

protected void writeRecordData(RecordHeader header,
                               RecordWriter rw)
                        throws IOException,
                               RecordsFileException
Updates the contents of the given record. A RecordsFileException is thrown if the new data does not fit in the space allocated to the record. The header's data count is updated, but not written to the file.

Throws:
IOException
RecordsFileException

writeRecordData

protected void writeRecordData(RecordHeader header,
                               byte[] data)
                        throws IOException,
                               RecordsFileException
Updates the contents of the given record. A RecordsFileException is thrown if the new data does not fit in the space allocated to the record. The header's data count is updated, but not written to the file.

Throws:
IOException
RecordsFileException

deleteRecord

public void deleteRecord(String key)
                  throws RecordsFileException,
                         IOException
Deletes a record.

Throws:
RecordsFileException
IOException

insureIndexSpace

protected void insureIndexSpace(int requiredNumRecords)
                         throws RecordsFileException,
                                IOException
Throws:
RecordsFileException
IOException

close

public void close()
           throws IOException,
                  RecordsFileException
Closes the file.

Throws:
IOException
RecordsFileException


© Copyright 2005 S.Chekanov. All Rights Reserved.
jHPlot package, a part of jHepWork: is a full-featured multiplatform data-analysis framework