Environment for
DeveLoping
KDD-Applications
Supported by Index-Structures

de.lmu.ifi.dbs.elki.persistent
Class OnDiskArray

java.lang.Object
  extended by de.lmu.ifi.dbs.elki.persistent.OnDiskArray
All Implemented Interfaces:
Serializable

public class OnDiskArray
extends Object
implements Serializable

On Disc Array storage for records of a given size. This can be used to implement various fixed size record-based data structures. The file format is designed to have a fixed-size header followed by the actual data.

Author:
Erich Schubert
See Also:
Serialized Form

Field Summary
private  RandomAccessFile file
          Random Access File object.
private  File filename
          File name.
private static int HEADER_POS_SIZE
          Position of file size (in records)
private  int headersize
          Size of the header in the file.
private static int INTERNAL_HEADER_SIZE
          Size of the classes header size.
private  FileLock lock
          Lock for the file that will be kept while writing.
protected  int magic
          Magic number used to identify files
private  int numrecs
          Number of records in the file.
private  int recordsize
          Size of the records in the file.
private static long serialVersionUID
          Serial version.
private  boolean writable
          Writable or read-only object
 
Constructor Summary
OnDiskArray(File filename, int magicseed, int extraheadersize, int recordsize, boolean writable)
          Constructor to open an existing file.
OnDiskArray(File filename, int magicseed, int extraheadersize, int recordsize, int initialsize)
          Constructor to write a new file.
 
Method Summary
 void close()
          Explicitly close the file.
protected  int getExtraHeaderSize()
          Return the size of the extra header.
 File getFilename()
          Get the file name.
 int getNumRecords()
          Get number of records in file.
protected  int getRecordsize()
          Get the size of a single record.
private  long indexToFileposition(long index)
          Compute file position from index number
 boolean isWritable()
          Check if the file is writable.
static int mixMagic(int magic1, int magic2)
          Mix two magic numbers into one, to obtain a combined magic.
 byte[] readExtraHeader()
          Read the extra header data.
 byte[] readRecord(int index)
          Read a single record from the file.
 void resizeFile(int newsize)
          Resize file to the intended size
 void writeExtraHeader(byte[] buf)
          Write the extra header data.
 void writeRecord(int index, byte[] data)
          Write a single record.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Serial version. NOTE: Change this version whenever the file structure is changed in an incompatible way: This will modify the file magic, and thus prevent applications from reading incompatible files.

See Also:
Constant Field Values

magic

protected int magic
Magic number used to identify files


headersize

private int headersize
Size of the header in the file. Note that the internal header is four integers already.


recordsize

private int recordsize
Size of the records in the file.


numrecs

private int numrecs
Number of records in the file.


filename

private File filename
File name.


file

private RandomAccessFile file
Random Access File object.


lock

private FileLock lock
Lock for the file that will be kept while writing.


writable

private boolean writable
Writable or read-only object


INTERNAL_HEADER_SIZE

private static final int INTERNAL_HEADER_SIZE
Size of the classes header size.

See Also:
Constant Field Values

HEADER_POS_SIZE

private static final int HEADER_POS_SIZE
Position of file size (in records)

See Also:
Constant Field Values
Constructor Detail

OnDiskArray

public OnDiskArray(File filename,
                   int magicseed,
                   int extraheadersize,
                   int recordsize,
                   int initialsize)
            throws IOException
Constructor to write a new file.

Parameters:
filename - File name to be opened.
magicseed - Magic number to derive real magic from.
extraheadersize - header size NOT including the internal header
recordsize - Record size
initialsize - Initial file size (in records)
Throws:
IOException

OnDiskArray

public OnDiskArray(File filename,
                   int magicseed,
                   int extraheadersize,
                   int recordsize,
                   boolean writable)
            throws IOException
Constructor to open an existing file.

Parameters:
filename - File name to be opened.
magicseed - Magic number to derive real magic from.
extraheadersize - header size NOT including the internal header
recordsize - Record size
writable - flag to open the file writable
Throws:
IOException
Method Detail

mixMagic

public static final int mixMagic(int magic1,
                                 int magic2)
Mix two magic numbers into one, to obtain a combined magic. Note: mixMagic(a,b) != mixMagic(b,a) usually.

Parameters:
magic1 - Magic number to mix.
magic2 - Magic number to mix.
Returns:
Mixed magic number.

indexToFileposition

private long indexToFileposition(long index)
Compute file position from index number

Parameters:
index - Index offset
Returns:
file position

resizeFile

public void resizeFile(int newsize)
                throws IOException
Resize file to the intended size

Parameters:
newsize - New file size.
Throws:
IOException

readRecord

public byte[] readRecord(int index)
                  throws IOException
Read a single record from the file.

Parameters:
index - Record index
Returns:
Byte array with the records data.
Throws:
IOException

writeRecord

public void writeRecord(int index,
                        byte[] data)
                 throws IOException
Write a single record.

Parameters:
index - Record index.
data - Array with record data. MUST have appropriate size.
Throws:
IOException

getExtraHeaderSize

protected int getExtraHeaderSize()
Return the size of the extra header. Accessor.

Returns:
Extra header size

readExtraHeader

public byte[] readExtraHeader()
                       throws IOException
Read the extra header data.

Returns:
additional header data
Throws:
IOException

writeExtraHeader

public void writeExtraHeader(byte[] buf)
                      throws IOException
Write the extra header data.

Parameters:
buf - Header data.
Throws:
IOException

getRecordsize

protected int getRecordsize()
Get the size of a single record.

Returns:
Record size.

getFilename

public File getFilename()
Get the file name.

Returns:
File name

isWritable

public boolean isWritable()
Check if the file is writable.

Returns:
true if the file is writable.

close

public void close()
           throws IOException
Explicitly close the file. Note: following operations will likely cause IOExceptions.

Throws:
IOException

getNumRecords

public int getNumRecords()
Get number of records in file.

Returns:
Number of records in the file.

Release 0.2.1 (2009-07-13_1605)