com.partnersoft.io.formats.old
Class ObjectDatafile

java.lang.Object
  extended by com.partnersoft.io.formats.old.ObjectDatafile

public abstract class ObjectDatafile
extends java.lang.Object


Constructor Summary
ObjectDatafile()
           
ObjectDatafile(java.lang.String filename)
           
 
Method Summary
 void appendRecord(java.lang.Object record)
          Appends a record to the end of the file.
 void clear()
           
 void close()
           
 int compare(int a, int b)
          Compares the two indexed records.
 int compare(int index, java.lang.Object key)
          Compares the record to the given key.
protected  int compareImp(java.io.RandomAccessFile filet, long seekA, long seekB)
          Subclasses may override this to compare two records directly.
protected  int compareImp(java.io.RandomAccessFile filet, java.lang.Object key)
          Subclasses may override this to compare the seeked-to record to a key.
 int findClosest(java.lang.Object key)
          Returns the index of the "closest" record matching the given key (this would be the insertion point if you were to insert the given key) Uses binary search, so the file has to be sorted first.
protected  java.io.RandomAccessFile getFile()
          If you just have to bang on the file, use this.
protected  int getHeaderSize()
          Get your header size, in case you've forgotten already.
protected  int getRecordSize()
          Get your record size, in case you've forgotten already.
 int getSize()
          Returns size of data file, in records.
 void openRead()
           
 void openWrite()
           
 java.lang.Object readHeader()
          Get object from header.
protected  java.lang.Object readHeaderImp(java.io.RandomAccessFile filet)
          Override this if you want something returned from read header.
 java.lang.Object readKey(int index)
          Reads a key from the given slot.
protected  java.lang.Object readKeyImp(java.io.RandomAccessFile filet)
          Subclasses may override this to read the key for a record.
 java.lang.Object readRecord(int index)
          Reads a record from the given slot.
 void readRecord(int index, java.lang.Object recyclable)
          This might be faster - recycle your own object rather than having me create a new one for you.
protected abstract  void readRecordImp(java.lang.Object recycleMe, java.io.RandomAccessFile filet)
          Subclasses must override this to read an object from the fixed-length format, given an object to put the results into.
protected abstract  java.lang.Object readRecordImp(java.io.RandomAccessFile filet)
          Subclasses must override this to read an object from the fixed-length format.
 int search(java.lang.Object key)
          Returns the index of the record matching the given key; -1 if it can't find one.
protected  void setHeaderSize(int newSize)
          Use this to set your header size.
protected  void setRecordSize(int newSize)
          Use this to set your record size in bytes.
 void sort()
          Sorts the file using quicksort.
 void writeHeader(java.lang.Object newHeader)
          Write object to header.
protected  void writeHeaderImp(java.lang.Object header, java.io.RandomAccessFile filet)
          Override this if you want it to do anything.
 void writeRecord(int index, java.lang.Object rec)
          Writes a record into the given slot.
protected abstract  void writeRecordImp(java.lang.Object source, java.io.RandomAccessFile filet)
          Subclasses must override this to write out the object in the desired form.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectDatafile

public ObjectDatafile()

ObjectDatafile

public ObjectDatafile(java.lang.String filename)
Method Detail

writeRecordImp

protected abstract void writeRecordImp(java.lang.Object source,
                                       java.io.RandomAccessFile filet)
                                throws java.io.IOException
Subclasses must override this to write out the object in the desired form. The RandomAccessFile is advanced to the correct spot; just write away.

Throws:
java.io.IOException

readRecordImp

protected abstract java.lang.Object readRecordImp(java.io.RandomAccessFile filet)
                                           throws java.io.IOException
Subclasses must override this to read an object from the fixed-length format. The RandomAccessFile is advanced to the correct spot; just read away.

Throws:
java.io.IOException

readRecordImp

protected abstract void readRecordImp(java.lang.Object recycleMe,
                                      java.io.RandomAccessFile filet)
                               throws java.io.IOException
Subclasses must override this to read an object from the fixed-length format, given an object to put the results into. Quite often, for speed, you don't want to create objects, but reuse them instead. This allows you to do that.

The RandomAccessFile is advanced to the correct spot; just read away.

Throws:
java.io.IOException

readKeyImp

protected java.lang.Object readKeyImp(java.io.RandomAccessFile filet)
                               throws java.io.IOException
Subclasses may override this to read the key for a record. Again, you can assume the file is at the correct location. Default is to simply read the whole record.

Throws:
java.io.IOException

compareImp

protected int compareImp(java.io.RandomAccessFile filet,
                         long seekA,
                         long seekB)
                  throws java.io.IOException
Subclasses may override this to compare two records directly. This must be overridden if you plan on using the sorting or searching features. Pretty standard: -1 means a is less than b; 0 means they are equal; 1 means b is less than a. The parameters include the seek positions for both A and B. Default returns 0.

Throws:
java.io.IOException

compareImp

protected int compareImp(java.io.RandomAccessFile filet,
                         java.lang.Object key)
                  throws java.io.IOException
Subclasses may override this to compare the seeked-to record to a key. This must be overridden if you plan on using the sorting or searching features. Pretty standard: -1 means a is less than b; 0 means they are equal; 1 means b is less than a. Default is 0.

Throws:
java.io.IOException

setRecordSize

protected void setRecordSize(int newSize)
Use this to set your record size in bytes. Very important!!


getRecordSize

protected int getRecordSize()
Get your record size, in case you've forgotten already. Pay attention!


setHeaderSize

protected void setHeaderSize(int newSize)
Use this to set your header size. Defaults to 0.


getHeaderSize

protected int getHeaderSize()
Get your header size, in case you've forgotten already.


readHeaderImp

protected java.lang.Object readHeaderImp(java.io.RandomAccessFile filet)
                                  throws java.io.IOException
Override this if you want something returned from read header. File preset to byte 0 for your convenience.

Throws:
java.io.IOException

writeHeaderImp

protected void writeHeaderImp(java.lang.Object header,
                              java.io.RandomAccessFile filet)
                       throws java.io.IOException
Override this if you want it to do anything. Be sure to set your header size!

Throws:
java.io.IOException

getFile

protected java.io.RandomAccessFile getFile()
If you just have to bang on the file, use this. Be careful, dammit!


clear

public void clear()
           throws java.io.IOException
Throws:
java.io.IOException

openRead

public void openRead()
              throws java.io.IOException
Throws:
java.io.IOException

openWrite

public void openWrite()
               throws java.io.IOException
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Throws:
java.io.IOException

appendRecord

public void appendRecord(java.lang.Object record)
                  throws java.io.IOException
Appends a record to the end of the file.

Throws:
java.io.IOException

writeRecord

public void writeRecord(int index,
                        java.lang.Object rec)
                 throws java.io.IOException
Writes a record into the given slot. The index is the record count, not the byte offset.

Throws:
java.io.IOException

readRecord

public java.lang.Object readRecord(int index)
                            throws java.io.IOException
Reads a record from the given slot. The index is the record count, not the byte offset.

Throws:
java.io.IOException

readKey

public java.lang.Object readKey(int index)
                         throws java.io.IOException
Reads a key from the given slot. The index is the record count, not the byte offset.

Throws:
java.io.IOException

readRecord

public void readRecord(int index,
                       java.lang.Object recyclable)
                throws java.io.IOException
This might be faster - recycle your own object rather than having me create a new one for you. Save the bytes!

Throws:
java.io.IOException

getSize

public int getSize()
            throws java.io.IOException
Returns size of data file, in records.

Throws:
java.io.IOException

compare

public int compare(int a,
                   int b)
            throws java.io.IOException
Compares the two indexed records.

Throws:
java.io.IOException

compare

public int compare(int index,
                   java.lang.Object key)
            throws java.io.IOException
Compares the record to the given key.

Throws:
java.io.IOException

sort

public void sort()
          throws java.io.IOException
Sorts the file using quicksort.

Throws:
java.io.IOException

search

public int search(java.lang.Object key)
           throws java.io.IOException
Returns the index of the record matching the given key; -1 if it can't find one. Uses binary search, so the file has to be sorted first.

Throws:
java.io.IOException

findClosest

public int findClosest(java.lang.Object key)
Returns the index of the "closest" record matching the given key (this would be the insertion point if you were to insert the given key) Uses binary search, so the file has to be sorted first.


readHeader

public java.lang.Object readHeader()
                            throws java.io.IOException
Get object from header.

Throws:
java.io.IOException

writeHeader

public void writeHeader(java.lang.Object newHeader)
                 throws java.io.IOException
Write object to header.

Throws:
java.io.IOException